Merge branch 'upstream' of git://git.linux-mips.org/pub/scm/upstream-linus
[linux-btrfs-devel.git] / drivers / staging / iio / adc / ad7745.c
blob4c13f26aa9ae3c5c7c8ba263309fa9d077815929
1 /*
2 * AD774X capacitive sensor driver supporting AD7745/6/7
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
7 */
9 #include <linux/interrupt.h>
10 #include <linux/gpio.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/sysfs.h>
15 #include <linux/list.h>
16 #include <linux/i2c.h>
18 #include "../iio.h"
19 #include "../sysfs.h"
22 * AD774X registers definition
25 #define AD774X_STATUS 0
26 #define AD774X_STATUS_RDY (1 << 2)
27 #define AD774X_STATUS_RDYVT (1 << 1)
28 #define AD774X_STATUS_RDYCAP (1 << 0)
29 #define AD774X_CAP_DATA_HIGH 1
30 #define AD774X_CAP_DATA_MID 2
31 #define AD774X_CAP_DATA_LOW 3
32 #define AD774X_VT_DATA_HIGH 4
33 #define AD774X_VT_DATA_MID 5
34 #define AD774X_VT_DATA_LOW 6
35 #define AD774X_CAP_SETUP 7
36 #define AD774X_VT_SETUP 8
37 #define AD774X_EXEC_SETUP 9
38 #define AD774X_CFG 10
39 #define AD774X_CAPDACA 11
40 #define AD774X_CAPDACB 12
41 #define AD774X_CAPDAC_EN (1 << 7)
42 #define AD774X_CAP_OFFH 13
43 #define AD774X_CAP_OFFL 14
44 #define AD774X_CAP_GAINH 15
45 #define AD774X_CAP_GAINL 16
46 #define AD774X_VOLT_GAINH 17
47 #define AD774X_VOLT_GAINL 18
49 #define AD774X_MAX_CONV_MODE 6
52 * struct ad774x_chip_info - chip specifc information
55 struct ad774x_chip_info {
56 struct i2c_client *client;
57 bool inter;
58 u16 cap_offs; /* Capacitive offset */
59 u16 cap_gain; /* Capacitive gain calibration */
60 u16 volt_gain; /* Voltage gain calibration */
61 u8 cap_setup;
62 u8 vt_setup;
63 u8 exec_setup;
65 char *conversion_mode;
68 struct ad774x_conversion_mode {
69 char *name;
70 u8 reg_cfg;
73 static struct ad774x_conversion_mode
74 ad774x_conv_mode_table[AD774X_MAX_CONV_MODE] = {
75 { "idle", 0 },
76 { "continuous-conversion", 1 },
77 { "single-conversion", 2 },
78 { "power-down", 3 },
79 { "offset-calibration", 5 },
80 { "gain-calibration", 6 },
84 * ad774x register access by I2C
87 static int ad774x_i2c_read(struct ad774x_chip_info *chip, u8 reg, u8 *data, int len)
89 struct i2c_client *client = chip->client;
90 int ret;
92 ret = i2c_master_send(client, &reg, 1);
93 if (ret < 0) {
94 dev_err(&client->dev, "I2C write error\n");
95 return ret;
98 ret = i2c_master_recv(client, data, len);
99 if (ret < 0) {
100 dev_err(&client->dev, "I2C read error\n");
101 return ret;
104 return ret;
107 static int ad774x_i2c_write(struct ad774x_chip_info *chip, u8 reg, u8 data)
109 struct i2c_client *client = chip->client;
110 int ret;
112 u8 tx[2] = {
113 reg,
114 data,
117 ret = i2c_master_send(client, tx, 2);
118 if (ret < 0)
119 dev_err(&client->dev, "I2C write error\n");
121 return ret;
125 * sysfs nodes
128 #define IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(_show) \
129 IIO_DEVICE_ATTR(available_conversion_modes, S_IRUGO, _show, NULL, 0)
130 #define IIO_DEV_ATTR_CONVERSION_MODE(_mode, _show, _store) \
131 IIO_DEVICE_ATTR(conversion_mode, _mode, _show, _store, 0)
132 #define IIO_DEV_ATTR_CAP_SETUP(_mode, _show, _store) \
133 IIO_DEVICE_ATTR(cap_setup, _mode, _show, _store, 0)
134 #define IIO_DEV_ATTR_VT_SETUP(_mode, _show, _store) \
135 IIO_DEVICE_ATTR(in0_setup, _mode, _show, _store, 0)
136 #define IIO_DEV_ATTR_EXEC_SETUP(_mode, _show, _store) \
137 IIO_DEVICE_ATTR(exec_setup, _mode, _show, _store, 0)
138 #define IIO_DEV_ATTR_VOLT_GAIN(_mode, _show, _store) \
139 IIO_DEVICE_ATTR(in0_gain, _mode, _show, _store, 0)
140 #define IIO_DEV_ATTR_CAP_OFFS(_mode, _show, _store) \
141 IIO_DEVICE_ATTR(cap_offs, _mode, _show, _store, 0)
142 #define IIO_DEV_ATTR_CAP_GAIN(_mode, _show, _store) \
143 IIO_DEVICE_ATTR(cap_gain, _mode, _show, _store, 0)
144 #define IIO_DEV_ATTR_CAP_DATA(_show) \
145 IIO_DEVICE_ATTR(cap0_raw, S_IRUGO, _show, NULL, 0)
146 #define IIO_DEV_ATTR_VT_DATA(_show) \
147 IIO_DEVICE_ATTR(in0_raw, S_IRUGO, _show, NULL, 0)
149 static ssize_t ad774x_show_conversion_modes(struct device *dev,
150 struct device_attribute *attr,
151 char *buf)
153 int i;
154 int len = 0;
156 for (i = 0; i < AD774X_MAX_CONV_MODE; i++)
157 len += sprintf(buf + len, "%s ", ad774x_conv_mode_table[i].name);
159 len += sprintf(buf + len, "\n");
161 return len;
164 static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad774x_show_conversion_modes);
166 static ssize_t ad774x_show_conversion_mode(struct device *dev,
167 struct device_attribute *attr,
168 char *buf)
170 struct iio_dev *dev_info = dev_get_drvdata(dev);
171 struct ad774x_chip_info *chip = iio_priv(dev_info);
173 return sprintf(buf, "%s\n", chip->conversion_mode);
176 static ssize_t ad774x_store_conversion_mode(struct device *dev,
177 struct device_attribute *attr,
178 const char *buf,
179 size_t len)
181 struct iio_dev *dev_info = dev_get_drvdata(dev);
182 struct ad774x_chip_info *chip = iio_priv(dev_info);
183 u8 cfg;
184 int i;
186 ad774x_i2c_read(chip, AD774X_CFG, &cfg, 1);
188 for (i = 0; i < AD774X_MAX_CONV_MODE; i++) {
189 if (strncmp(buf, ad774x_conv_mode_table[i].name,
190 strlen(ad774x_conv_mode_table[i].name) - 1) == 0) {
191 chip->conversion_mode = ad774x_conv_mode_table[i].name;
192 cfg |= 0x18 | ad774x_conv_mode_table[i].reg_cfg;
193 ad774x_i2c_write(chip, AD774X_CFG, cfg);
194 return len;
198 dev_err(dev, "not supported conversion mode\n");
200 return -EINVAL;
203 static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR,
204 ad774x_show_conversion_mode,
205 ad774x_store_conversion_mode);
207 static ssize_t ad774x_show_dac_value(struct device *dev,
208 struct device_attribute *attr,
209 char *buf)
211 struct iio_dev *dev_info = dev_get_drvdata(dev);
212 struct ad774x_chip_info *chip = iio_priv(dev_info);
213 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
214 u8 data;
216 ad774x_i2c_read(chip, this_attr->address, &data, 1);
218 return sprintf(buf, "%02x\n", data & 0x7F);
221 static ssize_t ad774x_store_dac_value(struct device *dev,
222 struct device_attribute *attr,
223 const char *buf,
224 size_t len)
226 struct iio_dev *dev_info = dev_get_drvdata(dev);
227 struct ad774x_chip_info *chip = iio_priv(dev_info);
228 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
229 unsigned long data;
230 int ret;
232 ret = strict_strtoul(buf, 10, &data);
234 if (!ret) {
235 ad774x_i2c_write(chip, this_attr->address,
236 (data ? AD774X_CAPDAC_EN : 0) | (data & 0x7F));
237 return len;
240 return -EINVAL;
243 static IIO_DEVICE_ATTR(capdac0_raw, S_IRUGO | S_IWUSR,
244 ad774x_show_dac_value,
245 ad774x_store_dac_value,
246 AD774X_CAPDACA);
248 static IIO_DEVICE_ATTR(capdac1_raw, S_IRUGO | S_IWUSR,
249 ad774x_show_dac_value,
250 ad774x_store_dac_value,
251 AD774X_CAPDACB);
253 static ssize_t ad774x_show_cap_setup(struct device *dev,
254 struct device_attribute *attr,
255 char *buf)
257 struct iio_dev *dev_info = dev_get_drvdata(dev);
258 struct ad774x_chip_info *chip = iio_priv(dev_info);
260 return sprintf(buf, "0x%02x\n", chip->cap_setup);
263 static ssize_t ad774x_store_cap_setup(struct device *dev,
264 struct device_attribute *attr,
265 const char *buf,
266 size_t len)
268 struct iio_dev *dev_info = dev_get_drvdata(dev);
269 struct ad774x_chip_info *chip = iio_priv(dev_info);
270 unsigned long data;
271 int ret;
273 ret = strict_strtoul(buf, 10, &data);
275 if ((!ret) && (data < 0x100)) {
276 ad774x_i2c_write(chip, AD774X_CAP_SETUP, data);
277 chip->cap_setup = data;
278 return len;
281 return -EINVAL;
284 static IIO_DEV_ATTR_CAP_SETUP(S_IRUGO | S_IWUSR,
285 ad774x_show_cap_setup,
286 ad774x_store_cap_setup);
288 static ssize_t ad774x_show_vt_setup(struct device *dev,
289 struct device_attribute *attr,
290 char *buf)
292 struct iio_dev *dev_info = dev_get_drvdata(dev);
293 struct ad774x_chip_info *chip = iio_priv(dev_info);
295 return sprintf(buf, "0x%02x\n", chip->vt_setup);
298 static ssize_t ad774x_store_vt_setup(struct device *dev,
299 struct device_attribute *attr,
300 const char *buf,
301 size_t len)
303 struct iio_dev *dev_info = dev_get_drvdata(dev);
304 struct ad774x_chip_info *chip = iio_priv(dev_info);
305 unsigned long data;
306 int ret;
308 ret = strict_strtoul(buf, 10, &data);
310 if ((!ret) && (data < 0x100)) {
311 ad774x_i2c_write(chip, AD774X_VT_SETUP, data);
312 chip->vt_setup = data;
313 return len;
316 return -EINVAL;
319 static IIO_DEV_ATTR_VT_SETUP(S_IRUGO | S_IWUSR,
320 ad774x_show_vt_setup,
321 ad774x_store_vt_setup);
323 static ssize_t ad774x_show_exec_setup(struct device *dev,
324 struct device_attribute *attr,
325 char *buf)
327 struct iio_dev *dev_info = dev_get_drvdata(dev);
328 struct ad774x_chip_info *chip = iio_priv(dev_info);
330 return sprintf(buf, "0x%02x\n", chip->exec_setup);
333 static ssize_t ad774x_store_exec_setup(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf,
336 size_t len)
338 struct iio_dev *dev_info = dev_get_drvdata(dev);
339 struct ad774x_chip_info *chip = iio_priv(dev_info);
340 unsigned long data;
341 int ret;
343 ret = strict_strtoul(buf, 10, &data);
345 if ((!ret) && (data < 0x100)) {
346 ad774x_i2c_write(chip, AD774X_EXEC_SETUP, data);
347 chip->exec_setup = data;
348 return len;
351 return -EINVAL;
354 static IIO_DEV_ATTR_EXEC_SETUP(S_IRUGO | S_IWUSR,
355 ad774x_show_exec_setup,
356 ad774x_store_exec_setup);
358 static ssize_t ad774x_show_volt_gain(struct device *dev,
359 struct device_attribute *attr,
360 char *buf)
362 struct iio_dev *dev_info = dev_get_drvdata(dev);
363 struct ad774x_chip_info *chip = iio_priv(dev_info);
365 return sprintf(buf, "%d\n", chip->volt_gain);
368 static ssize_t ad774x_store_volt_gain(struct device *dev,
369 struct device_attribute *attr,
370 const char *buf,
371 size_t len)
373 struct iio_dev *dev_info = dev_get_drvdata(dev);
374 struct ad774x_chip_info *chip = iio_priv(dev_info);
375 unsigned long data;
376 int ret;
378 ret = strict_strtoul(buf, 10, &data);
380 if ((!ret) && (data < 0x10000)) {
381 ad774x_i2c_write(chip, AD774X_VOLT_GAINH, data >> 8);
382 ad774x_i2c_write(chip, AD774X_VOLT_GAINL, data);
383 chip->volt_gain = data;
384 return len;
387 return -EINVAL;
390 static IIO_DEV_ATTR_VOLT_GAIN(S_IRUGO | S_IWUSR,
391 ad774x_show_volt_gain,
392 ad774x_store_volt_gain);
394 static ssize_t ad774x_show_cap_data(struct device *dev,
395 struct device_attribute *attr,
396 char *buf)
398 struct iio_dev *dev_info = dev_get_drvdata(dev);
399 struct ad774x_chip_info *chip = iio_priv(dev_info);
400 unsigned long data;
401 char tmp[3];
403 ad774x_i2c_read(chip, AD774X_CAP_DATA_HIGH, tmp, 3);
404 data = ((int)tmp[0] << 16) | ((int)tmp[1] << 8) | (int)tmp[2];
406 return sprintf(buf, "%ld\n", data);
409 static IIO_DEV_ATTR_CAP_DATA(ad774x_show_cap_data);
411 static ssize_t ad774x_show_vt_data(struct device *dev,
412 struct device_attribute *attr,
413 char *buf)
415 struct iio_dev *dev_info = dev_get_drvdata(dev);
416 struct ad774x_chip_info *chip = iio_priv(dev_info);
417 unsigned long data;
418 char tmp[3];
420 ad774x_i2c_read(chip, AD774X_VT_DATA_HIGH, tmp, 3);
421 data = ((int)tmp[0] << 16) | ((int)tmp[1] << 8) | (int)tmp[2];
423 return sprintf(buf, "%ld\n", data);
426 static IIO_DEV_ATTR_VT_DATA(ad774x_show_vt_data);
428 static ssize_t ad774x_show_cap_offs(struct device *dev,
429 struct device_attribute *attr,
430 char *buf)
432 struct iio_dev *dev_info = dev_get_drvdata(dev);
433 struct ad774x_chip_info *chip = iio_priv(dev_info);
435 return sprintf(buf, "%d\n", chip->cap_offs);
438 static ssize_t ad774x_store_cap_offs(struct device *dev,
439 struct device_attribute *attr,
440 const char *buf,
441 size_t len)
443 struct iio_dev *dev_info = dev_get_drvdata(dev);
444 struct ad774x_chip_info *chip = iio_priv(dev_info);
445 unsigned long data;
446 int ret;
448 ret = strict_strtoul(buf, 10, &data);
450 if ((!ret) && (data < 0x10000)) {
451 ad774x_i2c_write(chip, AD774X_CAP_OFFH, data >> 8);
452 ad774x_i2c_write(chip, AD774X_CAP_OFFL, data);
453 chip->cap_offs = data;
454 return len;
457 return -EINVAL;
460 static IIO_DEV_ATTR_CAP_OFFS(S_IRUGO | S_IWUSR,
461 ad774x_show_cap_offs,
462 ad774x_store_cap_offs);
464 static ssize_t ad774x_show_cap_gain(struct device *dev,
465 struct device_attribute *attr,
466 char *buf)
468 struct iio_dev *dev_info = dev_get_drvdata(dev);
469 struct ad774x_chip_info *chip = iio_priv(dev_info);
471 return sprintf(buf, "%d\n", chip->cap_gain);
474 static ssize_t ad774x_store_cap_gain(struct device *dev,
475 struct device_attribute *attr,
476 const char *buf,
477 size_t len)
479 struct iio_dev *dev_info = dev_get_drvdata(dev);
480 struct ad774x_chip_info *chip = iio_priv(dev_info);
481 unsigned long data;
482 int ret;
484 ret = strict_strtoul(buf, 10, &data);
486 if ((!ret) && (data < 0x10000)) {
487 ad774x_i2c_write(chip, AD774X_CAP_GAINH, data >> 8);
488 ad774x_i2c_write(chip, AD774X_CAP_GAINL, data);
489 chip->cap_gain = data;
490 return len;
493 return -EINVAL;
496 static IIO_DEV_ATTR_CAP_GAIN(S_IRUGO | S_IWUSR,
497 ad774x_show_cap_gain,
498 ad774x_store_cap_gain);
500 static struct attribute *ad774x_attributes[] = {
501 &iio_dev_attr_available_conversion_modes.dev_attr.attr,
502 &iio_dev_attr_conversion_mode.dev_attr.attr,
503 &iio_dev_attr_cap_setup.dev_attr.attr,
504 &iio_dev_attr_in0_setup.dev_attr.attr,
505 &iio_dev_attr_exec_setup.dev_attr.attr,
506 &iio_dev_attr_cap_offs.dev_attr.attr,
507 &iio_dev_attr_cap_gain.dev_attr.attr,
508 &iio_dev_attr_in0_gain.dev_attr.attr,
509 &iio_dev_attr_in0_raw.dev_attr.attr,
510 &iio_dev_attr_cap0_raw.dev_attr.attr,
511 &iio_dev_attr_capdac0_raw.dev_attr.attr,
512 &iio_dev_attr_capdac1_raw.dev_attr.attr,
513 NULL,
516 static const struct attribute_group ad774x_attribute_group = {
517 .attrs = ad774x_attributes,
521 * data ready events
524 #define IIO_EVENT_CODE_CAP_RDY 0
525 #define IIO_EVENT_CODE_VT_RDY 1
527 #define IIO_EVENT_ATTR_CAP_RDY_SH(_evlist, _show, _store, _mask) \
528 IIO_EVENT_ATTR_SH(cap_rdy, _evlist, _show, _store, _mask)
530 #define IIO_EVENT_ATTR_VT_RDY_SH(_evlist, _show, _store, _mask) \
531 IIO_EVENT_ATTR_SH(vt_rdy, _evlist, _show, _store, _mask)
533 static irqreturn_t ad774x_event_handler(int irq, void *private)
535 struct iio_dev *indio_dev = private;
536 struct ad774x_chip_info *chip = iio_priv(indio_dev);
537 u8 int_status;
539 ad774x_i2c_read(chip, AD774X_STATUS, &int_status, 1);
541 if (int_status & AD774X_STATUS_RDYCAP)
542 iio_push_event(indio_dev, 0,
543 IIO_EVENT_CODE_CAP_RDY,
544 iio_get_time_ns());
546 if (int_status & AD774X_STATUS_RDYVT)
547 iio_push_event(indio_dev, 0,
548 IIO_EVENT_CODE_VT_RDY,
549 iio_get_time_ns());
551 return IRQ_HANDLED;
554 static IIO_CONST_ATTR(cap_rdy_en, "1");
555 static IIO_CONST_ATTR(vt_rdy_en, "1");
557 static struct attribute *ad774x_event_attributes[] = {
558 &iio_const_attr_cap_rdy_en.dev_attr.attr,
559 &iio_const_attr_vt_rdy_en.dev_attr.attr,
560 NULL,
563 static struct attribute_group ad774x_event_attribute_group = {
564 .attrs = ad774x_event_attributes,
567 static const struct iio_info ad774x_info = {
568 .attrs = &ad774x_event_attribute_group,
569 .event_attrs = &ad774x_event_attribute_group,
570 .num_interrupt_lines = 1,
571 .driver_module = THIS_MODULE,
574 * device probe and remove
577 static int __devinit ad774x_probe(struct i2c_client *client,
578 const struct i2c_device_id *id)
580 int ret = 0, regdone = 0;
581 struct ad774x_chip_info *chip;
582 struct iio_dev *indio_dev;
584 indio_dev = iio_allocate_device(sizeof(*chip));
585 if (indio_dev == NULL) {
586 ret = -ENOMEM;
587 goto error_ret;
589 chip = iio_priv(indio_dev);
590 /* this is only used for device removal purposes */
591 i2c_set_clientdata(client, indio_dev);
593 chip->client = client;
595 /* Establish that the iio_dev is a child of the i2c device */
596 indio_dev->name = id->name;
597 indio_dev->dev.parent = &client->dev;
598 indio_dev->info = &ad774x_info;
599 indio_dev->modes = INDIO_DIRECT_MODE;
601 ret = iio_device_register(indio_dev);
602 if (ret)
603 goto error_free_dev;
604 regdone = 1;
606 if (client->irq) {
607 ret = request_threaded_irq(client->irq,
608 NULL,
609 &ad774x_event_handler,
610 IRQF_TRIGGER_FALLING,
611 "ad774x",
612 indio_dev);
613 if (ret)
614 goto error_free_dev;
617 dev_err(&client->dev, "%s capacitive sensor registered, irq: %d\n", id->name, client->irq);
619 return 0;
621 error_free_dev:
622 if (regdone)
623 free_irq(client->irq, indio_dev);
624 else
625 iio_free_device(indio_dev);
626 error_ret:
627 return ret;
630 static int __devexit ad774x_remove(struct i2c_client *client)
632 struct iio_dev *indio_dev = i2c_get_clientdata(client);
634 if (client->irq)
635 free_irq(client->irq, indio_dev);
636 iio_device_unregister(indio_dev);
638 return 0;
641 static const struct i2c_device_id ad774x_id[] = {
642 { "ad7745", 0 },
643 { "ad7746", 0 },
644 { "ad7747", 0 },
648 MODULE_DEVICE_TABLE(i2c, ad774x_id);
650 static struct i2c_driver ad774x_driver = {
651 .driver = {
652 .name = "ad774x",
654 .probe = ad774x_probe,
655 .remove = __devexit_p(ad774x_remove),
656 .id_table = ad774x_id,
659 static __init int ad774x_init(void)
661 return i2c_add_driver(&ad774x_driver);
664 static __exit void ad774x_exit(void)
666 i2c_del_driver(&ad774x_driver);
669 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
670 MODULE_DESCRIPTION("Analog Devices ad7745/6/7 capacitive sensor driver");
671 MODULE_LICENSE("GPL v2");
673 module_init(ad774x_init);
674 module_exit(ad774x_exit);