1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2021 Analog Devices, Inc.
4 * Author: Cosmin Tanislav <cosmin.tanislav@analog.com>
7 #include <linux/bitfield.h>
8 #include <linux/bitops.h>
9 #include <linux/iio/buffer.h>
10 #include <linux/iio/events.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/kfifo_buf.h>
13 #include <linux/iio/sysfs.h>
14 #include <linux/interrupt.h>
15 #include <linux/irq.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/regmap.h>
18 #include <linux/regulator/consumer.h>
19 #include <asm/unaligned.h>
23 #define ADXL367_REG_DEVID 0x00
24 #define ADXL367_DEVID_AD 0xAD
26 #define ADXL367_REG_STATUS 0x0B
27 #define ADXL367_STATUS_INACT_MASK BIT(5)
28 #define ADXL367_STATUS_ACT_MASK BIT(4)
29 #define ADXL367_STATUS_FIFO_FULL_MASK BIT(2)
31 #define ADXL367_FIFO_ENT_H_MASK GENMASK(1, 0)
33 #define ADXL367_REG_X_DATA_H 0x0E
34 #define ADXL367_REG_Y_DATA_H 0x10
35 #define ADXL367_REG_Z_DATA_H 0x12
36 #define ADXL367_REG_TEMP_DATA_H 0x14
37 #define ADXL367_REG_EX_ADC_DATA_H 0x16
38 #define ADXL367_DATA_MASK GENMASK(15, 2)
40 #define ADXL367_TEMP_25C 165
41 #define ADXL367_TEMP_PER_C 54
43 #define ADXL367_VOLTAGE_OFFSET 8192
44 #define ADXL367_VOLTAGE_MAX_MV 1000
45 #define ADXL367_VOLTAGE_MAX_RAW GENMASK(13, 0)
47 #define ADXL367_REG_RESET 0x1F
48 #define ADXL367_RESET_CODE 0x52
50 #define ADXL367_REG_THRESH_ACT_H 0x20
51 #define ADXL367_REG_THRESH_INACT_H 0x23
52 #define ADXL367_THRESH_MAX GENMASK(12, 0)
53 #define ADXL367_THRESH_VAL_H_MASK GENMASK(12, 6)
54 #define ADXL367_THRESH_H_MASK GENMASK(6, 0)
55 #define ADXL367_THRESH_VAL_L_MASK GENMASK(5, 0)
56 #define ADXL367_THRESH_L_MASK GENMASK(7, 2)
58 #define ADXL367_REG_TIME_ACT 0x22
59 #define ADXL367_REG_TIME_INACT_H 0x25
60 #define ADXL367_TIME_ACT_MAX GENMASK(7, 0)
61 #define ADXL367_TIME_INACT_MAX GENMASK(15, 0)
62 #define ADXL367_TIME_INACT_VAL_H_MASK GENMASK(15, 8)
63 #define ADXL367_TIME_INACT_H_MASK GENMASK(7, 0)
64 #define ADXL367_TIME_INACT_VAL_L_MASK GENMASK(7, 0)
65 #define ADXL367_TIME_INACT_L_MASK GENMASK(7, 0)
67 #define ADXL367_REG_ACT_INACT_CTL 0x27
68 #define ADXL367_ACT_EN_MASK GENMASK(1, 0)
69 #define ADXL367_ACT_LINKLOOP_MASK GENMASK(5, 4)
71 #define ADXL367_REG_FIFO_CTL 0x28
72 #define ADXL367_FIFO_CTL_FORMAT_MASK GENMASK(6, 3)
73 #define ADXL367_FIFO_CTL_MODE_MASK GENMASK(1, 0)
75 #define ADXL367_REG_FIFO_SAMPLES 0x29
76 #define ADXL367_FIFO_SIZE 512
77 #define ADXL367_FIFO_MAX_WATERMARK 511
79 #define ADXL367_SAMPLES_VAL_H_MASK BIT(8)
80 #define ADXL367_SAMPLES_H_MASK BIT(2)
81 #define ADXL367_SAMPLES_VAL_L_MASK GENMASK(7, 0)
82 #define ADXL367_SAMPLES_L_MASK GENMASK(7, 0)
84 #define ADXL367_REG_INT1_MAP 0x2A
85 #define ADXL367_INT_INACT_MASK BIT(5)
86 #define ADXL367_INT_ACT_MASK BIT(4)
87 #define ADXL367_INT_FIFO_WATERMARK_MASK BIT(2)
89 #define ADXL367_REG_FILTER_CTL 0x2C
90 #define ADXL367_FILTER_CTL_RANGE_MASK GENMASK(7, 6)
91 #define ADXL367_2G_RANGE_1G 4095
92 #define ADXL367_2G_RANGE_100MG 409
93 #define ADXL367_FILTER_CTL_ODR_MASK GENMASK(2, 0)
95 #define ADXL367_REG_POWER_CTL 0x2D
96 #define ADXL367_POWER_CTL_MODE_MASK GENMASK(1, 0)
98 #define ADXL367_REG_ADC_CTL 0x3C
99 #define ADXL367_REG_TEMP_CTL 0x3D
100 #define ADXL367_ADC_EN_MASK BIT(0)
108 enum adxl367_fifo_mode {
109 ADXL367_FIFO_MODE_DISABLED = 0b00,
110 ADXL367_FIFO_MODE_STREAM = 0b10,
113 enum adxl367_fifo_format {
114 ADXL367_FIFO_FORMAT_XYZ,
115 ADXL367_FIFO_FORMAT_X,
116 ADXL367_FIFO_FORMAT_Y,
117 ADXL367_FIFO_FORMAT_Z,
118 ADXL367_FIFO_FORMAT_XYZT,
119 ADXL367_FIFO_FORMAT_XT,
120 ADXL367_FIFO_FORMAT_YT,
121 ADXL367_FIFO_FORMAT_ZT,
122 ADXL367_FIFO_FORMAT_XYZA,
123 ADXL367_FIFO_FORMAT_XA,
124 ADXL367_FIFO_FORMAT_YA,
125 ADXL367_FIFO_FORMAT_ZA,
128 enum adxl367_op_mode {
129 ADXL367_OP_STANDBY = 0b00,
130 ADXL367_OP_MEASURE = 0b10,
133 enum adxl367_act_proc_mode {
134 ADXL367_LOOPED = 0b11,
137 enum adxl367_act_en_mode {
138 ADXL367_ACT_DISABLED = 0b00,
139 ADCL367_ACT_REF_ENABLED = 0b11,
142 enum adxl367_activity_type {
156 struct adxl367_state {
157 const struct adxl367_ops *ops;
161 struct regmap *regmap;
163 struct regulator_bulk_data regulators[2];
166 * Synchronize access to members of driver state, and ensure atomicity
167 * of consecutive regmap operations.
171 enum adxl367_odr odr;
172 enum adxl367_range range;
174 unsigned int act_threshold;
175 unsigned int act_time_ms;
176 unsigned int inact_threshold;
177 unsigned int inact_time_ms;
179 unsigned int fifo_set_size;
180 unsigned int fifo_watermark;
182 __be16 fifo_buf[ADXL367_FIFO_SIZE] ____cacheline_aligned;
184 u8 act_threshold_buf[2];
185 u8 inact_time_buf[2];
189 static const unsigned int adxl367_threshold_h_reg_tbl[] = {
190 [ADXL367_ACTIVITY] = ADXL367_REG_THRESH_ACT_H,
191 [ADXL367_INACTIVITY] = ADXL367_REG_THRESH_INACT_H,
194 static const unsigned int adxl367_act_en_shift_tbl[] = {
195 [ADXL367_ACTIVITY] = 0,
196 [ADXL367_INACTIVITY] = 2,
199 static const unsigned int adxl367_act_int_mask_tbl[] = {
200 [ADXL367_ACTIVITY] = ADXL367_INT_ACT_MASK,
201 [ADXL367_INACTIVITY] = ADXL367_INT_INACT_MASK,
204 static const int adxl367_samp_freq_tbl[][2] = {
205 [ADXL367_ODR_12P5HZ] = {12, 500000},
206 [ADXL367_ODR_25HZ] = {25, 0},
207 [ADXL367_ODR_50HZ] = {50, 0},
208 [ADXL367_ODR_100HZ] = {100, 0},
209 [ADXL367_ODR_200HZ] = {200, 0},
210 [ADXL367_ODR_400HZ] = {400, 0},
213 /* (g * 2) * 9.80665 * 1000000 / (2^14 - 1) */
214 static const int adxl367_range_scale_tbl[][2] = {
215 [ADXL367_2G_RANGE] = {0, 2394347},
216 [ADXL367_4G_RANGE] = {0, 4788695},
217 [ADXL367_8G_RANGE] = {0, 9577391},
220 static const int adxl367_range_scale_factor_tbl[] = {
221 [ADXL367_2G_RANGE] = 1,
222 [ADXL367_4G_RANGE] = 2,
223 [ADXL367_8G_RANGE] = 4,
227 ADXL367_X_CHANNEL_INDEX,
228 ADXL367_Y_CHANNEL_INDEX,
229 ADXL367_Z_CHANNEL_INDEX,
230 ADXL367_TEMP_CHANNEL_INDEX,
231 ADXL367_EX_ADC_CHANNEL_INDEX
234 #define ADXL367_X_CHANNEL_MASK BIT(ADXL367_X_CHANNEL_INDEX)
235 #define ADXL367_Y_CHANNEL_MASK BIT(ADXL367_Y_CHANNEL_INDEX)
236 #define ADXL367_Z_CHANNEL_MASK BIT(ADXL367_Z_CHANNEL_INDEX)
237 #define ADXL367_TEMP_CHANNEL_MASK BIT(ADXL367_TEMP_CHANNEL_INDEX)
238 #define ADXL367_EX_ADC_CHANNEL_MASK BIT(ADXL367_EX_ADC_CHANNEL_INDEX)
240 static const enum adxl367_fifo_format adxl367_fifo_formats[] = {
241 ADXL367_FIFO_FORMAT_X,
242 ADXL367_FIFO_FORMAT_Y,
243 ADXL367_FIFO_FORMAT_Z,
244 ADXL367_FIFO_FORMAT_XT,
245 ADXL367_FIFO_FORMAT_YT,
246 ADXL367_FIFO_FORMAT_ZT,
247 ADXL367_FIFO_FORMAT_XA,
248 ADXL367_FIFO_FORMAT_YA,
249 ADXL367_FIFO_FORMAT_ZA,
250 ADXL367_FIFO_FORMAT_XYZ,
251 ADXL367_FIFO_FORMAT_XYZT,
252 ADXL367_FIFO_FORMAT_XYZA,
255 static const unsigned long adxl367_channel_masks[] = {
256 ADXL367_X_CHANNEL_MASK,
257 ADXL367_Y_CHANNEL_MASK,
258 ADXL367_Z_CHANNEL_MASK,
259 ADXL367_X_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK,
260 ADXL367_Y_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK,
261 ADXL367_Z_CHANNEL_MASK | ADXL367_TEMP_CHANNEL_MASK,
262 ADXL367_X_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK,
263 ADXL367_Y_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK,
264 ADXL367_Z_CHANNEL_MASK | ADXL367_EX_ADC_CHANNEL_MASK,
265 ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK,
266 ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK |
267 ADXL367_TEMP_CHANNEL_MASK,
268 ADXL367_X_CHANNEL_MASK | ADXL367_Y_CHANNEL_MASK | ADXL367_Z_CHANNEL_MASK |
269 ADXL367_EX_ADC_CHANNEL_MASK,
273 static int adxl367_set_measure_en(struct adxl367_state *st, bool en)
275 enum adxl367_op_mode op_mode = en ? ADXL367_OP_MEASURE
276 : ADXL367_OP_STANDBY;
279 ret = regmap_update_bits(st->regmap, ADXL367_REG_POWER_CTL,
280 ADXL367_POWER_CTL_MODE_MASK,
281 FIELD_PREP(ADXL367_POWER_CTL_MODE_MASK,
287 * Wait for acceleration output to settle after entering
296 static void adxl367_scale_act_thresholds(struct adxl367_state *st,
297 enum adxl367_range old_range,
298 enum adxl367_range new_range)
300 st->act_threshold = st->act_threshold
301 * adxl367_range_scale_factor_tbl[old_range]
302 / adxl367_range_scale_factor_tbl[new_range];
303 st->inact_threshold = st->inact_threshold
304 * adxl367_range_scale_factor_tbl[old_range]
305 / adxl367_range_scale_factor_tbl[new_range];
308 static int _adxl367_set_act_threshold(struct adxl367_state *st,
309 enum adxl367_activity_type act,
310 unsigned int threshold)
312 u8 reg = adxl367_threshold_h_reg_tbl[act];
315 if (threshold > ADXL367_THRESH_MAX)
318 st->act_threshold_buf[0] = FIELD_PREP(ADXL367_THRESH_H_MASK,
319 FIELD_GET(ADXL367_THRESH_VAL_H_MASK,
321 st->act_threshold_buf[1] = FIELD_PREP(ADXL367_THRESH_L_MASK,
322 FIELD_GET(ADXL367_THRESH_VAL_L_MASK,
325 ret = regmap_bulk_write(st->regmap, reg, st->act_threshold_buf,
326 sizeof(st->act_threshold_buf));
330 if (act == ADXL367_ACTIVITY)
331 st->act_threshold = threshold;
333 st->inact_threshold = threshold;
338 static int adxl367_set_act_threshold(struct adxl367_state *st,
339 enum adxl367_activity_type act,
340 unsigned int threshold)
344 mutex_lock(&st->lock);
346 ret = adxl367_set_measure_en(st, false);
350 ret = _adxl367_set_act_threshold(st, act, threshold);
354 ret = adxl367_set_measure_en(st, true);
357 mutex_unlock(&st->lock);
362 static int adxl367_set_act_proc_mode(struct adxl367_state *st,
363 enum adxl367_act_proc_mode mode)
365 return regmap_update_bits(st->regmap, ADXL367_REG_ACT_INACT_CTL,
366 ADXL367_ACT_LINKLOOP_MASK,
367 FIELD_PREP(ADXL367_ACT_LINKLOOP_MASK,
371 static int adxl367_set_act_interrupt_en(struct adxl367_state *st,
372 enum adxl367_activity_type act,
375 unsigned int mask = adxl367_act_int_mask_tbl[act];
377 return regmap_update_bits(st->regmap, ADXL367_REG_INT1_MAP,
378 mask, en ? mask : 0);
381 static int adxl367_get_act_interrupt_en(struct adxl367_state *st,
382 enum adxl367_activity_type act,
385 unsigned int mask = adxl367_act_int_mask_tbl[act];
389 ret = regmap_read(st->regmap, ADXL367_REG_INT1_MAP, &val);
393 *en = !!(val & mask);
398 static int adxl367_set_act_en(struct adxl367_state *st,
399 enum adxl367_activity_type act,
400 enum adxl367_act_en_mode en)
402 unsigned int ctl_shift = adxl367_act_en_shift_tbl[act];
404 return regmap_update_bits(st->regmap, ADXL367_REG_ACT_INACT_CTL,
405 ADXL367_ACT_EN_MASK << ctl_shift,
409 static int adxl367_set_fifo_watermark_interrupt_en(struct adxl367_state *st,
412 return regmap_update_bits(st->regmap, ADXL367_REG_INT1_MAP,
413 ADXL367_INT_FIFO_WATERMARK_MASK,
414 en ? ADXL367_INT_FIFO_WATERMARK_MASK : 0);
417 static int adxl367_get_fifo_mode(struct adxl367_state *st,
418 enum adxl367_fifo_mode *fifo_mode)
423 ret = regmap_read(st->regmap, ADXL367_REG_FIFO_CTL, &val);
427 *fifo_mode = FIELD_GET(ADXL367_FIFO_CTL_MODE_MASK, val);
432 static int adxl367_set_fifo_mode(struct adxl367_state *st,
433 enum adxl367_fifo_mode fifo_mode)
435 return regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL,
436 ADXL367_FIFO_CTL_MODE_MASK,
437 FIELD_PREP(ADXL367_FIFO_CTL_MODE_MASK,
441 static int adxl367_set_fifo_format(struct adxl367_state *st,
442 enum adxl367_fifo_format fifo_format)
444 return regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL,
445 ADXL367_FIFO_CTL_FORMAT_MASK,
446 FIELD_PREP(ADXL367_FIFO_CTL_FORMAT_MASK,
450 static int adxl367_set_fifo_samples(struct adxl367_state *st,
451 unsigned int fifo_watermark,
452 unsigned int fifo_set_size)
454 unsigned int fifo_samples = fifo_watermark * fifo_set_size;
455 unsigned int fifo_samples_h, fifo_samples_l;
458 if (fifo_samples > ADXL367_FIFO_MAX_WATERMARK)
459 fifo_samples = ADXL367_FIFO_MAX_WATERMARK;
461 if (fifo_set_size == 0)
464 fifo_samples /= fifo_set_size;
466 fifo_samples_h = FIELD_PREP(ADXL367_SAMPLES_H_MASK,
467 FIELD_GET(ADXL367_SAMPLES_VAL_H_MASK,
469 fifo_samples_l = FIELD_PREP(ADXL367_SAMPLES_L_MASK,
470 FIELD_GET(ADXL367_SAMPLES_VAL_L_MASK,
473 ret = regmap_update_bits(st->regmap, ADXL367_REG_FIFO_CTL,
474 ADXL367_SAMPLES_H_MASK, fifo_samples_h);
478 return regmap_update_bits(st->regmap, ADXL367_REG_FIFO_SAMPLES,
479 ADXL367_SAMPLES_L_MASK, fifo_samples_l);
482 static int adxl367_set_fifo_set_size(struct adxl367_state *st,
483 unsigned int fifo_set_size)
487 ret = adxl367_set_fifo_samples(st, st->fifo_watermark, fifo_set_size);
491 st->fifo_set_size = fifo_set_size;
496 static int adxl367_set_fifo_watermark(struct adxl367_state *st,
497 unsigned int fifo_watermark)
501 ret = adxl367_set_fifo_samples(st, fifo_watermark, st->fifo_set_size);
505 st->fifo_watermark = fifo_watermark;
510 static int adxl367_set_range(struct iio_dev *indio_dev,
511 enum adxl367_range range)
513 struct adxl367_state *st = iio_priv(indio_dev);
516 ret = iio_device_claim_direct_mode(indio_dev);
520 mutex_lock(&st->lock);
522 ret = adxl367_set_measure_en(st, false);
526 ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
527 ADXL367_FILTER_CTL_RANGE_MASK,
528 FIELD_PREP(ADXL367_FILTER_CTL_RANGE_MASK,
533 adxl367_scale_act_thresholds(st, st->range, range);
535 /* Activity thresholds depend on range */
536 ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
541 ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
542 st->inact_threshold);
546 ret = adxl367_set_measure_en(st, true);
553 mutex_unlock(&st->lock);
555 iio_device_release_direct_mode(indio_dev);
560 static int adxl367_time_ms_to_samples(struct adxl367_state *st, unsigned int ms)
562 int freq_hz = adxl367_samp_freq_tbl[st->odr][0];
563 int freq_microhz = adxl367_samp_freq_tbl[st->odr][1];
564 /* Scale to decihertz to prevent precision loss in 12.5Hz case. */
565 int freq_dhz = freq_hz * 10 + freq_microhz / 100000;
567 return DIV_ROUND_CLOSEST(ms * freq_dhz, 10000);
570 static int _adxl367_set_act_time_ms(struct adxl367_state *st, unsigned int ms)
572 unsigned int val = adxl367_time_ms_to_samples(st, ms);
575 if (val > ADXL367_TIME_ACT_MAX)
576 val = ADXL367_TIME_ACT_MAX;
578 ret = regmap_write(st->regmap, ADXL367_REG_TIME_ACT, val);
582 st->act_time_ms = ms;
587 static int _adxl367_set_inact_time_ms(struct adxl367_state *st, unsigned int ms)
589 unsigned int val = adxl367_time_ms_to_samples(st, ms);
592 if (val > ADXL367_TIME_INACT_MAX)
593 val = ADXL367_TIME_INACT_MAX;
595 st->inact_time_buf[0] = FIELD_PREP(ADXL367_TIME_INACT_H_MASK,
596 FIELD_GET(ADXL367_TIME_INACT_VAL_H_MASK,
598 st->inact_time_buf[1] = FIELD_PREP(ADXL367_TIME_INACT_L_MASK,
599 FIELD_GET(ADXL367_TIME_INACT_VAL_L_MASK,
602 ret = regmap_bulk_write(st->regmap, ADXL367_REG_TIME_INACT_H,
603 st->inact_time_buf, sizeof(st->inact_time_buf));
607 st->inact_time_ms = ms;
612 static int adxl367_set_act_time_ms(struct adxl367_state *st,
613 enum adxl367_activity_type act,
618 mutex_lock(&st->lock);
620 ret = adxl367_set_measure_en(st, false);
624 if (act == ADXL367_ACTIVITY)
625 ret = _adxl367_set_act_time_ms(st, ms);
627 ret = _adxl367_set_inact_time_ms(st, ms);
632 ret = adxl367_set_measure_en(st, true);
635 mutex_unlock(&st->lock);
640 static int _adxl367_set_odr(struct adxl367_state *st, enum adxl367_odr odr)
644 ret = regmap_update_bits(st->regmap, ADXL367_REG_FILTER_CTL,
645 ADXL367_FILTER_CTL_ODR_MASK,
646 FIELD_PREP(ADXL367_FILTER_CTL_ODR_MASK,
651 /* Activity timers depend on ODR */
652 ret = _adxl367_set_act_time_ms(st, st->act_time_ms);
656 ret = _adxl367_set_inact_time_ms(st, st->inact_time_ms);
665 static int adxl367_set_odr(struct iio_dev *indio_dev, enum adxl367_odr odr)
667 struct adxl367_state *st = iio_priv(indio_dev);
670 ret = iio_device_claim_direct_mode(indio_dev);
674 mutex_lock(&st->lock);
676 ret = adxl367_set_measure_en(st, false);
680 ret = _adxl367_set_odr(st, odr);
684 ret = adxl367_set_measure_en(st, true);
687 mutex_unlock(&st->lock);
689 iio_device_release_direct_mode(indio_dev);
694 static int adxl367_set_temp_adc_en(struct adxl367_state *st, unsigned int reg,
697 return regmap_update_bits(st->regmap, reg, ADXL367_ADC_EN_MASK,
698 en ? ADXL367_ADC_EN_MASK : 0);
701 static int adxl367_set_temp_adc_reg_en(struct adxl367_state *st,
702 unsigned int reg, bool en)
707 case ADXL367_REG_TEMP_DATA_H:
708 ret = adxl367_set_temp_adc_en(st, ADXL367_REG_TEMP_CTL, en);
710 case ADXL367_REG_EX_ADC_DATA_H:
711 ret = adxl367_set_temp_adc_en(st, ADXL367_REG_ADC_CTL, en);
726 static int adxl367_set_temp_adc_mask_en(struct adxl367_state *st,
727 const unsigned long *active_scan_mask,
730 if (*active_scan_mask & ADXL367_TEMP_CHANNEL_MASK)
731 return adxl367_set_temp_adc_en(st, ADXL367_REG_TEMP_CTL, en);
732 else if (*active_scan_mask & ADXL367_EX_ADC_CHANNEL_MASK)
733 return adxl367_set_temp_adc_en(st, ADXL367_REG_ADC_CTL, en);
738 static int adxl367_find_odr(struct adxl367_state *st, int val, int val2,
739 enum adxl367_odr *odr)
741 size_t size = ARRAY_SIZE(adxl367_samp_freq_tbl);
744 for (i = 0; i < size; i++)
745 if (val == adxl367_samp_freq_tbl[i][0] &&
746 val2 == adxl367_samp_freq_tbl[i][1])
757 static int adxl367_find_range(struct adxl367_state *st, int val, int val2,
758 enum adxl367_range *range)
760 size_t size = ARRAY_SIZE(adxl367_range_scale_tbl);
763 for (i = 0; i < size; i++)
764 if (val == adxl367_range_scale_tbl[i][0] &&
765 val2 == adxl367_range_scale_tbl[i][1])
776 static int adxl367_read_sample(struct iio_dev *indio_dev,
777 struct iio_chan_spec const *chan,
780 struct adxl367_state *st = iio_priv(indio_dev);
784 ret = iio_device_claim_direct_mode(indio_dev);
788 mutex_lock(&st->lock);
790 ret = adxl367_set_temp_adc_reg_en(st, chan->address, true);
794 ret = regmap_bulk_read(st->regmap, chan->address, &st->sample_buf,
795 sizeof(st->sample_buf));
799 sample = FIELD_GET(ADXL367_DATA_MASK, be16_to_cpu(st->sample_buf));
800 *val = sign_extend32(sample, chan->scan_type.realbits - 1);
802 ret = adxl367_set_temp_adc_reg_en(st, chan->address, false);
805 mutex_unlock(&st->lock);
807 iio_device_release_direct_mode(indio_dev);
809 return ret ?: IIO_VAL_INT;
812 static int adxl367_get_status(struct adxl367_state *st, u8 *status,
817 /* Read STATUS, FIFO_ENT_L and FIFO_ENT_H */
818 ret = regmap_bulk_read(st->regmap, ADXL367_REG_STATUS,
819 st->status_buf, sizeof(st->status_buf));
823 st->status_buf[2] &= ADXL367_FIFO_ENT_H_MASK;
825 *status = st->status_buf[0];
826 *fifo_entries = get_unaligned_le16(&st->status_buf[1]);
831 static bool adxl367_push_event(struct iio_dev *indio_dev, u8 status)
835 if (FIELD_GET(ADXL367_STATUS_ACT_MASK, status))
836 ev_dir = IIO_EV_DIR_RISING;
837 else if (FIELD_GET(ADXL367_STATUS_INACT_MASK, status))
838 ev_dir = IIO_EV_DIR_FALLING;
842 iio_push_event(indio_dev,
843 IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
844 IIO_EV_TYPE_THRESH, ev_dir),
845 iio_get_time_ns(indio_dev));
850 static bool adxl367_push_fifo_data(struct iio_dev *indio_dev, u8 status,
853 struct adxl367_state *st = iio_priv(indio_dev);
857 if (!FIELD_GET(ADXL367_STATUS_FIFO_FULL_MASK, status))
860 fifo_entries -= fifo_entries % st->fifo_set_size;
862 ret = st->ops->read_fifo(st->context, st->fifo_buf, fifo_entries);
864 dev_err(st->dev, "Failed to read FIFO: %d\n", ret);
868 for (i = 0; i < fifo_entries; i += st->fifo_set_size)
869 iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);
874 static irqreturn_t adxl367_irq_handler(int irq, void *private)
876 struct iio_dev *indio_dev = private;
877 struct adxl367_state *st = iio_priv(indio_dev);
883 ret = adxl367_get_status(st, &status, &fifo_entries);
887 handled = adxl367_push_event(indio_dev, status);
888 handled |= adxl367_push_fifo_data(indio_dev, status, fifo_entries);
890 return handled ? IRQ_HANDLED : IRQ_NONE;
893 static int adxl367_reg_access(struct iio_dev *indio_dev,
895 unsigned int writeval,
896 unsigned int *readval)
898 struct adxl367_state *st = iio_priv(indio_dev);
901 return regmap_read(st->regmap, reg, readval);
903 return regmap_write(st->regmap, reg, writeval);
906 static int adxl367_read_raw(struct iio_dev *indio_dev,
907 struct iio_chan_spec const *chan,
908 int *val, int *val2, long info)
910 struct adxl367_state *st = iio_priv(indio_dev);
913 case IIO_CHAN_INFO_RAW:
914 return adxl367_read_sample(indio_dev, chan, val);
915 case IIO_CHAN_INFO_SCALE:
916 switch (chan->type) {
918 mutex_lock(&st->lock);
919 *val = adxl367_range_scale_tbl[st->range][0];
920 *val2 = adxl367_range_scale_tbl[st->range][1];
921 mutex_unlock(&st->lock);
922 return IIO_VAL_INT_PLUS_NANO;
925 *val2 = ADXL367_TEMP_PER_C;
926 return IIO_VAL_FRACTIONAL;
928 *val = ADXL367_VOLTAGE_MAX_MV;
929 *val2 = ADXL367_VOLTAGE_MAX_RAW;
930 return IIO_VAL_FRACTIONAL;
934 case IIO_CHAN_INFO_OFFSET:
935 switch (chan->type) {
937 *val = 25 * ADXL367_TEMP_PER_C - ADXL367_TEMP_25C;
940 *val = ADXL367_VOLTAGE_OFFSET;
945 case IIO_CHAN_INFO_SAMP_FREQ:
946 mutex_lock(&st->lock);
947 *val = adxl367_samp_freq_tbl[st->odr][0];
948 *val2 = adxl367_samp_freq_tbl[st->odr][1];
949 mutex_unlock(&st->lock);
950 return IIO_VAL_INT_PLUS_MICRO;
956 static int adxl367_write_raw(struct iio_dev *indio_dev,
957 struct iio_chan_spec const *chan,
958 int val, int val2, long info)
960 struct adxl367_state *st = iio_priv(indio_dev);
964 case IIO_CHAN_INFO_SAMP_FREQ: {
965 enum adxl367_odr odr;
967 ret = adxl367_find_odr(st, val, val2, &odr);
971 return adxl367_set_odr(indio_dev, odr);
973 case IIO_CHAN_INFO_SCALE: {
974 enum adxl367_range range;
976 ret = adxl367_find_range(st, val, val2, &range);
980 return adxl367_set_range(indio_dev, range);
987 static int adxl367_write_raw_get_fmt(struct iio_dev *indio_dev,
988 struct iio_chan_spec const *chan,
992 case IIO_CHAN_INFO_SCALE:
993 if (chan->type != IIO_ACCEL)
996 return IIO_VAL_INT_PLUS_NANO;
998 return IIO_VAL_INT_PLUS_MICRO;
1002 static int adxl367_read_avail(struct iio_dev *indio_dev,
1003 struct iio_chan_spec const *chan,
1004 const int **vals, int *type, int *length,
1008 case IIO_CHAN_INFO_SCALE:
1009 if (chan->type != IIO_ACCEL)
1012 *vals = (int *)adxl367_range_scale_tbl;
1013 *type = IIO_VAL_INT_PLUS_NANO;
1014 *length = ARRAY_SIZE(adxl367_range_scale_tbl) * 2;
1015 return IIO_AVAIL_LIST;
1016 case IIO_CHAN_INFO_SAMP_FREQ:
1017 *vals = (int *)adxl367_samp_freq_tbl;
1018 *type = IIO_VAL_INT_PLUS_MICRO;
1019 *length = ARRAY_SIZE(adxl367_samp_freq_tbl) * 2;
1020 return IIO_AVAIL_LIST;
1026 static int adxl367_read_event_value(struct iio_dev *indio_dev,
1027 const struct iio_chan_spec *chan,
1028 enum iio_event_type type,
1029 enum iio_event_direction dir,
1030 enum iio_event_info info,
1031 int *val, int *val2)
1033 struct adxl367_state *st = iio_priv(indio_dev);
1036 case IIO_EV_INFO_VALUE: {
1038 case IIO_EV_DIR_RISING:
1039 mutex_lock(&st->lock);
1040 *val = st->act_threshold;
1041 mutex_unlock(&st->lock);
1043 case IIO_EV_DIR_FALLING:
1044 mutex_lock(&st->lock);
1045 *val = st->inact_threshold;
1046 mutex_unlock(&st->lock);
1052 case IIO_EV_INFO_PERIOD:
1054 case IIO_EV_DIR_RISING:
1055 mutex_lock(&st->lock);
1056 *val = st->act_time_ms;
1057 mutex_unlock(&st->lock);
1059 return IIO_VAL_FRACTIONAL;
1060 case IIO_EV_DIR_FALLING:
1061 mutex_lock(&st->lock);
1062 *val = st->inact_time_ms;
1063 mutex_unlock(&st->lock);
1065 return IIO_VAL_FRACTIONAL;
1074 static int adxl367_write_event_value(struct iio_dev *indio_dev,
1075 const struct iio_chan_spec *chan,
1076 enum iio_event_type type,
1077 enum iio_event_direction dir,
1078 enum iio_event_info info,
1081 struct adxl367_state *st = iio_priv(indio_dev);
1084 case IIO_EV_INFO_VALUE:
1089 case IIO_EV_DIR_RISING:
1090 return adxl367_set_act_threshold(st, ADXL367_ACTIVITY, val);
1091 case IIO_EV_DIR_FALLING:
1092 return adxl367_set_act_threshold(st, ADXL367_INACTIVITY, val);
1096 case IIO_EV_INFO_PERIOD:
1100 val = val * 1000 + DIV_ROUND_UP(val2, 1000);
1102 case IIO_EV_DIR_RISING:
1103 return adxl367_set_act_time_ms(st, ADXL367_ACTIVITY, val);
1104 case IIO_EV_DIR_FALLING:
1105 return adxl367_set_act_time_ms(st, ADXL367_INACTIVITY, val);
1114 static int adxl367_read_event_config(struct iio_dev *indio_dev,
1115 const struct iio_chan_spec *chan,
1116 enum iio_event_type type,
1117 enum iio_event_direction dir)
1119 struct adxl367_state *st = iio_priv(indio_dev);
1124 case IIO_EV_DIR_RISING:
1125 ret = adxl367_get_act_interrupt_en(st, ADXL367_ACTIVITY, &en);
1127 case IIO_EV_DIR_FALLING:
1128 ret = adxl367_get_act_interrupt_en(st, ADXL367_INACTIVITY, &en);
1135 static int adxl367_write_event_config(struct iio_dev *indio_dev,
1136 const struct iio_chan_spec *chan,
1137 enum iio_event_type type,
1138 enum iio_event_direction dir,
1141 struct adxl367_state *st = iio_priv(indio_dev);
1142 enum adxl367_activity_type act;
1146 case IIO_EV_DIR_RISING:
1147 act = ADXL367_ACTIVITY;
1149 case IIO_EV_DIR_FALLING:
1150 act = ADXL367_INACTIVITY;
1156 ret = iio_device_claim_direct_mode(indio_dev);
1160 mutex_lock(&st->lock);
1162 ret = adxl367_set_measure_en(st, false);
1166 ret = adxl367_set_act_interrupt_en(st, act, state);
1170 ret = adxl367_set_act_en(st, act, state ? ADCL367_ACT_REF_ENABLED
1171 : ADXL367_ACT_DISABLED);
1175 ret = adxl367_set_measure_en(st, true);
1178 mutex_unlock(&st->lock);
1180 iio_device_release_direct_mode(indio_dev);
1185 static ssize_t adxl367_get_fifo_enabled(struct device *dev,
1186 struct device_attribute *attr,
1189 struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev));
1190 enum adxl367_fifo_mode fifo_mode;
1193 ret = adxl367_get_fifo_mode(st, &fifo_mode);
1197 return sysfs_emit(buf, "%d\n", fifo_mode != ADXL367_FIFO_MODE_DISABLED);
1200 static ssize_t adxl367_get_fifo_watermark(struct device *dev,
1201 struct device_attribute *attr,
1204 struct adxl367_state *st = iio_priv(dev_to_iio_dev(dev));
1205 unsigned int fifo_watermark;
1207 mutex_lock(&st->lock);
1208 fifo_watermark = st->fifo_watermark;
1209 mutex_unlock(&st->lock);
1211 return sysfs_emit(buf, "%d\n", fifo_watermark);
1214 static IIO_CONST_ATTR(hwfifo_watermark_min, "1");
1215 static IIO_CONST_ATTR(hwfifo_watermark_max,
1216 __stringify(ADXL367_FIFO_MAX_WATERMARK));
1217 static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
1218 adxl367_get_fifo_watermark, NULL, 0);
1219 static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
1220 adxl367_get_fifo_enabled, NULL, 0);
1222 static const struct attribute *adxl367_fifo_attributes[] = {
1223 &iio_const_attr_hwfifo_watermark_min.dev_attr.attr,
1224 &iio_const_attr_hwfifo_watermark_max.dev_attr.attr,
1225 &iio_dev_attr_hwfifo_watermark.dev_attr.attr,
1226 &iio_dev_attr_hwfifo_enabled.dev_attr.attr,
1230 static int adxl367_set_watermark(struct iio_dev *indio_dev, unsigned int val)
1232 struct adxl367_state *st = iio_priv(indio_dev);
1235 if (val > ADXL367_FIFO_MAX_WATERMARK)
1238 mutex_lock(&st->lock);
1240 ret = adxl367_set_measure_en(st, false);
1244 ret = adxl367_set_fifo_watermark(st, val);
1248 ret = adxl367_set_measure_en(st, true);
1251 mutex_unlock(&st->lock);
1256 static bool adxl367_find_mask_fifo_format(const unsigned long *scan_mask,
1257 enum adxl367_fifo_format *fifo_format)
1259 size_t size = ARRAY_SIZE(adxl367_fifo_formats);
1262 for (i = 0; i < size; i++)
1263 if (*scan_mask == adxl367_channel_masks[i])
1269 *fifo_format = adxl367_fifo_formats[i];
1274 static int adxl367_update_scan_mode(struct iio_dev *indio_dev,
1275 const unsigned long *active_scan_mask)
1277 struct adxl367_state *st = iio_priv(indio_dev);
1278 enum adxl367_fifo_format fifo_format;
1279 unsigned int fifo_set_size;
1282 if (!adxl367_find_mask_fifo_format(active_scan_mask, &fifo_format))
1285 fifo_set_size = bitmap_weight(active_scan_mask, indio_dev->masklength);
1287 mutex_lock(&st->lock);
1289 ret = adxl367_set_measure_en(st, false);
1293 ret = adxl367_set_fifo_format(st, fifo_format);
1297 ret = adxl367_set_fifo_set_size(st, fifo_set_size);
1301 ret = adxl367_set_measure_en(st, true);
1304 mutex_unlock(&st->lock);
1309 static int adxl367_buffer_postenable(struct iio_dev *indio_dev)
1311 struct adxl367_state *st = iio_priv(indio_dev);
1314 mutex_lock(&st->lock);
1316 ret = adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask,
1321 ret = adxl367_set_measure_en(st, false);
1325 ret = adxl367_set_fifo_watermark_interrupt_en(st, true);
1329 ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_STREAM);
1333 ret = adxl367_set_measure_en(st, true);
1336 mutex_unlock(&st->lock);
1341 static int adxl367_buffer_predisable(struct iio_dev *indio_dev)
1343 struct adxl367_state *st = iio_priv(indio_dev);
1346 mutex_lock(&st->lock);
1348 ret = adxl367_set_measure_en(st, false);
1352 ret = adxl367_set_fifo_mode(st, ADXL367_FIFO_MODE_DISABLED);
1356 ret = adxl367_set_fifo_watermark_interrupt_en(st, false);
1360 ret = adxl367_set_measure_en(st, true);
1364 ret = adxl367_set_temp_adc_mask_en(st, indio_dev->active_scan_mask,
1368 mutex_unlock(&st->lock);
1373 static const struct iio_buffer_setup_ops adxl367_buffer_ops = {
1374 .postenable = adxl367_buffer_postenable,
1375 .predisable = adxl367_buffer_predisable,
1378 static const struct iio_info adxl367_info = {
1379 .read_raw = adxl367_read_raw,
1380 .write_raw = adxl367_write_raw,
1381 .write_raw_get_fmt = adxl367_write_raw_get_fmt,
1382 .read_avail = adxl367_read_avail,
1383 .read_event_config = adxl367_read_event_config,
1384 .write_event_config = adxl367_write_event_config,
1385 .read_event_value = adxl367_read_event_value,
1386 .write_event_value = adxl367_write_event_value,
1387 .debugfs_reg_access = adxl367_reg_access,
1388 .hwfifo_set_watermark = adxl367_set_watermark,
1389 .update_scan_mode = adxl367_update_scan_mode,
1392 static const struct iio_event_spec adxl367_events[] = {
1394 .type = IIO_EV_TYPE_MAG_REFERENCED,
1395 .dir = IIO_EV_DIR_RISING,
1396 .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
1397 BIT(IIO_EV_INFO_PERIOD) |
1398 BIT(IIO_EV_INFO_VALUE),
1401 .type = IIO_EV_TYPE_MAG_REFERENCED,
1402 .dir = IIO_EV_DIR_FALLING,
1403 .mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
1404 BIT(IIO_EV_INFO_PERIOD) |
1405 BIT(IIO_EV_INFO_VALUE),
1409 #define ADXL367_ACCEL_CHANNEL(index, reg, axis) { \
1410 .type = IIO_ACCEL, \
1413 .channel2 = IIO_MOD_##axis, \
1414 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1415 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1416 .info_mask_shared_by_type_available = BIT(IIO_CHAN_INFO_SCALE), \
1417 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1418 .info_mask_shared_by_all_available = \
1419 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1420 .event_spec = adxl367_events, \
1421 .num_event_specs = ARRAY_SIZE(adxl367_events), \
1422 .scan_index = (index), \
1426 .storagebits = 16, \
1427 .endianness = IIO_BE, \
1431 #define ADXL367_CHANNEL(index, reg, _type) { \
1434 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
1435 BIT(IIO_CHAN_INFO_OFFSET) | \
1436 BIT(IIO_CHAN_INFO_SCALE), \
1437 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ), \
1438 .scan_index = (index), \
1442 .storagebits = 16, \
1443 .endianness = IIO_BE, \
1447 static const struct iio_chan_spec adxl367_channels[] = {
1448 ADXL367_ACCEL_CHANNEL(ADXL367_X_CHANNEL_INDEX, ADXL367_REG_X_DATA_H, X),
1449 ADXL367_ACCEL_CHANNEL(ADXL367_Y_CHANNEL_INDEX, ADXL367_REG_Y_DATA_H, Y),
1450 ADXL367_ACCEL_CHANNEL(ADXL367_Z_CHANNEL_INDEX, ADXL367_REG_Z_DATA_H, Z),
1451 ADXL367_CHANNEL(ADXL367_TEMP_CHANNEL_INDEX, ADXL367_REG_TEMP_DATA_H,
1453 ADXL367_CHANNEL(ADXL367_EX_ADC_CHANNEL_INDEX, ADXL367_REG_EX_ADC_DATA_H,
1457 static int adxl367_verify_devid(struct adxl367_state *st)
1462 ret = regmap_read_poll_timeout(st->regmap, ADXL367_REG_DEVID, val,
1463 val == ADXL367_DEVID_AD, 1000, 10000);
1465 return dev_err_probe(st->dev, -ENODEV,
1466 "Invalid dev id 0x%02X, expected 0x%02X\n",
1467 val, ADXL367_DEVID_AD);
1472 static int adxl367_setup(struct adxl367_state *st)
1476 ret = _adxl367_set_act_threshold(st, ADXL367_ACTIVITY,
1477 ADXL367_2G_RANGE_1G);
1481 ret = _adxl367_set_act_threshold(st, ADXL367_INACTIVITY,
1482 ADXL367_2G_RANGE_100MG);
1486 ret = adxl367_set_act_proc_mode(st, ADXL367_LOOPED);
1490 ret = _adxl367_set_odr(st, ADXL367_ODR_400HZ);
1494 ret = _adxl367_set_act_time_ms(st, 10);
1498 ret = _adxl367_set_inact_time_ms(st, 10000);
1502 return adxl367_set_measure_en(st, true);
1505 static void adxl367_disable_regulators(void *data)
1507 struct adxl367_state *st = data;
1509 regulator_bulk_disable(ARRAY_SIZE(st->regulators), st->regulators);
1512 int adxl367_probe(struct device *dev, const struct adxl367_ops *ops,
1513 void *context, struct regmap *regmap, int irq)
1515 struct iio_dev *indio_dev;
1516 struct adxl367_state *st;
1519 indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
1523 st = iio_priv(indio_dev);
1525 st->regmap = regmap;
1526 st->context = context;
1529 mutex_init(&st->lock);
1531 indio_dev->channels = adxl367_channels;
1532 indio_dev->num_channels = ARRAY_SIZE(adxl367_channels);
1533 indio_dev->available_scan_masks = adxl367_channel_masks;
1534 indio_dev->name = "adxl367";
1535 indio_dev->info = &adxl367_info;
1536 indio_dev->modes = INDIO_DIRECT_MODE;
1538 st->regulators[0].supply = "vdd";
1539 st->regulators[1].supply = "vddio";
1541 ret = devm_regulator_bulk_get(st->dev, ARRAY_SIZE(st->regulators),
1544 return dev_err_probe(st->dev, ret,
1545 "Failed to get regulators\n");
1547 ret = regulator_bulk_enable(ARRAY_SIZE(st->regulators), st->regulators);
1549 return dev_err_probe(st->dev, ret,
1550 "Failed to enable regulators\n");
1552 ret = devm_add_action_or_reset(st->dev, adxl367_disable_regulators, st);
1554 return dev_err_probe(st->dev, ret,
1555 "Failed to add regulators disable action\n");
1557 ret = regmap_write(st->regmap, ADXL367_REG_RESET, ADXL367_RESET_CODE);
1561 ret = adxl367_verify_devid(st);
1565 ret = adxl367_setup(st);
1569 ret = devm_iio_kfifo_buffer_setup_ext(st->dev, indio_dev,
1570 &adxl367_buffer_ops,
1571 adxl367_fifo_attributes);
1575 ret = devm_request_threaded_irq(st->dev, irq, NULL,
1576 adxl367_irq_handler, IRQF_ONESHOT,
1577 indio_dev->name, indio_dev);
1579 return dev_err_probe(st->dev, ret, "Failed to request irq\n");
1581 return devm_iio_device_register(dev, indio_dev);
1583 EXPORT_SYMBOL_NS_GPL(adxl367_probe, IIO_ADXL367);
1585 MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>");
1586 MODULE_DESCRIPTION("Analog Devices ADXL367 3-axis accelerometer driver");
1587 MODULE_LICENSE("GPL");