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/property.h> 14 #include <linux/spi/spi.h> 15 #include <linux/iio/iio.h> 16 #include <linux/iio/sysfs.h> 17 #include <linux/util_macros.h> 18 #include <linux/unaligned.h> 19 #include <dt-bindings/iio/temperature/thermocouple.h> 20 /* 21 * The MSB of the register value determines whether the following byte will 22 * be written or read. If it is 0, one or more byte reads will follow. 23 */ 24 #define MAX31856_RD_WR_BIT BIT(7) 25 26 #define MAX31856_CR0_AUTOCONVERT BIT(7) 27 #define MAX31856_CR0_1SHOT BIT(6) 28 #define MAX31856_CR0_OCFAULT BIT(4) 29 #define MAX31856_CR0_OCFAULT_MASK GENMASK(5, 4) 30 #define MAX31856_CR0_FILTER_50HZ BIT(0) 31 #define MAX31856_AVERAGING_MASK GENMASK(6, 4) 32 #define MAX31856_AVERAGING_SHIFT 4 33 #define MAX31856_TC_TYPE_MASK GENMASK(3, 0) 34 #define MAX31856_FAULT_OVUV BIT(1) 35 #define MAX31856_FAULT_OPEN BIT(0) 36 37 /* The MAX31856 registers */ 38 #define MAX31856_CR0_REG 0x00 39 #define MAX31856_CR1_REG 0x01 40 #define MAX31856_MASK_REG 0x02 41 #define MAX31856_CJHF_REG 0x03 42 #define MAX31856_CJLF_REG 0x04 43 #define MAX31856_LTHFTH_REG 0x05 44 #define MAX31856_LTHFTL_REG 0x06 45 #define MAX31856_LTLFTH_REG 0x07 46 #define MAX31856_LTLFTL_REG 0x08 47 #define MAX31856_CJTO_REG 0x09 48 #define MAX31856_CJTH_REG 0x0A 49 #define MAX31856_CJTL_REG 0x0B 50 #define MAX31856_LTCBH_REG 0x0C 51 #define MAX31856_LTCBM_REG 0x0D 52 #define MAX31856_LTCBL_REG 0x0E 53 #define MAX31856_SR_REG 0x0F 54 55 static const struct iio_chan_spec max31856_channels[] = { 56 { /* Thermocouple Temperature */ 57 .type = IIO_TEMP, 58 .info_mask_separate = 59 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) | 60 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE), 61 .info_mask_shared_by_type = 62 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) 63 }, 64 { /* Cold Junction Temperature */ 65 .type = IIO_TEMP, 66 .channel2 = IIO_MOD_TEMP_AMBIENT, 67 .modified = 1, 68 .info_mask_separate = 69 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE), 70 .info_mask_shared_by_type = 71 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO) 72 }, 73 }; 74 75 struct max31856_data { 76 struct spi_device *spi; 77 u32 thermocouple_type; 78 bool filter_50hz; 79 int averaging; 80 }; 81 82 static const char max31856_tc_types[] = { 83 'B', 'E', 'J', 'K', 'N', 'R', 'S', 'T' 84 }; 85 86 static int max31856_read(struct max31856_data *data, u8 reg, 87 u8 val[], unsigned int read_size) 88 { 89 return spi_write_then_read(data->spi, ®, 1, val, read_size); 90 } 91 92 static int max31856_write(struct max31856_data *data, u8 reg, 93 unsigned int val) 94 { 95 u8 buf[2]; 96 97 buf[0] = reg | (MAX31856_RD_WR_BIT); 98 buf[1] = val; 99 100 return spi_write(data->spi, buf, 2); 101 } 102 103 static int max31856_init(struct max31856_data *data) 104 { 105 int ret; 106 u8 reg_cr0_val, reg_cr1_val; 107 108 /* Start by changing to Off mode before making changes as 109 * some settings are recommended to be set only when the device 110 * is off 111 */ 112 ret = max31856_read(data, MAX31856_CR0_REG, ®_cr0_val, 1); 113 if (ret) 114 return ret; 115 116 reg_cr0_val &= ~MAX31856_CR0_AUTOCONVERT; 117 ret = max31856_write(data, MAX31856_CR0_REG, reg_cr0_val); 118 if (ret) 119 return ret; 120 121 /* Set thermocouple type based on dts property */ 122 ret = max31856_read(data, MAX31856_CR1_REG, ®_cr1_val, 1); 123 if (ret) 124 return ret; 125 126 reg_cr1_val &= ~MAX31856_TC_TYPE_MASK; 127 reg_cr1_val |= data->thermocouple_type; 128 129 reg_cr1_val &= ~MAX31856_AVERAGING_MASK; 130 reg_cr1_val |= data->averaging << MAX31856_AVERAGING_SHIFT; 131 132 ret = max31856_write(data, MAX31856_CR1_REG, reg_cr1_val); 133 if (ret) 134 return ret; 135 136 /* 137 * Enable Open circuit fault detection 138 * Read datasheet for more information: Table 4. 139 * Value 01 means : Enabled (Once every 16 conversions) 140 */ 141 reg_cr0_val &= ~MAX31856_CR0_OCFAULT_MASK; 142 reg_cr0_val |= MAX31856_CR0_OCFAULT; 143 144 /* Set Auto Conversion Mode */ 145 reg_cr0_val &= ~MAX31856_CR0_1SHOT; 146 reg_cr0_val |= MAX31856_CR0_AUTOCONVERT; 147 148 if (data->filter_50hz) 149 reg_cr0_val |= MAX31856_CR0_FILTER_50HZ; 150 else 151 reg_cr0_val &= ~MAX31856_CR0_FILTER_50HZ; 152 153 return max31856_write(data, MAX31856_CR0_REG, reg_cr0_val); 154 } 155 156 static int max31856_thermocouple_read(struct max31856_data *data, 157 struct iio_chan_spec const *chan, 158 int *val) 159 { 160 int ret, offset_cjto; 161 u8 reg_val[3]; 162 163 switch (chan->channel2) { 164 case IIO_NO_MOD: 165 /* 166 * Multibyte Read 167 * MAX31856_LTCBH_REG, MAX31856_LTCBM_REG, MAX31856_LTCBL_REG 168 */ 169 ret = max31856_read(data, MAX31856_LTCBH_REG, reg_val, 3); 170 if (ret) 171 return ret; 172 /* Skip last 5 dead bits of LTCBL */ 173 *val = get_unaligned_be24(®_val[0]) >> 5; 174 /* Check 7th bit of LTCBH reg. value for sign*/ 175 if (reg_val[0] & 0x80) 176 *val -= 0x80000; 177 break; 178 179 case IIO_MOD_TEMP_AMBIENT: 180 /* 181 * Multibyte Read 182 * MAX31856_CJTO_REG, MAX31856_CJTH_REG, MAX31856_CJTL_REG 183 */ 184 ret = max31856_read(data, MAX31856_CJTO_REG, reg_val, 3); 185 if (ret) 186 return ret; 187 /* Get Cold Junction Temp. offset register value */ 188 offset_cjto = reg_val[0]; 189 /* Get CJTH and CJTL value and skip last 2 dead bits of CJTL */ 190 *val = get_unaligned_be16(®_val[1]) >> 2; 191 /* As per datasheet add offset into CJTH and CJTL */ 192 *val += offset_cjto; 193 /* Check 7th bit of CJTH reg. value for sign */ 194 if (reg_val[1] & 0x80) 195 *val -= 0x4000; 196 break; 197 198 default: 199 return -EINVAL; 200 } 201 202 ret = max31856_read(data, MAX31856_SR_REG, reg_val, 1); 203 if (ret) 204 return ret; 205 /* Check for over/under voltage or open circuit fault */ 206 if (reg_val[0] & (MAX31856_FAULT_OVUV | MAX31856_FAULT_OPEN)) 207 return -EIO; 208 209 return ret; 210 } 211 212 static int max31856_read_raw(struct iio_dev *indio_dev, 213 struct iio_chan_spec const *chan, 214 int *val, int *val2, long mask) 215 { 216 struct max31856_data *data = iio_priv(indio_dev); 217 int ret; 218 219 switch (mask) { 220 case IIO_CHAN_INFO_RAW: 221 ret = max31856_thermocouple_read(data, chan, val); 222 if (ret) 223 return ret; 224 return IIO_VAL_INT; 225 case IIO_CHAN_INFO_SCALE: 226 switch (chan->channel2) { 227 case IIO_MOD_TEMP_AMBIENT: 228 /* Cold junction Temp. Data resolution is 0.015625 */ 229 *val = 15; 230 *val2 = 625000; /* 1000 * 0.015625 */ 231 ret = IIO_VAL_INT_PLUS_MICRO; 232 break; 233 default: 234 /* Thermocouple Temp. Data resolution is 0.0078125 */ 235 *val = 7; 236 *val2 = 812500; /* 1000 * 0.0078125) */ 237 return IIO_VAL_INT_PLUS_MICRO; 238 } 239 break; 240 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 241 *val = 1 << data->averaging; 242 return IIO_VAL_INT; 243 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE: 244 *val = max31856_tc_types[data->thermocouple_type]; 245 return IIO_VAL_CHAR; 246 default: 247 ret = -EINVAL; 248 break; 249 } 250 251 return ret; 252 } 253 254 static int max31856_write_raw_get_fmt(struct iio_dev *indio_dev, 255 struct iio_chan_spec const *chan, 256 long mask) 257 { 258 switch (mask) { 259 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE: 260 return IIO_VAL_CHAR; 261 default: 262 return IIO_VAL_INT; 263 } 264 } 265 266 static int max31856_write_raw(struct iio_dev *indio_dev, 267 struct iio_chan_spec const *chan, 268 int val, int val2, long mask) 269 { 270 struct max31856_data *data = iio_priv(indio_dev); 271 int msb; 272 273 switch (mask) { 274 case IIO_CHAN_INFO_OVERSAMPLING_RATIO: 275 if (val > 16 || val < 1) 276 return -EINVAL; 277 msb = fls(val) - 1; 278 /* Round up to next 2pow if needed */ 279 if (BIT(msb) < val) 280 msb++; 281 282 data->averaging = msb; 283 max31856_init(data); 284 break; 285 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE: 286 { 287 int tc_type = -1; 288 int i; 289 290 for (i = 0; i < ARRAY_SIZE(max31856_tc_types); i++) { 291 if (max31856_tc_types[i] == toupper(val)) { 292 tc_type = i; 293 break; 294 } 295 } 296 if (tc_type < 0) 297 return -EINVAL; 298 299 data->thermocouple_type = tc_type; 300 max31856_init(data); 301 break; 302 } 303 default: 304 return -EINVAL; 305 } 306 307 return 0; 308 } 309 310 static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf) 311 { 312 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 313 struct max31856_data *data = iio_priv(indio_dev); 314 u8 reg_val; 315 int ret; 316 bool fault; 317 318 ret = max31856_read(data, MAX31856_SR_REG, ®_val, 1); 319 if (ret) 320 return ret; 321 322 fault = reg_val & faultbit; 323 324 return sysfs_emit(buf, "%d\n", fault); 325 } 326 327 static ssize_t show_fault_ovuv(struct device *dev, 328 struct device_attribute *attr, 329 char *buf) 330 { 331 return show_fault(dev, MAX31856_FAULT_OVUV, buf); 332 } 333 334 static ssize_t show_fault_oc(struct device *dev, 335 struct device_attribute *attr, 336 char *buf) 337 { 338 return show_fault(dev, MAX31856_FAULT_OPEN, buf); 339 } 340 341 static ssize_t show_filter(struct device *dev, 342 struct device_attribute *attr, 343 char *buf) 344 { 345 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 346 struct max31856_data *data = iio_priv(indio_dev); 347 348 return sysfs_emit(buf, "%d\n", data->filter_50hz ? 50 : 60); 349 } 350 351 static ssize_t set_filter(struct device *dev, 352 struct device_attribute *attr, 353 const char *buf, 354 size_t len) 355 { 356 struct iio_dev *indio_dev = dev_to_iio_dev(dev); 357 struct max31856_data *data = iio_priv(indio_dev); 358 unsigned int freq; 359 int ret; 360 361 ret = kstrtouint(buf, 10, &freq); 362 if (ret) 363 return ret; 364 365 switch (freq) { 366 case 50: 367 data->filter_50hz = true; 368 break; 369 case 60: 370 data->filter_50hz = false; 371 break; 372 default: 373 return -EINVAL; 374 } 375 376 max31856_init(data); 377 return len; 378 } 379 380 static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0); 381 static IIO_DEVICE_ATTR(fault_oc, 0444, show_fault_oc, NULL, 0); 382 static IIO_DEVICE_ATTR(in_temp_filter_notch_center_frequency, 0644, 383 show_filter, set_filter, 0); 384 385 static struct attribute *max31856_attributes[] = { 386 &iio_dev_attr_fault_ovuv.dev_attr.attr, 387 &iio_dev_attr_fault_oc.dev_attr.attr, 388 &iio_dev_attr_in_temp_filter_notch_center_frequency.dev_attr.attr, 389 NULL, 390 }; 391 392 static const struct attribute_group max31856_group = { 393 .attrs = max31856_attributes, 394 }; 395 396 static const struct iio_info max31856_info = { 397 .read_raw = max31856_read_raw, 398 .write_raw = max31856_write_raw, 399 .write_raw_get_fmt = max31856_write_raw_get_fmt, 400 .attrs = &max31856_group, 401 }; 402 403 static int max31856_probe(struct spi_device *spi) 404 { 405 const struct spi_device_id *id = spi_get_device_id(spi); 406 struct iio_dev *indio_dev; 407 struct max31856_data *data; 408 int ret; 409 410 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data)); 411 if (!indio_dev) 412 return -ENOMEM; 413 414 data = iio_priv(indio_dev); 415 data->spi = spi; 416 data->filter_50hz = false; 417 418 spi_set_drvdata(spi, indio_dev); 419 420 indio_dev->info = &max31856_info; 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 = device_property_read_u32(&spi->dev, "thermocouple-type", &data->thermocouple_type); 427 if (ret) { 428 dev_info(&spi->dev, 429 "Could not read thermocouple type DT property, configuring as a K-Type\n"); 430 data->thermocouple_type = THERMOCOUPLE_TYPE_K; 431 } 432 433 /* 434 * no need to translate values as the supported types 435 * have the same value as the #defines 436 */ 437 switch (data->thermocouple_type) { 438 case THERMOCOUPLE_TYPE_B: 439 case THERMOCOUPLE_TYPE_E: 440 case THERMOCOUPLE_TYPE_J: 441 case THERMOCOUPLE_TYPE_K: 442 case THERMOCOUPLE_TYPE_N: 443 case THERMOCOUPLE_TYPE_R: 444 case THERMOCOUPLE_TYPE_S: 445 case THERMOCOUPLE_TYPE_T: 446 break; 447 default: 448 dev_err(&spi->dev, 449 "error: thermocouple-type %u not supported by max31856\n" 450 , data->thermocouple_type); 451 return -EINVAL; 452 } 453 454 ret = max31856_init(data); 455 if (ret) { 456 dev_err(&spi->dev, "error: Failed to configure max31856\n"); 457 return ret; 458 } 459 460 return devm_iio_device_register(&spi->dev, indio_dev); 461 } 462 463 static const struct spi_device_id max31856_id[] = { 464 { .name = "max31856" }, 465 { } 466 }; 467 MODULE_DEVICE_TABLE(spi, max31856_id); 468 469 static const struct of_device_id max31856_of_match[] = { 470 { .compatible = "maxim,max31856" }, 471 { } 472 }; 473 MODULE_DEVICE_TABLE(of, max31856_of_match); 474 475 static struct spi_driver max31856_driver = { 476 .driver = { 477 .name = "max31856", 478 .of_match_table = max31856_of_match, 479 }, 480 .probe = max31856_probe, 481 .id_table = max31856_id, 482 }; 483 module_spi_driver(max31856_driver); 484 485 MODULE_AUTHOR("Paresh Chaudhary <paresh.chaudhary@rockwellcollins.com>"); 486 MODULE_AUTHOR("Patrick Havelange <patrick.havelange@essensium.com>"); 487 MODULE_DESCRIPTION("Maxim MAX31856 thermocouple sensor driver"); 488 MODULE_LICENSE("GPL"); 489