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