aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/staging/iio/adc/ad7745.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/staging/iio/adc/ad7745.c')
-rw-r--r--drivers/staging/iio/adc/ad7745.c674
1 files changed, 674 insertions, 0 deletions
diff --git a/drivers/staging/iio/adc/ad7745.c b/drivers/staging/iio/adc/ad7745.c
new file mode 100644
index 00000000000..4c13f26aa9a
--- /dev/null
+++ b/drivers/staging/iio/adc/ad7745.c
@@ -0,0 +1,674 @@
1/*
2 * AD774X capacitive sensor driver supporting AD7745/6/7
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/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>
17
18#include "../iio.h"
19#include "../sysfs.h"
20
21/*
22 * AD774X registers definition
23 */
24
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
48
49#define AD774X_MAX_CONV_MODE 6
50
51/*
52 * struct ad774x_chip_info - chip specifc information
53 */
54
55struct 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;
64
65 char *conversion_mode;
66};
67
68struct ad774x_conversion_mode {
69 char *name;
70 u8 reg_cfg;
71};
72
73static struct ad774x_conversion_mode
74ad774x_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 },
81};
82
83/*
84 * ad774x register access by I2C
85 */
86
87static int ad774x_i2c_read(struct ad774x_chip_info *chip, u8 reg, u8 *data, int len)
88{
89 struct i2c_client *client = chip->client;
90 int ret;
91
92 ret = i2c_master_send(client, &reg, 1);
93 if (ret < 0) {
94 dev_err(&client->dev, "I2C write error\n");
95 return ret;
96 }
97
98 ret = i2c_master_recv(client, data, len);
99 if (ret < 0) {
100 dev_err(&client->dev, "I2C read error\n");
101 return ret;
102 }
103
104 return ret;
105}
106
107static int ad774x_i2c_write(struct ad774x_chip_info *chip, u8 reg, u8 data)
108{
109 struct i2c_client *client = chip->client;
110 int ret;
111
112 u8 tx[2] = {
113 reg,
114 data,
115 };
116
117 ret = i2c_master_send(client, tx, 2);
118 if (ret < 0)
119 dev_err(&client->dev, "I2C write error\n");
120
121 return ret;
122}
123
124/*
125 * sysfs nodes
126 */
127
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)
148
149static ssize_t ad774x_show_conversion_modes(struct device *dev,
150 struct device_attribute *attr,
151 char *buf)
152{
153 int i;
154 int len = 0;
155
156 for (i = 0; i < AD774X_MAX_CONV_MODE; i++)
157 len += sprintf(buf + len, "%s ", ad774x_conv_mode_table[i].name);
158
159 len += sprintf(buf + len, "\n");
160
161 return len;
162}
163
164static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad774x_show_conversion_modes);
165
166static ssize_t ad774x_show_conversion_mode(struct device *dev,
167 struct device_attribute *attr,
168 char *buf)
169{
170 struct iio_dev *dev_info = dev_get_drvdata(dev);
171 struct ad774x_chip_info *chip = iio_priv(dev_info);
172
173 return sprintf(buf, "%s\n", chip->conversion_mode);
174}
175
176static ssize_t ad774x_store_conversion_mode(struct device *dev,
177 struct device_attribute *attr,
178 const char *buf,
179 size_t len)
180{
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;
185
186 ad774x_i2c_read(chip, AD774X_CFG, &cfg, 1);
187
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;
195 }
196 }
197
198 dev_err(dev, "not supported conversion mode\n");
199
200 return -EINVAL;
201}
202
203static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO | S_IWUSR,
204 ad774x_show_conversion_mode,
205 ad774x_store_conversion_mode);
206
207static ssize_t ad774x_show_dac_value(struct device *dev,
208 struct device_attribute *attr,
209 char *buf)
210{
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;
215
216 ad774x_i2c_read(chip, this_attr->address, &data, 1);
217
218 return sprintf(buf, "%02x\n", data & 0x7F);
219}
220
221static ssize_t ad774x_store_dac_value(struct device *dev,
222 struct device_attribute *attr,
223 const char *buf,
224 size_t len)
225{
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;
231
232 ret = strict_strtoul(buf, 10, &data);
233
234 if (!ret) {
235 ad774x_i2c_write(chip, this_attr->address,
236 (data ? AD774X_CAPDAC_EN : 0) | (data & 0x7F));
237 return len;
238 }
239
240 return -EINVAL;
241}
242
243static IIO_DEVICE_ATTR(capdac0_raw, S_IRUGO | S_IWUSR,
244 ad774x_show_dac_value,
245 ad774x_store_dac_value,
246 AD774X_CAPDACA);
247
248static IIO_DEVICE_ATTR(capdac1_raw, S_IRUGO | S_IWUSR,
249 ad774x_show_dac_value,
250 ad774x_store_dac_value,
251 AD774X_CAPDACB);
252
253static ssize_t ad774x_show_cap_setup(struct device *dev,
254 struct device_attribute *attr,
255 char *buf)
256{
257 struct iio_dev *dev_info = dev_get_drvdata(dev);
258 struct ad774x_chip_info *chip = iio_priv(dev_info);
259
260 return sprintf(buf, "0x%02x\n", chip->cap_setup);
261}
262
263static ssize_t ad774x_store_cap_setup(struct device *dev,
264 struct device_attribute *attr,
265 const char *buf,
266 size_t len)
267{
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;
272
273 ret = strict_strtoul(buf, 10, &data);
274
275 if ((!ret) && (data < 0x100)) {
276 ad774x_i2c_write(chip, AD774X_CAP_SETUP, data);
277 chip->cap_setup = data;
278 return len;
279 }
280
281 return -EINVAL;
282}
283
284static IIO_DEV_ATTR_CAP_SETUP(S_IRUGO | S_IWUSR,
285 ad774x_show_cap_setup,
286 ad774x_store_cap_setup);
287
288static ssize_t ad774x_show_vt_setup(struct device *dev,
289 struct device_attribute *attr,
290 char *buf)
291{
292 struct iio_dev *dev_info = dev_get_drvdata(dev);
293 struct ad774x_chip_info *chip = iio_priv(dev_info);
294
295 return sprintf(buf, "0x%02x\n", chip->vt_setup);
296}
297
298static ssize_t ad774x_store_vt_setup(struct device *dev,
299 struct device_attribute *attr,
300 const char *buf,
301 size_t len)
302{
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;
307
308 ret = strict_strtoul(buf, 10, &data);
309
310 if ((!ret) && (data < 0x100)) {
311 ad774x_i2c_write(chip, AD774X_VT_SETUP, data);
312 chip->vt_setup = data;
313 return len;
314 }
315
316 return -EINVAL;
317}
318
319static IIO_DEV_ATTR_VT_SETUP(S_IRUGO | S_IWUSR,
320 ad774x_show_vt_setup,
321 ad774x_store_vt_setup);
322
323static ssize_t ad774x_show_exec_setup(struct device *dev,
324 struct device_attribute *attr,
325 char *buf)
326{
327 struct iio_dev *dev_info = dev_get_drvdata(dev);
328 struct ad774x_chip_info *chip = iio_priv(dev_info);
329
330 return sprintf(buf, "0x%02x\n", chip->exec_setup);
331}
332
333static ssize_t ad774x_store_exec_setup(struct device *dev,
334 struct device_attribute *attr,
335 const char *buf,
336 size_t len)
337{
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;
342
343 ret = strict_strtoul(buf, 10, &data);
344
345 if ((!ret) && (data < 0x100)) {
346 ad774x_i2c_write(chip, AD774X_EXEC_SETUP, data);
347 chip->exec_setup = data;
348 return len;
349 }
350
351 return -EINVAL;
352}
353
354static IIO_DEV_ATTR_EXEC_SETUP(S_IRUGO | S_IWUSR,
355 ad774x_show_exec_setup,
356 ad774x_store_exec_setup);
357
358static ssize_t ad774x_show_volt_gain(struct device *dev,
359 struct device_attribute *attr,
360 char *buf)
361{
362 struct iio_dev *dev_info = dev_get_drvdata(dev);
363 struct ad774x_chip_info *chip = iio_priv(dev_info);
364
365 return sprintf(buf, "%d\n", chip->volt_gain);
366}
367
368static ssize_t ad774x_store_volt_gain(struct device *dev,
369 struct device_attribute *attr,
370 const char *buf,
371 size_t len)
372{
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;
377
378 ret = strict_strtoul(buf, 10, &data);
379
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;
385 }
386
387 return -EINVAL;
388}
389
390static IIO_DEV_ATTR_VOLT_GAIN(S_IRUGO | S_IWUSR,
391 ad774x_show_volt_gain,
392 ad774x_store_volt_gain);
393
394static ssize_t ad774x_show_cap_data(struct device *dev,
395 struct device_attribute *attr,
396 char *buf)
397{
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];
402
403 ad774x_i2c_read(chip, AD774X_CAP_DATA_HIGH, tmp, 3);
404 data = ((int)tmp[0] << 16) | ((int)tmp[1] << 8) | (int)tmp[2];
405
406 return sprintf(buf, "%ld\n", data);
407}
408
409static IIO_DEV_ATTR_CAP_DATA(ad774x_show_cap_data);
410
411static ssize_t ad774x_show_vt_data(struct device *dev,
412 struct device_attribute *attr,
413 char *buf)
414{
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];
419
420 ad774x_i2c_read(chip, AD774X_VT_DATA_HIGH, tmp, 3);
421 data = ((int)tmp[0] << 16) | ((int)tmp[1] << 8) | (int)tmp[2];
422
423 return sprintf(buf, "%ld\n", data);
424}
425
426static IIO_DEV_ATTR_VT_DATA(ad774x_show_vt_data);
427
428static ssize_t ad774x_show_cap_offs(struct device *dev,
429 struct device_attribute *attr,
430 char *buf)
431{
432 struct iio_dev *dev_info = dev_get_drvdata(dev);
433 struct ad774x_chip_info *chip = iio_priv(dev_info);
434
435 return sprintf(buf, "%d\n", chip->cap_offs);
436}
437
438static ssize_t ad774x_store_cap_offs(struct device *dev,
439 struct device_attribute *attr,
440 const char *buf,
441 size_t len)
442{
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;
447
448 ret = strict_strtoul(buf, 10, &data);
449
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;
455 }
456
457 return -EINVAL;
458}
459
460static IIO_DEV_ATTR_CAP_OFFS(S_IRUGO | S_IWUSR,
461 ad774x_show_cap_offs,
462 ad774x_store_cap_offs);
463
464static ssize_t ad774x_show_cap_gain(struct device *dev,
465 struct device_attribute *attr,
466 char *buf)
467{
468 struct iio_dev *dev_info = dev_get_drvdata(dev);
469 struct ad774x_chip_info *chip = iio_priv(dev_info);
470
471 return sprintf(buf, "%d\n", chip->cap_gain);
472}
473
474static ssize_t ad774x_store_cap_gain(struct device *dev,
475 struct device_attribute *attr,
476 const char *buf,
477 size_t len)
478{
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;
483
484 ret = strict_strtoul(buf, 10, &data);
485
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;
491 }
492
493 return -EINVAL;
494}
495
496static IIO_DEV_ATTR_CAP_GAIN(S_IRUGO | S_IWUSR,
497 ad774x_show_cap_gain,
498 ad774x_store_cap_gain);
499
500static 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,
514};
515
516static const struct attribute_group ad774x_attribute_group = {
517 .attrs = ad774x_attributes,
518};
519
520/*
521 * data ready events
522 */
523
524#define IIO_EVENT_CODE_CAP_RDY 0
525#define IIO_EVENT_CODE_VT_RDY 1
526
527#define IIO_EVENT_ATTR_CAP_RDY_SH(_evlist, _show, _store, _mask) \
528 IIO_EVENT_ATTR_SH(cap_rdy, _evlist, _show, _store, _mask)
529
530#define IIO_EVENT_ATTR_VT_RDY_SH(_evlist, _show, _store, _mask) \
531 IIO_EVENT_ATTR_SH(vt_rdy, _evlist, _show, _store, _mask)
532
533static irqreturn_t ad774x_event_handler(int irq, void *private)
534{
535 struct iio_dev *indio_dev = private;
536 struct ad774x_chip_info *chip = iio_priv(indio_dev);
537 u8 int_status;
538
539 ad774x_i2c_read(chip, AD774X_STATUS, &int_status, 1);
540
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());
545
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());
550
551 return IRQ_HANDLED;
552}
553
554static IIO_CONST_ATTR(cap_rdy_en, "1");
555static IIO_CONST_ATTR(vt_rdy_en, "1");
556
557static 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,
561};
562
563static struct attribute_group ad774x_event_attribute_group = {
564 .attrs = ad774x_event_attributes,
565};
566
567static 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,
572};
573/*
574 * device probe and remove
575 */
576
577static int __devinit ad774x_probe(struct i2c_client *client,
578 const struct i2c_device_id *id)
579{
580 int ret = 0, regdone = 0;
581 struct ad774x_chip_info *chip;
582 struct iio_dev *indio_dev;
583
584 indio_dev = iio_allocate_device(sizeof(*chip));
585 if (indio_dev == NULL) {
586 ret = -ENOMEM;
587 goto error_ret;
588 }
589 chip = iio_priv(indio_dev);
590 /* this is only used for device removal purposes */
591 i2c_set_clientdata(client, indio_dev);
592
593 chip->client = client;
594
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;
600
601 ret = iio_device_register(indio_dev);
602 if (ret)
603 goto error_free_dev;
604 regdone = 1;
605
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;
615 }
616
617 dev_err(&client->dev, "%s capacitive sensor registered, irq: %d\n", id->name, client->irq);
618
619 return 0;
620
621error_free_dev:
622 if (regdone)
623 free_irq(client->irq, indio_dev);
624 else
625 iio_free_device(indio_dev);
626error_ret:
627 return ret;
628}
629
630static int __devexit ad774x_remove(struct i2c_client *client)
631{
632 struct iio_dev *indio_dev = i2c_get_clientdata(client);
633
634 if (client->irq)
635 free_irq(client->irq, indio_dev);
636 iio_device_unregister(indio_dev);
637
638 return 0;
639}
640
641static const struct i2c_device_id ad774x_id[] = {
642 { "ad7745", 0 },
643 { "ad7746", 0 },
644 { "ad7747", 0 },
645 {}
646};
647
648MODULE_DEVICE_TABLE(i2c, ad774x_id);
649
650static struct i2c_driver ad774x_driver = {
651 .driver = {
652 .name = "ad774x",
653 },
654 .probe = ad774x_probe,
655 .remove = __devexit_p(ad774x_remove),
656 .id_table = ad774x_id,
657};
658
659static __init int ad774x_init(void)
660{
661 return i2c_add_driver(&ad774x_driver);
662}
663
664static __exit void ad774x_exit(void)
665{
666 i2c_del_driver(&ad774x_driver);
667}
668
669MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
670MODULE_DESCRIPTION("Analog Devices ad7745/6/7 capacitive sensor driver");
671MODULE_LICENSE("GPL v2");
672
673module_init(ad774x_init);
674module_exit(ad774x_exit);