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/module.h>
10 #include <linux/init.h>
11 #include <linux/mutex.h>
12 #include <linux/err.h>
13 #include <linux/of.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>
21 
22 #define MAXIM_THERMOCOUPLE_DRV_NAME	"maxim_thermocouple"
23 
24 enum {
25 	MAX6675,
26 	MAX31855,
27 };
28 
29 static const struct iio_chan_spec max6675_channels[] = {
30 	{	/* thermocouple temperature */
31 		.type = IIO_TEMP,
32 		.info_mask_separate =
33 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
34 		.scan_index = 0,
35 		.scan_type = {
36 			.sign = 's',
37 			.realbits = 13,
38 			.storagebits = 16,
39 			.shift = 3,
40 			.endianness = IIO_BE,
41 		},
42 	},
43 	IIO_CHAN_SOFT_TIMESTAMP(1),
44 };
45 
46 static const struct iio_chan_spec max31855_channels[] = {
47 	{	/* thermocouple temperature */
48 		.type = IIO_TEMP,
49 		.address = 2,
50 		.info_mask_separate =
51 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
52 		.scan_index = 0,
53 		.scan_type = {
54 			.sign = 's',
55 			.realbits = 14,
56 			.storagebits = 16,
57 			.shift = 2,
58 			.endianness = IIO_BE,
59 		},
60 	},
61 	{	/* cold junction temperature */
62 		.type = IIO_TEMP,
63 		.address = 0,
64 		.channel2 = IIO_MOD_TEMP_AMBIENT,
65 		.modified = 1,
66 		.info_mask_separate =
67 			BIT(IIO_CHAN_INFO_RAW) | BIT(IIO_CHAN_INFO_SCALE),
68 		.scan_index = 1,
69 		.scan_type = {
70 			.sign = 's',
71 			.realbits = 12,
72 			.storagebits = 16,
73 			.shift = 4,
74 			.endianness = IIO_BE,
75 		},
76 	},
77 	IIO_CHAN_SOFT_TIMESTAMP(2),
78 };
79 
80 static const unsigned long max31855_scan_masks[] = {0x3, 0};
81 
82 struct maxim_thermocouple_chip {
83 	const struct iio_chan_spec *channels;
84 	const unsigned long *scan_masks;
85 	u8 num_channels;
86 	u8 read_size;
87 
88 	/* bit-check for valid input */
89 	u32 status_bit;
90 };
91 
92 static const struct maxim_thermocouple_chip maxim_thermocouple_chips[] = {
93 	[MAX6675] = {
94 			.channels = max6675_channels,
95 			.num_channels = ARRAY_SIZE(max6675_channels),
96 			.read_size = 2,
97 			.status_bit = BIT(2),
98 		},
99 	[MAX31855] = {
100 			.channels = max31855_channels,
101 			.num_channels = ARRAY_SIZE(max31855_channels),
102 			.read_size = 4,
103 			.scan_masks = max31855_scan_masks,
104 			.status_bit = BIT(16),
105 		},
106 };
107 
108 struct maxim_thermocouple_data {
109 	struct spi_device *spi;
110 	const struct maxim_thermocouple_chip *chip;
111 
112 	u8 buffer[16] ____cacheline_aligned;
113 };
114 
115 static int maxim_thermocouple_read(struct maxim_thermocouple_data *data,
116 				   struct iio_chan_spec const *chan, int *val)
117 {
118 	unsigned int storage_bytes = data->chip->read_size;
119 	unsigned int shift = chan->scan_type.shift + (chan->address * 8);
120 	__be16 buf16;
121 	__be32 buf32;
122 	int ret;
123 
124 	switch (storage_bytes) {
125 	case 2:
126 		ret = spi_read(data->spi, (void *)&buf16, storage_bytes);
127 		*val = be16_to_cpu(buf16);
128 		break;
129 	case 4:
130 		ret = spi_read(data->spi, (void *)&buf32, storage_bytes);
131 		*val = be32_to_cpu(buf32);
132 		break;
133 	default:
134 		ret = -EINVAL;
135 	}
136 
137 	if (ret)
138 		return ret;
139 
140 	/* check to be sure this is a valid reading */
141 	if (*val & data->chip->status_bit)
142 		return -EINVAL;
143 
144 	*val = sign_extend32(*val >> shift, chan->scan_type.realbits - 1);
145 
146 	return 0;
147 }
148 
149 static irqreturn_t maxim_thermocouple_trigger_handler(int irq, void *private)
150 {
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);
154 	int ret;
155 
156 	ret = spi_read(data->spi, data->buffer, data->chip->read_size);
157 	if (!ret) {
158 		iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
159 						   iio_get_time_ns(indio_dev));
160 	}
161 
162 	iio_trigger_notify_done(indio_dev->trig);
163 
164 	return IRQ_HANDLED;
165 }
166 
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)
170 {
171 	struct maxim_thermocouple_data *data = iio_priv(indio_dev);
172 	int ret = -EINVAL;
173 
174 	switch (mask) {
175 	case IIO_CHAN_INFO_RAW:
176 		ret = iio_device_claim_direct_mode(indio_dev);
177 		if (ret)
178 			return ret;
179 
180 		ret = maxim_thermocouple_read(data, chan, val);
181 		iio_device_release_direct_mode(indio_dev);
182 
183 		if (!ret)
184 			return IIO_VAL_INT;
185 
186 		break;
187 	case IIO_CHAN_INFO_SCALE:
188 		switch (chan->channel2) {
189 		case IIO_MOD_TEMP_AMBIENT:
190 			*val = 62;
191 			*val2 = 500000; /* 1000 * 0.0625 */
192 			ret = IIO_VAL_INT_PLUS_MICRO;
193 			break;
194 		default:
195 			*val = 250; /* 1000 * 0.25 */
196 			ret = IIO_VAL_INT;
197 		};
198 		break;
199 	}
200 
201 	return ret;
202 }
203 
204 static const struct iio_info maxim_thermocouple_info = {
205 	.read_raw = maxim_thermocouple_read_raw,
206 };
207 
208 static int maxim_thermocouple_probe(struct spi_device *spi)
209 {
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];
215 	int ret;
216 
217 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*data));
218 	if (!indio_dev)
219 		return -ENOMEM;
220 
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;
228 
229 	data = iio_priv(indio_dev);
230 	data->spi = spi;
231 	data->chip = chip;
232 
233 	ret = iio_triggered_buffer_setup(indio_dev, NULL,
234 				maxim_thermocouple_trigger_handler, NULL);
235 	if (ret)
236 		return ret;
237 
238 	ret = iio_device_register(indio_dev);
239 	if (ret)
240 		goto error_unreg_buffer;
241 
242 	return 0;
243 
244 error_unreg_buffer:
245 	iio_triggered_buffer_cleanup(indio_dev);
246 
247 	return ret;
248 }
249 
250 static int maxim_thermocouple_remove(struct spi_device *spi)
251 {
252 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
253 
254 	iio_device_unregister(indio_dev);
255 	iio_triggered_buffer_cleanup(indio_dev);
256 
257 	return 0;
258 }
259 
260 static const struct spi_device_id maxim_thermocouple_id[] = {
261 	{"max6675", MAX6675},
262 	{"max31855", MAX31855},
263 	{},
264 };
265 MODULE_DEVICE_TABLE(spi, maxim_thermocouple_id);
266 
267 static const struct of_device_id maxim_thermocouple_of_match[] = {
268         { .compatible = "maxim,max6675" },
269         { .compatible = "maxim,max31855" },
270         { },
271 };
272 MODULE_DEVICE_TABLE(of, maxim_thermocouple_of_match);
273 
274 static struct spi_driver maxim_thermocouple_driver = {
275 	.driver = {
276 		.name	= MAXIM_THERMOCOUPLE_DRV_NAME,
277 		.of_match_table = maxim_thermocouple_of_match,
278 	},
279 	.probe		= maxim_thermocouple_probe,
280 	.remove		= maxim_thermocouple_remove,
281 	.id_table	= maxim_thermocouple_id,
282 };
283 module_spi_driver(maxim_thermocouple_driver);
284 
285 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
286 MODULE_DESCRIPTION("Maxim thermocouple sensors");
287 MODULE_LICENSE("GPL");
288