1 // SPDX-License-Identifier: GPL-2.0
4 * Maxim MAX31856 thermocouple sensor driver
6 * Copyright (C) 2018-2019 Rockwell Collins
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 */
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 */
64 .channel2 = IIO_MOD_TEMP_AMBIENT,
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;
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, ®, 1, val, read_size);
90 static int max31856_write(struct max31856_data *data, u8 reg,
95 buf[0] = reg | (MAX31856_RD_WR_BIT);
98 return spi_write(data->spi, buf, 2);
101 static int max31856_init(struct max31856_data *data)
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
110 ret = max31856_read(data, MAX31856_CR0_REG, ®_cr0_val, 1);
114 reg_cr0_val &= ~MAX31856_CR0_AUTOCONVERT;
115 ret = max31856_write(data, MAX31856_CR0_REG, reg_cr0_val);
119 /* Set thermocouple type based on dts property */
120 ret = max31856_read(data, MAX31856_CR1_REG, ®_cr1_val, 1);
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);
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;
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,
158 int ret, offset_cjto;
161 switch (chan->channel2) {
165 * MAX31856_LTCBH_REG, MAX31856_LTCBM_REG, MAX31856_LTCBL_REG
167 ret = max31856_read(data, MAX31856_LTCBH_REG, reg_val, 3);
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)
177 case IIO_MOD_TEMP_AMBIENT:
180 * MAX31856_CJTO_REG, MAX31856_CJTH_REG, MAX31856_CJTL_REG
182 ret = max31856_read(data, MAX31856_CJTO_REG, reg_val, 3);
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 */
191 /* Check 7th bit of CJTH reg. value for sign */
192 if (reg_val[1] & 0x80)
200 ret = max31856_read(data, MAX31856_SR_REG, reg_val, 1);
203 /* Check for over/under voltage or open circuit fault */
204 if (reg_val[0] & (MAX31856_FAULT_OVUV | MAX31856_FAULT_OPEN))
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);
218 case IIO_CHAN_INFO_RAW:
219 ret = max31856_thermocouple_read(data, chan, val);
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 */
228 *val2 = 625000; /* 1000 * 0.015625 */
229 ret = IIO_VAL_INT_PLUS_MICRO;
232 /* Thermocouple Temp. Data resolution is 0.0078125 */
234 *val2 = 812500; /* 1000 * 0.0078125) */
235 return IIO_VAL_INT_PLUS_MICRO;
238 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
239 *val = 1 << data->averaging;
241 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
242 *val = max31856_tc_types[data->thermocouple_type];
252 static int max31856_write_raw_get_fmt(struct iio_dev *indio_dev,
253 struct iio_chan_spec const *chan,
257 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
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);
272 case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
273 if (val > 16 || val < 1)
276 /* Round up to next 2pow if needed */
280 data->averaging = msb;
283 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
288 for (i = 0; i < ARRAY_SIZE(max31856_tc_types); i++) {
289 if (max31856_tc_types[i] == toupper(val)) {
297 data->thermocouple_type = tc_type;
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);
316 ret = max31856_read(data, MAX31856_SR_REG, ®_val, 1);
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,
329 return show_fault(dev, MAX31856_FAULT_OVUV, buf);
332 static ssize_t show_fault_oc(struct device *dev,
333 struct device_attribute *attr,
336 return show_fault(dev, MAX31856_FAULT_OPEN, buf);
339 static ssize_t show_filter(struct device *dev,
340 struct device_attribute *attr,
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,
354 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
355 struct max31856_data *data = iio_priv(indio_dev);
359 ret = kstrtouint(buf, 10, &freq);
365 data->filter_50hz = true;
368 data->filter_50hz = false;
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,
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;
408 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
412 data = iio_priv(indio_dev);
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);
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:
451 "error: thermocouple-type %u not supported by max31856\n"
452 , data->thermocouple_type);
456 ret = max31856_init(data);
458 dev_err(&spi->dev, "error: Failed to configure max31856\n");
462 return devm_iio_device_register(&spi->dev, indio_dev);
465 static const struct spi_device_id max31856_id[] = {
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 = {
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");