1 // SPDX-License-Identifier: GPL-2.0+
3 * maxim_thermocouple.c - Support for Maxim thermocouple chips
5 * Copyright (C) 2016-2018 Matt Ranostay
6 * Author: <matt.ranostay@konsulko.com>
9 #include <linux/init.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/mutex.h>
13 #include <linux/err.h>
14 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
16 #include <linux/iio/sysfs.h>
17 #include <linux/iio/trigger.h>
18 #include <linux/iio/buffer.h>
19 #include <linux/iio/triggered_buffer.h>
20 #include <linux/iio/trigger_consumer.h>
22 #define MAXIM_THERMOCOUPLE_DRV_NAME "maxim_thermocouple"
36 static const char maxim_tc_types[] = {
37 'K', '?', 'K', 'J', 'N', 'S', 'T', 'E', 'R'
40 static const struct iio_chan_spec max6675_channels[] = {
41 { /* thermocouple temperature */
44 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
45 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE),
55 IIO_CHAN_SOFT_TIMESTAMP(1),
58 static const struct iio_chan_spec max31855_channels[] = {
59 { /* thermocouple temperature */
63 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
64 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE),
74 { /* cold junction temperature */
77 .channel2 = IIO_MOD_TEMP_AMBIENT,
80 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
90 IIO_CHAN_SOFT_TIMESTAMP(2),
93 static const unsigned long max31855_scan_masks[] = {0x3, 0};
95 struct maxim_thermocouple_chip {
96 const struct iio_chan_spec *channels;
97 const unsigned long *scan_masks;
101 /* bit-check for valid input */
105 static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
107 .channels = max6675_channels,
108 .num_channels = ARRAY_SIZE(max6675_channels),
110 .status_bit = BIT(2),
113 .channels = max31855_channels,
114 .num_channels = ARRAY_SIZE(max31855_channels),
116 .scan_masks = max31855_scan_masks,
117 .status_bit = BIT(16),
121 struct maxim_thermocouple_data {
122 struct spi_device *spi;
123 const struct maxim_thermocouple_chip *chip;
125 u8 buffer[16] __aligned(IIO_DMA_MINALIGN);
129 static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
130 struct iio_chan_spec const *chan, int *val)
132 unsigned int storage_bytes = data->chip->read_size;
133 unsigned int shift = chan->scan_type.shift + (chan->address * 8);
138 switch (storage_bytes) {
140 ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
141 *val = be16_to_cpu(buf16);
144 ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
145 *val = be32_to_cpu(buf32);
154 /* check to be sure this is a valid reading */
155 if (*val & data->chip->status_bit)
158 *val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
163 static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
165 struct iio_poll_func *pf = private;
166 struct iio_dev *indio_dev = pf->indio_dev;
167 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
170 ret = spi_read(data->spi, data->buffer, data->chip->read_size);
172 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
173 iio_get_time_ns(indio_dev));
176 iio_trigger_notify_done(indio_dev->trig);
181 static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
182 struct iio_chan_spec const *chan,
183 int *val, int *val2, long mask)
185 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
189 case IIO_CHAN_INFO_RAW:
190 ret = iio_device_claim_direct_mode(indio_dev);
194 ret = maxim_thermocouple_read(data, chan, val);
195 iio_device_release_direct_mode(indio_dev);
201 case IIO_CHAN_INFO_SCALE:
202 switch (chan->channel2) {
203 case IIO_MOD_TEMP_AMBIENT:
205 *val2 = 500000; /* 1000 * 0.0625 */
206 ret = IIO_VAL_INT_PLUS_MICRO;
209 *val = 250; /* 1000 * 0.25 */
213 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
214 *val = data->tc_type;
222 static const struct iio_info maxim_thermocouple_info = {
223 .read_raw = maxim_thermocouple_read_raw,
226 static int maxim_thermocouple_probe(struct spi_device *spi)
228 const struct spi_device_id *id = spi_get_device_id(spi);
229 struct iio_dev *indio_dev;
230 struct maxim_thermocouple_data *data;
231 const int chip_type = (id->driver_data == MAX6675) ? MAX6675 : MAX31855;
232 const struct maxim_thermocouple_chip *chip =
233 &maxim_thermocouple_chips[chip_type];
236 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
240 indio_dev->info = &maxim_thermocouple_info;
241 indio_dev->name = MAXIM_THERMOCOUPLE_DRV_NAME;
242 indio_dev->channels = chip->channels;
243 indio_dev->available_scan_masks = chip->scan_masks;
244 indio_dev->num_channels = chip->num_channels;
245 indio_dev->modes = INDIO_DIRECT_MODE;
247 data = iio_priv(indio_dev);
250 data->tc_type = maxim_tc_types[id->driver_data];
252 ret = devm_iio_triggered_buffer_setup(&spi->dev,
254 maxim_thermocouple_trigger_handler, NULL);
258 if (id->driver_data == MAX31855)
259 dev_warn(&spi->dev, "generic max31855 ID is deprecated\nplease use more specific part type");
261 return devm_iio_device_register(&spi->dev, indio_dev);
264 static const struct spi_device_id maxim_thermocouple_id[] = {
265 {"max6675", MAX6675},
266 {"max31855", MAX31855},
267 {"max31855k", MAX31855K},
268 {"max31855j", MAX31855J},
269 {"max31855n", MAX31855N},
270 {"max31855s", MAX31855S},
271 {"max31855t", MAX31855T},
272 {"max31855e", MAX31855E},
273 {"max31855r", MAX31855R},
276 MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
278 static const struct of_device_id maxim_thermocouple_of_match[] = {
279 { .compatible = "maxim,max6675" },
280 { .compatible = "maxim,max31855" },
281 { .compatible = "maxim,max31855k" },
282 { .compatible = "maxim,max31855j" },
283 { .compatible = "maxim,max31855n" },
284 { .compatible = "maxim,max31855s" },
285 { .compatible = "maxim,max31855t" },
286 { .compatible = "maxim,max31855e" },
287 { .compatible = "maxim,max31855r" },
290 MODULE_DEVICE_TABLE(of, maxim_thermocouple_of_match);
292 static struct spi_driver maxim_thermocouple_driver = {
294 .name = MAXIM_THERMOCOUPLE_DRV_NAME,
295 .of_match_table = maxim_thermocouple_of_match,
297 .probe = maxim_thermocouple_probe,
298 .id_table = maxim_thermocouple_id,
300 module_spi_driver(maxim_thermocouple_driver);
302 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
303 MODULE_DESCRIPTION("Maxim thermocouple sensors");
304 MODULE_LICENSE("GPL");