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