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/bitfield.h>
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/i2c.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 * struct ad7150_chip_info - instance specific chip data
52 * @client: i2c client for this device
53 * @current_event: device always has one type of event enabled.
54 * This element stores the event code of the current one.
55 * @threshold: thresholds for simple capacitance value events
56 * @thresh_sensitivity: threshold for simple capacitance offset
57 * from 'average' value.
58 * @mag_sensitity: threshold for magnitude of capacitance offset from
59 * from 'average' value.
60 * @thresh_timeout: a timeout, in samples from the moment an
61 * adaptive threshold event occurs to when the average
62 * value jumps to current value.
63 * @mag_timeout: a timeout, in sample from the moment an
64 * adaptive magnitude event occurs to when the average
65 * value jumps to the current value.
66 * @old_state: store state from previous event, allowing confirmation
68 * @conversion_mode: the current conversion mode.
69 * @state_lock: ensure consistent state of this structure wrt the
72 struct ad7150_chip_info {
73 struct i2c_client *client;
76 u8 thresh_sensitivity[2][2];
77 u8 mag_sensitivity[2][2];
78 u8 thresh_timeout[2][2];
81 char *conversion_mode;
82 struct mutex state_lock;
89 static const u8 ad7150_addresses[][6] = {
90 { AD7150_CH1_DATA_HIGH, AD7150_CH1_AVG_HIGH,
91 AD7150_CH1_SETUP, AD7150_CH1_THR_HOLD_H,
92 AD7150_CH1_SENSITIVITY, AD7150_CH1_TIMEOUT },
93 { AD7150_CH2_DATA_HIGH, AD7150_CH2_AVG_HIGH,
94 AD7150_CH2_SETUP, AD7150_CH2_THR_HOLD_H,
95 AD7150_CH2_SENSITIVITY, AD7150_CH2_TIMEOUT },
98 static int ad7150_read_raw(struct iio_dev *indio_dev,
99 struct iio_chan_spec const *chan,
105 struct ad7150_chip_info *chip = iio_priv(indio_dev);
108 case IIO_CHAN_INFO_RAW:
109 ret = i2c_smbus_read_word_data(chip->client,
110 ad7150_addresses[chan->channel][0]);
115 case IIO_CHAN_INFO_AVERAGE_RAW:
116 ret = i2c_smbus_read_word_data(chip->client,
117 ad7150_addresses[chan->channel][1]);
127 static int ad7150_read_event_config(struct iio_dev *indio_dev,
128 const struct iio_chan_spec *chan,
129 enum iio_event_type type,
130 enum iio_event_direction dir)
135 struct ad7150_chip_info *chip = iio_priv(indio_dev);
137 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
141 threshtype = (ret >> 5) & 0x03;
143 /*check if threshold mode is fixed or adaptive*/
144 thrfixed = FIELD_GET(AD7150_CFG_FIX, ret);
147 case IIO_EV_TYPE_MAG_ADAPTIVE:
148 if (dir == IIO_EV_DIR_RISING)
149 return !thrfixed && (threshtype == 0x1);
150 return !thrfixed && (threshtype == 0x0);
151 case IIO_EV_TYPE_THRESH_ADAPTIVE:
152 if (dir == IIO_EV_DIR_RISING)
153 return !thrfixed && (threshtype == 0x3);
154 return !thrfixed && (threshtype == 0x2);
155 case IIO_EV_TYPE_THRESH:
156 if (dir == IIO_EV_DIR_RISING)
157 return thrfixed && (threshtype == 0x1);
158 return thrfixed && (threshtype == 0x0);
165 /* lock should be held */
166 static int ad7150_write_event_params(struct iio_dev *indio_dev,
168 enum iio_event_type type,
169 enum iio_event_direction dir)
174 struct ad7150_chip_info *chip = iio_priv(indio_dev);
175 int rising = (dir == IIO_EV_DIR_RISING);
178 event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir);
180 if (event_code != chip->current_event)
184 /* Note completely different from the adaptive versions */
185 case IIO_EV_TYPE_THRESH:
186 value = chip->threshold[rising][chan];
187 return i2c_smbus_write_word_data(chip->client,
188 ad7150_addresses[chan][3],
190 case IIO_EV_TYPE_MAG_ADAPTIVE:
191 sens = chip->mag_sensitivity[rising][chan];
192 timeout = chip->mag_timeout[rising][chan];
194 case IIO_EV_TYPE_THRESH_ADAPTIVE:
195 sens = chip->thresh_sensitivity[rising][chan];
196 timeout = chip->thresh_timeout[rising][chan];
201 ret = i2c_smbus_write_byte_data(chip->client,
202 ad7150_addresses[chan][4],
207 ret = i2c_smbus_write_byte_data(chip->client,
208 ad7150_addresses[chan][5],
216 static int ad7150_write_event_config(struct iio_dev *indio_dev,
217 const struct iio_chan_spec *chan,
218 enum iio_event_type type,
219 enum iio_event_direction dir, int state)
221 u8 thresh_type, cfg, adaptive;
223 struct ad7150_chip_info *chip = iio_priv(indio_dev);
224 int rising = (dir == IIO_EV_DIR_RISING);
227 /* Something must always be turned on */
231 event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
232 if (event_code == chip->current_event)
234 mutex_lock(&chip->state_lock);
235 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
239 cfg = ret & ~((0x03 << 5) | BIT(7));
242 case IIO_EV_TYPE_MAG_ADAPTIVE:
249 case IIO_EV_TYPE_THRESH_ADAPTIVE:
256 case IIO_EV_TYPE_THRESH:
268 cfg |= (!adaptive << 7) | (thresh_type << 5);
270 ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
274 chip->current_event = event_code;
276 /* update control attributes */
277 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
279 mutex_unlock(&chip->state_lock);
284 static int ad7150_read_event_value(struct iio_dev *indio_dev,
285 const struct iio_chan_spec *chan,
286 enum iio_event_type type,
287 enum iio_event_direction dir,
288 enum iio_event_info info,
291 struct ad7150_chip_info *chip = iio_priv(indio_dev);
292 int rising = (dir == IIO_EV_DIR_RISING);
294 /* Complex register sharing going on here */
296 case IIO_EV_TYPE_MAG_ADAPTIVE:
297 *val = chip->mag_sensitivity[rising][chan->channel];
299 case IIO_EV_TYPE_THRESH_ADAPTIVE:
300 *val = chip->thresh_sensitivity[rising][chan->channel];
302 case IIO_EV_TYPE_THRESH:
303 *val = chip->threshold[rising][chan->channel];
310 static int ad7150_write_event_value(struct iio_dev *indio_dev,
311 const struct iio_chan_spec *chan,
312 enum iio_event_type type,
313 enum iio_event_direction dir,
314 enum iio_event_info info,
318 struct ad7150_chip_info *chip = iio_priv(indio_dev);
319 int rising = (dir == IIO_EV_DIR_RISING);
321 mutex_lock(&chip->state_lock);
323 case IIO_EV_TYPE_MAG_ADAPTIVE:
324 chip->mag_sensitivity[rising][chan->channel] = val;
326 case IIO_EV_TYPE_THRESH_ADAPTIVE:
327 chip->thresh_sensitivity[rising][chan->channel] = val;
329 case IIO_EV_TYPE_THRESH:
330 chip->threshold[rising][chan->channel] = val;
337 /* write back if active */
338 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
341 mutex_unlock(&chip->state_lock);
345 static ssize_t ad7150_show_timeout(struct device *dev,
346 struct device_attribute *attr,
349 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
350 struct ad7150_chip_info *chip = iio_priv(indio_dev);
351 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
354 /* use the event code for consistency reasons */
355 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
356 int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
357 == IIO_EV_DIR_RISING);
359 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
360 case IIO_EV_TYPE_MAG_ADAPTIVE:
361 value = chip->mag_timeout[rising][chan];
363 case IIO_EV_TYPE_THRESH_ADAPTIVE:
364 value = chip->thresh_timeout[rising][chan];
370 return sprintf(buf, "%d\n", value);
373 static ssize_t ad7150_store_timeout(struct device *dev,
374 struct device_attribute *attr,
378 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
379 struct ad7150_chip_info *chip = iio_priv(indio_dev);
380 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
381 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
382 enum iio_event_direction dir;
383 enum iio_event_type type;
388 type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
389 dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
390 rising = (dir == IIO_EV_DIR_RISING);
392 ret = kstrtou8(buf, 10, &data);
396 mutex_lock(&chip->state_lock);
398 case IIO_EV_TYPE_MAG_ADAPTIVE:
399 chip->mag_timeout[rising][chan] = data;
401 case IIO_EV_TYPE_THRESH_ADAPTIVE:
402 chip->thresh_timeout[rising][chan] = data;
409 ret = ad7150_write_event_params(indio_dev, chan, type, dir);
411 mutex_unlock(&chip->state_lock);
419 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
420 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
422 &ad7150_show_timeout, \
423 &ad7150_store_timeout, \
424 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
426 IIO_EV_TYPE_##ev_type, \
427 IIO_EV_DIR_##ev_dir))
428 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
429 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
430 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
431 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
432 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
433 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
434 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
435 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
437 static const struct iio_event_spec ad7150_events[] = {
439 .type = IIO_EV_TYPE_THRESH,
440 .dir = IIO_EV_DIR_RISING,
441 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
442 BIT(IIO_EV_INFO_ENABLE),
444 .type = IIO_EV_TYPE_THRESH,
445 .dir = IIO_EV_DIR_FALLING,
446 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
447 BIT(IIO_EV_INFO_ENABLE),
449 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
450 .dir = IIO_EV_DIR_RISING,
451 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
452 BIT(IIO_EV_INFO_ENABLE),
454 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
455 .dir = IIO_EV_DIR_FALLING,
456 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
457 BIT(IIO_EV_INFO_ENABLE),
459 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
460 .dir = IIO_EV_DIR_RISING,
461 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
462 BIT(IIO_EV_INFO_ENABLE),
464 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
465 .dir = IIO_EV_DIR_FALLING,
466 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
467 BIT(IIO_EV_INFO_ENABLE),
471 static const struct iio_chan_spec ad7150_channels[] = {
473 .type = IIO_CAPACITANCE,
476 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
477 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
478 .event_spec = ad7150_events,
479 .num_event_specs = ARRAY_SIZE(ad7150_events),
481 .type = IIO_CAPACITANCE,
484 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
485 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
486 .event_spec = ad7150_events,
487 .num_event_specs = ARRAY_SIZE(ad7150_events),
495 static irqreturn_t ad7150_event_handler(int irq, void *private)
497 struct iio_dev *indio_dev = private;
498 struct ad7150_chip_info *chip = iio_priv(indio_dev);
500 s64 timestamp = iio_get_time_ns(indio_dev);
503 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
509 if ((int_status & AD7150_STATUS_OUT1) &&
510 !(chip->old_state & AD7150_STATUS_OUT1))
511 iio_push_event(indio_dev,
512 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
517 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
518 (chip->old_state & AD7150_STATUS_OUT1))
519 iio_push_event(indio_dev,
520 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
526 if ((int_status & AD7150_STATUS_OUT2) &&
527 !(chip->old_state & AD7150_STATUS_OUT2))
528 iio_push_event(indio_dev,
529 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
534 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
535 (chip->old_state & AD7150_STATUS_OUT2))
536 iio_push_event(indio_dev,
537 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
542 /* store the status to avoid repushing same events */
543 chip->old_state = int_status;
548 /* Timeouts not currently handled by core */
549 static struct attribute *ad7150_event_attributes[] = {
550 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
552 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
554 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
556 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
558 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
560 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
562 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
564 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
569 static const struct attribute_group ad7150_event_attribute_group = {
570 .attrs = ad7150_event_attributes,
574 static const struct iio_info ad7150_info = {
575 .event_attrs = &ad7150_event_attribute_group,
576 .read_raw = &ad7150_read_raw,
577 .read_event_config = &ad7150_read_event_config,
578 .write_event_config = &ad7150_write_event_config,
579 .read_event_value = &ad7150_read_event_value,
580 .write_event_value = &ad7150_write_event_value,
584 * device probe and remove
587 static int ad7150_probe(struct i2c_client *client,
588 const struct i2c_device_id *id)
591 struct ad7150_chip_info *chip;
592 struct iio_dev *indio_dev;
594 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
597 chip = iio_priv(indio_dev);
598 mutex_init(&chip->state_lock);
599 /* this is only used for device removal purposes */
600 i2c_set_clientdata(client, indio_dev);
602 chip->client = client;
604 indio_dev->name = id->name;
605 indio_dev->channels = ad7150_channels;
606 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
607 /* Establish that the iio_dev is a child of the i2c device */
608 indio_dev->dev.parent = &client->dev;
610 indio_dev->info = &ad7150_info;
612 indio_dev->modes = INDIO_DIRECT_MODE;
615 ret = devm_request_threaded_irq(&client->dev, client->irq,
617 &ad7150_event_handler,
618 IRQF_TRIGGER_RISING |
619 IRQF_TRIGGER_FALLING |
627 if (client->dev.platform_data) {
628 ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
629 client->dev.platform_data,
631 &ad7150_event_handler,
632 IRQF_TRIGGER_RISING |
633 IRQF_TRIGGER_FALLING |
641 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
645 dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
646 id->name, client->irq);
651 static const struct i2c_device_id ad7150_id[] = {
658 MODULE_DEVICE_TABLE(i2c, ad7150_id);
660 static struct i2c_driver ad7150_driver = {
664 .probe = ad7150_probe,
665 .id_table = ad7150_id,
667 module_i2c_driver(ad7150_driver);
669 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
670 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
671 MODULE_LICENSE("GPL v2");