1 // SPDX-License-Identifier: GPL-2.0+
3 * AD7150 capacitive sensor driver supporting AD7150/1/6
5 * Copyright 2010-2011 Analog Devices Inc.
8 #include <linux/bitfield.h>
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/irq.h>
15 #include <linux/module.h>
17 #include <linux/iio/iio.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/events.h>
21 * AD7150 registers definition
24 #define AD7150_STATUS 0
25 #define AD7150_STATUS_OUT1 BIT(3)
26 #define AD7150_STATUS_OUT2 BIT(5)
27 #define AD7150_CH1_DATA_HIGH 1
28 #define AD7150_CH2_DATA_HIGH 3
29 #define AD7150_CH1_AVG_HIGH 5
30 #define AD7150_CH2_AVG_HIGH 7
31 #define AD7150_CH1_SENSITIVITY 9
32 #define AD7150_CH1_THR_HOLD_H 9
33 #define AD7150_CH1_TIMEOUT 10
34 #define AD7150_CH1_SETUP 11
35 #define AD7150_CH2_SENSITIVITY 12
36 #define AD7150_CH2_THR_HOLD_H 12
37 #define AD7150_CH2_TIMEOUT 13
38 #define AD7150_CH2_SETUP 14
40 #define AD7150_CFG_FIX BIT(7)
41 #define AD7150_PD_TIMER 16
42 #define AD7150_CH1_CAPDAC 17
43 #define AD7150_CH2_CAPDAC 18
51 #define AD7150_THRESHTYPE_MSK GENMASK(6, 5)
53 #define AD7150_CH_TIMEOUT_RECEDING GENMASK(3, 0)
54 #define AD7150_CH_TIMEOUT_APPROACHING GENMASK(7, 4)
62 * struct ad7150_chip_info - instance specific chip data
63 * @client: i2c client for this device
64 * @threshold: thresholds for simple capacitance value events
65 * @thresh_sensitivity: threshold for simple capacitance offset
66 * from 'average' value.
67 * @thresh_timeout: a timeout, in samples from the moment an
68 * adaptive threshold event occurs to when the average
69 * value jumps to current value. Note made up of two fields,
70 * 3:0 are for timeout receding - applies if below lower threshold
71 * 7:4 are for timeout approaching - applies if above upper threshold
72 * @state_lock: ensure consistent state of this structure wrt the
74 * @interrupts: one or two interrupt numbers depending on device type.
75 * @int_enabled: is a given interrupt currently enabled.
76 * @type: threshold type
77 * @dir: threshold direction
79 struct ad7150_chip_info {
80 struct i2c_client *client;
82 u8 thresh_sensitivity[2][2];
83 u8 thresh_timeout[2][2];
84 struct mutex state_lock;
87 enum iio_event_type type;
88 enum iio_event_direction dir;
95 static const u8 ad7150_addresses[][6] = {
96 { AD7150_CH1_DATA_HIGH, AD7150_CH1_AVG_HIGH,
97 AD7150_CH1_SETUP, AD7150_CH1_THR_HOLD_H,
98 AD7150_CH1_SENSITIVITY, AD7150_CH1_TIMEOUT },
99 { AD7150_CH2_DATA_HIGH, AD7150_CH2_AVG_HIGH,
100 AD7150_CH2_SETUP, AD7150_CH2_THR_HOLD_H,
101 AD7150_CH2_SENSITIVITY, AD7150_CH2_TIMEOUT },
104 static int ad7150_read_raw(struct iio_dev *indio_dev,
105 struct iio_chan_spec const *chan,
111 struct ad7150_chip_info *chip = iio_priv(indio_dev);
112 int channel = chan->channel;
115 case IIO_CHAN_INFO_RAW:
116 ret = i2c_smbus_read_word_swapped(chip->client,
117 ad7150_addresses[channel][0]);
123 case IIO_CHAN_INFO_AVERAGE_RAW:
124 ret = i2c_smbus_read_word_swapped(chip->client,
125 ad7150_addresses[channel][1]);
131 case IIO_CHAN_INFO_SAMP_FREQ:
132 /* Strangely same for both 1 and 2 chan parts */
140 static int ad7150_read_event_config(struct iio_dev *indio_dev,
141 const struct iio_chan_spec *chan,
142 enum iio_event_type type,
143 enum iio_event_direction dir)
148 struct ad7150_chip_info *chip = iio_priv(indio_dev);
150 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
154 threshtype = FIELD_GET(AD7150_THRESHTYPE_MSK, ret);
156 /*check if threshold mode is fixed or adaptive*/
157 thrfixed = FIELD_GET(AD7150_CFG_FIX, ret);
160 case IIO_EV_TYPE_THRESH_ADAPTIVE:
161 if (dir == IIO_EV_DIR_RISING)
162 return !thrfixed && (threshtype == 0x1);
163 return !thrfixed && (threshtype == 0x0);
164 case IIO_EV_TYPE_THRESH:
165 if (dir == IIO_EV_DIR_RISING)
166 return thrfixed && (threshtype == 0x1);
167 return thrfixed && (threshtype == 0x0);
174 /* state_lock should be held to ensure consistent state*/
176 static int ad7150_write_event_params(struct iio_dev *indio_dev,
178 enum iio_event_type type,
179 enum iio_event_direction dir)
181 struct ad7150_chip_info *chip = iio_priv(indio_dev);
182 int rising = (dir == IIO_EV_DIR_RISING);
184 /* Only update value live, if parameter is in use */
185 if ((type != chip->type) || (dir != chip->dir))
189 /* Note completely different from the adaptive versions */
190 case IIO_EV_TYPE_THRESH: {
191 u16 value = chip->threshold[rising][chan];
192 return i2c_smbus_write_word_swapped(chip->client,
193 ad7150_addresses[chan][3],
196 case IIO_EV_TYPE_THRESH_ADAPTIVE: {
200 sens = chip->thresh_sensitivity[rising][chan];
201 ret = i2c_smbus_write_byte_data(chip->client,
202 ad7150_addresses[chan][4],
208 * Single timeout register contains timeouts for both
211 timeout = FIELD_PREP(AD7150_CH_TIMEOUT_APPROACHING,
212 chip->thresh_timeout[1][chan]);
213 timeout |= FIELD_PREP(AD7150_CH_TIMEOUT_RECEDING,
214 chip->thresh_timeout[0][chan]);
215 return i2c_smbus_write_byte_data(chip->client,
216 ad7150_addresses[chan][5],
224 static int ad7150_write_event_config(struct iio_dev *indio_dev,
225 const struct iio_chan_spec *chan,
226 enum iio_event_type type,
227 enum iio_event_direction dir, int state)
229 u8 thresh_type, cfg, adaptive;
231 struct ad7150_chip_info *chip = iio_priv(indio_dev);
232 int rising = (dir == IIO_EV_DIR_RISING);
235 * There is only a single shared control and no on chip
236 * interrupt disables for the two interrupt lines.
237 * So, enabling will switch the events configured to enable
238 * whatever was most recently requested and if necessary enable_irq()
239 * the interrupt and any disable will disable_irq() for that
240 * channels interrupt.
243 if ((chip->int_enabled[chan->channel]) &&
244 (type == chip->type) && (dir == chip->dir)) {
245 disable_irq(chip->interrupts[chan->channel]);
246 chip->int_enabled[chan->channel] = false;
251 mutex_lock(&chip->state_lock);
252 if ((type != chip->type) || (dir != chip->dir)) {
255 * Need to temporarily disable both interrupts if
256 * enabled - this is to avoid races around changing
257 * config and thresholds.
258 * Note enable/disable_irq() are reference counted so
259 * no need to check if already enabled.
261 disable_irq(chip->interrupts[0]);
262 disable_irq(chip->interrupts[1]);
264 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
268 cfg = ret & ~((0x03 << 5) | BIT(7));
271 case IIO_EV_TYPE_THRESH_ADAPTIVE:
278 case IIO_EV_TYPE_THRESH:
290 cfg |= (!adaptive << 7) | (thresh_type << 5);
292 ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
297 * There is a potential race condition here, but not easy
298 * to close given we can't disable the interrupt at the
299 * chip side of things. Rely on the status bit.
304 /* update control attributes */
305 ret = ad7150_write_event_params(indio_dev, chan->channel, type,
309 /* reenable any irq's we disabled whilst changing mode */
310 enable_irq(chip->interrupts[0]);
311 enable_irq(chip->interrupts[1]);
313 if (!chip->int_enabled[chan->channel]) {
314 enable_irq(chip->interrupts[chan->channel]);
315 chip->int_enabled[chan->channel] = true;
319 mutex_unlock(&chip->state_lock);
324 static int ad7150_read_event_value(struct iio_dev *indio_dev,
325 const struct iio_chan_spec *chan,
326 enum iio_event_type type,
327 enum iio_event_direction dir,
328 enum iio_event_info info,
331 struct ad7150_chip_info *chip = iio_priv(indio_dev);
332 int rising = (dir == IIO_EV_DIR_RISING);
334 /* Complex register sharing going on here */
336 case IIO_EV_INFO_VALUE:
338 case IIO_EV_TYPE_THRESH_ADAPTIVE:
339 *val = chip->thresh_sensitivity[rising][chan->channel];
341 case IIO_EV_TYPE_THRESH:
342 *val = chip->threshold[rising][chan->channel];
347 case IIO_EV_INFO_TIMEOUT:
349 *val2 = chip->thresh_timeout[rising][chan->channel] * 10000;
350 return IIO_VAL_INT_PLUS_MICRO;
356 static int ad7150_write_event_value(struct iio_dev *indio_dev,
357 const struct iio_chan_spec *chan,
358 enum iio_event_type type,
359 enum iio_event_direction dir,
360 enum iio_event_info info,
364 struct ad7150_chip_info *chip = iio_priv(indio_dev);
365 int rising = (dir == IIO_EV_DIR_RISING);
367 mutex_lock(&chip->state_lock);
369 case IIO_EV_INFO_VALUE:
371 case IIO_EV_TYPE_THRESH_ADAPTIVE:
372 chip->thresh_sensitivity[rising][chan->channel] = val;
374 case IIO_EV_TYPE_THRESH:
375 chip->threshold[rising][chan->channel] = val;
382 case IIO_EV_INFO_TIMEOUT: {
384 * Raw timeout is in cycles of 10 msecs as long as both
385 * channels are enabled.
386 * In terms of INT_PLUS_MICRO, that is in units of 10,000
388 int timeout = val2 / 10000;
390 if (val != 0 || timeout < 0 || timeout > 15 || val2 % 10000) {
395 chip->thresh_timeout[rising][chan->channel] = timeout;
403 /* write back if active */
404 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
407 mutex_unlock(&chip->state_lock);
411 static const struct iio_event_spec ad7150_events[] = {
413 .type = IIO_EV_TYPE_THRESH,
414 .dir = IIO_EV_DIR_RISING,
415 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
416 BIT(IIO_EV_INFO_ENABLE),
418 .type = IIO_EV_TYPE_THRESH,
419 .dir = IIO_EV_DIR_FALLING,
420 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
421 BIT(IIO_EV_INFO_ENABLE),
423 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
424 .dir = IIO_EV_DIR_RISING,
425 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
426 BIT(IIO_EV_INFO_ENABLE) |
427 BIT(IIO_EV_INFO_TIMEOUT),
429 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
430 .dir = IIO_EV_DIR_FALLING,
431 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
432 BIT(IIO_EV_INFO_ENABLE) |
433 BIT(IIO_EV_INFO_TIMEOUT),
437 #define AD7150_CAPACITANCE_CHAN(_chan) { \
438 .type = IIO_CAPACITANCE, \
441 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
442 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
443 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
444 .event_spec = ad7150_events, \
445 .num_event_specs = ARRAY_SIZE(ad7150_events), \
448 #define AD7150_CAPACITANCE_CHAN_NO_IRQ(_chan) { \
449 .type = IIO_CAPACITANCE, \
452 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
453 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
454 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
457 static const struct iio_chan_spec ad7150_channels[] = {
458 AD7150_CAPACITANCE_CHAN(0),
459 AD7150_CAPACITANCE_CHAN(1),
462 static const struct iio_chan_spec ad7150_channels_no_irq[] = {
463 AD7150_CAPACITANCE_CHAN_NO_IRQ(0),
464 AD7150_CAPACITANCE_CHAN_NO_IRQ(1),
467 static const struct iio_chan_spec ad7151_channels[] = {
468 AD7150_CAPACITANCE_CHAN(0),
471 static const struct iio_chan_spec ad7151_channels_no_irq[] = {
472 AD7150_CAPACITANCE_CHAN_NO_IRQ(0),
475 static irqreturn_t __ad7150_event_handler(void *private, u8 status_mask,
478 struct iio_dev *indio_dev = private;
479 struct ad7150_chip_info *chip = iio_priv(indio_dev);
480 s64 timestamp = iio_get_time_ns(indio_dev);
483 int_status = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
487 if (!(int_status & status_mask))
490 iio_push_event(indio_dev,
491 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, channel,
492 chip->type, chip->dir),
498 static irqreturn_t ad7150_event_handler_ch1(int irq, void *private)
500 return __ad7150_event_handler(private, AD7150_STATUS_OUT1, 0);
503 static irqreturn_t ad7150_event_handler_ch2(int irq, void *private)
505 return __ad7150_event_handler(private, AD7150_STATUS_OUT2, 1);
508 static IIO_CONST_ATTR(in_capacitance_thresh_adaptive_timeout_available,
511 static struct attribute *ad7150_event_attributes[] = {
512 &iio_const_attr_in_capacitance_thresh_adaptive_timeout_available
517 static const struct attribute_group ad7150_event_attribute_group = {
518 .attrs = ad7150_event_attributes,
522 static const struct iio_info ad7150_info = {
523 .event_attrs = &ad7150_event_attribute_group,
524 .read_raw = &ad7150_read_raw,
525 .read_event_config = &ad7150_read_event_config,
526 .write_event_config = &ad7150_write_event_config,
527 .read_event_value = &ad7150_read_event_value,
528 .write_event_value = &ad7150_write_event_value,
531 static const struct iio_info ad7150_info_no_irq = {
532 .read_raw = &ad7150_read_raw,
535 static int ad7150_probe(struct i2c_client *client,
536 const struct i2c_device_id *id)
539 struct ad7150_chip_info *chip;
540 struct iio_dev *indio_dev;
542 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
546 chip = iio_priv(indio_dev);
547 mutex_init(&chip->state_lock);
548 chip->client = client;
550 indio_dev->name = id->name;
552 indio_dev->modes = INDIO_DIRECT_MODE;
554 chip->interrupts[0] = fwnode_irq_get(dev_fwnode(&client->dev), 0);
555 if (chip->interrupts[0] < 0)
556 return chip->interrupts[0];
557 if (id->driver_data == AD7150) {
558 chip->interrupts[1] = fwnode_irq_get(dev_fwnode(&client->dev), 1);
559 if (chip->interrupts[1] < 0)
560 return chip->interrupts[1];
562 if (chip->interrupts[0] &&
563 (id->driver_data == AD7151 || chip->interrupts[1])) {
564 irq_set_status_flags(chip->interrupts[0], IRQ_NOAUTOEN);
565 ret = devm_request_threaded_irq(&client->dev,
568 &ad7150_event_handler_ch1,
569 IRQF_TRIGGER_RISING |
576 indio_dev->info = &ad7150_info;
577 switch (id->driver_data) {
579 indio_dev->channels = ad7150_channels;
580 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
581 irq_set_status_flags(chip->interrupts[1], IRQ_NOAUTOEN);
582 ret = devm_request_threaded_irq(&client->dev,
585 &ad7150_event_handler_ch2,
586 IRQF_TRIGGER_RISING |
594 indio_dev->channels = ad7151_channels;
595 indio_dev->num_channels = ARRAY_SIZE(ad7151_channels);
602 indio_dev->info = &ad7150_info_no_irq;
603 switch (id->driver_data) {
605 indio_dev->channels = ad7150_channels_no_irq;
606 indio_dev->num_channels =
607 ARRAY_SIZE(ad7150_channels_no_irq);
610 indio_dev->channels = ad7151_channels_no_irq;
611 indio_dev->num_channels =
612 ARRAY_SIZE(ad7151_channels_no_irq);
619 return devm_iio_device_register(indio_dev->dev.parent, indio_dev);
622 static const struct i2c_device_id ad7150_id[] = {
623 { "ad7150", AD7150 },
624 { "ad7151", AD7151 },
625 { "ad7156", AD7150 },
629 MODULE_DEVICE_TABLE(i2c, ad7150_id);
631 static struct i2c_driver ad7150_driver = {
635 .probe = ad7150_probe,
636 .id_table = ad7150_id,
638 module_i2c_driver(ad7150_driver);
640 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
641 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
642 MODULE_LICENSE("GPL v2");