2 * AD774X capacitive sensor driver supporting AD7745/6/7
4 * Copyright 2010 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
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>
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
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
;
58 u16 cap_offs
; /* Capacitive offset */
59 u16 cap_gain
; /* Capacitive gain calibration */
60 u16 volt_gain
; /* Voltage gain calibration */
65 char *conversion_mode
;
68 struct ad774x_conversion_mode
{
73 static struct ad774x_conversion_mode
74 ad774x_conv_mode_table
[AD774X_MAX_CONV_MODE
] = {
76 { "continuous-conversion", 1 },
77 { "single-conversion", 2 },
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
;
92 ret
= i2c_master_send(client
, ®
, 1);
94 dev_err(&client
->dev
, "I2C write error\n");
98 ret
= i2c_master_recv(client
, data
, len
);
100 dev_err(&client
->dev
, "I2C read error\n");
107 static int ad774x_i2c_write(struct ad774x_chip_info
*chip
, u8 reg
, u8 data
)
109 struct i2c_client
*client
= chip
->client
;
117 ret
= i2c_master_send(client
, tx
, 2);
119 dev_err(&client
->dev
, "I2C write error\n");
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
,
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");
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
,
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
,
181 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
182 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
);
198 dev_err(dev
, "not supported conversion mode\n");
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
,
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
);
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
,
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
);
232 ret
= strict_strtoul(buf
, 10, &data
);
235 ad774x_i2c_write(chip
, this_attr
->address
,
236 (data
? AD774X_CAPDAC_EN
: 0) | (data
& 0x7F));
243 static IIO_DEVICE_ATTR(capdac0_raw
, S_IRUGO
| S_IWUSR
,
244 ad774x_show_dac_value
,
245 ad774x_store_dac_value
,
248 static IIO_DEVICE_ATTR(capdac1_raw
, S_IRUGO
| S_IWUSR
,
249 ad774x_show_dac_value
,
250 ad774x_store_dac_value
,
253 static ssize_t
ad774x_show_cap_setup(struct device
*dev
,
254 struct device_attribute
*attr
,
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
,
268 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
269 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
;
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
,
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
,
303 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
304 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
;
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
,
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
,
338 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
339 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
;
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
,
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
,
373 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
374 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
;
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
,
398 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
399 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
,
415 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
416 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
,
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
,
443 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
444 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
;
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
,
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
,
479 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
480 struct ad774x_chip_info
*chip
= iio_priv(dev_info
);
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
;
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
,
516 static const struct attribute_group ad774x_attribute_group
= {
517 .attrs
= ad774x_attributes
,
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
);
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
,
546 if (int_status
& AD774X_STATUS_RDYVT
)
547 iio_push_event(indio_dev
, 0,
548 IIO_EVENT_CODE_VT_RDY
,
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
,
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
) {
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
);
607 ret
= request_threaded_irq(client
->irq
,
609 &ad774x_event_handler
,
610 IRQF_TRIGGER_FALLING
,
617 dev_err(&client
->dev
, "%s capacitive sensor registered, irq: %d\n", id
->name
, client
->irq
);
623 free_irq(client
->irq
, indio_dev
);
625 iio_free_device(indio_dev
);
630 static int __devexit
ad774x_remove(struct i2c_client
*client
)
632 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
635 free_irq(client
->irq
, indio_dev
);
636 iio_device_unregister(indio_dev
);
641 static const struct i2c_device_id ad774x_id
[] = {
648 MODULE_DEVICE_TABLE(i2c
, ad774x_id
);
650 static struct i2c_driver ad774x_driver
= {
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
);