1 // SPDX-License-Identifier: GPL-2.0+
3 * ADXL380 3-Axis Digital Accelerometer core driver
5 * Copyright 2024 Analog Devices Inc.
8 #include <linux/bitfield.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/module.h>
12 #include <linux/property.h>
13 #include <linux/regmap.h>
14 #include <linux/units.h>
16 #include <linux/unaligned.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/events.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/kfifo_buf.h>
22 #include <linux/iio/sysfs.h>
24 #include <linux/regulator/consumer.h>
28 #define ADXL380_ID_VAL 380
29 #define ADXL382_ID_VAL 382
31 #define ADXL380_DEVID_AD_REG 0x00
32 #define ADLX380_PART_ID_REG 0x02
34 #define ADXL380_X_DATA_H_REG 0x15
35 #define ADXL380_Y_DATA_H_REG 0x17
36 #define ADXL380_Z_DATA_H_REG 0x19
37 #define ADXL380_T_DATA_H_REG 0x1B
39 #define ADXL380_MISC_0_REG 0x20
40 #define ADXL380_XL382_MSK BIT(7)
42 #define ADXL380_MISC_1_REG 0x21
44 #define ADXL380_X_DSM_OFFSET_REG 0x4D
46 #define ADXL380_ACT_INACT_CTL_REG 0x37
47 #define ADXL380_INACT_EN_MSK BIT(2)
48 #define ADXL380_ACT_EN_MSK BIT(0)
50 #define ADXL380_SNSR_AXIS_EN_REG 0x38
51 #define ADXL380_ACT_INACT_AXIS_EN_MSK GENMASK(2, 0)
53 #define ADXL380_THRESH_ACT_H_REG 0x39
54 #define ADXL380_TIME_ACT_H_REG 0x3B
55 #define ADXL380_THRESH_INACT_H_REG 0x3E
56 #define ADXL380_TIME_INACT_H_REG 0x40
57 #define ADXL380_THRESH_MAX GENMASK(12, 0)
58 #define ADXL380_TIME_MAX GENMASK(24, 0)
60 #define ADXL380_FIFO_CONFIG_0_REG 0x30
61 #define ADXL380_FIFO_SAMPLES_8_MSK BIT(0)
62 #define ADXL380_FIFO_MODE_MSK GENMASK(5, 4)
64 #define ADXL380_FIFO_DISABLED 0
65 #define ADXL380_FIFO_NORMAL 1
66 #define ADXL380_FIFO_STREAMED 2
67 #define ADXL380_FIFO_TRIGGERED 3
69 #define ADXL380_FIFO_CONFIG_1_REG 0x31
70 #define ADXL380_FIFO_STATUS_0_REG 0x1E
72 #define ADXL380_TAP_THRESH_REG 0x43
73 #define ADXL380_TAP_DUR_REG 0x44
74 #define ADXL380_TAP_LATENT_REG 0x45
75 #define ADXL380_TAP_WINDOW_REG 0x46
76 #define ADXL380_TAP_TIME_MAX GENMASK(7, 0)
78 #define ADXL380_TAP_CFG_REG 0x47
79 #define ADXL380_TAP_AXIS_MSK GENMASK(1, 0)
81 #define ADXL380_TRIG_CFG_REG 0x49
82 #define ADXL380_TRIG_CFG_DEC_2X_MSK BIT(7)
83 #define ADXL380_TRIG_CFG_SINC_RATE_MSK BIT(6)
85 #define ADXL380_FILTER_REG 0x50
86 #define ADXL380_FILTER_EQ_FILT_MSK BIT(6)
87 #define ADXL380_FILTER_LPF_MODE_MSK GENMASK(5, 4)
88 #define ADXL380_FILTER_HPF_PATH_MSK BIT(3)
89 #define ADXL380_FILTER_HPF_CORNER_MSK GENMASK(2, 0)
91 #define ADXL380_OP_MODE_REG 0x26
92 #define ADXL380_OP_MODE_RANGE_MSK GENMASK(7, 6)
93 #define ADXL380_OP_MODE_MSK GENMASK(3, 0)
94 #define ADXL380_OP_MODE_STANDBY 0
95 #define ADXL380_OP_MODE_HEART_SOUND 1
96 #define ADXL380_OP_MODE_ULP 2
97 #define ADXL380_OP_MODE_VLP 3
98 #define ADXL380_OP_MODE_LP 4
99 #define ADXL380_OP_MODE_LP_ULP 6
100 #define ADXL380_OP_MODE_LP_VLP 7
101 #define ADXL380_OP_MODE_RBW 8
102 #define ADXL380_OP_MODE_RBW_ULP 10
103 #define ADXL380_OP_MODE_RBW_VLP 11
104 #define ADXL380_OP_MODE_HP 12
105 #define ADXL380_OP_MODE_HP_ULP 14
106 #define ADXL380_OP_MODE_HP_VLP 15
108 #define ADXL380_OP_MODE_4G_RANGE 0
109 #define ADXL382_OP_MODE_15G_RANGE 0
110 #define ADXL380_OP_MODE_8G_RANGE 1
111 #define ADXL382_OP_MODE_30G_RANGE 1
112 #define ADXL380_OP_MODE_16G_RANGE 2
113 #define ADXL382_OP_MODE_60G_RANGE 2
115 #define ADXL380_DIG_EN_REG 0x27
116 #define ADXL380_CHAN_EN_MSK(chan) BIT(4 + (chan))
117 #define ADXL380_FIFO_EN_MSK BIT(3)
119 #define ADXL380_INT0_MAP0_REG 0x2B
120 #define ADXL380_INT1_MAP0_REG 0x2D
121 #define ADXL380_INT_MAP0_INACT_INT0_MSK BIT(6)
122 #define ADXL380_INT_MAP0_ACT_INT0_MSK BIT(5)
123 #define ADXL380_INT_MAP0_FIFO_WM_INT0_MSK BIT(3)
125 #define ADXL380_INT0_MAP1_REG 0x2C
126 #define ADXL380_INT1_MAP1_REG 0x2E
127 #define ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK BIT(1)
128 #define ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK BIT(0)
130 #define ADXL380_INT0_REG 0x5D
131 #define ADXL380_INT0_POL_MSK BIT(7)
133 #define ADXL380_RESET_REG 0x2A
134 #define ADXL380_FIFO_DATA 0x1D
136 #define ADXL380_DEVID_AD_VAL 0xAD
137 #define ADXL380_RESET_CODE 0x52
139 #define ADXL380_STATUS_0_REG 0x11
140 #define ADXL380_STATUS_0_FIFO_FULL_MSK BIT(1)
141 #define ADXL380_STATUS_0_FIFO_WM_MSK BIT(3)
143 #define ADXL380_STATUS_1_INACT_MSK BIT(6)
144 #define ADXL380_STATUS_1_ACT_MSK BIT(5)
145 #define ADXL380_STATUS_1_DOUBLE_TAP_MSK BIT(1)
146 #define ADXL380_STATUS_1_SINGLE_TAP_MSK BIT(0)
148 #define ADXL380_FIFO_SAMPLES 315UL
150 enum adxl380_channels
{
164 enum adxl380_activity_type
{
169 enum adxl380_tap_type
{
174 enum adxl380_tap_time_type
{
175 ADXL380_TAP_TIME_LATENT
,
176 ADXL380_TAP_TIME_WINDOW
,
179 static const int adxl380_range_scale_factor_tbl
[] = { 1, 2, 4 };
181 const struct adxl380_chip_info adxl380_chip_info
= {
183 .chip_id
= ADXL380_ID_VAL
,
185 [ADXL380_OP_MODE_4G_RANGE
] = { 0, 1307226 },
186 [ADXL380_OP_MODE_8G_RANGE
] = { 0, 2615434 },
187 [ADXL380_OP_MODE_16G_RANGE
] = { 0, 5229886 },
189 .samp_freq_tbl
= { 8000, 16000, 32000 },
191 * The datasheet defines an intercept of 470 LSB at 25 degC
192 * and a sensitivity of 10.2 LSB/C.
194 .temp_offset
= 25 * 102 / 10 - 470,
197 EXPORT_SYMBOL_NS_GPL(adxl380_chip_info
, IIO_ADXL380
);
199 const struct adxl380_chip_info adxl382_chip_info
= {
201 .chip_id
= ADXL382_ID_VAL
,
203 [ADXL382_OP_MODE_15G_RANGE
] = { 0, 4903325 },
204 [ADXL382_OP_MODE_30G_RANGE
] = { 0, 9806650 },
205 [ADXL382_OP_MODE_60G_RANGE
] = { 0, 19613300 },
207 .samp_freq_tbl
= { 16000, 32000, 64000 },
209 * The datasheet defines an intercept of 570 LSB at 25 degC
210 * and a sensitivity of 10.2 LSB/C.
212 .temp_offset
= 25 * 102 / 10 - 570,
214 EXPORT_SYMBOL_NS_GPL(adxl382_chip_info
, IIO_ADXL380
);
216 static const unsigned int adxl380_th_reg_high_addr
[2] = {
217 [ADXL380_ACTIVITY
] = ADXL380_THRESH_ACT_H_REG
,
218 [ADXL380_INACTIVITY
] = ADXL380_THRESH_INACT_H_REG
,
221 static const unsigned int adxl380_time_reg_high_addr
[2] = {
222 [ADXL380_ACTIVITY
] = ADXL380_TIME_ACT_H_REG
,
223 [ADXL380_INACTIVITY
] = ADXL380_TIME_INACT_H_REG
,
226 static const unsigned int adxl380_tap_time_reg
[2] = {
227 [ADXL380_TAP_TIME_LATENT
] = ADXL380_TAP_LATENT_REG
,
228 [ADXL380_TAP_TIME_WINDOW
] = ADXL380_TAP_WINDOW_REG
,
231 struct adxl380_state
{
232 struct regmap
*regmap
;
234 const struct adxl380_chip_info
*chip_info
;
236 * Synchronize access to members of driver state, and ensure atomicity
237 * of consecutive regmap operations.
240 enum adxl380_axis tap_axis_en
;
259 __be16 fifo_buf
[ADXL380_FIFO_SAMPLES
] __aligned(IIO_DMA_MINALIGN
);
262 bool adxl380_readable_noinc_reg(struct device
*dev
, unsigned int reg
)
264 return reg
== ADXL380_FIFO_DATA
;
266 EXPORT_SYMBOL_NS_GPL(adxl380_readable_noinc_reg
, IIO_ADXL380
);
268 static int adxl380_set_measure_en(struct adxl380_state
*st
, bool en
)
271 unsigned int act_inact_ctl
;
272 u8 op_mode
= ADXL380_OP_MODE_STANDBY
;
275 ret
= regmap_read(st
->regmap
, ADXL380_ACT_INACT_CTL_REG
, &act_inact_ctl
);
279 /* Activity/ Inactivity detection available only in VLP/ULP mode */
280 if (FIELD_GET(ADXL380_ACT_EN_MSK
, act_inact_ctl
) ||
281 FIELD_GET(ADXL380_INACT_EN_MSK
, act_inact_ctl
))
282 op_mode
= ADXL380_OP_MODE_VLP
;
284 op_mode
= ADXL380_OP_MODE_HP
;
287 return regmap_update_bits(st
->regmap
, ADXL380_OP_MODE_REG
,
289 FIELD_PREP(ADXL380_OP_MODE_MSK
, op_mode
));
292 static void adxl380_scale_act_inact_thresholds(struct adxl380_state
*st
,
296 st
->act_threshold
= mult_frac(st
->act_threshold
,
297 adxl380_range_scale_factor_tbl
[old_range
],
298 adxl380_range_scale_factor_tbl
[new_range
]);
299 st
->inact_threshold
= mult_frac(st
->inact_threshold
,
300 adxl380_range_scale_factor_tbl
[old_range
],
301 adxl380_range_scale_factor_tbl
[new_range
]);
304 static int adxl380_write_act_inact_threshold(struct adxl380_state
*st
,
305 enum adxl380_activity_type act
,
309 u8 reg
= adxl380_th_reg_high_addr
[act
];
311 if (th
> ADXL380_THRESH_MAX
)
314 ret
= regmap_write(st
->regmap
, reg
+ 1, th
& GENMASK(7, 0));
318 ret
= regmap_update_bits(st
->regmap
, reg
, GENMASK(2, 0), th
>> 8);
322 if (act
== ADXL380_ACTIVITY
)
323 st
->act_threshold
= th
;
325 st
->inact_threshold
= th
;
330 static int adxl380_set_act_inact_threshold(struct iio_dev
*indio_dev
,
331 enum adxl380_activity_type act
,
334 struct adxl380_state
*st
= iio_priv(indio_dev
);
337 guard(mutex
)(&st
->lock
);
339 ret
= adxl380_set_measure_en(st
, false);
343 ret
= adxl380_write_act_inact_threshold(st
, act
, th
);
347 return adxl380_set_measure_en(st
, true);
350 static int adxl380_set_tap_threshold_value(struct iio_dev
*indio_dev
, u8 th
)
353 struct adxl380_state
*st
= iio_priv(indio_dev
);
355 guard(mutex
)(&st
->lock
);
357 ret
= adxl380_set_measure_en(st
, false);
361 ret
= regmap_write(st
->regmap
, ADXL380_TAP_THRESH_REG
, th
);
365 st
->tap_threshold
= th
;
367 return adxl380_set_measure_en(st
, true);
370 static int _adxl380_write_tap_time_us(struct adxl380_state
*st
,
371 enum adxl380_tap_time_type tap_time_type
,
374 u8 reg
= adxl380_tap_time_reg
[tap_time_type
];
375 unsigned int reg_val
;
378 /* scale factor for tap window is 1250us / LSB */
379 reg_val
= DIV_ROUND_CLOSEST(us
, 1250);
380 if (reg_val
> ADXL380_TAP_TIME_MAX
)
381 reg_val
= ADXL380_TAP_TIME_MAX
;
383 ret
= regmap_write(st
->regmap
, reg
, reg_val
);
387 if (tap_time_type
== ADXL380_TAP_TIME_WINDOW
)
388 st
->tap_window_us
= us
;
390 st
->tap_latent_us
= us
;
395 static int adxl380_write_tap_time_us(struct adxl380_state
*st
,
396 enum adxl380_tap_time_type tap_time_type
, u32 us
)
400 guard(mutex
)(&st
->lock
);
402 ret
= adxl380_set_measure_en(st
, false);
406 ret
= _adxl380_write_tap_time_us(st
, tap_time_type
, us
);
410 return adxl380_set_measure_en(st
, true);
413 static int adxl380_write_tap_dur_us(struct iio_dev
*indio_dev
, u32 us
)
416 unsigned int reg_val
;
417 struct adxl380_state
*st
= iio_priv(indio_dev
);
419 /* 625us per code is the scale factor of TAP_DUR register */
420 reg_val
= DIV_ROUND_CLOSEST(us
, 625);
422 ret
= adxl380_set_measure_en(st
, false);
426 ret
= regmap_write(st
->regmap
, ADXL380_TAP_DUR_REG
, reg_val
);
430 return adxl380_set_measure_en(st
, true);
433 static int adxl380_read_chn(struct adxl380_state
*st
, u8 addr
)
437 guard(mutex
)(&st
->lock
);
439 ret
= regmap_bulk_read(st
->regmap
, addr
, &st
->transf_buf
, 2);
443 return get_unaligned_be16(st
->transf_buf
);
446 static int adxl380_get_odr(struct adxl380_state
*st
, int *odr
)
449 unsigned int trig_cfg
, odr_idx
;
451 ret
= regmap_read(st
->regmap
, ADXL380_TRIG_CFG_REG
, &trig_cfg
);
455 odr_idx
= (FIELD_GET(ADXL380_TRIG_CFG_SINC_RATE_MSK
, trig_cfg
) << 1) |
456 (FIELD_GET(ADXL380_TRIG_CFG_DEC_2X_MSK
, trig_cfg
) & 1);
458 *odr
= st
->chip_info
->samp_freq_tbl
[odr_idx
];
463 static const int adxl380_lpf_div
[] = {
467 static int adxl380_fill_lpf_tbl(struct adxl380_state
*st
)
472 ret
= adxl380_get_odr(st
, &odr
);
476 for (i
= 0; i
< ARRAY_SIZE(st
->lpf_tbl
); i
++)
477 st
->lpf_tbl
[i
] = DIV_ROUND_CLOSEST(odr
, adxl380_lpf_div
[i
]);
482 static const int adxl380_hpf_mul
[] = {
483 0, 247000, 62084, 15545, 3862, 954, 238,
486 static int adxl380_fill_hpf_tbl(struct adxl380_state
*st
)
492 ret
= adxl380_get_odr(st
, &odr_hz
);
496 for (i
= 0; i
< ARRAY_SIZE(adxl380_hpf_mul
); i
++) {
497 odr
= mul_u64_u32_shr(odr_hz
, MEGA
, 0);
498 multiplier
= adxl380_hpf_mul
[i
];
499 div
= div64_u64_rem(mul_u64_u32_shr(odr
, multiplier
, 0),
502 st
->hpf_tbl
[i
][0] = div
;
503 st
->hpf_tbl
[i
][1] = div_u64(rem
, MEGA
* 100);
509 static int adxl380_set_odr(struct adxl380_state
*st
, u8 odr
)
513 guard(mutex
)(&st
->lock
);
515 ret
= adxl380_set_measure_en(st
, false);
519 ret
= regmap_update_bits(st
->regmap
, ADXL380_TRIG_CFG_REG
,
520 ADXL380_TRIG_CFG_DEC_2X_MSK
,
521 FIELD_PREP(ADXL380_TRIG_CFG_DEC_2X_MSK
, odr
& 1));
525 ret
= regmap_update_bits(st
->regmap
, ADXL380_TRIG_CFG_REG
,
526 ADXL380_TRIG_CFG_SINC_RATE_MSK
,
527 FIELD_PREP(ADXL380_TRIG_CFG_SINC_RATE_MSK
, odr
>> 1));
531 ret
= adxl380_set_measure_en(st
, true);
535 ret
= adxl380_fill_lpf_tbl(st
);
539 return adxl380_fill_hpf_tbl(st
);
542 static int adxl380_find_match_1d_tbl(const int *array
, unsigned int size
,
547 for (i
= 0; i
< size
; i
++) {
555 static int adxl380_find_match_2d_tbl(const int (*freq_tbl
)[2], int n
, int val
, int val2
)
559 for (i
= 0; i
< n
; i
++) {
560 if (freq_tbl
[i
][0] == val
&& freq_tbl
[i
][1] == val2
)
567 static int adxl380_get_lpf(struct adxl380_state
*st
, int *lpf
)
570 unsigned int trig_cfg
, lpf_idx
;
572 guard(mutex
)(&st
->lock
);
574 ret
= regmap_read(st
->regmap
, ADXL380_FILTER_REG
, &trig_cfg
);
578 lpf_idx
= FIELD_GET(ADXL380_FILTER_LPF_MODE_MSK
, trig_cfg
);
580 *lpf
= st
->lpf_tbl
[lpf_idx
];
585 static int adxl380_set_lpf(struct adxl380_state
*st
, u8 lpf
)
590 guard(mutex
)(&st
->lock
);
592 ret
= adxl380_set_measure_en(st
, false);
599 ret
= regmap_update_bits(st
->regmap
, ADXL380_FILTER_REG
,
600 ADXL380_FILTER_EQ_FILT_MSK
,
601 FIELD_PREP(ADXL380_FILTER_EQ_FILT_MSK
, eq_bypass
));
605 ret
= regmap_update_bits(st
->regmap
, ADXL380_FILTER_REG
,
606 ADXL380_FILTER_LPF_MODE_MSK
,
607 FIELD_PREP(ADXL380_FILTER_LPF_MODE_MSK
, lpf
));
611 return adxl380_set_measure_en(st
, true);
614 static int adxl380_get_hpf(struct adxl380_state
*st
, int *hpf_int
, int *hpf_frac
)
617 unsigned int trig_cfg
, hpf_idx
;
619 guard(mutex
)(&st
->lock
);
621 ret
= regmap_read(st
->regmap
, ADXL380_FILTER_REG
, &trig_cfg
);
625 hpf_idx
= FIELD_GET(ADXL380_FILTER_HPF_CORNER_MSK
, trig_cfg
);
627 *hpf_int
= st
->hpf_tbl
[hpf_idx
][0];
628 *hpf_frac
= st
->hpf_tbl
[hpf_idx
][1];
633 static int adxl380_set_hpf(struct adxl380_state
*st
, u8 hpf
)
638 guard(mutex
)(&st
->lock
);
640 ret
= adxl380_set_measure_en(st
, false);
647 ret
= regmap_update_bits(st
->regmap
, ADXL380_FILTER_REG
,
648 ADXL380_FILTER_HPF_PATH_MSK
,
649 FIELD_PREP(ADXL380_FILTER_HPF_PATH_MSK
, hpf_path
));
653 ret
= regmap_update_bits(st
->regmap
, ADXL380_FILTER_REG
,
654 ADXL380_FILTER_HPF_CORNER_MSK
,
655 FIELD_PREP(ADXL380_FILTER_HPF_CORNER_MSK
, hpf
));
659 return adxl380_set_measure_en(st
, true);
662 static int _adxl380_set_act_inact_time_ms(struct adxl380_state
*st
,
663 enum adxl380_activity_type act
,
666 u8 reg
= adxl380_time_reg_high_addr
[act
];
667 unsigned int reg_val
;
670 /* 500us per code is the scale factor of TIME_ACT / TIME_INACT registers */
671 reg_val
= min(DIV_ROUND_CLOSEST(ms
* 1000, 500), ADXL380_TIME_MAX
);
673 put_unaligned_be24(reg_val
, &st
->transf_buf
[0]);
675 ret
= regmap_bulk_write(st
->regmap
, reg
, st
->transf_buf
, sizeof(st
->transf_buf
));
679 if (act
== ADXL380_ACTIVITY
)
680 st
->act_time_ms
= ms
;
682 st
->inact_time_ms
= ms
;
687 static int adxl380_set_act_inact_time_ms(struct adxl380_state
*st
,
688 enum adxl380_activity_type act
,
693 guard(mutex
)(&st
->lock
);
695 ret
= adxl380_set_measure_en(st
, false);
699 ret
= _adxl380_set_act_inact_time_ms(st
, act
, ms
);
703 return adxl380_set_measure_en(st
, true);
706 static int adxl380_set_range(struct adxl380_state
*st
, u8 range
)
710 guard(mutex
)(&st
->lock
);
712 ret
= adxl380_set_measure_en(st
, false);
716 ret
= regmap_update_bits(st
->regmap
, ADXL380_OP_MODE_REG
,
717 ADXL380_OP_MODE_RANGE_MSK
,
718 FIELD_PREP(ADXL380_OP_MODE_RANGE_MSK
, range
));
723 adxl380_scale_act_inact_thresholds(st
, st
->range
, range
);
725 /* Activity thresholds depend on range */
726 ret
= adxl380_write_act_inact_threshold(st
, ADXL380_ACTIVITY
,
731 ret
= adxl380_write_act_inact_threshold(st
, ADXL380_INACTIVITY
,
732 st
->inact_threshold
);
738 return adxl380_set_measure_en(st
, true);
741 static int adxl380_write_act_inact_en(struct adxl380_state
*st
,
742 enum adxl380_activity_type type
,
745 if (type
== ADXL380_ACTIVITY
)
746 return regmap_update_bits(st
->regmap
, ADXL380_ACT_INACT_CTL_REG
,
748 FIELD_PREP(ADXL380_ACT_EN_MSK
, en
));
750 return regmap_update_bits(st
->regmap
, ADXL380_ACT_INACT_CTL_REG
,
751 ADXL380_INACT_EN_MSK
,
752 FIELD_PREP(ADXL380_INACT_EN_MSK
, en
));
755 static int adxl380_read_act_inact_int(struct adxl380_state
*st
,
756 enum adxl380_activity_type type
,
760 unsigned int reg_val
;
762 guard(mutex
)(&st
->lock
);
764 ret
= regmap_read(st
->regmap
, st
->int_map
[0], ®_val
);
768 if (type
== ADXL380_ACTIVITY
)
769 *en
= FIELD_GET(ADXL380_INT_MAP0_ACT_INT0_MSK
, reg_val
);
771 *en
= FIELD_GET(ADXL380_INT_MAP0_INACT_INT0_MSK
, reg_val
);
776 static int adxl380_write_act_inact_int(struct adxl380_state
*st
,
777 enum adxl380_activity_type act
,
780 if (act
== ADXL380_ACTIVITY
)
781 return regmap_update_bits(st
->regmap
, st
->int_map
[0],
782 ADXL380_INT_MAP0_ACT_INT0_MSK
,
783 FIELD_PREP(ADXL380_INT_MAP0_ACT_INT0_MSK
, en
));
785 return regmap_update_bits(st
->regmap
, st
->int_map
[0],
786 ADXL380_INT_MAP0_INACT_INT0_MSK
,
787 FIELD_PREP(ADXL380_INT_MAP0_INACT_INT0_MSK
, en
));
790 static int adxl380_act_inact_config(struct adxl380_state
*st
,
791 enum adxl380_activity_type type
,
796 guard(mutex
)(&st
->lock
);
798 ret
= adxl380_set_measure_en(st
, false);
802 ret
= adxl380_write_act_inact_en(st
, type
, en
);
806 ret
= adxl380_write_act_inact_int(st
, type
, en
);
810 return adxl380_set_measure_en(st
, true);
813 static int adxl380_write_tap_axis(struct adxl380_state
*st
,
814 enum adxl380_axis axis
)
818 ret
= regmap_update_bits(st
->regmap
, ADXL380_TAP_CFG_REG
,
819 ADXL380_TAP_AXIS_MSK
,
820 FIELD_PREP(ADXL380_TAP_AXIS_MSK
, axis
));
825 st
->tap_axis_en
= axis
;
830 static int adxl380_read_tap_int(struct adxl380_state
*st
, enum adxl380_tap_type type
, bool *en
)
833 unsigned int reg_val
;
835 ret
= regmap_read(st
->regmap
, st
->int_map
[1], ®_val
);
839 if (type
== ADXL380_SINGLE_TAP
)
840 *en
= FIELD_GET(ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK
, reg_val
);
842 *en
= FIELD_GET(ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK
, reg_val
);
847 static int adxl380_write_tap_int(struct adxl380_state
*st
, enum adxl380_tap_type type
, bool en
)
849 if (type
== ADXL380_SINGLE_TAP
)
850 return regmap_update_bits(st
->regmap
, st
->int_map
[1],
851 ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK
,
852 FIELD_PREP(ADXL380_INT_MAP1_SINGLE_TAP_INT0_MSK
, en
));
854 return regmap_update_bits(st
->regmap
, st
->int_map
[1],
855 ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK
,
856 FIELD_PREP(ADXL380_INT_MAP1_DOUBLE_TAP_INT0_MSK
, en
));
859 static int adxl380_tap_config(struct adxl380_state
*st
,
860 enum adxl380_axis axis
,
861 enum adxl380_tap_type type
,
866 guard(mutex
)(&st
->lock
);
868 ret
= adxl380_set_measure_en(st
, false);
872 ret
= adxl380_write_tap_axis(st
, axis
);
876 ret
= adxl380_write_tap_int(st
, type
, en
);
880 return adxl380_set_measure_en(st
, true);
883 static int adxl380_set_fifo_samples(struct adxl380_state
*st
)
886 u16 fifo_samples
= st
->watermark
* st
->fifo_set_size
;
888 ret
= regmap_update_bits(st
->regmap
, ADXL380_FIFO_CONFIG_0_REG
,
889 ADXL380_FIFO_SAMPLES_8_MSK
,
890 FIELD_PREP(ADXL380_FIFO_SAMPLES_8_MSK
,
891 (fifo_samples
& BIT(8))));
895 return regmap_write(st
->regmap
, ADXL380_FIFO_CONFIG_1_REG
,
896 fifo_samples
& 0xFF);
899 static int adxl380_get_status(struct adxl380_state
*st
, u8
*status0
, u8
*status1
)
903 /* STATUS0, STATUS1 are adjacent regs */
904 ret
= regmap_bulk_read(st
->regmap
, ADXL380_STATUS_0_REG
,
909 *status0
= st
->transf_buf
[0];
910 *status1
= st
->transf_buf
[1];
915 static int adxl380_get_fifo_entries(struct adxl380_state
*st
, u16
*fifo_entries
)
919 ret
= regmap_bulk_read(st
->regmap
, ADXL380_FIFO_STATUS_0_REG
,
924 *fifo_entries
= st
->transf_buf
[0] | ((BIT(0) & st
->transf_buf
[1]) << 8);
929 static void adxl380_push_event(struct iio_dev
*indio_dev
, s64 timestamp
,
932 if (FIELD_GET(ADXL380_STATUS_1_ACT_MSK
, status1
))
933 iio_push_event(indio_dev
,
934 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X_OR_Y_OR_Z
,
935 IIO_EV_TYPE_THRESH
, IIO_EV_DIR_RISING
),
938 if (FIELD_GET(ADXL380_STATUS_1_INACT_MSK
, status1
))
939 iio_push_event(indio_dev
,
940 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X_OR_Y_OR_Z
,
941 IIO_EV_TYPE_THRESH
, IIO_EV_DIR_FALLING
),
943 if (FIELD_GET(ADXL380_STATUS_1_SINGLE_TAP_MSK
, status1
))
944 iio_push_event(indio_dev
,
945 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X_OR_Y_OR_Z
,
946 IIO_EV_TYPE_GESTURE
, IIO_EV_DIR_SINGLETAP
),
949 if (FIELD_GET(ADXL380_STATUS_1_DOUBLE_TAP_MSK
, status1
))
950 iio_push_event(indio_dev
,
951 IIO_MOD_EVENT_CODE(IIO_ACCEL
, 0, IIO_MOD_X_OR_Y_OR_Z
,
952 IIO_EV_TYPE_GESTURE
, IIO_EV_DIR_DOUBLETAP
),
956 static irqreturn_t
adxl380_irq_handler(int irq
, void *p
)
958 struct iio_dev
*indio_dev
= p
;
959 struct adxl380_state
*st
= iio_priv(indio_dev
);
965 guard(mutex
)(&st
->lock
);
967 ret
= adxl380_get_status(st
, &status0
, &status1
);
971 adxl380_push_event(indio_dev
, iio_get_time_ns(indio_dev
), status1
);
973 if (!FIELD_GET(ADXL380_STATUS_0_FIFO_WM_MSK
, status0
))
976 ret
= adxl380_get_fifo_entries(st
, &fifo_entries
);
980 for (i
= 0; i
< fifo_entries
; i
+= st
->fifo_set_size
) {
981 ret
= regmap_noinc_read(st
->regmap
, ADXL380_FIFO_DATA
,
983 2 * st
->fifo_set_size
);
986 iio_push_to_buffers(indio_dev
, &st
->fifo_buf
[i
]);
992 static int adxl380_write_calibbias_value(struct adxl380_state
*st
,
993 unsigned long chan_addr
,
998 guard(mutex
)(&st
->lock
);
1000 ret
= adxl380_set_measure_en(st
, false);
1004 ret
= regmap_write(st
->regmap
, ADXL380_X_DSM_OFFSET_REG
+ chan_addr
, calibbias
);
1008 return adxl380_set_measure_en(st
, true);
1011 static int adxl380_read_calibbias_value(struct adxl380_state
*st
,
1012 unsigned long chan_addr
,
1016 unsigned int reg_val
;
1018 guard(mutex
)(&st
->lock
);
1020 ret
= regmap_read(st
->regmap
, ADXL380_X_DSM_OFFSET_REG
+ chan_addr
, ®_val
);
1024 *calibbias
= sign_extend32(reg_val
, 7);
1029 static ssize_t
hwfifo_watermark_min_show(struct device
*dev
,
1030 struct device_attribute
*attr
,
1033 return sysfs_emit(buf
, "1\n");
1036 static ssize_t
hwfifo_watermark_max_show(struct device
*dev
,
1037 struct device_attribute
*attr
,
1040 return sysfs_emit(buf
, "%lu\n", ADXL380_FIFO_SAMPLES
);
1043 static ssize_t
adxl380_get_fifo_watermark(struct device
*dev
,
1044 struct device_attribute
*attr
,
1047 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1048 struct adxl380_state
*st
= iio_priv(indio_dev
);
1050 return sysfs_emit(buf
, "%d\n", st
->watermark
);
1053 static ssize_t
adxl380_get_fifo_enabled(struct device
*dev
,
1054 struct device_attribute
*attr
,
1057 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1058 struct adxl380_state
*st
= iio_priv(indio_dev
);
1060 unsigned int reg_val
;
1062 ret
= regmap_read(st
->regmap
, ADXL380_DIG_EN_REG
, ®_val
);
1066 return sysfs_emit(buf
, "%lu\n",
1067 FIELD_GET(ADXL380_FIFO_EN_MSK
, reg_val
));
1070 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_min
, 0);
1071 static IIO_DEVICE_ATTR_RO(hwfifo_watermark_max
, 0);
1072 static IIO_DEVICE_ATTR(hwfifo_watermark
, 0444,
1073 adxl380_get_fifo_watermark
, NULL
, 0);
1074 static IIO_DEVICE_ATTR(hwfifo_enabled
, 0444,
1075 adxl380_get_fifo_enabled
, NULL
, 0);
1077 static const struct iio_dev_attr
*adxl380_fifo_attributes
[] = {
1078 &iio_dev_attr_hwfifo_watermark_min
,
1079 &iio_dev_attr_hwfifo_watermark_max
,
1080 &iio_dev_attr_hwfifo_watermark
,
1081 &iio_dev_attr_hwfifo_enabled
,
1085 static int adxl380_buffer_postenable(struct iio_dev
*indio_dev
)
1087 struct adxl380_state
*st
= iio_priv(indio_dev
);
1091 guard(mutex
)(&st
->lock
);
1093 ret
= adxl380_set_measure_en(st
, false);
1097 ret
= regmap_update_bits(st
->regmap
,
1099 ADXL380_INT_MAP0_FIFO_WM_INT0_MSK
,
1100 FIELD_PREP(ADXL380_INT_MAP0_FIFO_WM_INT0_MSK
, 1));
1104 for_each_clear_bit(i
, indio_dev
->active_scan_mask
, ADXL380_CH_NUM
) {
1105 ret
= regmap_update_bits(st
->regmap
, ADXL380_DIG_EN_REG
,
1106 ADXL380_CHAN_EN_MSK(i
),
1112 st
->fifo_set_size
= bitmap_weight(indio_dev
->active_scan_mask
,
1113 iio_get_masklength(indio_dev
));
1115 if ((st
->watermark
* st
->fifo_set_size
) > ADXL380_FIFO_SAMPLES
)
1116 st
->watermark
= (ADXL380_FIFO_SAMPLES
/ st
->fifo_set_size
);
1118 ret
= adxl380_set_fifo_samples(st
);
1122 ret
= regmap_update_bits(st
->regmap
, ADXL380_DIG_EN_REG
, ADXL380_FIFO_EN_MSK
,
1123 FIELD_PREP(ADXL380_FIFO_EN_MSK
, 1));
1127 return adxl380_set_measure_en(st
, true);
1130 static int adxl380_buffer_predisable(struct iio_dev
*indio_dev
)
1132 struct adxl380_state
*st
= iio_priv(indio_dev
);
1135 guard(mutex
)(&st
->lock
);
1137 ret
= adxl380_set_measure_en(st
, false);
1141 ret
= regmap_update_bits(st
->regmap
,
1143 ADXL380_INT_MAP0_FIFO_WM_INT0_MSK
,
1144 FIELD_PREP(ADXL380_INT_MAP0_FIFO_WM_INT0_MSK
, 0));
1148 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
1149 ret
= regmap_update_bits(st
->regmap
, ADXL380_DIG_EN_REG
,
1150 ADXL380_CHAN_EN_MSK(i
),
1156 ret
= regmap_update_bits(st
->regmap
, ADXL380_DIG_EN_REG
, ADXL380_FIFO_EN_MSK
,
1157 FIELD_PREP(ADXL380_FIFO_EN_MSK
, 0));
1161 return adxl380_set_measure_en(st
, true);
1164 static const struct iio_buffer_setup_ops adxl380_buffer_ops
= {
1165 .postenable
= adxl380_buffer_postenable
,
1166 .predisable
= adxl380_buffer_predisable
,
1169 static int adxl380_read_raw(struct iio_dev
*indio_dev
,
1170 struct iio_chan_spec
const *chan
,
1171 int *val
, int *val2
, long info
)
1173 struct adxl380_state
*st
= iio_priv(indio_dev
);
1177 case IIO_CHAN_INFO_RAW
:
1178 ret
= iio_device_claim_direct_mode(indio_dev
);
1182 ret
= adxl380_read_chn(st
, chan
->address
);
1183 iio_device_release_direct_mode(indio_dev
);
1187 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
1188 chan
->scan_type
.realbits
- 1);
1190 case IIO_CHAN_INFO_SCALE
:
1191 switch (chan
->type
) {
1193 scoped_guard(mutex
, &st
->lock
) {
1194 *val
= st
->chip_info
->scale_tbl
[st
->range
][0];
1195 *val2
= st
->chip_info
->scale_tbl
[st
->range
][1];
1197 return IIO_VAL_INT_PLUS_NANO
;
1199 /* 10.2 LSB / Degree Celsius */
1202 return IIO_VAL_FRACTIONAL
;
1206 case IIO_CHAN_INFO_OFFSET
:
1207 switch (chan
->type
) {
1209 *val
= st
->chip_info
->temp_offset
;
1214 case IIO_CHAN_INFO_CALIBBIAS
:
1215 switch (chan
->type
) {
1217 ret
= adxl380_read_calibbias_value(st
, chan
->scan_index
, val
);
1224 case IIO_CHAN_INFO_SAMP_FREQ
:
1225 ret
= adxl380_get_odr(st
, val
);
1229 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
1230 ret
= adxl380_get_lpf(st
, val
);
1234 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
1235 ret
= adxl380_get_hpf(st
, val
, val2
);
1238 return IIO_VAL_INT_PLUS_MICRO
;
1244 static int adxl380_read_avail(struct iio_dev
*indio_dev
,
1245 struct iio_chan_spec
const *chan
,
1246 const int **vals
, int *type
, int *length
,
1249 struct adxl380_state
*st
= iio_priv(indio_dev
);
1251 if (chan
->type
!= IIO_ACCEL
)
1255 case IIO_CHAN_INFO_SCALE
:
1256 *vals
= (const int *)st
->chip_info
->scale_tbl
;
1257 *type
= IIO_VAL_INT_PLUS_NANO
;
1258 *length
= ARRAY_SIZE(st
->chip_info
->scale_tbl
) * 2;
1259 return IIO_AVAIL_LIST
;
1260 case IIO_CHAN_INFO_SAMP_FREQ
:
1261 *vals
= (const int *)st
->chip_info
->samp_freq_tbl
;
1262 *type
= IIO_VAL_INT
;
1263 *length
= ARRAY_SIZE(st
->chip_info
->samp_freq_tbl
);
1264 return IIO_AVAIL_LIST
;
1265 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
1266 *vals
= (const int *)st
->lpf_tbl
;
1267 *type
= IIO_VAL_INT
;
1268 *length
= ARRAY_SIZE(st
->lpf_tbl
);
1269 return IIO_AVAIL_LIST
;
1270 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
1271 *vals
= (const int *)st
->hpf_tbl
;
1272 *type
= IIO_VAL_INT_PLUS_MICRO
;
1273 /* Values are stored in a 2D matrix */
1274 *length
= ARRAY_SIZE(st
->hpf_tbl
) * 2;
1275 return IIO_AVAIL_LIST
;
1281 static int adxl380_write_raw(struct iio_dev
*indio_dev
,
1282 struct iio_chan_spec
const *chan
,
1283 int val
, int val2
, long info
)
1285 struct adxl380_state
*st
= iio_priv(indio_dev
);
1286 int odr_index
, lpf_index
, hpf_index
, range_index
;
1289 case IIO_CHAN_INFO_SAMP_FREQ
:
1290 odr_index
= adxl380_find_match_1d_tbl(st
->chip_info
->samp_freq_tbl
,
1291 ARRAY_SIZE(st
->chip_info
->samp_freq_tbl
),
1293 return adxl380_set_odr(st
, odr_index
);
1294 case IIO_CHAN_INFO_CALIBBIAS
:
1295 return adxl380_write_calibbias_value(st
, chan
->scan_index
, val
);
1296 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
1297 lpf_index
= adxl380_find_match_1d_tbl(st
->lpf_tbl
,
1298 ARRAY_SIZE(st
->lpf_tbl
),
1300 return adxl380_set_lpf(st
, lpf_index
);
1301 case IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY
:
1302 hpf_index
= adxl380_find_match_2d_tbl(st
->hpf_tbl
,
1303 ARRAY_SIZE(st
->hpf_tbl
),
1307 return adxl380_set_hpf(st
, hpf_index
);
1308 case IIO_CHAN_INFO_SCALE
:
1309 range_index
= adxl380_find_match_2d_tbl(st
->chip_info
->scale_tbl
,
1310 ARRAY_SIZE(st
->chip_info
->scale_tbl
),
1312 if (range_index
< 0)
1314 return adxl380_set_range(st
, range_index
);
1320 static int adxl380_write_raw_get_fmt(struct iio_dev
*indio_dev
,
1321 struct iio_chan_spec
const *chan
,
1325 case IIO_CHAN_INFO_SCALE
:
1326 if (chan
->type
!= IIO_ACCEL
)
1329 return IIO_VAL_INT_PLUS_NANO
;
1331 return IIO_VAL_INT_PLUS_MICRO
;
1335 static int adxl380_read_event_config(struct iio_dev
*indio_dev
,
1336 const struct iio_chan_spec
*chan
,
1337 enum iio_event_type type
,
1338 enum iio_event_direction dir
)
1340 struct adxl380_state
*st
= iio_priv(indio_dev
);
1343 bool tap_axis_en
= false;
1345 switch (chan
->channel2
) {
1347 tap_axis_en
= st
->tap_axis_en
== ADXL380_X_AXIS
;
1350 tap_axis_en
= st
->tap_axis_en
== ADXL380_Y_AXIS
;
1353 tap_axis_en
= st
->tap_axis_en
== ADXL380_Z_AXIS
;
1360 case IIO_EV_DIR_RISING
:
1361 ret
= adxl380_read_act_inact_int(st
, ADXL380_ACTIVITY
, &int_en
);
1365 case IIO_EV_DIR_FALLING
:
1366 ret
= adxl380_read_act_inact_int(st
, ADXL380_INACTIVITY
, &int_en
);
1370 case IIO_EV_DIR_SINGLETAP
:
1371 ret
= adxl380_read_tap_int(st
, ADXL380_SINGLE_TAP
, &int_en
);
1374 return int_en
&& tap_axis_en
;
1375 case IIO_EV_DIR_DOUBLETAP
:
1376 ret
= adxl380_read_tap_int(st
, ADXL380_DOUBLE_TAP
, &int_en
);
1379 return int_en
&& tap_axis_en
;
1385 static int adxl380_write_event_config(struct iio_dev
*indio_dev
,
1386 const struct iio_chan_spec
*chan
,
1387 enum iio_event_type type
,
1388 enum iio_event_direction dir
,
1391 struct adxl380_state
*st
= iio_priv(indio_dev
);
1392 enum adxl380_axis axis
;
1394 switch (chan
->channel2
) {
1396 axis
= ADXL380_X_AXIS
;
1399 axis
= ADXL380_Y_AXIS
;
1402 axis
= ADXL380_Z_AXIS
;
1409 case IIO_EV_DIR_RISING
:
1410 return adxl380_act_inact_config(st
, ADXL380_ACTIVITY
, state
);
1411 case IIO_EV_DIR_FALLING
:
1412 return adxl380_act_inact_config(st
, ADXL380_INACTIVITY
, state
);
1413 case IIO_EV_DIR_SINGLETAP
:
1414 return adxl380_tap_config(st
, axis
, ADXL380_SINGLE_TAP
, state
);
1415 case IIO_EV_DIR_DOUBLETAP
:
1416 return adxl380_tap_config(st
, axis
, ADXL380_DOUBLE_TAP
, state
);
1422 static int adxl380_read_event_value(struct iio_dev
*indio_dev
,
1423 const struct iio_chan_spec
*chan
,
1424 enum iio_event_type type
,
1425 enum iio_event_direction dir
,
1426 enum iio_event_info info
,
1427 int *val
, int *val2
)
1429 struct adxl380_state
*st
= iio_priv(indio_dev
);
1431 guard(mutex
)(&st
->lock
);
1434 case IIO_EV_TYPE_THRESH
:
1436 case IIO_EV_INFO_VALUE
: {
1438 case IIO_EV_DIR_RISING
:
1439 *val
= st
->act_threshold
;
1441 case IIO_EV_DIR_FALLING
:
1442 *val
= st
->inact_threshold
;
1448 case IIO_EV_INFO_PERIOD
:
1450 case IIO_EV_DIR_RISING
:
1451 *val
= st
->act_time_ms
;
1453 return IIO_VAL_FRACTIONAL
;
1454 case IIO_EV_DIR_FALLING
:
1455 *val
= st
->inact_time_ms
;
1457 return IIO_VAL_FRACTIONAL
;
1464 case IIO_EV_TYPE_GESTURE
:
1466 case IIO_EV_INFO_VALUE
:
1467 *val
= st
->tap_threshold
;
1469 case IIO_EV_INFO_RESET_TIMEOUT
:
1470 *val
= st
->tap_window_us
;
1472 return IIO_VAL_FRACTIONAL
;
1473 case IIO_EV_INFO_TAP2_MIN_DELAY
:
1474 *val
= st
->tap_latent_us
;
1476 return IIO_VAL_FRACTIONAL
;
1485 static int adxl380_write_event_value(struct iio_dev
*indio_dev
,
1486 const struct iio_chan_spec
*chan
,
1487 enum iio_event_type type
, enum iio_event_direction dir
,
1488 enum iio_event_info info
, int val
, int val2
)
1490 struct adxl380_state
*st
= iio_priv(indio_dev
);
1493 if (chan
->type
!= IIO_ACCEL
)
1497 case IIO_EV_TYPE_THRESH
:
1499 case IIO_EV_INFO_VALUE
:
1501 case IIO_EV_DIR_RISING
:
1502 return adxl380_set_act_inact_threshold(indio_dev
,
1503 ADXL380_ACTIVITY
, val
);
1504 case IIO_EV_DIR_FALLING
:
1505 return adxl380_set_act_inact_threshold(indio_dev
,
1506 ADXL380_INACTIVITY
, val
);
1510 case IIO_EV_INFO_PERIOD
:
1511 val_ms
= val
* 1000 + DIV_ROUND_UP(val2
, 1000);
1513 case IIO_EV_DIR_RISING
:
1514 return adxl380_set_act_inact_time_ms(st
,
1515 ADXL380_ACTIVITY
, val_ms
);
1516 case IIO_EV_DIR_FALLING
:
1517 return adxl380_set_act_inact_time_ms(st
,
1518 ADXL380_INACTIVITY
, val_ms
);
1526 case IIO_EV_TYPE_GESTURE
:
1528 case IIO_EV_INFO_VALUE
:
1529 return adxl380_set_tap_threshold_value(indio_dev
, val
);
1530 case IIO_EV_INFO_RESET_TIMEOUT
:
1531 val_us
= val
* 1000000 + val2
;
1532 return adxl380_write_tap_time_us(st
,
1533 ADXL380_TAP_TIME_WINDOW
,
1535 case IIO_EV_INFO_TAP2_MIN_DELAY
:
1536 val_us
= val
* 1000000 + val2
;
1537 return adxl380_write_tap_time_us(st
,
1538 ADXL380_TAP_TIME_LATENT
,
1548 static ssize_t
in_accel_gesture_tap_maxtomin_time_show(struct device
*dev
,
1549 struct device_attribute
*attr
,
1553 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1554 struct adxl380_state
*st
= iio_priv(indio_dev
);
1556 guard(mutex
)(&st
->lock
);
1558 vals
[0] = st
->tap_duration_us
;
1561 return iio_format_value(buf
, IIO_VAL_FRACTIONAL
, 2, vals
);
1564 static ssize_t
in_accel_gesture_tap_maxtomin_time_store(struct device
*dev
,
1565 struct device_attribute
*attr
,
1566 const char *buf
, size_t len
)
1568 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
1569 struct adxl380_state
*st
= iio_priv(indio_dev
);
1570 int ret
, val_int
, val_fract_us
;
1572 guard(mutex
)(&st
->lock
);
1574 ret
= iio_str_to_fixpoint(buf
, 100000, &val_int
, &val_fract_us
);
1578 /* maximum value is 255 * 625 us = 0.159375 seconds */
1579 if (val_int
|| val_fract_us
> 159375 || val_fract_us
< 0)
1582 ret
= adxl380_write_tap_dur_us(indio_dev
, val_fract_us
);
1589 static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_maxtomin_time
, 0);
1591 static struct attribute
*adxl380_event_attributes
[] = {
1592 &iio_dev_attr_in_accel_gesture_tap_maxtomin_time
.dev_attr
.attr
,
1596 static const struct attribute_group adxl380_event_attribute_group
= {
1597 .attrs
= adxl380_event_attributes
,
1600 static int adxl380_reg_access(struct iio_dev
*indio_dev
,
1602 unsigned int writeval
,
1603 unsigned int *readval
)
1605 struct adxl380_state
*st
= iio_priv(indio_dev
);
1608 return regmap_read(st
->regmap
, reg
, readval
);
1610 return regmap_write(st
->regmap
, reg
, writeval
);
1613 static int adxl380_set_watermark(struct iio_dev
*indio_dev
, unsigned int val
)
1615 struct adxl380_state
*st
= iio_priv(indio_dev
);
1617 st
->watermark
= min(val
, ADXL380_FIFO_SAMPLES
);
1622 static const struct iio_info adxl380_info
= {
1623 .read_raw
= adxl380_read_raw
,
1624 .read_avail
= &adxl380_read_avail
,
1625 .write_raw
= adxl380_write_raw
,
1626 .write_raw_get_fmt
= adxl380_write_raw_get_fmt
,
1627 .read_event_config
= adxl380_read_event_config
,
1628 .write_event_config
= adxl380_write_event_config
,
1629 .read_event_value
= adxl380_read_event_value
,
1630 .write_event_value
= adxl380_write_event_value
,
1631 .event_attrs
= &adxl380_event_attribute_group
,
1632 .debugfs_reg_access
= &adxl380_reg_access
,
1633 .hwfifo_set_watermark
= adxl380_set_watermark
,
1636 static const struct iio_event_spec adxl380_events
[] = {
1638 .type
= IIO_EV_TYPE_THRESH
,
1639 .dir
= IIO_EV_DIR_RISING
,
1640 .mask_shared_by_type
= BIT(IIO_EV_INFO_ENABLE
) |
1641 BIT(IIO_EV_INFO_VALUE
) |
1642 BIT(IIO_EV_INFO_PERIOD
),
1645 .type
= IIO_EV_TYPE_THRESH
,
1646 .dir
= IIO_EV_DIR_FALLING
,
1647 .mask_shared_by_type
= BIT(IIO_EV_INFO_ENABLE
) |
1648 BIT(IIO_EV_INFO_VALUE
) |
1649 BIT(IIO_EV_INFO_PERIOD
),
1652 .type
= IIO_EV_TYPE_GESTURE
,
1653 .dir
= IIO_EV_DIR_SINGLETAP
,
1654 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
1655 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
1656 BIT(IIO_EV_INFO_RESET_TIMEOUT
),
1659 .type
= IIO_EV_TYPE_GESTURE
,
1660 .dir
= IIO_EV_DIR_DOUBLETAP
,
1661 .mask_separate
= BIT(IIO_EV_INFO_ENABLE
),
1662 .mask_shared_by_type
= BIT(IIO_EV_INFO_VALUE
) |
1663 BIT(IIO_EV_INFO_RESET_TIMEOUT
) |
1664 BIT(IIO_EV_INFO_TAP2_MIN_DELAY
),
1668 #define ADXL380_ACCEL_CHANNEL(index, reg, axis) { \
1669 .type = IIO_ACCEL, \
1672 .channel2 = IIO_MOD_##axis, \
1673 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1674 BIT(IIO_CHAN_INFO_CALIBBIAS), \
1675 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1676 .info_mask_shared_by_all_available = \
1677 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1678 .info_mask_shared_by_type = \
1679 BIT(IIO_CHAN_INFO_SCALE) | \
1680 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
1681 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
1682 .info_mask_shared_by_type_available = \
1683 BIT(IIO_CHAN_INFO_SCALE) | \
1684 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY) | \
1685 BIT(IIO_CHAN_INFO_HIGH_PASS_FILTER_3DB_FREQUENCY), \
1686 .scan_index = index, \
1690 .storagebits = 16, \
1691 .endianness = IIO_BE, \
1693 .event_spec = adxl380_events, \
1694 .num_event_specs = ARRAY_SIZE(adxl380_events) \
1697 static const struct iio_chan_spec adxl380_channels
[] = {
1698 ADXL380_ACCEL_CHANNEL(0, ADXL380_X_DATA_H_REG
, X
),
1699 ADXL380_ACCEL_CHANNEL(1, ADXL380_Y_DATA_H_REG
, Y
),
1700 ADXL380_ACCEL_CHANNEL(2, ADXL380_Z_DATA_H_REG
, Z
),
1703 .address
= ADXL380_T_DATA_H_REG
,
1704 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
1705 BIT(IIO_CHAN_INFO_SCALE
) |
1706 BIT(IIO_CHAN_INFO_OFFSET
),
1713 .endianness
= IIO_BE
,
1718 static int adxl380_config_irq(struct iio_dev
*indio_dev
)
1720 struct adxl380_state
*st
= iio_priv(indio_dev
);
1721 unsigned long irq_flag
;
1722 struct irq_data
*desc
;
1727 st
->irq
= fwnode_irq_get_byname(dev_fwnode(st
->dev
), "INT0");
1729 st
->int_map
[0] = ADXL380_INT0_MAP0_REG
;
1730 st
->int_map
[1] = ADXL380_INT0_MAP1_REG
;
1732 st
->irq
= fwnode_irq_get_byname(dev_fwnode(st
->dev
), "INT1");
1734 return dev_err_probe(st
->dev
, -ENODEV
,
1735 "no interrupt name specified");
1736 st
->int_map
[0] = ADXL380_INT1_MAP0_REG
;
1737 st
->int_map
[1] = ADXL380_INT1_MAP1_REG
;
1740 desc
= irq_get_irq_data(st
->irq
);
1742 return dev_err_probe(st
->dev
, -EINVAL
, "Could not find IRQ %d\n", st
->irq
);
1744 irq_type
= irqd_get_trigger_type(desc
);
1745 if (irq_type
== IRQ_TYPE_LEVEL_HIGH
) {
1747 irq_flag
= IRQF_TRIGGER_HIGH
| IRQF_ONESHOT
;
1748 } else if (irq_type
== IRQ_TYPE_LEVEL_LOW
) {
1750 irq_flag
= IRQF_TRIGGER_LOW
| IRQF_ONESHOT
;
1752 return dev_err_probe(st
->dev
, -EINVAL
,
1753 "Invalid interrupt 0x%x. Only level interrupts supported\n",
1757 ret
= regmap_update_bits(st
->regmap
, ADXL380_INT0_REG
,
1758 ADXL380_INT0_POL_MSK
,
1759 FIELD_PREP(ADXL380_INT0_POL_MSK
, polarity
));
1763 return devm_request_threaded_irq(st
->dev
, st
->irq
, NULL
,
1764 adxl380_irq_handler
, irq_flag
,
1765 indio_dev
->name
, indio_dev
);
1768 static int adxl380_setup(struct iio_dev
*indio_dev
)
1770 unsigned int reg_val
;
1771 u16 part_id
, chip_id
;
1773 struct adxl380_state
*st
= iio_priv(indio_dev
);
1775 ret
= regmap_read(st
->regmap
, ADXL380_DEVID_AD_REG
, ®_val
);
1779 if (reg_val
!= ADXL380_DEVID_AD_VAL
)
1780 dev_warn(st
->dev
, "Unknown chip id %x\n", reg_val
);
1782 ret
= regmap_bulk_read(st
->regmap
, ADLX380_PART_ID_REG
,
1783 &st
->transf_buf
, 2);
1787 part_id
= get_unaligned_be16(st
->transf_buf
);
1790 if (part_id
!= ADXL380_ID_VAL
)
1791 dev_warn(st
->dev
, "Unknown part id %x\n", part_id
);
1793 ret
= regmap_read(st
->regmap
, ADXL380_MISC_0_REG
, ®_val
);
1797 /* Bit to differentiate between ADXL380/382. */
1798 if (reg_val
& ADXL380_XL382_MSK
)
1799 chip_id
= ADXL382_ID_VAL
;
1801 chip_id
= ADXL380_ID_VAL
;
1803 if (chip_id
!= st
->chip_info
->chip_id
)
1804 dev_warn(st
->dev
, "Unknown chip id %x\n", chip_id
);
1806 ret
= regmap_write(st
->regmap
, ADXL380_RESET_REG
, ADXL380_RESET_CODE
);
1811 * A latency of approximately 0.5 ms is required after soft reset.
1812 * Stated in the register REG_RESET description.
1816 for (i
= 0; i
< indio_dev
->num_channels
; i
++) {
1817 ret
= regmap_update_bits(st
->regmap
, ADXL380_DIG_EN_REG
,
1818 ADXL380_CHAN_EN_MSK(i
),
1824 ret
= regmap_update_bits(st
->regmap
, ADXL380_FIFO_CONFIG_0_REG
,
1825 ADXL380_FIFO_MODE_MSK
,
1826 FIELD_PREP(ADXL380_FIFO_MODE_MSK
, ADXL380_FIFO_STREAMED
));
1830 /* Select all 3 axis for act/inact detection. */
1831 ret
= regmap_update_bits(st
->regmap
, ADXL380_SNSR_AXIS_EN_REG
,
1832 ADXL380_ACT_INACT_AXIS_EN_MSK
,
1833 FIELD_PREP(ADXL380_ACT_INACT_AXIS_EN_MSK
,
1834 ADXL380_ACT_INACT_AXIS_EN_MSK
));
1838 ret
= adxl380_config_irq(indio_dev
);
1842 ret
= adxl380_fill_lpf_tbl(st
);
1846 ret
= adxl380_fill_hpf_tbl(st
);
1850 return adxl380_set_measure_en(st
, true);
1853 int adxl380_probe(struct device
*dev
, struct regmap
*regmap
,
1854 const struct adxl380_chip_info
*chip_info
)
1856 struct iio_dev
*indio_dev
;
1857 struct adxl380_state
*st
;
1860 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
1864 st
= iio_priv(indio_dev
);
1867 st
->regmap
= regmap
;
1868 st
->chip_info
= chip_info
;
1870 mutex_init(&st
->lock
);
1872 indio_dev
->channels
= adxl380_channels
;
1873 indio_dev
->num_channels
= ARRAY_SIZE(adxl380_channels
);
1874 indio_dev
->name
= chip_info
->name
;
1875 indio_dev
->info
= &adxl380_info
;
1876 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1878 ret
= devm_regulator_get_enable(dev
, "vddio");
1880 return dev_err_probe(st
->dev
, ret
,
1881 "Failed to get vddio regulator\n");
1883 ret
= devm_regulator_get_enable(st
->dev
, "vsupply");
1885 return dev_err_probe(st
->dev
, ret
,
1886 "Failed to get vsupply regulator\n");
1888 ret
= adxl380_setup(indio_dev
);
1892 ret
= devm_iio_kfifo_buffer_setup_ext(st
->dev
, indio_dev
,
1893 &adxl380_buffer_ops
,
1894 adxl380_fifo_attributes
);
1898 return devm_iio_device_register(dev
, indio_dev
);
1900 EXPORT_SYMBOL_NS_GPL(adxl380_probe
, IIO_ADXL380
);
1902 MODULE_AUTHOR("Ramona Gradinariu <ramona.gradinariu@analog.com>");
1903 MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>");
1904 MODULE_DESCRIPTION("Analog Devices ADXL380 3-axis accelerometer driver");
1905 MODULE_LICENSE("GPL");