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>
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
88 static const u8 ad7150_addresses[][6] = {
97 static int ad7150_read_raw(
struct iio_dev *indio_dev,
109 ad7150_addresses[chan->
channel][0]);
116 ad7150_addresses[chan->
channel][1]);
139 threshtype = (ret >> 5) & 0x03;
140 adaptive = !!(ret & 0x80);
145 return adaptive && (threshtype == 0x1);
147 return adaptive && (threshtype == 0x0);
150 return adaptive && (threshtype == 0x3);
152 return adaptive && (threshtype == 0x2);
156 return !adaptive && (threshtype == 0x1);
158 return !adaptive && (threshtype == 0x0);
164 static int ad7150_write_event_params(
struct iio_dev *indio_dev,
u64 event_code)
182 ad7150_addresses[chan][3],
199 ad7150_addresses[chan][4],
205 ad7150_addresses[chan][5],
213 static int ad7150_write_event_config(
struct iio_dev *indio_dev,
216 u8 thresh_type,
cfg, adaptive;
233 cfg = ret & ~((0x03 << 5) | (0x1 << 7));
262 cfg |= (!adaptive << 7) | (thresh_type << 5);
271 ret = ad7150_write_event_params(indio_dev, event_code);
278 static int ad7150_read_event_value(
struct iio_dev *indio_dev,
306 static int ad7150_write_event_value(
struct iio_dev *indio_dev,
333 ret = ad7150_write_event_params(indio_dev, event_code);
344 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
365 return sprintf(buf,
"%d\n", value);
373 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
399 ret = ad7150_write_event_params(indio_dev, this_attr->
address);
409 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
410 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
412 &ad7150_show_timeout, \
413 &ad7150_store_timeout, \
414 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
416 IIO_EV_TYPE_##ev_type, \
417 IIO_EV_DIR_##ev_dir))
418 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
419 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
420 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
421 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
422 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
423 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
424 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
425 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
461 static irqreturn_t ad7150_event_handler(
int irq,
void *
private)
463 struct iio_dev *indio_dev =
private;
483 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
500 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
515 static struct attribute *ad7150_event_attributes[] = {
516 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
518 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
520 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
522 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
524 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
526 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
528 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
530 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
536 .attrs = ad7150_event_attributes,
540 static const struct iio_info ad7150_info = {
541 .event_attrs = &ad7150_event_attribute_group,
543 .read_raw = &ad7150_read_raw,
544 .read_event_config = &ad7150_read_event_config,
545 .write_event_config = &ad7150_write_event_config,
546 .read_event_value = &ad7150_read_event_value,
547 .write_event_value = &ad7150_write_event_value,
562 if (indio_dev ==
NULL) {
566 chip = iio_priv(indio_dev);
569 i2c_set_clientdata(client, indio_dev);
573 indio_dev->
name =
id->name;
574 indio_dev->
channels = ad7150_channels;
577 indio_dev->
dev.parent = &client->
dev;
579 indio_dev->
info = &ad7150_info;
586 &ad7150_event_handler,
596 if (client->
dev.platform_data) {
598 client->
dev.platform_data,
600 &ad7150_event_handler,
612 goto error_free_irq2;
614 dev_info(&client->
dev,
"%s capacitive sensor registered,irq: %d\n",
619 if (client->
dev.platform_data)
620 free_irq(*(
unsigned int *)client->
dev.platform_data,
633 struct iio_dev *indio_dev = i2c_get_clientdata(client);
639 if (client->
dev.platform_data)
640 free_irq(*(
unsigned int *)client->
dev.platform_data, indio_dev);
660 .probe = ad7150_probe,
662 .id_table = ad7150_id,