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