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 |
| 24 | #define AD7150_STATUS_OUT1 (1 << 3) |
| 25 | #define AD7150_STATUS_OUT2 (1 << 5) |
| 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 |
| 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 |
| 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); |
| 146 | else |
| 147 | return adaptive && (threshtype == 0x0); |
| 148 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 149 | if (dir == IIO_EV_DIR_RISING) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 150 | return adaptive && (threshtype == 0x3); |
| 151 | else |
| 152 | return adaptive && (threshtype == 0x2); |
| 153 | |
| 154 | case IIO_EV_TYPE_THRESH: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 155 | if (dir == IIO_EV_DIR_RISING) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 156 | return !adaptive && (threshtype == 0x1); |
| 157 | else |
| 158 | return !adaptive && (threshtype == 0x0); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 159 | default: |
| 160 | break; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 161 | } |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 162 | return -EINVAL; |
| 163 | } |
| 164 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 165 | /* lock should be held */ |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 166 | static int ad7150_write_event_params(struct iio_dev *indio_dev, |
| 167 | unsigned int chan, enum iio_event_type type, |
| 168 | enum iio_event_direction dir) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 169 | { |
| 170 | int ret; |
| 171 | u16 value; |
| 172 | u8 sens, timeout; |
| 173 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 174 | int rising = (dir == IIO_EV_DIR_RISING); |
| 175 | u64 event_code; |
| 176 | |
| 177 | event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 178 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 179 | if (event_code != chip->current_event) |
| 180 | return 0; |
| 181 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 182 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 183 | /* Note completely different from the adaptive versions */ |
| 184 | case IIO_EV_TYPE_THRESH: |
| 185 | value = chip->threshold[rising][chan]; |
| 186 | ret = i2c_smbus_write_word_data(chip->client, |
| 187 | ad7150_addresses[chan][3], |
| 188 | swab16(value)); |
| 189 | if (ret < 0) |
| 190 | return ret; |
| 191 | return 0; |
| 192 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
| 193 | sens = chip->mag_sensitivity[rising][chan]; |
| 194 | timeout = chip->mag_timeout[rising][chan]; |
| 195 | break; |
| 196 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
| 197 | sens = chip->thresh_sensitivity[rising][chan]; |
| 198 | timeout = chip->thresh_timeout[rising][chan]; |
| 199 | break; |
| 200 | default: |
| 201 | return -EINVAL; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 202 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 203 | ret = i2c_smbus_write_byte_data(chip->client, |
| 204 | ad7150_addresses[chan][4], |
| 205 | sens); |
| 206 | if (ret < 0) |
| 207 | return ret; |
| 208 | |
| 209 | ret = i2c_smbus_write_byte_data(chip->client, |
| 210 | ad7150_addresses[chan][5], |
| 211 | timeout); |
| 212 | if (ret < 0) |
| 213 | return ret; |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | static int ad7150_write_event_config(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 219 | const struct iio_chan_spec *chan, enum iio_event_type type, |
| 220 | enum iio_event_direction dir, int state) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 221 | { |
| 222 | u8 thresh_type, cfg, adaptive; |
| 223 | int ret; |
| 224 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 225 | int rising = (dir == IIO_EV_DIR_RISING); |
| 226 | u64 event_code; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 227 | |
| 228 | /* Something must always be turned on */ |
| 229 | if (state == 0) |
| 230 | return -EINVAL; |
| 231 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 232 | event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 233 | if (event_code == chip->current_event) |
| 234 | return 0; |
| 235 | mutex_lock(&chip->state_lock); |
| 236 | ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG); |
| 237 | if (ret < 0) |
| 238 | goto error_ret; |
| 239 | |
| 240 | cfg = ret & ~((0x03 << 5) | (0x1 << 7)); |
| 241 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 242 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 243 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
| 244 | adaptive = 1; |
| 245 | if (rising) |
| 246 | thresh_type = 0x1; |
| 247 | else |
| 248 | thresh_type = 0x0; |
| 249 | break; |
| 250 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
| 251 | adaptive = 1; |
| 252 | if (rising) |
| 253 | thresh_type = 0x3; |
| 254 | else |
| 255 | thresh_type = 0x2; |
| 256 | break; |
| 257 | case IIO_EV_TYPE_THRESH: |
| 258 | adaptive = 0; |
| 259 | if (rising) |
| 260 | thresh_type = 0x1; |
| 261 | else |
| 262 | thresh_type = 0x0; |
| 263 | break; |
| 264 | default: |
| 265 | ret = -EINVAL; |
| 266 | goto error_ret; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 267 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 268 | |
| 269 | cfg |= (!adaptive << 7) | (thresh_type << 5); |
| 270 | |
| 271 | ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg); |
| 272 | if (ret < 0) |
| 273 | goto error_ret; |
| 274 | |
| 275 | chip->current_event = event_code; |
| 276 | |
| 277 | /* update control attributes */ |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 278 | ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 279 | error_ret: |
| 280 | mutex_unlock(&chip->state_lock); |
| 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int ad7150_read_event_value(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 286 | const struct iio_chan_spec *chan, |
| 287 | enum iio_event_type type, |
| 288 | enum iio_event_direction dir, |
| 289 | enum iio_event_info info, |
| 290 | int *val, int *val2) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 291 | { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 292 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 293 | int rising = (dir == IIO_EV_DIR_RISING); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 294 | |
| 295 | /* Complex register sharing going on here */ |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 296 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 297 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 298 | *val = chip->mag_sensitivity[rising][chan->channel]; |
| 299 | return IIO_VAL_INT; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 300 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 301 | *val = chip->thresh_sensitivity[rising][chan->channel]; |
| 302 | return IIO_VAL_INT; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 303 | case IIO_EV_TYPE_THRESH: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 304 | *val = chip->threshold[rising][chan->channel]; |
| 305 | return IIO_VAL_INT; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 306 | default: |
| 307 | return -EINVAL; |
Joe Perches | a22526e | 2013-10-10 16:07:59 -0700 | [diff] [blame] | 308 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | static int ad7150_write_event_value(struct iio_dev *indio_dev, |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 312 | const struct iio_chan_spec *chan, |
| 313 | enum iio_event_type type, |
| 314 | enum iio_event_direction dir, |
| 315 | enum iio_event_info info, |
| 316 | int val, int val2) |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 317 | { |
| 318 | int ret; |
| 319 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 320 | int rising = (dir == IIO_EV_DIR_RISING); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 321 | |
| 322 | mutex_lock(&chip->state_lock); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 323 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 324 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 325 | chip->mag_sensitivity[rising][chan->channel] = val; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 326 | break; |
| 327 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 328 | chip->thresh_sensitivity[rising][chan->channel] = val; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 329 | break; |
| 330 | case IIO_EV_TYPE_THRESH: |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 331 | chip->threshold[rising][chan->channel] = val; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 332 | break; |
| 333 | default: |
| 334 | ret = -EINVAL; |
| 335 | goto error_ret; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 336 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 337 | |
| 338 | /* write back if active */ |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 339 | ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 340 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 341 | error_ret: |
Dan Carpenter | ee760ab | 2011-10-06 09:17:00 +0300 | [diff] [blame] | 342 | mutex_unlock(&chip->state_lock); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | static ssize_t ad7150_show_timeout(struct device *dev, |
| 347 | struct device_attribute *attr, |
| 348 | char *buf) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 349 | { |
Lars-Peter Clausen | d30a7f9 | 2012-05-12 15:39:45 +0200 | [diff] [blame] | 350 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 84f79ec | 2011-10-06 17:14:37 +0100 | [diff] [blame] | 351 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 352 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
| 353 | u8 value; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 354 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 355 | /* use the event code for consistency reasons */ |
Lars-Peter Clausen | da36716 | 2012-02-13 10:25:32 +0100 | [diff] [blame] | 356 | int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 357 | int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address) |
| 358 | == IIO_EV_DIR_RISING); |
| 359 | |
| 360 | switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) { |
| 361 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
| 362 | value = chip->mag_timeout[rising][chan]; |
| 363 | break; |
| 364 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
| 365 | value = chip->thresh_timeout[rising][chan]; |
| 366 | break; |
| 367 | default: |
| 368 | return -EINVAL; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 369 | } |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 370 | |
| 371 | return sprintf(buf, "%d\n", value); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 372 | } |
| 373 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 374 | static ssize_t ad7150_store_timeout(struct device *dev, |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 375 | struct device_attribute *attr, |
| 376 | const char *buf, |
| 377 | size_t len) |
| 378 | { |
Lars-Peter Clausen | d30a7f9 | 2012-05-12 15:39:45 +0200 | [diff] [blame] | 379 | struct iio_dev *indio_dev = dev_to_iio_dev(dev); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 380 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
| 381 | struct iio_dev_attr *this_attr = to_iio_dev_attr(attr); |
Lars-Peter Clausen | da36716 | 2012-02-13 10:25:32 +0100 | [diff] [blame] | 382 | int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 383 | enum iio_event_direction dir; |
| 384 | enum iio_event_type type; |
| 385 | int rising; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 386 | u8 data; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 387 | int ret; |
| 388 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 389 | type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address); |
| 390 | dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address); |
| 391 | rising = (dir == IIO_EV_DIR_RISING); |
| 392 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 393 | ret = kstrtou8(buf, 10, &data); |
| 394 | if (ret < 0) |
| 395 | return ret; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 396 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 397 | mutex_lock(&chip->state_lock); |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 398 | switch (type) { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 399 | case IIO_EV_TYPE_MAG_ADAPTIVE: |
| 400 | chip->mag_timeout[rising][chan] = data; |
| 401 | break; |
| 402 | case IIO_EV_TYPE_THRESH_ADAPTIVE: |
| 403 | chip->thresh_timeout[rising][chan] = data; |
| 404 | break; |
| 405 | default: |
| 406 | ret = -EINVAL; |
| 407 | goto error_ret; |
Peter Senna Tschudin | 73327b4 | 2012-09-28 10:57:00 +0100 | [diff] [blame] | 408 | } |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 409 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 410 | ret = ad7150_write_event_params(indio_dev, chan, type, dir); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 411 | error_ret: |
| 412 | mutex_unlock(&chip->state_lock); |
| 413 | |
| 414 | if (ret < 0) |
| 415 | return ret; |
| 416 | |
| 417 | return len; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 420 | #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \ |
| 421 | IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \ |
| 422 | S_IRUGO | S_IWUSR, \ |
| 423 | &ad7150_show_timeout, \ |
| 424 | &ad7150_store_timeout, \ |
| 425 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \ |
| 426 | chan, \ |
| 427 | IIO_EV_TYPE_##ev_type, \ |
| 428 | IIO_EV_DIR_##ev_dir)) |
| 429 | static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING); |
| 430 | static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING); |
| 431 | static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING); |
| 432 | static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING); |
| 433 | static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING); |
| 434 | static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING); |
| 435 | static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING); |
| 436 | static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 437 | |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 438 | static const struct iio_event_spec ad7150_events[] = { |
| 439 | { |
| 440 | .type = IIO_EV_TYPE_THRESH, |
| 441 | .dir = IIO_EV_DIR_RISING, |
| 442 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 443 | BIT(IIO_EV_INFO_ENABLE), |
| 444 | }, { |
| 445 | .type = IIO_EV_TYPE_THRESH, |
| 446 | .dir = IIO_EV_DIR_FALLING, |
| 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_RISING, |
| 452 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 453 | BIT(IIO_EV_INFO_ENABLE), |
| 454 | }, { |
| 455 | .type = IIO_EV_TYPE_THRESH_ADAPTIVE, |
| 456 | .dir = IIO_EV_DIR_FALLING, |
| 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_RISING, |
| 462 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 463 | BIT(IIO_EV_INFO_ENABLE), |
| 464 | }, { |
| 465 | .type = IIO_EV_TYPE_MAG_ADAPTIVE, |
| 466 | .dir = IIO_EV_DIR_FALLING, |
| 467 | .mask_separate = BIT(IIO_EV_INFO_VALUE) | |
| 468 | BIT(IIO_EV_INFO_ENABLE), |
| 469 | }, |
| 470 | }; |
| 471 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 472 | static const struct iio_chan_spec ad7150_channels[] = { |
| 473 | { |
| 474 | .type = IIO_CAPACITANCE, |
| 475 | .indexed = 1, |
| 476 | .channel = 0, |
Jonathan Cameron | 0221519 | 2013-02-27 19:37:59 +0000 | [diff] [blame] | 477 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 478 | BIT(IIO_CHAN_INFO_AVERAGE_RAW), |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 479 | .event_spec = ad7150_events, |
| 480 | .num_event_specs = ARRAY_SIZE(ad7150_events), |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 481 | }, { |
| 482 | .type = IIO_CAPACITANCE, |
| 483 | .indexed = 1, |
| 484 | .channel = 1, |
Jonathan Cameron | 0221519 | 2013-02-27 19:37:59 +0000 | [diff] [blame] | 485 | .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | |
| 486 | BIT(IIO_CHAN_INFO_AVERAGE_RAW), |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 487 | .event_spec = ad7150_events, |
| 488 | .num_event_specs = ARRAY_SIZE(ad7150_events), |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 489 | }, |
| 490 | }; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 491 | |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 492 | /* |
| 493 | * threshold events |
| 494 | */ |
| 495 | |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 496 | static irqreturn_t ad7150_event_handler(int irq, void *private) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 497 | { |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 498 | struct iio_dev *indio_dev = private; |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 499 | struct ad7150_chip_info *chip = iio_priv(indio_dev); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 500 | u8 int_status; |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 501 | s64 timestamp = iio_get_time_ns(); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 502 | int ret; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 503 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 504 | ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS); |
| 505 | if (ret < 0) |
| 506 | return IRQ_HANDLED; |
| 507 | |
| 508 | int_status = ret; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 509 | |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 510 | if ((int_status & AD7150_STATUS_OUT1) && |
| 511 | !(chip->old_state & AD7150_STATUS_OUT1)) |
| 512 | iio_push_event(indio_dev, |
Michael Hennerich | 1b99232 | 2011-10-19 14:41:08 +0200 | [diff] [blame] | 513 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, |
Jonathan Cameron | d91a0ab | 2011-05-18 14:42:13 +0100 | [diff] [blame] | 514 | 0, |
| 515 | IIO_EV_TYPE_THRESH, |
| 516 | IIO_EV_DIR_RISING), |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 517 | timestamp); |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 518 | else if ((!(int_status & AD7150_STATUS_OUT1)) && |
| 519 | (chip->old_state & AD7150_STATUS_OUT1)) |
| 520 | iio_push_event(indio_dev, |
Michael Hennerich | 1b99232 | 2011-10-19 14:41:08 +0200 | [diff] [blame] | 521 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, |
Jonathan Cameron | d91a0ab | 2011-05-18 14:42:13 +0100 | [diff] [blame] | 522 | 0, |
| 523 | IIO_EV_TYPE_THRESH, |
| 524 | IIO_EV_DIR_FALLING), |
| 525 | timestamp); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 526 | |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 527 | if ((int_status & AD7150_STATUS_OUT2) && |
| 528 | !(chip->old_state & AD7150_STATUS_OUT2)) |
| 529 | iio_push_event(indio_dev, |
Michael Hennerich | 1b99232 | 2011-10-19 14:41:08 +0200 | [diff] [blame] | 530 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, |
Jonathan Cameron | d91a0ab | 2011-05-18 14:42:13 +0100 | [diff] [blame] | 531 | 1, |
| 532 | IIO_EV_TYPE_THRESH, |
| 533 | IIO_EV_DIR_RISING), |
| 534 | timestamp); |
Jonathan Cameron | 5aa9618 | 2011-08-30 12:41:06 +0100 | [diff] [blame] | 535 | else if ((!(int_status & AD7150_STATUS_OUT2)) && |
| 536 | (chip->old_state & AD7150_STATUS_OUT2)) |
| 537 | iio_push_event(indio_dev, |
Michael Hennerich | 1b99232 | 2011-10-19 14:41:08 +0200 | [diff] [blame] | 538 | IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, |
Jonathan Cameron | d91a0ab | 2011-05-18 14:42:13 +0100 | [diff] [blame] | 539 | 1, |
| 540 | IIO_EV_TYPE_THRESH, |
| 541 | IIO_EV_DIR_FALLING), |
| 542 | timestamp); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 543 | /* store the status to avoid repushing same events */ |
| 544 | chip->old_state = int_status; |
| 545 | |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 546 | return IRQ_HANDLED; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 547 | } |
| 548 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 549 | /* Timeouts not currently handled by core */ |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 550 | static struct attribute *ad7150_event_attributes[] = { |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 551 | &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout |
| 552 | .dev_attr.attr, |
| 553 | &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout |
| 554 | .dev_attr.attr, |
| 555 | &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout |
| 556 | .dev_attr.attr, |
| 557 | &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout |
| 558 | .dev_attr.attr, |
| 559 | &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout |
| 560 | .dev_attr.attr, |
| 561 | &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout |
| 562 | .dev_attr.attr, |
| 563 | &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout |
| 564 | .dev_attr.attr, |
| 565 | &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout |
| 566 | .dev_attr.attr, |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 567 | NULL, |
| 568 | }; |
| 569 | |
| 570 | static struct attribute_group ad7150_event_attribute_group = { |
| 571 | .attrs = ad7150_event_attributes, |
Jonathan Cameron | 8e7d967 | 2011-08-30 12:32:45 +0100 | [diff] [blame] | 572 | .name = "events", |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 573 | }; |
| 574 | |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 575 | static const struct iio_info ad7150_info = { |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 576 | .event_attrs = &ad7150_event_attribute_group, |
| 577 | .driver_module = THIS_MODULE, |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 578 | .read_raw = &ad7150_read_raw, |
Lars-Peter Clausen | 1489d62 | 2013-10-07 15:11:00 +0100 | [diff] [blame] | 579 | .read_event_config_new = &ad7150_read_event_config, |
| 580 | .write_event_config_new = &ad7150_write_event_config, |
| 581 | .read_event_value_new = &ad7150_read_event_value, |
| 582 | .write_event_value_new = &ad7150_write_event_value, |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 583 | }; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 584 | |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 585 | /* |
| 586 | * device probe and remove |
| 587 | */ |
| 588 | |
Bill Pemberton | 4ae1c61 | 2012-11-19 13:21:57 -0500 | [diff] [blame] | 589 | static int ad7150_probe(struct i2c_client *client, |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 590 | const struct i2c_device_id *id) |
| 591 | { |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 592 | int ret; |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 593 | struct ad7150_chip_info *chip; |
| 594 | struct iio_dev *indio_dev; |
| 595 | |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 596 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip)); |
| 597 | if (!indio_dev) |
| 598 | return -ENOMEM; |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 599 | chip = iio_priv(indio_dev); |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 600 | mutex_init(&chip->state_lock); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 601 | /* this is only used for device removal purposes */ |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 602 | i2c_set_clientdata(client, indio_dev); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 603 | |
| 604 | chip->client = client; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 605 | |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 606 | indio_dev->name = id->name; |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 607 | indio_dev->channels = ad7150_channels; |
| 608 | indio_dev->num_channels = ARRAY_SIZE(ad7150_channels); |
| 609 | /* Establish that the iio_dev is a child of the i2c device */ |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 610 | indio_dev->dev.parent = &client->dev; |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 611 | |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 612 | indio_dev->info = &ad7150_info; |
Jonathan Cameron | 6fe8135 | 2011-05-18 14:42:37 +0100 | [diff] [blame] | 613 | |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 614 | indio_dev->modes = INDIO_DIRECT_MODE; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 615 | |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 616 | if (client->irq) { |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 617 | ret = devm_request_threaded_irq(&client->dev, client->irq, |
Jonathan Cameron | 9b5d9b0 | 2011-05-18 14:41:08 +0100 | [diff] [blame] | 618 | NULL, |
| 619 | &ad7150_event_handler, |
| 620 | IRQF_TRIGGER_RISING | |
Lars-Peter Clausen | a91aff1 | 2012-07-02 10:54:45 +0200 | [diff] [blame] | 621 | IRQF_TRIGGER_FALLING | |
| 622 | IRQF_ONESHOT, |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 623 | "ad7150_irq1", |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 624 | indio_dev); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 625 | if (ret) |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 626 | return ret; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 627 | } |
| 628 | |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 629 | if (client->dev.platform_data) { |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 630 | ret = devm_request_threaded_irq(&client->dev, *(unsigned int *) |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 631 | client->dev.platform_data, |
| 632 | NULL, |
| 633 | &ad7150_event_handler, |
| 634 | IRQF_TRIGGER_RISING | |
Lars-Peter Clausen | a91aff1 | 2012-07-02 10:54:45 +0200 | [diff] [blame] | 635 | IRQF_TRIGGER_FALLING | |
| 636 | IRQF_ONESHOT, |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 637 | "ad7150_irq2", |
| 638 | indio_dev); |
| 639 | if (ret) |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 640 | return ret; |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 641 | } |
| 642 | |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 643 | ret = iio_device_register(indio_dev); |
| 644 | if (ret) |
Sachin Kamat | d15b73c | 2013-08-24 20:24:00 +0100 | [diff] [blame] | 645 | return ret; |
Jonathan Cameron | 26d25ae | 2011-09-02 17:14:40 +0100 | [diff] [blame] | 646 | |
Jonathan Cameron | 531efd6 | 2011-09-02 17:25:20 +0100 | [diff] [blame] | 647 | dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n", |
| 648 | id->name, client->irq); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 649 | |
| 650 | return 0; |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 651 | } |
| 652 | |
Bill Pemberton | 447d4f2 | 2012-11-19 13:26:37 -0500 | [diff] [blame] | 653 | static int ad7150_remove(struct i2c_client *client) |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 654 | { |
Jonathan Cameron | 46a6af3 | 2011-06-27 13:07:21 +0100 | [diff] [blame] | 655 | struct iio_dev *indio_dev = i2c_get_clientdata(client); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 656 | |
Jonathan Cameron | d2fffd6 | 2011-10-14 14:46:58 +0100 | [diff] [blame] | 657 | iio_device_unregister(indio_dev); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 658 | |
| 659 | return 0; |
| 660 | } |
| 661 | |
| 662 | static const struct i2c_device_id ad7150_id[] = { |
| 663 | { "ad7150", 0 }, |
| 664 | { "ad7151", 0 }, |
| 665 | { "ad7156", 0 }, |
| 666 | {} |
| 667 | }; |
| 668 | |
| 669 | MODULE_DEVICE_TABLE(i2c, ad7150_id); |
| 670 | |
| 671 | static struct i2c_driver ad7150_driver = { |
| 672 | .driver = { |
| 673 | .name = "ad7150", |
| 674 | }, |
| 675 | .probe = ad7150_probe, |
Bill Pemberton | e543acf | 2012-11-19 13:21:38 -0500 | [diff] [blame] | 676 | .remove = ad7150_remove, |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 677 | .id_table = ad7150_id, |
| 678 | }; |
Lars-Peter Clausen | 6e5af18 | 2011-11-16 10:13:38 +0100 | [diff] [blame] | 679 | module_i2c_driver(ad7150_driver); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 680 | |
| 681 | MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>"); |
Michael Hennerich | 4e687dd | 2011-09-02 17:25:22 +0100 | [diff] [blame] | 682 | MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver"); |
Barry Song | 54c5be3 | 2010-10-27 21:43:53 -0400 | [diff] [blame] | 683 | MODULE_LICENSE("GPL v2"); |