max31856.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  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. #include <linux/ctype.h>
  9. #include <linux/mod_devicetable.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 <asm/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. #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)
  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. static const struct iio_chan_spec max31856_channels[] = {
  53. { /* Thermocouple Temperature */
  54. .type = IIO_TEMP,
  55. .info_mask_separate =
  56. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
  57. BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE),
  58. .info_mask_shared_by_type =
  59. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)
  60. },
  61. { /* Cold Junction Temperature */
  62. .type = IIO_TEMP,
  63. .channel2 = IIO_MOD_TEMP_AMBIENT,
  64. .modified = 1,
  65. .info_mask_separate =
  66. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  67. .info_mask_shared_by_type =
  68. BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO)
  69. },
  70. };
  71. struct max31856_data {
  72. struct spi_device *spi;
  73. u32 thermocouple_type;
  74. bool filter_50hz;
  75. int averaging;
  76. };
  77. static const char max31856_tc_types[] = {
  78. 'B', 'E', 'J', 'K', 'N', 'R', 'S', 'T'
  79. };
  80. static int max31856_read(struct max31856_data *data, u8 reg,
  81. u8 val[], unsigned int read_size)
  82. {
  83. return spi_write_then_read(data->spi, &reg, 1, val, read_size);
  84. }
  85. static int max31856_write(struct max31856_data *data, u8 reg,
  86. unsigned int val)
  87. {
  88. u8 buf[2];
  89. buf[0] = reg | (MAX31856_RD_WR_BIT);
  90. buf[1] = val;
  91. return spi_write(data->spi, buf, 2);
  92. }
  93. static int max31856_init(struct max31856_data *data)
  94. {
  95. int ret;
  96. u8 reg_cr0_val, reg_cr1_val;
  97. /* Start by changing to Off mode before making changes as
  98. * some settings are recommended to be set only when the device
  99. * is off
  100. */
  101. ret = max31856_read(data, MAX31856_CR0_REG, &reg_cr0_val, 1);
  102. if (ret)
  103. return ret;
  104. reg_cr0_val &= ~MAX31856_CR0_AUTOCONVERT;
  105. ret = max31856_write(data, MAX31856_CR0_REG, reg_cr0_val);
  106. if (ret)
  107. return ret;
  108. /* Set thermocouple type based on dts property */
  109. ret = max31856_read(data, MAX31856_CR1_REG, &reg_cr1_val, 1);
  110. if (ret)
  111. return ret;
  112. reg_cr1_val &= ~MAX31856_TC_TYPE_MASK;
  113. reg_cr1_val |= data->thermocouple_type;
  114. reg_cr1_val &= ~MAX31856_AVERAGING_MASK;
  115. reg_cr1_val |= data->averaging << MAX31856_AVERAGING_SHIFT;
  116. ret = max31856_write(data, MAX31856_CR1_REG, reg_cr1_val);
  117. if (ret)
  118. return ret;
  119. /*
  120. * Enable Open circuit fault detection
  121. * Read datasheet for more information: Table 4.
  122. * Value 01 means : Enabled (Once every 16 conversions)
  123. */
  124. reg_cr0_val &= ~MAX31856_CR0_OCFAULT_MASK;
  125. reg_cr0_val |= MAX31856_CR0_OCFAULT;
  126. /* Set Auto Conversion Mode */
  127. reg_cr0_val &= ~MAX31856_CR0_1SHOT;
  128. reg_cr0_val |= MAX31856_CR0_AUTOCONVERT;
  129. if (data->filter_50hz)
  130. reg_cr0_val |= MAX31856_CR0_FILTER_50HZ;
  131. else
  132. reg_cr0_val &= ~MAX31856_CR0_FILTER_50HZ;
  133. return max31856_write(data, MAX31856_CR0_REG, reg_cr0_val);
  134. }
  135. static int max31856_thermocouple_read(struct max31856_data *data,
  136. struct iio_chan_spec const *chan,
  137. int *val)
  138. {
  139. int ret, offset_cjto;
  140. u8 reg_val[3];
  141. switch (chan->channel2) {
  142. case IIO_NO_MOD:
  143. /*
  144. * Multibyte Read
  145. * MAX31856_LTCBH_REG, MAX31856_LTCBM_REG, MAX31856_LTCBL_REG
  146. */
  147. ret = max31856_read(data, MAX31856_LTCBH_REG, reg_val, 3);
  148. if (ret)
  149. return ret;
  150. /* Skip last 5 dead bits of LTCBL */
  151. *val = get_unaligned_be24(&reg_val[0]) >> 5;
  152. /* Check 7th bit of LTCBH reg. value for sign*/
  153. if (reg_val[0] & 0x80)
  154. *val -= 0x80000;
  155. break;
  156. case IIO_MOD_TEMP_AMBIENT:
  157. /*
  158. * Multibyte Read
  159. * MAX31856_CJTO_REG, MAX31856_CJTH_REG, MAX31856_CJTL_REG
  160. */
  161. ret = max31856_read(data, MAX31856_CJTO_REG, reg_val, 3);
  162. if (ret)
  163. return ret;
  164. /* Get Cold Junction Temp. offset register value */
  165. offset_cjto = reg_val[0];
  166. /* Get CJTH and CJTL value and skip last 2 dead bits of CJTL */
  167. *val = get_unaligned_be16(&reg_val[1]) >> 2;
  168. /* As per datasheet add offset into CJTH and CJTL */
  169. *val += offset_cjto;
  170. /* Check 7th bit of CJTH reg. value for sign */
  171. if (reg_val[1] & 0x80)
  172. *val -= 0x4000;
  173. break;
  174. default:
  175. return -EINVAL;
  176. }
  177. ret = max31856_read(data, MAX31856_SR_REG, reg_val, 1);
  178. if (ret)
  179. return ret;
  180. /* Check for over/under voltage or open circuit fault */
  181. if (reg_val[0] & (MAX31856_FAULT_OVUV | MAX31856_FAULT_OPEN))
  182. return -EIO;
  183. return ret;
  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. switch (mask) {
  192. case IIO_CHAN_INFO_RAW:
  193. ret = max31856_thermocouple_read(data, chan, val);
  194. if (ret)
  195. return ret;
  196. return IIO_VAL_INT;
  197. case IIO_CHAN_INFO_SCALE:
  198. switch (chan->channel2) {
  199. case IIO_MOD_TEMP_AMBIENT:
  200. /* Cold junction Temp. Data resolution is 0.015625 */
  201. *val = 15;
  202. *val2 = 625000; /* 1000 * 0.015625 */
  203. ret = IIO_VAL_INT_PLUS_MICRO;
  204. break;
  205. default:
  206. /* Thermocouple Temp. Data resolution is 0.0078125 */
  207. *val = 7;
  208. *val2 = 812500; /* 1000 * 0.0078125) */
  209. return IIO_VAL_INT_PLUS_MICRO;
  210. }
  211. break;
  212. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  213. *val = 1 << data->averaging;
  214. return IIO_VAL_INT;
  215. case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
  216. *val = max31856_tc_types[data->thermocouple_type];
  217. return IIO_VAL_CHAR;
  218. default:
  219. ret = -EINVAL;
  220. break;
  221. }
  222. return ret;
  223. }
  224. static int max31856_write_raw_get_fmt(struct iio_dev *indio_dev,
  225. struct iio_chan_spec const *chan,
  226. long mask)
  227. {
  228. switch (mask) {
  229. case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
  230. return IIO_VAL_CHAR;
  231. default:
  232. return IIO_VAL_INT;
  233. }
  234. }
  235. static int max31856_write_raw(struct iio_dev *indio_dev,
  236. struct iio_chan_spec const *chan,
  237. int val, int val2, long mask)
  238. {
  239. struct max31856_data *data = iio_priv(indio_dev);
  240. int msb;
  241. switch (mask) {
  242. case IIO_CHAN_INFO_OVERSAMPLING_RATIO:
  243. if (val > 16 || val < 1)
  244. return -EINVAL;
  245. msb = fls(val) - 1;
  246. /* Round up to next 2pow if needed */
  247. if (BIT(msb) < val)
  248. msb++;
  249. data->averaging = msb;
  250. max31856_init(data);
  251. break;
  252. case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
  253. {
  254. int tc_type = -1;
  255. int i;
  256. for (i = 0; i < ARRAY_SIZE(max31856_tc_types); i++) {
  257. if (max31856_tc_types[i] == toupper(val)) {
  258. tc_type = i;
  259. break;
  260. }
  261. }
  262. if (tc_type < 0)
  263. return -EINVAL;
  264. data->thermocouple_type = tc_type;
  265. max31856_init(data);
  266. break;
  267. }
  268. default:
  269. return -EINVAL;
  270. }
  271. return 0;
  272. }
  273. static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf)
  274. {
  275. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  276. struct max31856_data *data = iio_priv(indio_dev);
  277. u8 reg_val;
  278. int ret;
  279. bool fault;
  280. ret = max31856_read(data, MAX31856_SR_REG, &reg_val, 1);
  281. if (ret)
  282. return ret;
  283. fault = reg_val & faultbit;
  284. return sysfs_emit(buf, "%d\n", fault);
  285. }
  286. static ssize_t show_fault_ovuv(struct device *dev,
  287. struct device_attribute *attr,
  288. char *buf)
  289. {
  290. return show_fault(dev, MAX31856_FAULT_OVUV, buf);
  291. }
  292. static ssize_t show_fault_oc(struct device *dev,
  293. struct device_attribute *attr,
  294. char *buf)
  295. {
  296. return show_fault(dev, MAX31856_FAULT_OPEN, buf);
  297. }
  298. static ssize_t show_filter(struct device *dev,
  299. struct device_attribute *attr,
  300. char *buf)
  301. {
  302. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  303. struct max31856_data *data = iio_priv(indio_dev);
  304. return sysfs_emit(buf, "%d\n", data->filter_50hz ? 50 : 60);
  305. }
  306. static ssize_t set_filter(struct device *dev,
  307. struct device_attribute *attr,
  308. const char *buf,
  309. size_t len)
  310. {
  311. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  312. struct max31856_data *data = iio_priv(indio_dev);
  313. unsigned int freq;
  314. int ret;
  315. ret = kstrtouint(buf, 10, &freq);
  316. if (ret)
  317. return ret;
  318. switch (freq) {
  319. case 50:
  320. data->filter_50hz = true;
  321. break;
  322. case 60:
  323. data->filter_50hz = false;
  324. break;
  325. default:
  326. return -EINVAL;
  327. }
  328. max31856_init(data);
  329. return len;
  330. }
  331. static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0);
  332. static IIO_DEVICE_ATTR(fault_oc, 0444, show_fault_oc, NULL, 0);
  333. static IIO_DEVICE_ATTR(in_temp_filter_notch_center_frequency, 0644,
  334. show_filter, set_filter, 0);
  335. static struct attribute *max31856_attributes[] = {
  336. &iio_dev_attr_fault_ovuv.dev_attr.attr,
  337. &iio_dev_attr_fault_oc.dev_attr.attr,
  338. &iio_dev_attr_in_temp_filter_notch_center_frequency.dev_attr.attr,
  339. NULL,
  340. };
  341. static const struct attribute_group max31856_group = {
  342. .attrs = max31856_attributes,
  343. };
  344. static const struct iio_info max31856_info = {
  345. .read_raw = max31856_read_raw,
  346. .write_raw = max31856_write_raw,
  347. .write_raw_get_fmt = max31856_write_raw_get_fmt,
  348. .attrs = &max31856_group,
  349. };
  350. static int max31856_probe(struct spi_device *spi)
  351. {
  352. const struct spi_device_id *id = spi_get_device_id(spi);
  353. struct iio_dev *indio_dev;
  354. struct max31856_data *data;
  355. int ret;
  356. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  357. if (!indio_dev)
  358. return -ENOMEM;
  359. data = iio_priv(indio_dev);
  360. data->spi = spi;
  361. data->filter_50hz = false;
  362. spi_set_drvdata(spi, indio_dev);
  363. indio_dev->info = &max31856_info;
  364. indio_dev->name = id->name;
  365. indio_dev->modes = INDIO_DIRECT_MODE;
  366. indio_dev->channels = max31856_channels;
  367. indio_dev->num_channels = ARRAY_SIZE(max31856_channels);
  368. ret = device_property_read_u32(&spi->dev, "thermocouple-type", &data->thermocouple_type);
  369. if (ret) {
  370. dev_info(&spi->dev,
  371. "Could not read thermocouple type DT property, configuring as a K-Type\n");
  372. data->thermocouple_type = THERMOCOUPLE_TYPE_K;
  373. }
  374. /*
  375. * no need to translate values as the supported types
  376. * have the same value as the #defines
  377. */
  378. switch (data->thermocouple_type) {
  379. case THERMOCOUPLE_TYPE_B:
  380. case THERMOCOUPLE_TYPE_E:
  381. case THERMOCOUPLE_TYPE_J:
  382. case THERMOCOUPLE_TYPE_K:
  383. case THERMOCOUPLE_TYPE_N:
  384. case THERMOCOUPLE_TYPE_R:
  385. case THERMOCOUPLE_TYPE_S:
  386. case THERMOCOUPLE_TYPE_T:
  387. break;
  388. default:
  389. dev_err(&spi->dev,
  390. "error: thermocouple-type %u not supported by max31856\n"
  391. , data->thermocouple_type);
  392. return -EINVAL;
  393. }
  394. ret = max31856_init(data);
  395. if (ret) {
  396. dev_err(&spi->dev, "error: Failed to configure max31856\n");
  397. return ret;
  398. }
  399. return devm_iio_device_register(&spi->dev, indio_dev);
  400. }
  401. static const struct spi_device_id max31856_id[] = {
  402. { "max31856", 0 },
  403. { }
  404. };
  405. MODULE_DEVICE_TABLE(spi, max31856_id);
  406. static const struct of_device_id max31856_of_match[] = {
  407. { .compatible = "maxim,max31856" },
  408. { }
  409. };
  410. MODULE_DEVICE_TABLE(of, max31856_of_match);
  411. static struct spi_driver max31856_driver = {
  412. .driver = {
  413. .name = "max31856",
  414. .of_match_table = max31856_of_match,
  415. },
  416. .probe = max31856_probe,
  417. .id_table = max31856_id,
  418. };
  419. module_spi_driver(max31856_driver);
  420. MODULE_AUTHOR("Paresh Chaudhary <[email protected]>");
  421. MODULE_AUTHOR("Patrick Havelange <[email protected]>");
  422. MODULE_DESCRIPTION("Maxim MAX31856 thermocouple sensor driver");
  423. MODULE_LICENSE("GPL");