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/mod_devicetable.h>
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/spi/spi.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
125 u8 buffer[16] __aligned(IIO_DMA_MINALIGN);
126 };
127
maxim_thermocouple_read(struct maxim_thermocouple_data * data,struct iio_chan_spec const * chan,int * val)128 static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
129 struct iio_chan_spec const *chan, int *val)
130 {
131 unsigned int storage_bytes = data->chip->read_size;
132 unsigned int shift = chan->scan_type.shift + (chan->address * 8);
133 __be16 buf16;
134 __be32 buf32;
135 int ret;
136
137 switch (storage_bytes) {
138 case 2:
139 ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
140 *val = be16_to_cpu(buf16);
141 break;
142 case 4:
143 ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
144 *val = be32_to_cpu(buf32);
145 break;
146 default:
147 ret = -EINVAL;
148 }
149
150 if (ret)
151 return ret;
152
153 /* check to be sure this is a valid reading */
154 if (*val & data->chip->status_bit)
155 return -EINVAL;
156
157 *val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
158
159 return 0;
160 }
161
maxim_thermocouple_trigger_handler(int irq,void * private)162 static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
163 {
164 struct iio_poll_func *pf = private;
165 struct iio_dev *indio_dev = pf->indio_dev;
166 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
167 int ret;
168
169 ret = spi_read(data->spi, data->buffer, data->chip->read_size);
170 if (!ret) {
171 iio_push_to_buffers_with_ts(indio_dev, data->buffer,
172 sizeof(data->buffer),
173 iio_get_time_ns(indio_dev));
174 }
175
176 iio_trigger_notify_done(indio_dev->trig);
177
178 return IRQ_HANDLED;
179 }
180
maxim_thermocouple_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)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)
184 {
185 struct maxim_thermocouple_data *data = iio_priv(indio_dev);
186 int ret;
187
188 switch (mask) {
189 case IIO_CHAN_INFO_RAW:
190 if (!iio_device_claim_direct(indio_dev))
191 return -EBUSY;
192
193 ret = maxim_thermocouple_read(data, chan, val);
194 iio_device_release_direct(indio_dev);
195 if (ret)
196 return ret;
197
198 return IIO_VAL_INT;
199 case IIO_CHAN_INFO_SCALE:
200 switch (chan->channel2) {
201 case IIO_MOD_TEMP_AMBIENT:
202 *val = 62;
203 *val2 = 500000; /* 1000 * 0.0625 */
204 return IIO_VAL_INT_PLUS_MICRO;
205 default:
206 *val = 250; /* 1000 * 0.25 */
207 return IIO_VAL_INT;
208 }
209 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE:
210 *val = data->tc_type;
211 return IIO_VAL_CHAR;
212 default:
213 return -EINVAL;
214 }
215 }
216
217 static const struct iio_info maxim_thermocouple_info = {
218 .read_raw = maxim_thermocouple_read_raw,
219 };
220
maxim_thermocouple_probe(struct spi_device * spi)221 static int maxim_thermocouple_probe(struct spi_device *spi)
222 {
223 const struct spi_device_id *id = spi_get_device_id(spi);
224 struct iio_dev *indio_dev;
225 struct maxim_thermocouple_data *data;
226 const int chip_type = (id->driver_data == MAX6675) ? MAX6675 : MAX31855;
227 const struct maxim_thermocouple_chip *chip =
228 &maxim_thermocouple_chips[chip_type];
229 int ret;
230
231 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
232 if (!indio_dev)
233 return -ENOMEM;
234
235 indio_dev->info = &maxim_thermocouple_info;
236 indio_dev->name = MAXIM_THERMOCOUPLE_DRV_NAME;
237 indio_dev->channels = chip->channels;
238 indio_dev->available_scan_masks = chip->scan_masks;
239 indio_dev->num_channels = chip->num_channels;
240 indio_dev->modes = INDIO_DIRECT_MODE;
241
242 data = iio_priv(indio_dev);
243 data->spi = spi;
244 data->chip = chip;
245 data->tc_type = maxim_tc_types[id->driver_data];
246
247 ret = devm_iio_triggered_buffer_setup(&spi->dev,
248 indio_dev, NULL,
249 maxim_thermocouple_trigger_handler, NULL);
250 if (ret)
251 return ret;
252
253 if (id->driver_data == MAX31855)
254 dev_warn(&spi->dev, "generic max31855 ID is deprecated\nplease use more specific part type");
255
256 return devm_iio_device_register(&spi->dev, indio_dev);
257 }
258
259 static const struct spi_device_id maxim_thermocouple_id[] = {
260 {"max6675", MAX6675},
261 {"max31855", MAX31855},
262 {"max31855k", MAX31855K},
263 {"max31855j", MAX31855J},
264 {"max31855n", MAX31855N},
265 {"max31855s", MAX31855S},
266 {"max31855t", MAX31855T},
267 {"max31855e", MAX31855E},
268 {"max31855r", MAX31855R},
269 { }
270 };
271 MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
272
273 static const struct of_device_id maxim_thermocouple_of_match[] = {
274 { .compatible = "maxim,max6675" },
275 { .compatible = "maxim,max31855" },
276 { .compatible = "maxim,max31855k" },
277 { .compatible = "maxim,max31855j" },
278 { .compatible = "maxim,max31855n" },
279 { .compatible = "maxim,max31855s" },
280 { .compatible = "maxim,max31855t" },
281 { .compatible = "maxim,max31855e" },
282 { .compatible = "maxim,max31855r" },
283 { },
284 };
285 MODULE_DEVICE_TABLE(of, maxim_thermocouple_of_match);
286
287 static struct spi_driver maxim_thermocouple_driver = {
288 .driver = {
289 .name = MAXIM_THERMOCOUPLE_DRV_NAME,
290 .of_match_table = maxim_thermocouple_of_match,
291 },
292 .probe = maxim_thermocouple_probe,
293 .id_table = maxim_thermocouple_id,
294 };
295 module_spi_driver(maxim_thermocouple_driver);
296
297 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
298 MODULE_DESCRIPTION("Maxim thermocouple sensors");
299 MODULE_LICENSE("GPL");
300