2 * MS5611 pressure and temperature sensor driver
4 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
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/sysfs.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/trigger_consumer.h>
27 #define MS5611_INIT_OSR(_cmd, _conv_usec, _rate) \
28 { .cmd = _cmd, .conv_usec = _conv_usec, .rate = _rate }
30 static const struct ms5611_osr ms5611_avail_pressure_osr
[] = {
31 MS5611_INIT_OSR(0x40, 600, 256),
32 MS5611_INIT_OSR(0x42, 1170, 512),
33 MS5611_INIT_OSR(0x44, 2280, 1024),
34 MS5611_INIT_OSR(0x46, 4540, 2048),
35 MS5611_INIT_OSR(0x48, 9040, 4096)
38 static const struct ms5611_osr ms5611_avail_temp_osr
[] = {
39 MS5611_INIT_OSR(0x50, 600, 256),
40 MS5611_INIT_OSR(0x52, 1170, 512),
41 MS5611_INIT_OSR(0x54, 2280, 1024),
42 MS5611_INIT_OSR(0x56, 4540, 2048),
43 MS5611_INIT_OSR(0x58, 9040, 4096)
46 static const char ms5611_show_osr
[] = "256 512 1024 2048 4096";
48 static IIO_CONST_ATTR(oversampling_ratio_available
, ms5611_show_osr
);
50 static struct attribute
*ms5611_attributes
[] = {
51 &iio_const_attr_oversampling_ratio_available
.dev_attr
.attr
,
55 static const struct attribute_group ms5611_attribute_group
= {
56 .attrs
= ms5611_attributes
,
59 static bool ms5611_prom_is_valid(u16
*prom
, size_t len
)
62 uint16_t crc
= 0, crc_orig
= prom
[7] & 0x000F;
66 for (i
= 0; i
< len
* 2; i
++) {
68 crc
^= prom
[i
>> 1] & 0x00FF;
70 crc
^= prom
[i
>> 1] >> 8;
72 for (j
= 0; j
< 8; j
++) {
74 crc
= (crc
<< 1) ^ 0x3000;
80 crc
= (crc
>> 12) & 0x000F;
82 return crc_orig
!= 0x0000 && crc
== crc_orig
;
85 static int ms5611_read_prom(struct iio_dev
*indio_dev
)
88 struct ms5611_state
*st
= iio_priv(indio_dev
);
90 for (i
= 0; i
< MS5611_PROM_WORDS_NB
; i
++) {
91 ret
= st
->read_prom_word(&indio_dev
->dev
,
92 i
, &st
->chip_info
->prom
[i
]);
94 dev_err(&indio_dev
->dev
,
95 "failed to read prom at %d\n", i
);
100 if (!ms5611_prom_is_valid(st
->chip_info
->prom
, MS5611_PROM_WORDS_NB
)) {
101 dev_err(&indio_dev
->dev
, "PROM integrity check failed\n");
108 static int ms5611_read_temp_and_pressure(struct iio_dev
*indio_dev
,
109 s32
*temp
, s32
*pressure
)
112 struct ms5611_state
*st
= iio_priv(indio_dev
);
114 ret
= st
->read_adc_temp_and_pressure(&indio_dev
->dev
, temp
, pressure
);
116 dev_err(&indio_dev
->dev
,
117 "failed to read temperature and pressure\n");
121 return st
->chip_info
->temp_and_pressure_compensate(st
->chip_info
,
125 static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info
*chip_info
,
126 s32
*temp
, s32
*pressure
)
128 s32 t
= *temp
, p
= *pressure
;
131 dt
= t
- (chip_info
->prom
[5] << 8);
132 off
= ((s64
)chip_info
->prom
[2] << 16) + ((chip_info
->prom
[4] * dt
) >> 7);
133 sens
= ((s64
)chip_info
->prom
[1] << 15) + ((chip_info
->prom
[3] * dt
) >> 8);
135 t
= 2000 + ((chip_info
->prom
[6] * dt
) >> 23);
139 t2
= (dt
* dt
) >> 31;
140 off2
= (5 * (t
- 2000) * (t
- 2000)) >> 1;
144 s64 tmp
= (t
+ 1500) * (t
+ 1500);
147 sens2
+= (11 * tmp
) >> 1;
156 *pressure
= (((p
* sens
) >> 21) - off
) >> 15;
161 static int ms5607_temp_and_pressure_compensate(struct ms5611_chip_info
*chip_info
,
162 s32
*temp
, s32
*pressure
)
164 s32 t
= *temp
, p
= *pressure
;
167 dt
= t
- (chip_info
->prom
[5] << 8);
168 off
= ((s64
)chip_info
->prom
[2] << 17) + ((chip_info
->prom
[4] * dt
) >> 6);
169 sens
= ((s64
)chip_info
->prom
[1] << 16) + ((chip_info
->prom
[3] * dt
) >> 7);
171 t
= 2000 + ((chip_info
->prom
[6] * dt
) >> 23);
173 s64 off2
, sens2
, t2
, tmp
;
175 t2
= (dt
* dt
) >> 31;
176 tmp
= (t
- 2000) * (t
- 2000);
177 off2
= (61 * tmp
) >> 4;
181 tmp
= (t
+ 1500) * (t
+ 1500);
192 *pressure
= (((p
* sens
) >> 21) - off
) >> 15;
197 static int ms5611_reset(struct iio_dev
*indio_dev
)
200 struct ms5611_state
*st
= iio_priv(indio_dev
);
202 ret
= st
->reset(&indio_dev
->dev
);
204 dev_err(&indio_dev
->dev
, "failed to reset device\n");
208 usleep_range(3000, 4000);
213 static irqreturn_t
ms5611_trigger_handler(int irq
, void *p
)
215 struct iio_poll_func
*pf
= p
;
216 struct iio_dev
*indio_dev
= pf
->indio_dev
;
217 struct ms5611_state
*st
= iio_priv(indio_dev
);
218 s32 buf
[4]; /* s32 (pressure) + s32 (temp) + 2 * s32 (timestamp) */
221 mutex_lock(&st
->lock
);
222 ret
= ms5611_read_temp_and_pressure(indio_dev
, &buf
[1], &buf
[0]);
223 mutex_unlock(&st
->lock
);
227 iio_push_to_buffers_with_timestamp(indio_dev
, buf
,
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 struct ms5611_chip_info chip_info_tbl
[] = {
346 .temp_and_pressure_compensate
= ms5611_temp_and_pressure_compensate
,
349 .temp_and_pressure_compensate
= ms5607_temp_and_pressure_compensate
,
353 static const struct iio_chan_spec ms5611_channels
[] = {
355 .type
= IIO_PRESSURE
,
356 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
357 BIT(IIO_CHAN_INFO_SCALE
) |
358 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
364 .endianness
= IIO_CPU
,
369 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
) |
370 BIT(IIO_CHAN_INFO_SCALE
) |
371 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
),
377 .endianness
= IIO_CPU
,
380 IIO_CHAN_SOFT_TIMESTAMP(2),
383 static const struct iio_info ms5611_info
= {
384 .read_raw
= &ms5611_read_raw
,
385 .write_raw
= &ms5611_write_raw
,
386 .attrs
= &ms5611_attribute_group
,
389 static int ms5611_init(struct iio_dev
*indio_dev
)
392 struct ms5611_state
*st
= iio_priv(indio_dev
);
394 /* Enable attached regulator if any. */
395 st
->vdd
= devm_regulator_get(indio_dev
->dev
.parent
, "vdd");
397 return PTR_ERR(st
->vdd
);
399 ret
= regulator_enable(st
->vdd
);
401 dev_err(indio_dev
->dev
.parent
,
402 "failed to enable Vdd supply: %d\n", ret
);
406 ret
= ms5611_reset(indio_dev
);
408 goto err_regulator_disable
;
410 ret
= ms5611_read_prom(indio_dev
);
412 goto err_regulator_disable
;
416 err_regulator_disable
:
417 regulator_disable(st
->vdd
);
421 static void ms5611_fini(const struct iio_dev
*indio_dev
)
423 const struct ms5611_state
*st
= iio_priv(indio_dev
);
425 regulator_disable(st
->vdd
);
428 int ms5611_probe(struct iio_dev
*indio_dev
, struct device
*dev
,
429 const char *name
, int type
)
432 struct ms5611_state
*st
= iio_priv(indio_dev
);
434 mutex_init(&st
->lock
);
435 st
->chip_info
= &chip_info_tbl
[type
];
437 &ms5611_avail_temp_osr
[ARRAY_SIZE(ms5611_avail_temp_osr
) - 1];
439 &ms5611_avail_pressure_osr
[ARRAY_SIZE(ms5611_avail_pressure_osr
)
441 indio_dev
->dev
.parent
= dev
;
442 indio_dev
->name
= name
;
443 indio_dev
->info
= &ms5611_info
;
444 indio_dev
->channels
= ms5611_channels
;
445 indio_dev
->num_channels
= ARRAY_SIZE(ms5611_channels
);
446 indio_dev
->modes
= INDIO_DIRECT_MODE
;
447 indio_dev
->available_scan_masks
= ms5611_scan_masks
;
449 ret
= ms5611_init(indio_dev
);
453 ret
= iio_triggered_buffer_setup(indio_dev
, NULL
,
454 ms5611_trigger_handler
, NULL
);
456 dev_err(dev
, "iio triggered buffer setup failed\n");
460 ret
= iio_device_register(indio_dev
);
462 dev_err(dev
, "unable to register iio device\n");
463 goto err_buffer_cleanup
;
469 iio_triggered_buffer_cleanup(indio_dev
);
471 ms5611_fini(indio_dev
);
474 EXPORT_SYMBOL(ms5611_probe
);
476 int ms5611_remove(struct iio_dev
*indio_dev
)
478 iio_device_unregister(indio_dev
);
479 iio_triggered_buffer_cleanup(indio_dev
);
480 ms5611_fini(indio_dev
);
484 EXPORT_SYMBOL(ms5611_remove
);
486 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
487 MODULE_DESCRIPTION("MS5611 core driver");
488 MODULE_LICENSE("GPL v2");