1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright 2021 Google LLC.
5 * Driver for Semtech's SX9360 capacitive proximity/button solution.
6 * Based on SX9360 driver and copy of datasheet at:
7 * https://edit.wpgdadawant.com/uploads/news_file/program/2019/30184/tech_files/program_30184_suggest_other_file.pdf
10 #include <linux/bits.h>
11 #include <linux/bitfield.h>
12 #include <linux/delay.h>
13 #include <linux/i2c.h>
14 #include <linux/interrupt.h>
15 #include <linux/kernel.h>
16 #include <linux/log2.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
20 #include <linux/property.h>
21 #include <linux/regmap.h>
23 #include <linux/iio/iio.h>
25 #include "sx_common.h"
27 /* Nominal Oscillator Frequency. */
28 #define SX9360_FOSC_MHZ 4
29 #define SX9360_FOSC_HZ (SX9360_FOSC_MHZ * 1000000)
31 /* Register definitions. */
32 #define SX9360_REG_IRQ_SRC SX_COMMON_REG_IRQ_SRC
33 #define SX9360_REG_STAT 0x01
34 #define SX9360_REG_STAT_COMPSTAT_MASK GENMASK(2, 1)
35 #define SX9360_REG_IRQ_MSK 0x02
36 #define SX9360_CONVDONE_IRQ BIT(0)
37 #define SX9360_FAR_IRQ BIT(2)
38 #define SX9360_CLOSE_IRQ BIT(3)
39 #define SX9360_REG_IRQ_CFG 0x03
41 #define SX9360_REG_GNRL_CTRL0 0x10
42 #define SX9360_REG_GNRL_CTRL0_PHEN_MASK GENMASK(1, 0)
43 #define SX9360_REG_GNRL_CTRL1 0x11
44 #define SX9360_REG_GNRL_CTRL1_SCANPERIOD_MASK GENMASK(2, 0)
45 #define SX9360_REG_GNRL_CTRL2 0x12
46 #define SX9360_REG_GNRL_CTRL2_PERIOD_102MS 0x32
47 #define SX9360_REG_GNRL_REG_2_PERIOD_MS(_r) \
48 (((_r) * 8192) / (SX9360_FOSC_HZ / 1000))
49 #define SX9360_REG_GNRL_FREQ_2_REG(_f) (((_f) * 8192) / SX9360_FOSC_HZ)
50 #define SX9360_REG_GNRL_REG_2_FREQ(_r) (SX9360_FOSC_HZ / ((_r) * 8192))
52 #define SX9360_REG_AFE_CTRL1 0x21
53 #define SX9360_REG_AFE_CTRL1_RESFILTIN_MASK GENMASK(3, 0)
54 #define SX9360_REG_AFE_CTRL1_RESFILTIN_0OHMS 0
55 #define SX9360_REG_AFE_PARAM0_PHR 0x22
56 #define SX9360_REG_AFE_PARAM1_PHR 0x23
57 #define SX9360_REG_AFE_PARAM0_PHM 0x24
58 #define SX9360_REG_AFE_PARAM0_RSVD 0x08
59 #define SX9360_REG_AFE_PARAM0_RESOLUTION_MASK GENMASK(2, 0)
60 #define SX9360_REG_AFE_PARAM0_RESOLUTION_128 0x02
61 #define SX9360_REG_AFE_PARAM1_PHM 0x25
62 #define SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF 0x40
63 #define SX9360_REG_AFE_PARAM1_FREQ_83_33HZ 0x06
65 #define SX9360_REG_PROX_CTRL0_PHR 0x40
66 #define SX9360_REG_PROX_CTRL0_PHM 0x41
67 #define SX9360_REG_PROX_CTRL0_GAIN_MASK GENMASK(5, 3)
68 #define SX9360_REG_PROX_CTRL0_GAIN_1 0x80
69 #define SX9360_REG_PROX_CTRL0_RAWFILT_MASK GENMASK(2, 0)
70 #define SX9360_REG_PROX_CTRL0_RAWFILT_1P50 0x01
71 #define SX9360_REG_PROX_CTRL1 0x42
72 #define SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_MASK GENMASK(5, 3)
73 #define SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_16K 0x20
74 #define SX9360_REG_PROX_CTRL2 0x43
75 #define SX9360_REG_PROX_CTRL2_AVGDEB_MASK GENMASK(7, 6)
76 #define SX9360_REG_PROX_CTRL2_AVGDEB_2SAMPLES 0x40
77 #define SX9360_REG_PROX_CTRL2_AVGPOS_THRESH_16K 0x20
78 #define SX9360_REG_PROX_CTRL3 0x44
79 #define SX9360_REG_PROX_CTRL3_AVGNEG_FILT_MASK GENMASK(5, 3)
80 #define SX9360_REG_PROX_CTRL3_AVGNEG_FILT_2 0x08
81 #define SX9360_REG_PROX_CTRL3_AVGPOS_FILT_MASK GENMASK(2, 0)
82 #define SX9360_REG_PROX_CTRL3_AVGPOS_FILT_256 0x04
83 #define SX9360_REG_PROX_CTRL4 0x45
84 #define SX9360_REG_PROX_CTRL4_HYST_MASK GENMASK(5, 4)
85 #define SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK GENMASK(3, 2)
86 #define SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK GENMASK(1, 0)
87 #define SX9360_REG_PROX_CTRL5 0x46
88 #define SX9360_REG_PROX_CTRL5_PROXTHRESH_32 0x08
90 #define SX9360_REG_REF_CORR0 0x60
91 #define SX9360_REG_REF_CORR1 0x61
93 #define SX9360_REG_USEFUL_PHR_MSB 0x90
94 #define SX9360_REG_USEFUL_PHR_LSB 0x91
96 #define SX9360_REG_OFFSET_PMR_MSB 0x92
97 #define SX9360_REG_OFFSET_PMR_LSB 0x93
99 #define SX9360_REG_USEFUL_PHM_MSB 0x94
100 #define SX9360_REG_USEFUL_PHM_LSB 0x95
102 #define SX9360_REG_AVG_PHM_MSB 0x96
103 #define SX9360_REG_AVG_PHM_LSB 0x97
105 #define SX9360_REG_DIFF_PHM_MSB 0x98
106 #define SX9360_REG_DIFF_PHM_LSB 0x99
108 #define SX9360_REG_OFFSET_PHM_MSB 0x9a
109 #define SX9360_REG_OFFSET_PHM_LSB 0x9b
111 #define SX9360_REG_USE_FILTER_MSB 0x9a
112 #define SX9360_REG_USE_FILTER_LSB 0x9b
114 #define SX9360_REG_RESET 0xcf
115 /* Write this to REG_RESET to do a soft reset. */
116 #define SX9360_SOFT_RESET 0xde
118 #define SX9360_REG_WHOAMI 0xfa
119 #define SX9360_WHOAMI_VALUE 0x60
121 #define SX9360_REG_REVISION 0xfe
123 /* 2 channels, Phase Reference and Measurement. */
124 #define SX9360_NUM_CHANNELS 2
126 static const struct iio_chan_spec sx9360_channels
[] = {
128 .type
= IIO_PROXIMITY
,
129 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
130 BIT(IIO_CHAN_INFO_HARDWAREGAIN
),
131 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
132 .info_mask_separate_available
=
133 BIT(IIO_CHAN_INFO_HARDWAREGAIN
),
134 .info_mask_shared_by_all_available
=
135 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
137 .address
= SX9360_REG_USEFUL_PHR_MSB
,
144 .endianness
= IIO_BE
,
148 .type
= IIO_PROXIMITY
,
149 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
150 BIT(IIO_CHAN_INFO_HARDWAREGAIN
),
151 .info_mask_shared_by_all
= BIT(IIO_CHAN_INFO_SAMP_FREQ
),
152 .info_mask_separate_available
=
153 BIT(IIO_CHAN_INFO_HARDWAREGAIN
),
154 .info_mask_shared_by_all_available
=
155 BIT(IIO_CHAN_INFO_SAMP_FREQ
),
157 .address
= SX9360_REG_USEFUL_PHM_MSB
,
158 .event_spec
= sx_common_events
,
159 .num_event_specs
= ARRAY_SIZE(sx_common_events
),
166 .endianness
= IIO_BE
,
169 IIO_CHAN_SOFT_TIMESTAMP(2),
173 * Each entry contains the integer part (val) and the fractional part, in micro
174 * seconds. It conforms to the IIO output IIO_VAL_INT_PLUS_MICRO.
176 * The frequency control register holds the period, with a ~2ms increment.
177 * Therefore the smallest frequency is 4MHz / (2047 * 8192),
178 * The fastest is 4MHz / 8192.
179 * The interval is not linear, but given there is 2047 possible value,
180 * Returns the fake increment of (Max-Min)/2047
182 static const struct {
185 } sx9360_samp_freq_interval
[] = {
186 { 0, 281250 }, /* 4MHz / (8192 * 2047) */
188 { 448, 281250 }, /* 4MHz / 8192 */
191 static const struct regmap_range sx9360_writable_reg_ranges
[] = {
193 * To set COMPSTAT for compensation, even if datasheet says register is
196 regmap_reg_range(SX9360_REG_STAT
, SX9360_REG_IRQ_CFG
),
197 regmap_reg_range(SX9360_REG_GNRL_CTRL0
, SX9360_REG_GNRL_CTRL2
),
198 regmap_reg_range(SX9360_REG_AFE_CTRL1
, SX9360_REG_AFE_PARAM1_PHM
),
199 regmap_reg_range(SX9360_REG_PROX_CTRL0_PHR
, SX9360_REG_PROX_CTRL5
),
200 regmap_reg_range(SX9360_REG_REF_CORR0
, SX9360_REG_REF_CORR1
),
201 regmap_reg_range(SX9360_REG_OFFSET_PMR_MSB
, SX9360_REG_OFFSET_PMR_LSB
),
202 regmap_reg_range(SX9360_REG_RESET
, SX9360_REG_RESET
),
205 static const struct regmap_access_table sx9360_writeable_regs
= {
206 .yes_ranges
= sx9360_writable_reg_ranges
,
207 .n_yes_ranges
= ARRAY_SIZE(sx9360_writable_reg_ranges
),
211 * All allocated registers are readable, so we just list unallocated
214 static const struct regmap_range sx9360_non_readable_reg_ranges
[] = {
215 regmap_reg_range(SX9360_REG_IRQ_CFG
+ 1, SX9360_REG_GNRL_CTRL0
- 1),
216 regmap_reg_range(SX9360_REG_GNRL_CTRL2
+ 1, SX9360_REG_AFE_CTRL1
- 1),
217 regmap_reg_range(SX9360_REG_AFE_PARAM1_PHM
+ 1,
218 SX9360_REG_PROX_CTRL0_PHR
- 1),
219 regmap_reg_range(SX9360_REG_PROX_CTRL5
+ 1, SX9360_REG_REF_CORR0
- 1),
220 regmap_reg_range(SX9360_REG_REF_CORR1
+ 1,
221 SX9360_REG_USEFUL_PHR_MSB
- 1),
222 regmap_reg_range(SX9360_REG_USE_FILTER_LSB
+ 1, SX9360_REG_RESET
- 1),
223 regmap_reg_range(SX9360_REG_RESET
+ 1, SX9360_REG_WHOAMI
- 1),
224 regmap_reg_range(SX9360_REG_WHOAMI
+ 1, SX9360_REG_REVISION
- 1),
227 static const struct regmap_access_table sx9360_readable_regs
= {
228 .no_ranges
= sx9360_non_readable_reg_ranges
,
229 .n_no_ranges
= ARRAY_SIZE(sx9360_non_readable_reg_ranges
),
232 static const struct regmap_range sx9360_volatile_reg_ranges
[] = {
233 regmap_reg_range(SX9360_REG_IRQ_SRC
, SX9360_REG_STAT
),
234 regmap_reg_range(SX9360_REG_USEFUL_PHR_MSB
, SX9360_REG_USE_FILTER_LSB
),
235 regmap_reg_range(SX9360_REG_WHOAMI
, SX9360_REG_WHOAMI
),
236 regmap_reg_range(SX9360_REG_REVISION
, SX9360_REG_REVISION
),
239 static const struct regmap_access_table sx9360_volatile_regs
= {
240 .yes_ranges
= sx9360_volatile_reg_ranges
,
241 .n_yes_ranges
= ARRAY_SIZE(sx9360_volatile_reg_ranges
),
244 static const struct regmap_config sx9360_regmap_config
= {
248 .max_register
= SX9360_REG_REVISION
,
249 .cache_type
= REGCACHE_RBTREE
,
251 .wr_table
= &sx9360_writeable_regs
,
252 .rd_table
= &sx9360_readable_regs
,
253 .volatile_table
= &sx9360_volatile_regs
,
256 static int sx9360_read_prox_data(struct sx_common_data
*data
,
257 const struct iio_chan_spec
*chan
,
260 return regmap_bulk_read(data
->regmap
, chan
->address
, val
, sizeof(*val
));
264 * If we have no interrupt support, we have to wait for a scan period
265 * after enabling a channel to get a result.
267 static int sx9360_wait_for_sample(struct sx_common_data
*data
)
272 ret
= regmap_bulk_read(data
->regmap
, SX9360_REG_GNRL_CTRL1
,
276 msleep(SX9360_REG_GNRL_REG_2_PERIOD_MS(be16_to_cpu(buf
)));
281 static int sx9360_read_gain(struct sx_common_data
*data
,
282 const struct iio_chan_spec
*chan
, int *val
)
284 unsigned int reg
, regval
;
287 reg
= SX9360_REG_PROX_CTRL0_PHR
+ chan
->channel
;
288 ret
= regmap_read(data
->regmap
, reg
, ®val
);
292 *val
= 1 << FIELD_GET(SX9360_REG_PROX_CTRL0_GAIN_MASK
, regval
);
297 static int sx9360_read_samp_freq(struct sx_common_data
*data
,
303 ret
= regmap_bulk_read(data
->regmap
, SX9360_REG_GNRL_CTRL1
,
307 divisor
= be16_to_cpu(buf
);
313 *val
= SX9360_FOSC_HZ
;
314 *val2
= divisor
* 8192;
316 return IIO_VAL_FRACTIONAL
;
319 static int sx9360_read_raw(struct iio_dev
*indio_dev
,
320 const struct iio_chan_spec
*chan
,
321 int *val
, int *val2
, long mask
)
323 struct sx_common_data
*data
= iio_priv(indio_dev
);
326 case IIO_CHAN_INFO_RAW
:
327 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
)
328 return sx_common_read_proximity(data
, chan
, val
);
330 case IIO_CHAN_INFO_HARDWAREGAIN
:
331 iio_device_claim_direct_scoped(return -EBUSY
, indio_dev
)
332 return sx9360_read_gain(data
, chan
, val
);
334 case IIO_CHAN_INFO_SAMP_FREQ
:
335 return sx9360_read_samp_freq(data
, val
, val2
);
341 static const char *sx9360_channel_labels
[SX9360_NUM_CHANNELS
] = {
345 static int sx9360_read_label(struct iio_dev
*iio_dev
, const struct iio_chan_spec
*chan
,
348 return sysfs_emit(label
, "%s\n", sx9360_channel_labels
[chan
->channel
]);
351 static const int sx9360_gain_vals
[] = { 1, 2, 4, 8 };
353 static int sx9360_read_avail(struct iio_dev
*indio_dev
,
354 struct iio_chan_spec
const *chan
,
355 const int **vals
, int *type
, int *length
,
358 if (chan
->type
!= IIO_PROXIMITY
)
362 case IIO_CHAN_INFO_HARDWAREGAIN
:
364 *length
= ARRAY_SIZE(sx9360_gain_vals
);
365 *vals
= sx9360_gain_vals
;
366 return IIO_AVAIL_LIST
;
367 case IIO_CHAN_INFO_SAMP_FREQ
:
368 *type
= IIO_VAL_INT_PLUS_MICRO
;
369 *length
= ARRAY_SIZE(sx9360_samp_freq_interval
) * 2;
370 *vals
= (int *)sx9360_samp_freq_interval
;
371 return IIO_AVAIL_RANGE
;
377 static int sx9360_set_samp_freq(struct sx_common_data
*data
,
383 reg
= val
* 8192 / SX9360_FOSC_HZ
+ val2
* 8192 / (SX9360_FOSC_MHZ
);
384 buf
= cpu_to_be16(reg
);
385 guard(mutex
)(&data
->mutex
);
387 return regmap_bulk_write(data
->regmap
, SX9360_REG_GNRL_CTRL1
, &buf
,
391 static int sx9360_read_thresh(struct sx_common_data
*data
, int *val
)
396 ret
= regmap_read(data
->regmap
, SX9360_REG_PROX_CTRL5
, ®val
);
403 *val
= (regval
* regval
) / 2;
408 static int sx9360_read_hysteresis(struct sx_common_data
*data
, int *val
)
410 unsigned int regval
, pthresh
;
413 ret
= sx9360_read_thresh(data
, &pthresh
);
417 ret
= regmap_read(data
->regmap
, SX9360_REG_PROX_CTRL4
, ®val
);
421 regval
= FIELD_GET(SX9360_REG_PROX_CTRL4_HYST_MASK
, regval
);
425 *val
= pthresh
>> (5 - regval
);
430 static int sx9360_read_far_debounce(struct sx_common_data
*data
, int *val
)
435 ret
= regmap_read(data
->regmap
, SX9360_REG_PROX_CTRL4
, ®val
);
439 regval
= FIELD_GET(SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK
, regval
);
448 static int sx9360_read_close_debounce(struct sx_common_data
*data
, int *val
)
453 ret
= regmap_read(data
->regmap
, SX9360_REG_PROX_CTRL4
, ®val
);
457 regval
= FIELD_GET(SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK
, regval
);
466 static int sx9360_read_event_val(struct iio_dev
*indio_dev
,
467 const struct iio_chan_spec
*chan
,
468 enum iio_event_type type
,
469 enum iio_event_direction dir
,
470 enum iio_event_info info
, int *val
, int *val2
)
472 struct sx_common_data
*data
= iio_priv(indio_dev
);
474 if (chan
->type
!= IIO_PROXIMITY
)
478 case IIO_EV_INFO_VALUE
:
479 return sx9360_read_thresh(data
, val
);
480 case IIO_EV_INFO_PERIOD
:
482 case IIO_EV_DIR_RISING
:
483 return sx9360_read_far_debounce(data
, val
);
484 case IIO_EV_DIR_FALLING
:
485 return sx9360_read_close_debounce(data
, val
);
489 case IIO_EV_INFO_HYSTERESIS
:
490 return sx9360_read_hysteresis(data
, val
);
496 static int sx9360_write_thresh(struct sx_common_data
*data
, int _val
)
498 unsigned int val
= _val
;
501 val
= int_sqrt(2 * val
);
506 guard(mutex
)(&data
->mutex
);
507 return regmap_write(data
->regmap
, SX9360_REG_PROX_CTRL5
, val
);
510 static int sx9360_write_hysteresis(struct sx_common_data
*data
, int _val
)
512 unsigned int hyst
, val
= _val
;
515 ret
= sx9360_read_thresh(data
, &pthresh
);
521 else if (val
>= pthresh
>> 2)
523 else if (val
>= pthresh
>> 3)
525 else if (val
>= pthresh
>> 4)
530 hyst
= FIELD_PREP(SX9360_REG_PROX_CTRL4_HYST_MASK
, hyst
);
531 guard(mutex
)(&data
->mutex
);
532 return regmap_update_bits(data
->regmap
, SX9360_REG_PROX_CTRL4
,
533 SX9360_REG_PROX_CTRL4_HYST_MASK
, hyst
);
536 static int sx9360_write_far_debounce(struct sx_common_data
*data
, int _val
)
538 unsigned int regval
, val
= _val
;
542 if (!FIELD_FIT(SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK
, val
))
545 regval
= FIELD_PREP(SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK
, val
);
547 guard(mutex
)(&data
->mutex
);
548 return regmap_update_bits(data
->regmap
, SX9360_REG_PROX_CTRL4
,
549 SX9360_REG_PROX_CTRL4_FAR_DEBOUNCE_MASK
,
553 static int sx9360_write_close_debounce(struct sx_common_data
*data
, int _val
)
555 unsigned int regval
, val
= _val
;
559 if (!FIELD_FIT(SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK
, val
))
562 regval
= FIELD_PREP(SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK
, val
);
564 guard(mutex
)(&data
->mutex
);
565 return regmap_update_bits(data
->regmap
, SX9360_REG_PROX_CTRL4
,
566 SX9360_REG_PROX_CTRL4_CLOSE_DEBOUNCE_MASK
,
570 static int sx9360_write_event_val(struct iio_dev
*indio_dev
,
571 const struct iio_chan_spec
*chan
,
572 enum iio_event_type type
,
573 enum iio_event_direction dir
,
574 enum iio_event_info info
, int val
, int val2
)
576 struct sx_common_data
*data
= iio_priv(indio_dev
);
578 if (chan
->type
!= IIO_PROXIMITY
)
582 case IIO_EV_INFO_VALUE
:
583 return sx9360_write_thresh(data
, val
);
584 case IIO_EV_INFO_PERIOD
:
586 case IIO_EV_DIR_RISING
:
587 return sx9360_write_far_debounce(data
, val
);
588 case IIO_EV_DIR_FALLING
:
589 return sx9360_write_close_debounce(data
, val
);
593 case IIO_EV_INFO_HYSTERESIS
:
594 return sx9360_write_hysteresis(data
, val
);
600 static int sx9360_write_gain(struct sx_common_data
*data
,
601 const struct iio_chan_spec
*chan
, int val
)
603 unsigned int gain
, reg
;
606 reg
= SX9360_REG_PROX_CTRL0_PHR
+ chan
->channel
;
607 gain
= FIELD_PREP(SX9360_REG_PROX_CTRL0_GAIN_MASK
, gain
);
609 guard(mutex
)(&data
->mutex
);
610 return regmap_update_bits(data
->regmap
, reg
,
611 SX9360_REG_PROX_CTRL0_GAIN_MASK
,
615 static int sx9360_write_raw(struct iio_dev
*indio_dev
,
616 const struct iio_chan_spec
*chan
, int val
, int val2
,
619 struct sx_common_data
*data
= iio_priv(indio_dev
);
622 case IIO_CHAN_INFO_SAMP_FREQ
:
623 return sx9360_set_samp_freq(data
, val
, val2
);
624 case IIO_CHAN_INFO_HARDWAREGAIN
:
625 return sx9360_write_gain(data
, chan
, val
);
631 static const struct sx_common_reg_default sx9360_default_regs
[] = {
632 { SX9360_REG_IRQ_MSK
, 0x00 },
633 { SX9360_REG_IRQ_CFG
, 0x00, "irq_cfg" },
635 * The lower 2 bits should not be set as it enable sensors measurements.
636 * Turning the detection on before the configuration values are set to
637 * good values can cause the device to return erroneous readings.
639 { SX9360_REG_GNRL_CTRL0
, 0x00, "gnrl_ctrl0" },
640 { SX9360_REG_GNRL_CTRL1
, 0x00, "gnrl_ctrl1" },
641 { SX9360_REG_GNRL_CTRL2
, SX9360_REG_GNRL_CTRL2_PERIOD_102MS
, "gnrl_ctrl2" },
643 { SX9360_REG_AFE_CTRL1
, SX9360_REG_AFE_CTRL1_RESFILTIN_0OHMS
, "afe_ctrl0" },
644 { SX9360_REG_AFE_PARAM0_PHR
, SX9360_REG_AFE_PARAM0_RSVD
|
645 SX9360_REG_AFE_PARAM0_RESOLUTION_128
, "afe_param0_phr" },
646 { SX9360_REG_AFE_PARAM1_PHR
, SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF
|
647 SX9360_REG_AFE_PARAM1_FREQ_83_33HZ
, "afe_param1_phr" },
648 { SX9360_REG_AFE_PARAM0_PHM
, SX9360_REG_AFE_PARAM0_RSVD
|
649 SX9360_REG_AFE_PARAM0_RESOLUTION_128
, "afe_param0_phm" },
650 { SX9360_REG_AFE_PARAM1_PHM
, SX9360_REG_AFE_PARAM1_AGAIN_PHM_6PF
|
651 SX9360_REG_AFE_PARAM1_FREQ_83_33HZ
, "afe_param1_phm" },
653 { SX9360_REG_PROX_CTRL0_PHR
, SX9360_REG_PROX_CTRL0_GAIN_1
|
654 SX9360_REG_PROX_CTRL0_RAWFILT_1P50
, "prox_ctrl0_phr" },
655 { SX9360_REG_PROX_CTRL0_PHM
, SX9360_REG_PROX_CTRL0_GAIN_1
|
656 SX9360_REG_PROX_CTRL0_RAWFILT_1P50
, "prox_ctrl0_phm" },
657 { SX9360_REG_PROX_CTRL1
, SX9360_REG_PROX_CTRL1_AVGNEG_THRESH_16K
, "prox_ctrl1" },
658 { SX9360_REG_PROX_CTRL2
, SX9360_REG_PROX_CTRL2_AVGDEB_2SAMPLES
|
659 SX9360_REG_PROX_CTRL2_AVGPOS_THRESH_16K
, "prox_ctrl2" },
660 { SX9360_REG_PROX_CTRL3
, SX9360_REG_PROX_CTRL3_AVGNEG_FILT_2
|
661 SX9360_REG_PROX_CTRL3_AVGPOS_FILT_256
, "prox_ctrl3" },
662 { SX9360_REG_PROX_CTRL4
, 0x00, "prox_ctrl4" },
663 { SX9360_REG_PROX_CTRL5
, SX9360_REG_PROX_CTRL5_PROXTHRESH_32
, "prox_ctrl5" },
666 /* Activate all channels and perform an initial compensation. */
667 static int sx9360_init_compensation(struct iio_dev
*indio_dev
)
669 struct sx_common_data
*data
= iio_priv(indio_dev
);
673 /* run the compensation phase on all channels */
674 ret
= regmap_set_bits(data
->regmap
, SX9360_REG_STAT
,
675 SX9360_REG_STAT_COMPSTAT_MASK
);
679 return regmap_read_poll_timeout(data
->regmap
, SX9360_REG_STAT
, val
,
680 !(val
& SX9360_REG_STAT_COMPSTAT_MASK
),
684 static const struct sx_common_reg_default
*
685 sx9360_get_default_reg(struct device
*dev
, int idx
,
686 struct sx_common_reg_default
*reg_def
)
688 u32 raw
= 0, pos
= 0;
691 memcpy(reg_def
, &sx9360_default_regs
[idx
], sizeof(*reg_def
));
692 switch (reg_def
->reg
) {
693 case SX9360_REG_AFE_CTRL1
:
694 ret
= device_property_read_u32(dev
,
695 "semtech,input-precharge-resistor-ohms",
700 reg_def
->def
&= ~SX9360_REG_AFE_CTRL1_RESFILTIN_MASK
;
701 reg_def
->def
|= FIELD_PREP(SX9360_REG_AFE_CTRL1_RESFILTIN_MASK
,
704 case SX9360_REG_AFE_PARAM0_PHR
:
705 case SX9360_REG_AFE_PARAM0_PHM
:
706 ret
= device_property_read_u32(dev
, "semtech,resolution", &raw
);
710 raw
= ilog2(raw
) - 3;
712 reg_def
->def
&= ~SX9360_REG_AFE_PARAM0_RESOLUTION_MASK
;
713 reg_def
->def
|= FIELD_PREP(SX9360_REG_AFE_PARAM0_RESOLUTION_MASK
, raw
);
715 case SX9360_REG_PROX_CTRL0_PHR
:
716 case SX9360_REG_PROX_CTRL0_PHM
:
717 ret
= device_property_read_u32(dev
, "semtech,proxraw-strength", &raw
);
721 reg_def
->def
&= ~SX9360_REG_PROX_CTRL0_RAWFILT_MASK
;
722 reg_def
->def
|= FIELD_PREP(SX9360_REG_PROX_CTRL0_RAWFILT_MASK
, raw
);
724 case SX9360_REG_PROX_CTRL3
:
725 ret
= device_property_read_u32(dev
, "semtech,avg-pos-strength",
730 /* Powers of 2, except for a gap between 16 and 64 */
731 raw
= clamp(ilog2(pos
), 3, 11) - (pos
>= 32 ? 4 : 3);
732 reg_def
->def
&= ~SX9360_REG_PROX_CTRL3_AVGPOS_FILT_MASK
;
733 reg_def
->def
|= FIELD_PREP(SX9360_REG_PROX_CTRL3_AVGPOS_FILT_MASK
, raw
);
740 static int sx9360_check_whoami(struct device
*dev
, struct iio_dev
*indio_dev
)
743 * Only one sensor for this driver. Assuming the device tree
744 * is correct, just set the sensor name.
746 indio_dev
->name
= "sx9360";
750 static const struct sx_common_chip_info sx9360_chip_info
= {
751 .reg_stat
= SX9360_REG_STAT
,
752 .reg_irq_msk
= SX9360_REG_IRQ_MSK
,
753 .reg_enable_chan
= SX9360_REG_GNRL_CTRL0
,
754 .reg_reset
= SX9360_REG_RESET
,
756 .mask_enable_chan
= SX9360_REG_GNRL_CTRL0_PHEN_MASK
,
758 .num_channels
= SX9360_NUM_CHANNELS
,
759 .num_default_regs
= ARRAY_SIZE(sx9360_default_regs
),
762 .read_prox_data
= sx9360_read_prox_data
,
763 .check_whoami
= sx9360_check_whoami
,
764 .init_compensation
= sx9360_init_compensation
,
765 .wait_for_sample
= sx9360_wait_for_sample
,
766 .get_default_reg
= sx9360_get_default_reg
,
769 .iio_channels
= sx9360_channels
,
770 .num_iio_channels
= ARRAY_SIZE(sx9360_channels
),
772 .read_raw
= sx9360_read_raw
,
773 .read_avail
= sx9360_read_avail
,
774 .read_label
= sx9360_read_label
,
775 .read_event_value
= sx9360_read_event_val
,
776 .write_event_value
= sx9360_write_event_val
,
777 .write_raw
= sx9360_write_raw
,
778 .read_event_config
= sx_common_read_event_config
,
779 .write_event_config
= sx_common_write_event_config
,
783 static int sx9360_probe(struct i2c_client
*client
)
785 return sx_common_probe(client
, &sx9360_chip_info
, &sx9360_regmap_config
);
788 static int sx9360_suspend(struct device
*dev
)
790 struct sx_common_data
*data
= iio_priv(dev_get_drvdata(dev
));
794 disable_irq_nosync(data
->client
->irq
);
796 guard(mutex
)(&data
->mutex
);
797 ret
= regmap_read(data
->regmap
, SX9360_REG_GNRL_CTRL0
, ®val
);
802 FIELD_GET(SX9360_REG_GNRL_CTRL0_PHEN_MASK
, regval
);
805 /* Disable all phases, send the device to sleep. */
806 return regmap_write(data
->regmap
, SX9360_REG_GNRL_CTRL0
, 0);
809 static int sx9360_resume(struct device
*dev
)
811 struct sx_common_data
*data
= iio_priv(dev_get_drvdata(dev
));
813 scoped_guard(mutex
, &data
->mutex
) {
814 int ret
= regmap_update_bits(data
->regmap
,
815 SX9360_REG_GNRL_CTRL0
,
816 SX9360_REG_GNRL_CTRL0_PHEN_MASK
,
821 enable_irq(data
->client
->irq
);
825 static DEFINE_SIMPLE_DEV_PM_OPS(sx9360_pm_ops
, sx9360_suspend
, sx9360_resume
);
827 static const struct acpi_device_id sx9360_acpi_match
[] = {
828 { "STH9360", SX9360_WHOAMI_VALUE
},
829 { "SAMM0208", SX9360_WHOAMI_VALUE
},
832 MODULE_DEVICE_TABLE(acpi
, sx9360_acpi_match
);
834 static const struct of_device_id sx9360_of_match
[] = {
835 { .compatible
= "semtech,sx9360", (void *)SX9360_WHOAMI_VALUE
},
838 MODULE_DEVICE_TABLE(of
, sx9360_of_match
);
840 static const struct i2c_device_id sx9360_id
[] = {
841 {"sx9360", SX9360_WHOAMI_VALUE
},
844 MODULE_DEVICE_TABLE(i2c
, sx9360_id
);
846 static struct i2c_driver sx9360_driver
= {
849 .acpi_match_table
= sx9360_acpi_match
,
850 .of_match_table
= sx9360_of_match
,
851 .pm
= pm_sleep_ptr(&sx9360_pm_ops
),
854 * Lots of i2c transfers in probe + over 200 ms waiting in
855 * sx9360_init_compensation() mean a slow probe; prefer async
856 * so we don't delay boot if we're builtin to the kernel.
858 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
860 .probe
= sx9360_probe
,
861 .id_table
= sx9360_id
,
863 module_i2c_driver(sx9360_driver
);
865 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
866 MODULE_DESCRIPTION("Driver for Semtech SX9360 proximity sensor");
867 MODULE_LICENSE("GPL v2");
868 MODULE_IMPORT_NS(SEMTECH_PROX
);