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