1 // SPDX-License-Identifier: GPL-2.0+
3 * ADXL372 3-Axis Digital Accelerometer core driver
5 * Copyright 2018 Analog Devices Inc.
8 #include <linux/bitops.h>
9 #include <linux/interrupt.h>
10 #include <linux/irq.h>
11 #include <linux/module.h>
12 #include <linux/regmap.h>
13 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/events.h>
19 #include <linux/iio/trigger.h>
20 #include <linux/iio/trigger_consumer.h>
21 #include <linux/iio/triggered_buffer.h>
25 /* ADXL372 registers definition */
26 #define ADXL372_DEVID 0x00
27 #define ADXL372_DEVID_MST 0x01
28 #define ADXL372_PARTID 0x02
29 #define ADXL372_STATUS_1 0x04
30 #define ADXL372_STATUS_2 0x05
31 #define ADXL372_FIFO_ENTRIES_2 0x06
32 #define ADXL372_FIFO_ENTRIES_1 0x07
33 #define ADXL372_X_DATA_H 0x08
34 #define ADXL372_X_DATA_L 0x09
35 #define ADXL372_Y_DATA_H 0x0A
36 #define ADXL372_Y_DATA_L 0x0B
37 #define ADXL372_Z_DATA_H 0x0C
38 #define ADXL372_Z_DATA_L 0x0D
39 #define ADXL372_X_MAXPEAK_H 0x15
40 #define ADXL372_X_MAXPEAK_L 0x16
41 #define ADXL372_Y_MAXPEAK_H 0x17
42 #define ADXL372_Y_MAXPEAK_L 0x18
43 #define ADXL372_Z_MAXPEAK_H 0x19
44 #define ADXL372_Z_MAXPEAK_L 0x1A
45 #define ADXL372_OFFSET_X 0x20
46 #define ADXL372_OFFSET_Y 0x21
47 #define ADXL372_OFFSET_Z 0x22
48 #define ADXL372_X_THRESH_ACT_H 0x23
49 #define ADXL372_X_THRESH_ACT_L 0x24
50 #define ADXL372_Y_THRESH_ACT_H 0x25
51 #define ADXL372_Y_THRESH_ACT_L 0x26
52 #define ADXL372_Z_THRESH_ACT_H 0x27
53 #define ADXL372_Z_THRESH_ACT_L 0x28
54 #define ADXL372_TIME_ACT 0x29
55 #define ADXL372_X_THRESH_INACT_H 0x2A
56 #define ADXL372_X_THRESH_INACT_L 0x2B
57 #define ADXL372_Y_THRESH_INACT_H 0x2C
58 #define ADXL372_Y_THRESH_INACT_L 0x2D
59 #define ADXL372_Z_THRESH_INACT_H 0x2E
60 #define ADXL372_Z_THRESH_INACT_L 0x2F
61 #define ADXL372_TIME_INACT_H 0x30
62 #define ADXL372_TIME_INACT_L 0x31
63 #define ADXL372_X_THRESH_ACT2_H 0x32
64 #define ADXL372_X_THRESH_ACT2_L 0x33
65 #define ADXL372_Y_THRESH_ACT2_H 0x34
66 #define ADXL372_Y_THRESH_ACT2_L 0x35
67 #define ADXL372_Z_THRESH_ACT2_H 0x36
68 #define ADXL372_Z_THRESH_ACT2_L 0x37
69 #define ADXL372_HPF 0x38
70 #define ADXL372_FIFO_SAMPLES 0x39
71 #define ADXL372_FIFO_CTL 0x3A
72 #define ADXL372_INT1_MAP 0x3B
73 #define ADXL372_INT2_MAP 0x3C
74 #define ADXL372_TIMING 0x3D
75 #define ADXL372_MEASURE 0x3E
76 #define ADXL372_POWER_CTL 0x3F
77 #define ADXL372_SELF_TEST 0x40
78 #define ADXL372_RESET 0x41
79 #define ADXL372_FIFO_DATA 0x42
81 #define ADXL372_DEVID_VAL 0xAD
82 #define ADXL372_PARTID_VAL 0xFA
83 #define ADXL372_RESET_CODE 0x52
85 /* ADXL372_POWER_CTL */
86 #define ADXL372_POWER_CTL_MODE_MSK GENMASK_ULL(1, 0)
87 #define ADXL372_POWER_CTL_MODE(x) (((x) & 0x3) << 0)
90 #define ADXL372_MEASURE_LINKLOOP_MSK GENMASK_ULL(5, 4)
91 #define ADXL372_MEASURE_LINKLOOP_MODE(x) (((x) & 0x3) << 4)
92 #define ADXL372_MEASURE_BANDWIDTH_MSK GENMASK_ULL(2, 0)
93 #define ADXL372_MEASURE_BANDWIDTH_MODE(x) (((x) & 0x7) << 0)
96 #define ADXL372_TIMING_ODR_MSK GENMASK_ULL(7, 5)
97 #define ADXL372_TIMING_ODR_MODE(x) (((x) & 0x7) << 5)
99 /* ADXL372_FIFO_CTL */
100 #define ADXL372_FIFO_CTL_FORMAT_MSK GENMASK(5, 3)
101 #define ADXL372_FIFO_CTL_FORMAT_MODE(x) (((x) & 0x7) << 3)
102 #define ADXL372_FIFO_CTL_MODE_MSK GENMASK(2, 1)
103 #define ADXL372_FIFO_CTL_MODE_MODE(x) (((x) & 0x3) << 1)
104 #define ADXL372_FIFO_CTL_SAMPLES_MSK BIT(1)
105 #define ADXL372_FIFO_CTL_SAMPLES_MODE(x) (((x) > 0xFF) ? 1 : 0)
107 /* ADXL372_STATUS_1 */
108 #define ADXL372_STATUS_1_DATA_RDY(x) (((x) >> 0) & 0x1)
109 #define ADXL372_STATUS_1_FIFO_RDY(x) (((x) >> 1) & 0x1)
110 #define ADXL372_STATUS_1_FIFO_FULL(x) (((x) >> 2) & 0x1)
111 #define ADXL372_STATUS_1_FIFO_OVR(x) (((x) >> 3) & 0x1)
112 #define ADXL372_STATUS_1_USR_NVM_BUSY(x) (((x) >> 5) & 0x1)
113 #define ADXL372_STATUS_1_AWAKE(x) (((x) >> 6) & 0x1)
114 #define ADXL372_STATUS_1_ERR_USR_REGS(x) (((x) >> 7) & 0x1)
116 /* ADXL372_INT1_MAP */
117 #define ADXL372_INT1_MAP_DATA_RDY_MSK BIT(0)
118 #define ADXL372_INT1_MAP_DATA_RDY_MODE(x) (((x) & 0x1) << 0)
119 #define ADXL372_INT1_MAP_FIFO_RDY_MSK BIT(1)
120 #define ADXL372_INT1_MAP_FIFO_RDY_MODE(x) (((x) & 0x1) << 1)
121 #define ADXL372_INT1_MAP_FIFO_FULL_MSK BIT(2)
122 #define ADXL372_INT1_MAP_FIFO_FULL_MODE(x) (((x) & 0x1) << 2)
123 #define ADXL372_INT1_MAP_FIFO_OVR_MSK BIT(3)
124 #define ADXL372_INT1_MAP_FIFO_OVR_MODE(x) (((x) & 0x1) << 3)
125 #define ADXL372_INT1_MAP_INACT_MSK BIT(4)
126 #define ADXL372_INT1_MAP_INACT_MODE(x) (((x) & 0x1) << 4)
127 #define ADXL372_INT1_MAP_ACT_MSK BIT(5)
128 #define ADXL372_INT1_MAP_ACT_MODE(x) (((x) & 0x1) << 5)
129 #define ADXL372_INT1_MAP_AWAKE_MSK BIT(6)
130 #define ADXL372_INT1_MAP_AWAKE_MODE(x) (((x) & 0x1) << 6)
131 #define ADXL372_INT1_MAP_LOW_MSK BIT(7)
132 #define ADXL372_INT1_MAP_LOW_MODE(x) (((x) & 0x1) << 7)
134 /* The ADXL372 includes a deep, 512 sample FIFO buffer */
135 #define ADXL372_FIFO_SIZE 512
138 * At +/- 200g with 12-bit resolution, scale is computed as:
139 * (200 + 200) * 9.81 / (2^12 - 1) = 0.958241
141 #define ADXL372_USCALE 958241
143 enum adxl372_op_mode
{
147 ADXL372_FULL_BW_MEASUREMENT
,
150 enum adxl372_act_proc_mode
{
156 enum adxl372_th_activity
{
170 enum adxl372_bandwidth
{
178 static const unsigned int adxl372_th_reg_high_addr
[3] = {
179 [ADXL372_ACTIVITY
] = ADXL372_X_THRESH_ACT_H
,
180 [ADXL372_ACTIVITY2
] = ADXL372_X_THRESH_ACT2_H
,
181 [ADXL372_INACTIVITY
] = ADXL372_X_THRESH_INACT_H
,
184 enum adxl372_fifo_format
{
192 ADXL372_XYZ_PEAK_FIFO
,
195 enum adxl372_fifo_mode
{
196 ADXL372_FIFO_BYPASSED
,
197 ADXL372_FIFO_STREAMED
,
198 ADXL372_FIFO_TRIGGERED
,
199 ADXL372_FIFO_OLD_SAVED
202 static const int adxl372_samp_freq_tbl
[5] = {
203 400, 800, 1600, 3200, 6400,
206 static const int adxl372_bw_freq_tbl
[5] = {
207 200, 400, 800, 1600, 3200,
210 struct adxl372_axis_lookup
{
212 enum adxl372_fifo_format fifo_format
;
215 static const struct adxl372_axis_lookup adxl372_axis_lookup_table
[] = {
216 { BIT(0), ADXL372_X_FIFO
},
217 { BIT(1), ADXL372_Y_FIFO
},
218 { BIT(2), ADXL372_Z_FIFO
},
219 { BIT(0) | BIT(1), ADXL372_XY_FIFO
},
220 { BIT(0) | BIT(2), ADXL372_XZ_FIFO
},
221 { BIT(1) | BIT(2), ADXL372_YZ_FIFO
},
222 { BIT(0) | BIT(1) | BIT(2), ADXL372_XYZ_FIFO
},
225 #define ADXL372_ACCEL_CHANNEL(index, reg, axis) { \
229 .channel2 = IIO_MOD_##axis, \
230 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
231 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
232 BIT(IIO_CHAN_INFO_SAMP_FREQ) | \
233 BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY), \
234 .scan_index = index, \
243 static const struct iio_chan_spec adxl372_channels
[] = {
244 ADXL372_ACCEL_CHANNEL(0, ADXL372_X_DATA_H
, X
),
245 ADXL372_ACCEL_CHANNEL(1, ADXL372_Y_DATA_H
, Y
),
246 ADXL372_ACCEL_CHANNEL(2, ADXL372_Z_DATA_H
, Z
),
249 struct adxl372_state
{
252 struct regmap
*regmap
;
253 struct iio_trigger
*dready_trig
;
254 enum adxl372_fifo_mode fifo_mode
;
255 enum adxl372_fifo_format fifo_format
;
256 enum adxl372_op_mode op_mode
;
257 enum adxl372_act_proc_mode act_proc_mode
;
258 enum adxl372_odr odr
;
259 enum adxl372_bandwidth bw
;
266 __be16 fifo_buf
[ADXL372_FIFO_SIZE
];
269 static const unsigned long adxl372_channel_masks
[] = {
270 BIT(0), BIT(1), BIT(2),
274 BIT(0) | BIT(1) | BIT(2),
278 static int adxl372_read_axis(struct adxl372_state
*st
, u8 addr
)
283 ret
= regmap_bulk_read(st
->regmap
, addr
, ®val
, sizeof(regval
));
287 return be16_to_cpu(regval
);
290 static int adxl372_set_op_mode(struct adxl372_state
*st
,
291 enum adxl372_op_mode op_mode
)
295 ret
= regmap_update_bits(st
->regmap
, ADXL372_POWER_CTL
,
296 ADXL372_POWER_CTL_MODE_MSK
,
297 ADXL372_POWER_CTL_MODE(op_mode
));
301 st
->op_mode
= op_mode
;
306 static int adxl372_set_odr(struct adxl372_state
*st
,
307 enum adxl372_odr odr
)
311 ret
= regmap_update_bits(st
->regmap
, ADXL372_TIMING
,
312 ADXL372_TIMING_ODR_MSK
,
313 ADXL372_TIMING_ODR_MODE(odr
));
322 static int adxl372_find_closest_match(const int *array
,
323 unsigned int size
, int val
)
327 for (i
= 0; i
< size
; i
++) {
335 static int adxl372_set_bandwidth(struct adxl372_state
*st
,
336 enum adxl372_bandwidth bw
)
340 ret
= regmap_update_bits(st
->regmap
, ADXL372_MEASURE
,
341 ADXL372_MEASURE_BANDWIDTH_MSK
,
342 ADXL372_MEASURE_BANDWIDTH_MODE(bw
));
351 static int adxl372_set_act_proc_mode(struct adxl372_state
*st
,
352 enum adxl372_act_proc_mode mode
)
356 ret
= regmap_update_bits(st
->regmap
,
358 ADXL372_MEASURE_LINKLOOP_MSK
,
359 ADXL372_MEASURE_LINKLOOP_MODE(mode
));
363 st
->act_proc_mode
= mode
;
368 static int adxl372_set_activity_threshold(struct adxl372_state
*st
,
369 enum adxl372_th_activity act
,
370 bool ref_en
, bool enable
,
371 unsigned int threshold
)
373 unsigned char buf
[6];
374 unsigned char th_reg_high_val
, th_reg_low_val
, th_reg_high_addr
;
376 /* scale factor is 100 mg/code */
377 th_reg_high_val
= (threshold
/ 100) >> 3;
378 th_reg_low_val
= ((threshold
/ 100) << 5) | (ref_en
<< 1) | enable
;
379 th_reg_high_addr
= adxl372_th_reg_high_addr
[act
];
381 buf
[0] = th_reg_high_val
;
382 buf
[1] = th_reg_low_val
;
383 buf
[2] = th_reg_high_val
;
384 buf
[3] = th_reg_low_val
;
385 buf
[4] = th_reg_high_val
;
386 buf
[5] = th_reg_low_val
;
388 return regmap_bulk_write(st
->regmap
, th_reg_high_addr
,
389 buf
, ARRAY_SIZE(buf
));
392 static int adxl372_set_activity_time_ms(struct adxl372_state
*st
,
393 unsigned int act_time_ms
)
395 unsigned int reg_val
, scale_factor
;
399 * 3.3 ms per code is the scale factor of the TIME_ACT register for
400 * ODR = 6400 Hz. It is 6.6 ms per code for ODR = 3200 Hz and below.
402 if (st
->odr
== ADXL372_ODR_6400HZ
)
407 reg_val
= DIV_ROUND_CLOSEST(act_time_ms
* 1000, scale_factor
);
409 /* TIME_ACT register is 8 bits wide */
413 ret
= regmap_write(st
->regmap
, ADXL372_TIME_ACT
, reg_val
);
417 st
->act_time_ms
= act_time_ms
;
422 static int adxl372_set_inactivity_time_ms(struct adxl372_state
*st
,
423 unsigned int inact_time_ms
)
425 unsigned int reg_val_h
, reg_val_l
, res
, scale_factor
;
429 * 13 ms per code is the scale factor of the TIME_INACT register for
430 * ODR = 6400 Hz. It is 26 ms per code for ODR = 3200 Hz and below.
432 if (st
->odr
== ADXL372_ODR_6400HZ
)
437 res
= DIV_ROUND_CLOSEST(inact_time_ms
, scale_factor
);
438 reg_val_h
= (res
>> 8) & 0xFF;
439 reg_val_l
= res
& 0xFF;
441 ret
= regmap_write(st
->regmap
, ADXL372_TIME_INACT_H
, reg_val_h
);
445 ret
= regmap_write(st
->regmap
, ADXL372_TIME_INACT_L
, reg_val_l
);
449 st
->inact_time_ms
= inact_time_ms
;
454 static int adxl372_set_interrupts(struct adxl372_state
*st
,
455 unsigned char int1_bitmask
,
456 unsigned char int2_bitmask
)
460 ret
= regmap_write(st
->regmap
, ADXL372_INT1_MAP
, int1_bitmask
);
464 return regmap_write(st
->regmap
, ADXL372_INT2_MAP
, int2_bitmask
);
467 static int adxl372_configure_fifo(struct adxl372_state
*st
)
469 unsigned int fifo_samples
, fifo_ctl
;
472 /* FIFO must be configured while in standby mode */
473 ret
= adxl372_set_op_mode(st
, ADXL372_STANDBY
);
477 fifo_samples
= st
->watermark
& 0xFF;
478 fifo_ctl
= ADXL372_FIFO_CTL_FORMAT_MODE(st
->fifo_format
) |
479 ADXL372_FIFO_CTL_MODE_MODE(st
->fifo_mode
) |
480 ADXL372_FIFO_CTL_SAMPLES_MODE(st
->watermark
);
482 ret
= regmap_write(st
->regmap
, ADXL372_FIFO_SAMPLES
, fifo_samples
);
486 ret
= regmap_write(st
->regmap
, ADXL372_FIFO_CTL
, fifo_ctl
);
490 return adxl372_set_op_mode(st
, ADXL372_FULL_BW_MEASUREMENT
);
493 static int adxl372_get_status(struct adxl372_state
*st
,
494 u8
*status1
, u8
*status2
,
501 /* STATUS1, STATUS2, FIFO_ENTRIES2 and FIFO_ENTRIES are adjacent regs */
502 ret
= regmap_bulk_read(st
->regmap
, ADXL372_STATUS_1
,
507 val
= be32_to_cpu(buf
);
509 *status1
= (val
>> 24) & 0x0F;
510 *status2
= (val
>> 16) & 0x0F;
512 * FIFO_ENTRIES contains the least significant byte, and FIFO_ENTRIES2
513 * contains the two most significant bits
515 *fifo_entries
= val
& 0x3FF;
520 static irqreturn_t
adxl372_trigger_handler(int irq
, void *p
)
522 struct iio_poll_func
*pf
= p
;
523 struct iio_dev
*indio_dev
= pf
->indio_dev
;
524 struct adxl372_state
*st
= iio_priv(indio_dev
);
529 ret
= adxl372_get_status(st
, &status1
, &status2
, &fifo_entries
);
533 if (st
->fifo_mode
!= ADXL372_FIFO_BYPASSED
&&
534 ADXL372_STATUS_1_FIFO_FULL(status1
)) {
536 * When reading data from multiple axes from the FIFO,
537 * to ensure that data is not overwritten and stored out
538 * of order at least one sample set must be left in the
539 * FIFO after every read.
541 fifo_entries
-= st
->fifo_set_size
;
543 /* Read data from the FIFO */
544 ret
= regmap_noinc_read(st
->regmap
, ADXL372_FIFO_DATA
,
546 fifo_entries
* sizeof(u16
));
550 /* Each sample is 2 bytes */
551 for (i
= 0; i
< fifo_entries
* sizeof(u16
);
552 i
+= st
->fifo_set_size
* sizeof(u16
))
553 iio_push_to_buffers(indio_dev
, &st
->fifo_buf
[i
]);
556 iio_trigger_notify_done(indio_dev
->trig
);
560 static int adxl372_setup(struct adxl372_state
*st
)
565 ret
= regmap_read(st
->regmap
, ADXL372_DEVID
, ®val
);
569 if (regval
!= ADXL372_DEVID_VAL
) {
570 dev_err(st
->dev
, "Invalid chip id %x\n", regval
);
574 ret
= adxl372_set_op_mode(st
, ADXL372_STANDBY
);
578 /* Set threshold for activity detection to 1g */
579 ret
= adxl372_set_activity_threshold(st
, ADXL372_ACTIVITY
,
584 /* Set threshold for inactivity detection to 100mg */
585 ret
= adxl372_set_activity_threshold(st
, ADXL372_INACTIVITY
,
590 /* Set activity processing in Looped mode */
591 ret
= adxl372_set_act_proc_mode(st
, ADXL372_LOOPED
);
595 ret
= adxl372_set_odr(st
, ADXL372_ODR_6400HZ
);
599 ret
= adxl372_set_bandwidth(st
, ADXL372_BW_3200HZ
);
603 /* Set activity timer to 1ms */
604 ret
= adxl372_set_activity_time_ms(st
, 1);
608 /* Set inactivity timer to 10s */
609 ret
= adxl372_set_inactivity_time_ms(st
, 10000);
613 /* Set the mode of operation to full bandwidth measurement mode */
614 return adxl372_set_op_mode(st
, ADXL372_FULL_BW_MEASUREMENT
);
617 static int adxl372_reg_access(struct iio_dev
*indio_dev
,
619 unsigned int writeval
,
620 unsigned int *readval
)
622 struct adxl372_state
*st
= iio_priv(indio_dev
);
625 return regmap_read(st
->regmap
, reg
, readval
);
627 return regmap_write(st
->regmap
, reg
, writeval
);
630 static int adxl372_read_raw(struct iio_dev
*indio_dev
,
631 struct iio_chan_spec
const *chan
,
632 int *val
, int *val2
, long info
)
634 struct adxl372_state
*st
= iio_priv(indio_dev
);
638 case IIO_CHAN_INFO_RAW
:
639 ret
= iio_device_claim_direct_mode(indio_dev
);
643 ret
= adxl372_read_axis(st
, chan
->address
);
644 iio_device_release_direct_mode(indio_dev
);
648 *val
= sign_extend32(ret
>> chan
->scan_type
.shift
,
649 chan
->scan_type
.realbits
- 1);
651 case IIO_CHAN_INFO_SCALE
:
653 *val2
= ADXL372_USCALE
;
654 return IIO_VAL_INT_PLUS_MICRO
;
655 case IIO_CHAN_INFO_SAMP_FREQ
:
656 *val
= adxl372_samp_freq_tbl
[st
->odr
];
658 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
659 *val
= adxl372_bw_freq_tbl
[st
->bw
];
666 static int adxl372_write_raw(struct iio_dev
*indio_dev
,
667 struct iio_chan_spec
const *chan
,
668 int val
, int val2
, long info
)
670 struct adxl372_state
*st
= iio_priv(indio_dev
);
671 int odr_index
, bw_index
, ret
;
674 case IIO_CHAN_INFO_SAMP_FREQ
:
675 odr_index
= adxl372_find_closest_match(adxl372_samp_freq_tbl
,
676 ARRAY_SIZE(adxl372_samp_freq_tbl
),
678 ret
= adxl372_set_odr(st
, odr_index
);
682 * The timer period depends on the ODR selected.
683 * At 3200 Hz and below, it is 6.6 ms; at 6400 Hz, it is 3.3 ms
685 ret
= adxl372_set_activity_time_ms(st
, st
->act_time_ms
);
689 * The timer period depends on the ODR selected.
690 * At 3200 Hz and below, it is 26 ms; at 6400 Hz, it is 13 ms
692 ret
= adxl372_set_inactivity_time_ms(st
, st
->inact_time_ms
);
696 * The maximum bandwidth is constrained to at most half of
697 * the ODR to ensure that the Nyquist criteria is not violated
699 if (st
->bw
> odr_index
)
700 ret
= adxl372_set_bandwidth(st
, odr_index
);
703 case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY
:
704 bw_index
= adxl372_find_closest_match(adxl372_bw_freq_tbl
,
705 ARRAY_SIZE(adxl372_bw_freq_tbl
),
707 return adxl372_set_bandwidth(st
, bw_index
);
713 static ssize_t
adxl372_show_filter_freq_avail(struct device
*dev
,
714 struct device_attribute
*attr
,
717 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
718 struct adxl372_state
*st
= iio_priv(indio_dev
);
722 for (i
= 0; i
<= st
->odr
; i
++)
723 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
724 "%d ", adxl372_bw_freq_tbl
[i
]);
731 static ssize_t
adxl372_get_fifo_enabled(struct device
*dev
,
732 struct device_attribute
*attr
,
735 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
736 struct adxl372_state
*st
= iio_priv(indio_dev
);
738 return sprintf(buf
, "%d\n", st
->fifo_mode
);
741 static ssize_t
adxl372_get_fifo_watermark(struct device
*dev
,
742 struct device_attribute
*attr
,
745 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
746 struct adxl372_state
*st
= iio_priv(indio_dev
);
748 return sprintf(buf
, "%d\n", st
->watermark
);
751 static IIO_CONST_ATTR(hwfifo_watermark_min
, "1");
752 static IIO_CONST_ATTR(hwfifo_watermark_max
,
753 __stringify(ADXL372_FIFO_SIZE
));
754 static IIO_DEVICE_ATTR(hwfifo_watermark
, 0444,
755 adxl372_get_fifo_watermark
, NULL
, 0);
756 static IIO_DEVICE_ATTR(hwfifo_enabled
, 0444,
757 adxl372_get_fifo_enabled
, NULL
, 0);
759 static const struct attribute
*adxl372_fifo_attributes
[] = {
760 &iio_const_attr_hwfifo_watermark_min
.dev_attr
.attr
,
761 &iio_const_attr_hwfifo_watermark_max
.dev_attr
.attr
,
762 &iio_dev_attr_hwfifo_watermark
.dev_attr
.attr
,
763 &iio_dev_attr_hwfifo_enabled
.dev_attr
.attr
,
767 static int adxl372_set_watermark(struct iio_dev
*indio_dev
, unsigned int val
)
769 struct adxl372_state
*st
= iio_priv(indio_dev
);
771 if (val
> ADXL372_FIFO_SIZE
)
772 val
= ADXL372_FIFO_SIZE
;
779 static int adxl372_buffer_postenable(struct iio_dev
*indio_dev
)
781 struct adxl372_state
*st
= iio_priv(indio_dev
);
785 ret
= adxl372_set_interrupts(st
, ADXL372_INT1_MAP_FIFO_FULL_MSK
, 0);
789 mask
= *indio_dev
->active_scan_mask
;
791 for (i
= 0; i
< ARRAY_SIZE(adxl372_axis_lookup_table
); i
++) {
792 if (mask
== adxl372_axis_lookup_table
[i
].bits
)
796 if (i
== ARRAY_SIZE(adxl372_axis_lookup_table
))
799 st
->fifo_format
= adxl372_axis_lookup_table
[i
].fifo_format
;
800 st
->fifo_set_size
= bitmap_weight(indio_dev
->active_scan_mask
,
801 indio_dev
->masklength
);
803 * The 512 FIFO samples can be allotted in several ways, such as:
804 * 170 sample sets of concurrent 3-axis data
805 * 256 sample sets of concurrent 2-axis data (user selectable)
806 * 512 sample sets of single-axis data
808 if ((st
->watermark
* st
->fifo_set_size
) > ADXL372_FIFO_SIZE
)
809 st
->watermark
= (ADXL372_FIFO_SIZE
/ st
->fifo_set_size
);
811 st
->fifo_mode
= ADXL372_FIFO_STREAMED
;
813 ret
= adxl372_configure_fifo(st
);
815 st
->fifo_mode
= ADXL372_FIFO_BYPASSED
;
816 adxl372_set_interrupts(st
, 0, 0);
820 return iio_triggered_buffer_postenable(indio_dev
);
823 static int adxl372_buffer_predisable(struct iio_dev
*indio_dev
)
825 struct adxl372_state
*st
= iio_priv(indio_dev
);
828 ret
= iio_triggered_buffer_predisable(indio_dev
);
832 adxl372_set_interrupts(st
, 0, 0);
833 st
->fifo_mode
= ADXL372_FIFO_BYPASSED
;
834 adxl372_configure_fifo(st
);
839 static const struct iio_buffer_setup_ops adxl372_buffer_ops
= {
840 .postenable
= adxl372_buffer_postenable
,
841 .predisable
= adxl372_buffer_predisable
,
844 static int adxl372_dready_trig_set_state(struct iio_trigger
*trig
,
847 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
848 struct adxl372_state
*st
= iio_priv(indio_dev
);
849 unsigned long int mask
= 0;
852 mask
= ADXL372_INT1_MAP_FIFO_FULL_MSK
;
854 return adxl372_set_interrupts(st
, mask
, 0);
857 static int adxl372_validate_trigger(struct iio_dev
*indio_dev
,
858 struct iio_trigger
*trig
)
860 struct adxl372_state
*st
= iio_priv(indio_dev
);
862 if (st
->dready_trig
!= trig
)
868 static const struct iio_trigger_ops adxl372_trigger_ops
= {
869 .validate_device
= &iio_trigger_validate_own_device
,
870 .set_trigger_state
= adxl372_dready_trig_set_state
,
873 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("400 800 1600 3200 6400");
874 static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available
,
875 0444, adxl372_show_filter_freq_avail
, NULL
, 0);
877 static struct attribute
*adxl372_attributes
[] = {
878 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
879 &iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available
.dev_attr
.attr
,
883 static const struct attribute_group adxl372_attrs_group
= {
884 .attrs
= adxl372_attributes
,
887 static const struct iio_info adxl372_info
= {
888 .validate_trigger
= &adxl372_validate_trigger
,
889 .attrs
= &adxl372_attrs_group
,
890 .read_raw
= adxl372_read_raw
,
891 .write_raw
= adxl372_write_raw
,
892 .debugfs_reg_access
= &adxl372_reg_access
,
893 .hwfifo_set_watermark
= adxl372_set_watermark
,
896 bool adxl372_readable_noinc_reg(struct device
*dev
, unsigned int reg
)
898 return (reg
== ADXL372_FIFO_DATA
);
900 EXPORT_SYMBOL_GPL(adxl372_readable_noinc_reg
);
902 int adxl372_probe(struct device
*dev
, struct regmap
*regmap
,
903 int irq
, const char *name
)
905 struct iio_dev
*indio_dev
;
906 struct adxl372_state
*st
;
909 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*st
));
913 st
= iio_priv(indio_dev
);
914 dev_set_drvdata(dev
, indio_dev
);
920 indio_dev
->channels
= adxl372_channels
;
921 indio_dev
->num_channels
= ARRAY_SIZE(adxl372_channels
);
922 indio_dev
->available_scan_masks
= adxl372_channel_masks
;
923 indio_dev
->dev
.parent
= dev
;
924 indio_dev
->name
= name
;
925 indio_dev
->info
= &adxl372_info
;
926 indio_dev
->modes
= INDIO_DIRECT_MODE
| INDIO_BUFFER_SOFTWARE
;
928 ret
= adxl372_setup(st
);
930 dev_err(dev
, "ADXL372 setup failed\n");
934 ret
= devm_iio_triggered_buffer_setup(dev
,
936 adxl372_trigger_handler
,
937 &adxl372_buffer_ops
);
941 iio_buffer_set_attrs(indio_dev
->buffer
, adxl372_fifo_attributes
);
944 st
->dready_trig
= devm_iio_trigger_alloc(dev
,
948 if (st
->dready_trig
== NULL
)
951 st
->dready_trig
->ops
= &adxl372_trigger_ops
;
952 st
->dready_trig
->dev
.parent
= dev
;
953 iio_trigger_set_drvdata(st
->dready_trig
, indio_dev
);
954 ret
= devm_iio_trigger_register(dev
, st
->dready_trig
);
958 indio_dev
->trig
= iio_trigger_get(st
->dready_trig
);
960 ret
= devm_request_threaded_irq(dev
, st
->irq
,
961 iio_trigger_generic_data_rdy_poll
,
963 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
964 indio_dev
->name
, st
->dready_trig
);
969 return devm_iio_device_register(dev
, indio_dev
);
971 EXPORT_SYMBOL_GPL(adxl372_probe
);
973 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
974 MODULE_DESCRIPTION("Analog Devices ADXL372 3-axis accelerometer driver");
975 MODULE_LICENSE("GPL");