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 BIT(3)
25 #define AD7150_STATUS_OUT2 BIT(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 BIT(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);
146 return adaptive && (threshtype == 0x0);
147 case IIO_EV_TYPE_THRESH_ADAPTIVE:
148 if (dir == IIO_EV_DIR_RISING)
149 return adaptive && (threshtype == 0x3);
150 return adaptive && (threshtype == 0x2);
151 case IIO_EV_TYPE_THRESH:
152 if (dir == IIO_EV_DIR_RISING)
153 return !adaptive && (threshtype == 0x1);
154 return !adaptive && (threshtype == 0x0);
161 /* lock should be held */
162 static int ad7150_write_event_params(struct iio_dev *indio_dev,
164 enum iio_event_type type,
165 enum iio_event_direction dir)
170 struct ad7150_chip_info *chip = iio_priv(indio_dev);
171 int rising = (dir == IIO_EV_DIR_RISING);
174 event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir);
176 if (event_code != chip->current_event)
180 /* Note completely different from the adaptive versions */
181 case IIO_EV_TYPE_THRESH:
182 value = chip->threshold[rising][chan];
183 return i2c_smbus_write_word_data(chip->client,
184 ad7150_addresses[chan][3],
186 case IIO_EV_TYPE_MAG_ADAPTIVE:
187 sens = chip->mag_sensitivity[rising][chan];
188 timeout = chip->mag_timeout[rising][chan];
190 case IIO_EV_TYPE_THRESH_ADAPTIVE:
191 sens = chip->thresh_sensitivity[rising][chan];
192 timeout = chip->thresh_timeout[rising][chan];
197 ret = i2c_smbus_write_byte_data(chip->client,
198 ad7150_addresses[chan][4],
203 ret = i2c_smbus_write_byte_data(chip->client,
204 ad7150_addresses[chan][5],
212 static int ad7150_write_event_config(struct iio_dev *indio_dev,
213 const struct iio_chan_spec *chan,
214 enum iio_event_type type,
215 enum iio_event_direction dir, int state)
217 u8 thresh_type, cfg, adaptive;
219 struct ad7150_chip_info *chip = iio_priv(indio_dev);
220 int rising = (dir == IIO_EV_DIR_RISING);
223 /* Something must always be turned on */
227 event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
228 if (event_code == chip->current_event)
230 mutex_lock(&chip->state_lock);
231 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
235 cfg = ret & ~((0x03 << 5) | (0x1 << 7));
238 case IIO_EV_TYPE_MAG_ADAPTIVE:
245 case IIO_EV_TYPE_THRESH_ADAPTIVE:
252 case IIO_EV_TYPE_THRESH:
264 cfg |= (!adaptive << 7) | (thresh_type << 5);
266 ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
270 chip->current_event = event_code;
272 /* update control attributes */
273 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
275 mutex_unlock(&chip->state_lock);
280 static int ad7150_read_event_value(struct iio_dev *indio_dev,
281 const struct iio_chan_spec *chan,
282 enum iio_event_type type,
283 enum iio_event_direction dir,
284 enum iio_event_info info,
287 struct ad7150_chip_info *chip = iio_priv(indio_dev);
288 int rising = (dir == IIO_EV_DIR_RISING);
290 /* Complex register sharing going on here */
292 case IIO_EV_TYPE_MAG_ADAPTIVE:
293 *val = chip->mag_sensitivity[rising][chan->channel];
295 case IIO_EV_TYPE_THRESH_ADAPTIVE:
296 *val = chip->thresh_sensitivity[rising][chan->channel];
298 case IIO_EV_TYPE_THRESH:
299 *val = chip->threshold[rising][chan->channel];
306 static int ad7150_write_event_value(struct iio_dev *indio_dev,
307 const struct iio_chan_spec *chan,
308 enum iio_event_type type,
309 enum iio_event_direction dir,
310 enum iio_event_info info,
314 struct ad7150_chip_info *chip = iio_priv(indio_dev);
315 int rising = (dir == IIO_EV_DIR_RISING);
317 mutex_lock(&chip->state_lock);
319 case IIO_EV_TYPE_MAG_ADAPTIVE:
320 chip->mag_sensitivity[rising][chan->channel] = val;
322 case IIO_EV_TYPE_THRESH_ADAPTIVE:
323 chip->thresh_sensitivity[rising][chan->channel] = val;
325 case IIO_EV_TYPE_THRESH:
326 chip->threshold[rising][chan->channel] = val;
333 /* write back if active */
334 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
337 mutex_unlock(&chip->state_lock);
341 static ssize_t ad7150_show_timeout(struct device *dev,
342 struct device_attribute *attr,
345 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
346 struct ad7150_chip_info *chip = iio_priv(indio_dev);
347 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
350 /* use the event code for consistency reasons */
351 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
352 int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
353 == IIO_EV_DIR_RISING);
355 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
356 case IIO_EV_TYPE_MAG_ADAPTIVE:
357 value = chip->mag_timeout[rising][chan];
359 case IIO_EV_TYPE_THRESH_ADAPTIVE:
360 value = chip->thresh_timeout[rising][chan];
366 return sprintf(buf, "%d\n", value);
369 static ssize_t ad7150_store_timeout(struct device *dev,
370 struct device_attribute *attr,
374 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
375 struct ad7150_chip_info *chip = iio_priv(indio_dev);
376 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
377 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
378 enum iio_event_direction dir;
379 enum iio_event_type type;
384 type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
385 dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
386 rising = (dir == IIO_EV_DIR_RISING);
388 ret = kstrtou8(buf, 10, &data);
392 mutex_lock(&chip->state_lock);
394 case IIO_EV_TYPE_MAG_ADAPTIVE:
395 chip->mag_timeout[rising][chan] = data;
397 case IIO_EV_TYPE_THRESH_ADAPTIVE:
398 chip->thresh_timeout[rising][chan] = data;
405 ret = ad7150_write_event_params(indio_dev, chan, type, dir);
407 mutex_unlock(&chip->state_lock);
415 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
416 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
418 &ad7150_show_timeout, \
419 &ad7150_store_timeout, \
420 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
422 IIO_EV_TYPE_##ev_type, \
423 IIO_EV_DIR_##ev_dir))
424 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
425 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
426 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
427 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
428 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
429 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
430 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
431 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
433 static const struct iio_event_spec ad7150_events[] = {
435 .type = IIO_EV_TYPE_THRESH,
436 .dir = IIO_EV_DIR_RISING,
437 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
438 BIT(IIO_EV_INFO_ENABLE),
440 .type = IIO_EV_TYPE_THRESH,
441 .dir = IIO_EV_DIR_FALLING,
442 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
443 BIT(IIO_EV_INFO_ENABLE),
445 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
446 .dir = IIO_EV_DIR_RISING,
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_FALLING,
452 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
453 BIT(IIO_EV_INFO_ENABLE),
455 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
456 .dir = IIO_EV_DIR_RISING,
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_FALLING,
462 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
463 BIT(IIO_EV_INFO_ENABLE),
467 static const struct iio_chan_spec ad7150_channels[] = {
469 .type = IIO_CAPACITANCE,
472 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
473 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
474 .event_spec = ad7150_events,
475 .num_event_specs = ARRAY_SIZE(ad7150_events),
477 .type = IIO_CAPACITANCE,
480 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
481 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
482 .event_spec = ad7150_events,
483 .num_event_specs = ARRAY_SIZE(ad7150_events),
491 static irqreturn_t ad7150_event_handler(int irq, void *private)
493 struct iio_dev *indio_dev = private;
494 struct ad7150_chip_info *chip = iio_priv(indio_dev);
496 s64 timestamp = iio_get_time_ns();
499 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
505 if ((int_status & AD7150_STATUS_OUT1) &&
506 !(chip->old_state & AD7150_STATUS_OUT1))
507 iio_push_event(indio_dev,
508 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
513 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
514 (chip->old_state & AD7150_STATUS_OUT1))
515 iio_push_event(indio_dev,
516 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
522 if ((int_status & AD7150_STATUS_OUT2) &&
523 !(chip->old_state & AD7150_STATUS_OUT2))
524 iio_push_event(indio_dev,
525 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
530 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
531 (chip->old_state & AD7150_STATUS_OUT2))
532 iio_push_event(indio_dev,
533 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
538 /* store the status to avoid repushing same events */
539 chip->old_state = int_status;
544 /* Timeouts not currently handled by core */
545 static struct attribute *ad7150_event_attributes[] = {
546 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
548 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
550 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
552 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
554 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
556 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
558 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
560 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
565 static struct attribute_group ad7150_event_attribute_group = {
566 .attrs = ad7150_event_attributes,
570 static const struct iio_info ad7150_info = {
571 .event_attrs = &ad7150_event_attribute_group,
572 .driver_module = THIS_MODULE,
573 .read_raw = &ad7150_read_raw,
574 .read_event_config = &ad7150_read_event_config,
575 .write_event_config = &ad7150_write_event_config,
576 .read_event_value = &ad7150_read_event_value,
577 .write_event_value = &ad7150_write_event_value,
581 * device probe and remove
584 static int ad7150_probe(struct i2c_client *client,
585 const struct i2c_device_id *id)
588 struct ad7150_chip_info *chip;
589 struct iio_dev *indio_dev;
591 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
594 chip = iio_priv(indio_dev);
595 mutex_init(&chip->state_lock);
596 /* this is only used for device removal purposes */
597 i2c_set_clientdata(client, indio_dev);
599 chip->client = client;
601 indio_dev->name = id->name;
602 indio_dev->channels = ad7150_channels;
603 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
604 /* Establish that the iio_dev is a child of the i2c device */
605 indio_dev->dev.parent = &client->dev;
607 indio_dev->info = &ad7150_info;
609 indio_dev->modes = INDIO_DIRECT_MODE;
612 ret = devm_request_threaded_irq(&client->dev, client->irq,
614 &ad7150_event_handler,
615 IRQF_TRIGGER_RISING |
616 IRQF_TRIGGER_FALLING |
624 if (client->dev.platform_data) {
625 ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
626 client->dev.platform_data,
628 &ad7150_event_handler,
629 IRQF_TRIGGER_RISING |
630 IRQF_TRIGGER_FALLING |
638 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
642 dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
643 id->name, client->irq);
648 static const struct i2c_device_id ad7150_id[] = {
655 MODULE_DEVICE_TABLE(i2c, ad7150_id);
657 static struct i2c_driver ad7150_driver = {
661 .probe = ad7150_probe,
662 .id_table = ad7150_id,
664 module_i2c_driver(ad7150_driver);
667 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
668 MODULE_LICENSE("GPL v2");