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>
14 #include <linux/of_device.h>
15 #include <linux/spi/spi.h>
16 #include <linux/iio/iio.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"
29 static const struct iio_chan_spec max6675_channels[] = {
30 { /* thermocouple temperature */
33 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
43 IIO_CHAN_SOFT_TIMESTAMP(1),
46 static const struct iio_chan_spec max31855_channels[] = {
47 { /* thermocouple temperature */
51 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
61 { /* cold junction temperature */
64 .channel2 = IIO_MOD_TEMP_AMBIENT,
67 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
77 IIO_CHAN_SOFT_TIMESTAMP(2),
80 static const unsigned long max31855_scan_masks[] = {0x3, 0};
82 struct maxim_thermocouple_chip {
83 const struct iio_chan_spec *channels;
84 const unsigned long *scan_masks;
88 /* bit-check for valid input */
92 static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
94 .channels = max6675_channels,
95 .num_channels = ARRAY_SIZE(max6675_channels),
100 .channels = max31855_channels,
101 .num_channels = ARRAY_SIZE(max31855_channels),
103 .scan_masks = max31855_scan_masks,
104 .status_bit = BIT(16),
108 struct maxim_thermocouple_data {
109 struct spi_device *spi;
110 const struct maxim_thermocouple_chip *chip;
112 u8 buffer[16] ____cacheline_aligned;
115 static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
116 struct iio_chan_spec const *chan, int *val)
118 unsigned int storage_bytes = data->chip->read_size;
119 unsigned int shift = chan->scan_type.shift + (chan->address * 8);
124 switch (storage_bytes) {
126 ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
127 *val = be16_to_cpu(buf16);
130 ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
131 *val = be32_to_cpu(buf32);
140 /* check to be sure this is a valid reading */
141 if (*val & data->chip->status_bit)
144 *val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
149 static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
151 struct iio_poll_func *pf = private;
152 struct iio_dev *indio_dev = pf->indio_dev;
153 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
156 ret = spi_read(data->spi, data->buffer, data->chip->read_size);
158 iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
159 iio_get_time_ns(indio_dev));
162 iio_trigger_notify_done(indio_dev->trig);
167 static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
168 struct iio_chan_spec const *chan,
169 int *val, int *val2, long mask)
171 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
175 case IIO_CHAN_INFO_RAW:
176 ret = iio_device_claim_direct_mode(indio_dev);
180 ret = maxim_thermocouple_read(data, chan, val);
181 iio_device_release_direct_mode(indio_dev);
187 case IIO_CHAN_INFO_SCALE:
188 switch (chan->channel2) {
189 case IIO_MOD_TEMP_AMBIENT:
191 *val2 = 500000; /* 1000 * 0.0625 */
192 ret = IIO_VAL_INT_PLUS_MICRO;
195 *val = 250; /* 1000 * 0.25 */
204 static const struct iio_info maxim_thermocouple_info = {
205 .read_raw = maxim_thermocouple_read_raw,
208 static int maxim_thermocouple_probe(struct spi_device *spi)
210 const struct spi_device_id *id = spi_get_device_id(spi);
211 struct iio_dev *indio_dev;
212 struct maxim_thermocouple_data *data;
213 const struct maxim_thermocouple_chip *chip =
214 &maxim_thermocouple_chips[id->driver_data];
217 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
221 indio_dev->info = &maxim_thermocouple_info;
222 indio_dev->name = MAXIM_THERMOCOUPLE_DRV_NAME;
223 indio_dev->channels = chip->channels;
224 indio_dev->available_scan_masks = chip->scan_masks;
225 indio_dev->num_channels = chip->num_channels;
226 indio_dev->modes = INDIO_DIRECT_MODE;
227 indio_dev->dev.parent = &spi->dev;
229 data = iio_priv(indio_dev);
233 ret = iio_triggered_buffer_setup(indio_dev, NULL,
234 maxim_thermocouple_trigger_handler, NULL);
238 ret = iio_device_register(indio_dev);
240 goto error_unreg_buffer;
245 iio_triggered_buffer_cleanup(indio_dev);
250 static int maxim_thermocouple_remove(struct spi_device *spi)
252 struct iio_dev *indio_dev = spi_get_drvdata(spi);
254 iio_device_unregister(indio_dev);
255 iio_triggered_buffer_cleanup(indio_dev);
260 static const struct spi_device_id maxim_thermocouple_id[] = {
261 {"max6675", MAX6675},
262 {"max31855", MAX31855},
265 MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
267 static const struct of_device_id maxim_thermocouple_of_match[] = {
268 { .compatible = "maxim,max6675" },
269 { .compatible = "maxim,max31855" },
272 MODULE_DEVICE_TABLE(of, maxim_thermocouple_of_match);
274 static struct spi_driver maxim_thermocouple_driver = {
276 .name = MAXIM_THERMOCOUPLE_DRV_NAME,
277 .of_match_table = maxim_thermocouple_of_match,
279 .probe = maxim_thermocouple_probe,
280 .remove = maxim_thermocouple_remove,
281 .id_table = maxim_thermocouple_id,
283 module_spi_driver(maxim_thermocouple_driver);
285 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
286 MODULE_DESCRIPTION("Maxim thermocouple sensors");
287 MODULE_LICENSE("GPL");