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);
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,
163 unsigned int chan, enum iio_event_type type,
164 enum iio_event_direction dir)
169 struct ad7150_chip_info *chip = iio_priv(indio_dev);
170 int rising = (dir == IIO_EV_DIR_RISING);
173 event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir);
175 if (event_code != chip->current_event)
179 /* Note completely different from the adaptive versions */
180 case IIO_EV_TYPE_THRESH:
181 value = chip->threshold[rising][chan];
182 ret = i2c_smbus_write_word_data(chip->client,
183 ad7150_addresses[chan][3],
188 case IIO_EV_TYPE_MAG_ADAPTIVE:
189 sens = chip->mag_sensitivity[rising][chan];
190 timeout = chip->mag_timeout[rising][chan];
192 case IIO_EV_TYPE_THRESH_ADAPTIVE:
193 sens = chip->thresh_sensitivity[rising][chan];
194 timeout = chip->thresh_timeout[rising][chan];
199 ret = i2c_smbus_write_byte_data(chip->client,
200 ad7150_addresses[chan][4],
205 ret = i2c_smbus_write_byte_data(chip->client,
206 ad7150_addresses[chan][5],
214 static int ad7150_write_event_config(struct iio_dev *indio_dev,
215 const struct iio_chan_spec *chan, enum iio_event_type type,
216 enum iio_event_direction dir, int state)
218 u8 thresh_type, cfg, adaptive;
220 struct ad7150_chip_info *chip = iio_priv(indio_dev);
221 int rising = (dir == IIO_EV_DIR_RISING);
224 /* Something must always be turned on */
228 event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
229 if (event_code == chip->current_event)
231 mutex_lock(&chip->state_lock);
232 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
236 cfg = ret & ~((0x03 << 5) | (0x1 << 7));
239 case IIO_EV_TYPE_MAG_ADAPTIVE:
246 case IIO_EV_TYPE_THRESH_ADAPTIVE:
253 case IIO_EV_TYPE_THRESH:
265 cfg |= (!adaptive << 7) | (thresh_type << 5);
267 ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
271 chip->current_event = event_code;
273 /* update control attributes */
274 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
276 mutex_unlock(&chip->state_lock);
281 static int ad7150_read_event_value(struct iio_dev *indio_dev,
282 const struct iio_chan_spec *chan,
283 enum iio_event_type type,
284 enum iio_event_direction dir,
285 enum iio_event_info info,
288 struct ad7150_chip_info *chip = iio_priv(indio_dev);
289 int rising = (dir == IIO_EV_DIR_RISING);
291 /* Complex register sharing going on here */
293 case IIO_EV_TYPE_MAG_ADAPTIVE:
294 *val = chip->mag_sensitivity[rising][chan->channel];
296 case IIO_EV_TYPE_THRESH_ADAPTIVE:
297 *val = chip->thresh_sensitivity[rising][chan->channel];
299 case IIO_EV_TYPE_THRESH:
300 *val = chip->threshold[rising][chan->channel];
307 static int ad7150_write_event_value(struct iio_dev *indio_dev,
308 const struct iio_chan_spec *chan,
309 enum iio_event_type type,
310 enum iio_event_direction dir,
311 enum iio_event_info info,
315 struct ad7150_chip_info *chip = iio_priv(indio_dev);
316 int rising = (dir == IIO_EV_DIR_RISING);
318 mutex_lock(&chip->state_lock);
320 case IIO_EV_TYPE_MAG_ADAPTIVE:
321 chip->mag_sensitivity[rising][chan->channel] = val;
323 case IIO_EV_TYPE_THRESH_ADAPTIVE:
324 chip->thresh_sensitivity[rising][chan->channel] = val;
326 case IIO_EV_TYPE_THRESH:
327 chip->threshold[rising][chan->channel] = val;
334 /* write back if active */
335 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
338 mutex_unlock(&chip->state_lock);
342 static ssize_t ad7150_show_timeout(struct device *dev,
343 struct device_attribute *attr,
346 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
347 struct ad7150_chip_info *chip = iio_priv(indio_dev);
348 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
351 /* use the event code for consistency reasons */
352 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
353 int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
354 == IIO_EV_DIR_RISING);
356 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
357 case IIO_EV_TYPE_MAG_ADAPTIVE:
358 value = chip->mag_timeout[rising][chan];
360 case IIO_EV_TYPE_THRESH_ADAPTIVE:
361 value = chip->thresh_timeout[rising][chan];
367 return sprintf(buf, "%d\n", value);
370 static ssize_t ad7150_store_timeout(struct device *dev,
371 struct device_attribute *attr,
375 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
376 struct ad7150_chip_info *chip = iio_priv(indio_dev);
377 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
378 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
379 enum iio_event_direction dir;
380 enum iio_event_type type;
385 type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
386 dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
387 rising = (dir == IIO_EV_DIR_RISING);
389 ret = kstrtou8(buf, 10, &data);
393 mutex_lock(&chip->state_lock);
395 case IIO_EV_TYPE_MAG_ADAPTIVE:
396 chip->mag_timeout[rising][chan] = data;
398 case IIO_EV_TYPE_THRESH_ADAPTIVE:
399 chip->thresh_timeout[rising][chan] = data;
406 ret = ad7150_write_event_params(indio_dev, chan, type, dir);
408 mutex_unlock(&chip->state_lock);
416 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
417 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
419 &ad7150_show_timeout, \
420 &ad7150_store_timeout, \
421 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
423 IIO_EV_TYPE_##ev_type, \
424 IIO_EV_DIR_##ev_dir))
425 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
426 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
427 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
428 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
429 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
430 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
431 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
432 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
434 static const struct iio_event_spec ad7150_events[] = {
436 .type = IIO_EV_TYPE_THRESH,
437 .dir = IIO_EV_DIR_RISING,
438 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
439 BIT(IIO_EV_INFO_ENABLE),
441 .type = IIO_EV_TYPE_THRESH,
442 .dir = IIO_EV_DIR_FALLING,
443 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
444 BIT(IIO_EV_INFO_ENABLE),
446 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
447 .dir = IIO_EV_DIR_RISING,
448 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
449 BIT(IIO_EV_INFO_ENABLE),
451 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
452 .dir = IIO_EV_DIR_FALLING,
453 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
454 BIT(IIO_EV_INFO_ENABLE),
456 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
457 .dir = IIO_EV_DIR_RISING,
458 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
459 BIT(IIO_EV_INFO_ENABLE),
461 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
462 .dir = IIO_EV_DIR_FALLING,
463 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
464 BIT(IIO_EV_INFO_ENABLE),
468 static const struct iio_chan_spec ad7150_channels[] = {
470 .type = IIO_CAPACITANCE,
473 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
474 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
475 .event_spec = ad7150_events,
476 .num_event_specs = ARRAY_SIZE(ad7150_events),
478 .type = IIO_CAPACITANCE,
481 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
482 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
483 .event_spec = ad7150_events,
484 .num_event_specs = ARRAY_SIZE(ad7150_events),
492 static irqreturn_t ad7150_event_handler(int irq, void *private)
494 struct iio_dev *indio_dev = private;
495 struct ad7150_chip_info *chip = iio_priv(indio_dev);
497 s64 timestamp = iio_get_time_ns();
500 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
506 if ((int_status & AD7150_STATUS_OUT1) &&
507 !(chip->old_state & AD7150_STATUS_OUT1))
508 iio_push_event(indio_dev,
509 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
514 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
515 (chip->old_state & AD7150_STATUS_OUT1))
516 iio_push_event(indio_dev,
517 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
523 if ((int_status & AD7150_STATUS_OUT2) &&
524 !(chip->old_state & AD7150_STATUS_OUT2))
525 iio_push_event(indio_dev,
526 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
531 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
532 (chip->old_state & AD7150_STATUS_OUT2))
533 iio_push_event(indio_dev,
534 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
539 /* store the status to avoid repushing same events */
540 chip->old_state = int_status;
545 /* Timeouts not currently handled by core */
546 static struct attribute *ad7150_event_attributes[] = {
547 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
549 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
551 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
553 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
555 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
557 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
559 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
561 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
566 static struct attribute_group ad7150_event_attribute_group = {
567 .attrs = ad7150_event_attributes,
571 static const struct iio_info ad7150_info = {
572 .event_attrs = &ad7150_event_attribute_group,
573 .driver_module = THIS_MODULE,
574 .read_raw = &ad7150_read_raw,
575 .read_event_config = &ad7150_read_event_config,
576 .write_event_config = &ad7150_write_event_config,
577 .read_event_value = &ad7150_read_event_value,
578 .write_event_value = &ad7150_write_event_value,
582 * device probe and remove
585 static int ad7150_probe(struct i2c_client *client,
586 const struct i2c_device_id *id)
589 struct ad7150_chip_info *chip;
590 struct iio_dev *indio_dev;
592 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
595 chip = iio_priv(indio_dev);
596 mutex_init(&chip->state_lock);
597 /* this is only used for device removal purposes */
598 i2c_set_clientdata(client, indio_dev);
600 chip->client = client;
602 indio_dev->name = id->name;
603 indio_dev->channels = ad7150_channels;
604 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
605 /* Establish that the iio_dev is a child of the i2c device */
606 indio_dev->dev.parent = &client->dev;
608 indio_dev->info = &ad7150_info;
610 indio_dev->modes = INDIO_DIRECT_MODE;
613 ret = devm_request_threaded_irq(&client->dev, client->irq,
615 &ad7150_event_handler,
616 IRQF_TRIGGER_RISING |
617 IRQF_TRIGGER_FALLING |
625 if (client->dev.platform_data) {
626 ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
627 client->dev.platform_data,
629 &ad7150_event_handler,
630 IRQF_TRIGGER_RISING |
631 IRQF_TRIGGER_FALLING |
639 ret = iio_device_register(indio_dev);
643 dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
644 id->name, client->irq);
649 static int ad7150_remove(struct i2c_client *client)
651 struct iio_dev *indio_dev = i2c_get_clientdata(client);
653 iio_device_unregister(indio_dev);
658 static const struct i2c_device_id ad7150_id[] = {
665 MODULE_DEVICE_TABLE(i2c, ad7150_id);
667 static struct i2c_driver ad7150_driver = {
671 .probe = ad7150_probe,
672 .remove = ad7150_remove,
673 .id_table = ad7150_id,
675 module_i2c_driver(ad7150_driver);
677 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
678 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
679 MODULE_LICENSE("GPL v2");