| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  *  max517.c - Support for Maxim MAX517, MAX518 and MAX519 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  This program is free software; you can redistribute it and/or modify | 
					
						
							|  |  |  |  *  it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  *  the Free Software Foundation; either version 2 of the License, or | 
					
						
							|  |  |  |  *  (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  *  but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  *  GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  *  You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |  *  along with this program; if not, write to the Free Software | 
					
						
							|  |  |  |  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <linux/module.h>
 | 
					
						
							|  |  |  | #include <linux/init.h>
 | 
					
						
							|  |  |  | #include <linux/slab.h>
 | 
					
						
							|  |  |  | #include <linux/jiffies.h>
 | 
					
						
							|  |  |  | #include <linux/i2c.h>
 | 
					
						
							|  |  |  | #include <linux/err.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-25 15:54:58 +01:00
										 |  |  | #include <linux/iio/iio.h>
 | 
					
						
							|  |  |  | #include <linux/iio/sysfs.h>
 | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:28 +02:00
										 |  |  | #include <linux/iio/dac/max517.h>
 | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define MAX517_DRV_NAME	"max517"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Commands */ | 
					
						
							|  |  |  | #define COMMAND_CHANNEL0	0x00
 | 
					
						
							|  |  |  | #define COMMAND_CHANNEL1	0x01 /* for MAX518 and MAX519 */
 | 
					
						
							|  |  |  | #define COMMAND_PD		0x08 /* Power Down */
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | enum max517_device_ids { | 
					
						
							|  |  |  | 	ID_MAX517, | 
					
						
							|  |  |  | 	ID_MAX518, | 
					
						
							|  |  |  | 	ID_MAX519, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | struct max517_data { | 
					
						
							| 
									
										
										
										
											2011-01-29 16:36:46 +01:00
										 |  |  | 	struct i2c_client	*client; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	unsigned short		vref_mv[2]; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * channel: bit 0: channel 1 | 
					
						
							|  |  |  |  *          bit 1: channel 2 | 
					
						
							|  |  |  |  * (this way, it's possible to set both channels at once) | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | static int max517_set_value(struct iio_dev *indio_dev, | 
					
						
							|  |  |  | 	long val, int channel) | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-06 17:14:38 +01:00
										 |  |  | 	struct max517_data *data = iio_priv(indio_dev); | 
					
						
							| 
									
										
										
										
											2011-01-29 16:36:46 +01:00
										 |  |  | 	struct i2c_client *client = data->client; | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	u8 outbuf[2]; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	int res; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (val < 0 || val > 255) | 
					
						
							|  |  |  | 		return -EINVAL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	outbuf[0] = channel; | 
					
						
							|  |  |  | 	outbuf[1] = val; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	res = i2c_master_send(client, outbuf, 2); | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	if (res < 0) | 
					
						
							|  |  |  | 		return res; | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	else if (res != 2) | 
					
						
							|  |  |  | 		return -EIO; | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		return 0; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | static int max517_read_raw(struct iio_dev *indio_dev, | 
					
						
							|  |  |  | 			   struct iio_chan_spec const *chan, | 
					
						
							|  |  |  | 			   int *val, | 
					
						
							|  |  |  | 			   int *val2, | 
					
						
							|  |  |  | 			   long m) | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2011-10-06 17:14:38 +01:00
										 |  |  | 	struct max517_data *data = iio_priv(indio_dev); | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	unsigned int scale_uv; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (m) { | 
					
						
							|  |  |  | 	case IIO_CHAN_INFO_SCALE: | 
					
						
							|  |  |  | 		/* Corresponds to Vref / 2^(bits) */ | 
					
						
							|  |  |  | 		scale_uv = (data->vref_mv[chan->channel] * 1000) >> 8; | 
					
						
							|  |  |  | 		*val =  scale_uv / 1000000; | 
					
						
							|  |  |  | 		*val2 = scale_uv % 1000000; | 
					
						
							|  |  |  | 		return IIO_VAL_INT_PLUS_MICRO; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return -EINVAL; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | static int max517_write_raw(struct iio_dev *indio_dev, | 
					
						
							|  |  |  | 	struct iio_chan_spec const *chan, int val, int val2, long mask) | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	int ret; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	switch (mask) { | 
					
						
							|  |  |  | 	case IIO_CHAN_INFO_RAW: | 
					
						
							|  |  |  | 		ret = max517_set_value(indio_dev, val, chan->channel); | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	default: | 
					
						
							|  |  |  | 		ret = -EINVAL; | 
					
						
							|  |  |  | 		break; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	return ret; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-20 19:37:05 +01:00
										 |  |  | #ifdef CONFIG_PM_SLEEP
 | 
					
						
							|  |  |  | static int max517_suspend(struct device *dev) | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	u8 outbuf = COMMAND_PD; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-20 19:37:05 +01:00
										 |  |  | 	return i2c_master_send(to_i2c_client(dev), &outbuf, 1); | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-20 19:37:05 +01:00
										 |  |  | static int max517_resume(struct device *dev) | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | { | 
					
						
							|  |  |  | 	u8 outbuf = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-20 19:37:05 +01:00
										 |  |  | 	return i2c_master_send(to_i2c_client(dev), &outbuf, 1); | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-02-20 19:37:05 +01:00
										 |  |  | static SIMPLE_DEV_PM_OPS(max517_pm_ops, max517_suspend, max517_resume); | 
					
						
							|  |  |  | #define MAX517_PM_OPS (&max517_pm_ops)
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #define MAX517_PM_OPS NULL
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-18 14:42:37 +01:00
										 |  |  | static const struct iio_info max517_info = { | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	.read_raw = max517_read_raw, | 
					
						
							|  |  |  | 	.write_raw = max517_write_raw, | 
					
						
							| 
									
										
										
										
											2011-05-18 14:42:37 +01:00
										 |  |  | 	.driver_module = THIS_MODULE, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | #define MAX517_CHANNEL(chan) {				\
 | 
					
						
							|  |  |  | 	.type = IIO_VOLTAGE,				\ | 
					
						
							|  |  |  | 	.indexed = 1,					\ | 
					
						
							|  |  |  | 	.output = 1,					\ | 
					
						
							|  |  |  | 	.channel = (chan),				\ | 
					
						
							|  |  |  | 	.info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT |	\ | 
					
						
							|  |  |  | 	IIO_CHAN_INFO_SCALE_SEPARATE_BIT,		\ | 
					
						
							|  |  |  | 	.scan_type = IIO_ST('u', 8, 8, 0),		\ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct iio_chan_spec max517_channels[] = { | 
					
						
							|  |  |  | 	MAX517_CHANNEL(0), | 
					
						
							|  |  |  | 	MAX517_CHANNEL(1) | 
					
						
							| 
									
										
										
										
											2011-05-18 14:42:37 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-21 13:21:43 -08:00
										 |  |  | static int max517_probe(struct i2c_client *client, | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 			const struct i2c_device_id *id) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	struct max517_data *data; | 
					
						
							| 
									
										
										
										
											2011-06-27 13:07:35 +01:00
										 |  |  | 	struct iio_dev *indio_dev; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	struct max517_platform_data *platform_data = client->dev.platform_data; | 
					
						
							|  |  |  | 	int err; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-04-26 13:35:01 +02:00
										 |  |  | 	indio_dev = iio_device_alloc(sizeof(*data)); | 
					
						
							| 
									
										
										
										
											2011-06-27 13:07:35 +01:00
										 |  |  | 	if (indio_dev == NULL) { | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 		err = -ENOMEM; | 
					
						
							|  |  |  | 		goto exit; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2011-06-27 13:07:35 +01:00
										 |  |  | 	data = iio_priv(indio_dev); | 
					
						
							|  |  |  | 	i2c_set_clientdata(client, indio_dev); | 
					
						
							| 
									
										
										
										
											2011-01-29 16:36:46 +01:00
										 |  |  | 	data->client = client; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	/* establish that the iio_dev is a child of the i2c device */ | 
					
						
							| 
									
										
										
										
											2011-06-27 13:07:35 +01:00
										 |  |  | 	indio_dev->dev.parent = &client->dev; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	/* reduced channel set for MAX517 */ | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	if (id->driver_data == ID_MAX517) | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 		indio_dev->num_channels = 1; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	else | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 		indio_dev->num_channels = 2; | 
					
						
							|  |  |  | 	indio_dev->channels = max517_channels; | 
					
						
							| 
									
										
										
										
											2011-06-27 13:07:35 +01:00
										 |  |  | 	indio_dev->modes = INDIO_DIRECT_MODE; | 
					
						
							| 
									
										
										
										
											2012-06-04 11:36:20 +02:00
										 |  |  | 	indio_dev->info = &max517_info; | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Reference voltage on MAX518 and default is 5V, else take vref_mv | 
					
						
							|  |  |  | 	 * from platform_data | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	if (id->driver_data == ID_MAX518 || !platform_data) { | 
					
						
							|  |  |  | 		data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */ | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		data->vref_mv[0] = platform_data->vref_mv[0]; | 
					
						
							|  |  |  | 		data->vref_mv[1] = platform_data->vref_mv[1]; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-06-27 13:07:35 +01:00
										 |  |  | 	err = iio_device_register(indio_dev); | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	if (err) | 
					
						
							|  |  |  | 		goto exit_free_device; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dev_info(&client->dev, "DAC registered\n"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | exit_free_device: | 
					
						
							| 
									
										
										
										
											2012-04-26 13:35:01 +02:00
										 |  |  | 	iio_device_free(indio_dev); | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | exit: | 
					
						
							|  |  |  | 	return err; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2012-12-21 13:21:43 -08:00
										 |  |  | static int max517_remove(struct i2c_client *client) | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2012-05-02 01:13:33 +02:00
										 |  |  | 	iio_device_unregister(i2c_get_clientdata(client)); | 
					
						
							| 
									
										
										
										
											2012-04-26 13:35:01 +02:00
										 |  |  | 	iio_device_free(i2c_get_clientdata(client)); | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return 0; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static const struct i2c_device_id max517_id[] = { | 
					
						
							|  |  |  | 	{ "max517", ID_MAX517 }, | 
					
						
							|  |  |  | 	{ "max518", ID_MAX518 }, | 
					
						
							|  |  |  | 	{ "max519", ID_MAX519 }, | 
					
						
							|  |  |  | 	{ } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | MODULE_DEVICE_TABLE(i2c, max517_id); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static struct i2c_driver max517_driver = { | 
					
						
							|  |  |  | 	.driver = { | 
					
						
							|  |  |  | 		.name	= MAX517_DRV_NAME, | 
					
						
							| 
									
										
										
										
											2012-02-20 19:37:05 +01:00
										 |  |  | 		.pm		= MAX517_PM_OPS, | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	}, | 
					
						
							|  |  |  | 	.probe		= max517_probe, | 
					
						
							| 
									
										
										
										
											2012-12-21 13:21:43 -08:00
										 |  |  | 	.remove		= max517_remove, | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 	.id_table	= max517_id, | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2011-11-16 10:13:38 +01:00
										 |  |  | module_i2c_driver(max517_driver); | 
					
						
							| 
									
										
										
										
											2011-01-12 11:41:59 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>"); | 
					
						
							|  |  |  | MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC"); | 
					
						
							|  |  |  | MODULE_LICENSE("GPL"); |