xref: /openbmc/linux/drivers/iio/proximity/sx9310.c (revision 297e77e5)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright 2018 Google LLC.
4  *
5  * Driver for Semtech's SX9310/SX9311 capacitive proximity/button solution.
6  * Based on SX9500 driver and Semtech driver using the input framework
7  * <https://my.syncplicity.com/share/teouwsim8niiaud/
8  *          linux-driver-SX9310_NoSmartHSensing>.
9  * Reworked April 2019 by Evan Green <evgreen@chromium.org>
10  * and January 2020 by Daniel Campello <campello@chromium.org>
11  */
12 
13 #include <linux/acpi.h>
14 #include <linux/delay.h>
15 #include <linux/i2c.h>
16 #include <linux/irq.h>
17 #include <linux/kernel.h>
18 #include <linux/module.h>
19 #include <linux/of.h>
20 #include <linux/pm.h>
21 #include <linux/regmap.h>
22 #include <linux/slab.h>
23 
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/events.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/triggered_buffer.h>
30 #include <linux/iio/trigger_consumer.h>
31 
32 /* Register definitions. */
33 #define SX9310_REG_IRQ_SRC				0x00
34 #define SX9310_REG_STAT0				0x01
35 #define SX9310_REG_STAT1				0x02
36 #define SX9310_REG_IRQ_MSK				0x03
37 #define   SX9310_CONVDONE_IRQ				BIT(3)
38 #define   SX9310_FAR_IRQ				BIT(5)
39 #define   SX9310_CLOSE_IRQ				BIT(6)
40 #define   SX9310_EVENT_IRQ				(SX9310_FAR_IRQ | \
41 							 SX9310_CLOSE_IRQ)
42 #define SX9310_REG_IRQ_FUNC				0x04
43 
44 #define SX9310_REG_PROX_CTRL0				0x10
45 #define   SX9310_REG_PROX_CTRL0_PROXSTAT2		0x10
46 #define   SX9310_REG_PROX_CTRL0_EN_MASK			0x0F
47 #define SX9310_REG_PROX_CTRL1				0x11
48 #define SX9310_REG_PROX_CTRL2				0x12
49 #define   SX9310_REG_PROX_CTRL2_COMBMODE_ALL		0x80
50 #define   SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC	0x04
51 #define SX9310_REG_PROX_CTRL3				0x13
52 #define   SX9310_REG_PROX_CTRL3_GAIN0_X8		0x0c
53 #define   SX9310_REG_PROX_CTRL3_GAIN12_X4		0x02
54 #define SX9310_REG_PROX_CTRL4				0x14
55 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST	0x07
56 #define SX9310_REG_PROX_CTRL5				0x15
57 #define   SX9310_REG_PROX_CTRL5_RANGE_SMALL		0xc0
58 #define   SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1		0x04
59 #define   SX9310_REG_PROX_CTRL5_RAWFILT_1P25		0x02
60 #define SX9310_REG_PROX_CTRL6				0x16
61 #define   SX9310_REG_PROX_CTRL6_COMP_COMMON		0x20
62 #define SX9310_REG_PROX_CTRL7				0x17
63 #define   SX9310_REG_PROX_CTRL7_AVGNEGFILT_2		0x08
64 #define   SX9310_REG_PROX_CTRL7_AVGPOSFILT_512		0x05
65 #define SX9310_REG_PROX_CTRL8				0x18
66 #define SX9310_REG_PROX_CTRL9				0x19
67 #define   SX9310_REG_PROX_CTRL8_9_PTHRESH12_28		0x40
68 #define   SX9310_REG_PROX_CTRL8_9_PTHRESH_96		0x88
69 #define   SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900	0x03
70 #define   SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500	0x05
71 #define SX9310_REG_PROX_CTRL10				0x1a
72 #define   SX9310_REG_PROX_CTRL10_HYST_6PCT		0x10
73 #define   SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8	0x12
74 #define   SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8		0x03
75 #define SX9310_REG_PROX_CTRL11				0x1b
76 #define SX9310_REG_PROX_CTRL12				0x1c
77 #define SX9310_REG_PROX_CTRL13				0x1d
78 #define SX9310_REG_PROX_CTRL14				0x1e
79 #define SX9310_REG_PROX_CTRL15				0x1f
80 #define SX9310_REG_PROX_CTRL16				0x20
81 #define SX9310_REG_PROX_CTRL17				0x21
82 #define SX9310_REG_PROX_CTRL18				0x22
83 #define SX9310_REG_PROX_CTRL19				0x23
84 #define SX9310_REG_SAR_CTRL0				0x2a
85 #define   SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES		0x40
86 #define   SX9310_REG_SAR_CTRL0_SARHYST_8		0x10
87 #define SX9310_REG_SAR_CTRL1				0x2b
88 /* Each increment of the slope register is 0.0078125. */
89 #define   SX9310_REG_SAR_CTRL1_SLOPE(_hnslope)		(_hnslope / 78125)
90 #define SX9310_REG_SAR_CTRL2				0x2c
91 #define   SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT	0x3c
92 
93 #define SX9310_REG_SENSOR_SEL				0x30
94 
95 #define SX9310_REG_USE_MSB				0x31
96 #define SX9310_REG_USE_LSB				0x32
97 
98 #define SX9310_REG_AVG_MSB				0x33
99 #define SX9310_REG_AVG_LSB				0x34
100 
101 #define SX9310_REG_DIFF_MSB				0x35
102 #define SX9310_REG_DIFF_LSB				0x36
103 
104 #define SX9310_REG_OFFSET_MSB				0x37
105 #define SX9310_REG_OFFSET_LSB				0x38
106 
107 #define SX9310_REG_SAR_MSB				0x39
108 #define SX9310_REG_SAR_LSB				0x3a
109 
110 #define SX9310_REG_I2CADDR				0x40
111 #define SX9310_REG_PAUSE				0x41
112 #define SX9310_REG_WHOAMI				0x42
113 #define   SX9310_WHOAMI_VALUE				0x01
114 #define   SX9311_WHOAMI_VALUE				0x02
115 
116 #define SX9310_REG_RESET				0x7f
117 #define   SX9310_SOFT_RESET				0xde
118 
119 #define SX9310_SCAN_PERIOD_MASK				GENMASK(7, 4)
120 #define SX9310_SCAN_PERIOD_SHIFT			4
121 
122 #define SX9310_COMPSTAT_MASK				GENMASK(3, 0)
123 
124 /* 4 hardware channels, as defined in STAT0: COMB, CS2, CS1 and CS0. */
125 #define SX9310_NUM_CHANNELS				4
126 #define SX9310_CHAN_ENABLED_MASK			GENMASK(3, 0)
127 
128 struct sx9310_data {
129 	/* Serialize access to registers and channel configuration */
130 	struct mutex mutex;
131 	struct i2c_client *client;
132 	struct iio_trigger *trig;
133 	struct regmap *regmap;
134 	/*
135 	 * Last reading of the proximity status for each channel.
136 	 * We only send an event to user space when this changes.
137 	 */
138 	bool prox_stat[SX9310_NUM_CHANNELS];
139 	bool trigger_enabled;
140 	__be16 buffer[SX9310_NUM_CHANNELS +
141 		      4]; /* 64-bit data + 64-bit timestamp */
142 	/* Remember enabled channels and sample rate during suspend. */
143 	unsigned int suspend_ctrl0;
144 	struct completion completion;
145 	unsigned int chan_read, chan_event;
146 	int channel_users[SX9310_NUM_CHANNELS];
147 	int whoami;
148 };
149 
150 static const struct iio_event_spec sx9310_events[] = {
151 	{
152 		.type = IIO_EV_TYPE_THRESH,
153 		.dir = IIO_EV_DIR_EITHER,
154 		.mask_separate = BIT(IIO_EV_INFO_ENABLE),
155 	},
156 };
157 
158 #define SX9310_NAMED_CHANNEL(idx, name)					 \
159 	{								 \
160 		.type = IIO_PROXIMITY,					 \
161 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		 \
162 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
163 		.indexed = 1,						 \
164 		.channel = idx,						 \
165 		.extend_name = name,					 \
166 		.address = SX9310_REG_DIFF_MSB,				 \
167 		.event_spec = sx9310_events,				 \
168 		.num_event_specs = ARRAY_SIZE(sx9310_events),		 \
169 		.scan_index = idx,					 \
170 		.scan_type = {						 \
171 			.sign = 's',					 \
172 			.realbits = 12,					 \
173 			.storagebits = 16,				 \
174 			.endianness = IIO_BE,				 \
175 		},							 \
176 	}
177 #define SX9310_CHANNEL(idx) SX9310_NAMED_CHANNEL(idx, NULL)
178 
179 static const struct iio_chan_spec sx9310_channels[] = {
180 	SX9310_CHANNEL(0),			/* CS0 */
181 	SX9310_CHANNEL(1),			/* CS1 */
182 	SX9310_CHANNEL(2),			/* CS2 */
183 	SX9310_NAMED_CHANNEL(3, "comb"),	/* COMB */
184 
185 	IIO_CHAN_SOFT_TIMESTAMP(4),
186 };
187 
188 /*
189  * Each entry contains the integer part (val) and the fractional part, in micro
190  * seconds. It conforms to the IIO output IIO_VAL_INT_PLUS_MICRO.
191  */
192 static const struct {
193 	int val;
194 	int val2;
195 } sx9310_samp_freq_table[] = {
196 	{ 500, 0 }, /* 0000: Min (no idle time) */
197 	{ 66, 666666 }, /* 0001: 15 ms */
198 	{ 33, 333333 }, /* 0010: 30 ms (Typ.) */
199 	{ 22, 222222 }, /* 0011: 45 ms */
200 	{ 16, 666666 }, /* 0100: 60 ms */
201 	{ 11, 111111 }, /* 0101: 90 ms */
202 	{ 8, 333333 }, /* 0110: 120 ms */
203 	{ 5, 0 }, /* 0111: 200 ms */
204 	{ 2, 500000 }, /* 1000: 400 ms */
205 	{ 1, 666666 }, /* 1001: 600 ms */
206 	{ 1, 250000 }, /* 1010: 800 ms */
207 	{ 1, 0 }, /* 1011: 1 s */
208 	{ 0, 500000 }, /* 1100: 2 s */
209 	{ 0, 333333 }, /* 1101: 3 s */
210 	{ 0, 250000 }, /* 1110: 4 s */
211 	{ 0, 200000 }, /* 1111: 5 s */
212 };
213 static const unsigned int sx9310_scan_period_table[] = {
214 	2,   15,  30,  45,   60,   90,	 120,  200,
215 	400, 600, 800, 1000, 2000, 3000, 4000, 5000,
216 };
217 
218 static ssize_t sx9310_show_samp_freq_avail(struct device *dev,
219 					   struct device_attribute *attr,
220 					   char *buf)
221 {
222 	size_t len = 0;
223 	int i;
224 
225 	for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++)
226 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%d ",
227 				 sx9310_samp_freq_table[i].val,
228 				 sx9310_samp_freq_table[i].val2);
229 	buf[len - 1] = '\n';
230 	return len;
231 }
232 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sx9310_show_samp_freq_avail);
233 
234 static const struct regmap_range sx9310_writable_reg_ranges[] = {
235 	regmap_reg_range(SX9310_REG_IRQ_MSK, SX9310_REG_IRQ_FUNC),
236 	regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19),
237 	regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2),
238 	regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SENSOR_SEL),
239 	regmap_reg_range(SX9310_REG_OFFSET_MSB, SX9310_REG_OFFSET_LSB),
240 	regmap_reg_range(SX9310_REG_PAUSE, SX9310_REG_PAUSE),
241 	regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
242 };
243 
244 static const struct regmap_access_table sx9310_writeable_regs = {
245 	.yes_ranges = sx9310_writable_reg_ranges,
246 	.n_yes_ranges = ARRAY_SIZE(sx9310_writable_reg_ranges),
247 };
248 
249 static const struct regmap_range sx9310_readable_reg_ranges[] = {
250 	regmap_reg_range(SX9310_REG_IRQ_SRC, SX9310_REG_IRQ_FUNC),
251 	regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19),
252 	regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2),
253 	regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SAR_LSB),
254 	regmap_reg_range(SX9310_REG_I2CADDR, SX9310_REG_WHOAMI),
255 	regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
256 };
257 
258 static const struct regmap_access_table sx9310_readable_regs = {
259 	.yes_ranges = sx9310_readable_reg_ranges,
260 	.n_yes_ranges = ARRAY_SIZE(sx9310_readable_reg_ranges),
261 };
262 
263 static const struct regmap_range sx9310_volatile_reg_ranges[] = {
264 	regmap_reg_range(SX9310_REG_IRQ_SRC, SX9310_REG_STAT1),
265 	regmap_reg_range(SX9310_REG_USE_MSB, SX9310_REG_DIFF_LSB),
266 	regmap_reg_range(SX9310_REG_SAR_MSB, SX9310_REG_SAR_LSB),
267 	regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
268 };
269 
270 static const struct regmap_access_table sx9310_volatile_regs = {
271 	.yes_ranges = sx9310_volatile_reg_ranges,
272 	.n_yes_ranges = ARRAY_SIZE(sx9310_volatile_reg_ranges),
273 };
274 
275 static const struct regmap_config sx9310_regmap_config = {
276 	.reg_bits = 8,
277 	.val_bits = 8,
278 
279 	.max_register = SX9310_REG_RESET,
280 	.cache_type = REGCACHE_RBTREE,
281 
282 	.wr_table = &sx9310_writeable_regs,
283 	.rd_table = &sx9310_readable_regs,
284 	.volatile_table = &sx9310_volatile_regs,
285 };
286 
287 static int sx9310_update_chan_en(struct sx9310_data *data,
288 				 unsigned int chan_read,
289 				 unsigned int chan_event)
290 {
291 	int ret;
292 
293 	if ((data->chan_read | data->chan_event) != (chan_read | chan_event)) {
294 		ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
295 					 SX9310_CHAN_ENABLED_MASK,
296 					 chan_read | chan_event);
297 		if (ret)
298 			return ret;
299 	}
300 	data->chan_read = chan_read;
301 	data->chan_event = chan_event;
302 	return 0;
303 }
304 
305 static int sx9310_get_read_channel(struct sx9310_data *data, int channel)
306 {
307 	return sx9310_update_chan_en(data, data->chan_read | BIT(channel),
308 				     data->chan_event);
309 }
310 
311 static int sx9310_put_read_channel(struct sx9310_data *data, int channel)
312 {
313 	return sx9310_update_chan_en(data, data->chan_read & ~BIT(channel),
314 				     data->chan_event);
315 }
316 
317 static int sx9310_get_event_channel(struct sx9310_data *data, int channel)
318 {
319 	return sx9310_update_chan_en(data, data->chan_read,
320 				     data->chan_event | BIT(channel));
321 }
322 
323 static int sx9310_put_event_channel(struct sx9310_data *data, int channel)
324 {
325 	return sx9310_update_chan_en(data, data->chan_read,
326 				     data->chan_event & ~BIT(channel));
327 }
328 
329 static int sx9310_enable_irq(struct sx9310_data *data, unsigned int irq)
330 {
331 	return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, irq);
332 }
333 
334 static int sx9310_disable_irq(struct sx9310_data *data, unsigned int irq)
335 {
336 	return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, 0);
337 }
338 
339 static int sx9310_read_prox_data(struct sx9310_data *data,
340 				 const struct iio_chan_spec *chan, __be16 *val)
341 {
342 	int ret;
343 
344 	ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel);
345 	if (ret < 0)
346 		return ret;
347 
348 	return regmap_bulk_read(data->regmap, chan->address, val, 2);
349 }
350 
351 /*
352  * If we have no interrupt support, we have to wait for a scan period
353  * after enabling a channel to get a result.
354  */
355 static int sx9310_wait_for_sample(struct sx9310_data *data)
356 {
357 	int ret;
358 	unsigned int val;
359 
360 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val);
361 	if (ret < 0)
362 		return ret;
363 
364 	val = (val & SX9310_SCAN_PERIOD_MASK) >> SX9310_SCAN_PERIOD_SHIFT;
365 
366 	msleep(sx9310_scan_period_table[val]);
367 
368 	return 0;
369 }
370 
371 static int sx9310_read_proximity(struct sx9310_data *data,
372 				 const struct iio_chan_spec *chan, int *val)
373 {
374 	int ret = 0;
375 	__be16 rawval;
376 
377 	mutex_lock(&data->mutex);
378 
379 	ret = sx9310_get_read_channel(data, chan->channel);
380 	if (ret < 0)
381 		goto out;
382 
383 	ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
384 	if (ret < 0)
385 		goto out_put_channel;
386 
387 	mutex_unlock(&data->mutex);
388 
389 	if (data->client->irq > 0) {
390 		ret = wait_for_completion_interruptible(&data->completion);
391 		reinit_completion(&data->completion);
392 	} else {
393 		ret = sx9310_wait_for_sample(data);
394 	}
395 
396 	mutex_lock(&data->mutex);
397 
398 	if (ret < 0)
399 		goto out_disable_irq;
400 
401 	ret = sx9310_read_prox_data(data, chan, &rawval);
402 	if (ret < 0)
403 		goto out_disable_irq;
404 
405 	*val = sign_extend32(be16_to_cpu(rawval),
406 			     (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));
407 
408 	ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
409 	if (ret < 0)
410 		goto out_put_channel;
411 
412 	ret = sx9310_put_read_channel(data, chan->channel);
413 	if (ret < 0)
414 		goto out;
415 
416 	mutex_unlock(&data->mutex);
417 
418 	return IIO_VAL_INT;
419 
420 out_disable_irq:
421 	sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
422 out_put_channel:
423 	sx9310_put_read_channel(data, chan->channel);
424 out:
425 	mutex_unlock(&data->mutex);
426 
427 	return ret;
428 }
429 
430 static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
431 {
432 	unsigned int regval;
433 	int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
434 
435 	if (ret < 0)
436 		return ret;
437 
438 	regval = (regval & SX9310_SCAN_PERIOD_MASK) >> SX9310_SCAN_PERIOD_SHIFT;
439 	*val = sx9310_samp_freq_table[regval].val;
440 	*val2 = sx9310_samp_freq_table[regval].val2;
441 
442 	return IIO_VAL_INT_PLUS_MICRO;
443 }
444 
445 static int sx9310_read_raw(struct iio_dev *indio_dev,
446 			   const struct iio_chan_spec *chan, int *val,
447 			   int *val2, long mask)
448 {
449 	struct sx9310_data *data = iio_priv(indio_dev);
450 	int ret;
451 
452 	if (chan->type != IIO_PROXIMITY)
453 		return -EINVAL;
454 
455 	switch (mask) {
456 	case IIO_CHAN_INFO_RAW:
457 		ret = iio_device_claim_direct_mode(indio_dev);
458 		if (ret)
459 			return ret;
460 
461 		ret = sx9310_read_proximity(data, chan, val);
462 		iio_device_release_direct_mode(indio_dev);
463 		return ret;
464 	case IIO_CHAN_INFO_SAMP_FREQ:
465 		return sx9310_read_samp_freq(data, val, val2);
466 	default:
467 		return -EINVAL;
468 	}
469 }
470 
471 static int sx9310_set_samp_freq(struct sx9310_data *data, int val, int val2)
472 {
473 	int i, ret;
474 
475 	for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++)
476 		if (val == sx9310_samp_freq_table[i].val &&
477 		    val2 == sx9310_samp_freq_table[i].val2)
478 			break;
479 
480 	if (i == ARRAY_SIZE(sx9310_samp_freq_table))
481 		return -EINVAL;
482 
483 	mutex_lock(&data->mutex);
484 
485 	ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
486 				 SX9310_SCAN_PERIOD_MASK,
487 				 i << SX9310_SCAN_PERIOD_SHIFT);
488 
489 	mutex_unlock(&data->mutex);
490 
491 	return ret;
492 }
493 
494 static int sx9310_write_raw(struct iio_dev *indio_dev,
495 			    const struct iio_chan_spec *chan, int val, int val2,
496 			    long mask)
497 {
498 	struct sx9310_data *data = iio_priv(indio_dev);
499 
500 	if (chan->type != IIO_PROXIMITY)
501 		return -EINVAL;
502 
503 	if (mask != IIO_CHAN_INFO_SAMP_FREQ)
504 		return -EINVAL;
505 
506 	return sx9310_set_samp_freq(data, val, val2);
507 }
508 
509 static irqreturn_t sx9310_irq_handler(int irq, void *private)
510 {
511 	struct iio_dev *indio_dev = private;
512 	struct sx9310_data *data = iio_priv(indio_dev);
513 
514 	if (data->trigger_enabled)
515 		iio_trigger_poll(data->trig);
516 
517 	/*
518 	 * Even if no event is enabled, we need to wake the thread to
519 	 * clear the interrupt state by reading SX9310_REG_IRQ_SRC.  It
520 	 * is not possible to do that here because regmap_read takes a
521 	 * mutex.
522 	 */
523 	return IRQ_WAKE_THREAD;
524 }
525 
526 static void sx9310_push_events(struct iio_dev *indio_dev)
527 {
528 	int ret;
529 	unsigned int val, chan;
530 	struct sx9310_data *data = iio_priv(indio_dev);
531 	s64 timestamp = iio_get_time_ns(indio_dev);
532 
533 	/* Read proximity state on all channels */
534 	ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val);
535 	if (ret < 0) {
536 		dev_err(&data->client->dev, "i2c transfer error in irq\n");
537 		return;
538 	}
539 
540 	for (chan = 0; chan < SX9310_NUM_CHANNELS; chan++) {
541 		int dir;
542 		u64 ev;
543 		bool new_prox = val & BIT(chan);
544 
545 		if (!(data->chan_event & BIT(chan)))
546 			continue;
547 		if (new_prox == data->prox_stat[chan])
548 			/* No change on this channel. */
549 			continue;
550 
551 		dir = new_prox ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
552 		ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
553 					  IIO_EV_TYPE_THRESH, dir);
554 
555 		iio_push_event(indio_dev, ev, timestamp);
556 		data->prox_stat[chan] = new_prox;
557 	}
558 }
559 
560 static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
561 {
562 	struct iio_dev *indio_dev = private;
563 	struct sx9310_data *data = iio_priv(indio_dev);
564 	int ret;
565 	unsigned int val;
566 
567 	mutex_lock(&data->mutex);
568 
569 	ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
570 	if (ret < 0) {
571 		dev_err(&data->client->dev, "i2c transfer error in irq\n");
572 		goto out;
573 	}
574 
575 	if (val & SX9310_EVENT_IRQ)
576 		sx9310_push_events(indio_dev);
577 
578 	if (val & SX9310_CONVDONE_IRQ)
579 		complete(&data->completion);
580 
581 out:
582 	mutex_unlock(&data->mutex);
583 
584 	return IRQ_HANDLED;
585 }
586 
587 static int sx9310_read_event_config(struct iio_dev *indio_dev,
588 				    const struct iio_chan_spec *chan,
589 				    enum iio_event_type type,
590 				    enum iio_event_direction dir)
591 {
592 	struct sx9310_data *data = iio_priv(indio_dev);
593 
594 	return !!(data->chan_event & BIT(chan->channel));
595 }
596 
597 static int sx9310_write_event_config(struct iio_dev *indio_dev,
598 				     const struct iio_chan_spec *chan,
599 				     enum iio_event_type type,
600 				     enum iio_event_direction dir, int state)
601 {
602 	struct sx9310_data *data = iio_priv(indio_dev);
603 	int ret;
604 
605 	/* If the state hasn't changed, there's nothing to do. */
606 	if (!!(data->chan_event & BIT(chan->channel)) == state)
607 		return 0;
608 
609 	mutex_lock(&data->mutex);
610 	if (state) {
611 		ret = sx9310_get_event_channel(data, chan->channel);
612 		if (ret < 0)
613 			goto out_unlock;
614 		if (!(data->chan_event & ~BIT(chan->channel))) {
615 			ret = sx9310_enable_irq(data, SX9310_EVENT_IRQ);
616 			if (ret < 0)
617 				sx9310_put_event_channel(data, chan->channel);
618 		}
619 	} else {
620 		ret = sx9310_put_event_channel(data, chan->channel);
621 		if (ret < 0)
622 			goto out_unlock;
623 		if (!data->chan_event) {
624 			ret = sx9310_disable_irq(data, SX9310_EVENT_IRQ);
625 			if (ret < 0)
626 				sx9310_get_event_channel(data, chan->channel);
627 		}
628 	}
629 
630 out_unlock:
631 	mutex_unlock(&data->mutex);
632 	return ret;
633 }
634 
635 static struct attribute *sx9310_attributes[] = {
636 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
637 	NULL,
638 };
639 
640 static const struct attribute_group sx9310_attribute_group = {
641 	.attrs = sx9310_attributes,
642 };
643 
644 static const struct iio_info sx9310_info = {
645 	.attrs = &sx9310_attribute_group,
646 	.read_raw = sx9310_read_raw,
647 	.write_raw = sx9310_write_raw,
648 	.read_event_config = sx9310_read_event_config,
649 	.write_event_config = sx9310_write_event_config,
650 };
651 
652 static int sx9310_set_trigger_state(struct iio_trigger *trig, bool state)
653 {
654 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
655 	struct sx9310_data *data = iio_priv(indio_dev);
656 	int ret = 0;
657 
658 	mutex_lock(&data->mutex);
659 
660 	if (state)
661 		ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
662 	else if (!data->chan_read)
663 		ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
664 	if (ret < 0)
665 		goto out;
666 
667 	data->trigger_enabled = state;
668 
669 out:
670 	mutex_unlock(&data->mutex);
671 
672 	return ret;
673 }
674 
675 static const struct iio_trigger_ops sx9310_trigger_ops = {
676 	.set_trigger_state = sx9310_set_trigger_state,
677 };
678 
679 static irqreturn_t sx9310_trigger_handler(int irq, void *private)
680 {
681 	struct iio_poll_func *pf = private;
682 	struct iio_dev *indio_dev = pf->indio_dev;
683 	struct sx9310_data *data = iio_priv(indio_dev);
684 	__be16 val;
685 	int bit, ret, i = 0;
686 
687 	mutex_lock(&data->mutex);
688 
689 	for_each_set_bit(bit, indio_dev->active_scan_mask,
690 			 indio_dev->masklength) {
691 		ret = sx9310_read_prox_data(data, &indio_dev->channels[bit],
692 					    &val);
693 		if (ret < 0)
694 			goto out;
695 
696 		data->buffer[i++] = val;
697 	}
698 
699 	iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
700 					   pf->timestamp);
701 
702 out:
703 	mutex_unlock(&data->mutex);
704 
705 	iio_trigger_notify_done(indio_dev->trig);
706 
707 	return IRQ_HANDLED;
708 }
709 
710 static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
711 {
712 	struct sx9310_data *data = iio_priv(indio_dev);
713 	unsigned int channels = 0;
714 	int bit, ret;
715 
716 	mutex_lock(&data->mutex);
717 	for_each_set_bit(bit, indio_dev->active_scan_mask,
718 			 indio_dev->masklength)
719 		channels |= BIT(indio_dev->channels[bit].channel);
720 
721 	ret = sx9310_update_chan_en(data, channels, data->chan_event);
722 	mutex_unlock(&data->mutex);
723 	return ret;
724 }
725 
726 static int sx9310_buffer_postdisable(struct iio_dev *indio_dev)
727 {
728 	struct sx9310_data *data = iio_priv(indio_dev);
729 	int ret;
730 
731 	mutex_lock(&data->mutex);
732 	ret = sx9310_update_chan_en(data, 0, data->chan_event);
733 	mutex_unlock(&data->mutex);
734 	return ret;
735 }
736 
737 static const struct iio_buffer_setup_ops sx9310_buffer_setup_ops = {
738 	.preenable = sx9310_buffer_preenable,
739 	.postdisable = sx9310_buffer_postdisable,
740 };
741 
742 struct sx9310_reg_default {
743 	u8 reg;
744 	u8 def;
745 };
746 
747 #define SX_INIT(_reg, _def)			\
748 	{					\
749 		.reg = SX9310_REG_##_reg,	\
750 		.def = _def,			\
751 	}
752 
753 static const struct sx9310_reg_default sx9310_default_regs[] = {
754 	SX_INIT(IRQ_MSK, 0x00),
755 	SX_INIT(IRQ_FUNC, 0x00),
756 	/*
757 	 * The lower 4 bits should not be set as it enable sensors measurements.
758 	 * Turning the detection on before the configuration values are set to
759 	 * good values can cause the device to return erroneous readings.
760 	 */
761 	SX_INIT(PROX_CTRL0, SX9310_REG_PROX_CTRL0_PROXSTAT2),
762 	SX_INIT(PROX_CTRL1, 0x00),
763 	SX_INIT(PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_ALL |
764 			    SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC),
765 	SX_INIT(PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
766 			    SX9310_REG_PROX_CTRL3_GAIN12_X4),
767 	SX_INIT(PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST),
768 	SX_INIT(PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
769 			    SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
770 			    SX9310_REG_PROX_CTRL5_RAWFILT_1P25),
771 	SX_INIT(PROX_CTRL6, SX9310_REG_PROX_CTRL6_COMP_COMMON),
772 	SX_INIT(PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
773 			    SX9310_REG_PROX_CTRL7_AVGPOSFILT_512),
774 	SX_INIT(PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
775 			    SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500),
776 	SX_INIT(PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH12_28 |
777 			    SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900),
778 	SX_INIT(PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
779 			     SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8 |
780 			     SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8),
781 	SX_INIT(PROX_CTRL11, 0x00),
782 	SX_INIT(PROX_CTRL12, 0x00),
783 	SX_INIT(PROX_CTRL13, 0x00),
784 	SX_INIT(PROX_CTRL14, 0x00),
785 	SX_INIT(PROX_CTRL15, 0x00),
786 	SX_INIT(PROX_CTRL16, 0x00),
787 	SX_INIT(PROX_CTRL17, 0x00),
788 	SX_INIT(PROX_CTRL18, 0x00),
789 	SX_INIT(PROX_CTRL19, 0x00),
790 	SX_INIT(SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
791 			   SX9310_REG_SAR_CTRL0_SARHYST_8),
792 	SX_INIT(SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250)),
793 	SX_INIT(SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT),
794 };
795 
796 /* Activate all channels and perform an initial compensation. */
797 static int sx9310_init_compensation(struct iio_dev *indio_dev)
798 {
799 	struct sx9310_data *data = iio_priv(indio_dev);
800 	int i, ret;
801 	unsigned int val;
802 	unsigned int ctrl0;
803 
804 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0);
805 	if (ret < 0)
806 		return ret;
807 
808 	/* run the compensation phase on all channels */
809 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
810 			   ctrl0 | SX9310_REG_PROX_CTRL0_EN_MASK);
811 	if (ret < 0)
812 		return ret;
813 
814 	for (i = 100; i >= 0; i--) {
815 		msleep(20);
816 		ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
817 		if (ret < 0)
818 			goto out;
819 		if (!(val & SX9310_COMPSTAT_MASK))
820 			break;
821 	}
822 
823 	if (i < 0) {
824 		dev_err(&data->client->dev,
825 			"initial compensation timed out: 0x%02x", val);
826 		ret = -ETIMEDOUT;
827 	}
828 
829 out:
830 	regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
831 	return ret;
832 }
833 
834 static int sx9310_init_device(struct iio_dev *indio_dev)
835 {
836 	struct sx9310_data *data = iio_priv(indio_dev);
837 	const struct sx9310_reg_default *initval;
838 	int ret;
839 	unsigned int i, val;
840 
841 	ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET);
842 	if (ret < 0)
843 		return ret;
844 
845 	usleep_range(1000, 2000); /* power-up time is ~1ms. */
846 
847 	/* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
848 	ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
849 	if (ret < 0)
850 		return ret;
851 
852 	/* Program some sane defaults. */
853 	for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
854 		initval = &sx9310_default_regs[i];
855 		ret = regmap_write(data->regmap, initval->reg, initval->def);
856 		if (ret < 0)
857 			return ret;
858 	}
859 
860 	return sx9310_init_compensation(indio_dev);
861 }
862 
863 static int sx9310_set_indio_dev_name(struct device *dev,
864 				     struct iio_dev *indio_dev,
865 				     const struct i2c_device_id *id, int whoami)
866 {
867 	const struct acpi_device_id *acpi_id;
868 
869 	/* id will be NULL when enumerated via ACPI */
870 	if (id) {
871 		if (id->driver_data != whoami)
872 			dev_err(dev, "WHOAMI does not match i2c_device_id: %s",
873 				id->name);
874 	} else if (ACPI_HANDLE(dev)) {
875 		acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
876 		if (!acpi_id)
877 			return -ENODEV;
878 		if (acpi_id->driver_data != whoami)
879 			dev_err(dev, "WHOAMI does not match acpi_device_id: %s",
880 				acpi_id->id);
881 	} else
882 		return -ENODEV;
883 
884 	switch (whoami) {
885 	case SX9310_WHOAMI_VALUE:
886 		indio_dev->name = "sx9310";
887 		break;
888 	case SX9311_WHOAMI_VALUE:
889 		indio_dev->name = "sx9311";
890 		break;
891 	default:
892 		dev_err(dev, "unexpected WHOAMI response: %u", whoami);
893 		return -ENODEV;
894 	}
895 
896 	return 0;
897 }
898 
899 static int sx9310_probe(struct i2c_client *client,
900 			const struct i2c_device_id *id)
901 {
902 	int ret;
903 	struct iio_dev *indio_dev;
904 	struct sx9310_data *data;
905 
906 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
907 	if (indio_dev == NULL)
908 		return -ENOMEM;
909 
910 	data = iio_priv(indio_dev);
911 	data->client = client;
912 	mutex_init(&data->mutex);
913 	init_completion(&data->completion);
914 
915 	data->regmap = devm_regmap_init_i2c(client, &sx9310_regmap_config);
916 	if (IS_ERR(data->regmap))
917 		return PTR_ERR(data->regmap);
918 
919 	ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
920 	if (ret < 0) {
921 		dev_err(&client->dev, "error in reading WHOAMI register: %d",
922 			ret);
923 		return ret;
924 	}
925 
926 	ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, id,
927 					data->whoami);
928 	if (ret < 0)
929 		return ret;
930 
931 	ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
932 	indio_dev->channels = sx9310_channels;
933 	indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
934 	indio_dev->info = &sx9310_info;
935 	indio_dev->modes = INDIO_DIRECT_MODE;
936 	i2c_set_clientdata(client, indio_dev);
937 
938 	ret = sx9310_init_device(indio_dev);
939 	if (ret < 0)
940 		return ret;
941 
942 	if (client->irq) {
943 		ret = devm_request_threaded_irq(&client->dev, client->irq,
944 						sx9310_irq_handler,
945 						sx9310_irq_thread_handler,
946 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
947 						"sx9310_event", indio_dev);
948 		if (ret < 0)
949 			return ret;
950 
951 		data->trig =
952 			devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
953 					       indio_dev->name, indio_dev->id);
954 		if (!data->trig)
955 			return -ENOMEM;
956 
957 		data->trig->dev.parent = &client->dev;
958 		data->trig->ops = &sx9310_trigger_ops;
959 		iio_trigger_set_drvdata(data->trig, indio_dev);
960 
961 		ret = devm_iio_trigger_register(&client->dev, data->trig);
962 		if (ret)
963 			return ret;
964 	}
965 
966 	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
967 					      iio_pollfunc_store_time,
968 					      sx9310_trigger_handler,
969 					      &sx9310_buffer_setup_ops);
970 	if (ret < 0)
971 		return ret;
972 
973 	return devm_iio_device_register(&client->dev, indio_dev);
974 }
975 
976 static int __maybe_unused sx9310_suspend(struct device *dev)
977 {
978 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
979 	struct sx9310_data *data = iio_priv(indio_dev);
980 	u8 ctrl0;
981 	int ret;
982 
983 	disable_irq_nosync(data->client->irq);
984 
985 	mutex_lock(&data->mutex);
986 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
987 			  &data->suspend_ctrl0);
988 
989 	if (ret)
990 		goto out;
991 
992 	ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_EN_MASK;
993 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
994 	if (ret)
995 		goto out;
996 
997 	ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 0);
998 
999 out:
1000 	mutex_unlock(&data->mutex);
1001 	return ret;
1002 }
1003 
1004 static int __maybe_unused sx9310_resume(struct device *dev)
1005 {
1006 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1007 	struct sx9310_data *data = iio_priv(indio_dev);
1008 	int ret;
1009 
1010 	mutex_lock(&data->mutex);
1011 	ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 1);
1012 	if (ret)
1013 		goto out;
1014 
1015 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
1016 			   data->suspend_ctrl0);
1017 
1018 out:
1019 	mutex_unlock(&data->mutex);
1020 
1021 	enable_irq(data->client->irq);
1022 
1023 	return ret;
1024 }
1025 
1026 static const struct dev_pm_ops sx9310_pm_ops = {
1027 	SET_SYSTEM_SLEEP_PM_OPS(sx9310_suspend, sx9310_resume)
1028 };
1029 
1030 static const struct acpi_device_id sx9310_acpi_match[] = {
1031 	{ "STH9310", SX9310_WHOAMI_VALUE },
1032 	{ "STH9311", SX9311_WHOAMI_VALUE },
1033 	{},
1034 };
1035 MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);
1036 
1037 static const struct of_device_id sx9310_of_match[] = {
1038 	{ .compatible = "semtech,sx9310" },
1039 	{ .compatible = "semtech,sx9311" },
1040 	{},
1041 };
1042 MODULE_DEVICE_TABLE(of, sx9310_of_match);
1043 
1044 static const struct i2c_device_id sx9310_id[] = {
1045 	{ "sx9310", SX9310_WHOAMI_VALUE },
1046 	{ "sx9311", SX9311_WHOAMI_VALUE },
1047 	{},
1048 };
1049 MODULE_DEVICE_TABLE(i2c, sx9310_id);
1050 
1051 static struct i2c_driver sx9310_driver = {
1052 	.driver = {
1053 		.name	= "sx9310",
1054 		.acpi_match_table = ACPI_PTR(sx9310_acpi_match),
1055 		.of_match_table = of_match_ptr(sx9310_of_match),
1056 		.pm = &sx9310_pm_ops,
1057 	},
1058 	.probe		= sx9310_probe,
1059 	.id_table	= sx9310_id,
1060 };
1061 module_i2c_driver(sx9310_driver);
1062 
1063 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
1064 MODULE_AUTHOR("Daniel Campello <campello@chromium.org>");
1065 MODULE_DESCRIPTION("Driver for Semtech SX9310/SX9311 proximity sensor");
1066 MODULE_LICENSE("GPL v2");
1067