xref: /openbmc/linux/drivers/iio/proximity/sx9310.c (revision b8265621)
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 	.postenable = iio_triggered_buffer_postenable,
740 	.predisable = iio_triggered_buffer_predisable,
741 	.postdisable = sx9310_buffer_postdisable,
742 };
743 
744 struct sx9310_reg_default {
745 	u8 reg;
746 	u8 def;
747 };
748 
749 #define SX_INIT(_reg, _def)			\
750 	{					\
751 		.reg = SX9310_REG_##_reg,	\
752 		.def = _def,			\
753 	}
754 
755 static const struct sx9310_reg_default sx9310_default_regs[] = {
756 	SX_INIT(IRQ_MSK, 0x00),
757 	SX_INIT(IRQ_FUNC, 0x00),
758 	/*
759 	 * The lower 4 bits should not be set as it enable sensors measurements.
760 	 * Turning the detection on before the configuration values are set to
761 	 * good values can cause the device to return erroneous readings.
762 	 */
763 	SX_INIT(PROX_CTRL0, SX9310_REG_PROX_CTRL0_PROXSTAT2),
764 	SX_INIT(PROX_CTRL1, 0x00),
765 	SX_INIT(PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_ALL |
766 			    SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC),
767 	SX_INIT(PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
768 			    SX9310_REG_PROX_CTRL3_GAIN12_X4),
769 	SX_INIT(PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST),
770 	SX_INIT(PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
771 			    SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
772 			    SX9310_REG_PROX_CTRL5_RAWFILT_1P25),
773 	SX_INIT(PROX_CTRL6, SX9310_REG_PROX_CTRL6_COMP_COMMON),
774 	SX_INIT(PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
775 			    SX9310_REG_PROX_CTRL7_AVGPOSFILT_512),
776 	SX_INIT(PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
777 			    SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500),
778 	SX_INIT(PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH12_28 |
779 			    SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900),
780 	SX_INIT(PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
781 			     SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_8 |
782 			     SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_8),
783 	SX_INIT(PROX_CTRL11, 0x00),
784 	SX_INIT(PROX_CTRL12, 0x00),
785 	SX_INIT(PROX_CTRL13, 0x00),
786 	SX_INIT(PROX_CTRL14, 0x00),
787 	SX_INIT(PROX_CTRL15, 0x00),
788 	SX_INIT(PROX_CTRL16, 0x00),
789 	SX_INIT(PROX_CTRL17, 0x00),
790 	SX_INIT(PROX_CTRL18, 0x00),
791 	SX_INIT(PROX_CTRL19, 0x00),
792 	SX_INIT(SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
793 			   SX9310_REG_SAR_CTRL0_SARHYST_8),
794 	SX_INIT(SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250)),
795 	SX_INIT(SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT),
796 };
797 
798 /* Activate all channels and perform an initial compensation. */
799 static int sx9310_init_compensation(struct iio_dev *indio_dev)
800 {
801 	struct sx9310_data *data = iio_priv(indio_dev);
802 	int i, ret;
803 	unsigned int val;
804 	unsigned int ctrl0;
805 
806 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0);
807 	if (ret < 0)
808 		return ret;
809 
810 	/* run the compensation phase on all channels */
811 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
812 			   ctrl0 | SX9310_REG_PROX_CTRL0_EN_MASK);
813 	if (ret < 0)
814 		return ret;
815 
816 	for (i = 100; i >= 0; i--) {
817 		msleep(20);
818 		ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
819 		if (ret < 0)
820 			goto out;
821 		if (!(val & SX9310_COMPSTAT_MASK))
822 			break;
823 	}
824 
825 	if (i < 0) {
826 		dev_err(&data->client->dev,
827 			"initial compensation timed out: 0x%02x", val);
828 		ret = -ETIMEDOUT;
829 	}
830 
831 out:
832 	regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
833 	return ret;
834 }
835 
836 static int sx9310_init_device(struct iio_dev *indio_dev)
837 {
838 	struct sx9310_data *data = iio_priv(indio_dev);
839 	const struct sx9310_reg_default *initval;
840 	int ret;
841 	unsigned int i, val;
842 
843 	ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET);
844 	if (ret < 0)
845 		return ret;
846 
847 	usleep_range(1000, 2000); /* power-up time is ~1ms. */
848 
849 	/* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
850 	ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
851 	if (ret < 0)
852 		return ret;
853 
854 	/* Program some sane defaults. */
855 	for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
856 		initval = &sx9310_default_regs[i];
857 		ret = regmap_write(data->regmap, initval->reg, initval->def);
858 		if (ret < 0)
859 			return ret;
860 	}
861 
862 	return sx9310_init_compensation(indio_dev);
863 }
864 
865 static int sx9310_set_indio_dev_name(struct device *dev,
866 				     struct iio_dev *indio_dev,
867 				     const struct i2c_device_id *id, int whoami)
868 {
869 	const struct acpi_device_id *acpi_id;
870 
871 	/* id will be NULL when enumerated via ACPI */
872 	if (id) {
873 		if (id->driver_data != whoami)
874 			dev_err(dev, "WHOAMI does not match i2c_device_id: %s",
875 				id->name);
876 	} else if (ACPI_HANDLE(dev)) {
877 		acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
878 		if (!acpi_id)
879 			return -ENODEV;
880 		if (acpi_id->driver_data != whoami)
881 			dev_err(dev, "WHOAMI does not match acpi_device_id: %s",
882 				acpi_id->id);
883 	} else
884 		return -ENODEV;
885 
886 	switch (whoami) {
887 	case SX9310_WHOAMI_VALUE:
888 		indio_dev->name = "sx9310";
889 		break;
890 	case SX9311_WHOAMI_VALUE:
891 		indio_dev->name = "sx9311";
892 		break;
893 	default:
894 		dev_err(dev, "unexpected WHOAMI response: %u", whoami);
895 		return -ENODEV;
896 	}
897 
898 	return 0;
899 }
900 
901 static int sx9310_probe(struct i2c_client *client,
902 			const struct i2c_device_id *id)
903 {
904 	int ret;
905 	struct iio_dev *indio_dev;
906 	struct sx9310_data *data;
907 
908 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
909 	if (indio_dev == NULL)
910 		return -ENOMEM;
911 
912 	data = iio_priv(indio_dev);
913 	data->client = client;
914 	mutex_init(&data->mutex);
915 	init_completion(&data->completion);
916 
917 	data->regmap = devm_regmap_init_i2c(client, &sx9310_regmap_config);
918 	if (IS_ERR(data->regmap))
919 		return PTR_ERR(data->regmap);
920 
921 	ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
922 	if (ret < 0) {
923 		dev_err(&client->dev, "error in reading WHOAMI register: %d",
924 			ret);
925 		return ret;
926 	}
927 
928 	ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, id,
929 					data->whoami);
930 	if (ret < 0)
931 		return ret;
932 
933 	ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
934 	indio_dev->dev.parent = &client->dev;
935 	indio_dev->channels = sx9310_channels;
936 	indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
937 	indio_dev->info = &sx9310_info;
938 	indio_dev->modes = INDIO_DIRECT_MODE;
939 	i2c_set_clientdata(client, indio_dev);
940 
941 	ret = sx9310_init_device(indio_dev);
942 	if (ret < 0)
943 		return ret;
944 
945 	if (client->irq) {
946 		ret = devm_request_threaded_irq(&client->dev, client->irq,
947 						sx9310_irq_handler,
948 						sx9310_irq_thread_handler,
949 						IRQF_TRIGGER_LOW | IRQF_ONESHOT,
950 						"sx9310_event", indio_dev);
951 		if (ret < 0)
952 			return ret;
953 
954 		data->trig =
955 			devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
956 					       indio_dev->name, indio_dev->id);
957 		if (!data->trig)
958 			return -ENOMEM;
959 
960 		data->trig->dev.parent = &client->dev;
961 		data->trig->ops = &sx9310_trigger_ops;
962 		iio_trigger_set_drvdata(data->trig, indio_dev);
963 
964 		ret = devm_iio_trigger_register(&client->dev, data->trig);
965 		if (ret)
966 			return ret;
967 	}
968 
969 	ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
970 					      iio_pollfunc_store_time,
971 					      sx9310_trigger_handler,
972 					      &sx9310_buffer_setup_ops);
973 	if (ret < 0)
974 		return ret;
975 
976 	return devm_iio_device_register(&client->dev, indio_dev);
977 }
978 
979 static int __maybe_unused sx9310_suspend(struct device *dev)
980 {
981 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
982 	struct sx9310_data *data = iio_priv(indio_dev);
983 	u8 ctrl0;
984 	int ret;
985 
986 	disable_irq_nosync(data->client->irq);
987 
988 	mutex_lock(&data->mutex);
989 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
990 			  &data->suspend_ctrl0);
991 
992 	if (ret)
993 		goto out;
994 
995 	ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_EN_MASK;
996 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
997 	if (ret)
998 		goto out;
999 
1000 	ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 0);
1001 
1002 out:
1003 	mutex_unlock(&data->mutex);
1004 	return ret;
1005 }
1006 
1007 static int __maybe_unused sx9310_resume(struct device *dev)
1008 {
1009 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1010 	struct sx9310_data *data = iio_priv(indio_dev);
1011 	int ret;
1012 
1013 	mutex_lock(&data->mutex);
1014 	ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 1);
1015 	if (ret)
1016 		goto out;
1017 
1018 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
1019 			   data->suspend_ctrl0);
1020 
1021 out:
1022 	mutex_unlock(&data->mutex);
1023 
1024 	enable_irq(data->client->irq);
1025 
1026 	return ret;
1027 }
1028 
1029 static const struct dev_pm_ops sx9310_pm_ops = {
1030 	SET_SYSTEM_SLEEP_PM_OPS(sx9310_suspend, sx9310_resume)
1031 };
1032 
1033 static const struct acpi_device_id sx9310_acpi_match[] = {
1034 	{ "STH9310", SX9310_WHOAMI_VALUE },
1035 	{ "STH9311", SX9311_WHOAMI_VALUE },
1036 	{},
1037 };
1038 MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);
1039 
1040 static const struct of_device_id sx9310_of_match[] = {
1041 	{ .compatible = "semtech,sx9310" },
1042 	{ .compatible = "semtech,sx9311" },
1043 	{},
1044 };
1045 MODULE_DEVICE_TABLE(of, sx9310_of_match);
1046 
1047 static const struct i2c_device_id sx9310_id[] = {
1048 	{ "sx9310", SX9310_WHOAMI_VALUE },
1049 	{ "sx9311", SX9311_WHOAMI_VALUE },
1050 	{},
1051 };
1052 MODULE_DEVICE_TABLE(i2c, sx9310_id);
1053 
1054 static struct i2c_driver sx9310_driver = {
1055 	.driver = {
1056 		.name	= "sx9310",
1057 		.acpi_match_table = ACPI_PTR(sx9310_acpi_match),
1058 		.of_match_table = of_match_ptr(sx9310_of_match),
1059 		.pm = &sx9310_pm_ops,
1060 	},
1061 	.probe		= sx9310_probe,
1062 	.id_table	= sx9310_id,
1063 };
1064 module_i2c_driver(sx9310_driver);
1065 
1066 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
1067 MODULE_AUTHOR("Daniel Campello <campello@chromium.org>");
1068 MODULE_DESCRIPTION("Driver for Semtech SX9310/SX9311 proximity sensor");
1069 MODULE_LICENSE("GPL v2");
1070