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