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/module.h>
10 #include <linux/init.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/trigger.h>
16 #include <linux/iio/buffer.h>
17 #include <linux/iio/triggered_buffer.h>
18 #include <linux/iio/trigger_consumer.h>
20 #define MAXIM_THERMOCOUPLE_DRV_NAME "maxim_thermocouple"
27 static const struct iio_chan_spec max6675_channels[] = {
28 { /* thermocouple temperature */
31 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
41 IIO_CHAN_SOFT_TIMESTAMP(1),
44 static const struct iio_chan_spec max31855_channels[] = {
45 { /* thermocouple temperature */
49 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
59 { /* cold junction temperature */
62 .channel2 = IIO_MOD_TEMP_AMBIENT,
65 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
75 IIO_CHAN_SOFT_TIMESTAMP(2),
78 static const unsigned long max31855_scan_masks[] = {0x3, 0};
80 struct maxim_thermocouple_chip {
81 const struct iio_chan_spec *channels;
82 const unsigned long *scan_masks;
86 /* bit-check for valid input */
90 static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
92 .channels = max6675_channels,
93 .num_channels = ARRAY_SIZE(max6675_channels),
98 .channels = max31855_channels,
99 .num_channels = ARRAY_SIZE(max31855_channels),
101 .scan_masks = max31855_scan_masks,
102 .status_bit = BIT(16),
106 struct maxim_thermocouple_data {
107 struct spi_device *spi;
108 const struct maxim_thermocouple_chip *chip;
110 u8 buffer[16] ____cacheline_aligned;
113 static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
114 struct iio_chan_spec const *chan, int *val)
116 unsigned int storage_bytes = data->chip->read_size;
117 unsigned int shift = chan->scan_type.shift + (chan->address * 8);
122 switch (storage_bytes) {
124 ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
125 *val = be16_to_cpu(buf16);
128 ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
129 *val = be32_to_cpu(buf32);
138 /* check to be sure this is a valid reading */
139 if (*val & data->chip->status_bit)
142 *val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
147 static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
149 struct iio_poll_func *pf = private;
150 struct iio_dev *indio_dev = pf->indio_dev;
151 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
154 ret = spi_read(data->spi, data->buffer, data->chip->read_size);
156 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
157 iio_get_time_ns(indio_dev));
160 iio_trigger_notify_done(indio_dev->trig);
165 static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
166 struct iio_chan_spec const *chan,
167 int *val, int *val2, long mask)
169 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
173 case IIO_CHAN_INFO_RAW:
174 ret = iio_device_claim_direct_mode(indio_dev);
178 ret = maxim_thermocouple_read(data, chan, val);
179 iio_device_release_direct_mode(indio_dev);
185 case IIO_CHAN_INFO_SCALE:
186 switch (chan->channel2) {
187 case IIO_MOD_TEMP_AMBIENT:
189 *val2 = 500000; /* 1000 * 0.0625 */
190 ret = IIO_VAL_INT_PLUS_MICRO;
193 *val = 250; /* 1000 * 0.25 */
202 static const struct iio_info maxim_thermocouple_info = {
203 .read_raw = maxim_thermocouple_read_raw,
206 static int maxim_thermocouple_probe(struct spi_device *spi)
208 const struct spi_device_id *id = spi_get_device_id(spi);
209 struct iio_dev *indio_dev;
210 struct maxim_thermocouple_data *data;
211 const struct maxim_thermocouple_chip *chip =
212 &maxim_thermocouple_chips[id->driver_data];
215 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
219 indio_dev->info = &maxim_thermocouple_info;
220 indio_dev->name = MAXIM_THERMOCOUPLE_DRV_NAME;
221 indio_dev->channels = chip->channels;
222 indio_dev->available_scan_masks = chip->scan_masks;
223 indio_dev->num_channels = chip->num_channels;
224 indio_dev->modes = INDIO_DIRECT_MODE;
225 indio_dev->dev.parent = &spi->dev;
227 data = iio_priv(indio_dev);
231 ret = iio_triggered_buffer_setup(indio_dev, NULL,
232 maxim_thermocouple_trigger_handler, NULL);
236 ret = iio_device_register(indio_dev);
238 goto error_unreg_buffer;
243 iio_triggered_buffer_cleanup(indio_dev);
248 static int maxim_thermocouple_remove(struct spi_device *spi)
250 struct iio_dev *indio_dev = spi_get_drvdata(spi);
252 iio_device_unregister(indio_dev);
253 iio_triggered_buffer_cleanup(indio_dev);
258 static const struct spi_device_id maxim_thermocouple_id[] = {
259 {"max6675", MAX6675},
260 {"max31855", MAX31855},
263 MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
265 static struct spi_driver maxim_thermocouple_driver = {
267 .name = MAXIM_THERMOCOUPLE_DRV_NAME,
269 .probe = maxim_thermocouple_probe,
270 .remove = maxim_thermocouple_remove,
271 .id_table = maxim_thermocouple_id,
273 module_spi_driver(maxim_thermocouple_driver);
275 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
276 MODULE_DESCRIPTION("Maxim thermocouple sensors");
277 MODULE_LICENSE("GPL");