1 // SPDX-License-Identifier: GPL-2.0
4 * Maxim MAX31856 thermocouple sensor driver
6 * Copyright (C) 2018-2019 Rockwell Collins
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/err.h>
12 #include <linux/spi/spi.h>
13 #include <linux/iio/iio.h>
14 #include <linux/iio/sysfs.h>
15 #include <dt-bindings/iio/temperature/thermocouple.h>
17 * The MSB of the register value determines whether the following byte will
18 * be written or read. If it is 0, one or more byte reads will follow.
20 #define MAX31856_RD_WR_BIT BIT(7)
22 #define MAX31856_CR0_AUTOCONVERT BIT(7)
23 #define MAX31856_CR0_1SHOT BIT(6)
24 #define MAX31856_CR0_OCFAULT BIT(4)
25 #define MAX31856_CR0_OCFAULT_MASK GENMASK(5, 4)
26 #define MAX31856_TC_TYPE_MASK GENMASK(3, 0)
27 #define MAX31856_FAULT_OVUV BIT(1)
28 #define MAX31856_FAULT_OPEN BIT(0)
30 /* The MAX31856 registers */
31 #define MAX31856_CR0_REG 0x00
32 #define MAX31856_CR1_REG 0x01
33 #define MAX31856_MASK_REG 0x02
34 #define MAX31856_CJHF_REG 0x03
35 #define MAX31856_CJLF_REG 0x04
36 #define MAX31856_LTHFTH_REG 0x05
37 #define MAX31856_LTHFTL_REG 0x06
38 #define MAX31856_LTLFTH_REG 0x07
39 #define MAX31856_LTLFTL_REG 0x08
40 #define MAX31856_CJTO_REG 0x09
41 #define MAX31856_CJTH_REG 0x0A
42 #define MAX31856_CJTL_REG 0x0B
43 #define MAX31856_LTCBH_REG 0x0C
44 #define MAX31856_LTCBM_REG 0x0D
45 #define MAX31856_LTCBL_REG 0x0E
46 #define MAX31856_SR_REG 0x0F
48 static const struct iio_chan_spec max31856_channels
[] = {
49 { /* Thermocouple Temperature */
52 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
54 { /* Cold Junction Temperature */
56 .channel2
= IIO_MOD_TEMP_AMBIENT
,
59 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
63 struct max31856_data
{
64 struct spi_device
*spi
;
65 u32 thermocouple_type
;
68 static int max31856_read(struct max31856_data
*data
, u8 reg
,
69 u8 val
[], unsigned int read_size
)
71 return spi_write_then_read(data
->spi
, ®
, 1, val
, read_size
);
74 static int max31856_write(struct max31856_data
*data
, u8 reg
,
79 buf
[0] = reg
| (MAX31856_RD_WR_BIT
);
82 return spi_write(data
->spi
, buf
, 2);
85 static int max31856_init(struct max31856_data
*data
)
88 u8 reg_cr0_val
, reg_cr1_val
;
90 /* Start by changing to Off mode before making changes as
91 * some settings are recommended to be set only when the device
94 ret
= max31856_read(data
, MAX31856_CR0_REG
, ®_cr0_val
, 1);
98 reg_cr0_val
&= ~MAX31856_CR0_AUTOCONVERT
;
99 ret
= max31856_write(data
, MAX31856_CR0_REG
, reg_cr0_val
);
103 /* Set thermocouple type based on dts property */
104 ret
= max31856_read(data
, MAX31856_CR1_REG
, ®_cr1_val
, 1);
108 reg_cr1_val
&= ~MAX31856_TC_TYPE_MASK
;
109 reg_cr1_val
|= data
->thermocouple_type
;
110 ret
= max31856_write(data
, MAX31856_CR1_REG
, reg_cr1_val
);
115 * Enable Open circuit fault detection
116 * Read datasheet for more information: Table 4.
117 * Value 01 means : Enabled (Once every 16 conversions)
119 reg_cr0_val
&= ~MAX31856_CR0_OCFAULT_MASK
;
120 reg_cr0_val
|= MAX31856_CR0_OCFAULT
;
122 /* Set Auto Conversion Mode */
123 reg_cr0_val
&= ~MAX31856_CR0_1SHOT
;
124 reg_cr0_val
|= MAX31856_CR0_AUTOCONVERT
;
126 return max31856_write(data
, MAX31856_CR0_REG
, reg_cr0_val
);
129 static int max31856_thermocouple_read(struct max31856_data
*data
,
130 struct iio_chan_spec
const *chan
,
133 int ret
, offset_cjto
;
136 switch (chan
->channel2
) {
140 * MAX31856_LTCBH_REG, MAX31856_LTCBM_REG, MAX31856_LTCBL_REG
142 ret
= max31856_read(data
, MAX31856_LTCBH_REG
, reg_val
, 3);
145 /* Skip last 5 dead bits of LTCBL */
146 *val
= (reg_val
[0] << 16 | reg_val
[1] << 8 | reg_val
[2]) >> 5;
147 /* Check 7th bit of LTCBH reg. value for sign*/
148 if (reg_val
[0] & 0x80)
152 case IIO_MOD_TEMP_AMBIENT
:
155 * MAX31856_CJTO_REG, MAX31856_CJTH_REG, MAX31856_CJTL_REG
157 ret
= max31856_read(data
, MAX31856_CJTO_REG
, reg_val
, 3);
160 /* Get Cold Junction Temp. offset register value */
161 offset_cjto
= reg_val
[0];
162 /* Get CJTH and CJTL value and skip last 2 dead bits of CJTL */
163 *val
= (reg_val
[1] << 8 | reg_val
[2]) >> 2;
164 /* As per datasheet add offset into CJTH and CJTL */
166 /* Check 7th bit of CJTH reg. value for sign */
167 if (reg_val
[1] & 0x80)
175 ret
= max31856_read(data
, MAX31856_SR_REG
, reg_val
, 1);
178 /* Check for over/under voltage or open circuit fault */
179 if (reg_val
[0] & (MAX31856_FAULT_OVUV
| MAX31856_FAULT_OPEN
))
185 static int max31856_read_raw(struct iio_dev
*indio_dev
,
186 struct iio_chan_spec
const *chan
,
187 int *val
, int *val2
, long mask
)
189 struct max31856_data
*data
= iio_priv(indio_dev
);
193 case IIO_CHAN_INFO_RAW
:
194 ret
= max31856_thermocouple_read(data
, chan
, val
);
198 case IIO_CHAN_INFO_SCALE
:
199 switch (chan
->channel2
) {
200 case IIO_MOD_TEMP_AMBIENT
:
201 /* Cold junction Temp. Data resolution is 0.015625 */
203 *val2
= 625000; /* 1000 * 0.015625 */
204 ret
= IIO_VAL_INT_PLUS_MICRO
;
207 /* Thermocouple Temp. Data resolution is 0.0078125 */
209 *val2
= 812500; /* 1000 * 0.0078125) */
210 return IIO_VAL_INT_PLUS_MICRO
;
221 static ssize_t
show_fault(struct device
*dev
, u8 faultbit
, char *buf
)
223 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
224 struct max31856_data
*data
= iio_priv(indio_dev
);
229 ret
= max31856_read(data
, MAX31856_SR_REG
, ®_val
, 1);
233 fault
= reg_val
& faultbit
;
235 return sprintf(buf
, "%d\n", fault
);
238 static ssize_t
show_fault_ovuv(struct device
*dev
,
239 struct device_attribute
*attr
,
242 return show_fault(dev
, MAX31856_FAULT_OVUV
, buf
);
245 static ssize_t
show_fault_oc(struct device
*dev
,
246 struct device_attribute
*attr
,
249 return show_fault(dev
, MAX31856_FAULT_OPEN
, buf
);
252 static IIO_DEVICE_ATTR(fault_ovuv
, 0444, show_fault_ovuv
, NULL
, 0);
253 static IIO_DEVICE_ATTR(fault_oc
, 0444, show_fault_oc
, NULL
, 0);
255 static struct attribute
*max31856_attributes
[] = {
256 &iio_dev_attr_fault_ovuv
.dev_attr
.attr
,
257 &iio_dev_attr_fault_oc
.dev_attr
.attr
,
261 static const struct attribute_group max31856_group
= {
262 .attrs
= max31856_attributes
,
265 static const struct iio_info max31856_info
= {
266 .read_raw
= max31856_read_raw
,
267 .attrs
= &max31856_group
,
270 static int max31856_probe(struct spi_device
*spi
)
272 const struct spi_device_id
*id
= spi_get_device_id(spi
);
273 struct iio_dev
*indio_dev
;
274 struct max31856_data
*data
;
277 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*data
));
281 data
= iio_priv(indio_dev
);
284 spi_set_drvdata(spi
, indio_dev
);
286 indio_dev
->info
= &max31856_info
;
287 indio_dev
->dev
.parent
= &spi
->dev
;
288 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
289 indio_dev
->name
= id
->name
;
290 indio_dev
->modes
= INDIO_DIRECT_MODE
;
291 indio_dev
->channels
= max31856_channels
;
292 indio_dev
->num_channels
= ARRAY_SIZE(max31856_channels
);
294 ret
= of_property_read_u32(spi
->dev
.of_node
, "thermocouple-type",
295 &data
->thermocouple_type
);
299 "Could not read thermocouple type DT property, configuring as a K-Type\n");
300 data
->thermocouple_type
= THERMOCOUPLE_TYPE_K
;
304 * no need to translate values as the supported types
305 * have the same value as the #defines
307 switch (data
->thermocouple_type
) {
308 case THERMOCOUPLE_TYPE_B
:
309 case THERMOCOUPLE_TYPE_E
:
310 case THERMOCOUPLE_TYPE_J
:
311 case THERMOCOUPLE_TYPE_K
:
312 case THERMOCOUPLE_TYPE_N
:
313 case THERMOCOUPLE_TYPE_R
:
314 case THERMOCOUPLE_TYPE_S
:
315 case THERMOCOUPLE_TYPE_T
:
319 "error: thermocouple-type %u not supported by max31856\n"
320 , data
->thermocouple_type
);
324 ret
= max31856_init(data
);
326 dev_err(&spi
->dev
, "error: Failed to configure max31856\n");
330 return devm_iio_device_register(&spi
->dev
, indio_dev
);
333 static const struct spi_device_id max31856_id
[] = {
337 MODULE_DEVICE_TABLE(spi
, max31856_id
);
339 static const struct of_device_id max31856_of_match
[] = {
340 { .compatible
= "maxim,max31856" },
343 MODULE_DEVICE_TABLE(of
, max31856_of_match
);
345 static struct spi_driver max31856_driver
= {
348 .of_match_table
= max31856_of_match
,
350 .probe
= max31856_probe
,
351 .id_table
= max31856_id
,
353 module_spi_driver(max31856_driver
);
355 MODULE_AUTHOR("Paresh Chaudhary <paresh.chaudhary@rockwellcollins.com>");
356 MODULE_AUTHOR("Patrick Havelange <patrick.havelange@essensium.com>");
357 MODULE_DESCRIPTION("Maxim MAX31856 thermocouple sensor driver");
358 MODULE_LICENSE("GPL");