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
9 #include <linux/init.h>
10 #include <linux/module.h>
11 #include <linux/err.h>
12 #include <linux/spi/spi.h>
13 #include <linux/types.h>
14 #include <linux/iio/iio.h>
15 #include <linux/iio/sysfs.h>
16 #include <linux/iio/trigger.h>
17 #include <linux/iio/buffer.h>
18 #include <linux/iio/triggered_buffer.h>
19 #include <linux/iio/trigger_consumer.h>
20
21 #define MAXIM_THERMOCOUPLE_DRV_NAME "maxim_thermocouple"
22
23 enum {
24 MAX6675,
25 MAX31855,
26 MAX31855K,
27 MAX31855J,
28 MAX31855N,
29 MAX31855S,
30 MAX31855T,
31 MAX31855E,
32 MAX31855R,
33 };
34
35 static const char maxim_tc_types[] = {
36 'K', '?', 'K', 'J', 'N', 'S', 'T', 'E', 'R'
37 };
38
39 static const struct iio_chan_spec max6675_channels[] = {
40 { /* thermocouple temperature */
41 .type = IIO_TEMP,
42 .info_mask_separate =
43 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
44 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE),
45 .scan_index = 0,
46 .scan_type = {
47 .sign = 's',
48 .realbits = 13,
49 .storagebits = 16,
50 .shift = 3,
51 .endianness = IIO_BE,
52 },
53 },
54 IIO_CHAN_SOFT_TIMESTAMP(1),
55 };
56
57 static const struct iio_chan_spec max31855_channels[] = {
58 { /* thermocouple temperature */
59 .type = IIO_TEMP,
60 .address = 2,
61 .info_mask_separate =
62 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE) |
63 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE),
64 .scan_index = 0,
65 .scan_type = {
66 .sign = 's',
67 .realbits = 14,
68 .storagebits = 16,
69 .shift = 2,
70 .endianness = IIO_BE,
71 },
72 },
73 { /* cold junction temperature */
74 .type = IIO_TEMP,
75 .address = 0,
76 .channel2 = IIO_MOD_TEMP_AMBIENT,
77 .modified = 1,
78 .info_mask_separate =
79 BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
80 .scan_index = 1,
81 .scan_type = {
82 .sign = 's',
83 .realbits = 12,
84 .storagebits = 16,
85 .shift = 4,
86 .endianness = IIO_BE,
87 },
88 },
89 IIO_CHAN_SOFT_TIMESTAMP(2),
90 };
91
92 static const unsigned long max31855_scan_masks[] = {0x3, 0};
93
94 struct maxim_thermocouple_chip {
95 const struct iio_chan_spec *channels;
96 const unsigned long *scan_masks;
97 u8 num_channels;
98 u8 read_size;
99
100 /* bit-check for valid input */
101 u32 status_bit;
102 };
103
104 static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
105 [MAX6675] = {
106 .channels = max6675_channels,
107 .num_channels = ARRAY_SIZE(max6675_channels),
108 .read_size = 2,
109 .status_bit = BIT(2),
110 },
111 [MAX31855] = {
112 .channels = max31855_channels,
113 .num_channels = ARRAY_SIZE(max31855_channels),
114 .read_size = 4,
115 .scan_masks = max31855_scan_masks,
116 .status_bit = BIT(16),
117 },
118 };
119
120 struct maxim_thermocouple_data {
121 struct spi_device *spi;
122 const struct maxim_thermocouple_chip *chip;
123 char tc_type;
124 /* Buffer for reading up to 2 hardware channels. */
125 struct {
126 union {
127 __be16 raw16;
128 __be32 raw32;
129 __be16 raw[2];
130 };
131 aligned_s64 timestamp;
132 } buffer __aligned(IIO_DMA_MINALIGN);
133 };
134
maxim_thermocouple_read(struct maxim_thermocouple_data * data,struct iio_chan_spec const * chan,int * val)135 static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
136 struct iio_chan_spec const *chan, int *val)
137 {
138 unsigned int storage_bytes = data->chip->read_size;
139 unsigned int shift = chan->scan_type.shift + (chan->address * 8);
140 int ret;
141
142 switch (storage_bytes) {
143 case 2:
144 ret = spi_read(data->spi, &data->buffer.raw16, storage_bytes);
145 *val = be16_to_cpu(data->buffer.raw16);
146 break;
147 case 4:
148 ret = spi_read(data->spi, &data->buffer.raw32, storage_bytes);
149 *val = be32_to_cpu(data->buffer.raw32);
150 break;
151 default:
152 ret = -EINVAL;
153 }
154
155 if (ret)
156 return ret;
157
158 /* check to be sure this is a valid reading */
159 if (*val & data->chip->status_bit)
160 return -EINVAL;
161
162 *val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
163
164 return 0;
165 }
166
maxim_thermocouple_trigger_handler(int irq,void * private)167 static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
168 {
169 struct iio_poll_func *pf = private;
170 struct iio_dev *indio_dev = pf->indio_dev;
171 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
172 int ret;
173
174 ret = spi_read(data->spi, data->buffer.raw, data->chip->read_size);
175 if (!ret) {
176 iio_push_to_buffers_with_ts(indio_dev, &data->buffer,
177 sizeof(data->buffer),
178 iio_get_time_ns(indio_dev));
179 }
180
181 iio_trigger_notify_done(indio_dev->trig);
182
183 return IRQ_HANDLED;
184 }
185
maxim_thermocouple_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)186 static int maxim_thermocouple_read_raw(struct iio_dev *indio_dev,
187 struct iio_chan_spec const *chan,
188 int *val, int *val2, long mask)
189 {
190 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
191 int ret;
192
193 switch (mask) {
194 case IIO_CHAN_INFO_RAW:
195 if (!iio_device_claim_direct(indio_dev))
196 return -EBUSY;
197
198 ret = maxim_thermocouple_read(data, chan, val);
199 iio_device_release_direct(indio_dev);
200 if (ret)
201 return ret;
202
203 return IIO_VAL_INT;
204 case IIO_CHAN_INFO_SCALE:
205 switch (chan->channel2) {
206 case IIO_MOD_TEMP_AMBIENT:
207 *val = 62;
208 *val2 = 500000; /* 1000 * 0.0625 */
209 return IIO_VAL_INT_PLUS_MICRO;
210 default:
211 *val = 250; /* 1000 * 0.25 */
212 return IIO_VAL_INT;
213 }
214 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
215 *val = data->tc_type;
216 return IIO_VAL_CHAR;
217 default:
218 return -EINVAL;
219 }
220 }
221
222 static const struct iio_info maxim_thermocouple_info = {
223 .read_raw = maxim_thermocouple_read_raw,
224 };
225
maxim_thermocouple_probe(struct spi_device * spi)226 static int maxim_thermocouple_probe(struct spi_device *spi)
227 {
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];
234 int ret;
235
236 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
237 if (!indio_dev)
238 return -ENOMEM;
239
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;
246
247 data = iio_priv(indio_dev);
248 data->spi = spi;
249 data->chip = chip;
250 data->tc_type = maxim_tc_types[id->driver_data];
251
252 ret = devm_iio_triggered_buffer_setup(&spi->dev,
253 indio_dev, NULL,
254 maxim_thermocouple_trigger_handler, NULL);
255 if (ret)
256 return ret;
257
258 if (id->driver_data == MAX31855)
259 dev_warn(&spi->dev, "generic max31855 ID is deprecated\nplease use more specific part type");
260
261 return devm_iio_device_register(&spi->dev, indio_dev);
262 }
263
264 static const struct spi_device_id maxim_thermocouple_id[] = {
265 { .name = "max6675", .driver_data = MAX6675 },
266 { .name = "max31855", .driver_data = MAX31855 },
267 { .name = "max31855k", .driver_data = MAX31855K },
268 { .name = "max31855j", .driver_data = MAX31855J },
269 { .name = "max31855n", .driver_data = MAX31855N },
270 { .name = "max31855s", .driver_data = MAX31855S },
271 { .name = "max31855t", .driver_data = MAX31855T },
272 { .name = "max31855e", .driver_data = MAX31855E },
273 { .name = "max31855r", .driver_data = MAX31855R },
274 { }
275 };
276 MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
277
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" },
288 { },
289 };
290 MODULE_DEVICE_TABLE(of, maxim_thermocouple_of_match);
291
292 static struct spi_driver maxim_thermocouple_driver = {
293 .driver = {
294 .name = MAXIM_THERMOCOUPLE_DRV_NAME,
295 .of_match_table = maxim_thermocouple_of_match,
296 },
297 .probe = maxim_thermocouple_probe,
298 .id_table = maxim_thermocouple_id,
299 };
300 module_spi_driver(maxim_thermocouple_driver);
301
302 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
303 MODULE_DESCRIPTION("Maxim thermocouple sensors");
304 MODULE_LICENSE("GPL");
305