Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 1 | /* |
| 2 | * AD7150 capacitive sensor driver supporting AD7150/1/6 |
| 3 | * |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 4 | * Copyright 2010-2011 Analog Devices Inc. |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 5 | * |
| 6 | * Licensed under the GPL-2 or later. |
| 7 | */ |
| 8 | |
| 9 | #include <linux/interrupt.h> |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 10 | #include <linux/device.h> |
| 11 | #include <linux/kernel.h> |
| 12 | #include <linux/slab.h> |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 13 | #include <linux/i2c.h> |
Paul Gortmaker | 99c9785 | 2011-07-03 15:49:50 -0400 | [diff] [blame] | 14 | #include <linux/module.h> |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 15 | |
Jonathan Cameron | 06458e2 | 2012-04-25 15:54:58 +0100 | [diff] [blame] | 16 | #include <linux/iio/iio.h> |
| 17 | #include <linux/iio/sysfs.h> |
| 18 | #include <linux/iio/events.h> |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 19 | /* |
| 20 | * AD7150 registers definition |
| 21 | */ |
| 22 | |
| 23 | #define AD7150_STATUS 0 |
Shraddha Barke | c0559ae | 2015-12-29 16:57:30 +0530 | [diff] [blame] | 24 | #define AD7150_STATUS_OUT1 BIT(3) |
| 25 | #define AD7150_STATUS_OUT2 BIT(5) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 26 | #define AD7150_CH1_DATA_HIGH 1 |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 27 | #define AD7150_CH2_DATA_HIGH 3 |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 28 | #define AD7150_CH1_AVG_HIGH 5 |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 29 | #define AD7150_CH2_AVG_HIGH 7 |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 30 | #define AD7150_CH1_SENSITIVITY 9 |
| 31 | #define AD7150_CH1_THR_HOLD_H 9 |
| 32 | #define AD7150_CH1_TIMEOUT 10 |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 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 |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 37 | #define AD7150_CH2_SETUP 14 |
| 38 | #define AD7150_CFG 15 |
Shraddha Barke | c0559ae | 2015-12-29 16:57:30 +0530 | [diff] [blame] | 39 | #define AD7150_CFG_FIX BIT(7) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 40 | #define AD7150_PD_TIMER 16 |
| 41 | #define AD7150_CH1_CAPDAC 17 |
| 42 | #define AD7150_CH2_CAPDAC 18 |
| 43 | #define AD7150_SN3 19 |
| 44 | #define AD7150_SN2 20 |
| 45 | #define AD7150_SN1 21 |
| 46 | #define AD7150_SN0 22 |
| 47 | #define AD7150_ID 23 |
| 48 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 49 | /** |
| 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 |
| 66 | * of new condition. |
| 67 | * @conversion_mode: the current conversion mode. |
| 68 | * @state_lock: ensure consistent state of this structure wrt the |
| 69 | * hardware. |
| 70 | */ |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 71 | struct ad7150_chip_info { |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 72 | struct i2c_client *client; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 73 | u64 current_event; |
| 74 | u16 threshold[2][2]; |
| 75 | u8 thresh_sensitivity[2][2]; |
| 76 | u8 mag_sensitivity[2][2]; |
| 77 | u8 thresh_timeout[2][2]; |
| 78 | u8 mag_timeout[2][2]; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 79 | int old_state; |
| 80 | char *conversion_mode; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 81 | struct mutex state_lock; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 82 | }; |
| 83 | |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 84 | /* |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 85 | * sysfs nodes |
| 86 | */ |
| 87 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 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 }, |
| 95 | }; |
| 96 | |
| 97 | static int ad7150_read_raw(struct iio_dev *indio_dev, |
| 98 | struct iio_chan_spec const *chan, |
| 99 | int *val, |
| 100 | int *val2, |
| 101 | long mask) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 102 | { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 103 | int ret; |
| 104 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 105 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 106 | switch (mask) { |
Jonathan Cameron | e33e075 | 2012-04-15 17:41:26 +0100 | [diff] [blame] | 107 | case IIO_CHAN_INFO_RAW: |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 108 | ret = i2c_smbus_read_word_data(chip->client, |
| 109 | ad7150_addresses[chan->channel][0]); |
| 110 | if (ret < 0) |
| 111 | return ret; |
| 112 | *val = swab16(ret); |
| 113 | return IIO_VAL_INT; |
Jonathan Cameron | c8a9f80 | 2011-10-26 17:41:36 +0100 | [diff] [blame] | 114 | case IIO_CHAN_INFO_AVERAGE_RAW: |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 115 | ret = i2c_smbus_read_word_data(chip->client, |
| 116 | ad7150_addresses[chan->channel][1]); |
| 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | *val = swab16(ret); |
| 120 | return IIO_VAL_INT; |
| 121 | default: |
| 122 | return -EINVAL; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 123 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 124 | } |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 125 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 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) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 129 | { |
| 130 | int ret; |
| 131 | u8 threshtype; |
| 132 | bool adaptive; |
| 133 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 134 | |
| 135 | ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG); |
| 136 | if (ret < 0) |
| 137 | return ret; |
| 138 | |
| 139 | threshtype = (ret >> 5) & 0x03; |
| 140 | adaptive = !!(ret & 0x80); |
| 141 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 142 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 143 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 144 | if (dir == IIO_EV_DIR_RISING) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 145 | return adaptive && (threshtype == 0x1); |
Catalina Mocanu | 288903f | 2014-09-19 15:55:05 -0700 | [diff] [blame] | 146 | return adaptive && (threshtype == 0x0); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 147 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 148 | if (dir == IIO_EV_DIR_RISING) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 149 | return adaptive && (threshtype == 0x3); |
Catalina Mocanu | 288903f | 2014-09-19 15:55:05 -0700 | [diff] [blame] | 150 | return adaptive && (threshtype == 0x2); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 151 | case IIO_EV_TYPE_THRESH: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 152 | if (dir == IIO_EV_DIR_RISING) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 153 | return !adaptive && (threshtype == 0x1); |
Catalina Mocanu | 288903f | 2014-09-19 15:55:05 -0700 | [diff] [blame] | 154 | return !adaptive && (threshtype == 0x0); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 155 | default: |
| 156 | break; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 157 | } |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 158 | return -EINVAL; |
| 159 | } |
| 160 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 161 | /* lock should be held */ |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 162 | static int ad7150_write_event_params(struct iio_dev *indio_dev, |
Shraddha Barke | c4f0ebd | 2015-12-29 16:57:31 +0530 | [diff] [blame] | 163 | unsigned int chan, |
| 164 | enum iio_event_type type, |
| 165 | enum iio_event_direction dir) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 166 | { |
| 167 | int ret; |
| 168 | u16 value; |
| 169 | u8 sens, timeout; |
| 170 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 171 | int rising = (dir == IIO_EV_DIR_RISING); |
| 172 | u64 event_code; |
| 173 | |
| 174 | event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 175 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 176 | if (event_code != chip->current_event) |
| 177 | return 0; |
| 178 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 179 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 180 | /* Note completely different from the adaptive versions */ |
| 181 | case IIO_EV_TYPE_THRESH: |
| 182 | value = chip->threshold[rising][chan]; |
Ioana Ciornei | 4a737ec | 2015-10-14 22:34:27 +0300 | [diff] [blame] | 183 | return i2c_smbus_write_word_data(chip->client, |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 184 | ad7150_addresses[chan][3], |
| 185 | swab16(value)); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 186 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
| 187 | sens = chip->mag_sensitivity[rising][chan]; |
| 188 | timeout = chip->mag_timeout[rising][chan]; |
| 189 | break; |
| 190 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
| 191 | sens = chip->thresh_sensitivity[rising][chan]; |
| 192 | timeout = chip->thresh_timeout[rising][chan]; |
| 193 | break; |
| 194 | default: |
| 195 | return -EINVAL; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 196 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 197 | ret = i2c_smbus_write_byte_data(chip->client, |
| 198 | ad7150_addresses[chan][4], |
| 199 | sens); |
| 200 | if (ret < 0) |
| 201 | return ret; |
| 202 | |
| 203 | ret = i2c_smbus_write_byte_data(chip->client, |
| 204 | ad7150_addresses[chan][5], |
| 205 | timeout); |
| 206 | if (ret < 0) |
| 207 | return ret; |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | static int ad7150_write_event_config(struct iio_dev *indio_dev, |
Shraddha Barke | c4f0ebd | 2015-12-29 16:57:31 +0530 | [diff] [blame] | 213 | const struct iio_chan_spec *chan, |
| 214 | enum iio_event_type type, |
| 215 | enum iio_event_direction dir, int state) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 216 | { |
| 217 | u8 thresh_type, cfg, adaptive; |
| 218 | int ret; |
| 219 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 220 | int rising = (dir == IIO_EV_DIR_RISING); |
| 221 | u64 event_code; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 222 | |
| 223 | /* Something must always be turned on */ |
Cristina Moraru | e6e65f9 | 2015-10-20 22:55:43 +0300 | [diff] [blame] | 224 | if (!state) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 225 | return -EINVAL; |
| 226 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 227 | event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 228 | if (event_code == chip->current_event) |
| 229 | return 0; |
| 230 | mutex_lock(&chip->state_lock); |
| 231 | ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG); |
| 232 | if (ret < 0) |
| 233 | goto error_ret; |
| 234 | |
| 235 | cfg = ret & ~((0x03 << 5) | (0x1 << 7)); |
| 236 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 237 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 238 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
| 239 | adaptive = 1; |
| 240 | if (rising) |
| 241 | thresh_type = 0x1; |
| 242 | else |
| 243 | thresh_type = 0x0; |
| 244 | break; |
| 245 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
| 246 | adaptive = 1; |
| 247 | if (rising) |
| 248 | thresh_type = 0x3; |
| 249 | else |
| 250 | thresh_type = 0x2; |
| 251 | break; |
| 252 | case IIO_EV_TYPE_THRESH: |
| 253 | adaptive = 0; |
| 254 | if (rising) |
| 255 | thresh_type = 0x1; |
| 256 | else |
| 257 | thresh_type = 0x0; |
| 258 | break; |
| 259 | default: |
| 260 | ret = -EINVAL; |
| 261 | goto error_ret; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 262 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 263 | |
| 264 | cfg |= (!adaptive << 7) | (thresh_type << 5); |
| 265 | |
| 266 | ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg); |
| 267 | if (ret < 0) |
| 268 | goto error_ret; |
| 269 | |
| 270 | chip->current_event = event_code; |
| 271 | |
| 272 | /* update control attributes */ |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 273 | ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 274 | error_ret: |
| 275 | mutex_unlock(&chip->state_lock); |
| 276 | |
| 277 | return 0; |
| 278 | } |
| 279 | |
| 280 | static int ad7150_read_event_value(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 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, |
| 285 | int *val, int *val2) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 286 | { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 287 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 288 | int rising = (dir == IIO_EV_DIR_RISING); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 289 | |
| 290 | /* Complex register sharing going on here */ |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 291 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 292 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 293 | *val = chip->mag_sensitivity[rising][chan->channel]; |
| 294 | return IIO_VAL_INT; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 295 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 296 | *val = chip->thresh_sensitivity[rising][chan->channel]; |
| 297 | return IIO_VAL_INT; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 298 | case IIO_EV_TYPE_THRESH: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 299 | *val = chip->threshold[rising][chan->channel]; |
| 300 | return IIO_VAL_INT; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 301 | default: |
| 302 | return -EINVAL; |
Joe Perches | a22526e | 2013-10-10 16:07:59 -0700 | [diff] [blame] | 303 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | static int ad7150_write_event_value(struct iio_dev *indio_dev, |
Shraddha Barke | c4f0ebd | 2015-12-29 16:57:31 +0530 | [diff] [blame] | 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, |
| 311 | int val, int val2) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 312 | { |
| 313 | int ret; |
| 314 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 315 | int rising = (dir == IIO_EV_DIR_RISING); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 316 | |
| 317 | mutex_lock(&chip->state_lock); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 318 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 319 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 320 | chip->mag_sensitivity[rising][chan->channel] = val; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 321 | break; |
| 322 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 323 | chip->thresh_sensitivity[rising][chan->channel] = val; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 324 | break; |
| 325 | case IIO_EV_TYPE_THRESH: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 326 | chip->threshold[rising][chan->channel] = val; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 327 | break; |
| 328 | default: |
| 329 | ret = -EINVAL; |
| 330 | goto error_ret; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 331 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 332 | |
| 333 | /* write back if active */ |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 334 | ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 335 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 336 | error_ret: |
Dan Carpenter | ee760ab | 2011-10-06 09:17:00 +0300 | [diff] [blame] | 337 | mutex_unlock(&chip->state_lock); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 338 | return ret; |
| 339 | } |
| 340 | |
| 341 | static ssize_t ad7150_show_timeout(struct device *dev, |
| 342 | struct device_attribute *attr, |
| 343 | char *buf) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 344 | { |
Lars-Peter Clausen | d30a7f9 | 2012-05-12 15:39:45 +0200 | [diff] [blame] | 345 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 346 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 347 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 348 | u8 value; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 349 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 350 | /* use the event code for consistency reasons */ |
Lars-Peter Clausen | da36716 | 2012-02-13 10:25:32 +0100 | [diff] [blame] | 351 | int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 352 | int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address) |
| 353 | == IIO_EV_DIR_RISING); |
| 354 | |
| 355 | switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) { |
| 356 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
| 357 | value = chip->mag_timeout[rising][chan]; |
| 358 | break; |
| 359 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
| 360 | value = chip->thresh_timeout[rising][chan]; |
| 361 | break; |
| 362 | default: |
| 363 | return -EINVAL; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 364 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 365 | |
| 366 | return sprintf(buf, "%d\n", value); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 369 | static ssize_t ad7150_store_timeout(struct device *dev, |
Shraddha Barke | c4f0ebd | 2015-12-29 16:57:31 +0530 | [diff] [blame] | 370 | struct device_attribute *attr, |
| 371 | const char *buf, |
| 372 | size_t len) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 373 | { |
Lars-Peter Clausen | d30a7f9 | 2012-05-12 15:39:45 +0200 | [diff] [blame] | 374 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 375 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
| 376 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | da36716 | 2012-02-13 10:25:32 +0100 | [diff] [blame] | 377 | int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 378 | enum iio_event_direction dir; |
| 379 | enum iio_event_type type; |
| 380 | int rising; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 381 | u8 data; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 382 | int ret; |
| 383 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 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); |
| 387 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 388 | ret = kstrtou8(buf, 10, &data); |
| 389 | if (ret < 0) |
| 390 | return ret; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 391 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 392 | mutex_lock(&chip->state_lock); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 393 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 394 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
| 395 | chip->mag_timeout[rising][chan] = data; |
| 396 | break; |
| 397 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
| 398 | chip->thresh_timeout[rising][chan] = data; |
| 399 | break; |
| 400 | default: |
| 401 | ret = -EINVAL; |
| 402 | goto error_ret; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 403 | } |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 404 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 405 | ret = ad7150_write_event_params(indio_dev, chan, type, dir); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 406 | error_ret: |
| 407 | mutex_unlock(&chip->state_lock); |
| 408 | |
| 409 | if (ret < 0) |
| 410 | return ret; |
| 411 | |
| 412 | return len; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 413 | } |
| 414 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 415 | #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \ |
| 416 | IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \ |
| 417 | S_IRUGO | S_IWUSR, \ |
| 418 | &ad7150_show_timeout, \ |
| 419 | &ad7150_store_timeout, \ |
| 420 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \ |
| 421 | chan, \ |
| 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); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 432 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 433 | static const struct iio_event_spec ad7150_events[] = { |
| 434 | { |
| 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), |
| 439 | }, { |
| 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), |
| 444 | }, { |
| 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), |
| 449 | }, { |
| 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), |
| 454 | }, { |
| 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), |
| 459 | }, { |
| 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), |
| 464 | }, |
| 465 | }; |
| 466 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 467 | static const struct iio_chan_spec ad7150_channels[] = { |
| 468 | { |
| 469 | .type = IIO_CAPACITANCE, |
| 470 | .indexed = 1, |
| 471 | .channel = 0, |
Jonathan Cameron | 0221519 | 2013-02-27 19:37:59 +0000 | [diff] [blame] | 472 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 473 | BIT(IIO_CHAN_INFO_AVERAGE_RAW), |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 474 | .event_spec = ad7150_events, |
| 475 | .num_event_specs = ARRAY_SIZE(ad7150_events), |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 476 | }, { |
| 477 | .type = IIO_CAPACITANCE, |
| 478 | .indexed = 1, |
| 479 | .channel = 1, |
Jonathan Cameron | 0221519 | 2013-02-27 19:37:59 +0000 | [diff] [blame] | 480 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 481 | BIT(IIO_CHAN_INFO_AVERAGE_RAW), |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 482 | .event_spec = ad7150_events, |
| 483 | .num_event_specs = ARRAY_SIZE(ad7150_events), |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 484 | }, |
| 485 | }; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 486 | |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 487 | /* |
| 488 | * threshold events |
| 489 | */ |
| 490 | |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 491 | static irqreturn_t ad7150_event_handler(int irq, void *private) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 492 | { |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 493 | struct iio_dev *indio_dev = private; |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 494 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 495 | u8 int_status; |
Gregor Boirie | bc2b7da | 2016-03-09 19:05:49 +0100 | [diff] [blame] | 496 | s64 timestamp = iio_get_time_ns(indio_dev); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 497 | int ret; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 498 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 499 | ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS); |
| 500 | if (ret < 0) |
| 501 | return IRQ_HANDLED; |
| 502 | |
| 503 | int_status = ret; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 504 | |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 505 | if ((int_status & AD7150_STATUS_OUT1) && |
| 506 | !(chip->old_state & AD7150_STATUS_OUT1)) |
| 507 | iio_push_event(indio_dev, |
Michael Hennerich | 1b99232 | 2011-10-19 14:41:08 +0200 | [diff] [blame] | 508 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, |
Jonathan Cameron | d91a0ab | 2011-05-18 14:42:13 +0100 | [diff] [blame] | 509 | 0, |
| 510 | IIO_EV_TYPE_THRESH, |
| 511 | IIO_EV_DIR_RISING), |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 512 | timestamp); |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 513 | else if ((!(int_status & AD7150_STATUS_OUT1)) && |
| 514 | (chip->old_state & AD7150_STATUS_OUT1)) |
| 515 | iio_push_event(indio_dev, |
Michael Hennerich | 1b99232 | 2011-10-19 14:41:08 +0200 | [diff] [blame] | 516 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, |
Jonathan Cameron | d91a0ab | 2011-05-18 14:42:13 +0100 | [diff] [blame] | 517 | 0, |
| 518 | IIO_EV_TYPE_THRESH, |
| 519 | IIO_EV_DIR_FALLING), |
| 520 | timestamp); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 521 | |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 522 | if ((int_status & AD7150_STATUS_OUT2) && |
| 523 | !(chip->old_state & AD7150_STATUS_OUT2)) |
| 524 | iio_push_event(indio_dev, |
Michael Hennerich | 1b99232 | 2011-10-19 14:41:08 +0200 | [diff] [blame] | 525 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, |
Jonathan Cameron | d91a0ab | 2011-05-18 14:42:13 +0100 | [diff] [blame] | 526 | 1, |
| 527 | IIO_EV_TYPE_THRESH, |
| 528 | IIO_EV_DIR_RISING), |
| 529 | timestamp); |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 530 | else if ((!(int_status & AD7150_STATUS_OUT2)) && |
| 531 | (chip->old_state & AD7150_STATUS_OUT2)) |
| 532 | iio_push_event(indio_dev, |
Michael Hennerich | 1b99232 | 2011-10-19 14:41:08 +0200 | [diff] [blame] | 533 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, |
Jonathan Cameron | d91a0ab | 2011-05-18 14:42:13 +0100 | [diff] [blame] | 534 | 1, |
| 535 | IIO_EV_TYPE_THRESH, |
| 536 | IIO_EV_DIR_FALLING), |
| 537 | timestamp); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 538 | /* store the status to avoid repushing same events */ |
| 539 | chip->old_state = int_status; |
| 540 | |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 541 | return IRQ_HANDLED; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 542 | } |
| 543 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 544 | /* Timeouts not currently handled by core */ |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 545 | static struct attribute *ad7150_event_attributes[] = { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 546 | &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout |
| 547 | .dev_attr.attr, |
| 548 | &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout |
| 549 | .dev_attr.attr, |
| 550 | &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout |
| 551 | .dev_attr.attr, |
| 552 | &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout |
| 553 | .dev_attr.attr, |
| 554 | &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout |
| 555 | .dev_attr.attr, |
| 556 | &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout |
| 557 | .dev_attr.attr, |
| 558 | &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout |
| 559 | .dev_attr.attr, |
| 560 | &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout |
| 561 | .dev_attr.attr, |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 562 | NULL, |
| 563 | }; |
| 564 | |
| 565 | static struct attribute_group ad7150_event_attribute_group = { |
| 566 | .attrs = ad7150_event_attributes, |
Jonathan Cameron | 8e7d967 | 2011-08-30 12:32:45 +0100 | [diff] [blame] | 567 | .name = "events", |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 568 | }; |
| 569 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 570 | static const struct iio_info ad7150_info = { |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 571 | .event_attrs = &ad7150_event_attribute_group, |
| 572 | .driver_module = THIS_MODULE, |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 573 | .read_raw = &ad7150_read_raw, |
Lars-Peter Clausen | cb95585 | 2013-12-07 10:45:00 +0000 | [diff] [blame] | 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, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 578 | }; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 579 | |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 580 | /* |
| 581 | * device probe and remove |
| 582 | */ |
| 583 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 584 | static int ad7150_probe(struct i2c_client *client, |
Shraddha Barke | c4f0ebd | 2015-12-29 16:57:31 +0530 | [diff] [blame] | 585 | const struct i2c_device_id *id) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 586 | { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 587 | int ret; |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 588 | struct ad7150_chip_info *chip; |
| 589 | struct iio_dev *indio_dev; |
| 590 | |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 591 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); |
| 592 | if (!indio_dev) |
| 593 | return -ENOMEM; |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 594 | chip = iio_priv(indio_dev); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 595 | mutex_init(&chip->state_lock); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 596 | /* this is only used for device removal purposes */ |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 597 | i2c_set_clientdata(client, indio_dev); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 598 | |
| 599 | chip->client = client; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 600 | |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 601 | indio_dev->name = id->name; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 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 */ |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 605 | indio_dev->dev.parent = &client->dev; |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 606 | |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 607 | indio_dev->info = &ad7150_info; |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 608 | |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 609 | indio_dev->modes = INDIO_DIRECT_MODE; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 610 | |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 611 | if (client->irq) { |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 612 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 613 | NULL, |
| 614 | &ad7150_event_handler, |
| 615 | IRQF_TRIGGER_RISING | |
Lars-Peter Clausen | a91aff1 | 2012-07-02 10:54:45 +0200 | [diff] [blame] | 616 | IRQF_TRIGGER_FALLING | |
| 617 | IRQF_ONESHOT, |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 618 | "ad7150_irq1", |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 619 | indio_dev); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 620 | if (ret) |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 621 | return ret; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 624 | if (client->dev.platform_data) { |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 625 | ret = devm_request_threaded_irq(&client->dev, *(unsigned int *) |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 626 | client->dev.platform_data, |
| 627 | NULL, |
| 628 | &ad7150_event_handler, |
| 629 | IRQF_TRIGGER_RISING | |
Lars-Peter Clausen | a91aff1 | 2012-07-02 10:54:45 +0200 | [diff] [blame] | 630 | IRQF_TRIGGER_FALLING | |
| 631 | IRQF_ONESHOT, |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 632 | "ad7150_irq2", |
| 633 | indio_dev); |
| 634 | if (ret) |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 635 | return ret; |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 636 | } |
| 637 | |
Ioana Ciornei | 252fb58 | 2015-10-02 13:37:48 +0300 | [diff] [blame] | 638 | ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev); |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 639 | if (ret) |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 640 | return ret; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 641 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 642 | dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n", |
| 643 | id->name, client->irq); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 644 | |
| 645 | return 0; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 646 | } |
| 647 | |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 648 | static const struct i2c_device_id ad7150_id[] = { |
| 649 | { "ad7150", 0 }, |
| 650 | { "ad7151", 0 }, |
| 651 | { "ad7156", 0 }, |
| 652 | {} |
| 653 | }; |
| 654 | |
| 655 | MODULE_DEVICE_TABLE(i2c, ad7150_id); |
| 656 | |
| 657 | static struct i2c_driver ad7150_driver = { |
| 658 | .driver = { |
| 659 | .name = "ad7150", |
| 660 | }, |
| 661 | .probe = ad7150_probe, |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 662 | .id_table = ad7150_id, |
| 663 | }; |
Lars-Peter Clausen | 6e5af18 | 2011-11-16 10:13:38 +0100 | [diff] [blame] | 664 | module_i2c_driver(ad7150_driver); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 665 | |
| 666 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 667 | MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver"); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 668 | MODULE_LICENSE("GPL v2"); |