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