From nobody Wed Dec 17 11:47:36 2025 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id CBD68C77B73 for ; Thu, 20 Apr 2023 20:01:33 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232200AbjDTUBb (ORCPT ); Thu, 20 Apr 2023 16:01:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35518 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229963AbjDTUB3 (ORCPT ); Thu, 20 Apr 2023 16:01:29 -0400 Received: from mout.kundenserver.de (mout.kundenserver.de [212.227.126.135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BFFE3B2; Thu, 20 Apr 2023 13:01:26 -0700 (PDT) Received: from localhost ([185.104.138.31]) by mrelayeu.kundenserver.de (mreue010 [212.227.15.167]) with ESMTPSA (Nemesis) id 1MsI4Q-1q9rMb0w3e-00toK1; Thu, 20 Apr 2023 22:00:47 +0200 Date: Thu, 20 Apr 2023 22:00:45 +0200 From: Andreas Klinger To: linux-iio@vger.kernel.org, devicetree@vger.kernel.org Cc: Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Angel Iglesias , linux-kernel@vger.kernel.org Subject: [PATCH v3 2/3] iio: pressure: Honeywell mprls0025pa pressure sensor Message-ID: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Provags-ID: V03:K1:wbwTSvJ5Dbe+I/G5nnOVcZ507zZy+P2+t7FfcToAualWhg1frWn rn3pI7Qk8VGEkHiOuxscjdLuVCGPdXdIWPKR3zljENkKJvWAg3rIyuH3rBxlUuLy7o+8AT+ mI5xUUj6wOAhC1l7BYLsZP5p3n0EdCEn/jJaXx1gr1D1GnRXRv3KLfJdvOxOBfXWx7K4TID W4Tj0Tubdqb5pvO1ymJNw== UI-OutboundReport: notjunk:1;M01:P0:SmZcF8Jdo1E=;EqQ2guUqjfFyZjOXkuao9TU06CX 7Xz5ew8DibZs94ZwqwyV1ecDG6iBUWqW7ij5ThN4yDj64HNoeCn4XlHT2jj94fbMT5r7rFWEj XNOaNGrk7RRLHVQJcFOUGbikm+VzQIbHoFLl9cyfdVYDi2r0BtTXUiMv89GEv5VBkae4k5Z+1 8mQN4hFoPTOkmizfEE9hWH1bPoalG+t411zLo4zhv4m7w9vhx+255rlWXa1CTkCEBZ/RQdZM9 aiec/qWXAWQrUm8KZeYezzsZYkHRRXRp5ktlH00HhlnRHa03lcIrDJ1POrJyRZrFkNXb2ZGqn BFObaqR4JC6V/YnMakiEP5voOtGfVRhBUfGR4wofLKbDooN5aZzr1ErHXsBZESBBLu8vbZHsr nyJRPh1L25SY7p0cwPyXVbojeqJfzkxyFWWufIxq5wIjVG/Q+fBYz2iKQHYsBGJd8ybWa6Y9N 9/0TfPaL7dSIxj5k0UFhFYsPbLCbCg721VznIpxB74wUvStwSSqSubGSh8ok5ijkPjXTaQRZz cgQQXUp+sVR8WaMew+fQN1hyroO4+j3Zo5hnlcx/JtSrDqs6lTVHgJigzMjhziQfrWYXdZrn7 Ap/IUFTg+cWMM0qmnCRaJo9ao9Ohm3g1HDPZgAh+MflBZJ4LeVFuW+UXwPRKYuNd1tZf40Xo8 ecQKawbHe2PT2wsEG5lEkaVQKMs5XBSQeBa8FpijOfN53KtiYRKjwUCleDIfgOM= Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Honeywell mprls0025pa is a series of pressure sensors. Add initial I2C support. Note: - Buffered mode is supported - SPI mode is not supported Signed-off-by: Andreas Klinger --- drivers/iio/pressure/Kconfig | 12 + drivers/iio/pressure/Makefile | 1 + drivers/iio/pressure/mprls0025pa.c | 429 +++++++++++++++++++++++++++++ 3 files changed, 442 insertions(+) create mode 100644 drivers/iio/pressure/mprls0025pa.c diff --git a/drivers/iio/pressure/Kconfig b/drivers/iio/pressure/Kconfig index c9453389e4f7..c51e53bd54bc 100644 --- a/drivers/iio/pressure/Kconfig +++ b/drivers/iio/pressure/Kconfig @@ -148,6 +148,18 @@ config MPL3115 To compile this driver as a module, choose M here: the module will be called mpl3115. =20 +config MPRLS0025PA + tristate "Honeywell MPRLS0025PA (MicroPressure sensors series)" + depends on I2C + select IIO_BUFFER + select IIO_TRIGGERED_BUFFER + help + Say Y here to build support for the Honeywell MicroPressure pressure se= nsor series. + There are many different types with different pressure range. These ran= ges can be set up + in the device tree. + + To compile this driver as a module, choose M here: the module will be c= alled mpr. + config MS5611 tristate "Measurement Specialties MS5611 pressure sensor driver" select IIO_BUFFER diff --git a/drivers/iio/pressure/Makefile b/drivers/iio/pressure/Makefile index 083ae87ff48f..c90f77210e94 100644 --- a/drivers/iio/pressure/Makefile +++ b/drivers/iio/pressure/Makefile @@ -19,6 +19,7 @@ obj-$(CONFIG_MPL115) +=3D mpl115.o obj-$(CONFIG_MPL115_I2C) +=3D mpl115_i2c.o obj-$(CONFIG_MPL115_SPI) +=3D mpl115_spi.o obj-$(CONFIG_MPL3115) +=3D mpl3115.o +obj-$(CONFIG_MPRLS0025PA) +=3D mprls0025pa.o obj-$(CONFIG_MS5611) +=3D ms5611_core.o obj-$(CONFIG_MS5611_I2C) +=3D ms5611_i2c.o obj-$(CONFIG_MS5611_SPI) +=3D ms5611_spi.o diff --git a/drivers/iio/pressure/mprls0025pa.c b/drivers/iio/pressure/mprl= s0025pa.c new file mode 100644 index 000000000000..3d82f6790e6d --- /dev/null +++ b/drivers/iio/pressure/mprls0025pa.c @@ -0,0 +1,429 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * MPR - MicroPressure pressure sensor series driver + * + * Copyright (c) Andreas Klinger + * + * Data sheet: + * https://prod-edam.honeywell.com/content/dam/honeywell-edam/sps/siot/en= -us/products/sensors/ \ + * pressure-sensors/board-mount-pressure-sensors/micropressure-mpr-seri= es/documents/ \ + * sps-siot-mpr-series-datasheet-32332628-ciid-172626.pdf + * + * 7-bit I2C default slave address: 0x18 + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include + +/* bits in i2c status byte */ +#define MPR_I2C_POWER BIT(6) /* device is powered */ +#define MPR_I2C_BUSY BIT(5) /* device is busy */ +#define MPR_I2C_MEMORY BIT(2) /* integrity test passed */ +#define MPR_I2C_MATH BIT(0) /* internal math saturation */ + +#define MPR_NANO_PART 1000000000LL + +/* + * _INPUT interface: + * Calculation formular from the datasheet: + * pressure =3D (press_cnt - outputmin) * scale + pmin + * with: + * * pressure - measured pressure in Pascal + * * press_cnt - raw value read from sensor + * * pmin - minimum pressure range value of sensor (data->pmin) + * * pmax - maximum pressure range value of sensor (data->pmax) + * * outputmin - minimum numerical range raw value delivered by sensor (MP= R_OUT_MIN) + * * outputmax - maximum numerical range raw value delivered by sensor (MP= R_OUT_MAX) + * * scale - (pmax - pmin) / (outputmax - outputmin) + * + * _RAW interface: + * pressure =3D (raw + offset) * scale + * --> need to adjust offset for fitting into _RAW interface + * Values for _RAW interface: + * * raw - press_cnt + * * scale - (pmax - pmin) / (outputmax - outputmin) + * * offset - (-1 * outputmin) - pmin / scale + * note: With all sensors from the datasheet pmin =3D 0 whi= ch reduces the offset to + * (-1 * outputmin) + */ + +/* + * transfer function A: 10% to 90% of 2^24 + * transfer function B: 2.5% to 22.5% of 2^24 + * transfer function C: 20% to 80% of 2^24 + */ +enum mpr_func_id { + MPR_FUNCTION_A, + MPR_FUNCTION_B, + MPR_FUNCTION_C, +}; + +struct mpr_func_spec { + u32 output_min; + u32 output_max; +}; + +static const struct mpr_func_spec mpr_func_spec[] =3D { + [MPR_FUNCTION_A] =3D {.output_min =3D 1677722, .output_max =3D 15099494}, + [MPR_FUNCTION_B] =3D {.output_min =3D 419430, .output_max =3D 3774874}, + [MPR_FUNCTION_C] =3D {.output_min =3D 3355443, .output_max =3D 13421773}, +}; + +struct mpr_chan { + s32 pres; /* pressure value */ + s64 ts; /* timestamp */ +}; + +struct mpr_data { + struct i2c_client *client; + struct mutex lock; /* i2c transactions */ + u32 pmin; /* minimal pressure in pascal */ + u32 pmax; /* maximal pressure in pascal */ + u32 function; /* transfer function */ + u32 outmin; /* minimal numerical range raw value from sensor */ + u32 outmax; /* maximal numerical range raw value from sensor */ + int scale; /* int part of scale */ + int scale2; /* nano part of scale */ + int offset; /* int part of offset */ + int offset2; /* nano part of offset */ + struct gpio_desc *gpiod_reset; /* reset */ + int irq; /* end of conversion interrupt */ + struct completion completion; /* handshake from irq to read */ + struct mpr_chan chan; /* channel values for buffered mode */ + struct regulator *vdd; /* optional external voltage regulator */ +}; + +static const struct iio_chan_spec mpr_channels[] =3D { + { + .type =3D IIO_PRESSURE, + .info_mask_separate =3D BIT(IIO_CHAN_INFO_RAW) | + BIT(IIO_CHAN_INFO_SCALE) | + BIT(IIO_CHAN_INFO_OFFSET), + .scan_index =3D 0, + .scan_type =3D { + .sign =3D 's', + .realbits =3D 32, + .storagebits =3D 32, + .endianness =3D IIO_CPU, + }, + }, + IIO_CHAN_SOFT_TIMESTAMP(1), +}; + +static void mpr_reset(struct mpr_data *data) +{ + if (data->gpiod_reset) { + gpiod_set_value(data->gpiod_reset, 0); + udelay(10); + gpiod_set_value(data->gpiod_reset, 1); + } +} + +/** + * mpr_read_pressure() - Read pressure value from sensor via I2C + * @data: Pointer to private data struct. + * @press: Output value read from sensor. + * + * Reading from the sensor by sending and receiving I2C telegrams. + * + * If there is an end of conversion (EOC) interrupt registered the functio= n waits for a maximum of + * one second for the interrupt. + * + * Context: The function can sleep and data->lock should be held when call= ing it. + * Return: + * * 0 - OK, the pressure value could be read + * * -ETIMEDOUT - Timeout while waiting for the EOC interrupt or busy flag= is still set after nloops + * attempts of reading + */ +static int mpr_read_pressure(struct mpr_data *data, s32 *press) +{ + struct device *dev =3D &data->client->dev; + int ret, i; + u8 wdata[] =3D {0xAA, 0x00, 0x00}; + s32 status; + int nloops =3D 10; + u8 buf[5]; + + reinit_completion(&data->completion); + + ret =3D i2c_master_send(data->client, wdata, sizeof(wdata)); + if (ret < 0) { + dev_err(dev, "error while writing ret: %d\n", ret); + return ret; + } + if (ret !=3D sizeof(wdata)) { + dev_err(dev, "received size doesn't fit - ret: %d / %d\n", ret, sizeof(w= data)); + return -EIO; + } + + if (data->irq > 0) { + ret =3D wait_for_completion_timeout(&data->completion, HZ); + if (!ret) { + dev_err(dev, "timeout while waiting for eoc interrupt\n"); + return -ETIMEDOUT; + } + } else { + /* wait until status indicates data is ready */ + for (i =3D 0; i < nloops; i++) { + /* + * datasheet only says to wait at least 5 ms for the data but leave the + * maximum response time open + * --> let's try it nloops (10) times which seems to be quite long + */ + usleep_range(5000, 10000); + status =3D i2c_smbus_read_byte(data->client); + if (status < 0) { + dev_err(dev, "error while reading, status: %d\n", status); + return status; + } + if (!(status & MPR_I2C_BUSY)) + break; + } + if (i =3D=3D nloops) { + dev_err(dev, "timeout while reading\n"); + return -ETIMEDOUT; + } + } + + ret =3D i2c_master_recv(data->client, buf, sizeof(buf)); + if (ret < 0) { + dev_err(dev, "error in i2c_master_recv ret: %d\n", ret); + return ret; + } + if (ret !=3D sizeof(buf)) { + dev_err(dev, "received size doesn't fit - ret: %d / %d\n", ret, sizeof(b= uf)); + return -EIO; + } + + if (buf[0] & MPR_I2C_BUSY) { + /* it should never be the case that status still indicates business */ + dev_err(dev, "data still not ready: %08x\n", buf[0]); + return -ETIMEDOUT; + } + + *press =3D get_unaligned_be24(&buf[1]); + + dev_dbg(dev, "received: %*ph cnt: %d\n", ret, buf, *press); + + return 0; +} + +static irqreturn_t mpr_eoc_handler(int irq, void *p) +{ + struct mpr_data *data =3D (struct mpr_data *)p; + + complete(&data->completion); + + return IRQ_HANDLED; +} + +static irqreturn_t mpr_trigger_handler(int irq, void *p) +{ + int ret; + struct iio_poll_func *pf =3D p; + struct iio_dev *indio_dev =3D pf->indio_dev; + struct mpr_data *data =3D iio_priv(indio_dev); + + mutex_lock(&data->lock); + ret =3D mpr_read_pressure(data, &data->chan.pres); + if (ret < 0) { + mutex_unlock(&data->lock); + goto err; + } + + iio_push_to_buffers_with_timestamp(indio_dev, &data->chan, iio_get_time_n= s(indio_dev)); + mutex_unlock(&data->lock); + +err: + iio_trigger_notify_done(indio_dev->trig); + + return IRQ_HANDLED; +} + +static int mpr_read_raw(struct iio_dev *indio_dev, struct iio_chan_spec co= nst *chan, int *val, + int *val2, long mask) +{ + int ret; + s32 pressure; + struct mpr_data *data =3D iio_priv(indio_dev); + + if (chan->type !=3D IIO_PRESSURE) + return -EINVAL; + + switch (mask) { + case IIO_CHAN_INFO_RAW: + mutex_lock(&data->lock); + ret =3D mpr_read_pressure(data, &pressure); + mutex_unlock(&data->lock); + if (ret < 0) + return ret; + *val =3D pressure; + return IIO_VAL_INT; + case IIO_CHAN_INFO_SCALE: + *val =3D data->scale; + *val2 =3D data->scale2; + return IIO_VAL_INT_PLUS_NANO; + case IIO_CHAN_INFO_OFFSET: + *val =3D data->offset; + *val2 =3D data->offset2; + return IIO_VAL_INT_PLUS_NANO; + default: + return -EINVAL; + } + + return -EINVAL; +} + +static const struct iio_info mpr_info =3D { + .read_raw =3D &mpr_read_raw, +}; + +static void mpr_vdd_disable(void *vdd) +{ + regulator_disable(vdd); +} + +static int mpr_probe(struct i2c_client *client, const struct i2c_device_id= *id) +{ + int ret; + struct mpr_data *data; + struct iio_dev *indio_dev; + struct device *dev =3D &client->dev; + s64 scale, offset; + + dev_dbg(dev, "client: %s id: %s\n", client->name, id->name); + + if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE)) + return dev_err_probe(dev, -EOPNOTSUPP, "I2C functionality not supported\= n"); + + indio_dev =3D devm_iio_device_alloc(dev, sizeof(*data)); + if (!indio_dev) + return dev_err_probe(dev, -ENOMEM, "couldn't get iio_dev\n"); + + data =3D iio_priv(indio_dev); + data->client =3D client; + data->irq =3D client->irq; + + mutex_init(&data->lock); + init_completion(&data->completion); + + indio_dev->name =3D "mprls0025pa"; + indio_dev->info =3D &mpr_info; + indio_dev->channels =3D mpr_channels; + indio_dev->num_channels =3D ARRAY_SIZE(mpr_channels); + indio_dev->modes =3D INDIO_DIRECT_MODE; + + data->vdd =3D devm_regulator_get_optional(dev, "vdd"); + if (IS_ERR(data->vdd)) { + dev_dbg(dev, "can't get vdd supply err: %pe\n", data->vdd); + data->vdd =3D NULL; + } else { + ret =3D regulator_enable(data->vdd); + if (ret) + return dev_err_probe(dev, ret, "can't enable vdd supply\n"); + ret =3D devm_add_action_or_reset(dev, mpr_vdd_disable, data->vdd); + if (ret) + return dev_err_probe(dev, ret, "can't add vdd disable action\n"); + } + + if (dev_fwnode(dev)) { + ret =3D device_property_read_u32(dev, "honeywell,pmin-pascal", &data->pm= in); + if (ret) + return dev_err_probe(dev, ret, "honeywell,pmin-pascal missing in DT\n"); + ret =3D device_property_read_u32(dev, "honeywell,pmax-pascal", &data->pm= ax); + if (ret) + return dev_err_probe(dev, ret, "honeywell,pmax-pascal missing in DT\n"); + ret =3D device_property_read_u32(dev, "honeywell,transfer-function", &da= ta->function); + if (ret) + return dev_err_probe(dev, ret, + "honeywell,transfer-function missing in DT\n"); + if (data->function > MPR_FUNCTION_C) + return dev_err_probe(dev, -EINVAL, + "honeywell,transfer-function %d not supported\n", data->function); + + if (data->irq > 0) { + ret =3D devm_request_irq(dev, data->irq, mpr_eoc_handler, + IRQF_TRIGGER_RISING, client->name, data); + if (ret) + return dev_err_probe(dev, ret, "request irq %d failed\n", + data->irq); + } + } else { + /* when loaded as i2c device we need to use default values */ + dev_notice(dev, "no dt node found; using defaults\n"); + data->pmin =3D 0; + data->pmax =3D 172369; /* 25 psi */ + data->function =3D MPR_FUNCTION_A; + } + + data->outmin =3D mpr_func_spec[data->function].output_min; + data->outmax =3D mpr_func_spec[data->function].output_max; + + scale =3D div_s64(((s64)(data->pmax - data->pmin)) * MPR_NANO_PART, + data->outmax - data->outmin); + data->scale =3D div_s64(scale, MPR_NANO_PART); + data->scale2 =3D scale - data->scale * MPR_NANO_PART; + + offset =3D ((-1LL) * (s64)data->outmin) * MPR_NANO_PART - + div_s64(div_s64((s64)data->pmin * MPR_NANO_PART, scale), MPR_NANO_PART); + data->offset =3D div_s64(offset, MPR_NANO_PART); + data->offset2 =3D offset - data->offset * MPR_NANO_PART; + + data->gpiod_reset =3D devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_HIG= H); + if (IS_ERR(data->gpiod_reset)) { + dev_dbg(dev, "didn't get reset-gpios: err=3D%pe\n", data->gpiod_reset); + data->gpiod_reset =3D NULL; + } + + mpr_reset(data); + + ret =3D devm_iio_triggered_buffer_setup(dev, indio_dev, NULL, mpr_trigger= _handler, NULL); + if (ret) + return dev_err_probe(dev, ret, "iio triggered buffer setup failed\n"); + + ret =3D devm_iio_device_register(dev, indio_dev); + if (ret) + return dev_err_probe(dev, ret, "unable to register iio device\n"); + + return 0; +} + +static const struct of_device_id mpr_matches[] =3D { + { .compatible =3D "honeywell,mprls0025pa" }, + { } +}; +MODULE_DEVICE_TABLE(of, mpr_matches); + +static const struct i2c_device_id mpr_id[] =3D { + { "mprls0025pa" }, + { } +}; +MODULE_DEVICE_TABLE(i2c, mpr_id); + +static struct i2c_driver mpr_driver =3D { + .probe =3D mpr_probe, + .id_table =3D mpr_id, + .driver =3D { + .name =3D "mprls0025pa", + .of_match_table =3D mpr_matches, + }, +}; +module_i2c_driver(mpr_driver); + +MODULE_AUTHOR("Andreas Klinger "); +MODULE_DESCRIPTION("Honeywell MPR series I2C driver"); +MODULE_LICENSE("GPL"); --=20 2.30.2