irqchip/atmel-aic: Remove duplicate bit operation
[linux-2.6/btrfs-unstable.git] / drivers / iio / pressure / ms5611_core.c
blob2f3d9b4aca4ec695b99f75e53e93af6107bf1d01
1 /*
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.
10 * Data sheet:
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>
20 #include "ms5611.h"
22 static bool ms5611_prom_is_valid(u16 *prom, size_t len)
24 int i, j;
25 uint16_t crc = 0, crc_orig = prom[7] & 0x000F;
27 prom[7] &= 0xFF00;
29 for (i = 0; i < len * 2; i++) {
30 if (i % 2 == 1)
31 crc ^= prom[i >> 1] & 0x00FF;
32 else
33 crc ^= prom[i >> 1] >> 8;
35 for (j = 0; j < 8; j++) {
36 if (crc & 0x8000)
37 crc = (crc << 1) ^ 0x3000;
38 else
39 crc <<= 1;
43 crc = (crc >> 12) & 0x000F;
45 return crc_orig != 0x0000 && crc == crc_orig;
48 static int ms5611_read_prom(struct iio_dev *indio_dev)
50 int ret, i;
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]);
56 if (ret < 0) {
57 dev_err(&indio_dev->dev,
58 "failed to read prom at %d\n", i);
59 return ret;
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");
65 return -ENODEV;
68 return 0;
71 static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev,
72 s32 *temp, s32 *pressure)
74 int ret;
75 struct ms5611_state *st = iio_priv(indio_dev);
77 ret = st->read_adc_temp_and_pressure(&indio_dev->dev, temp, pressure);
78 if (ret < 0) {
79 dev_err(&indio_dev->dev,
80 "failed to read temperature and pressure\n");
81 return ret;
84 return st->chip_info->temp_and_pressure_compensate(st->chip_info,
85 temp, pressure);
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;
92 s64 off, sens, dt;
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);
99 if (t < 2000) {
100 s64 off2, sens2, t2;
102 t2 = (dt * dt) >> 31;
103 off2 = (5 * (t - 2000) * (t - 2000)) >> 1;
104 sens2 = off2 >> 1;
106 if (t < -1500) {
107 s64 tmp = (t + 1500) * (t + 1500);
109 off2 += 7 * tmp;
110 sens2 += (11 * tmp) >> 1;
113 t -= t2;
114 off -= off2;
115 sens -= sens2;
118 *temp = t;
119 *pressure = (((p * sens) >> 21) - off) >> 15;
121 return 0;
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;
128 s64 off, sens, dt;
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);
135 if (t < 2000) {
136 s64 off2, sens2, t2;
138 t2 = (dt * dt) >> 31;
139 off2 = (61 * (t - 2000) * (t - 2000)) >> 4;
140 sens2 = off2 << 1;
142 if (t < -1500) {
143 s64 tmp = (t + 1500) * (t + 1500);
145 off2 += 15 * tmp;
146 sens2 += (8 * tmp);
149 t -= t2;
150 off -= off2;
151 sens -= sens2;
154 *temp = t;
155 *pressure = (((p * sens) >> 21) - off) >> 15;
157 return 0;
160 static int ms5611_reset(struct iio_dev *indio_dev)
162 int ret;
163 struct ms5611_state *st = iio_priv(indio_dev);
165 ret = st->reset(&indio_dev->dev);
166 if (ret < 0) {
167 dev_err(&indio_dev->dev, "failed to reset device\n");
168 return ret;
171 usleep_range(3000, 4000);
173 return 0;
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)
180 int ret;
181 s32 temp, pressure;
182 struct ms5611_state *st = iio_priv(indio_dev);
184 switch (mask) {
185 case IIO_CHAN_INFO_PROCESSED:
186 mutex_lock(&st->lock);
187 ret = ms5611_read_temp_and_pressure(indio_dev,
188 &temp, &pressure);
189 mutex_unlock(&st->lock);
190 if (ret < 0)
191 return ret;
193 switch (chan->type) {
194 case IIO_TEMP:
195 *val = temp * 10;
196 return IIO_VAL_INT;
197 case IIO_PRESSURE:
198 *val = pressure / 1000;
199 *val2 = (pressure % 1000) * 1000;
200 return IIO_VAL_INT_PLUS_MICRO;
201 default:
202 return -EINVAL;
206 return -EINVAL;
209 static struct ms5611_chip_info chip_info_tbl[] = {
210 [MS5611] = {
211 .temp_and_pressure_compensate = ms5611_temp_and_pressure_compensate,
213 [MS5607] = {
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),
224 .type = IIO_TEMP,
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)
236 int ret;
238 ret = ms5611_reset(indio_dev);
239 if (ret < 0)
240 return ret;
242 return ms5611_read_prom(indio_dev);
245 int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, int type)
247 int ret;
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);
260 if (ret < 0)
261 return ret;
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");