• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * AD7150 capacitive sensor driver supporting AD7150/1/6
3  *
4  * Copyright 2010-2011 Analog Devices Inc.
5  *
6  * Licensed under the GPL-2 or later.
7  */
8 
9 #include <linux/interrupt.h>
10 #include <linux/device.h>
11 #include <linux/kernel.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/module.h>
15 
16 #include <linux/iio/iio.h>
17 #include <linux/iio/sysfs.h>
18 #include <linux/iio/events.h>
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
27 #define AD7150_CH2_DATA_HIGH       3
28 #define AD7150_CH1_AVG_HIGH        5
29 #define AD7150_CH2_AVG_HIGH        7
30 #define AD7150_CH1_SENSITIVITY     9
31 #define AD7150_CH1_THR_HOLD_H      9
32 #define AD7150_CH1_TIMEOUT         10
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
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 
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  */
71 struct ad7150_chip_info {
72 	struct i2c_client *client;
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];
79 	int old_state;
80 	char *conversion_mode;
81 	struct mutex state_lock;
82 };
83 
84 /*
85  * sysfs nodes
86  */
87 
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 
ad7150_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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)
102 {
103 	int ret;
104 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
105 
106 	switch (mask) {
107 	case IIO_CHAN_INFO_RAW:
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 IIO_CHAN_INFO_AVERAGE_RAW:
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;
123 	}
124 }
125 
ad7150_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)126 static int ad7150_read_event_config(struct iio_dev *indio_dev,
127 	const struct iio_chan_spec *chan, enum iio_event_type type,
128 	enum iio_event_direction dir)
129 {
130 	int ret;
131 	u8 threshtype;
132 	bool adaptive;
133 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
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 (type) {
143 	case IIO_EV_TYPE_MAG_ADAPTIVE:
144 		if (dir == IIO_EV_DIR_RISING)
145 			return adaptive && (threshtype == 0x1);
146 		return adaptive && (threshtype == 0x0);
147 	case IIO_EV_TYPE_THRESH_ADAPTIVE:
148 		if (dir == IIO_EV_DIR_RISING)
149 			return adaptive && (threshtype == 0x3);
150 		return adaptive && (threshtype == 0x2);
151 	case IIO_EV_TYPE_THRESH:
152 		if (dir == IIO_EV_DIR_RISING)
153 			return !adaptive && (threshtype == 0x1);
154 		return !adaptive && (threshtype == 0x0);
155 	default:
156 		break;
157 	}
158 	return -EINVAL;
159 }
160 
161 /* lock should be held */
ad7150_write_event_params(struct iio_dev * indio_dev,unsigned int chan,enum iio_event_type type,enum iio_event_direction dir)162 static int ad7150_write_event_params(struct iio_dev *indio_dev,
163 	 unsigned int chan, enum iio_event_type type,
164 	 enum iio_event_direction dir)
165 {
166 	int ret;
167 	u16 value;
168 	u8 sens, timeout;
169 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
170 	int rising = (dir == IIO_EV_DIR_RISING);
171 	u64 event_code;
172 
173 	event_code = IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE, chan, type, dir);
174 
175 	if (event_code != chip->current_event)
176 		return 0;
177 
178 	switch (type) {
179 		/* Note completely different from the adaptive versions */
180 	case IIO_EV_TYPE_THRESH:
181 		value = chip->threshold[rising][chan];
182 		return i2c_smbus_write_word_data(chip->client,
183 						ad7150_addresses[chan][3],
184 						swab16(value));
185 	case IIO_EV_TYPE_MAG_ADAPTIVE:
186 		sens = chip->mag_sensitivity[rising][chan];
187 		timeout = chip->mag_timeout[rising][chan];
188 		break;
189 	case IIO_EV_TYPE_THRESH_ADAPTIVE:
190 		sens = chip->thresh_sensitivity[rising][chan];
191 		timeout = chip->thresh_timeout[rising][chan];
192 		break;
193 	default:
194 		return -EINVAL;
195 	}
196 	ret = i2c_smbus_write_byte_data(chip->client,
197 					ad7150_addresses[chan][4],
198 					sens);
199 	if (ret < 0)
200 		return ret;
201 
202 	ret = i2c_smbus_write_byte_data(chip->client,
203 					ad7150_addresses[chan][5],
204 					timeout);
205 	if (ret < 0)
206 		return ret;
207 
208 	return 0;
209 }
210 
ad7150_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)211 static int ad7150_write_event_config(struct iio_dev *indio_dev,
212 	const struct iio_chan_spec *chan, enum iio_event_type type,
213 	enum iio_event_direction dir, int state)
214 {
215 	u8 thresh_type, cfg, adaptive;
216 	int ret;
217 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
218 	int rising = (dir == IIO_EV_DIR_RISING);
219 	u64 event_code;
220 
221 	/* Something must always be turned on */
222 	if (!state)
223 		return -EINVAL;
224 
225 	event_code = IIO_UNMOD_EVENT_CODE(chan->type, chan->channel, type, dir);
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 (type) {
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, chan->channel, type, dir);
272 error_ret:
273 	mutex_unlock(&chip->state_lock);
274 
275 	return ret;
276 }
277 
ad7150_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)278 static int ad7150_read_event_value(struct iio_dev *indio_dev,
279 				   const struct iio_chan_spec *chan,
280 				   enum iio_event_type type,
281 				   enum iio_event_direction dir,
282 				   enum iio_event_info info,
283 				   int *val, int *val2)
284 {
285 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
286 	int rising = (dir == IIO_EV_DIR_RISING);
287 
288 	/* Complex register sharing going on here */
289 	switch (type) {
290 	case IIO_EV_TYPE_MAG_ADAPTIVE:
291 		*val = chip->mag_sensitivity[rising][chan->channel];
292 		return IIO_VAL_INT;
293 	case IIO_EV_TYPE_THRESH_ADAPTIVE:
294 		*val = chip->thresh_sensitivity[rising][chan->channel];
295 		return IIO_VAL_INT;
296 	case IIO_EV_TYPE_THRESH:
297 		*val = chip->threshold[rising][chan->channel];
298 		return IIO_VAL_INT;
299 	default:
300 		return -EINVAL;
301 	}
302 }
303 
ad7150_write_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)304 static int ad7150_write_event_value(struct iio_dev *indio_dev,
305 				   const struct iio_chan_spec *chan,
306 				   enum iio_event_type type,
307 				   enum iio_event_direction dir,
308 				   enum iio_event_info info,
309 				   int val, int val2)
310 {
311 	int ret;
312 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
313 	int rising = (dir == IIO_EV_DIR_RISING);
314 
315 	mutex_lock(&chip->state_lock);
316 	switch (type) {
317 	case IIO_EV_TYPE_MAG_ADAPTIVE:
318 		chip->mag_sensitivity[rising][chan->channel] = val;
319 		break;
320 	case IIO_EV_TYPE_THRESH_ADAPTIVE:
321 		chip->thresh_sensitivity[rising][chan->channel] = val;
322 		break;
323 	case IIO_EV_TYPE_THRESH:
324 		chip->threshold[rising][chan->channel] = val;
325 		break;
326 	default:
327 		ret = -EINVAL;
328 		goto error_ret;
329 	}
330 
331 	/* write back if active */
332 	ret = ad7150_write_event_params(indio_dev, chan->channel, type, dir);
333 
334 error_ret:
335 	mutex_unlock(&chip->state_lock);
336 	return ret;
337 }
338 
ad7150_show_timeout(struct device * dev,struct device_attribute * attr,char * buf)339 static ssize_t ad7150_show_timeout(struct device *dev,
340 				   struct device_attribute *attr,
341 				   char *buf)
342 {
343 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
344 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
345 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
346 	u8 value;
347 
348 	/* use the event code for consistency reasons */
349 	int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
350 	int rising = !!(IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address)
351 			== IIO_EV_DIR_RISING);
352 
353 	switch (IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address)) {
354 	case IIO_EV_TYPE_MAG_ADAPTIVE:
355 		value = chip->mag_timeout[rising][chan];
356 		break;
357 	case IIO_EV_TYPE_THRESH_ADAPTIVE:
358 		value = chip->thresh_timeout[rising][chan];
359 		break;
360 	default:
361 		return -EINVAL;
362 	}
363 
364 	return sprintf(buf, "%d\n", value);
365 }
366 
ad7150_store_timeout(struct device * dev,struct device_attribute * attr,const char * buf,size_t len)367 static ssize_t ad7150_store_timeout(struct device *dev,
368 		struct device_attribute *attr,
369 		const char *buf,
370 		size_t len)
371 {
372 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
373 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
374 	struct iio_dev_attr *this_attr = to_iio_dev_attr(attr);
375 	int chan = IIO_EVENT_CODE_EXTRACT_CHAN(this_attr->address);
376 	enum iio_event_direction dir;
377 	enum iio_event_type type;
378 	int rising;
379 	u8 data;
380 	int ret;
381 
382 	type = IIO_EVENT_CODE_EXTRACT_TYPE(this_attr->address);
383 	dir = IIO_EVENT_CODE_EXTRACT_DIR(this_attr->address);
384 	rising = (dir == IIO_EV_DIR_RISING);
385 
386 	ret = kstrtou8(buf, 10, &data);
387 	if (ret < 0)
388 		return ret;
389 
390 	mutex_lock(&chip->state_lock);
391 	switch (type) {
392 	case IIO_EV_TYPE_MAG_ADAPTIVE:
393 		chip->mag_timeout[rising][chan] = data;
394 		break;
395 	case IIO_EV_TYPE_THRESH_ADAPTIVE:
396 		chip->thresh_timeout[rising][chan] = data;
397 		break;
398 	default:
399 		ret = -EINVAL;
400 		goto error_ret;
401 	}
402 
403 	ret = ad7150_write_event_params(indio_dev, chan, type, dir);
404 error_ret:
405 	mutex_unlock(&chip->state_lock);
406 
407 	if (ret < 0)
408 		return ret;
409 
410 	return len;
411 }
412 
413 #define AD7150_TIMEOUT(chan, type, dir, ev_type, ev_dir)		\
414 	IIO_DEVICE_ATTR(in_capacitance##chan##_##type##_##dir##_timeout, \
415 		S_IRUGO | S_IWUSR,					\
416 		&ad7150_show_timeout,					\
417 		&ad7150_store_timeout,					\
418 		IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,			\
419 				     chan,				\
420 				     IIO_EV_TYPE_##ev_type,		\
421 				     IIO_EV_DIR_##ev_dir))
422 static AD7150_TIMEOUT(0, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
423 static AD7150_TIMEOUT(0, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
424 static AD7150_TIMEOUT(1, mag_adaptive, rising, MAG_ADAPTIVE, RISING);
425 static AD7150_TIMEOUT(1, mag_adaptive, falling, MAG_ADAPTIVE, FALLING);
426 static AD7150_TIMEOUT(0, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
427 static AD7150_TIMEOUT(0, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
428 static AD7150_TIMEOUT(1, thresh_adaptive, rising, THRESH_ADAPTIVE, RISING);
429 static AD7150_TIMEOUT(1, thresh_adaptive, falling, THRESH_ADAPTIVE, FALLING);
430 
431 static const struct iio_event_spec ad7150_events[] = {
432 	{
433 		.type = IIO_EV_TYPE_THRESH,
434 		.dir = IIO_EV_DIR_RISING,
435 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
436 			BIT(IIO_EV_INFO_ENABLE),
437 	}, {
438 		.type = IIO_EV_TYPE_THRESH,
439 		.dir = IIO_EV_DIR_FALLING,
440 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
441 			BIT(IIO_EV_INFO_ENABLE),
442 	}, {
443 		.type = IIO_EV_TYPE_THRESH_ADAPTIVE,
444 		.dir = IIO_EV_DIR_RISING,
445 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
446 			BIT(IIO_EV_INFO_ENABLE),
447 	}, {
448 		.type = IIO_EV_TYPE_THRESH_ADAPTIVE,
449 		.dir = IIO_EV_DIR_FALLING,
450 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
451 			BIT(IIO_EV_INFO_ENABLE),
452 	}, {
453 		.type = IIO_EV_TYPE_MAG_ADAPTIVE,
454 		.dir = IIO_EV_DIR_RISING,
455 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
456 			BIT(IIO_EV_INFO_ENABLE),
457 	}, {
458 		.type = IIO_EV_TYPE_MAG_ADAPTIVE,
459 		.dir = IIO_EV_DIR_FALLING,
460 		.mask_separate = BIT(IIO_EV_INFO_VALUE) |
461 			BIT(IIO_EV_INFO_ENABLE),
462 	},
463 };
464 
465 static const struct iio_chan_spec ad7150_channels[] = {
466 	{
467 		.type = IIO_CAPACITANCE,
468 		.indexed = 1,
469 		.channel = 0,
470 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
471 		BIT(IIO_CHAN_INFO_AVERAGE_RAW),
472 		.event_spec = ad7150_events,
473 		.num_event_specs = ARRAY_SIZE(ad7150_events),
474 	}, {
475 		.type = IIO_CAPACITANCE,
476 		.indexed = 1,
477 		.channel = 1,
478 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
479 		BIT(IIO_CHAN_INFO_AVERAGE_RAW),
480 		.event_spec = ad7150_events,
481 		.num_event_specs = ARRAY_SIZE(ad7150_events),
482 	},
483 };
484 
485 /*
486  * threshold events
487  */
488 
ad7150_event_handler(int irq,void * private)489 static irqreturn_t ad7150_event_handler(int irq, void *private)
490 {
491 	struct iio_dev *indio_dev = private;
492 	struct ad7150_chip_info *chip = iio_priv(indio_dev);
493 	u8 int_status;
494 	s64 timestamp = iio_get_time_ns();
495 	int ret;
496 
497 	ret = i2c_smbus_read_byte_data(chip->client, AD7150_STATUS);
498 	if (ret < 0)
499 		return IRQ_HANDLED;
500 
501 	int_status = ret;
502 
503 	if ((int_status & AD7150_STATUS_OUT1) &&
504 	    !(chip->old_state & AD7150_STATUS_OUT1))
505 		iio_push_event(indio_dev,
506 			       IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
507 						    0,
508 						    IIO_EV_TYPE_THRESH,
509 						    IIO_EV_DIR_RISING),
510 				timestamp);
511 	else if ((!(int_status & AD7150_STATUS_OUT1)) &&
512 		 (chip->old_state & AD7150_STATUS_OUT1))
513 		iio_push_event(indio_dev,
514 			       IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
515 						    0,
516 						    IIO_EV_TYPE_THRESH,
517 						    IIO_EV_DIR_FALLING),
518 			       timestamp);
519 
520 	if ((int_status & AD7150_STATUS_OUT2) &&
521 	    !(chip->old_state & AD7150_STATUS_OUT2))
522 		iio_push_event(indio_dev,
523 			       IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
524 						    1,
525 						    IIO_EV_TYPE_THRESH,
526 						    IIO_EV_DIR_RISING),
527 			       timestamp);
528 	else if ((!(int_status & AD7150_STATUS_OUT2)) &&
529 		 (chip->old_state & AD7150_STATUS_OUT2))
530 		iio_push_event(indio_dev,
531 			       IIO_UNMOD_EVENT_CODE(IIO_CAPACITANCE,
532 						    1,
533 						    IIO_EV_TYPE_THRESH,
534 						    IIO_EV_DIR_FALLING),
535 			       timestamp);
536 	/* store the status to avoid repushing same events */
537 	chip->old_state = int_status;
538 
539 	return IRQ_HANDLED;
540 }
541 
542 /* Timeouts not currently handled by core */
543 static struct attribute *ad7150_event_attributes[] = {
544 	&iio_dev_attr_in_capacitance0_mag_adaptive_rising_timeout
545 	.dev_attr.attr,
546 	&iio_dev_attr_in_capacitance0_mag_adaptive_falling_timeout
547 	.dev_attr.attr,
548 	&iio_dev_attr_in_capacitance1_mag_adaptive_rising_timeout
549 	.dev_attr.attr,
550 	&iio_dev_attr_in_capacitance1_mag_adaptive_falling_timeout
551 	.dev_attr.attr,
552 	&iio_dev_attr_in_capacitance0_thresh_adaptive_rising_timeout
553 	.dev_attr.attr,
554 	&iio_dev_attr_in_capacitance0_thresh_adaptive_falling_timeout
555 	.dev_attr.attr,
556 	&iio_dev_attr_in_capacitance1_thresh_adaptive_rising_timeout
557 	.dev_attr.attr,
558 	&iio_dev_attr_in_capacitance1_thresh_adaptive_falling_timeout
559 	.dev_attr.attr,
560 	NULL,
561 };
562 
563 static struct attribute_group ad7150_event_attribute_group = {
564 	.attrs = ad7150_event_attributes,
565 	.name = "events",
566 };
567 
568 static const struct iio_info ad7150_info = {
569 	.event_attrs = &ad7150_event_attribute_group,
570 	.driver_module = THIS_MODULE,
571 	.read_raw = &ad7150_read_raw,
572 	.read_event_config = &ad7150_read_event_config,
573 	.write_event_config = &ad7150_write_event_config,
574 	.read_event_value = &ad7150_read_event_value,
575 	.write_event_value = &ad7150_write_event_value,
576 };
577 
578 /*
579  * device probe and remove
580  */
581 
ad7150_probe(struct i2c_client * client,const struct i2c_device_id * id)582 static int ad7150_probe(struct i2c_client *client,
583 		const struct i2c_device_id *id)
584 {
585 	int ret;
586 	struct ad7150_chip_info *chip;
587 	struct iio_dev *indio_dev;
588 
589 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*chip));
590 	if (!indio_dev)
591 		return -ENOMEM;
592 	chip = iio_priv(indio_dev);
593 	mutex_init(&chip->state_lock);
594 	/* this is only used for device removal purposes */
595 	i2c_set_clientdata(client, indio_dev);
596 
597 	chip->client = client;
598 
599 	indio_dev->name = id->name;
600 	indio_dev->channels = ad7150_channels;
601 	indio_dev->num_channels = ARRAY_SIZE(ad7150_channels);
602 	/* Establish that the iio_dev is a child of the i2c device */
603 	indio_dev->dev.parent = &client->dev;
604 
605 	indio_dev->info = &ad7150_info;
606 
607 	indio_dev->modes = INDIO_DIRECT_MODE;
608 
609 	if (client->irq) {
610 		ret = devm_request_threaded_irq(&client->dev, client->irq,
611 					   NULL,
612 					   &ad7150_event_handler,
613 					   IRQF_TRIGGER_RISING |
614 					   IRQF_TRIGGER_FALLING |
615 					   IRQF_ONESHOT,
616 					   "ad7150_irq1",
617 					   indio_dev);
618 		if (ret)
619 			return ret;
620 	}
621 
622 	if (client->dev.platform_data) {
623 		ret = devm_request_threaded_irq(&client->dev, *(unsigned int *)
624 					   client->dev.platform_data,
625 					   NULL,
626 					   &ad7150_event_handler,
627 					   IRQF_TRIGGER_RISING |
628 					   IRQF_TRIGGER_FALLING |
629 					   IRQF_ONESHOT,
630 					   "ad7150_irq2",
631 					   indio_dev);
632 		if (ret)
633 			return ret;
634 	}
635 
636 	ret = devm_iio_device_register(indio_dev->dev.parent, indio_dev);
637 	if (ret)
638 		return ret;
639 
640 	dev_info(&client->dev, "%s capacitive sensor registered,irq: %d\n",
641 		 id->name, client->irq);
642 
643 	return 0;
644 }
645 
646 static const struct i2c_device_id ad7150_id[] = {
647 	{ "ad7150", 0 },
648 	{ "ad7151", 0 },
649 	{ "ad7156", 0 },
650 	{}
651 };
652 
653 MODULE_DEVICE_TABLE(i2c, ad7150_id);
654 
655 static struct i2c_driver ad7150_driver = {
656 	.driver = {
657 		.name = "ad7150",
658 	},
659 	.probe = ad7150_probe,
660 	.id_table = ad7150_id,
661 };
662 module_i2c_driver(ad7150_driver);
663 
664 MODULE_AUTHOR("Barry Song <21cnbao@gmail.com>");
665 MODULE_DESCRIPTION("Analog Devices AD7150/1/6 capacitive sensor driver");
666 MODULE_LICENSE("GPL v2");
667