maxim_thermocouple.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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: <matt.ranostay@konsulko.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/mutex.h>
  11. #include <linux/err.h>
  12. #include <linux/spi/spi.h>
  13. #include <linux/iio/iio.h>
  14. #include <linux/iio/trigger.h>
  15. #include <linux/iio/buffer.h>
  16. #include <linux/iio/triggered_buffer.h>
  17. #include <linux/iio/trigger_consumer.h>
  18. #define MAXIM_THERMOCOUPLE_DRV_NAME "maxim_thermocouple"
  19. enum {
  20. MAX6675,
  21. MAX31855,
  22. };
  23. static const struct iio_chan_spec max6675_channels[] = {
  24. { /* thermocouple temperature */
  25. .type = IIO_TEMP,
  26. .info_mask_separate =
  27. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  28. .scan_index = 0,
  29. .scan_type = {
  30. .sign = 's',
  31. .realbits = 13,
  32. .storagebits = 16,
  33. .shift = 3,
  34. .endianness = IIO_BE,
  35. },
  36. },
  37. IIO_CHAN_SOFT_TIMESTAMP(1),
  38. };
  39. static const struct iio_chan_spec max31855_channels[] = {
  40. { /* thermocouple temperature */
  41. .type = IIO_TEMP,
  42. .address = 2,
  43. .info_mask_separate =
  44. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  45. .scan_index = 0,
  46. .scan_type = {
  47. .sign = 's',
  48. .realbits = 14,
  49. .storagebits = 16,
  50. .shift = 2,
  51. .endianness = IIO_BE,
  52. },
  53. },
  54. { /* cold junction temperature */
  55. .type = IIO_TEMP,
  56. .address = 0,
  57. .channel2 = IIO_MOD_TEMP_AMBIENT,
  58. .modified = 1,
  59. .info_mask_separate =
  60. BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
  61. .scan_index = 1,
  62. .scan_type = {
  63. .sign = 's',
  64. .realbits = 12,
  65. .storagebits = 16,
  66. .shift = 4,
  67. .endianness = IIO_BE,
  68. },
  69. },
  70. IIO_CHAN_SOFT_TIMESTAMP(2),
  71. };
  72. static const unsigned long max31855_scan_masks[] = {0x3, 0};
  73. struct maxim_thermocouple_chip {
  74. const struct iio_chan_spec *channels;
  75. const unsigned long *scan_masks;
  76. u8 num_channels;
  77. u8 read_size;
  78. /* bit-check for valid input */
  79. u32 status_bit;
  80. };
  81. static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
  82. [MAX6675] = {
  83. .channels = max6675_channels,
  84. .num_channels = ARRAY_SIZE(max6675_channels),
  85. .read_size = 2,
  86. .status_bit = BIT(2),
  87. },
  88. [MAX31855] = {
  89. .channels = max31855_channels,
  90. .num_channels = ARRAY_SIZE(max31855_channels),
  91. .read_size = 4,
  92. .scan_masks = max31855_scan_masks,
  93. .status_bit = BIT(16),
  94. },
  95. };
  96. struct maxim_thermocouple_data {
  97. struct spi_device *spi;
  98. const struct maxim_thermocouple_chip *chip;
  99. u8 buffer[16] ____cacheline_aligned;
  100. };
  101. static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
  102. struct iio_chan_spec const *chan, int *val)
  103. {
  104. unsigned int storage_bytes = data->chip->read_size;
  105. unsigned int shift = chan->scan_type.shift + (chan->address * 8);
  106. __be16 buf16;
  107. __be32 buf32;
  108. int ret;
  109. switch (storage_bytes) {
  110. case 2:
  111. ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
  112. *val = be16_to_cpu(buf16);
  113. break;
  114. case 4:
  115. ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
  116. *val = be32_to_cpu(buf32);
  117. break;
  118. default:
  119. ret = -EINVAL;
  120. }
  121. if (ret)
  122. return ret;
  123. /* check to be sure this is a valid reading */
  124. if (*val & data->chip->status_bit)
  125. return -EINVAL;
  126. *val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
  127. return 0;
  128. }
  129. static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
  130. {
  131. struct iio_poll_func *pf = private;
  132. struct iio_dev *indio_dev = pf->indio_dev;
  133. struct maxim_thermocouple_data *data = iio_priv(indio_dev);
  134. int ret;
  135. ret = spi_read(data->spi, data->buffer, data->chip->read_size);
  136. if (!ret) {
  137. iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
  138. iio_get_time_ns(indio_dev));
  139. }
  140. iio_trigger_notify_done(indio_dev->trig);
  141. return IRQ_HANDLED;
  142. }
  143. static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
  144. struct iio_chan_spec const *chan,
  145. int *val, int *val2, long mask)
  146. {
  147. struct maxim_thermocouple_data *data = iio_priv(indio_dev);
  148. int ret = -EINVAL;
  149. switch (mask) {
  150. case IIO_CHAN_INFO_RAW:
  151. ret = iio_device_claim_direct_mode(indio_dev);
  152. if (ret)
  153. return ret;
  154. ret = maxim_thermocouple_read(data, chan, val);
  155. iio_device_release_direct_mode(indio_dev);
  156. if (!ret)
  157. return IIO_VAL_INT;
  158. break;
  159. case IIO_CHAN_INFO_SCALE:
  160. switch (chan->channel2) {
  161. case IIO_MOD_TEMP_AMBIENT:
  162. *val = 62;
  163. *val2 = 500000; /* 1000 * 0.0625 */
  164. ret = IIO_VAL_INT_PLUS_MICRO;
  165. break;
  166. default:
  167. *val = 250; /* 1000 * 0.25 */
  168. ret = IIO_VAL_INT;
  169. };
  170. break;
  171. }
  172. return ret;
  173. }
  174. static const struct iio_info maxim_thermocouple_info = {
  175. .read_raw = maxim_thermocouple_read_raw,
  176. };
  177. static int maxim_thermocouple_probe(struct spi_device *spi)
  178. {
  179. const struct spi_device_id *id = spi_get_device_id(spi);
  180. struct iio_dev *indio_dev;
  181. struct maxim_thermocouple_data *data;
  182. const struct maxim_thermocouple_chip *chip =
  183. &maxim_thermocouple_chips[id->driver_data];
  184. int ret;
  185. indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
  186. if (!indio_dev)
  187. return -ENOMEM;
  188. indio_dev->info = &maxim_thermocouple_info;
  189. indio_dev->name = MAXIM_THERMOCOUPLE_DRV_NAME;
  190. indio_dev->channels = chip->channels;
  191. indio_dev->available_scan_masks = chip->scan_masks;
  192. indio_dev->num_channels = chip->num_channels;
  193. indio_dev->modes = INDIO_DIRECT_MODE;
  194. indio_dev->dev.parent = &spi->dev;
  195. data = iio_priv(indio_dev);
  196. data->spi = spi;
  197. data->chip = chip;
  198. ret = iio_triggered_buffer_setup(indio_dev, NULL,
  199. maxim_thermocouple_trigger_handler, NULL);
  200. if (ret)
  201. return ret;
  202. ret = iio_device_register(indio_dev);
  203. if (ret)
  204. goto error_unreg_buffer;
  205. return 0;
  206. error_unreg_buffer:
  207. iio_triggered_buffer_cleanup(indio_dev);
  208. return ret;
  209. }
  210. static int maxim_thermocouple_remove(struct spi_device *spi)
  211. {
  212. struct iio_dev *indio_dev = spi_get_drvdata(spi);
  213. iio_device_unregister(indio_dev);
  214. iio_triggered_buffer_cleanup(indio_dev);
  215. return 0;
  216. }
  217. static const struct spi_device_id maxim_thermocouple_id[] = {
  218. {"max6675", MAX6675},
  219. {"max31855", MAX31855},
  220. {},
  221. };
  222. MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
  223. static struct spi_driver maxim_thermocouple_driver = {
  224. .driver = {
  225. .name = MAXIM_THERMOCOUPLE_DRV_NAME,
  226. },
  227. .probe = maxim_thermocouple_probe,
  228. .remove = maxim_thermocouple_remove,
  229. .id_table = maxim_thermocouple_id,
  230. };
  231. module_spi_driver(maxim_thermocouple_driver);
  232. MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
  233. MODULE_DESCRIPTION("Maxim thermocouple sensors");
  234. MODULE_LICENSE("GPL");