staging: iio: max517: Fix iio_info changes
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / iio / dac / max517.c
blob2fe34d21b6aa2268658d4c6148ad07fa11087747
1 /*
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>
28 #include "../iio.h"
29 #include "dac.h"
31 #include "max517.h"
33 #define MAX517_DRV_NAME "max517"
35 /* Commands */
36 #define COMMAND_CHANNEL0 0x00
37 #define COMMAND_CHANNEL1 0x01 /* for MAX518 and MAX519 */
38 #define COMMAND_PD 0x08 /* Power Down */
40 enum max517_device_ids {
41 ID_MAX517,
42 ID_MAX518,
43 ID_MAX519,
46 struct max517_data {
47 struct iio_dev *indio_dev;
48 struct i2c_client *client;
49 unsigned short vref_mv[2];
53 * channel: bit 0: channel 1
54 * bit 1: channel 2
55 * (this way, it's possible to set both channels at once)
57 static ssize_t max517_set_value(struct device *dev,
58 struct device_attribute *attr,
59 const char *buf, size_t count, int channel)
61 struct iio_dev *dev_info = dev_get_drvdata(dev);
62 struct max517_data *data = iio_dev_get_devdata(dev_info);
63 struct i2c_client *client = data->client;
64 u8 outbuf[4]; /* 1x or 2x command + value */
65 int outbuf_size = 0;
66 int res;
67 long val;
69 res = strict_strtol(buf, 10, &val);
71 if (res)
72 return res;
74 if (val < 0 || val > 255)
75 return -EINVAL;
77 if (channel & 1) {
78 outbuf[outbuf_size++] = COMMAND_CHANNEL0;
79 outbuf[outbuf_size++] = val;
81 if (channel & 2) {
82 outbuf[outbuf_size++] = COMMAND_CHANNEL1;
83 outbuf[outbuf_size++] = val;
87 * At this point, there are always 1 or 2 two-byte commands in
88 * outbuf. With 2 commands, the device can set two outputs
89 * simultaneously, latching the values upon the end of the I2C
90 * transfer.
93 res = i2c_master_send(client, outbuf, outbuf_size);
94 if (res < 0)
95 return res;
97 return count;
100 static ssize_t max517_set_value_1(struct device *dev,
101 struct device_attribute *attr,
102 const char *buf, size_t count)
104 return max517_set_value(dev, attr, buf, count, 1);
106 static IIO_DEV_ATTR_OUT_RAW(1, max517_set_value_1, 0);
108 static ssize_t max517_set_value_2(struct device *dev,
109 struct device_attribute *attr,
110 const char *buf, size_t count)
112 return max517_set_value(dev, attr, buf, count, 2);
114 static IIO_DEV_ATTR_OUT_RAW(2, max517_set_value_2, 1);
116 static ssize_t max517_set_value_both(struct device *dev,
117 struct device_attribute *attr,
118 const char *buf, size_t count)
120 return max517_set_value(dev, attr, buf, count, 3);
122 static IIO_DEVICE_ATTR_NAMED(out1and2_raw, out1&2_raw, S_IWUSR, NULL,
123 max517_set_value_both, -1);
125 static ssize_t max517_show_scale(struct device *dev,
126 struct device_attribute *attr,
127 char *buf, int channel)
129 struct iio_dev *dev_info = dev_get_drvdata(dev);
130 struct max517_data *data = iio_dev_get_devdata(dev_info);
131 /* Corresponds to Vref / 2^(bits) */
132 unsigned int scale_uv = (data->vref_mv[channel - 1] * 1000) >> 8;
134 return sprintf(buf, "%d.%03d\n", scale_uv / 1000, scale_uv % 1000);
137 static ssize_t max517_show_scale1(struct device *dev,
138 struct device_attribute *attr,
139 char *buf)
141 return max517_show_scale(dev, attr, buf, 1);
143 static IIO_DEVICE_ATTR(out1_scale, S_IRUGO, max517_show_scale1, NULL, 0);
145 static ssize_t max517_show_scale2(struct device *dev,
146 struct device_attribute *attr,
147 char *buf)
149 return max517_show_scale(dev, attr, buf, 2);
151 static IIO_DEVICE_ATTR(out2_scale, S_IRUGO, max517_show_scale2, NULL, 0);
153 /* On MAX517 variant, we have one output */
154 static struct attribute *max517_attributes[] = {
155 &iio_dev_attr_out1_raw.dev_attr.attr,
156 &iio_dev_attr_out1_scale.dev_attr.attr,
157 NULL
160 static struct attribute_group max517_attribute_group = {
161 .attrs = max517_attributes,
164 /* On MAX518 and MAX519 variant, we have two outputs */
165 static struct attribute *max518_attributes[] = {
166 &iio_dev_attr_out1_raw.dev_attr.attr,
167 &iio_dev_attr_out1_scale.dev_attr.attr,
168 &iio_dev_attr_out2_raw.dev_attr.attr,
169 &iio_dev_attr_out2_scale.dev_attr.attr,
170 &iio_dev_attr_out1and2_raw.dev_attr.attr,
171 NULL
174 static struct attribute_group max518_attribute_group = {
175 .attrs = max518_attributes,
178 static int max517_suspend(struct i2c_client *client, pm_message_t mesg)
180 u8 outbuf = COMMAND_PD;
182 return i2c_master_send(client, &outbuf, 1);
185 static int max517_resume(struct i2c_client *client)
187 u8 outbuf = 0;
189 return i2c_master_send(client, &outbuf, 1);
192 static const struct iio_info max517_info = {
193 .attrs = &max517_attribute_group,
194 .driver_module = THIS_MODULE,
197 static const struct iio_info max518_info = {
198 .attrs = &max518_attribute_group,
199 .driver_module = THIS_MODULE,
202 static int max517_probe(struct i2c_client *client,
203 const struct i2c_device_id *id)
205 struct max517_data *data;
206 struct max517_platform_data *platform_data = client->dev.platform_data;
207 int err;
209 data = kzalloc(sizeof(struct max517_data), GFP_KERNEL);
210 if (!data) {
211 err = -ENOMEM;
212 goto exit;
215 i2c_set_clientdata(client, data);
217 data->client = client;
219 data->indio_dev = iio_allocate_device(0);
220 if (data->indio_dev == NULL) {
221 err = -ENOMEM;
222 goto exit_free_data;
225 /* establish that the iio_dev is a child of the i2c device */
226 data->indio_dev->dev.parent = &client->dev;
228 /* reduced attribute set for MAX517 */
229 if (id->driver_data == ID_MAX517)
230 data->indio_dev->info = &max517_info;
231 else
232 data->indio_dev->info = &max518_info;
233 data->indio_dev->dev_data = (void *)(data);
234 data->indio_dev->modes = INDIO_DIRECT_MODE;
237 * Reference voltage on MAX518 and default is 5V, else take vref_mv
238 * from platform_data
240 if (id->driver_data == ID_MAX518 || !platform_data) {
241 data->vref_mv[0] = data->vref_mv[1] = 5000; /* mV */
242 } else {
243 data->vref_mv[0] = platform_data->vref_mv[0];
244 data->vref_mv[1] = platform_data->vref_mv[1];
247 err = iio_device_register(data->indio_dev);
248 if (err)
249 goto exit_free_device;
251 dev_info(&client->dev, "DAC registered\n");
253 return 0;
255 exit_free_device:
256 iio_free_device(data->indio_dev);
257 exit_free_data:
258 kfree(data);
259 exit:
260 return err;
263 static int max517_remove(struct i2c_client *client)
265 struct max517_data *data = i2c_get_clientdata(client);
267 iio_free_device(data->indio_dev);
268 kfree(data);
270 return 0;
273 static const struct i2c_device_id max517_id[] = {
274 { "max517", ID_MAX517 },
275 { "max518", ID_MAX518 },
276 { "max519", ID_MAX519 },
279 MODULE_DEVICE_TABLE(i2c, max517_id);
281 static struct i2c_driver max517_driver = {
282 .driver = {
283 .name = MAX517_DRV_NAME,
285 .probe = max517_probe,
286 .remove = max517_remove,
287 .suspend = max517_suspend,
288 .resume = max517_resume,
289 .id_table = max517_id,
292 static int __init max517_init(void)
294 return i2c_add_driver(&max517_driver);
297 static void __exit max517_exit(void)
299 i2c_del_driver(&max517_driver);
302 MODULE_AUTHOR("Roland Stigge <stigge@antcom.de>");
303 MODULE_DESCRIPTION("MAX517/MAX518/MAX519 8-bit DAC");
304 MODULE_LICENSE("GPL");
306 module_init(max517_init);
307 module_exit(max517_exit);