2 * MS5611 pressure and temperature sensor driver
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
11 * http://www.meas-spec.com/downloads/MS5611-01BA03.pdf
12 * http://www.meas-spec.com/downloads/MS5607-02BA03.pdf
16 #include <linux/module.h>
17 #include <linux/iio/iio.h>
18 #include <linux/delay.h>
19 #include <linux/regulator/consumer.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/trigger_consumer.h>
26 static bool ms5611_prom_is_valid(u16 *prom, size_t len)
29 uint16_t crc = 0, crc_orig = prom[7] & 0x000F;
33 for (i = 0; i < len * 2; i++) {
35 crc ^= prom[i >> 1] & 0x00FF;
37 crc ^= prom[i >> 1] >> 8;
39 for (j = 0; j < 8; j++) {
41 crc = (crc << 1) ^ 0x3000;
47 crc = (crc >> 12) & 0x000F;
49 return crc_orig != 0x0000 && crc == crc_orig;
52 static int ms5611_read_prom(struct iio_dev *indio_dev)
55 struct ms5611_state *st = iio_priv(indio_dev);
57 for (i = 0; i < MS5611_PROM_WORDS_NB; i++) {
58 ret = st->read_prom_word(&indio_dev->dev,
59 i, &st->chip_info->prom[i]);
61 dev_err(&indio_dev->dev,
62 "failed to read prom at %d\n", i);
67 if (!ms5611_prom_is_valid(st->chip_info->prom, MS5611_PROM_WORDS_NB)) {
68 dev_err(&indio_dev->dev, "PROM integrity check failed\n");
75 static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev,
76 s32 *temp, s32 *pressure)
79 struct ms5611_state *st = iio_priv(indio_dev);
81 ret = st->read_adc_temp_and_pressure(&indio_dev->dev, temp, pressure);
83 dev_err(&indio_dev->dev,
84 "failed to read temperature and pressure\n");
88 return st->chip_info->temp_and_pressure_compensate(st->chip_info,
92 static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
93 s32 *temp, s32 *pressure)
95 s32 t = *temp, p = *pressure;
98 dt = t - (chip_info->prom[5] << 8);
99 off = ((s64)chip_info->prom[2] << 16) + ((chip_info->prom[4] * dt) >> 7);
100 sens = ((s64)chip_info->prom[1] << 15) + ((chip_info->prom[3] * dt) >> 8);
102 t = 2000 + ((chip_info->prom[6] * dt) >> 23);
106 t2 = (dt * dt) >> 31;
107 off2 = (5 * (t - 2000) * (t - 2000)) >> 1;
111 s64 tmp = (t + 1500) * (t + 1500);
114 sens2 += (11 * tmp) >> 1;
123 *pressure = (((p * sens) >> 21) - off) >> 15;
128 static int ms5607_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
129 s32 *temp, s32 *pressure)
131 s32 t = *temp, p = *pressure;
134 dt = t - (chip_info->prom[5] << 8);
135 off = ((s64)chip_info->prom[2] << 17) + ((chip_info->prom[4] * dt) >> 6);
136 sens = ((s64)chip_info->prom[1] << 16) + ((chip_info->prom[3] * dt) >> 7);
138 t = 2000 + ((chip_info->prom[6] * dt) >> 23);
140 s64 off2, sens2, t2, tmp;
142 t2 = (dt * dt) >> 31;
143 tmp = (t - 2000) * (t - 2000);
144 off2 = (61 * tmp) >> 4;
148 tmp = (t + 1500) * (t + 1500);
159 *pressure = (((p * sens) >> 21) - off) >> 15;
164 static int ms5611_reset(struct iio_dev *indio_dev)
167 struct ms5611_state *st = iio_priv(indio_dev);
169 ret = st->reset(&indio_dev->dev);
171 dev_err(&indio_dev->dev, "failed to reset device\n");
175 usleep_range(3000, 4000);
180 static irqreturn_t ms5611_trigger_handler(int irq, void *p)
182 struct iio_poll_func *pf = p;
183 struct iio_dev *indio_dev = pf->indio_dev;
184 struct ms5611_state *st = iio_priv(indio_dev);
185 s32 buf[4]; /* s32 (pressure) + s32 (temp) + 2 * s32 (timestamp) */
188 mutex_lock(&st->lock);
189 ret = ms5611_read_temp_and_pressure(indio_dev, &buf[1], &buf[0]);
190 mutex_unlock(&st->lock);
194 iio_push_to_buffers_with_timestamp(indio_dev, buf, iio_get_time_ns());
197 iio_trigger_notify_done(indio_dev->trig);
202 static int ms5611_read_raw(struct iio_dev *indio_dev,
203 struct iio_chan_spec const *chan,
204 int *val, int *val2, long mask)
208 struct ms5611_state *st = iio_priv(indio_dev);
211 case IIO_CHAN_INFO_PROCESSED:
212 mutex_lock(&st->lock);
213 ret = ms5611_read_temp_and_pressure(indio_dev,
215 mutex_unlock(&st->lock);
219 switch (chan->type) {
224 *val = pressure / 1000;
225 *val2 = (pressure % 1000) * 1000;
226 return IIO_VAL_INT_PLUS_MICRO;
230 case IIO_CHAN_INFO_SCALE:
231 switch (chan->type) {
238 return IIO_VAL_INT_PLUS_MICRO;
247 static const unsigned long ms5611_scan_masks[] = {0x3, 0};
249 static struct ms5611_chip_info chip_info_tbl[] = {
251 .temp_and_pressure_compensate = ms5611_temp_and_pressure_compensate,
254 .temp_and_pressure_compensate = ms5607_temp_and_pressure_compensate,
258 static const struct iio_chan_spec ms5611_channels[] = {
260 .type = IIO_PRESSURE,
261 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
262 BIT(IIO_CHAN_INFO_SCALE),
268 .endianness = IIO_CPU,
273 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
274 BIT(IIO_CHAN_INFO_SCALE),
280 .endianness = IIO_CPU,
283 IIO_CHAN_SOFT_TIMESTAMP(2),
286 static const struct iio_info ms5611_info = {
287 .read_raw = &ms5611_read_raw,
288 .driver_module = THIS_MODULE,
291 static int ms5611_init(struct iio_dev *indio_dev)
294 struct regulator *vdd = devm_regulator_get(indio_dev->dev.parent,
297 /* Enable attached regulator if any. */
299 ret = regulator_enable(vdd);
301 dev_err(indio_dev->dev.parent,
302 "failed to enable Vdd supply: %d\n", ret);
307 ret = ms5611_reset(indio_dev);
311 return ms5611_read_prom(indio_dev);
314 int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
315 const char *name, int type)
318 struct ms5611_state *st = iio_priv(indio_dev);
320 mutex_init(&st->lock);
321 st->chip_info = &chip_info_tbl[type];
322 indio_dev->dev.parent = dev;
323 indio_dev->name = name;
324 indio_dev->info = &ms5611_info;
325 indio_dev->channels = ms5611_channels;
326 indio_dev->num_channels = ARRAY_SIZE(ms5611_channels);
327 indio_dev->modes = INDIO_DIRECT_MODE;
328 indio_dev->available_scan_masks = ms5611_scan_masks;
330 ret = ms5611_init(indio_dev);
334 ret = iio_triggered_buffer_setup(indio_dev, NULL,
335 ms5611_trigger_handler, NULL);
337 dev_err(dev, "iio triggered buffer setup failed\n");
341 ret = iio_device_register(indio_dev);
343 dev_err(dev, "unable to register iio device\n");
344 goto err_buffer_cleanup;
350 iio_triggered_buffer_cleanup(indio_dev);
354 EXPORT_SYMBOL(ms5611_probe);
356 int ms5611_remove(struct iio_dev *indio_dev)
358 iio_device_unregister(indio_dev);
359 iio_triggered_buffer_cleanup(indio_dev);
363 EXPORT_SYMBOL(ms5611_remove);
366 MODULE_DESCRIPTION("MS5611 core driver");
367 MODULE_LICENSE("GPL v2");