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 return i2c_smbus_write_word_data(chip->client,
183 ad7150_addresses[chan][3],
185 case IIO_EV_TYPE_MAG_ADAPTIVE:
186 sens = chip->mag_sensitivity[rising][chan];
187 timeout = chip->mag_timeout[rising][chan];
189 case IIO_EV_TYPE_THRESH_ADAPTIVE:
190 sens = chip->thresh_sensitivity[rising][chan];
191 timeout = chip->thresh_timeout[rising][chan];
196 ret = i2c_smbus_write_byte_data(chip->client,
197 ad7150_addresses[chan][4],
202 ret = i2c_smbus_write_byte_data(chip->client,
203 ad7150_addresses[chan][5],
211 static int ad7150_write_event_config(struct iio_dev *indio_dev,
212 const struct iio_chan_spec *chan, enum iio_event_type type,
213 enum iio_event_direction dir, int state)
215 u8 thresh_type, cfg, adaptive;
217 struct ad7150_chip_info *chip = iio_priv(indio_dev);
218 int rising = (dir == IIO_EV_DIR_RISING);
221 /* Something must always be turned on */
225 event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
226 if (event_code == chip->current_event)
228 mutex_lock(&chip->state_lock);
229 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
233 cfg = ret & ~((0x03 << 5) | (0x1 << 7));
236 case IIO_EV_TYPE_MAG_ADAPTIVE:
243 case IIO_EV_TYPE_THRESH_ADAPTIVE:
250 case IIO_EV_TYPE_THRESH:
262 cfg |= (!adaptive << 7) | (thresh_type << 5);
264 ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
268 chip->current_event = event_code;
270 /* update control attributes */
271 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
273 mutex_unlock(&chip->state_lock);
278 static int ad7150_read_event_value(struct iio_dev *indio_dev,
279 const struct iio_chan_spec *chan,
280 enum iio_event_type type,
281 enum iio_event_direction dir,
282 enum iio_event_info info,
285 struct ad7150_chip_info *chip = iio_priv(indio_dev);
286 int rising = (dir == IIO_EV_DIR_RISING);
288 /* Complex register sharing going on here */
290 case IIO_EV_TYPE_MAG_ADAPTIVE:
291 *val = chip->mag_sensitivity[rising][chan->channel];
293 case IIO_EV_TYPE_THRESH_ADAPTIVE:
294 *val = chip->thresh_sensitivity[rising][chan->channel];
296 case IIO_EV_TYPE_THRESH:
297 *val = chip->threshold[rising][chan->channel];
304 static int ad7150_write_event_value(struct iio_dev *indio_dev,
305 const struct iio_chan_spec *chan,
306 enum iio_event_type type,
307 enum iio_event_direction dir,
308 enum iio_event_info info,
312 struct ad7150_chip_info *chip = iio_priv(indio_dev);
313 int rising = (dir == IIO_EV_DIR_RISING);
315 mutex_lock(&chip->state_lock);
317 case IIO_EV_TYPE_MAG_ADAPTIVE:
318 chip->mag_sensitivity[rising][chan->channel] = val;
320 case IIO_EV_TYPE_THRESH_ADAPTIVE:
321 chip->thresh_sensitivity[rising][chan->channel] = val;
323 case IIO_EV_TYPE_THRESH:
324 chip->threshold[rising][chan->channel] = val;
331 /* write back if active */
332 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
335 mutex_unlock(&chip->state_lock);
339 static ssize_t ad7150_show_timeout(struct device *dev,
340 struct device_attribute *attr,
343 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
344 struct ad7150_chip_info *chip = iio_priv(indio_dev);
345 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
348 /* use the event code for consistency reasons */
349 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
350 int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
351 == IIO_EV_DIR_RISING);
353 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
354 case IIO_EV_TYPE_MAG_ADAPTIVE:
355 value = chip->mag_timeout[rising][chan];
357 case IIO_EV_TYPE_THRESH_ADAPTIVE:
358 value = chip->thresh_timeout[rising][chan];
364 return sprintf(buf, "%d\n", value);
367 static ssize_t ad7150_store_timeout(struct device *dev,
368 struct device_attribute *attr,
372 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
373 struct ad7150_chip_info *chip = iio_priv(indio_dev);
374 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
375 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
376 enum iio_event_direction dir;
377 enum iio_event_type type;
382 type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
383 dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
384 rising = (dir == IIO_EV_DIR_RISING);
386 ret = kstrtou8(buf, 10, &data);
390 mutex_lock(&chip->state_lock);
392 case IIO_EV_TYPE_MAG_ADAPTIVE:
393 chip->mag_timeout[rising][chan] = data;
395 case IIO_EV_TYPE_THRESH_ADAPTIVE:
396 chip->thresh_timeout[rising][chan] = data;
403 ret = ad7150_write_event_params(indio_dev, chan, type, dir);
405 mutex_unlock(&chip->state_lock);
413 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
414 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
416 &ad7150_show_timeout, \
417 &ad7150_store_timeout, \
418 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
420 IIO_EV_TYPE_##ev_type, \
421 IIO_EV_DIR_##ev_dir))
422 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
423 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
424 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
425 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
426 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
427 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
428 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
429 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
431 static const struct iio_event_spec ad7150_events[] = {
433 .type = IIO_EV_TYPE_THRESH,
434 .dir = IIO_EV_DIR_RISING,
435 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
436 BIT(IIO_EV_INFO_ENABLE),
438 .type = IIO_EV_TYPE_THRESH,
439 .dir = IIO_EV_DIR_FALLING,
440 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
441 BIT(IIO_EV_INFO_ENABLE),
443 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
444 .dir = IIO_EV_DIR_RISING,
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_FALLING,
450 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
451 BIT(IIO_EV_INFO_ENABLE),
453 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
454 .dir = IIO_EV_DIR_RISING,
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_FALLING,
460 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
461 BIT(IIO_EV_INFO_ENABLE),
465 static const struct iio_chan_spec ad7150_channels[] = {
467 .type = IIO_CAPACITANCE,
470 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
471 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
472 .event_spec = ad7150_events,
473 .num_event_specs = ARRAY_SIZE(ad7150_events),
475 .type = IIO_CAPACITANCE,
478 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
479 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
480 .event_spec = ad7150_events,
481 .num_event_specs = ARRAY_SIZE(ad7150_events),
489 static irqreturn_t ad7150_event_handler(int irq, void *private)
491 struct iio_dev *indio_dev = private;
492 struct ad7150_chip_info *chip = iio_priv(indio_dev);
494 s64 timestamp = iio_get_time_ns();
497 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
503 if ((int_status & AD7150_STATUS_OUT1) &&
504 !(chip->old_state & AD7150_STATUS_OUT1))
505 iio_push_event(indio_dev,
506 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
511 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
512 (chip->old_state & AD7150_STATUS_OUT1))
513 iio_push_event(indio_dev,
514 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
520 if ((int_status & AD7150_STATUS_OUT2) &&
521 !(chip->old_state & AD7150_STATUS_OUT2))
522 iio_push_event(indio_dev,
523 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
528 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
529 (chip->old_state & AD7150_STATUS_OUT2))
530 iio_push_event(indio_dev,
531 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
536 /* store the status to avoid repushing same events */
537 chip->old_state = int_status;
542 /* Timeouts not currently handled by core */
543 static struct attribute *ad7150_event_attributes[] = {
544 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
546 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
548 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
550 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
552 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
554 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
556 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
558 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
563 static struct attribute_group ad7150_event_attribute_group = {
564 .attrs = ad7150_event_attributes,
568 static const struct iio_info ad7150_info = {
569 .event_attrs = &ad7150_event_attribute_group,
570 .driver_module = THIS_MODULE,
571 .read_raw = &ad7150_read_raw,
572 .read_event_config = &ad7150_read_event_config,
573 .write_event_config = &ad7150_write_event_config,
574 .read_event_value = &ad7150_read_event_value,
575 .write_event_value = &ad7150_write_event_value,
579 * device probe and remove
582 static int ad7150_probe(struct i2c_client *client,
583 const struct i2c_device_id *id)
586 struct ad7150_chip_info *chip;
587 struct iio_dev *indio_dev;
589 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
592 chip = iio_priv(indio_dev);
593 mutex_init(&chip->state_lock);
594 /* this is only used for device removal purposes */
595 i2c_set_clientdata(client, indio_dev);
597 chip->client = client;
599 indio_dev->name = id->name;
600 indio_dev->channels = ad7150_channels;
601 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
602 /* Establish that the iio_dev is a child of the i2c device */
603 indio_dev->dev.parent = &client->dev;
605 indio_dev->info = &ad7150_info;
607 indio_dev->modes = INDIO_DIRECT_MODE;
610 ret = devm_request_threaded_irq(&client->dev, client->irq,
612 &ad7150_event_handler,
613 IRQF_TRIGGER_RISING |
614 IRQF_TRIGGER_FALLING |
622 if (client->dev.platform_data) {
623 ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
624 client->dev.platform_data,
626 &ad7150_event_handler,
627 IRQF_TRIGGER_RISING |
628 IRQF_TRIGGER_FALLING |
636 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
640 dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
641 id->name, client->irq);
646 static const struct i2c_device_id ad7150_id[] = {
653 MODULE_DEVICE_TABLE(i2c, ad7150_id);
655 static struct i2c_driver ad7150_driver = {
659 .probe = ad7150_probe,
660 .id_table = ad7150_id,
662 module_i2c_driver(ad7150_driver);
664 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
665 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
666 MODULE_LICENSE("GPL v2");