Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / adc / ad7152.c
blob21f5f380fb5e70a2682502e43e8f373503361089
1 /*
2 * AD7152 capacitive sensor driver supporting AD7152/3
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
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>
16 #include "../iio.h"
17 #include "../sysfs.h"
20 * AD7152 registers definition
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
46 #define AD7152_MAX_CONV_MODE 6
49 * struct ad7152_chip_info - chip specifc information
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;
64 struct ad7152_conversion_mode {
65 char *name;
66 u8 reg_cfg;
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 },
80 * ad7152 register access by I2C
83 static int ad7152_i2c_read(struct ad7152_chip_info *chip, u8 reg, u8 *data, int len)
85 struct i2c_client *client = chip->client;
86 int ret;
88 ret = i2c_master_send(client, &reg, 1);
89 if (ret < 0) {
90 dev_err(&client->dev, "I2C write error\n");
91 return ret;
94 ret = i2c_master_recv(client, data, len);
95 if (ret < 0) {
96 dev_err(&client->dev, "I2C read error\n");
99 return ret;
102 static int ad7152_i2c_write(struct ad7152_chip_info *chip, u8 reg, u8 data)
104 struct i2c_client *client = chip->client;
105 int ret;
107 u8 tx[2] = {
108 reg,
109 data,
112 ret = i2c_master_send(client, tx, 2);
113 if (ret < 0)
114 dev_err(&client->dev, "I2C write error\n");
116 return ret;
120 * sysfs nodes
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)
146 static ssize_t ad7152_show_conversion_modes(struct device *dev,
147 struct device_attribute *attr,
148 char *buf)
150 int i;
151 int len = 0;
153 for (i = 0; i < AD7152_MAX_CONV_MODE; i++)
154 len += sprintf(buf + len, "%s ", ad7152_conv_mode_table[i].name);
156 len += sprintf(buf + len, "\n");
158 return len;
161 static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad7152_show_conversion_modes);
163 static ssize_t ad7152_show_ch1_value(struct device *dev,
164 struct device_attribute *attr,
165 char *buf)
167 struct iio_dev *dev_info = dev_get_drvdata(dev);
168 struct ad7152_chip_info *chip = iio_priv(dev_info);
169 u8 data[2];
171 ad7152_i2c_read(chip, AD7152_CH1_DATA_HIGH, data, 2);
172 return sprintf(buf, "%d\n", ((int)data[0] << 8) | data[1]);
175 static IIO_DEV_ATTR_CH1_VALUE(ad7152_show_ch1_value);
177 static ssize_t ad7152_show_ch2_value(struct device *dev,
178 struct device_attribute *attr,
179 char *buf)
181 struct iio_dev *dev_info = dev_get_drvdata(dev);
182 struct ad7152_chip_info *chip = iio_priv(dev_info);
183 u8 data[2];
185 ad7152_i2c_read(chip, AD7152_CH2_DATA_HIGH, data, 2);
186 return sprintf(buf, "%d\n", ((int)data[0] << 8) | data[1]);
189 static IIO_DEV_ATTR_CH2_VALUE(ad7152_show_ch2_value);
191 static ssize_t ad7152_show_conversion_mode(struct device *dev,
192 struct device_attribute *attr,
193 char *buf)
195 struct iio_dev *dev_info = dev_get_drvdata(dev);
196 struct ad7152_chip_info *chip = iio_priv(dev_info);
198 return sprintf(buf, "%s\n", chip->conversion_mode);
201 static ssize_t ad7152_store_conversion_mode(struct device *dev,
202 struct device_attribute *attr,
203 const char *buf,
204 size_t len)
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;
211 ad7152_i2c_read(chip, AD7152_CFG, &cfg, 1);
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;
222 dev_err(dev, "not supported conversion mode\n");
224 return -EINVAL;
227 static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR,
228 ad7152_show_conversion_mode,
229 ad7152_store_conversion_mode);
231 static ssize_t ad7152_show_ch1_offset(struct device *dev,
232 struct device_attribute *attr,
233 char *buf)
235 struct iio_dev *dev_info = dev_get_drvdata(dev);
236 struct ad7152_chip_info *chip = iio_priv(dev_info);
238 return sprintf(buf, "%d\n", chip->ch1_offset);
241 static ssize_t ad7152_store_ch1_offset(struct device *dev,
242 struct device_attribute *attr,
243 const char *buf,
244 size_t len)
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;
251 ret = strict_strtoul(buf, 10, &data);
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;
260 return -EINVAL;
263 static IIO_DEV_ATTR_CH1_OFFSET(S_IRUGO | S_IWUSR,
264 ad7152_show_ch1_offset,
265 ad7152_store_ch1_offset);
267 static ssize_t ad7152_show_ch2_offset(struct device *dev,
268 struct device_attribute *attr,
269 char *buf)
271 struct iio_dev *dev_info = dev_get_drvdata(dev);
272 struct ad7152_chip_info *chip = iio_priv(dev_info);
274 return sprintf(buf, "%d\n", chip->ch2_offset);
277 static ssize_t ad7152_store_ch2_offset(struct device *dev,
278 struct device_attribute *attr,
279 const char *buf,
280 size_t len)
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;
287 ret = strict_strtoul(buf, 10, &data);
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;
296 return -EINVAL;
299 static IIO_DEV_ATTR_CH2_OFFSET(S_IRUGO | S_IWUSR,
300 ad7152_show_ch2_offset,
301 ad7152_store_ch2_offset);
303 static ssize_t ad7152_show_ch1_gain(struct device *dev,
304 struct device_attribute *attr,
305 char *buf)
307 struct iio_dev *dev_info = dev_get_drvdata(dev);
308 struct ad7152_chip_info *chip = iio_priv(dev_info);
310 return sprintf(buf, "%d\n", chip->ch1_gain);
313 static ssize_t ad7152_store_ch1_gain(struct device *dev,
314 struct device_attribute *attr,
315 const char *buf,
316 size_t len)
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;
323 ret = strict_strtoul(buf, 10, &data);
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;
332 return -EINVAL;
335 static IIO_DEV_ATTR_CH1_GAIN(S_IRUGO | S_IWUSR,
336 ad7152_show_ch1_gain,
337 ad7152_store_ch1_gain);
339 static ssize_t ad7152_show_ch2_gain(struct device *dev,
340 struct device_attribute *attr,
341 char *buf)
343 struct iio_dev *dev_info = dev_get_drvdata(dev);
344 struct ad7152_chip_info *chip = iio_priv(dev_info);
346 return sprintf(buf, "%d\n", chip->ch2_gain);
349 static ssize_t ad7152_store_ch2_gain(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf,
352 size_t len)
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;
359 ret = strict_strtoul(buf, 10, &data);
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;
368 return -EINVAL;
371 static IIO_DEV_ATTR_CH2_GAIN(S_IRUGO | S_IWUSR,
372 ad7152_show_ch2_gain,
373 ad7152_store_ch2_gain);
375 static ssize_t ad7152_show_ch1_setup(struct device *dev,
376 struct device_attribute *attr,
377 char *buf)
379 struct iio_dev *dev_info = dev_get_drvdata(dev);
380 struct ad7152_chip_info *chip = iio_priv(dev_info);
382 return sprintf(buf, "0x%02x\n", chip->ch1_setup);
385 static ssize_t ad7152_store_ch1_setup(struct device *dev,
386 struct device_attribute *attr,
387 const char *buf,
388 size_t len)
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;
395 ret = strict_strtoul(buf, 10, &data);
397 if ((!ret) && (data < 0x100)) {
398 ad7152_i2c_write(chip, AD7152_CH1_SETUP, data);
399 chip->ch1_setup = data;
400 return len;
403 return -EINVAL;
406 static IIO_DEV_ATTR_CH1_SETUP(S_IRUGO | S_IWUSR,
407 ad7152_show_ch1_setup,
408 ad7152_store_ch1_setup);
410 static ssize_t ad7152_show_ch2_setup(struct device *dev,
411 struct device_attribute *attr,
412 char *buf)
414 struct iio_dev *dev_info = dev_get_drvdata(dev);
415 struct ad7152_chip_info *chip = iio_priv(dev_info);
417 return sprintf(buf, "0x%02x\n", chip->ch2_setup);
420 static ssize_t ad7152_store_ch2_setup(struct device *dev,
421 struct device_attribute *attr,
422 const char *buf,
423 size_t len)
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;
430 ret = strict_strtoul(buf, 10, &data);
432 if ((!ret) && (data < 0x100)) {
433 ad7152_i2c_write(chip, AD7152_CH2_SETUP, data);
434 chip->ch2_setup = data;
435 return len;
438 return -EINVAL;
441 static IIO_DEV_ATTR_CH2_SETUP(S_IRUGO | S_IWUSR,
442 ad7152_show_ch2_setup,
443 ad7152_store_ch2_setup);
445 static ssize_t ad7152_show_filter_rate_setup(struct device *dev,
446 struct device_attribute *attr,
447 char *buf)
449 struct iio_dev *dev_info = dev_get_drvdata(dev);
450 struct ad7152_chip_info *chip = iio_priv(dev_info);
452 return sprintf(buf, "0x%02x\n", chip->filter_rate_setup);
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)
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;
465 ret = strict_strtoul(buf, 10, &data);
467 if ((!ret) && (data < 0x100)) {
468 ad7152_i2c_write(chip, AD7152_CFG2, data);
469 chip->filter_rate_setup = data;
470 return len;
473 return -EINVAL;
476 static IIO_DEV_ATTR_FILTER_RATE_SETUP(S_IRUGO | S_IWUSR,
477 ad7152_show_filter_rate_setup,
478 ad7152_store_filter_rate_setup);
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,
495 static const struct attribute_group ad7152_attribute_group = {
496 .attrs = ad7152_attributes,
499 static const struct iio_info ad7152_info = {
500 .attrs = &ad7152_attribute_group,
501 .driver_module = THIS_MODULE,
504 * device probe and remove
507 static int __devinit ad7152_probe(struct i2c_client *client,
508 const struct i2c_device_id *id)
510 int ret = 0;
511 struct ad7152_chip_info *chip;
512 struct iio_dev *indio_dev;
514 indio_dev = iio_allocate_device(sizeof(*chip));
515 if (indio_dev == NULL) {
516 ret = -ENOMEM;
517 goto error_ret;
519 chip = iio_priv(indio_dev);
520 /* this is only used for device removal purposes */
521 i2c_set_clientdata(client, indio_dev);
523 chip->client = client;
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;
531 ret = iio_device_register(indio_dev);
532 if (ret)
533 goto error_free_dev;
535 dev_err(&client->dev, "%s capacitive sensor registered\n", id->name);
537 return 0;
539 error_free_dev:
540 iio_free_device(indio_dev);
541 error_ret:
542 return ret;
545 static int __devexit ad7152_remove(struct i2c_client *client)
547 struct iio_dev *indio_dev = i2c_get_clientdata(client);
549 iio_device_unregister(indio_dev);
551 return 0;
554 static const struct i2c_device_id ad7152_id[] = {
555 { "ad7152", 0 },
556 { "ad7153", 0 },
560 MODULE_DEVICE_TABLE(i2c, ad7152_id);
562 static struct i2c_driver ad7152_driver = {
563 .driver = {
564 .name = "ad7152",
566 .probe = ad7152_probe,
567 .remove = __devexit_p(ad7152_remove),
568 .id_table = ad7152_id,
571 static __init int ad7152_init(void)
573 return i2c_add_driver(&ad7152_driver);
576 static __exit void ad7152_exit(void)
578 i2c_del_driver(&ad7152_driver);
581 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
582 MODULE_DESCRIPTION("Analog Devices ad7152/3 capacitive sensor driver");
583 MODULE_LICENSE("GPL v2");
585 module_init(ad7152_init);
586 module_exit(ad7152_exit);