dm writecache: add cond_resched to loop in persistent_memory_claim()
[linux/fpc-iii.git] / drivers / iio / temperature / max31856.c
blobb4cb21ab2e856edae8195258c14cef4735dbac09
1 // SPDX-License-Identifier: GPL-2.0
2 /* max31856.c
4 * Maxim MAX31856 thermocouple sensor driver
6 * Copyright (C) 2018-2019 Rockwell Collins
7 */
9 #include <linux/ctype.h>
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/err.h>
13 #include <linux/spi/spi.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/util_macros.h>
17 #include <dt-bindings/iio/temperature/thermocouple.h>
19 * The MSB of the register value determines whether the following byte will
20 * be written or read. If it is 0, one or more byte reads will follow.
22 #define MAX31856_RD_WR_BIT BIT(7)
24 #define MAX31856_CR0_AUTOCONVERT BIT(7)
25 #define MAX31856_CR0_1SHOT BIT(6)
26 #define MAX31856_CR0_OCFAULT BIT(4)
27 #define MAX31856_CR0_OCFAULT_MASK GENMASK(5, 4)
28 #define MAX31856_CR0_FILTER_50HZ BIT(0)
29 #define MAX31856_AVERAGING_MASK GENMASK(6, 4)
30 #define MAX31856_AVERAGING_SHIFT 4
31 #define MAX31856_TC_TYPE_MASK GENMASK(3, 0)
32 #define MAX31856_FAULT_OVUV BIT(1)
33 #define MAX31856_FAULT_OPEN BIT(0)
35 /* The MAX31856 registers */
36 #define MAX31856_CR0_REG 0x00
37 #define MAX31856_CR1_REG 0x01
38 #define MAX31856_MASK_REG 0x02
39 #define MAX31856_CJHF_REG 0x03
40 #define MAX31856_CJLF_REG 0x04
41 #define MAX31856_LTHFTH_REG 0x05
42 #define MAX31856_LTHFTL_REG 0x06
43 #define MAX31856_LTLFTH_REG 0x07
44 #define MAX31856_LTLFTL_REG 0x08
45 #define MAX31856_CJTO_REG 0x09
46 #define MAX31856_CJTH_REG 0x0A
47 #define MAX31856_CJTL_REG 0x0B
48 #define MAX31856_LTCBH_REG 0x0C
49 #define MAX31856_LTCBM_REG 0x0D
50 #define MAX31856_LTCBL_REG 0x0E
51 #define MAX31856_SR_REG 0x0F
53 static const struct iio_chan_spec max31856_channels[] = {
54 { /* Thermocouple Temperature */
55 .type = IIO_TEMP,
56 .info_mask_separate =
57 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
58 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE),
59 .info_mask_shared_by_type =
60 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)
62 { /* Cold Junction Temperature */
63 .type = IIO_TEMP,
64 .channel2 = IIO_MOD_TEMP_AMBIENT,
65 .modified = 1,
66 .info_mask_separate =
67 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
68 .info_mask_shared_by_type =
69 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)
73 struct max31856_data {
74 struct spi_device *spi;
75 u32 thermocouple_type;
76 bool filter_50hz;
77 int averaging;
80 static const char max31856_tc_types[] = {
81 'B', 'E', 'J', 'K', 'N', 'R', 'S', 'T'
84 static int max31856_read(struct max31856_data *data, u8 reg,
85 u8 val[], unsigned int read_size)
87 return spi_write_then_read(data->spi, &reg, 1, val, read_size);
90 static int max31856_write(struct max31856_data *data, u8 reg,
91 unsigned int val)
93 u8 buf[2];
95 buf[0] = reg | (MAX31856_RD_WR_BIT);
96 buf[1] = val;
98 return spi_write(data->spi, buf, 2);
101 static int max31856_init(struct max31856_data *data)
103 int ret;
104 u8 reg_cr0_val, reg_cr1_val;
106 /* Start by changing to Off mode before making changes as
107 * some settings are recommended to be set only when the device
108 * is off
110 ret = max31856_read(data, MAX31856_CR0_REG, &reg_cr0_val, 1);
111 if (ret)
112 return ret;
114 reg_cr0_val &= ~MAX31856_CR0_AUTOCONVERT;
115 ret = max31856_write(data, MAX31856_CR0_REG, reg_cr0_val);
116 if (ret)
117 return ret;
119 /* Set thermocouple type based on dts property */
120 ret = max31856_read(data, MAX31856_CR1_REG, &reg_cr1_val, 1);
121 if (ret)
122 return ret;
124 reg_cr1_val &= ~MAX31856_TC_TYPE_MASK;
125 reg_cr1_val |= data->thermocouple_type;
127 reg_cr1_val &= ~MAX31856_AVERAGING_MASK;
128 reg_cr1_val |= data->averaging << MAX31856_AVERAGING_SHIFT;
130 ret = max31856_write(data, MAX31856_CR1_REG, reg_cr1_val);
131 if (ret)
132 return ret;
135 * Enable Open circuit fault detection
136 * Read datasheet for more information: Table 4.
137 * Value 01 means : Enabled (Once every 16 conversions)
139 reg_cr0_val &= ~MAX31856_CR0_OCFAULT_MASK;
140 reg_cr0_val |= MAX31856_CR0_OCFAULT;
142 /* Set Auto Conversion Mode */
143 reg_cr0_val &= ~MAX31856_CR0_1SHOT;
144 reg_cr0_val |= MAX31856_CR0_AUTOCONVERT;
146 if (data->filter_50hz)
147 reg_cr0_val |= MAX31856_CR0_FILTER_50HZ;
148 else
149 reg_cr0_val &= ~MAX31856_CR0_FILTER_50HZ;
151 return max31856_write(data, MAX31856_CR0_REG, reg_cr0_val);
154 static int max31856_thermocouple_read(struct max31856_data *data,
155 struct iio_chan_spec const *chan,
156 int *val)
158 int ret, offset_cjto;
159 u8 reg_val[3];
161 switch (chan->channel2) {
162 case IIO_NO_MOD:
164 * Multibyte Read
165 * MAX31856_LTCBH_REG, MAX31856_LTCBM_REG, MAX31856_LTCBL_REG
167 ret = max31856_read(data, MAX31856_LTCBH_REG, reg_val, 3);
168 if (ret)
169 return ret;
170 /* Skip last 5 dead bits of LTCBL */
171 *val = (reg_val[0] << 16 | reg_val[1] << 8 | reg_val[2]) >> 5;
172 /* Check 7th bit of LTCBH reg. value for sign*/
173 if (reg_val[0] & 0x80)
174 *val -= 0x80000;
175 break;
177 case IIO_MOD_TEMP_AMBIENT:
179 * Multibyte Read
180 * MAX31856_CJTO_REG, MAX31856_CJTH_REG, MAX31856_CJTL_REG
182 ret = max31856_read(data, MAX31856_CJTO_REG, reg_val, 3);
183 if (ret)
184 return ret;
185 /* Get Cold Junction Temp. offset register value */
186 offset_cjto = reg_val[0];
187 /* Get CJTH and CJTL value and skip last 2 dead bits of CJTL */
188 *val = (reg_val[1] << 8 | reg_val[2]) >> 2;
189 /* As per datasheet add offset into CJTH and CJTL */
190 *val += offset_cjto;
191 /* Check 7th bit of CJTH reg. value for sign */
192 if (reg_val[1] & 0x80)
193 *val -= 0x4000;
194 break;
196 default:
197 return -EINVAL;
200 ret = max31856_read(data, MAX31856_SR_REG, reg_val, 1);
201 if (ret)
202 return ret;
203 /* Check for over/under voltage or open circuit fault */
204 if (reg_val[0] & (MAX31856_FAULT_OVUV | MAX31856_FAULT_OPEN))
205 return -EIO;
207 return ret;
210 static int max31856_read_raw(struct iio_dev *indio_dev,
211 struct iio_chan_spec const *chan,
212 int *val, int *val2, long mask)
214 struct max31856_data *data = iio_priv(indio_dev);
215 int ret;
217 switch (mask) {
218 case IIO_CHAN_INFO_RAW:
219 ret = max31856_thermocouple_read(data, chan, val);
220 if (ret)
221 return ret;
222 return IIO_VAL_INT;
223 case IIO_CHAN_INFO_SCALE:
224 switch (chan->channel2) {
225 case IIO_MOD_TEMP_AMBIENT:
226 /* Cold junction Temp. Data resolution is 0.015625 */
227 *val = 15;
228 *val2 = 625000; /* 1000 * 0.015625 */
229 ret = IIO_VAL_INT_PLUS_MICRO;
230 break;
231 default:
232 /* Thermocouple Temp. Data resolution is 0.0078125 */
233 *val = 7;
234 *val2 = 812500; /* 1000 * 0.0078125) */
235 return IIO_VAL_INT_PLUS_MICRO;
237 break;
238 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
239 *val = 1 << data->averaging;
240 return IIO_VAL_INT;
241 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
242 *val = max31856_tc_types[data->thermocouple_type];
243 return IIO_VAL_CHAR;
244 default:
245 ret = -EINVAL;
246 break;
249 return ret;
252 static int max31856_write_raw_get_fmt(struct iio_dev *indio_dev,
253 struct iio_chan_spec const *chan,
254 long mask)
256 switch (mask) {
257 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
258 return IIO_VAL_CHAR;
259 default:
260 return IIO_VAL_INT;
264 static int max31856_write_raw(struct iio_dev *indio_dev,
265 struct iio_chan_spec const *chan,
266 int val, int val2, long mask)
268 struct max31856_data *data = iio_priv(indio_dev);
269 int msb;
271 switch (mask) {
272 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
273 if (val > 16 || val < 1)
274 return -EINVAL;
275 msb = fls(val) - 1;
276 /* Round up to next 2pow if needed */
277 if (BIT(msb) < val)
278 msb++;
280 data->averaging = msb;
281 max31856_init(data);
282 break;
283 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
285 int tc_type = -1;
286 int i;
288 for (i = 0; i < ARRAY_SIZE(max31856_tc_types); i++) {
289 if (max31856_tc_types[i] == toupper(val)) {
290 tc_type = i;
291 break;
294 if (tc_type < 0)
295 return -EINVAL;
297 data->thermocouple_type = tc_type;
298 max31856_init(data);
299 break;
301 default:
302 return -EINVAL;
305 return 0;
308 static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf)
310 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
311 struct max31856_data *data = iio_priv(indio_dev);
312 u8 reg_val;
313 int ret;
314 bool fault;
316 ret = max31856_read(data, MAX31856_SR_REG, &reg_val, 1);
317 if (ret)
318 return ret;
320 fault = reg_val & faultbit;
322 return sprintf(buf, "%d\n", fault);
325 static ssize_t show_fault_ovuv(struct device *dev,
326 struct device_attribute *attr,
327 char *buf)
329 return show_fault(dev, MAX31856_FAULT_OVUV, buf);
332 static ssize_t show_fault_oc(struct device *dev,
333 struct device_attribute *attr,
334 char *buf)
336 return show_fault(dev, MAX31856_FAULT_OPEN, buf);
339 static ssize_t show_filter(struct device *dev,
340 struct device_attribute *attr,
341 char *buf)
343 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
344 struct max31856_data *data = iio_priv(indio_dev);
346 return sprintf(buf, "%d\n", data->filter_50hz ? 50 : 60);
349 static ssize_t set_filter(struct device *dev,
350 struct device_attribute *attr,
351 const char *buf,
352 size_t len)
354 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
355 struct max31856_data *data = iio_priv(indio_dev);
356 unsigned int freq;
357 int ret;
359 ret = kstrtouint(buf, 10, &freq);
360 if (ret)
361 return ret;
363 switch (freq) {
364 case 50:
365 data->filter_50hz = true;
366 break;
367 case 60:
368 data->filter_50hz = false;
369 break;
370 default:
371 return -EINVAL;
374 max31856_init(data);
375 return len;
378 static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0);
379 static IIO_DEVICE_ATTR(fault_oc, 0444, show_fault_oc, NULL, 0);
380 static IIO_DEVICE_ATTR(in_temp_filter_notch_center_frequency, 0644,
381 show_filter, set_filter, 0);
383 static struct attribute *max31856_attributes[] = {
384 &iio_dev_attr_fault_ovuv.dev_attr.attr,
385 &iio_dev_attr_fault_oc.dev_attr.attr,
386 &iio_dev_attr_in_temp_filter_notch_center_frequency.dev_attr.attr,
387 NULL,
390 static const struct attribute_group max31856_group = {
391 .attrs = max31856_attributes,
394 static const struct iio_info max31856_info = {
395 .read_raw = max31856_read_raw,
396 .write_raw = max31856_write_raw,
397 .write_raw_get_fmt = max31856_write_raw_get_fmt,
398 .attrs = &max31856_group,
401 static int max31856_probe(struct spi_device *spi)
403 const struct spi_device_id *id = spi_get_device_id(spi);
404 struct iio_dev *indio_dev;
405 struct max31856_data *data;
406 int ret;
408 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
409 if (!indio_dev)
410 return -ENOMEM;
412 data = iio_priv(indio_dev);
413 data->spi = spi;
414 data->filter_50hz = false;
416 spi_set_drvdata(spi, indio_dev);
418 indio_dev->info = &max31856_info;
419 indio_dev->dev.parent = &spi->dev;
420 indio_dev->dev.of_node = spi->dev.of_node;
421 indio_dev->name = id->name;
422 indio_dev->modes = INDIO_DIRECT_MODE;
423 indio_dev->channels = max31856_channels;
424 indio_dev->num_channels = ARRAY_SIZE(max31856_channels);
426 ret = of_property_read_u32(spi->dev.of_node, "thermocouple-type",
427 &data->thermocouple_type);
429 if (ret) {
430 dev_info(&spi->dev,
431 "Could not read thermocouple type DT property, configuring as a K-Type\n");
432 data->thermocouple_type = THERMOCOUPLE_TYPE_K;
436 * no need to translate values as the supported types
437 * have the same value as the #defines
439 switch (data->thermocouple_type) {
440 case THERMOCOUPLE_TYPE_B:
441 case THERMOCOUPLE_TYPE_E:
442 case THERMOCOUPLE_TYPE_J:
443 case THERMOCOUPLE_TYPE_K:
444 case THERMOCOUPLE_TYPE_N:
445 case THERMOCOUPLE_TYPE_R:
446 case THERMOCOUPLE_TYPE_S:
447 case THERMOCOUPLE_TYPE_T:
448 break;
449 default:
450 dev_err(&spi->dev,
451 "error: thermocouple-type %u not supported by max31856\n"
452 , data->thermocouple_type);
453 return -EINVAL;
456 ret = max31856_init(data);
457 if (ret) {
458 dev_err(&spi->dev, "error: Failed to configure max31856\n");
459 return ret;
462 return devm_iio_device_register(&spi->dev, indio_dev);
465 static const struct spi_device_id max31856_id[] = {
466 { "max31856", 0 },
469 MODULE_DEVICE_TABLE(spi, max31856_id);
471 static const struct of_device_id max31856_of_match[] = {
472 { .compatible = "maxim,max31856" },
475 MODULE_DEVICE_TABLE(of, max31856_of_match);
477 static struct spi_driver max31856_driver = {
478 .driver = {
479 .name = "max31856",
480 .of_match_table = max31856_of_match,
482 .probe = max31856_probe,
483 .id_table = max31856_id,
485 module_spi_driver(max31856_driver);
487 MODULE_AUTHOR("Paresh Chaudhary <paresh.chaudhary@rockwellcollins.com>");
488 MODULE_AUTHOR("Patrick Havelange <patrick.havelange@essensium.com>");
489 MODULE_DESCRIPTION("Maxim MAX31856 thermocouple sensor driver");
490 MODULE_LICENSE("GPL");