blob: 5578a077fcfbd2699209530fa9c1a1605bb0007c [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
Shraddha Barkec0559ae2015-12-29 16:57:30 +053024#define AD7150_STATUS_OUT1 BIT(3)
25#define AD7150_STATUS_OUT2 BIT(5)
Barry Song54c5be32010-10-27 21:43:53 -040026#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
Shraddha Barkec0559ae2015-12-29 16:57:30 +053039#define AD7150_CFG_FIX BIT(7)
Barry Song54c5be32010-10-27 21:43:53 -040040#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);
Catalina Mocanu288903f2014-09-19 15:55:05 -0700146 return adaptive && (threshtype == 0x0);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100147 case IIO_EV_TYPE_THRESH_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100148 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100149 return adaptive && (threshtype == 0x3);
Catalina Mocanu288903f2014-09-19 15:55:05 -0700150 return adaptive && (threshtype == 0x2);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100151 case IIO_EV_TYPE_THRESH:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100152 if (dir == IIO_EV_DIR_RISING)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100153 return !adaptive && (threshtype == 0x1);
Catalina Mocanu288903f2014-09-19 15:55:05 -0700154 return !adaptive && (threshtype == 0x0);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100155 default:
156 break;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100157 }
Barry Song54c5be32010-10-27 21:43:53 -0400158 return -EINVAL;
159}
160
Jonathan Cameron531efd62011-09-02 17:25:20 +0100161/* lock should be held */
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100162static int ad7150_write_event_params(struct iio_dev *indio_dev,
Shraddha Barkec4f0ebd2015-12-29 16:57:31 +0530163 unsigned int chan,
164 enum iio_event_type type,
165 enum iio_event_direction dir)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100166{
167 int ret;
168 u16 value;
169 u8 sens, timeout;
170 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100171 int rising = (dir == IIO_EV_DIR_RISING);
172 u64 event_code;
173
174 event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir);
Barry Song54c5be32010-10-27 21:43:53 -0400175
Jonathan Cameron531efd62011-09-02 17:25:20 +0100176 if (event_code != chip->current_event)
177 return 0;
178
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100179 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100180 /* Note completely different from the adaptive versions */
181 case IIO_EV_TYPE_THRESH:
182 value = chip->threshold[rising][chan];
Ioana Ciornei4a737ec2015-10-14 22:34:27 +0300183 return i2c_smbus_write_word_data(chip->client,
Jonathan Cameron531efd62011-09-02 17:25:20 +0100184 ad7150_addresses[chan][3],
185 swab16(value));
Jonathan Cameron531efd62011-09-02 17:25:20 +0100186 case IIO_EV_TYPE_MAG_ADAPTIVE:
187 sens = chip->mag_sensitivity[rising][chan];
188 timeout = chip->mag_timeout[rising][chan];
189 break;
190 case IIO_EV_TYPE_THRESH_ADAPTIVE:
191 sens = chip->thresh_sensitivity[rising][chan];
192 timeout = chip->thresh_timeout[rising][chan];
193 break;
194 default:
195 return -EINVAL;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100196 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100197 ret = i2c_smbus_write_byte_data(chip->client,
198 ad7150_addresses[chan][4],
199 sens);
200 if (ret < 0)
201 return ret;
202
203 ret = i2c_smbus_write_byte_data(chip->client,
204 ad7150_addresses[chan][5],
205 timeout);
206 if (ret < 0)
207 return ret;
208
209 return 0;
210}
211
212static int ad7150_write_event_config(struct iio_dev *indio_dev,
Shraddha Barkec4f0ebd2015-12-29 16:57:31 +0530213 const struct iio_chan_spec *chan,
214 enum iio_event_type type,
215 enum iio_event_direction dir, int state)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100216{
217 u8 thresh_type, cfg, adaptive;
218 int ret;
219 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100220 int rising = (dir == IIO_EV_DIR_RISING);
221 u64 event_code;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100222
223 /* Something must always be turned on */
Cristina Morarue6e65f92015-10-20 22:55:43 +0300224 if (!state)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100225 return -EINVAL;
226
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100227 event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100228 if (event_code == chip->current_event)
229 return 0;
230 mutex_lock(&chip->state_lock);
231 ret = i2c_smbus_read_byte_data(chip->client, AD7150_CFG);
232 if (ret < 0)
233 goto error_ret;
234
235 cfg = ret & ~((0x03 << 5) | (0x1 << 7));
236
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100237 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100238 case IIO_EV_TYPE_MAG_ADAPTIVE:
239 adaptive = 1;
240 if (rising)
241 thresh_type = 0x1;
242 else
243 thresh_type = 0x0;
244 break;
245 case IIO_EV_TYPE_THRESH_ADAPTIVE:
246 adaptive = 1;
247 if (rising)
248 thresh_type = 0x3;
249 else
250 thresh_type = 0x2;
251 break;
252 case IIO_EV_TYPE_THRESH:
253 adaptive = 0;
254 if (rising)
255 thresh_type = 0x1;
256 else
257 thresh_type = 0x0;
258 break;
259 default:
260 ret = -EINVAL;
261 goto error_ret;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100262 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100263
264 cfg |= (!adaptive << 7) | (thresh_type << 5);
265
266 ret = i2c_smbus_write_byte_data(chip->client, AD7150_CFG, cfg);
267 if (ret < 0)
268 goto error_ret;
269
270 chip->current_event = event_code;
271
272 /* update control attributes */
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100273 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100274error_ret:
275 mutex_unlock(&chip->state_lock);
276
277 return 0;
278}
279
280static int ad7150_read_event_value(struct iio_dev *indio_dev,
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100281 const struct iio_chan_spec *chan,
282 enum iio_event_type type,
283 enum iio_event_direction dir,
284 enum iio_event_info info,
285 int *val, int *val2)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100286{
Jonathan Cameron531efd62011-09-02 17:25:20 +0100287 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100288 int rising = (dir == IIO_EV_DIR_RISING);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100289
290 /* Complex register sharing going on here */
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100291 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100292 case IIO_EV_TYPE_MAG_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100293 *val = chip->mag_sensitivity[rising][chan->channel];
294 return IIO_VAL_INT;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100295 case IIO_EV_TYPE_THRESH_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100296 *val = chip->thresh_sensitivity[rising][chan->channel];
297 return IIO_VAL_INT;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100298 case IIO_EV_TYPE_THRESH:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100299 *val = chip->threshold[rising][chan->channel];
300 return IIO_VAL_INT;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100301 default:
302 return -EINVAL;
Joe Perchesa22526e2013-10-10 16:07:59 -0700303 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100304}
305
306static int ad7150_write_event_value(struct iio_dev *indio_dev,
Shraddha Barkec4f0ebd2015-12-29 16:57:31 +0530307 const struct iio_chan_spec *chan,
308 enum iio_event_type type,
309 enum iio_event_direction dir,
310 enum iio_event_info info,
311 int val, int val2)
Jonathan Cameron531efd62011-09-02 17:25:20 +0100312{
313 int ret;
314 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100315 int rising = (dir == IIO_EV_DIR_RISING);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100316
317 mutex_lock(&chip->state_lock);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100318 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100319 case IIO_EV_TYPE_MAG_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100320 chip->mag_sensitivity[rising][chan->channel] = val;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100321 break;
322 case IIO_EV_TYPE_THRESH_ADAPTIVE:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100323 chip->thresh_sensitivity[rising][chan->channel] = val;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100324 break;
325 case IIO_EV_TYPE_THRESH:
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100326 chip->threshold[rising][chan->channel] = val;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100327 break;
328 default:
329 ret = -EINVAL;
330 goto error_ret;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100331 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100332
333 /* write back if active */
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100334 ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100335
Jonathan Cameron531efd62011-09-02 17:25:20 +0100336error_ret:
Dan Carpenteree760ab2011-10-06 09:17:00 +0300337 mutex_unlock(&chip->state_lock);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100338 return ret;
339}
340
341static ssize_t ad7150_show_timeout(struct device *dev,
342 struct device_attribute *attr,
343 char *buf)
Barry Song54c5be32010-10-27 21:43:53 -0400344{
Lars-Peter Clausend30a7f92012-05-12 15:39:45 +0200345 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron84f79ec2011-10-06 17:14:37 +0100346 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100347 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
348 u8 value;
Barry Song54c5be32010-10-27 21:43:53 -0400349
Jonathan Cameron531efd62011-09-02 17:25:20 +0100350 /* use the event code for consistency reasons */
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100351 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100352 int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
353 == IIO_EV_DIR_RISING);
354
355 switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
356 case IIO_EV_TYPE_MAG_ADAPTIVE:
357 value = chip->mag_timeout[rising][chan];
358 break;
359 case IIO_EV_TYPE_THRESH_ADAPTIVE:
360 value = chip->thresh_timeout[rising][chan];
361 break;
362 default:
363 return -EINVAL;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100364 }
Jonathan Cameron531efd62011-09-02 17:25:20 +0100365
366 return sprintf(buf, "%d\n", value);
Barry Song54c5be32010-10-27 21:43:53 -0400367}
368
Jonathan Cameron531efd62011-09-02 17:25:20 +0100369static ssize_t ad7150_store_timeout(struct device *dev,
Shraddha Barkec4f0ebd2015-12-29 16:57:31 +0530370 struct device_attribute *attr,
371 const char *buf,
372 size_t len)
Barry Song54c5be32010-10-27 21:43:53 -0400373{
Lars-Peter Clausend30a7f92012-05-12 15:39:45 +0200374 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100375 struct ad7150_chip_info *chip = iio_priv(indio_dev);
376 struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
Lars-Peter Clausenda367162012-02-13 10:25:32 +0100377 int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100378 enum iio_event_direction dir;
379 enum iio_event_type type;
380 int rising;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100381 u8 data;
Barry Song54c5be32010-10-27 21:43:53 -0400382 int ret;
383
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100384 type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
385 dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
386 rising = (dir == IIO_EV_DIR_RISING);
387
Jonathan Cameron531efd62011-09-02 17:25:20 +0100388 ret = kstrtou8(buf, 10, &data);
389 if (ret < 0)
390 return ret;
Barry Song54c5be32010-10-27 21:43:53 -0400391
Jonathan Cameron531efd62011-09-02 17:25:20 +0100392 mutex_lock(&chip->state_lock);
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100393 switch (type) {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100394 case IIO_EV_TYPE_MAG_ADAPTIVE:
395 chip->mag_timeout[rising][chan] = data;
396 break;
397 case IIO_EV_TYPE_THRESH_ADAPTIVE:
398 chip->thresh_timeout[rising][chan] = data;
399 break;
400 default:
401 ret = -EINVAL;
402 goto error_ret;
Peter Senna Tschudin73327b42012-09-28 10:57:00 +0100403 }
Barry Song54c5be32010-10-27 21:43:53 -0400404
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100405 ret = ad7150_write_event_params(indio_dev, chan, type, dir);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100406error_ret:
407 mutex_unlock(&chip->state_lock);
408
409 if (ret < 0)
410 return ret;
411
412 return len;
Barry Song54c5be32010-10-27 21:43:53 -0400413}
414
Jonathan Cameron531efd62011-09-02 17:25:20 +0100415#define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir) \
416 IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
417 S_IRUGO | S_IWUSR, \
418 &ad7150_show_timeout, \
419 &ad7150_store_timeout, \
420 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, \
421 chan, \
422 IIO_EV_TYPE_##ev_type, \
423 IIO_EV_DIR_##ev_dir))
424static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
425static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
426static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
427static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
428static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
429static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
430static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
431static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
Barry Song54c5be32010-10-27 21:43:53 -0400432
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100433static const struct iio_event_spec ad7150_events[] = {
434 {
435 .type = IIO_EV_TYPE_THRESH,
436 .dir = IIO_EV_DIR_RISING,
437 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
438 BIT(IIO_EV_INFO_ENABLE),
439 }, {
440 .type = IIO_EV_TYPE_THRESH,
441 .dir = IIO_EV_DIR_FALLING,
442 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
443 BIT(IIO_EV_INFO_ENABLE),
444 }, {
445 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
446 .dir = IIO_EV_DIR_RISING,
447 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
448 BIT(IIO_EV_INFO_ENABLE),
449 }, {
450 .type = IIO_EV_TYPE_THRESH_ADAPTIVE,
451 .dir = IIO_EV_DIR_FALLING,
452 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
453 BIT(IIO_EV_INFO_ENABLE),
454 }, {
455 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
456 .dir = IIO_EV_DIR_RISING,
457 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
458 BIT(IIO_EV_INFO_ENABLE),
459 }, {
460 .type = IIO_EV_TYPE_MAG_ADAPTIVE,
461 .dir = IIO_EV_DIR_FALLING,
462 .mask_separate = BIT(IIO_EV_INFO_VALUE) |
463 BIT(IIO_EV_INFO_ENABLE),
464 },
465};
466
Jonathan Cameron531efd62011-09-02 17:25:20 +0100467static const struct iio_chan_spec ad7150_channels[] = {
468 {
469 .type = IIO_CAPACITANCE,
470 .indexed = 1,
471 .channel = 0,
Jonathan Cameron02215192013-02-27 19:37:59 +0000472 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
473 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100474 .event_spec = ad7150_events,
475 .num_event_specs = ARRAY_SIZE(ad7150_events),
Jonathan Cameron531efd62011-09-02 17:25:20 +0100476 }, {
477 .type = IIO_CAPACITANCE,
478 .indexed = 1,
479 .channel = 1,
Jonathan Cameron02215192013-02-27 19:37:59 +0000480 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
481 BIT(IIO_CHAN_INFO_AVERAGE_RAW),
Lars-Peter Clausen1489d622013-10-07 15:11:00 +0100482 .event_spec = ad7150_events,
483 .num_event_specs = ARRAY_SIZE(ad7150_events),
Jonathan Cameron531efd62011-09-02 17:25:20 +0100484 },
485};
Barry Song54c5be32010-10-27 21:43:53 -0400486
Barry Song54c5be32010-10-27 21:43:53 -0400487/*
488 * threshold events
489 */
490
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100491static irqreturn_t ad7150_event_handler(int irq, void *private)
Barry Song54c5be32010-10-27 21:43:53 -0400492{
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100493 struct iio_dev *indio_dev = private;
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100494 struct ad7150_chip_info *chip = iio_priv(indio_dev);
Barry Song54c5be32010-10-27 21:43:53 -0400495 u8 int_status;
Gregor Boiriebc2b7da2016-03-09 19:05:49 +0100496 s64 timestamp = iio_get_time_ns(indio_dev);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100497 int ret;
Barry Song54c5be32010-10-27 21:43:53 -0400498
Jonathan Cameron531efd62011-09-02 17:25:20 +0100499 ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
500 if (ret < 0)
501 return IRQ_HANDLED;
502
503 int_status = ret;
Barry Song54c5be32010-10-27 21:43:53 -0400504
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100505 if ((int_status & AD7150_STATUS_OUT1) &&
506 !(chip->old_state & AD7150_STATUS_OUT1))
507 iio_push_event(indio_dev,
Michael Hennerich1b992322011-10-19 14:41:08 +0200508 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
Jonathan Camerond91a0ab2011-05-18 14:42:13 +0100509 0,
510 IIO_EV_TYPE_THRESH,
511 IIO_EV_DIR_RISING),
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100512 timestamp);
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100513 else if ((!(int_status & AD7150_STATUS_OUT1)) &&
514 (chip->old_state & AD7150_STATUS_OUT1))
515 iio_push_event(indio_dev,
Michael Hennerich1b992322011-10-19 14:41:08 +0200516 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
Jonathan Camerond91a0ab2011-05-18 14:42:13 +0100517 0,
518 IIO_EV_TYPE_THRESH,
519 IIO_EV_DIR_FALLING),
520 timestamp);
Barry Song54c5be32010-10-27 21:43:53 -0400521
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100522 if ((int_status & AD7150_STATUS_OUT2) &&
523 !(chip->old_state & AD7150_STATUS_OUT2))
524 iio_push_event(indio_dev,
Michael Hennerich1b992322011-10-19 14:41:08 +0200525 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
Jonathan Camerond91a0ab2011-05-18 14:42:13 +0100526 1,
527 IIO_EV_TYPE_THRESH,
528 IIO_EV_DIR_RISING),
529 timestamp);
Jonathan Cameron5aa96182011-08-30 12:41:06 +0100530 else if ((!(int_status & AD7150_STATUS_OUT2)) &&
531 (chip->old_state & AD7150_STATUS_OUT2))
532 iio_push_event(indio_dev,
Michael Hennerich1b992322011-10-19 14:41:08 +0200533 IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
Jonathan Camerond91a0ab2011-05-18 14:42:13 +0100534 1,
535 IIO_EV_TYPE_THRESH,
536 IIO_EV_DIR_FALLING),
537 timestamp);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100538 /* store the status to avoid repushing same events */
539 chip->old_state = int_status;
540
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100541 return IRQ_HANDLED;
Barry Song54c5be32010-10-27 21:43:53 -0400542}
543
Jonathan Cameron531efd62011-09-02 17:25:20 +0100544/* Timeouts not currently handled by core */
Barry Song54c5be32010-10-27 21:43:53 -0400545static struct attribute *ad7150_event_attributes[] = {
Jonathan Cameron531efd62011-09-02 17:25:20 +0100546 &iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
547 .dev_attr.attr,
548 &iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
549 .dev_attr.attr,
550 &iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
551 .dev_attr.attr,
552 &iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
553 .dev_attr.attr,
554 &iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
555 .dev_attr.attr,
556 &iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
557 .dev_attr.attr,
558 &iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
559 .dev_attr.attr,
560 &iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
561 .dev_attr.attr,
Barry Song54c5be32010-10-27 21:43:53 -0400562 NULL,
563};
564
565static struct attribute_group ad7150_event_attribute_group = {
566 .attrs = ad7150_event_attributes,
Jonathan Cameron8e7d9672011-08-30 12:32:45 +0100567 .name = "events",
Barry Song54c5be32010-10-27 21:43:53 -0400568};
569
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100570static const struct iio_info ad7150_info = {
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100571 .event_attrs = &ad7150_event_attribute_group,
572 .driver_module = THIS_MODULE,
Jonathan Cameron531efd62011-09-02 17:25:20 +0100573 .read_raw = &ad7150_read_raw,
Lars-Peter Clausencb955852013-12-07 10:45:00 +0000574 .read_event_config = &ad7150_read_event_config,
575 .write_event_config = &ad7150_write_event_config,
576 .read_event_value = &ad7150_read_event_value,
577 .write_event_value = &ad7150_write_event_value,
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100578};
Jonathan Cameron531efd62011-09-02 17:25:20 +0100579
Barry Song54c5be32010-10-27 21:43:53 -0400580/*
581 * device probe and remove
582 */
583
Bill Pemberton4ae1c612012-11-19 13:21:57 -0500584static int ad7150_probe(struct i2c_client *client,
Shraddha Barkec4f0ebd2015-12-29 16:57:31 +0530585 const struct i2c_device_id *id)
Barry Song54c5be32010-10-27 21:43:53 -0400586{
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100587 int ret;
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100588 struct ad7150_chip_info *chip;
589 struct iio_dev *indio_dev;
590
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100591 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
592 if (!indio_dev)
593 return -ENOMEM;
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100594 chip = iio_priv(indio_dev);
Jonathan Cameron531efd62011-09-02 17:25:20 +0100595 mutex_init(&chip->state_lock);
Barry Song54c5be32010-10-27 21:43:53 -0400596 /* this is only used for device removal purposes */
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100597 i2c_set_clientdata(client, indio_dev);
Barry Song54c5be32010-10-27 21:43:53 -0400598
599 chip->client = client;
Barry Song54c5be32010-10-27 21:43:53 -0400600
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100601 indio_dev->name = id->name;
Jonathan Cameron531efd62011-09-02 17:25:20 +0100602 indio_dev->channels = ad7150_channels;
603 indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
604 /* Establish that the iio_dev is a child of the i2c device */
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100605 indio_dev->dev.parent = &client->dev;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100606
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100607 indio_dev->info = &ad7150_info;
Jonathan Cameron6fe81352011-05-18 14:42:37 +0100608
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100609 indio_dev->modes = INDIO_DIRECT_MODE;
Barry Song54c5be32010-10-27 21:43:53 -0400610
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100611 if (client->irq) {
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100612 ret = devm_request_threaded_irq(&client->dev, client->irq,
Jonathan Cameron9b5d9b02011-05-18 14:41:08 +0100613 NULL,
614 &ad7150_event_handler,
615 IRQF_TRIGGER_RISING |
Lars-Peter Clausena91aff12012-07-02 10:54:45 +0200616 IRQF_TRIGGER_FALLING |
617 IRQF_ONESHOT,
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100618 "ad7150_irq1",
Jonathan Cameron46a6af32011-06-27 13:07:21 +0100619 indio_dev);
Barry Song54c5be32010-10-27 21:43:53 -0400620 if (ret)
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100621 return ret;
Barry Song54c5be32010-10-27 21:43:53 -0400622 }
623
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100624 if (client->dev.platform_data) {
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100625 ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100626 client->dev.platform_data,
627 NULL,
628 &ad7150_event_handler,
629 IRQF_TRIGGER_RISING |
Lars-Peter Clausena91aff12012-07-02 10:54:45 +0200630 IRQF_TRIGGER_FALLING |
631 IRQF_ONESHOT,
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100632 "ad7150_irq2",
633 indio_dev);
634 if (ret)
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100635 return ret;
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100636 }
637
Ioana Ciornei252fb582015-10-02 13:37:48 +0300638 ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100639 if (ret)
Sachin Kamatd15b73c2013-08-24 20:24:00 +0100640 return ret;
Jonathan Cameron26d25ae2011-09-02 17:14:40 +0100641
Jonathan Cameron531efd62011-09-02 17:25:20 +0100642 dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
643 id->name, client->irq);
Barry Song54c5be32010-10-27 21:43:53 -0400644
645 return 0;
Barry Song54c5be32010-10-27 21:43:53 -0400646}
647
Barry Song54c5be32010-10-27 21:43:53 -0400648static const struct i2c_device_id ad7150_id[] = {
649 { "ad7150", 0 },
650 { "ad7151", 0 },
651 { "ad7156", 0 },
652 {}
653};
654
655MODULE_DEVICE_TABLE(i2c, ad7150_id);
656
657static struct i2c_driver ad7150_driver = {
658 .driver = {
659 .name = "ad7150",
660 },
661 .probe = ad7150_probe,
Barry Song54c5be32010-10-27 21:43:53 -0400662 .id_table = ad7150_id,
663};
Lars-Peter Clausen6e5af182011-11-16 10:13:38 +0100664module_i2c_driver(ad7150_driver);
Barry Song54c5be32010-10-27 21:43:53 -0400665
666MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
Michael Hennerich4e687dd2011-09-02 17:25:22 +0100667MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
Barry Song54c5be32010-10-27 21:43:53 -0400668MODULE_LICENSE("GPL v2");