maxim_thermocouple.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * maxim_thermocouple.c - Support for Maxim thermocouple chips
  4. *
  5. * Copyright (C) 2016-2018 Matt Ranostay
  6. * Author: <[email protected]>
  7. */
  8. #include <linux/init.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/module.h>
  11. #include <linux/mutex.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/iio/trigger.h>
  17. #include <linux/iio/buffer.h>
  18. #include <linux/iio/triggered_buffer.h>
  19. #include <linux/iio/trigger_consumer.h>
  20. #define MAXIM_THERMOCOUPLE_DRV_NAME "maxim_thermocouple"
  21. enum {
  22. MAX6675,
  23. MAX31855,
  24. MAX31855K,
  25. MAX31855J,
  26. MAX31855N,
  27. MAX31855S,
  28. MAX31855T,
  29. MAX31855E,
  30. MAX31855R,
  31. };
  32. static const char maxim_tc_types[] = {
  33. 'K', '?', 'K', 'J', 'N', 'S', 'T', 'E', 'R'
  34. };
  35. static const struct iio_chan_spec max6675_channels[] = {
  36. { /* thermocouple temperature */
  37. .type = IIO_TEMP,
  38. .info_mask_separate =
  39. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
  40. BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE),
  41. .scan_index = 0,
  42. .scan_type = {
  43. .sign = 's',
  44. .realbits = 13,
  45. .storagebits = 16,
  46. .shift = 3,
  47. .endianness = IIO_BE,
  48. },
  49. },
  50. IIO_CHAN_SOFT_TIMESTAMP(1),
  51. };
  52. static const struct iio_chan_spec max31855_channels[] = {
  53. { /* thermocouple temperature */
  54. .type = IIO_TEMP,
  55. .address = 2,
  56. .info_mask_separate =
  57. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
  58. BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE),
  59. .scan_index = 0,
  60. .scan_type = {
  61. .sign = 's',
  62. .realbits = 14,
  63. .storagebits = 16,
  64. .shift = 2,
  65. .endianness = IIO_BE,
  66. },
  67. },
  68. { /* cold junction temperature */
  69. .type = IIO_TEMP,
  70. .address = 0,
  71. .channel2 = IIO_MOD_TEMP_AMBIENT,
  72. .modified = 1,
  73. .info_mask_separate =
  74. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  75. .scan_index = 1,
  76. .scan_type = {
  77. .sign = 's',
  78. .realbits = 12,
  79. .storagebits = 16,
  80. .shift = 4,
  81. .endianness = IIO_BE,
  82. },
  83. },
  84. IIO_CHAN_SOFT_TIMESTAMP(2),
  85. };
  86. static const unsigned long max31855_scan_masks[] = {0x3, 0};
  87. struct maxim_thermocouple_chip {
  88. const struct iio_chan_spec *channels;
  89. const unsigned long *scan_masks;
  90. u8 num_channels;
  91. u8 read_size;
  92. /* bit-check for valid input */
  93. u32 status_bit;
  94. };
  95. static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
  96. [MAX6675] = {
  97. .channels = max6675_channels,
  98. .num_channels = ARRAY_SIZE(max6675_channels),
  99. .read_size = 2,
  100. .status_bit = BIT(2),
  101. },
  102. [MAX31855] = {
  103. .channels = max31855_channels,
  104. .num_channels = ARRAY_SIZE(max31855_channels),
  105. .read_size = 4,
  106. .scan_masks = max31855_scan_masks,
  107. .status_bit = BIT(16),
  108. },
  109. };
  110. struct maxim_thermocouple_data {
  111. struct spi_device *spi;
  112. const struct maxim_thermocouple_chip *chip;
  113. u8 buffer[16] __aligned(IIO_DMA_MINALIGN);
  114. char tc_type;
  115. };
  116. static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
  117. struct iio_chan_spec const *chan, int *val)
  118. {
  119. unsigned int storage_bytes = data->chip->read_size;
  120. unsigned int shift = chan->scan_type.shift + (chan->address * 8);
  121. __be16 buf16;
  122. __be32 buf32;
  123. int ret;
  124. switch (storage_bytes) {
  125. case 2:
  126. ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
  127. *val = be16_to_cpu(buf16);
  128. break;
  129. case 4:
  130. ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
  131. *val = be32_to_cpu(buf32);
  132. break;
  133. default:
  134. ret = -EINVAL;
  135. }
  136. if (ret)
  137. return ret;
  138. /* check to be sure this is a valid reading */
  139. if (*val & data->chip->status_bit)
  140. return -EINVAL;
  141. *val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
  142. return 0;
  143. }
  144. static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
  145. {
  146. struct iio_poll_func *pf = private;
  147. struct iio_dev *indio_dev = pf->indio_dev;
  148. struct maxim_thermocouple_data *data = iio_priv(indio_dev);
  149. int ret;
  150. ret = spi_read(data->spi, data->buffer, data->chip->read_size);
  151. if (!ret) {
  152. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  153. iio_get_time_ns(indio_dev));
  154. }
  155. iio_trigger_notify_done(indio_dev->trig);
  156. return IRQ_HANDLED;
  157. }
  158. static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
  159. struct iio_chan_spec const *chan,
  160. int *val, int *val2, long mask)
  161. {
  162. struct maxim_thermocouple_data *data = iio_priv(indio_dev);
  163. int ret = -EINVAL;
  164. switch (mask) {
  165. case IIO_CHAN_INFO_RAW:
  166. ret = iio_device_claim_direct_mode(indio_dev);
  167. if (ret)
  168. return ret;
  169. ret = maxim_thermocouple_read(data, chan, val);
  170. iio_device_release_direct_mode(indio_dev);
  171. if (!ret)
  172. return IIO_VAL_INT;
  173. break;
  174. case IIO_CHAN_INFO_SCALE:
  175. switch (chan->channel2) {
  176. case IIO_MOD_TEMP_AMBIENT:
  177. *val = 62;
  178. *val2 = 500000; /* 1000 * 0.0625 */
  179. ret = IIO_VAL_INT_PLUS_MICRO;
  180. break;
  181. default:
  182. *val = 250; /* 1000 * 0.25 */
  183. ret = IIO_VAL_INT;
  184. }
  185. break;
  186. case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
  187. *val = data->tc_type;
  188. ret = IIO_VAL_CHAR;
  189. break;
  190. }
  191. return ret;
  192. }
  193. static const struct iio_info maxim_thermocouple_info = {
  194. .read_raw = maxim_thermocouple_read_raw,
  195. };
  196. static int maxim_thermocouple_probe(struct spi_device *spi)
  197. {
  198. const struct spi_device_id *id = spi_get_device_id(spi);
  199. struct iio_dev *indio_dev;
  200. struct maxim_thermocouple_data *data;
  201. const int chip_type = (id->driver_data == MAX6675) ? MAX6675 : MAX31855;
  202. const struct maxim_thermocouple_chip *chip =
  203. &maxim_thermocouple_chips[chip_type];
  204. int ret;
  205. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  206. if (!indio_dev)
  207. return -ENOMEM;
  208. indio_dev->info = &maxim_thermocouple_info;
  209. indio_dev->name = MAXIM_THERMOCOUPLE_DRV_NAME;
  210. indio_dev->channels = chip->channels;
  211. indio_dev->available_scan_masks = chip->scan_masks;
  212. indio_dev->num_channels = chip->num_channels;
  213. indio_dev->modes = INDIO_DIRECT_MODE;
  214. data = iio_priv(indio_dev);
  215. data->spi = spi;
  216. data->chip = chip;
  217. data->tc_type = maxim_tc_types[id->driver_data];
  218. ret = devm_iio_triggered_buffer_setup(&spi->dev,
  219. indio_dev, NULL,
  220. maxim_thermocouple_trigger_handler, NULL);
  221. if (ret)
  222. return ret;
  223. if (id->driver_data == MAX31855)
  224. dev_warn(&spi->dev, "generic max31855 ID is deprecated\nplease use more specific part type");
  225. return devm_iio_device_register(&spi->dev, indio_dev);
  226. }
  227. static const struct spi_device_id maxim_thermocouple_id[] = {
  228. {"max6675", MAX6675},
  229. {"max31855", MAX31855},
  230. {"max31855k", MAX31855K},
  231. {"max31855j", MAX31855J},
  232. {"max31855n", MAX31855N},
  233. {"max31855s", MAX31855S},
  234. {"max31855t", MAX31855T},
  235. {"max31855e", MAX31855E},
  236. {"max31855r", MAX31855R},
  237. {},
  238. };
  239. MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
  240. static const struct of_device_id maxim_thermocouple_of_match[] = {
  241. { .compatible = "maxim,max6675" },
  242. { .compatible = "maxim,max31855" },
  243. { .compatible = "maxim,max31855k" },
  244. { .compatible = "maxim,max31855j" },
  245. { .compatible = "maxim,max31855n" },
  246. { .compatible = "maxim,max31855s" },
  247. { .compatible = "maxim,max31855t" },
  248. { .compatible = "maxim,max31855e" },
  249. { .compatible = "maxim,max31855r" },
  250. { },
  251. };
  252. MODULE_DEVICE_TABLE(of, maxim_thermocouple_of_match);
  253. static struct spi_driver maxim_thermocouple_driver = {
  254. .driver = {
  255. .name = MAXIM_THERMOCOUPLE_DRV_NAME,
  256. .of_match_table = maxim_thermocouple_of_match,
  257. },
  258. .probe = maxim_thermocouple_probe,
  259. .id_table = maxim_thermocouple_id,
  260. };
  261. module_spi_driver(maxim_thermocouple_driver);
  262. MODULE_AUTHOR("Matt Ranostay <[email protected]>");
  263. MODULE_DESCRIPTION("Maxim thermocouple sensors");
  264. MODULE_LICENSE("GPL");