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 
bm1390_read_temp(struct bm1390_data * data,int * temp)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 
bm1390_pressure_read(struct bm1390_data * data,u32 * pressure)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 
bm1390_meas_set(struct bm1390_data * data,enum bm1390_meas_mode mode)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 
bm1390_read_data(struct bm1390_data * data,struct iio_chan_spec const * chan,int * val,int * val2)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 
bm1390_read_raw(struct iio_dev * idev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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 
__bm1390_fifo_flush(struct iio_dev * idev,unsigned int samples,s64 timestamp)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 	if (ret)
421 		return ret;
422 
423 	for (i = 0; i < smp_lvl; i++) {
424 		buffer[i].temp = temp;
425 		iio_push_to_buffers(idev, &buffer[i]);
426 	}
427 
428 	return smp_lvl;
429 }
430 
bm1390_fifo_flush(struct iio_dev * idev,unsigned int samples)431 static int bm1390_fifo_flush(struct iio_dev *idev, unsigned int samples)
432 {
433 	struct bm1390_data *data = iio_priv(idev);
434 	s64 timestamp;
435 	int ret;
436 
437 	/*
438 	 * If fifo_flush is being called from IRQ handler we know the stored
439 	 * timestamp is fairly accurate for the last stored sample. If we are
440 	 * called as a result of a read operation from userspace and hence
441 	 * before the watermark interrupt was triggered, take a timestamp
442 	 * now. We can fall anywhere in between two samples so the error in this
443 	 * case is at most one sample period.
444 	 * We need to have the IRQ disabled or we risk of messing-up
445 	 * the timestamps. If we are ran from IRQ, then the
446 	 * IRQF_ONESHOT has us covered - but if we are ran by the
447 	 * user-space read we need to disable the IRQ to be on a safe
448 	 * side. We do this usng synchronous disable so that if the
449 	 * IRQ thread is being ran on other CPU we wait for it to be
450 	 * finished.
451 	 */
452 
453 	timestamp = iio_get_time_ns(idev);
454 	mutex_lock(&data->mutex);
455 	ret = __bm1390_fifo_flush(idev, samples, timestamp);
456 	mutex_unlock(&data->mutex);
457 
458 	return ret;
459 }
460 
bm1390_set_watermark(struct iio_dev * idev,unsigned int val)461 static int bm1390_set_watermark(struct iio_dev *idev, unsigned int val)
462 {
463 	struct bm1390_data *data = iio_priv(idev);
464 
465 	if (val < BM1390_WMI_MIN || val > BM1390_WMI_MAX)
466 		return -EINVAL;
467 
468 	mutex_lock(&data->mutex);
469 	data->watermark = val;
470 	mutex_unlock(&data->mutex);
471 
472 	return 0;
473 }
474 
475 static const struct iio_info bm1390_noirq_info = {
476 	.read_raw = &bm1390_read_raw,
477 };
478 
479 static const struct iio_info bm1390_info = {
480 	.read_raw = &bm1390_read_raw,
481 	.hwfifo_set_watermark = bm1390_set_watermark,
482 	.hwfifo_flush_to_buffer = bm1390_fifo_flush,
483 };
484 
bm1390_chip_init(struct bm1390_data * data)485 static int bm1390_chip_init(struct bm1390_data *data)
486 {
487 	int ret;
488 
489 	ret = regmap_write_bits(data->regmap, BM1390_REG_POWER,
490 				BM1390_MASK_POWER, BM1390_POWER_ON);
491 	if (ret)
492 		return ret;
493 
494 	msleep(1);
495 
496 	ret = regmap_write_bits(data->regmap, BM1390_REG_RESET,
497 				BM1390_MASK_RESET, BM1390_RESET);
498 	if (ret)
499 		return ret;
500 
501 	msleep(1);
502 
503 	ret = regmap_write_bits(data->regmap, BM1390_REG_RESET,
504 				BM1390_MASK_RESET, BM1390_RESET_RELEASE);
505 	if (ret)
506 		return ret;
507 
508 	msleep(1);
509 
510 	ret = regmap_reinit_cache(data->regmap, &bm1390_regmap);
511 	if (ret) {
512 		dev_err(data->dev, "Failed to reinit reg cache\n");
513 		return ret;
514 	}
515 
516 	/*
517 	 * Default to use IIR filter in "middle" mode. Also the AVE_NUM must
518 	 * be fixed when IIR is in use.
519 	 */
520 	ret = regmap_update_bits(data->regmap, BM1390_REG_MODE_CTRL,
521 				 BM1390_MASK_AVE_NUM, BM1390_IIR_AVE_NUM);
522 	if (ret)
523 		return ret;
524 
525 	return regmap_update_bits(data->regmap, BM1390_REG_FIFO_CTRL,
526 				  BM1390_MASK_IIR_MODE, BM1390_IIR_MODE_MID);
527 }
528 
bm1390_fifo_set_wmi(struct bm1390_data * data)529 static int bm1390_fifo_set_wmi(struct bm1390_data *data)
530 {
531 	u8 regval;
532 
533 	regval = FIELD_PREP(BM1390_MASK_FIFO_LEN,
534 			    data->watermark - BM1390_WMI_MIN);
535 
536 	return regmap_update_bits(data->regmap, BM1390_REG_FIFO_CTRL,
537 				  BM1390_MASK_FIFO_LEN, regval);
538 }
539 
bm1390_fifo_enable(struct iio_dev * idev)540 static int bm1390_fifo_enable(struct iio_dev *idev)
541 {
542 	struct bm1390_data *data = iio_priv(idev);
543 	int ret;
544 
545 	/* We can't do buffered stuff without IRQ as we never get WMI */
546 	if (data->irq <= 0)
547 		return -EINVAL;
548 
549 	mutex_lock(&data->mutex);
550 	if (data->trigger_enabled) {
551 		ret = -EBUSY;
552 		goto unlock_out;
553 	}
554 
555 	/* Update watermark to HW */
556 	ret = bm1390_fifo_set_wmi(data);
557 	if (ret)
558 		goto unlock_out;
559 
560 	/* Enable WMI_IRQ */
561 	ret = regmap_set_bits(data->regmap, BM1390_REG_MODE_CTRL,
562 			      BM1390_MASK_WMI_EN);
563 	if (ret)
564 		goto unlock_out;
565 
566 	/* Enable FIFO */
567 	ret = regmap_set_bits(data->regmap, BM1390_REG_FIFO_CTRL,
568 			      BM1390_MASK_FIFO_EN);
569 	if (ret)
570 		goto unlock_out;
571 
572 	data->state = BM1390_STATE_FIFO;
573 
574 	data->old_timestamp = iio_get_time_ns(idev);
575 	ret = bm1390_meas_set(data, BM1390_MEAS_MODE_CONTINUOUS);
576 
577 unlock_out:
578 	mutex_unlock(&data->mutex);
579 
580 	return ret;
581 }
582 
bm1390_fifo_disable(struct iio_dev * idev)583 static int bm1390_fifo_disable(struct iio_dev *idev)
584 {
585 	struct bm1390_data *data = iio_priv(idev);
586 	int ret;
587 
588 	msleep(1);
589 
590 	mutex_lock(&data->mutex);
591 	ret = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP);
592 	if (ret)
593 		goto unlock_out;
594 
595 	/* Disable FIFO */
596 	ret = regmap_clear_bits(data->regmap, BM1390_REG_FIFO_CTRL,
597 				BM1390_MASK_FIFO_EN);
598 	if (ret)
599 		goto unlock_out;
600 
601 	data->state = BM1390_STATE_SAMPLE;
602 
603 	/* Disable WMI_IRQ */
604 	ret = regmap_clear_bits(data->regmap, BM1390_REG_MODE_CTRL,
605 				 BM1390_MASK_WMI_EN);
606 
607 unlock_out:
608 	mutex_unlock(&data->mutex);
609 
610 	return ret;
611 }
612 
bm1390_buffer_postenable(struct iio_dev * idev)613 static int bm1390_buffer_postenable(struct iio_dev *idev)
614 {
615 	/*
616 	 * If we use data-ready trigger, then the IRQ masks should be handled by
617 	 * trigger enable and the hardware buffer is not used but we just update
618 	 * results to the IIO FIFO when data-ready triggers.
619 	 */
620 	if (iio_device_get_current_mode(idev) == INDIO_BUFFER_TRIGGERED)
621 		return 0;
622 
623 	return bm1390_fifo_enable(idev);
624 }
625 
bm1390_buffer_predisable(struct iio_dev * idev)626 static int bm1390_buffer_predisable(struct iio_dev *idev)
627 {
628 	if (iio_device_get_current_mode(idev) == INDIO_BUFFER_TRIGGERED)
629 		return 0;
630 
631 	return bm1390_fifo_disable(idev);
632 }
633 
634 static const struct iio_buffer_setup_ops bm1390_buffer_ops = {
635 	.postenable = bm1390_buffer_postenable,
636 	.predisable = bm1390_buffer_predisable,
637 };
638 
bm1390_trigger_handler(int irq,void * p)639 static irqreturn_t bm1390_trigger_handler(int irq, void *p)
640 {
641 	struct iio_poll_func *pf = p;
642 	struct iio_dev *idev = pf->indio_dev;
643 	struct bm1390_data *data = iio_priv(idev);
644 	int ret, status;
645 
646 	/* DRDY is acked by reading status reg */
647 	ret = regmap_read(data->regmap, BM1390_REG_STATUS, &status);
648 	if (ret || !status)
649 		return IRQ_NONE;
650 
651 	dev_dbg(data->dev, "DRDY trig status 0x%x\n", status);
652 
653 	if (test_bit(BM1390_CHAN_PRESSURE, idev->active_scan_mask)) {
654 		ret = bm1390_pressure_read(data, &data->buf.pressure);
655 		if (ret) {
656 			dev_warn(data->dev, "sample read failed %d\n", ret);
657 			return IRQ_NONE;
658 		}
659 	}
660 
661 	if (test_bit(BM1390_CHAN_TEMP, idev->active_scan_mask)) {
662 		ret = regmap_bulk_read(data->regmap, BM1390_REG_TEMP_HI,
663 				       &data->buf.temp, sizeof(data->buf.temp));
664 		if (ret) {
665 			dev_warn(data->dev, "temp read failed %d\n", ret);
666 			return IRQ_HANDLED;
667 		}
668 	}
669 
670 	iio_push_to_buffers_with_timestamp(idev, &data->buf, data->timestamp);
671 	iio_trigger_notify_done(idev->trig);
672 
673 	return IRQ_HANDLED;
674 }
675 
676 /* Get timestamps and wake the thread if we need to read data */
bm1390_irq_handler(int irq,void * private)677 static irqreturn_t bm1390_irq_handler(int irq, void *private)
678 {
679 	struct iio_dev *idev = private;
680 	struct bm1390_data *data = iio_priv(idev);
681 
682 	data->timestamp = iio_get_time_ns(idev);
683 
684 	if (data->state == BM1390_STATE_FIFO || data->trigger_enabled)
685 		return IRQ_WAKE_THREAD;
686 
687 	return IRQ_NONE;
688 }
689 
bm1390_irq_thread_handler(int irq,void * private)690 static irqreturn_t bm1390_irq_thread_handler(int irq, void *private)
691 {
692 	struct iio_dev *idev = private;
693 	struct bm1390_data *data = iio_priv(idev);
694 	int ret = IRQ_NONE;
695 
696 	mutex_lock(&data->mutex);
697 
698 	if (data->trigger_enabled) {
699 		iio_trigger_poll_nested(data->trig);
700 		ret = IRQ_HANDLED;
701 	} else if (data->state == BM1390_STATE_FIFO) {
702 		int ok;
703 
704 		ok = __bm1390_fifo_flush(idev, BM1390_FIFO_LENGTH,
705 					 data->timestamp);
706 		if (ok > 0)
707 			ret = IRQ_HANDLED;
708 	}
709 
710 	mutex_unlock(&data->mutex);
711 
712 	return ret;
713 }
714 
bm1390_set_drdy_irq(struct bm1390_data * data,bool en)715 static int bm1390_set_drdy_irq(struct bm1390_data *data, bool en)
716 {
717 	if (en)
718 		return regmap_set_bits(data->regmap, BM1390_REG_MODE_CTRL,
719 				       BM1390_MASK_DRDY_EN);
720 	return regmap_clear_bits(data->regmap, BM1390_REG_MODE_CTRL,
721 				 BM1390_MASK_DRDY_EN);
722 }
723 
bm1390_trigger_set_state(struct iio_trigger * trig,bool state)724 static int bm1390_trigger_set_state(struct iio_trigger *trig,
725 				    bool state)
726 {
727 	struct bm1390_data *data = iio_trigger_get_drvdata(trig);
728 	int ret = 0;
729 
730 	mutex_lock(&data->mutex);
731 
732 	if (data->trigger_enabled == state)
733 		goto unlock_out;
734 
735 	if (data->state == BM1390_STATE_FIFO) {
736 		dev_warn(data->dev, "Can't set trigger when FIFO enabled\n");
737 		ret = -EBUSY;
738 		goto unlock_out;
739 	}
740 
741 	data->trigger_enabled = state;
742 
743 	if (state) {
744 		ret = bm1390_meas_set(data, BM1390_MEAS_MODE_CONTINUOUS);
745 		if (ret)
746 			goto unlock_out;
747 	} else {
748 		int dummy;
749 
750 		ret = bm1390_meas_set(data, BM1390_MEAS_MODE_STOP);
751 		if (ret)
752 			goto unlock_out;
753 
754 		/*
755 		 * We need to read the status register in order to ACK the
756 		 * data-ready which may have been generated just before we
757 		 * disabled the measurement.
758 		 */
759 		ret = regmap_read(data->regmap, BM1390_REG_STATUS, &dummy);
760 		if (ret)
761 			dev_warn(data->dev, "status read failed\n");
762 	}
763 
764 	ret = bm1390_set_drdy_irq(data, state);
765 
766 unlock_out:
767 	mutex_unlock(&data->mutex);
768 
769 	return ret;
770 }
771 
772 static const struct iio_trigger_ops bm1390_trigger_ops = {
773 	.set_trigger_state = bm1390_trigger_set_state,
774 };
775 
bm1390_setup_buffer(struct bm1390_data * data,struct iio_dev * idev)776 static int bm1390_setup_buffer(struct bm1390_data *data, struct iio_dev *idev)
777 {
778 	int ret;
779 
780 	ret = devm_iio_triggered_buffer_setup(data->dev, idev,
781 					      &iio_pollfunc_store_time,
782 					      &bm1390_trigger_handler,
783 					      &bm1390_buffer_ops);
784 
785 	if (ret)
786 		return dev_err_probe(data->dev, ret,
787 				     "iio_triggered_buffer_setup FAIL\n");
788 
789 	idev->available_scan_masks = bm1390_scan_masks;
790 
791 	return 0;
792 }
793 
bm1390_setup_trigger(struct bm1390_data * data,struct iio_dev * idev,int irq)794 static int bm1390_setup_trigger(struct bm1390_data *data, struct iio_dev *idev,
795 				int irq)
796 {
797 	struct iio_trigger *itrig;
798 	char *name;
799 	int ret;
800 
801 	itrig = devm_iio_trigger_alloc(data->dev, "%sdata-rdy-dev%d", idev->name,
802 					    iio_device_id(idev));
803 	if (!itrig)
804 		return -ENOMEM;
805 
806 	data->trig = itrig;
807 
808 	itrig->ops = &bm1390_trigger_ops;
809 	iio_trigger_set_drvdata(itrig, data);
810 
811 	name = devm_kasprintf(data->dev, GFP_KERNEL, "%s-bm1390",
812 			      dev_name(data->dev));
813 	if (name == NULL)
814 		return -ENOMEM;
815 
816 	ret = devm_request_threaded_irq(data->dev, irq, bm1390_irq_handler,
817 					&bm1390_irq_thread_handler,
818 					IRQF_ONESHOT, name, idev);
819 	if (ret)
820 		return dev_err_probe(data->dev, ret, "Could not request IRQ\n");
821 
822 
823 	ret = devm_iio_trigger_register(data->dev, itrig);
824 	if (ret)
825 		return dev_err_probe(data->dev, ret,
826 				     "Trigger registration failed\n");
827 
828 	return 0;
829 }
830 
bm1390_probe(struct i2c_client * i2c)831 static int bm1390_probe(struct i2c_client *i2c)
832 {
833 	struct bm1390_data *data;
834 	struct regmap *regmap;
835 	struct iio_dev *idev;
836 	struct device *dev;
837 	unsigned int part_id;
838 	int ret;
839 
840 	dev = &i2c->dev;
841 
842 	regmap = devm_regmap_init_i2c(i2c, &bm1390_regmap);
843 	if (IS_ERR(regmap))
844 		return dev_err_probe(dev, PTR_ERR(regmap),
845 				     "Failed to initialize Regmap\n");
846 
847 	ret = devm_regulator_get_enable(dev, "vdd");
848 	if (ret)
849 		return dev_err_probe(dev, ret, "Failed to get regulator\n");
850 
851 	ret = regmap_read(regmap, BM1390_REG_PART_ID, &part_id);
852 	if (ret)
853 		return dev_err_probe(dev, ret, "Failed to access sensor\n");
854 
855 	if (part_id != BM1390_ID)
856 		dev_warn(dev, "unknown device 0x%x\n", part_id);
857 
858 	idev = devm_iio_device_alloc(dev, sizeof(*data));
859 	if (!idev)
860 		return -ENOMEM;
861 
862 	data = iio_priv(idev);
863 	data->regmap = regmap;
864 	data->dev = dev;
865 	data->irq = i2c->irq;
866 	/*
867 	 * For now we just allow BM1390_WMI_MIN to BM1390_WMI_MAX and
868 	 * discard every other configuration when triggered mode is not used.
869 	 */
870 	data->watermark = BM1390_WMI_MAX;
871 	mutex_init(&data->mutex);
872 
873 	idev->channels = bm1390_channels;
874 	idev->num_channels = ARRAY_SIZE(bm1390_channels);
875 	idev->name = "bm1390";
876 	idev->modes = INDIO_DIRECT_MODE;
877 
878 	ret = bm1390_chip_init(data);
879 	if (ret)
880 		return dev_err_probe(dev, ret, "sensor init failed\n");
881 
882 	ret = bm1390_setup_buffer(data, idev);
883 	if (ret)
884 		return ret;
885 
886 	/* No trigger if we don't have IRQ for data-ready and WMI */
887 	if (i2c->irq > 0) {
888 		idev->info = &bm1390_info;
889 		idev->modes |= INDIO_BUFFER_SOFTWARE;
890 		ret = bm1390_setup_trigger(data, idev, i2c->irq);
891 		if (ret)
892 			return ret;
893 	} else {
894 		idev->info = &bm1390_noirq_info;
895 	}
896 
897 	ret = devm_iio_device_register(dev, idev);
898 	if (ret < 0)
899 		return dev_err_probe(dev, ret,
900 				     "Unable to register iio device\n");
901 
902 	return 0;
903 }
904 
905 static const struct of_device_id bm1390_of_match[] = {
906 	{ .compatible = "rohm,bm1390glv-z" },
907 	{}
908 };
909 MODULE_DEVICE_TABLE(of, bm1390_of_match);
910 
911 static const struct i2c_device_id bm1390_id[] = {
912 	{ "bm1390glv-z", },
913 	{}
914 };
915 MODULE_DEVICE_TABLE(i2c, bm1390_id);
916 
917 static struct i2c_driver bm1390_driver = {
918 	.driver = {
919 		.name = "bm1390",
920 		.of_match_table = bm1390_of_match,
921 		/*
922 		 * Probing explicitly requires a few millisecond of sleep.
923 		 * Enabling the VDD regulator may include ramp up rates.
924 		 */
925 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
926 	},
927 	.probe = bm1390_probe,
928 	.id_table = bm1390_id,
929 };
930 module_i2c_driver(bm1390_driver);
931 
932 MODULE_AUTHOR("Matti Vaittinen <mazziesaccount@gmail.com>");
933 MODULE_DESCRIPTION("Driver for ROHM BM1390 pressure sensor");
934 MODULE_LICENSE("GPL");
935