1 // SPDX-License-Identifier: GPL-2.0
3 * MS5611 pressure and temperature sensor driver
5 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
8 * http://www.meas-spec.com/downloads/MS5611-01BA03.pdf
9 * http://www.meas-spec.com/downloads/MS5607-02BA03.pdf
13 #include <linux/module.h>
14 #include <linux/iio/iio.h>
15 #include <linux/delay.h>
16 #include <linux/regulator/consumer.h>
18 #include <linux/iio/sysfs.h>
19 #include <linux/iio/buffer.h>
20 #include <linux/iio/triggered_buffer.h>
21 #include <linux/iio/trigger_consumer.h>
24 #define MS5611_INIT_OSR(_cmd, _conv_usec, _rate) \
25 { .cmd = _cmd, .conv_usec = _conv_usec, .rate = _rate }
27 static const struct ms5611_osr ms5611_avail_pressure_osr[] = {
28 MS5611_INIT_OSR(0x40, 600, 256),
29 MS5611_INIT_OSR(0x42, 1170, 512),
30 MS5611_INIT_OSR(0x44, 2280, 1024),
31 MS5611_INIT_OSR(0x46, 4540, 2048),
32 MS5611_INIT_OSR(0x48, 9040, 4096)
35 static const struct ms5611_osr ms5611_avail_temp_osr[] = {
36 MS5611_INIT_OSR(0x50, 600, 256),
37 MS5611_INIT_OSR(0x52, 1170, 512),
38 MS5611_INIT_OSR(0x54, 2280, 1024),
39 MS5611_INIT_OSR(0x56, 4540, 2048),
40 MS5611_INIT_OSR(0x58, 9040, 4096)
43 static const char ms5611_show_osr[] = "256 512 1024 2048 4096";
45 static IIO_CONST_ATTR(oversampling_ratio_available, ms5611_show_osr);
47 static struct attribute *ms5611_attributes[] = {
48 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
52 static const struct attribute_group ms5611_attribute_group = {
53 .attrs = ms5611_attributes,
56 static bool ms5611_prom_is_valid(u16 *prom, size_t len)
59 uint16_t crc = 0, crc_orig = prom[7] & 0x000F;
63 for (i = 0; i < len * 2; i++) {
65 crc ^= prom[i >> 1] & 0x00FF;
67 crc ^= prom[i >> 1] >> 8;
69 for (j = 0; j < 8; j++) {
71 crc = (crc << 1) ^ 0x3000;
77 crc = (crc >> 12) & 0x000F;
79 return crc == crc_orig;
82 static int ms5611_read_prom(struct iio_dev *indio_dev)
85 struct ms5611_state *st = iio_priv(indio_dev);
87 for (i = 0; i < MS5611_PROM_WORDS_NB; i++) {
88 ret = st->read_prom_word(st, i, &st->prom[i]);
90 dev_err(&indio_dev->dev,
91 "failed to read prom at %d\n", i);
96 if (!ms5611_prom_is_valid(st->prom, MS5611_PROM_WORDS_NB)) {
97 dev_err(&indio_dev->dev, "PROM integrity check failed\n");
104 static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev,
105 s32 *temp, s32 *pressure)
108 struct ms5611_state *st = iio_priv(indio_dev);
110 ret = st->read_adc_temp_and_pressure(st, temp, pressure);
112 dev_err(&indio_dev->dev,
113 "failed to read temperature and pressure\n");
117 return st->compensate_temp_and_pressure(st, temp, pressure);
120 static int ms5611_temp_and_pressure_compensate(struct ms5611_state *st,
121 s32 *temp, s32 *pressure)
123 s32 t = *temp, p = *pressure;
126 dt = t - (st->prom[5] << 8);
127 off = ((s64)st->prom[2] << 16) + ((st->prom[4] * dt) >> 7);
128 sens = ((s64)st->prom[1] << 15) + ((st->prom[3] * dt) >> 8);
130 t = 2000 + ((st->prom[6] * dt) >> 23);
134 t2 = (dt * dt) >> 31;
135 off2 = (5 * (t - 2000) * (t - 2000)) >> 1;
139 s64 tmp = (t + 1500) * (t + 1500);
142 sens2 += (11 * tmp) >> 1;
151 *pressure = (((p * sens) >> 21) - off) >> 15;
156 static int ms5607_temp_and_pressure_compensate(struct ms5611_state *st,
157 s32 *temp, s32 *pressure)
159 s32 t = *temp, p = *pressure;
162 dt = t - (st->prom[5] << 8);
163 off = ((s64)st->prom[2] << 17) + ((st->prom[4] * dt) >> 6);
164 sens = ((s64)st->prom[1] << 16) + ((st->prom[3] * dt) >> 7);
166 t = 2000 + ((st->prom[6] * dt) >> 23);
168 s64 off2, sens2, t2, tmp;
170 t2 = (dt * dt) >> 31;
171 tmp = (t - 2000) * (t - 2000);
172 off2 = (61 * tmp) >> 4;
176 tmp = (t + 1500) * (t + 1500);
187 *pressure = (((p * sens) >> 21) - off) >> 15;
192 static int ms5611_reset(struct iio_dev *indio_dev)
195 struct ms5611_state *st = iio_priv(indio_dev);
199 dev_err(&indio_dev->dev, "failed to reset device\n");
203 usleep_range(3000, 4000);
208 static irqreturn_t ms5611_trigger_handler(int irq, void *p)
210 struct iio_poll_func *pf = p;
211 struct iio_dev *indio_dev = pf->indio_dev;
212 struct ms5611_state *st = iio_priv(indio_dev);
213 /* Ensure buffer elements are naturally aligned */
220 mutex_lock(&st->lock);
221 ret = ms5611_read_temp_and_pressure(indio_dev, &scan.channels[1],
223 mutex_unlock(&st->lock);
227 iio_push_to_buffers_with_timestamp(indio_dev, &scan,
228 iio_get_time_ns(indio_dev));
231 iio_trigger_notify_done(indio_dev->trig);
236 static int ms5611_read_raw(struct iio_dev *indio_dev,
237 struct iio_chan_spec const *chan,
238 int *val, int *val2, long mask)
242 struct ms5611_state *st = iio_priv(indio_dev);
245 case IIO_CHAN_INFO_PROCESSED:
246 mutex_lock(&st->lock);
247 ret = ms5611_read_temp_and_pressure(indio_dev,
249 mutex_unlock(&st->lock);
253 switch (chan->type) {
258 *val = pressure / 1000;
259 *val2 = (pressure % 1000) * 1000;
260 return IIO_VAL_INT_PLUS_MICRO;
264 case IIO_CHAN_INFO_SCALE:
265 switch (chan->type) {
272 return IIO_VAL_INT_PLUS_MICRO;
276 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
277 if (chan->type != IIO_TEMP && chan->type != IIO_PRESSURE)
279 mutex_lock(&st->lock);
280 if (chan->type == IIO_TEMP)
281 *val = (int)st->temp_osr->rate;
283 *val = (int)st->pressure_osr->rate;
284 mutex_unlock(&st->lock);
291 static const struct ms5611_osr *ms5611_find_osr(int rate,
292 const struct ms5611_osr *osr,
297 for (r = 0; r < count; r++)
298 if ((unsigned short)rate == osr[r].rate)
305 static int ms5611_write_raw(struct iio_dev *indio_dev,
306 struct iio_chan_spec const *chan,
307 int val, int val2, long mask)
309 struct ms5611_state *st = iio_priv(indio_dev);
310 const struct ms5611_osr *osr = NULL;
313 if (mask != IIO_CHAN_INFO_OVERSAMPLING_RATIO)
316 if (chan->type == IIO_TEMP)
317 osr = ms5611_find_osr(val, ms5611_avail_temp_osr,
318 ARRAY_SIZE(ms5611_avail_temp_osr));
319 else if (chan->type == IIO_PRESSURE)
320 osr = ms5611_find_osr(val, ms5611_avail_pressure_osr,
321 ARRAY_SIZE(ms5611_avail_pressure_osr));
325 ret = iio_device_claim_direct_mode(indio_dev);
329 mutex_lock(&st->lock);
331 if (chan->type == IIO_TEMP)
334 st->pressure_osr = osr;
336 mutex_unlock(&st->lock);
337 iio_device_release_direct_mode(indio_dev);
342 static const unsigned long ms5611_scan_masks[] = {0x3, 0};
344 static const struct iio_chan_spec ms5611_channels[] = {
346 .type = IIO_PRESSURE,
347 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
348 BIT(IIO_CHAN_INFO_SCALE) |
349 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
355 .endianness = IIO_CPU,
360 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
361 BIT(IIO_CHAN_INFO_SCALE) |
362 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
368 .endianness = IIO_CPU,
371 IIO_CHAN_SOFT_TIMESTAMP(2),
374 static const struct iio_info ms5611_info = {
375 .read_raw = &ms5611_read_raw,
376 .write_raw = &ms5611_write_raw,
377 .attrs = &ms5611_attribute_group,
380 static int ms5611_init(struct iio_dev *indio_dev)
383 struct ms5611_state *st = iio_priv(indio_dev);
385 /* Enable attached regulator if any. */
386 st->vdd = devm_regulator_get(indio_dev->dev.parent, "vdd");
388 return PTR_ERR(st->vdd);
390 ret = regulator_enable(st->vdd);
392 dev_err(indio_dev->dev.parent,
393 "failed to enable Vdd supply: %d\n", ret);
397 ret = ms5611_reset(indio_dev);
399 goto err_regulator_disable;
401 ret = ms5611_read_prom(indio_dev);
403 goto err_regulator_disable;
407 err_regulator_disable:
408 regulator_disable(st->vdd);
412 static void ms5611_fini(const struct iio_dev *indio_dev)
414 const struct ms5611_state *st = iio_priv(indio_dev);
416 regulator_disable(st->vdd);
419 int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
420 const char *name, int type)
423 struct ms5611_state *st = iio_priv(indio_dev);
425 mutex_init(&st->lock);
429 st->compensate_temp_and_pressure =
430 ms5611_temp_and_pressure_compensate;
433 st->compensate_temp_and_pressure =
434 ms5607_temp_and_pressure_compensate;
441 &ms5611_avail_temp_osr[ARRAY_SIZE(ms5611_avail_temp_osr) - 1];
443 &ms5611_avail_pressure_osr[ARRAY_SIZE(ms5611_avail_pressure_osr)
445 indio_dev->name = name;
446 indio_dev->info = &ms5611_info;
447 indio_dev->channels = ms5611_channels;
448 indio_dev->num_channels = ARRAY_SIZE(ms5611_channels);
449 indio_dev->modes = INDIO_DIRECT_MODE;
450 indio_dev->available_scan_masks = ms5611_scan_masks;
452 ret = ms5611_init(indio_dev);
456 ret = iio_triggered_buffer_setup(indio_dev, NULL,
457 ms5611_trigger_handler, NULL);
459 dev_err(dev, "iio triggered buffer setup failed\n");
463 ret = iio_device_register(indio_dev);
465 dev_err(dev, "unable to register iio device\n");
466 goto err_buffer_cleanup;
472 iio_triggered_buffer_cleanup(indio_dev);
474 ms5611_fini(indio_dev);
477 EXPORT_SYMBOL(ms5611_probe);
479 int ms5611_remove(struct iio_dev *indio_dev)
481 iio_device_unregister(indio_dev);
482 iio_triggered_buffer_cleanup(indio_dev);
483 ms5611_fini(indio_dev);
487 EXPORT_SYMBOL(ms5611_remove);
489 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
490 MODULE_DESCRIPTION("MS5611 core driver");
491 MODULE_LICENSE("GPL v2");