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 <asm/unaligned.h>
18 #include <dt-bindings/iio/temperature/thermocouple.h>
20 * The MSB of the register value determines whether the following byte will
21 * be written or read. If it is 0, one or more byte reads will follow.
23 #define MAX31856_RD_WR_BIT BIT(7)
25 #define MAX31856_CR0_AUTOCONVERT BIT(7)
26 #define MAX31856_CR0_1SHOT BIT(6)
27 #define MAX31856_CR0_OCFAULT BIT(4)
28 #define MAX31856_CR0_OCFAULT_MASK GENMASK(5, 4)
29 #define MAX31856_CR0_FILTER_50HZ BIT(0)
30 #define MAX31856_AVERAGING_MASK GENMASK(6, 4)
31 #define MAX31856_AVERAGING_SHIFT 4
32 #define MAX31856_TC_TYPE_MASK GENMASK(3, 0)
33 #define MAX31856_FAULT_OVUV BIT(1)
34 #define MAX31856_FAULT_OPEN BIT(0)
36 /* The MAX31856 registers */
37 #define MAX31856_CR0_REG 0x00
38 #define MAX31856_CR1_REG 0x01
39 #define MAX31856_MASK_REG 0x02
40 #define MAX31856_CJHF_REG 0x03
41 #define MAX31856_CJLF_REG 0x04
42 #define MAX31856_LTHFTH_REG 0x05
43 #define MAX31856_LTHFTL_REG 0x06
44 #define MAX31856_LTLFTH_REG 0x07
45 #define MAX31856_LTLFTL_REG 0x08
46 #define MAX31856_CJTO_REG 0x09
47 #define MAX31856_CJTH_REG 0x0A
48 #define MAX31856_CJTL_REG 0x0B
49 #define MAX31856_LTCBH_REG 0x0C
50 #define MAX31856_LTCBM_REG 0x0D
51 #define MAX31856_LTCBL_REG 0x0E
52 #define MAX31856_SR_REG 0x0F
54 static const struct iio_chan_spec max31856_channels
[] = {
55 { /* Thermocouple Temperature */
58 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
) |
59 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE
),
60 .info_mask_shared_by_type
=
61 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
)
63 { /* Cold Junction Temperature */
65 .channel2
= IIO_MOD_TEMP_AMBIENT
,
68 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
69 .info_mask_shared_by_type
=
70 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO
)
74 struct max31856_data
{
75 struct spi_device
*spi
;
76 u32 thermocouple_type
;
81 static const char max31856_tc_types
[] = {
82 'B', 'E', 'J', 'K', 'N', 'R', 'S', 'T'
85 static int max31856_read(struct max31856_data
*data
, u8 reg
,
86 u8 val
[], unsigned int read_size
)
88 return spi_write_then_read(data
->spi
, ®
, 1, val
, read_size
);
91 static int max31856_write(struct max31856_data
*data
, u8 reg
,
96 buf
[0] = reg
| (MAX31856_RD_WR_BIT
);
99 return spi_write(data
->spi
, buf
, 2);
102 static int max31856_init(struct max31856_data
*data
)
105 u8 reg_cr0_val
, reg_cr1_val
;
107 /* Start by changing to Off mode before making changes as
108 * some settings are recommended to be set only when the device
111 ret
= max31856_read(data
, MAX31856_CR0_REG
, ®_cr0_val
, 1);
115 reg_cr0_val
&= ~MAX31856_CR0_AUTOCONVERT
;
116 ret
= max31856_write(data
, MAX31856_CR0_REG
, reg_cr0_val
);
120 /* Set thermocouple type based on dts property */
121 ret
= max31856_read(data
, MAX31856_CR1_REG
, ®_cr1_val
, 1);
125 reg_cr1_val
&= ~MAX31856_TC_TYPE_MASK
;
126 reg_cr1_val
|= data
->thermocouple_type
;
128 reg_cr1_val
&= ~MAX31856_AVERAGING_MASK
;
129 reg_cr1_val
|= data
->averaging
<< MAX31856_AVERAGING_SHIFT
;
131 ret
= max31856_write(data
, MAX31856_CR1_REG
, reg_cr1_val
);
136 * Enable Open circuit fault detection
137 * Read datasheet for more information: Table 4.
138 * Value 01 means : Enabled (Once every 16 conversions)
140 reg_cr0_val
&= ~MAX31856_CR0_OCFAULT_MASK
;
141 reg_cr0_val
|= MAX31856_CR0_OCFAULT
;
143 /* Set Auto Conversion Mode */
144 reg_cr0_val
&= ~MAX31856_CR0_1SHOT
;
145 reg_cr0_val
|= MAX31856_CR0_AUTOCONVERT
;
147 if (data
->filter_50hz
)
148 reg_cr0_val
|= MAX31856_CR0_FILTER_50HZ
;
150 reg_cr0_val
&= ~MAX31856_CR0_FILTER_50HZ
;
152 return max31856_write(data
, MAX31856_CR0_REG
, reg_cr0_val
);
155 static int max31856_thermocouple_read(struct max31856_data
*data
,
156 struct iio_chan_spec
const *chan
,
159 int ret
, offset_cjto
;
162 switch (chan
->channel2
) {
166 * MAX31856_LTCBH_REG, MAX31856_LTCBM_REG, MAX31856_LTCBL_REG
168 ret
= max31856_read(data
, MAX31856_LTCBH_REG
, reg_val
, 3);
171 /* Skip last 5 dead bits of LTCBL */
172 *val
= get_unaligned_be24(®_val
[0]) >> 5;
173 /* Check 7th bit of LTCBH reg. value for sign*/
174 if (reg_val
[0] & 0x80)
178 case IIO_MOD_TEMP_AMBIENT
:
181 * MAX31856_CJTO_REG, MAX31856_CJTH_REG, MAX31856_CJTL_REG
183 ret
= max31856_read(data
, MAX31856_CJTO_REG
, reg_val
, 3);
186 /* Get Cold Junction Temp. offset register value */
187 offset_cjto
= reg_val
[0];
188 /* Get CJTH and CJTL value and skip last 2 dead bits of CJTL */
189 *val
= get_unaligned_be16(®_val
[1]) >> 2;
190 /* As per datasheet add offset into CJTH and CJTL */
192 /* Check 7th bit of CJTH reg. value for sign */
193 if (reg_val
[1] & 0x80)
201 ret
= max31856_read(data
, MAX31856_SR_REG
, reg_val
, 1);
204 /* Check for over/under voltage or open circuit fault */
205 if (reg_val
[0] & (MAX31856_FAULT_OVUV
| MAX31856_FAULT_OPEN
))
211 static int max31856_read_raw(struct iio_dev
*indio_dev
,
212 struct iio_chan_spec
const *chan
,
213 int *val
, int *val2
, long mask
)
215 struct max31856_data
*data
= iio_priv(indio_dev
);
219 case IIO_CHAN_INFO_RAW
:
220 ret
= max31856_thermocouple_read(data
, chan
, val
);
224 case IIO_CHAN_INFO_SCALE
:
225 switch (chan
->channel2
) {
226 case IIO_MOD_TEMP_AMBIENT
:
227 /* Cold junction Temp. Data resolution is 0.015625 */
229 *val2
= 625000; /* 1000 * 0.015625 */
230 ret
= IIO_VAL_INT_PLUS_MICRO
;
233 /* Thermocouple Temp. Data resolution is 0.0078125 */
235 *val2
= 812500; /* 1000 * 0.0078125) */
236 return IIO_VAL_INT_PLUS_MICRO
;
239 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
240 *val
= 1 << data
->averaging
;
242 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE
:
243 *val
= max31856_tc_types
[data
->thermocouple_type
];
253 static int max31856_write_raw_get_fmt(struct iio_dev
*indio_dev
,
254 struct iio_chan_spec
const *chan
,
258 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE
:
265 static int max31856_write_raw(struct iio_dev
*indio_dev
,
266 struct iio_chan_spec
const *chan
,
267 int val
, int val2
, long mask
)
269 struct max31856_data
*data
= iio_priv(indio_dev
);
273 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
274 if (val
> 16 || val
< 1)
277 /* Round up to next 2pow if needed */
281 data
->averaging
= msb
;
284 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE
:
289 for (i
= 0; i
< ARRAY_SIZE(max31856_tc_types
); i
++) {
290 if (max31856_tc_types
[i
] == toupper(val
)) {
298 data
->thermocouple_type
= tc_type
;
309 static ssize_t
show_fault(struct device
*dev
, u8 faultbit
, char *buf
)
311 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
312 struct max31856_data
*data
= iio_priv(indio_dev
);
317 ret
= max31856_read(data
, MAX31856_SR_REG
, ®_val
, 1);
321 fault
= reg_val
& faultbit
;
323 return sprintf(buf
, "%d\n", fault
);
326 static ssize_t
show_fault_ovuv(struct device
*dev
,
327 struct device_attribute
*attr
,
330 return show_fault(dev
, MAX31856_FAULT_OVUV
, buf
);
333 static ssize_t
show_fault_oc(struct device
*dev
,
334 struct device_attribute
*attr
,
337 return show_fault(dev
, MAX31856_FAULT_OPEN
, buf
);
340 static ssize_t
show_filter(struct device
*dev
,
341 struct device_attribute
*attr
,
344 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
345 struct max31856_data
*data
= iio_priv(indio_dev
);
347 return sprintf(buf
, "%d\n", data
->filter_50hz
? 50 : 60);
350 static ssize_t
set_filter(struct device
*dev
,
351 struct device_attribute
*attr
,
355 struct iio_dev
*indio_dev
= dev_to_iio_dev(dev
);
356 struct max31856_data
*data
= iio_priv(indio_dev
);
360 ret
= kstrtouint(buf
, 10, &freq
);
366 data
->filter_50hz
= true;
369 data
->filter_50hz
= false;
379 static IIO_DEVICE_ATTR(fault_ovuv
, 0444, show_fault_ovuv
, NULL
, 0);
380 static IIO_DEVICE_ATTR(fault_oc
, 0444, show_fault_oc
, NULL
, 0);
381 static IIO_DEVICE_ATTR(in_temp_filter_notch_center_frequency
, 0644,
382 show_filter
, set_filter
, 0);
384 static struct attribute
*max31856_attributes
[] = {
385 &iio_dev_attr_fault_ovuv
.dev_attr
.attr
,
386 &iio_dev_attr_fault_oc
.dev_attr
.attr
,
387 &iio_dev_attr_in_temp_filter_notch_center_frequency
.dev_attr
.attr
,
391 static const struct attribute_group max31856_group
= {
392 .attrs
= max31856_attributes
,
395 static const struct iio_info max31856_info
= {
396 .read_raw
= max31856_read_raw
,
397 .write_raw
= max31856_write_raw
,
398 .write_raw_get_fmt
= max31856_write_raw_get_fmt
,
399 .attrs
= &max31856_group
,
402 static int max31856_probe(struct spi_device
*spi
)
404 const struct spi_device_id
*id
= spi_get_device_id(spi
);
405 struct iio_dev
*indio_dev
;
406 struct max31856_data
*data
;
409 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*data
));
413 data
= iio_priv(indio_dev
);
415 data
->filter_50hz
= false;
417 spi_set_drvdata(spi
, indio_dev
);
419 indio_dev
->info
= &max31856_info
;
420 indio_dev
->name
= id
->name
;
421 indio_dev
->modes
= INDIO_DIRECT_MODE
;
422 indio_dev
->channels
= max31856_channels
;
423 indio_dev
->num_channels
= ARRAY_SIZE(max31856_channels
);
425 ret
= of_property_read_u32(spi
->dev
.of_node
, "thermocouple-type",
426 &data
->thermocouple_type
);
430 "Could not read thermocouple type DT property, configuring as a K-Type\n");
431 data
->thermocouple_type
= THERMOCOUPLE_TYPE_K
;
435 * no need to translate values as the supported types
436 * have the same value as the #defines
438 switch (data
->thermocouple_type
) {
439 case THERMOCOUPLE_TYPE_B
:
440 case THERMOCOUPLE_TYPE_E
:
441 case THERMOCOUPLE_TYPE_J
:
442 case THERMOCOUPLE_TYPE_K
:
443 case THERMOCOUPLE_TYPE_N
:
444 case THERMOCOUPLE_TYPE_R
:
445 case THERMOCOUPLE_TYPE_S
:
446 case THERMOCOUPLE_TYPE_T
:
450 "error: thermocouple-type %u not supported by max31856\n"
451 , data
->thermocouple_type
);
455 ret
= max31856_init(data
);
457 dev_err(&spi
->dev
, "error: Failed to configure max31856\n");
461 return devm_iio_device_register(&spi
->dev
, indio_dev
);
464 static const struct spi_device_id max31856_id
[] = {
468 MODULE_DEVICE_TABLE(spi
, max31856_id
);
470 static const struct of_device_id max31856_of_match
[] = {
471 { .compatible
= "maxim,max31856" },
474 MODULE_DEVICE_TABLE(of
, max31856_of_match
);
476 static struct spi_driver max31856_driver
= {
479 .of_match_table
= max31856_of_match
,
481 .probe
= max31856_probe
,
482 .id_table
= max31856_id
,
484 module_spi_driver(max31856_driver
);
486 MODULE_AUTHOR("Paresh Chaudhary <paresh.chaudhary@rockwellcollins.com>");
487 MODULE_AUTHOR("Patrick Havelange <patrick.havelange@essensium.com>");
488 MODULE_DESCRIPTION("Maxim MAX31856 thermocouple sensor driver");
489 MODULE_LICENSE("GPL");