1 /*
2  * STMicroelectronics st_lsm6dsx FIFO buffer library driver
3  *
4  * LSM6DS3/LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC: The FIFO buffer can be
5  * configured to store data from gyroscope and accelerometer. Samples are
6  * queued without any tag according to a specific pattern based on
7  * 'FIFO data sets' (6 bytes each):
8  *  - 1st data set is reserved for gyroscope data
9  *  - 2nd data set is reserved for accelerometer data
10  * The FIFO pattern changes depending on the ODRs and decimation factors
11  * assigned to the FIFO data sets. The first sequence of data stored in FIFO
12  * buffer contains the data of all the enabled FIFO data sets
13  * (e.g. Gx, Gy, Gz, Ax, Ay, Az), then data are repeated depending on the
14  * value of the decimation factor and ODR set for each FIFO data set.
15  * FIFO supported modes:
16  *  - BYPASS: FIFO disabled
17  *  - CONTINUOUS: FIFO enabled. When the buffer is full, the FIFO index
18  *    restarts from the beginning and the oldest sample is overwritten
19  *
20  * Copyright 2016 STMicroelectronics Inc.
21  *
22  * Lorenzo Bianconi <lorenzo.bianconi@st.com>
23  * Denis Ciocca <denis.ciocca@st.com>
24  *
25  * Licensed under the GPL-2.
26  */
27 #include <linux/module.h>
28 #include <linux/interrupt.h>
29 #include <linux/irq.h>
30 #include <linux/iio/kfifo_buf.h>
31 #include <linux/iio/iio.h>
32 #include <linux/iio/buffer.h>
33 #include <linux/regmap.h>
34 #include <linux/bitfield.h>
35 
36 #include <linux/platform_data/st_sensors_pdata.h>
37 
38 #include "st_lsm6dsx.h"
39 
40 #define ST_LSM6DSX_REG_HLACTIVE_ADDR		0x12
41 #define ST_LSM6DSX_REG_HLACTIVE_MASK		BIT(5)
42 #define ST_LSM6DSX_REG_PP_OD_ADDR		0x12
43 #define ST_LSM6DSX_REG_PP_OD_MASK		BIT(4)
44 #define ST_LSM6DSX_REG_FIFO_MODE_ADDR		0x0a
45 #define ST_LSM6DSX_FIFO_MODE_MASK		GENMASK(2, 0)
46 #define ST_LSM6DSX_FIFO_ODR_MASK		GENMASK(6, 3)
47 #define ST_LSM6DSX_FIFO_EMPTY_MASK		BIT(12)
48 #define ST_LSM6DSX_REG_FIFO_OUTL_ADDR		0x3e
49 #define ST_LSM6DSX_REG_TS_RESET_ADDR		0x42
50 
51 #define ST_LSM6DSX_MAX_FIFO_ODR_VAL		0x08
52 
53 #define ST_LSM6DSX_TS_SENSITIVITY		25000UL /* 25us */
54 #define ST_LSM6DSX_TS_RESET_VAL			0xaa
55 
56 struct st_lsm6dsx_decimator_entry {
57 	u8 decimator;
58 	u8 val;
59 };
60 
61 static const
62 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = {
63 	{  0, 0x0 },
64 	{  1, 0x1 },
65 	{  2, 0x2 },
66 	{  3, 0x3 },
67 	{  4, 0x4 },
68 	{  8, 0x5 },
69 	{ 16, 0x6 },
70 	{ 32, 0x7 },
71 };
72 
st_lsm6dsx_get_decimator_val(u8 val)73 static int st_lsm6dsx_get_decimator_val(u8 val)
74 {
75 	const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table);
76 	int i;
77 
78 	for (i = 0; i < max_size; i++)
79 		if (st_lsm6dsx_decimator_table[i].decimator == val)
80 			break;
81 
82 	return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val;
83 }
84 
st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw * hw,u16 * max_odr,u16 * min_odr)85 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw,
86 				       u16 *max_odr, u16 *min_odr)
87 {
88 	struct st_lsm6dsx_sensor *sensor;
89 	int i;
90 
91 	*max_odr = 0, *min_odr = ~0;
92 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
93 		sensor = iio_priv(hw->iio_devs[i]);
94 
95 		if (!(hw->enable_mask & BIT(sensor->id)))
96 			continue;
97 
98 		*max_odr = max_t(u16, *max_odr, sensor->odr);
99 		*min_odr = min_t(u16, *min_odr, sensor->odr);
100 	}
101 }
102 
st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw * hw)103 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw)
104 {
105 	u16 max_odr, min_odr, sip = 0, ts_sip = 0;
106 	const struct st_lsm6dsx_reg *ts_dec_reg;
107 	struct st_lsm6dsx_sensor *sensor;
108 	int err = 0, i;
109 	u8 data;
110 
111 	st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr);
112 
113 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
114 		const struct st_lsm6dsx_reg *dec_reg;
115 
116 		sensor = iio_priv(hw->iio_devs[i]);
117 		/* update fifo decimators and sample in pattern */
118 		if (hw->enable_mask & BIT(sensor->id)) {
119 			sensor->sip = sensor->odr / min_odr;
120 			sensor->decimator = max_odr / sensor->odr;
121 			data = st_lsm6dsx_get_decimator_val(sensor->decimator);
122 		} else {
123 			sensor->sip = 0;
124 			sensor->decimator = 0;
125 			data = 0;
126 		}
127 		ts_sip = max_t(u16, ts_sip, sensor->sip);
128 
129 		dec_reg = &hw->settings->decimator[sensor->id];
130 		if (dec_reg->addr) {
131 			int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask);
132 
133 			err = regmap_update_bits(hw->regmap, dec_reg->addr,
134 						 dec_reg->mask, val);
135 			if (err < 0)
136 				return err;
137 		}
138 		sip += sensor->sip;
139 	}
140 	hw->sip = sip + ts_sip;
141 	hw->ts_sip = ts_sip;
142 
143 	/*
144 	 * update hw ts decimator if necessary. Decimator for hw timestamp
145 	 * is always 1 or 0 in order to have a ts sample for each data
146 	 * sample in FIFO
147 	 */
148 	ts_dec_reg = &hw->settings->ts_settings.decimator;
149 	if (ts_dec_reg->addr) {
150 		int val, ts_dec = !!hw->ts_sip;
151 
152 		val = ST_LSM6DSX_SHIFT_VAL(ts_dec, ts_dec_reg->mask);
153 		err = regmap_update_bits(hw->regmap, ts_dec_reg->addr,
154 					 ts_dec_reg->mask, val);
155 	}
156 	return err;
157 }
158 
st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw * hw,enum st_lsm6dsx_fifo_mode fifo_mode)159 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw,
160 			     enum st_lsm6dsx_fifo_mode fifo_mode)
161 {
162 	int err;
163 
164 	err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
165 				 ST_LSM6DSX_FIFO_MODE_MASK,
166 				 FIELD_PREP(ST_LSM6DSX_FIFO_MODE_MASK,
167 					    fifo_mode));
168 	if (err < 0)
169 		return err;
170 
171 	hw->fifo_mode = fifo_mode;
172 
173 	return 0;
174 }
175 
st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor * sensor,bool enable)176 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor,
177 				   bool enable)
178 {
179 	struct st_lsm6dsx_hw *hw = sensor->hw;
180 	u8 data;
181 
182 	data = hw->enable_mask ? ST_LSM6DSX_MAX_FIFO_ODR_VAL : 0;
183 	return regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_FIFO_MODE_ADDR,
184 				 ST_LSM6DSX_FIFO_ODR_MASK,
185 				 FIELD_PREP(ST_LSM6DSX_FIFO_ODR_MASK, data));
186 }
187 
st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor * sensor,u16 watermark)188 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark)
189 {
190 	u16 fifo_watermark = ~0, cur_watermark, fifo_th_mask;
191 	struct st_lsm6dsx_hw *hw = sensor->hw;
192 	struct st_lsm6dsx_sensor *cur_sensor;
193 	int i, err, data;
194 	__le16 wdata;
195 
196 	if (!hw->sip)
197 		return 0;
198 
199 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
200 		cur_sensor = iio_priv(hw->iio_devs[i]);
201 
202 		if (!(hw->enable_mask & BIT(cur_sensor->id)))
203 			continue;
204 
205 		cur_watermark = (cur_sensor == sensor) ? watermark
206 						       : cur_sensor->watermark;
207 
208 		fifo_watermark = min_t(u16, fifo_watermark, cur_watermark);
209 	}
210 
211 	fifo_watermark = max_t(u16, fifo_watermark, hw->sip);
212 	fifo_watermark = (fifo_watermark / hw->sip) * hw->sip;
213 	fifo_watermark = fifo_watermark * hw->settings->fifo_ops.th_wl;
214 
215 	err = regmap_read(hw->regmap, hw->settings->fifo_ops.fifo_th.addr + 1,
216 			  &data);
217 	if (err < 0)
218 		return err;
219 
220 	fifo_th_mask = hw->settings->fifo_ops.fifo_th.mask;
221 	fifo_watermark = ((data << 8) & ~fifo_th_mask) |
222 			 (fifo_watermark & fifo_th_mask);
223 
224 	wdata = cpu_to_le16(fifo_watermark);
225 	return regmap_bulk_write(hw->regmap,
226 				 hw->settings->fifo_ops.fifo_th.addr,
227 				 &wdata, sizeof(wdata));
228 }
229 
st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw * hw)230 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw)
231 {
232 	struct st_lsm6dsx_sensor *sensor;
233 	int i, err;
234 
235 	/* reset hw ts counter */
236 	err = regmap_write(hw->regmap, ST_LSM6DSX_REG_TS_RESET_ADDR,
237 			   ST_LSM6DSX_TS_RESET_VAL);
238 	if (err < 0)
239 		return err;
240 
241 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
242 		sensor = iio_priv(hw->iio_devs[i]);
243 		/*
244 		 * store enable buffer timestamp as reference for
245 		 * hw timestamp
246 		 */
247 		sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]);
248 	}
249 	return 0;
250 }
251 
252 /*
253  * Set max bulk read to ST_LSM6DSX_MAX_WORD_LEN in order to avoid
254  * a kmalloc for each bus access
255  */
st_lsm6dsx_read_block(struct st_lsm6dsx_hw * hw,u8 * data,unsigned int data_len)256 static inline int st_lsm6dsx_read_block(struct st_lsm6dsx_hw *hw, u8 *data,
257 					unsigned int data_len)
258 {
259 	unsigned int word_len, read_len = 0;
260 	int err;
261 
262 	while (read_len < data_len) {
263 		word_len = min_t(unsigned int, data_len - read_len,
264 				 ST_LSM6DSX_MAX_WORD_LEN);
265 		err = regmap_bulk_read(hw->regmap,
266 				       ST_LSM6DSX_REG_FIFO_OUTL_ADDR,
267 				       data + read_len, word_len);
268 		if (err < 0)
269 			return err;
270 		read_len += word_len;
271 	}
272 	return 0;
273 }
274 
275 #define ST_LSM6DSX_IIO_BUFF_SIZE	(ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \
276 					       sizeof(s64)) + sizeof(s64))
277 /**
278  * st_lsm6dsx_read_fifo() - hw FIFO read routine
279  * @hw: Pointer to instance of struct st_lsm6dsx_hw.
280  *
281  * Read samples from the hw FIFO and push them to IIO buffers.
282  *
283  * Return: Number of bytes read from the FIFO
284  */
st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw * hw)285 static int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw)
286 {
287 	u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE;
288 	u16 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask;
289 	int err, acc_sip, gyro_sip, ts_sip, read_len, offset;
290 	struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor;
291 	u8 gyro_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
292 	u8 acc_buff[ST_LSM6DSX_IIO_BUFF_SIZE];
293 	bool reset_ts = false;
294 	__le16 fifo_status;
295 	s64 ts = 0;
296 
297 	err = regmap_bulk_read(hw->regmap,
298 			       hw->settings->fifo_ops.fifo_diff.addr,
299 			       &fifo_status, sizeof(fifo_status));
300 	if (err < 0) {
301 		dev_err(hw->dev, "failed to read fifo status (err=%d)\n",
302 			err);
303 		return err;
304 	}
305 
306 	if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK))
307 		return 0;
308 
309 	fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) *
310 		   ST_LSM6DSX_CHAN_SIZE;
311 	fifo_len = (fifo_len / pattern_len) * pattern_len;
312 
313 	acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]);
314 	gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]);
315 
316 	for (read_len = 0; read_len < fifo_len; read_len += pattern_len) {
317 		err = st_lsm6dsx_read_block(hw, hw->buff, pattern_len);
318 		if (err < 0) {
319 			dev_err(hw->dev,
320 				"failed to read pattern from fifo (err=%d)\n",
321 				err);
322 			return err;
323 		}
324 
325 		/*
326 		 * Data are written to the FIFO with a specific pattern
327 		 * depending on the configured ODRs. The first sequence of data
328 		 * stored in FIFO contains the data of all enabled sensors
329 		 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated
330 		 * depending on the value of the decimation factor set for each
331 		 * sensor.
332 		 *
333 		 * Supposing the FIFO is storing data from gyroscope and
334 		 * accelerometer at different ODRs:
335 		 *   - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz
336 		 * Since the gyroscope ODR is twice the accelerometer one, the
337 		 * following pattern is repeated every 9 samples:
338 		 *   - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, ..
339 		 */
340 		gyro_sip = gyro_sensor->sip;
341 		acc_sip = acc_sensor->sip;
342 		ts_sip = hw->ts_sip;
343 		offset = 0;
344 
345 		while (acc_sip > 0 || gyro_sip > 0) {
346 			if (gyro_sip > 0) {
347 				memcpy(gyro_buff, &hw->buff[offset],
348 				       ST_LSM6DSX_SAMPLE_SIZE);
349 				offset += ST_LSM6DSX_SAMPLE_SIZE;
350 			}
351 			if (acc_sip > 0) {
352 				memcpy(acc_buff, &hw->buff[offset],
353 				       ST_LSM6DSX_SAMPLE_SIZE);
354 				offset += ST_LSM6DSX_SAMPLE_SIZE;
355 			}
356 
357 			if (ts_sip-- > 0) {
358 				u8 data[ST_LSM6DSX_SAMPLE_SIZE];
359 
360 				memcpy(data, &hw->buff[offset], sizeof(data));
361 				/*
362 				 * hw timestamp is 3B long and it is stored
363 				 * in FIFO using 6B as 4th FIFO data set
364 				 * according to this schema:
365 				 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0]
366 				 */
367 				ts = data[1] << 16 | data[0] << 8 | data[3];
368 				/*
369 				 * check if hw timestamp engine is going to
370 				 * reset (the sensor generates an interrupt
371 				 * to signal the hw timestamp will reset in
372 				 * 1.638s)
373 				 */
374 				if (!reset_ts && ts >= 0xff0000)
375 					reset_ts = true;
376 				ts *= ST_LSM6DSX_TS_SENSITIVITY;
377 
378 				offset += ST_LSM6DSX_SAMPLE_SIZE;
379 			}
380 
381 			if (gyro_sip-- > 0)
382 				iio_push_to_buffers_with_timestamp(
383 					hw->iio_devs[ST_LSM6DSX_ID_GYRO],
384 					gyro_buff, gyro_sensor->ts_ref + ts);
385 			if (acc_sip-- > 0)
386 				iio_push_to_buffers_with_timestamp(
387 					hw->iio_devs[ST_LSM6DSX_ID_ACC],
388 					acc_buff, acc_sensor->ts_ref + ts);
389 		}
390 	}
391 
392 	if (unlikely(reset_ts)) {
393 		err = st_lsm6dsx_reset_hw_ts(hw);
394 		if (err < 0) {
395 			dev_err(hw->dev, "failed to reset hw ts (err=%d)\n",
396 				err);
397 			return err;
398 		}
399 	}
400 	return read_len;
401 }
402 
st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw * hw)403 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw)
404 {
405 	int err;
406 
407 	mutex_lock(&hw->fifo_lock);
408 
409 	st_lsm6dsx_read_fifo(hw);
410 	err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS);
411 
412 	mutex_unlock(&hw->fifo_lock);
413 
414 	return err;
415 }
416 
st_lsm6dsx_update_fifo(struct iio_dev * iio_dev,bool enable)417 static int st_lsm6dsx_update_fifo(struct iio_dev *iio_dev, bool enable)
418 {
419 	struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev);
420 	struct st_lsm6dsx_hw *hw = sensor->hw;
421 	int err;
422 
423 	mutex_lock(&hw->conf_lock);
424 
425 	if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS) {
426 		err = st_lsm6dsx_flush_fifo(hw);
427 		if (err < 0)
428 			goto out;
429 	}
430 
431 	if (enable) {
432 		err = st_lsm6dsx_sensor_enable(sensor);
433 		if (err < 0)
434 			goto out;
435 	} else {
436 		err = st_lsm6dsx_sensor_disable(sensor);
437 		if (err < 0)
438 			goto out;
439 	}
440 
441 	err = st_lsm6dsx_set_fifo_odr(sensor, enable);
442 	if (err < 0)
443 		goto out;
444 
445 	err = st_lsm6dsx_update_decimators(hw);
446 	if (err < 0)
447 		goto out;
448 
449 	err = st_lsm6dsx_update_watermark(sensor, sensor->watermark);
450 	if (err < 0)
451 		goto out;
452 
453 	if (hw->enable_mask) {
454 		/* reset hw ts counter */
455 		err = st_lsm6dsx_reset_hw_ts(hw);
456 		if (err < 0)
457 			goto out;
458 
459 		err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT);
460 	}
461 
462 out:
463 	mutex_unlock(&hw->conf_lock);
464 
465 	return err;
466 }
467 
st_lsm6dsx_handler_irq(int irq,void * private)468 static irqreturn_t st_lsm6dsx_handler_irq(int irq, void *private)
469 {
470 	struct st_lsm6dsx_hw *hw = private;
471 
472 	return hw->sip > 0 ? IRQ_WAKE_THREAD : IRQ_NONE;
473 }
474 
st_lsm6dsx_handler_thread(int irq,void * private)475 static irqreturn_t st_lsm6dsx_handler_thread(int irq, void *private)
476 {
477 	struct st_lsm6dsx_hw *hw = private;
478 	int fifo_len = 0, len;
479 
480 	/*
481 	 * If we are using edge IRQs, new samples can arrive while
482 	 * processing current interrupt since there are no hw
483 	 * guarantees the irq line stays "low" long enough to properly
484 	 * detect the new interrupt. In this case the new sample will
485 	 * be missed.
486 	 * Polling FIFO status register allow us to read new
487 	 * samples even if the interrupt arrives while processing
488 	 * previous data and the timeslot where the line is "low" is
489 	 * too short to be properly detected.
490 	 */
491 	do {
492 		mutex_lock(&hw->fifo_lock);
493 		len = st_lsm6dsx_read_fifo(hw);
494 		mutex_unlock(&hw->fifo_lock);
495 
496 		if (len > 0)
497 			fifo_len += len;
498 	} while (len > 0);
499 
500 	return fifo_len ? IRQ_HANDLED : IRQ_NONE;
501 }
502 
st_lsm6dsx_buffer_preenable(struct iio_dev * iio_dev)503 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev)
504 {
505 	return st_lsm6dsx_update_fifo(iio_dev, true);
506 }
507 
st_lsm6dsx_buffer_postdisable(struct iio_dev * iio_dev)508 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev)
509 {
510 	return st_lsm6dsx_update_fifo(iio_dev, false);
511 }
512 
513 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = {
514 	.preenable = st_lsm6dsx_buffer_preenable,
515 	.postdisable = st_lsm6dsx_buffer_postdisable,
516 };
517 
st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw * hw)518 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw)
519 {
520 	struct device_node *np = hw->dev->of_node;
521 	struct st_sensors_platform_data *pdata;
522 	struct iio_buffer *buffer;
523 	unsigned long irq_type;
524 	bool irq_active_low;
525 	int i, err;
526 
527 	irq_type = irqd_get_trigger_type(irq_get_irq_data(hw->irq));
528 
529 	switch (irq_type) {
530 	case IRQF_TRIGGER_HIGH:
531 	case IRQF_TRIGGER_RISING:
532 		irq_active_low = false;
533 		break;
534 	case IRQF_TRIGGER_LOW:
535 	case IRQF_TRIGGER_FALLING:
536 		irq_active_low = true;
537 		break;
538 	default:
539 		dev_info(hw->dev, "mode %lx unsupported\n", irq_type);
540 		return -EINVAL;
541 	}
542 
543 	err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_HLACTIVE_ADDR,
544 				 ST_LSM6DSX_REG_HLACTIVE_MASK,
545 				 FIELD_PREP(ST_LSM6DSX_REG_HLACTIVE_MASK,
546 					    irq_active_low));
547 	if (err < 0)
548 		return err;
549 
550 	pdata = (struct st_sensors_platform_data *)hw->dev->platform_data;
551 	if ((np && of_property_read_bool(np, "drive-open-drain")) ||
552 	    (pdata && pdata->open_drain)) {
553 		err = regmap_update_bits(hw->regmap, ST_LSM6DSX_REG_PP_OD_ADDR,
554 					 ST_LSM6DSX_REG_PP_OD_MASK,
555 					 FIELD_PREP(ST_LSM6DSX_REG_PP_OD_MASK,
556 						    1));
557 		if (err < 0)
558 			return err;
559 
560 		irq_type |= IRQF_SHARED;
561 	}
562 
563 	err = devm_request_threaded_irq(hw->dev, hw->irq,
564 					st_lsm6dsx_handler_irq,
565 					st_lsm6dsx_handler_thread,
566 					irq_type | IRQF_ONESHOT,
567 					"lsm6dsx", hw);
568 	if (err) {
569 		dev_err(hw->dev, "failed to request trigger irq %d\n",
570 			hw->irq);
571 		return err;
572 	}
573 
574 	for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) {
575 		buffer = devm_iio_kfifo_allocate(hw->dev);
576 		if (!buffer)
577 			return -ENOMEM;
578 
579 		iio_device_attach_buffer(hw->iio_devs[i], buffer);
580 		hw->iio_devs[i]->modes |= INDIO_BUFFER_SOFTWARE;
581 		hw->iio_devs[i]->setup_ops = &st_lsm6dsx_buffer_ops;
582 	}
583 
584 	return 0;
585 }
586