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, ®_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, ®_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, ®_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 ®_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 ®_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 ®_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, ®_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 ®_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 ®_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 ®_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 goto out; 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 goto out; 1404 } 1405 } 1406 1407 iio_push_to_buffers_with_timestamp(indio_dev, &data->buffer, 1408 iio_get_time_ns(indio_dev)); 1409 1410 out: 1411 iio_trigger_notify_done(indio_dev->trig); 1412 1413 return IRQ_HANDLED; 1414 } 1415 1416 static int bmi323_set_average(struct bmi323_data *data, 1417 enum bmi323_sensor_type sensor, int avg) 1418 { 1419 int raw = ARRAY_SIZE(bmi323_accel_gyro_avrg); 1420 1421 while (raw--) 1422 if (avg == bmi323_accel_gyro_avrg[raw]) 1423 break; 1424 if (raw < 0) 1425 return -EINVAL; 1426 1427 guard(mutex)(&data->mutex); 1428 return regmap_update_bits(data->regmap, bmi323_hw[sensor].config, 1429 BMI323_ACC_GYRO_CONF_AVG_MSK, 1430 FIELD_PREP(BMI323_ACC_GYRO_CONF_AVG_MSK, 1431 raw)); 1432 } 1433 1434 static int bmi323_get_average(struct bmi323_data *data, 1435 enum bmi323_sensor_type sensor, int *avg) 1436 { 1437 int ret, value, raw; 1438 1439 scoped_guard(mutex, &data->mutex) { 1440 ret = regmap_read(data->regmap, bmi323_hw[sensor].config, &value); 1441 if (ret) 1442 return ret; 1443 } 1444 1445 raw = FIELD_GET(BMI323_ACC_GYRO_CONF_AVG_MSK, value); 1446 *avg = bmi323_accel_gyro_avrg[raw]; 1447 1448 return IIO_VAL_INT; 1449 } 1450 1451 static int bmi323_enable_steps(struct bmi323_data *data, int val) 1452 { 1453 int ret; 1454 1455 guard(mutex)(&data->mutex); 1456 if (data->odrhz[BMI323_ACCEL] < 200) { 1457 dev_err(data->dev, "Invalid accelerometer parameter\n"); 1458 return -EINVAL; 1459 } 1460 1461 ret = bmi323_feature_engine_events(data, BMI323_FEAT_IO0_STP_CNT_MSK, 1462 val ? 1 : 0); 1463 if (ret) 1464 return ret; 1465 1466 set_mask_bits(&data->feature_events, BMI323_FEAT_IO0_STP_CNT_MSK, 1467 FIELD_PREP(BMI323_FEAT_IO0_STP_CNT_MSK, val ? 1 : 0)); 1468 1469 return 0; 1470 } 1471 1472 static int bmi323_read_steps(struct bmi323_data *data, int *val) 1473 { 1474 int ret; 1475 1476 guard(mutex)(&data->mutex); 1477 if (!FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK, data->feature_events)) 1478 return -EINVAL; 1479 1480 ret = regmap_bulk_read(data->regmap, BMI323_FEAT_IO2_REG, 1481 data->steps_count, 1482 ARRAY_SIZE(data->steps_count)); 1483 if (ret) 1484 return ret; 1485 1486 *val = get_unaligned_le32(data->steps_count); 1487 1488 return IIO_VAL_INT; 1489 } 1490 1491 static int bmi323_read_axis(struct bmi323_data *data, 1492 struct iio_chan_spec const *chan, int *val) 1493 { 1494 enum bmi323_sensor_type sensor; 1495 unsigned int value; 1496 u8 addr; 1497 int ret; 1498 1499 ret = bmi323_get_error_status(data); 1500 if (ret) 1501 return -EINVAL; 1502 1503 sensor = bmi323_iio_to_sensor(chan->type); 1504 addr = bmi323_hw[sensor].data + (chan->channel2 - IIO_MOD_X); 1505 1506 scoped_guard(mutex, &data->mutex) { 1507 ret = regmap_read(data->regmap, addr, &value); 1508 if (ret) 1509 return ret; 1510 } 1511 1512 *val = sign_extend32(value, chan->scan_type.realbits - 1); 1513 1514 return IIO_VAL_INT; 1515 } 1516 1517 static int bmi323_get_temp_data(struct bmi323_data *data, int *val) 1518 { 1519 unsigned int value; 1520 int ret; 1521 1522 ret = bmi323_get_error_status(data); 1523 if (ret) 1524 return -EINVAL; 1525 1526 scoped_guard(mutex, &data->mutex) { 1527 ret = regmap_read(data->regmap, BMI323_TEMP_REG, &value); 1528 if (ret) 1529 return ret; 1530 } 1531 1532 *val = sign_extend32(value, 15); 1533 1534 return IIO_VAL_INT; 1535 } 1536 1537 static int bmi323_get_odr(struct bmi323_data *data, 1538 enum bmi323_sensor_type sensor, int *odr, int *uodr) 1539 { 1540 int ret, value, odr_raw; 1541 1542 scoped_guard(mutex, &data->mutex) { 1543 ret = regmap_read(data->regmap, bmi323_hw[sensor].config, &value); 1544 if (ret) 1545 return ret; 1546 } 1547 1548 odr_raw = FIELD_GET(BMI323_ACC_GYRO_CONF_ODR_MSK, value); 1549 *odr = bmi323_acc_gyro_odr[odr_raw - 1][0]; 1550 *uodr = bmi323_acc_gyro_odr[odr_raw - 1][1]; 1551 1552 return IIO_VAL_INT_PLUS_MICRO; 1553 } 1554 1555 static int bmi323_configure_power_mode(struct bmi323_data *data, 1556 enum bmi323_sensor_type sensor, 1557 int odr_index) 1558 { 1559 enum bmi323_opr_mode mode; 1560 1561 if (bmi323_acc_gyro_odr[odr_index][0] > 25) 1562 mode = ACC_GYRO_MODE_CONTINOUS; 1563 else 1564 mode = ACC_GYRO_MODE_DUTYCYCLE; 1565 1566 return bmi323_set_mode(data, sensor, mode); 1567 } 1568 1569 static int bmi323_set_odr(struct bmi323_data *data, 1570 enum bmi323_sensor_type sensor, int odr, int uodr) 1571 { 1572 int odr_raw, ret; 1573 1574 odr_raw = ARRAY_SIZE(bmi323_acc_gyro_odr); 1575 1576 while (odr_raw--) 1577 if (odr == bmi323_acc_gyro_odr[odr_raw][0] && 1578 uodr == bmi323_acc_gyro_odr[odr_raw][1]) 1579 break; 1580 if (odr_raw < 0) 1581 return -EINVAL; 1582 1583 ret = bmi323_configure_power_mode(data, sensor, odr_raw); 1584 if (ret) 1585 return -EINVAL; 1586 1587 guard(mutex)(&data->mutex); 1588 data->odrhz[sensor] = bmi323_acc_gyro_odr[odr_raw][0]; 1589 data->odrns[sensor] = bmi323_acc_gyro_odrns[odr_raw]; 1590 1591 odr_raw++; 1592 1593 return regmap_update_bits(data->regmap, bmi323_hw[sensor].config, 1594 BMI323_ACC_GYRO_CONF_ODR_MSK, 1595 FIELD_PREP(BMI323_ACC_GYRO_CONF_ODR_MSK, 1596 odr_raw)); 1597 } 1598 1599 static int bmi323_get_scale(struct bmi323_data *data, 1600 enum bmi323_sensor_type sensor, int *val2) 1601 { 1602 int ret, value, scale_raw; 1603 1604 scoped_guard(mutex, &data->mutex) { 1605 ret = regmap_read(data->regmap, bmi323_hw[sensor].config, 1606 &value); 1607 if (ret) 1608 return ret; 1609 } 1610 1611 scale_raw = FIELD_GET(BMI323_ACC_GYRO_CONF_SCL_MSK, value); 1612 *val2 = bmi323_hw[sensor].scale_table[scale_raw][1]; 1613 1614 return IIO_VAL_INT_PLUS_MICRO; 1615 } 1616 1617 static int bmi323_set_scale(struct bmi323_data *data, 1618 enum bmi323_sensor_type sensor, int val, int val2) 1619 { 1620 int scale_raw; 1621 1622 scale_raw = bmi323_hw[sensor].scale_table_len; 1623 1624 while (scale_raw--) 1625 if (val == bmi323_hw[sensor].scale_table[scale_raw][0] && 1626 val2 == bmi323_hw[sensor].scale_table[scale_raw][1]) 1627 break; 1628 if (scale_raw < 0) 1629 return -EINVAL; 1630 1631 guard(mutex)(&data->mutex); 1632 return regmap_update_bits(data->regmap, bmi323_hw[sensor].config, 1633 BMI323_ACC_GYRO_CONF_SCL_MSK, 1634 FIELD_PREP(BMI323_ACC_GYRO_CONF_SCL_MSK, 1635 scale_raw)); 1636 } 1637 1638 static int bmi323_read_avail(struct iio_dev *indio_dev, 1639 struct iio_chan_spec const *chan, 1640 const int **vals, int *type, int *length, 1641 long mask) 1642 { 1643 enum bmi323_sensor_type sensor; 1644 1645 switch (mask) { 1646 case IIO_CHAN_INFO_SAMP_FREQ: 1647 *type = IIO_VAL_INT_PLUS_MICRO; 1648 *vals = (const int *)bmi323_acc_gyro_odr; 1649 *length = ARRAY_SIZE(bmi323_acc_gyro_odr) * 2; 1650 return IIO_AVAIL_LIST; 1651 case IIO_CHAN_INFO_SCALE: 1652 sensor = bmi323_iio_to_sensor(chan->type); 1653 *type = IIO_VAL_INT_PLUS_MICRO; 1654 *vals = (const int *)bmi323_hw[sensor].scale_table; 1655 *length = bmi323_hw[sensor].scale_table_len * 2; 1656 return IIO_AVAIL_LIST; 1657 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 1658 *type = IIO_VAL_INT; 1659 *vals = (const int *)bmi323_accel_gyro_avrg; 1660 *length = ARRAY_SIZE(bmi323_accel_gyro_avrg); 1661 return IIO_AVAIL_LIST; 1662 default: 1663 return -EINVAL; 1664 } 1665 } 1666 1667 static int bmi323_write_raw(struct iio_dev *indio_dev, 1668 struct iio_chan_spec const *chan, int val, 1669 int val2, long mask) 1670 { 1671 struct bmi323_data *data = iio_priv(indio_dev); 1672 1673 switch (mask) { 1674 case IIO_CHAN_INFO_SAMP_FREQ: 1675 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) 1676 return bmi323_set_odr(data, 1677 bmi323_iio_to_sensor(chan->type), 1678 val, val2); 1679 unreachable(); 1680 case IIO_CHAN_INFO_SCALE: 1681 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) 1682 return bmi323_set_scale(data, 1683 bmi323_iio_to_sensor(chan->type), 1684 val, val2); 1685 unreachable(); 1686 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 1687 iio_device_claim_direct_scoped(return -EBUSY, indio_dev) 1688 return bmi323_set_average(data, 1689 bmi323_iio_to_sensor(chan->type), 1690 val); 1691 unreachable(); 1692 case IIO_CHAN_INFO_ENABLE: 1693 return bmi323_enable_steps(data, val); 1694 case IIO_CHAN_INFO_PROCESSED: { 1695 guard(mutex)(&data->mutex); 1696 1697 if (val || !FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK, 1698 data->feature_events)) 1699 return -EINVAL; 1700 1701 /* Clear step counter value */ 1702 return bmi323_update_ext_reg(data, BMI323_STEP_SC1_REG, 1703 BMI323_STEP_SC1_RST_CNT_MSK, 1704 FIELD_PREP(BMI323_STEP_SC1_RST_CNT_MSK, 1705 1)); 1706 } 1707 default: 1708 return -EINVAL; 1709 } 1710 } 1711 1712 static int bmi323_read_raw(struct iio_dev *indio_dev, 1713 struct iio_chan_spec const *chan, int *val, 1714 int *val2, long mask) 1715 { 1716 struct bmi323_data *data = iio_priv(indio_dev); 1717 1718 switch (mask) { 1719 case IIO_CHAN_INFO_PROCESSED: 1720 return bmi323_read_steps(data, val); 1721 case IIO_CHAN_INFO_RAW: 1722 switch (chan->type) { 1723 case IIO_ACCEL: 1724 case IIO_ANGL_VEL: 1725 iio_device_claim_direct_scoped(return -EBUSY, 1726 indio_dev) 1727 return bmi323_read_axis(data, chan, val); 1728 unreachable(); 1729 case IIO_TEMP: 1730 return bmi323_get_temp_data(data, val); 1731 default: 1732 return -EINVAL; 1733 } 1734 case IIO_CHAN_INFO_SAMP_FREQ: 1735 return bmi323_get_odr(data, bmi323_iio_to_sensor(chan->type), 1736 val, val2); 1737 case IIO_CHAN_INFO_SCALE: 1738 switch (chan->type) { 1739 case IIO_ACCEL: 1740 case IIO_ANGL_VEL: 1741 *val = 0; 1742 return bmi323_get_scale(data, 1743 bmi323_iio_to_sensor(chan->type), 1744 val2); 1745 case IIO_TEMP: 1746 *val = BMI323_TEMP_SCALE / MEGA; 1747 *val2 = BMI323_TEMP_SCALE % MEGA; 1748 return IIO_VAL_INT_PLUS_MICRO; 1749 default: 1750 return -EINVAL; 1751 } 1752 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 1753 return bmi323_get_average(data, 1754 bmi323_iio_to_sensor(chan->type), 1755 val); 1756 case IIO_CHAN_INFO_OFFSET: 1757 switch (chan->type) { 1758 case IIO_TEMP: 1759 *val = BMI323_TEMP_OFFSET; 1760 return IIO_VAL_INT; 1761 default: 1762 return -EINVAL; 1763 } 1764 case IIO_CHAN_INFO_ENABLE: 1765 scoped_guard(mutex, &data->mutex) 1766 *val = FIELD_GET(BMI323_FEAT_IO0_STP_CNT_MSK, 1767 data->feature_events); 1768 return IIO_VAL_INT; 1769 default: 1770 return -EINVAL; 1771 } 1772 } 1773 1774 static const struct iio_info bmi323_info = { 1775 .read_raw = bmi323_read_raw, 1776 .write_raw = bmi323_write_raw, 1777 .read_avail = bmi323_read_avail, 1778 .hwfifo_set_watermark = bmi323_set_watermark, 1779 .write_event_config = bmi323_write_event_config, 1780 .read_event_config = bmi323_read_event_config, 1781 .write_event_value = bmi323_write_event_value, 1782 .read_event_value = bmi323_read_event_value, 1783 .event_attrs = &bmi323_event_attribute_group, 1784 }; 1785 1786 #define BMI323_SCAN_MASK_ACCEL_3AXIS \ 1787 (BIT(BMI323_ACCEL_X) | BIT(BMI323_ACCEL_Y) | BIT(BMI323_ACCEL_Z)) 1788 1789 #define BMI323_SCAN_MASK_GYRO_3AXIS \ 1790 (BIT(BMI323_GYRO_X) | BIT(BMI323_GYRO_Y) | BIT(BMI323_GYRO_Z)) 1791 1792 static const unsigned long bmi323_avail_scan_masks[] = { 1793 /* 3-axis accel */ 1794 BMI323_SCAN_MASK_ACCEL_3AXIS, 1795 /* 3-axis gyro */ 1796 BMI323_SCAN_MASK_GYRO_3AXIS, 1797 /* 3-axis accel + 3-axis gyro */ 1798 BMI323_SCAN_MASK_ACCEL_3AXIS | BMI323_SCAN_MASK_GYRO_3AXIS, 1799 0 1800 }; 1801 1802 static int bmi323_int_pin_config(struct bmi323_data *data, 1803 enum bmi323_irq_pin irq_pin, 1804 bool active_high, bool open_drain, bool latch) 1805 { 1806 unsigned int mask, field_value; 1807 int ret; 1808 1809 ret = regmap_update_bits(data->regmap, BMI323_IO_INT_CONF_REG, 1810 BMI323_IO_INT_LTCH_MSK, 1811 FIELD_PREP(BMI323_IO_INT_LTCH_MSK, latch)); 1812 if (ret) 1813 return ret; 1814 1815 ret = bmi323_update_ext_reg(data, BMI323_GEN_SET1_REG, 1816 BMI323_GEN_HOLD_DUR_MSK, 1817 FIELD_PREP(BMI323_GEN_HOLD_DUR_MSK, 0)); 1818 if (ret) 1819 return ret; 1820 1821 switch (irq_pin) { 1822 case BMI323_IRQ_INT1: 1823 mask = BMI323_IO_INT1_LVL_OD_OP_MSK; 1824 1825 field_value = FIELD_PREP(BMI323_IO_INT1_LVL_MSK, active_high) | 1826 FIELD_PREP(BMI323_IO_INT1_OD_MSK, open_drain) | 1827 FIELD_PREP(BMI323_IO_INT1_OP_EN_MSK, 1); 1828 break; 1829 case BMI323_IRQ_INT2: 1830 mask = BMI323_IO_INT2_LVL_OD_OP_MSK; 1831 1832 field_value = FIELD_PREP(BMI323_IO_INT2_LVL_MSK, active_high) | 1833 FIELD_PREP(BMI323_IO_INT2_OD_MSK, open_drain) | 1834 FIELD_PREP(BMI323_IO_INT2_OP_EN_MSK, 1); 1835 break; 1836 default: 1837 return -EINVAL; 1838 } 1839 1840 return regmap_update_bits(data->regmap, BMI323_IO_INT_CTR_REG, mask, 1841 field_value); 1842 } 1843 1844 static int bmi323_trigger_probe(struct bmi323_data *data, 1845 struct iio_dev *indio_dev) 1846 { 1847 bool open_drain, active_high, latch; 1848 struct fwnode_handle *fwnode; 1849 enum bmi323_irq_pin irq_pin; 1850 int ret, irq, irq_type; 1851 struct irq_data *desc; 1852 1853 fwnode = dev_fwnode(data->dev); 1854 if (!fwnode) 1855 return -ENODEV; 1856 1857 irq = fwnode_irq_get_byname(fwnode, "INT1"); 1858 if (irq > 0) { 1859 irq_pin = BMI323_IRQ_INT1; 1860 } else { 1861 irq = fwnode_irq_get_byname(fwnode, "INT2"); 1862 if (irq < 0) 1863 return 0; 1864 1865 irq_pin = BMI323_IRQ_INT2; 1866 } 1867 1868 desc = irq_get_irq_data(irq); 1869 if (!desc) 1870 return dev_err_probe(data->dev, -EINVAL, 1871 "Could not find IRQ %d\n", irq); 1872 1873 irq_type = irqd_get_trigger_type(desc); 1874 switch (irq_type) { 1875 case IRQF_TRIGGER_RISING: 1876 latch = false; 1877 active_high = true; 1878 break; 1879 case IRQF_TRIGGER_HIGH: 1880 latch = true; 1881 active_high = true; 1882 break; 1883 case IRQF_TRIGGER_FALLING: 1884 latch = false; 1885 active_high = false; 1886 break; 1887 case IRQF_TRIGGER_LOW: 1888 latch = true; 1889 active_high = false; 1890 break; 1891 default: 1892 return dev_err_probe(data->dev, -EINVAL, 1893 "Invalid interrupt type 0x%x specified\n", 1894 irq_type); 1895 } 1896 1897 open_drain = fwnode_property_read_bool(fwnode, "drive-open-drain"); 1898 1899 ret = bmi323_int_pin_config(data, irq_pin, active_high, open_drain, 1900 latch); 1901 if (ret) 1902 return dev_err_probe(data->dev, ret, 1903 "Failed to configure irq line\n"); 1904 1905 data->trig = devm_iio_trigger_alloc(data->dev, "%s-trig-%d", 1906 indio_dev->name, irq_pin); 1907 if (!data->trig) 1908 return -ENOMEM; 1909 1910 data->trig->ops = &bmi323_trigger_ops; 1911 iio_trigger_set_drvdata(data->trig, data); 1912 1913 ret = devm_request_threaded_irq(data->dev, irq, NULL, 1914 bmi323_irq_thread_handler, 1915 IRQF_ONESHOT, "bmi323-int", indio_dev); 1916 if (ret) 1917 return dev_err_probe(data->dev, ret, "Failed to request IRQ\n"); 1918 1919 ret = devm_iio_trigger_register(data->dev, data->trig); 1920 if (ret) 1921 return dev_err_probe(data->dev, ret, 1922 "Trigger registration failed\n"); 1923 1924 data->irq_pin = irq_pin; 1925 1926 return 0; 1927 } 1928 1929 static int bmi323_feature_engine_enable(struct bmi323_data *data, bool en) 1930 { 1931 unsigned int feature_status; 1932 int ret; 1933 1934 if (!en) 1935 return regmap_write(data->regmap, BMI323_FEAT_CTRL_REG, 0); 1936 1937 ret = regmap_write(data->regmap, BMI323_FEAT_IO2_REG, 0x012c); 1938 if (ret) 1939 return ret; 1940 1941 ret = regmap_write(data->regmap, BMI323_FEAT_IO_STATUS_REG, 1942 BMI323_FEAT_IO_STATUS_MSK); 1943 if (ret) 1944 return ret; 1945 1946 ret = regmap_write(data->regmap, BMI323_FEAT_CTRL_REG, 1947 BMI323_FEAT_ENG_EN_MSK); 1948 if (ret) 1949 return ret; 1950 1951 /* 1952 * It takes around 4 msec to enable the Feature engine, so check 1953 * the status of the feature engine every 2 msec for a maximum 1954 * of 5 trials. 1955 */ 1956 ret = regmap_read_poll_timeout(data->regmap, BMI323_FEAT_IO1_REG, 1957 feature_status, 1958 FIELD_GET(BMI323_FEAT_IO1_ERR_MSK, 1959 feature_status) == 1, 1960 BMI323_FEAT_ENG_POLL, 1961 BMI323_FEAT_ENG_TIMEOUT); 1962 if (ret) 1963 return dev_err_probe(data->dev, -EINVAL, 1964 "Failed to enable feature engine\n"); 1965 1966 return 0; 1967 } 1968 1969 static void bmi323_disable(void *data_ptr) 1970 { 1971 struct bmi323_data *data = data_ptr; 1972 1973 bmi323_set_mode(data, BMI323_ACCEL, ACC_GYRO_MODE_DISABLE); 1974 bmi323_set_mode(data, BMI323_GYRO, ACC_GYRO_MODE_DISABLE); 1975 } 1976 1977 static int bmi323_set_bw(struct bmi323_data *data, 1978 enum bmi323_sensor_type sensor, enum bmi323_3db_bw bw) 1979 { 1980 return regmap_update_bits(data->regmap, bmi323_hw[sensor].config, 1981 BMI323_ACC_GYRO_CONF_BW_MSK, 1982 FIELD_PREP(BMI323_ACC_GYRO_CONF_BW_MSK, bw)); 1983 } 1984 1985 static int bmi323_init(struct bmi323_data *data) 1986 { 1987 int ret, val; 1988 1989 /* 1990 * Perform soft reset to make sure the device is in a known state after 1991 * start up. A delay of 1.5 ms is required after reset. 1992 * See datasheet section 5.17 "Soft Reset". 1993 */ 1994 ret = regmap_write(data->regmap, BMI323_CMD_REG, BMI323_RST_VAL); 1995 if (ret) 1996 return ret; 1997 1998 usleep_range(1500, 2000); 1999 2000 /* 2001 * Dummy read is required to enable SPI interface after reset. 2002 * See datasheet section 7.2.1 "Protocol Selection". 2003 */ 2004 regmap_read(data->regmap, BMI323_CHIP_ID_REG, &val); 2005 2006 ret = regmap_read(data->regmap, BMI323_STATUS_REG, &val); 2007 if (ret) 2008 return ret; 2009 2010 if (!FIELD_GET(BMI323_STATUS_POR_MSK, val)) 2011 return dev_err_probe(data->dev, -EINVAL, 2012 "Sensor initialization error\n"); 2013 2014 ret = regmap_read(data->regmap, BMI323_CHIP_ID_REG, &val); 2015 if (ret) 2016 return ret; 2017 2018 if (FIELD_GET(BMI323_CHIP_ID_MSK, val) != BMI323_CHIP_ID_VAL) 2019 return dev_err_probe(data->dev, -EINVAL, "Chip ID mismatch\n"); 2020 2021 ret = bmi323_feature_engine_enable(data, true); 2022 if (ret) 2023 return ret; 2024 2025 ret = regmap_read(data->regmap, BMI323_ERR_REG, &val); 2026 if (ret) 2027 return ret; 2028 2029 if (val) 2030 return dev_err_probe(data->dev, -EINVAL, 2031 "Sensor power error = 0x%x\n", val); 2032 2033 /* 2034 * Set the Bandwidth coefficient which defines the 3 dB cutoff 2035 * frequency in relation to the ODR. 2036 */ 2037 ret = bmi323_set_bw(data, BMI323_ACCEL, BMI323_BW_ODR_BY_2); 2038 if (ret) 2039 return ret; 2040 2041 ret = bmi323_set_bw(data, BMI323_GYRO, BMI323_BW_ODR_BY_2); 2042 if (ret) 2043 return ret; 2044 2045 ret = bmi323_set_odr(data, BMI323_ACCEL, 25, 0); 2046 if (ret) 2047 return ret; 2048 2049 ret = bmi323_set_odr(data, BMI323_GYRO, 25, 0); 2050 if (ret) 2051 return ret; 2052 2053 return devm_add_action_or_reset(data->dev, bmi323_disable, data); 2054 } 2055 2056 int bmi323_core_probe(struct device *dev) 2057 { 2058 static const char * const regulator_names[] = { "vdd", "vddio" }; 2059 struct iio_dev *indio_dev; 2060 struct bmi323_data *data; 2061 struct regmap *regmap; 2062 int ret; 2063 2064 regmap = dev_get_regmap(dev, NULL); 2065 if (!regmap) 2066 return dev_err_probe(dev, -ENODEV, "Failed to get regmap\n"); 2067 2068 indio_dev = devm_iio_device_alloc(dev, sizeof(*data)); 2069 if (!indio_dev) 2070 return dev_err_probe(dev, -ENOMEM, 2071 "Failed to allocate device\n"); 2072 2073 ret = devm_regulator_bulk_get_enable(dev, ARRAY_SIZE(regulator_names), 2074 regulator_names); 2075 if (ret) 2076 return dev_err_probe(dev, ret, "Failed to enable regulators\n"); 2077 2078 data = iio_priv(indio_dev); 2079 data->dev = dev; 2080 data->regmap = regmap; 2081 mutex_init(&data->mutex); 2082 2083 ret = bmi323_init(data); 2084 if (ret) 2085 return -EINVAL; 2086 2087 if (!iio_read_acpi_mount_matrix(dev, &data->orientation, "ROTM")) { 2088 ret = iio_read_mount_matrix(dev, &data->orientation); 2089 if (ret) 2090 return ret; 2091 } 2092 2093 indio_dev->name = "bmi323-imu"; 2094 indio_dev->info = &bmi323_info; 2095 indio_dev->channels = bmi323_channels; 2096 indio_dev->num_channels = ARRAY_SIZE(bmi323_channels); 2097 indio_dev->available_scan_masks = bmi323_avail_scan_masks; 2098 indio_dev->modes = INDIO_DIRECT_MODE | INDIO_BUFFER_SOFTWARE; 2099 dev_set_drvdata(data->dev, indio_dev); 2100 2101 ret = bmi323_trigger_probe(data, indio_dev); 2102 if (ret) 2103 return -EINVAL; 2104 2105 ret = devm_iio_triggered_buffer_setup_ext(data->dev, indio_dev, 2106 &iio_pollfunc_store_time, 2107 bmi323_trigger_handler, 2108 IIO_BUFFER_DIRECTION_IN, 2109 &bmi323_buffer_ops, 2110 bmi323_fifo_attributes); 2111 if (ret) 2112 return dev_err_probe(data->dev, ret, 2113 "Failed to setup trigger buffer\n"); 2114 2115 ret = devm_iio_device_register(data->dev, indio_dev); 2116 if (ret) 2117 return dev_err_probe(data->dev, ret, 2118 "Unable to register iio device\n"); 2119 2120 return 0; 2121 } 2122 EXPORT_SYMBOL_NS_GPL(bmi323_core_probe, IIO_BMI323); 2123 2124 MODULE_DESCRIPTION("Bosch BMI323 IMU driver"); 2125 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>"); 2126 MODULE_LICENSE("GPL"); 2127