1 // SPDX-License-Identifier: GPL-2.0+
3 * AD7150 capacitive sensor driver supporting AD7150/1/6
5 * Copyright 2010-2011 Analog Devices Inc.
8 #include <linux/bitfield.h>
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 #define AD7150_THRESHTYPE_MSK GENMASK(6, 5)
53 * struct ad7150_chip_info - instance specific chip data
54 * @client: i2c client for this device
55 * @current_event: device always has one type of event enabled.
56 * This element stores the event code of the current one.
57 * @threshold: thresholds for simple capacitance value events
58 * @thresh_sensitivity: threshold for simple capacitance offset
59 * from 'average' value.
60 * @mag_sensitity: threshold for magnitude of capacitance offset from
61 * from 'average' value.
62 * @thresh_timeout: a timeout, in samples from the moment an
63 * adaptive threshold event occurs to when the average
64 * value jumps to current value.
65 * @mag_timeout: a timeout, in sample from the moment an
66 * adaptive magnitude event occurs to when the average
67 * value jumps to the current value.
68 * @old_state: store state from previous event, allowing confirmation
70 * @conversion_mode: the current conversion mode.
71 * @state_lock: ensure consistent state of this structure wrt the
74 struct ad7150_chip_info {
75 struct i2c_client *client;
78 u8 thresh_sensitivity[2][2];
79 u8 mag_sensitivity[2][2];
80 u8 thresh_timeout[2][2];
83 char *conversion_mode;
84 struct mutex state_lock;
91 static const u8 ad7150_addresses[][6] = {
92 { AD7150_CH1_DATA_HIGH, AD7150_CH1_AVG_HIGH,
93 AD7150_CH1_SETUP, AD7150_CH1_THR_HOLD_H,
94 AD7150_CH1_SENSITIVITY, AD7150_CH1_TIMEOUT },
95 { AD7150_CH2_DATA_HIGH, AD7150_CH2_AVG_HIGH,
96 AD7150_CH2_SETUP, AD7150_CH2_THR_HOLD_H,
97 AD7150_CH2_SENSITIVITY, AD7150_CH2_TIMEOUT },
100 static int ad7150_read_raw(struct iio_dev *indio_dev,
101 struct iio_chan_spec const *chan,
107 struct ad7150_chip_info *chip = iio_priv(indio_dev);
108 int channel = chan->channel;
111 case IIO_CHAN_INFO_RAW:
112 ret = i2c_smbus_read_word_data(chip->client,
113 ad7150_addresses[channel][0]);
118 case IIO_CHAN_INFO_AVERAGE_RAW:
119 ret = i2c_smbus_read_word_data(chip->client,
120 ad7150_addresses[channel][1]);
130 static int ad7150_read_event_config(struct iio_dev *indio_dev,
131 const struct iio_chan_spec *chan,
132 enum iio_event_type type,
133 enum iio_event_direction dir)
138 struct ad7150_chip_info *chip = iio_priv(indio_dev);
140 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
144 threshtype = FIELD_GET(AD7150_THRESHTYPE_MSK, ret);
146 /*check if threshold mode is fixed or adaptive*/
147 thrfixed = FIELD_GET(AD7150_CFG_FIX, ret);
150 case IIO_EV_TYPE_MAG_ADAPTIVE:
151 if (dir == IIO_EV_DIR_RISING)
152 return !thrfixed && (threshtype == 0x1);
153 return !thrfixed && (threshtype == 0x0);
154 case IIO_EV_TYPE_THRESH_ADAPTIVE:
155 if (dir == IIO_EV_DIR_RISING)
156 return !thrfixed && (threshtype == 0x3);
157 return !thrfixed && (threshtype == 0x2);
158 case IIO_EV_TYPE_THRESH:
159 if (dir == IIO_EV_DIR_RISING)
160 return thrfixed && (threshtype == 0x1);
161 return thrfixed && (threshtype == 0x0);
168 /* state_lock should be held to ensure consistent state*/
170 static int ad7150_write_event_params(struct iio_dev *indio_dev,
172 enum iio_event_type type,
173 enum iio_event_direction dir)
178 struct ad7150_chip_info *chip = iio_priv(indio_dev);
179 int rising = (dir == IIO_EV_DIR_RISING);
182 event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir);
184 if (event_code != chip->current_event)
188 /* Note completely different from the adaptive versions */
189 case IIO_EV_TYPE_THRESH:
190 value = chip->threshold[rising][chan];
191 return i2c_smbus_write_word_data(chip->client,
192 ad7150_addresses[chan][3],
194 case IIO_EV_TYPE_MAG_ADAPTIVE:
195 sens = chip->mag_sensitivity[rising][chan];
196 timeout = chip->mag_timeout[rising][chan];
198 case IIO_EV_TYPE_THRESH_ADAPTIVE:
199 sens = chip->thresh_sensitivity[rising][chan];
200 timeout = chip->thresh_timeout[rising][chan];
205 ret = i2c_smbus_write_byte_data(chip->client,
206 ad7150_addresses[chan][4],
210 return i2c_smbus_write_byte_data(chip->client,
211 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) ? 1 : 0;
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 #define AD7150_CAPACITANCE_CHAN(_chan) { \
471 .type = IIO_CAPACITANCE, \
474 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
475 BIT(IIO_CHAN_INFO_AVERAGE_RAW), \
476 .event_spec = ad7150_events, \
477 .num_event_specs = ARRAY_SIZE(ad7150_events), \
480 static const struct iio_chan_spec ad7150_channels[] = {
481 AD7150_CAPACITANCE_CHAN(0),
482 AD7150_CAPACITANCE_CHAN(1)
485 static irqreturn_t ad7150_event_handler(int irq, void *private)
487 struct iio_dev *indio_dev = private;
488 struct ad7150_chip_info *chip = iio_priv(indio_dev);
490 s64 timestamp = iio_get_time_ns(indio_dev);
493 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
499 if ((int_status & AD7150_STATUS_OUT1) &&
500 !(chip->old_state & AD7150_STATUS_OUT1))
501 iio_push_event(indio_dev,
502 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
507 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
508 (chip->old_state & AD7150_STATUS_OUT1))
509 iio_push_event(indio_dev,
510 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
516 if ((int_status & AD7150_STATUS_OUT2) &&
517 !(chip->old_state & AD7150_STATUS_OUT2))
518 iio_push_event(indio_dev,
519 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
524 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
525 (chip->old_state & AD7150_STATUS_OUT2))
526 iio_push_event(indio_dev,
527 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
532 /* store the status to avoid repushing same events */
533 chip->old_state = int_status;
538 /* Timeouts not currently handled by core */
539 static struct attribute *ad7150_event_attributes[] = {
540 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
542 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
544 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
546 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
548 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
550 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
552 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
554 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
559 static const struct attribute_group ad7150_event_attribute_group = {
560 .attrs = ad7150_event_attributes,
564 static const struct iio_info ad7150_info = {
565 .event_attrs = &ad7150_event_attribute_group,
566 .read_raw = &ad7150_read_raw,
567 .read_event_config = &ad7150_read_event_config,
568 .write_event_config = &ad7150_write_event_config,
569 .read_event_value = &ad7150_read_event_value,
570 .write_event_value = &ad7150_write_event_value,
573 static int ad7150_probe(struct i2c_client *client,
574 const struct i2c_device_id *id)
577 struct ad7150_chip_info *chip;
578 struct iio_dev *indio_dev;
580 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
583 chip = iio_priv(indio_dev);
584 mutex_init(&chip->state_lock);
585 /* this is only used for device removal purposes */
586 i2c_set_clientdata(client, indio_dev);
588 chip->client = client;
590 indio_dev->name = id->name;
591 indio_dev->channels = ad7150_channels;
592 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
594 indio_dev->info = &ad7150_info;
596 indio_dev->modes = INDIO_DIRECT_MODE;
599 ret = devm_request_threaded_irq(&client->dev, client->irq,
601 &ad7150_event_handler,
602 IRQF_TRIGGER_RISING |
603 IRQF_TRIGGER_FALLING |
611 if (client->dev.platform_data) {
612 ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
613 client->dev.platform_data,
615 &ad7150_event_handler,
616 IRQF_TRIGGER_RISING |
617 IRQF_TRIGGER_FALLING |
625 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
629 dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
630 id->name, client->irq);
635 static const struct i2c_device_id ad7150_id[] = {
642 MODULE_DEVICE_TABLE(i2c, ad7150_id);
644 static struct i2c_driver ad7150_driver = {
648 .probe = ad7150_probe,
649 .id_table = ad7150_id,
651 module_i2c_driver(ad7150_driver);
653 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
654 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
655 MODULE_LICENSE("GPL v2");