1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* MPRLS0025PA - Honeywell MicroPressure pressure sensor series driver
*
* Copyright (c) Andreas Klinger <ak@it-klinger.de>
*
* 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-series/documents/
* sps-siot-mpr-series-datasheet-32332628-ciid-172626.pdf
*
* 7-bit I2C default slave address: 0x18
*/
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/i2c.h>
#include <linux/math64.h>
#include <linux/mod_devicetable.h>
#include <linux/module.h>
#include <linux/property.h>
#include <linux/units.h>
#include <linux/gpio/consumer.h>
#include <linux/iio/buffer.h>
#include <linux/iio/iio.h>
#include <linux/iio/trigger_consumer.h>
#include <linux/iio/triggered_buffer.h>
#include <linux/regulator/consumer.h>
#include <asm/unaligned.h>
/* 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_I2C_ERR_FLAG (MPR_I2C_BUSY | MPR_I2C_MEMORY | MPR_I2C_MATH)
/*
* support _RAW sysfs interface:
*
* Calculation formula from the datasheet:
* pressure = (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
* (mpr_func_spec.output_min)
* * outputmax - maximum numerical range raw value delivered by sensor
* (mpr_func_spec.output_max)
* * scale - (pmax - pmin) / (outputmax - outputmin)
*
* formula of the userspace:
* pressure = (raw + offset) * scale
*
* Values given to the userspace in sysfs interface:
* * raw - press_cnt
* * offset - (-1 * outputmin) - pmin / scale
* note: With all sensors from the datasheet pmin = 0
* which 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[] = {
[MPR_FUNCTION_A] = {.output_min = 1677722, .output_max = 15099494},
[MPR_FUNCTION_B] = {.output_min = 419430, .output_max = 3774874},
[MPR_FUNCTION_C] = {.output_min = 3355443, .output_max = 13421773},
};
struct mpr_chan {
s32 pres; /* pressure value */
s64 ts; /* timestamp */
};
struct mpr_data {
struct i2c_client *client;
struct mutex lock; /*
* access to device during read
*/
u32 pmin; /* minimal pressure in pascal */
u32 pmax; /* maximal pressure in pascal */
enum mpr_func_id 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 irq;
* used to distinguish between
* irq mode and reading in a
* loop until data is ready
*/
struct completion completion; /* handshake from irq to read */
struct mpr_chan chan; /*
* channel values for buffered
* mode
*/
};
static const struct iio_chan_spec mpr_channels[] = {
{
.type = IIO_PRESSURE,
.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
BIT(IIO_CHAN_INFO_SCALE) |
BIT(IIO_CHAN_INFO_OFFSET),
.scan_index = 0,
.scan_type = {
.sign = 's',
.realbits = 32,
.storagebits = 32,
.endianness = 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 function
* waits for a maximum of one second for the interrupt.
*
* Context: The function can sleep and data->lock should be held when calling 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 = &data->client->dev;
int ret, i;
u8 wdata[] = {0xAA, 0x00, 0x00};
s32 status;
int nloops = 10;
u8 buf[4];
reinit_completion(&data->completion);
ret = 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 != sizeof(wdata)) {
dev_err(dev, "received size doesn't fit - ret: %d / %u\n", ret,
(u32)sizeof(wdata));
return -EIO;
}
if (data->irq > 0) {
ret = wait_for_completion_timeout(&data->completion, HZ);
if (!ret) {
dev_err(dev, "timeout while waiting for eoc irq\n");
return -ETIMEDOUT;
}
} else {
/* wait until status indicates data is ready */
for (i = 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 = 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_ERR_FLAG))
break;
}
if (i == nloops) {
dev_err(dev, "timeout while reading\n");
return -ETIMEDOUT;
}
}
ret = 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 != sizeof(buf)) {
dev_err(dev, "received size doesn't fit - ret: %d / %u\n", ret,
(u32)sizeof(buf));
return -EIO;
}
if (buf[0] & MPR_I2C_ERR_FLAG) {
/*
* 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 = 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 = p;
complete(&data->completion);
return IRQ_HANDLED;
}
static irqreturn_t mpr_trigger_handler(int irq, void *p)
{
int ret;
struct iio_poll_func *pf = p;
struct iio_dev *indio_dev = pf->indio_dev;
struct mpr_data *data = iio_priv(indio_dev);
mutex_lock(&data->lock);
ret = mpr_read_pressure(data, &data->chan.pres);
if (ret < 0)
goto err;
iio_push_to_buffers_with_timestamp(indio_dev, &data->chan,
iio_get_time_ns(indio_dev));
err:
mutex_unlock(&data->lock);
iio_trigger_notify_done(indio_dev->trig);
return IRQ_HANDLED;
}
static int mpr_read_raw(struct iio_dev *indio_dev,
struct iio_chan_spec const *chan, int *val, int *val2, long mask)
{
int ret;
s32 pressure;
struct mpr_data *data = iio_priv(indio_dev);
if (chan->type != IIO_PRESSURE)
return -EINVAL;
switch (mask) {
case IIO_CHAN_INFO_RAW:
mutex_lock(&data->lock);
ret = mpr_read_pressure(data, &pressure);
mutex_unlock(&data->lock);
if (ret < 0)
return ret;
*val = pressure;
return IIO_VAL_INT;
case IIO_CHAN_INFO_SCALE:
*val = data->scale;
*val2 = data->scale2;
return IIO_VAL_INT_PLUS_NANO;
case IIO_CHAN_INFO_OFFSET:
*val = data->offset;
*val2 = data->offset2;
return IIO_VAL_INT_PLUS_NANO;
default:
return -EINVAL;
}
}
static const struct iio_info mpr_info = {
.read_raw = &mpr_read_raw,
};
static int mpr_probe(struct i2c_client *client)
{
int ret;
struct mpr_data *data;
struct iio_dev *indio_dev;
struct device *dev = &client->dev;
s64 scale, offset;
u32 func;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_READ_BYTE))
return dev_err_probe(dev, -EOPNOTSUPP,
"I2C functionality not supported\n");
indio_dev = devm_iio_device_alloc(dev, sizeof(*data));
if (!indio_dev)
return dev_err_probe(dev, -ENOMEM, "couldn't get iio_dev\n");
data = iio_priv(indio_dev);
data->client = client;
data->irq = client->irq;
mutex_init(&data->lock);
init_completion(&data->completion);
indio_dev->name = "mprls0025pa";
indio_dev->info = &mpr_info;
indio_dev->channels = mpr_channels;
indio_dev->num_channels = ARRAY_SIZE(mpr_channels);
indio_dev->modes = INDIO_DIRECT_MODE;
ret = devm_regulator_get_enable(dev, "vdd");
if (ret)
return dev_err_probe(dev, ret,
"can't get and enable vdd supply\n");
ret = device_property_read_u32(dev, "honeywell,pmin-pascal",
&data->pmin);
if (ret)
return dev_err_probe(dev, ret,
"honeywell,pmin-pascal could not be read\n");
ret = device_property_read_u32(dev, "honeywell,pmax-pascal",
&data->pmax);
if (ret)
return dev_err_probe(dev, ret,
"honeywell,pmax-pascal could not be read\n");
ret = device_property_read_u32(dev,
"honeywell,transfer-function", &func);
if (ret)
return dev_err_probe(dev, ret,
"honeywell,transfer-function could not be read\n");
data->function = func - 1;
if (data->function > MPR_FUNCTION_C)
return dev_err_probe(dev, -EINVAL,
"honeywell,transfer-function %d invalid\n",
data->function);
data->outmin = mpr_func_spec[data->function].output_min;
data->outmax = mpr_func_spec[data->function].output_max;
/* use 64 bit calculation for preserving a reasonable precision */
scale = div_s64(((s64)(data->pmax - data->pmin)) * NANO,
data->outmax - data->outmin);
data->scale = div_s64_rem(scale, NANO, &data->scale2);
/*
* multiply with NANO before dividing by scale and later divide by NANO
* again.
*/
offset = ((-1LL) * (s64)data->outmin) * NANO -
div_s64(div_s64((s64)data->pmin * NANO, scale), NANO);
data->offset = div_s64_rem(offset, NANO, &data->offset2);
if (data->irq > 0) {
ret = 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);
}
data->gpiod_reset = devm_gpiod_get_optional(dev, "reset",
GPIOD_OUT_HIGH);
if (IS_ERR(data->gpiod_reset))
return dev_err_probe(dev, PTR_ERR(data->gpiod_reset),
"request reset-gpio failed\n");
mpr_reset(data);
ret = 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 = 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[] = {
{ .compatible = "honeywell,mprls0025pa" },
{ }
};
MODULE_DEVICE_TABLE(of, mpr_matches);
static const struct i2c_device_id mpr_id[] = {
{ "mprls0025pa" },
{ }
};
MODULE_DEVICE_TABLE(i2c, mpr_id);
static struct i2c_driver mpr_driver = {
.probe = mpr_probe,
.id_table = mpr_id,
.driver = {
.name = "mprls0025pa",
.of_match_table = mpr_matches,
},
};
module_i2c_driver(mpr_driver);
MODULE_AUTHOR("Andreas Klinger <ak@it-klinger.de>");
MODULE_DESCRIPTION("Honeywell MPRLS0025PA I2C driver");
MODULE_LICENSE("GPL");
|