xref: /linux/drivers/iio/imu/bmi323/bmi323_core.c (revision 2573c25e2c482b53b6e1142ff3cd28f6de13e659)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * IIO core driver for Bosch BMI323 6-Axis IMU.
4  *
5  * Copyright (C) 2023, Jagath Jog J <jagathjog1996@gmail.com>
6  *
7  * Datasheet: https://www.bosch-sensortec.com/media/boschsensortec/downloads/datasheets/bst-bmi323-ds000.pdf
8  */
9 
10 #include <linux/bitfield.h>
11 #include <linux/cleanup.h>
12 #include <linux/device.h>
13 #include <linux/interrupt.h>
14 #include <linux/minmax.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/property.h>
18 #include <linux/regmap.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/units.h>
21 
22 #include <asm/unaligned.h>
23 
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/events.h>
26 #include <linux/iio/iio.h>
27 #include <linux/iio/sysfs.h>
28 #include <linux/iio/trigger.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
31 
32 #include "bmi323.h"
33 
34 enum bmi323_sensor_type {
35 	BMI323_ACCEL,
36 	BMI323_GYRO,
37 	BMI323_SENSORS_CNT,
38 };
39 
40 enum bmi323_opr_mode {
41 	ACC_GYRO_MODE_DISABLE = 0x00,
42 	GYRO_DRIVE_MODE_ENABLED = 0x01,
43 	ACC_GYRO_MODE_DUTYCYCLE = 0x03,
44 	ACC_GYRO_MODE_CONTINOUS = 0x04,
45 	ACC_GYRO_MODE_HIGH_PERF = 0x07,
46 };
47 
48 enum bmi323_state {
49 	BMI323_IDLE,
50 	BMI323_BUFFER_DRDY_TRIGGERED,
51 	BMI323_BUFFER_FIFO,
52 };
53 
54 enum bmi323_irq_pin {
55 	BMI323_IRQ_DISABLED,
56 	BMI323_IRQ_INT1,
57 	BMI323_IRQ_INT2,
58 };
59 
60 enum bmi323_3db_bw {
61 	BMI323_BW_ODR_BY_2,
62 	BMI323_BW_ODR_BY_4,
63 };
64 
65 enum bmi323_scan {
66 	BMI323_ACCEL_X,
67 	BMI323_ACCEL_Y,
68 	BMI323_ACCEL_Z,
69 	BMI323_GYRO_X,
70 	BMI323_GYRO_Y,
71 	BMI323_GYRO_Z,
72 	BMI323_CHAN_MAX
73 };
74 
75 struct bmi323_hw {
76 	u8 data;
77 	u8 config;
78 	const int (*scale_table)[2];
79 	int scale_table_len;
80 };
81 
82 /*
83  * The accelerometer supports +-2G/4G/8G/16G ranges, and the resolution of
84  * each sample is 16 bits, signed.
85  * At +-8G the scale can calculated by
86  * ((8 + 8) * 9.80665 / (2^16 - 1)) * 10^6 = 2394.23819 scale in micro
87  *
88  */
89 static const int bmi323_accel_scale[][2] = {
90 	{ 0, 598 },
91 	{ 0, 1197 },
92 	{ 0, 2394 },
93 	{ 0, 4788 },
94 };
95 
96 static const int bmi323_gyro_scale[][2] = {
97 	{ 0, 66 },
98 	{ 0, 133 },
99 	{ 0, 266 },
100 	{ 0, 532 },
101 	{ 0, 1065 },
102 };
103 
104 static const int bmi323_accel_gyro_avrg[] = {0, 2, 4, 8, 16, 32, 64};
105 
106 static const struct bmi323_hw bmi323_hw[2] = {
107 	[BMI323_ACCEL] = {
108 		.data = BMI323_ACCEL_X_REG,
109 		.config = BMI323_ACC_CONF_REG,
110 		.scale_table = bmi323_accel_scale,
111 		.scale_table_len = ARRAY_SIZE(bmi323_accel_scale),
112 	},
113 	[BMI323_GYRO] = {
114 		.data = BMI323_GYRO_X_REG,
115 		.config = BMI323_GYRO_CONF_REG,
116 		.scale_table = bmi323_gyro_scale,
117 		.scale_table_len = ARRAY_SIZE(bmi323_gyro_scale),
118 	},
119 };
120 
121 struct bmi323_data {
122 	struct device *dev;
123 	struct regmap *regmap;
124 	struct iio_mount_matrix orientation;
125 	enum bmi323_irq_pin irq_pin;
126 	struct iio_trigger *trig;
127 	bool drdy_trigger_enabled;
128 	enum bmi323_state state;
129 	s64 fifo_tstamp, old_fifo_tstamp;
130 	u32 odrns[BMI323_SENSORS_CNT];
131 	u32 odrhz[BMI323_SENSORS_CNT];
132 	unsigned int feature_events;
133 
134 	/*
135 	 * Lock to protect the members of device's private data from concurrent
136 	 * access and also to serialize the access of extended registers.
137 	 * See bmi323_write_ext_reg(..) for more info.
138 	 */
139 	struct mutex mutex;
140 	int watermark;
141 	__le16 fifo_buff[BMI323_FIFO_FULL_IN_WORDS] __aligned(IIO_DMA_MINALIGN);
142 	struct {
143 		__le16 channels[BMI323_CHAN_MAX];
144 		s64 ts __aligned(8);
145 	} buffer;
146 	__le16 steps_count[BMI323_STEP_LEN];
147 };
148 
149 static const struct iio_mount_matrix *
150 bmi323_get_mount_matrix(const struct iio_dev *idev,
151 			const struct iio_chan_spec *chan)
152 {
153 	struct bmi323_data *data = iio_priv(idev);
154 
155 	return &data->orientation;
156 }
157 
158 static const struct iio_chan_spec_ext_info bmi323_ext_info[] = {
159 	IIO_MOUNT_MATRIX(IIO_SHARED_BY_TYPE, bmi323_get_mount_matrix),
160 	{ }
161 };
162 
163 static const struct iio_event_spec bmi323_step_wtrmrk_event = {
164 	.type = IIO_EV_TYPE_CHANGE,
165 	.dir = IIO_EV_DIR_NONE,
166 	.mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
167 			       BIT(IIO_EV_INFO_VALUE),
168 };
169 
170 static const struct iio_event_spec bmi323_accel_event[] = {
171 	{
172 		.type = IIO_EV_TYPE_MAG,
173 		.dir = IIO_EV_DIR_FALLING,
174 		.mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
175 				       BIT(IIO_EV_INFO_PERIOD) |
176 				       BIT(IIO_EV_INFO_HYSTERESIS) |
177 				       BIT(IIO_EV_INFO_ENABLE),
178 	},
179 	{
180 		.type = IIO_EV_TYPE_MAG,
181 		.dir = IIO_EV_DIR_RISING,
182 		.mask_shared_by_type = BIT(IIO_EV_INFO_VALUE) |
183 				       BIT(IIO_EV_INFO_PERIOD) |
184 				       BIT(IIO_EV_INFO_HYSTERESIS) |
185 				       BIT(IIO_EV_INFO_ENABLE),
186 	},
187 	{
188 		.type = IIO_EV_TYPE_GESTURE,
189 		.dir = IIO_EV_DIR_SINGLETAP,
190 		.mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
191 				       BIT(IIO_EV_INFO_VALUE) |
192 				       BIT(IIO_EV_INFO_RESET_TIMEOUT),
193 	},
194 	{
195 		.type = IIO_EV_TYPE_GESTURE,
196 		.dir = IIO_EV_DIR_DOUBLETAP,
197 		.mask_shared_by_type = BIT(IIO_EV_INFO_ENABLE) |
198 				       BIT(IIO_EV_INFO_VALUE) |
199 				       BIT(IIO_EV_INFO_RESET_TIMEOUT) |
200 				       BIT(IIO_EV_INFO_TAP2_MIN_DELAY),
201 	},
202 };
203 
204 #define BMI323_ACCEL_CHANNEL(_type, _axis, _index) {			\
205 	.type = _type,							\
206 	.modified = 1,							\
207 	.channel2 = IIO_MOD_##_axis,					\
208 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
209 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
210 				    BIT(IIO_CHAN_INFO_SCALE) |		\
211 				    BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
212 	.info_mask_shared_by_type_available =				\
213 				    BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
214 				    BIT(IIO_CHAN_INFO_SCALE) |		\
215 				    BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
216 	.scan_index = _index,						\
217 	.scan_type = {							\
218 		.sign = 's',						\
219 		.realbits = 16,						\
220 		.storagebits = 16,					\
221 		.endianness = IIO_LE,					\
222 	},								\
223 	.ext_info = bmi323_ext_info,					\
224 	.event_spec = bmi323_accel_event,				\
225 	.num_event_specs = ARRAY_SIZE(bmi323_accel_event),		\
226 }
227 
228 #define BMI323_GYRO_CHANNEL(_type, _axis, _index) {			\
229 	.type = _type,							\
230 	.modified = 1,							\
231 	.channel2 = IIO_MOD_##_axis,					\
232 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),			\
233 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
234 				    BIT(IIO_CHAN_INFO_SCALE) |		\
235 				    BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
236 	.info_mask_shared_by_type_available =				\
237 				    BIT(IIO_CHAN_INFO_SAMP_FREQ) |	\
238 				    BIT(IIO_CHAN_INFO_SCALE) |		\
239 				    BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
240 	.scan_index = _index,						\
241 	.scan_type = {							\
242 		.sign = 's',						\
243 		.realbits = 16,						\
244 		.storagebits = 16,					\
245 		.endianness = IIO_LE,					\
246 	},								\
247 	.ext_info = bmi323_ext_info,					\
248 }
249 
250 static const struct iio_chan_spec bmi323_channels[] = {
251 	BMI323_ACCEL_CHANNEL(IIO_ACCEL, X, BMI323_ACCEL_X),
252 	BMI323_ACCEL_CHANNEL(IIO_ACCEL, Y, BMI323_ACCEL_Y),
253 	BMI323_ACCEL_CHANNEL(IIO_ACCEL, Z, BMI323_ACCEL_Z),
254 	BMI323_GYRO_CHANNEL(IIO_ANGL_VEL, X, BMI323_GYRO_X),
255 	BMI323_GYRO_CHANNEL(IIO_ANGL_VEL, Y, BMI323_GYRO_Y),
256 	BMI323_GYRO_CHANNEL(IIO_ANGL_VEL, Z, BMI323_GYRO_Z),
257 	{
258 		.type = IIO_TEMP,
259 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
260 				      BIT(IIO_CHAN_INFO_OFFSET) |
261 				      BIT(IIO_CHAN_INFO_SCALE),
262 		.scan_index = -1,
263 	},
264 	{
265 		.type = IIO_STEPS,
266 		.info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
267 				      BIT(IIO_CHAN_INFO_ENABLE),
268 		.scan_index = -1,
269 		.event_spec = &bmi323_step_wtrmrk_event,
270 		.num_event_specs = 1,
271 
272 	},
273 	IIO_CHAN_SOFT_TIMESTAMP(BMI323_CHAN_MAX),
274 };
275 
276 static const int bmi323_acc_gyro_odr[][2] = {
277 	{ 0, 781250 },
278 	{ 1, 562500 },
279 	{ 3, 125000 },
280 	{ 6, 250000 },
281 	{ 12, 500000 },
282 	{ 25, 0 },
283 	{ 50, 0 },
284 	{ 100, 0 },
285 	{ 200, 0 },
286 	{ 400, 0 },
287 	{ 800, 0 },
288 };
289 
290 static const int bmi323_acc_gyro_odrns[] = {
291 	1280 * MEGA,
292 	640 * MEGA,
293 	320 * MEGA,
294 	160 * MEGA,
295 	80 * MEGA,
296 	40 * MEGA,
297 	20 * MEGA,
298 	10 * MEGA,
299 	5 * MEGA,
300 	2500 * KILO,
301 	1250 * KILO,
302 };
303 
304 static enum bmi323_sensor_type bmi323_iio_to_sensor(enum iio_chan_type iio_type)
305 {
306 	switch (iio_type) {
307 	case IIO_ACCEL:
308 		return BMI323_ACCEL;
309 	case IIO_ANGL_VEL:
310 		return BMI323_GYRO;
311 	default:
312 		return -EINVAL;
313 	}
314 }
315 
316 static int bmi323_set_mode(struct bmi323_data *data,
317 			   enum bmi323_sensor_type sensor,
318 			   enum bmi323_opr_mode mode)
319 {
320 	guard(mutex)(&data->mutex);
321 	return regmap_update_bits(data->regmap, bmi323_hw[sensor].config,
322 				  BMI323_ACC_GYRO_CONF_MODE_MSK,
323 				  FIELD_PREP(BMI323_ACC_GYRO_CONF_MODE_MSK,
324 					     mode));
325 }
326 
327 /*
328  * When writing data to extended register there must be no communication to
329  * any other register before write transaction is complete.
330  * See datasheet section 6.2 Extended Register Map Description.
331  */
332 static int bmi323_write_ext_reg(struct bmi323_data *data, unsigned int ext_addr,
333 				unsigned int ext_data)
334 {
335 	int ret, feature_status;
336 
337 	ret = regmap_read(data->regmap, BMI323_FEAT_DATA_STATUS,
338 			  &feature_status);
339 	if (ret)
340 		return ret;
341 
342 	if (!FIELD_GET(BMI323_FEAT_DATA_TX_RDY_MSK, feature_status))
343 		return -EBUSY;
344 
345 	ret = regmap_write(data->regmap, BMI323_FEAT_DATA_ADDR, ext_addr);
346 	if (ret)
347 		return ret;
348 
349 	return regmap_write(data->regmap, BMI323_FEAT_DATA_TX, ext_data);
350 }
351 
352 /*
353  * When reading data from extended register there must be no communication to
354  * any other register before read transaction is complete.
355  * See datasheet section 6.2 Extended Register Map Description.
356  */
357 static int bmi323_read_ext_reg(struct bmi323_data *data, unsigned int ext_addr,
358 			       unsigned int *ext_data)
359 {
360 	int ret, feature_status;
361 
362 	ret = regmap_read(data->regmap, BMI323_FEAT_DATA_STATUS,
363 			  &feature_status);
364 	if (ret)
365 		return ret;
366 
367 	if (!FIELD_GET(BMI323_FEAT_DATA_TX_RDY_MSK, feature_status))
368 		return -EBUSY;
369 
370 	ret = regmap_write(data->regmap, BMI323_FEAT_DATA_ADDR, ext_addr);
371 	if (ret)
372 		return ret;
373 
374 	return regmap_read(data->regmap, BMI323_FEAT_DATA_TX, ext_data);
375 }
376 
377 static int bmi323_update_ext_reg(struct bmi323_data *data,
378 				 unsigned int ext_addr,
379 				 unsigned int mask, unsigned int ext_data)
380 {
381 	unsigned int value;
382 	int ret;
383 
384 	ret = bmi323_read_ext_reg(data, ext_addr, &value);
385 	if (ret)
386 		return ret;
387 
388 	set_mask_bits(&value, mask, ext_data);
389 
390 	return bmi323_write_ext_reg(data, ext_addr, value);
391 }
392 
393 static int bmi323_get_error_status(struct bmi323_data *data)
394 {
395 	int error, ret;
396 
397 	guard(mutex)(&data->mutex);
398 	ret = regmap_read(data->regmap, BMI323_ERR_REG, &error);
399 	if (ret)
400 		return ret;
401 
402 	if (error)
403 		dev_err(data->dev, "Sensor error 0x%x\n", error);
404 
405 	return error;
406 }
407 
408 static int bmi323_feature_engine_events(struct bmi323_data *data,
409 					const unsigned int event_mask,
410 					bool state)
411 {
412 	unsigned int value;
413 	int ret;
414 
415 	ret = regmap_read(data->regmap, BMI323_FEAT_IO0_REG, &value);
416 	if (ret)
417 		return ret;
418 
419 	/* Register must be cleared before changing an active config */
420 	ret = regmap_write(data->regmap, BMI323_FEAT_IO0_REG, 0);
421 	if (ret)
422 		return ret;
423 
424 	if (state)
425 		value |= event_mask;
426 	else
427 		value &= ~event_mask;
428 
429 	ret = regmap_write(data->regmap, BMI323_FEAT_IO0_REG, value);
430 	if (ret)
431 		return ret;
432 
433 	return regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG,
434 			    BMI323_FEAT_IO_STATUS_MSK);
435 }
436 
437 static int bmi323_step_wtrmrk_en(struct bmi323_data *data, int state)
438 {
439 	enum bmi323_irq_pin step_irq;
440 	int ret;
441 
442 	guard(mutex)(&data->mutex);
443 	if (!FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK, data->feature_events))
444 		return -EINVAL;
445 
446 	if (state)
447 		step_irq = data->irq_pin;
448 	else
449 		step_irq = BMI323_IRQ_DISABLED;
450 
451 	ret = bmi323_update_ext_reg(data, BMI323_STEP_SC1_REG,
452 				    BMI323_STEP_SC1_WTRMRK_MSK,
453 				    FIELD_PREP(BMI323_STEP_SC1_WTRMRK_MSK,
454 					       state ? 1 : 0));
455 	if (ret)
456 		return ret;
457 
458 	return regmap_update_bits(data->regmap, BMI323_INT_MAP1_REG,
459 				  BMI323_STEP_CNT_MSK,
460 				  FIELD_PREP(BMI323_STEP_CNT_MSK, step_irq));
461 }
462 
463 static int bmi323_motion_config_reg(enum iio_event_direction dir)
464 {
465 	switch (dir) {
466 	case IIO_EV_DIR_RISING:
467 		return BMI323_ANYMO1_REG;
468 	case IIO_EV_DIR_FALLING:
469 		return BMI323_NOMO1_REG;
470 	default:
471 		return -EINVAL;
472 	}
473 }
474 
475 static int bmi323_motion_event_en(struct bmi323_data *data,
476 				  enum iio_event_direction dir, int state)
477 {
478 	unsigned int state_value = state ? BMI323_FEAT_XYZ_MSK : 0;
479 	int config, ret, msk, raw, field_value;
480 	enum bmi323_irq_pin motion_irq;
481 	int irq_msk, irq_field_val;
482 
483 	if (state)
484 		motion_irq = data->irq_pin;
485 	else
486 		motion_irq = BMI323_IRQ_DISABLED;
487 
488 	switch (dir) {
489 	case IIO_EV_DIR_RISING:
490 		msk = BMI323_FEAT_IO0_XYZ_MOTION_MSK;
491 		raw = 512;
492 		config = BMI323_ANYMO1_REG;
493 		irq_msk = BMI323_MOTION_MSK;
494 		irq_field_val = FIELD_PREP(BMI323_MOTION_MSK, motion_irq);
495 		field_value = FIELD_PREP(BMI323_FEAT_IO0_XYZ_MOTION_MSK,
496 					 state_value);
497 		break;
498 	case IIO_EV_DIR_FALLING:
499 		msk = BMI323_FEAT_IO0_XYZ_NOMOTION_MSK;
500 		raw = 0;
501 		config = BMI323_NOMO1_REG;
502 		irq_msk = BMI323_NOMOTION_MSK;
503 		irq_field_val = FIELD_PREP(BMI323_NOMOTION_MSK, motion_irq);
504 		field_value = FIELD_PREP(BMI323_FEAT_IO0_XYZ_NOMOTION_MSK,
505 					 state_value);
506 		break;
507 	default:
508 		return -EINVAL;
509 	}
510 
511 	guard(mutex)(&data->mutex);
512 	ret = bmi323_feature_engine_events(data, msk, state);
513 	if (ret)
514 		return ret;
515 
516 	ret = bmi323_update_ext_reg(data, config,
517 				    BMI323_MO1_REF_UP_MSK,
518 				    FIELD_PREP(BMI323_MO1_REF_UP_MSK, 0));
519 	if (ret)
520 		return ret;
521 
522 	/* Set initial value to avoid interrupts while enabling*/
523 	ret = bmi323_update_ext_reg(data, config,
524 				    BMI323_MO1_SLOPE_TH_MSK,
525 				    FIELD_PREP(BMI323_MO1_SLOPE_TH_MSK, raw));
526 	if (ret)
527 		return ret;
528 
529 	ret = regmap_update_bits(data->regmap, BMI323_INT_MAP1_REG, irq_msk,
530 				 irq_field_val);
531 	if (ret)
532 		return ret;
533 
534 	set_mask_bits(&data->feature_events, msk, field_value);
535 
536 	return 0;
537 }
538 
539 static int bmi323_tap_event_en(struct bmi323_data *data,
540 			       enum iio_event_direction dir, int state)
541 {
542 	enum bmi323_irq_pin tap_irq;
543 	int ret, tap_enabled;
544 
545 	guard(mutex)(&data->mutex);
546 
547 	if (data->odrhz[BMI323_ACCEL] < 200) {
548 		dev_err(data->dev, "Invalid accelerometer parameter\n");
549 		return -EINVAL;
550 	}
551 
552 	switch (dir) {
553 	case IIO_EV_DIR_SINGLETAP:
554 		ret = bmi323_feature_engine_events(data,
555 						   BMI323_FEAT_IO0_S_TAP_MSK,
556 						   state);
557 		if (ret)
558 			return ret;
559 
560 		set_mask_bits(&data->feature_events, BMI323_FEAT_IO0_S_TAP_MSK,
561 			      FIELD_PREP(BMI323_FEAT_IO0_S_TAP_MSK, state));
562 		break;
563 	case IIO_EV_DIR_DOUBLETAP:
564 		ret = bmi323_feature_engine_events(data,
565 						   BMI323_FEAT_IO0_D_TAP_MSK,
566 						   state);
567 		if (ret)
568 			return ret;
569 
570 		set_mask_bits(&data->feature_events, BMI323_FEAT_IO0_D_TAP_MSK,
571 			      FIELD_PREP(BMI323_FEAT_IO0_D_TAP_MSK, state));
572 		break;
573 	default:
574 		return -EINVAL;
575 	}
576 
577 	tap_enabled = FIELD_GET(BMI323_FEAT_IO0_S_TAP_MSK |
578 				BMI323_FEAT_IO0_D_TAP_MSK,
579 				data->feature_events);
580 
581 	if (tap_enabled)
582 		tap_irq = data->irq_pin;
583 	else
584 		tap_irq = BMI323_IRQ_DISABLED;
585 
586 	ret = regmap_update_bits(data->regmap, BMI323_INT_MAP2_REG,
587 				 BMI323_TAP_MSK,
588 				 FIELD_PREP(BMI323_TAP_MSK, tap_irq));
589 	if (ret)
590 		return ret;
591 
592 	if (!state)
593 		return 0;
594 
595 	ret = bmi323_update_ext_reg(data, BMI323_TAP1_REG,
596 				    BMI323_TAP1_MAX_PEAKS_MSK,
597 				    FIELD_PREP(BMI323_TAP1_MAX_PEAKS_MSK,
598 					       0x04));
599 	if (ret)
600 		return ret;
601 
602 	ret = bmi323_update_ext_reg(data, BMI323_TAP1_REG,
603 				    BMI323_TAP1_AXIS_SEL_MSK,
604 				    FIELD_PREP(BMI323_TAP1_AXIS_SEL_MSK,
605 					       BMI323_AXIS_XYZ_MSK));
606 	if (ret)
607 		return ret;
608 
609 	return bmi323_update_ext_reg(data, BMI323_TAP1_REG,
610 				     BMI323_TAP1_TIMOUT_MSK,
611 				     FIELD_PREP(BMI323_TAP1_TIMOUT_MSK,
612 						0));
613 }
614 
615 static ssize_t in_accel_gesture_tap_wait_dur_show(struct device *dev,
616 						  struct device_attribute *attr,
617 						  char *buf)
618 {
619 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
620 	struct bmi323_data *data = iio_priv(indio_dev);
621 	unsigned int reg_value, raw;
622 	int ret, val[2];
623 
624 	scoped_guard(mutex, &data->mutex) {
625 		ret = bmi323_read_ext_reg(data, BMI323_TAP2_REG, &reg_value);
626 		if (ret)
627 			return ret;
628 	}
629 
630 	raw = FIELD_GET(BMI323_TAP2_MAX_DUR_MSK, reg_value);
631 	val[0] = raw / BMI323_MAX_GES_DUR_SCALE;
632 	val[1] = BMI323_RAW_TO_MICRO(raw, BMI323_MAX_GES_DUR_SCALE);
633 
634 	return iio_format_value(buf, IIO_VAL_INT_PLUS_MICRO, ARRAY_SIZE(val),
635 				val);
636 }
637 
638 static ssize_t in_accel_gesture_tap_wait_dur_store(struct device *dev,
639 						   struct device_attribute *attr,
640 						   const char *buf, size_t len)
641 {
642 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
643 	struct bmi323_data *data = iio_priv(indio_dev);
644 	int ret, val_int, val_fract, raw;
645 
646 	ret = iio_str_to_fixpoint(buf, 100000, &val_int, &val_fract);
647 	if (ret)
648 		return ret;
649 
650 	raw = BMI323_INT_MICRO_TO_RAW(val_int, val_fract,
651 				      BMI323_MAX_GES_DUR_SCALE);
652 	if (!in_range(raw, 0, 64))
653 		return -EINVAL;
654 
655 	guard(mutex)(&data->mutex);
656 	ret = bmi323_update_ext_reg(data, BMI323_TAP2_REG,
657 				    BMI323_TAP2_MAX_DUR_MSK,
658 				    FIELD_PREP(BMI323_TAP2_MAX_DUR_MSK, raw));
659 	if (ret)
660 		return ret;
661 
662 	return len;
663 }
664 
665 /*
666  * Maximum duration from first tap within the second tap is expected to happen.
667  * This timeout is applicable only if gesture_tap_wait_timeout is enabled.
668  */
669 static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_wait_dur, 0);
670 
671 static ssize_t in_accel_gesture_tap_wait_timeout_show(struct device *dev,
672 						      struct device_attribute *attr,
673 						      char *buf)
674 {
675 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
676 	struct bmi323_data *data = iio_priv(indio_dev);
677 	unsigned int reg_value, raw;
678 	int ret;
679 
680 	scoped_guard(mutex, &data->mutex) {
681 		ret = bmi323_read_ext_reg(data, BMI323_TAP1_REG, &reg_value);
682 		if (ret)
683 			return ret;
684 	}
685 
686 	raw = FIELD_GET(BMI323_TAP1_TIMOUT_MSK, reg_value);
687 
688 	return iio_format_value(buf, IIO_VAL_INT, 1, &raw);
689 }
690 
691 static ssize_t in_accel_gesture_tap_wait_timeout_store(struct device *dev,
692 						       struct device_attribute *attr,
693 						       const char *buf,
694 						       size_t len)
695 {
696 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
697 	struct bmi323_data *data = iio_priv(indio_dev);
698 	bool val;
699 	int ret;
700 
701 	ret = kstrtobool(buf, &val);
702 	if (ret)
703 		return ret;
704 
705 	guard(mutex)(&data->mutex);
706 	ret = bmi323_update_ext_reg(data, BMI323_TAP1_REG,
707 				    BMI323_TAP1_TIMOUT_MSK,
708 				    FIELD_PREP(BMI323_TAP1_TIMOUT_MSK, val));
709 	if (ret)
710 		return ret;
711 
712 	return len;
713 }
714 
715 /* Enable/disable gesture confirmation with wait time */
716 static IIO_DEVICE_ATTR_RW(in_accel_gesture_tap_wait_timeout, 0);
717 
718 static IIO_CONST_ATTR(in_accel_gesture_tap_wait_dur_available,
719 		      "[0.0 0.04 2.52]");
720 
721 static IIO_CONST_ATTR(in_accel_gesture_doubletap_tap2_min_delay_available,
722 		      "[0.005 0.005 0.075]");
723 
724 static IIO_CONST_ATTR(in_accel_gesture_tap_reset_timeout_available,
725 		      "[0.04 0.04 0.6]");
726 
727 static IIO_CONST_ATTR(in_accel_gesture_tap_value_available, "[0.0 0.002 1.99]");
728 
729 static IIO_CONST_ATTR(in_accel_mag_value_available, "[0.0 0.002 7.99]");
730 
731 static IIO_CONST_ATTR(in_accel_mag_period_available, "[0.0 0.02 162.0]");
732 
733 static IIO_CONST_ATTR(in_accel_mag_hysteresis_available, "[0.0 0.002 1.99]");
734 
735 static struct attribute *bmi323_event_attributes[] = {
736 	&iio_const_attr_in_accel_gesture_tap_value_available.dev_attr.attr,
737 	&iio_const_attr_in_accel_gesture_tap_reset_timeout_available.dev_attr.attr,
738 	&iio_const_attr_in_accel_gesture_doubletap_tap2_min_delay_available.dev_attr.attr,
739 	&iio_const_attr_in_accel_gesture_tap_wait_dur_available.dev_attr.attr,
740 	&iio_dev_attr_in_accel_gesture_tap_wait_timeout.dev_attr.attr,
741 	&iio_dev_attr_in_accel_gesture_tap_wait_dur.dev_attr.attr,
742 	&iio_const_attr_in_accel_mag_value_available.dev_attr.attr,
743 	&iio_const_attr_in_accel_mag_period_available.dev_attr.attr,
744 	&iio_const_attr_in_accel_mag_hysteresis_available.dev_attr.attr,
745 	NULL
746 };
747 
748 static const struct attribute_group bmi323_event_attribute_group = {
749 	.attrs = bmi323_event_attributes,
750 };
751 
752 static int bmi323_write_event_config(struct iio_dev *indio_dev,
753 				     const struct iio_chan_spec *chan,
754 				     enum iio_event_type type,
755 				     enum iio_event_direction dir, int state)
756 {
757 	struct bmi323_data *data = iio_priv(indio_dev);
758 
759 	switch (type) {
760 	case IIO_EV_TYPE_MAG:
761 		return bmi323_motion_event_en(data, dir, state);
762 	case IIO_EV_TYPE_GESTURE:
763 		return bmi323_tap_event_en(data, dir, state);
764 	case IIO_EV_TYPE_CHANGE:
765 		return bmi323_step_wtrmrk_en(data, state);
766 	default:
767 		return -EINVAL;
768 	}
769 }
770 
771 static int bmi323_read_event_config(struct iio_dev *indio_dev,
772 				    const struct iio_chan_spec *chan,
773 				    enum iio_event_type type,
774 				    enum iio_event_direction dir)
775 {
776 	struct bmi323_data *data = iio_priv(indio_dev);
777 	int ret, value, reg_val;
778 
779 	guard(mutex)(&data->mutex);
780 
781 	switch (chan->type) {
782 	case IIO_ACCEL:
783 		switch (dir) {
784 		case IIO_EV_DIR_SINGLETAP:
785 			ret = FIELD_GET(BMI323_FEAT_IO0_S_TAP_MSK,
786 					data->feature_events);
787 			break;
788 		case IIO_EV_DIR_DOUBLETAP:
789 			ret = FIELD_GET(BMI323_FEAT_IO0_D_TAP_MSK,
790 					data->feature_events);
791 			break;
792 		case IIO_EV_DIR_RISING:
793 			value = FIELD_GET(BMI323_FEAT_IO0_XYZ_MOTION_MSK,
794 					  data->feature_events);
795 			ret = value ? 1 : 0;
796 			break;
797 		case IIO_EV_DIR_FALLING:
798 			value = FIELD_GET(BMI323_FEAT_IO0_XYZ_NOMOTION_MSK,
799 					  data->feature_events);
800 			ret = value ? 1 : 0;
801 			break;
802 		default:
803 			ret = -EINVAL;
804 			break;
805 		}
806 		return ret;
807 	case IIO_STEPS:
808 		ret = regmap_read(data->regmap, BMI323_INT_MAP1_REG, &reg_val);
809 		if (ret)
810 			return ret;
811 
812 		return FIELD_GET(BMI323_STEP_CNT_MSK, reg_val) ? 1 : 0;
813 	default:
814 		return -EINVAL;
815 	}
816 }
817 
818 static int bmi323_write_event_value(struct iio_dev *indio_dev,
819 				    const struct iio_chan_spec *chan,
820 				    enum iio_event_type type,
821 				    enum iio_event_direction dir,
822 				    enum iio_event_info info,
823 				    int val, int val2)
824 {
825 	struct bmi323_data *data = iio_priv(indio_dev);
826 	unsigned int raw;
827 	int reg;
828 
829 	guard(mutex)(&data->mutex);
830 
831 	switch (type) {
832 	case IIO_EV_TYPE_GESTURE:
833 		switch (info) {
834 		case IIO_EV_INFO_VALUE:
835 			if (!in_range(val, 0, 2))
836 				return -EINVAL;
837 
838 			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
839 						      BMI323_TAP_THRES_SCALE);
840 
841 			return bmi323_update_ext_reg(data, BMI323_TAP2_REG,
842 						     BMI323_TAP2_THRES_MSK,
843 						     FIELD_PREP(BMI323_TAP2_THRES_MSK,
844 								raw));
845 		case IIO_EV_INFO_RESET_TIMEOUT:
846 			if (val || !in_range(val2, 40000, 560001))
847 				return -EINVAL;
848 
849 			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
850 						      BMI323_QUITE_TIM_GES_SCALE);
851 
852 			return bmi323_update_ext_reg(data, BMI323_TAP3_REG,
853 						     BMI323_TAP3_QT_AFT_GES_MSK,
854 						     FIELD_PREP(BMI323_TAP3_QT_AFT_GES_MSK,
855 								raw));
856 		case IIO_EV_INFO_TAP2_MIN_DELAY:
857 			if (val || !in_range(val2, 5000, 70001))
858 				return -EINVAL;
859 
860 			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
861 						      BMI323_DUR_BW_TAP_SCALE);
862 
863 			return bmi323_update_ext_reg(data, BMI323_TAP3_REG,
864 						     BMI323_TAP3_QT_BW_TAP_MSK,
865 						     FIELD_PREP(BMI323_TAP3_QT_BW_TAP_MSK,
866 								raw));
867 		default:
868 			return -EINVAL;
869 		}
870 	case IIO_EV_TYPE_MAG:
871 		reg = bmi323_motion_config_reg(dir);
872 		if (reg < 0)
873 			return -EINVAL;
874 
875 		switch (info) {
876 		case IIO_EV_INFO_VALUE:
877 			if (!in_range(val, 0, 8))
878 				return -EINVAL;
879 
880 			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
881 						      BMI323_MOTION_THRES_SCALE);
882 
883 			return bmi323_update_ext_reg(data, reg,
884 						     BMI323_MO1_SLOPE_TH_MSK,
885 						     FIELD_PREP(BMI323_MO1_SLOPE_TH_MSK,
886 								raw));
887 		case IIO_EV_INFO_PERIOD:
888 			if (!in_range(val, 0, 163))
889 				return -EINVAL;
890 
891 			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
892 						      BMI323_MOTION_DURAT_SCALE);
893 
894 			return bmi323_update_ext_reg(data,
895 						     reg + BMI323_MO3_OFFSET,
896 						     BMI323_MO3_DURA_MSK,
897 						     FIELD_PREP(BMI323_MO3_DURA_MSK,
898 								raw));
899 		case IIO_EV_INFO_HYSTERESIS:
900 			if (!in_range(val, 0, 2))
901 				return -EINVAL;
902 
903 			raw = BMI323_INT_MICRO_TO_RAW(val, val2,
904 						      BMI323_MOTION_HYSTR_SCALE);
905 
906 			return bmi323_update_ext_reg(data,
907 						     reg + BMI323_MO2_OFFSET,
908 						     BMI323_MO2_HYSTR_MSK,
909 						     FIELD_PREP(BMI323_MO2_HYSTR_MSK,
910 								raw));
911 		default:
912 			return -EINVAL;
913 		}
914 	case IIO_EV_TYPE_CHANGE:
915 		if (!in_range(val, 0, 20461))
916 			return -EINVAL;
917 
918 		raw = val / 20;
919 		return bmi323_update_ext_reg(data, BMI323_STEP_SC1_REG,
920 					     BMI323_STEP_SC1_WTRMRK_MSK,
921 					     FIELD_PREP(BMI323_STEP_SC1_WTRMRK_MSK,
922 							raw));
923 	default:
924 		return -EINVAL;
925 	}
926 }
927 
928 static int bmi323_read_event_value(struct iio_dev *indio_dev,
929 				   const struct iio_chan_spec *chan,
930 				   enum iio_event_type type,
931 				   enum iio_event_direction dir,
932 				   enum iio_event_info info,
933 				   int *val, int *val2)
934 {
935 	struct bmi323_data *data = iio_priv(indio_dev);
936 	unsigned int raw, reg_value;
937 	int ret, reg;
938 
939 	guard(mutex)(&data->mutex);
940 
941 	switch (type) {
942 	case IIO_EV_TYPE_GESTURE:
943 		switch (info) {
944 		case IIO_EV_INFO_VALUE:
945 			ret = bmi323_read_ext_reg(data, BMI323_TAP2_REG,
946 						  &reg_value);
947 			if (ret)
948 				return ret;
949 
950 			raw = FIELD_GET(BMI323_TAP2_THRES_MSK, reg_value);
951 			*val = raw / BMI323_TAP_THRES_SCALE;
952 			*val2 = BMI323_RAW_TO_MICRO(raw, BMI323_TAP_THRES_SCALE);
953 			return IIO_VAL_INT_PLUS_MICRO;
954 		case IIO_EV_INFO_RESET_TIMEOUT:
955 			ret = bmi323_read_ext_reg(data, BMI323_TAP3_REG,
956 						  &reg_value);
957 			if (ret)
958 				return ret;
959 
960 			raw = FIELD_GET(BMI323_TAP3_QT_AFT_GES_MSK, reg_value);
961 			*val = 0;
962 			*val2 = BMI323_RAW_TO_MICRO(raw,
963 						    BMI323_QUITE_TIM_GES_SCALE);
964 			return IIO_VAL_INT_PLUS_MICRO;
965 		case IIO_EV_INFO_TAP2_MIN_DELAY:
966 			ret = bmi323_read_ext_reg(data, BMI323_TAP3_REG,
967 						  &reg_value);
968 			if (ret)
969 				return ret;
970 
971 			raw = FIELD_GET(BMI323_TAP3_QT_BW_TAP_MSK, reg_value);
972 			*val = 0;
973 			*val2 = BMI323_RAW_TO_MICRO(raw,
974 						    BMI323_DUR_BW_TAP_SCALE);
975 			return IIO_VAL_INT_PLUS_MICRO;
976 		default:
977 			return -EINVAL;
978 		}
979 	case IIO_EV_TYPE_MAG:
980 		reg = bmi323_motion_config_reg(dir);
981 		if (reg < 0)
982 			return -EINVAL;
983 
984 		switch (info) {
985 		case IIO_EV_INFO_VALUE:
986 			ret = bmi323_read_ext_reg(data, reg, &reg_value);
987 			if (ret)
988 				return ret;
989 
990 			raw = FIELD_GET(BMI323_MO1_SLOPE_TH_MSK, reg_value);
991 			*val = raw / BMI323_MOTION_THRES_SCALE;
992 			*val2 = BMI323_RAW_TO_MICRO(raw,
993 						    BMI323_MOTION_THRES_SCALE);
994 			return IIO_VAL_INT_PLUS_MICRO;
995 		case IIO_EV_INFO_PERIOD:
996 			ret = bmi323_read_ext_reg(data,
997 						  reg + BMI323_MO3_OFFSET,
998 						  &reg_value);
999 			if (ret)
1000 				return ret;
1001 
1002 			raw = FIELD_GET(BMI323_MO3_DURA_MSK, reg_value);
1003 			*val = raw / BMI323_MOTION_DURAT_SCALE;
1004 			*val2 = BMI323_RAW_TO_MICRO(raw,
1005 						    BMI323_MOTION_DURAT_SCALE);
1006 			return IIO_VAL_INT_PLUS_MICRO;
1007 		case IIO_EV_INFO_HYSTERESIS:
1008 			ret = bmi323_read_ext_reg(data,
1009 						  reg + BMI323_MO2_OFFSET,
1010 						  &reg_value);
1011 			if (ret)
1012 				return ret;
1013 
1014 			raw = FIELD_GET(BMI323_MO2_HYSTR_MSK, reg_value);
1015 			*val = raw / BMI323_MOTION_HYSTR_SCALE;
1016 			*val2 = BMI323_RAW_TO_MICRO(raw,
1017 						    BMI323_MOTION_HYSTR_SCALE);
1018 			return IIO_VAL_INT_PLUS_MICRO;
1019 		default:
1020 			return -EINVAL;
1021 		}
1022 	case IIO_EV_TYPE_CHANGE:
1023 		ret = bmi323_read_ext_reg(data, BMI323_STEP_SC1_REG,
1024 					  &reg_value);
1025 		if (ret)
1026 			return ret;
1027 
1028 		raw = FIELD_GET(BMI323_STEP_SC1_WTRMRK_MSK, reg_value);
1029 		*val = raw * 20;
1030 		return IIO_VAL_INT;
1031 	default:
1032 		return -EINVAL;
1033 	}
1034 }
1035 
1036 static int __bmi323_fifo_flush(struct iio_dev *indio_dev)
1037 {
1038 	struct bmi323_data *data = iio_priv(indio_dev);
1039 	int i, ret, fifo_lvl, frame_count, bit, index;
1040 	__le16 *frame, *pchannels;
1041 	u64 sample_period;
1042 	s64 tstamp;
1043 
1044 	guard(mutex)(&data->mutex);
1045 	ret = regmap_read(data->regmap, BMI323_FIFO_FILL_LEVEL_REG, &fifo_lvl);
1046 	if (ret)
1047 		return ret;
1048 
1049 	fifo_lvl = min(fifo_lvl, BMI323_FIFO_FULL_IN_WORDS);
1050 
1051 	frame_count = fifo_lvl / BMI323_FIFO_FRAME_LENGTH;
1052 	if (!frame_count)
1053 		return -EINVAL;
1054 
1055 	if (fifo_lvl % BMI323_FIFO_FRAME_LENGTH)
1056 		dev_warn(data->dev, "Bad FIFO alignment\n");
1057 
1058 	/*
1059 	 * Approximate timestamps for each of the sample based on the sampling
1060 	 * frequency, timestamp for last sample and number of samples.
1061 	 */
1062 	if (data->old_fifo_tstamp) {
1063 		sample_period = data->fifo_tstamp - data->old_fifo_tstamp;
1064 		do_div(sample_period, frame_count);
1065 	} else {
1066 		sample_period = data->odrns[BMI323_ACCEL];
1067 	}
1068 
1069 	tstamp = data->fifo_tstamp - (frame_count - 1) * sample_period;
1070 
1071 	ret = regmap_noinc_read(data->regmap, BMI323_FIFO_DATA_REG,
1072 				&data->fifo_buff[0],
1073 				fifo_lvl * BMI323_BYTES_PER_SAMPLE);
1074 	if (ret)
1075 		return ret;
1076 
1077 	for (i = 0; i < frame_count; i++) {
1078 		frame = &data->fifo_buff[i * BMI323_FIFO_FRAME_LENGTH];
1079 		pchannels = &data->buffer.channels[0];
1080 
1081 		index = 0;
1082 		for_each_set_bit(bit, indio_dev->active_scan_mask,
1083 				 BMI323_CHAN_MAX)
1084 			pchannels[index++] = frame[bit];
1085 
1086 		iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
1087 						   tstamp);
1088 
1089 		tstamp += sample_period;
1090 	}
1091 
1092 	return frame_count;
1093 }
1094 
1095 static int bmi323_set_watermark(struct iio_dev *indio_dev, unsigned int val)
1096 {
1097 	struct bmi323_data *data = iio_priv(indio_dev);
1098 
1099 	val = min(val, (u32)BMI323_FIFO_FULL_IN_FRAMES);
1100 
1101 	guard(mutex)(&data->mutex);
1102 	data->watermark = val;
1103 
1104 	return 0;
1105 }
1106 
1107 static int bmi323_fifo_disable(struct bmi323_data *data)
1108 {
1109 	int ret;
1110 
1111 	guard(mutex)(&data->mutex);
1112 	ret = regmap_write(data->regmap, BMI323_FIFO_CONF_REG, 0);
1113 	if (ret)
1114 		return ret;
1115 
1116 	ret = regmap_update_bits(data->regmap, BMI323_INT_MAP2_REG,
1117 				 BMI323_FIFO_WTRMRK_MSK,
1118 				 FIELD_PREP(BMI323_FIFO_WTRMRK_MSK, 0));
1119 	if (ret)
1120 		return ret;
1121 
1122 	data->fifo_tstamp = 0;
1123 	data->state = BMI323_IDLE;
1124 
1125 	return 0;
1126 }
1127 
1128 static int bmi323_buffer_predisable(struct iio_dev *indio_dev)
1129 {
1130 	struct bmi323_data *data = iio_priv(indio_dev);
1131 
1132 	if (iio_device_get_current_mode(indio_dev) == INDIO_BUFFER_TRIGGERED)
1133 		return 0;
1134 
1135 	return bmi323_fifo_disable(data);
1136 }
1137 
1138 static int bmi323_update_watermark(struct bmi323_data *data)
1139 {
1140 	int wtrmrk;
1141 
1142 	wtrmrk = data->watermark * BMI323_FIFO_FRAME_LENGTH;
1143 
1144 	return regmap_write(data->regmap, BMI323_FIFO_WTRMRK_REG, wtrmrk);
1145 }
1146 
1147 static int bmi323_fifo_enable(struct bmi323_data *data)
1148 {
1149 	int ret;
1150 
1151 	guard(mutex)(&data->mutex);
1152 	ret = regmap_update_bits(data->regmap, BMI323_FIFO_CONF_REG,
1153 				 BMI323_FIFO_CONF_ACC_GYR_EN_MSK,
1154 				 FIELD_PREP(BMI323_FIFO_CONF_ACC_GYR_EN_MSK,
1155 					    BMI323_FIFO_ACC_GYR_MSK));
1156 	if (ret)
1157 		return ret;
1158 
1159 	ret = regmap_update_bits(data->regmap, BMI323_INT_MAP2_REG,
1160 				 BMI323_FIFO_WTRMRK_MSK,
1161 				 FIELD_PREP(BMI323_FIFO_WTRMRK_MSK,
1162 					    data->irq_pin));
1163 	if (ret)
1164 		return ret;
1165 
1166 	ret = bmi323_update_watermark(data);
1167 	if (ret)
1168 		return ret;
1169 
1170 	ret = regmap_write(data->regmap, BMI323_FIFO_CTRL_REG,
1171 			   BMI323_FIFO_FLUSH_MSK);
1172 	if (ret)
1173 		return ret;
1174 
1175 	data->state = BMI323_BUFFER_FIFO;
1176 
1177 	return 0;
1178 }
1179 
1180 static int bmi323_buffer_preenable(struct iio_dev *indio_dev)
1181 {
1182 	struct bmi323_data *data = iio_priv(indio_dev);
1183 
1184 	guard(mutex)(&data->mutex);
1185 	/*
1186 	 * When the ODR of the accelerometer and gyroscope do not match, the
1187 	 * maximum ODR value between the accelerometer and gyroscope is used
1188 	 * for FIFO and the signal with lower ODR will insert dummy frame.
1189 	 * So allow buffer read only when ODR's of accelero and gyro are equal.
1190 	 * See datasheet section 5.7 "FIFO Data Buffering".
1191 	 */
1192 	if (data->odrns[BMI323_ACCEL] != data->odrns[BMI323_GYRO]) {
1193 		dev_err(data->dev, "Accelero and Gyro ODR doesn't match\n");
1194 		return -EINVAL;
1195 	}
1196 
1197 	return 0;
1198 }
1199 
1200 static int bmi323_buffer_postenable(struct iio_dev *indio_dev)
1201 {
1202 	struct bmi323_data *data = iio_priv(indio_dev);
1203 
1204 	if (iio_device_get_current_mode(indio_dev) == INDIO_BUFFER_TRIGGERED)
1205 		return 0;
1206 
1207 	return bmi323_fifo_enable(data);
1208 }
1209 
1210 static ssize_t hwfifo_watermark_show(struct device *dev,
1211 				     struct device_attribute *attr, char *buf)
1212 {
1213 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1214 	struct bmi323_data *data = iio_priv(indio_dev);
1215 	int wm;
1216 
1217 	scoped_guard(mutex, &data->mutex)
1218 		wm = data->watermark;
1219 
1220 	return sysfs_emit(buf, "%d\n", wm);
1221 }
1222 static IIO_DEVICE_ATTR_RO(hwfifo_watermark, 0);
1223 
1224 static ssize_t hwfifo_enabled_show(struct device *dev,
1225 				   struct device_attribute *attr,
1226 				   char *buf)
1227 {
1228 	struct iio_dev *indio_dev = dev_to_iio_dev(dev);
1229 	struct bmi323_data *data = iio_priv(indio_dev);
1230 	bool state;
1231 
1232 	scoped_guard(mutex, &data->mutex)
1233 		state = data->state == BMI323_BUFFER_FIFO;
1234 
1235 	return sysfs_emit(buf, "%d\n", state);
1236 }
1237 static IIO_DEVICE_ATTR_RO(hwfifo_enabled, 0);
1238 
1239 static const struct iio_dev_attr *bmi323_fifo_attributes[] = {
1240 	&iio_dev_attr_hwfifo_watermark,
1241 	&iio_dev_attr_hwfifo_enabled,
1242 	NULL
1243 };
1244 
1245 static const struct iio_buffer_setup_ops bmi323_buffer_ops = {
1246 	.preenable = bmi323_buffer_preenable,
1247 	.postenable = bmi323_buffer_postenable,
1248 	.predisable = bmi323_buffer_predisable,
1249 };
1250 
1251 static irqreturn_t bmi323_irq_thread_handler(int irq, void *private)
1252 {
1253 	struct iio_dev *indio_dev = private;
1254 	struct bmi323_data *data = iio_priv(indio_dev);
1255 	unsigned int status_addr, status, feature_event;
1256 	s64 timestamp = iio_get_time_ns(indio_dev);
1257 	int ret;
1258 
1259 	if (data->irq_pin == BMI323_IRQ_INT1)
1260 		status_addr = BMI323_STATUS_INT1_REG;
1261 	else
1262 		status_addr = BMI323_STATUS_INT2_REG;
1263 
1264 	scoped_guard(mutex, &data->mutex) {
1265 		ret = regmap_read(data->regmap, status_addr, &status);
1266 		if (ret)
1267 			return IRQ_NONE;
1268 	}
1269 
1270 	if (!status || FIELD_GET(BMI323_STATUS_ERROR_MSK, status))
1271 		return IRQ_NONE;
1272 
1273 	if (FIELD_GET(BMI323_STATUS_FIFO_WTRMRK_MSK, status)) {
1274 		data->old_fifo_tstamp = data->fifo_tstamp;
1275 		data->fifo_tstamp = iio_get_time_ns(indio_dev);
1276 		ret = __bmi323_fifo_flush(indio_dev);
1277 		if (ret < 0)
1278 			return IRQ_NONE;
1279 	}
1280 
1281 	if (FIELD_GET(BMI323_STATUS_ACC_GYR_DRDY_MSK, status))
1282 		iio_trigger_poll_nested(data->trig);
1283 
1284 	if (FIELD_GET(BMI323_STATUS_MOTION_MSK, status))
1285 		iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
1286 							     IIO_MOD_X_OR_Y_OR_Z,
1287 							     IIO_EV_TYPE_MAG,
1288 							     IIO_EV_DIR_RISING),
1289 			       timestamp);
1290 
1291 	if (FIELD_GET(BMI323_STATUS_NOMOTION_MSK, status))
1292 		iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
1293 							     IIO_MOD_X_OR_Y_OR_Z,
1294 							     IIO_EV_TYPE_MAG,
1295 							     IIO_EV_DIR_FALLING),
1296 			       timestamp);
1297 
1298 	if (FIELD_GET(BMI323_STATUS_STP_WTR_MSK, status))
1299 		iio_push_event(indio_dev, IIO_MOD_EVENT_CODE(IIO_STEPS, 0,
1300 							     IIO_NO_MOD,
1301 							     IIO_EV_TYPE_CHANGE,
1302 							     IIO_EV_DIR_NONE),
1303 			       timestamp);
1304 
1305 	if (FIELD_GET(BMI323_STATUS_TAP_MSK, status)) {
1306 		scoped_guard(mutex, &data->mutex) {
1307 			ret = regmap_read(data->regmap,
1308 					  BMI323_FEAT_EVNT_EXT_REG,
1309 					  &feature_event);
1310 			if (ret)
1311 				return IRQ_NONE;
1312 		}
1313 
1314 		if (FIELD_GET(BMI323_FEAT_EVNT_EXT_S_MSK, feature_event)) {
1315 			iio_push_event(indio_dev,
1316 				       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
1317 							  IIO_MOD_X_OR_Y_OR_Z,
1318 							  IIO_EV_TYPE_GESTURE,
1319 							  IIO_EV_DIR_SINGLETAP),
1320 				       timestamp);
1321 		}
1322 
1323 		if (FIELD_GET(BMI323_FEAT_EVNT_EXT_D_MSK, feature_event))
1324 			iio_push_event(indio_dev,
1325 				       IIO_MOD_EVENT_CODE(IIO_ACCEL, 0,
1326 							  IIO_MOD_X_OR_Y_OR_Z,
1327 							  IIO_EV_TYPE_GESTURE,
1328 							  IIO_EV_DIR_DOUBLETAP),
1329 				       timestamp);
1330 	}
1331 
1332 	return IRQ_HANDLED;
1333 }
1334 
1335 static int bmi323_set_drdy_irq(struct bmi323_data *data,
1336 			       enum bmi323_irq_pin irq_pin)
1337 {
1338 	int ret;
1339 
1340 	ret = regmap_update_bits(data->regmap, BMI323_INT_MAP2_REG,
1341 				 BMI323_GYR_DRDY_MSK,
1342 				 FIELD_PREP(BMI323_GYR_DRDY_MSK, irq_pin));
1343 	if (ret)
1344 		return ret;
1345 
1346 	return regmap_update_bits(data->regmap, BMI323_INT_MAP2_REG,
1347 				  BMI323_ACC_DRDY_MSK,
1348 				  FIELD_PREP(BMI323_ACC_DRDY_MSK, irq_pin));
1349 }
1350 
1351 static int bmi323_data_rdy_trigger_set_state(struct iio_trigger *trig,
1352 					     bool state)
1353 {
1354 	struct bmi323_data *data = iio_trigger_get_drvdata(trig);
1355 	enum bmi323_irq_pin irq_pin;
1356 
1357 	guard(mutex)(&data->mutex);
1358 
1359 	if (data->state == BMI323_BUFFER_FIFO) {
1360 		dev_warn(data->dev, "Can't set trigger when FIFO enabled\n");
1361 		return -EBUSY;
1362 	}
1363 
1364 	if (state) {
1365 		data->state = BMI323_BUFFER_DRDY_TRIGGERED;
1366 		irq_pin = data->irq_pin;
1367 	} else {
1368 		data->state = BMI323_IDLE;
1369 		irq_pin = BMI323_IRQ_DISABLED;
1370 	}
1371 
1372 	return bmi323_set_drdy_irq(data, irq_pin);
1373 }
1374 
1375 static const struct iio_trigger_ops bmi323_trigger_ops = {
1376 	.set_trigger_state = &bmi323_data_rdy_trigger_set_state,
1377 };
1378 
1379 static irqreturn_t bmi323_trigger_handler(int irq, void *p)
1380 {
1381 	struct iio_poll_func *pf = p;
1382 	struct iio_dev *indio_dev = pf->indio_dev;
1383 	struct bmi323_data *data = iio_priv(indio_dev);
1384 	int ret, bit, index = 0;
1385 
1386 	/* Lock to protect the data->buffer */
1387 	guard(mutex)(&data->mutex);
1388 
1389 	if (*indio_dev->active_scan_mask == BMI323_ALL_CHAN_MSK) {
1390 		ret = regmap_bulk_read(data->regmap, BMI323_ACCEL_X_REG,
1391 				       &data->buffer.channels,
1392 				       ARRAY_SIZE(data->buffer.channels));
1393 		if (ret)
1394 			return IRQ_NONE;
1395 	} else {
1396 		for_each_set_bit(bit, indio_dev->active_scan_mask,
1397 				 BMI323_CHAN_MAX) {
1398 			ret = regmap_raw_read(data->regmap,
1399 					      BMI323_ACCEL_X_REG + bit,
1400 					      &data->buffer.channels[index++],
1401 					      BMI323_BYTES_PER_SAMPLE);
1402 			if (ret)
1403 				return IRQ_NONE;
1404 		}
1405 	}
1406 
1407 	iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer,
1408 					   iio_get_time_ns(indio_dev));
1409 
1410 	iio_trigger_notify_done(indio_dev->trig);
1411 
1412 	return IRQ_HANDLED;
1413 }
1414 
1415 static int bmi323_set_average(struct bmi323_data *data,
1416 			      enum bmi323_sensor_type sensor, int avg)
1417 {
1418 	int raw = ARRAY_SIZE(bmi323_accel_gyro_avrg);
1419 
1420 	while (raw--)
1421 		if (avg == bmi323_accel_gyro_avrg[raw])
1422 			break;
1423 	if (raw < 0)
1424 		return -EINVAL;
1425 
1426 	guard(mutex)(&data->mutex);
1427 	return regmap_update_bits(data->regmap, bmi323_hw[sensor].config,
1428 				 BMI323_ACC_GYRO_CONF_AVG_MSK,
1429 				 FIELD_PREP(BMI323_ACC_GYRO_CONF_AVG_MSK,
1430 					    raw));
1431 }
1432 
1433 static int bmi323_get_average(struct bmi323_data *data,
1434 			      enum bmi323_sensor_type sensor, int *avg)
1435 {
1436 	int ret, value, raw;
1437 
1438 	scoped_guard(mutex, &data->mutex) {
1439 		ret = regmap_read(data->regmap, bmi323_hw[sensor].config, &value);
1440 		if (ret)
1441 			return ret;
1442 	}
1443 
1444 	raw = FIELD_GET(BMI323_ACC_GYRO_CONF_AVG_MSK, value);
1445 	*avg = bmi323_accel_gyro_avrg[raw];
1446 
1447 	return IIO_VAL_INT;
1448 }
1449 
1450 static int bmi323_enable_steps(struct bmi323_data *data, int val)
1451 {
1452 	int ret;
1453 
1454 	guard(mutex)(&data->mutex);
1455 	if (data->odrhz[BMI323_ACCEL] < 200) {
1456 		dev_err(data->dev, "Invalid accelerometer parameter\n");
1457 		return -EINVAL;
1458 	}
1459 
1460 	ret = bmi323_feature_engine_events(data, BMI323_FEAT_IO0_STP_CNT_MSK,
1461 					   val ? 1 : 0);
1462 	if (ret)
1463 		return ret;
1464 
1465 	set_mask_bits(&data->feature_events, BMI323_FEAT_IO0_STP_CNT_MSK,
1466 		      FIELD_PREP(BMI323_FEAT_IO0_STP_CNT_MSK, val ? 1 : 0));
1467 
1468 	return 0;
1469 }
1470 
1471 static int bmi323_read_steps(struct bmi323_data *data, int *val)
1472 {
1473 	int ret;
1474 
1475 	guard(mutex)(&data->mutex);
1476 	if (!FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK, data->feature_events))
1477 		return -EINVAL;
1478 
1479 	ret = regmap_bulk_read(data->regmap, BMI323_FEAT_IO2_REG,
1480 			       data->steps_count,
1481 			       ARRAY_SIZE(data->steps_count));
1482 	if (ret)
1483 		return ret;
1484 
1485 	*val = get_unaligned_le32(data->steps_count);
1486 
1487 	return IIO_VAL_INT;
1488 }
1489 
1490 static int bmi323_read_axis(struct bmi323_data *data,
1491 			    struct iio_chan_spec const *chan, int *val)
1492 {
1493 	enum bmi323_sensor_type sensor;
1494 	unsigned int value;
1495 	u8 addr;
1496 	int ret;
1497 
1498 	ret = bmi323_get_error_status(data);
1499 	if (ret)
1500 		return -EINVAL;
1501 
1502 	sensor = bmi323_iio_to_sensor(chan->type);
1503 	addr = bmi323_hw[sensor].data + (chan->channel2 - IIO_MOD_X);
1504 
1505 	scoped_guard(mutex, &data->mutex) {
1506 		ret = regmap_read(data->regmap, addr, &value);
1507 		if (ret)
1508 			return ret;
1509 	}
1510 
1511 	*val = sign_extend32(value, chan->scan_type.realbits - 1);
1512 
1513 	return IIO_VAL_INT;
1514 }
1515 
1516 static int bmi323_get_temp_data(struct bmi323_data *data, int *val)
1517 {
1518 	unsigned int value;
1519 	int ret;
1520 
1521 	ret = bmi323_get_error_status(data);
1522 	if (ret)
1523 		return -EINVAL;
1524 
1525 	scoped_guard(mutex, &data->mutex) {
1526 		ret = regmap_read(data->regmap, BMI323_TEMP_REG, &value);
1527 		if (ret)
1528 			return ret;
1529 	}
1530 
1531 	*val = sign_extend32(value, 15);
1532 
1533 	return IIO_VAL_INT;
1534 }
1535 
1536 static int bmi323_get_odr(struct bmi323_data *data,
1537 			  enum bmi323_sensor_type sensor, int *odr, int *uodr)
1538 {
1539 	int ret, value, odr_raw;
1540 
1541 	scoped_guard(mutex, &data->mutex) {
1542 		ret = regmap_read(data->regmap, bmi323_hw[sensor].config, &value);
1543 		if (ret)
1544 			return ret;
1545 	}
1546 
1547 	odr_raw = FIELD_GET(BMI323_ACC_GYRO_CONF_ODR_MSK, value);
1548 	*odr = bmi323_acc_gyro_odr[odr_raw - 1][0];
1549 	*uodr = bmi323_acc_gyro_odr[odr_raw - 1][1];
1550 
1551 	return IIO_VAL_INT_PLUS_MICRO;
1552 }
1553 
1554 static int bmi323_configure_power_mode(struct bmi323_data *data,
1555 				       enum bmi323_sensor_type sensor,
1556 				       int odr_index)
1557 {
1558 	enum bmi323_opr_mode mode;
1559 
1560 	if (bmi323_acc_gyro_odr[odr_index][0] > 25)
1561 		mode = ACC_GYRO_MODE_CONTINOUS;
1562 	else
1563 		mode = ACC_GYRO_MODE_DUTYCYCLE;
1564 
1565 	return bmi323_set_mode(data, sensor, mode);
1566 }
1567 
1568 static int bmi323_set_odr(struct bmi323_data *data,
1569 			  enum bmi323_sensor_type sensor, int odr, int uodr)
1570 {
1571 	int odr_raw, ret;
1572 
1573 	odr_raw = ARRAY_SIZE(bmi323_acc_gyro_odr);
1574 
1575 	while (odr_raw--)
1576 		if (odr == bmi323_acc_gyro_odr[odr_raw][0] &&
1577 		    uodr == bmi323_acc_gyro_odr[odr_raw][1])
1578 			break;
1579 	if (odr_raw < 0)
1580 		return -EINVAL;
1581 
1582 	ret = bmi323_configure_power_mode(data, sensor, odr_raw);
1583 	if (ret)
1584 		return -EINVAL;
1585 
1586 	guard(mutex)(&data->mutex);
1587 	data->odrhz[sensor] = bmi323_acc_gyro_odr[odr_raw][0];
1588 	data->odrns[sensor] = bmi323_acc_gyro_odrns[odr_raw];
1589 
1590 	odr_raw++;
1591 
1592 	return regmap_update_bits(data->regmap, bmi323_hw[sensor].config,
1593 				  BMI323_ACC_GYRO_CONF_ODR_MSK,
1594 				  FIELD_PREP(BMI323_ACC_GYRO_CONF_ODR_MSK,
1595 					     odr_raw));
1596 }
1597 
1598 static int bmi323_get_scale(struct bmi323_data *data,
1599 			    enum bmi323_sensor_type sensor, int *val2)
1600 {
1601 	int ret, value, scale_raw;
1602 
1603 	scoped_guard(mutex, &data->mutex) {
1604 		ret = regmap_read(data->regmap, bmi323_hw[sensor].config,
1605 				  &value);
1606 		if (ret)
1607 			return ret;
1608 	}
1609 
1610 	scale_raw = FIELD_GET(BMI323_ACC_GYRO_CONF_SCL_MSK, value);
1611 	*val2 = bmi323_hw[sensor].scale_table[scale_raw][1];
1612 
1613 	return IIO_VAL_INT_PLUS_MICRO;
1614 }
1615 
1616 static int bmi323_set_scale(struct bmi323_data *data,
1617 			    enum bmi323_sensor_type sensor, int val, int val2)
1618 {
1619 	int scale_raw;
1620 
1621 	scale_raw = bmi323_hw[sensor].scale_table_len;
1622 
1623 	while (scale_raw--)
1624 		if (val == bmi323_hw[sensor].scale_table[scale_raw][0] &&
1625 		    val2 == bmi323_hw[sensor].scale_table[scale_raw][1])
1626 			break;
1627 	if (scale_raw < 0)
1628 		return -EINVAL;
1629 
1630 	guard(mutex)(&data->mutex);
1631 	return regmap_update_bits(data->regmap, bmi323_hw[sensor].config,
1632 				  BMI323_ACC_GYRO_CONF_SCL_MSK,
1633 				  FIELD_PREP(BMI323_ACC_GYRO_CONF_SCL_MSK,
1634 					     scale_raw));
1635 }
1636 
1637 static int bmi323_read_avail(struct iio_dev *indio_dev,
1638 			     struct iio_chan_spec const *chan,
1639 			     const int **vals, int *type, int *length,
1640 			     long mask)
1641 {
1642 	enum bmi323_sensor_type sensor;
1643 
1644 	switch (mask) {
1645 	case IIO_CHAN_INFO_SAMP_FREQ:
1646 		*type = IIO_VAL_INT_PLUS_MICRO;
1647 		*vals = (const int *)bmi323_acc_gyro_odr;
1648 		*length = ARRAY_SIZE(bmi323_acc_gyro_odr) * 2;
1649 		return IIO_AVAIL_LIST;
1650 	case IIO_CHAN_INFO_SCALE:
1651 		sensor = bmi323_iio_to_sensor(chan->type);
1652 		*type = IIO_VAL_INT_PLUS_MICRO;
1653 		*vals = (const int *)bmi323_hw[sensor].scale_table;
1654 		*length = bmi323_hw[sensor].scale_table_len * 2;
1655 		return IIO_AVAIL_LIST;
1656 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1657 		*type = IIO_VAL_INT;
1658 		*vals = (const int *)bmi323_accel_gyro_avrg;
1659 		*length = ARRAY_SIZE(bmi323_accel_gyro_avrg);
1660 		return IIO_AVAIL_LIST;
1661 	default:
1662 		return -EINVAL;
1663 	}
1664 }
1665 
1666 static int bmi323_write_raw(struct iio_dev *indio_dev,
1667 			    struct iio_chan_spec const *chan, int val,
1668 			    int val2, long mask)
1669 {
1670 	struct bmi323_data *data = iio_priv(indio_dev);
1671 
1672 	switch (mask) {
1673 	case IIO_CHAN_INFO_SAMP_FREQ:
1674 		iio_device_claim_direct_scoped(return -EBUSY, indio_dev)
1675 			return bmi323_set_odr(data,
1676 					      bmi323_iio_to_sensor(chan->type),
1677 					      val, val2);
1678 		unreachable();
1679 	case IIO_CHAN_INFO_SCALE:
1680 		iio_device_claim_direct_scoped(return -EBUSY, indio_dev)
1681 			return bmi323_set_scale(data,
1682 						bmi323_iio_to_sensor(chan->type),
1683 						val, val2);
1684 		unreachable();
1685 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1686 		iio_device_claim_direct_scoped(return -EBUSY, indio_dev)
1687 			return bmi323_set_average(data,
1688 						  bmi323_iio_to_sensor(chan->type),
1689 						  val);
1690 		unreachable();
1691 	case IIO_CHAN_INFO_ENABLE:
1692 		return bmi323_enable_steps(data, val);
1693 	case IIO_CHAN_INFO_PROCESSED: {
1694 		guard(mutex)(&data->mutex);
1695 
1696 		if (val || !FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK,
1697 				      data->feature_events))
1698 			return -EINVAL;
1699 
1700 		/* Clear step counter value */
1701 		return bmi323_update_ext_reg(data, BMI323_STEP_SC1_REG,
1702 					     BMI323_STEP_SC1_RST_CNT_MSK,
1703 					     FIELD_PREP(BMI323_STEP_SC1_RST_CNT_MSK,
1704 							1));
1705 	}
1706 	default:
1707 		return -EINVAL;
1708 	}
1709 }
1710 
1711 static int bmi323_read_raw(struct iio_dev *indio_dev,
1712 			   struct iio_chan_spec const *chan, int *val,
1713 			   int *val2, long mask)
1714 {
1715 	struct bmi323_data *data = iio_priv(indio_dev);
1716 
1717 	switch (mask) {
1718 	case IIO_CHAN_INFO_PROCESSED:
1719 		return bmi323_read_steps(data, val);
1720 	case IIO_CHAN_INFO_RAW:
1721 		switch (chan->type) {
1722 		case IIO_ACCEL:
1723 		case IIO_ANGL_VEL:
1724 			iio_device_claim_direct_scoped(return -EBUSY,
1725 						       indio_dev)
1726 				return bmi323_read_axis(data, chan, val);
1727 			unreachable();
1728 		case IIO_TEMP:
1729 			return bmi323_get_temp_data(data, val);
1730 		default:
1731 			return -EINVAL;
1732 		}
1733 	case IIO_CHAN_INFO_SAMP_FREQ:
1734 		return bmi323_get_odr(data, bmi323_iio_to_sensor(chan->type),
1735 				      val, val2);
1736 	case IIO_CHAN_INFO_SCALE:
1737 		switch (chan->type) {
1738 		case IIO_ACCEL:
1739 		case IIO_ANGL_VEL:
1740 			*val = 0;
1741 			return bmi323_get_scale(data,
1742 						bmi323_iio_to_sensor(chan->type),
1743 						val2);
1744 		case IIO_TEMP:
1745 			*val = BMI323_TEMP_SCALE / MEGA;
1746 			*val2 = BMI323_TEMP_SCALE % MEGA;
1747 			return IIO_VAL_INT_PLUS_MICRO;
1748 		default:
1749 			return -EINVAL;
1750 		}
1751 	case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
1752 		return bmi323_get_average(data,
1753 					  bmi323_iio_to_sensor(chan->type),
1754 					  val);
1755 	case IIO_CHAN_INFO_OFFSET:
1756 		switch (chan->type) {
1757 		case IIO_TEMP:
1758 			*val = BMI323_TEMP_OFFSET;
1759 			return IIO_VAL_INT;
1760 		default:
1761 			return -EINVAL;
1762 		}
1763 	case IIO_CHAN_INFO_ENABLE:
1764 		scoped_guard(mutex, &data->mutex)
1765 			*val = FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK,
1766 					 data->feature_events);
1767 		return IIO_VAL_INT;
1768 	default:
1769 		return -EINVAL;
1770 	}
1771 }
1772 
1773 static const struct iio_info bmi323_info = {
1774 	.read_raw = bmi323_read_raw,
1775 	.write_raw = bmi323_write_raw,
1776 	.read_avail = bmi323_read_avail,
1777 	.hwfifo_set_watermark = bmi323_set_watermark,
1778 	.write_event_config = bmi323_write_event_config,
1779 	.read_event_config = bmi323_read_event_config,
1780 	.write_event_value = bmi323_write_event_value,
1781 	.read_event_value = bmi323_read_event_value,
1782 	.event_attrs = &bmi323_event_attribute_group,
1783 };
1784 
1785 #define BMI323_SCAN_MASK_ACCEL_3AXIS		\
1786 	(BIT(BMI323_ACCEL_X) | BIT(BMI323_ACCEL_Y) | BIT(BMI323_ACCEL_Z))
1787 
1788 #define BMI323_SCAN_MASK_GYRO_3AXIS		\
1789 	(BIT(BMI323_GYRO_X) | BIT(BMI323_GYRO_Y) | BIT(BMI323_GYRO_Z))
1790 
1791 static const unsigned long bmi323_avail_scan_masks[] = {
1792 	/* 3-axis accel */
1793 	BMI323_SCAN_MASK_ACCEL_3AXIS,
1794 	/* 3-axis gyro */
1795 	BMI323_SCAN_MASK_GYRO_3AXIS,
1796 	/* 3-axis accel + 3-axis gyro */
1797 	BMI323_SCAN_MASK_ACCEL_3AXIS | BMI323_SCAN_MASK_GYRO_3AXIS,
1798 	0
1799 };
1800 
1801 static int bmi323_int_pin_config(struct bmi323_data *data,
1802 				 enum bmi323_irq_pin irq_pin,
1803 				 bool active_high, bool open_drain, bool latch)
1804 {
1805 	unsigned int mask, field_value;
1806 	int ret;
1807 
1808 	ret = regmap_update_bits(data->regmap, BMI323_IO_INT_CONF_REG,
1809 				 BMI323_IO_INT_LTCH_MSK,
1810 				 FIELD_PREP(BMI323_IO_INT_LTCH_MSK, latch));
1811 	if (ret)
1812 		return ret;
1813 
1814 	ret = bmi323_update_ext_reg(data, BMI323_GEN_SET1_REG,
1815 				    BMI323_GEN_HOLD_DUR_MSK,
1816 				    FIELD_PREP(BMI323_GEN_HOLD_DUR_MSK, 0));
1817 	if (ret)
1818 		return ret;
1819 
1820 	switch (irq_pin) {
1821 	case BMI323_IRQ_INT1:
1822 		mask = BMI323_IO_INT1_LVL_OD_OP_MSK;
1823 
1824 		field_value = FIELD_PREP(BMI323_IO_INT1_LVL_MSK, active_high) |
1825 			      FIELD_PREP(BMI323_IO_INT1_OD_MSK, open_drain) |
1826 			      FIELD_PREP(BMI323_IO_INT1_OP_EN_MSK, 1);
1827 		break;
1828 	case BMI323_IRQ_INT2:
1829 		mask = BMI323_IO_INT2_LVL_OD_OP_MSK;
1830 
1831 		field_value = FIELD_PREP(BMI323_IO_INT2_LVL_MSK, active_high) |
1832 			      FIELD_PREP(BMI323_IO_INT2_OD_MSK, open_drain) |
1833 			      FIELD_PREP(BMI323_IO_INT2_OP_EN_MSK, 1);
1834 		break;
1835 	default:
1836 		return -EINVAL;
1837 	}
1838 
1839 	return regmap_update_bits(data->regmap, BMI323_IO_INT_CTR_REG, mask,
1840 				  field_value);
1841 }
1842 
1843 static int bmi323_trigger_probe(struct bmi323_data *data,
1844 				struct iio_dev *indio_dev)
1845 {
1846 	bool open_drain, active_high, latch;
1847 	struct fwnode_handle *fwnode;
1848 	enum bmi323_irq_pin irq_pin;
1849 	int ret, irq, irq_type;
1850 	struct irq_data *desc;
1851 
1852 	fwnode = dev_fwnode(data->dev);
1853 	if (!fwnode)
1854 		return -ENODEV;
1855 
1856 	irq = fwnode_irq_get_byname(fwnode, "INT1");
1857 	if (irq > 0) {
1858 		irq_pin = BMI323_IRQ_INT1;
1859 	} else {
1860 		irq = fwnode_irq_get_byname(fwnode, "INT2");
1861 		if (irq < 0)
1862 			return 0;
1863 
1864 		irq_pin = BMI323_IRQ_INT2;
1865 	}
1866 
1867 	desc = irq_get_irq_data(irq);
1868 	if (!desc)
1869 		return dev_err_probe(data->dev, -EINVAL,
1870 				     "Could not find IRQ %d\n", irq);
1871 
1872 	irq_type = irqd_get_trigger_type(desc);
1873 	switch (irq_type) {
1874 	case IRQF_TRIGGER_RISING:
1875 		latch = false;
1876 		active_high = true;
1877 		break;
1878 	case IRQF_TRIGGER_HIGH:
1879 		latch = true;
1880 		active_high = true;
1881 		break;
1882 	case IRQF_TRIGGER_FALLING:
1883 		latch = false;
1884 		active_high = false;
1885 		break;
1886 	case IRQF_TRIGGER_LOW:
1887 		latch = true;
1888 		active_high = false;
1889 		break;
1890 	default:
1891 		return dev_err_probe(data->dev, -EINVAL,
1892 				     "Invalid interrupt type 0x%x specified\n",
1893 				     irq_type);
1894 	}
1895 
1896 	open_drain = fwnode_property_read_bool(fwnode, "drive-open-drain");
1897 
1898 	ret = bmi323_int_pin_config(data, irq_pin, active_high, open_drain,
1899 				    latch);
1900 	if (ret)
1901 		return dev_err_probe(data->dev, ret,
1902 				     "Failed to configure irq line\n");
1903 
1904 	data->trig = devm_iio_trigger_alloc(data->dev, "%s-trig-%d",
1905 					    indio_dev->name, irq_pin);
1906 	if (!data->trig)
1907 		return -ENOMEM;
1908 
1909 	data->trig->ops = &bmi323_trigger_ops;
1910 	iio_trigger_set_drvdata(data->trig, data);
1911 
1912 	ret = devm_request_threaded_irq(data->dev, irq, NULL,
1913 					bmi323_irq_thread_handler,
1914 					IRQF_ONESHOT, "bmi323-int", indio_dev);
1915 	if (ret)
1916 		return dev_err_probe(data->dev, ret, "Failed to request IRQ\n");
1917 
1918 	ret = devm_iio_trigger_register(data->dev, data->trig);
1919 	if (ret)
1920 		return dev_err_probe(data->dev, ret,
1921 				     "Trigger registration failed\n");
1922 
1923 	data->irq_pin = irq_pin;
1924 
1925 	return 0;
1926 }
1927 
1928 static int bmi323_feature_engine_enable(struct bmi323_data *data, bool en)
1929 {
1930 	unsigned int feature_status;
1931 	int ret;
1932 
1933 	if (!en)
1934 		return regmap_write(data->regmap, BMI323_FEAT_CTRL_REG, 0);
1935 
1936 	ret = regmap_write(data->regmap, BMI323_FEAT_IO2_REG, 0x012c);
1937 	if (ret)
1938 		return ret;
1939 
1940 	ret = regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG,
1941 			   BMI323_FEAT_IO_STATUS_MSK);
1942 	if (ret)
1943 		return ret;
1944 
1945 	ret = regmap_write(data->regmap, BMI323_FEAT_CTRL_REG,
1946 			   BMI323_FEAT_ENG_EN_MSK);
1947 	if (ret)
1948 		return ret;
1949 
1950 	/*
1951 	 * It takes around 4 msec to enable the Feature engine, so check
1952 	 * the status of the feature engine every 2 msec for a maximum
1953 	 * of 5 trials.
1954 	 */
1955 	ret = regmap_read_poll_timeout(data->regmap, BMI323_FEAT_IO1_REG,
1956 				       feature_status,
1957 				       FIELD_GET(BMI323_FEAT_IO1_ERR_MSK,
1958 						 feature_status) == 1,
1959 				       BMI323_FEAT_ENG_POLL,
1960 				       BMI323_FEAT_ENG_TIMEOUT);
1961 	if (ret)
1962 		return dev_err_probe(data->dev, -EINVAL,
1963 				"Failed to enable feature engine\n");
1964 
1965 	return 0;
1966 }
1967 
1968 static void bmi323_disable(void *data_ptr)
1969 {
1970 	struct bmi323_data *data = data_ptr;
1971 
1972 	bmi323_set_mode(data, BMI323_ACCEL, ACC_GYRO_MODE_DISABLE);
1973 	bmi323_set_mode(data, BMI323_GYRO, ACC_GYRO_MODE_DISABLE);
1974 }
1975 
1976 static int bmi323_set_bw(struct bmi323_data *data,
1977 			 enum bmi323_sensor_type sensor, enum bmi323_3db_bw bw)
1978 {
1979 	return regmap_update_bits(data->regmap, bmi323_hw[sensor].config,
1980 				  BMI323_ACC_GYRO_CONF_BW_MSK,
1981 				  FIELD_PREP(BMI323_ACC_GYRO_CONF_BW_MSK, bw));
1982 }
1983 
1984 static int bmi323_init(struct bmi323_data *data)
1985 {
1986 	int ret, val;
1987 
1988 	/*
1989 	 * Perform soft reset to make sure the device is in a known state after
1990 	 * start up. A delay of 1.5 ms is required after reset.
1991 	 * See datasheet section 5.17 "Soft Reset".
1992 	 */
1993 	ret = regmap_write(data->regmap, BMI323_CMD_REG, BMI323_RST_VAL);
1994 	if (ret)
1995 		return ret;
1996 
1997 	usleep_range(1500, 2000);
1998 
1999 	/*
2000 	 * Dummy read is required to enable SPI interface after reset.
2001 	 * See datasheet section 7.2.1 "Protocol Selection".
2002 	 */
2003 	regmap_read(data->regmap, BMI323_CHIP_ID_REG, &val);
2004 
2005 	ret = regmap_read(data->regmap, BMI323_STATUS_REG, &val);
2006 	if (ret)
2007 		return ret;
2008 
2009 	if (!FIELD_GET(BMI323_STATUS_POR_MSK, val))
2010 		return dev_err_probe(data->dev, -EINVAL,
2011 				     "Sensor initialization error\n");
2012 
2013 	ret = regmap_read(data->regmap, BMI323_CHIP_ID_REG, &val);
2014 	if (ret)
2015 		return ret;
2016 
2017 	if (FIELD_GET(BMI323_CHIP_ID_MSK, val) != BMI323_CHIP_ID_VAL)
2018 		return dev_err_probe(data->dev, -EINVAL, "Chip ID mismatch\n");
2019 
2020 	ret = bmi323_feature_engine_enable(data, true);
2021 	if (ret)
2022 		return ret;
2023 
2024 	ret = regmap_read(data->regmap, BMI323_ERR_REG, &val);
2025 	if (ret)
2026 		return ret;
2027 
2028 	if (val)
2029 		return dev_err_probe(data->dev, -EINVAL,
2030 				     "Sensor power error = 0x%x\n", val);
2031 
2032 	/*
2033 	 * Set the Bandwidth coefficient which defines the 3 dB cutoff
2034 	 * frequency in relation to the ODR.
2035 	 */
2036 	ret = bmi323_set_bw(data, BMI323_ACCEL, BMI323_BW_ODR_BY_2);
2037 	if (ret)
2038 		return ret;
2039 
2040 	ret = bmi323_set_bw(data, BMI323_GYRO, BMI323_BW_ODR_BY_2);
2041 	if (ret)
2042 		return ret;
2043 
2044 	ret = bmi323_set_odr(data, BMI323_ACCEL, 25, 0);
2045 	if (ret)
2046 		return ret;
2047 
2048 	ret = bmi323_set_odr(data, BMI323_GYRO, 25, 0);
2049 	if (ret)
2050 		return ret;
2051 
2052 	return devm_add_action_or_reset(data->dev, bmi323_disable, data);
2053 }
2054 
2055 int bmi323_core_probe(struct device *dev)
2056 {
2057 	static const char * const regulator_names[] = { "vdd", "vddio" };
2058 	struct iio_dev *indio_dev;
2059 	struct bmi323_data *data;
2060 	struct regmap *regmap;
2061 	int ret;
2062 
2063 	regmap = dev_get_regmap(dev, NULL);
2064 	if (!regmap)
2065 		return dev_err_probe(dev, -ENODEV, "Failed to get regmap\n");
2066 
2067 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
2068 	if (!indio_dev)
2069 		return dev_err_probe(dev, -ENOMEM,
2070 				     "Failed to allocate device\n");
2071 
2072 	ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulator_names),
2073 					     regulator_names);
2074 	if (ret)
2075 		return dev_err_probe(dev, ret, "Failed to enable regulators\n");
2076 
2077 	data = iio_priv(indio_dev);
2078 	data->dev = dev;
2079 	data->regmap = regmap;
2080 	mutex_init(&data->mutex);
2081 
2082 	ret = bmi323_init(data);
2083 	if (ret)
2084 		return -EINVAL;
2085 
2086 	ret = iio_read_mount_matrix(dev, &data->orientation);
2087 	if (ret)
2088 		return ret;
2089 
2090 	indio_dev->name = "bmi323-imu";
2091 	indio_dev->info = &bmi323_info;
2092 	indio_dev->channels = bmi323_channels;
2093 	indio_dev->num_channels = ARRAY_SIZE(bmi323_channels);
2094 	indio_dev->available_scan_masks = bmi323_avail_scan_masks;
2095 	indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE;
2096 	dev_set_drvdata(data->dev, indio_dev);
2097 
2098 	ret = bmi323_trigger_probe(data, indio_dev);
2099 	if (ret)
2100 		return -EINVAL;
2101 
2102 	ret = devm_iio_triggered_buffer_setup_ext(data->dev, indio_dev,
2103 						  &iio_pollfunc_store_time,
2104 						  bmi323_trigger_handler,
2105 						  IIO_BUFFER_DIRECTION_IN,
2106 						  &bmi323_buffer_ops,
2107 						  bmi323_fifo_attributes);
2108 	if (ret)
2109 		return dev_err_probe(data->dev, ret,
2110 				     "Failed to setup trigger buffer\n");
2111 
2112 	ret = devm_iio_device_register(data->dev, indio_dev);
2113 	if (ret)
2114 		return dev_err_probe(data->dev, ret,
2115 				     "Unable to register iio device\n");
2116 
2117 	return 0;
2118 }
2119 EXPORT_SYMBOL_NS_GPL(bmi323_core_probe, IIO_BMI323);
2120 
2121 MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
2122 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
2123 MODULE_LICENSE("GPL");
2124