xref: /linux/drivers/iio/proximity/sx9310.c (revision c79c3c34f75d72a066e292b10aa50fc758c97c89)
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 in April 2019 by Evan Green <evgreen@chromium.org>
10  * and in January 2020 by Daniel Campello <campello@chromium.org>.
11  */
12 
13 #include <linux/acpi.h>
14 #include <linux/bitfield.h>
15 #include <linux/delay.h>
16 #include <linux/i2c.h>
17 #include <linux/irq.h>
18 #include <linux/kernel.h>
19 #include <linux/log2.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/module.h>
22 #include <linux/pm.h>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
26 
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/events.h>
29 #include <linux/iio/iio.h>
30 #include <linux/iio/sysfs.h>
31 #include <linux/iio/trigger.h>
32 #include <linux/iio/triggered_buffer.h>
33 #include <linux/iio/trigger_consumer.h>
34 
35 /* Register definitions. */
36 #define SX9310_REG_IRQ_SRC				0x00
37 #define SX9310_REG_STAT0				0x01
38 #define SX9310_REG_STAT1				0x02
39 #define SX9310_REG_STAT1_COMPSTAT_MASK			GENMASK(3, 0)
40 #define SX9310_REG_IRQ_MSK				0x03
41 #define   SX9310_CONVDONE_IRQ				BIT(3)
42 #define   SX9310_FAR_IRQ				BIT(5)
43 #define   SX9310_CLOSE_IRQ				BIT(6)
44 #define SX9310_REG_IRQ_FUNC				0x04
45 
46 #define SX9310_REG_PROX_CTRL0				0x10
47 #define   SX9310_REG_PROX_CTRL0_SENSOREN_MASK		GENMASK(3, 0)
48 #define   SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK		GENMASK(7, 4)
49 #define   SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS		0x01
50 #define SX9310_REG_PROX_CTRL1				0x11
51 #define SX9310_REG_PROX_CTRL2				0x12
52 #define   SX9310_REG_PROX_CTRL2_COMBMODE_MASK		GENMASK(7, 6)
53 #define   SX9310_REG_PROX_CTRL2_COMBMODE_CS0_CS1_CS2_CS3 (0x03 << 6)
54 #define   SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2	(0x02 << 6)
55 #define   SX9310_REG_PROX_CTRL2_COMBMODE_CS0_CS1	(0x01 << 6)
56 #define   SX9310_REG_PROX_CTRL2_COMBMODE_CS3		(0x00 << 6)
57 #define   SX9310_REG_PROX_CTRL2_SHIELDEN_MASK		GENMASK(3, 2)
58 #define   SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC	(0x01 << 2)
59 #define   SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND		(0x02 << 2)
60 #define SX9310_REG_PROX_CTRL3				0x13
61 #define   SX9310_REG_PROX_CTRL3_GAIN0_MASK		GENMASK(3, 2)
62 #define   SX9310_REG_PROX_CTRL3_GAIN0_X8		(0x03 << 2)
63 #define   SX9310_REG_PROX_CTRL3_GAIN12_MASK		GENMASK(1, 0)
64 #define   SX9310_REG_PROX_CTRL3_GAIN12_X4		0x02
65 #define SX9310_REG_PROX_CTRL4				0x14
66 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_MASK		GENMASK(2, 0)
67 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST	0x07
68 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_VERY_FINE	0x06
69 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_FINE		0x05
70 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_MEDIUM	0x04
71 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_MEDIUM_COARSE 0x03
72 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_COARSE	0x02
73 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_VERY_COARSE	0x01
74 #define   SX9310_REG_PROX_CTRL4_RESOLUTION_COARSEST	0x00
75 #define SX9310_REG_PROX_CTRL5				0x15
76 #define   SX9310_REG_PROX_CTRL5_RANGE_SMALL		(0x03 << 6)
77 #define   SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK	GENMASK(3, 2)
78 #define   SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1		(0x01 << 2)
79 #define   SX9310_REG_PROX_CTRL5_RAWFILT_MASK		GENMASK(1, 0)
80 #define   SX9310_REG_PROX_CTRL5_RAWFILT_SHIFT		0
81 #define   SX9310_REG_PROX_CTRL5_RAWFILT_1P25		0x02
82 #define SX9310_REG_PROX_CTRL6				0x16
83 #define   SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT	0x20
84 #define SX9310_REG_PROX_CTRL7				0x17
85 #define   SX9310_REG_PROX_CTRL7_AVGNEGFILT_2		(0x01 << 3)
86 #define   SX9310_REG_PROX_CTRL7_AVGPOSFILT_MASK		GENMASK(2, 0)
87 #define   SX9310_REG_PROX_CTRL7_AVGPOSFILT_SHIFT	0
88 #define   SX9310_REG_PROX_CTRL7_AVGPOSFILT_512		0x05
89 #define SX9310_REG_PROX_CTRL8				0x18
90 #define   SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK		GENMASK(7, 3)
91 #define SX9310_REG_PROX_CTRL9				0x19
92 #define   SX9310_REG_PROX_CTRL8_9_PTHRESH_28		(0x08 << 3)
93 #define   SX9310_REG_PROX_CTRL8_9_PTHRESH_96		(0x11 << 3)
94 #define   SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900	0x03
95 #define   SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500	0x05
96 #define SX9310_REG_PROX_CTRL10				0x1a
97 #define   SX9310_REG_PROX_CTRL10_HYST_MASK		GENMASK(5, 4)
98 #define   SX9310_REG_PROX_CTRL10_HYST_6PCT		(0x01 << 4)
99 #define   SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK	GENMASK(3, 2)
100 #define   SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK	GENMASK(1, 0)
101 #define   SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2		0x01
102 #define SX9310_REG_PROX_CTRL11				0x1b
103 #define SX9310_REG_PROX_CTRL12				0x1c
104 #define SX9310_REG_PROX_CTRL13				0x1d
105 #define SX9310_REG_PROX_CTRL14				0x1e
106 #define SX9310_REG_PROX_CTRL15				0x1f
107 #define SX9310_REG_PROX_CTRL16				0x20
108 #define SX9310_REG_PROX_CTRL17				0x21
109 #define SX9310_REG_PROX_CTRL18				0x22
110 #define SX9310_REG_PROX_CTRL19				0x23
111 #define SX9310_REG_SAR_CTRL0				0x2a
112 #define   SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES		(0x02 << 5)
113 #define   SX9310_REG_SAR_CTRL0_SARHYST_8		(0x02 << 3)
114 #define SX9310_REG_SAR_CTRL1				0x2b
115 /* Each increment of the slope register is 0.0078125. */
116 #define   SX9310_REG_SAR_CTRL1_SLOPE(_hnslope)		(_hnslope / 78125)
117 #define SX9310_REG_SAR_CTRL2				0x2c
118 #define   SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT	0x3c
119 
120 #define SX9310_REG_SENSOR_SEL				0x30
121 #define SX9310_REG_USE_MSB				0x31
122 #define SX9310_REG_USE_LSB				0x32
123 #define SX9310_REG_AVG_MSB				0x33
124 #define SX9310_REG_AVG_LSB				0x34
125 #define SX9310_REG_DIFF_MSB				0x35
126 #define SX9310_REG_DIFF_LSB				0x36
127 #define SX9310_REG_OFFSET_MSB				0x37
128 #define SX9310_REG_OFFSET_LSB				0x38
129 #define SX9310_REG_SAR_MSB				0x39
130 #define SX9310_REG_SAR_LSB				0x3a
131 #define SX9310_REG_I2C_ADDR				0x40
132 #define SX9310_REG_PAUSE				0x41
133 #define SX9310_REG_WHOAMI				0x42
134 #define   SX9310_WHOAMI_VALUE				0x01
135 #define   SX9311_WHOAMI_VALUE				0x02
136 #define SX9310_REG_RESET				0x7f
137 #define   SX9310_SOFT_RESET				0xde
138 
139 
140 /* 4 hardware channels, as defined in STAT0: COMB, CS2, CS1 and CS0. */
141 #define SX9310_NUM_CHANNELS				4
142 static_assert(SX9310_NUM_CHANNELS < BITS_PER_LONG);
143 
144 struct sx9310_data {
145 	/* Serialize access to registers and channel configuration */
146 	struct mutex mutex;
147 	struct i2c_client *client;
148 	struct iio_trigger *trig;
149 	struct regmap *regmap;
150 	struct regulator_bulk_data supplies[2];
151 	/*
152 	 * Last reading of the proximity status for each channel.
153 	 * We only send an event to user space when this changes.
154 	 */
155 	unsigned long chan_prox_stat;
156 	bool trigger_enabled;
157 	/* Ensure correct alignment of timestamp when present. */
158 	struct {
159 		__be16 channels[SX9310_NUM_CHANNELS];
160 		s64 ts __aligned(8);
161 	} buffer;
162 	/* Remember enabled channels and sample rate during suspend. */
163 	unsigned int suspend_ctrl0;
164 	struct completion completion;
165 	unsigned long chan_read;
166 	unsigned long chan_event;
167 	unsigned int whoami;
168 };
169 
170 static const struct iio_event_spec sx9310_events[] = {
171 	{
172 		.type = IIO_EV_TYPE_THRESH,
173 		.dir = IIO_EV_DIR_RISING,
174 		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
175 	},
176 	{
177 		.type = IIO_EV_TYPE_THRESH,
178 		.dir = IIO_EV_DIR_FALLING,
179 		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD),
180 	},
181 	{
182 		.type = IIO_EV_TYPE_THRESH,
183 		.dir = IIO_EV_DIR_EITHER,
184 		.mask_separate = BIT(IIO_EV_INFO_ENABLE) |
185 				 BIT(IIO_EV_INFO_HYSTERESIS) |
186 				 BIT(IIO_EV_INFO_VALUE),
187 	},
188 };
189 
190 #define SX9310_NAMED_CHANNEL(idx, name)					 \
191 	{								 \
192 		.type = IIO_PROXIMITY,					 \
193 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |		 \
194 				      BIT(IIO_CHAN_INFO_HARDWAREGAIN),   \
195 		.info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
196 		.info_mask_separate_available =				 \
197 			BIT(IIO_CHAN_INFO_HARDWAREGAIN),		 \
198 		.indexed = 1,						 \
199 		.channel = idx,						 \
200 		.extend_name = name,					 \
201 		.address = SX9310_REG_DIFF_MSB,				 \
202 		.event_spec = sx9310_events,				 \
203 		.num_event_specs = ARRAY_SIZE(sx9310_events),		 \
204 		.scan_index = idx,					 \
205 		.scan_type = {						 \
206 			.sign = 's',					 \
207 			.realbits = 12,					 \
208 			.storagebits = 16,				 \
209 			.endianness = IIO_BE,				 \
210 		},							 \
211 	}
212 #define SX9310_CHANNEL(idx) SX9310_NAMED_CHANNEL(idx, NULL)
213 
214 static const struct iio_chan_spec sx9310_channels[] = {
215 	SX9310_CHANNEL(0),			/* CS0 */
216 	SX9310_CHANNEL(1),			/* CS1 */
217 	SX9310_CHANNEL(2),			/* CS2 */
218 	SX9310_NAMED_CHANNEL(3, "comb"),	/* COMB */
219 
220 	IIO_CHAN_SOFT_TIMESTAMP(4),
221 };
222 
223 /*
224  * Each entry contains the integer part (val) and the fractional part, in micro
225  * seconds. It conforms to the IIO output IIO_VAL_INT_PLUS_MICRO.
226  */
227 static const struct {
228 	int val;
229 	int val2;
230 } sx9310_samp_freq_table[] = {
231 	{ 500, 0 }, /* 0000: Min (no idle time) */
232 	{ 66, 666666 }, /* 0001: 15 ms */
233 	{ 33, 333333 }, /* 0010: 30 ms (Typ.) */
234 	{ 22, 222222 }, /* 0011: 45 ms */
235 	{ 16, 666666 }, /* 0100: 60 ms */
236 	{ 11, 111111 }, /* 0101: 90 ms */
237 	{ 8, 333333 }, /* 0110: 120 ms */
238 	{ 5, 0 }, /* 0111: 200 ms */
239 	{ 2, 500000 }, /* 1000: 400 ms */
240 	{ 1, 666666 }, /* 1001: 600 ms */
241 	{ 1, 250000 }, /* 1010: 800 ms */
242 	{ 1, 0 }, /* 1011: 1 s */
243 	{ 0, 500000 }, /* 1100: 2 s */
244 	{ 0, 333333 }, /* 1101: 3 s */
245 	{ 0, 250000 }, /* 1110: 4 s */
246 	{ 0, 200000 }, /* 1111: 5 s */
247 };
248 static const unsigned int sx9310_scan_period_table[] = {
249 	2,   15,  30,  45,   60,   90,	 120,  200,
250 	400, 600, 800, 1000, 2000, 3000, 4000, 5000,
251 };
252 
253 static ssize_t sx9310_show_samp_freq_avail(struct device *dev,
254 					   struct device_attribute *attr,
255 					   char *buf)
256 {
257 	size_t len = 0;
258 	int i;
259 
260 	for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++)
261 		len += scnprintf(buf + len, PAGE_SIZE - len, "%d.%d ",
262 				 sx9310_samp_freq_table[i].val,
263 				 sx9310_samp_freq_table[i].val2);
264 	buf[len - 1] = '\n';
265 	return len;
266 }
267 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sx9310_show_samp_freq_avail);
268 
269 static const struct regmap_range sx9310_writable_reg_ranges[] = {
270 	regmap_reg_range(SX9310_REG_IRQ_MSK, SX9310_REG_IRQ_FUNC),
271 	regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19),
272 	regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2),
273 	regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SENSOR_SEL),
274 	regmap_reg_range(SX9310_REG_OFFSET_MSB, SX9310_REG_OFFSET_LSB),
275 	regmap_reg_range(SX9310_REG_PAUSE, SX9310_REG_PAUSE),
276 	regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
277 };
278 
279 static const struct regmap_access_table sx9310_writeable_regs = {
280 	.yes_ranges = sx9310_writable_reg_ranges,
281 	.n_yes_ranges = ARRAY_SIZE(sx9310_writable_reg_ranges),
282 };
283 
284 static const struct regmap_range sx9310_readable_reg_ranges[] = {
285 	regmap_reg_range(SX9310_REG_IRQ_SRC, SX9310_REG_IRQ_FUNC),
286 	regmap_reg_range(SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL19),
287 	regmap_reg_range(SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL2),
288 	regmap_reg_range(SX9310_REG_SENSOR_SEL, SX9310_REG_SAR_LSB),
289 	regmap_reg_range(SX9310_REG_I2C_ADDR, SX9310_REG_WHOAMI),
290 	regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
291 };
292 
293 static const struct regmap_access_table sx9310_readable_regs = {
294 	.yes_ranges = sx9310_readable_reg_ranges,
295 	.n_yes_ranges = ARRAY_SIZE(sx9310_readable_reg_ranges),
296 };
297 
298 static const struct regmap_range sx9310_volatile_reg_ranges[] = {
299 	regmap_reg_range(SX9310_REG_IRQ_SRC, SX9310_REG_STAT1),
300 	regmap_reg_range(SX9310_REG_USE_MSB, SX9310_REG_DIFF_LSB),
301 	regmap_reg_range(SX9310_REG_SAR_MSB, SX9310_REG_SAR_LSB),
302 	regmap_reg_range(SX9310_REG_RESET, SX9310_REG_RESET),
303 };
304 
305 static const struct regmap_access_table sx9310_volatile_regs = {
306 	.yes_ranges = sx9310_volatile_reg_ranges,
307 	.n_yes_ranges = ARRAY_SIZE(sx9310_volatile_reg_ranges),
308 };
309 
310 static const struct regmap_config sx9310_regmap_config = {
311 	.reg_bits = 8,
312 	.val_bits = 8,
313 
314 	.max_register = SX9310_REG_RESET,
315 	.cache_type = REGCACHE_RBTREE,
316 
317 	.wr_table = &sx9310_writeable_regs,
318 	.rd_table = &sx9310_readable_regs,
319 	.volatile_table = &sx9310_volatile_regs,
320 };
321 
322 static int sx9310_update_chan_en(struct sx9310_data *data,
323 				 unsigned long chan_read,
324 				 unsigned long chan_event)
325 {
326 	int ret;
327 	unsigned long channels = chan_read | chan_event;
328 
329 	if ((data->chan_read | data->chan_event) != channels) {
330 		ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL0,
331 					 SX9310_REG_PROX_CTRL0_SENSOREN_MASK,
332 					 channels);
333 		if (ret)
334 			return ret;
335 	}
336 	data->chan_read = chan_read;
337 	data->chan_event = chan_event;
338 	return 0;
339 }
340 
341 static int sx9310_get_read_channel(struct sx9310_data *data, int channel)
342 {
343 	return sx9310_update_chan_en(data, data->chan_read | BIT(channel),
344 				     data->chan_event);
345 }
346 
347 static int sx9310_put_read_channel(struct sx9310_data *data, int channel)
348 {
349 	return sx9310_update_chan_en(data, data->chan_read & ~BIT(channel),
350 				     data->chan_event);
351 }
352 
353 static int sx9310_get_event_channel(struct sx9310_data *data, int channel)
354 {
355 	return sx9310_update_chan_en(data, data->chan_read,
356 				     data->chan_event | BIT(channel));
357 }
358 
359 static int sx9310_put_event_channel(struct sx9310_data *data, int channel)
360 {
361 	return sx9310_update_chan_en(data, data->chan_read,
362 				     data->chan_event & ~BIT(channel));
363 }
364 
365 static int sx9310_enable_irq(struct sx9310_data *data, unsigned int irq)
366 {
367 	if (!data->client->irq)
368 		return 0;
369 	return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, irq);
370 }
371 
372 static int sx9310_disable_irq(struct sx9310_data *data, unsigned int irq)
373 {
374 	if (!data->client->irq)
375 		return 0;
376 	return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, 0);
377 }
378 
379 static int sx9310_read_prox_data(struct sx9310_data *data,
380 				 const struct iio_chan_spec *chan, __be16 *val)
381 {
382 	int ret;
383 
384 	ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel);
385 	if (ret)
386 		return ret;
387 
388 	return regmap_bulk_read(data->regmap, chan->address, val, sizeof(*val));
389 }
390 
391 /*
392  * If we have no interrupt support, we have to wait for a scan period
393  * after enabling a channel to get a result.
394  */
395 static int sx9310_wait_for_sample(struct sx9310_data *data)
396 {
397 	int ret;
398 	unsigned int val;
399 
400 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val);
401 	if (ret)
402 		return ret;
403 
404 	val = FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, val);
405 
406 	msleep(sx9310_scan_period_table[val]);
407 
408 	return 0;
409 }
410 
411 static int sx9310_read_proximity(struct sx9310_data *data,
412 				 const struct iio_chan_spec *chan, int *val)
413 {
414 	int ret;
415 	__be16 rawval;
416 
417 	mutex_lock(&data->mutex);
418 
419 	ret = sx9310_get_read_channel(data, chan->channel);
420 	if (ret)
421 		goto out;
422 
423 	ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
424 	if (ret)
425 		goto out_put_channel;
426 
427 	mutex_unlock(&data->mutex);
428 
429 	if (data->client->irq) {
430 		ret = wait_for_completion_interruptible(&data->completion);
431 		reinit_completion(&data->completion);
432 	} else {
433 		ret = sx9310_wait_for_sample(data);
434 	}
435 
436 	mutex_lock(&data->mutex);
437 
438 	if (ret)
439 		goto out_disable_irq;
440 
441 	ret = sx9310_read_prox_data(data, chan, &rawval);
442 	if (ret)
443 		goto out_disable_irq;
444 
445 	*val = sign_extend32(be16_to_cpu(rawval),
446 			     chan->address == SX9310_REG_DIFF_MSB ? 11 : 15);
447 
448 	ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
449 	if (ret)
450 		goto out_put_channel;
451 
452 	ret = sx9310_put_read_channel(data, chan->channel);
453 	if (ret)
454 		goto out;
455 
456 	mutex_unlock(&data->mutex);
457 
458 	return IIO_VAL_INT;
459 
460 out_disable_irq:
461 	sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
462 out_put_channel:
463 	sx9310_put_read_channel(data, chan->channel);
464 out:
465 	mutex_unlock(&data->mutex);
466 
467 	return ret;
468 }
469 
470 static int sx9310_read_gain(struct sx9310_data *data,
471 			    const struct iio_chan_spec *chan, int *val)
472 {
473 	unsigned int regval, gain;
474 	int ret;
475 
476 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL3, &regval);
477 	if (ret)
478 		return ret;
479 
480 	switch (chan->channel) {
481 	case 0:
482 	case 3:
483 		gain = FIELD_GET(SX9310_REG_PROX_CTRL3_GAIN0_MASK, regval);
484 		break;
485 	case 1:
486 	case 2:
487 		gain = FIELD_GET(SX9310_REG_PROX_CTRL3_GAIN12_MASK, regval);
488 		break;
489 	default:
490 		return -EINVAL;
491 	}
492 
493 	*val = 1 << gain;
494 
495 	return IIO_VAL_INT;
496 }
497 
498 static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
499 {
500 	unsigned int regval;
501 	int ret;
502 
503 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
504 	if (ret)
505 		return ret;
506 
507 	regval = FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, regval);
508 	*val = sx9310_samp_freq_table[regval].val;
509 	*val2 = sx9310_samp_freq_table[regval].val2;
510 
511 	return IIO_VAL_INT_PLUS_MICRO;
512 }
513 
514 static int sx9310_read_raw(struct iio_dev *indio_dev,
515 			   const struct iio_chan_spec *chan, int *val,
516 			   int *val2, long mask)
517 {
518 	struct sx9310_data *data = iio_priv(indio_dev);
519 	int ret;
520 
521 	if (chan->type != IIO_PROXIMITY)
522 		return -EINVAL;
523 
524 	switch (mask) {
525 	case IIO_CHAN_INFO_RAW:
526 		ret = iio_device_claim_direct_mode(indio_dev);
527 		if (ret)
528 			return ret;
529 
530 		ret = sx9310_read_proximity(data, chan, val);
531 		iio_device_release_direct_mode(indio_dev);
532 		return ret;
533 	case IIO_CHAN_INFO_HARDWAREGAIN:
534 		ret = iio_device_claim_direct_mode(indio_dev);
535 		if (ret)
536 			return ret;
537 
538 		ret = sx9310_read_gain(data, chan, val);
539 		iio_device_release_direct_mode(indio_dev);
540 		return ret;
541 	case IIO_CHAN_INFO_SAMP_FREQ:
542 		return sx9310_read_samp_freq(data, val, val2);
543 	default:
544 		return -EINVAL;
545 	}
546 }
547 
548 static const int sx9310_gain_vals[] = { 1, 2, 4, 8 };
549 
550 static int sx9310_read_avail(struct iio_dev *indio_dev,
551 			     struct iio_chan_spec const *chan,
552 			     const int **vals, int *type, int *length,
553 			     long mask)
554 {
555 	if (chan->type != IIO_PROXIMITY)
556 		return -EINVAL;
557 
558 	switch (mask) {
559 	case IIO_CHAN_INFO_HARDWAREGAIN:
560 		*type = IIO_VAL_INT;
561 		*length = ARRAY_SIZE(sx9310_gain_vals);
562 		*vals = sx9310_gain_vals;
563 		return IIO_AVAIL_LIST;
564 	}
565 
566 	return -EINVAL;
567 }
568 
569 static const unsigned int sx9310_pthresh_codes[] = {
570 	2, 4, 6, 8, 12, 16, 20, 24, 28, 32, 40, 48, 56, 64, 72, 80, 88, 96, 112,
571 	128, 144, 160, 192, 224, 256, 320, 384, 512, 640, 768, 1024, 1536
572 };
573 
574 static int sx9310_get_thresh_reg(unsigned int channel)
575 {
576 	switch (channel) {
577 	case 0:
578 	case 3:
579 		return SX9310_REG_PROX_CTRL8;
580 	case 1:
581 	case 2:
582 		return SX9310_REG_PROX_CTRL9;
583 	}
584 
585 	return -EINVAL;
586 }
587 
588 static int sx9310_read_thresh(struct sx9310_data *data,
589 			      const struct iio_chan_spec *chan, int *val)
590 {
591 	unsigned int reg;
592 	unsigned int regval;
593 	int ret;
594 
595 	reg = ret = sx9310_get_thresh_reg(chan->channel);
596 	if (ret < 0)
597 		return ret;
598 
599 	ret = regmap_read(data->regmap, reg, &regval);
600 	if (ret)
601 		return ret;
602 
603 	regval = FIELD_GET(SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK, regval);
604 	if (regval >= ARRAY_SIZE(sx9310_pthresh_codes))
605 		return -EINVAL;
606 
607 	*val = sx9310_pthresh_codes[regval];
608 	return IIO_VAL_INT;
609 }
610 
611 static int sx9310_read_hysteresis(struct sx9310_data *data,
612 				  const struct iio_chan_spec *chan, int *val)
613 {
614 	unsigned int regval, pthresh;
615 	int ret;
616 
617 	ret = sx9310_read_thresh(data, chan, &pthresh);
618 	if (ret < 0)
619 		return ret;
620 
621 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL10, &regval);
622 	if (ret)
623 		return ret;
624 
625 	regval = FIELD_GET(SX9310_REG_PROX_CTRL10_HYST_MASK, regval);
626 	if (!regval)
627 		regval = 5;
628 
629 	/* regval is at most 5 */
630 	*val = pthresh >> (5 - regval);
631 
632 	return IIO_VAL_INT;
633 }
634 
635 static int sx9310_read_far_debounce(struct sx9310_data *data, int *val)
636 {
637 	unsigned int regval;
638 	int ret;
639 
640 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL10, &regval);
641 	if (ret)
642 		return ret;
643 
644 	regval = FIELD_GET(SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK, regval);
645 	if (regval)
646 		*val = 1 << regval;
647 	else
648 		*val = 0;
649 
650 	return IIO_VAL_INT;
651 }
652 
653 static int sx9310_read_close_debounce(struct sx9310_data *data, int *val)
654 {
655 	unsigned int regval;
656 	int ret;
657 
658 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL10, &regval);
659 	if (ret)
660 		return ret;
661 
662 	regval = FIELD_GET(SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK, regval);
663 	if (regval)
664 		*val = 1 << regval;
665 	else
666 		*val = 0;
667 
668 	return IIO_VAL_INT;
669 }
670 
671 static int sx9310_read_event_val(struct iio_dev *indio_dev,
672 				 const struct iio_chan_spec *chan,
673 				 enum iio_event_type type,
674 				 enum iio_event_direction dir,
675 				 enum iio_event_info info, int *val, int *val2)
676 {
677 	struct sx9310_data *data = iio_priv(indio_dev);
678 
679 	if (chan->type != IIO_PROXIMITY)
680 		return -EINVAL;
681 
682 	switch (info) {
683 	case IIO_EV_INFO_VALUE:
684 		return sx9310_read_thresh(data, chan, val);
685 	case IIO_EV_INFO_PERIOD:
686 		switch (dir) {
687 		case IIO_EV_DIR_RISING:
688 			return sx9310_read_far_debounce(data, val);
689 		case IIO_EV_DIR_FALLING:
690 			return sx9310_read_close_debounce(data, val);
691 		default:
692 			return -EINVAL;
693 		}
694 	case IIO_EV_INFO_HYSTERESIS:
695 		return sx9310_read_hysteresis(data, chan, val);
696 	default:
697 		return -EINVAL;
698 	}
699 }
700 
701 static int sx9310_write_thresh(struct sx9310_data *data,
702 			       const struct iio_chan_spec *chan, int val)
703 {
704 	unsigned int reg;
705 	unsigned int regval;
706 	int ret, i;
707 
708 	reg = ret = sx9310_get_thresh_reg(chan->channel);
709 	if (ret < 0)
710 		return ret;
711 
712 	for (i = 0; i < ARRAY_SIZE(sx9310_pthresh_codes); i++) {
713 		if (sx9310_pthresh_codes[i] == val) {
714 			regval = i;
715 			break;
716 		}
717 	}
718 
719 	if (i == ARRAY_SIZE(sx9310_pthresh_codes))
720 		return -EINVAL;
721 
722 	regval = FIELD_PREP(SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK, regval);
723 	mutex_lock(&data->mutex);
724 	ret = regmap_update_bits(data->regmap, reg,
725 				 SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK, regval);
726 	mutex_unlock(&data->mutex);
727 
728 	return ret;
729 }
730 
731 static int sx9310_write_hysteresis(struct sx9310_data *data,
732 				   const struct iio_chan_spec *chan, int _val)
733 {
734 	unsigned int hyst, val = _val;
735 	int ret, pthresh;
736 
737 	ret = sx9310_read_thresh(data, chan, &pthresh);
738 	if (ret < 0)
739 		return ret;
740 
741 	if (val == 0)
742 		hyst = 0;
743 	else if (val == pthresh >> 2)
744 		hyst = 3;
745 	else if (val == pthresh >> 3)
746 		hyst = 2;
747 	else if (val == pthresh >> 4)
748 		hyst = 1;
749 	else
750 		return -EINVAL;
751 
752 	hyst = FIELD_PREP(SX9310_REG_PROX_CTRL10_HYST_MASK, hyst);
753 	mutex_lock(&data->mutex);
754 	ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10,
755 				 SX9310_REG_PROX_CTRL10_HYST_MASK, hyst);
756 	mutex_unlock(&data->mutex);
757 
758 	return ret;
759 }
760 
761 static int sx9310_write_far_debounce(struct sx9310_data *data, int val)
762 {
763 	int ret;
764 	unsigned int regval;
765 
766 	val = ilog2(val);
767 	regval = FIELD_PREP(SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK, val);
768 
769 	mutex_lock(&data->mutex);
770 	ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10,
771 				 SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK,
772 				 regval);
773 	mutex_unlock(&data->mutex);
774 
775 	return ret;
776 }
777 
778 static int sx9310_write_close_debounce(struct sx9310_data *data, int val)
779 {
780 	int ret;
781 	unsigned int regval;
782 
783 	val = ilog2(val);
784 	regval = FIELD_PREP(SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK, val);
785 
786 	mutex_lock(&data->mutex);
787 	ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL10,
788 				 SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK,
789 				 regval);
790 	mutex_unlock(&data->mutex);
791 
792 	return ret;
793 }
794 
795 static int sx9310_write_event_val(struct iio_dev *indio_dev,
796 				  const struct iio_chan_spec *chan,
797 				  enum iio_event_type type,
798 				  enum iio_event_direction dir,
799 				  enum iio_event_info info, int val, int val2)
800 {
801 	struct sx9310_data *data = iio_priv(indio_dev);
802 
803 	if (chan->type != IIO_PROXIMITY)
804 		return -EINVAL;
805 
806 	switch (info) {
807 	case IIO_EV_INFO_VALUE:
808 		return sx9310_write_thresh(data, chan, val);
809 	case IIO_EV_INFO_PERIOD:
810 		switch (dir) {
811 		case IIO_EV_DIR_RISING:
812 			return sx9310_write_far_debounce(data, val);
813 		case IIO_EV_DIR_FALLING:
814 			return sx9310_write_close_debounce(data, val);
815 		default:
816 			return -EINVAL;
817 		}
818 	case IIO_EV_INFO_HYSTERESIS:
819 		return sx9310_write_hysteresis(data, chan, val);
820 	default:
821 		return -EINVAL;
822 	}
823 }
824 
825 static int sx9310_set_samp_freq(struct sx9310_data *data, int val, int val2)
826 {
827 	int i, ret;
828 
829 	for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++)
830 		if (val == sx9310_samp_freq_table[i].val &&
831 		    val2 == sx9310_samp_freq_table[i].val2)
832 			break;
833 
834 	if (i == ARRAY_SIZE(sx9310_samp_freq_table))
835 		return -EINVAL;
836 
837 	mutex_lock(&data->mutex);
838 
839 	ret = regmap_update_bits(
840 		data->regmap, SX9310_REG_PROX_CTRL0,
841 		SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK,
842 		FIELD_PREP(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, i));
843 
844 	mutex_unlock(&data->mutex);
845 
846 	return ret;
847 }
848 
849 static int sx9310_write_gain(struct sx9310_data *data,
850 			    const struct iio_chan_spec *chan, int val)
851 {
852 	unsigned int gain, mask;
853 	int ret;
854 
855 	gain = ilog2(val);
856 
857 	switch (chan->channel) {
858 	case 0:
859 	case 3:
860 		mask = SX9310_REG_PROX_CTRL3_GAIN0_MASK;
861 		gain = FIELD_PREP(SX9310_REG_PROX_CTRL3_GAIN0_MASK, gain);
862 		break;
863 	case 1:
864 	case 2:
865 		mask = SX9310_REG_PROX_CTRL3_GAIN12_MASK;
866 		gain = FIELD_PREP(SX9310_REG_PROX_CTRL3_GAIN12_MASK, gain);
867 		break;
868 	default:
869 		return -EINVAL;
870 	}
871 
872 	mutex_lock(&data->mutex);
873 	ret = regmap_update_bits(data->regmap, SX9310_REG_PROX_CTRL3, mask,
874 				 gain);
875 	mutex_unlock(&data->mutex);
876 
877 	return ret;
878 }
879 
880 static int sx9310_write_raw(struct iio_dev *indio_dev,
881 			    const struct iio_chan_spec *chan, int val, int val2,
882 			    long mask)
883 {
884 	struct sx9310_data *data = iio_priv(indio_dev);
885 
886 	if (chan->type != IIO_PROXIMITY)
887 		return -EINVAL;
888 
889 	switch (mask) {
890 	case IIO_CHAN_INFO_SAMP_FREQ:
891 		return sx9310_set_samp_freq(data, val, val2);
892 	case IIO_CHAN_INFO_HARDWAREGAIN:
893 		return sx9310_write_gain(data, chan, val);
894 	}
895 
896 	return -EINVAL;
897 }
898 
899 static irqreturn_t sx9310_irq_handler(int irq, void *private)
900 {
901 	struct iio_dev *indio_dev = private;
902 	struct sx9310_data *data = iio_priv(indio_dev);
903 
904 	if (data->trigger_enabled)
905 		iio_trigger_poll(data->trig);
906 
907 	/*
908 	 * Even if no event is enabled, we need to wake the thread to clear the
909 	 * interrupt state by reading SX9310_REG_IRQ_SRC.
910 	 * It is not possible to do that here because regmap_read takes a mutex.
911 	 */
912 	return IRQ_WAKE_THREAD;
913 }
914 
915 static void sx9310_push_events(struct iio_dev *indio_dev)
916 {
917 	int ret;
918 	unsigned int val, chan;
919 	struct sx9310_data *data = iio_priv(indio_dev);
920 	s64 timestamp = iio_get_time_ns(indio_dev);
921 	unsigned long prox_changed;
922 
923 	/* Read proximity state on all channels */
924 	ret = regmap_read(data->regmap, SX9310_REG_STAT0, &val);
925 	if (ret) {
926 		dev_err(&data->client->dev, "i2c transfer error in irq\n");
927 		return;
928 	}
929 
930 	/*
931 	 * Only iterate over channels with changes on proximity status that have
932 	 * events enabled.
933 	 */
934 	prox_changed = (data->chan_prox_stat ^ val) & data->chan_event;
935 
936 	for_each_set_bit(chan, &prox_changed, SX9310_NUM_CHANNELS) {
937 		int dir;
938 		u64 ev;
939 
940 		dir = (val & BIT(chan)) ? IIO_EV_DIR_FALLING : IIO_EV_DIR_RISING;
941 		ev = IIO_UNMOD_EVENT_CODE(IIO_PROXIMITY, chan,
942 					  IIO_EV_TYPE_THRESH, dir);
943 
944 		iio_push_event(indio_dev, ev, timestamp);
945 	}
946 	data->chan_prox_stat = val;
947 }
948 
949 static irqreturn_t sx9310_irq_thread_handler(int irq, void *private)
950 {
951 	struct iio_dev *indio_dev = private;
952 	struct sx9310_data *data = iio_priv(indio_dev);
953 	int ret;
954 	unsigned int val;
955 
956 	mutex_lock(&data->mutex);
957 
958 	ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
959 	if (ret) {
960 		dev_err(&data->client->dev, "i2c transfer error in irq\n");
961 		goto out;
962 	}
963 
964 	if (val & (SX9310_FAR_IRQ | SX9310_CLOSE_IRQ))
965 		sx9310_push_events(indio_dev);
966 
967 	if (val & SX9310_CONVDONE_IRQ)
968 		complete(&data->completion);
969 
970 out:
971 	mutex_unlock(&data->mutex);
972 
973 	return IRQ_HANDLED;
974 }
975 
976 static int sx9310_read_event_config(struct iio_dev *indio_dev,
977 				    const struct iio_chan_spec *chan,
978 				    enum iio_event_type type,
979 				    enum iio_event_direction dir)
980 {
981 	struct sx9310_data *data = iio_priv(indio_dev);
982 
983 	return !!(data->chan_event & BIT(chan->channel));
984 }
985 
986 static int sx9310_write_event_config(struct iio_dev *indio_dev,
987 				     const struct iio_chan_spec *chan,
988 				     enum iio_event_type type,
989 				     enum iio_event_direction dir, int state)
990 {
991 	struct sx9310_data *data = iio_priv(indio_dev);
992 	unsigned int eventirq = SX9310_FAR_IRQ | SX9310_CLOSE_IRQ;
993 	int ret;
994 
995 	/* If the state hasn't changed, there's nothing to do. */
996 	if (!!(data->chan_event & BIT(chan->channel)) == state)
997 		return 0;
998 
999 	mutex_lock(&data->mutex);
1000 	if (state) {
1001 		ret = sx9310_get_event_channel(data, chan->channel);
1002 		if (ret)
1003 			goto out_unlock;
1004 		if (!(data->chan_event & ~BIT(chan->channel))) {
1005 			ret = sx9310_enable_irq(data, eventirq);
1006 			if (ret)
1007 				sx9310_put_event_channel(data, chan->channel);
1008 		}
1009 	} else {
1010 		ret = sx9310_put_event_channel(data, chan->channel);
1011 		if (ret)
1012 			goto out_unlock;
1013 		if (!data->chan_event) {
1014 			ret = sx9310_disable_irq(data, eventirq);
1015 			if (ret)
1016 				sx9310_get_event_channel(data, chan->channel);
1017 		}
1018 	}
1019 
1020 out_unlock:
1021 	mutex_unlock(&data->mutex);
1022 	return ret;
1023 }
1024 
1025 static struct attribute *sx9310_attributes[] = {
1026 	&iio_dev_attr_sampling_frequency_available.dev_attr.attr,
1027 	NULL
1028 };
1029 
1030 static const struct attribute_group sx9310_attribute_group = {
1031 	.attrs = sx9310_attributes,
1032 };
1033 
1034 static const struct iio_info sx9310_info = {
1035 	.attrs = &sx9310_attribute_group,
1036 	.read_raw = sx9310_read_raw,
1037 	.read_avail = sx9310_read_avail,
1038 	.read_event_value = sx9310_read_event_val,
1039 	.write_event_value = sx9310_write_event_val,
1040 	.write_raw = sx9310_write_raw,
1041 	.read_event_config = sx9310_read_event_config,
1042 	.write_event_config = sx9310_write_event_config,
1043 };
1044 
1045 static int sx9310_set_trigger_state(struct iio_trigger *trig, bool state)
1046 {
1047 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1048 	struct sx9310_data *data = iio_priv(indio_dev);
1049 	int ret = 0;
1050 
1051 	mutex_lock(&data->mutex);
1052 
1053 	if (state)
1054 		ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
1055 	else if (!data->chan_read)
1056 		ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
1057 	if (ret)
1058 		goto out;
1059 
1060 	data->trigger_enabled = state;
1061 
1062 out:
1063 	mutex_unlock(&data->mutex);
1064 
1065 	return ret;
1066 }
1067 
1068 static const struct iio_trigger_ops sx9310_trigger_ops = {
1069 	.set_trigger_state = sx9310_set_trigger_state,
1070 };
1071 
1072 static irqreturn_t sx9310_trigger_handler(int irq, void *private)
1073 {
1074 	struct iio_poll_func *pf = private;
1075 	struct iio_dev *indio_dev = pf->indio_dev;
1076 	struct sx9310_data *data = iio_priv(indio_dev);
1077 	__be16 val;
1078 	int bit, ret, i = 0;
1079 
1080 	mutex_lock(&data->mutex);
1081 
1082 	for_each_set_bit(bit, indio_dev->active_scan_mask,
1083 			 indio_dev->masklength) {
1084 		ret = sx9310_read_prox_data(data, &indio_dev->channels[bit],
1085 					    &val);
1086 		if (ret)
1087 			goto out;
1088 
1089 		data->buffer.channels[i++] = val;
1090 	}
1091 
1092 	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
1093 					   pf->timestamp);
1094 
1095 out:
1096 	mutex_unlock(&data->mutex);
1097 
1098 	iio_trigger_notify_done(indio_dev->trig);
1099 
1100 	return IRQ_HANDLED;
1101 }
1102 
1103 static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
1104 {
1105 	struct sx9310_data *data = iio_priv(indio_dev);
1106 	unsigned long channels = 0;
1107 	int bit, ret;
1108 
1109 	mutex_lock(&data->mutex);
1110 	for_each_set_bit(bit, indio_dev->active_scan_mask,
1111 			 indio_dev->masklength)
1112 		__set_bit(indio_dev->channels[bit].channel, &channels);
1113 
1114 	ret = sx9310_update_chan_en(data, channels, data->chan_event);
1115 	mutex_unlock(&data->mutex);
1116 	return ret;
1117 }
1118 
1119 static int sx9310_buffer_postdisable(struct iio_dev *indio_dev)
1120 {
1121 	struct sx9310_data *data = iio_priv(indio_dev);
1122 	int ret;
1123 
1124 	mutex_lock(&data->mutex);
1125 	ret = sx9310_update_chan_en(data, 0, data->chan_event);
1126 	mutex_unlock(&data->mutex);
1127 	return ret;
1128 }
1129 
1130 static const struct iio_buffer_setup_ops sx9310_buffer_setup_ops = {
1131 	.preenable = sx9310_buffer_preenable,
1132 	.postdisable = sx9310_buffer_postdisable,
1133 };
1134 
1135 struct sx9310_reg_default {
1136 	u8 reg;
1137 	u8 def;
1138 };
1139 
1140 static const struct sx9310_reg_default sx9310_default_regs[] = {
1141 	{ SX9310_REG_IRQ_MSK, 0x00 },
1142 	{ SX9310_REG_IRQ_FUNC, 0x00 },
1143 	/*
1144 	 * The lower 4 bits should not be set as it enable sensors measurements.
1145 	 * Turning the detection on before the configuration values are set to
1146 	 * good values can cause the device to return erroneous readings.
1147 	 */
1148 	{ SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS },
1149 	{ SX9310_REG_PROX_CTRL1, 0x00 },
1150 	{ SX9310_REG_PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 |
1151 				 SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC },
1152 	{ SX9310_REG_PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
1153 				 SX9310_REG_PROX_CTRL3_GAIN12_X4 },
1154 	{ SX9310_REG_PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST },
1155 	{ SX9310_REG_PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
1156 				 SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
1157 				 SX9310_REG_PROX_CTRL5_RAWFILT_1P25 },
1158 	{ SX9310_REG_PROX_CTRL6, SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT },
1159 	{ SX9310_REG_PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
1160 				 SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 },
1161 	{ SX9310_REG_PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
1162 				 SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 },
1163 	{ SX9310_REG_PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH_28 |
1164 				 SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 },
1165 	{ SX9310_REG_PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
1166 				  SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 },
1167 	{ SX9310_REG_PROX_CTRL11, 0x00 },
1168 	{ SX9310_REG_PROX_CTRL12, 0x00 },
1169 	{ SX9310_REG_PROX_CTRL13, 0x00 },
1170 	{ SX9310_REG_PROX_CTRL14, 0x00 },
1171 	{ SX9310_REG_PROX_CTRL15, 0x00 },
1172 	{ SX9310_REG_PROX_CTRL16, 0x00 },
1173 	{ SX9310_REG_PROX_CTRL17, 0x00 },
1174 	{ SX9310_REG_PROX_CTRL18, 0x00 },
1175 	{ SX9310_REG_PROX_CTRL19, 0x00 },
1176 	{ SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
1177 				SX9310_REG_SAR_CTRL0_SARHYST_8 },
1178 	{ SX9310_REG_SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250) },
1179 	{ SX9310_REG_SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT },
1180 };
1181 
1182 /* Activate all channels and perform an initial compensation. */
1183 static int sx9310_init_compensation(struct iio_dev *indio_dev)
1184 {
1185 	struct sx9310_data *data = iio_priv(indio_dev);
1186 	int ret;
1187 	unsigned int val;
1188 	unsigned int ctrl0;
1189 
1190 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0);
1191 	if (ret)
1192 		return ret;
1193 
1194 	/* run the compensation phase on all channels */
1195 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
1196 			   ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK);
1197 	if (ret)
1198 		return ret;
1199 
1200 	ret = regmap_read_poll_timeout(data->regmap, SX9310_REG_STAT1, val,
1201 				       !(val & SX9310_REG_STAT1_COMPSTAT_MASK),
1202 				       20000, 2000000);
1203 	if (ret) {
1204 		if (ret == -ETIMEDOUT)
1205 			dev_err(&data->client->dev,
1206 				"initial compensation timed out: 0x%02x\n",
1207 				val);
1208 		return ret;
1209 	}
1210 
1211 	regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
1212 	return ret;
1213 }
1214 
1215 static const struct sx9310_reg_default *
1216 sx9310_get_default_reg(struct sx9310_data *data, int i,
1217 		       struct sx9310_reg_default *reg_def)
1218 {
1219 	int ret;
1220 	const struct device_node *np = data->client->dev.of_node;
1221 	u32 combined[SX9310_NUM_CHANNELS] = { 4, 4, 4, 4 };
1222 	unsigned long comb_mask = 0;
1223 	const char *res;
1224 	u32 start = 0, raw = 0, pos = 0;
1225 
1226 	memcpy(reg_def, &sx9310_default_regs[i], sizeof(*reg_def));
1227 	if (!np)
1228 		return reg_def;
1229 
1230 	switch (reg_def->reg) {
1231 	case SX9310_REG_PROX_CTRL2:
1232 		if (of_property_read_bool(np, "semtech,cs0-ground")) {
1233 			reg_def->def &= ~SX9310_REG_PROX_CTRL2_SHIELDEN_MASK;
1234 			reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
1235 		}
1236 
1237 		reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
1238 		of_property_read_u32_array(np, "semtech,combined-sensors",
1239 					   combined, ARRAY_SIZE(combined));
1240 		for (i = 0; i < ARRAY_SIZE(combined); i++) {
1241 			if (combined[i] <= SX9310_NUM_CHANNELS)
1242 				comb_mask |= BIT(combined[i]);
1243 		}
1244 
1245 		comb_mask &= 0xf;
1246 		if (comb_mask == (BIT(3) | BIT(2) | BIT(1) | BIT(0)))
1247 			reg_def->def |= SX9310_REG_PROX_CTRL2_COMBMODE_CS0_CS1_CS2_CS3;
1248 		else if (comb_mask == (BIT(1) | BIT(2)))
1249 			reg_def->def |= SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2;
1250 		else if (comb_mask == (BIT(0) | BIT(1)))
1251 			reg_def->def |= SX9310_REG_PROX_CTRL2_COMBMODE_CS0_CS1;
1252 		else if (comb_mask == BIT(3))
1253 			reg_def->def |= SX9310_REG_PROX_CTRL2_COMBMODE_CS3;
1254 
1255 		break;
1256 	case SX9310_REG_PROX_CTRL4:
1257 		ret = of_property_read_string(np, "semtech,resolution", &res);
1258 		if (ret)
1259 			break;
1260 
1261 		reg_def->def &= ~SX9310_REG_PROX_CTRL4_RESOLUTION_MASK;
1262 		if (!strcmp(res, "coarsest"))
1263 			reg_def->def |= SX9310_REG_PROX_CTRL4_RESOLUTION_COARSEST;
1264 		else if (!strcmp(res, "very-coarse"))
1265 			reg_def->def |= SX9310_REG_PROX_CTRL4_RESOLUTION_VERY_COARSE;
1266 		else if (!strcmp(res, "coarse"))
1267 			reg_def->def |= SX9310_REG_PROX_CTRL4_RESOLUTION_COARSE;
1268 		else if (!strcmp(res, "medium-coarse"))
1269 			reg_def->def |= SX9310_REG_PROX_CTRL4_RESOLUTION_MEDIUM_COARSE;
1270 		else if (!strcmp(res, "medium"))
1271 			reg_def->def |= SX9310_REG_PROX_CTRL4_RESOLUTION_MEDIUM;
1272 		else if (!strcmp(res, "fine"))
1273 			reg_def->def |= SX9310_REG_PROX_CTRL4_RESOLUTION_FINE;
1274 		else if (!strcmp(res, "very-fine"))
1275 			reg_def->def |= SX9310_REG_PROX_CTRL4_RESOLUTION_VERY_FINE;
1276 		else if (!strcmp(res, "finest"))
1277 			reg_def->def |= SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST;
1278 
1279 		break;
1280 	case SX9310_REG_PROX_CTRL5:
1281 		ret = of_property_read_u32(np, "semtech,startup-sensor", &start);
1282 		if (ret) {
1283 			start = FIELD_GET(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
1284 					  reg_def->def);
1285 		}
1286 
1287 		reg_def->def &= ~SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK;
1288 		reg_def->def |= FIELD_PREP(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK,
1289 					   start);
1290 
1291 		ret = of_property_read_u32(np, "semtech,proxraw-strength", &raw);
1292 		if (ret) {
1293 			raw = FIELD_GET(SX9310_REG_PROX_CTRL5_RAWFILT_MASK,
1294 					reg_def->def);
1295 		} else {
1296 			raw = ilog2(raw);
1297 		}
1298 
1299 		reg_def->def &= ~SX9310_REG_PROX_CTRL5_RAWFILT_MASK;
1300 		reg_def->def |= FIELD_PREP(SX9310_REG_PROX_CTRL5_RAWFILT_MASK,
1301 					   raw);
1302 		break;
1303 	case SX9310_REG_PROX_CTRL7:
1304 		ret = of_property_read_u32(np, "semtech,avg-pos-strength", &pos);
1305 		if (ret)
1306 			break;
1307 
1308 		/* Powers of 2, except for a gap between 16 and 64 */
1309 		pos = clamp(ilog2(pos), 3, 11) - (pos >= 32 ? 4 : 3);
1310 		reg_def->def &= ~SX9310_REG_PROX_CTRL7_AVGPOSFILT_MASK;
1311 		reg_def->def |= FIELD_PREP(SX9310_REG_PROX_CTRL7_AVGPOSFILT_MASK,
1312 					   pos);
1313 		break;
1314 	}
1315 
1316 	return reg_def;
1317 }
1318 
1319 static int sx9310_init_device(struct iio_dev *indio_dev)
1320 {
1321 	struct sx9310_data *data = iio_priv(indio_dev);
1322 	struct sx9310_reg_default tmp;
1323 	const struct sx9310_reg_default *initval;
1324 	int ret;
1325 	unsigned int i, val;
1326 
1327 	ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET);
1328 	if (ret)
1329 		return ret;
1330 
1331 	usleep_range(1000, 2000); /* power-up time is ~1ms. */
1332 
1333 	/* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
1334 	ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
1335 	if (ret)
1336 		return ret;
1337 
1338 	/* Program some sane defaults. */
1339 	for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
1340 		initval = sx9310_get_default_reg(data, i, &tmp);
1341 		ret = regmap_write(data->regmap, initval->reg, initval->def);
1342 		if (ret)
1343 			return ret;
1344 	}
1345 
1346 	return sx9310_init_compensation(indio_dev);
1347 }
1348 
1349 static int sx9310_set_indio_dev_name(struct device *dev,
1350 				     struct iio_dev *indio_dev,
1351 				     unsigned int whoami)
1352 {
1353 	unsigned int long ddata;
1354 
1355 	ddata = (uintptr_t)device_get_match_data(dev);
1356 	if (ddata != whoami) {
1357 		dev_err(dev, "WHOAMI does not match device data: %u\n", whoami);
1358 		return -ENODEV;
1359 	}
1360 
1361 	switch (whoami) {
1362 	case SX9310_WHOAMI_VALUE:
1363 		indio_dev->name = "sx9310";
1364 		break;
1365 	case SX9311_WHOAMI_VALUE:
1366 		indio_dev->name = "sx9311";
1367 		break;
1368 	default:
1369 		dev_err(dev, "unexpected WHOAMI response: %u\n", whoami);
1370 		return -ENODEV;
1371 	}
1372 
1373 	return 0;
1374 }
1375 
1376 static void sx9310_regulator_disable(void *_data)
1377 {
1378 	struct sx9310_data *data = _data;
1379 
1380 	regulator_bulk_disable(ARRAY_SIZE(data->supplies), data->supplies);
1381 }
1382 
1383 static int sx9310_probe(struct i2c_client *client)
1384 {
1385 	int ret;
1386 	struct device *dev = &client->dev;
1387 	struct iio_dev *indio_dev;
1388 	struct sx9310_data *data;
1389 
1390 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
1391 	if (!indio_dev)
1392 		return -ENOMEM;
1393 
1394 	data = iio_priv(indio_dev);
1395 	data->client = client;
1396 	data->supplies[0].supply = "vdd";
1397 	data->supplies[1].supply = "svdd";
1398 	mutex_init(&data->mutex);
1399 	init_completion(&data->completion);
1400 
1401 	data->regmap = devm_regmap_init_i2c(client, &sx9310_regmap_config);
1402 	if (IS_ERR(data->regmap))
1403 		return PTR_ERR(data->regmap);
1404 
1405 	ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(data->supplies),
1406 				      data->supplies);
1407 	if (ret)
1408 		return ret;
1409 
1410 	ret = regulator_bulk_enable(ARRAY_SIZE(data->supplies), data->supplies);
1411 	if (ret)
1412 		return ret;
1413 	/* Must wait for Tpor time after initial power up */
1414 	usleep_range(1000, 1100);
1415 
1416 	ret = devm_add_action_or_reset(dev, sx9310_regulator_disable, data);
1417 	if (ret)
1418 		return ret;
1419 
1420 	ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
1421 	if (ret) {
1422 		dev_err(dev, "error in reading WHOAMI register: %d", ret);
1423 		return ret;
1424 	}
1425 
1426 	ret = sx9310_set_indio_dev_name(dev, indio_dev, data->whoami);
1427 	if (ret)
1428 		return ret;
1429 
1430 	ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(dev));
1431 	indio_dev->channels = sx9310_channels;
1432 	indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
1433 	indio_dev->info = &sx9310_info;
1434 	indio_dev->modes = INDIO_DIRECT_MODE;
1435 	i2c_set_clientdata(client, indio_dev);
1436 
1437 	ret = sx9310_init_device(indio_dev);
1438 	if (ret)
1439 		return ret;
1440 
1441 	if (client->irq) {
1442 		ret = devm_request_threaded_irq(dev, client->irq,
1443 						sx9310_irq_handler,
1444 						sx9310_irq_thread_handler,
1445 						IRQF_ONESHOT,
1446 						"sx9310_event", indio_dev);
1447 		if (ret)
1448 			return ret;
1449 
1450 		data->trig = devm_iio_trigger_alloc(dev, "%s-dev%d",
1451 						    indio_dev->name,
1452 						    indio_dev->id);
1453 		if (!data->trig)
1454 			return -ENOMEM;
1455 
1456 		data->trig->dev.parent = dev;
1457 		data->trig->ops = &sx9310_trigger_ops;
1458 		iio_trigger_set_drvdata(data->trig, indio_dev);
1459 
1460 		ret = devm_iio_trigger_register(dev, data->trig);
1461 		if (ret)
1462 			return ret;
1463 	}
1464 
1465 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev,
1466 					      iio_pollfunc_store_time,
1467 					      sx9310_trigger_handler,
1468 					      &sx9310_buffer_setup_ops);
1469 	if (ret)
1470 		return ret;
1471 
1472 	return devm_iio_device_register(dev, indio_dev);
1473 }
1474 
1475 static int __maybe_unused sx9310_suspend(struct device *dev)
1476 {
1477 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1478 	struct sx9310_data *data = iio_priv(indio_dev);
1479 	u8 ctrl0;
1480 	int ret;
1481 
1482 	disable_irq_nosync(data->client->irq);
1483 
1484 	mutex_lock(&data->mutex);
1485 	ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
1486 			  &data->suspend_ctrl0);
1487 	if (ret)
1488 		goto out;
1489 
1490 	ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK;
1491 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
1492 	if (ret)
1493 		goto out;
1494 
1495 	ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 0);
1496 
1497 out:
1498 	mutex_unlock(&data->mutex);
1499 	return ret;
1500 }
1501 
1502 static int __maybe_unused sx9310_resume(struct device *dev)
1503 {
1504 	struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1505 	struct sx9310_data *data = iio_priv(indio_dev);
1506 	int ret;
1507 
1508 	mutex_lock(&data->mutex);
1509 	ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 1);
1510 	if (ret)
1511 		goto out;
1512 
1513 	ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
1514 			   data->suspend_ctrl0);
1515 
1516 out:
1517 	mutex_unlock(&data->mutex);
1518 	if (ret)
1519 		return ret;
1520 
1521 	enable_irq(data->client->irq);
1522 	return 0;
1523 }
1524 
1525 static const struct dev_pm_ops sx9310_pm_ops = {
1526 	SET_SYSTEM_SLEEP_PM_OPS(sx9310_suspend, sx9310_resume)
1527 };
1528 
1529 static const struct acpi_device_id sx9310_acpi_match[] = {
1530 	{ "STH9310", SX9310_WHOAMI_VALUE },
1531 	{ "STH9311", SX9311_WHOAMI_VALUE },
1532 	{}
1533 };
1534 MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);
1535 
1536 static const struct of_device_id sx9310_of_match[] = {
1537 	{ .compatible = "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE },
1538 	{ .compatible = "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE },
1539 	{}
1540 };
1541 MODULE_DEVICE_TABLE(of, sx9310_of_match);
1542 
1543 static const struct i2c_device_id sx9310_id[] = {
1544 	{ "sx9310", SX9310_WHOAMI_VALUE },
1545 	{ "sx9311", SX9311_WHOAMI_VALUE },
1546 	{}
1547 };
1548 MODULE_DEVICE_TABLE(i2c, sx9310_id);
1549 
1550 static struct i2c_driver sx9310_driver = {
1551 	.driver = {
1552 		.name	= "sx9310",
1553 		.acpi_match_table = sx9310_acpi_match,
1554 		.of_match_table = sx9310_of_match,
1555 		.pm = &sx9310_pm_ops,
1556 
1557 		/*
1558 		 * Lots of i2c transfers in probe + over 200 ms waiting in
1559 		 * sx9310_init_compensation() mean a slow probe; prefer async
1560 		 * so we don't delay boot if we're builtin to the kernel.
1561 		 */
1562 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
1563 	},
1564 	.probe_new	= sx9310_probe,
1565 	.id_table	= sx9310_id,
1566 };
1567 module_i2c_driver(sx9310_driver);
1568 
1569 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
1570 MODULE_AUTHOR("Daniel Campello <campello@chromium.org>");
1571 MODULE_DESCRIPTION("Driver for Semtech SX9310/SX9311 proximity sensor");
1572 MODULE_LICENSE("GPL v2");
1573