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>
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/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
);
107 case IIO_CHAN_INFO_RAW
:
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
,
127 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
128 enum iio_event_direction dir
)
133 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
135 ret
= i2c_smbus_read_byte_data(chip
->client
, AD7150_CFG
);
139 threshtype
= (ret
>> 5) & 0x03;
140 adaptive
= !!(ret
& 0x80);
143 case IIO_EV_TYPE_MAG_ADAPTIVE
:
144 if (dir
== IIO_EV_DIR_RISING
)
145 return adaptive
&& (threshtype
== 0x1);
147 return adaptive
&& (threshtype
== 0x0);
148 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
149 if (dir
== IIO_EV_DIR_RISING
)
150 return adaptive
&& (threshtype
== 0x3);
152 return adaptive
&& (threshtype
== 0x2);
154 case IIO_EV_TYPE_THRESH
:
155 if (dir
== IIO_EV_DIR_RISING
)
156 return !adaptive
&& (threshtype
== 0x1);
158 return !adaptive
&& (threshtype
== 0x0);
165 /* lock should be held */
166 static int ad7150_write_event_params(struct iio_dev
*indio_dev
,
167 unsigned int chan
, enum iio_event_type type
,
168 enum iio_event_direction dir
)
173 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
174 int rising
= (dir
== IIO_EV_DIR_RISING
);
177 event_code
= IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
, chan
, type
, dir
);
179 if (event_code
!= chip
->current_event
)
183 /* Note completely different from the adaptive versions */
184 case IIO_EV_TYPE_THRESH
:
185 value
= chip
->threshold
[rising
][chan
];
186 ret
= i2c_smbus_write_word_data(chip
->client
,
187 ad7150_addresses
[chan
][3],
192 case IIO_EV_TYPE_MAG_ADAPTIVE
:
193 sens
= chip
->mag_sensitivity
[rising
][chan
];
194 timeout
= chip
->mag_timeout
[rising
][chan
];
196 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
197 sens
= chip
->thresh_sensitivity
[rising
][chan
];
198 timeout
= chip
->thresh_timeout
[rising
][chan
];
203 ret
= i2c_smbus_write_byte_data(chip
->client
,
204 ad7150_addresses
[chan
][4],
209 ret
= i2c_smbus_write_byte_data(chip
->client
,
210 ad7150_addresses
[chan
][5],
218 static int ad7150_write_event_config(struct iio_dev
*indio_dev
,
219 const struct iio_chan_spec
*chan
, enum iio_event_type type
,
220 enum iio_event_direction dir
, int state
)
222 u8 thresh_type
, cfg
, adaptive
;
224 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
225 int rising
= (dir
== IIO_EV_DIR_RISING
);
228 /* Something must always be turned on */
232 event_code
= IIO_UNMOD_EVENT_CODE(chan
->type
, chan
->channel
, type
, dir
);
233 if (event_code
== chip
->current_event
)
235 mutex_lock(&chip
->state_lock
);
236 ret
= i2c_smbus_read_byte_data(chip
->client
, AD7150_CFG
);
240 cfg
= ret
& ~((0x03 << 5) | (0x1 << 7));
243 case IIO_EV_TYPE_MAG_ADAPTIVE
:
250 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
257 case IIO_EV_TYPE_THRESH
:
269 cfg
|= (!adaptive
<< 7) | (thresh_type
<< 5);
271 ret
= i2c_smbus_write_byte_data(chip
->client
, AD7150_CFG
, cfg
);
275 chip
->current_event
= event_code
;
277 /* update control attributes */
278 ret
= ad7150_write_event_params(indio_dev
, chan
->channel
, type
, dir
);
280 mutex_unlock(&chip
->state_lock
);
285 static int ad7150_read_event_value(struct iio_dev
*indio_dev
,
286 const struct iio_chan_spec
*chan
,
287 enum iio_event_type type
,
288 enum iio_event_direction dir
,
289 enum iio_event_info info
,
292 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
293 int rising
= (dir
== IIO_EV_DIR_RISING
);
295 /* Complex register sharing going on here */
297 case IIO_EV_TYPE_MAG_ADAPTIVE
:
298 *val
= chip
->mag_sensitivity
[rising
][chan
->channel
];
300 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
301 *val
= chip
->thresh_sensitivity
[rising
][chan
->channel
];
303 case IIO_EV_TYPE_THRESH
:
304 *val
= chip
->threshold
[rising
][chan
->channel
];
311 static int ad7150_write_event_value(struct iio_dev
*indio_dev
,
312 const struct iio_chan_spec
*chan
,
313 enum iio_event_type type
,
314 enum iio_event_direction dir
,
315 enum iio_event_info info
,
319 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
320 int rising
= (dir
== IIO_EV_DIR_RISING
);
322 mutex_lock(&chip
->state_lock
);
324 case IIO_EV_TYPE_MAG_ADAPTIVE
:
325 chip
->mag_sensitivity
[rising
][chan
->channel
] = val
;
327 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
328 chip
->thresh_sensitivity
[rising
][chan
->channel
] = val
;
330 case IIO_EV_TYPE_THRESH
:
331 chip
->threshold
[rising
][chan
->channel
] = val
;
338 /* write back if active */
339 ret
= ad7150_write_event_params(indio_dev
, chan
->channel
, type
, dir
);
342 mutex_unlock(&chip
->state_lock
);
346 static ssize_t
ad7150_show_timeout(struct device
*dev
,
347 struct device_attribute
*attr
,
350 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
351 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
352 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
355 /* use the event code for consistency reasons */
356 int chan
= IIO_EVENT_CODE_EXTRACT_CHAN(this_attr
->address
);
357 int rising
= !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr
->address
)
358 == IIO_EV_DIR_RISING
);
360 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr
->address
)) {
361 case IIO_EV_TYPE_MAG_ADAPTIVE
:
362 value
= chip
->mag_timeout
[rising
][chan
];
364 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
365 value
= chip
->thresh_timeout
[rising
][chan
];
371 return sprintf(buf
, "%d\n", value
);
374 static ssize_t
ad7150_store_timeout(struct device
*dev
,
375 struct device_attribute
*attr
,
379 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
380 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
381 struct iio_dev_attr
*this_attr
= to_iio_dev_attr(attr
);
382 int chan
= IIO_EVENT_CODE_EXTRACT_CHAN(this_attr
->address
);
383 enum iio_event_direction dir
;
384 enum iio_event_type type
;
389 type
= IIO_EVENT_CODE_EXTRACT_TYPE(this_attr
->address
);
390 dir
= IIO_EVENT_CODE_EXTRACT_DIR(this_attr
->address
);
391 rising
= (dir
== IIO_EV_DIR_RISING
);
393 ret
= kstrtou8(buf
, 10, &data
);
397 mutex_lock(&chip
->state_lock
);
399 case IIO_EV_TYPE_MAG_ADAPTIVE
:
400 chip
->mag_timeout
[rising
][chan
] = data
;
402 case IIO_EV_TYPE_THRESH_ADAPTIVE
:
403 chip
->thresh_timeout
[rising
][chan
] = data
;
410 ret
= ad7150_write_event_params(indio_dev
, chan
, type
, dir
);
412 mutex_unlock(&chip
->state_lock
);
420 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
421 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
423 &ad7150_show_timeout, \
424 &ad7150_store_timeout, \
425 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
427 IIO_EV_TYPE_##ev_type, \
428 IIO_EV_DIR_##ev_dir))
429 static AD7150_TIMEOUT(0, mag_adaptive
, rising
, MAG_ADAPTIVE
, RISING
);
430 static AD7150_TIMEOUT(0, mag_adaptive
, falling
, MAG_ADAPTIVE
, FALLING
);
431 static AD7150_TIMEOUT(1, mag_adaptive
, rising
, MAG_ADAPTIVE
, RISING
);
432 static AD7150_TIMEOUT(1, mag_adaptive
, falling
, MAG_ADAPTIVE
, FALLING
);
433 static AD7150_TIMEOUT(0, thresh_adaptive
, rising
, THRESH_ADAPTIVE
, RISING
);
434 static AD7150_TIMEOUT(0, thresh_adaptive
, falling
, THRESH_ADAPTIVE
, FALLING
);
435 static AD7150_TIMEOUT(1, thresh_adaptive
, rising
, THRESH_ADAPTIVE
, RISING
);
436 static AD7150_TIMEOUT(1, thresh_adaptive
, falling
, THRESH_ADAPTIVE
, FALLING
);
438 static const struct iio_event_spec ad7150_events
[] = {
440 .type
= IIO_EV_TYPE_THRESH
,
441 .dir
= IIO_EV_DIR_RISING
,
442 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
443 BIT(IIO_EV_INFO_ENABLE
),
445 .type
= IIO_EV_TYPE_THRESH
,
446 .dir
= IIO_EV_DIR_FALLING
,
447 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
448 BIT(IIO_EV_INFO_ENABLE
),
450 .type
= IIO_EV_TYPE_THRESH_ADAPTIVE
,
451 .dir
= IIO_EV_DIR_RISING
,
452 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
453 BIT(IIO_EV_INFO_ENABLE
),
455 .type
= IIO_EV_TYPE_THRESH_ADAPTIVE
,
456 .dir
= IIO_EV_DIR_FALLING
,
457 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
458 BIT(IIO_EV_INFO_ENABLE
),
460 .type
= IIO_EV_TYPE_MAG_ADAPTIVE
,
461 .dir
= IIO_EV_DIR_RISING
,
462 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
463 BIT(IIO_EV_INFO_ENABLE
),
465 .type
= IIO_EV_TYPE_MAG_ADAPTIVE
,
466 .dir
= IIO_EV_DIR_FALLING
,
467 .mask_separate
= BIT(IIO_EV_INFO_VALUE
) |
468 BIT(IIO_EV_INFO_ENABLE
),
472 static const struct iio_chan_spec ad7150_channels
[] = {
474 .type
= IIO_CAPACITANCE
,
477 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
478 BIT(IIO_CHAN_INFO_AVERAGE_RAW
),
479 .event_spec
= ad7150_events
,
480 .num_event_specs
= ARRAY_SIZE(ad7150_events
),
482 .type
= IIO_CAPACITANCE
,
485 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
486 BIT(IIO_CHAN_INFO_AVERAGE_RAW
),
487 .event_spec
= ad7150_events
,
488 .num_event_specs
= ARRAY_SIZE(ad7150_events
),
496 static irqreturn_t
ad7150_event_handler(int irq
, void *private)
498 struct iio_dev
*indio_dev
= private;
499 struct ad7150_chip_info
*chip
= iio_priv(indio_dev
);
501 s64 timestamp
= iio_get_time_ns();
504 ret
= i2c_smbus_read_byte_data(chip
->client
, AD7150_STATUS
);
510 if ((int_status
& AD7150_STATUS_OUT1
) &&
511 !(chip
->old_state
& AD7150_STATUS_OUT1
))
512 iio_push_event(indio_dev
,
513 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
,
518 else if ((!(int_status
& AD7150_STATUS_OUT1
)) &&
519 (chip
->old_state
& AD7150_STATUS_OUT1
))
520 iio_push_event(indio_dev
,
521 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
,
527 if ((int_status
& AD7150_STATUS_OUT2
) &&
528 !(chip
->old_state
& AD7150_STATUS_OUT2
))
529 iio_push_event(indio_dev
,
530 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
,
535 else if ((!(int_status
& AD7150_STATUS_OUT2
)) &&
536 (chip
->old_state
& AD7150_STATUS_OUT2
))
537 iio_push_event(indio_dev
,
538 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE
,
543 /* store the status to avoid repushing same events */
544 chip
->old_state
= int_status
;
549 /* Timeouts not currently handled by core */
550 static struct attribute
*ad7150_event_attributes
[] = {
551 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
553 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
555 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
557 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
559 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
561 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
563 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
565 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
570 static struct attribute_group ad7150_event_attribute_group
= {
571 .attrs
= ad7150_event_attributes
,
575 static const struct iio_info ad7150_info
= {
576 .event_attrs
= &ad7150_event_attribute_group
,
577 .driver_module
= THIS_MODULE
,
578 .read_raw
= &ad7150_read_raw
,
579 .read_event_config
= &ad7150_read_event_config
,
580 .write_event_config
= &ad7150_write_event_config
,
581 .read_event_value
= &ad7150_read_event_value
,
582 .write_event_value
= &ad7150_write_event_value
,
586 * device probe and remove
589 static int ad7150_probe(struct i2c_client
*client
,
590 const struct i2c_device_id
*id
)
593 struct ad7150_chip_info
*chip
;
594 struct iio_dev
*indio_dev
;
596 indio_dev
= devm_iio_device_alloc(&client
->dev
, sizeof(*chip
));
599 chip
= iio_priv(indio_dev
);
600 mutex_init(&chip
->state_lock
);
601 /* this is only used for device removal purposes */
602 i2c_set_clientdata(client
, indio_dev
);
604 chip
->client
= client
;
606 indio_dev
->name
= id
->name
;
607 indio_dev
->channels
= ad7150_channels
;
608 indio_dev
->num_channels
= ARRAY_SIZE(ad7150_channels
);
609 /* Establish that the iio_dev is a child of the i2c device */
610 indio_dev
->dev
.parent
= &client
->dev
;
612 indio_dev
->info
= &ad7150_info
;
614 indio_dev
->modes
= INDIO_DIRECT_MODE
;
617 ret
= devm_request_threaded_irq(&client
->dev
, client
->irq
,
619 &ad7150_event_handler
,
620 IRQF_TRIGGER_RISING
|
621 IRQF_TRIGGER_FALLING
|
629 if (client
->dev
.platform_data
) {
630 ret
= devm_request_threaded_irq(&client
->dev
, *(unsigned int *)
631 client
->dev
.platform_data
,
633 &ad7150_event_handler
,
634 IRQF_TRIGGER_RISING
|
635 IRQF_TRIGGER_FALLING
|
643 ret
= iio_device_register(indio_dev
);
647 dev_info(&client
->dev
, "%s capacitive sensor registered,irq: %d\n",
648 id
->name
, client
->irq
);
653 static int ad7150_remove(struct i2c_client
*client
)
655 struct iio_dev
*indio_dev
= i2c_get_clientdata(client
);
657 iio_device_unregister(indio_dev
);
662 static const struct i2c_device_id ad7150_id
[] = {
669 MODULE_DEVICE_TABLE(i2c
, ad7150_id
);
671 static struct i2c_driver ad7150_driver
= {
675 .probe
= ad7150_probe
,
676 .remove
= ad7150_remove
,
677 .id_table
= ad7150_id
,
679 module_i2c_driver(ad7150_driver
);
681 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
682 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
683 MODULE_LICENSE("GPL v2");