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/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> 18 /* 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. 21 */ 22 #define MAX31856_RD_WR_BIT BIT(7) 23 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) 34 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 52 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) 61 }, 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) 70 }, 71 }; 72 73 struct max31856_data { 74 struct spi_device *spi; 75 u32 thermocouple_type; 76 bool filter_50hz; 77 int averaging; 78 }; 79 80 static const char max31856_tc_types[] = { 81 'B', 'E', 'J', 'K', 'N', 'R', 'S', 'T' 82 }; 83 84 static int max31856_read(struct max31856_data *data, u8 reg, 85 u8 val[], unsigned int read_size) 86 { 87 return spi_write_then_read(data->spi, ®, 1, val, read_size); 88 } 89 90 static int max31856_write(struct max31856_data *data, u8 reg, 91 unsigned int val) 92 { 93 u8 buf[2]; 94 95 buf[0] = reg | (MAX31856_RD_WR_BIT); 96 buf[1] = val; 97 98 return spi_write(data->spi, buf, 2); 99 } 100 101 static int max31856_init(struct max31856_data *data) 102 { 103 int ret; 104 u8 reg_cr0_val, reg_cr1_val; 105 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 109 */ 110 ret = max31856_read(data, MAX31856_CR0_REG, ®_cr0_val, 1); 111 if (ret) 112 return ret; 113 114 reg_cr0_val &= ~MAX31856_CR0_AUTOCONVERT; 115 ret = max31856_write(data, MAX31856_CR0_REG, reg_cr0_val); 116 if (ret) 117 return ret; 118 119 /* Set thermocouple type based on dts property */ 120 ret = max31856_read(data, MAX31856_CR1_REG, ®_cr1_val, 1); 121 if (ret) 122 return ret; 123 124 reg_cr1_val &= ~MAX31856_TC_TYPE_MASK; 125 reg_cr1_val |= data->thermocouple_type; 126 127 reg_cr1_val &= ~MAX31856_AVERAGING_MASK; 128 reg_cr1_val |= data->averaging << MAX31856_AVERAGING_SHIFT; 129 130 ret = max31856_write(data, MAX31856_CR1_REG, reg_cr1_val); 131 if (ret) 132 return ret; 133 134 /* 135 * Enable Open circuit fault detection 136 * Read datasheet for more information: Table 4. 137 * Value 01 means : Enabled (Once every 16 conversions) 138 */ 139 reg_cr0_val &= ~MAX31856_CR0_OCFAULT_MASK; 140 reg_cr0_val |= MAX31856_CR0_OCFAULT; 141 142 /* Set Auto Conversion Mode */ 143 reg_cr0_val &= ~MAX31856_CR0_1SHOT; 144 reg_cr0_val |= MAX31856_CR0_AUTOCONVERT; 145 146 if (data->filter_50hz) 147 reg_cr0_val |= MAX31856_CR0_FILTER_50HZ; 148 else 149 reg_cr0_val &= ~MAX31856_CR0_FILTER_50HZ; 150 151 return max31856_write(data, MAX31856_CR0_REG, reg_cr0_val); 152 } 153 154 static int max31856_thermocouple_read(struct max31856_data *data, 155 struct iio_chan_spec const *chan, 156 int *val) 157 { 158 int ret, offset_cjto; 159 u8 reg_val[3]; 160 161 switch (chan->channel2) { 162 case IIO_NO_MOD: 163 /* 164 * Multibyte Read 165 * MAX31856_LTCBH_REG, MAX31856_LTCBM_REG, MAX31856_LTCBL_REG 166 */ 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; 176 177 case IIO_MOD_TEMP_AMBIENT: 178 /* 179 * Multibyte Read 180 * MAX31856_CJTO_REG, MAX31856_CJTH_REG, MAX31856_CJTL_REG 181 */ 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; 195 196 default: 197 return -EINVAL; 198 } 199 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; 206 207 return ret; 208 } 209 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) 213 { 214 struct max31856_data *data = iio_priv(indio_dev); 215 int ret; 216 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; 236 } 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; 247 } 248 249 return ret; 250 } 251 252 static int max31856_write_raw_get_fmt(struct iio_dev *indio_dev, 253 struct iio_chan_spec const *chan, 254 long mask) 255 { 256 switch (mask) { 257 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE: 258 return IIO_VAL_CHAR; 259 default: 260 return IIO_VAL_INT; 261 } 262 } 263 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) 267 { 268 struct max31856_data *data = iio_priv(indio_dev); 269 int msb; 270 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++; 279 280 data->averaging = msb; 281 max31856_init(data); 282 break; 283 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE: 284 { 285 int tc_type = -1; 286 int i; 287 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; 292 } 293 } 294 if (tc_type < 0) 295 return -EINVAL; 296 297 data->thermocouple_type = tc_type; 298 max31856_init(data); 299 break; 300 } 301 default: 302 return -EINVAL; 303 } 304 305 return 0; 306 } 307 308 static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf) 309 { 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; 315 316 ret = max31856_read(data, MAX31856_SR_REG, ®_val, 1); 317 if (ret) 318 return ret; 319 320 fault = reg_val & faultbit; 321 322 return sprintf(buf, "%d\n", fault); 323 } 324 325 static ssize_t show_fault_ovuv(struct device *dev, 326 struct device_attribute *attr, 327 char *buf) 328 { 329 return show_fault(dev, MAX31856_FAULT_OVUV, buf); 330 } 331 332 static ssize_t show_fault_oc(struct device *dev, 333 struct device_attribute *attr, 334 char *buf) 335 { 336 return show_fault(dev, MAX31856_FAULT_OPEN, buf); 337 } 338 339 static ssize_t show_filter(struct device *dev, 340 struct device_attribute *attr, 341 char *buf) 342 { 343 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 344 struct max31856_data *data = iio_priv(indio_dev); 345 346 return sprintf(buf, "%d\n", data->filter_50hz ? 50 : 60); 347 } 348 349 static ssize_t set_filter(struct device *dev, 350 struct device_attribute *attr, 351 const char *buf, 352 size_t len) 353 { 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; 358 359 ret = kstrtouint(buf, 10, &freq); 360 if (ret) 361 return ret; 362 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; 372 } 373 374 max31856_init(data); 375 return len; 376 } 377 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); 382 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, 388 }; 389 390 static const struct attribute_group max31856_group = { 391 .attrs = max31856_attributes, 392 }; 393 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, 399 }; 400 401 static int max31856_probe(struct spi_device *spi) 402 { 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; 407 408 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); 409 if (!indio_dev) 410 return -ENOMEM; 411 412 data = iio_priv(indio_dev); 413 data->spi = spi; 414 data->filter_50hz = false; 415 416 spi_set_drvdata(spi, indio_dev); 417 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); 425 426 ret = of_property_read_u32(spi->dev.of_node, "thermocouple-type", 427 &data->thermocouple_type); 428 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; 433 } 434 435 /* 436 * no need to translate values as the supported types 437 * have the same value as the #defines 438 */ 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; 454 } 455 456 ret = max31856_init(data); 457 if (ret) { 458 dev_err(&spi->dev, "error: Failed to configure max31856\n"); 459 return ret; 460 } 461 462 return devm_iio_device_register(&spi->dev, indio_dev); 463 } 464 465 static const struct spi_device_id max31856_id[] = { 466 { "max31856", 0 }, 467 { } 468 }; 469 MODULE_DEVICE_TABLE(spi, max31856_id); 470 471 static const struct of_device_id max31856_of_match[] = { 472 { .compatible = "maxim,max31856" }, 473 { } 474 }; 475 MODULE_DEVICE_TABLE(of, max31856_of_match); 476 477 static struct spi_driver max31856_driver = { 478 .driver = { 479 .name = "max31856", 480 .of_match_table = max31856_of_match, 481 }, 482 .probe = max31856_probe, 483 .id_table = max31856_id, 484 }; 485 module_spi_driver(max31856_driver); 486 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"); 491