1 // SPDX-License-Identifier: GPL-2.0 2 /* max31856.c 3 * 4 * Maxim MAX31856 thermocouple sensor driver 5 * 6 * Copyright (C) 2018-2019 Rockwell Collins 7 */ 8 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> 16 /* 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. 19 */ 20 #define MAX31856_RD_WR_BIT BIT(7) 21 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) 29 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 47 48 static const struct iio_chan_spec max31856_channels[] = { 49 { /* Thermocouple Temperature */ 50 .type = IIO_TEMP, 51 .info_mask_separate = 52 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 53 }, 54 { /* Cold Junction Temperature */ 55 .type = IIO_TEMP, 56 .channel2 = IIO_MOD_TEMP_AMBIENT, 57 .modified = 1, 58 .info_mask_separate = 59 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 60 }, 61 }; 62 63 struct max31856_data { 64 struct spi_device *spi; 65 u32 thermocouple_type; 66 }; 67 68 static int max31856_read(struct max31856_data *data, u8 reg, 69 u8 val[], unsigned int read_size) 70 { 71 return spi_write_then_read(data->spi, ®, 1, val, read_size); 72 } 73 74 static int max31856_write(struct max31856_data *data, u8 reg, 75 unsigned int val) 76 { 77 u8 buf[2]; 78 79 buf[0] = reg | (MAX31856_RD_WR_BIT); 80 buf[1] = val; 81 82 return spi_write(data->spi, buf, 2); 83 } 84 85 static int max31856_init(struct max31856_data *data) 86 { 87 int ret; 88 u8 reg_cr0_val, reg_cr1_val; 89 90 /* Start by changing to Off mode before making changes as 91 * some settings are recommended to be set only when the device 92 * is off 93 */ 94 ret = max31856_read(data, MAX31856_CR0_REG, ®_cr0_val, 1); 95 if (ret) 96 return ret; 97 98 reg_cr0_val &= ~MAX31856_CR0_AUTOCONVERT; 99 ret = max31856_write(data, MAX31856_CR0_REG, reg_cr0_val); 100 if (ret) 101 return ret; 102 103 /* Set thermocouple type based on dts property */ 104 ret = max31856_read(data, MAX31856_CR1_REG, ®_cr1_val, 1); 105 if (ret) 106 return ret; 107 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); 111 if (ret) 112 return ret; 113 114 /* 115 * Enable Open circuit fault detection 116 * Read datasheet for more information: Table 4. 117 * Value 01 means : Enabled (Once every 16 conversions) 118 */ 119 reg_cr0_val &= ~MAX31856_CR0_OCFAULT_MASK; 120 reg_cr0_val |= MAX31856_CR0_OCFAULT; 121 122 /* Set Auto Conversion Mode */ 123 reg_cr0_val &= ~MAX31856_CR0_1SHOT; 124 reg_cr0_val |= MAX31856_CR0_AUTOCONVERT; 125 126 return max31856_write(data, MAX31856_CR0_REG, reg_cr0_val); 127 } 128 129 static int max31856_thermocouple_read(struct max31856_data *data, 130 struct iio_chan_spec const *chan, 131 int *val) 132 { 133 int ret, offset_cjto; 134 u8 reg_val[3]; 135 136 switch (chan->channel2) { 137 case IIO_NO_MOD: 138 /* 139 * Multibyte Read 140 * MAX31856_LTCBH_REG, MAX31856_LTCBM_REG, MAX31856_LTCBL_REG 141 */ 142 ret = max31856_read(data, MAX31856_LTCBH_REG, reg_val, 3); 143 if (ret) 144 return ret; 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) 149 *val -= 0x80000; 150 break; 151 152 case IIO_MOD_TEMP_AMBIENT: 153 /* 154 * Multibyte Read 155 * MAX31856_CJTO_REG, MAX31856_CJTH_REG, MAX31856_CJTL_REG 156 */ 157 ret = max31856_read(data, MAX31856_CJTO_REG, reg_val, 3); 158 if (ret) 159 return ret; 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 */ 165 *val += offset_cjto; 166 /* Check 7th bit of CJTH reg. value for sign */ 167 if (reg_val[1] & 0x80) 168 *val -= 0x4000; 169 break; 170 171 default: 172 return -EINVAL; 173 } 174 175 ret = max31856_read(data, MAX31856_SR_REG, reg_val, 1); 176 if (ret) 177 return ret; 178 /* Check for over/under voltage or open circuit fault */ 179 if (reg_val[0] & (MAX31856_FAULT_OVUV | MAX31856_FAULT_OPEN)) 180 return -EIO; 181 182 return ret; 183 } 184 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) 188 { 189 struct max31856_data *data = iio_priv(indio_dev); 190 int ret; 191 192 switch (mask) { 193 case IIO_CHAN_INFO_RAW: 194 ret = max31856_thermocouple_read(data, chan, val); 195 if (ret) 196 return ret; 197 return IIO_VAL_INT; 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 */ 202 *val = 15; 203 *val2 = 625000; /* 1000 * 0.015625 */ 204 ret = IIO_VAL_INT_PLUS_MICRO; 205 break; 206 default: 207 /* Thermocouple Temp. Data resolution is 0.0078125 */ 208 *val = 7; 209 *val2 = 812500; /* 1000 * 0.0078125) */ 210 return IIO_VAL_INT_PLUS_MICRO; 211 } 212 break; 213 default: 214 ret = -EINVAL; 215 break; 216 } 217 218 return ret; 219 } 220 221 static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf) 222 { 223 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 224 struct max31856_data *data = iio_priv(indio_dev); 225 u8 reg_val; 226 int ret; 227 bool fault; 228 229 ret = max31856_read(data, MAX31856_SR_REG, ®_val, 1); 230 if (ret) 231 return ret; 232 233 fault = reg_val & faultbit; 234 235 return sprintf(buf, "%d\n", fault); 236 } 237 238 static ssize_t show_fault_ovuv(struct device *dev, 239 struct device_attribute *attr, 240 char *buf) 241 { 242 return show_fault(dev, MAX31856_FAULT_OVUV, buf); 243 } 244 245 static ssize_t show_fault_oc(struct device *dev, 246 struct device_attribute *attr, 247 char *buf) 248 { 249 return show_fault(dev, MAX31856_FAULT_OPEN, buf); 250 } 251 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); 254 255 static struct attribute *max31856_attributes[] = { 256 &iio_dev_attr_fault_ovuv.dev_attr.attr, 257 &iio_dev_attr_fault_oc.dev_attr.attr, 258 NULL, 259 }; 260 261 static const struct attribute_group max31856_group = { 262 .attrs = max31856_attributes, 263 }; 264 265 static const struct iio_info max31856_info = { 266 .read_raw = max31856_read_raw, 267 .attrs = &max31856_group, 268 }; 269 270 static int max31856_probe(struct spi_device *spi) 271 { 272 const struct spi_device_id *id = spi_get_device_id(spi); 273 struct iio_dev *indio_dev; 274 struct max31856_data *data; 275 int ret; 276 277 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); 278 if (!indio_dev) 279 return -ENOMEM; 280 281 data = iio_priv(indio_dev); 282 data->spi = spi; 283 284 spi_set_drvdata(spi, indio_dev); 285 286 indio_dev->info = &max31856_info; 287 indio_dev->name = id->name; 288 indio_dev->modes = INDIO_DIRECT_MODE; 289 indio_dev->channels = max31856_channels; 290 indio_dev->num_channels = ARRAY_SIZE(max31856_channels); 291 292 ret = of_property_read_u32(spi->dev.of_node, "thermocouple-type", 293 &data->thermocouple_type); 294 295 if (ret) { 296 dev_info(&spi->dev, 297 "Could not read thermocouple type DT property, configuring as a K-Type\n"); 298 data->thermocouple_type = THERMOCOUPLE_TYPE_K; 299 } 300 301 /* 302 * no need to translate values as the supported types 303 * have the same value as the #defines 304 */ 305 switch (data->thermocouple_type) { 306 case THERMOCOUPLE_TYPE_B: 307 case THERMOCOUPLE_TYPE_E: 308 case THERMOCOUPLE_TYPE_J: 309 case THERMOCOUPLE_TYPE_K: 310 case THERMOCOUPLE_TYPE_N: 311 case THERMOCOUPLE_TYPE_R: 312 case THERMOCOUPLE_TYPE_S: 313 case THERMOCOUPLE_TYPE_T: 314 break; 315 default: 316 dev_err(&spi->dev, 317 "error: thermocouple-type %u not supported by max31856\n" 318 , data->thermocouple_type); 319 return -EINVAL; 320 } 321 322 ret = max31856_init(data); 323 if (ret) { 324 dev_err(&spi->dev, "error: Failed to configure max31856\n"); 325 return ret; 326 } 327 328 return devm_iio_device_register(&spi->dev, indio_dev); 329 } 330 331 static const struct spi_device_id max31856_id[] = { 332 { "max31856", 0 }, 333 { } 334 }; 335 MODULE_DEVICE_TABLE(spi, max31856_id); 336 337 static const struct of_device_id max31856_of_match[] = { 338 { .compatible = "maxim,max31856" }, 339 { } 340 }; 341 MODULE_DEVICE_TABLE(of, max31856_of_match); 342 343 static struct spi_driver max31856_driver = { 344 .driver = { 345 .name = "max31856", 346 .of_match_table = max31856_of_match, 347 }, 348 .probe = max31856_probe, 349 .id_table = max31856_id, 350 }; 351 module_spi_driver(max31856_driver); 352 353 MODULE_AUTHOR("Paresh Chaudhary <paresh.chaudhary@rockwellcollins.com>"); 354 MODULE_AUTHOR("Patrick Havelange <patrick.havelange@essensium.com>"); 355 MODULE_DESCRIPTION("Maxim MAX31856 thermocouple sensor driver"); 356 MODULE_LICENSE("GPL"); 357