Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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