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, enum iio_event_type type,
129 enum iio_event_direction dir)
134 struct ad7150_chip_info *chip = iio_priv(indio_dev);
136 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
140 threshtype = (ret >> 5) & 0x03;
142 /*check if threshold mode is fixed or adaptive*/
143 thrfixed = FIELD_GET(AD7150_CFG_FIX, ret);
146 case IIO_EV_TYPE_MAG_ADAPTIVE:
147 if (dir == IIO_EV_DIR_RISING)
148 return !thrfixed && (threshtype == 0x1);
149 return !thrfixed && (threshtype == 0x0);
150 case IIO_EV_TYPE_THRESH_ADAPTIVE:
151 if (dir == IIO_EV_DIR_RISING)
152 return !thrfixed && (threshtype == 0x3);
153 return !thrfixed && (threshtype == 0x2);
154 case IIO_EV_TYPE_THRESH:
155 if (dir == IIO_EV_DIR_RISING)
156 return thrfixed && (threshtype == 0x1);
157 return thrfixed && (threshtype == 0x0);
164 /* lock should be held */
165 static int ad7150_write_event_params(struct iio_dev *indio_dev,
167 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 return i2c_smbus_write_word_data(chip->client,
187 ad7150_addresses[chan][3],
189 case IIO_EV_TYPE_MAG_ADAPTIVE:
190 sens = chip->mag_sensitivity[rising][chan];
191 timeout = chip->mag_timeout[rising][chan];
193 case IIO_EV_TYPE_THRESH_ADAPTIVE:
194 sens = chip->thresh_sensitivity[rising][chan];
195 timeout = chip->thresh_timeout[rising][chan];
200 ret = i2c_smbus_write_byte_data(chip->client,
201 ad7150_addresses[chan][4],
206 ret = i2c_smbus_write_byte_data(chip->client,
207 ad7150_addresses[chan][5],
215 static int ad7150_write_event_config(struct iio_dev *indio_dev,
216 const struct iio_chan_spec *chan,
217 enum iio_event_type type,
218 enum iio_event_direction dir, int state)
220 u8 thresh_type, cfg, adaptive;
222 struct ad7150_chip_info *chip = iio_priv(indio_dev);
223 int rising = (dir == IIO_EV_DIR_RISING);
226 /* Something must always be turned on */
230 event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
231 if (event_code == chip->current_event)
233 mutex_lock(&chip->state_lock);
234 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
238 cfg = ret & ~((0x03 << 5) | BIT(7));
241 case IIO_EV_TYPE_MAG_ADAPTIVE:
248 case IIO_EV_TYPE_THRESH_ADAPTIVE:
255 case IIO_EV_TYPE_THRESH:
267 cfg |= (!adaptive << 7) | (thresh_type << 5);
269 ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
273 chip->current_event = event_code;
275 /* update control attributes */
276 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
278 mutex_unlock(&chip->state_lock);
283 static int ad7150_read_event_value(struct iio_dev *indio_dev,
284 const struct iio_chan_spec *chan,
285 enum iio_event_type type,
286 enum iio_event_direction dir,
287 enum iio_event_info info,
290 struct ad7150_chip_info *chip = iio_priv(indio_dev);
291 int rising = (dir == IIO_EV_DIR_RISING);
293 /* Complex register sharing going on here */
295 case IIO_EV_TYPE_MAG_ADAPTIVE:
296 *val = chip->mag_sensitivity[rising][chan->channel];
298 case IIO_EV_TYPE_THRESH_ADAPTIVE:
299 *val = chip->thresh_sensitivity[rising][chan->channel];
301 case IIO_EV_TYPE_THRESH:
302 *val = chip->threshold[rising][chan->channel];
309 static int ad7150_write_event_value(struct iio_dev *indio_dev,
310 const struct iio_chan_spec *chan,
311 enum iio_event_type type,
312 enum iio_event_direction dir,
313 enum iio_event_info info,
317 struct ad7150_chip_info *chip = iio_priv(indio_dev);
318 int rising = (dir == IIO_EV_DIR_RISING);
320 mutex_lock(&chip->state_lock);
322 case IIO_EV_TYPE_MAG_ADAPTIVE:
323 chip->mag_sensitivity[rising][chan->channel] = val;
325 case IIO_EV_TYPE_THRESH_ADAPTIVE:
326 chip->thresh_sensitivity[rising][chan->channel] = val;
328 case IIO_EV_TYPE_THRESH:
329 chip->threshold[rising][chan->channel] = val;
336 /* write back if active */
337 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
340 mutex_unlock(&chip->state_lock);
344 static ssize_t ad7150_show_timeout(struct device *dev,
345 struct device_attribute *attr,
348 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
349 struct ad7150_chip_info *chip = iio_priv(indio_dev);
350 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
353 /* use the event code for consistency reasons */
354 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
355 int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
356 == IIO_EV_DIR_RISING);
358 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
359 case IIO_EV_TYPE_MAG_ADAPTIVE:
360 value = chip->mag_timeout[rising][chan];
362 case IIO_EV_TYPE_THRESH_ADAPTIVE:
363 value = chip->thresh_timeout[rising][chan];
369 return sprintf(buf, "%d\n", value);
372 static ssize_t ad7150_store_timeout(struct device *dev,
373 struct device_attribute *attr,
377 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
378 struct ad7150_chip_info *chip = iio_priv(indio_dev);
379 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
380 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
381 enum iio_event_direction dir;
382 enum iio_event_type type;
387 type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
388 dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
389 rising = (dir == IIO_EV_DIR_RISING);
391 ret = kstrtou8(buf, 10, &data);
395 mutex_lock(&chip->state_lock);
397 case IIO_EV_TYPE_MAG_ADAPTIVE:
398 chip->mag_timeout[rising][chan] = data;
400 case IIO_EV_TYPE_THRESH_ADAPTIVE:
401 chip->thresh_timeout[rising][chan] = data;
408 ret = ad7150_write_event_params(indio_dev, chan, type, dir);
410 mutex_unlock(&chip->state_lock);
418 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
419 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
421 &ad7150_show_timeout, \
422 &ad7150_store_timeout, \
423 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
425 IIO_EV_TYPE_##ev_type, \
426 IIO_EV_DIR_##ev_dir))
427 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
428 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
429 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
430 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
431 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
432 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
433 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
434 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
436 static const struct iio_event_spec ad7150_events[] = {
438 .type = IIO_EV_TYPE_THRESH,
439 .dir = IIO_EV_DIR_RISING,
440 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
441 BIT(IIO_EV_INFO_ENABLE),
443 .type = IIO_EV_TYPE_THRESH,
444 .dir = IIO_EV_DIR_FALLING,
445 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
446 BIT(IIO_EV_INFO_ENABLE),
448 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
449 .dir = IIO_EV_DIR_RISING,
450 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
451 BIT(IIO_EV_INFO_ENABLE),
453 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
454 .dir = IIO_EV_DIR_FALLING,
455 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
456 BIT(IIO_EV_INFO_ENABLE),
458 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
459 .dir = IIO_EV_DIR_RISING,
460 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
461 BIT(IIO_EV_INFO_ENABLE),
463 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
464 .dir = IIO_EV_DIR_FALLING,
465 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
466 BIT(IIO_EV_INFO_ENABLE),
470 static const struct iio_chan_spec ad7150_channels[] = {
472 .type = IIO_CAPACITANCE,
475 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
476 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
477 .event_spec = ad7150_events,
478 .num_event_specs = ARRAY_SIZE(ad7150_events),
480 .type = IIO_CAPACITANCE,
483 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
484 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
485 .event_spec = ad7150_events,
486 .num_event_specs = ARRAY_SIZE(ad7150_events),
494 static irqreturn_t ad7150_event_handler(int irq, void *private)
496 struct iio_dev *indio_dev = private;
497 struct ad7150_chip_info *chip = iio_priv(indio_dev);
499 s64 timestamp = iio_get_time_ns(indio_dev);
502 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
508 if ((int_status & AD7150_STATUS_OUT1) &&
509 !(chip->old_state & AD7150_STATUS_OUT1))
510 iio_push_event(indio_dev,
511 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
516 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
517 (chip->old_state & AD7150_STATUS_OUT1))
518 iio_push_event(indio_dev,
519 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
525 if ((int_status & AD7150_STATUS_OUT2) &&
526 !(chip->old_state & AD7150_STATUS_OUT2))
527 iio_push_event(indio_dev,
528 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
533 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
534 (chip->old_state & AD7150_STATUS_OUT2))
535 iio_push_event(indio_dev,
536 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
541 /* store the status to avoid repushing same events */
542 chip->old_state = int_status;
547 /* Timeouts not currently handled by core */
548 static struct attribute *ad7150_event_attributes[] = {
549 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
551 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
553 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
555 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
557 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
559 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
561 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
563 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
568 static const struct attribute_group ad7150_event_attribute_group = {
569 .attrs = ad7150_event_attributes,
573 static const struct iio_info ad7150_info = {
574 .event_attrs = &ad7150_event_attribute_group,
575 .driver_module = THIS_MODULE,
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");