blob: 4ad8194c70a906c3f821b884ed8a71ae0f3a87b2 [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{
Daniel Campello364e8532020-08-03 17:58:03 -0600326 if (!data->client->irq)
327 return 0;
Daniel Campello72ad02b2020-03-10 14:06:59 -0600328 return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, irq);
329}
330
331static int sx9310_disable_irq(struct sx9310_data *data, unsigned int irq)
332{
Daniel Campello364e8532020-08-03 17:58:03 -0600333 if (!data->client->irq)
334 return 0;
Daniel Campello72ad02b2020-03-10 14:06:59 -0600335 return regmap_update_bits(data->regmap, SX9310_REG_IRQ_MSK, irq, 0);
336}
337
338static int sx9310_read_prox_data(struct sx9310_data *data,
339 const struct iio_chan_spec *chan, __be16 *val)
340{
341 int ret;
342
343 ret = regmap_write(data->regmap, SX9310_REG_SENSOR_SEL, chan->channel);
344 if (ret < 0)
345 return ret;
346
347 return regmap_bulk_read(data->regmap, chan->address, val, 2);
348}
349
350/*
351 * If we have no interrupt support, we have to wait for a scan period
352 * after enabling a channel to get a result.
353 */
354static int sx9310_wait_for_sample(struct sx9310_data *data)
355{
356 int ret;
357 unsigned int val;
358
359 ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &val);
360 if (ret < 0)
361 return ret;
362
Daniel Campellod9f753f2020-08-03 17:58:02 -0600363 val = FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, val);
Daniel Campello72ad02b2020-03-10 14:06:59 -0600364
365 msleep(sx9310_scan_period_table[val]);
366
367 return 0;
368}
369
370static int sx9310_read_proximity(struct sx9310_data *data,
371 const struct iio_chan_spec *chan, int *val)
372{
373 int ret = 0;
374 __be16 rawval;
375
376 mutex_lock(&data->mutex);
377
378 ret = sx9310_get_read_channel(data, chan->channel);
379 if (ret < 0)
380 goto out;
381
382 ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
383 if (ret < 0)
384 goto out_put_channel;
385
386 mutex_unlock(&data->mutex);
387
Daniel Campello364e8532020-08-03 17:58:03 -0600388 if (data->client->irq) {
Daniel Campello72ad02b2020-03-10 14:06:59 -0600389 ret = wait_for_completion_interruptible(&data->completion);
390 reinit_completion(&data->completion);
391 } else {
392 ret = sx9310_wait_for_sample(data);
393 }
394
395 mutex_lock(&data->mutex);
396
397 if (ret < 0)
398 goto out_disable_irq;
399
400 ret = sx9310_read_prox_data(data, chan, &rawval);
401 if (ret < 0)
402 goto out_disable_irq;
403
404 *val = sign_extend32(be16_to_cpu(rawval),
405 (chan->address == SX9310_REG_DIFF_MSB ? 11 : 15));
406
407 ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
408 if (ret < 0)
409 goto out_put_channel;
410
411 ret = sx9310_put_read_channel(data, chan->channel);
412 if (ret < 0)
413 goto out;
414
415 mutex_unlock(&data->mutex);
416
417 return IIO_VAL_INT;
418
419out_disable_irq:
420 sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
421out_put_channel:
422 sx9310_put_read_channel(data, chan->channel);
423out:
424 mutex_unlock(&data->mutex);
425
426 return ret;
427}
428
429static int sx9310_read_samp_freq(struct sx9310_data *data, int *val, int *val2)
430{
431 unsigned int regval;
432 int ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &regval);
433
434 if (ret < 0)
435 return ret;
436
Daniel Campellod9f753f2020-08-03 17:58:02 -0600437 regval = FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, regval);
Daniel Campello72ad02b2020-03-10 14:06:59 -0600438 *val = sx9310_samp_freq_table[regval].val;
439 *val2 = sx9310_samp_freq_table[regval].val2;
440
441 return IIO_VAL_INT_PLUS_MICRO;
442}
443
444static int sx9310_read_raw(struct iio_dev *indio_dev,
445 const struct iio_chan_spec *chan, int *val,
446 int *val2, long mask)
447{
448 struct sx9310_data *data = iio_priv(indio_dev);
449 int ret;
450
451 if (chan->type != IIO_PROXIMITY)
452 return -EINVAL;
453
454 switch (mask) {
455 case IIO_CHAN_INFO_RAW:
456 ret = iio_device_claim_direct_mode(indio_dev);
457 if (ret)
458 return ret;
459
460 ret = sx9310_read_proximity(data, chan, val);
461 iio_device_release_direct_mode(indio_dev);
462 return ret;
463 case IIO_CHAN_INFO_SAMP_FREQ:
464 return sx9310_read_samp_freq(data, val, val2);
465 default:
466 return -EINVAL;
467 }
468}
469
470static int sx9310_set_samp_freq(struct sx9310_data *data, int val, int val2)
471{
472 int i, ret;
473
474 for (i = 0; i < ARRAY_SIZE(sx9310_samp_freq_table); i++)
475 if (val == sx9310_samp_freq_table[i].val &&
476 val2 == sx9310_samp_freq_table[i].val2)
477 break;
478
479 if (i == ARRAY_SIZE(sx9310_samp_freq_table))
480 return -EINVAL;
481
482 mutex_lock(&data->mutex);
483
Daniel Campellod9f753f2020-08-03 17:58:02 -0600484 ret = regmap_update_bits(
485 data->regmap, SX9310_REG_PROX_CTRL0,
486 SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK,
487 FIELD_PREP(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK, i));
Daniel Campello72ad02b2020-03-10 14:06:59 -0600488
489 mutex_unlock(&data->mutex);
490
491 return ret;
492}
493
494static 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
509static 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
526static 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
560static 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
Daniel Campellod9f753f2020-08-03 17:58:02 -0600575 if (val & (SX9310_FAR_IRQ | SX9310_CLOSE_IRQ))
Daniel Campello72ad02b2020-03-10 14:06:59 -0600576 sx9310_push_events(indio_dev);
577
578 if (val & SX9310_CONVDONE_IRQ)
579 complete(&data->completion);
580
581out:
582 mutex_unlock(&data->mutex);
583
584 return IRQ_HANDLED;
585}
586
587static 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
597static 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);
Daniel Campellod9f753f2020-08-03 17:58:02 -0600603 unsigned int eventirq = SX9310_FAR_IRQ | SX9310_CLOSE_IRQ;
Daniel Campello72ad02b2020-03-10 14:06:59 -0600604 int ret;
605
606 /* If the state hasn't changed, there's nothing to do. */
607 if (!!(data->chan_event & BIT(chan->channel)) == state)
608 return 0;
609
610 mutex_lock(&data->mutex);
611 if (state) {
612 ret = sx9310_get_event_channel(data, chan->channel);
613 if (ret < 0)
614 goto out_unlock;
615 if (!(data->chan_event & ~BIT(chan->channel))) {
Daniel Campellod9f753f2020-08-03 17:58:02 -0600616 ret = sx9310_enable_irq(data, eventirq);
Daniel Campello72ad02b2020-03-10 14:06:59 -0600617 if (ret < 0)
618 sx9310_put_event_channel(data, chan->channel);
619 }
620 } else {
621 ret = sx9310_put_event_channel(data, chan->channel);
622 if (ret < 0)
623 goto out_unlock;
624 if (!data->chan_event) {
Daniel Campellod9f753f2020-08-03 17:58:02 -0600625 ret = sx9310_disable_irq(data, eventirq);
Daniel Campello72ad02b2020-03-10 14:06:59 -0600626 if (ret < 0)
627 sx9310_get_event_channel(data, chan->channel);
628 }
629 }
630
631out_unlock:
632 mutex_unlock(&data->mutex);
633 return ret;
634}
635
636static struct attribute *sx9310_attributes[] = {
637 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
638 NULL,
639};
640
641static const struct attribute_group sx9310_attribute_group = {
642 .attrs = sx9310_attributes,
643};
644
645static const struct iio_info sx9310_info = {
646 .attrs = &sx9310_attribute_group,
647 .read_raw = sx9310_read_raw,
648 .write_raw = sx9310_write_raw,
649 .read_event_config = sx9310_read_event_config,
650 .write_event_config = sx9310_write_event_config,
651};
652
653static int sx9310_set_trigger_state(struct iio_trigger *trig, bool state)
654{
655 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
656 struct sx9310_data *data = iio_priv(indio_dev);
657 int ret = 0;
658
659 mutex_lock(&data->mutex);
660
661 if (state)
662 ret = sx9310_enable_irq(data, SX9310_CONVDONE_IRQ);
663 else if (!data->chan_read)
664 ret = sx9310_disable_irq(data, SX9310_CONVDONE_IRQ);
665 if (ret < 0)
666 goto out;
667
668 data->trigger_enabled = state;
669
670out:
671 mutex_unlock(&data->mutex);
672
673 return ret;
674}
675
676static const struct iio_trigger_ops sx9310_trigger_ops = {
677 .set_trigger_state = sx9310_set_trigger_state,
678};
679
680static irqreturn_t sx9310_trigger_handler(int irq, void *private)
681{
682 struct iio_poll_func *pf = private;
683 struct iio_dev *indio_dev = pf->indio_dev;
684 struct sx9310_data *data = iio_priv(indio_dev);
685 __be16 val;
686 int bit, ret, i = 0;
687
688 mutex_lock(&data->mutex);
689
690 for_each_set_bit(bit, indio_dev->active_scan_mask,
691 indio_dev->masklength) {
692 ret = sx9310_read_prox_data(data, &indio_dev->channels[bit],
693 &val);
694 if (ret < 0)
695 goto out;
696
697 data->buffer[i++] = val;
698 }
699
700 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
701 pf->timestamp);
702
703out:
704 mutex_unlock(&data->mutex);
705
706 iio_trigger_notify_done(indio_dev->trig);
707
708 return IRQ_HANDLED;
709}
710
711static int sx9310_buffer_preenable(struct iio_dev *indio_dev)
712{
713 struct sx9310_data *data = iio_priv(indio_dev);
714 unsigned int channels = 0;
715 int bit, ret;
716
717 mutex_lock(&data->mutex);
718 for_each_set_bit(bit, indio_dev->active_scan_mask,
719 indio_dev->masklength)
720 channels |= BIT(indio_dev->channels[bit].channel);
721
722 ret = sx9310_update_chan_en(data, channels, data->chan_event);
723 mutex_unlock(&data->mutex);
724 return ret;
725}
726
727static int sx9310_buffer_postdisable(struct iio_dev *indio_dev)
728{
729 struct sx9310_data *data = iio_priv(indio_dev);
730 int ret;
731
732 mutex_lock(&data->mutex);
733 ret = sx9310_update_chan_en(data, 0, data->chan_event);
734 mutex_unlock(&data->mutex);
735 return ret;
736}
737
738static const struct iio_buffer_setup_ops sx9310_buffer_setup_ops = {
739 .preenable = sx9310_buffer_preenable,
Daniel Campello72ad02b2020-03-10 14:06:59 -0600740 .postdisable = sx9310_buffer_postdisable,
741};
742
743struct sx9310_reg_default {
744 u8 reg;
745 u8 def;
746};
747
Daniel Campello72ad02b2020-03-10 14:06:59 -0600748static const struct sx9310_reg_default sx9310_default_regs[] = {
Daniel Campellod9f753f2020-08-03 17:58:02 -0600749 { SX9310_REG_IRQ_MSK, 0x00 },
750 { SX9310_REG_IRQ_FUNC, 0x00 },
Daniel Campello72ad02b2020-03-10 14:06:59 -0600751 /*
752 * The lower 4 bits should not be set as it enable sensors measurements.
753 * Turning the detection on before the configuration values are set to
754 * good values can cause the device to return erroneous readings.
755 */
Daniel Campellod9f753f2020-08-03 17:58:02 -0600756 { SX9310_REG_PROX_CTRL0, SX9310_REG_PROX_CTRL0_SCANPERIOD_15MS },
757 { SX9310_REG_PROX_CTRL1, 0x00 },
758 { SX9310_REG_PROX_CTRL2, SX9310_REG_PROX_CTRL2_COMBMODE_CS1_CS2 |
759 SX9310_REG_PROX_CTRL2_SHIELDEN_DYNAMIC },
760 { SX9310_REG_PROX_CTRL3, SX9310_REG_PROX_CTRL3_GAIN0_X8 |
761 SX9310_REG_PROX_CTRL3_GAIN12_X4 },
762 { SX9310_REG_PROX_CTRL4, SX9310_REG_PROX_CTRL4_RESOLUTION_FINEST },
763 { SX9310_REG_PROX_CTRL5, SX9310_REG_PROX_CTRL5_RANGE_SMALL |
764 SX9310_REG_PROX_CTRL5_STARTUPSENS_CS1 |
765 SX9310_REG_PROX_CTRL5_RAWFILT_1P25 },
766 { SX9310_REG_PROX_CTRL6, SX9310_REG_PROX_CTRL6_AVGTHRESH_DEFAULT },
767 { SX9310_REG_PROX_CTRL7, SX9310_REG_PROX_CTRL7_AVGNEGFILT_2 |
768 SX9310_REG_PROX_CTRL7_AVGPOSFILT_512 },
769 { SX9310_REG_PROX_CTRL8, SX9310_REG_PROX_CTRL8_9_PTHRESH_96 |
770 SX9310_REG_PROX_CTRL8_9_BODYTHRESH_1500 },
771 { SX9310_REG_PROX_CTRL9, SX9310_REG_PROX_CTRL8_9_PTHRESH_28 |
772 SX9310_REG_PROX_CTRL8_9_BODYTHRESH_900 },
773 { SX9310_REG_PROX_CTRL10, SX9310_REG_PROX_CTRL10_HYST_6PCT |
774 SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_2 },
775 { SX9310_REG_PROX_CTRL11, 0x00 },
776 { SX9310_REG_PROX_CTRL12, 0x00 },
777 { SX9310_REG_PROX_CTRL13, 0x00 },
778 { SX9310_REG_PROX_CTRL14, 0x00 },
779 { SX9310_REG_PROX_CTRL15, 0x00 },
780 { SX9310_REG_PROX_CTRL16, 0x00 },
781 { SX9310_REG_PROX_CTRL17, 0x00 },
782 { SX9310_REG_PROX_CTRL18, 0x00 },
783 { SX9310_REG_PROX_CTRL19, 0x00 },
784 { SX9310_REG_SAR_CTRL0, SX9310_REG_SAR_CTRL0_SARDEB_4_SAMPLES |
785 SX9310_REG_SAR_CTRL0_SARHYST_8 },
786 { SX9310_REG_SAR_CTRL1, SX9310_REG_SAR_CTRL1_SLOPE(10781250) },
787 { SX9310_REG_SAR_CTRL2, SX9310_REG_SAR_CTRL2_SAROFFSET_DEFAULT },
Daniel Campello72ad02b2020-03-10 14:06:59 -0600788};
789
790/* Activate all channels and perform an initial compensation. */
791static int sx9310_init_compensation(struct iio_dev *indio_dev)
792{
793 struct sx9310_data *data = iio_priv(indio_dev);
794 int i, ret;
795 unsigned int val;
796 unsigned int ctrl0;
797
798 ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0, &ctrl0);
799 if (ret < 0)
800 return ret;
801
802 /* run the compensation phase on all channels */
803 ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
Daniel Campellod9f753f2020-08-03 17:58:02 -0600804 ctrl0 | SX9310_REG_PROX_CTRL0_SENSOREN_MASK);
Daniel Campello72ad02b2020-03-10 14:06:59 -0600805 if (ret < 0)
806 return ret;
807
808 for (i = 100; i >= 0; i--) {
809 msleep(20);
810 ret = regmap_read(data->regmap, SX9310_REG_STAT1, &val);
811 if (ret < 0)
812 goto out;
Daniel Campellod9f753f2020-08-03 17:58:02 -0600813 if (!(val & SX9310_REG_STAT1_COMPSTAT_MASK))
Daniel Campello72ad02b2020-03-10 14:06:59 -0600814 break;
815 }
816
817 if (i < 0) {
818 dev_err(&data->client->dev,
819 "initial compensation timed out: 0x%02x", val);
820 ret = -ETIMEDOUT;
821 }
822
823out:
824 regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
825 return ret;
826}
827
828static int sx9310_init_device(struct iio_dev *indio_dev)
829{
830 struct sx9310_data *data = iio_priv(indio_dev);
831 const struct sx9310_reg_default *initval;
832 int ret;
833 unsigned int i, val;
834
835 ret = regmap_write(data->regmap, SX9310_REG_RESET, SX9310_SOFT_RESET);
836 if (ret < 0)
837 return ret;
838
839 usleep_range(1000, 2000); /* power-up time is ~1ms. */
840
841 /* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
842 ret = regmap_read(data->regmap, SX9310_REG_IRQ_SRC, &val);
843 if (ret < 0)
844 return ret;
845
846 /* Program some sane defaults. */
847 for (i = 0; i < ARRAY_SIZE(sx9310_default_regs); i++) {
848 initval = &sx9310_default_regs[i];
849 ret = regmap_write(data->regmap, initval->reg, initval->def);
850 if (ret < 0)
851 return ret;
852 }
853
854 return sx9310_init_compensation(indio_dev);
855}
856
857static int sx9310_set_indio_dev_name(struct device *dev,
858 struct iio_dev *indio_dev,
859 const struct i2c_device_id *id, int whoami)
860{
861 const struct acpi_device_id *acpi_id;
862
863 /* id will be NULL when enumerated via ACPI */
864 if (id) {
865 if (id->driver_data != whoami)
866 dev_err(dev, "WHOAMI does not match i2c_device_id: %s",
867 id->name);
868 } else if (ACPI_HANDLE(dev)) {
869 acpi_id = acpi_match_device(dev->driver->acpi_match_table, dev);
870 if (!acpi_id)
871 return -ENODEV;
872 if (acpi_id->driver_data != whoami)
873 dev_err(dev, "WHOAMI does not match acpi_device_id: %s",
874 acpi_id->id);
875 } else
876 return -ENODEV;
877
878 switch (whoami) {
879 case SX9310_WHOAMI_VALUE:
880 indio_dev->name = "sx9310";
881 break;
882 case SX9311_WHOAMI_VALUE:
883 indio_dev->name = "sx9311";
884 break;
885 default:
886 dev_err(dev, "unexpected WHOAMI response: %u", whoami);
887 return -ENODEV;
888 }
889
890 return 0;
891}
892
893static int sx9310_probe(struct i2c_client *client,
894 const struct i2c_device_id *id)
895{
896 int ret;
897 struct iio_dev *indio_dev;
898 struct sx9310_data *data;
899
900 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
901 if (indio_dev == NULL)
902 return -ENOMEM;
903
904 data = iio_priv(indio_dev);
905 data->client = client;
906 mutex_init(&data->mutex);
907 init_completion(&data->completion);
908
909 data->regmap = devm_regmap_init_i2c(client, &sx9310_regmap_config);
910 if (IS_ERR(data->regmap))
911 return PTR_ERR(data->regmap);
912
913 ret = regmap_read(data->regmap, SX9310_REG_WHOAMI, &data->whoami);
914 if (ret < 0) {
915 dev_err(&client->dev, "error in reading WHOAMI register: %d",
916 ret);
917 return ret;
918 }
919
920 ret = sx9310_set_indio_dev_name(&client->dev, indio_dev, id,
921 data->whoami);
922 if (ret < 0)
923 return ret;
924
925 ACPI_COMPANION_SET(&indio_dev->dev, ACPI_COMPANION(&client->dev));
Daniel Campello72ad02b2020-03-10 14:06:59 -0600926 indio_dev->channels = sx9310_channels;
927 indio_dev->num_channels = ARRAY_SIZE(sx9310_channels);
928 indio_dev->info = &sx9310_info;
929 indio_dev->modes = INDIO_DIRECT_MODE;
930 i2c_set_clientdata(client, indio_dev);
931
932 ret = sx9310_init_device(indio_dev);
933 if (ret < 0)
934 return ret;
935
936 if (client->irq) {
937 ret = devm_request_threaded_irq(&client->dev, client->irq,
938 sx9310_irq_handler,
939 sx9310_irq_thread_handler,
940 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
941 "sx9310_event", indio_dev);
942 if (ret < 0)
943 return ret;
944
945 data->trig =
946 devm_iio_trigger_alloc(&client->dev, "%s-dev%d",
947 indio_dev->name, indio_dev->id);
948 if (!data->trig)
949 return -ENOMEM;
950
951 data->trig->dev.parent = &client->dev;
952 data->trig->ops = &sx9310_trigger_ops;
953 iio_trigger_set_drvdata(data->trig, indio_dev);
954
955 ret = devm_iio_trigger_register(&client->dev, data->trig);
956 if (ret)
957 return ret;
958 }
959
960 ret = devm_iio_triggered_buffer_setup(&client->dev, indio_dev,
961 iio_pollfunc_store_time,
962 sx9310_trigger_handler,
963 &sx9310_buffer_setup_ops);
964 if (ret < 0)
965 return ret;
966
967 return devm_iio_device_register(&client->dev, indio_dev);
968}
969
970static int __maybe_unused sx9310_suspend(struct device *dev)
971{
972 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
973 struct sx9310_data *data = iio_priv(indio_dev);
974 u8 ctrl0;
975 int ret;
976
977 disable_irq_nosync(data->client->irq);
978
979 mutex_lock(&data->mutex);
980 ret = regmap_read(data->regmap, SX9310_REG_PROX_CTRL0,
981 &data->suspend_ctrl0);
982
983 if (ret)
984 goto out;
985
Daniel Campellod9f753f2020-08-03 17:58:02 -0600986 ctrl0 = data->suspend_ctrl0 & ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK;
Daniel Campello72ad02b2020-03-10 14:06:59 -0600987 ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0, ctrl0);
988 if (ret)
989 goto out;
990
991 ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 0);
992
993out:
994 mutex_unlock(&data->mutex);
995 return ret;
996}
997
998static int __maybe_unused sx9310_resume(struct device *dev)
999{
1000 struct iio_dev *indio_dev = i2c_get_clientdata(to_i2c_client(dev));
1001 struct sx9310_data *data = iio_priv(indio_dev);
1002 int ret;
1003
1004 mutex_lock(&data->mutex);
1005 ret = regmap_write(data->regmap, SX9310_REG_PAUSE, 1);
1006 if (ret)
1007 goto out;
1008
1009 ret = regmap_write(data->regmap, SX9310_REG_PROX_CTRL0,
1010 data->suspend_ctrl0);
1011
1012out:
1013 mutex_unlock(&data->mutex);
Daniel Campello364e8532020-08-03 17:58:03 -06001014 if (ret)
1015 return ret;
Daniel Campello72ad02b2020-03-10 14:06:59 -06001016
1017 enable_irq(data->client->irq);
Daniel Campello364e8532020-08-03 17:58:03 -06001018 return 0;
Daniel Campello72ad02b2020-03-10 14:06:59 -06001019}
1020
1021static const struct dev_pm_ops sx9310_pm_ops = {
1022 SET_SYSTEM_SLEEP_PM_OPS(sx9310_suspend, sx9310_resume)
1023};
1024
1025static const struct acpi_device_id sx9310_acpi_match[] = {
1026 { "STH9310", SX9310_WHOAMI_VALUE },
1027 { "STH9311", SX9311_WHOAMI_VALUE },
1028 {},
1029};
1030MODULE_DEVICE_TABLE(acpi, sx9310_acpi_match);
1031
1032static const struct of_device_id sx9310_of_match[] = {
1033 { .compatible = "semtech,sx9310" },
1034 { .compatible = "semtech,sx9311" },
1035 {},
1036};
1037MODULE_DEVICE_TABLE(of, sx9310_of_match);
1038
1039static const struct i2c_device_id sx9310_id[] = {
1040 { "sx9310", SX9310_WHOAMI_VALUE },
1041 { "sx9311", SX9311_WHOAMI_VALUE },
1042 {},
1043};
1044MODULE_DEVICE_TABLE(i2c, sx9310_id);
1045
1046static struct i2c_driver sx9310_driver = {
1047 .driver = {
1048 .name = "sx9310",
1049 .acpi_match_table = ACPI_PTR(sx9310_acpi_match),
1050 .of_match_table = of_match_ptr(sx9310_of_match),
1051 .pm = &sx9310_pm_ops,
1052 },
1053 .probe = sx9310_probe,
1054 .id_table = sx9310_id,
1055};
1056module_i2c_driver(sx9310_driver);
1057
1058MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
1059MODULE_AUTHOR("Daniel Campello <campello@chromium.org>");
1060MODULE_DESCRIPTION("Driver for Semtech SX9310/SX9311 proximity sensor");
1061MODULE_LICENSE("GPL v2");