1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * STMicroelectronics st_lsm6dsx FIFO buffer library driver 4 * 5 * LSM6DS3/LSM6DS3H/LSM6DSL/LSM6DSM/ISM330DLC/LSM6DS3TR-C: 6 * The FIFO buffer can be configured to store data from gyroscope and 7 * accelerometer. Samples are queued without any tag according to a 8 * specific pattern based on 'FIFO data sets' (6 bytes each): 9 * - 1st data set is reserved for gyroscope data 10 * - 2nd data set is reserved for accelerometer data 11 * The FIFO pattern changes depending on the ODRs and decimation factors 12 * assigned to the FIFO data sets. The first sequence of data stored in FIFO 13 * buffer contains the data of all the enabled FIFO data sets 14 * (e.g. Gx, Gy, Gz, Ax, Ay, Az), then data are repeated depending on the 15 * value of the decimation factor and ODR set for each FIFO data set. 16 * 17 * LSM6DSO/LSM6DSOX/ASM330LHH/LSM6DSR/LSM6DSRX/ISM330DHCX: 18 * The FIFO buffer can be configured to store data from gyroscope and 19 * accelerometer. Each sample is queued with a tag (1B) indicating data 20 * source (gyroscope, accelerometer, hw timer). 21 * 22 * FIFO supported modes: 23 * - BYPASS: FIFO disabled 24 * - CONTINUOUS: FIFO enabled. When the buffer is full, the FIFO index 25 * restarts from the beginning and the oldest sample is overwritten 26 * 27 * Copyright 2016 STMicroelectronics Inc. 28 * 29 * Lorenzo Bianconi <lorenzo.bianconi@st.com> 30 * Denis Ciocca <denis.ciocca@st.com> 31 */ 32 #include <linux/module.h> 33 #include <linux/iio/kfifo_buf.h> 34 #include <linux/iio/iio.h> 35 #include <linux/iio/buffer.h> 36 #include <linux/regmap.h> 37 #include <linux/bitfield.h> 38 39 #include <linux/platform_data/st_sensors_pdata.h> 40 41 #include "st_lsm6dsx.h" 42 43 #define ST_LSM6DSX_REG_FIFO_MODE_ADDR 0x0a 44 #define ST_LSM6DSX_FIFO_MODE_MASK GENMASK(2, 0) 45 #define ST_LSM6DSX_FIFO_ODR_MASK GENMASK(6, 3) 46 #define ST_LSM6DSX_FIFO_EMPTY_MASK BIT(12) 47 #define ST_LSM6DSX_REG_FIFO_OUTL_ADDR 0x3e 48 #define ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR 0x78 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_RESET_VAL 0xaa 54 55 struct st_lsm6dsx_decimator_entry { 56 u8 decimator; 57 u8 val; 58 }; 59 60 enum st_lsm6dsx_fifo_tag { 61 ST_LSM6DSX_GYRO_TAG = 0x01, 62 ST_LSM6DSX_ACC_TAG = 0x02, 63 ST_LSM6DSX_TS_TAG = 0x04, 64 ST_LSM6DSX_EXT0_TAG = 0x0f, 65 ST_LSM6DSX_EXT1_TAG = 0x10, 66 ST_LSM6DSX_EXT2_TAG = 0x11, 67 }; 68 69 static const 70 struct st_lsm6dsx_decimator_entry st_lsm6dsx_decimator_table[] = { 71 { 0, 0x0 }, 72 { 1, 0x1 }, 73 { 2, 0x2 }, 74 { 3, 0x3 }, 75 { 4, 0x4 }, 76 { 8, 0x5 }, 77 { 16, 0x6 }, 78 { 32, 0x7 }, 79 }; 80 81 static int st_lsm6dsx_get_decimator_val(u8 val) 82 { 83 const int max_size = ARRAY_SIZE(st_lsm6dsx_decimator_table); 84 int i; 85 86 for (i = 0; i < max_size; i++) 87 if (st_lsm6dsx_decimator_table[i].decimator == val) 88 break; 89 90 return i == max_size ? 0 : st_lsm6dsx_decimator_table[i].val; 91 } 92 93 static void st_lsm6dsx_get_max_min_odr(struct st_lsm6dsx_hw *hw, 94 u16 *max_odr, u16 *min_odr) 95 { 96 struct st_lsm6dsx_sensor *sensor; 97 int i; 98 99 *max_odr = 0, *min_odr = ~0; 100 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) { 101 if (!hw->iio_devs[i]) 102 continue; 103 104 sensor = iio_priv(hw->iio_devs[i]); 105 106 if (!(hw->enable_mask & BIT(sensor->id))) 107 continue; 108 109 *max_odr = max_t(u16, *max_odr, sensor->odr); 110 *min_odr = min_t(u16, *min_odr, sensor->odr); 111 } 112 } 113 114 static int st_lsm6dsx_update_decimators(struct st_lsm6dsx_hw *hw) 115 { 116 u16 max_odr, min_odr, sip = 0, ts_sip = 0; 117 const struct st_lsm6dsx_reg *ts_dec_reg; 118 struct st_lsm6dsx_sensor *sensor; 119 int err = 0, i; 120 u8 data; 121 122 st_lsm6dsx_get_max_min_odr(hw, &max_odr, &min_odr); 123 124 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) { 125 const struct st_lsm6dsx_reg *dec_reg; 126 127 if (!hw->iio_devs[i]) 128 continue; 129 130 sensor = iio_priv(hw->iio_devs[i]); 131 /* update fifo decimators and sample in pattern */ 132 if (hw->enable_mask & BIT(sensor->id)) { 133 sensor->sip = sensor->odr / min_odr; 134 sensor->decimator = max_odr / sensor->odr; 135 data = st_lsm6dsx_get_decimator_val(sensor->decimator); 136 } else { 137 sensor->sip = 0; 138 sensor->decimator = 0; 139 data = 0; 140 } 141 ts_sip = max_t(u16, ts_sip, sensor->sip); 142 143 dec_reg = &hw->settings->decimator[sensor->id]; 144 if (dec_reg->addr) { 145 int val = ST_LSM6DSX_SHIFT_VAL(data, dec_reg->mask); 146 147 err = st_lsm6dsx_update_bits_locked(hw, dec_reg->addr, 148 dec_reg->mask, 149 val); 150 if (err < 0) 151 return err; 152 } 153 sip += sensor->sip; 154 } 155 hw->sip = sip + ts_sip; 156 hw->ts_sip = ts_sip; 157 158 /* 159 * update hw ts decimator if necessary. Decimator for hw timestamp 160 * is always 1 or 0 in order to have a ts sample for each data 161 * sample in FIFO 162 */ 163 ts_dec_reg = &hw->settings->ts_settings.decimator; 164 if (ts_dec_reg->addr) { 165 int val, ts_dec = !!hw->ts_sip; 166 167 val = ST_LSM6DSX_SHIFT_VAL(ts_dec, ts_dec_reg->mask); 168 err = st_lsm6dsx_update_bits_locked(hw, ts_dec_reg->addr, 169 ts_dec_reg->mask, val); 170 } 171 return err; 172 } 173 174 int st_lsm6dsx_set_fifo_mode(struct st_lsm6dsx_hw *hw, 175 enum st_lsm6dsx_fifo_mode fifo_mode) 176 { 177 unsigned int data; 178 int err; 179 180 data = FIELD_PREP(ST_LSM6DSX_FIFO_MODE_MASK, fifo_mode); 181 err = st_lsm6dsx_update_bits_locked(hw, ST_LSM6DSX_REG_FIFO_MODE_ADDR, 182 ST_LSM6DSX_FIFO_MODE_MASK, data); 183 if (err < 0) 184 return err; 185 186 hw->fifo_mode = fifo_mode; 187 188 return 0; 189 } 190 191 static int st_lsm6dsx_set_fifo_odr(struct st_lsm6dsx_sensor *sensor, 192 bool enable) 193 { 194 struct st_lsm6dsx_hw *hw = sensor->hw; 195 const struct st_lsm6dsx_reg *batch_reg; 196 u8 data; 197 198 batch_reg = &hw->settings->batch[sensor->id]; 199 if (batch_reg->addr) { 200 int val; 201 202 if (enable) { 203 int err; 204 205 err = st_lsm6dsx_check_odr(sensor, sensor->odr, 206 &data); 207 if (err < 0) 208 return err; 209 } else { 210 data = 0; 211 } 212 val = ST_LSM6DSX_SHIFT_VAL(data, batch_reg->mask); 213 return st_lsm6dsx_update_bits_locked(hw, batch_reg->addr, 214 batch_reg->mask, val); 215 } else { 216 data = hw->enable_mask ? ST_LSM6DSX_MAX_FIFO_ODR_VAL : 0; 217 return st_lsm6dsx_update_bits_locked(hw, 218 ST_LSM6DSX_REG_FIFO_MODE_ADDR, 219 ST_LSM6DSX_FIFO_ODR_MASK, 220 FIELD_PREP(ST_LSM6DSX_FIFO_ODR_MASK, 221 data)); 222 } 223 } 224 225 int st_lsm6dsx_update_watermark(struct st_lsm6dsx_sensor *sensor, u16 watermark) 226 { 227 u16 fifo_watermark = ~0, cur_watermark, fifo_th_mask; 228 struct st_lsm6dsx_hw *hw = sensor->hw; 229 struct st_lsm6dsx_sensor *cur_sensor; 230 int i, err, data; 231 __le16 wdata; 232 233 if (!hw->sip) 234 return 0; 235 236 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) { 237 if (!hw->iio_devs[i]) 238 continue; 239 240 cur_sensor = iio_priv(hw->iio_devs[i]); 241 242 if (!(hw->enable_mask & BIT(cur_sensor->id))) 243 continue; 244 245 cur_watermark = (cur_sensor == sensor) ? watermark 246 : cur_sensor->watermark; 247 248 fifo_watermark = min_t(u16, fifo_watermark, cur_watermark); 249 } 250 251 fifo_watermark = max_t(u16, fifo_watermark, hw->sip); 252 fifo_watermark = (fifo_watermark / hw->sip) * hw->sip; 253 fifo_watermark = fifo_watermark * hw->settings->fifo_ops.th_wl; 254 255 mutex_lock(&hw->page_lock); 256 err = regmap_read(hw->regmap, hw->settings->fifo_ops.fifo_th.addr + 1, 257 &data); 258 if (err < 0) 259 goto out; 260 261 fifo_th_mask = hw->settings->fifo_ops.fifo_th.mask; 262 fifo_watermark = ((data << 8) & ~fifo_th_mask) | 263 (fifo_watermark & fifo_th_mask); 264 265 wdata = cpu_to_le16(fifo_watermark); 266 err = regmap_bulk_write(hw->regmap, 267 hw->settings->fifo_ops.fifo_th.addr, 268 &wdata, sizeof(wdata)); 269 out: 270 mutex_unlock(&hw->page_lock); 271 return err; 272 } 273 274 static int st_lsm6dsx_reset_hw_ts(struct st_lsm6dsx_hw *hw) 275 { 276 struct st_lsm6dsx_sensor *sensor; 277 int i, err; 278 279 /* reset hw ts counter */ 280 err = st_lsm6dsx_write_locked(hw, ST_LSM6DSX_REG_TS_RESET_ADDR, 281 ST_LSM6DSX_TS_RESET_VAL); 282 if (err < 0) 283 return err; 284 285 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) { 286 if (!hw->iio_devs[i]) 287 continue; 288 289 sensor = iio_priv(hw->iio_devs[i]); 290 /* 291 * store enable buffer timestamp as reference for 292 * hw timestamp 293 */ 294 sensor->ts_ref = iio_get_time_ns(hw->iio_devs[i]); 295 } 296 return 0; 297 } 298 299 /* 300 * Set max bulk read to ST_LSM6DSX_MAX_WORD_LEN/ST_LSM6DSX_MAX_TAGGED_WORD_LEN 301 * in order to avoid a kmalloc for each bus access 302 */ 303 static inline int st_lsm6dsx_read_block(struct st_lsm6dsx_hw *hw, u8 addr, 304 u8 *data, unsigned int data_len, 305 unsigned int max_word_len) 306 { 307 unsigned int word_len, read_len = 0; 308 int err; 309 310 while (read_len < data_len) { 311 word_len = min_t(unsigned int, data_len - read_len, 312 max_word_len); 313 err = st_lsm6dsx_read_locked(hw, addr, data + read_len, 314 word_len); 315 if (err < 0) 316 return err; 317 read_len += word_len; 318 } 319 return 0; 320 } 321 322 #define ST_LSM6DSX_IIO_BUFF_SIZE (ALIGN(ST_LSM6DSX_SAMPLE_SIZE, \ 323 sizeof(s64)) + sizeof(s64)) 324 /** 325 * st_lsm6dsx_read_fifo() - hw FIFO read routine 326 * @hw: Pointer to instance of struct st_lsm6dsx_hw. 327 * 328 * Read samples from the hw FIFO and push them to IIO buffers. 329 * 330 * Return: Number of bytes read from the FIFO 331 */ 332 int st_lsm6dsx_read_fifo(struct st_lsm6dsx_hw *hw) 333 { 334 u16 fifo_len, pattern_len = hw->sip * ST_LSM6DSX_SAMPLE_SIZE; 335 u16 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask; 336 int err, acc_sip, gyro_sip, ts_sip, read_len, offset; 337 struct st_lsm6dsx_sensor *acc_sensor, *gyro_sensor; 338 u8 gyro_buff[ST_LSM6DSX_IIO_BUFF_SIZE]; 339 u8 acc_buff[ST_LSM6DSX_IIO_BUFF_SIZE]; 340 bool reset_ts = false; 341 __le16 fifo_status; 342 s64 ts = 0; 343 344 err = st_lsm6dsx_read_locked(hw, 345 hw->settings->fifo_ops.fifo_diff.addr, 346 &fifo_status, sizeof(fifo_status)); 347 if (err < 0) { 348 dev_err(hw->dev, "failed to read fifo status (err=%d)\n", 349 err); 350 return err; 351 } 352 353 if (fifo_status & cpu_to_le16(ST_LSM6DSX_FIFO_EMPTY_MASK)) 354 return 0; 355 356 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) * 357 ST_LSM6DSX_CHAN_SIZE; 358 fifo_len = (fifo_len / pattern_len) * pattern_len; 359 360 acc_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_ACC]); 361 gyro_sensor = iio_priv(hw->iio_devs[ST_LSM6DSX_ID_GYRO]); 362 363 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) { 364 err = st_lsm6dsx_read_block(hw, ST_LSM6DSX_REG_FIFO_OUTL_ADDR, 365 hw->buff, pattern_len, 366 ST_LSM6DSX_MAX_WORD_LEN); 367 if (err < 0) { 368 dev_err(hw->dev, 369 "failed to read pattern from fifo (err=%d)\n", 370 err); 371 return err; 372 } 373 374 /* 375 * Data are written to the FIFO with a specific pattern 376 * depending on the configured ODRs. The first sequence of data 377 * stored in FIFO contains the data of all enabled sensors 378 * (e.g. Gx, Gy, Gz, Ax, Ay, Az, Ts), then data are repeated 379 * depending on the value of the decimation factor set for each 380 * sensor. 381 * 382 * Supposing the FIFO is storing data from gyroscope and 383 * accelerometer at different ODRs: 384 * - gyroscope ODR = 208Hz, accelerometer ODR = 104Hz 385 * Since the gyroscope ODR is twice the accelerometer one, the 386 * following pattern is repeated every 9 samples: 387 * - Gx, Gy, Gz, Ax, Ay, Az, Ts, Gx, Gy, Gz, Ts, Gx, .. 388 */ 389 gyro_sip = gyro_sensor->sip; 390 acc_sip = acc_sensor->sip; 391 ts_sip = hw->ts_sip; 392 offset = 0; 393 394 while (acc_sip > 0 || gyro_sip > 0) { 395 if (gyro_sip > 0) { 396 memcpy(gyro_buff, &hw->buff[offset], 397 ST_LSM6DSX_SAMPLE_SIZE); 398 offset += ST_LSM6DSX_SAMPLE_SIZE; 399 } 400 if (acc_sip > 0) { 401 memcpy(acc_buff, &hw->buff[offset], 402 ST_LSM6DSX_SAMPLE_SIZE); 403 offset += ST_LSM6DSX_SAMPLE_SIZE; 404 } 405 406 if (ts_sip-- > 0) { 407 u8 data[ST_LSM6DSX_SAMPLE_SIZE]; 408 409 memcpy(data, &hw->buff[offset], sizeof(data)); 410 /* 411 * hw timestamp is 3B long and it is stored 412 * in FIFO using 6B as 4th FIFO data set 413 * according to this schema: 414 * B0 = ts[15:8], B1 = ts[23:16], B3 = ts[7:0] 415 */ 416 ts = data[1] << 16 | data[0] << 8 | data[3]; 417 /* 418 * check if hw timestamp engine is going to 419 * reset (the sensor generates an interrupt 420 * to signal the hw timestamp will reset in 421 * 1.638s) 422 */ 423 if (!reset_ts && ts >= 0xff0000) 424 reset_ts = true; 425 ts *= hw->ts_gain; 426 427 offset += ST_LSM6DSX_SAMPLE_SIZE; 428 } 429 430 if (gyro_sip-- > 0) 431 iio_push_to_buffers_with_timestamp( 432 hw->iio_devs[ST_LSM6DSX_ID_GYRO], 433 gyro_buff, gyro_sensor->ts_ref + ts); 434 if (acc_sip-- > 0) 435 iio_push_to_buffers_with_timestamp( 436 hw->iio_devs[ST_LSM6DSX_ID_ACC], 437 acc_buff, acc_sensor->ts_ref + ts); 438 } 439 } 440 441 if (unlikely(reset_ts)) { 442 err = st_lsm6dsx_reset_hw_ts(hw); 443 if (err < 0) { 444 dev_err(hw->dev, "failed to reset hw ts (err=%d)\n", 445 err); 446 return err; 447 } 448 } 449 return read_len; 450 } 451 452 #define ST_LSM6DSX_INVALID_SAMPLE 0x7ffd 453 static int 454 st_lsm6dsx_push_tagged_data(struct st_lsm6dsx_hw *hw, u8 tag, 455 u8 *data, s64 ts) 456 { 457 s16 val = le16_to_cpu(*(__le16 *)data); 458 struct st_lsm6dsx_sensor *sensor; 459 struct iio_dev *iio_dev; 460 461 /* invalid sample during bootstrap phase */ 462 if (val >= ST_LSM6DSX_INVALID_SAMPLE) 463 return -EINVAL; 464 465 /* 466 * EXT_TAG are managed in FIFO fashion so ST_LSM6DSX_EXT0_TAG 467 * corresponds to the first enabled channel, ST_LSM6DSX_EXT1_TAG 468 * to the second one and ST_LSM6DSX_EXT2_TAG to the last enabled 469 * channel 470 */ 471 switch (tag) { 472 case ST_LSM6DSX_GYRO_TAG: 473 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_GYRO]; 474 break; 475 case ST_LSM6DSX_ACC_TAG: 476 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_ACC]; 477 break; 478 case ST_LSM6DSX_EXT0_TAG: 479 if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0)) 480 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT0]; 481 else if (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1)) 482 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1]; 483 else 484 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2]; 485 break; 486 case ST_LSM6DSX_EXT1_TAG: 487 if ((hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT0)) && 488 (hw->enable_mask & BIT(ST_LSM6DSX_ID_EXT1))) 489 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT1]; 490 else 491 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2]; 492 break; 493 case ST_LSM6DSX_EXT2_TAG: 494 iio_dev = hw->iio_devs[ST_LSM6DSX_ID_EXT2]; 495 break; 496 default: 497 return -EINVAL; 498 } 499 500 sensor = iio_priv(iio_dev); 501 iio_push_to_buffers_with_timestamp(iio_dev, data, 502 ts + sensor->ts_ref); 503 504 return 0; 505 } 506 507 /** 508 * st_lsm6dsx_read_tagged_fifo() - tagged hw FIFO read routine 509 * @hw: Pointer to instance of struct st_lsm6dsx_hw. 510 * 511 * Read samples from the hw FIFO and push them to IIO buffers. 512 * 513 * Return: Number of bytes read from the FIFO 514 */ 515 int st_lsm6dsx_read_tagged_fifo(struct st_lsm6dsx_hw *hw) 516 { 517 u16 pattern_len = hw->sip * ST_LSM6DSX_TAGGED_SAMPLE_SIZE; 518 u16 fifo_len, fifo_diff_mask; 519 u8 iio_buff[ST_LSM6DSX_IIO_BUFF_SIZE], tag; 520 bool reset_ts = false; 521 int i, err, read_len; 522 __le16 fifo_status; 523 s64 ts = 0; 524 525 err = st_lsm6dsx_read_locked(hw, 526 hw->settings->fifo_ops.fifo_diff.addr, 527 &fifo_status, sizeof(fifo_status)); 528 if (err < 0) { 529 dev_err(hw->dev, "failed to read fifo status (err=%d)\n", 530 err); 531 return err; 532 } 533 534 fifo_diff_mask = hw->settings->fifo_ops.fifo_diff.mask; 535 fifo_len = (le16_to_cpu(fifo_status) & fifo_diff_mask) * 536 ST_LSM6DSX_TAGGED_SAMPLE_SIZE; 537 if (!fifo_len) 538 return 0; 539 540 for (read_len = 0; read_len < fifo_len; read_len += pattern_len) { 541 err = st_lsm6dsx_read_block(hw, 542 ST_LSM6DSX_REG_FIFO_OUT_TAG_ADDR, 543 hw->buff, pattern_len, 544 ST_LSM6DSX_MAX_TAGGED_WORD_LEN); 545 if (err < 0) { 546 dev_err(hw->dev, 547 "failed to read pattern from fifo (err=%d)\n", 548 err); 549 return err; 550 } 551 552 for (i = 0; i < pattern_len; 553 i += ST_LSM6DSX_TAGGED_SAMPLE_SIZE) { 554 memcpy(iio_buff, &hw->buff[i + ST_LSM6DSX_TAG_SIZE], 555 ST_LSM6DSX_SAMPLE_SIZE); 556 557 tag = hw->buff[i] >> 3; 558 if (tag == ST_LSM6DSX_TS_TAG) { 559 /* 560 * hw timestamp is 4B long and it is stored 561 * in FIFO according to this schema: 562 * B0 = ts[7:0], B1 = ts[15:8], B2 = ts[23:16], 563 * B3 = ts[31:24] 564 */ 565 ts = le32_to_cpu(*((__le32 *)iio_buff)); 566 /* 567 * check if hw timestamp engine is going to 568 * reset (the sensor generates an interrupt 569 * to signal the hw timestamp will reset in 570 * 1.638s) 571 */ 572 if (!reset_ts && ts >= 0xffff0000) 573 reset_ts = true; 574 ts *= hw->ts_gain; 575 } else { 576 st_lsm6dsx_push_tagged_data(hw, tag, iio_buff, 577 ts); 578 } 579 } 580 } 581 582 if (unlikely(reset_ts)) { 583 err = st_lsm6dsx_reset_hw_ts(hw); 584 if (err < 0) 585 return err; 586 } 587 return read_len; 588 } 589 590 int st_lsm6dsx_flush_fifo(struct st_lsm6dsx_hw *hw) 591 { 592 int err; 593 594 if (!hw->settings->fifo_ops.read_fifo) 595 return -ENOTSUPP; 596 597 mutex_lock(&hw->fifo_lock); 598 599 hw->settings->fifo_ops.read_fifo(hw); 600 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_BYPASS); 601 602 mutex_unlock(&hw->fifo_lock); 603 604 return err; 605 } 606 607 int st_lsm6dsx_update_fifo(struct st_lsm6dsx_sensor *sensor, bool enable) 608 { 609 struct st_lsm6dsx_hw *hw = sensor->hw; 610 int err; 611 612 mutex_lock(&hw->conf_lock); 613 614 if (hw->fifo_mode != ST_LSM6DSX_FIFO_BYPASS) { 615 err = st_lsm6dsx_flush_fifo(hw); 616 if (err < 0) 617 goto out; 618 } 619 620 if (sensor->id == ST_LSM6DSX_ID_EXT0 || 621 sensor->id == ST_LSM6DSX_ID_EXT1 || 622 sensor->id == ST_LSM6DSX_ID_EXT2) { 623 err = st_lsm6dsx_shub_set_enable(sensor, enable); 624 if (err < 0) 625 goto out; 626 } else { 627 err = st_lsm6dsx_sensor_set_enable(sensor, enable); 628 if (err < 0) 629 goto out; 630 631 err = st_lsm6dsx_set_fifo_odr(sensor, enable); 632 if (err < 0) 633 goto out; 634 } 635 636 err = st_lsm6dsx_update_decimators(hw); 637 if (err < 0) 638 goto out; 639 640 err = st_lsm6dsx_update_watermark(sensor, sensor->watermark); 641 if (err < 0) 642 goto out; 643 644 if (hw->enable_mask) { 645 /* reset hw ts counter */ 646 err = st_lsm6dsx_reset_hw_ts(hw); 647 if (err < 0) 648 goto out; 649 650 err = st_lsm6dsx_set_fifo_mode(hw, ST_LSM6DSX_FIFO_CONT); 651 } 652 653 out: 654 mutex_unlock(&hw->conf_lock); 655 656 return err; 657 } 658 659 static int st_lsm6dsx_buffer_preenable(struct iio_dev *iio_dev) 660 { 661 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); 662 struct st_lsm6dsx_hw *hw = sensor->hw; 663 664 if (!hw->settings->fifo_ops.update_fifo) 665 return -ENOTSUPP; 666 667 return hw->settings->fifo_ops.update_fifo(sensor, true); 668 } 669 670 static int st_lsm6dsx_buffer_postdisable(struct iio_dev *iio_dev) 671 { 672 struct st_lsm6dsx_sensor *sensor = iio_priv(iio_dev); 673 struct st_lsm6dsx_hw *hw = sensor->hw; 674 675 if (!hw->settings->fifo_ops.update_fifo) 676 return -ENOTSUPP; 677 678 return hw->settings->fifo_ops.update_fifo(sensor, false); 679 } 680 681 static const struct iio_buffer_setup_ops st_lsm6dsx_buffer_ops = { 682 .preenable = st_lsm6dsx_buffer_preenable, 683 .postdisable = st_lsm6dsx_buffer_postdisable, 684 }; 685 686 int st_lsm6dsx_fifo_setup(struct st_lsm6dsx_hw *hw) 687 { 688 struct iio_buffer *buffer; 689 int i; 690 691 for (i = 0; i < ST_LSM6DSX_ID_MAX; i++) { 692 if (!hw->iio_devs[i]) 693 continue; 694 695 buffer = devm_iio_kfifo_allocate(hw->dev); 696 if (!buffer) 697 return -ENOMEM; 698 699 iio_device_attach_buffer(hw->iio_devs[i], buffer); 700 hw->iio_devs[i]->modes |= INDIO_BUFFER_SOFTWARE; 701 hw->iio_devs[i]->setup_ops = &st_lsm6dsx_buffer_ops; 702 } 703 704 return 0; 705 } 706