1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2018 Google LLC.
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>.
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>
23 #include <linux/regmap.h>
24 #include <linux/regulator/consumer.h>
25 #include <linux/slab.h>
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>
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
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
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
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
);
145 /* Serialize access to registers and channel configuration */
147 struct i2c_client
*client
;
148 struct iio_trigger
*trig
;
149 struct regmap
*regmap
;
150 struct regulator_bulk_data supplies
[2];
152 * Last reading of the proximity status for each channel.
153 * We only send an event to user space when this changes.
155 unsigned long chan_prox_stat
;
156 bool trigger_enabled
;
157 /* Ensure correct alignment of timestamp when present. */
159 __be16 channels
[SX9310_NUM_CHANNELS
];
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
;
170 static const struct iio_event_spec sx9310_events
[] = {
172 .type
= IIO_EV_TYPE_THRESH
,
173 .dir
= IIO_EV_DIR_RISING
,
174 .mask_shared_by_all
= BIT(IIO_EV_INFO_PERIOD
),
177 .type
= IIO_EV_TYPE_THRESH
,
178 .dir
= IIO_EV_DIR_FALLING
,
179 .mask_shared_by_all
= BIT(IIO_EV_INFO_PERIOD
),
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
),
190 #define SX9310_NAMED_CHANNEL(idx, name) \
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), \
200 .extend_name = name, \
201 .address = SX9310_REG_DIFF_MSB, \
202 .event_spec = sx9310_events, \
203 .num_event_specs = ARRAY_SIZE(sx9310_events), \
209 .endianness = IIO_BE, \
212 #define SX9310_CHANNEL(idx) SX9310_NAMED_CHANNEL(idx, NULL)
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 */
220 IIO_CHAN_SOFT_TIMESTAMP(4),
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.
227 static const struct {
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 */
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,
253 static ssize_t
sx9310_show_samp_freq_avail(struct device
*dev
,
254 struct device_attribute
*attr
,
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
);
267 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(sx9310_show_samp_freq_avail
);
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
),
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
),
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
),
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
),
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
),
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
),
310 static const struct regmap_config sx9310_regmap_config
= {
314 .max_register
= SX9310_REG_RESET
,
315 .cache_type
= REGCACHE_RBTREE
,
317 .wr_table
= &sx9310_writeable_regs
,
318 .rd_table
= &sx9310_readable_regs
,
319 .volatile_table
= &sx9310_volatile_regs
,
322 static int sx9310_update_chan_en(struct sx9310_data
*data
,
323 unsigned long chan_read
,
324 unsigned long chan_event
)
327 unsigned long channels
= chan_read
| chan_event
;
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
,
336 data
->chan_read
= chan_read
;
337 data
->chan_event
= chan_event
;
341 static int sx9310_get_read_channel(struct sx9310_data
*data
, int channel
)
343 return sx9310_update_chan_en(data
, data
->chan_read
| BIT(channel
),
347 static int sx9310_put_read_channel(struct sx9310_data
*data
, int channel
)
349 return sx9310_update_chan_en(data
, data
->chan_read
& ~BIT(channel
),
353 static int sx9310_get_event_channel(struct sx9310_data
*data
, int channel
)
355 return sx9310_update_chan_en(data
, data
->chan_read
,
356 data
->chan_event
| BIT(channel
));
359 static int sx9310_put_event_channel(struct sx9310_data
*data
, int channel
)
361 return sx9310_update_chan_en(data
, data
->chan_read
,
362 data
->chan_event
& ~BIT(channel
));
365 static int sx9310_enable_irq(struct sx9310_data
*data
, unsigned int irq
)
367 if (!data
->client
->irq
)
369 return regmap_update_bits(data
->regmap
, SX9310_REG_IRQ_MSK
, irq
, irq
);
372 static int sx9310_disable_irq(struct sx9310_data
*data
, unsigned int irq
)
374 if (!data
->client
->irq
)
376 return regmap_update_bits(data
->regmap
, SX9310_REG_IRQ_MSK
, irq
, 0);
379 static int sx9310_read_prox_data(struct sx9310_data
*data
,
380 const struct iio_chan_spec
*chan
, __be16
*val
)
384 ret
= regmap_write(data
->regmap
, SX9310_REG_SENSOR_SEL
, chan
->channel
);
388 return regmap_bulk_read(data
->regmap
, chan
->address
, val
, sizeof(*val
));
392 * If we have no interrupt support, we have to wait for a scan period
393 * after enabling a channel to get a result.
395 static int sx9310_wait_for_sample(struct sx9310_data
*data
)
400 ret
= regmap_read(data
->regmap
, SX9310_REG_PROX_CTRL0
, &val
);
404 val
= FIELD_GET(SX9310_REG_PROX_CTRL0_SCANPERIOD_MASK
, val
);
406 msleep(sx9310_scan_period_table
[val
]);
411 static int sx9310_read_proximity(struct sx9310_data
*data
,
412 const struct iio_chan_spec
*chan
, int *val
)
417 mutex_lock(&data
->mutex
);
419 ret
= sx9310_get_read_channel(data
, chan
->channel
);
423 ret
= sx9310_enable_irq(data
, SX9310_CONVDONE_IRQ
);
425 goto out_put_channel
;
427 mutex_unlock(&data
->mutex
);
429 if (data
->client
->irq
) {
430 ret
= wait_for_completion_interruptible(&data
->completion
);
431 reinit_completion(&data
->completion
);
433 ret
= sx9310_wait_for_sample(data
);
436 mutex_lock(&data
->mutex
);
439 goto out_disable_irq
;
441 ret
= sx9310_read_prox_data(data
, chan
, &rawval
);
443 goto out_disable_irq
;
445 *val
= sign_extend32(be16_to_cpu(rawval
),
446 chan
->address
== SX9310_REG_DIFF_MSB
? 11 : 15);
448 ret
= sx9310_disable_irq(data
, SX9310_CONVDONE_IRQ
);
450 goto out_put_channel
;
452 ret
= sx9310_put_read_channel(data
, chan
->channel
);
456 mutex_unlock(&data
->mutex
);
461 sx9310_disable_irq(data
, SX9310_CONVDONE_IRQ
);
463 sx9310_put_read_channel(data
, chan
->channel
);
465 mutex_unlock(&data
->mutex
);
470 static int sx9310_read_gain(struct sx9310_data
*data
,
471 const struct iio_chan_spec
*chan
, int *val
)
473 unsigned int regval
, gain
;
476 ret
= regmap_read(data
->regmap
, SX9310_REG_PROX_CTRL3
, ®val
);
480 switch (chan
->channel
) {
483 gain
= FIELD_GET(SX9310_REG_PROX_CTRL3_GAIN0_MASK
, regval
);
487 gain
= FIELD_GET(SX9310_REG_PROX_CTRL3_GAIN12_MASK
, regval
);
498 static int sx9310_read_samp_freq(struct sx9310_data
*data
, int *val
, int *val2
)
503 ret
= regmap_read(data
->regmap
, SX9310_REG_PROX_CTRL0
, ®val
);
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
;
511 return IIO_VAL_INT_PLUS_MICRO
;
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
)
518 struct sx9310_data
*data
= iio_priv(indio_dev
);
521 if (chan
->type
!= IIO_PROXIMITY
)
525 case IIO_CHAN_INFO_RAW
:
526 ret
= iio_device_claim_direct_mode(indio_dev
);
530 ret
= sx9310_read_proximity(data
, chan
, val
);
531 iio_device_release_direct_mode(indio_dev
);
533 case IIO_CHAN_INFO_HARDWAREGAIN
:
534 ret
= iio_device_claim_direct_mode(indio_dev
);
538 ret
= sx9310_read_gain(data
, chan
, val
);
539 iio_device_release_direct_mode(indio_dev
);
541 case IIO_CHAN_INFO_SAMP_FREQ
:
542 return sx9310_read_samp_freq(data
, val
, val2
);
548 static const int sx9310_gain_vals
[] = { 1, 2, 4, 8 };
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
,
555 if (chan
->type
!= IIO_PROXIMITY
)
559 case IIO_CHAN_INFO_HARDWAREGAIN
:
561 *length
= ARRAY_SIZE(sx9310_gain_vals
);
562 *vals
= sx9310_gain_vals
;
563 return IIO_AVAIL_LIST
;
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
574 static int sx9310_get_thresh_reg(unsigned int channel
)
579 return SX9310_REG_PROX_CTRL8
;
582 return SX9310_REG_PROX_CTRL9
;
588 static int sx9310_read_thresh(struct sx9310_data
*data
,
589 const struct iio_chan_spec
*chan
, int *val
)
595 reg
= ret
= sx9310_get_thresh_reg(chan
->channel
);
599 ret
= regmap_read(data
->regmap
, reg
, ®val
);
603 regval
= FIELD_GET(SX9310_REG_PROX_CTRL8_9_PTHRESH_MASK
, regval
);
604 if (regval
> ARRAY_SIZE(sx9310_pthresh_codes
))
607 *val
= sx9310_pthresh_codes
[regval
];
611 static int sx9310_read_hysteresis(struct sx9310_data
*data
,
612 const struct iio_chan_spec
*chan
, int *val
)
614 unsigned int regval
, pthresh
;
617 ret
= sx9310_read_thresh(data
, chan
, &pthresh
);
621 ret
= regmap_read(data
->regmap
, SX9310_REG_PROX_CTRL10
, ®val
);
625 regval
= FIELD_GET(SX9310_REG_PROX_CTRL10_HYST_MASK
, regval
);
629 /* regval is at most 5 */
630 *val
= pthresh
>> (5 - regval
);
635 static int sx9310_read_far_debounce(struct sx9310_data
*data
, int *val
)
640 ret
= regmap_read(data
->regmap
, SX9310_REG_PROX_CTRL10
, ®val
);
644 regval
= FIELD_GET(SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK
, regval
);
653 static int sx9310_read_close_debounce(struct sx9310_data
*data
, int *val
)
658 ret
= regmap_read(data
->regmap
, SX9310_REG_PROX_CTRL10
, ®val
);
662 regval
= FIELD_GET(SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK
, regval
);
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
)
677 struct sx9310_data
*data
= iio_priv(indio_dev
);
679 if (chan
->type
!= IIO_PROXIMITY
)
683 case IIO_EV_INFO_VALUE
:
684 return sx9310_read_thresh(data
, chan
, val
);
685 case IIO_EV_INFO_PERIOD
:
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
);
694 case IIO_EV_INFO_HYSTERESIS
:
695 return sx9310_read_hysteresis(data
, chan
, val
);
701 static int sx9310_write_thresh(struct sx9310_data
*data
,
702 const struct iio_chan_spec
*chan
, int val
)
708 reg
= ret
= sx9310_get_thresh_reg(chan
->channel
);
712 for (i
= 0; i
< ARRAY_SIZE(sx9310_pthresh_codes
); i
++) {
713 if (sx9310_pthresh_codes
[i
] == val
) {
719 if (i
== ARRAY_SIZE(sx9310_pthresh_codes
))
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
);
731 static int sx9310_write_hysteresis(struct sx9310_data
*data
,
732 const struct iio_chan_spec
*chan
, int _val
)
734 unsigned int hyst
, val
= _val
;
737 ret
= sx9310_read_thresh(data
, chan
, &pthresh
);
743 else if (val
== pthresh
>> 2)
745 else if (val
== pthresh
>> 3)
747 else if (val
== pthresh
>> 4)
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
);
761 static int sx9310_write_far_debounce(struct sx9310_data
*data
, int val
)
767 regval
= FIELD_PREP(SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK
, val
);
769 mutex_lock(&data
->mutex
);
770 ret
= regmap_update_bits(data
->regmap
, SX9310_REG_PROX_CTRL10
,
771 SX9310_REG_PROX_CTRL10_FAR_DEBOUNCE_MASK
,
773 mutex_unlock(&data
->mutex
);
778 static int sx9310_write_close_debounce(struct sx9310_data
*data
, int val
)
784 regval
= FIELD_PREP(SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK
, val
);
786 mutex_lock(&data
->mutex
);
787 ret
= regmap_update_bits(data
->regmap
, SX9310_REG_PROX_CTRL10
,
788 SX9310_REG_PROX_CTRL10_CLOSE_DEBOUNCE_MASK
,
790 mutex_unlock(&data
->mutex
);
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
)
801 struct sx9310_data
*data
= iio_priv(indio_dev
);
803 if (chan
->type
!= IIO_PROXIMITY
)
807 case IIO_EV_INFO_VALUE
:
808 return sx9310_write_thresh(data
, chan
, val
);
809 case IIO_EV_INFO_PERIOD
:
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
);
818 case IIO_EV_INFO_HYSTERESIS
:
819 return sx9310_write_hysteresis(data
, chan
, val
);
825 static int sx9310_set_samp_freq(struct sx9310_data
*data
, int val
, int val2
)
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
)
834 if (i
== ARRAY_SIZE(sx9310_samp_freq_table
))
837 mutex_lock(&data
->mutex
);
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
));
844 mutex_unlock(&data
->mutex
);
849 static int sx9310_write_gain(struct sx9310_data
*data
,
850 const struct iio_chan_spec
*chan
, int val
)
852 unsigned int gain
, mask
;
857 switch (chan
->channel
) {
860 mask
= SX9310_REG_PROX_CTRL3_GAIN0_MASK
;
861 gain
= FIELD_PREP(SX9310_REG_PROX_CTRL3_GAIN0_MASK
, gain
);
865 mask
= SX9310_REG_PROX_CTRL3_GAIN12_MASK
;
866 gain
= FIELD_PREP(SX9310_REG_PROX_CTRL3_GAIN12_MASK
, gain
);
872 mutex_lock(&data
->mutex
);
873 ret
= regmap_update_bits(data
->regmap
, SX9310_REG_PROX_CTRL3
, mask
,
875 mutex_unlock(&data
->mutex
);
880 static int sx9310_write_raw(struct iio_dev
*indio_dev
,
881 const struct iio_chan_spec
*chan
, int val
, int val2
,
884 struct sx9310_data
*data
= iio_priv(indio_dev
);
886 if (chan
->type
!= IIO_PROXIMITY
)
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
);
899 static irqreturn_t
sx9310_irq_handler(int irq
, void *private)
901 struct iio_dev
*indio_dev
= private;
902 struct sx9310_data
*data
= iio_priv(indio_dev
);
904 if (data
->trigger_enabled
)
905 iio_trigger_poll(data
->trig
);
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.
912 return IRQ_WAKE_THREAD
;
915 static void sx9310_push_events(struct iio_dev
*indio_dev
)
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
;
923 /* Read proximity state on all channels */
924 ret
= regmap_read(data
->regmap
, SX9310_REG_STAT0
, &val
);
926 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
931 * Only iterate over channels with changes on proximity status that have
934 prox_changed
= (data
->chan_prox_stat
^ val
) & data
->chan_event
;
936 for_each_set_bit(chan
, &prox_changed
, SX9310_NUM_CHANNELS
) {
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
);
944 iio_push_event(indio_dev
, ev
, timestamp
);
946 data
->chan_prox_stat
= val
;
949 static irqreturn_t
sx9310_irq_thread_handler(int irq
, void *private)
951 struct iio_dev
*indio_dev
= private;
952 struct sx9310_data
*data
= iio_priv(indio_dev
);
956 mutex_lock(&data
->mutex
);
958 ret
= regmap_read(data
->regmap
, SX9310_REG_IRQ_SRC
, &val
);
960 dev_err(&data
->client
->dev
, "i2c transfer error in irq\n");
964 if (val
& (SX9310_FAR_IRQ
| SX9310_CLOSE_IRQ
))
965 sx9310_push_events(indio_dev
);
967 if (val
& SX9310_CONVDONE_IRQ
)
968 complete(&data
->completion
);
971 mutex_unlock(&data
->mutex
);
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
)
981 struct sx9310_data
*data
= iio_priv(indio_dev
);
983 return !!(data
->chan_event
& BIT(chan
->channel
));
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
)
991 struct sx9310_data
*data
= iio_priv(indio_dev
);
992 unsigned int eventirq
= SX9310_FAR_IRQ
| SX9310_CLOSE_IRQ
;
995 /* If the state hasn't changed, there's nothing to do. */
996 if (!!(data
->chan_event
& BIT(chan
->channel
)) == state
)
999 mutex_lock(&data
->mutex
);
1001 ret
= sx9310_get_event_channel(data
, chan
->channel
);
1004 if (!(data
->chan_event
& ~BIT(chan
->channel
))) {
1005 ret
= sx9310_enable_irq(data
, eventirq
);
1007 sx9310_put_event_channel(data
, chan
->channel
);
1010 ret
= sx9310_put_event_channel(data
, chan
->channel
);
1013 if (!data
->chan_event
) {
1014 ret
= sx9310_disable_irq(data
, eventirq
);
1016 sx9310_get_event_channel(data
, chan
->channel
);
1021 mutex_unlock(&data
->mutex
);
1025 static struct attribute
*sx9310_attributes
[] = {
1026 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
1030 static const struct attribute_group sx9310_attribute_group
= {
1031 .attrs
= sx9310_attributes
,
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
,
1045 static int sx9310_set_trigger_state(struct iio_trigger
*trig
, bool state
)
1047 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
1048 struct sx9310_data
*data
= iio_priv(indio_dev
);
1051 mutex_lock(&data
->mutex
);
1054 ret
= sx9310_enable_irq(data
, SX9310_CONVDONE_IRQ
);
1055 else if (!data
->chan_read
)
1056 ret
= sx9310_disable_irq(data
, SX9310_CONVDONE_IRQ
);
1060 data
->trigger_enabled
= state
;
1063 mutex_unlock(&data
->mutex
);
1068 static const struct iio_trigger_ops sx9310_trigger_ops
= {
1069 .set_trigger_state
= sx9310_set_trigger_state
,
1072 static irqreturn_t
sx9310_trigger_handler(int irq
, void *private)
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
);
1078 int bit
, ret
, i
= 0;
1080 mutex_lock(&data
->mutex
);
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
],
1089 data
->buffer
.channels
[i
++] = val
;
1092 iio_push_to_buffers_with_timestamp(indio_dev
, &data
->buffer
,
1096 mutex_unlock(&data
->mutex
);
1098 iio_trigger_notify_done(indio_dev
->trig
);
1103 static int sx9310_buffer_preenable(struct iio_dev
*indio_dev
)
1105 struct sx9310_data
*data
= iio_priv(indio_dev
);
1106 unsigned long channels
= 0;
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
);
1114 ret
= sx9310_update_chan_en(data
, channels
, data
->chan_event
);
1115 mutex_unlock(&data
->mutex
);
1119 static int sx9310_buffer_postdisable(struct iio_dev
*indio_dev
)
1121 struct sx9310_data
*data
= iio_priv(indio_dev
);
1124 mutex_lock(&data
->mutex
);
1125 ret
= sx9310_update_chan_en(data
, 0, data
->chan_event
);
1126 mutex_unlock(&data
->mutex
);
1130 static const struct iio_buffer_setup_ops sx9310_buffer_setup_ops
= {
1131 .preenable
= sx9310_buffer_preenable
,
1132 .postdisable
= sx9310_buffer_postdisable
,
1135 struct sx9310_reg_default
{
1140 static const struct sx9310_reg_default sx9310_default_regs
[] = {
1141 { SX9310_REG_IRQ_MSK
, 0x00 },
1142 { SX9310_REG_IRQ_FUNC
, 0x00 },
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.
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
},
1182 /* Activate all channels and perform an initial compensation. */
1183 static int sx9310_init_compensation(struct iio_dev
*indio_dev
)
1185 struct sx9310_data
*data
= iio_priv(indio_dev
);
1190 ret
= regmap_read(data
->regmap
, SX9310_REG_PROX_CTRL0
, &ctrl0
);
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
);
1200 ret
= regmap_read_poll_timeout(data
->regmap
, SX9310_REG_STAT1
, val
,
1201 !(val
& SX9310_REG_STAT1_COMPSTAT_MASK
),
1204 if (ret
== -ETIMEDOUT
)
1205 dev_err(&data
->client
->dev
,
1206 "initial compensation timed out: 0x%02x\n",
1211 regmap_write(data
->regmap
, SX9310_REG_PROX_CTRL0
, ctrl0
);
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
)
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;
1224 u32 start
= 0, raw
= 0, pos
= 0;
1226 memcpy(reg_def
, &sx9310_default_regs
[i
], sizeof(*reg_def
));
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
;
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
]);
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
;
1256 case SX9310_REG_PROX_CTRL4
:
1257 ret
= of_property_read_string(np
, "semtech,resolution", &res
);
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
;
1280 case SX9310_REG_PROX_CTRL5
:
1281 ret
= of_property_read_u32(np
, "semtech,startup-sensor", &start
);
1283 start
= FIELD_GET(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK
,
1287 reg_def
->def
&= ~SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK
;
1288 reg_def
->def
|= FIELD_PREP(SX9310_REG_PROX_CTRL5_STARTUPSENS_MASK
,
1291 ret
= of_property_read_u32(np
, "semtech,proxraw-strength", &raw
);
1293 raw
= FIELD_GET(SX9310_REG_PROX_CTRL5_RAWFILT_MASK
,
1299 reg_def
->def
&= ~SX9310_REG_PROX_CTRL5_RAWFILT_MASK
;
1300 reg_def
->def
|= FIELD_PREP(SX9310_REG_PROX_CTRL5_RAWFILT_MASK
,
1303 case SX9310_REG_PROX_CTRL7
:
1304 ret
= of_property_read_u32(np
, "semtech,avg-pos-strength", &pos
);
1308 pos
= min(max(ilog2(pos
), 3), 10) - 3;
1309 reg_def
->def
&= ~SX9310_REG_PROX_CTRL7_AVGPOSFILT_MASK
;
1310 reg_def
->def
|= FIELD_PREP(SX9310_REG_PROX_CTRL7_AVGPOSFILT_MASK
,
1318 static int sx9310_init_device(struct iio_dev
*indio_dev
)
1320 struct sx9310_data
*data
= iio_priv(indio_dev
);
1321 struct sx9310_reg_default tmp
;
1322 const struct sx9310_reg_default
*initval
;
1324 unsigned int i
, val
;
1326 ret
= regmap_write(data
->regmap
, SX9310_REG_RESET
, SX9310_SOFT_RESET
);
1330 usleep_range(1000, 2000); /* power-up time is ~1ms. */
1332 /* Clear reset interrupt state by reading SX9310_REG_IRQ_SRC. */
1333 ret
= regmap_read(data
->regmap
, SX9310_REG_IRQ_SRC
, &val
);
1337 /* Program some sane defaults. */
1338 for (i
= 0; i
< ARRAY_SIZE(sx9310_default_regs
); i
++) {
1339 initval
= sx9310_get_default_reg(data
, i
, &tmp
);
1340 ret
= regmap_write(data
->regmap
, initval
->reg
, initval
->def
);
1345 return sx9310_init_compensation(indio_dev
);
1348 static int sx9310_set_indio_dev_name(struct device
*dev
,
1349 struct iio_dev
*indio_dev
,
1350 unsigned int whoami
)
1352 unsigned int long ddata
;
1354 ddata
= (uintptr_t)device_get_match_data(dev
);
1355 if (ddata
!= whoami
) {
1356 dev_err(dev
, "WHOAMI does not match device data: %u\n", whoami
);
1361 case SX9310_WHOAMI_VALUE
:
1362 indio_dev
->name
= "sx9310";
1364 case SX9311_WHOAMI_VALUE
:
1365 indio_dev
->name
= "sx9311";
1368 dev_err(dev
, "unexpected WHOAMI response: %u\n", whoami
);
1375 static void sx9310_regulator_disable(void *_data
)
1377 struct sx9310_data
*data
= _data
;
1379 regulator_bulk_disable(ARRAY_SIZE(data
->supplies
), data
->supplies
);
1382 static int sx9310_probe(struct i2c_client
*client
)
1385 struct device
*dev
= &client
->dev
;
1386 struct iio_dev
*indio_dev
;
1387 struct sx9310_data
*data
;
1389 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*data
));
1393 data
= iio_priv(indio_dev
);
1394 data
->client
= client
;
1395 data
->supplies
[0].supply
= "vdd";
1396 data
->supplies
[1].supply
= "svdd";
1397 mutex_init(&data
->mutex
);
1398 init_completion(&data
->completion
);
1400 data
->regmap
= devm_regmap_init_i2c(client
, &sx9310_regmap_config
);
1401 if (IS_ERR(data
->regmap
))
1402 return PTR_ERR(data
->regmap
);
1404 ret
= devm_regulator_bulk_get(dev
, ARRAY_SIZE(data
->supplies
),
1409 ret
= regulator_bulk_enable(ARRAY_SIZE(data
->supplies
), data
->supplies
);
1412 /* Must wait for Tpor time after initial power up */
1413 usleep_range(1000, 1100);
1415 ret
= devm_add_action_or_reset(dev
, sx9310_regulator_disable
, data
);
1419 ret
= regmap_read(data
->regmap
, SX9310_REG_WHOAMI
, &data
->whoami
);
1421 dev_err(dev
, "error in reading WHOAMI register: %d", ret
);
1425 ret
= sx9310_set_indio_dev_name(dev
, indio_dev
, data
->whoami
);
1429 ACPI_COMPANION_SET(&indio_dev
->dev
, ACPI_COMPANION(dev
));
1430 indio_dev
->channels
= sx9310_channels
;
1431 indio_dev
->num_channels
= ARRAY_SIZE(sx9310_channels
);
1432 indio_dev
->info
= &sx9310_info
;
1433 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1434 i2c_set_clientdata(client
, indio_dev
);
1436 ret
= sx9310_init_device(indio_dev
);
1441 ret
= devm_request_threaded_irq(dev
, client
->irq
,
1443 sx9310_irq_thread_handler
,
1445 "sx9310_event", indio_dev
);
1449 data
->trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d",
1455 data
->trig
->dev
.parent
= dev
;
1456 data
->trig
->ops
= &sx9310_trigger_ops
;
1457 iio_trigger_set_drvdata(data
->trig
, indio_dev
);
1459 ret
= devm_iio_trigger_register(dev
, data
->trig
);
1464 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
,
1465 iio_pollfunc_store_time
,
1466 sx9310_trigger_handler
,
1467 &sx9310_buffer_setup_ops
);
1471 return devm_iio_device_register(dev
, indio_dev
);
1474 static int __maybe_unused
sx9310_suspend(struct device
*dev
)
1476 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1477 struct sx9310_data
*data
= iio_priv(indio_dev
);
1481 disable_irq_nosync(data
->client
->irq
);
1483 mutex_lock(&data
->mutex
);
1484 ret
= regmap_read(data
->regmap
, SX9310_REG_PROX_CTRL0
,
1485 &data
->suspend_ctrl0
);
1489 ctrl0
= data
->suspend_ctrl0
& ~SX9310_REG_PROX_CTRL0_SENSOREN_MASK
;
1490 ret
= regmap_write(data
->regmap
, SX9310_REG_PROX_CTRL0
, ctrl0
);
1494 ret
= regmap_write(data
->regmap
, SX9310_REG_PAUSE
, 0);
1497 mutex_unlock(&data
->mutex
);
1501 static int __maybe_unused
sx9310_resume(struct device
*dev
)
1503 struct iio_dev
*indio_dev
= i2c_get_clientdata(to_i2c_client(dev
));
1504 struct sx9310_data
*data
= iio_priv(indio_dev
);
1507 mutex_lock(&data
->mutex
);
1508 ret
= regmap_write(data
->regmap
, SX9310_REG_PAUSE
, 1);
1512 ret
= regmap_write(data
->regmap
, SX9310_REG_PROX_CTRL0
,
1513 data
->suspend_ctrl0
);
1516 mutex_unlock(&data
->mutex
);
1520 enable_irq(data
->client
->irq
);
1524 static const struct dev_pm_ops sx9310_pm_ops
= {
1525 SET_SYSTEM_SLEEP_PM_OPS(sx9310_suspend
, sx9310_resume
)
1528 static const struct acpi_device_id sx9310_acpi_match
[] = {
1529 { "STH9310", SX9310_WHOAMI_VALUE
},
1530 { "STH9311", SX9311_WHOAMI_VALUE
},
1533 MODULE_DEVICE_TABLE(acpi
, sx9310_acpi_match
);
1535 static const struct of_device_id sx9310_of_match
[] = {
1536 { .compatible
= "semtech,sx9310", (void *)SX9310_WHOAMI_VALUE
},
1537 { .compatible
= "semtech,sx9311", (void *)SX9311_WHOAMI_VALUE
},
1540 MODULE_DEVICE_TABLE(of
, sx9310_of_match
);
1542 static const struct i2c_device_id sx9310_id
[] = {
1543 { "sx9310", SX9310_WHOAMI_VALUE
},
1544 { "sx9311", SX9311_WHOAMI_VALUE
},
1547 MODULE_DEVICE_TABLE(i2c
, sx9310_id
);
1549 static struct i2c_driver sx9310_driver
= {
1552 .acpi_match_table
= sx9310_acpi_match
,
1553 .of_match_table
= sx9310_of_match
,
1554 .pm
= &sx9310_pm_ops
,
1557 * Lots of i2c transfers in probe + over 200 ms waiting in
1558 * sx9310_init_compensation() mean a slow probe; prefer async
1559 * so we don't delay boot if we're builtin to the kernel.
1561 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
1563 .probe_new
= sx9310_probe
,
1564 .id_table
= sx9310_id
,
1566 module_i2c_driver(sx9310_driver
);
1568 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
1569 MODULE_AUTHOR("Daniel Campello <campello@chromium.org>");
1570 MODULE_DESCRIPTION("Driver for Semtech SX9310/SX9311 proximity sensor");
1571 MODULE_LICENSE("GPL v2");