1 // SPDX-License-Identifier: GPL-2.0-only
3 * BM1390 ROHM pressure sensor
5 * Copyright (c) 2023, ROHM Semiconductor.
6 * https://fscdn.rohm.com/en/products/databook/datasheet/ic/sensor/pressure/bm1390glv-z-e.pdf
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>
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>
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)
39 * Data-sheet states that when the IIR is used, the AVE_NUM must be set to
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
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
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
63 #define BM1390_ID 0x34
66 static const struct regmap_range bm1390_volatile_ranges
[] = {
68 .range_min
= BM1390_REG_STATUS
,
69 .range_max
= BM1390_REG_STATUS
,
72 .range_min
= BM1390_REG_FIFO_LVL
,
73 .range_max
= BM1390_REG_TEMP_LO
,
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
),
82 static const struct regmap_range bm1390_precious_ranges
[] = {
84 .range_min
= BM1390_REG_STATUS
,
85 .range_max
= BM1390_REG_STATUS
,
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
),
94 static const struct regmap_range bm1390_read_only_ranges
[] = {
96 .range_min
= BM1390_REG_MANUFACT_ID
,
97 .range_max
= BM1390_REG_PART_ID
,
99 .range_min
= BM1390_REG_FIFO_LVL
,
100 .range_max
= BM1390_REG_TEMP_LO
,
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
),
109 static const struct regmap_range bm1390_noinc_read_ranges
[] = {
111 .range_min
= BM1390_REG_PRESSURE_BASE
,
112 .range_max
= BM1390_REG_TEMP_LO
,
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
),
121 static const struct regmap_config bm1390_regmap
= {
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,
138 struct bm1390_data_buf
{
144 /* BM1390 has FIFO for 4 pressure samples */
145 #define BM1390_FIFO_LENGTH 4
148 s64 timestamp
, old_timestamp
;
149 struct iio_trigger
*trig
;
150 struct regmap
*regmap
;
152 struct bm1390_data_buf buf
;
155 bool trigger_enabled
;
158 /* Prevent accessing sensor during FIFO read sequence */
163 BM1390_CHAN_PRESSURE
,
167 static const struct iio_chan_spec bm1390_channels
[] = {
169 .type
= IIO_PRESSURE
,
170 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
),
172 * When IIR is used, we must fix amount of averaged samples.
173 * Thus we don't allow setting oversampling ratio.
175 .info_mask_shared_by_type
= BIT(IIO_CHAN_INFO_SCALE
),
176 .scan_index
= BM1390_CHAN_PRESSURE
,
181 .endianness
= IIO_LE
,
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
,
193 .endianness
= IIO_BE
,
196 IIO_CHAN_SOFT_TIMESTAMP(2),
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.
203 static const unsigned long bm1390_scan_masks
[] = {
204 BIT(BM1390_CHAN_PRESSURE
),
205 BIT(BM1390_CHAN_PRESSURE
) | BIT(BM1390_CHAN_TEMP
),
209 static int bm1390_read_temp(struct bm1390_data
*data
, int *temp
)
214 ret
= regmap_bulk_read(data
->regmap
, BM1390_REG_TEMP_HI
, &temp_raw
,
219 *temp
= be16_to_cpu(temp_raw
);
224 static int bm1390_pressure_read(struct bm1390_data
*data
, u32
*pressure
)
226 /* Pressure data is in 3 8-bit registers */
230 ret
= regmap_bulk_read(data
->regmap
, BM1390_REG_PRESSURE_BASE
,
235 *pressure
= (u32
)(raw
[2] >> 2 | raw
[1] << 6 | raw
[0] << 14);
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,
247 static int bm1390_meas_set(struct bm1390_data
*data
, enum bm1390_meas_mode mode
)
249 return regmap_update_bits(data
->regmap
, BM1390_REG_MODE_CTRL
,
250 BM1390_MASK_MEAS_MODE
, mode
);
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.
259 #define BM1390_MAX_MEAS_TIME_MS 205
261 static int bm1390_read_data(struct bm1390_data
*data
,
262 struct iio_chan_spec
const *chan
, int *val
, int *val2
)
266 mutex_lock(&data
->mutex
);
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.
271 ret
= bm1390_meas_set(data
, BM1390_MEAS_MODE_CONTINUOUS
);
275 switch (chan
->type
) {
277 msleep(BM1390_MAX_MEAS_TIME_MS
);
278 ret
= bm1390_pressure_read(data
, val
);
281 msleep(BM1390_MAX_MEAS_TIME_MS
);
282 ret
= bm1390_read_temp(data
, val
);
287 warn
= bm1390_meas_set(data
, BM1390_MEAS_MODE_STOP
);
289 dev_warn(data
->dev
, "Failed to stop measurement (%d)\n", warn
);
291 mutex_unlock(&data
->mutex
);
296 static int bm1390_read_raw(struct iio_dev
*idev
,
297 struct iio_chan_spec
const *chan
,
298 int *val
, int *val2
, long mask
)
300 struct bm1390_data
*data
= iio_priv(idev
);
304 case IIO_CHAN_INFO_SCALE
:
305 if (chan
->type
== IIO_TEMP
) {
309 return IIO_VAL_INT_PLUS_MICRO
;
310 } else if (chan
->type
== IIO_PRESSURE
) {
312 * pressure in hPa is register value divided by 2048.
313 * This means kPa is 1/20480 times the register value,
318 return IIO_VAL_FRACTIONAL
;
322 case IIO_CHAN_INFO_RAW
:
323 ret
= iio_device_claim_direct_mode(idev
);
327 ret
= bm1390_read_data(data
, chan
, val
, val2
);
328 iio_device_release_direct_mode(idev
);
338 static int __bm1390_fifo_flush(struct iio_dev
*idev
, unsigned int samples
,
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
;
348 ret
= regmap_read(data
->regmap
, BM1390_REG_FIFO_LVL
, &smp_lvl
);
352 smp_lvl
= FIELD_GET(BM1390_MASK_FIFO_LVL
, smp_lvl
);
356 if (smp_lvl
> BM1390_FIFO_LENGTH
) {
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.
362 dev_err(data
->dev
, "bad FIFO level %d\n", smp_lvl
);
363 smp_lvl
= BM1390_FIFO_LENGTH
;
366 sample_period
= timestamp
- data
->old_timestamp
;
367 do_div(sample_period
, smp_lvl
);
369 if (samples
&& smp_lvl
> samples
)
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
379 for (i
= 0; i
< smp_lvl
; i
++) {
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.
387 if (test_bit(BM1390_CHAN_PRESSURE
, idev
->active_scan_mask
)) {
388 ret
= bm1390_pressure_read(data
, &buffer
[i
].pressure
);
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.
399 * Idea is to always keep the "old_timestamp" matching the
400 * timestamp which we are currently handling.
402 data
->old_timestamp
+= sample_period
;
403 buffer
[i
].ts
= data
->old_timestamp
;
405 /* Reading the FIFO_LVL closes the FIFO access sequence */
406 warn
= regmap_read(data
->regmap
, BM1390_REG_FIFO_LVL
, &dummy
);
408 dev_warn(data
->dev
, "Closing FIFO sequence failed\n");
413 if (test_bit(BM1390_CHAN_TEMP
, idev
->active_scan_mask
)) {
414 ret
= regmap_bulk_read(data
->regmap
, BM1390_REG_TEMP_HI
, &temp
,
420 for (i
= 0; i
< smp_lvl
; i
++) {
421 buffer
[i
].temp
= temp
;
422 iio_push_to_buffers(idev
, &buffer
[i
]);
428 static int bm1390_fifo_flush(struct iio_dev
*idev
, unsigned int samples
)
430 struct bm1390_data
*data
= iio_priv(idev
);
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
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
);
458 static int bm1390_set_watermark(struct iio_dev
*idev
, unsigned int val
)
460 struct bm1390_data
*data
= iio_priv(idev
);
462 if (val
< BM1390_WMI_MIN
|| val
> BM1390_WMI_MAX
)
465 mutex_lock(&data
->mutex
);
466 data
->watermark
= val
;
467 mutex_unlock(&data
->mutex
);
472 static const struct iio_info bm1390_noirq_info
= {
473 .read_raw
= &bm1390_read_raw
,
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
,
482 static int bm1390_chip_init(struct bm1390_data
*data
)
486 ret
= regmap_write_bits(data
->regmap
, BM1390_REG_POWER
,
487 BM1390_MASK_POWER
, BM1390_POWER_ON
);
493 ret
= regmap_write_bits(data
->regmap
, BM1390_REG_RESET
,
494 BM1390_MASK_RESET
, BM1390_RESET
);
500 ret
= regmap_write_bits(data
->regmap
, BM1390_REG_RESET
,
501 BM1390_MASK_RESET
, BM1390_RESET_RELEASE
);
507 ret
= regmap_reinit_cache(data
->regmap
, &bm1390_regmap
);
509 dev_err(data
->dev
, "Failed to reinit reg cache\n");
514 * Default to use IIR filter in "middle" mode. Also the AVE_NUM must
515 * be fixed when IIR is in use.
517 ret
= regmap_update_bits(data
->regmap
, BM1390_REG_MODE_CTRL
,
518 BM1390_MASK_AVE_NUM
, BM1390_IIR_AVE_NUM
);
522 return regmap_update_bits(data
->regmap
, BM1390_REG_FIFO_CTRL
,
523 BM1390_MASK_IIR_MODE
, BM1390_IIR_MODE_MID
);
526 static int bm1390_fifo_set_wmi(struct bm1390_data
*data
)
530 regval
= FIELD_PREP(BM1390_MASK_FIFO_LEN
,
531 data
->watermark
- BM1390_WMI_MIN
);
533 return regmap_update_bits(data
->regmap
, BM1390_REG_FIFO_CTRL
,
534 BM1390_MASK_FIFO_LEN
, regval
);
537 static int bm1390_fifo_enable(struct iio_dev
*idev
)
539 struct bm1390_data
*data
= iio_priv(idev
);
542 /* We can't do buffered stuff without IRQ as we never get WMI */
546 mutex_lock(&data
->mutex
);
547 if (data
->trigger_enabled
) {
552 /* Update watermark to HW */
553 ret
= bm1390_fifo_set_wmi(data
);
558 ret
= regmap_set_bits(data
->regmap
, BM1390_REG_MODE_CTRL
,
564 ret
= regmap_set_bits(data
->regmap
, BM1390_REG_FIFO_CTRL
,
565 BM1390_MASK_FIFO_EN
);
569 data
->state
= BM1390_STATE_FIFO
;
571 data
->old_timestamp
= iio_get_time_ns(idev
);
572 ret
= bm1390_meas_set(data
, BM1390_MEAS_MODE_CONTINUOUS
);
575 mutex_unlock(&data
->mutex
);
580 static int bm1390_fifo_disable(struct iio_dev
*idev
)
582 struct bm1390_data
*data
= iio_priv(idev
);
587 mutex_lock(&data
->mutex
);
588 ret
= bm1390_meas_set(data
, BM1390_MEAS_MODE_STOP
);
593 ret
= regmap_clear_bits(data
->regmap
, BM1390_REG_FIFO_CTRL
,
594 BM1390_MASK_FIFO_EN
);
598 data
->state
= BM1390_STATE_SAMPLE
;
600 /* Disable WMI_IRQ */
601 ret
= regmap_clear_bits(data
->regmap
, BM1390_REG_MODE_CTRL
,
605 mutex_unlock(&data
->mutex
);
610 static int bm1390_buffer_postenable(struct iio_dev
*idev
)
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.
617 if (iio_device_get_current_mode(idev
) == INDIO_BUFFER_TRIGGERED
)
620 return bm1390_fifo_enable(idev
);
623 static int bm1390_buffer_predisable(struct iio_dev
*idev
)
625 if (iio_device_get_current_mode(idev
) == INDIO_BUFFER_TRIGGERED
)
628 return bm1390_fifo_disable(idev
);
631 static const struct iio_buffer_setup_ops bm1390_buffer_ops
= {
632 .postenable
= bm1390_buffer_postenable
,
633 .predisable
= bm1390_buffer_predisable
,
636 static irqreturn_t
bm1390_trigger_handler(int irq
, void *p
)
638 struct iio_poll_func
*pf
= p
;
639 struct iio_dev
*idev
= pf
->indio_dev
;
640 struct bm1390_data
*data
= iio_priv(idev
);
643 /* DRDY is acked by reading status reg */
644 ret
= regmap_read(data
->regmap
, BM1390_REG_STATUS
, &status
);
648 dev_dbg(data
->dev
, "DRDY trig status 0x%x\n", status
);
650 if (test_bit(BM1390_CHAN_PRESSURE
, idev
->active_scan_mask
)) {
651 ret
= bm1390_pressure_read(data
, &data
->buf
.pressure
);
653 dev_warn(data
->dev
, "sample read failed %d\n", ret
);
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
));
662 dev_warn(data
->dev
, "temp read failed %d\n", ret
);
667 iio_push_to_buffers_with_timestamp(idev
, &data
->buf
, data
->timestamp
);
668 iio_trigger_notify_done(idev
->trig
);
673 /* Get timestamps and wake the thread if we need to read data */
674 static irqreturn_t
bm1390_irq_handler(int irq
, void *private)
676 struct iio_dev
*idev
= private;
677 struct bm1390_data
*data
= iio_priv(idev
);
679 data
->timestamp
= iio_get_time_ns(idev
);
681 if (data
->state
== BM1390_STATE_FIFO
|| data
->trigger_enabled
)
682 return IRQ_WAKE_THREAD
;
687 static irqreturn_t
bm1390_irq_thread_handler(int irq
, void *private)
689 struct iio_dev
*idev
= private;
690 struct bm1390_data
*data
= iio_priv(idev
);
693 mutex_lock(&data
->mutex
);
695 if (data
->trigger_enabled
) {
696 iio_trigger_poll_nested(data
->trig
);
698 } else if (data
->state
== BM1390_STATE_FIFO
) {
701 ok
= __bm1390_fifo_flush(idev
, BM1390_FIFO_LENGTH
,
707 mutex_unlock(&data
->mutex
);
712 static int bm1390_set_drdy_irq(struct bm1390_data
*data
, bool 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
);
721 static int bm1390_trigger_set_state(struct iio_trigger
*trig
,
724 struct bm1390_data
*data
= iio_trigger_get_drvdata(trig
);
727 mutex_lock(&data
->mutex
);
729 if (data
->trigger_enabled
== state
)
732 if (data
->state
== BM1390_STATE_FIFO
) {
733 dev_warn(data
->dev
, "Can't set trigger when FIFO enabled\n");
738 data
->trigger_enabled
= state
;
741 ret
= bm1390_meas_set(data
, BM1390_MEAS_MODE_CONTINUOUS
);
747 ret
= bm1390_meas_set(data
, BM1390_MEAS_MODE_STOP
);
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.
756 ret
= regmap_read(data
->regmap
, BM1390_REG_STATUS
, &dummy
);
758 dev_warn(data
->dev
, "status read failed\n");
761 ret
= bm1390_set_drdy_irq(data
, state
);
764 mutex_unlock(&data
->mutex
);
769 static const struct iio_trigger_ops bm1390_trigger_ops
= {
770 .set_trigger_state
= bm1390_trigger_set_state
,
773 static int bm1390_setup_buffer(struct bm1390_data
*data
, struct iio_dev
*idev
)
777 ret
= devm_iio_triggered_buffer_setup(data
->dev
, idev
,
778 &iio_pollfunc_store_time
,
779 &bm1390_trigger_handler
,
783 return dev_err_probe(data
->dev
, ret
,
784 "iio_triggered_buffer_setup FAIL\n");
786 idev
->available_scan_masks
= bm1390_scan_masks
;
791 static int bm1390_setup_trigger(struct bm1390_data
*data
, struct iio_dev
*idev
,
794 struct iio_trigger
*itrig
;
798 itrig
= devm_iio_trigger_alloc(data
->dev
, "%sdata-rdy-dev%d", idev
->name
,
799 iio_device_id(idev
));
805 itrig
->ops
= &bm1390_trigger_ops
;
806 iio_trigger_set_drvdata(itrig
, data
);
808 name
= devm_kasprintf(data
->dev
, GFP_KERNEL
, "%s-bm1390",
809 dev_name(data
->dev
));
813 ret
= devm_request_threaded_irq(data
->dev
, irq
, bm1390_irq_handler
,
814 &bm1390_irq_thread_handler
,
815 IRQF_ONESHOT
, name
, idev
);
817 return dev_err_probe(data
->dev
, ret
, "Could not request IRQ\n");
820 ret
= devm_iio_trigger_register(data
->dev
, itrig
);
822 return dev_err_probe(data
->dev
, ret
,
823 "Trigger registration failed\n");
828 static int bm1390_probe(struct i2c_client
*i2c
)
830 struct bm1390_data
*data
;
831 struct regmap
*regmap
;
832 struct iio_dev
*idev
;
834 unsigned int part_id
;
839 regmap
= devm_regmap_init_i2c(i2c
, &bm1390_regmap
);
841 return dev_err_probe(dev
, PTR_ERR(regmap
),
842 "Failed to initialize Regmap\n");
844 ret
= devm_regulator_get_enable(dev
, "vdd");
846 return dev_err_probe(dev
, ret
, "Failed to get regulator\n");
848 ret
= regmap_read(regmap
, BM1390_REG_PART_ID
, &part_id
);
850 return dev_err_probe(dev
, ret
, "Failed to access sensor\n");
852 if (part_id
!= BM1390_ID
)
853 dev_warn(dev
, "unknown device 0x%x\n", part_id
);
855 idev
= devm_iio_device_alloc(dev
, sizeof(*data
));
859 data
= iio_priv(idev
);
860 data
->regmap
= regmap
;
862 data
->irq
= i2c
->irq
;
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.
867 data
->watermark
= BM1390_WMI_MAX
;
868 mutex_init(&data
->mutex
);
870 idev
->channels
= bm1390_channels
;
871 idev
->num_channels
= ARRAY_SIZE(bm1390_channels
);
872 idev
->name
= "bm1390";
873 idev
->modes
= INDIO_DIRECT_MODE
;
875 ret
= bm1390_chip_init(data
);
877 return dev_err_probe(dev
, ret
, "sensor init failed\n");
879 ret
= bm1390_setup_buffer(data
, idev
);
883 /* No trigger if we don't have IRQ for data-ready and WMI */
885 idev
->info
= &bm1390_info
;
886 idev
->modes
|= INDIO_BUFFER_SOFTWARE
;
887 ret
= bm1390_setup_trigger(data
, idev
, i2c
->irq
);
891 idev
->info
= &bm1390_noirq_info
;
894 ret
= devm_iio_device_register(dev
, idev
);
896 return dev_err_probe(dev
, ret
,
897 "Unable to register iio device\n");
902 static const struct of_device_id bm1390_of_match
[] = {
903 { .compatible
= "rohm,bm1390glv-z" },
906 MODULE_DEVICE_TABLE(of
, bm1390_of_match
);
908 static const struct i2c_device_id bm1390_id
[] = {
912 MODULE_DEVICE_TABLE(i2c
, bm1390_id
);
914 static struct i2c_driver bm1390_driver
= {
917 .of_match_table
= bm1390_of_match
,
919 * Probing explicitly requires a few millisecond of sleep.
920 * Enabling the VDD regulator may include ramp up rates.
922 .probe_type
= PROBE_PREFER_ASYNCHRONOUS
,
924 .probe
= bm1390_probe
,
925 .id_table
= bm1390_id
,
927 module_i2c_driver(bm1390_driver
);
929 MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
930 MODULE_DESCRIPTION("Driver for ROHM BM1390 pressure sensor");
931 MODULE_LICENSE("GPL");