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