2 * max517.c - Support for Maxim MAX517, MAX518 and MAX519
4 * Copyright (C) 2010, 2011 Roland Stigge <stigge@antcom.de>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/jiffies.h>
25 #include <linux/i2c.h>
26 #include <linux/err.h>
34 #define MAX517_DRV_NAME "max517"
37 #define COMMAND_CHANNEL0 0x00
38 #define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
39 #define COMMAND_PD 0x08 /* Power Down */
41 enum max517_device_ids {
48 struct iio_dev *indio_dev;
49 struct i2c_client *client;
50 unsigned short vref_mv[2];
54 * channel: bit 0: channel 1
56 * (this way, it's possible to set both channels at once)
58 static ssize_t max517_set_value(struct device *dev,
59 struct device_attribute *attr,
60 const char *buf, size_t count, int channel)
62 struct iio_dev *dev_info = dev_get_drvdata(dev);
63 struct max517_data *data = iio_priv(dev_info);
64 struct i2c_client *client = data->client;
65 u8 outbuf[4]; /* 1x or 2x command + value */
70 res = strict_strtol(buf, 10, &val);
75 if (val < 0 || val > 255)
79 outbuf[outbuf_size++] = COMMAND_CHANNEL0;
80 outbuf[outbuf_size++] = val;
83 outbuf[outbuf_size++] = COMMAND_CHANNEL1;
84 outbuf[outbuf_size++] = val;
88 * At this point, there are always 1 or 2 two-byte commands in
89 * outbuf. With 2 commands, the device can set two outputs
90 * simultaneously, latching the values upon the end of the I2C
94 res = i2c_master_send(client, outbuf, outbuf_size);
101 static ssize_t max517_set_value_1(struct device *dev,
102 struct device_attribute *attr,
103 const char *buf, size_t count)
105 return max517_set_value(dev, attr, buf, count, 1);
107 static IIO_DEV_ATTR_OUT_RAW(1, max517_set_value_1, 0);
109 static ssize_t max517_set_value_2(struct device *dev,
110 struct device_attribute *attr,
111 const char *buf, size_t count)
113 return max517_set_value(dev, attr, buf, count, 2);
115 static IIO_DEV_ATTR_OUT_RAW(2, max517_set_value_2, 1);
117 static ssize_t max517_set_value_both(struct device *dev,
118 struct device_attribute *attr,
119 const char *buf, size_t count)
121 return max517_set_value(dev, attr, buf, count, 3);
123 static IIO_DEVICE_ATTR_NAMED(out1and2_raw, out1&2_raw, S_IWUSR, NULL,
124 max517_set_value_both, -1);
126 static ssize_t max517_show_scale(struct device *dev,
127 struct device_attribute *attr,
128 char *buf, int channel)
130 struct iio_dev *dev_info = dev_get_drvdata(dev);
131 struct max517_data *data = iio_priv(dev_info);
132 /* Corresponds to Vref / 2^(bits) */
133 unsigned int scale_uv = (data->vref_mv[channel - 1] * 1000) >> 8;
135 return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
138 static ssize_t max517_show_scale1(struct device *dev,
139 struct device_attribute *attr,
142 return max517_show_scale(dev, attr, buf, 1);
144 static IIO_DEVICE_ATTR(out1_scale, S_IRUGO, max517_show_scale1, NULL, 0);
146 static ssize_t max517_show_scale2(struct device *dev,
147 struct device_attribute *attr,
150 return max517_show_scale(dev, attr, buf, 2);
152 static IIO_DEVICE_ATTR(out2_scale, S_IRUGO, max517_show_scale2, NULL, 0);
154 /* On MAX517 variant, we have one output */
155 static struct attribute *max517_attributes[] = {
156 &iio_dev_attr_out1_raw.dev_attr.attr,
157 &iio_dev_attr_out1_scale.dev_attr.attr,
161 static struct attribute_group max517_attribute_group = {
162 .attrs = max517_attributes,
165 /* On MAX518 and MAX519 variant, we have two outputs */
166 static struct attribute *max518_attributes[] = {
167 &iio_dev_attr_out1_raw.dev_attr.attr,
168 &iio_dev_attr_out1_scale.dev_attr.attr,
169 &iio_dev_attr_out2_raw.dev_attr.attr,
170 &iio_dev_attr_out2_scale.dev_attr.attr,
171 &iio_dev_attr_out1and2_raw.dev_attr.attr,
175 static struct attribute_group max518_attribute_group = {
176 .attrs = max518_attributes,
179 static int max517_suspend(struct i2c_client *client, pm_message_t mesg)
181 u8 outbuf = COMMAND_PD;
183 return i2c_master_send(client, &outbuf, 1);
186 static int max517_resume(struct i2c_client *client)
190 return i2c_master_send(client, &outbuf, 1);
193 static const struct iio_info max517_info = {
194 .attrs = &max517_attribute_group,
195 .driver_module = THIS_MODULE,
198 static const struct iio_info max518_info = {
199 .attrs = &max518_attribute_group,
200 .driver_module = THIS_MODULE,
203 static int max517_probe(struct i2c_client *client,
204 const struct i2c_device_id *id)
206 struct max517_data *data;
207 struct iio_dev *indio_dev;
208 struct max517_platform_data *platform_data = client->dev.platform_data;
211 indio_dev = iio_allocate_device(sizeof(*data));
212 if (indio_dev == NULL) {
216 data = iio_priv(indio_dev);
217 i2c_set_clientdata(client, indio_dev);
218 data->client = client;
220 /* establish that the iio_dev is a child of the i2c device */
221 indio_dev->dev.parent = &client->dev;
223 /* reduced attribute set for MAX517 */
224 if (id->driver_data == ID_MAX517)
225 indio_dev->info = &max517_info;
227 indio_dev->info = &max518_info;
228 indio_dev->modes = INDIO_DIRECT_MODE;
231 * Reference voltage on MAX518 and default is 5V, else take vref_mv
234 if (id->driver_data == ID_MAX518 || !platform_data) {
235 data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
237 data->vref_mv[0] = platform_data->vref_mv[0];
238 data->vref_mv[1] = platform_data->vref_mv[1];
241 err = iio_device_register(indio_dev);
243 goto exit_free_device;
245 dev_info(&client->dev, "DAC registered\n");
250 iio_free_device(indio_dev);
255 static int max517_remove(struct i2c_client *client)
257 iio_free_device(i2c_get_clientdata(client));
262 static const struct i2c_device_id max517_id[] = {
263 { "max517", ID_MAX517 },
264 { "max518", ID_MAX518 },
265 { "max519", ID_MAX519 },
268 MODULE_DEVICE_TABLE(i2c, max517_id);
270 static struct i2c_driver max517_driver = {
272 .name = MAX517_DRV_NAME,
274 .probe = max517_probe,
275 .remove = max517_remove,
276 .suspend = max517_suspend,
277 .resume = max517_resume,
278 .id_table = max517_id,
281 static int __init max517_init(void)
283 return i2c_add_driver(&max517_driver);
286 static void __exit max517_exit(void)
288 i2c_del_driver(&max517_driver);
291 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
292 MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
293 MODULE_LICENSE("GPL");
295 module_init(max517_init);
296 module_exit(max517_exit);