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
;
57 struct iio_dev
*indio_dev
;
59 u16 cap_offs
; /* Capacitive offset */
60 u16 cap_gain
; /* Capacitive gain calibration */
61 u16 volt_gain
; /* Voltage gain calibration */
66 char *conversion_mode
;
69 struct ad774x_conversion_mode
{
74 static struct ad774x_conversion_mode
75 ad774x_conv_mode_table
[AD774X_MAX_CONV_MODE
] = {
77 { "continuous-conversion", 1 },
78 { "single-conversion", 2 },
80 { "offset-calibration", 5 },
81 { "gain-calibration", 6 },
85 * ad774x register access by I2C
88 static int ad774x_i2c_read(struct ad774x_chip_info
*chip
, u8 reg
, u8
*data
, int len
)
90 struct i2c_client
*client
= chip
->client
;
93 ret
= i2c_master_send(client
, ®
, 1);
95 dev_err(&client
->dev
, "I2C write error\n");
99 ret
= i2c_master_recv(client
, data
, len
);
101 dev_err(&client
->dev
, "I2C read error\n");
108 static int ad774x_i2c_write(struct ad774x_chip_info
*chip
, u8 reg
, u8 data
)
110 struct i2c_client
*client
= chip
->client
;
118 ret
= i2c_master_send(client
, tx
, 2);
120 dev_err(&client
->dev
, "I2C write error\n");
129 #define IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(_show) \
130 IIO_DEVICE_ATTR(available_conversion_modes, S_IRUGO, _show, NULL, 0)
131 #define IIO_DEV_ATTR_CONVERSION_MODE(_mode, _show, _store) \
132 IIO_DEVICE_ATTR(conversion_mode, _mode, _show, _store, 0)
133 #define IIO_DEV_ATTR_CAP_SETUP(_mode, _show, _store) \
134 IIO_DEVICE_ATTR(cap_setup, _mode, _show, _store, 0)
135 #define IIO_DEV_ATTR_VT_SETUP(_mode, _show, _store) \
136 IIO_DEVICE_ATTR(in0_setup, _mode, _show, _store, 0)
137 #define IIO_DEV_ATTR_EXEC_SETUP(_mode, _show, _store) \
138 IIO_DEVICE_ATTR(exec_setup, _mode, _show, _store, 0)
139 #define IIO_DEV_ATTR_VOLT_GAIN(_mode, _show, _store) \
140 IIO_DEVICE_ATTR(in0_gain, _mode, _show, _store, 0)
141 #define IIO_DEV_ATTR_CAP_OFFS(_mode, _show, _store) \
142 IIO_DEVICE_ATTR(cap_offs, _mode, _show, _store, 0)
143 #define IIO_DEV_ATTR_CAP_GAIN(_mode, _show, _store) \
144 IIO_DEVICE_ATTR(cap_gain, _mode, _show, _store, 0)
145 #define IIO_DEV_ATTR_CAP_DATA(_show) \
146 IIO_DEVICE_ATTR(cap0_raw, S_IRUGO, _show, NULL, 0)
147 #define IIO_DEV_ATTR_VT_DATA(_show) \
148 IIO_DEVICE_ATTR(in0_raw, S_IRUGO, _show, NULL, 0)
150 static ssize_t
ad774x_show_conversion_modes(struct device
*dev
,
151 struct device_attribute
*attr
,
157 for (i
= 0; i
< AD774X_MAX_CONV_MODE
; i
++)
158 len
+= sprintf(buf
+ len
, "%s ", ad774x_conv_mode_table
[i
].name
);
160 len
+= sprintf(buf
+ len
, "\n");
165 static IIO_DEV_ATTR_AVAIL_CONVERSION_MODES(ad774x_show_conversion_modes
);
167 static ssize_t
ad774x_show_conversion_mode(struct device
*dev
,
168 struct device_attribute
*attr
,
171 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
172 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
174 return sprintf(buf
, "%s\n", chip
->conversion_mode
);
177 static ssize_t
ad774x_store_conversion_mode(struct device
*dev
,
178 struct device_attribute
*attr
,
182 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
183 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
187 ad774x_i2c_read(chip
, AD774X_CFG
, &cfg
, 1);
189 for (i
= 0; i
< AD774X_MAX_CONV_MODE
; i
++) {
190 if (strncmp(buf
, ad774x_conv_mode_table
[i
].name
,
191 strlen(ad774x_conv_mode_table
[i
].name
) - 1) == 0) {
192 chip
->conversion_mode
= ad774x_conv_mode_table
[i
].name
;
193 cfg
|= 0x18 | ad774x_conv_mode_table
[i
].reg_cfg
;
194 ad774x_i2c_write(chip
, AD774X_CFG
, cfg
);
199 dev_err(dev
, "not supported conversion mode\n");
204 static IIO_DEV_ATTR_CONVERSION_MODE(S_IRUGO
| S_IWUSR
,
205 ad774x_show_conversion_mode
,
206 ad774x_store_conversion_mode
);
208 static ssize_t
ad774x_show_dac_value(struct device
*dev
,
209 struct device_attribute
*attr
,
212 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
213 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
214 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
217 ad774x_i2c_read(chip
, this_attr
->address
, &data
, 1);
219 return sprintf(buf
, "%02x\n", data
& 0x7F);
222 static ssize_t
ad774x_store_dac_value(struct device
*dev
,
223 struct device_attribute
*attr
,
227 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
228 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
229 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
233 ret
= strict_strtoul(buf
, 10, &data
);
236 ad774x_i2c_write(chip
, this_attr
->address
,
237 (data
? AD774X_CAPDAC_EN
: 0) | (data
& 0x7F));
244 static IIO_DEVICE_ATTR(capdac0_raw
, S_IRUGO
| S_IWUSR
,
245 ad774x_show_dac_value
,
246 ad774x_store_dac_value
,
249 static IIO_DEVICE_ATTR(capdac1_raw
, S_IRUGO
| S_IWUSR
,
250 ad774x_show_dac_value
,
251 ad774x_store_dac_value
,
254 static ssize_t
ad774x_show_cap_setup(struct device
*dev
,
255 struct device_attribute
*attr
,
258 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
259 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
261 return sprintf(buf
, "0x%02x\n", chip
->cap_setup
);
264 static ssize_t
ad774x_store_cap_setup(struct device
*dev
,
265 struct device_attribute
*attr
,
269 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
270 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
274 ret
= strict_strtoul(buf
, 10, &data
);
276 if ((!ret
) && (data
< 0x100)) {
277 ad774x_i2c_write(chip
, AD774X_CAP_SETUP
, data
);
278 chip
->cap_setup
= data
;
285 static IIO_DEV_ATTR_CAP_SETUP(S_IRUGO
| S_IWUSR
,
286 ad774x_show_cap_setup
,
287 ad774x_store_cap_setup
);
289 static ssize_t
ad774x_show_vt_setup(struct device
*dev
,
290 struct device_attribute
*attr
,
293 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
294 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
296 return sprintf(buf
, "0x%02x\n", chip
->vt_setup
);
299 static ssize_t
ad774x_store_vt_setup(struct device
*dev
,
300 struct device_attribute
*attr
,
304 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
305 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
309 ret
= strict_strtoul(buf
, 10, &data
);
311 if ((!ret
) && (data
< 0x100)) {
312 ad774x_i2c_write(chip
, AD774X_VT_SETUP
, data
);
313 chip
->vt_setup
= data
;
320 static IIO_DEV_ATTR_VT_SETUP(S_IRUGO
| S_IWUSR
,
321 ad774x_show_vt_setup
,
322 ad774x_store_vt_setup
);
324 static ssize_t
ad774x_show_exec_setup(struct device
*dev
,
325 struct device_attribute
*attr
,
328 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
329 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
331 return sprintf(buf
, "0x%02x\n", chip
->exec_setup
);
334 static ssize_t
ad774x_store_exec_setup(struct device
*dev
,
335 struct device_attribute
*attr
,
339 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
340 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
344 ret
= strict_strtoul(buf
, 10, &data
);
346 if ((!ret
) && (data
< 0x100)) {
347 ad774x_i2c_write(chip
, AD774X_EXEC_SETUP
, data
);
348 chip
->exec_setup
= data
;
355 static IIO_DEV_ATTR_EXEC_SETUP(S_IRUGO
| S_IWUSR
,
356 ad774x_show_exec_setup
,
357 ad774x_store_exec_setup
);
359 static ssize_t
ad774x_show_volt_gain(struct device
*dev
,
360 struct device_attribute
*attr
,
363 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
364 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
366 return sprintf(buf
, "%d\n", chip
->volt_gain
);
369 static ssize_t
ad774x_store_volt_gain(struct device
*dev
,
370 struct device_attribute
*attr
,
374 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
375 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
379 ret
= strict_strtoul(buf
, 10, &data
);
381 if ((!ret
) && (data
< 0x10000)) {
382 ad774x_i2c_write(chip
, AD774X_VOLT_GAINH
, data
>> 8);
383 ad774x_i2c_write(chip
, AD774X_VOLT_GAINL
, data
);
384 chip
->volt_gain
= data
;
391 static IIO_DEV_ATTR_VOLT_GAIN(S_IRUGO
| S_IWUSR
,
392 ad774x_show_volt_gain
,
393 ad774x_store_volt_gain
);
395 static ssize_t
ad774x_show_cap_data(struct device
*dev
,
396 struct device_attribute
*attr
,
399 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
400 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
404 ad774x_i2c_read(chip
, AD774X_CAP_DATA_HIGH
, tmp
, 3);
405 data
= ((int)tmp
[0] << 16) | ((int)tmp
[1] << 8) | (int)tmp
[2];
407 return sprintf(buf
, "%ld\n", data
);
410 static IIO_DEV_ATTR_CAP_DATA(ad774x_show_cap_data
);
412 static ssize_t
ad774x_show_vt_data(struct device
*dev
,
413 struct device_attribute
*attr
,
416 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
417 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
421 ad774x_i2c_read(chip
, AD774X_VT_DATA_HIGH
, tmp
, 3);
422 data
= ((int)tmp
[0] << 16) | ((int)tmp
[1] << 8) | (int)tmp
[2];
424 return sprintf(buf
, "%ld\n", data
);
427 static IIO_DEV_ATTR_VT_DATA(ad774x_show_vt_data
);
429 static ssize_t
ad774x_show_cap_offs(struct device
*dev
,
430 struct device_attribute
*attr
,
433 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
434 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
436 return sprintf(buf
, "%d\n", chip
->cap_offs
);
439 static ssize_t
ad774x_store_cap_offs(struct device
*dev
,
440 struct device_attribute
*attr
,
444 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
445 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
449 ret
= strict_strtoul(buf
, 10, &data
);
451 if ((!ret
) && (data
< 0x10000)) {
452 ad774x_i2c_write(chip
, AD774X_CAP_OFFH
, data
>> 8);
453 ad774x_i2c_write(chip
, AD774X_CAP_OFFL
, data
);
454 chip
->cap_offs
= data
;
461 static IIO_DEV_ATTR_CAP_OFFS(S_IRUGO
| S_IWUSR
,
462 ad774x_show_cap_offs
,
463 ad774x_store_cap_offs
);
465 static ssize_t
ad774x_show_cap_gain(struct device
*dev
,
466 struct device_attribute
*attr
,
469 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
470 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
472 return sprintf(buf
, "%d\n", chip
->cap_gain
);
475 static ssize_t
ad774x_store_cap_gain(struct device
*dev
,
476 struct device_attribute
*attr
,
480 struct iio_dev
*dev_info
= dev_get_drvdata(dev
);
481 struct ad774x_chip_info
*chip
= dev_info
->dev_data
;
485 ret
= strict_strtoul(buf
, 10, &data
);
487 if ((!ret
) && (data
< 0x10000)) {
488 ad774x_i2c_write(chip
, AD774X_CAP_GAINH
, data
>> 8);
489 ad774x_i2c_write(chip
, AD774X_CAP_GAINL
, data
);
490 chip
->cap_gain
= data
;
497 static IIO_DEV_ATTR_CAP_GAIN(S_IRUGO
| S_IWUSR
,
498 ad774x_show_cap_gain
,
499 ad774x_store_cap_gain
);
501 static struct attribute
*ad774x_attributes
[] = {
502 &iio_dev_attr_available_conversion_modes
.dev_attr
.attr
,
503 &iio_dev_attr_conversion_mode
.dev_attr
.attr
,
504 &iio_dev_attr_cap_setup
.dev_attr
.attr
,
505 &iio_dev_attr_in0_setup
.dev_attr
.attr
,
506 &iio_dev_attr_exec_setup
.dev_attr
.attr
,
507 &iio_dev_attr_cap_offs
.dev_attr
.attr
,
508 &iio_dev_attr_cap_gain
.dev_attr
.attr
,
509 &iio_dev_attr_in0_gain
.dev_attr
.attr
,
510 &iio_dev_attr_in0_raw
.dev_attr
.attr
,
511 &iio_dev_attr_cap0_raw
.dev_attr
.attr
,
512 &iio_dev_attr_capdac0_raw
.dev_attr
.attr
,
513 &iio_dev_attr_capdac1_raw
.dev_attr
.attr
,
517 static const struct attribute_group ad774x_attribute_group
= {
518 .attrs
= ad774x_attributes
,
525 #define IIO_EVENT_CODE_CAP_RDY 0
526 #define IIO_EVENT_CODE_VT_RDY 1
528 #define IIO_EVENT_ATTR_CAP_RDY_SH(_evlist, _show, _store, _mask) \
529 IIO_EVENT_ATTR_SH(cap_rdy, _evlist, _show, _store, _mask)
531 #define IIO_EVENT_ATTR_VT_RDY_SH(_evlist, _show, _store, _mask) \
532 IIO_EVENT_ATTR_SH(vt_rdy, _evlist, _show, _store, _mask)
534 static irqreturn_t
ad774x_event_handler(int irq
, void *private)
536 struct iio_dev
*indio_dev
= private;
537 struct ad774x_chip_info
*chip
= iio_dev_get_devdata(indio_dev
);
540 ad774x_i2c_read(chip
, AD774X_STATUS
, &int_status
, 1);
542 if (int_status
& AD774X_STATUS_RDYCAP
)
543 iio_push_event(indio_dev
, 0,
544 IIO_EVENT_CODE_CAP_RDY
,
547 if (int_status
& AD774X_STATUS_RDYVT
)
548 iio_push_event(indio_dev
, 0,
549 IIO_EVENT_CODE_VT_RDY
,
555 static IIO_CONST_ATTR(cap_rdy_en
, "1");
556 static IIO_CONST_ATTR(vt_rdy_en
, "1");
558 static struct attribute
*ad774x_event_attributes
[] = {
559 &iio_const_attr_cap_rdy_en
.dev_attr
.attr
,
560 &iio_const_attr_vt_rdy_en
.dev_attr
.attr
,
564 static struct attribute_group ad774x_event_attribute_group
= {
565 .attrs
= ad774x_event_attributes
,
569 * device probe and remove
572 static int __devinit
ad774x_probe(struct i2c_client
*client
,
573 const struct i2c_device_id
*id
)
575 int ret
= 0, regdone
= 0;
576 struct ad774x_chip_info
*chip
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
582 /* this is only used for device removal purposes */
583 i2c_set_clientdata(client
, chip
);
585 chip
->client
= client
;
587 chip
->indio_dev
= iio_allocate_device(0);
588 if (chip
->indio_dev
== NULL
) {
590 goto error_free_chip
;
593 /* Establish that the iio_dev is a child of the i2c device */
594 chip
->indio_dev
->name
= id
->name
;
595 chip
->indio_dev
->dev
.parent
= &client
->dev
;
596 chip
->indio_dev
->attrs
= &ad774x_attribute_group
;
597 chip
->indio_dev
->event_attrs
= &ad774x_event_attribute_group
;
598 chip
->indio_dev
->dev_data
= (void *)(chip
);
599 chip
->indio_dev
->driver_module
= THIS_MODULE
;
600 chip
->indio_dev
->num_interrupt_lines
= 1;
601 chip
->indio_dev
->modes
= INDIO_DIRECT_MODE
;
603 ret
= iio_device_register(chip
->indio_dev
);
609 ret
= request_threaded_irq(client
->irq
,
611 &ad774x_event_handler
,
612 IRQF_TRIGGER_FALLING
,
619 dev_err(&client
->dev
, "%s capacitive sensor registered, irq: %d\n", id
->name
, client
->irq
);
625 free_irq(client
->irq
, chip
->indio_dev
);
627 iio_free_device(chip
->indio_dev
);
634 static int __devexit
ad774x_remove(struct i2c_client
*client
)
636 struct ad774x_chip_info
*chip
= i2c_get_clientdata(client
);
637 struct iio_dev
*indio_dev
= chip
->indio_dev
;
640 free_irq(client
->irq
, indio_dev
);
641 iio_device_unregister(indio_dev
);
647 static const struct i2c_device_id ad774x_id
[] = {
654 MODULE_DEVICE_TABLE(i2c
, ad774x_id
);
656 static struct i2c_driver ad774x_driver
= {
660 .probe
= ad774x_probe
,
661 .remove
= __devexit_p(ad774x_remove
),
662 .id_table
= ad774x_id
,
665 static __init
int ad774x_init(void)
667 return i2c_add_driver(&ad774x_driver
);
670 static __exit
void ad774x_exit(void)
672 i2c_del_driver(&ad774x_driver
);
675 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
676 MODULE_DESCRIPTION("Analog Devices ad7745/6/7 capacitive sensor driver");
677 MODULE_LICENSE("GPL v2");
679 module_init(ad774x_init
);
680 module_exit(ad774x_exit
);