maxim_thermocouple.c 6.7 KB

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