blob: 7e7f9890a642ad7816f05f4a1ffa3952ff6a0f8d [file] [log] [blame]
Barry Song54c5be32010-10-27 21:43:53 -04001/*
2 * AD7150 capacitive sensor driver supporting AD7150/1/6
3 *
Michael Hennerich4e687dd2011-09-02 17:25:22 +01004 * Copyright 2010-2011 Analog Devices Inc.
Barry Song54c5be32010-10-27 21:43:53 -04005 *
6 * Licensed under the GPL-2 or later.
7 */
8
9#include <linux/interrupt.h>
Barry Song54c5be32010-10-27 21:43:53 -040010#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/slab.h>
Barry Song54c5be32010-10-27 21:43:53 -040013#include <linux/i2c.h>
Paul Gortmaker99c97852011-07-03 15:49:50 -040014#include <linux/module.h>
Barry Song54c5be32010-10-27 21:43:53 -040015
Jonathan Cameron06458e22012-04-25 15:54:58 +010016#include <linux/iio/iio.h>
17#include <linux/iio/sysfs.h>
18#include <linux/iio/events.h>
Barry Song54c5be32010-10-27 21:43:53 -040019/*
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 Song54c5be32010-10-27 21:43:53 -040027#define AD7150_CH2_DATA_HIGH 3
Barry Song54c5be32010-10-27 21:43:53 -040028#define AD7150_CH1_AVG_HIGH 5
Barry Song54c5be32010-10-27 21:43:53 -040029#define AD7150_CH2_AVG_HIGH 7
Barry Song54c5be32010-10-27 21:43:53 -040030#define AD7150_CH1_SENSITIVITY 9
31#define AD7150_CH1_THR_HOLD_H 9
32#define AD7150_CH1_TIMEOUT 10
Barry Song54c5be32010-10-27 21:43:53 -040033#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 Song54c5be32010-10-27 21:43:53 -040037#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 Cameron531efd62011-09-02 17:25:20 +010049/**
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 Song54c5be32010-10-27 21:43:53 -040071struct ad7150_chip_info {
Barry Song54c5be32010-10-27 21:43:53 -040072 struct i2c_client *client;
Jonathan Cameron531efd62011-09-02 17:25:20 +010073 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 Song54c5be32010-10-27 21:43:53 -040079 int old_state;
80 char *conversion_mode;
Jonathan Cameron531efd62011-09-02 17:25:20 +010081 struct mutex state_lock;
Barry Song54c5be32010-10-27 21:43:53 -040082};
83
Barry Song54c5be32010-10-27 21:43:53 -040084/*
Barry Song54c5be32010-10-27 21:43:53 -040085 * sysfs nodes
86 */
87
Jonathan Cameron531efd62011-09-02 17:25:20 +010088static 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
97static 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 Song54c5be32010-10-27 21:43:53 -0400102{
Jonathan Cameron531efd62011-09-02 17:25:20 +0100103 int ret;
104 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Barry Song54c5be32010-10-27 21:43:53 -0400105
Jonathan Cameron531efd62011-09-02 17:25:20 +0100106 switch (mask) {
Jonathan Camerone33e0752012-04-15 17:41:26 +0100107 case IIO_CHAN_INFO_RAW:
Jonathan Cameron531efd62011-09-02 17:25:20 +0100108 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 Cameronc8a9f802011-10-26 17:41:36 +0100114 case IIO_CHAN_INFO_AVERAGE_RAW:
Jonathan Cameron531efd62011-09-02 17:25:20 +0100115 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 Song54c5be32010-10-27 21:43:53 -0400123 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100124}
Barry Song54c5be32010-10-27 21:43:53 -0400125
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100126static 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 Cameron531efd62011-09-02 17:25:20 +0100129{
130 int ret;
131 u8 threshtype;
132 bool adaptive;
133 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100134
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 Clausen1489d622013-10-07 15:11:00 +0100142 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100143 case IIO_EV_TYPE_MAG_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100144 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100145 return adaptive && (threshtype == 0x1);
146 else
147 return adaptive && (threshtype == 0x0);
148 case IIO_EV_TYPE_THRESH_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100149 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100150 return adaptive && (threshtype == 0x3);
151 else
152 return adaptive && (threshtype == 0x2);
153
154 case IIO_EV_TYPE_THRESH:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100155 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100156 return !adaptive && (threshtype == 0x1);
157 else
158 return !adaptive && (threshtype == 0x0);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100159 default:
160 break;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100161 }
Barry Song54c5be32010-10-27 21:43:53 -0400162 return -EINVAL;
163}
164
Jonathan Cameron531efd62011-09-02 17:25:20 +0100165/* lock should be held */
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100166static 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 Cameron531efd62011-09-02 17:25:20 +0100169{
170 int ret;
171 u16 value;
172 u8 sens, timeout;
173 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100174 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 Song54c5be32010-10-27 21:43:53 -0400178
Jonathan Cameron531efd62011-09-02 17:25:20 +0100179 if (event_code != chip->current_event)
180 return 0;
181
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100182 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100183 /* 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 Tschudin73327b42012-09-28 10:57:00 +0100202 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100203 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
218static int ad7150_write_event_config(struct iio_dev *indio_dev,
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100219 const struct iio_chan_spec *chan, enum iio_event_type type,
220 enum iio_event_direction dir, int state)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100221{
222 u8 thresh_type, cfg, adaptive;
223 int ret;
224 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100225 int rising = (dir == IIO_EV_DIR_RISING);
226 u64 event_code;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100227
228 /* Something must always be turned on */
229 if (state == 0)
230 return -EINVAL;
231
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100232 event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100233 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 Clausen1489d622013-10-07 15:11:00 +0100242 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100243 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 Tschudin73327b42012-09-28 10:57:00 +0100267 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100268
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 Clausen1489d622013-10-07 15:11:00 +0100278 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100279error_ret:
280 mutex_unlock(&chip->state_lock);
281
282 return 0;
283}
284
285static int ad7150_read_event_value(struct iio_dev *indio_dev,
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100286 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 Cameron531efd62011-09-02 17:25:20 +0100291{
Jonathan Cameron531efd62011-09-02 17:25:20 +0100292 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100293 int rising = (dir == IIO_EV_DIR_RISING);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100294
295 /* Complex register sharing going on here */
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100296 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100297 case IIO_EV_TYPE_MAG_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100298 *val = chip->mag_sensitivity[rising][chan->channel];
299 return IIO_VAL_INT;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100300 case IIO_EV_TYPE_THRESH_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100301 *val = chip->thresh_sensitivity[rising][chan->channel];
302 return IIO_VAL_INT;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100303 case IIO_EV_TYPE_THRESH:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100304 *val = chip->threshold[rising][chan->channel];
305 return IIO_VAL_INT;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100306 default:
307 return -EINVAL;
Joe Perchesa22526e2013-10-10 16:07:59 -0700308 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100309}
310
311static int ad7150_write_event_value(struct iio_dev *indio_dev,
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100312 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 Cameron531efd62011-09-02 17:25:20 +0100317{
318 int ret;
319 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100320 int rising = (dir == IIO_EV_DIR_RISING);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100321
322 mutex_lock(&chip->state_lock);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100323 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100324 case IIO_EV_TYPE_MAG_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100325 chip->mag_sensitivity[rising][chan->channel] = val;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100326 break;
327 case IIO_EV_TYPE_THRESH_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100328 chip->thresh_sensitivity[rising][chan->channel] = val;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100329 break;
330 case IIO_EV_TYPE_THRESH:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100331 chip->threshold[rising][chan->channel] = val;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100332 break;
333 default:
334 ret = -EINVAL;
335 goto error_ret;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100336 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100337
338 /* write back if active */
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100339 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100340
Jonathan Cameron531efd62011-09-02 17:25:20 +0100341error_ret:
Dan Carpenteree760ab2011-10-06 09:17:00 +0300342 mutex_unlock(&chip->state_lock);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100343 return ret;
344}
345
346static ssize_t ad7150_show_timeout(struct device *dev,
347 struct device_attribute *attr,
348 char *buf)
Barry Song54c5be32010-10-27 21:43:53 -0400349{
Lars-Peter Clausend30a7f92012-05-12 15:39:45 +0200350 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100351 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100352 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
353 u8 value;
Barry Song54c5be32010-10-27 21:43:53 -0400354
Jonathan Cameron531efd62011-09-02 17:25:20 +0100355 /* use the event code for consistency reasons */
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100356 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100357 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 Tschudin73327b42012-09-28 10:57:00 +0100369 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100370
371 return sprintf(buf, "%d\n", value);
Barry Song54c5be32010-10-27 21:43:53 -0400372}
373
Jonathan Cameron531efd62011-09-02 17:25:20 +0100374static ssize_t ad7150_store_timeout(struct device *dev,
Barry Song54c5be32010-10-27 21:43:53 -0400375 struct device_attribute *attr,
376 const char *buf,
377 size_t len)
378{
Lars-Peter Clausend30a7f92012-05-12 15:39:45 +0200379 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100380 struct ad7150_chip_info *chip = iio_priv(indio_dev);
381 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100382 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100383 enum iio_event_direction dir;
384 enum iio_event_type type;
385 int rising;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100386 u8 data;
Barry Song54c5be32010-10-27 21:43:53 -0400387 int ret;
388
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100389 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 Cameron531efd62011-09-02 17:25:20 +0100393 ret = kstrtou8(buf, 10, &data);
394 if (ret < 0)
395 return ret;
Barry Song54c5be32010-10-27 21:43:53 -0400396
Jonathan Cameron531efd62011-09-02 17:25:20 +0100397 mutex_lock(&chip->state_lock);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100398 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100399 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 Tschudin73327b42012-09-28 10:57:00 +0100408 }
Barry Song54c5be32010-10-27 21:43:53 -0400409
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100410 ret = ad7150_write_event_params(indio_dev, chan, type, dir);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100411error_ret:
412 mutex_unlock(&chip->state_lock);
413
414 if (ret < 0)
415 return ret;
416
417 return len;
Barry Song54c5be32010-10-27 21:43:53 -0400418}
419
Jonathan Cameron531efd62011-09-02 17:25:20 +0100420#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))
429static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
430static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
431static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
432static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
433static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
434static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
435static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
436static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
Barry Song54c5be32010-10-27 21:43:53 -0400437
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100438static 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 Cameron531efd62011-09-02 17:25:20 +0100472static const struct iio_chan_spec ad7150_channels[] = {
473 {
474 .type = IIO_CAPACITANCE,
475 .indexed = 1,
476 .channel = 0,
Jonathan Cameron02215192013-02-27 19:37:59 +0000477 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
478 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100479 .event_spec = ad7150_events,
480 .num_event_specs = ARRAY_SIZE(ad7150_events),
Jonathan Cameron531efd62011-09-02 17:25:20 +0100481 }, {
482 .type = IIO_CAPACITANCE,
483 .indexed = 1,
484 .channel = 1,
Jonathan Cameron02215192013-02-27 19:37:59 +0000485 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
486 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100487 .event_spec = ad7150_events,
488 .num_event_specs = ARRAY_SIZE(ad7150_events),
Jonathan Cameron531efd62011-09-02 17:25:20 +0100489 },
490};
Barry Song54c5be32010-10-27 21:43:53 -0400491
Barry Song54c5be32010-10-27 21:43:53 -0400492/*
493 * threshold events
494 */
495
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100496static irqreturn_t ad7150_event_handler(int irq, void *private)
Barry Song54c5be32010-10-27 21:43:53 -0400497{
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100498 struct iio_dev *indio_dev = private;
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100499 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Barry Song54c5be32010-10-27 21:43:53 -0400500 u8 int_status;
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100501 s64 timestamp = iio_get_time_ns();
Jonathan Cameron531efd62011-09-02 17:25:20 +0100502 int ret;
Barry Song54c5be32010-10-27 21:43:53 -0400503
Jonathan Cameron531efd62011-09-02 17:25:20 +0100504 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
505 if (ret < 0)
506 return IRQ_HANDLED;
507
508 int_status = ret;
Barry Song54c5be32010-10-27 21:43:53 -0400509
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100510 if ((int_status & AD7150_STATUS_OUT1) &&
511 !(chip->old_state & AD7150_STATUS_OUT1))
512 iio_push_event(indio_dev,
Michael Hennerich1b992322011-10-19 14:41:08 +0200513 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
Jonathan Camerond91a0ab2011-05-18 14:42:13 +0100514 0,
515 IIO_EV_TYPE_THRESH,
516 IIO_EV_DIR_RISING),
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100517 timestamp);
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100518 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
519 (chip->old_state & AD7150_STATUS_OUT1))
520 iio_push_event(indio_dev,
Michael Hennerich1b992322011-10-19 14:41:08 +0200521 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
Jonathan Camerond91a0ab2011-05-18 14:42:13 +0100522 0,
523 IIO_EV_TYPE_THRESH,
524 IIO_EV_DIR_FALLING),
525 timestamp);
Barry Song54c5be32010-10-27 21:43:53 -0400526
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100527 if ((int_status & AD7150_STATUS_OUT2) &&
528 !(chip->old_state & AD7150_STATUS_OUT2))
529 iio_push_event(indio_dev,
Michael Hennerich1b992322011-10-19 14:41:08 +0200530 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
Jonathan Camerond91a0ab2011-05-18 14:42:13 +0100531 1,
532 IIO_EV_TYPE_THRESH,
533 IIO_EV_DIR_RISING),
534 timestamp);
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100535 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
536 (chip->old_state & AD7150_STATUS_OUT2))
537 iio_push_event(indio_dev,
Michael Hennerich1b992322011-10-19 14:41:08 +0200538 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
Jonathan Camerond91a0ab2011-05-18 14:42:13 +0100539 1,
540 IIO_EV_TYPE_THRESH,
541 IIO_EV_DIR_FALLING),
542 timestamp);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100543 /* store the status to avoid repushing same events */
544 chip->old_state = int_status;
545
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100546 return IRQ_HANDLED;
Barry Song54c5be32010-10-27 21:43:53 -0400547}
548
Jonathan Cameron531efd62011-09-02 17:25:20 +0100549/* Timeouts not currently handled by core */
Barry Song54c5be32010-10-27 21:43:53 -0400550static struct attribute *ad7150_event_attributes[] = {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100551 &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 Song54c5be32010-10-27 21:43:53 -0400567 NULL,
568};
569
570static struct attribute_group ad7150_event_attribute_group = {
571 .attrs = ad7150_event_attributes,
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100572 .name = "events",
Barry Song54c5be32010-10-27 21:43:53 -0400573};
574
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100575static const struct iio_info ad7150_info = {
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100576 .event_attrs = &ad7150_event_attribute_group,
577 .driver_module = THIS_MODULE,
Jonathan Cameron531efd62011-09-02 17:25:20 +0100578 .read_raw = &ad7150_read_raw,
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100579 .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 Cameron6fe81352011-05-18 14:42:37 +0100583};
Jonathan Cameron531efd62011-09-02 17:25:20 +0100584
Barry Song54c5be32010-10-27 21:43:53 -0400585/*
586 * device probe and remove
587 */
588
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500589static int ad7150_probe(struct i2c_client *client,
Barry Song54c5be32010-10-27 21:43:53 -0400590 const struct i2c_device_id *id)
591{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100592 int ret;
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100593 struct ad7150_chip_info *chip;
594 struct iio_dev *indio_dev;
595
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100596 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
597 if (!indio_dev)
598 return -ENOMEM;
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100599 chip = iio_priv(indio_dev);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100600 mutex_init(&chip->state_lock);
Barry Song54c5be32010-10-27 21:43:53 -0400601 /* this is only used for device removal purposes */
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100602 i2c_set_clientdata(client, indio_dev);
Barry Song54c5be32010-10-27 21:43:53 -0400603
604 chip->client = client;
Barry Song54c5be32010-10-27 21:43:53 -0400605
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100606 indio_dev->name = id->name;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100607 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 Cameron46a6af32011-06-27 13:07:21 +0100610 indio_dev->dev.parent = &client->dev;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100611
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100612 indio_dev->info = &ad7150_info;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100613
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100614 indio_dev->modes = INDIO_DIRECT_MODE;
Barry Song54c5be32010-10-27 21:43:53 -0400615
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100616 if (client->irq) {
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100617 ret = devm_request_threaded_irq(&client->dev, client->irq,
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100618 NULL,
619 &ad7150_event_handler,
620 IRQF_TRIGGER_RISING |
Lars-Peter Clausena91aff12012-07-02 10:54:45 +0200621 IRQF_TRIGGER_FALLING |
622 IRQF_ONESHOT,
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100623 "ad7150_irq1",
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100624 indio_dev);
Barry Song54c5be32010-10-27 21:43:53 -0400625 if (ret)
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100626 return ret;
Barry Song54c5be32010-10-27 21:43:53 -0400627 }
628
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100629 if (client->dev.platform_data) {
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100630 ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100631 client->dev.platform_data,
632 NULL,
633 &ad7150_event_handler,
634 IRQF_TRIGGER_RISING |
Lars-Peter Clausena91aff12012-07-02 10:54:45 +0200635 IRQF_TRIGGER_FALLING |
636 IRQF_ONESHOT,
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100637 "ad7150_irq2",
638 indio_dev);
639 if (ret)
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100640 return ret;
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100641 }
642
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100643 ret = iio_device_register(indio_dev);
644 if (ret)
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100645 return ret;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100646
Jonathan Cameron531efd62011-09-02 17:25:20 +0100647 dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
648 id->name, client->irq);
Barry Song54c5be32010-10-27 21:43:53 -0400649
650 return 0;
Barry Song54c5be32010-10-27 21:43:53 -0400651}
652
Bill Pemberton447d4f22012-11-19 13:26:37 -0500653static int ad7150_remove(struct i2c_client *client)
Barry Song54c5be32010-10-27 21:43:53 -0400654{
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100655 struct iio_dev *indio_dev = i2c_get_clientdata(client);
Barry Song54c5be32010-10-27 21:43:53 -0400656
Jonathan Camerond2fffd62011-10-14 14:46:58 +0100657 iio_device_unregister(indio_dev);
Barry Song54c5be32010-10-27 21:43:53 -0400658
659 return 0;
660}
661
662static const struct i2c_device_id ad7150_id[] = {
663 { "ad7150", 0 },
664 { "ad7151", 0 },
665 { "ad7156", 0 },
666 {}
667};
668
669MODULE_DEVICE_TABLE(i2c, ad7150_id);
670
671static struct i2c_driver ad7150_driver = {
672 .driver = {
673 .name = "ad7150",
674 },
675 .probe = ad7150_probe,
Bill Pembertone543acf2012-11-19 13:21:38 -0500676 .remove = ad7150_remove,
Barry Song54c5be32010-10-27 21:43:53 -0400677 .id_table = ad7150_id,
678};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100679module_i2c_driver(ad7150_driver);
Barry Song54c5be32010-10-27 21:43:53 -0400680
681MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100682MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
Barry Song54c5be32010-10-27 21:43:53 -0400683MODULE_LICENSE("GPL v2");