Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / iio / pressure / rohm-bm1390.c
blobf24d9f92768123fee772320fcecf906e69c23c40
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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
7 */
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
40 * value 110b
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
65 /* Regmap configs */
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,
98 }, {
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 = {
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,
133 enum {
134 BM1390_STATE_SAMPLE,
135 BM1390_STATE_FIFO,
138 struct bm1390_data_buf {
139 u32 pressure;
140 __be16 temp;
141 s64 ts __aligned(8);
144 /* BM1390 has FIFO for 4 pressure samples */
145 #define BM1390_FIFO_LENGTH 4
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;
158 /* Prevent accessing sensor during FIFO read sequence */
159 struct mutex mutex;
162 enum {
163 BM1390_CHAN_PRESSURE,
164 BM1390_CHAN_TEMP,
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,
177 .scan_type = {
178 .sign = 'u',
179 .realbits = 22,
180 .storagebits = 32,
181 .endianness = IIO_LE,
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,
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)
211 __be16 temp_raw;
212 int ret;
214 ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI, &temp_raw,
215 sizeof(temp_raw));
216 if (ret)
217 return ret;
219 *temp = be16_to_cpu(temp_raw);
221 return 0;
224 static int bm1390_pressure_read(struct bm1390_data *data, u32 *pressure)
226 /* Pressure data is in 3 8-bit registers */
227 u8 raw[3];
228 int ret;
230 ret = regmap_bulk_read(data->regmap, BM1390_REG_PRESSURE_BASE,
231 raw, sizeof(raw));
232 if (ret < 0)
233 return ret;
235 *pressure = (u32)(raw[2] >> 2 | raw[1] << 6 | raw[0] << 14);
237 return 0;
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)
264 int ret, warn;
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);
272 if (ret)
273 goto unlock_out;
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;
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);
293 return ret;
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);
301 int ret;
303 switch (mask) {
304 case IIO_CHAN_INFO_SCALE:
305 if (chan->type == IIO_TEMP) {
306 *val = 31;
307 *val2 = 250000;
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,
315 *val = 1;
316 *val2 = 2048;
318 return IIO_VAL_FRACTIONAL;
321 return -EINVAL;
322 case IIO_CHAN_INFO_RAW:
323 ret = iio_device_claim_direct_mode(idev);
324 if (ret)
325 return ret;
327 ret = bm1390_read_data(data, chan, val, val2);
328 iio_device_release_direct_mode(idev);
329 if (ret)
330 return ret;
332 return IIO_VAL_INT;
333 default:
334 return -EINVAL;
338 static int __bm1390_fifo_flush(struct iio_dev *idev, unsigned int samples,
339 s64 timestamp)
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;
348 ret = regmap_read(data->regmap, BM1390_REG_FIFO_LVL, &smp_lvl);
349 if (ret)
350 return ret;
352 smp_lvl = FIELD_GET(BM1390_MASK_FIFO_LVL, smp_lvl);
353 if (!smp_lvl)
354 return 0;
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)
370 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
377 * only after that.
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);
389 if (ret)
390 break;
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);
407 if (warn)
408 dev_warn(data->dev, "Closing FIFO sequence failed\n");
410 if (ret)
411 return ret;
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;
420 for (i = 0; i < smp_lvl; i++) {
421 buffer[i].temp = temp;
422 iio_push_to_buffers(idev, &buffer[i]);
425 return smp_lvl;
428 static int bm1390_fifo_flush(struct iio_dev *idev, unsigned int samples)
430 struct bm1390_data *data = iio_priv(idev);
431 s64 timestamp;
432 int ret;
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.
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);
455 return ret;
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)
463 return -EINVAL;
465 mutex_lock(&data->mutex);
466 data->watermark = val;
467 mutex_unlock(&data->mutex);
469 return 0;
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)
484 int ret;
486 ret = regmap_write_bits(data->regmap, BM1390_REG_POWER,
487 BM1390_MASK_POWER, BM1390_POWER_ON);
488 if (ret)
489 return ret;
491 msleep(1);
493 ret = regmap_write_bits(data->regmap, BM1390_REG_RESET,
494 BM1390_MASK_RESET, BM1390_RESET);
495 if (ret)
496 return ret;
498 msleep(1);
500 ret = regmap_write_bits(data->regmap, BM1390_REG_RESET,
501 BM1390_MASK_RESET, BM1390_RESET_RELEASE);
502 if (ret)
503 return ret;
505 msleep(1);
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;
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);
519 if (ret)
520 return ret;
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)
528 u8 regval;
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);
540 int ret;
542 /* We can't do buffered stuff without IRQ as we never get WMI */
543 if (data->irq <= 0)
544 return -EINVAL;
546 mutex_lock(&data->mutex);
547 if (data->trigger_enabled) {
548 ret = -EBUSY;
549 goto unlock_out;
552 /* Update watermark to HW */
553 ret = bm1390_fifo_set_wmi(data);
554 if (ret)
555 goto unlock_out;
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;
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;
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);
574 unlock_out:
575 mutex_unlock(&data->mutex);
577 return ret;
580 static int bm1390_fifo_disable(struct iio_dev *idev)
582 struct bm1390_data *data = iio_priv(idev);
583 int ret;
585 msleep(1);
587 mutex_lock(&data->mutex);
588 ret = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP);
589 if (ret)
590 goto unlock_out;
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;
598 data->state = BM1390_STATE_SAMPLE;
600 /* Disable WMI_IRQ */
601 ret = regmap_clear_bits(data->regmap, BM1390_REG_MODE_CTRL,
602 BM1390_MASK_WMI_EN);
604 unlock_out:
605 mutex_unlock(&data->mutex);
607 return ret;
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)
618 return 0;
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)
626 return 0;
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);
641 int ret, status;
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;
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);
652 if (ret) {
653 dev_warn(data->dev, "sample read failed %d\n", ret);
654 return IRQ_NONE;
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;
667 iio_push_to_buffers_with_timestamp(idev, &data->buf, data->timestamp);
668 iio_trigger_notify_done(idev->trig);
670 return IRQ_HANDLED;
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;
684 return IRQ_NONE;
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);
691 int ret = IRQ_NONE;
693 mutex_lock(&data->mutex);
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;
701 ok = __bm1390_fifo_flush(idev, BM1390_FIFO_LENGTH,
702 data->timestamp);
703 if (ok > 0)
704 ret = IRQ_HANDLED;
707 mutex_unlock(&data->mutex);
709 return ret;
712 static int bm1390_set_drdy_irq(struct bm1390_data *data, bool en)
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);
721 static int bm1390_trigger_set_state(struct iio_trigger *trig,
722 bool state)
724 struct bm1390_data *data = iio_trigger_get_drvdata(trig);
725 int ret = 0;
727 mutex_lock(&data->mutex);
729 if (data->trigger_enabled == state)
730 goto unlock_out;
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;
738 data->trigger_enabled = state;
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;
747 ret = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP);
748 if (ret)
749 goto unlock_out;
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);
757 if (ret)
758 dev_warn(data->dev, "status read failed\n");
761 ret = bm1390_set_drdy_irq(data, state);
763 unlock_out:
764 mutex_unlock(&data->mutex);
766 return ret;
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)
775 int ret;
777 ret = devm_iio_triggered_buffer_setup(data->dev, idev,
778 &iio_pollfunc_store_time,
779 &bm1390_trigger_handler,
780 &bm1390_buffer_ops);
782 if (ret)
783 return dev_err_probe(data->dev, ret,
784 "iio_triggered_buffer_setup FAIL\n");
786 idev->available_scan_masks = bm1390_scan_masks;
788 return 0;
791 static int bm1390_setup_trigger(struct bm1390_data *data, struct iio_dev *idev,
792 int irq)
794 struct iio_trigger *itrig;
795 char *name;
796 int ret;
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;
803 data->trig = itrig;
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));
810 if (name == NULL)
811 return -ENOMEM;
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");
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");
825 return 0;
828 static int bm1390_probe(struct i2c_client *i2c)
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;
837 dev = &i2c->dev;
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");
844 ret = devm_regulator_get_enable(dev, "vdd");
845 if (ret)
846 return dev_err_probe(dev, ret, "Failed to get regulator\n");
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");
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));
856 if (!idev)
857 return -ENOMEM;
859 data = iio_priv(idev);
860 data->regmap = regmap;
861 data->dev = dev;
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);
876 if (ret)
877 return dev_err_probe(dev, ret, "sensor init failed\n");
879 ret = bm1390_setup_buffer(data, idev);
880 if (ret)
881 return ret;
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;
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");
899 return 0;
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[] = {
909 { "bm1390glv-z", },
912 MODULE_DEVICE_TABLE(i2c, bm1390_id);
914 static struct i2c_driver bm1390_driver = {
915 .driver = {
916 .name = "bm1390",
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");