2 * AD7150 capacitive sensor driver supporting AD7150/1/6
4 * Copyright 2010-2011 Analog Devices Inc.
6 * Licensed under the GPL-2 or later.
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
18 #include "../events.h"
20 * AD7150 registers definition
23 #define AD7150_STATUS 0
24 #define AD7150_STATUS_OUT1 (1 << 3)
25 #define AD7150_STATUS_OUT2 (1 << 5)
26 #define AD7150_CH1_DATA_HIGH 1
27 #define AD7150_CH2_DATA_HIGH 3
28 #define AD7150_CH1_AVG_HIGH 5
29 #define AD7150_CH2_AVG_HIGH 7
30 #define AD7150_CH1_SENSITIVITY 9
31 #define AD7150_CH1_THR_HOLD_H 9
32 #define AD7150_CH1_TIMEOUT 10
33 #define AD7150_CH1_SETUP 11
34 #define AD7150_CH2_SENSITIVITY 12
35 #define AD7150_CH2_THR_HOLD_H 12
36 #define AD7150_CH2_TIMEOUT 13
37 #define AD7150_CH2_SETUP 14
39 #define AD7150_CFG_FIX (1 << 7)
40 #define AD7150_PD_TIMER 16
41 #define AD7150_CH1_CAPDAC 17
42 #define AD7150_CH2_CAPDAC 18
50 * struct ad7150_chip_info - instance specific chip data
51 * @client: i2c client for this device
52 * @current_event: device always has one type of event enabled.
53 * This element stores the event code of the current one.
54 * @threshold: thresholds for simple capacitance value events
55 * @thresh_sensitivity: threshold for simple capacitance offset
56 * from 'average' value.
57 * @mag_sensitity: threshold for magnitude of capacitance offset from
58 * from 'average' value.
59 * @thresh_timeout: a timeout, in samples from the moment an
60 * adaptive threshold event occurs to when the average
61 * value jumps to current value.
62 * @mag_timeout: a timeout, in sample from the moment an
63 * adaptive magnitude event occurs to when the average
64 * value jumps to the current value.
65 * @old_state: store state from previous event, allowing confirmation
67 * @conversion_mode: the current conversion mode.
68 * @state_lock: ensure consistent state of this structure wrt the
71 struct ad7150_chip_info
{
72 struct i2c_client
*client
;
75 u8 thresh_sensitivity
[2][2];
76 u8 mag_sensitivity
[2][2];
77 u8 thresh_timeout
[2][2];
80 char *conversion_mode
;
81 struct mutex state_lock
;
88 static const u8 ad7150_addresses
[][6] = {
89 { AD7150_CH1_DATA_HIGH
, AD7150_CH1_AVG_HIGH
,
90 AD7150_CH1_SETUP
, AD7150_CH1_THR_HOLD_H
,
91 AD7150_CH1_SENSITIVITY
, AD7150_CH1_TIMEOUT
},
92 { AD7150_CH2_DATA_HIGH
, AD7150_CH2_AVG_HIGH
,
93 AD7150_CH2_SETUP
, AD7150_CH2_THR_HOLD_H
,
94 AD7150_CH2_SENSITIVITY
, AD7150_CH2_TIMEOUT
},
97 static int ad7150_read_raw(struct iio_dev
*indio_dev
,
98 struct iio_chan_spec
const *chan
,
104 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
108 ret
= i2c_smbus_read_word_data(chip
->client
,
109 ad7150_addresses
[chan
->channel
][0]);
114 case IIO_CHAN_INFO_AVERAGE_RAW
:
115 ret
= i2c_smbus_read_word_data(chip
->client
,
116 ad7150_addresses
[chan
->channel
][1]);
126 static int ad7150_read_event_config(struct iio_dev
*indio_dev
, u64 event_code
)
131 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
132 int rising
= !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
135 ret
= i2c_smbus_read_byte_data(chip
->client
, AD7150_CFG
);
139 threshtype
= (ret
>> 5) & 0x03;
140 adaptive
= !!(ret
& 0x80);
142 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code
)) {
143 case IIO_EV_TYPE_MAG_ADAPTIVE
:
145 return adaptive
&& (threshtype
== 0x1);
147 return adaptive
&& (threshtype
== 0x0);
148 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
150 return adaptive
&& (threshtype
== 0x3);
152 return adaptive
&& (threshtype
== 0x2);
154 case IIO_EV_TYPE_THRESH
:
156 return !adaptive
&& (threshtype
== 0x1);
158 return !adaptive
&& (threshtype
== 0x0);
163 /* lock should be held */
164 static int ad7150_write_event_params(struct iio_dev
*indio_dev
, u64 event_code
)
169 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
170 int chan
= IIO_EVENT_CODE_EXTRACT_NUM(event_code
);
171 int rising
= !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
174 if (event_code
!= chip
->current_event
)
177 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code
)) {
178 /* Note completely different from the adaptive versions */
179 case IIO_EV_TYPE_THRESH
:
180 value
= chip
->threshold
[rising
][chan
];
181 ret
= i2c_smbus_write_word_data(chip
->client
,
182 ad7150_addresses
[chan
][3],
187 case IIO_EV_TYPE_MAG_ADAPTIVE
:
188 sens
= chip
->mag_sensitivity
[rising
][chan
];
189 timeout
= chip
->mag_timeout
[rising
][chan
];
191 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
192 sens
= chip
->thresh_sensitivity
[rising
][chan
];
193 timeout
= chip
->thresh_timeout
[rising
][chan
];
198 ret
= i2c_smbus_write_byte_data(chip
->client
,
199 ad7150_addresses
[chan
][4],
204 ret
= i2c_smbus_write_byte_data(chip
->client
,
205 ad7150_addresses
[chan
][5],
213 static int ad7150_write_event_config(struct iio_dev
*indio_dev
,
214 u64 event_code
, int state
)
216 u8 thresh_type
, cfg
, adaptive
;
218 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
219 int rising
= !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
222 /* Something must always be turned on */
226 if (event_code
== chip
->current_event
)
228 mutex_lock(&chip
->state_lock
);
229 ret
= i2c_smbus_read_byte_data(chip
->client
, AD7150_CFG
);
233 cfg
= ret
& ~((0x03 << 5) | (0x1 << 7));
235 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code
)) {
236 case IIO_EV_TYPE_MAG_ADAPTIVE
:
243 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
250 case IIO_EV_TYPE_THRESH
:
262 cfg
|= (!adaptive
<< 7) | (thresh_type
<< 5);
264 ret
= i2c_smbus_write_byte_data(chip
->client
, AD7150_CFG
, cfg
);
268 chip
->current_event
= event_code
;
270 /* update control attributes */
271 ret
= ad7150_write_event_params(indio_dev
, event_code
);
273 mutex_unlock(&chip
->state_lock
);
278 static int ad7150_read_event_value(struct iio_dev
*indio_dev
,
282 int chan
= IIO_EVENT_CODE_EXTRACT_NUM(event_code
);
283 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
284 int rising
= !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
287 /* Complex register sharing going on here */
288 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code
)) {
289 case IIO_EV_TYPE_MAG_ADAPTIVE
:
290 *val
= chip
->mag_sensitivity
[rising
][chan
];
293 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
294 *val
= chip
->thresh_sensitivity
[rising
][chan
];
297 case IIO_EV_TYPE_THRESH
:
298 *val
= chip
->threshold
[rising
][chan
];
306 static int ad7150_write_event_value(struct iio_dev
*indio_dev
,
311 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
312 int chan
= IIO_EVENT_CODE_EXTRACT_NUM(event_code
);
313 int rising
= !!(IIO_EVENT_CODE_EXTRACT_DIR(event_code
) ==
316 mutex_lock(&chip
->state_lock
);
317 switch (IIO_EVENT_CODE_EXTRACT_TYPE(event_code
)) {
318 case IIO_EV_TYPE_MAG_ADAPTIVE
:
319 chip
->mag_sensitivity
[rising
][chan
] = val
;
321 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
322 chip
->thresh_sensitivity
[rising
][chan
] = val
;
324 case IIO_EV_TYPE_THRESH
:
325 chip
->threshold
[rising
][chan
] = val
;
332 /* write back if active */
333 ret
= ad7150_write_event_params(indio_dev
, event_code
);
336 mutex_unlock(&chip
->state_lock
);
340 static ssize_t
ad7150_show_timeout(struct device
*dev
,
341 struct device_attribute
*attr
,
344 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
345 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
346 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
349 /* use the event code for consistency reasons */
350 int chan
= IIO_EVENT_CODE_EXTRACT_NUM(this_attr
->address
);
351 int rising
= !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr
->address
)
352 == IIO_EV_DIR_RISING
);
354 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr
->address
)) {
355 case IIO_EV_TYPE_MAG_ADAPTIVE
:
356 value
= chip
->mag_timeout
[rising
][chan
];
358 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
359 value
= chip
->thresh_timeout
[rising
][chan
];
365 return sprintf(buf
, "%d\n", value
);
368 static ssize_t
ad7150_store_timeout(struct device
*dev
,
369 struct device_attribute
*attr
,
373 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
374 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
375 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
376 int chan
= IIO_EVENT_CODE_EXTRACT_NUM(this_attr
->address
);
377 int rising
= !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr
->address
) ==
382 ret
= kstrtou8(buf
, 10, &data
);
386 mutex_lock(&chip
->state_lock
);
387 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr
->address
)) {
388 case IIO_EV_TYPE_MAG_ADAPTIVE
:
389 chip
->mag_timeout
[rising
][chan
] = data
;
391 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
392 chip
->thresh_timeout
[rising
][chan
] = data
;
399 ret
= ad7150_write_event_params(indio_dev
, this_attr
->address
);
401 mutex_unlock(&chip
->state_lock
);
409 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
410 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
412 &ad7150_show_timeout, \
413 &ad7150_store_timeout, \
414 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
416 IIO_EV_TYPE_##ev_type, \
417 IIO_EV_DIR_##ev_dir))
418 static AD7150_TIMEOUT(0, mag_adaptive
, rising
, MAG_ADAPTIVE
, RISING
);
419 static AD7150_TIMEOUT(0, mag_adaptive
, falling
, MAG_ADAPTIVE
, FALLING
);
420 static AD7150_TIMEOUT(1, mag_adaptive
, rising
, MAG_ADAPTIVE
, RISING
);
421 static AD7150_TIMEOUT(1, mag_adaptive
, falling
, MAG_ADAPTIVE
, FALLING
);
422 static AD7150_TIMEOUT(0, thresh_adaptive
, rising
, THRESH_ADAPTIVE
, RISING
);
423 static AD7150_TIMEOUT(0, thresh_adaptive
, falling
, THRESH_ADAPTIVE
, FALLING
);
424 static AD7150_TIMEOUT(1, thresh_adaptive
, rising
, THRESH_ADAPTIVE
, RISING
);
425 static AD7150_TIMEOUT(1, thresh_adaptive
, falling
, THRESH_ADAPTIVE
, FALLING
);
427 static const struct iio_chan_spec ad7150_channels
[] = {
429 .type
= IIO_CAPACITANCE
,
432 .info_mask
= IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE_BIT
,
434 IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_RISING
) |
435 IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_FALLING
) |
436 IIO_EV_BIT(IIO_EV_TYPE_THRESH_ADAPTIVE
, IIO_EV_DIR_RISING
) |
437 IIO_EV_BIT(IIO_EV_TYPE_THRESH_ADAPTIVE
, IIO_EV_DIR_FALLING
) |
438 IIO_EV_BIT(IIO_EV_TYPE_MAG_ADAPTIVE
, IIO_EV_DIR_RISING
) |
439 IIO_EV_BIT(IIO_EV_TYPE_MAG_ADAPTIVE
, IIO_EV_DIR_FALLING
)
441 .type
= IIO_CAPACITANCE
,
444 .info_mask
= IIO_CHAN_INFO_AVERAGE_RAW_SEPARATE_BIT
,
446 IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_RISING
) |
447 IIO_EV_BIT(IIO_EV_TYPE_THRESH
, IIO_EV_DIR_FALLING
) |
448 IIO_EV_BIT(IIO_EV_TYPE_THRESH_ADAPTIVE
, IIO_EV_DIR_RISING
) |
449 IIO_EV_BIT(IIO_EV_TYPE_THRESH_ADAPTIVE
, IIO_EV_DIR_FALLING
) |
450 IIO_EV_BIT(IIO_EV_TYPE_MAG_ADAPTIVE
, IIO_EV_DIR_RISING
) |
451 IIO_EV_BIT(IIO_EV_TYPE_MAG_ADAPTIVE
, IIO_EV_DIR_FALLING
)
459 static irqreturn_t
ad7150_event_handler(int irq
, void *private)
461 struct iio_dev
*indio_dev
= private;
462 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
464 s64 timestamp
= iio_get_time_ns();
467 ret
= i2c_smbus_read_byte_data(chip
->client
, AD7150_STATUS
);
473 if ((int_status
& AD7150_STATUS_OUT1
) &&
474 !(chip
->old_state
& AD7150_STATUS_OUT1
))
475 iio_push_event(indio_dev
,
476 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
,
481 else if ((!(int_status
& AD7150_STATUS_OUT1
)) &&
482 (chip
->old_state
& AD7150_STATUS_OUT1
))
483 iio_push_event(indio_dev
,
484 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
,
490 if ((int_status
& AD7150_STATUS_OUT2
) &&
491 !(chip
->old_state
& AD7150_STATUS_OUT2
))
492 iio_push_event(indio_dev
,
493 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
,
498 else if ((!(int_status
& AD7150_STATUS_OUT2
)) &&
499 (chip
->old_state
& AD7150_STATUS_OUT2
))
500 iio_push_event(indio_dev
,
501 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
,
506 /* store the status to avoid repushing same events */
507 chip
->old_state
= int_status
;
512 /* Timeouts not currently handled by core */
513 static struct attribute
*ad7150_event_attributes
[] = {
514 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
516 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
518 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
520 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
522 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
524 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
526 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
528 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
533 static struct attribute_group ad7150_event_attribute_group
= {
534 .attrs
= ad7150_event_attributes
,
538 static const struct iio_info ad7150_info
= {
539 .event_attrs
= &ad7150_event_attribute_group
,
540 .driver_module
= THIS_MODULE
,
541 .read_raw
= &ad7150_read_raw
,
542 .read_event_config
= &ad7150_read_event_config
,
543 .write_event_config
= &ad7150_write_event_config
,
544 .read_event_value
= &ad7150_read_event_value
,
545 .write_event_value
= &ad7150_write_event_value
,
549 * device probe and remove
552 static int __devinit
ad7150_probe(struct i2c_client
*client
,
553 const struct i2c_device_id
*id
)
556 struct ad7150_chip_info
*chip
;
557 struct iio_dev
*indio_dev
;
559 indio_dev
= iio_allocate_device(sizeof(*chip
));
560 if (indio_dev
== NULL
) {
564 chip
= iio_priv(indio_dev
);
565 mutex_init(&chip
->state_lock
);
566 /* this is only used for device removal purposes */
567 i2c_set_clientdata(client
, indio_dev
);
569 chip
->client
= client
;
571 indio_dev
->name
= id
->name
;
572 indio_dev
->channels
= ad7150_channels
;
573 indio_dev
->num_channels
= ARRAY_SIZE(ad7150_channels
);
574 /* Establish that the iio_dev is a child of the i2c device */
575 indio_dev
->dev
.parent
= &client
->dev
;
577 indio_dev
->info
= &ad7150_info
;
579 indio_dev
->modes
= INDIO_DIRECT_MODE
;
582 ret
= request_threaded_irq(client
->irq
,
584 &ad7150_event_handler
,
585 IRQF_TRIGGER_RISING
|
586 IRQF_TRIGGER_FALLING
,
593 if (client
->dev
.platform_data
) {
594 ret
= request_threaded_irq(*(unsigned int *)
595 client
->dev
.platform_data
,
597 &ad7150_event_handler
,
598 IRQF_TRIGGER_RISING
|
599 IRQF_TRIGGER_FALLING
,
606 ret
= iio_device_register(indio_dev
);
608 goto error_free_irq2
;
610 dev_info(&client
->dev
, "%s capacitive sensor registered,irq: %d\n",
611 id
->name
, client
->irq
);
615 if (client
->dev
.platform_data
)
616 free_irq(*(unsigned int *)client
->dev
.platform_data
,
620 free_irq(client
->irq
, indio_dev
);
622 iio_free_device(indio_dev
);
627 static int __devexit
ad7150_remove(struct i2c_client
*client
)
629 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
631 iio_device_unregister(indio_dev
);
633 free_irq(client
->irq
, indio_dev
);
635 if (client
->dev
.platform_data
)
636 free_irq(*(unsigned int *)client
->dev
.platform_data
, indio_dev
);
638 iio_free_device(indio_dev
);
643 static const struct i2c_device_id ad7150_id
[] = {
650 MODULE_DEVICE_TABLE(i2c
, ad7150_id
);
652 static struct i2c_driver ad7150_driver
= {
656 .probe
= ad7150_probe
,
657 .remove
= __devexit_p(ad7150_remove
),
658 .id_table
= ad7150_id
,
660 module_i2c_driver(ad7150_driver
);
662 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
663 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
664 MODULE_LICENSE("GPL v2");