These changes are the raw update to linux-4.4.6-rt14. Kernel sources
[kvmfornfv.git] / kernel / drivers / iio / pressure / ms5611_core.c
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
20 #include "ms5611.h"
21
22 static bool ms5611_prom_is_valid(u16 *prom, size_t len)
23 {
24         int i, j;
25         uint16_t crc = 0, crc_orig = prom[7] & 0x000F;
26
27         prom[7] &= 0xFF00;
28
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;
34
35                 for (j = 0; j < 8; j++) {
36                         if (crc & 0x8000)
37                                 crc = (crc << 1) ^ 0x3000;
38                         else
39                                 crc <<= 1;
40                 }
41         }
42
43         crc = (crc >> 12) & 0x000F;
44
45         return crc_orig != 0x0000 && crc == crc_orig;
46 }
47
48 static int ms5611_read_prom(struct iio_dev *indio_dev)
49 {
50         int ret, i;
51         struct ms5611_state *st = iio_priv(indio_dev);
52
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;
60                 }
61         }
62
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;
66         }
67
68         return 0;
69 }
70
71 static int ms5611_read_temp_and_pressure(struct iio_dev *indio_dev,
72                                          s32 *temp, s32 *pressure)
73 {
74         int ret;
75         struct ms5611_state *st = iio_priv(indio_dev);
76
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;
82         }
83
84         return st->chip_info->temp_and_pressure_compensate(st->chip_info,
85                                                            temp, pressure);
86 }
87
88 static int ms5611_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
89                                                s32 *temp, s32 *pressure)
90 {
91         s32 t = *temp, p = *pressure;
92         s64 off, sens, dt;
93
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);
97
98         t = 2000 + ((chip_info->prom[6] * dt) >> 23);
99         if (t < 2000) {
100                 s64 off2, sens2, t2;
101
102                 t2 = (dt * dt) >> 31;
103                 off2 = (5 * (t - 2000) * (t - 2000)) >> 1;
104                 sens2 = off2 >> 1;
105
106                 if (t < -1500) {
107                         s64 tmp = (t + 1500) * (t + 1500);
108
109                         off2 += 7 * tmp;
110                         sens2 += (11 * tmp) >> 1;
111                 }
112
113                 t -= t2;
114                 off -= off2;
115                 sens -= sens2;
116         }
117
118         *temp = t;
119         *pressure = (((p * sens) >> 21) - off) >> 15;
120
121         return 0;
122 }
123
124 static int ms5607_temp_and_pressure_compensate(struct ms5611_chip_info *chip_info,
125                                                s32 *temp, s32 *pressure)
126 {
127         s32 t = *temp, p = *pressure;
128         s64 off, sens, dt;
129
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);
133
134         t = 2000 + ((chip_info->prom[6] * dt) >> 23);
135         if (t < 2000) {
136                 s64 off2, sens2, t2;
137
138                 t2 = (dt * dt) >> 31;
139                 off2 = (61 * (t - 2000) * (t - 2000)) >> 4;
140                 sens2 = off2 << 1;
141
142                 if (t < -1500) {
143                         s64 tmp = (t + 1500) * (t + 1500);
144
145                         off2 += 15 * tmp;
146                         sens2 += (8 * tmp);
147                 }
148
149                 t -= t2;
150                 off -= off2;
151                 sens -= sens2;
152         }
153
154         *temp = t;
155         *pressure = (((p * sens) >> 21) - off) >> 15;
156
157         return 0;
158 }
159
160 static int ms5611_reset(struct iio_dev *indio_dev)
161 {
162         int ret;
163         struct ms5611_state *st = iio_priv(indio_dev);
164
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;
169         }
170
171         usleep_range(3000, 4000);
172
173         return 0;
174 }
175
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)
179 {
180         int ret;
181         s32 temp, pressure;
182         struct ms5611_state *st = iio_priv(indio_dev);
183
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;
192
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;
203                 }
204         }
205
206         return -EINVAL;
207 }
208
209 static struct ms5611_chip_info chip_info_tbl[] = {
210         [MS5611] = {
211                 .temp_and_pressure_compensate = ms5611_temp_and_pressure_compensate,
212         },
213         [MS5607] = {
214                 .temp_and_pressure_compensate = ms5607_temp_and_pressure_compensate,
215         }
216 };
217
218 static const struct iio_chan_spec ms5611_channels[] = {
219         {
220                 .type = IIO_PRESSURE,
221                 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
222         },
223         {
224                 .type = IIO_TEMP,
225                 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED),
226         }
227 };
228
229 static const struct iio_info ms5611_info = {
230         .read_raw = &ms5611_read_raw,
231         .driver_module = THIS_MODULE,
232 };
233
234 static int ms5611_init(struct iio_dev *indio_dev)
235 {
236         int ret;
237
238         ret = ms5611_reset(indio_dev);
239         if (ret < 0)
240                 return ret;
241
242         return ms5611_read_prom(indio_dev);
243 }
244
245 int ms5611_probe(struct iio_dev *indio_dev, struct device *dev, int type)
246 {
247         int ret;
248         struct ms5611_state *st = iio_priv(indio_dev);
249
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;
258
259         ret = ms5611_init(indio_dev);
260         if (ret < 0)
261                 return ret;
262
263         return devm_iio_device_register(dev, indio_dev);
264 }
265 EXPORT_SYMBOL(ms5611_probe);
266
267 MODULE_AUTHOR("Tomasz Duszynski <tduszyns@gmail.com>");
268 MODULE_DESCRIPTION("MS5611 core driver");
269 MODULE_LICENSE("GPL v2");