1 /*
2 * MS5611 pressure and temperature sensor driver
3 *
4 * Copyright (c) Tomasz Duszynski <tduszyns@gmail.com>
5 *
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.
9 *
10 * Data sheet:
11 * http://www.meas-spec.com/downloads/MS5611-01BA03.pdf
12 * http://www.meas-spec.com/downloads/MS5607-02BA03.pdf
13 *
14 */
15
16 #include <linux/module.h>
17 #include <linux/iio/iio.h>
18 #include <linux/delay.h>
19 #include <linux/regulator/consumer.h>
20
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>
25 #include "ms5611.h"
26
27 #define MS5611_INIT_OSR(_cmd, _conv_usec, _rate) \
28 { .cmd = _cmd, .conv_usec = _conv_usec, .rate = _rate }
29
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)
36 };
37
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)
44 };
45
46 static const char ms5611_show_osr[] = "256 512 1024 2048 4096";
47
48 static IIO_CONST_ATTR(oversampling_ratio_available, ms5611_show_osr);
49
50 static struct attribute *ms5611_attributes[] = {
51 &iio_const_attr_oversampling_ratio_available.dev_attr.attr,
52 NULL,
53 };
54
55 static const struct attribute_group ms5611_attribute_group = {
56 .attrs = ms5611_attributes,
57 };
58
ms5611_prom_is_valid(u16 * prom,size_t len)59 static bool ms5611_prom_is_valid(u16 *prom, size_t len)
60 {
61 int i, j;
62 uint16_t crc = 0, crc_orig = prom[7] & 0x000F;
63
64 prom[7] &= 0xFF00;
65
66 for (i = 0; i < len * 2; i++) {
67 if (i % 2 == 1)
68 crc ^= prom[i >> 1] & 0x00FF;
69 else
70 crc ^= prom[i >> 1] >> 8;
71
72 for (j = 0; j < 8; j++) {
73 if (crc & 0x8000)
74 crc = (crc << 1) ^ 0x3000;
75 else
76 crc <<= 1;
77 }
78 }
79
80 crc = (crc >> 12) & 0x000F;
81
82 return crc_orig != 0x0000 && crc == crc_orig;
83 }
84
ms5611_read_prom(struct iio_dev * indio_dev)85 static int ms5611_read_prom(struct iio_dev *indio_dev)
86 {
87 int ret, i;
88 struct ms5611_state *st = iio_priv(indio_dev);
89
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]);
93 if (ret < 0) {
94 dev_err(&indio_dev->dev,
95 "failed to read prom at %d\n", i);
96 return ret;
97 }
98 }
99
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");
102 return -ENODEV;
103 }
104
105 return 0;
106 }
107
ms5611_read_temp_and_pressure(struct iio_dev * indio_dev,s32 * temp,s32 * pressure)108 static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev,
109 s32 *temp, s32 *pressure)
110 {
111 int ret;
112 struct ms5611_state *st = iio_priv(indio_dev);
113
114 ret = st->read_adc_temp_and_pressure(&indio_dev->dev, temp, pressure);
115 if (ret < 0) {
116 dev_err(&indio_dev->dev,
117 "failed to read temperature and pressure\n");
118 return ret;
119 }
120
121 return st->chip_info->temp_and_pressure_compensate(st->chip_info,
122 temp, pressure);
123 }
124
ms5611_temp_and_pressure_compensate(struct ms5611_chip_info * chip_info,s32 * temp,s32 * pressure)125 static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
126 s32 *temp, s32 *pressure)
127 {
128 s32 t = *temp, p = *pressure;
129 s64 off, sens, dt;
130
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);
134
135 t = 2000 + ((chip_info->prom[6] * dt) >> 23);
136 if (t < 2000) {
137 s64 off2, sens2, t2;
138
139 t2 = (dt * dt) >> 31;
140 off2 = (5 * (t - 2000) * (t - 2000)) >> 1;
141 sens2 = off2 >> 1;
142
143 if (t < -1500) {
144 s64 tmp = (t + 1500) * (t + 1500);
145
146 off2 += 7 * tmp;
147 sens2 += (11 * tmp) >> 1;
148 }
149
150 t -= t2;
151 off -= off2;
152 sens -= sens2;
153 }
154
155 *temp = t;
156 *pressure = (((p * sens) >> 21) - off) >> 15;
157
158 return 0;
159 }
160
ms5607_temp_and_pressure_compensate(struct ms5611_chip_info * chip_info,s32 * temp,s32 * pressure)161 static int ms5607_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
162 s32 *temp, s32 *pressure)
163 {
164 s32 t = *temp, p = *pressure;
165 s64 off, sens, dt;
166
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);
170
171 t = 2000 + ((chip_info->prom[6] * dt) >> 23);
172 if (t < 2000) {
173 s64 off2, sens2, t2, tmp;
174
175 t2 = (dt * dt) >> 31;
176 tmp = (t - 2000) * (t - 2000);
177 off2 = (61 * tmp) >> 4;
178 sens2 = tmp << 1;
179
180 if (t < -1500) {
181 tmp = (t + 1500) * (t + 1500);
182 off2 += 15 * tmp;
183 sens2 += 8 * tmp;
184 }
185
186 t -= t2;
187 off -= off2;
188 sens -= sens2;
189 }
190
191 *temp = t;
192 *pressure = (((p * sens) >> 21) - off) >> 15;
193
194 return 0;
195 }
196
ms5611_reset(struct iio_dev * indio_dev)197 static int ms5611_reset(struct iio_dev *indio_dev)
198 {
199 int ret;
200 struct ms5611_state *st = iio_priv(indio_dev);
201
202 ret = st->reset(&indio_dev->dev);
203 if (ret < 0) {
204 dev_err(&indio_dev->dev, "failed to reset device\n");
205 return ret;
206 }
207
208 usleep_range(3000, 4000);
209
210 return 0;
211 }
212
ms5611_trigger_handler(int irq,void * p)213 static irqreturn_t ms5611_trigger_handler(int irq, void *p)
214 {
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 /* Ensure buffer elements are naturally aligned */
219 struct {
220 s32 channels[2];
221 s64 ts __aligned(8);
222 } scan;
223 int ret;
224
225 mutex_lock(&st->lock);
226 ret = ms5611_read_temp_and_pressure(indio_dev, &scan.channels[1],
227 &scan.channels[0]);
228 mutex_unlock(&st->lock);
229 if (ret < 0)
230 goto err;
231
232 iio_push_to_buffers_with_timestamp(indio_dev, &scan,
233 iio_get_time_ns(indio_dev));
234
235 err:
236 iio_trigger_notify_done(indio_dev->trig);
237
238 return IRQ_HANDLED;
239 }
240
ms5611_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)241 static int ms5611_read_raw(struct iio_dev *indio_dev,
242 struct iio_chan_spec const *chan,
243 int *val, int *val2, long mask)
244 {
245 int ret;
246 s32 temp, pressure;
247 struct ms5611_state *st = iio_priv(indio_dev);
248
249 switch (mask) {
250 case IIO_CHAN_INFO_PROCESSED:
251 mutex_lock(&st->lock);
252 ret = ms5611_read_temp_and_pressure(indio_dev,
253 &temp, &pressure);
254 mutex_unlock(&st->lock);
255 if (ret < 0)
256 return ret;
257
258 switch (chan->type) {
259 case IIO_TEMP:
260 *val = temp * 10;
261 return IIO_VAL_INT;
262 case IIO_PRESSURE:
263 *val = pressure / 1000;
264 *val2 = (pressure % 1000) * 1000;
265 return IIO_VAL_INT_PLUS_MICRO;
266 default:
267 return -EINVAL;
268 }
269 case IIO_CHAN_INFO_SCALE:
270 switch (chan->type) {
271 case IIO_TEMP:
272 *val = 10;
273 return IIO_VAL_INT;
274 case IIO_PRESSURE:
275 *val = 0;
276 *val2 = 1000;
277 return IIO_VAL_INT_PLUS_MICRO;
278 default:
279 return -EINVAL;
280 }
281 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
282 if (chan->type != IIO_TEMP && chan->type != IIO_PRESSURE)
283 break;
284 mutex_lock(&st->lock);
285 if (chan->type == IIO_TEMP)
286 *val = (int)st->temp_osr->rate;
287 else
288 *val = (int)st->pressure_osr->rate;
289 mutex_unlock(&st->lock);
290 return IIO_VAL_INT;
291 }
292
293 return -EINVAL;
294 }
295
ms5611_find_osr(int rate,const struct ms5611_osr * osr,size_t count)296 static const struct ms5611_osr *ms5611_find_osr(int rate,
297 const struct ms5611_osr *osr,
298 size_t count)
299 {
300 unsigned int r;
301
302 for (r = 0; r < count; r++)
303 if ((unsigned short)rate == osr[r].rate)
304 break;
305 if (r >= count)
306 return NULL;
307 return &osr[r];
308 }
309
ms5611_write_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int val,int val2,long mask)310 static int ms5611_write_raw(struct iio_dev *indio_dev,
311 struct iio_chan_spec const *chan,
312 int val, int val2, long mask)
313 {
314 struct ms5611_state *st = iio_priv(indio_dev);
315 const struct ms5611_osr *osr = NULL;
316 int ret;
317
318 if (mask != IIO_CHAN_INFO_OVERSAMPLING_RATIO)
319 return -EINVAL;
320
321 if (chan->type == IIO_TEMP)
322 osr = ms5611_find_osr(val, ms5611_avail_temp_osr,
323 ARRAY_SIZE(ms5611_avail_temp_osr));
324 else if (chan->type == IIO_PRESSURE)
325 osr = ms5611_find_osr(val, ms5611_avail_pressure_osr,
326 ARRAY_SIZE(ms5611_avail_pressure_osr));
327 if (!osr)
328 return -EINVAL;
329
330 ret = iio_device_claim_direct_mode(indio_dev);
331 if (ret)
332 return ret;
333
334 mutex_lock(&st->lock);
335
336 if (chan->type == IIO_TEMP)
337 st->temp_osr = osr;
338 else
339 st->pressure_osr = osr;
340
341 mutex_unlock(&st->lock);
342 iio_device_release_direct_mode(indio_dev);
343
344 return 0;
345 }
346
347 static const unsigned long ms5611_scan_masks[] = {0x3, 0};
348
349 static struct ms5611_chip_info chip_info_tbl[] = {
350 [MS5611] = {
351 .temp_and_pressure_compensate = ms5611_temp_and_pressure_compensate,
352 },
353 [MS5607] = {
354 .temp_and_pressure_compensate = ms5607_temp_and_pressure_compensate,
355 }
356 };
357
358 static const struct iio_chan_spec ms5611_channels[] = {
359 {
360 .type = IIO_PRESSURE,
361 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
362 BIT(IIO_CHAN_INFO_SCALE) |
363 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
364 .scan_index = 0,
365 .scan_type = {
366 .sign = 's',
367 .realbits = 32,
368 .storagebits = 32,
369 .endianness = IIO_CPU,
370 },
371 },
372 {
373 .type = IIO_TEMP,
374 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED) |
375 BIT(IIO_CHAN_INFO_SCALE) |
376 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO),
377 .scan_index = 1,
378 .scan_type = {
379 .sign = 's',
380 .realbits = 32,
381 .storagebits = 32,
382 .endianness = IIO_CPU,
383 },
384 },
385 IIO_CHAN_SOFT_TIMESTAMP(2),
386 };
387
388 static const struct iio_info ms5611_info = {
389 .read_raw = &ms5611_read_raw,
390 .write_raw = &ms5611_write_raw,
391 .attrs = &ms5611_attribute_group,
392 };
393
ms5611_init(struct iio_dev * indio_dev)394 static int ms5611_init(struct iio_dev *indio_dev)
395 {
396 int ret;
397 struct ms5611_state *st = iio_priv(indio_dev);
398
399 /* Enable attached regulator if any. */
400 st->vdd = devm_regulator_get(indio_dev->dev.parent, "vdd");
401 if (IS_ERR(st->vdd))
402 return PTR_ERR(st->vdd);
403
404 ret = regulator_enable(st->vdd);
405 if (ret) {
406 dev_err(indio_dev->dev.parent,
407 "failed to enable Vdd supply: %d\n", ret);
408 return ret;
409 }
410
411 ret = ms5611_reset(indio_dev);
412 if (ret < 0)
413 goto err_regulator_disable;
414
415 ret = ms5611_read_prom(indio_dev);
416 if (ret < 0)
417 goto err_regulator_disable;
418
419 return 0;
420
421 err_regulator_disable:
422 regulator_disable(st->vdd);
423 return ret;
424 }
425
ms5611_fini(const struct iio_dev * indio_dev)426 static void ms5611_fini(const struct iio_dev *indio_dev)
427 {
428 const struct ms5611_state *st = iio_priv(indio_dev);
429
430 regulator_disable(st->vdd);
431 }
432
ms5611_probe(struct iio_dev * indio_dev,struct device * dev,const char * name,int type)433 int ms5611_probe(struct iio_dev *indio_dev, struct device *dev,
434 const char *name, int type)
435 {
436 int ret;
437 struct ms5611_state *st = iio_priv(indio_dev);
438
439 mutex_init(&st->lock);
440 st->chip_info = &chip_info_tbl[type];
441 st->temp_osr =
442 &ms5611_avail_temp_osr[ARRAY_SIZE(ms5611_avail_temp_osr) - 1];
443 st->pressure_osr =
444 &ms5611_avail_pressure_osr[ARRAY_SIZE(ms5611_avail_pressure_osr)
445 - 1];
446 indio_dev->dev.parent = dev;
447 indio_dev->name = name;
448 indio_dev->info = &ms5611_info;
449 indio_dev->channels = ms5611_channels;
450 indio_dev->num_channels = ARRAY_SIZE(ms5611_channels);
451 indio_dev->modes = INDIO_DIRECT_MODE;
452 indio_dev->available_scan_masks = ms5611_scan_masks;
453
454 ret = ms5611_init(indio_dev);
455 if (ret < 0)
456 return ret;
457
458 ret = iio_triggered_buffer_setup(indio_dev, NULL,
459 ms5611_trigger_handler, NULL);
460 if (ret < 0) {
461 dev_err(dev, "iio triggered buffer setup failed\n");
462 goto err_fini;
463 }
464
465 ret = iio_device_register(indio_dev);
466 if (ret < 0) {
467 dev_err(dev, "unable to register iio device\n");
468 goto err_buffer_cleanup;
469 }
470
471 return 0;
472
473 err_buffer_cleanup:
474 iio_triggered_buffer_cleanup(indio_dev);
475 err_fini:
476 ms5611_fini(indio_dev);
477 return ret;
478 }
479 EXPORT_SYMBOL(ms5611_probe);
480
ms5611_remove(struct iio_dev * indio_dev)481 int ms5611_remove(struct iio_dev *indio_dev)
482 {
483 iio_device_unregister(indio_dev);
484 iio_triggered_buffer_cleanup(indio_dev);
485 ms5611_fini(indio_dev);
486
487 return 0;
488 }
489 EXPORT_SYMBOL(ms5611_remove);
490
491 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
492 MODULE_DESCRIPTION("MS5611 core driver");
493 MODULE_LICENSE("GPL v2");
494