Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.git] / drivers / staging / iio / adc / ad7152.c
1 /*
2  * AD7152 capacitive sensor driver supporting AD7152/3
3  *
4  * Copyright 2010 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/sysfs.h>
14 #include <linux/i2c.h>
15
16 #include "../iio.h"
17 #include "../sysfs.h"
18
19 /*
20  * AD7152 registers definition
21  */
22
23 #define AD7152_STATUS              0
24 #define AD7152_STATUS_RDY1         (1 << 0)
25 #define AD7152_STATUS_RDY2         (1 << 1)
26 #define AD7152_CH1_DATA_HIGH       1
27 #define AD7152_CH1_DATA_LOW        2
28 #define AD7152_CH2_DATA_HIGH       3
29 #define AD7152_CH2_DATA_LOW        4
30 #define AD7152_CH1_OFFS_HIGH       5
31 #define AD7152_CH1_OFFS_LOW        6
32 #define AD7152_CH2_OFFS_HIGH       7
33 #define AD7152_CH2_OFFS_LOW        8
34 #define AD7152_CH1_GAIN_HIGH       9
35 #define AD7152_CH1_GAIN_LOW        10
36 #define AD7152_CH1_SETUP           11
37 #define AD7152_CH2_GAIN_HIGH       12
38 #define AD7152_CH2_GAIN_LOW        13
39 #define AD7152_CH2_SETUP           14
40 #define AD7152_CFG                 15
41 #define AD7152_RESEVERD            16
42 #define AD7152_CAPDAC_POS          17
43 #define AD7152_CAPDAC_NEG          18
44 #define AD7152_CFG2                26
45
46 #define AD7152_MAX_CONV_MODE       6
47
48 /*
49  * struct ad7152_chip_info - chip specifc information
50  */
51
52 struct ad7152_chip_info {
53         struct i2c_client *client;
54         u16 ch1_offset;     /* Channel 1 offset calibration coefficient */
55         u16 ch1_gain;       /* Channel 1 gain coefficient */
56         u8  ch1_setup;
57         u16 ch2_offset;     /* Channel 2 offset calibration coefficient */
58         u16 ch2_gain;       /* Channel 1 gain coefficient */
59         u8  ch2_setup;
60         u8  filter_rate_setup; /* Capacitive channel digital filter setup; conversion time/update rate setup per channel */
61         char *conversion_mode;
62 };
63
64 struct ad7152_conversion_mode {
65         char *name;
66         u8 reg_cfg;
67 };
68
69 static struct ad7152_conversion_mode
70 ad7152_conv_mode_table[AD7152_MAX_CONV_MODE] = {
71         { "idle", 0 },
72         { "continuous-conversion", 1 },
73         { "single-conversion", 2 },
74         { "power-down", 3 },
75         { "offset-calibration", 5 },
76         { "gain-calibration", 6 },
77 };
78
79 /*
80  * ad7152 register access by I2C
81  */
82
83 static int ad7152_i2c_read(struct ad7152_chip_info *chip, u8 reg, u8 *data, int len)
84 {
85         struct i2c_client *client = chip->client;
86         int ret;
87
88         ret = i2c_master_send(client, &reg, 1);
89         if (ret < 0) {
90                 dev_err(&client->dev, "I2C write error\n");
91                 return ret;
92         }
93
94         ret = i2c_master_recv(client, data, len);
95         if (ret < 0) {
96                 dev_err(&client->dev, "I2C read error\n");
97         }
98
99         return ret;
100 }
101
102 static int ad7152_i2c_write(struct ad7152_chip_info *chip, u8 reg, u8 data)
103 {
104         struct i2c_client *client = chip->client;
105         int ret;
106
107         u8 tx[2] = {
108                 reg,
109                 data,
110         };
111
112         ret = i2c_master_send(client, tx, 2);
113         if (ret < 0)
114                 dev_err(&client->dev, "I2C write error\n");
115
116         return ret;
117 }
118
119 /*
120  * sysfs nodes
121  */
122
123 #define IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(_show)                              \
124         IIO_DEVICE_ATTR(available_conversion_modes, S_IRUGO, _show, NULL, 0)
125 #define IIO_DEV_ATTR_CONVERSION_MODE(_mode, _show, _store)              \
126         IIO_DEVICE_ATTR(conversion_mode, _mode, _show, _store, 0)
127 #define IIO_DEV_ATTR_CH1_OFFSET(_mode, _show, _store)           \
128         IIO_DEVICE_ATTR(ch1_offset, _mode, _show, _store, 0)
129 #define IIO_DEV_ATTR_CH2_OFFSET(_mode, _show, _store)           \
130         IIO_DEVICE_ATTR(ch2_offset, _mode, _show, _store, 0)
131 #define IIO_DEV_ATTR_CH1_GAIN(_mode, _show, _store)             \
132         IIO_DEVICE_ATTR(ch1_gain, _mode, _show, _store, 0)
133 #define IIO_DEV_ATTR_CH2_GAIN(_mode, _show, _store)             \
134         IIO_DEVICE_ATTR(ch2_gain, _mode, _show, _store, 0)
135 #define IIO_DEV_ATTR_CH1_VALUE(_show)           \
136         IIO_DEVICE_ATTR(ch1_value, S_IRUGO, _show, NULL, 0)
137 #define IIO_DEV_ATTR_CH2_VALUE(_show)           \
138         IIO_DEVICE_ATTR(ch2_value, S_IRUGO, _show, NULL, 0)
139 #define IIO_DEV_ATTR_CH1_SETUP(_mode, _show, _store)            \
140         IIO_DEVICE_ATTR(ch1_setup, _mode, _show, _store, 0)
141 #define IIO_DEV_ATTR_CH2_SETUP(_mode, _show, _store)              \
142         IIO_DEVICE_ATTR(ch2_setup, _mode, _show, _store, 0)
143 #define IIO_DEV_ATTR_FILTER_RATE_SETUP(_mode, _show, _store)              \
144         IIO_DEVICE_ATTR(filter_rate_setup, _mode, _show, _store, 0)
145
146 static ssize_t ad7152_show_conversion_modes(struct device *dev,
147                 struct device_attribute *attr,
148                 char *buf)
149 {
150         int i;
151         int len = 0;
152
153         for (i = 0; i < AD7152_MAX_CONV_MODE; i++)
154                 len += sprintf(buf + len, "%s ", ad7152_conv_mode_table[i].name);
155
156         len += sprintf(buf + len, "\n");
157
158         return len;
159 }
160
161 static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad7152_show_conversion_modes);
162
163 static ssize_t ad7152_show_ch1_value(struct device *dev,
164                 struct device_attribute *attr,
165                 char *buf)
166 {
167         struct iio_dev *dev_info = dev_get_drvdata(dev);
168         struct ad7152_chip_info *chip = iio_priv(dev_info);
169         u8 data[2];
170
171         ad7152_i2c_read(chip, AD7152_CH1_DATA_HIGH, data, 2);
172         return sprintf(buf, "%d\n", ((int)data[0] << 8) | data[1]);
173 }
174
175 static IIO_DEV_ATTR_CH1_VALUE(ad7152_show_ch1_value);
176
177 static ssize_t ad7152_show_ch2_value(struct device *dev,
178                 struct device_attribute *attr,
179                 char *buf)
180 {
181         struct iio_dev *dev_info = dev_get_drvdata(dev);
182         struct ad7152_chip_info *chip = iio_priv(dev_info);
183         u8 data[2];
184
185         ad7152_i2c_read(chip, AD7152_CH2_DATA_HIGH, data, 2);
186         return sprintf(buf, "%d\n", ((int)data[0] << 8) | data[1]);
187 }
188
189 static IIO_DEV_ATTR_CH2_VALUE(ad7152_show_ch2_value);
190
191 static ssize_t ad7152_show_conversion_mode(struct device *dev,
192                 struct device_attribute *attr,
193                 char *buf)
194 {
195         struct iio_dev *dev_info = dev_get_drvdata(dev);
196         struct ad7152_chip_info *chip = iio_priv(dev_info);
197
198         return sprintf(buf, "%s\n", chip->conversion_mode);
199 }
200
201 static ssize_t ad7152_store_conversion_mode(struct device *dev,
202                 struct device_attribute *attr,
203                 const char *buf,
204                 size_t len)
205 {
206         struct iio_dev *dev_info = dev_get_drvdata(dev);
207         struct ad7152_chip_info *chip = iio_priv(dev_info);
208         u8 cfg;
209         int i;
210
211         ad7152_i2c_read(chip, AD7152_CFG, &cfg, 1);
212
213         for (i = 0; i < AD7152_MAX_CONV_MODE; i++)
214                 if (strncmp(buf, ad7152_conv_mode_table[i].name,
215                                 strlen(ad7152_conv_mode_table[i].name) - 1) == 0) {
216                         chip->conversion_mode = ad7152_conv_mode_table[i].name;
217                         cfg |= 0x18 | ad7152_conv_mode_table[i].reg_cfg;
218                         ad7152_i2c_write(chip, AD7152_CFG, cfg);
219                         return len;
220                 }
221
222         dev_err(dev, "not supported conversion mode\n");
223
224         return -EINVAL;
225 }
226
227 static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR,
228                 ad7152_show_conversion_mode,
229                 ad7152_store_conversion_mode);
230
231 static ssize_t ad7152_show_ch1_offset(struct device *dev,
232                 struct device_attribute *attr,
233                 char *buf)
234 {
235         struct iio_dev *dev_info = dev_get_drvdata(dev);
236         struct ad7152_chip_info *chip = iio_priv(dev_info);
237
238         return sprintf(buf, "%d\n", chip->ch1_offset);
239 }
240
241 static ssize_t ad7152_store_ch1_offset(struct device *dev,
242                 struct device_attribute *attr,
243                 const char *buf,
244                 size_t len)
245 {
246         struct iio_dev *dev_info = dev_get_drvdata(dev);
247         struct ad7152_chip_info *chip = iio_priv(dev_info);
248         unsigned long data;
249         int ret;
250
251         ret = strict_strtoul(buf, 10, &data);
252
253         if ((!ret) && (data < 0x10000)) {
254                 ad7152_i2c_write(chip, AD7152_CH1_OFFS_HIGH, data >> 8);
255                 ad7152_i2c_write(chip, AD7152_CH1_OFFS_LOW, data);
256                 chip->ch1_offset = data;
257                 return len;
258         }
259
260         return -EINVAL;
261 }
262
263 static IIO_DEV_ATTR_CH1_OFFSET(S_IRUGO | S_IWUSR,
264                 ad7152_show_ch1_offset,
265                 ad7152_store_ch1_offset);
266
267 static ssize_t ad7152_show_ch2_offset(struct device *dev,
268                 struct device_attribute *attr,
269                 char *buf)
270 {
271         struct iio_dev *dev_info = dev_get_drvdata(dev);
272         struct ad7152_chip_info *chip = iio_priv(dev_info);
273
274         return sprintf(buf, "%d\n", chip->ch2_offset);
275 }
276
277 static ssize_t ad7152_store_ch2_offset(struct device *dev,
278                 struct device_attribute *attr,
279                 const char *buf,
280                 size_t len)
281 {
282         struct iio_dev *dev_info = dev_get_drvdata(dev);
283         struct ad7152_chip_info *chip = iio_priv(dev_info);
284         unsigned long data;
285         int ret;
286
287         ret = strict_strtoul(buf, 10, &data);
288
289         if ((!ret) && (data < 0x10000)) {
290                 ad7152_i2c_write(chip, AD7152_CH2_OFFS_HIGH, data >> 8);
291                 ad7152_i2c_write(chip, AD7152_CH2_OFFS_LOW, data);
292                 chip->ch2_offset = data;
293                 return len;
294         }
295
296         return -EINVAL;
297 }
298
299 static IIO_DEV_ATTR_CH2_OFFSET(S_IRUGO | S_IWUSR,
300                 ad7152_show_ch2_offset,
301                 ad7152_store_ch2_offset);
302
303 static ssize_t ad7152_show_ch1_gain(struct device *dev,
304                 struct device_attribute *attr,
305                 char *buf)
306 {
307         struct iio_dev *dev_info = dev_get_drvdata(dev);
308         struct ad7152_chip_info *chip = iio_priv(dev_info);
309
310         return sprintf(buf, "%d\n", chip->ch1_gain);
311 }
312
313 static ssize_t ad7152_store_ch1_gain(struct device *dev,
314                 struct device_attribute *attr,
315                 const char *buf,
316                 size_t len)
317 {
318         struct iio_dev *dev_info = dev_get_drvdata(dev);
319         struct ad7152_chip_info *chip = iio_priv(dev_info);
320         unsigned long data;
321         int ret;
322
323         ret = strict_strtoul(buf, 10, &data);
324
325         if ((!ret) && (data < 0x10000)) {
326                 ad7152_i2c_write(chip, AD7152_CH1_GAIN_HIGH, data >> 8);
327                 ad7152_i2c_write(chip, AD7152_CH1_GAIN_LOW, data);
328                 chip->ch1_gain = data;
329                 return len;
330         }
331
332         return -EINVAL;
333 }
334
335 static IIO_DEV_ATTR_CH1_GAIN(S_IRUGO | S_IWUSR,
336                 ad7152_show_ch1_gain,
337                 ad7152_store_ch1_gain);
338
339 static ssize_t ad7152_show_ch2_gain(struct device *dev,
340                 struct device_attribute *attr,
341                 char *buf)
342 {
343         struct iio_dev *dev_info = dev_get_drvdata(dev);
344         struct ad7152_chip_info *chip = iio_priv(dev_info);
345
346         return sprintf(buf, "%d\n", chip->ch2_gain);
347 }
348
349 static ssize_t ad7152_store_ch2_gain(struct device *dev,
350                 struct device_attribute *attr,
351                 const char *buf,
352                 size_t len)
353 {
354         struct iio_dev *dev_info = dev_get_drvdata(dev);
355         struct ad7152_chip_info *chip = iio_priv(dev_info);
356         unsigned long data;
357         int ret;
358
359         ret = strict_strtoul(buf, 10, &data);
360
361         if ((!ret) && (data < 0x10000)) {
362                 ad7152_i2c_write(chip, AD7152_CH2_GAIN_HIGH, data >> 8);
363                 ad7152_i2c_write(chip, AD7152_CH2_GAIN_LOW, data);
364                 chip->ch2_gain = data;
365                 return len;
366         }
367
368         return -EINVAL;
369 }
370
371 static IIO_DEV_ATTR_CH2_GAIN(S_IRUGO | S_IWUSR,
372                 ad7152_show_ch2_gain,
373                 ad7152_store_ch2_gain);
374
375 static ssize_t ad7152_show_ch1_setup(struct device *dev,
376                 struct device_attribute *attr,
377                 char *buf)
378 {
379         struct iio_dev *dev_info = dev_get_drvdata(dev);
380         struct ad7152_chip_info *chip = iio_priv(dev_info);
381
382         return sprintf(buf, "0x%02x\n", chip->ch1_setup);
383 }
384
385 static ssize_t ad7152_store_ch1_setup(struct device *dev,
386                 struct device_attribute *attr,
387                 const char *buf,
388                 size_t len)
389 {
390         struct iio_dev *dev_info = dev_get_drvdata(dev);
391         struct ad7152_chip_info *chip = iio_priv(dev_info);
392         unsigned long data;
393         int ret;
394
395         ret = strict_strtoul(buf, 10, &data);
396
397         if ((!ret) && (data < 0x100)) {
398                 ad7152_i2c_write(chip, AD7152_CH1_SETUP, data);
399                 chip->ch1_setup = data;
400                 return len;
401         }
402
403         return -EINVAL;
404 }
405
406 static IIO_DEV_ATTR_CH1_SETUP(S_IRUGO | S_IWUSR,
407                 ad7152_show_ch1_setup,
408                 ad7152_store_ch1_setup);
409
410 static ssize_t ad7152_show_ch2_setup(struct device *dev,
411                 struct device_attribute *attr,
412                 char *buf)
413 {
414         struct iio_dev *dev_info = dev_get_drvdata(dev);
415         struct ad7152_chip_info *chip = iio_priv(dev_info);
416
417         return sprintf(buf, "0x%02x\n", chip->ch2_setup);
418 }
419
420 static ssize_t ad7152_store_ch2_setup(struct device *dev,
421                 struct device_attribute *attr,
422                 const char *buf,
423                 size_t len)
424 {
425         struct iio_dev *dev_info = dev_get_drvdata(dev);
426         struct ad7152_chip_info *chip = iio_priv(dev_info);
427         unsigned long data;
428         int ret;
429
430         ret = strict_strtoul(buf, 10, &data);
431
432         if ((!ret) && (data < 0x100)) {
433                 ad7152_i2c_write(chip, AD7152_CH2_SETUP, data);
434                 chip->ch2_setup = data;
435                 return len;
436         }
437
438         return -EINVAL;
439 }
440
441 static IIO_DEV_ATTR_CH2_SETUP(S_IRUGO | S_IWUSR,
442                 ad7152_show_ch2_setup,
443                 ad7152_store_ch2_setup);
444
445 static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
446                 struct device_attribute *attr,
447                 char *buf)
448 {
449         struct iio_dev *dev_info = dev_get_drvdata(dev);
450         struct ad7152_chip_info *chip = iio_priv(dev_info);
451
452         return sprintf(buf, "0x%02x\n", chip->filter_rate_setup);
453 }
454
455 static ssize_t ad7152_store_filter_rate_setup(struct device *dev,
456                 struct device_attribute *attr,
457                 const char *buf,
458                 size_t len)
459 {
460         struct iio_dev *dev_info = dev_get_drvdata(dev);
461         struct ad7152_chip_info *chip = iio_priv(dev_info);
462         unsigned long data;
463         int ret;
464
465         ret = strict_strtoul(buf, 10, &data);
466
467         if ((!ret) && (data < 0x100)) {
468                 ad7152_i2c_write(chip, AD7152_CFG2, data);
469                 chip->filter_rate_setup = data;
470                 return len;
471         }
472
473         return -EINVAL;
474 }
475
476 static IIO_DEV_ATTR_FILTER_RATE_SETUP(S_IRUGO | S_IWUSR,
477                 ad7152_show_filter_rate_setup,
478                 ad7152_store_filter_rate_setup);
479
480 static struct attribute *ad7152_attributes[] = {
481         &iio_dev_attr_available_conversion_modes.dev_attr.attr,
482         &iio_dev_attr_conversion_mode.dev_attr.attr,
483         &iio_dev_attr_ch1_gain.dev_attr.attr,
484         &iio_dev_attr_ch2_gain.dev_attr.attr,
485         &iio_dev_attr_ch1_offset.dev_attr.attr,
486         &iio_dev_attr_ch2_offset.dev_attr.attr,
487         &iio_dev_attr_ch1_value.dev_attr.attr,
488         &iio_dev_attr_ch2_value.dev_attr.attr,
489         &iio_dev_attr_ch1_setup.dev_attr.attr,
490         &iio_dev_attr_ch2_setup.dev_attr.attr,
491         &iio_dev_attr_filter_rate_setup.dev_attr.attr,
492         NULL,
493 };
494
495 static const struct attribute_group ad7152_attribute_group = {
496         .attrs = ad7152_attributes,
497 };
498
499 static const struct iio_info ad7152_info = {
500         .attrs = &ad7152_attribute_group,
501         .driver_module = THIS_MODULE,
502 };
503 /*
504  * device probe and remove
505  */
506
507 static int __devinit ad7152_probe(struct i2c_client *client,
508                 const struct i2c_device_id *id)
509 {
510         int ret = 0;
511         struct ad7152_chip_info *chip;
512         struct iio_dev *indio_dev;
513
514         indio_dev = iio_allocate_device(sizeof(*chip));
515         if (indio_dev == NULL) {
516                 ret = -ENOMEM;
517                 goto error_ret;
518         }
519         chip = iio_priv(indio_dev);
520         /* this is only used for device removal purposes */
521         i2c_set_clientdata(client, indio_dev);
522
523         chip->client = client;
524
525         /* Echipabilish that the iio_dev is a child of the i2c device */
526         indio_dev->name = id->name;
527         indio_dev->dev.parent = &client->dev;
528         indio_dev->info = &ad7152_info;
529         indio_dev->modes = INDIO_DIRECT_MODE;
530
531         ret = iio_device_register(indio_dev);
532         if (ret)
533                 goto error_free_dev;
534
535         dev_err(&client->dev, "%s capacitive sensor registered\n", id->name);
536
537         return 0;
538
539 error_free_dev:
540         iio_free_device(indio_dev);
541 error_ret:
542         return ret;
543 }
544
545 static int __devexit ad7152_remove(struct i2c_client *client)
546 {
547         struct iio_dev *indio_dev = i2c_get_clientdata(client);
548
549         iio_device_unregister(indio_dev);
550
551         return 0;
552 }
553
554 static const struct i2c_device_id ad7152_id[] = {
555         { "ad7152", 0 },
556         { "ad7153", 0 },
557         {}
558 };
559
560 MODULE_DEVICE_TABLE(i2c, ad7152_id);
561
562 static struct i2c_driver ad7152_driver = {
563         .driver = {
564                 .name = "ad7152",
565         },
566         .probe = ad7152_probe,
567         .remove = __devexit_p(ad7152_remove),
568         .id_table = ad7152_id,
569 };
570
571 static __init int ad7152_init(void)
572 {
573         return i2c_add_driver(&ad7152_driver);
574 }
575
576 static __exit void ad7152_exit(void)
577 {
578         i2c_del_driver(&ad7152_driver);
579 }
580
581 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
582 MODULE_DESCRIPTION("Analog Devices ad7152/3 capacitive sensor driver");
583 MODULE_LICENSE("GPL v2");
584
585 module_init(ad7152_init);
586 module_exit(ad7152_exit);