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/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>
23 #define MAXIM_THERMOCOUPLE_DRV_NAME "maxim_thermocouple"
37 static const char maxim_tc_types
[] = {
38 'K', '?', 'K', 'J', 'N', 'S', 'T', 'E', 'R'
41 static const struct iio_chan_spec max6675_channels
[] = {
42 { /* thermocouple temperature */
45 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
) |
46 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE
),
56 IIO_CHAN_SOFT_TIMESTAMP(1),
59 static const struct iio_chan_spec max31855_channels
[] = {
60 { /* thermocouple temperature */
64 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
) |
65 BIT(IIO_CHAN_INFO_THERMOCOUPLE_TYPE
),
75 { /* cold junction temperature */
78 .channel2
= IIO_MOD_TEMP_AMBIENT
,
81 BIT(IIO_CHAN_INFO_RAW
) | BIT(IIO_CHAN_INFO_SCALE
),
91 IIO_CHAN_SOFT_TIMESTAMP(2),
94 static const unsigned long max31855_scan_masks
[] = {0x3, 0};
96 struct maxim_thermocouple_chip
{
97 const struct iio_chan_spec
*channels
;
98 const unsigned long *scan_masks
;
102 /* bit-check for valid input */
106 static const struct maxim_thermocouple_chip maxim_thermocouple_chips
[] = {
108 .channels
= max6675_channels
,
109 .num_channels
= ARRAY_SIZE(max6675_channels
),
111 .status_bit
= BIT(2),
114 .channels
= max31855_channels
,
115 .num_channels
= ARRAY_SIZE(max31855_channels
),
117 .scan_masks
= max31855_scan_masks
,
118 .status_bit
= BIT(16),
122 struct maxim_thermocouple_data
{
123 struct spi_device
*spi
;
124 const struct maxim_thermocouple_chip
*chip
;
126 u8 buffer
[16] ____cacheline_aligned
;
130 static int maxim_thermocouple_read(struct maxim_thermocouple_data
*data
,
131 struct iio_chan_spec
const *chan
, int *val
)
133 unsigned int storage_bytes
= data
->chip
->read_size
;
134 unsigned int shift
= chan
->scan_type
.shift
+ (chan
->address
* 8);
139 switch (storage_bytes
) {
141 ret
= spi_read(data
->spi
, (void *)&buf16
, storage_bytes
);
142 *val
= be16_to_cpu(buf16
);
145 ret
= spi_read(data
->spi
, (void *)&buf32
, storage_bytes
);
146 *val
= be32_to_cpu(buf32
);
155 /* check to be sure this is a valid reading */
156 if (*val
& data
->chip
->status_bit
)
159 *val
= sign_extend32(*val
>> shift
, chan
->scan_type
.realbits
- 1);
164 static irqreturn_t
maxim_thermocouple_trigger_handler(int irq
, void *private)
166 struct iio_poll_func
*pf
= private;
167 struct iio_dev
*indio_dev
= pf
->indio_dev
;
168 struct maxim_thermocouple_data
*data
= iio_priv(indio_dev
);
171 ret
= spi_read(data
->spi
, data
->buffer
, data
->chip
->read_size
);
173 iio_push_to_buffers_with_timestamp(indio_dev
, data
->buffer
,
174 iio_get_time_ns(indio_dev
));
177 iio_trigger_notify_done(indio_dev
->trig
);
182 static int maxim_thermocouple_read_raw(struct iio_dev
*indio_dev
,
183 struct iio_chan_spec
const *chan
,
184 int *val
, int *val2
, long mask
)
186 struct maxim_thermocouple_data
*data
= iio_priv(indio_dev
);
190 case IIO_CHAN_INFO_RAW
:
191 ret
= iio_device_claim_direct_mode(indio_dev
);
195 ret
= maxim_thermocouple_read(data
, chan
, val
);
196 iio_device_release_direct_mode(indio_dev
);
202 case IIO_CHAN_INFO_SCALE
:
203 switch (chan
->channel2
) {
204 case IIO_MOD_TEMP_AMBIENT
:
206 *val2
= 500000; /* 1000 * 0.0625 */
207 ret
= IIO_VAL_INT_PLUS_MICRO
;
210 *val
= 250; /* 1000 * 0.25 */
214 case IIO_CHAN_INFO_THERMOCOUPLE_TYPE
:
215 *val
= data
->tc_type
;
223 static const struct iio_info maxim_thermocouple_info
= {
224 .read_raw
= maxim_thermocouple_read_raw
,
227 static int maxim_thermocouple_probe(struct spi_device
*spi
)
229 const struct spi_device_id
*id
= spi_get_device_id(spi
);
230 struct iio_dev
*indio_dev
;
231 struct maxim_thermocouple_data
*data
;
232 const int chip_type
= (id
->driver_data
== MAX6675
) ? MAX6675
: MAX31855
;
233 const struct maxim_thermocouple_chip
*chip
=
234 &maxim_thermocouple_chips
[chip_type
];
237 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*data
));
241 indio_dev
->info
= &maxim_thermocouple_info
;
242 indio_dev
->name
= MAXIM_THERMOCOUPLE_DRV_NAME
;
243 indio_dev
->channels
= chip
->channels
;
244 indio_dev
->available_scan_masks
= chip
->scan_masks
;
245 indio_dev
->num_channels
= chip
->num_channels
;
246 indio_dev
->modes
= INDIO_DIRECT_MODE
;
248 data
= iio_priv(indio_dev
);
251 data
->tc_type
= maxim_tc_types
[id
->driver_data
];
253 ret
= devm_iio_triggered_buffer_setup(&spi
->dev
,
255 maxim_thermocouple_trigger_handler
, NULL
);
259 if (id
->driver_data
== MAX31855
)
260 dev_warn(&spi
->dev
, "generic max31855 ID is deprecated\nplease use more specific part type");
262 return devm_iio_device_register(&spi
->dev
, indio_dev
);
265 static const struct spi_device_id maxim_thermocouple_id
[] = {
266 {"max6675", MAX6675
},
267 {"max31855", MAX31855
},
268 {"max31855k", MAX31855K
},
269 {"max31855j", MAX31855J
},
270 {"max31855n", MAX31855N
},
271 {"max31855s", MAX31855S
},
272 {"max31855t", MAX31855T
},
273 {"max31855e", MAX31855E
},
274 {"max31855r", MAX31855R
},
277 MODULE_DEVICE_TABLE(spi
, maxim_thermocouple_id
);
279 static const struct of_device_id maxim_thermocouple_of_match
[] = {
280 { .compatible
= "maxim,max6675" },
281 { .compatible
= "maxim,max31855" },
282 { .compatible
= "maxim,max31855k" },
283 { .compatible
= "maxim,max31855j" },
284 { .compatible
= "maxim,max31855n" },
285 { .compatible
= "maxim,max31855s" },
286 { .compatible
= "maxim,max31855t" },
287 { .compatible
= "maxim,max31855e" },
288 { .compatible
= "maxim,max31855r" },
291 MODULE_DEVICE_TABLE(of
, maxim_thermocouple_of_match
);
293 static struct spi_driver maxim_thermocouple_driver
= {
295 .name
= MAXIM_THERMOCOUPLE_DRV_NAME
,
296 .of_match_table
= maxim_thermocouple_of_match
,
298 .probe
= maxim_thermocouple_probe
,
299 .id_table
= maxim_thermocouple_id
,
301 module_spi_driver(maxim_thermocouple_driver
);
303 MODULE_AUTHOR("Matt Ranostay <matt.ranostay@konsulko.com>");
304 MODULE_DESCRIPTION("Maxim thermocouple sensors");
305 MODULE_LICENSE("GPL");