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>
22 static bool ms5611_prom_is_valid(u16
*prom
, size_t len
)
25 uint16_t crc
= 0, crc_orig
= prom
[7] & 0x000F;
29 for (i
= 0; i
< len
* 2; i
++) {
31 crc
^= prom
[i
>> 1] & 0x00FF;
33 crc
^= prom
[i
>> 1] >> 8;
35 for (j
= 0; j
< 8; j
++) {
37 crc
= (crc
<< 1) ^ 0x3000;
43 crc
= (crc
>> 12) & 0x000F;
45 return crc_orig
!= 0x0000 && crc
== crc_orig
;
48 static int ms5611_read_prom(struct iio_dev
*indio_dev
)
51 struct ms5611_state
*st
= iio_priv(indio_dev
);
53 for (i
= 0; i
< MS5611_PROM_WORDS_NB
; i
++) {
54 ret
= st
->read_prom_word(&indio_dev
->dev
,
55 i
, &st
->chip_info
->prom
[i
]);
57 dev_err(&indio_dev
->dev
,
58 "failed to read prom at %d\n", i
);
63 if (!ms5611_prom_is_valid(st
->chip_info
->prom
, MS5611_PROM_WORDS_NB
)) {
64 dev_err(&indio_dev
->dev
, "PROM integrity check failed\n");
71 static int ms5611_read_temp_and_pressure(struct iio_dev
*indio_dev
,
72 s32
*temp
, s32
*pressure
)
75 struct ms5611_state
*st
= iio_priv(indio_dev
);
77 ret
= st
->read_adc_temp_and_pressure(&indio_dev
->dev
, temp
, pressure
);
79 dev_err(&indio_dev
->dev
,
80 "failed to read temperature and pressure\n");
84 return st
->chip_info
->temp_and_pressure_compensate(st
->chip_info
,
88 static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info
*chip_info
,
89 s32
*temp
, s32
*pressure
)
91 s32 t
= *temp
, p
= *pressure
;
94 dt
= t
- (chip_info
->prom
[5] << 8);
95 off
= ((s64
)chip_info
->prom
[2] << 16) + ((chip_info
->prom
[4] * dt
) >> 7);
96 sens
= ((s64
)chip_info
->prom
[1] << 15) + ((chip_info
->prom
[3] * dt
) >> 8);
98 t
= 2000 + ((chip_info
->prom
[6] * dt
) >> 23);
102 t2
= (dt
* dt
) >> 31;
103 off2
= (5 * (t
- 2000) * (t
- 2000)) >> 1;
107 s64 tmp
= (t
+ 1500) * (t
+ 1500);
110 sens2
+= (11 * tmp
) >> 1;
119 *pressure
= (((p
* sens
) >> 21) - off
) >> 15;
124 static int ms5607_temp_and_pressure_compensate(struct ms5611_chip_info
*chip_info
,
125 s32
*temp
, s32
*pressure
)
127 s32 t
= *temp
, p
= *pressure
;
130 dt
= t
- (chip_info
->prom
[5] << 8);
131 off
= ((s64
)chip_info
->prom
[2] << 17) + ((chip_info
->prom
[4] * dt
) >> 6);
132 sens
= ((s64
)chip_info
->prom
[1] << 16) + ((chip_info
->prom
[3] * dt
) >> 7);
134 t
= 2000 + ((chip_info
->prom
[6] * dt
) >> 23);
138 t2
= (dt
* dt
) >> 31;
139 off2
= (61 * (t
- 2000) * (t
- 2000)) >> 4;
143 s64 tmp
= (t
+ 1500) * (t
+ 1500);
155 *pressure
= (((p
* sens
) >> 21) - off
) >> 15;
160 static int ms5611_reset(struct iio_dev
*indio_dev
)
163 struct ms5611_state
*st
= iio_priv(indio_dev
);
165 ret
= st
->reset(&indio_dev
->dev
);
167 dev_err(&indio_dev
->dev
, "failed to reset device\n");
171 usleep_range(3000, 4000);
176 static int ms5611_read_raw(struct iio_dev
*indio_dev
,
177 struct iio_chan_spec
const *chan
,
178 int *val
, int *val2
, long mask
)
182 struct ms5611_state
*st
= iio_priv(indio_dev
);
185 case IIO_CHAN_INFO_PROCESSED
:
186 mutex_lock(&st
->lock
);
187 ret
= ms5611_read_temp_and_pressure(indio_dev
,
189 mutex_unlock(&st
->lock
);
193 switch (chan
->type
) {
198 *val
= pressure
/ 1000;
199 *val2
= (pressure
% 1000) * 1000;
200 return IIO_VAL_INT_PLUS_MICRO
;
209 static struct ms5611_chip_info chip_info_tbl
[] = {
211 .temp_and_pressure_compensate
= ms5611_temp_and_pressure_compensate
,
214 .temp_and_pressure_compensate
= ms5607_temp_and_pressure_compensate
,
218 static const struct iio_chan_spec ms5611_channels
[] = {
220 .type
= IIO_PRESSURE
,
221 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
225 .info_mask_separate
= BIT(IIO_CHAN_INFO_PROCESSED
),
229 static const struct iio_info ms5611_info
= {
230 .read_raw
= &ms5611_read_raw
,
231 .driver_module
= THIS_MODULE
,
234 static int ms5611_init(struct iio_dev
*indio_dev
)
238 ret
= ms5611_reset(indio_dev
);
242 return ms5611_read_prom(indio_dev
);
245 int ms5611_probe(struct iio_dev
*indio_dev
, struct device
*dev
, int type
)
248 struct ms5611_state
*st
= iio_priv(indio_dev
);
250 mutex_init(&st
->lock
);
251 st
->chip_info
= &chip_info_tbl
[type
];
252 indio_dev
->dev
.parent
= dev
;
253 indio_dev
->name
= dev
->driver
->name
;
254 indio_dev
->info
= &ms5611_info
;
255 indio_dev
->channels
= ms5611_channels
;
256 indio_dev
->num_channels
= ARRAY_SIZE(ms5611_channels
);
257 indio_dev
->modes
= INDIO_DIRECT_MODE
;
259 ret
= ms5611_init(indio_dev
);
263 return devm_iio_device_register(dev
, indio_dev
);
265 EXPORT_SYMBOL(ms5611_probe
);
267 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
268 MODULE_DESCRIPTION("MS5611 core driver");
269 MODULE_LICENSE("GPL v2");