1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MPRLS0025PA - Honeywell MicroPressure pressure sensor series driver
5 * Copyright (c) Andreas Klinger <ak@it-klinger.de>
8 * https://prod-edam.honeywell.com/content/dam/honeywell-edam/sps/siot/en-us/
9 * products/sensors/pressure-sensors/board-mount-pressure-sensors/
10 * micropressure-mpr-series/documents/
11 * sps-siot-mpr-series-datasheet-32332628-ciid-172626.pdf
13 * 7-bit I2C default slave address: 0x18
16 #include <linux/delay.h>
17 #include <linux/device.h>
18 #include <linux/i2c.h>
19 #include <linux/math64.h>
20 #include <linux/mod_devicetable.h>
21 #include <linux/module.h>
22 #include <linux/property.h>
23 #include <linux/units.h>
25 #include <linux/gpio/consumer.h>
27 #include <linux/iio/buffer.h>
28 #include <linux/iio/iio.h>
29 #include <linux/iio/trigger_consumer.h>
30 #include <linux/iio/triggered_buffer.h>
32 #include <linux/regulator/consumer.h>
34 #include <asm/unaligned.h>
36 /* bits in i2c status byte */
37 #define MPR_I2C_POWER BIT(6) /* device is powered */
38 #define MPR_I2C_BUSY BIT(5) /* device is busy */
39 #define MPR_I2C_MEMORY BIT(2) /* integrity test passed */
40 #define MPR_I2C_MATH BIT(0) /* internal math saturation */
43 * support _RAW sysfs interface:
45 * Calculation formula from the datasheet:
46 * pressure = (press_cnt - outputmin) * scale + pmin
48 * * pressure - measured pressure in Pascal
49 * * press_cnt - raw value read from sensor
50 * * pmin - minimum pressure range value of sensor (data->pmin)
51 * * pmax - maximum pressure range value of sensor (data->pmax)
52 * * outputmin - minimum numerical range raw value delivered by sensor
53 * (mpr_func_spec.output_min)
54 * * outputmax - maximum numerical range raw value delivered by sensor
55 * (mpr_func_spec.output_max)
56 * * scale - (pmax - pmin) / (outputmax - outputmin)
58 * formula of the userspace:
59 * pressure = (raw + offset) * scale
61 * Values given to the userspace in sysfs interface:
63 * * offset - (-1 * outputmin) - pmin / scale
64 * note: With all sensors from the datasheet pmin = 0
65 * which reduces the offset to (-1 * outputmin)
69 * transfer function A: 10% to 90% of 2^24
70 * transfer function B: 2.5% to 22.5% of 2^24
71 * transfer function C: 20% to 80% of 2^24
79 struct mpr_func_spec {
84 static const struct mpr_func_spec mpr_func_spec[] = {
85 [MPR_FUNCTION_A] = {.output_min = 1677722, .output_max = 15099494},
86 [MPR_FUNCTION_B] = {.output_min = 419430, .output_max = 3774874},
87 [MPR_FUNCTION_C] = {.output_min = 3355443, .output_max = 13421773},
91 s32 pres; /* pressure value */
92 s64 ts; /* timestamp */
96 struct i2c_client *client;
98 * access to device during read
100 u32 pmin; /* minimal pressure in pascal */
101 u32 pmax; /* maximal pressure in pascal */
102 enum mpr_func_id function; /* transfer function */
104 * minimal numerical range raw
108 * maximal numerical range raw
111 int scale; /* int part of scale */
112 int scale2; /* nano part of scale */
113 int offset; /* int part of offset */
114 int offset2; /* nano part of offset */
115 struct gpio_desc *gpiod_reset; /* reset */
117 * end of conversion irq;
118 * used to distinguish between
119 * irq mode and reading in a
120 * loop until data is ready
122 struct completion completion; /* handshake from irq to read */
123 struct mpr_chan chan; /*
124 * channel values for buffered
129 static const struct iio_chan_spec mpr_channels[] = {
131 .type = IIO_PRESSURE,
132 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
133 BIT(IIO_CHAN_INFO_SCALE) |
134 BIT(IIO_CHAN_INFO_OFFSET),
140 .endianness = IIO_CPU,
143 IIO_CHAN_SOFT_TIMESTAMP(1),
146 static void mpr_reset(struct mpr_data *data)
148 if (data->gpiod_reset) {
149 gpiod_set_value(data->gpiod_reset, 0);
151 gpiod_set_value(data->gpiod_reset, 1);
156 * mpr_read_pressure() - Read pressure value from sensor via I2C
157 * @data: Pointer to private data struct.
158 * @press: Output value read from sensor.
160 * Reading from the sensor by sending and receiving I2C telegrams.
162 * If there is an end of conversion (EOC) interrupt registered the function
163 * waits for a maximum of one second for the interrupt.
165 * Context: The function can sleep and data->lock should be held when calling it
167 * * 0 - OK, the pressure value could be read
168 * * -ETIMEDOUT - Timeout while waiting for the EOC interrupt or busy flag is
169 * still set after nloops attempts of reading
171 static int mpr_read_pressure(struct mpr_data *data, s32 *press)
173 struct device *dev = &data->client->dev;
175 u8 wdata[] = {0xAA, 0x00, 0x00};
180 reinit_completion(&data->completion);
182 ret = i2c_master_send(data->client, wdata, sizeof(wdata));
184 dev_err(dev, "error while writing ret: %d\n", ret);
187 if (ret != sizeof(wdata)) {
188 dev_err(dev, "received size doesn't fit - ret: %d / %u\n", ret,
194 ret = wait_for_completion_timeout(&data->completion, HZ);
196 dev_err(dev, "timeout while waiting for eoc irq\n");
200 /* wait until status indicates data is ready */
201 for (i = 0; i < nloops; i++) {
203 * datasheet only says to wait at least 5 ms for the
204 * data but leave the maximum response time open
205 * --> let's try it nloops (10) times which seems to be
208 usleep_range(5000, 10000);
209 status = i2c_smbus_read_byte(data->client);
212 "error while reading, status: %d\n",
216 if (!(status & MPR_I2C_BUSY))
220 dev_err(dev, "timeout while reading\n");
225 ret = i2c_master_recv(data->client, buf, sizeof(buf));
227 dev_err(dev, "error in i2c_master_recv ret: %d\n", ret);
230 if (ret != sizeof(buf)) {
231 dev_err(dev, "received size doesn't fit - ret: %d / %u\n", ret,
236 if (buf[0] & MPR_I2C_BUSY) {
238 * it should never be the case that status still indicates
241 dev_err(dev, "data still not ready: %08x\n", buf[0]);
245 *press = get_unaligned_be24(&buf[1]);
247 dev_dbg(dev, "received: %*ph cnt: %d\n", ret, buf, *press);
252 static irqreturn_t mpr_eoc_handler(int irq, void *p)
254 struct mpr_data *data = p;
256 complete(&data->completion);
261 static irqreturn_t mpr_trigger_handler(int irq, void *p)
264 struct iio_poll_func *pf = p;
265 struct iio_dev *indio_dev = pf->indio_dev;
266 struct mpr_data *data = iio_priv(indio_dev);
268 mutex_lock(&data->lock);
269 ret = mpr_read_pressure(data, &data->chan.pres);
273 iio_push_to_buffers_with_timestamp(indio_dev, &data->chan,
274 iio_get_time_ns(indio_dev));
277 mutex_unlock(&data->lock);
278 iio_trigger_notify_done(indio_dev->trig);
283 static int mpr_read_raw(struct iio_dev *indio_dev,
284 struct iio_chan_spec const *chan, int *val, int *val2, long mask)
288 struct mpr_data *data = iio_priv(indio_dev);
290 if (chan->type != IIO_PRESSURE)
294 case IIO_CHAN_INFO_RAW:
295 mutex_lock(&data->lock);
296 ret = mpr_read_pressure(data, &pressure);
297 mutex_unlock(&data->lock);
302 case IIO_CHAN_INFO_SCALE:
304 *val2 = data->scale2;
305 return IIO_VAL_INT_PLUS_NANO;
306 case IIO_CHAN_INFO_OFFSET:
308 *val2 = data->offset2;
309 return IIO_VAL_INT_PLUS_NANO;
315 static const struct iio_info mpr_info = {
316 .read_raw = &mpr_read_raw,
319 static int mpr_probe(struct i2c_client *client)
322 struct mpr_data *data;
323 struct iio_dev *indio_dev;
324 struct device *dev = &client->dev;
327 if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE))
328 return dev_err_probe(dev, -EOPNOTSUPP,
329 "I2C functionality not supported\n");
331 indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
333 return dev_err_probe(dev, -ENOMEM, "couldn't get iio_dev\n");
335 data = iio_priv(indio_dev);
336 data->client = client;
337 data->irq = client->irq;
339 mutex_init(&data->lock);
340 init_completion(&data->completion);
342 indio_dev->name = "mprls0025pa";
343 indio_dev->info = &mpr_info;
344 indio_dev->channels = mpr_channels;
345 indio_dev->num_channels = ARRAY_SIZE(mpr_channels);
346 indio_dev->modes = INDIO_DIRECT_MODE;
348 ret = devm_regulator_get_enable(dev, "vdd");
350 return dev_err_probe(dev, ret,
351 "can't get and enable vdd supply\n");
353 if (dev_fwnode(dev)) {
354 ret = device_property_read_u32(dev, "honeywell,pmin-pascal",
357 return dev_err_probe(dev, ret,
358 "honeywell,pmin-pascal could not be read\n");
359 ret = device_property_read_u32(dev, "honeywell,pmax-pascal",
362 return dev_err_probe(dev, ret,
363 "honeywell,pmax-pascal could not be read\n");
364 ret = device_property_read_u32(dev,
365 "honeywell,transfer-function", &data->function);
367 return dev_err_probe(dev, ret,
368 "honeywell,transfer-function could not be read\n");
369 if (data->function > MPR_FUNCTION_C)
370 return dev_err_probe(dev, -EINVAL,
371 "honeywell,transfer-function %d invalid\n",
374 /* when loaded as i2c device we need to use default values */
375 dev_notice(dev, "firmware node not found; using defaults\n");
377 data->pmax = 172369; /* 25 psi */
378 data->function = MPR_FUNCTION_A;
381 data->outmin = mpr_func_spec[data->function].output_min;
382 data->outmax = mpr_func_spec[data->function].output_max;
384 /* use 64 bit calculation for preserving a reasonable precision */
385 scale = div_s64(((s64)(data->pmax - data->pmin)) * NANO,
386 data->outmax - data->outmin);
387 data->scale = div_s64_rem(scale, NANO, &data->scale2);
389 * multiply with NANO before dividing by scale and later divide by NANO
392 offset = ((-1LL) * (s64)data->outmin) * NANO -
393 div_s64(div_s64((s64)data->pmin * NANO, scale), NANO);
394 data->offset = div_s64_rem(offset, NANO, &data->offset2);
397 ret = devm_request_irq(dev, data->irq, mpr_eoc_handler,
398 IRQF_TRIGGER_RISING, client->name, data);
400 return dev_err_probe(dev, ret,
401 "request irq %d failed\n", data->irq);
404 data->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
406 if (IS_ERR(data->gpiod_reset))
407 return dev_err_probe(dev, PTR_ERR(data->gpiod_reset),
408 "request reset-gpio failed\n");
412 ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
413 mpr_trigger_handler, NULL);
415 return dev_err_probe(dev, ret,
416 "iio triggered buffer setup failed\n");
418 ret = devm_iio_device_register(dev, indio_dev);
420 return dev_err_probe(dev, ret,
421 "unable to register iio device\n");
426 static const struct of_device_id mpr_matches[] = {
427 { .compatible = "honeywell,mprls0025pa" },
430 MODULE_DEVICE_TABLE(of, mpr_matches);
432 static const struct i2c_device_id mpr_id[] = {
436 MODULE_DEVICE_TABLE(i2c, mpr_id);
438 static struct i2c_driver mpr_driver = {
442 .name = "mprls0025pa",
443 .of_match_table = mpr_matches,
446 module_i2c_driver(mpr_driver);
448 MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
449 MODULE_DESCRIPTION("Honeywell MPRLS0025PA I2C driver");
450 MODULE_LICENSE("GPL");