1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * BM1390 ROHM pressure sensor 4 * 5 * Copyright (c) 2023, ROHM Semiconductor. 6 * https://fscdn.rohm.com/en/products/databook/datasheet/ic/sensor/pressure/bm1390glv-z-e.pdf 7 */ 8 9 #include <linux/bitfield.h> 10 #include <linux/bits.h> 11 #include <linux/device.h> 12 #include <linux/i2c.h> 13 #include <linux/module.h> 14 #include <linux/regmap.h> 15 #include <linux/regulator/consumer.h> 16 17 #include <linux/iio/iio.h> 18 #include <linux/iio/trigger.h> 19 #include <linux/iio/trigger_consumer.h> 20 #include <linux/iio/triggered_buffer.h> 21 22 #define BM1390_REG_MANUFACT_ID 0x0f 23 #define BM1390_REG_PART_ID 0x10 24 #define BM1390_REG_POWER 0x12 25 #define BM1390_MASK_POWER BIT(0) 26 #define BM1390_POWER_ON BM1390_MASK_POWER 27 #define BM1390_POWER_OFF 0x00 28 #define BM1390_REG_RESET 0x13 29 #define BM1390_MASK_RESET BIT(0) 30 #define BM1390_RESET_RELEASE BM1390_MASK_RESET 31 #define BM1390_RESET 0x00 32 #define BM1390_REG_MODE_CTRL 0x14 33 #define BM1390_MASK_MEAS_MODE GENMASK(1, 0) 34 #define BM1390_MASK_DRDY_EN BIT(4) 35 #define BM1390_MASK_WMI_EN BIT(2) 36 #define BM1390_MASK_AVE_NUM GENMASK(7, 5) 37 38 /* 39 * Data-sheet states that when the IIR is used, the AVE_NUM must be set to 40 * value 110b 41 */ 42 #define BM1390_IIR_AVE_NUM 0x06 43 #define BM1390_REG_FIFO_CTRL 0x15 44 #define BM1390_MASK_IIR_MODE GENMASK(1, 0) 45 #define BM1390_IIR_MODE_OFF 0x0 46 #define BM1390_IIR_MODE_WEAK 0x1 47 #define BM1390_IIR_MODE_MID 0x2 48 #define BM1390_IIR_MODE_STRONG 0x3 49 50 #define BM1390_MASK_FIFO_LEN BIT(6) 51 #define BM1390_MASK_FIFO_EN BIT(7) 52 #define BM1390_WMI_MIN 2 53 #define BM1390_WMI_MAX 3 54 55 #define BM1390_REG_FIFO_LVL 0x18 56 #define BM1390_MASK_FIFO_LVL GENMASK(2, 0) 57 #define BM1390_REG_STATUS 0x19 58 #define BM1390_REG_PRESSURE_BASE 0x1a 59 #define BM1390_REG_TEMP_HI 0x1d 60 #define BM1390_REG_TEMP_LO 0x1e 61 #define BM1390_MAX_REGISTER BM1390_REG_TEMP_LO 62 63 #define BM1390_ID 0x34 64 65 /* Regmap configs */ 66 static const struct regmap_range bm1390_volatile_ranges[] = { 67 { 68 .range_min = BM1390_REG_STATUS, 69 .range_max = BM1390_REG_STATUS, 70 }, 71 { 72 .range_min = BM1390_REG_FIFO_LVL, 73 .range_max = BM1390_REG_TEMP_LO, 74 }, 75 }; 76 77 static const struct regmap_access_table bm1390_volatile_regs = { 78 .yes_ranges = &bm1390_volatile_ranges[0], 79 .n_yes_ranges = ARRAY_SIZE(bm1390_volatile_ranges), 80 }; 81 82 static const struct regmap_range bm1390_precious_ranges[] = { 83 { 84 .range_min = BM1390_REG_STATUS, 85 .range_max = BM1390_REG_STATUS, 86 }, 87 }; 88 89 static const struct regmap_access_table bm1390_precious_regs = { 90 .yes_ranges = &bm1390_precious_ranges[0], 91 .n_yes_ranges = ARRAY_SIZE(bm1390_precious_ranges), 92 }; 93 94 static const struct regmap_range bm1390_read_only_ranges[] = { 95 { 96 .range_min = BM1390_REG_MANUFACT_ID, 97 .range_max = BM1390_REG_PART_ID, 98 }, { 99 .range_min = BM1390_REG_FIFO_LVL, 100 .range_max = BM1390_REG_TEMP_LO, 101 }, 102 }; 103 104 static const struct regmap_access_table bm1390_ro_regs = { 105 .no_ranges = &bm1390_read_only_ranges[0], 106 .n_no_ranges = ARRAY_SIZE(bm1390_read_only_ranges), 107 }; 108 109 static const struct regmap_range bm1390_noinc_read_ranges[] = { 110 { 111 .range_min = BM1390_REG_PRESSURE_BASE, 112 .range_max = BM1390_REG_TEMP_LO, 113 }, 114 }; 115 116 static const struct regmap_access_table bm1390_nir_regs = { 117 .yes_ranges = &bm1390_noinc_read_ranges[0], 118 .n_yes_ranges = ARRAY_SIZE(bm1390_noinc_read_ranges), 119 }; 120 121 static const struct regmap_config bm1390_regmap = { 122 .reg_bits = 8, 123 .val_bits = 8, 124 .volatile_table = &bm1390_volatile_regs, 125 .wr_table = &bm1390_ro_regs, 126 .rd_noinc_table = &bm1390_nir_regs, 127 .precious_table = &bm1390_precious_regs, 128 .max_register = BM1390_MAX_REGISTER, 129 .cache_type = REGCACHE_RBTREE, 130 .disable_locking = true, 131 }; 132 133 enum { 134 BM1390_STATE_SAMPLE, 135 BM1390_STATE_FIFO, 136 }; 137 138 struct bm1390_data_buf { 139 u32 pressure; 140 __be16 temp; 141 s64 ts __aligned(8); 142 }; 143 144 /* BM1390 has FIFO for 4 pressure samples */ 145 #define BM1390_FIFO_LENGTH 4 146 147 struct bm1390_data { 148 s64 timestamp, old_timestamp; 149 struct iio_trigger *trig; 150 struct regmap *regmap; 151 struct device *dev; 152 struct bm1390_data_buf buf; 153 int irq; 154 unsigned int state; 155 bool trigger_enabled; 156 u8 watermark; 157 158 /* Prevent accessing sensor during FIFO read sequence */ 159 struct mutex mutex; 160 }; 161 162 enum { 163 BM1390_CHAN_PRESSURE, 164 BM1390_CHAN_TEMP, 165 }; 166 167 static const struct iio_chan_spec bm1390_channels[] = { 168 { 169 .type = IIO_PRESSURE, 170 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 171 /* 172 * When IIR is used, we must fix amount of averaged samples. 173 * Thus we don't allow setting oversampling ratio. 174 */ 175 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 176 .scan_index = BM1390_CHAN_PRESSURE, 177 .scan_type = { 178 .sign = 'u', 179 .realbits = 22, 180 .storagebits = 32, 181 .endianness = IIO_LE, 182 }, 183 }, 184 { 185 .type = IIO_TEMP, 186 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), 187 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), 188 .scan_index = BM1390_CHAN_TEMP, 189 .scan_type = { 190 .sign = 's', 191 .realbits = 16, 192 .storagebits = 16, 193 .endianness = IIO_BE, 194 }, 195 }, 196 IIO_CHAN_SOFT_TIMESTAMP(2), 197 }; 198 199 /* 200 * We can't skip reading the pressure because the watermark IRQ is acked 201 * only when the pressure data is read from the FIFO. 202 */ 203 static const unsigned long bm1390_scan_masks[] = { 204 BIT(BM1390_CHAN_PRESSURE), 205 BIT(BM1390_CHAN_PRESSURE) | BIT(BM1390_CHAN_TEMP), 206 0 207 }; 208 209 static int bm1390_read_temp(struct bm1390_data *data, int *temp) 210 { 211 __be16 temp_raw; 212 int ret; 213 214 ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI, &temp_raw, 215 sizeof(temp_raw)); 216 if (ret) 217 return ret; 218 219 *temp = be16_to_cpu(temp_raw); 220 221 return 0; 222 } 223 224 static int bm1390_pressure_read(struct bm1390_data *data, u32 *pressure) 225 { 226 /* Pressure data is in 3 8-bit registers */ 227 u8 raw[3]; 228 int ret; 229 230 ret = regmap_bulk_read(data->regmap, BM1390_REG_PRESSURE_BASE, 231 raw, sizeof(raw)); 232 if (ret < 0) 233 return ret; 234 235 *pressure = (u32)(raw[2] >> 2 | raw[1] << 6 | raw[0] << 14); 236 237 return 0; 238 } 239 240 /* The enum values map directly to register bits */ 241 enum bm1390_meas_mode { 242 BM1390_MEAS_MODE_STOP = 0x0, 243 BM1390_MEAS_MODE_1SHOT = 0x1, 244 BM1390_MEAS_MODE_CONTINUOUS = 0x2, 245 }; 246 247 static int bm1390_meas_set(struct bm1390_data *data, enum bm1390_meas_mode mode) 248 { 249 return regmap_update_bits(data->regmap, BM1390_REG_MODE_CTRL, 250 BM1390_MASK_MEAS_MODE, mode); 251 } 252 253 /* 254 * If the trigger is not used we just wait until the measurement has 255 * completed. The data-sheet says maximum measurement cycle (regardless 256 * the AVE_NUM) is 200 mS so let's just sleep at least that long. If speed 257 * is needed the trigger should be used. 258 */ 259 #define BM1390_MAX_MEAS_TIME_MS 205 260 261 static int bm1390_read_data(struct bm1390_data *data, 262 struct iio_chan_spec const *chan, int *val, int *val2) 263 { 264 int ret, warn; 265 266 mutex_lock(&data->mutex); 267 /* 268 * We use 'continuous mode' even for raw read because according to the 269 * data-sheet an one-shot mode can't be used with IIR filter. 270 */ 271 ret = bm1390_meas_set(data, BM1390_MEAS_MODE_CONTINUOUS); 272 if (ret) 273 goto unlock_out; 274 275 switch (chan->type) { 276 case IIO_PRESSURE: 277 msleep(BM1390_MAX_MEAS_TIME_MS); 278 ret = bm1390_pressure_read(data, val); 279 break; 280 case IIO_TEMP: 281 msleep(BM1390_MAX_MEAS_TIME_MS); 282 ret = bm1390_read_temp(data, val); 283 break; 284 default: 285 ret = -EINVAL; 286 } 287 warn = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP); 288 if (warn) 289 dev_warn(data->dev, "Failed to stop measurement (%d)\n", warn); 290 unlock_out: 291 mutex_unlock(&data->mutex); 292 293 return ret; 294 } 295 296 static int bm1390_read_raw(struct iio_dev *idev, 297 struct iio_chan_spec const *chan, 298 int *val, int *val2, long mask) 299 { 300 struct bm1390_data *data = iio_priv(idev); 301 int ret; 302 303 switch (mask) { 304 case IIO_CHAN_INFO_SCALE: 305 if (chan->type == IIO_TEMP) { 306 *val = 31; 307 *val2 = 250000; 308 309 return IIO_VAL_INT_PLUS_MICRO; 310 } else if (chan->type == IIO_PRESSURE) { 311 /* 312 * pressure in hPa is register value divided by 2048. 313 * This means kPa is 1/20480 times the register value, 314 */ 315 *val = 1; 316 *val2 = 2048; 317 318 return IIO_VAL_FRACTIONAL; 319 } 320 321 return -EINVAL; 322 case IIO_CHAN_INFO_RAW: 323 ret = iio_device_claim_direct_mode(idev); 324 if (ret) 325 return ret; 326 327 ret = bm1390_read_data(data, chan, val, val2); 328 iio_device_release_direct_mode(idev); 329 if (ret) 330 return ret; 331 332 return IIO_VAL_INT; 333 default: 334 return -EINVAL; 335 } 336 } 337 338 static int __bm1390_fifo_flush(struct iio_dev *idev, unsigned int samples, 339 s64 timestamp) 340 { 341 /* BM1390_FIFO_LENGTH is small so we shouldn't run out of stack */ 342 struct bm1390_data_buf buffer[BM1390_FIFO_LENGTH]; 343 struct bm1390_data *data = iio_priv(idev); 344 int smp_lvl, ret, i, warn, dummy; 345 u64 sample_period; 346 __be16 temp = 0; 347 348 ret = regmap_read(data->regmap, BM1390_REG_FIFO_LVL, &smp_lvl); 349 if (ret) 350 return ret; 351 352 smp_lvl = FIELD_GET(BM1390_MASK_FIFO_LVL, smp_lvl); 353 if (!smp_lvl) 354 return 0; 355 356 if (smp_lvl > BM1390_FIFO_LENGTH) { 357 /* 358 * The fifo holds maximum of 4 samples so valid values 359 * should be 0, 1, 2, 3, 4 - rest are probably bit errors 360 * in I2C line. Don't overflow if this happens. 361 */ 362 dev_err(data->dev, "bad FIFO level %d\n", smp_lvl); 363 smp_lvl = BM1390_FIFO_LENGTH; 364 } 365 366 sample_period = timestamp - data->old_timestamp; 367 do_div(sample_period, smp_lvl); 368 369 if (samples && smp_lvl > samples) 370 smp_lvl = samples; 371 372 373 /* 374 * After some testing it appears that the temperature is not readable 375 * until the FIFO access has been done after the WMI. Thus, we need 376 * to read the all pressure values to memory and read the temperature 377 * only after that. 378 */ 379 for (i = 0; i < smp_lvl; i++) { 380 /* 381 * When we start reading data from the FIFO the sensor goes to 382 * special FIFO reading mode. If any other register is accessed 383 * during the FIFO read, samples can be dropped. Prevent access 384 * until FIFO_LVL is read. We have mutex locked and we do also 385 * go performing reading of FIFO_LVL even if this read fails. 386 */ 387 if (test_bit(BM1390_CHAN_PRESSURE, idev->active_scan_mask)) { 388 ret = bm1390_pressure_read(data, &buffer[i].pressure); 389 if (ret) 390 break; 391 } 392 393 /* 394 * Old timestamp is either the previous sample IRQ time, 395 * previous flush-time or, if this was first sample, the enable 396 * time. When we add a sample period to that we should get the 397 * best approximation of the time-stamp we are handling. 398 * 399 * Idea is to always keep the "old_timestamp" matching the 400 * timestamp which we are currently handling. 401 */ 402 data->old_timestamp += sample_period; 403 buffer[i].ts = data->old_timestamp; 404 } 405 /* Reading the FIFO_LVL closes the FIFO access sequence */ 406 warn = regmap_read(data->regmap, BM1390_REG_FIFO_LVL, &dummy); 407 if (warn) 408 dev_warn(data->dev, "Closing FIFO sequence failed\n"); 409 410 if (ret) 411 return ret; 412 413 if (test_bit(BM1390_CHAN_TEMP, idev->active_scan_mask)) { 414 ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI, &temp, 415 sizeof(temp)); 416 if (ret) 417 return ret; 418 } 419 420 for (i = 0; i < smp_lvl; i++) { 421 buffer[i].temp = temp; 422 iio_push_to_buffers(idev, &buffer[i]); 423 } 424 425 return smp_lvl; 426 } 427 428 static int bm1390_fifo_flush(struct iio_dev *idev, unsigned int samples) 429 { 430 struct bm1390_data *data = iio_priv(idev); 431 s64 timestamp; 432 int ret; 433 434 /* 435 * If fifo_flush is being called from IRQ handler we know the stored 436 * timestamp is fairly accurate for the last stored sample. If we are 437 * called as a result of a read operation from userspace and hence 438 * before the watermark interrupt was triggered, take a timestamp 439 * now. We can fall anywhere in between two samples so the error in this 440 * case is at most one sample period. 441 * We need to have the IRQ disabled or we risk of messing-up 442 * the timestamps. If we are ran from IRQ, then the 443 * IRQF_ONESHOT has us covered - but if we are ran by the 444 * user-space read we need to disable the IRQ to be on a safe 445 * side. We do this usng synchronous disable so that if the 446 * IRQ thread is being ran on other CPU we wait for it to be 447 * finished. 448 */ 449 450 timestamp = iio_get_time_ns(idev); 451 mutex_lock(&data->mutex); 452 ret = __bm1390_fifo_flush(idev, samples, timestamp); 453 mutex_unlock(&data->mutex); 454 455 return ret; 456 } 457 458 static int bm1390_set_watermark(struct iio_dev *idev, unsigned int val) 459 { 460 struct bm1390_data *data = iio_priv(idev); 461 462 if (val < BM1390_WMI_MIN || val > BM1390_WMI_MAX) 463 return -EINVAL; 464 465 mutex_lock(&data->mutex); 466 data->watermark = val; 467 mutex_unlock(&data->mutex); 468 469 return 0; 470 } 471 472 static const struct iio_info bm1390_noirq_info = { 473 .read_raw = &bm1390_read_raw, 474 }; 475 476 static const struct iio_info bm1390_info = { 477 .read_raw = &bm1390_read_raw, 478 .hwfifo_set_watermark = bm1390_set_watermark, 479 .hwfifo_flush_to_buffer = bm1390_fifo_flush, 480 }; 481 482 static int bm1390_chip_init(struct bm1390_data *data) 483 { 484 int ret; 485 486 ret = regmap_write_bits(data->regmap, BM1390_REG_POWER, 487 BM1390_MASK_POWER, BM1390_POWER_ON); 488 if (ret) 489 return ret; 490 491 msleep(1); 492 493 ret = regmap_write_bits(data->regmap, BM1390_REG_RESET, 494 BM1390_MASK_RESET, BM1390_RESET); 495 if (ret) 496 return ret; 497 498 msleep(1); 499 500 ret = regmap_write_bits(data->regmap, BM1390_REG_RESET, 501 BM1390_MASK_RESET, BM1390_RESET_RELEASE); 502 if (ret) 503 return ret; 504 505 msleep(1); 506 507 ret = regmap_reinit_cache(data->regmap, &bm1390_regmap); 508 if (ret) { 509 dev_err(data->dev, "Failed to reinit reg cache\n"); 510 return ret; 511 } 512 513 /* 514 * Default to use IIR filter in "middle" mode. Also the AVE_NUM must 515 * be fixed when IIR is in use. 516 */ 517 ret = regmap_update_bits(data->regmap, BM1390_REG_MODE_CTRL, 518 BM1390_MASK_AVE_NUM, BM1390_IIR_AVE_NUM); 519 if (ret) 520 return ret; 521 522 return regmap_update_bits(data->regmap, BM1390_REG_FIFO_CTRL, 523 BM1390_MASK_IIR_MODE, BM1390_IIR_MODE_MID); 524 } 525 526 static int bm1390_fifo_set_wmi(struct bm1390_data *data) 527 { 528 u8 regval; 529 530 regval = FIELD_PREP(BM1390_MASK_FIFO_LEN, 531 data->watermark - BM1390_WMI_MIN); 532 533 return regmap_update_bits(data->regmap, BM1390_REG_FIFO_CTRL, 534 BM1390_MASK_FIFO_LEN, regval); 535 } 536 537 static int bm1390_fifo_enable(struct iio_dev *idev) 538 { 539 struct bm1390_data *data = iio_priv(idev); 540 int ret; 541 542 /* We can't do buffered stuff without IRQ as we never get WMI */ 543 if (data->irq <= 0) 544 return -EINVAL; 545 546 mutex_lock(&data->mutex); 547 if (data->trigger_enabled) { 548 ret = -EBUSY; 549 goto unlock_out; 550 } 551 552 /* Update watermark to HW */ 553 ret = bm1390_fifo_set_wmi(data); 554 if (ret) 555 goto unlock_out; 556 557 /* Enable WMI_IRQ */ 558 ret = regmap_set_bits(data->regmap, BM1390_REG_MODE_CTRL, 559 BM1390_MASK_WMI_EN); 560 if (ret) 561 goto unlock_out; 562 563 /* Enable FIFO */ 564 ret = regmap_set_bits(data->regmap, BM1390_REG_FIFO_CTRL, 565 BM1390_MASK_FIFO_EN); 566 if (ret) 567 goto unlock_out; 568 569 data->state = BM1390_STATE_FIFO; 570 571 data->old_timestamp = iio_get_time_ns(idev); 572 ret = bm1390_meas_set(data, BM1390_MEAS_MODE_CONTINUOUS); 573 574 unlock_out: 575 mutex_unlock(&data->mutex); 576 577 return ret; 578 } 579 580 static int bm1390_fifo_disable(struct iio_dev *idev) 581 { 582 struct bm1390_data *data = iio_priv(idev); 583 int ret; 584 585 msleep(1); 586 587 mutex_lock(&data->mutex); 588 ret = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP); 589 if (ret) 590 goto unlock_out; 591 592 /* Disable FIFO */ 593 ret = regmap_clear_bits(data->regmap, BM1390_REG_FIFO_CTRL, 594 BM1390_MASK_FIFO_EN); 595 if (ret) 596 goto unlock_out; 597 598 data->state = BM1390_STATE_SAMPLE; 599 600 /* Disable WMI_IRQ */ 601 ret = regmap_clear_bits(data->regmap, BM1390_REG_MODE_CTRL, 602 BM1390_MASK_WMI_EN); 603 604 unlock_out: 605 mutex_unlock(&data->mutex); 606 607 return ret; 608 } 609 610 static int bm1390_buffer_postenable(struct iio_dev *idev) 611 { 612 /* 613 * If we use data-ready trigger, then the IRQ masks should be handled by 614 * trigger enable and the hardware buffer is not used but we just update 615 * results to the IIO FIFO when data-ready triggers. 616 */ 617 if (iio_device_get_current_mode(idev) == INDIO_BUFFER_TRIGGERED) 618 return 0; 619 620 return bm1390_fifo_enable(idev); 621 } 622 623 static int bm1390_buffer_predisable(struct iio_dev *idev) 624 { 625 if (iio_device_get_current_mode(idev) == INDIO_BUFFER_TRIGGERED) 626 return 0; 627 628 return bm1390_fifo_disable(idev); 629 } 630 631 static const struct iio_buffer_setup_ops bm1390_buffer_ops = { 632 .postenable = bm1390_buffer_postenable, 633 .predisable = bm1390_buffer_predisable, 634 }; 635 636 static irqreturn_t bm1390_trigger_handler(int irq, void *p) 637 { 638 struct iio_poll_func *pf = p; 639 struct iio_dev *idev = pf->indio_dev; 640 struct bm1390_data *data = iio_priv(idev); 641 int ret, status; 642 643 /* DRDY is acked by reading status reg */ 644 ret = regmap_read(data->regmap, BM1390_REG_STATUS, &status); 645 if (ret || !status) 646 return IRQ_NONE; 647 648 dev_dbg(data->dev, "DRDY trig status 0x%x\n", status); 649 650 if (test_bit(BM1390_CHAN_PRESSURE, idev->active_scan_mask)) { 651 ret = bm1390_pressure_read(data, &data->buf.pressure); 652 if (ret) { 653 dev_warn(data->dev, "sample read failed %d\n", ret); 654 return IRQ_NONE; 655 } 656 } 657 658 if (test_bit(BM1390_CHAN_TEMP, idev->active_scan_mask)) { 659 ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI, 660 &data->buf.temp, sizeof(data->buf.temp)); 661 if (ret) { 662 dev_warn(data->dev, "temp read failed %d\n", ret); 663 return IRQ_HANDLED; 664 } 665 } 666 667 iio_push_to_buffers_with_timestamp(idev, &data->buf, data->timestamp); 668 iio_trigger_notify_done(idev->trig); 669 670 return IRQ_HANDLED; 671 } 672 673 /* Get timestamps and wake the thread if we need to read data */ 674 static irqreturn_t bm1390_irq_handler(int irq, void *private) 675 { 676 struct iio_dev *idev = private; 677 struct bm1390_data *data = iio_priv(idev); 678 679 data->timestamp = iio_get_time_ns(idev); 680 681 if (data->state == BM1390_STATE_FIFO || data->trigger_enabled) 682 return IRQ_WAKE_THREAD; 683 684 return IRQ_NONE; 685 } 686 687 static irqreturn_t bm1390_irq_thread_handler(int irq, void *private) 688 { 689 struct iio_dev *idev = private; 690 struct bm1390_data *data = iio_priv(idev); 691 int ret = IRQ_NONE; 692 693 mutex_lock(&data->mutex); 694 695 if (data->trigger_enabled) { 696 iio_trigger_poll_nested(data->trig); 697 ret = IRQ_HANDLED; 698 } else if (data->state == BM1390_STATE_FIFO) { 699 int ok; 700 701 ok = __bm1390_fifo_flush(idev, BM1390_FIFO_LENGTH, 702 data->timestamp); 703 if (ok > 0) 704 ret = IRQ_HANDLED; 705 } 706 707 mutex_unlock(&data->mutex); 708 709 return ret; 710 } 711 712 static int bm1390_set_drdy_irq(struct bm1390_data *data, bool en) 713 { 714 if (en) 715 return regmap_set_bits(data->regmap, BM1390_REG_MODE_CTRL, 716 BM1390_MASK_DRDY_EN); 717 return regmap_clear_bits(data->regmap, BM1390_REG_MODE_CTRL, 718 BM1390_MASK_DRDY_EN); 719 } 720 721 static int bm1390_trigger_set_state(struct iio_trigger *trig, 722 bool state) 723 { 724 struct bm1390_data *data = iio_trigger_get_drvdata(trig); 725 int ret = 0; 726 727 mutex_lock(&data->mutex); 728 729 if (data->trigger_enabled == state) 730 goto unlock_out; 731 732 if (data->state == BM1390_STATE_FIFO) { 733 dev_warn(data->dev, "Can't set trigger when FIFO enabled\n"); 734 ret = -EBUSY; 735 goto unlock_out; 736 } 737 738 data->trigger_enabled = state; 739 740 if (state) { 741 ret = bm1390_meas_set(data, BM1390_MEAS_MODE_CONTINUOUS); 742 if (ret) 743 goto unlock_out; 744 } else { 745 int dummy; 746 747 ret = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP); 748 if (ret) 749 goto unlock_out; 750 751 /* 752 * We need to read the status register in order to ACK the 753 * data-ready which may have been generated just before we 754 * disabled the measurement. 755 */ 756 ret = regmap_read(data->regmap, BM1390_REG_STATUS, &dummy); 757 if (ret) 758 dev_warn(data->dev, "status read failed\n"); 759 } 760 761 ret = bm1390_set_drdy_irq(data, state); 762 763 unlock_out: 764 mutex_unlock(&data->mutex); 765 766 return ret; 767 } 768 769 static const struct iio_trigger_ops bm1390_trigger_ops = { 770 .set_trigger_state = bm1390_trigger_set_state, 771 }; 772 773 static int bm1390_setup_buffer(struct bm1390_data *data, struct iio_dev *idev) 774 { 775 int ret; 776 777 ret = devm_iio_triggered_buffer_setup(data->dev, idev, 778 &iio_pollfunc_store_time, 779 &bm1390_trigger_handler, 780 &bm1390_buffer_ops); 781 782 if (ret) 783 return dev_err_probe(data->dev, ret, 784 "iio_triggered_buffer_setup FAIL\n"); 785 786 idev->available_scan_masks = bm1390_scan_masks; 787 788 return 0; 789 } 790 791 static int bm1390_setup_trigger(struct bm1390_data *data, struct iio_dev *idev, 792 int irq) 793 { 794 struct iio_trigger *itrig; 795 char *name; 796 int ret; 797 798 itrig = devm_iio_trigger_alloc(data->dev, "%sdata-rdy-dev%d", idev->name, 799 iio_device_id(idev)); 800 if (!itrig) 801 return -ENOMEM; 802 803 data->trig = itrig; 804 805 itrig->ops = &bm1390_trigger_ops; 806 iio_trigger_set_drvdata(itrig, data); 807 808 name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-bm1390", 809 dev_name(data->dev)); 810 if (name == NULL) 811 return -ENOMEM; 812 813 ret = devm_request_threaded_irq(data->dev, irq, bm1390_irq_handler, 814 &bm1390_irq_thread_handler, 815 IRQF_ONESHOT, name, idev); 816 if (ret) 817 return dev_err_probe(data->dev, ret, "Could not request IRQ\n"); 818 819 820 ret = devm_iio_trigger_register(data->dev, itrig); 821 if (ret) 822 return dev_err_probe(data->dev, ret, 823 "Trigger registration failed\n"); 824 825 return 0; 826 } 827 828 static int bm1390_probe(struct i2c_client *i2c) 829 { 830 struct bm1390_data *data; 831 struct regmap *regmap; 832 struct iio_dev *idev; 833 struct device *dev; 834 unsigned int part_id; 835 int ret; 836 837 dev = &i2c->dev; 838 839 regmap = devm_regmap_init_i2c(i2c, &bm1390_regmap); 840 if (IS_ERR(regmap)) 841 return dev_err_probe(dev, PTR_ERR(regmap), 842 "Failed to initialize Regmap\n"); 843 844 ret = devm_regulator_get_enable(dev, "vdd"); 845 if (ret) 846 return dev_err_probe(dev, ret, "Failed to get regulator\n"); 847 848 ret = regmap_read(regmap, BM1390_REG_PART_ID, &part_id); 849 if (ret) 850 return dev_err_probe(dev, ret, "Failed to access sensor\n"); 851 852 if (part_id != BM1390_ID) 853 dev_warn(dev, "unknown device 0x%x\n", part_id); 854 855 idev = devm_iio_device_alloc(dev, sizeof(*data)); 856 if (!idev) 857 return -ENOMEM; 858 859 data = iio_priv(idev); 860 data->regmap = regmap; 861 data->dev = dev; 862 data->irq = i2c->irq; 863 /* 864 * For now we just allow BM1390_WMI_MIN to BM1390_WMI_MAX and 865 * discard every other configuration when triggered mode is not used. 866 */ 867 data->watermark = BM1390_WMI_MAX; 868 mutex_init(&data->mutex); 869 870 idev->channels = bm1390_channels; 871 idev->num_channels = ARRAY_SIZE(bm1390_channels); 872 idev->name = "bm1390"; 873 idev->modes = INDIO_DIRECT_MODE; 874 875 ret = bm1390_chip_init(data); 876 if (ret) 877 return dev_err_probe(dev, ret, "sensor init failed\n"); 878 879 ret = bm1390_setup_buffer(data, idev); 880 if (ret) 881 return ret; 882 883 /* No trigger if we don't have IRQ for data-ready and WMI */ 884 if (i2c->irq > 0) { 885 idev->info = &bm1390_info; 886 idev->modes |= INDIO_BUFFER_SOFTWARE; 887 ret = bm1390_setup_trigger(data, idev, i2c->irq); 888 if (ret) 889 return ret; 890 } else { 891 idev->info = &bm1390_noirq_info; 892 } 893 894 ret = devm_iio_device_register(dev, idev); 895 if (ret < 0) 896 return dev_err_probe(dev, ret, 897 "Unable to register iio device\n"); 898 899 return 0; 900 } 901 902 static const struct of_device_id bm1390_of_match[] = { 903 { .compatible = "rohm,bm1390glv-z" }, 904 {} 905 }; 906 MODULE_DEVICE_TABLE(of, bm1390_of_match); 907 908 static const struct i2c_device_id bm1390_id[] = { 909 { "bm1390glv-z", }, 910 {} 911 }; 912 MODULE_DEVICE_TABLE(i2c, bm1390_id); 913 914 static struct i2c_driver bm1390_driver = { 915 .driver = { 916 .name = "bm1390", 917 .of_match_table = bm1390_of_match, 918 /* 919 * Probing explicitly requires a few millisecond of sleep. 920 * Enabling the VDD regulator may include ramp up rates. 921 */ 922 .probe_type = PROBE_PREFER_ASYNCHRONOUS, 923 }, 924 .probe = bm1390_probe, 925 .id_table = bm1390_id, 926 }; 927 module_i2c_driver(bm1390_driver); 928 929 MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>"); 930 MODULE_DESCRIPTION("Driver for ROHM BM1390 pressure sensor"); 931 MODULE_LICENSE("GPL"); 932