xref: /openbmc/linux/drivers/iio/pressure/mprls0025pa.c (revision 5ee9cd065836e5934710ca35653bce7905add20b)
1713337d9SAndreas Klinger // SPDX-License-Identifier: GPL-2.0-or-later
2713337d9SAndreas Klinger /*
3713337d9SAndreas Klinger  * MPRLS0025PA - Honeywell MicroPressure pressure sensor series driver
4713337d9SAndreas Klinger  *
5713337d9SAndreas Klinger  * Copyright (c) Andreas Klinger <ak@it-klinger.de>
6713337d9SAndreas Klinger  *
7713337d9SAndreas Klinger  * Data sheet:
8713337d9SAndreas Klinger  *  https://prod-edam.honeywell.com/content/dam/honeywell-edam/sps/siot/en-us/
9713337d9SAndreas Klinger  *    products/sensors/pressure-sensors/board-mount-pressure-sensors/
10713337d9SAndreas Klinger  *    micropressure-mpr-series/documents/
11713337d9SAndreas Klinger  *    sps-siot-mpr-series-datasheet-32332628-ciid-172626.pdf
12713337d9SAndreas Klinger  *
13713337d9SAndreas Klinger  * 7-bit I2C default slave address: 0x18
14713337d9SAndreas Klinger  */
15713337d9SAndreas Klinger 
16713337d9SAndreas Klinger #include <linux/delay.h>
17713337d9SAndreas Klinger #include <linux/device.h>
18713337d9SAndreas Klinger #include <linux/i2c.h>
19713337d9SAndreas Klinger #include <linux/math64.h>
20713337d9SAndreas Klinger #include <linux/mod_devicetable.h>
21713337d9SAndreas Klinger #include <linux/module.h>
22713337d9SAndreas Klinger #include <linux/property.h>
23713337d9SAndreas Klinger #include <linux/units.h>
24713337d9SAndreas Klinger 
25713337d9SAndreas Klinger #include <linux/gpio/consumer.h>
26713337d9SAndreas Klinger 
27713337d9SAndreas Klinger #include <linux/iio/buffer.h>
28713337d9SAndreas Klinger #include <linux/iio/iio.h>
29713337d9SAndreas Klinger #include <linux/iio/trigger_consumer.h>
30713337d9SAndreas Klinger #include <linux/iio/triggered_buffer.h>
31713337d9SAndreas Klinger 
32713337d9SAndreas Klinger #include <linux/regulator/consumer.h>
33713337d9SAndreas Klinger 
34713337d9SAndreas Klinger #include <asm/unaligned.h>
35713337d9SAndreas Klinger 
36713337d9SAndreas Klinger /* bits in i2c status byte */
37713337d9SAndreas Klinger #define MPR_I2C_POWER	BIT(6)	/* device is powered */
38713337d9SAndreas Klinger #define MPR_I2C_BUSY	BIT(5)	/* device is busy */
39713337d9SAndreas Klinger #define MPR_I2C_MEMORY	BIT(2)	/* integrity test passed */
40713337d9SAndreas Klinger #define MPR_I2C_MATH	BIT(0)	/* internal math saturation */
41713337d9SAndreas Klinger 
42713337d9SAndreas Klinger /*
43713337d9SAndreas Klinger  * support _RAW sysfs interface:
44713337d9SAndreas Klinger  *
45713337d9SAndreas Klinger  * Calculation formula from the datasheet:
46713337d9SAndreas Klinger  * pressure = (press_cnt - outputmin) * scale + pmin
47713337d9SAndreas Klinger  * with:
48713337d9SAndreas Klinger  * * pressure	- measured pressure in Pascal
49713337d9SAndreas Klinger  * * press_cnt	- raw value read from sensor
50713337d9SAndreas Klinger  * * pmin	- minimum pressure range value of sensor (data->pmin)
51713337d9SAndreas Klinger  * * pmax	- maximum pressure range value of sensor (data->pmax)
52713337d9SAndreas Klinger  * * outputmin	- minimum numerical range raw value delivered by sensor
53713337d9SAndreas Klinger  *						(mpr_func_spec.output_min)
54713337d9SAndreas Klinger  * * outputmax	- maximum numerical range raw value delivered by sensor
55713337d9SAndreas Klinger  *						(mpr_func_spec.output_max)
56713337d9SAndreas Klinger  * * scale	- (pmax - pmin) / (outputmax - outputmin)
57713337d9SAndreas Klinger  *
58713337d9SAndreas Klinger  * formula of the userspace:
59713337d9SAndreas Klinger  * pressure = (raw + offset) * scale
60713337d9SAndreas Klinger  *
61713337d9SAndreas Klinger  * Values given to the userspace in sysfs interface:
62713337d9SAndreas Klinger  * * raw	- press_cnt
63713337d9SAndreas Klinger  * * offset	- (-1 * outputmin) - pmin / scale
64713337d9SAndreas Klinger  *                note: With all sensors from the datasheet pmin = 0
65713337d9SAndreas Klinger  *                which reduces the offset to (-1 * outputmin)
66713337d9SAndreas Klinger  */
67713337d9SAndreas Klinger 
68713337d9SAndreas Klinger /*
69713337d9SAndreas Klinger  * transfer function A: 10%   to 90%   of 2^24
70713337d9SAndreas Klinger  * transfer function B:  2.5% to 22.5% of 2^24
71713337d9SAndreas Klinger  * transfer function C: 20%   to 80%   of 2^24
72713337d9SAndreas Klinger  */
73713337d9SAndreas Klinger enum mpr_func_id {
74713337d9SAndreas Klinger 	MPR_FUNCTION_A,
75713337d9SAndreas Klinger 	MPR_FUNCTION_B,
76713337d9SAndreas Klinger 	MPR_FUNCTION_C,
77713337d9SAndreas Klinger };
78713337d9SAndreas Klinger 
79713337d9SAndreas Klinger struct mpr_func_spec {
80713337d9SAndreas Klinger 	u32			output_min;
81713337d9SAndreas Klinger 	u32			output_max;
82713337d9SAndreas Klinger };
83713337d9SAndreas Klinger 
84713337d9SAndreas Klinger static const struct mpr_func_spec mpr_func_spec[] = {
85713337d9SAndreas Klinger 	[MPR_FUNCTION_A] = {.output_min = 1677722, .output_max = 15099494},
86713337d9SAndreas Klinger 	[MPR_FUNCTION_B] = {.output_min =  419430, .output_max =  3774874},
87713337d9SAndreas Klinger 	[MPR_FUNCTION_C] = {.output_min = 3355443, .output_max = 13421773},
88713337d9SAndreas Klinger };
89713337d9SAndreas Klinger 
90713337d9SAndreas Klinger struct mpr_chan {
91713337d9SAndreas Klinger 	s32			pres;		/* pressure value */
92713337d9SAndreas Klinger 	s64			ts;		/* timestamp */
93713337d9SAndreas Klinger };
94713337d9SAndreas Klinger 
95713337d9SAndreas Klinger struct mpr_data {
96713337d9SAndreas Klinger 	struct i2c_client	*client;
97713337d9SAndreas Klinger 	struct mutex		lock;		/*
98713337d9SAndreas Klinger 						 * access to device during read
99713337d9SAndreas Klinger 						 */
100713337d9SAndreas Klinger 	u32			pmin;		/* minimal pressure in pascal */
101713337d9SAndreas Klinger 	u32			pmax;		/* maximal pressure in pascal */
102713337d9SAndreas Klinger 	enum mpr_func_id	function;	/* transfer function */
103713337d9SAndreas Klinger 	u32			outmin;		/*
104713337d9SAndreas Klinger 						 * minimal numerical range raw
105713337d9SAndreas Klinger 						 * value from sensor
106713337d9SAndreas Klinger 						 */
107713337d9SAndreas Klinger 	u32			outmax;		/*
108713337d9SAndreas Klinger 						 * maximal numerical range raw
109713337d9SAndreas Klinger 						 * value from sensor
110713337d9SAndreas Klinger 						 */
111713337d9SAndreas Klinger 	int                     scale;          /* int part of scale */
112713337d9SAndreas Klinger 	int                     scale2;         /* nano part of scale */
113713337d9SAndreas Klinger 	int                     offset;         /* int part of offset */
114713337d9SAndreas Klinger 	int                     offset2;        /* nano part of offset */
115713337d9SAndreas Klinger 	struct gpio_desc	*gpiod_reset;	/* reset */
116713337d9SAndreas Klinger 	int			irq;		/*
117713337d9SAndreas Klinger 						 * end of conversion irq;
118713337d9SAndreas Klinger 						 * used to distinguish between
119713337d9SAndreas Klinger 						 * irq mode and reading in a
120713337d9SAndreas Klinger 						 * loop until data is ready
121713337d9SAndreas Klinger 						 */
122713337d9SAndreas Klinger 	struct completion	completion;	/* handshake from irq to read */
123713337d9SAndreas Klinger 	struct mpr_chan		chan;		/*
124713337d9SAndreas Klinger 						 * channel values for buffered
125713337d9SAndreas Klinger 						 * mode
126713337d9SAndreas Klinger 						 */
127713337d9SAndreas Klinger };
128713337d9SAndreas Klinger 
129713337d9SAndreas Klinger static const struct iio_chan_spec mpr_channels[] = {
130713337d9SAndreas Klinger 	{
131713337d9SAndreas Klinger 		.type = IIO_PRESSURE,
132713337d9SAndreas Klinger 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
133713337d9SAndreas Klinger 					BIT(IIO_CHAN_INFO_SCALE) |
134713337d9SAndreas Klinger 					BIT(IIO_CHAN_INFO_OFFSET),
135713337d9SAndreas Klinger 		.scan_index = 0,
136713337d9SAndreas Klinger 		.scan_type = {
137713337d9SAndreas Klinger 			.sign = 's',
138713337d9SAndreas Klinger 			.realbits = 32,
139713337d9SAndreas Klinger 			.storagebits = 32,
140713337d9SAndreas Klinger 			.endianness = IIO_CPU,
141713337d9SAndreas Klinger 		},
142713337d9SAndreas Klinger 	},
143713337d9SAndreas Klinger 	IIO_CHAN_SOFT_TIMESTAMP(1),
144713337d9SAndreas Klinger };
145713337d9SAndreas Klinger 
mpr_reset(struct mpr_data * data)146713337d9SAndreas Klinger static void mpr_reset(struct mpr_data *data)
147713337d9SAndreas Klinger {
148713337d9SAndreas Klinger 	if (data->gpiod_reset) {
149713337d9SAndreas Klinger 		gpiod_set_value(data->gpiod_reset, 0);
150713337d9SAndreas Klinger 		udelay(10);
151713337d9SAndreas Klinger 		gpiod_set_value(data->gpiod_reset, 1);
152713337d9SAndreas Klinger 	}
153713337d9SAndreas Klinger }
154713337d9SAndreas Klinger 
155713337d9SAndreas Klinger /**
156713337d9SAndreas Klinger  * mpr_read_pressure() - Read pressure value from sensor via I2C
157713337d9SAndreas Klinger  * @data: Pointer to private data struct.
158713337d9SAndreas Klinger  * @press: Output value read from sensor.
159713337d9SAndreas Klinger  *
160713337d9SAndreas Klinger  * Reading from the sensor by sending and receiving I2C telegrams.
161713337d9SAndreas Klinger  *
162713337d9SAndreas Klinger  * If there is an end of conversion (EOC) interrupt registered the function
163713337d9SAndreas Klinger  * waits for a maximum of one second for the interrupt.
164713337d9SAndreas Klinger  *
165713337d9SAndreas Klinger  * Context: The function can sleep and data->lock should be held when calling it
166713337d9SAndreas Klinger  * Return:
167713337d9SAndreas Klinger  * * 0		- OK, the pressure value could be read
168713337d9SAndreas Klinger  * * -ETIMEDOUT	- Timeout while waiting for the EOC interrupt or busy flag is
169713337d9SAndreas Klinger  *		  still set after nloops attempts of reading
170713337d9SAndreas Klinger  */
mpr_read_pressure(struct mpr_data * data,s32 * press)171713337d9SAndreas Klinger static int mpr_read_pressure(struct mpr_data *data, s32 *press)
172713337d9SAndreas Klinger {
173713337d9SAndreas Klinger 	struct device *dev = &data->client->dev;
174713337d9SAndreas Klinger 	int ret, i;
175713337d9SAndreas Klinger 	u8 wdata[] = {0xAA, 0x00, 0x00};
176713337d9SAndreas Klinger 	s32 status;
177713337d9SAndreas Klinger 	int nloops = 10;
178713337d9SAndreas Klinger 	u8 buf[4];
179713337d9SAndreas Klinger 
180713337d9SAndreas Klinger 	reinit_completion(&data->completion);
181713337d9SAndreas Klinger 
182713337d9SAndreas Klinger 	ret = i2c_master_send(data->client, wdata, sizeof(wdata));
183713337d9SAndreas Klinger 	if (ret < 0) {
184713337d9SAndreas Klinger 		dev_err(dev, "error while writing ret: %d\n", ret);
185713337d9SAndreas Klinger 		return ret;
186713337d9SAndreas Klinger 	}
187713337d9SAndreas Klinger 	if (ret != sizeof(wdata)) {
188713337d9SAndreas Klinger 		dev_err(dev, "received size doesn't fit - ret: %d / %u\n", ret,
189713337d9SAndreas Klinger 							(u32)sizeof(wdata));
190713337d9SAndreas Klinger 		return -EIO;
191713337d9SAndreas Klinger 	}
192713337d9SAndreas Klinger 
193713337d9SAndreas Klinger 	if (data->irq > 0) {
194713337d9SAndreas Klinger 		ret = wait_for_completion_timeout(&data->completion, HZ);
195713337d9SAndreas Klinger 		if (!ret) {
196713337d9SAndreas Klinger 			dev_err(dev, "timeout while waiting for eoc irq\n");
197713337d9SAndreas Klinger 			return -ETIMEDOUT;
198713337d9SAndreas Klinger 		}
199713337d9SAndreas Klinger 	} else {
200713337d9SAndreas Klinger 		/* wait until status indicates data is ready */
201713337d9SAndreas Klinger 		for (i = 0; i < nloops; i++) {
202713337d9SAndreas Klinger 			/*
203713337d9SAndreas Klinger 			 * datasheet only says to wait at least 5 ms for the
204713337d9SAndreas Klinger 			 * data but leave the maximum response time open
205713337d9SAndreas Klinger 			 * --> let's try it nloops (10) times which seems to be
206713337d9SAndreas Klinger 			 *     quite long
207713337d9SAndreas Klinger 			 */
208713337d9SAndreas Klinger 			usleep_range(5000, 10000);
209713337d9SAndreas Klinger 			status = i2c_smbus_read_byte(data->client);
210713337d9SAndreas Klinger 			if (status < 0) {
211713337d9SAndreas Klinger 				dev_err(dev,
212713337d9SAndreas Klinger 					"error while reading, status: %d\n",
213713337d9SAndreas Klinger 					status);
214713337d9SAndreas Klinger 				return status;
215713337d9SAndreas Klinger 			}
216713337d9SAndreas Klinger 			if (!(status & MPR_I2C_BUSY))
217713337d9SAndreas Klinger 				break;
218713337d9SAndreas Klinger 		}
219713337d9SAndreas Klinger 		if (i == nloops) {
220713337d9SAndreas Klinger 			dev_err(dev, "timeout while reading\n");
221713337d9SAndreas Klinger 			return -ETIMEDOUT;
222713337d9SAndreas Klinger 		}
223713337d9SAndreas Klinger 	}
224713337d9SAndreas Klinger 
225713337d9SAndreas Klinger 	ret = i2c_master_recv(data->client, buf, sizeof(buf));
226713337d9SAndreas Klinger 	if (ret < 0) {
227713337d9SAndreas Klinger 		dev_err(dev, "error in i2c_master_recv ret: %d\n", ret);
228713337d9SAndreas Klinger 		return ret;
229713337d9SAndreas Klinger 	}
230713337d9SAndreas Klinger 	if (ret != sizeof(buf)) {
231713337d9SAndreas Klinger 		dev_err(dev, "received size doesn't fit - ret: %d / %u\n", ret,
232713337d9SAndreas Klinger 							(u32)sizeof(buf));
233713337d9SAndreas Klinger 		return -EIO;
234713337d9SAndreas Klinger 	}
235713337d9SAndreas Klinger 
236713337d9SAndreas Klinger 	if (buf[0] & MPR_I2C_BUSY) {
237713337d9SAndreas Klinger 		/*
238713337d9SAndreas Klinger 		 * it should never be the case that status still indicates
239713337d9SAndreas Klinger 		 * business
240713337d9SAndreas Klinger 		 */
241713337d9SAndreas Klinger 		dev_err(dev, "data still not ready: %08x\n", buf[0]);
242713337d9SAndreas Klinger 		return -ETIMEDOUT;
243713337d9SAndreas Klinger 	}
244713337d9SAndreas Klinger 
245713337d9SAndreas Klinger 	*press = get_unaligned_be24(&buf[1]);
246713337d9SAndreas Klinger 
247713337d9SAndreas Klinger 	dev_dbg(dev, "received: %*ph cnt: %d\n", ret, buf, *press);
248713337d9SAndreas Klinger 
249713337d9SAndreas Klinger 	return 0;
250713337d9SAndreas Klinger }
251713337d9SAndreas Klinger 
mpr_eoc_handler(int irq,void * p)252713337d9SAndreas Klinger static irqreturn_t mpr_eoc_handler(int irq, void *p)
253713337d9SAndreas Klinger {
254713337d9SAndreas Klinger 	struct mpr_data *data = p;
255713337d9SAndreas Klinger 
256713337d9SAndreas Klinger 	complete(&data->completion);
257713337d9SAndreas Klinger 
258713337d9SAndreas Klinger 	return IRQ_HANDLED;
259713337d9SAndreas Klinger }
260713337d9SAndreas Klinger 
mpr_trigger_handler(int irq,void * p)261713337d9SAndreas Klinger static irqreturn_t mpr_trigger_handler(int irq, void *p)
262713337d9SAndreas Klinger {
263713337d9SAndreas Klinger 	int ret;
264713337d9SAndreas Klinger 	struct iio_poll_func *pf = p;
265713337d9SAndreas Klinger 	struct iio_dev *indio_dev = pf->indio_dev;
266713337d9SAndreas Klinger 	struct mpr_data *data = iio_priv(indio_dev);
267713337d9SAndreas Klinger 
268713337d9SAndreas Klinger 	mutex_lock(&data->lock);
269713337d9SAndreas Klinger 	ret = mpr_read_pressure(data, &data->chan.pres);
270713337d9SAndreas Klinger 	if (ret < 0)
271713337d9SAndreas Klinger 		goto err;
272713337d9SAndreas Klinger 
273713337d9SAndreas Klinger 	iio_push_to_buffers_with_timestamp(indio_dev, &data->chan,
274713337d9SAndreas Klinger 						iio_get_time_ns(indio_dev));
275713337d9SAndreas Klinger 
276713337d9SAndreas Klinger err:
277713337d9SAndreas Klinger 	mutex_unlock(&data->lock);
278713337d9SAndreas Klinger 	iio_trigger_notify_done(indio_dev->trig);
279713337d9SAndreas Klinger 
280713337d9SAndreas Klinger 	return IRQ_HANDLED;
281713337d9SAndreas Klinger }
282713337d9SAndreas Klinger 
mpr_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)283713337d9SAndreas Klinger static int mpr_read_raw(struct iio_dev *indio_dev,
284713337d9SAndreas Klinger 	struct iio_chan_spec const *chan, int *val, int *val2, long mask)
285713337d9SAndreas Klinger {
286713337d9SAndreas Klinger 	int ret;
287713337d9SAndreas Klinger 	s32 pressure;
288713337d9SAndreas Klinger 	struct mpr_data *data = iio_priv(indio_dev);
289713337d9SAndreas Klinger 
290713337d9SAndreas Klinger 	if (chan->type != IIO_PRESSURE)
291713337d9SAndreas Klinger 		return -EINVAL;
292713337d9SAndreas Klinger 
293713337d9SAndreas Klinger 	switch (mask) {
294713337d9SAndreas Klinger 	case IIO_CHAN_INFO_RAW:
295713337d9SAndreas Klinger 		mutex_lock(&data->lock);
296713337d9SAndreas Klinger 		ret = mpr_read_pressure(data, &pressure);
297713337d9SAndreas Klinger 		mutex_unlock(&data->lock);
298713337d9SAndreas Klinger 		if (ret < 0)
299713337d9SAndreas Klinger 			return ret;
300713337d9SAndreas Klinger 		*val = pressure;
301713337d9SAndreas Klinger 		return IIO_VAL_INT;
302713337d9SAndreas Klinger 	case IIO_CHAN_INFO_SCALE:
303713337d9SAndreas Klinger 		*val = data->scale;
304713337d9SAndreas Klinger 		*val2 = data->scale2;
305713337d9SAndreas Klinger 		return IIO_VAL_INT_PLUS_NANO;
306713337d9SAndreas Klinger 	case IIO_CHAN_INFO_OFFSET:
307713337d9SAndreas Klinger 		*val = data->offset;
308713337d9SAndreas Klinger 		*val2 = data->offset2;
309713337d9SAndreas Klinger 		return IIO_VAL_INT_PLUS_NANO;
310713337d9SAndreas Klinger 	default:
311713337d9SAndreas Klinger 		return -EINVAL;
312713337d9SAndreas Klinger 	}
313713337d9SAndreas Klinger }
314713337d9SAndreas Klinger 
315713337d9SAndreas Klinger static const struct iio_info mpr_info = {
316713337d9SAndreas Klinger 	.read_raw = &mpr_read_raw,
317713337d9SAndreas Klinger };
318713337d9SAndreas Klinger 
mpr_probe(struct i2c_client * client)319713337d9SAndreas Klinger static int mpr_probe(struct i2c_client *client)
320713337d9SAndreas Klinger {
321713337d9SAndreas Klinger 	int ret;
322713337d9SAndreas Klinger 	struct mpr_data *data;
323713337d9SAndreas Klinger 	struct iio_dev *indio_dev;
324713337d9SAndreas Klinger 	struct device *dev = &client->dev;
325713337d9SAndreas Klinger 	s64 scale, offset;
326*330e860aSPetre Rodan 	u32 func;
327713337d9SAndreas Klinger 
328713337d9SAndreas Klinger 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE))
329713337d9SAndreas Klinger 		return dev_err_probe(dev, -EOPNOTSUPP,
330713337d9SAndreas Klinger 					"I2C functionality not supported\n");
331713337d9SAndreas Klinger 
332713337d9SAndreas Klinger 	indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
333713337d9SAndreas Klinger 	if (!indio_dev)
334713337d9SAndreas Klinger 		return dev_err_probe(dev, -ENOMEM, "couldn't get iio_dev\n");
335713337d9SAndreas Klinger 
336713337d9SAndreas Klinger 	data = iio_priv(indio_dev);
337713337d9SAndreas Klinger 	data->client = client;
338713337d9SAndreas Klinger 	data->irq = client->irq;
339713337d9SAndreas Klinger 
340713337d9SAndreas Klinger 	mutex_init(&data->lock);
341713337d9SAndreas Klinger 	init_completion(&data->completion);
342713337d9SAndreas Klinger 
343713337d9SAndreas Klinger 	indio_dev->name = "mprls0025pa";
344713337d9SAndreas Klinger 	indio_dev->info = &mpr_info;
345713337d9SAndreas Klinger 	indio_dev->channels = mpr_channels;
346713337d9SAndreas Klinger 	indio_dev->num_channels = ARRAY_SIZE(mpr_channels);
347713337d9SAndreas Klinger 	indio_dev->modes = INDIO_DIRECT_MODE;
348713337d9SAndreas Klinger 
349713337d9SAndreas Klinger 	ret = devm_regulator_get_enable(dev, "vdd");
350713337d9SAndreas Klinger 	if (ret)
351713337d9SAndreas Klinger 		return dev_err_probe(dev, ret,
352713337d9SAndreas Klinger 				"can't get and enable vdd supply\n");
353713337d9SAndreas Klinger 
354713337d9SAndreas Klinger 	if (dev_fwnode(dev)) {
355713337d9SAndreas Klinger 		ret = device_property_read_u32(dev, "honeywell,pmin-pascal",
356713337d9SAndreas Klinger 								&data->pmin);
357713337d9SAndreas Klinger 		if (ret)
358713337d9SAndreas Klinger 			return dev_err_probe(dev, ret,
359713337d9SAndreas Klinger 				"honeywell,pmin-pascal could not be read\n");
360713337d9SAndreas Klinger 		ret = device_property_read_u32(dev, "honeywell,pmax-pascal",
361713337d9SAndreas Klinger 								&data->pmax);
362713337d9SAndreas Klinger 		if (ret)
363713337d9SAndreas Klinger 			return dev_err_probe(dev, ret,
364713337d9SAndreas Klinger 				"honeywell,pmax-pascal could not be read\n");
365713337d9SAndreas Klinger 		ret = device_property_read_u32(dev,
366*330e860aSPetre Rodan 				"honeywell,transfer-function", &func);
367713337d9SAndreas Klinger 		if (ret)
368713337d9SAndreas Klinger 			return dev_err_probe(dev, ret,
369713337d9SAndreas Klinger 				"honeywell,transfer-function could not be read\n");
370*330e860aSPetre Rodan 		data->function = func - 1;
371713337d9SAndreas Klinger 		if (data->function > MPR_FUNCTION_C)
372713337d9SAndreas Klinger 			return dev_err_probe(dev, -EINVAL,
373713337d9SAndreas Klinger 				"honeywell,transfer-function %d invalid\n",
374713337d9SAndreas Klinger 								data->function);
375713337d9SAndreas Klinger 	} else {
376713337d9SAndreas Klinger 		/* when loaded as i2c device we need to use default values */
377713337d9SAndreas Klinger 		dev_notice(dev, "firmware node not found; using defaults\n");
378713337d9SAndreas Klinger 		data->pmin = 0;
379713337d9SAndreas Klinger 		data->pmax = 172369; /* 25 psi */
380713337d9SAndreas Klinger 		data->function = MPR_FUNCTION_A;
381713337d9SAndreas Klinger 	}
382713337d9SAndreas Klinger 
383713337d9SAndreas Klinger 	data->outmin = mpr_func_spec[data->function].output_min;
384713337d9SAndreas Klinger 	data->outmax = mpr_func_spec[data->function].output_max;
385713337d9SAndreas Klinger 
386713337d9SAndreas Klinger 	/* use 64 bit calculation for preserving a reasonable precision */
387713337d9SAndreas Klinger 	scale = div_s64(((s64)(data->pmax - data->pmin)) * NANO,
388713337d9SAndreas Klinger 						data->outmax - data->outmin);
389713337d9SAndreas Klinger 	data->scale = div_s64_rem(scale, NANO, &data->scale2);
390713337d9SAndreas Klinger 	/*
391713337d9SAndreas Klinger 	 * multiply with NANO before dividing by scale and later divide by NANO
392713337d9SAndreas Klinger 	 * again.
393713337d9SAndreas Klinger 	 */
394713337d9SAndreas Klinger 	offset = ((-1LL) * (s64)data->outmin) * NANO -
395713337d9SAndreas Klinger 			div_s64(div_s64((s64)data->pmin * NANO, scale), NANO);
396713337d9SAndreas Klinger 	data->offset = div_s64_rem(offset, NANO, &data->offset2);
397713337d9SAndreas Klinger 
398713337d9SAndreas Klinger 	if (data->irq > 0) {
399713337d9SAndreas Klinger 		ret = devm_request_irq(dev, data->irq, mpr_eoc_handler,
400713337d9SAndreas Klinger 				IRQF_TRIGGER_RISING, client->name, data);
401713337d9SAndreas Klinger 		if (ret)
402713337d9SAndreas Klinger 			return dev_err_probe(dev, ret,
403713337d9SAndreas Klinger 				"request irq %d failed\n", data->irq);
404713337d9SAndreas Klinger 	}
405713337d9SAndreas Klinger 
406713337d9SAndreas Klinger 	data->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
407713337d9SAndreas Klinger 							GPIOD_OUT_HIGH);
408713337d9SAndreas Klinger 	if (IS_ERR(data->gpiod_reset))
409713337d9SAndreas Klinger 		return dev_err_probe(dev, PTR_ERR(data->gpiod_reset),
410713337d9SAndreas Klinger 						"request reset-gpio failed\n");
411713337d9SAndreas Klinger 
412713337d9SAndreas Klinger 	mpr_reset(data);
413713337d9SAndreas Klinger 
414713337d9SAndreas Klinger 	ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
415713337d9SAndreas Klinger 						mpr_trigger_handler, NULL);
416713337d9SAndreas Klinger 	if (ret)
417713337d9SAndreas Klinger 		return dev_err_probe(dev, ret,
418713337d9SAndreas Klinger 					"iio triggered buffer setup failed\n");
419713337d9SAndreas Klinger 
420713337d9SAndreas Klinger 	ret = devm_iio_device_register(dev, indio_dev);
421713337d9SAndreas Klinger 	if (ret)
422713337d9SAndreas Klinger 		return dev_err_probe(dev, ret,
423713337d9SAndreas Klinger 					"unable to register iio device\n");
424713337d9SAndreas Klinger 
425713337d9SAndreas Klinger 	return 0;
426713337d9SAndreas Klinger }
427713337d9SAndreas Klinger 
428713337d9SAndreas Klinger static const struct of_device_id mpr_matches[] = {
429713337d9SAndreas Klinger 	{ .compatible = "honeywell,mprls0025pa" },
430713337d9SAndreas Klinger 	{ }
431713337d9SAndreas Klinger };
432713337d9SAndreas Klinger MODULE_DEVICE_TABLE(of, mpr_matches);
433713337d9SAndreas Klinger 
434713337d9SAndreas Klinger static const struct i2c_device_id mpr_id[] = {
435713337d9SAndreas Klinger 	{ "mprls0025pa" },
436713337d9SAndreas Klinger 	{ }
437713337d9SAndreas Klinger };
438713337d9SAndreas Klinger MODULE_DEVICE_TABLE(i2c, mpr_id);
439713337d9SAndreas Klinger 
440713337d9SAndreas Klinger static struct i2c_driver mpr_driver = {
441713337d9SAndreas Klinger 	.probe		= mpr_probe,
442713337d9SAndreas Klinger 	.id_table	= mpr_id,
443713337d9SAndreas Klinger 	.driver		= {
444713337d9SAndreas Klinger 		.name		= "mprls0025pa",
445713337d9SAndreas Klinger 		.of_match_table = mpr_matches,
446713337d9SAndreas Klinger 	},
447713337d9SAndreas Klinger };
448713337d9SAndreas Klinger module_i2c_driver(mpr_driver);
449713337d9SAndreas Klinger 
450713337d9SAndreas Klinger MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
451713337d9SAndreas Klinger MODULE_DESCRIPTION("Honeywell MPRLS0025PA I2C driver");
452713337d9SAndreas Klinger MODULE_LICENSE("GPL");
453