xref: /linux/drivers/iio/accel/adxl372.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
14097da40SStefan Popa // SPDX-License-Identifier: GPL-2.0+
24097da40SStefan Popa /*
3d9e8fd04SStefan Popa  * ADXL372 3-Axis Digital Accelerometer core driver
44097da40SStefan Popa  *
54097da40SStefan Popa  * Copyright 2018 Analog Devices Inc.
64097da40SStefan Popa  */
74097da40SStefan Popa 
8b0fc6783SStefan Popa #include <linux/bitfield.h>
94097da40SStefan Popa #include <linux/bitops.h>
10f4f55ce3SStefan Popa #include <linux/interrupt.h>
11f4f55ce3SStefan Popa #include <linux/irq.h>
124097da40SStefan Popa #include <linux/module.h>
134097da40SStefan Popa #include <linux/regmap.h>
144097da40SStefan Popa #include <linux/spi/spi.h>
154097da40SStefan Popa 
164097da40SStefan Popa #include <linux/iio/iio.h>
174097da40SStefan Popa #include <linux/iio/sysfs.h>
18f4f55ce3SStefan Popa #include <linux/iio/buffer.h>
19f4f55ce3SStefan Popa #include <linux/iio/events.h>
20f4f55ce3SStefan Popa #include <linux/iio/trigger.h>
21f4f55ce3SStefan Popa #include <linux/iio/trigger_consumer.h>
22f4f55ce3SStefan Popa #include <linux/iio/triggered_buffer.h>
234097da40SStefan Popa 
24d9e8fd04SStefan Popa #include "adxl372.h"
25d9e8fd04SStefan Popa 
264097da40SStefan Popa /* ADXL372 registers definition */
274097da40SStefan Popa #define ADXL372_DEVID			0x00
284097da40SStefan Popa #define ADXL372_DEVID_MST		0x01
294097da40SStefan Popa #define ADXL372_PARTID			0x02
304097da40SStefan Popa #define ADXL372_STATUS_1		0x04
314097da40SStefan Popa #define ADXL372_STATUS_2		0x05
324097da40SStefan Popa #define ADXL372_FIFO_ENTRIES_2		0x06
334097da40SStefan Popa #define ADXL372_FIFO_ENTRIES_1		0x07
344097da40SStefan Popa #define ADXL372_X_DATA_H		0x08
354097da40SStefan Popa #define ADXL372_X_DATA_L		0x09
364097da40SStefan Popa #define ADXL372_Y_DATA_H		0x0A
374097da40SStefan Popa #define ADXL372_Y_DATA_L		0x0B
384097da40SStefan Popa #define ADXL372_Z_DATA_H		0x0C
394097da40SStefan Popa #define ADXL372_Z_DATA_L		0x0D
404097da40SStefan Popa #define ADXL372_X_MAXPEAK_H		0x15
414097da40SStefan Popa #define ADXL372_X_MAXPEAK_L		0x16
424097da40SStefan Popa #define ADXL372_Y_MAXPEAK_H		0x17
434097da40SStefan Popa #define ADXL372_Y_MAXPEAK_L		0x18
444097da40SStefan Popa #define ADXL372_Z_MAXPEAK_H		0x19
454097da40SStefan Popa #define ADXL372_Z_MAXPEAK_L		0x1A
464097da40SStefan Popa #define ADXL372_OFFSET_X		0x20
474097da40SStefan Popa #define ADXL372_OFFSET_Y		0x21
484097da40SStefan Popa #define ADXL372_OFFSET_Z		0x22
494097da40SStefan Popa #define ADXL372_X_THRESH_ACT_H		0x23
504097da40SStefan Popa #define ADXL372_X_THRESH_ACT_L		0x24
514097da40SStefan Popa #define ADXL372_Y_THRESH_ACT_H		0x25
524097da40SStefan Popa #define ADXL372_Y_THRESH_ACT_L		0x26
534097da40SStefan Popa #define ADXL372_Z_THRESH_ACT_H		0x27
544097da40SStefan Popa #define ADXL372_Z_THRESH_ACT_L		0x28
554097da40SStefan Popa #define ADXL372_TIME_ACT		0x29
564097da40SStefan Popa #define ADXL372_X_THRESH_INACT_H	0x2A
574097da40SStefan Popa #define ADXL372_X_THRESH_INACT_L	0x2B
584097da40SStefan Popa #define ADXL372_Y_THRESH_INACT_H	0x2C
594097da40SStefan Popa #define ADXL372_Y_THRESH_INACT_L	0x2D
604097da40SStefan Popa #define ADXL372_Z_THRESH_INACT_H	0x2E
614097da40SStefan Popa #define ADXL372_Z_THRESH_INACT_L	0x2F
624097da40SStefan Popa #define ADXL372_TIME_INACT_H		0x30
634097da40SStefan Popa #define ADXL372_TIME_INACT_L		0x31
644097da40SStefan Popa #define ADXL372_X_THRESH_ACT2_H		0x32
654097da40SStefan Popa #define ADXL372_X_THRESH_ACT2_L		0x33
664097da40SStefan Popa #define ADXL372_Y_THRESH_ACT2_H		0x34
674097da40SStefan Popa #define ADXL372_Y_THRESH_ACT2_L		0x35
684097da40SStefan Popa #define ADXL372_Z_THRESH_ACT2_H		0x36
694097da40SStefan Popa #define ADXL372_Z_THRESH_ACT2_L		0x37
704097da40SStefan Popa #define ADXL372_HPF			0x38
714097da40SStefan Popa #define ADXL372_FIFO_SAMPLES		0x39
724097da40SStefan Popa #define ADXL372_FIFO_CTL		0x3A
734097da40SStefan Popa #define ADXL372_INT1_MAP		0x3B
744097da40SStefan Popa #define ADXL372_INT2_MAP		0x3C
754097da40SStefan Popa #define ADXL372_TIMING			0x3D
764097da40SStefan Popa #define ADXL372_MEASURE			0x3E
774097da40SStefan Popa #define ADXL372_POWER_CTL		0x3F
784097da40SStefan Popa #define ADXL372_SELF_TEST		0x40
794097da40SStefan Popa #define ADXL372_RESET			0x41
804097da40SStefan Popa #define ADXL372_FIFO_DATA		0x42
814097da40SStefan Popa 
824097da40SStefan Popa #define ADXL372_DEVID_VAL		0xAD
834097da40SStefan Popa #define ADXL372_PARTID_VAL		0xFA
844097da40SStefan Popa #define ADXL372_RESET_CODE		0x52
854097da40SStefan Popa 
864097da40SStefan Popa /* ADXL372_POWER_CTL */
874097da40SStefan Popa #define ADXL372_POWER_CTL_MODE_MSK		GENMASK_ULL(1, 0)
884097da40SStefan Popa #define ADXL372_POWER_CTL_MODE(x)		(((x) & 0x3) << 0)
894097da40SStefan Popa 
904097da40SStefan Popa /* ADXL372_MEASURE */
914097da40SStefan Popa #define ADXL372_MEASURE_LINKLOOP_MSK		GENMASK_ULL(5, 4)
924097da40SStefan Popa #define ADXL372_MEASURE_LINKLOOP_MODE(x)	(((x) & 0x3) << 4)
934097da40SStefan Popa #define ADXL372_MEASURE_BANDWIDTH_MSK		GENMASK_ULL(2, 0)
944097da40SStefan Popa #define ADXL372_MEASURE_BANDWIDTH_MODE(x)	(((x) & 0x7) << 0)
954097da40SStefan Popa 
964097da40SStefan Popa /* ADXL372_TIMING */
974097da40SStefan Popa #define ADXL372_TIMING_ODR_MSK			GENMASK_ULL(7, 5)
984097da40SStefan Popa #define ADXL372_TIMING_ODR_MODE(x)		(((x) & 0x7) << 5)
994097da40SStefan Popa 
1004097da40SStefan Popa /* ADXL372_FIFO_CTL */
1014097da40SStefan Popa #define ADXL372_FIFO_CTL_FORMAT_MSK		GENMASK(5, 3)
1024097da40SStefan Popa #define ADXL372_FIFO_CTL_FORMAT_MODE(x)		(((x) & 0x7) << 3)
1034097da40SStefan Popa #define ADXL372_FIFO_CTL_MODE_MSK		GENMASK(2, 1)
1044097da40SStefan Popa #define ADXL372_FIFO_CTL_MODE_MODE(x)		(((x) & 0x3) << 1)
1054097da40SStefan Popa #define ADXL372_FIFO_CTL_SAMPLES_MSK		BIT(1)
1064097da40SStefan Popa #define ADXL372_FIFO_CTL_SAMPLES_MODE(x)	(((x) > 0xFF) ? 1 : 0)
1074097da40SStefan Popa 
1084097da40SStefan Popa /* ADXL372_STATUS_1 */
1094097da40SStefan Popa #define ADXL372_STATUS_1_DATA_RDY(x)		(((x) >> 0) & 0x1)
1104097da40SStefan Popa #define ADXL372_STATUS_1_FIFO_RDY(x)		(((x) >> 1) & 0x1)
1114097da40SStefan Popa #define ADXL372_STATUS_1_FIFO_FULL(x)		(((x) >> 2) & 0x1)
1124097da40SStefan Popa #define ADXL372_STATUS_1_FIFO_OVR(x)		(((x) >> 3) & 0x1)
1134097da40SStefan Popa #define ADXL372_STATUS_1_USR_NVM_BUSY(x)	(((x) >> 5) & 0x1)
1144097da40SStefan Popa #define ADXL372_STATUS_1_AWAKE(x)		(((x) >> 6) & 0x1)
1154097da40SStefan Popa #define ADXL372_STATUS_1_ERR_USR_REGS(x)	(((x) >> 7) & 0x1)
1164097da40SStefan Popa 
117b0fc6783SStefan Popa /* ADXL372_STATUS_2 */
118b0fc6783SStefan Popa #define ADXL372_STATUS_2_INACT(x)		(((x) >> 4) & 0x1)
119b0fc6783SStefan Popa #define ADXL372_STATUS_2_ACT(x)			(((x) >> 5) & 0x1)
120b0fc6783SStefan Popa #define ADXL372_STATUS_2_AC2(x)			(((x) >> 6) & 0x1)
121b0fc6783SStefan Popa 
1224097da40SStefan Popa /* ADXL372_INT1_MAP */
1234097da40SStefan Popa #define ADXL372_INT1_MAP_DATA_RDY_MSK		BIT(0)
1244097da40SStefan Popa #define ADXL372_INT1_MAP_DATA_RDY_MODE(x)	(((x) & 0x1) << 0)
1254097da40SStefan Popa #define ADXL372_INT1_MAP_FIFO_RDY_MSK		BIT(1)
1264097da40SStefan Popa #define ADXL372_INT1_MAP_FIFO_RDY_MODE(x)	(((x) & 0x1) << 1)
1274097da40SStefan Popa #define ADXL372_INT1_MAP_FIFO_FULL_MSK		BIT(2)
1284097da40SStefan Popa #define ADXL372_INT1_MAP_FIFO_FULL_MODE(x)	(((x) & 0x1) << 2)
1294097da40SStefan Popa #define ADXL372_INT1_MAP_FIFO_OVR_MSK		BIT(3)
1304097da40SStefan Popa #define ADXL372_INT1_MAP_FIFO_OVR_MODE(x)	(((x) & 0x1) << 3)
1314097da40SStefan Popa #define ADXL372_INT1_MAP_INACT_MSK		BIT(4)
1324097da40SStefan Popa #define ADXL372_INT1_MAP_INACT_MODE(x)		(((x) & 0x1) << 4)
1334097da40SStefan Popa #define ADXL372_INT1_MAP_ACT_MSK		BIT(5)
1344097da40SStefan Popa #define ADXL372_INT1_MAP_ACT_MODE(x)		(((x) & 0x1) << 5)
1354097da40SStefan Popa #define ADXL372_INT1_MAP_AWAKE_MSK		BIT(6)
1364097da40SStefan Popa #define ADXL372_INT1_MAP_AWAKE_MODE(x)		(((x) & 0x1) << 6)
1374097da40SStefan Popa #define ADXL372_INT1_MAP_LOW_MSK		BIT(7)
1384097da40SStefan Popa #define ADXL372_INT1_MAP_LOW_MODE(x)		(((x) & 0x1) << 7)
1394097da40SStefan Popa 
140b0fc6783SStefan Popa /* ADX372_THRESH */
141b0fc6783SStefan Popa #define ADXL372_THRESH_VAL_H_MSK	GENMASK(10, 3)
142b0fc6783SStefan Popa #define ADXL372_THRESH_VAL_H_SEL(x)	FIELD_GET(ADXL372_THRESH_VAL_H_MSK, x)
143b0fc6783SStefan Popa #define ADXL372_THRESH_VAL_L_MSK	GENMASK(2, 0)
144b0fc6783SStefan Popa #define ADXL372_THRESH_VAL_L_SEL(x)	FIELD_GET(ADXL372_THRESH_VAL_L_MSK, x)
145b0fc6783SStefan Popa 
146f4f55ce3SStefan Popa /* The ADXL372 includes a deep, 512 sample FIFO buffer */
147f4f55ce3SStefan Popa #define ADXL372_FIFO_SIZE			512
148b0fc6783SStefan Popa #define ADXL372_X_AXIS_EN(x)			((x) & BIT(0))
149b0fc6783SStefan Popa #define ADXL372_Y_AXIS_EN(x)			((x) & BIT(1))
150b0fc6783SStefan Popa #define ADXL372_Z_AXIS_EN(x)			((x) & BIT(2))
151f4f55ce3SStefan Popa 
1524097da40SStefan Popa /*
1534097da40SStefan Popa  * At +/- 200g with 12-bit resolution, scale is computed as:
1544097da40SStefan Popa  * (200 + 200) * 9.81 / (2^12 - 1) = 0.958241
1554097da40SStefan Popa  */
1564097da40SStefan Popa #define ADXL372_USCALE	958241
1574097da40SStefan Popa 
1584097da40SStefan Popa enum adxl372_op_mode {
1594097da40SStefan Popa 	ADXL372_STANDBY,
1604097da40SStefan Popa 	ADXL372_WAKE_UP,
1614097da40SStefan Popa 	ADXL372_INSTANT_ON,
1624097da40SStefan Popa 	ADXL372_FULL_BW_MEASUREMENT,
1634097da40SStefan Popa };
1644097da40SStefan Popa 
1654097da40SStefan Popa enum adxl372_act_proc_mode {
1664097da40SStefan Popa 	ADXL372_DEFAULT,
1674097da40SStefan Popa 	ADXL372_LINKED,
1684097da40SStefan Popa 	ADXL372_LOOPED,
1694097da40SStefan Popa };
1704097da40SStefan Popa 
1714097da40SStefan Popa enum adxl372_th_activity {
1724097da40SStefan Popa 	ADXL372_ACTIVITY,
1734097da40SStefan Popa 	ADXL372_ACTIVITY2,
1744097da40SStefan Popa 	ADXL372_INACTIVITY,
1754097da40SStefan Popa };
1764097da40SStefan Popa 
1774097da40SStefan Popa enum adxl372_odr {
1784097da40SStefan Popa 	ADXL372_ODR_400HZ,
1794097da40SStefan Popa 	ADXL372_ODR_800HZ,
1804097da40SStefan Popa 	ADXL372_ODR_1600HZ,
1814097da40SStefan Popa 	ADXL372_ODR_3200HZ,
1824097da40SStefan Popa 	ADXL372_ODR_6400HZ,
1834097da40SStefan Popa };
1844097da40SStefan Popa 
1854097da40SStefan Popa enum adxl372_bandwidth {
1864097da40SStefan Popa 	ADXL372_BW_200HZ,
1874097da40SStefan Popa 	ADXL372_BW_400HZ,
1884097da40SStefan Popa 	ADXL372_BW_800HZ,
1894097da40SStefan Popa 	ADXL372_BW_1600HZ,
1904097da40SStefan Popa 	ADXL372_BW_3200HZ,
1914097da40SStefan Popa };
1924097da40SStefan Popa 
1934097da40SStefan Popa static const unsigned int adxl372_th_reg_high_addr[3] = {
1944097da40SStefan Popa 	[ADXL372_ACTIVITY] = ADXL372_X_THRESH_ACT_H,
1954097da40SStefan Popa 	[ADXL372_ACTIVITY2] = ADXL372_X_THRESH_ACT2_H,
1964097da40SStefan Popa 	[ADXL372_INACTIVITY] = ADXL372_X_THRESH_INACT_H,
1974097da40SStefan Popa };
1984097da40SStefan Popa 
199f4f55ce3SStefan Popa enum adxl372_fifo_format {
200f4f55ce3SStefan Popa 	ADXL372_XYZ_FIFO,
201f4f55ce3SStefan Popa 	ADXL372_X_FIFO,
202f4f55ce3SStefan Popa 	ADXL372_Y_FIFO,
203f4f55ce3SStefan Popa 	ADXL372_XY_FIFO,
204f4f55ce3SStefan Popa 	ADXL372_Z_FIFO,
205f4f55ce3SStefan Popa 	ADXL372_XZ_FIFO,
206f4f55ce3SStefan Popa 	ADXL372_YZ_FIFO,
207f4f55ce3SStefan Popa 	ADXL372_XYZ_PEAK_FIFO,
208f4f55ce3SStefan Popa };
209f4f55ce3SStefan Popa 
210f4f55ce3SStefan Popa enum adxl372_fifo_mode {
211f4f55ce3SStefan Popa 	ADXL372_FIFO_BYPASSED,
212f4f55ce3SStefan Popa 	ADXL372_FIFO_STREAMED,
213f4f55ce3SStefan Popa 	ADXL372_FIFO_TRIGGERED,
214f4f55ce3SStefan Popa 	ADXL372_FIFO_OLD_SAVED
215f4f55ce3SStefan Popa };
216f4f55ce3SStefan Popa 
217f4f55ce3SStefan Popa static const int adxl372_samp_freq_tbl[5] = {
218f4f55ce3SStefan Popa 	400, 800, 1600, 3200, 6400,
219f4f55ce3SStefan Popa };
220f4f55ce3SStefan Popa 
2217ec040afSStefan Popa static const int adxl372_bw_freq_tbl[5] = {
2227ec040afSStefan Popa 	200, 400, 800, 1600, 3200,
2237ec040afSStefan Popa };
2247ec040afSStefan Popa 
225f4f55ce3SStefan Popa struct adxl372_axis_lookup {
226f4f55ce3SStefan Popa 	unsigned int bits;
227f4f55ce3SStefan Popa 	enum adxl372_fifo_format fifo_format;
228f4f55ce3SStefan Popa };
229f4f55ce3SStefan Popa 
230f4f55ce3SStefan Popa static const struct adxl372_axis_lookup adxl372_axis_lookup_table[] = {
231f4f55ce3SStefan Popa 	{ BIT(0), ADXL372_X_FIFO },
232f4f55ce3SStefan Popa 	{ BIT(1), ADXL372_Y_FIFO },
233f4f55ce3SStefan Popa 	{ BIT(2), ADXL372_Z_FIFO },
234f4f55ce3SStefan Popa 	{ BIT(0) | BIT(1), ADXL372_XY_FIFO },
235f4f55ce3SStefan Popa 	{ BIT(0) | BIT(2), ADXL372_XZ_FIFO },
236f4f55ce3SStefan Popa 	{ BIT(1) | BIT(2), ADXL372_YZ_FIFO },
237f4f55ce3SStefan Popa 	{ BIT(0) | BIT(1) | BIT(2), ADXL372_XYZ_FIFO },
238f4f55ce3SStefan Popa };
239f4f55ce3SStefan Popa 
240b0fc6783SStefan Popa static const struct iio_event_spec adxl372_events[] = {
241b0fc6783SStefan Popa 	{
242b0fc6783SStefan Popa 		.type = IIO_EV_TYPE_THRESH,
243b0fc6783SStefan Popa 		.dir = IIO_EV_DIR_RISING,
244b0fc6783SStefan Popa 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
245b0fc6783SStefan Popa 		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD) | BIT(IIO_EV_INFO_ENABLE),
246b0fc6783SStefan Popa 	}, {
247b0fc6783SStefan Popa 		.type = IIO_EV_TYPE_THRESH,
248b0fc6783SStefan Popa 		.dir = IIO_EV_DIR_FALLING,
249b0fc6783SStefan Popa 		.mask_separate = BIT(IIO_EV_INFO_VALUE),
250b0fc6783SStefan Popa 		.mask_shared_by_all = BIT(IIO_EV_INFO_PERIOD) | BIT(IIO_EV_INFO_ENABLE),
251b0fc6783SStefan Popa 	},
252b0fc6783SStefan Popa };
253b0fc6783SStefan Popa 
2544097da40SStefan Popa #define ADXL372_ACCEL_CHANNEL(index, reg, axis) {			\
2554097da40SStefan Popa 	.type = IIO_ACCEL,						\
2564097da40SStefan Popa 	.address = reg,							\
2574097da40SStefan Popa 	.modified = 1,							\
2584097da40SStefan Popa 	.channel2 = IIO_MOD_##axis,					\
2594097da40SStefan Popa 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
2605e605a4dSStefan Popa 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) |		\
2617ec040afSStefan Popa 				    BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
2627ec040afSStefan Popa 		BIT(IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY),	\
263f4f55ce3SStefan Popa 	.scan_index = index,						\
264f4f55ce3SStefan Popa 	.scan_type = {							\
265f4f55ce3SStefan Popa 		.sign = 's',						\
266f4f55ce3SStefan Popa 		.realbits = 12,						\
267f4f55ce3SStefan Popa 		.storagebits = 16,					\
268f4f55ce3SStefan Popa 		.shift = 4,						\
269cb2116ffSAlexandru Tachici 		.endianness = IIO_BE,					\
270f4f55ce3SStefan Popa 	},								\
271b0fc6783SStefan Popa 	.event_spec = adxl372_events,					\
272b0fc6783SStefan Popa 	.num_event_specs = ARRAY_SIZE(adxl372_events)			\
2734097da40SStefan Popa }
2744097da40SStefan Popa 
2754097da40SStefan Popa static const struct iio_chan_spec adxl372_channels[] = {
2764097da40SStefan Popa 	ADXL372_ACCEL_CHANNEL(0, ADXL372_X_DATA_H, X),
2774097da40SStefan Popa 	ADXL372_ACCEL_CHANNEL(1, ADXL372_Y_DATA_H, Y),
2784097da40SStefan Popa 	ADXL372_ACCEL_CHANNEL(2, ADXL372_Z_DATA_H, Z),
2794097da40SStefan Popa };
2804097da40SStefan Popa 
2814097da40SStefan Popa struct adxl372_state {
282d9e8fd04SStefan Popa 	int				irq;
283d9e8fd04SStefan Popa 	struct device			*dev;
2844097da40SStefan Popa 	struct regmap			*regmap;
285f4f55ce3SStefan Popa 	struct iio_trigger		*dready_trig;
286b0fc6783SStefan Popa 	struct iio_trigger		*peak_datardy_trig;
287f4f55ce3SStefan Popa 	enum adxl372_fifo_mode		fifo_mode;
288f4f55ce3SStefan Popa 	enum adxl372_fifo_format	fifo_format;
289b0fc6783SStefan Popa 	unsigned int			fifo_axis_mask;
2904097da40SStefan Popa 	enum adxl372_op_mode		op_mode;
2914097da40SStefan Popa 	enum adxl372_act_proc_mode	act_proc_mode;
2924097da40SStefan Popa 	enum adxl372_odr		odr;
2934097da40SStefan Popa 	enum adxl372_bandwidth		bw;
2944097da40SStefan Popa 	u32				act_time_ms;
2954097da40SStefan Popa 	u32				inact_time_ms;
296f4f55ce3SStefan Popa 	u8				fifo_set_size;
297b0fc6783SStefan Popa 	unsigned long			int1_bitmask;
298b0fc6783SStefan Popa 	unsigned long			int2_bitmask;
299f4f55ce3SStefan Popa 	u16				watermark;
300f4f55ce3SStefan Popa 	__be16				fifo_buf[ADXL372_FIFO_SIZE];
301b0fc6783SStefan Popa 	bool				peak_fifo_mode_en;
302b0fc6783SStefan Popa 	struct mutex			threshold_m; /* lock for threshold */
303f4f55ce3SStefan Popa };
304f4f55ce3SStefan Popa 
305f4f55ce3SStefan Popa static const unsigned long adxl372_channel_masks[] = {
306f4f55ce3SStefan Popa 	BIT(0), BIT(1), BIT(2),
307f4f55ce3SStefan Popa 	BIT(0) | BIT(1),
308f4f55ce3SStefan Popa 	BIT(0) | BIT(2),
309f4f55ce3SStefan Popa 	BIT(1) | BIT(2),
310f4f55ce3SStefan Popa 	BIT(0) | BIT(1) | BIT(2),
311f4f55ce3SStefan Popa 	0
3124097da40SStefan Popa };
3134097da40SStefan Popa 
adxl372_read_threshold_value(struct iio_dev * indio_dev,unsigned int addr,u16 * threshold)314b0fc6783SStefan Popa static ssize_t adxl372_read_threshold_value(struct iio_dev *indio_dev, unsigned int addr,
315b0fc6783SStefan Popa 					    u16 *threshold)
316b0fc6783SStefan Popa {
317b0fc6783SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
318b0fc6783SStefan Popa 	__be16 raw_regval;
319b0fc6783SStefan Popa 	u16 regval;
320b0fc6783SStefan Popa 	int ret;
321b0fc6783SStefan Popa 
322b0fc6783SStefan Popa 	ret = regmap_bulk_read(st->regmap, addr, &raw_regval, sizeof(raw_regval));
323b0fc6783SStefan Popa 	if (ret < 0)
324b0fc6783SStefan Popa 		return ret;
325b0fc6783SStefan Popa 
326b0fc6783SStefan Popa 	regval = be16_to_cpu(raw_regval);
327b0fc6783SStefan Popa 	regval >>= 5;
328b0fc6783SStefan Popa 
329b0fc6783SStefan Popa 	*threshold = regval;
330b0fc6783SStefan Popa 
331b0fc6783SStefan Popa 	return 0;
332b0fc6783SStefan Popa }
333b0fc6783SStefan Popa 
adxl372_write_threshold_value(struct iio_dev * indio_dev,unsigned int addr,u16 threshold)334b0fc6783SStefan Popa static ssize_t adxl372_write_threshold_value(struct iio_dev *indio_dev, unsigned int addr,
335b0fc6783SStefan Popa 					     u16 threshold)
336b0fc6783SStefan Popa {
337b0fc6783SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
338b0fc6783SStefan Popa 	int ret;
339b0fc6783SStefan Popa 
340b0fc6783SStefan Popa 	mutex_lock(&st->threshold_m);
341b0fc6783SStefan Popa 	ret = regmap_write(st->regmap, addr, ADXL372_THRESH_VAL_H_SEL(threshold));
342b0fc6783SStefan Popa 	if (ret < 0)
343b0fc6783SStefan Popa 		goto unlock;
344b0fc6783SStefan Popa 
345b0fc6783SStefan Popa 	ret = regmap_update_bits(st->regmap, addr + 1, GENMASK(7, 5),
346b0fc6783SStefan Popa 				 ADXL372_THRESH_VAL_L_SEL(threshold) << 5);
347b0fc6783SStefan Popa 
348b0fc6783SStefan Popa unlock:
349b0fc6783SStefan Popa 	mutex_unlock(&st->threshold_m);
350b0fc6783SStefan Popa 
351b0fc6783SStefan Popa 	return ret;
352b0fc6783SStefan Popa }
353b0fc6783SStefan Popa 
adxl372_read_axis(struct adxl372_state * st,u8 addr)3544097da40SStefan Popa static int adxl372_read_axis(struct adxl372_state *st, u8 addr)
3554097da40SStefan Popa {
3564097da40SStefan Popa 	__be16 regval;
3574097da40SStefan Popa 	int ret;
3584097da40SStefan Popa 
3594097da40SStefan Popa 	ret = regmap_bulk_read(st->regmap, addr, &regval, sizeof(regval));
3604097da40SStefan Popa 	if (ret < 0)
3614097da40SStefan Popa 		return ret;
3624097da40SStefan Popa 
3634097da40SStefan Popa 	return be16_to_cpu(regval);
3644097da40SStefan Popa }
3654097da40SStefan Popa 
adxl372_set_op_mode(struct adxl372_state * st,enum adxl372_op_mode op_mode)3664097da40SStefan Popa static int adxl372_set_op_mode(struct adxl372_state *st,
3674097da40SStefan Popa 			       enum adxl372_op_mode op_mode)
3684097da40SStefan Popa {
3694097da40SStefan Popa 	int ret;
3704097da40SStefan Popa 
3714097da40SStefan Popa 	ret = regmap_update_bits(st->regmap, ADXL372_POWER_CTL,
3724097da40SStefan Popa 				 ADXL372_POWER_CTL_MODE_MSK,
3734097da40SStefan Popa 				 ADXL372_POWER_CTL_MODE(op_mode));
3744097da40SStefan Popa 	if (ret < 0)
3754097da40SStefan Popa 		return ret;
3764097da40SStefan Popa 
3774097da40SStefan Popa 	st->op_mode = op_mode;
3784097da40SStefan Popa 
3794097da40SStefan Popa 	return ret;
3804097da40SStefan Popa }
3814097da40SStefan Popa 
adxl372_set_odr(struct adxl372_state * st,enum adxl372_odr odr)3824097da40SStefan Popa static int adxl372_set_odr(struct adxl372_state *st,
3834097da40SStefan Popa 			   enum adxl372_odr odr)
3844097da40SStefan Popa {
3854097da40SStefan Popa 	int ret;
3864097da40SStefan Popa 
3874097da40SStefan Popa 	ret = regmap_update_bits(st->regmap, ADXL372_TIMING,
3884097da40SStefan Popa 				 ADXL372_TIMING_ODR_MSK,
3894097da40SStefan Popa 				 ADXL372_TIMING_ODR_MODE(odr));
3904097da40SStefan Popa 	if (ret < 0)
3914097da40SStefan Popa 		return ret;
3924097da40SStefan Popa 
3934097da40SStefan Popa 	st->odr = odr;
3944097da40SStefan Popa 
3954097da40SStefan Popa 	return ret;
3964097da40SStefan Popa }
3974097da40SStefan Popa 
adxl372_find_closest_match(const int * array,unsigned int size,int val)3985e605a4dSStefan Popa static int adxl372_find_closest_match(const int *array,
3995e605a4dSStefan Popa 				      unsigned int size, int val)
4005e605a4dSStefan Popa {
4015e605a4dSStefan Popa 	int i;
4025e605a4dSStefan Popa 
4035e605a4dSStefan Popa 	for (i = 0; i < size; i++) {
4045e605a4dSStefan Popa 		if (val <= array[i])
4055e605a4dSStefan Popa 			return i;
4065e605a4dSStefan Popa 	}
4075e605a4dSStefan Popa 
4085e605a4dSStefan Popa 	return size - 1;
4095e605a4dSStefan Popa }
4105e605a4dSStefan Popa 
adxl372_set_bandwidth(struct adxl372_state * st,enum adxl372_bandwidth bw)4114097da40SStefan Popa static int adxl372_set_bandwidth(struct adxl372_state *st,
4124097da40SStefan Popa 				 enum adxl372_bandwidth bw)
4134097da40SStefan Popa {
4144097da40SStefan Popa 	int ret;
4154097da40SStefan Popa 
4164097da40SStefan Popa 	ret = regmap_update_bits(st->regmap, ADXL372_MEASURE,
4174097da40SStefan Popa 				 ADXL372_MEASURE_BANDWIDTH_MSK,
4184097da40SStefan Popa 				 ADXL372_MEASURE_BANDWIDTH_MODE(bw));
4194097da40SStefan Popa 	if (ret < 0)
4204097da40SStefan Popa 		return ret;
4214097da40SStefan Popa 
4224097da40SStefan Popa 	st->bw = bw;
4234097da40SStefan Popa 
4244097da40SStefan Popa 	return ret;
4254097da40SStefan Popa }
4264097da40SStefan Popa 
adxl372_set_act_proc_mode(struct adxl372_state * st,enum adxl372_act_proc_mode mode)4274097da40SStefan Popa static int adxl372_set_act_proc_mode(struct adxl372_state *st,
4284097da40SStefan Popa 				     enum adxl372_act_proc_mode mode)
4294097da40SStefan Popa {
4304097da40SStefan Popa 	int ret;
4314097da40SStefan Popa 
4324097da40SStefan Popa 	ret = regmap_update_bits(st->regmap,
4334097da40SStefan Popa 				 ADXL372_MEASURE,
4344097da40SStefan Popa 				 ADXL372_MEASURE_LINKLOOP_MSK,
4354097da40SStefan Popa 				 ADXL372_MEASURE_LINKLOOP_MODE(mode));
4364097da40SStefan Popa 	if (ret < 0)
4374097da40SStefan Popa 		return ret;
4384097da40SStefan Popa 
4394097da40SStefan Popa 	st->act_proc_mode = mode;
4404097da40SStefan Popa 
4414097da40SStefan Popa 	return ret;
4424097da40SStefan Popa }
4434097da40SStefan Popa 
adxl372_set_activity_threshold(struct adxl372_state * st,enum adxl372_th_activity act,bool ref_en,bool enable,unsigned int threshold)4444097da40SStefan Popa static int adxl372_set_activity_threshold(struct adxl372_state *st,
4454097da40SStefan Popa 					  enum adxl372_th_activity act,
4464097da40SStefan Popa 					  bool ref_en, bool enable,
4474097da40SStefan Popa 					  unsigned int threshold)
4484097da40SStefan Popa {
4494097da40SStefan Popa 	unsigned char buf[6];
4504097da40SStefan Popa 	unsigned char th_reg_high_val, th_reg_low_val, th_reg_high_addr;
4514097da40SStefan Popa 
4524097da40SStefan Popa 	/* scale factor is 100 mg/code */
4534097da40SStefan Popa 	th_reg_high_val = (threshold / 100) >> 3;
4544097da40SStefan Popa 	th_reg_low_val = ((threshold / 100) << 5) | (ref_en << 1) | enable;
4554097da40SStefan Popa 	th_reg_high_addr = adxl372_th_reg_high_addr[act];
4564097da40SStefan Popa 
4574097da40SStefan Popa 	buf[0] = th_reg_high_val;
4584097da40SStefan Popa 	buf[1] = th_reg_low_val;
4594097da40SStefan Popa 	buf[2] = th_reg_high_val;
4604097da40SStefan Popa 	buf[3] = th_reg_low_val;
4614097da40SStefan Popa 	buf[4] = th_reg_high_val;
4624097da40SStefan Popa 	buf[5] = th_reg_low_val;
4634097da40SStefan Popa 
4644097da40SStefan Popa 	return regmap_bulk_write(st->regmap, th_reg_high_addr,
4654097da40SStefan Popa 				 buf, ARRAY_SIZE(buf));
4664097da40SStefan Popa }
4674097da40SStefan Popa 
adxl372_set_activity_time_ms(struct adxl372_state * st,unsigned int act_time_ms)4684097da40SStefan Popa static int adxl372_set_activity_time_ms(struct adxl372_state *st,
4694097da40SStefan Popa 					unsigned int act_time_ms)
4704097da40SStefan Popa {
4714097da40SStefan Popa 	unsigned int reg_val, scale_factor;
4724097da40SStefan Popa 	int ret;
4734097da40SStefan Popa 
4744097da40SStefan Popa 	/*
4754097da40SStefan Popa 	 * 3.3 ms per code is the scale factor of the TIME_ACT register for
4764097da40SStefan Popa 	 * ODR = 6400 Hz. It is 6.6 ms per code for ODR = 3200 Hz and below.
4774097da40SStefan Popa 	 */
4784097da40SStefan Popa 	if (st->odr == ADXL372_ODR_6400HZ)
4794097da40SStefan Popa 		scale_factor = 3300;
4804097da40SStefan Popa 	else
4814097da40SStefan Popa 		scale_factor = 6600;
4824097da40SStefan Popa 
4834097da40SStefan Popa 	reg_val = DIV_ROUND_CLOSEST(act_time_ms * 1000, scale_factor);
4844097da40SStefan Popa 
4854097da40SStefan Popa 	/* TIME_ACT register is 8 bits wide */
4864097da40SStefan Popa 	if (reg_val > 0xFF)
4874097da40SStefan Popa 		reg_val = 0xFF;
4884097da40SStefan Popa 
4894097da40SStefan Popa 	ret = regmap_write(st->regmap, ADXL372_TIME_ACT, reg_val);
4904097da40SStefan Popa 	if (ret < 0)
4914097da40SStefan Popa 		return ret;
4924097da40SStefan Popa 
4934097da40SStefan Popa 	st->act_time_ms = act_time_ms;
4944097da40SStefan Popa 
4954097da40SStefan Popa 	return ret;
4964097da40SStefan Popa }
4974097da40SStefan Popa 
adxl372_set_inactivity_time_ms(struct adxl372_state * st,unsigned int inact_time_ms)4984097da40SStefan Popa static int adxl372_set_inactivity_time_ms(struct adxl372_state *st,
4994097da40SStefan Popa 					  unsigned int inact_time_ms)
5004097da40SStefan Popa {
5014097da40SStefan Popa 	unsigned int reg_val_h, reg_val_l, res, scale_factor;
5024097da40SStefan Popa 	int ret;
5034097da40SStefan Popa 
5044097da40SStefan Popa 	/*
5054097da40SStefan Popa 	 * 13 ms per code is the scale factor of the TIME_INACT register for
5064097da40SStefan Popa 	 * ODR = 6400 Hz. It is 26 ms per code for ODR = 3200 Hz and below.
5074097da40SStefan Popa 	 */
5084097da40SStefan Popa 	if (st->odr == ADXL372_ODR_6400HZ)
5094097da40SStefan Popa 		scale_factor = 13;
5104097da40SStefan Popa 	else
5114097da40SStefan Popa 		scale_factor = 26;
5124097da40SStefan Popa 
5134097da40SStefan Popa 	res = DIV_ROUND_CLOSEST(inact_time_ms, scale_factor);
5144097da40SStefan Popa 	reg_val_h = (res >> 8) & 0xFF;
5154097da40SStefan Popa 	reg_val_l = res & 0xFF;
5164097da40SStefan Popa 
5174097da40SStefan Popa 	ret = regmap_write(st->regmap, ADXL372_TIME_INACT_H, reg_val_h);
5184097da40SStefan Popa 	if (ret < 0)
5194097da40SStefan Popa 		return ret;
5204097da40SStefan Popa 
5214097da40SStefan Popa 	ret = regmap_write(st->regmap, ADXL372_TIME_INACT_L, reg_val_l);
5224097da40SStefan Popa 	if (ret < 0)
5234097da40SStefan Popa 		return ret;
5244097da40SStefan Popa 
5254097da40SStefan Popa 	st->inact_time_ms = inact_time_ms;
5264097da40SStefan Popa 
5274097da40SStefan Popa 	return ret;
5284097da40SStefan Popa }
5294097da40SStefan Popa 
adxl372_set_interrupts(struct adxl372_state * st,unsigned long int1_bitmask,unsigned long int2_bitmask)530f4f55ce3SStefan Popa static int adxl372_set_interrupts(struct adxl372_state *st,
531b0fc6783SStefan Popa 				  unsigned long int1_bitmask,
532b0fc6783SStefan Popa 				  unsigned long int2_bitmask)
533f4f55ce3SStefan Popa {
534f4f55ce3SStefan Popa 	int ret;
535f4f55ce3SStefan Popa 
536f4f55ce3SStefan Popa 	ret = regmap_write(st->regmap, ADXL372_INT1_MAP, int1_bitmask);
537f4f55ce3SStefan Popa 	if (ret < 0)
538f4f55ce3SStefan Popa 		return ret;
539f4f55ce3SStefan Popa 
540f4f55ce3SStefan Popa 	return regmap_write(st->regmap, ADXL372_INT2_MAP, int2_bitmask);
541f4f55ce3SStefan Popa }
542f4f55ce3SStefan Popa 
adxl372_configure_fifo(struct adxl372_state * st)543f4f55ce3SStefan Popa static int adxl372_configure_fifo(struct adxl372_state *st)
544f4f55ce3SStefan Popa {
545f4f55ce3SStefan Popa 	unsigned int fifo_samples, fifo_ctl;
546f4f55ce3SStefan Popa 	int ret;
547f4f55ce3SStefan Popa 
548f4f55ce3SStefan Popa 	/* FIFO must be configured while in standby mode */
549f4f55ce3SStefan Popa 	ret = adxl372_set_op_mode(st, ADXL372_STANDBY);
550f4f55ce3SStefan Popa 	if (ret < 0)
551f4f55ce3SStefan Popa 		return ret;
552f4f55ce3SStefan Popa 
553d202ce47SStefan Popa 	/*
554d202ce47SStefan Popa 	 * watermark stores the number of sets; we need to write the FIFO
555d202ce47SStefan Popa 	 * registers with the number of samples
556d202ce47SStefan Popa 	 */
557d202ce47SStefan Popa 	fifo_samples = (st->watermark * st->fifo_set_size);
558f4f55ce3SStefan Popa 	fifo_ctl = ADXL372_FIFO_CTL_FORMAT_MODE(st->fifo_format) |
559f4f55ce3SStefan Popa 		   ADXL372_FIFO_CTL_MODE_MODE(st->fifo_mode) |
560d202ce47SStefan Popa 		   ADXL372_FIFO_CTL_SAMPLES_MODE(fifo_samples);
561f4f55ce3SStefan Popa 
562d202ce47SStefan Popa 	ret = regmap_write(st->regmap,
563d202ce47SStefan Popa 			   ADXL372_FIFO_SAMPLES, fifo_samples & 0xFF);
564f4f55ce3SStefan Popa 	if (ret < 0)
565f4f55ce3SStefan Popa 		return ret;
566f4f55ce3SStefan Popa 
567f4f55ce3SStefan Popa 	ret = regmap_write(st->regmap, ADXL372_FIFO_CTL, fifo_ctl);
568f4f55ce3SStefan Popa 	if (ret < 0)
569f4f55ce3SStefan Popa 		return ret;
570f4f55ce3SStefan Popa 
571f4f55ce3SStefan Popa 	return adxl372_set_op_mode(st, ADXL372_FULL_BW_MEASUREMENT);
572f4f55ce3SStefan Popa }
573f4f55ce3SStefan Popa 
adxl372_get_status(struct adxl372_state * st,u8 * status1,u8 * status2,u16 * fifo_entries)574f4f55ce3SStefan Popa static int adxl372_get_status(struct adxl372_state *st,
575f4f55ce3SStefan Popa 			      u8 *status1, u8 *status2,
576f4f55ce3SStefan Popa 			      u16 *fifo_entries)
577f4f55ce3SStefan Popa {
578f4f55ce3SStefan Popa 	__be32 buf;
579f4f55ce3SStefan Popa 	u32 val;
580f4f55ce3SStefan Popa 	int ret;
581f4f55ce3SStefan Popa 
582f4f55ce3SStefan Popa 	/* STATUS1, STATUS2, FIFO_ENTRIES2 and FIFO_ENTRIES are adjacent regs */
583f4f55ce3SStefan Popa 	ret = regmap_bulk_read(st->regmap, ADXL372_STATUS_1,
584f4f55ce3SStefan Popa 			       &buf, sizeof(buf));
585f4f55ce3SStefan Popa 	if (ret < 0)
586f4f55ce3SStefan Popa 		return ret;
587f4f55ce3SStefan Popa 
588f4f55ce3SStefan Popa 	val = be32_to_cpu(buf);
589f4f55ce3SStefan Popa 
590f4f55ce3SStefan Popa 	*status1 = (val >> 24) & 0x0F;
591f4f55ce3SStefan Popa 	*status2 = (val >> 16) & 0x0F;
592f4f55ce3SStefan Popa 	/*
593f4f55ce3SStefan Popa 	 * FIFO_ENTRIES contains the least significant byte, and FIFO_ENTRIES2
594f4f55ce3SStefan Popa 	 * contains the two most significant bits
595f4f55ce3SStefan Popa 	 */
596f4f55ce3SStefan Popa 	*fifo_entries = val & 0x3FF;
597f4f55ce3SStefan Popa 
598f4f55ce3SStefan Popa 	return ret;
599f4f55ce3SStefan Popa }
600f4f55ce3SStefan Popa 
adxl372_arrange_axis_data(struct adxl372_state * st,__be16 * sample)601b0fc6783SStefan Popa static void adxl372_arrange_axis_data(struct adxl372_state *st, __be16 *sample)
602b0fc6783SStefan Popa {
603b0fc6783SStefan Popa 	__be16	axis_sample[3];
604b0fc6783SStefan Popa 	int i = 0;
605b0fc6783SStefan Popa 
606b0fc6783SStefan Popa 	memset(axis_sample, 0, 3 * sizeof(__be16));
607b0fc6783SStefan Popa 	if (ADXL372_X_AXIS_EN(st->fifo_axis_mask))
608b0fc6783SStefan Popa 		axis_sample[i++] = sample[0];
609b0fc6783SStefan Popa 	if (ADXL372_Y_AXIS_EN(st->fifo_axis_mask))
610b0fc6783SStefan Popa 		axis_sample[i++] = sample[1];
611b0fc6783SStefan Popa 	if (ADXL372_Z_AXIS_EN(st->fifo_axis_mask))
612b0fc6783SStefan Popa 		axis_sample[i++] = sample[2];
613b0fc6783SStefan Popa 
614b0fc6783SStefan Popa 	memcpy(sample, axis_sample, 3 * sizeof(__be16));
615b0fc6783SStefan Popa }
616b0fc6783SStefan Popa 
adxl372_push_event(struct iio_dev * indio_dev,s64 timestamp,u8 status2)617b0fc6783SStefan Popa static void adxl372_push_event(struct iio_dev *indio_dev, s64 timestamp, u8 status2)
618b0fc6783SStefan Popa {
619b0fc6783SStefan Popa 	unsigned int ev_dir = IIO_EV_DIR_NONE;
620b0fc6783SStefan Popa 
621b0fc6783SStefan Popa 	if (ADXL372_STATUS_2_ACT(status2))
622b0fc6783SStefan Popa 		ev_dir = IIO_EV_DIR_RISING;
623b0fc6783SStefan Popa 
624b0fc6783SStefan Popa 	if (ADXL372_STATUS_2_INACT(status2))
625b0fc6783SStefan Popa 		ev_dir = IIO_EV_DIR_FALLING;
626b0fc6783SStefan Popa 
627b0fc6783SStefan Popa 	if (ev_dir != IIO_EV_DIR_NONE)
628b0fc6783SStefan Popa 		iio_push_event(indio_dev,
629b0fc6783SStefan Popa 			       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0, IIO_MOD_X_OR_Y_OR_Z,
630b0fc6783SStefan Popa 						  IIO_EV_TYPE_THRESH, ev_dir),
631b0fc6783SStefan Popa 			       timestamp);
632b0fc6783SStefan Popa }
633b0fc6783SStefan Popa 
adxl372_trigger_handler(int irq,void * p)634f4f55ce3SStefan Popa static irqreturn_t adxl372_trigger_handler(int irq, void  *p)
635f4f55ce3SStefan Popa {
636f4f55ce3SStefan Popa 	struct iio_poll_func *pf = p;
637f4f55ce3SStefan Popa 	struct iio_dev *indio_dev = pf->indio_dev;
638f4f55ce3SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
639f4f55ce3SStefan Popa 	u8 status1, status2;
640f4f55ce3SStefan Popa 	u16 fifo_entries;
641f4f55ce3SStefan Popa 	int i, ret;
642f4f55ce3SStefan Popa 
643f4f55ce3SStefan Popa 	ret = adxl372_get_status(st, &status1, &status2, &fifo_entries);
644f4f55ce3SStefan Popa 	if (ret < 0)
645f4f55ce3SStefan Popa 		goto err;
646f4f55ce3SStefan Popa 
647b0fc6783SStefan Popa 	adxl372_push_event(indio_dev, iio_get_time_ns(indio_dev), status2);
648b0fc6783SStefan Popa 
649f4f55ce3SStefan Popa 	if (st->fifo_mode != ADXL372_FIFO_BYPASSED &&
650f4f55ce3SStefan Popa 	    ADXL372_STATUS_1_FIFO_FULL(status1)) {
651f4f55ce3SStefan Popa 		/*
652f4f55ce3SStefan Popa 		 * When reading data from multiple axes from the FIFO,
653f4f55ce3SStefan Popa 		 * to ensure that data is not overwritten and stored out
654f4f55ce3SStefan Popa 		 * of order at least one sample set must be left in the
655f4f55ce3SStefan Popa 		 * FIFO after every read.
656f4f55ce3SStefan Popa 		 */
657f4f55ce3SStefan Popa 		fifo_entries -= st->fifo_set_size;
658f4f55ce3SStefan Popa 
659f4f55ce3SStefan Popa 		/* Read data from the FIFO */
660f4f55ce3SStefan Popa 		ret = regmap_noinc_read(st->regmap, ADXL372_FIFO_DATA,
661f4f55ce3SStefan Popa 					st->fifo_buf,
662f4f55ce3SStefan Popa 					fifo_entries * sizeof(u16));
663f4f55ce3SStefan Popa 		if (ret < 0)
664f4f55ce3SStefan Popa 			goto err;
665f4f55ce3SStefan Popa 
666f4f55ce3SStefan Popa 		/* Each sample is 2 bytes */
667b0fc6783SStefan Popa 		for (i = 0; i < fifo_entries; i += st->fifo_set_size) {
668b0fc6783SStefan Popa 			/* filter peak detection data */
669b0fc6783SStefan Popa 			if (st->peak_fifo_mode_en)
670b0fc6783SStefan Popa 				adxl372_arrange_axis_data(st, &st->fifo_buf[i]);
671f4f55ce3SStefan Popa 			iio_push_to_buffers(indio_dev, &st->fifo_buf[i]);
672f4f55ce3SStefan Popa 		}
673b0fc6783SStefan Popa 	}
674f4f55ce3SStefan Popa err:
675f4f55ce3SStefan Popa 	iio_trigger_notify_done(indio_dev->trig);
676f4f55ce3SStefan Popa 	return IRQ_HANDLED;
677f4f55ce3SStefan Popa }
678f4f55ce3SStefan Popa 
adxl372_setup(struct adxl372_state * st)6794097da40SStefan Popa static int adxl372_setup(struct adxl372_state *st)
6804097da40SStefan Popa {
6814097da40SStefan Popa 	unsigned int regval;
6824097da40SStefan Popa 	int ret;
6834097da40SStefan Popa 
6844097da40SStefan Popa 	ret = regmap_read(st->regmap, ADXL372_DEVID, &regval);
6854097da40SStefan Popa 	if (ret < 0)
6864097da40SStefan Popa 		return ret;
6874097da40SStefan Popa 
6884097da40SStefan Popa 	if (regval != ADXL372_DEVID_VAL) {
689d9e8fd04SStefan Popa 		dev_err(st->dev, "Invalid chip id %x\n", regval);
6904097da40SStefan Popa 		return -ENODEV;
6914097da40SStefan Popa 	}
6924097da40SStefan Popa 
693d9a997bdSStefan Popa 	/*
694d9a997bdSStefan Popa 	 * Perform a software reset to make sure the device is in a consistent
695d9a997bdSStefan Popa 	 * state after start up.
696d9a997bdSStefan Popa 	 */
697d9a997bdSStefan Popa 	ret = regmap_write(st->regmap, ADXL372_RESET, ADXL372_RESET_CODE);
698d9a997bdSStefan Popa 	if (ret < 0)
699d9a997bdSStefan Popa 		return ret;
700d9a997bdSStefan Popa 
7014097da40SStefan Popa 	ret = adxl372_set_op_mode(st, ADXL372_STANDBY);
7024097da40SStefan Popa 	if (ret < 0)
7034097da40SStefan Popa 		return ret;
7044097da40SStefan Popa 
7054097da40SStefan Popa 	/* Set threshold for activity detection to 1g */
7064097da40SStefan Popa 	ret = adxl372_set_activity_threshold(st, ADXL372_ACTIVITY,
7074097da40SStefan Popa 					     true, true, 1000);
7084097da40SStefan Popa 	if (ret < 0)
7094097da40SStefan Popa 		return ret;
7104097da40SStefan Popa 
7114097da40SStefan Popa 	/* Set threshold for inactivity detection to 100mg */
7124097da40SStefan Popa 	ret = adxl372_set_activity_threshold(st, ADXL372_INACTIVITY,
7134097da40SStefan Popa 					     true, true, 100);
7144097da40SStefan Popa 	if (ret < 0)
7154097da40SStefan Popa 		return ret;
7164097da40SStefan Popa 
7174097da40SStefan Popa 	/* Set activity processing in Looped mode */
7184097da40SStefan Popa 	ret = adxl372_set_act_proc_mode(st, ADXL372_LOOPED);
7194097da40SStefan Popa 	if (ret < 0)
7204097da40SStefan Popa 		return ret;
7214097da40SStefan Popa 
7224097da40SStefan Popa 	ret = adxl372_set_odr(st, ADXL372_ODR_6400HZ);
7234097da40SStefan Popa 	if (ret < 0)
7244097da40SStefan Popa 		return ret;
7254097da40SStefan Popa 
7264097da40SStefan Popa 	ret = adxl372_set_bandwidth(st, ADXL372_BW_3200HZ);
7274097da40SStefan Popa 	if (ret < 0)
7284097da40SStefan Popa 		return ret;
7294097da40SStefan Popa 
7304097da40SStefan Popa 	/* Set activity timer to 1ms */
7314097da40SStefan Popa 	ret = adxl372_set_activity_time_ms(st, 1);
7324097da40SStefan Popa 	if (ret < 0)
7334097da40SStefan Popa 		return ret;
7344097da40SStefan Popa 
7354097da40SStefan Popa 	/* Set inactivity timer to 10s */
7364097da40SStefan Popa 	ret = adxl372_set_inactivity_time_ms(st, 10000);
7374097da40SStefan Popa 	if (ret < 0)
7384097da40SStefan Popa 		return ret;
7394097da40SStefan Popa 
7404097da40SStefan Popa 	/* Set the mode of operation to full bandwidth measurement mode */
7414097da40SStefan Popa 	return adxl372_set_op_mode(st, ADXL372_FULL_BW_MEASUREMENT);
7424097da40SStefan Popa }
7434097da40SStefan Popa 
adxl372_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)7444097da40SStefan Popa static int adxl372_reg_access(struct iio_dev *indio_dev,
7454097da40SStefan Popa 			      unsigned int reg,
7464097da40SStefan Popa 			      unsigned int writeval,
7474097da40SStefan Popa 			      unsigned int *readval)
7484097da40SStefan Popa {
7494097da40SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
7504097da40SStefan Popa 
7514097da40SStefan Popa 	if (readval)
7524097da40SStefan Popa 		return regmap_read(st->regmap, reg, readval);
7534097da40SStefan Popa 	else
7544097da40SStefan Popa 		return regmap_write(st->regmap, reg, writeval);
7554097da40SStefan Popa }
7564097da40SStefan Popa 
adxl372_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long info)7574097da40SStefan Popa static int adxl372_read_raw(struct iio_dev *indio_dev,
7584097da40SStefan Popa 			    struct iio_chan_spec const *chan,
7594097da40SStefan Popa 			    int *val, int *val2, long info)
7604097da40SStefan Popa {
7614097da40SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
7624097da40SStefan Popa 	int ret;
7634097da40SStefan Popa 
7644097da40SStefan Popa 	switch (info) {
7654097da40SStefan Popa 	case IIO_CHAN_INFO_RAW:
766f4f55ce3SStefan Popa 		ret = iio_device_claim_direct_mode(indio_dev);
767f4f55ce3SStefan Popa 		if (ret)
768f4f55ce3SStefan Popa 			return ret;
769f4f55ce3SStefan Popa 
7704097da40SStefan Popa 		ret = adxl372_read_axis(st, chan->address);
771f4f55ce3SStefan Popa 		iio_device_release_direct_mode(indio_dev);
7724097da40SStefan Popa 		if (ret < 0)
7734097da40SStefan Popa 			return ret;
7744097da40SStefan Popa 
7754097da40SStefan Popa 		*val = sign_extend32(ret >> chan->scan_type.shift,
7764097da40SStefan Popa 				     chan->scan_type.realbits - 1);
7774097da40SStefan Popa 		return IIO_VAL_INT;
7784097da40SStefan Popa 	case IIO_CHAN_INFO_SCALE:
7794097da40SStefan Popa 		*val = 0;
7804097da40SStefan Popa 		*val2 = ADXL372_USCALE;
7814097da40SStefan Popa 		return IIO_VAL_INT_PLUS_MICRO;
7825e605a4dSStefan Popa 	case IIO_CHAN_INFO_SAMP_FREQ:
7835e605a4dSStefan Popa 		*val = adxl372_samp_freq_tbl[st->odr];
7845e605a4dSStefan Popa 		return IIO_VAL_INT;
7857ec040afSStefan Popa 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
7867ec040afSStefan Popa 		*val = adxl372_bw_freq_tbl[st->bw];
7877ec040afSStefan Popa 		return IIO_VAL_INT;
7885e605a4dSStefan Popa 	}
7895e605a4dSStefan Popa 
7905e605a4dSStefan Popa 	return -EINVAL;
7915e605a4dSStefan Popa }
7925e605a4dSStefan Popa 
adxl372_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long info)7935e605a4dSStefan Popa static int adxl372_write_raw(struct iio_dev *indio_dev,
7945e605a4dSStefan Popa 			     struct iio_chan_spec const *chan,
7955e605a4dSStefan Popa 			     int val, int val2, long info)
7965e605a4dSStefan Popa {
7975e605a4dSStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
7987ec040afSStefan Popa 	int odr_index, bw_index, ret;
7995e605a4dSStefan Popa 
8005e605a4dSStefan Popa 	switch (info) {
8015e605a4dSStefan Popa 	case IIO_CHAN_INFO_SAMP_FREQ:
8025e605a4dSStefan Popa 		odr_index = adxl372_find_closest_match(adxl372_samp_freq_tbl,
8035e605a4dSStefan Popa 					ARRAY_SIZE(adxl372_samp_freq_tbl),
8045e605a4dSStefan Popa 					val);
8055e605a4dSStefan Popa 		ret = adxl372_set_odr(st, odr_index);
8065e605a4dSStefan Popa 		if (ret < 0)
8075e605a4dSStefan Popa 			return ret;
8085e605a4dSStefan Popa 		/*
8095e605a4dSStefan Popa 		 * The timer period depends on the ODR selected.
8105e605a4dSStefan Popa 		 * At 3200 Hz and below, it is 6.6 ms; at 6400 Hz, it is 3.3 ms
8115e605a4dSStefan Popa 		 */
8125e605a4dSStefan Popa 		ret = adxl372_set_activity_time_ms(st, st->act_time_ms);
8135e605a4dSStefan Popa 		if (ret < 0)
8145e605a4dSStefan Popa 			return ret;
8155e605a4dSStefan Popa 		/*
8165e605a4dSStefan Popa 		 * The timer period depends on the ODR selected.
8175e605a4dSStefan Popa 		 * At 3200 Hz and below, it is 26 ms; at 6400 Hz, it is 13 ms
8185e605a4dSStefan Popa 		 */
8195e605a4dSStefan Popa 		ret = adxl372_set_inactivity_time_ms(st, st->inact_time_ms);
8205e605a4dSStefan Popa 		if (ret < 0)
8215e605a4dSStefan Popa 			return ret;
8225e605a4dSStefan Popa 		/*
8235e605a4dSStefan Popa 		 * The maximum bandwidth is constrained to at most half of
8245e605a4dSStefan Popa 		 * the ODR to ensure that the Nyquist criteria is not violated
8255e605a4dSStefan Popa 		 */
8265e605a4dSStefan Popa 		if (st->bw > odr_index)
8275e605a4dSStefan Popa 			ret = adxl372_set_bandwidth(st, odr_index);
8285e605a4dSStefan Popa 
8295e605a4dSStefan Popa 		return ret;
8307ec040afSStefan Popa 	case IIO_CHAN_INFO_LOW_PASS_FILTER_3DB_FREQUENCY:
8317ec040afSStefan Popa 		bw_index = adxl372_find_closest_match(adxl372_bw_freq_tbl,
8327ec040afSStefan Popa 					ARRAY_SIZE(adxl372_bw_freq_tbl),
8337ec040afSStefan Popa 					val);
8347ec040afSStefan Popa 		return adxl372_set_bandwidth(st, bw_index);
8354097da40SStefan Popa 	default:
8364097da40SStefan Popa 		return -EINVAL;
8374097da40SStefan Popa 	}
8384097da40SStefan Popa }
8394097da40SStefan Popa 
adxl372_read_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int * val,int * val2)840b0fc6783SStefan Popa static int adxl372_read_event_value(struct iio_dev *indio_dev, const struct iio_chan_spec *chan,
841b0fc6783SStefan Popa 				    enum iio_event_type type, enum iio_event_direction dir,
842b0fc6783SStefan Popa 				    enum iio_event_info info, int *val, int *val2)
843b0fc6783SStefan Popa {
844b0fc6783SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
845b0fc6783SStefan Popa 	unsigned int addr;
846b0fc6783SStefan Popa 	u16 raw_value;
847b0fc6783SStefan Popa 	int ret;
848b0fc6783SStefan Popa 
849b0fc6783SStefan Popa 	switch (info) {
850b0fc6783SStefan Popa 	case IIO_EV_INFO_VALUE:
851b0fc6783SStefan Popa 		switch (dir) {
852b0fc6783SStefan Popa 		case IIO_EV_DIR_RISING:
853b0fc6783SStefan Popa 			addr = ADXL372_X_THRESH_ACT_H + 2 * chan->scan_index;
854b0fc6783SStefan Popa 			ret = adxl372_read_threshold_value(indio_dev, addr, &raw_value);
855b0fc6783SStefan Popa 			if (ret < 0)
856b0fc6783SStefan Popa 				return ret;
857b0fc6783SStefan Popa 			*val = raw_value * ADXL372_USCALE;
858b0fc6783SStefan Popa 			*val2 = 1000000;
859b0fc6783SStefan Popa 			return IIO_VAL_FRACTIONAL;
860b0fc6783SStefan Popa 		case IIO_EV_DIR_FALLING:
861b0fc6783SStefan Popa 			addr = ADXL372_X_THRESH_INACT_H + 2 * chan->scan_index;
862b0fc6783SStefan Popa 			ret =  adxl372_read_threshold_value(indio_dev, addr, &raw_value);
863b0fc6783SStefan Popa 			if (ret < 0)
864b0fc6783SStefan Popa 				return ret;
865b0fc6783SStefan Popa 			*val = raw_value * ADXL372_USCALE;
866b0fc6783SStefan Popa 			*val2 = 1000000;
867b0fc6783SStefan Popa 			return IIO_VAL_FRACTIONAL;
868b0fc6783SStefan Popa 		default:
869b0fc6783SStefan Popa 			return -EINVAL;
870b0fc6783SStefan Popa 		}
871b0fc6783SStefan Popa 	case IIO_EV_INFO_PERIOD:
872b0fc6783SStefan Popa 		switch (dir) {
873b0fc6783SStefan Popa 		case IIO_EV_DIR_RISING:
874b0fc6783SStefan Popa 			*val = st->act_time_ms;
875b0fc6783SStefan Popa 			*val2 = 1000;
876b0fc6783SStefan Popa 			return IIO_VAL_FRACTIONAL;
877b0fc6783SStefan Popa 		case IIO_EV_DIR_FALLING:
878b0fc6783SStefan Popa 			*val = st->inact_time_ms;
879b0fc6783SStefan Popa 			*val2 = 1000;
880b0fc6783SStefan Popa 			return IIO_VAL_FRACTIONAL;
881b0fc6783SStefan Popa 		default:
882b0fc6783SStefan Popa 			return -EINVAL;
883b0fc6783SStefan Popa 		}
884b0fc6783SStefan Popa 	default:
885b0fc6783SStefan Popa 		return -EINVAL;
886b0fc6783SStefan Popa 	}
887b0fc6783SStefan Popa }
888b0fc6783SStefan Popa 
adxl372_write_event_value(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,enum iio_event_info info,int val,int val2)889b0fc6783SStefan Popa static int adxl372_write_event_value(struct iio_dev *indio_dev, const struct iio_chan_spec *chan,
890b0fc6783SStefan Popa 				     enum iio_event_type type, enum iio_event_direction dir,
891b0fc6783SStefan Popa 				     enum iio_event_info info, int val, int val2)
892b0fc6783SStefan Popa {
893b0fc6783SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
894b0fc6783SStefan Popa 	unsigned int val_ms;
895b0fc6783SStefan Popa 	unsigned int addr;
896b0fc6783SStefan Popa 	u16 raw_val;
897b0fc6783SStefan Popa 
898b0fc6783SStefan Popa 	switch (info) {
899b0fc6783SStefan Popa 	case IIO_EV_INFO_VALUE:
900b0fc6783SStefan Popa 		raw_val = DIV_ROUND_UP(val * 1000000, ADXL372_USCALE);
901b0fc6783SStefan Popa 		switch (dir) {
902b0fc6783SStefan Popa 		case IIO_EV_DIR_RISING:
903b0fc6783SStefan Popa 			addr = ADXL372_X_THRESH_ACT_H + 2 * chan->scan_index;
904b0fc6783SStefan Popa 			return adxl372_write_threshold_value(indio_dev, addr, raw_val);
905b0fc6783SStefan Popa 		case IIO_EV_DIR_FALLING:
906b0fc6783SStefan Popa 			addr = ADXL372_X_THRESH_INACT_H + 2 * chan->scan_index;
907b0fc6783SStefan Popa 			return adxl372_write_threshold_value(indio_dev, addr, raw_val);
908b0fc6783SStefan Popa 		default:
909b0fc6783SStefan Popa 			return -EINVAL;
910b0fc6783SStefan Popa 		}
911b0fc6783SStefan Popa 	case IIO_EV_INFO_PERIOD:
912b0fc6783SStefan Popa 		val_ms = val * 1000 + DIV_ROUND_UP(val2, 1000);
913b0fc6783SStefan Popa 		switch (dir) {
914b0fc6783SStefan Popa 		case IIO_EV_DIR_RISING:
915b0fc6783SStefan Popa 			return adxl372_set_activity_time_ms(st, val_ms);
916b0fc6783SStefan Popa 		case IIO_EV_DIR_FALLING:
917b0fc6783SStefan Popa 			return adxl372_set_inactivity_time_ms(st, val_ms);
918b0fc6783SStefan Popa 		default:
919b0fc6783SStefan Popa 			return -EINVAL;
920b0fc6783SStefan Popa 		}
921b0fc6783SStefan Popa 	default:
922b0fc6783SStefan Popa 		return -EINVAL;
923b0fc6783SStefan Popa 	}
924b0fc6783SStefan Popa }
925b0fc6783SStefan Popa 
adxl372_read_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir)926b0fc6783SStefan Popa static int adxl372_read_event_config(struct iio_dev *indio_dev, const struct iio_chan_spec *chan,
927b0fc6783SStefan Popa 				     enum iio_event_type type, enum iio_event_direction dir)
928b0fc6783SStefan Popa {
929b0fc6783SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
930b0fc6783SStefan Popa 
931b0fc6783SStefan Popa 	switch (dir) {
932b0fc6783SStefan Popa 	case IIO_EV_DIR_RISING:
933b0fc6783SStefan Popa 		return FIELD_GET(ADXL372_INT1_MAP_ACT_MSK, st->int1_bitmask);
934b0fc6783SStefan Popa 	case IIO_EV_DIR_FALLING:
935b0fc6783SStefan Popa 		return FIELD_GET(ADXL372_INT1_MAP_INACT_MSK, st->int1_bitmask);
936b0fc6783SStefan Popa 	default:
937b0fc6783SStefan Popa 		return -EINVAL;
938b0fc6783SStefan Popa 	}
939b0fc6783SStefan Popa }
940b0fc6783SStefan Popa 
adxl372_write_event_config(struct iio_dev * indio_dev,const struct iio_chan_spec * chan,enum iio_event_type type,enum iio_event_direction dir,int state)941b0fc6783SStefan Popa static int adxl372_write_event_config(struct iio_dev *indio_dev, const struct iio_chan_spec *chan,
942b0fc6783SStefan Popa 				      enum iio_event_type type, enum iio_event_direction dir,
943b0fc6783SStefan Popa 				      int state)
944b0fc6783SStefan Popa {
945b0fc6783SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
946b0fc6783SStefan Popa 
947b0fc6783SStefan Popa 	switch (dir) {
948b0fc6783SStefan Popa 	case IIO_EV_DIR_RISING:
949b0fc6783SStefan Popa 		set_mask_bits(&st->int1_bitmask, ADXL372_INT1_MAP_ACT_MSK,
950b0fc6783SStefan Popa 			      ADXL372_INT1_MAP_ACT_MODE(state));
951b0fc6783SStefan Popa 		break;
952b0fc6783SStefan Popa 	case IIO_EV_DIR_FALLING:
953b0fc6783SStefan Popa 		set_mask_bits(&st->int1_bitmask, ADXL372_INT1_MAP_INACT_MSK,
954b0fc6783SStefan Popa 			      ADXL372_INT1_MAP_INACT_MODE(state));
955b0fc6783SStefan Popa 		break;
956b0fc6783SStefan Popa 	default:
957b0fc6783SStefan Popa 		return -EINVAL;
958b0fc6783SStefan Popa 	}
959b0fc6783SStefan Popa 
960b0fc6783SStefan Popa 	return adxl372_set_interrupts(st, st->int1_bitmask, 0);
961b0fc6783SStefan Popa }
962b0fc6783SStefan Popa 
adxl372_show_filter_freq_avail(struct device * dev,struct device_attribute * attr,char * buf)9637ec040afSStefan Popa static ssize_t adxl372_show_filter_freq_avail(struct device *dev,
9647ec040afSStefan Popa 					      struct device_attribute *attr,
9657ec040afSStefan Popa 					      char *buf)
9667ec040afSStefan Popa {
9677ec040afSStefan Popa 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
9687ec040afSStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
9697ec040afSStefan Popa 	int i;
9707ec040afSStefan Popa 	size_t len = 0;
9717ec040afSStefan Popa 
9727ec040afSStefan Popa 	for (i = 0; i <= st->odr; i++)
9737ec040afSStefan Popa 		len += scnprintf(buf + len, PAGE_SIZE - len,
9747ec040afSStefan Popa 				 "%d ", adxl372_bw_freq_tbl[i]);
9757ec040afSStefan Popa 
9767ec040afSStefan Popa 	buf[len - 1] = '\n';
9777ec040afSStefan Popa 
9787ec040afSStefan Popa 	return len;
9797ec040afSStefan Popa }
9807ec040afSStefan Popa 
adxl372_get_fifo_enabled(struct device * dev,struct device_attribute * attr,char * buf)981f4f55ce3SStefan Popa static ssize_t adxl372_get_fifo_enabled(struct device *dev,
982f4f55ce3SStefan Popa 					  struct device_attribute *attr,
983f4f55ce3SStefan Popa 					  char *buf)
984f4f55ce3SStefan Popa {
985f4f55ce3SStefan Popa 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
986f4f55ce3SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
987f4f55ce3SStefan Popa 
988f4f55ce3SStefan Popa 	return sprintf(buf, "%d\n", st->fifo_mode);
989f4f55ce3SStefan Popa }
990f4f55ce3SStefan Popa 
adxl372_get_fifo_watermark(struct device * dev,struct device_attribute * attr,char * buf)991f4f55ce3SStefan Popa static ssize_t adxl372_get_fifo_watermark(struct device *dev,
992f4f55ce3SStefan Popa 					  struct device_attribute *attr,
993f4f55ce3SStefan Popa 					  char *buf)
994f4f55ce3SStefan Popa {
995f4f55ce3SStefan Popa 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
996f4f55ce3SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
997f4f55ce3SStefan Popa 
998f4f55ce3SStefan Popa 	return sprintf(buf, "%d\n", st->watermark);
999f4f55ce3SStefan Popa }
1000f4f55ce3SStefan Popa 
1001f7e68045SMatti Vaittinen IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_min, "1");
1002f7e68045SMatti Vaittinen IIO_STATIC_CONST_DEVICE_ATTR(hwfifo_watermark_max,
1003f7e68045SMatti Vaittinen 			     __stringify(ADXL372_FIFO_SIZE));
1004f4f55ce3SStefan Popa static IIO_DEVICE_ATTR(hwfifo_watermark, 0444,
1005f4f55ce3SStefan Popa 		       adxl372_get_fifo_watermark, NULL, 0);
1006f4f55ce3SStefan Popa static IIO_DEVICE_ATTR(hwfifo_enabled, 0444,
1007f4f55ce3SStefan Popa 		       adxl372_get_fifo_enabled, NULL, 0);
1008f4f55ce3SStefan Popa 
10090a33755cSMatti Vaittinen static const struct iio_dev_attr *adxl372_fifo_attributes[] = {
10100a33755cSMatti Vaittinen 	&iio_dev_attr_hwfifo_watermark_min,
10110a33755cSMatti Vaittinen 	&iio_dev_attr_hwfifo_watermark_max,
10120a33755cSMatti Vaittinen 	&iio_dev_attr_hwfifo_watermark,
10130a33755cSMatti Vaittinen 	&iio_dev_attr_hwfifo_enabled,
1014f4f55ce3SStefan Popa 	NULL,
1015f4f55ce3SStefan Popa };
1016f4f55ce3SStefan Popa 
adxl372_set_watermark(struct iio_dev * indio_dev,unsigned int val)1017f4f55ce3SStefan Popa static int adxl372_set_watermark(struct iio_dev *indio_dev, unsigned int val)
1018f4f55ce3SStefan Popa {
1019f4f55ce3SStefan Popa 	struct adxl372_state *st  = iio_priv(indio_dev);
1020f4f55ce3SStefan Popa 
1021f4f55ce3SStefan Popa 	if (val > ADXL372_FIFO_SIZE)
1022f4f55ce3SStefan Popa 		val = ADXL372_FIFO_SIZE;
1023f4f55ce3SStefan Popa 
1024f4f55ce3SStefan Popa 	st->watermark = val;
1025f4f55ce3SStefan Popa 
1026f4f55ce3SStefan Popa 	return 0;
1027f4f55ce3SStefan Popa }
1028f4f55ce3SStefan Popa 
adxl372_buffer_postenable(struct iio_dev * indio_dev)1029f4f55ce3SStefan Popa static int adxl372_buffer_postenable(struct iio_dev *indio_dev)
1030f4f55ce3SStefan Popa {
1031f4f55ce3SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
1032f4f55ce3SStefan Popa 	unsigned int mask;
1033f4f55ce3SStefan Popa 	int i, ret;
1034f4f55ce3SStefan Popa 
1035b0fc6783SStefan Popa 	st->int1_bitmask |= ADXL372_INT1_MAP_FIFO_FULL_MSK;
1036b0fc6783SStefan Popa 	ret = adxl372_set_interrupts(st, st->int1_bitmask, 0);
10370e4f0b42SAlexandru Ardelean 	if (ret < 0)
1038f11d59d8SLars-Peter Clausen 		return ret;
10390e4f0b42SAlexandru Ardelean 
1040f4f55ce3SStefan Popa 	mask = *indio_dev->active_scan_mask;
1041f4f55ce3SStefan Popa 
1042f4f55ce3SStefan Popa 	for (i = 0; i < ARRAY_SIZE(adxl372_axis_lookup_table); i++) {
1043f4f55ce3SStefan Popa 		if (mask == adxl372_axis_lookup_table[i].bits)
1044f4f55ce3SStefan Popa 			break;
1045f4f55ce3SStefan Popa 	}
1046f4f55ce3SStefan Popa 
1047f11d59d8SLars-Peter Clausen 	if (i == ARRAY_SIZE(adxl372_axis_lookup_table))
1048f11d59d8SLars-Peter Clausen 		return -EINVAL;
1049f4f55ce3SStefan Popa 
1050f4f55ce3SStefan Popa 	st->fifo_format = adxl372_axis_lookup_table[i].fifo_format;
1051b0fc6783SStefan Popa 	st->fifo_axis_mask = adxl372_axis_lookup_table[i].bits;
1052f4f55ce3SStefan Popa 	st->fifo_set_size = bitmap_weight(indio_dev->active_scan_mask,
1053*6bb0d80eSNuno Sa 					  iio_get_masklength(indio_dev));
1054b0fc6783SStefan Popa 
1055b0fc6783SStefan Popa 	/* Configure the FIFO to store sets of impact event peak. */
1056b0fc6783SStefan Popa 	if (st->peak_fifo_mode_en) {
1057b0fc6783SStefan Popa 		st->fifo_set_size = 3;
1058b0fc6783SStefan Popa 		st->fifo_format = ADXL372_XYZ_PEAK_FIFO;
1059b0fc6783SStefan Popa 	}
1060b0fc6783SStefan Popa 
1061f4f55ce3SStefan Popa 	/*
1062f4f55ce3SStefan Popa 	 * The 512 FIFO samples can be allotted in several ways, such as:
1063f4f55ce3SStefan Popa 	 * 170 sample sets of concurrent 3-axis data
1064f4f55ce3SStefan Popa 	 * 256 sample sets of concurrent 2-axis data (user selectable)
1065f4f55ce3SStefan Popa 	 * 512 sample sets of single-axis data
1066b0fc6783SStefan Popa 	 * 170 sets of impact event peak (x, y, z)
1067f4f55ce3SStefan Popa 	 */
1068f4f55ce3SStefan Popa 	if ((st->watermark * st->fifo_set_size) > ADXL372_FIFO_SIZE)
1069f4f55ce3SStefan Popa 		st->watermark = (ADXL372_FIFO_SIZE  / st->fifo_set_size);
1070f4f55ce3SStefan Popa 
1071f4f55ce3SStefan Popa 	st->fifo_mode = ADXL372_FIFO_STREAMED;
1072f4f55ce3SStefan Popa 
1073f4f55ce3SStefan Popa 	ret = adxl372_configure_fifo(st);
1074f4f55ce3SStefan Popa 	if (ret < 0) {
1075f4f55ce3SStefan Popa 		st->fifo_mode = ADXL372_FIFO_BYPASSED;
1076b0fc6783SStefan Popa 		st->int1_bitmask &= ~ADXL372_INT1_MAP_FIFO_FULL_MSK;
1077b0fc6783SStefan Popa 		adxl372_set_interrupts(st, st->int1_bitmask, 0);
1078f11d59d8SLars-Peter Clausen 		return ret;
1079f4f55ce3SStefan Popa 	}
1080f4f55ce3SStefan Popa 
10810e4f0b42SAlexandru Ardelean 	return 0;
1082f4f55ce3SStefan Popa }
1083f4f55ce3SStefan Popa 
adxl372_buffer_predisable(struct iio_dev * indio_dev)1084f4f55ce3SStefan Popa static int adxl372_buffer_predisable(struct iio_dev *indio_dev)
1085f4f55ce3SStefan Popa {
1086f4f55ce3SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
1087f4f55ce3SStefan Popa 
1088b0fc6783SStefan Popa 	st->int1_bitmask &= ~ADXL372_INT1_MAP_FIFO_FULL_MSK;
1089b0fc6783SStefan Popa 	adxl372_set_interrupts(st, st->int1_bitmask, 0);
1090f4f55ce3SStefan Popa 	st->fifo_mode = ADXL372_FIFO_BYPASSED;
1091f4f55ce3SStefan Popa 	adxl372_configure_fifo(st);
1092f4f55ce3SStefan Popa 
1093f11d59d8SLars-Peter Clausen 	return 0;
1094f4f55ce3SStefan Popa }
1095f4f55ce3SStefan Popa 
1096f4f55ce3SStefan Popa static const struct iio_buffer_setup_ops adxl372_buffer_ops = {
1097f4f55ce3SStefan Popa 	.postenable = adxl372_buffer_postenable,
1098f4f55ce3SStefan Popa 	.predisable = adxl372_buffer_predisable,
1099f4f55ce3SStefan Popa };
1100f4f55ce3SStefan Popa 
adxl372_dready_trig_set_state(struct iio_trigger * trig,bool state)1101f4f55ce3SStefan Popa static int adxl372_dready_trig_set_state(struct iio_trigger *trig,
1102f4f55ce3SStefan Popa 					 bool state)
1103f4f55ce3SStefan Popa {
1104f4f55ce3SStefan Popa 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1105f4f55ce3SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
1106f4f55ce3SStefan Popa 
1107f4f55ce3SStefan Popa 	if (state)
1108b0fc6783SStefan Popa 		st->int1_bitmask |= ADXL372_INT1_MAP_FIFO_FULL_MSK;
1109f4f55ce3SStefan Popa 
1110b0fc6783SStefan Popa 	return adxl372_set_interrupts(st, st->int1_bitmask, 0);
1111f4f55ce3SStefan Popa }
1112f4f55ce3SStefan Popa 
adxl372_validate_trigger(struct iio_dev * indio_dev,struct iio_trigger * trig)11131c412a32SStefan Popa static int adxl372_validate_trigger(struct iio_dev *indio_dev,
11141c412a32SStefan Popa 				    struct iio_trigger *trig)
11151c412a32SStefan Popa {
11161c412a32SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
11171c412a32SStefan Popa 
1118b0fc6783SStefan Popa 	if (st->dready_trig != trig && st->peak_datardy_trig != trig)
11191c412a32SStefan Popa 		return -EINVAL;
11201c412a32SStefan Popa 
11211c412a32SStefan Popa 	return 0;
11221c412a32SStefan Popa }
11231c412a32SStefan Popa 
1124f4f55ce3SStefan Popa static const struct iio_trigger_ops adxl372_trigger_ops = {
11251c412a32SStefan Popa 	.validate_device = &iio_trigger_validate_own_device,
1126f4f55ce3SStefan Popa 	.set_trigger_state = adxl372_dready_trig_set_state,
1127f4f55ce3SStefan Popa };
1128f4f55ce3SStefan Popa 
adxl372_peak_dready_trig_set_state(struct iio_trigger * trig,bool state)1129b0fc6783SStefan Popa static int adxl372_peak_dready_trig_set_state(struct iio_trigger *trig,
1130b0fc6783SStefan Popa 					      bool state)
1131b0fc6783SStefan Popa {
1132b0fc6783SStefan Popa 	struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
1133b0fc6783SStefan Popa 	struct adxl372_state *st = iio_priv(indio_dev);
1134b0fc6783SStefan Popa 
1135b0fc6783SStefan Popa 	if (state)
1136b0fc6783SStefan Popa 		st->int1_bitmask |= ADXL372_INT1_MAP_FIFO_FULL_MSK;
1137b0fc6783SStefan Popa 
1138b0fc6783SStefan Popa 	st->peak_fifo_mode_en = state;
1139b0fc6783SStefan Popa 
1140b0fc6783SStefan Popa 	return adxl372_set_interrupts(st, st->int1_bitmask, 0);
1141b0fc6783SStefan Popa }
1142b0fc6783SStefan Popa 
1143b0fc6783SStefan Popa static const struct iio_trigger_ops adxl372_peak_data_trigger_ops = {
1144b0fc6783SStefan Popa 	.validate_device = &iio_trigger_validate_own_device,
1145b0fc6783SStefan Popa 	.set_trigger_state = adxl372_peak_dready_trig_set_state,
1146b0fc6783SStefan Popa };
1147b0fc6783SStefan Popa 
11485e605a4dSStefan Popa static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("400 800 1600 3200 6400");
11497ec040afSStefan Popa static IIO_DEVICE_ATTR(in_accel_filter_low_pass_3db_frequency_available,
11507ec040afSStefan Popa 		       0444, adxl372_show_filter_freq_avail, NULL, 0);
11515e605a4dSStefan Popa 
11525e605a4dSStefan Popa static struct attribute *adxl372_attributes[] = {
11535e605a4dSStefan Popa 	&iio_const_attr_sampling_frequency_available.dev_attr.attr,
11547ec040afSStefan Popa 	&iio_dev_attr_in_accel_filter_low_pass_3db_frequency_available.dev_attr.attr,
11555e605a4dSStefan Popa 	NULL,
11565e605a4dSStefan Popa };
11575e605a4dSStefan Popa 
11585e605a4dSStefan Popa static const struct attribute_group adxl372_attrs_group = {
11595e605a4dSStefan Popa 	.attrs = adxl372_attributes,
11605e605a4dSStefan Popa };
11615e605a4dSStefan Popa 
11624097da40SStefan Popa static const struct iio_info adxl372_info = {
11631c412a32SStefan Popa 	.validate_trigger = &adxl372_validate_trigger,
11645e605a4dSStefan Popa 	.attrs = &adxl372_attrs_group,
11654097da40SStefan Popa 	.read_raw = adxl372_read_raw,
11665e605a4dSStefan Popa 	.write_raw = adxl372_write_raw,
1167b0fc6783SStefan Popa 	.read_event_config = adxl372_read_event_config,
1168b0fc6783SStefan Popa 	.write_event_config = adxl372_write_event_config,
1169b0fc6783SStefan Popa 	.read_event_value = adxl372_read_event_value,
1170b0fc6783SStefan Popa 	.write_event_value = adxl372_write_event_value,
11714097da40SStefan Popa 	.debugfs_reg_access = &adxl372_reg_access,
1172f4f55ce3SStefan Popa 	.hwfifo_set_watermark = adxl372_set_watermark,
11734097da40SStefan Popa };
11744097da40SStefan Popa 
adxl372_readable_noinc_reg(struct device * dev,unsigned int reg)1175d9e8fd04SStefan Popa bool adxl372_readable_noinc_reg(struct device *dev, unsigned int reg)
1176f4f55ce3SStefan Popa {
1177f4f55ce3SStefan Popa 	return (reg == ADXL372_FIFO_DATA);
1178f4f55ce3SStefan Popa }
1179489c75afSJonathan Cameron EXPORT_SYMBOL_NS_GPL(adxl372_readable_noinc_reg, IIO_ADXL372);
1180f4f55ce3SStefan Popa 
adxl372_probe(struct device * dev,struct regmap * regmap,int irq,const char * name)1181d9e8fd04SStefan Popa int adxl372_probe(struct device *dev, struct regmap *regmap,
1182d9e8fd04SStefan Popa 		  int irq, const char *name)
11834097da40SStefan Popa {
11844097da40SStefan Popa 	struct iio_dev *indio_dev;
11854097da40SStefan Popa 	struct adxl372_state *st;
11864097da40SStefan Popa 	int ret;
11874097da40SStefan Popa 
1188d9e8fd04SStefan Popa 	indio_dev = devm_iio_device_alloc(dev, sizeof(*st));
11894097da40SStefan Popa 	if (!indio_dev)
11904097da40SStefan Popa 		return -ENOMEM;
11914097da40SStefan Popa 
11924097da40SStefan Popa 	st = iio_priv(indio_dev);
1193d9e8fd04SStefan Popa 	dev_set_drvdata(dev, indio_dev);
11944097da40SStefan Popa 
1195d9e8fd04SStefan Popa 	st->dev = dev;
11964097da40SStefan Popa 	st->regmap = regmap;
1197d9e8fd04SStefan Popa 	st->irq = irq;
11984097da40SStefan Popa 
1199b0fc6783SStefan Popa 	mutex_init(&st->threshold_m);
1200b0fc6783SStefan Popa 
12014097da40SStefan Popa 	indio_dev->channels = adxl372_channels;
12024097da40SStefan Popa 	indio_dev->num_channels = ARRAY_SIZE(adxl372_channels);
1203f4f55ce3SStefan Popa 	indio_dev->available_scan_masks = adxl372_channel_masks;
1204d9e8fd04SStefan Popa 	indio_dev->name = name;
12054097da40SStefan Popa 	indio_dev->info = &adxl372_info;
1206f4f55ce3SStefan Popa 	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
12074097da40SStefan Popa 
12084097da40SStefan Popa 	ret = adxl372_setup(st);
12094097da40SStefan Popa 	if (ret < 0) {
1210d9e8fd04SStefan Popa 		dev_err(dev, "ADXL372 setup failed\n");
12114097da40SStefan Popa 		return ret;
12124097da40SStefan Popa 	}
12134097da40SStefan Popa 
1214abef6bc9SAlexandru Ardelean 	ret = devm_iio_triggered_buffer_setup_ext(dev,
1215f4f55ce3SStefan Popa 						  indio_dev, NULL,
1216f4f55ce3SStefan Popa 						  adxl372_trigger_handler,
1217c02cd5c1SAlexandru Ardelean 						  IIO_BUFFER_DIRECTION_IN,
1218abef6bc9SAlexandru Ardelean 						  &adxl372_buffer_ops,
1219abef6bc9SAlexandru Ardelean 						  adxl372_fifo_attributes);
1220f4f55ce3SStefan Popa 	if (ret < 0)
1221f4f55ce3SStefan Popa 		return ret;
1222f4f55ce3SStefan Popa 
1223d9e8fd04SStefan Popa 	if (st->irq) {
1224d9e8fd04SStefan Popa 		st->dready_trig = devm_iio_trigger_alloc(dev,
1225f4f55ce3SStefan Popa 							 "%s-dev%d",
1226f4f55ce3SStefan Popa 							 indio_dev->name,
122715ea2878SJonathan Cameron 							 iio_device_id(indio_dev));
1228f4f55ce3SStefan Popa 		if (st->dready_trig == NULL)
1229f4f55ce3SStefan Popa 			return -ENOMEM;
1230f4f55ce3SStefan Popa 
1231b0fc6783SStefan Popa 		st->peak_datardy_trig = devm_iio_trigger_alloc(dev,
1232b0fc6783SStefan Popa 							       "%s-dev%d-peak",
1233b0fc6783SStefan Popa 							       indio_dev->name,
123415ea2878SJonathan Cameron 							       iio_device_id(indio_dev));
1235b0fc6783SStefan Popa 		if (!st->peak_datardy_trig)
1236b0fc6783SStefan Popa 			return -ENOMEM;
1237b0fc6783SStefan Popa 
1238f4f55ce3SStefan Popa 		st->dready_trig->ops = &adxl372_trigger_ops;
1239b0fc6783SStefan Popa 		st->peak_datardy_trig->ops = &adxl372_peak_data_trigger_ops;
1240f4f55ce3SStefan Popa 		iio_trigger_set_drvdata(st->dready_trig, indio_dev);
1241b0fc6783SStefan Popa 		iio_trigger_set_drvdata(st->peak_datardy_trig, indio_dev);
1242d9e8fd04SStefan Popa 		ret = devm_iio_trigger_register(dev, st->dready_trig);
1243f4f55ce3SStefan Popa 		if (ret < 0)
1244f4f55ce3SStefan Popa 			return ret;
1245f4f55ce3SStefan Popa 
1246b0fc6783SStefan Popa 		ret = devm_iio_trigger_register(dev, st->peak_datardy_trig);
1247b0fc6783SStefan Popa 		if (ret < 0)
1248b0fc6783SStefan Popa 			return ret;
1249b0fc6783SStefan Popa 
1250f4f55ce3SStefan Popa 		indio_dev->trig = iio_trigger_get(st->dready_trig);
1251f4f55ce3SStefan Popa 
1252d9e8fd04SStefan Popa 		ret = devm_request_threaded_irq(dev, st->irq,
1253f4f55ce3SStefan Popa 					iio_trigger_generic_data_rdy_poll,
1254f4f55ce3SStefan Popa 					NULL,
1255f4f55ce3SStefan Popa 					IRQF_TRIGGER_RISING | IRQF_ONESHOT,
1256f4f55ce3SStefan Popa 					indio_dev->name, st->dready_trig);
1257f4f55ce3SStefan Popa 		if (ret < 0)
1258f4f55ce3SStefan Popa 			return ret;
1259f4f55ce3SStefan Popa 	}
1260f4f55ce3SStefan Popa 
1261d9e8fd04SStefan Popa 	return devm_iio_device_register(dev, indio_dev);
12624097da40SStefan Popa }
1263489c75afSJonathan Cameron EXPORT_SYMBOL_NS_GPL(adxl372_probe, IIO_ADXL372);
12644097da40SStefan Popa 
12654097da40SStefan Popa MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
12664097da40SStefan Popa MODULE_DESCRIPTION("Analog Devices ADXL372 3-axis accelerometer driver");
12674097da40SStefan Popa MODULE_LICENSE("GPL");
1268