max31865.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) Linumiz 2021
  4. *
  5. * max31865.c - Maxim MAX31865 RTD-to-Digital Converter sensor driver
  6. *
  7. * Author: Navin Sankar Velliangiri <[email protected]>
  8. */
  9. #include <linux/ctype.h>
  10. #include <linux/delay.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/mod_devicetable.h>
  14. #include <linux/module.h>
  15. #include <linux/iio/iio.h>
  16. #include <linux/iio/sysfs.h>
  17. #include <linux/property.h>
  18. #include <linux/spi/spi.h>
  19. #include <asm/unaligned.h>
  20. /*
  21. * The MSB of the register value determines whether the following byte will
  22. * be written or read. If it is 0, read will follow and if it is 1, write
  23. * will follow.
  24. */
  25. #define MAX31865_RD_WR_BIT BIT(7)
  26. #define MAX31865_CFG_VBIAS BIT(7)
  27. #define MAX31865_CFG_1SHOT BIT(5)
  28. #define MAX31865_3WIRE_RTD BIT(4)
  29. #define MAX31865_FAULT_STATUS_CLEAR BIT(1)
  30. #define MAX31865_FILTER_50HZ BIT(0)
  31. /* The MAX31865 registers */
  32. #define MAX31865_CFG_REG 0x00
  33. #define MAX31865_RTD_MSB 0x01
  34. #define MAX31865_FAULT_STATUS 0x07
  35. #define MAX31865_FAULT_OVUV BIT(2)
  36. static const char max31865_show_samp_freq[] = "50 60";
  37. static const struct iio_chan_spec max31865_channels[] = {
  38. { /* RTD Temperature */
  39. .type = IIO_TEMP,
  40. .info_mask_separate =
  41. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE)
  42. },
  43. };
  44. struct max31865_data {
  45. struct spi_device *spi;
  46. struct mutex lock;
  47. bool filter_50hz;
  48. bool three_wire;
  49. u8 buf[2] __aligned(IIO_DMA_MINALIGN);
  50. };
  51. static int max31865_read(struct max31865_data *data, u8 reg,
  52. unsigned int read_size)
  53. {
  54. return spi_write_then_read(data->spi, &reg, 1, data->buf, read_size);
  55. }
  56. static int max31865_write(struct max31865_data *data, size_t len)
  57. {
  58. return spi_write(data->spi, data->buf, len);
  59. }
  60. static int enable_bias(struct max31865_data *data)
  61. {
  62. u8 cfg;
  63. int ret;
  64. ret = max31865_read(data, MAX31865_CFG_REG, 1);
  65. if (ret)
  66. return ret;
  67. cfg = data->buf[0];
  68. data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT;
  69. data->buf[1] = cfg | MAX31865_CFG_VBIAS;
  70. return max31865_write(data, 2);
  71. }
  72. static int disable_bias(struct max31865_data *data)
  73. {
  74. u8 cfg;
  75. int ret;
  76. ret = max31865_read(data, MAX31865_CFG_REG, 1);
  77. if (ret)
  78. return ret;
  79. cfg = data->buf[0];
  80. cfg &= ~MAX31865_CFG_VBIAS;
  81. data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT;
  82. data->buf[1] = cfg;
  83. return max31865_write(data, 2);
  84. }
  85. static int max31865_rtd_read(struct max31865_data *data, int *val)
  86. {
  87. u8 reg;
  88. int ret;
  89. /* Enable BIAS to start the conversion */
  90. ret = enable_bias(data);
  91. if (ret)
  92. return ret;
  93. /* wait 10.5ms before initiating the conversion */
  94. msleep(11);
  95. ret = max31865_read(data, MAX31865_CFG_REG, 1);
  96. if (ret)
  97. return ret;
  98. reg = data->buf[0];
  99. reg |= MAX31865_CFG_1SHOT | MAX31865_FAULT_STATUS_CLEAR;
  100. data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT;
  101. data->buf[1] = reg;
  102. ret = max31865_write(data, 2);
  103. if (ret)
  104. return ret;
  105. if (data->filter_50hz) {
  106. /* 50Hz filter mode requires 62.5ms to complete */
  107. msleep(63);
  108. } else {
  109. /* 60Hz filter mode requires 52ms to complete */
  110. msleep(52);
  111. }
  112. ret = max31865_read(data, MAX31865_RTD_MSB, 2);
  113. if (ret)
  114. return ret;
  115. *val = get_unaligned_be16(&data->buf) >> 1;
  116. return disable_bias(data);
  117. }
  118. static int max31865_read_raw(struct iio_dev *indio_dev,
  119. struct iio_chan_spec const *chan,
  120. int *val, int *val2, long mask)
  121. {
  122. struct max31865_data *data = iio_priv(indio_dev);
  123. int ret;
  124. switch (mask) {
  125. case IIO_CHAN_INFO_RAW:
  126. mutex_lock(&data->lock);
  127. ret = max31865_rtd_read(data, val);
  128. mutex_unlock(&data->lock);
  129. if (ret)
  130. return ret;
  131. return IIO_VAL_INT;
  132. case IIO_CHAN_INFO_SCALE:
  133. /* Temp. Data resolution is 0.03125 degree centigrade */
  134. *val = 31;
  135. *val2 = 250000; /* 1000 * 0.03125 */
  136. return IIO_VAL_INT_PLUS_MICRO;
  137. default:
  138. return -EINVAL;
  139. }
  140. }
  141. static int max31865_init(struct max31865_data *data)
  142. {
  143. u8 cfg;
  144. int ret;
  145. ret = max31865_read(data, MAX31865_CFG_REG, 1);
  146. if (ret)
  147. return ret;
  148. cfg = data->buf[0];
  149. if (data->three_wire)
  150. /* 3-wire RTD connection */
  151. cfg |= MAX31865_3WIRE_RTD;
  152. if (data->filter_50hz)
  153. /* 50Hz noise rejection filter */
  154. cfg |= MAX31865_FILTER_50HZ;
  155. data->buf[0] = MAX31865_CFG_REG | MAX31865_RD_WR_BIT;
  156. data->buf[1] = cfg;
  157. return max31865_write(data, 2);
  158. }
  159. static ssize_t show_fault(struct device *dev, u8 faultbit, char *buf)
  160. {
  161. int ret;
  162. bool fault;
  163. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  164. struct max31865_data *data = iio_priv(indio_dev);
  165. ret = max31865_read(data, MAX31865_FAULT_STATUS, 1);
  166. if (ret)
  167. return ret;
  168. fault = data->buf[0] & faultbit;
  169. return sysfs_emit(buf, "%d\n", fault);
  170. }
  171. static ssize_t show_fault_ovuv(struct device *dev,
  172. struct device_attribute *attr,
  173. char *buf)
  174. {
  175. return show_fault(dev, MAX31865_FAULT_OVUV, buf);
  176. }
  177. static ssize_t show_filter(struct device *dev,
  178. struct device_attribute *attr,
  179. char *buf)
  180. {
  181. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  182. struct max31865_data *data = iio_priv(indio_dev);
  183. return sysfs_emit(buf, "%d\n", data->filter_50hz ? 50 : 60);
  184. }
  185. static ssize_t set_filter(struct device *dev,
  186. struct device_attribute *attr,
  187. const char *buf,
  188. size_t len)
  189. {
  190. struct iio_dev *indio_dev = dev_to_iio_dev(dev);
  191. struct max31865_data *data = iio_priv(indio_dev);
  192. unsigned int freq;
  193. int ret;
  194. ret = kstrtouint(buf, 10, &freq);
  195. if (ret)
  196. return ret;
  197. switch (freq) {
  198. case 50:
  199. data->filter_50hz = true;
  200. break;
  201. case 60:
  202. data->filter_50hz = false;
  203. break;
  204. default:
  205. return -EINVAL;
  206. }
  207. mutex_lock(&data->lock);
  208. ret = max31865_init(data);
  209. mutex_unlock(&data->lock);
  210. if (ret)
  211. return ret;
  212. return len;
  213. }
  214. static IIO_CONST_ATTR_SAMP_FREQ_AVAIL(max31865_show_samp_freq);
  215. static IIO_DEVICE_ATTR(fault_ovuv, 0444, show_fault_ovuv, NULL, 0);
  216. static IIO_DEVICE_ATTR(in_filter_notch_center_frequency, 0644,
  217. show_filter, set_filter, 0);
  218. static struct attribute *max31865_attributes[] = {
  219. &iio_dev_attr_fault_ovuv.dev_attr.attr,
  220. &iio_const_attr_sampling_frequency_available.dev_attr.attr,
  221. &iio_dev_attr_in_filter_notch_center_frequency.dev_attr.attr,
  222. NULL,
  223. };
  224. static const struct attribute_group max31865_group = {
  225. .attrs = max31865_attributes,
  226. };
  227. static const struct iio_info max31865_info = {
  228. .read_raw = max31865_read_raw,
  229. .attrs = &max31865_group,
  230. };
  231. static int max31865_probe(struct spi_device *spi)
  232. {
  233. const struct spi_device_id *id = spi_get_device_id(spi);
  234. struct iio_dev *indio_dev;
  235. struct max31865_data *data;
  236. int ret;
  237. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  238. if (!indio_dev)
  239. return -ENOMEM;
  240. data = iio_priv(indio_dev);
  241. data->spi = spi;
  242. data->filter_50hz = false;
  243. mutex_init(&data->lock);
  244. indio_dev->info = &max31865_info;
  245. indio_dev->name = id->name;
  246. indio_dev->modes = INDIO_DIRECT_MODE;
  247. indio_dev->channels = max31865_channels;
  248. indio_dev->num_channels = ARRAY_SIZE(max31865_channels);
  249. if (device_property_read_bool(&spi->dev, "maxim,3-wire")) {
  250. /* select 3 wire */
  251. data->three_wire = 1;
  252. } else {
  253. /* select 2 or 4 wire */
  254. data->three_wire = 0;
  255. }
  256. ret = max31865_init(data);
  257. if (ret) {
  258. dev_err(&spi->dev, "error: Failed to configure max31865\n");
  259. return ret;
  260. }
  261. return devm_iio_device_register(&spi->dev, indio_dev);
  262. }
  263. static const struct spi_device_id max31865_id[] = {
  264. { "max31865", 0 },
  265. { }
  266. };
  267. MODULE_DEVICE_TABLE(spi, max31865_id);
  268. static const struct of_device_id max31865_of_match[] = {
  269. { .compatible = "maxim,max31865" },
  270. { }
  271. };
  272. MODULE_DEVICE_TABLE(of, max31865_of_match);
  273. static struct spi_driver max31865_driver = {
  274. .driver = {
  275. .name = "max31865",
  276. .of_match_table = max31865_of_match,
  277. },
  278. .probe = max31865_probe,
  279. .id_table = max31865_id,
  280. };
  281. module_spi_driver(max31865_driver);
  282. MODULE_AUTHOR("Navin Sankar Velliangiri <[email protected]>");
  283. MODULE_DESCRIPTION("Maxim MAX31865 RTD-to-Digital Converter sensor driver");
  284. MODULE_LICENSE("GPL v2");