xref: /openbmc/linux/drivers/iio/proximity/sx9500.c (revision 98ddec80)
1 /*
2  * Copyright (c) 2014 Intel Corporation
3  *
4  * Driver for Semtech's SX9500 capacitive proximity/button solution.
5  * Datasheet available at
6  * <http://www.semtech.com/images/datasheet/sx9500.pdf>.
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU General Public License version 2 as published by
10  * the Free Software Foundation.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/module.h>
16 #include <linux/i2c.h>
17 #include <linux/irq.h>
18 #include <linux/acpi.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/regmap.h>
21 #include <linux/pm.h>
22 #include <linux/delay.h>
23 
24 #include <linux/iio/iio.h>
25 #include <linux/iio/buffer.h>
26 #include <linux/iio/sysfs.h>
27 #include <linux/iio/events.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/trigger_consumer.h>
31 
32 #define SX9500_DRIVER_NAME		"sx9500"
33 #define SX9500_IRQ_NAME			"sx9500_event"
34 
35 /* Register definitions. */
36 #define SX9500_REG_IRQ_SRC		0x00
37 #define SX9500_REG_STAT			0x01
38 #define SX9500_REG_IRQ_MSK		0x03
39 
40 #define SX9500_REG_PROX_CTRL0		0x06
41 #define SX9500_REG_PROX_CTRL1		0x07
42 #define SX9500_REG_PROX_CTRL2		0x08
43 #define SX9500_REG_PROX_CTRL3		0x09
44 #define SX9500_REG_PROX_CTRL4		0x0a
45 #define SX9500_REG_PROX_CTRL5		0x0b
46 #define SX9500_REG_PROX_CTRL6		0x0c
47 #define SX9500_REG_PROX_CTRL7		0x0d
48 #define SX9500_REG_PROX_CTRL8		0x0e
49 
50 #define SX9500_REG_SENSOR_SEL		0x20
51 #define SX9500_REG_USE_MSB		0x21
52 #define SX9500_REG_USE_LSB		0x22
53 #define SX9500_REG_AVG_MSB		0x23
54 #define SX9500_REG_AVG_LSB		0x24
55 #define SX9500_REG_DIFF_MSB		0x25
56 #define SX9500_REG_DIFF_LSB		0x26
57 #define SX9500_REG_OFFSET_MSB		0x27
58 #define SX9500_REG_OFFSET_LSB		0x28
59 
60 #define SX9500_REG_RESET		0x7f
61 
62 /* Write this to REG_RESET to do a soft reset. */
63 #define SX9500_SOFT_RESET		0xde
64 
65 #define SX9500_SCAN_PERIOD_MASK		GENMASK(6, 4)
66 #define SX9500_SCAN_PERIOD_SHIFT	4
67 
68 /*
69  * These serve for identifying IRQ source in the IRQ_SRC register, and
70  * also for masking the IRQs in the IRQ_MSK register.
71  */
72 #define SX9500_CLOSE_IRQ		BIT(6)
73 #define SX9500_FAR_IRQ			BIT(5)
74 #define SX9500_CONVDONE_IRQ		BIT(3)
75 
76 #define SX9500_PROXSTAT_SHIFT		4
77 #define SX9500_COMPSTAT_MASK		GENMASK(3, 0)
78 
79 #define SX9500_NUM_CHANNELS		4
80 #define SX9500_CHAN_MASK		GENMASK(SX9500_NUM_CHANNELS - 1, 0)
81 
82 struct sx9500_data {
83 	struct mutex mutex;
84 	struct i2c_client *client;
85 	struct iio_trigger *trig;
86 	struct regmap *regmap;
87 	struct gpio_desc *gpiod_rst;
88 	/*
89 	 * Last reading of the proximity status for each channel.  We
90 	 * only send an event to user space when this changes.
91 	 */
92 	bool prox_stat[SX9500_NUM_CHANNELS];
93 	bool event_enabled[SX9500_NUM_CHANNELS];
94 	bool trigger_enabled;
95 	u16 *buffer;
96 	/* Remember enabled channels and sample rate during suspend. */
97 	unsigned int suspend_ctrl0;
98 	struct completion completion;
99 	int data_rdy_users, close_far_users;
100 	int channel_users[SX9500_NUM_CHANNELS];
101 };
102 
103 static const struct iio_event_spec sx9500_events[] = {
104 	{
105 		.type = IIO_EV_TYPE_THRESH,
106 		.dir = IIO_EV_DIR_EITHER,
107 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
108 	},
109 };
110 
111 #define SX9500_CHANNEL(idx)					\
112 	{							\
113 		.type = IIO_PROXIMITY,				\
114 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),	\
115 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
116 		.indexed = 1,					\
117 		.channel = idx,					\
118 		.event_spec = sx9500_events,			\
119 		.num_event_specs = ARRAY_SIZE(sx9500_events),	\
120 		.scan_index = idx,				\
121 		.scan_type = {					\
122 			.sign = 'u',				\
123 			.realbits = 16,				\
124 			.storagebits = 16,			\
125 			.shift = 0,				\
126 		},						\
127 	}
128 
129 static const struct iio_chan_spec sx9500_channels[] = {
130 	SX9500_CHANNEL(0),
131 	SX9500_CHANNEL(1),
132 	SX9500_CHANNEL(2),
133 	SX9500_CHANNEL(3),
134 	IIO_CHAN_SOFT_TIMESTAMP(4),
135 };
136 
137 static const struct {
138 	int val;
139 	int val2;
140 } sx9500_samp_freq_table[] = {
141 	{33, 333333},
142 	{16, 666666},
143 	{11, 111111},
144 	{8, 333333},
145 	{6, 666666},
146 	{5, 0},
147 	{3, 333333},
148 	{2, 500000},
149 };
150 
151 static const unsigned int sx9500_scan_period_table[] = {
152 	30, 60, 90, 120, 150, 200, 300, 400,
153 };
154 
155 static const struct regmap_range sx9500_writable_reg_ranges[] = {
156 	regmap_reg_range(SX9500_REG_IRQ_MSK, SX9500_REG_IRQ_MSK),
157 	regmap_reg_range(SX9500_REG_PROX_CTRL0, SX9500_REG_PROX_CTRL8),
158 	regmap_reg_range(SX9500_REG_SENSOR_SEL, SX9500_REG_SENSOR_SEL),
159 	regmap_reg_range(SX9500_REG_OFFSET_MSB, SX9500_REG_OFFSET_LSB),
160 	regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
161 };
162 
163 static const struct regmap_access_table sx9500_writeable_regs = {
164 	.yes_ranges = sx9500_writable_reg_ranges,
165 	.n_yes_ranges = ARRAY_SIZE(sx9500_writable_reg_ranges),
166 };
167 
168 /*
169  * All allocated registers are readable, so we just list unallocated
170  * ones.
171  */
172 static const struct regmap_range sx9500_non_readable_reg_ranges[] = {
173 	regmap_reg_range(SX9500_REG_STAT + 1, SX9500_REG_STAT + 1),
174 	regmap_reg_range(SX9500_REG_IRQ_MSK + 1, SX9500_REG_PROX_CTRL0 - 1),
175 	regmap_reg_range(SX9500_REG_PROX_CTRL8 + 1, SX9500_REG_SENSOR_SEL - 1),
176 	regmap_reg_range(SX9500_REG_OFFSET_LSB + 1, SX9500_REG_RESET - 1),
177 };
178 
179 static const struct regmap_access_table sx9500_readable_regs = {
180 	.no_ranges = sx9500_non_readable_reg_ranges,
181 	.n_no_ranges = ARRAY_SIZE(sx9500_non_readable_reg_ranges),
182 };
183 
184 static const struct regmap_range sx9500_volatile_reg_ranges[] = {
185 	regmap_reg_range(SX9500_REG_IRQ_SRC, SX9500_REG_STAT),
186 	regmap_reg_range(SX9500_REG_USE_MSB, SX9500_REG_OFFSET_LSB),
187 	regmap_reg_range(SX9500_REG_RESET, SX9500_REG_RESET),
188 };
189 
190 static const struct regmap_access_table sx9500_volatile_regs = {
191 	.yes_ranges = sx9500_volatile_reg_ranges,
192 	.n_yes_ranges = ARRAY_SIZE(sx9500_volatile_reg_ranges),
193 };
194 
195 static const struct regmap_config sx9500_regmap_config = {
196 	.reg_bits = 8,
197 	.val_bits = 8,
198 
199 	.max_register = SX9500_REG_RESET,
200 	.cache_type = REGCACHE_RBTREE,
201 
202 	.wr_table = &sx9500_writeable_regs,
203 	.rd_table = &sx9500_readable_regs,
204 	.volatile_table = &sx9500_volatile_regs,
205 };
206 
207 static int sx9500_inc_users(struct sx9500_data *data, int *counter,
208 			    unsigned int reg, unsigned int bitmask)
209 {
210 	(*counter)++;
211 	if (*counter != 1)
212 		/* Bit is already active, nothing to do. */
213 		return 0;
214 
215 	return regmap_update_bits(data->regmap, reg, bitmask, bitmask);
216 }
217 
218 static int sx9500_dec_users(struct sx9500_data *data, int *counter,
219 			    unsigned int reg, unsigned int bitmask)
220 {
221 	(*counter)--;
222 	if (*counter != 0)
223 		/* There are more users, do not deactivate. */
224 		return 0;
225 
226 	return regmap_update_bits(data->regmap, reg, bitmask, 0);
227 }
228 
229 static int sx9500_inc_chan_users(struct sx9500_data *data, int chan)
230 {
231 	return sx9500_inc_users(data, &data->channel_users[chan],
232 				SX9500_REG_PROX_CTRL0, BIT(chan));
233 }
234 
235 static int sx9500_dec_chan_users(struct sx9500_data *data, int chan)
236 {
237 	return sx9500_dec_users(data, &data->channel_users[chan],
238 				SX9500_REG_PROX_CTRL0, BIT(chan));
239 }
240 
241 static int sx9500_inc_data_rdy_users(struct sx9500_data *data)
242 {
243 	return sx9500_inc_users(data, &data->data_rdy_users,
244 				SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ);
245 }
246 
247 static int sx9500_dec_data_rdy_users(struct sx9500_data *data)
248 {
249 	return sx9500_dec_users(data, &data->data_rdy_users,
250 				SX9500_REG_IRQ_MSK, SX9500_CONVDONE_IRQ);
251 }
252 
253 static int sx9500_inc_close_far_users(struct sx9500_data *data)
254 {
255 	return sx9500_inc_users(data, &data->close_far_users,
256 				SX9500_REG_IRQ_MSK,
257 				SX9500_CLOSE_IRQ | SX9500_FAR_IRQ);
258 }
259 
260 static int sx9500_dec_close_far_users(struct sx9500_data *data)
261 {
262 	return sx9500_dec_users(data, &data->close_far_users,
263 				SX9500_REG_IRQ_MSK,
264 				SX9500_CLOSE_IRQ | SX9500_FAR_IRQ);
265 }
266 
267 static int sx9500_read_prox_data(struct sx9500_data *data,
268 				 const struct iio_chan_spec *chan,
269 				 int *val)
270 {
271 	int ret;
272 	__be16 regval;
273 
274 	ret = regmap_write(data->regmap, SX9500_REG_SENSOR_SEL, chan->channel);
275 	if (ret < 0)
276 		return ret;
277 
278 	ret = regmap_bulk_read(data->regmap, SX9500_REG_USE_MSB, &regval, 2);
279 	if (ret < 0)
280 		return ret;
281 
282 	*val = be16_to_cpu(regval);
283 
284 	return IIO_VAL_INT;
285 }
286 
287 /*
288  * If we have no interrupt support, we have to wait for a scan period
289  * after enabling a channel to get a result.
290  */
291 static int sx9500_wait_for_sample(struct sx9500_data *data)
292 {
293 	int ret;
294 	unsigned int val;
295 
296 	ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &val);
297 	if (ret < 0)
298 		return ret;
299 
300 	val = (val & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
301 
302 	msleep(sx9500_scan_period_table[val]);
303 
304 	return 0;
305 }
306 
307 static int sx9500_read_proximity(struct sx9500_data *data,
308 				 const struct iio_chan_spec *chan,
309 				 int *val)
310 {
311 	int ret;
312 
313 	mutex_lock(&data->mutex);
314 
315 	ret = sx9500_inc_chan_users(data, chan->channel);
316 	if (ret < 0)
317 		goto out;
318 
319 	ret = sx9500_inc_data_rdy_users(data);
320 	if (ret < 0)
321 		goto out_dec_chan;
322 
323 	mutex_unlock(&data->mutex);
324 
325 	if (data->client->irq > 0)
326 		ret = wait_for_completion_interruptible(&data->completion);
327 	else
328 		ret = sx9500_wait_for_sample(data);
329 
330 	mutex_lock(&data->mutex);
331 
332 	if (ret < 0)
333 		goto out_dec_data_rdy;
334 
335 	ret = sx9500_read_prox_data(data, chan, val);
336 	if (ret < 0)
337 		goto out_dec_data_rdy;
338 
339 	ret = sx9500_dec_data_rdy_users(data);
340 	if (ret < 0)
341 		goto out_dec_chan;
342 
343 	ret = sx9500_dec_chan_users(data, chan->channel);
344 	if (ret < 0)
345 		goto out;
346 
347 	ret = IIO_VAL_INT;
348 
349 	goto out;
350 
351 out_dec_data_rdy:
352 	sx9500_dec_data_rdy_users(data);
353 out_dec_chan:
354 	sx9500_dec_chan_users(data, chan->channel);
355 out:
356 	mutex_unlock(&data->mutex);
357 	reinit_completion(&data->completion);
358 
359 	return ret;
360 }
361 
362 static int sx9500_read_samp_freq(struct sx9500_data *data,
363 				 int *val, int *val2)
364 {
365 	int ret;
366 	unsigned int regval;
367 
368 	mutex_lock(&data->mutex);
369 	ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0, &regval);
370 	mutex_unlock(&data->mutex);
371 
372 	if (ret < 0)
373 		return ret;
374 
375 	regval = (regval & SX9500_SCAN_PERIOD_MASK) >> SX9500_SCAN_PERIOD_SHIFT;
376 	*val = sx9500_samp_freq_table[regval].val;
377 	*val2 = sx9500_samp_freq_table[regval].val2;
378 
379 	return IIO_VAL_INT_PLUS_MICRO;
380 }
381 
382 static int sx9500_read_raw(struct iio_dev *indio_dev,
383 			   const struct iio_chan_spec *chan,
384 			   int *val, int *val2, long mask)
385 {
386 	struct sx9500_data *data = iio_priv(indio_dev);
387 	int ret;
388 
389 	switch (chan->type) {
390 	case IIO_PROXIMITY:
391 		switch (mask) {
392 		case IIO_CHAN_INFO_RAW:
393 			ret = iio_device_claim_direct_mode(indio_dev);
394 			if (ret)
395 				return ret;
396 			ret = sx9500_read_proximity(data, chan, val);
397 			iio_device_release_direct_mode(indio_dev);
398 			return ret;
399 		case IIO_CHAN_INFO_SAMP_FREQ:
400 			return sx9500_read_samp_freq(data, val, val2);
401 		default:
402 			return -EINVAL;
403 		}
404 	default:
405 		return -EINVAL;
406 	}
407 }
408 
409 static int sx9500_set_samp_freq(struct sx9500_data *data,
410 				int val, int val2)
411 {
412 	int i, ret;
413 
414 	for (i = 0; i < ARRAY_SIZE(sx9500_samp_freq_table); i++)
415 		if (val == sx9500_samp_freq_table[i].val &&
416 		    val2 == sx9500_samp_freq_table[i].val2)
417 			break;
418 
419 	if (i == ARRAY_SIZE(sx9500_samp_freq_table))
420 		return -EINVAL;
421 
422 	mutex_lock(&data->mutex);
423 
424 	ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
425 				 SX9500_SCAN_PERIOD_MASK,
426 				 i << SX9500_SCAN_PERIOD_SHIFT);
427 
428 	mutex_unlock(&data->mutex);
429 
430 	return ret;
431 }
432 
433 static int sx9500_write_raw(struct iio_dev *indio_dev,
434 			    const struct iio_chan_spec *chan,
435 			    int val, int val2, long mask)
436 {
437 	struct sx9500_data *data = iio_priv(indio_dev);
438 
439 	switch (chan->type) {
440 	case IIO_PROXIMITY:
441 		switch (mask) {
442 		case IIO_CHAN_INFO_SAMP_FREQ:
443 			return sx9500_set_samp_freq(data, val, val2);
444 		default:
445 			return -EINVAL;
446 		}
447 	default:
448 		return -EINVAL;
449 	}
450 }
451 
452 static irqreturn_t sx9500_irq_handler(int irq, void *private)
453 {
454 	struct iio_dev *indio_dev = private;
455 	struct sx9500_data *data = iio_priv(indio_dev);
456 
457 	if (data->trigger_enabled)
458 		iio_trigger_poll(data->trig);
459 
460 	/*
461 	 * Even if no event is enabled, we need to wake the thread to
462 	 * clear the interrupt state by reading SX9500_REG_IRQ_SRC.  It
463 	 * is not possible to do that here because regmap_read takes a
464 	 * mutex.
465 	 */
466 	return IRQ_WAKE_THREAD;
467 }
468 
469 static void sx9500_push_events(struct iio_dev *indio_dev)
470 {
471 	int ret;
472 	unsigned int val, chan;
473 	struct sx9500_data *data = iio_priv(indio_dev);
474 
475 	ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
476 	if (ret < 0) {
477 		dev_err(&data->client->dev, "i2c transfer error in irq\n");
478 		return;
479 	}
480 
481 	val >>= SX9500_PROXSTAT_SHIFT;
482 	for (chan = 0; chan < SX9500_NUM_CHANNELS; chan++) {
483 		int dir;
484 		u64 ev;
485 		bool new_prox = val & BIT(chan);
486 
487 		if (!data->event_enabled[chan])
488 			continue;
489 		if (new_prox == data->prox_stat[chan])
490 			/* No change on this channel. */
491 			continue;
492 
493 		dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
494 		ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
495 					  IIO_EV_TYPE_THRESH, dir);
496 		iio_push_event(indio_dev, ev, iio_get_time_ns(indio_dev));
497 		data->prox_stat[chan] = new_prox;
498 	}
499 }
500 
501 static irqreturn_t sx9500_irq_thread_handler(int irq, void *private)
502 {
503 	struct iio_dev *indio_dev = private;
504 	struct sx9500_data *data = iio_priv(indio_dev);
505 	int ret;
506 	unsigned int val;
507 
508 	mutex_lock(&data->mutex);
509 
510 	ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
511 	if (ret < 0) {
512 		dev_err(&data->client->dev, "i2c transfer error in irq\n");
513 		goto out;
514 	}
515 
516 	if (val & (SX9500_CLOSE_IRQ | SX9500_FAR_IRQ))
517 		sx9500_push_events(indio_dev);
518 
519 	if (val & SX9500_CONVDONE_IRQ)
520 		complete(&data->completion);
521 
522 out:
523 	mutex_unlock(&data->mutex);
524 
525 	return IRQ_HANDLED;
526 }
527 
528 static int sx9500_read_event_config(struct iio_dev *indio_dev,
529 				    const struct iio_chan_spec *chan,
530 				    enum iio_event_type type,
531 				    enum iio_event_direction dir)
532 {
533 	struct sx9500_data *data = iio_priv(indio_dev);
534 
535 	if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
536 	    dir != IIO_EV_DIR_EITHER)
537 		return -EINVAL;
538 
539 	return data->event_enabled[chan->channel];
540 }
541 
542 static int sx9500_write_event_config(struct iio_dev *indio_dev,
543 				     const struct iio_chan_spec *chan,
544 				     enum iio_event_type type,
545 				     enum iio_event_direction dir,
546 				     int state)
547 {
548 	struct sx9500_data *data = iio_priv(indio_dev);
549 	int ret;
550 
551 	if (chan->type != IIO_PROXIMITY || type != IIO_EV_TYPE_THRESH ||
552 	    dir != IIO_EV_DIR_EITHER)
553 		return -EINVAL;
554 
555 	mutex_lock(&data->mutex);
556 
557 	if (state == 1) {
558 		ret = sx9500_inc_chan_users(data, chan->channel);
559 		if (ret < 0)
560 			goto out_unlock;
561 		ret = sx9500_inc_close_far_users(data);
562 		if (ret < 0)
563 			goto out_undo_chan;
564 	} else {
565 		ret = sx9500_dec_chan_users(data, chan->channel);
566 		if (ret < 0)
567 			goto out_unlock;
568 		ret = sx9500_dec_close_far_users(data);
569 		if (ret < 0)
570 			goto out_undo_chan;
571 	}
572 
573 	data->event_enabled[chan->channel] = state;
574 	goto out_unlock;
575 
576 out_undo_chan:
577 	if (state == 1)
578 		sx9500_dec_chan_users(data, chan->channel);
579 	else
580 		sx9500_inc_chan_users(data, chan->channel);
581 out_unlock:
582 	mutex_unlock(&data->mutex);
583 	return ret;
584 }
585 
586 static int sx9500_update_scan_mode(struct iio_dev *indio_dev,
587 				   const unsigned long *scan_mask)
588 {
589 	struct sx9500_data *data = iio_priv(indio_dev);
590 
591 	mutex_lock(&data->mutex);
592 	kfree(data->buffer);
593 	data->buffer = kzalloc(indio_dev->scan_bytes, GFP_KERNEL);
594 	mutex_unlock(&data->mutex);
595 
596 	if (data->buffer == NULL)
597 		return -ENOMEM;
598 
599 	return 0;
600 }
601 
602 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(
603 	"2.500000 3.333333 5 6.666666 8.333333 11.111111 16.666666 33.333333");
604 
605 static struct attribute *sx9500_attributes[] = {
606 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
607 	NULL,
608 };
609 
610 static const struct attribute_group sx9500_attribute_group = {
611 	.attrs = sx9500_attributes,
612 };
613 
614 static const struct iio_info sx9500_info = {
615 	.attrs = &sx9500_attribute_group,
616 	.read_raw = &sx9500_read_raw,
617 	.write_raw = &sx9500_write_raw,
618 	.read_event_config = &sx9500_read_event_config,
619 	.write_event_config = &sx9500_write_event_config,
620 	.update_scan_mode = &sx9500_update_scan_mode,
621 };
622 
623 static int sx9500_set_trigger_state(struct iio_trigger *trig,
624 				    bool state)
625 {
626 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
627 	struct sx9500_data *data = iio_priv(indio_dev);
628 	int ret;
629 
630 	mutex_lock(&data->mutex);
631 
632 	if (state)
633 		ret = sx9500_inc_data_rdy_users(data);
634 	else
635 		ret = sx9500_dec_data_rdy_users(data);
636 	if (ret < 0)
637 		goto out;
638 
639 	data->trigger_enabled = state;
640 
641 out:
642 	mutex_unlock(&data->mutex);
643 
644 	return ret;
645 }
646 
647 static const struct iio_trigger_ops sx9500_trigger_ops = {
648 	.set_trigger_state = sx9500_set_trigger_state,
649 };
650 
651 static irqreturn_t sx9500_trigger_handler(int irq, void *private)
652 {
653 	struct iio_poll_func *pf = private;
654 	struct iio_dev *indio_dev = pf->indio_dev;
655 	struct sx9500_data *data = iio_priv(indio_dev);
656 	int val, bit, ret, i = 0;
657 
658 	mutex_lock(&data->mutex);
659 
660 	for_each_set_bit(bit, indio_dev->active_scan_mask,
661 			 indio_dev->masklength) {
662 		ret = sx9500_read_prox_data(data, &indio_dev->channels[bit],
663 					    &val);
664 		if (ret < 0)
665 			goto out;
666 
667 		data->buffer[i++] = val;
668 	}
669 
670 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
671 					   iio_get_time_ns(indio_dev));
672 
673 out:
674 	mutex_unlock(&data->mutex);
675 
676 	iio_trigger_notify_done(indio_dev->trig);
677 
678 	return IRQ_HANDLED;
679 }
680 
681 static int sx9500_buffer_preenable(struct iio_dev *indio_dev)
682 {
683 	struct sx9500_data *data = iio_priv(indio_dev);
684 	int ret = 0, i;
685 
686 	mutex_lock(&data->mutex);
687 
688 	for (i = 0; i < SX9500_NUM_CHANNELS; i++)
689 		if (test_bit(i, indio_dev->active_scan_mask)) {
690 			ret = sx9500_inc_chan_users(data, i);
691 			if (ret)
692 				break;
693 		}
694 
695 	if (ret)
696 		for (i = i - 1; i >= 0; i--)
697 			if (test_bit(i, indio_dev->active_scan_mask))
698 				sx9500_dec_chan_users(data, i);
699 
700 	mutex_unlock(&data->mutex);
701 
702 	return ret;
703 }
704 
705 static int sx9500_buffer_predisable(struct iio_dev *indio_dev)
706 {
707 	struct sx9500_data *data = iio_priv(indio_dev);
708 	int ret = 0, i;
709 
710 	iio_triggered_buffer_predisable(indio_dev);
711 
712 	mutex_lock(&data->mutex);
713 
714 	for (i = 0; i < SX9500_NUM_CHANNELS; i++)
715 		if (test_bit(i, indio_dev->active_scan_mask)) {
716 			ret = sx9500_dec_chan_users(data, i);
717 			if (ret)
718 				break;
719 		}
720 
721 	if (ret)
722 		for (i = i - 1; i >= 0; i--)
723 			if (test_bit(i, indio_dev->active_scan_mask))
724 				sx9500_inc_chan_users(data, i);
725 
726 	mutex_unlock(&data->mutex);
727 
728 	return ret;
729 }
730 
731 static const struct iio_buffer_setup_ops sx9500_buffer_setup_ops = {
732 	.preenable = sx9500_buffer_preenable,
733 	.postenable = iio_triggered_buffer_postenable,
734 	.predisable = sx9500_buffer_predisable,
735 };
736 
737 struct sx9500_reg_default {
738 	u8 reg;
739 	u8 def;
740 };
741 
742 static const struct sx9500_reg_default sx9500_default_regs[] = {
743 	{
744 		.reg = SX9500_REG_PROX_CTRL1,
745 		/* Shield enabled, small range. */
746 		.def = 0x43,
747 	},
748 	{
749 		.reg = SX9500_REG_PROX_CTRL2,
750 		/* x8 gain, 167kHz frequency, finest resolution. */
751 		.def = 0x77,
752 	},
753 	{
754 		.reg = SX9500_REG_PROX_CTRL3,
755 		/* Doze enabled, 2x scan period doze, no raw filter. */
756 		.def = 0x40,
757 	},
758 	{
759 		.reg = SX9500_REG_PROX_CTRL4,
760 		/* Average threshold. */
761 		.def = 0x30,
762 	},
763 	{
764 		.reg = SX9500_REG_PROX_CTRL5,
765 		/*
766 		 * Debouncer off, lowest average negative filter,
767 		 * highest average postive filter.
768 		 */
769 		.def = 0x0f,
770 	},
771 	{
772 		.reg = SX9500_REG_PROX_CTRL6,
773 		/* Proximity detection threshold: 280 */
774 		.def = 0x0e,
775 	},
776 	{
777 		.reg = SX9500_REG_PROX_CTRL7,
778 		/*
779 		 * No automatic compensation, compensate each pin
780 		 * independently, proximity hysteresis: 32, close
781 		 * debouncer off, far debouncer off.
782 		 */
783 		.def = 0x00,
784 	},
785 	{
786 		.reg = SX9500_REG_PROX_CTRL8,
787 		/* No stuck timeout, no periodic compensation. */
788 		.def = 0x00,
789 	},
790 	{
791 		.reg = SX9500_REG_PROX_CTRL0,
792 		/* Scan period: 30ms, all sensors disabled. */
793 		.def = 0x00,
794 	},
795 };
796 
797 /* Activate all channels and perform an initial compensation. */
798 static int sx9500_init_compensation(struct iio_dev *indio_dev)
799 {
800 	struct sx9500_data *data = iio_priv(indio_dev);
801 	int i, ret;
802 	unsigned int val;
803 
804 	ret = regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
805 				 SX9500_CHAN_MASK, SX9500_CHAN_MASK);
806 	if (ret < 0)
807 		return ret;
808 
809 	for (i = 10; i >= 0; i--) {
810 		usleep_range(10000, 20000);
811 		ret = regmap_read(data->regmap, SX9500_REG_STAT, &val);
812 		if (ret < 0)
813 			goto out;
814 		if (!(val & SX9500_COMPSTAT_MASK))
815 			break;
816 	}
817 
818 	if (i < 0) {
819 		dev_err(&data->client->dev, "initial compensation timed out");
820 		ret = -ETIMEDOUT;
821 	}
822 
823 out:
824 	regmap_update_bits(data->regmap, SX9500_REG_PROX_CTRL0,
825 			   SX9500_CHAN_MASK, 0);
826 	return ret;
827 }
828 
829 static int sx9500_init_device(struct iio_dev *indio_dev)
830 {
831 	struct sx9500_data *data = iio_priv(indio_dev);
832 	int ret, i;
833 	unsigned int val;
834 
835 	if (data->gpiod_rst) {
836 		gpiod_set_value_cansleep(data->gpiod_rst, 0);
837 		usleep_range(1000, 2000);
838 		gpiod_set_value_cansleep(data->gpiod_rst, 1);
839 		usleep_range(1000, 2000);
840 	}
841 
842 	ret = regmap_write(data->regmap, SX9500_REG_IRQ_MSK, 0);
843 	if (ret < 0)
844 		return ret;
845 
846 	ret = regmap_write(data->regmap, SX9500_REG_RESET,
847 			   SX9500_SOFT_RESET);
848 	if (ret < 0)
849 		return ret;
850 
851 	ret = regmap_read(data->regmap, SX9500_REG_IRQ_SRC, &val);
852 	if (ret < 0)
853 		return ret;
854 
855 	for (i = 0; i < ARRAY_SIZE(sx9500_default_regs); i++) {
856 		ret = regmap_write(data->regmap,
857 				   sx9500_default_regs[i].reg,
858 				   sx9500_default_regs[i].def);
859 		if (ret < 0)
860 			return ret;
861 	}
862 
863 	return sx9500_init_compensation(indio_dev);
864 }
865 
866 static const struct acpi_gpio_params reset_gpios = { 0, 0, false };
867 static const struct acpi_gpio_params interrupt_gpios = { 2, 0, false };
868 
869 static const struct acpi_gpio_mapping acpi_sx9500_gpios[] = {
870 	{ "reset-gpios", &reset_gpios, 1 },
871 	/*
872 	 * Some platforms have a bug in ACPI GPIO description making IRQ
873 	 * GPIO to be output only. Ask the GPIO core to ignore this limit.
874 	 */
875 	{ "interrupt-gpios", &interrupt_gpios, 1, ACPI_GPIO_QUIRK_NO_IO_RESTRICTION },
876 	{ },
877 };
878 
879 static void sx9500_gpio_probe(struct i2c_client *client,
880 			      struct sx9500_data *data)
881 {
882 	struct gpio_desc *gpiod_int;
883 	struct device *dev;
884 	int ret;
885 
886 	if (!client)
887 		return;
888 
889 	dev = &client->dev;
890 
891 	ret = devm_acpi_dev_add_driver_gpios(dev, acpi_sx9500_gpios);
892 	if (ret)
893 		dev_dbg(dev, "Unable to add GPIO mapping table\n");
894 
895 	if (client->irq <= 0) {
896 		gpiod_int = devm_gpiod_get(dev, "interrupt", GPIOD_IN);
897 		if (IS_ERR(gpiod_int))
898 			dev_err(dev, "gpio get irq failed\n");
899 		else
900 			client->irq = gpiod_to_irq(gpiod_int);
901 	}
902 
903 	data->gpiod_rst = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
904 	if (IS_ERR(data->gpiod_rst)) {
905 		dev_warn(dev, "gpio get reset pin failed\n");
906 		data->gpiod_rst = NULL;
907 	}
908 }
909 
910 static int sx9500_probe(struct i2c_client *client,
911 			const struct i2c_device_id *id)
912 {
913 	int ret;
914 	struct iio_dev *indio_dev;
915 	struct sx9500_data *data;
916 
917 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
918 	if (indio_dev == NULL)
919 		return -ENOMEM;
920 
921 	data = iio_priv(indio_dev);
922 	data->client = client;
923 	mutex_init(&data->mutex);
924 	init_completion(&data->completion);
925 	data->trigger_enabled = false;
926 
927 	data->regmap = devm_regmap_init_i2c(client, &sx9500_regmap_config);
928 	if (IS_ERR(data->regmap))
929 		return PTR_ERR(data->regmap);
930 
931 	indio_dev->dev.parent = &client->dev;
932 	indio_dev->name = SX9500_DRIVER_NAME;
933 	indio_dev->channels = sx9500_channels;
934 	indio_dev->num_channels = ARRAY_SIZE(sx9500_channels);
935 	indio_dev->info = &sx9500_info;
936 	indio_dev->modes = INDIO_DIRECT_MODE;
937 	i2c_set_clientdata(client, indio_dev);
938 
939 	sx9500_gpio_probe(client, data);
940 
941 	ret = sx9500_init_device(indio_dev);
942 	if (ret < 0)
943 		return ret;
944 
945 	if (client->irq <= 0)
946 		dev_warn(&client->dev, "no valid irq found\n");
947 	else {
948 		ret = devm_request_threaded_irq(&client->dev, client->irq,
949 				sx9500_irq_handler, sx9500_irq_thread_handler,
950 				IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
951 				SX9500_IRQ_NAME, indio_dev);
952 		if (ret < 0)
953 			return ret;
954 
955 		data->trig = devm_iio_trigger_alloc(&client->dev,
956 				"%s-dev%d", indio_dev->name, indio_dev->id);
957 		if (!data->trig)
958 			return -ENOMEM;
959 
960 		data->trig->dev.parent = &client->dev;
961 		data->trig->ops = &sx9500_trigger_ops;
962 		iio_trigger_set_drvdata(data->trig, indio_dev);
963 
964 		ret = iio_trigger_register(data->trig);
965 		if (ret)
966 			return ret;
967 	}
968 
969 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
970 					 sx9500_trigger_handler,
971 					 &sx9500_buffer_setup_ops);
972 	if (ret < 0)
973 		goto out_trigger_unregister;
974 
975 	ret = iio_device_register(indio_dev);
976 	if (ret < 0)
977 		goto out_buffer_cleanup;
978 
979 	return 0;
980 
981 out_buffer_cleanup:
982 	iio_triggered_buffer_cleanup(indio_dev);
983 out_trigger_unregister:
984 	if (client->irq > 0)
985 		iio_trigger_unregister(data->trig);
986 
987 	return ret;
988 }
989 
990 static int sx9500_remove(struct i2c_client *client)
991 {
992 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
993 	struct sx9500_data *data = iio_priv(indio_dev);
994 
995 	iio_device_unregister(indio_dev);
996 	iio_triggered_buffer_cleanup(indio_dev);
997 	if (client->irq > 0)
998 		iio_trigger_unregister(data->trig);
999 	kfree(data->buffer);
1000 
1001 	return 0;
1002 }
1003 
1004 #ifdef CONFIG_PM_SLEEP
1005 static int sx9500_suspend(struct device *dev)
1006 {
1007 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1008 	struct sx9500_data *data = iio_priv(indio_dev);
1009 	int ret;
1010 
1011 	mutex_lock(&data->mutex);
1012 	ret = regmap_read(data->regmap, SX9500_REG_PROX_CTRL0,
1013 			  &data->suspend_ctrl0);
1014 	if (ret < 0)
1015 		goto out;
1016 
1017 	/*
1018 	 * Scan period doesn't matter because when all the sensors are
1019 	 * deactivated the device is in sleep mode.
1020 	 */
1021 	ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0, 0);
1022 
1023 out:
1024 	mutex_unlock(&data->mutex);
1025 	return ret;
1026 }
1027 
1028 static int sx9500_resume(struct device *dev)
1029 {
1030 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1031 	struct sx9500_data *data = iio_priv(indio_dev);
1032 	int ret;
1033 
1034 	mutex_lock(&data->mutex);
1035 	ret = regmap_write(data->regmap, SX9500_REG_PROX_CTRL0,
1036 			   data->suspend_ctrl0);
1037 	mutex_unlock(&data->mutex);
1038 
1039 	return ret;
1040 }
1041 #endif /* CONFIG_PM_SLEEP */
1042 
1043 static const struct dev_pm_ops sx9500_pm_ops = {
1044 	SET_SYSTEM_SLEEP_PM_OPS(sx9500_suspend, sx9500_resume)
1045 };
1046 
1047 static const struct acpi_device_id sx9500_acpi_match[] = {
1048 	{"SSX9500", 0},
1049 	{"SASX9500", 0},
1050 	{ },
1051 };
1052 MODULE_DEVICE_TABLE(acpi, sx9500_acpi_match);
1053 
1054 static const struct of_device_id sx9500_of_match[] = {
1055 	{ .compatible = "semtech,sx9500", },
1056 	{ }
1057 };
1058 MODULE_DEVICE_TABLE(of, sx9500_of_match);
1059 
1060 static const struct i2c_device_id sx9500_id[] = {
1061 	{"sx9500", 0},
1062 	{ },
1063 };
1064 MODULE_DEVICE_TABLE(i2c, sx9500_id);
1065 
1066 static struct i2c_driver sx9500_driver = {
1067 	.driver = {
1068 		.name	= SX9500_DRIVER_NAME,
1069 		.acpi_match_table = ACPI_PTR(sx9500_acpi_match),
1070 		.of_match_table = of_match_ptr(sx9500_of_match),
1071 		.pm = &sx9500_pm_ops,
1072 	},
1073 	.probe		= sx9500_probe,
1074 	.remove		= sx9500_remove,
1075 	.id_table	= sx9500_id,
1076 };
1077 module_i2c_driver(sx9500_driver);
1078 
1079 MODULE_AUTHOR("Vlad Dogaru <vlad.dogaru@intel.com>");
1080 MODULE_DESCRIPTION("Driver for Semtech SX9500 proximity sensor");
1081 MODULE_LICENSE("GPL v2");
1082