1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
4 * driver
5 *
6 * Copyright 2019 Analog Devices Inc.
7 */
8#include <linux/bitfield.h>
9#include <linux/completion.h>
10#include <linux/device.h>
11#include <linux/kernel.h>
12#include <linux/iio/iio.h>
13#include <linux/interrupt.h>
14#include <linux/list.h>
15#include <linux/module.h>
16#include <linux/of_gpio.h>
17#include <linux/regmap.h>
18#include <linux/spi/spi.h>
19
20/* register map */
21#define LTC2983_STATUS_REG			0x0000
22#define LTC2983_TEMP_RES_START_REG		0x0010
23#define LTC2983_TEMP_RES_END_REG		0x005F
24#define LTC2983_GLOBAL_CONFIG_REG		0x00F0
25#define LTC2983_MULT_CHANNEL_START_REG		0x00F4
26#define LTC2983_MULT_CHANNEL_END_REG		0x00F7
27#define LTC2983_MUX_CONFIG_REG			0x00FF
28#define LTC2983_CHAN_ASSIGN_START_REG		0x0200
29#define LTC2983_CHAN_ASSIGN_END_REG		0x024F
30#define LTC2983_CUST_SENS_TBL_START_REG		0x0250
31#define LTC2983_CUST_SENS_TBL_END_REG		0x03CF
32
33#define LTC2983_DIFFERENTIAL_CHAN_MIN		2
34#define LTC2983_MAX_CHANNELS_NR			20
35#define LTC2983_MIN_CHANNELS_NR			1
36#define LTC2983_SLEEP				0x97
37#define LTC2983_CUSTOM_STEINHART_SIZE		24
38#define LTC2983_CUSTOM_SENSOR_ENTRY_SZ		6
39#define LTC2983_CUSTOM_STEINHART_ENTRY_SZ	4
40
41#define LTC2983_CHAN_START_ADDR(chan) \
42			(((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
43#define LTC2983_CHAN_RES_ADDR(chan) \
44			(((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
45#define LTC2983_THERMOCOUPLE_DIFF_MASK		BIT(3)
46#define LTC2983_THERMOCOUPLE_SGL(x) \
47				FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
48#define LTC2983_THERMOCOUPLE_OC_CURR_MASK	GENMASK(1, 0)
49#define LTC2983_THERMOCOUPLE_OC_CURR(x) \
50				FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
51#define LTC2983_THERMOCOUPLE_OC_CHECK_MASK	BIT(2)
52#define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
53			FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
54
55#define LTC2983_THERMISTOR_DIFF_MASK		BIT(2)
56#define LTC2983_THERMISTOR_SGL(x) \
57				FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
58#define LTC2983_THERMISTOR_R_SHARE_MASK		BIT(1)
59#define LTC2983_THERMISTOR_R_SHARE(x) \
60				FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
61#define LTC2983_THERMISTOR_C_ROTATE_MASK	BIT(0)
62#define LTC2983_THERMISTOR_C_ROTATE(x) \
63				FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
64
65#define LTC2983_DIODE_DIFF_MASK			BIT(2)
66#define LTC2983_DIODE_SGL(x) \
67			FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
68#define LTC2983_DIODE_3_CONV_CYCLE_MASK		BIT(1)
69#define LTC2983_DIODE_3_CONV_CYCLE(x) \
70				FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
71#define LTC2983_DIODE_AVERAGE_ON_MASK		BIT(0)
72#define LTC2983_DIODE_AVERAGE_ON(x) \
73				FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
74
75#define LTC2983_RTD_4_WIRE_MASK			BIT(3)
76#define LTC2983_RTD_ROTATION_MASK		BIT(1)
77#define LTC2983_RTD_C_ROTATE(x) \
78			FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
79#define LTC2983_RTD_KELVIN_R_SENSE_MASK		GENMASK(3, 2)
80#define LTC2983_RTD_N_WIRES_MASK		GENMASK(3, 2)
81#define LTC2983_RTD_N_WIRES(x) \
82			FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
83#define LTC2983_RTD_R_SHARE_MASK		BIT(0)
84#define LTC2983_RTD_R_SHARE(x) \
85			FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
86
87#define LTC2983_COMMON_HARD_FAULT_MASK	GENMASK(31, 30)
88#define LTC2983_COMMON_SOFT_FAULT_MASK	GENMASK(27, 25)
89
90#define	LTC2983_STATUS_START_MASK	BIT(7)
91#define	LTC2983_STATUS_START(x)		FIELD_PREP(LTC2983_STATUS_START_MASK, x)
92#define	LTC2983_STATUS_UP_MASK		GENMASK(7, 6)
93#define	LTC2983_STATUS_UP(reg)		FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
94
95#define	LTC2983_STATUS_CHAN_SEL_MASK	GENMASK(4, 0)
96#define	LTC2983_STATUS_CHAN_SEL(x) \
97				FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
98
99#define LTC2983_TEMP_UNITS_MASK		BIT(2)
100#define LTC2983_TEMP_UNITS(x)		FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
101
102#define LTC2983_NOTCH_FREQ_MASK		GENMASK(1, 0)
103#define LTC2983_NOTCH_FREQ(x)		FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
104
105#define LTC2983_RES_VALID_MASK		BIT(24)
106#define LTC2983_DATA_MASK		GENMASK(23, 0)
107#define LTC2983_DATA_SIGN_BIT		23
108
109#define LTC2983_CHAN_TYPE_MASK		GENMASK(31, 27)
110#define LTC2983_CHAN_TYPE(x)		FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
111
112/* cold junction for thermocouples and rsense for rtd's and thermistor's */
113#define LTC2983_CHAN_ASSIGN_MASK	GENMASK(26, 22)
114#define LTC2983_CHAN_ASSIGN(x)		FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
115
116#define LTC2983_CUSTOM_LEN_MASK		GENMASK(5, 0)
117#define LTC2983_CUSTOM_LEN(x)		FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
118
119#define LTC2983_CUSTOM_ADDR_MASK	GENMASK(11, 6)
120#define LTC2983_CUSTOM_ADDR(x)		FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
121
122#define LTC2983_THERMOCOUPLE_CFG_MASK	GENMASK(21, 18)
123#define LTC2983_THERMOCOUPLE_CFG(x) \
124				FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
125#define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK	GENMASK(31, 29)
126#define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK	GENMASK(28, 25)
127
128#define LTC2983_RTD_CFG_MASK		GENMASK(21, 18)
129#define LTC2983_RTD_CFG(x)		FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
130#define LTC2983_RTD_EXC_CURRENT_MASK	GENMASK(17, 14)
131#define LTC2983_RTD_EXC_CURRENT(x) \
132				FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
133#define LTC2983_RTD_CURVE_MASK		GENMASK(13, 12)
134#define LTC2983_RTD_CURVE(x)		FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
135
136#define LTC2983_THERMISTOR_CFG_MASK	GENMASK(21, 19)
137#define LTC2983_THERMISTOR_CFG(x) \
138				FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
139#define LTC2983_THERMISTOR_EXC_CURRENT_MASK	GENMASK(18, 15)
140#define LTC2983_THERMISTOR_EXC_CURRENT(x) \
141			FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
142
143#define LTC2983_DIODE_CFG_MASK		GENMASK(26, 24)
144#define LTC2983_DIODE_CFG(x)		FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
145#define LTC2983_DIODE_EXC_CURRENT_MASK	GENMASK(23, 22)
146#define LTC2983_DIODE_EXC_CURRENT(x) \
147				FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
148#define LTC2983_DIODE_IDEAL_FACTOR_MASK	GENMASK(21, 0)
149#define LTC2983_DIODE_IDEAL_FACTOR(x) \
150				FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
151
152#define LTC2983_R_SENSE_VAL_MASK	GENMASK(26, 0)
153#define LTC2983_R_SENSE_VAL(x)		FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
154
155#define LTC2983_ADC_SINGLE_ENDED_MASK	BIT(26)
156#define LTC2983_ADC_SINGLE_ENDED(x) \
157				FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
158
159enum {
160	LTC2983_SENSOR_THERMOCOUPLE = 1,
161	LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
162	LTC2983_SENSOR_RTD = 10,
163	LTC2983_SENSOR_RTD_CUSTOM = 18,
164	LTC2983_SENSOR_THERMISTOR = 19,
165	LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
166	LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
167	LTC2983_SENSOR_DIODE = 28,
168	LTC2983_SENSOR_SENSE_RESISTOR = 29,
169	LTC2983_SENSOR_DIRECT_ADC = 30,
170};
171
172#define to_thermocouple(_sensor) \
173		container_of(_sensor, struct ltc2983_thermocouple, sensor)
174
175#define to_rtd(_sensor) \
176		container_of(_sensor, struct ltc2983_rtd, sensor)
177
178#define to_thermistor(_sensor) \
179		container_of(_sensor, struct ltc2983_thermistor, sensor)
180
181#define to_diode(_sensor) \
182		container_of(_sensor, struct ltc2983_diode, sensor)
183
184#define to_rsense(_sensor) \
185		container_of(_sensor, struct ltc2983_rsense, sensor)
186
187#define to_adc(_sensor) \
188		container_of(_sensor, struct ltc2983_adc, sensor)
189
190struct ltc2983_data {
191	struct regmap *regmap;
192	struct spi_device *spi;
193	struct mutex lock;
194	struct completion completion;
195	struct iio_chan_spec *iio_chan;
196	struct ltc2983_sensor **sensors;
197	u32 mux_delay_config;
198	u32 filter_notch_freq;
199	u16 custom_table_size;
200	u8 num_channels;
201	u8 iio_channels;
202	/*
203	 * DMA (thus cache coherency maintenance) requires the
204	 * transfer buffers to live in their own cache lines.
205	 * Holds the converted temperature
206	 */
207	__be32 temp ____cacheline_aligned;
208	__be32 chan_val;
209};
210
211struct ltc2983_sensor {
212	int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
213	int (*assign_chan)(struct ltc2983_data *st,
214			   const struct ltc2983_sensor *sensor);
215	/* specifies the sensor channel */
216	u32 chan;
217	/* sensor type */
218	u32 type;
219};
220
221struct ltc2983_custom_sensor {
222	/* raw table sensor data */
223	u8 *table;
224	size_t size;
225	/* address offset */
226	s8 offset;
227	bool is_steinhart;
228};
229
230struct ltc2983_thermocouple {
231	struct ltc2983_sensor sensor;
232	struct ltc2983_custom_sensor *custom;
233	u32 sensor_config;
234	u32 cold_junction_chan;
235};
236
237struct ltc2983_rtd {
238	struct ltc2983_sensor sensor;
239	struct ltc2983_custom_sensor *custom;
240	u32 sensor_config;
241	u32 r_sense_chan;
242	u32 excitation_current;
243	u32 rtd_curve;
244};
245
246struct ltc2983_thermistor {
247	struct ltc2983_sensor sensor;
248	struct ltc2983_custom_sensor *custom;
249	u32 sensor_config;
250	u32 r_sense_chan;
251	u32 excitation_current;
252};
253
254struct ltc2983_diode {
255	struct ltc2983_sensor sensor;
256	u32 sensor_config;
257	u32 excitation_current;
258	u32 ideal_factor_value;
259};
260
261struct ltc2983_rsense {
262	struct ltc2983_sensor sensor;
263	u32 r_sense_val;
264};
265
266struct ltc2983_adc {
267	struct ltc2983_sensor sensor;
268	bool single_ended;
269};
270
271/*
272 * Convert to Q format numbers. These number's are integers where
273 * the number of integer and fractional bits are specified. The resolution
274 * is given by 1/@resolution and tell us the number of fractional bits. For
275 * instance a resolution of 2^-10 means we have 10 fractional bits.
276 */
277static u32 __convert_to_raw(const u64 val, const u32 resolution)
278{
279	u64 __res = val * resolution;
280
281	/* all values are multiplied by 1000000 to remove the fraction */
282	do_div(__res, 1000000);
283
284	return __res;
285}
286
287static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
288{
289	s64 __res = -(s32)val;
290
291	__res = __convert_to_raw(__res, resolution);
292
293	return (u32)-__res;
294}
295
296static int __ltc2983_fault_handler(const struct ltc2983_data *st,
297				   const u32 result, const u32 hard_mask,
298				   const u32 soft_mask)
299{
300	const struct device *dev = &st->spi->dev;
301
302	if (result & hard_mask) {
303		dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
304		return -EIO;
305	} else if (result & soft_mask) {
306		/* just print a warning */
307		dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
308	}
309
310	return 0;
311}
312
313static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
314					const struct ltc2983_sensor *sensor,
315					u32 chan_val)
316{
317	u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
318
319	chan_val |= LTC2983_CHAN_TYPE(sensor->type);
320	dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
321		chan_val);
322	st->chan_val = cpu_to_be32(chan_val);
323	return regmap_bulk_write(st->regmap, reg, &st->chan_val,
324				 sizeof(st->chan_val));
325}
326
327static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
328					  struct ltc2983_custom_sensor *custom,
329					  u32 *chan_val)
330{
331	u32 reg;
332	u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
333		LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
334	const struct device *dev = &st->spi->dev;
335	/*
336	 * custom->size holds the raw size of the table. However, when
337	 * configuring the sensor channel, we must write the number of
338	 * entries of the table minus 1. For steinhart sensors 0 is written
339	 * since the size is constant!
340	 */
341	const u8 len = custom->is_steinhart ? 0 :
342		(custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
343	/*
344	 * Check if the offset was assigned already. It should be for steinhart
345	 * sensors. When coming from sleep, it should be assigned for all.
346	 */
347	if (custom->offset < 0) {
348		/*
349		 * This needs to be done again here because, from the moment
350		 * when this test was done (successfully) for this custom
351		 * sensor, a steinhart sensor might have been added changing
352		 * custom_table_size...
353		 */
354		if (st->custom_table_size + custom->size >
355		    (LTC2983_CUST_SENS_TBL_END_REG -
356		     LTC2983_CUST_SENS_TBL_START_REG) + 1) {
357			dev_err(dev,
358				"Not space left(%d) for new custom sensor(%zu)",
359				st->custom_table_size,
360				custom->size);
361			return -EINVAL;
362		}
363
364		custom->offset = st->custom_table_size /
365					LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
366		st->custom_table_size += custom->size;
367	}
368
369	reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
370
371	*chan_val |= LTC2983_CUSTOM_LEN(len);
372	*chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
373	dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
374		reg, custom->offset,
375		custom->size);
376	/* write custom sensor table */
377	return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
378}
379
380static struct ltc2983_custom_sensor *__ltc2983_custom_sensor_new(
381						struct ltc2983_data *st,
382						const struct device_node *np,
383						const char *propname,
384						const bool is_steinhart,
385						const u32 resolution,
386						const bool has_signed)
387{
388	struct ltc2983_custom_sensor *new_custom;
389	u8 index, n_entries, tbl = 0;
390	struct device *dev = &st->spi->dev;
391	/*
392	 * For custom steinhart, the full u32 is taken. For all the others
393	 * the MSB is discarded.
394	 */
395	const u8 n_size = is_steinhart ? 4 : 3;
396	const u8 e_size = is_steinhart ? sizeof(u32) : sizeof(u64);
397
398	n_entries = of_property_count_elems_of_size(np, propname, e_size);
399	/* n_entries must be an even number */
400	if (!n_entries || (n_entries % 2) != 0) {
401		dev_err(dev, "Number of entries either 0 or not even\n");
402		return ERR_PTR(-EINVAL);
403	}
404
405	new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
406	if (!new_custom)
407		return ERR_PTR(-ENOMEM);
408
409	new_custom->size = n_entries * n_size;
410	/* check Steinhart size */
411	if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) {
412		dev_err(dev, "Steinhart sensors size(%zu) must be 24",
413							new_custom->size);
414		return ERR_PTR(-EINVAL);
415	}
416	/* Check space on the table. */
417	if (st->custom_table_size + new_custom->size >
418	    (LTC2983_CUST_SENS_TBL_END_REG -
419	     LTC2983_CUST_SENS_TBL_START_REG) + 1) {
420		dev_err(dev, "No space left(%d) for new custom sensor(%zu)",
421				st->custom_table_size, new_custom->size);
422		return ERR_PTR(-EINVAL);
423	}
424
425	/* allocate the table */
426	new_custom->table = devm_kzalloc(dev, new_custom->size, GFP_KERNEL);
427	if (!new_custom->table)
428		return ERR_PTR(-ENOMEM);
429
430	for (index = 0; index < n_entries; index++) {
431		u64 temp = 0, j;
432		/*
433		 * Steinhart sensors are configured with raw values in the
434		 * devicetree. For the other sensors we must convert the
435		 * value to raw. The odd index's correspond to temperarures
436		 * and always have 1/1024 of resolution. Temperatures also
437		 * come in kelvin, so signed values is not possible
438		 */
439		if (!is_steinhart) {
440			of_property_read_u64_index(np, propname, index, &temp);
441
442			if ((index % 2) != 0)
443				temp = __convert_to_raw(temp, 1024);
444			else if (has_signed && (s64)temp < 0)
445				temp = __convert_to_raw_sign(temp, resolution);
446			else
447				temp = __convert_to_raw(temp, resolution);
448		} else {
449			u32 t32;
450
451			of_property_read_u32_index(np, propname, index, &t32);
452			temp = t32;
453		}
454
455		for (j = 0; j < n_size; j++)
456			new_custom->table[tbl++] =
457				temp >> (8 * (n_size - j - 1));
458	}
459
460	new_custom->is_steinhart = is_steinhart;
461	/*
462	 * This is done to first add all the steinhart sensors to the table,
463	 * in order to maximize the table usage. If we mix adding steinhart
464	 * with the other sensors, we might have to do some roundup to make
465	 * sure that sensor_addr - 0x250(start address) is a multiple of 4
466	 * (for steinhart), and a multiple of 6 for all the other sensors.
467	 * Since we have const 24 bytes for steinhart sensors and 24 is
468	 * also a multiple of 6, we guarantee that the first non-steinhart
469	 * sensor will sit in a correct address without the need of filling
470	 * addresses.
471	 */
472	if (is_steinhart) {
473		new_custom->offset = st->custom_table_size /
474					LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
475		st->custom_table_size += new_custom->size;
476	} else {
477		/* mark as unset. This is checked later on the assign phase */
478		new_custom->offset = -1;
479	}
480
481	return new_custom;
482}
483
484static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
485					      const u32 result)
486{
487	return __ltc2983_fault_handler(st, result,
488				       LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
489				       LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
490}
491
492static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
493					const u32 result)
494{
495	return __ltc2983_fault_handler(st, result,
496				       LTC2983_COMMON_HARD_FAULT_MASK,
497				       LTC2983_COMMON_SOFT_FAULT_MASK);
498}
499
500static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
501				const struct ltc2983_sensor *sensor)
502{
503	struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
504	u32 chan_val;
505
506	chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
507	chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
508
509	if (thermo->custom) {
510		int ret;
511
512		ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
513							  &chan_val);
514		if (ret)
515			return ret;
516	}
517	return __ltc2983_chan_assign_common(st, sensor, chan_val);
518}
519
520static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
521				   const struct ltc2983_sensor *sensor)
522{
523	struct ltc2983_rtd *rtd = to_rtd(sensor);
524	u32 chan_val;
525
526	chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
527	chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
528	chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
529	chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
530
531	if (rtd->custom) {
532		int ret;
533
534		ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
535							  &chan_val);
536		if (ret)
537			return ret;
538	}
539	return __ltc2983_chan_assign_common(st, sensor, chan_val);
540}
541
542static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
543					  const struct ltc2983_sensor *sensor)
544{
545	struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
546	u32 chan_val;
547
548	chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
549	chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
550	chan_val |=
551		LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
552
553	if (thermistor->custom) {
554		int ret;
555
556		ret = __ltc2983_chan_custom_sensor_assign(st,
557							  thermistor->custom,
558							  &chan_val);
559		if (ret)
560			return ret;
561	}
562	return __ltc2983_chan_assign_common(st, sensor, chan_val);
563}
564
565static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
566				     const struct ltc2983_sensor *sensor)
567{
568	struct ltc2983_diode *diode = to_diode(sensor);
569	u32 chan_val;
570
571	chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
572	chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
573	chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
574
575	return __ltc2983_chan_assign_common(st, sensor, chan_val);
576}
577
578static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
579				       const struct ltc2983_sensor *sensor)
580{
581	struct ltc2983_rsense *rsense = to_rsense(sensor);
582	u32 chan_val;
583
584	chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
585
586	return __ltc2983_chan_assign_common(st, sensor, chan_val);
587}
588
589static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
590				   const struct ltc2983_sensor *sensor)
591{
592	struct ltc2983_adc *adc = to_adc(sensor);
593	u32 chan_val;
594
595	chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
596
597	return __ltc2983_chan_assign_common(st, sensor, chan_val);
598}
599
600static struct ltc2983_sensor *ltc2983_thermocouple_new(
601					const struct device_node *child,
602					struct ltc2983_data *st,
603					const struct ltc2983_sensor *sensor)
604{
605	struct ltc2983_thermocouple *thermo;
606	struct device_node *phandle;
607	u32 oc_current;
608	int ret;
609
610	thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
611	if (!thermo)
612		return ERR_PTR(-ENOMEM);
613
614	if (of_property_read_bool(child, "adi,single-ended"))
615		thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
616
617	ret = of_property_read_u32(child, "adi,sensor-oc-current-microamp",
618				   &oc_current);
619	if (!ret) {
620		switch (oc_current) {
621		case 10:
622			thermo->sensor_config |=
623					LTC2983_THERMOCOUPLE_OC_CURR(0);
624			break;
625		case 100:
626			thermo->sensor_config |=
627					LTC2983_THERMOCOUPLE_OC_CURR(1);
628			break;
629		case 500:
630			thermo->sensor_config |=
631					LTC2983_THERMOCOUPLE_OC_CURR(2);
632			break;
633		case 1000:
634			thermo->sensor_config |=
635					LTC2983_THERMOCOUPLE_OC_CURR(3);
636			break;
637		default:
638			dev_err(&st->spi->dev,
639				"Invalid open circuit current:%u", oc_current);
640			return ERR_PTR(-EINVAL);
641		}
642
643		thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
644	}
645	/* validate channel index */
646	if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
647	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
648		dev_err(&st->spi->dev,
649			"Invalid chann:%d for differential thermocouple",
650			sensor->chan);
651		return ERR_PTR(-EINVAL);
652	}
653
654	phandle = of_parse_phandle(child, "adi,cold-junction-handle", 0);
655	if (phandle) {
656		int ret;
657
658		ret = of_property_read_u32(phandle, "reg",
659					   &thermo->cold_junction_chan);
660		if (ret) {
661			/*
662			 * This would be catched later but we can just return
663			 * the error right away.
664			 */
665			dev_err(&st->spi->dev, "Property reg must be given\n");
666			of_node_put(phandle);
667			return ERR_PTR(-EINVAL);
668		}
669	}
670
671	/* check custom sensor */
672	if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
673		const char *propname = "adi,custom-thermocouple";
674
675		thermo->custom = __ltc2983_custom_sensor_new(st, child,
676							     propname, false,
677							     16384, true);
678		if (IS_ERR(thermo->custom)) {
679			of_node_put(phandle);
680			return ERR_CAST(thermo->custom);
681		}
682	}
683
684	/* set common parameters */
685	thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
686	thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
687
688	of_node_put(phandle);
689	return &thermo->sensor;
690}
691
692static struct ltc2983_sensor *ltc2983_rtd_new(const struct device_node *child,
693					  struct ltc2983_data *st,
694					  const struct ltc2983_sensor *sensor)
695{
696	struct ltc2983_rtd *rtd;
697	int ret = 0;
698	struct device *dev = &st->spi->dev;
699	struct device_node *phandle;
700	u32 excitation_current = 0, n_wires = 0;
701
702	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
703	if (!rtd)
704		return ERR_PTR(-ENOMEM);
705
706	phandle = of_parse_phandle(child, "adi,rsense-handle", 0);
707	if (!phandle) {
708		dev_err(dev, "Property adi,rsense-handle missing or invalid");
709		return ERR_PTR(-EINVAL);
710	}
711
712	ret = of_property_read_u32(phandle, "reg", &rtd->r_sense_chan);
713	if (ret) {
714		dev_err(dev, "Property reg must be given\n");
715		goto fail;
716	}
717
718	ret = of_property_read_u32(child, "adi,number-of-wires", &n_wires);
719	if (!ret) {
720		switch (n_wires) {
721		case 2:
722			rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
723			break;
724		case 3:
725			rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
726			break;
727		case 4:
728			rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
729			break;
730		case 5:
731			/* 4 wires, Kelvin Rsense */
732			rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
733			break;
734		default:
735			dev_err(dev, "Invalid number of wires:%u\n", n_wires);
736			ret = -EINVAL;
737			goto fail;
738		}
739	}
740
741	if (of_property_read_bool(child, "adi,rsense-share")) {
742		/* Current rotation is only available with rsense sharing */
743		if (of_property_read_bool(child, "adi,current-rotate")) {
744			if (n_wires == 2 || n_wires == 3) {
745				dev_err(dev,
746					"Rotation not allowed for 2/3 Wire RTDs");
747				ret = -EINVAL;
748				goto fail;
749			}
750			rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
751		} else {
752			rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
753		}
754	}
755	/*
756	 * rtd channel indexes are a bit more complicated to validate.
757	 * For 4wire RTD with rotation, the channel selection cannot be
758	 * >=19 since the chann + 1 is used in this configuration.
759	 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
760	 * <=1 since chanel - 1 and channel - 2 are used.
761	 */
762	if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
763		/* 4-wire */
764		u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
765			max = LTC2983_MAX_CHANNELS_NR;
766
767		if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
768			max = LTC2983_MAX_CHANNELS_NR - 1;
769
770		if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
771		     == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
772		    (rtd->r_sense_chan <=  min)) {
773			/* kelvin rsense*/
774			dev_err(dev,
775				"Invalid rsense chann:%d to use in kelvin rsense",
776				rtd->r_sense_chan);
777
778			ret = -EINVAL;
779			goto fail;
780		}
781
782		if (sensor->chan < min || sensor->chan > max) {
783			dev_err(dev, "Invalid chann:%d for the rtd config",
784				sensor->chan);
785
786			ret = -EINVAL;
787			goto fail;
788		}
789	} else {
790		/* same as differential case */
791		if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
792			dev_err(&st->spi->dev,
793				"Invalid chann:%d for RTD", sensor->chan);
794
795			ret = -EINVAL;
796			goto fail;
797		}
798	}
799
800	/* check custom sensor */
801	if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
802		rtd->custom = __ltc2983_custom_sensor_new(st, child,
803							  "adi,custom-rtd",
804							  false, 2048, false);
805		if (IS_ERR(rtd->custom)) {
806			of_node_put(phandle);
807			return ERR_CAST(rtd->custom);
808		}
809	}
810
811	/* set common parameters */
812	rtd->sensor.fault_handler = ltc2983_common_fault_handler;
813	rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
814
815	ret = of_property_read_u32(child, "adi,excitation-current-microamp",
816				   &excitation_current);
817	if (ret) {
818		/* default to 5uA */
819		rtd->excitation_current = 1;
820	} else {
821		switch (excitation_current) {
822		case 5:
823			rtd->excitation_current = 0x01;
824			break;
825		case 10:
826			rtd->excitation_current = 0x02;
827			break;
828		case 25:
829			rtd->excitation_current = 0x03;
830			break;
831		case 50:
832			rtd->excitation_current = 0x04;
833			break;
834		case 100:
835			rtd->excitation_current = 0x05;
836			break;
837		case 250:
838			rtd->excitation_current = 0x06;
839			break;
840		case 500:
841			rtd->excitation_current = 0x07;
842			break;
843		case 1000:
844			rtd->excitation_current = 0x08;
845			break;
846		default:
847			dev_err(&st->spi->dev,
848				"Invalid value for excitation current(%u)",
849				excitation_current);
850			ret = -EINVAL;
851			goto fail;
852		}
853	}
854
855	of_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
856
857	of_node_put(phandle);
858	return &rtd->sensor;
859fail:
860	of_node_put(phandle);
861	return ERR_PTR(ret);
862}
863
864static struct ltc2983_sensor *ltc2983_thermistor_new(
865					const struct device_node *child,
866					struct ltc2983_data *st,
867					const struct ltc2983_sensor *sensor)
868{
869	struct ltc2983_thermistor *thermistor;
870	struct device *dev = &st->spi->dev;
871	struct device_node *phandle;
872	u32 excitation_current = 0;
873	int ret = 0;
874
875	thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
876	if (!thermistor)
877		return ERR_PTR(-ENOMEM);
878
879	phandle = of_parse_phandle(child, "adi,rsense-handle", 0);
880	if (!phandle) {
881		dev_err(dev, "Property adi,rsense-handle missing or invalid");
882		return ERR_PTR(-EINVAL);
883	}
884
885	ret = of_property_read_u32(phandle, "reg", &thermistor->r_sense_chan);
886	if (ret) {
887		dev_err(dev, "rsense channel must be configured...\n");
888		goto fail;
889	}
890
891	if (of_property_read_bool(child, "adi,single-ended")) {
892		thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
893	} else if (of_property_read_bool(child, "adi,rsense-share")) {
894		/* rotation is only possible if sharing rsense */
895		if (of_property_read_bool(child, "adi,current-rotate"))
896			thermistor->sensor_config =
897						LTC2983_THERMISTOR_C_ROTATE(1);
898		else
899			thermistor->sensor_config =
900						LTC2983_THERMISTOR_R_SHARE(1);
901	}
902	/* validate channel index */
903	if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
904	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
905		dev_err(&st->spi->dev,
906			"Invalid chann:%d for differential thermistor",
907			sensor->chan);
908		ret = -EINVAL;
909		goto fail;
910	}
911
912	/* check custom sensor */
913	if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
914		bool steinhart = false;
915		const char *propname;
916
917		if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
918			steinhart = true;
919			propname = "adi,custom-steinhart";
920		} else {
921			propname = "adi,custom-thermistor";
922		}
923
924		thermistor->custom = __ltc2983_custom_sensor_new(st, child,
925								 propname,
926								 steinhart,
927								 64, false);
928		if (IS_ERR(thermistor->custom)) {
929			of_node_put(phandle);
930			return ERR_CAST(thermistor->custom);
931		}
932	}
933	/* set common parameters */
934	thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
935	thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
936
937	ret = of_property_read_u32(child, "adi,excitation-current-nanoamp",
938				   &excitation_current);
939	if (ret) {
940		/* Auto range is not allowed for custom sensors */
941		if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
942			/* default to 1uA */
943			thermistor->excitation_current = 0x03;
944		else
945			/* default to auto-range */
946			thermistor->excitation_current = 0x0c;
947	} else {
948		switch (excitation_current) {
949		case 0:
950			/* auto range */
951			if (sensor->type >=
952			    LTC2983_SENSOR_THERMISTOR_STEINHART) {
953				dev_err(&st->spi->dev,
954					"Auto Range not allowed for custom sensors\n");
955				ret = -EINVAL;
956				goto fail;
957			}
958			thermistor->excitation_current = 0x0c;
959			break;
960		case 250:
961			thermistor->excitation_current = 0x01;
962			break;
963		case 500:
964			thermistor->excitation_current = 0x02;
965			break;
966		case 1000:
967			thermistor->excitation_current = 0x03;
968			break;
969		case 5000:
970			thermistor->excitation_current = 0x04;
971			break;
972		case 10000:
973			thermistor->excitation_current = 0x05;
974			break;
975		case 25000:
976			thermistor->excitation_current = 0x06;
977			break;
978		case 50000:
979			thermistor->excitation_current = 0x07;
980			break;
981		case 100000:
982			thermistor->excitation_current = 0x08;
983			break;
984		case 250000:
985			thermistor->excitation_current = 0x09;
986			break;
987		case 500000:
988			thermistor->excitation_current = 0x0a;
989			break;
990		case 1000000:
991			thermistor->excitation_current = 0x0b;
992			break;
993		default:
994			dev_err(&st->spi->dev,
995				"Invalid value for excitation current(%u)",
996				excitation_current);
997			ret = -EINVAL;
998			goto fail;
999		}
1000	}
1001
1002	of_node_put(phandle);
1003	return &thermistor->sensor;
1004fail:
1005	of_node_put(phandle);
1006	return ERR_PTR(ret);
1007}
1008
1009static struct ltc2983_sensor *ltc2983_diode_new(
1010					const struct device_node *child,
1011					const struct ltc2983_data *st,
1012					const struct ltc2983_sensor *sensor)
1013{
1014	struct ltc2983_diode *diode;
1015	u32 temp = 0, excitation_current = 0;
1016	int ret;
1017
1018	diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
1019	if (!diode)
1020		return ERR_PTR(-ENOMEM);
1021
1022	if (of_property_read_bool(child, "adi,single-ended"))
1023		diode->sensor_config = LTC2983_DIODE_SGL(1);
1024
1025	if (of_property_read_bool(child, "adi,three-conversion-cycles"))
1026		diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
1027
1028	if (of_property_read_bool(child, "adi,average-on"))
1029		diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
1030
1031	/* validate channel index */
1032	if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
1033	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1034		dev_err(&st->spi->dev,
1035			"Invalid chann:%d for differential thermistor",
1036			sensor->chan);
1037		return ERR_PTR(-EINVAL);
1038	}
1039	/* set common parameters */
1040	diode->sensor.fault_handler = ltc2983_common_fault_handler;
1041	diode->sensor.assign_chan = ltc2983_diode_assign_chan;
1042
1043	ret = of_property_read_u32(child, "adi,excitation-current-microamp",
1044				   &excitation_current);
1045	if (!ret) {
1046		switch (excitation_current) {
1047		case 10:
1048			diode->excitation_current = 0x00;
1049			break;
1050		case 20:
1051			diode->excitation_current = 0x01;
1052			break;
1053		case 40:
1054			diode->excitation_current = 0x02;
1055			break;
1056		case 80:
1057			diode->excitation_current = 0x03;
1058			break;
1059		default:
1060			dev_err(&st->spi->dev,
1061				"Invalid value for excitation current(%u)",
1062				excitation_current);
1063			return ERR_PTR(-EINVAL);
1064		}
1065	}
1066
1067	of_property_read_u32(child, "adi,ideal-factor-value", &temp);
1068
1069	/* 2^20 resolution */
1070	diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
1071
1072	return &diode->sensor;
1073}
1074
1075static struct ltc2983_sensor *ltc2983_r_sense_new(struct device_node *child,
1076					struct ltc2983_data *st,
1077					const struct ltc2983_sensor *sensor)
1078{
1079	struct ltc2983_rsense *rsense;
1080	int ret;
1081	u32 temp;
1082
1083	rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
1084	if (!rsense)
1085		return ERR_PTR(-ENOMEM);
1086
1087	/* validate channel index */
1088	if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1089		dev_err(&st->spi->dev, "Invalid chann:%d for r_sense",
1090			sensor->chan);
1091		return ERR_PTR(-EINVAL);
1092	}
1093
1094	ret = of_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
1095	if (ret) {
1096		dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n");
1097		return ERR_PTR(-EINVAL);
1098	}
1099	/*
1100	 * Times 1000 because we have milli-ohms and __convert_to_raw
1101	 * expects scales of 1000000 which are used for all other
1102	 * properties.
1103	 * 2^10 resolution
1104	 */
1105	rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
1106
1107	/* set common parameters */
1108	rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
1109
1110	return &rsense->sensor;
1111}
1112
1113static struct ltc2983_sensor *ltc2983_adc_new(struct device_node *child,
1114					 struct ltc2983_data *st,
1115					 const struct ltc2983_sensor *sensor)
1116{
1117	struct ltc2983_adc *adc;
1118
1119	adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
1120	if (!adc)
1121		return ERR_PTR(-ENOMEM);
1122
1123	if (of_property_read_bool(child, "adi,single-ended"))
1124		adc->single_ended = true;
1125
1126	if (!adc->single_ended &&
1127	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1128		dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n",
1129			sensor->chan);
1130		return ERR_PTR(-EINVAL);
1131	}
1132	/* set common parameters */
1133	adc->sensor.assign_chan = ltc2983_adc_assign_chan;
1134	adc->sensor.fault_handler = ltc2983_common_fault_handler;
1135
1136	return &adc->sensor;
1137}
1138
1139static int ltc2983_chan_read(struct ltc2983_data *st,
1140			const struct ltc2983_sensor *sensor, int *val)
1141{
1142	u32 start_conversion = 0;
1143	int ret;
1144	unsigned long time;
1145
1146	start_conversion = LTC2983_STATUS_START(true);
1147	start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
1148	dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
1149		sensor->chan, start_conversion);
1150	/* start conversion */
1151	ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
1152	if (ret)
1153		return ret;
1154
1155	reinit_completion(&st->completion);
1156	/*
1157	 * wait for conversion to complete.
1158	 * 300 ms should be more than enough to complete the conversion.
1159	 * Depending on the sensor configuration, there are 2/3 conversions
1160	 * cycles of 82ms.
1161	 */
1162	time = wait_for_completion_timeout(&st->completion,
1163					   msecs_to_jiffies(300));
1164	if (!time) {
1165		dev_warn(&st->spi->dev, "Conversion timed out\n");
1166		return -ETIMEDOUT;
1167	}
1168
1169	/* read the converted data */
1170	ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
1171			       &st->temp, sizeof(st->temp));
1172	if (ret)
1173		return ret;
1174
1175	*val = __be32_to_cpu(st->temp);
1176
1177	if (!(LTC2983_RES_VALID_MASK & *val)) {
1178		dev_err(&st->spi->dev, "Invalid conversion detected\n");
1179		return -EIO;
1180	}
1181
1182	ret = sensor->fault_handler(st, *val);
1183	if (ret)
1184		return ret;
1185
1186	*val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
1187	return 0;
1188}
1189
1190static int ltc2983_read_raw(struct iio_dev *indio_dev,
1191			    struct iio_chan_spec const *chan,
1192			    int *val, int *val2, long mask)
1193{
1194	struct ltc2983_data *st = iio_priv(indio_dev);
1195	int ret;
1196
1197	/* sanity check */
1198	if (chan->address >= st->num_channels) {
1199		dev_err(&st->spi->dev, "Invalid chan address:%ld",
1200			chan->address);
1201		return -EINVAL;
1202	}
1203
1204	switch (mask) {
1205	case IIO_CHAN_INFO_RAW:
1206		mutex_lock(&st->lock);
1207		ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
1208		mutex_unlock(&st->lock);
1209		return ret ?: IIO_VAL_INT;
1210	case IIO_CHAN_INFO_SCALE:
1211		switch (chan->type) {
1212		case IIO_TEMP:
1213			/* value in milli degrees */
1214			*val = 1000;
1215			/* 2^10 */
1216			*val2 = 1024;
1217			return IIO_VAL_FRACTIONAL;
1218		case IIO_VOLTAGE:
1219			/* value in millivolt */
1220			*val = 1000;
1221			/* 2^21 */
1222			*val2 = 2097152;
1223			return IIO_VAL_FRACTIONAL;
1224		default:
1225			return -EINVAL;
1226		}
1227	}
1228
1229	return -EINVAL;
1230}
1231
1232static int ltc2983_reg_access(struct iio_dev *indio_dev,
1233			      unsigned int reg,
1234			      unsigned int writeval,
1235			      unsigned int *readval)
1236{
1237	struct ltc2983_data *st = iio_priv(indio_dev);
1238
1239	if (readval)
1240		return regmap_read(st->regmap, reg, readval);
1241	else
1242		return regmap_write(st->regmap, reg, writeval);
1243}
1244
1245static irqreturn_t ltc2983_irq_handler(int irq, void *data)
1246{
1247	struct ltc2983_data *st = data;
1248
1249	complete(&st->completion);
1250	return IRQ_HANDLED;
1251}
1252
1253#define LTC2983_CHAN(__type, index, __address) ({ \
1254	struct iio_chan_spec __chan = { \
1255		.type = __type, \
1256		.indexed = 1, \
1257		.channel = index, \
1258		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1259		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1260		.address = __address, \
1261	}; \
1262	__chan; \
1263})
1264
1265static int ltc2983_parse_dt(struct ltc2983_data *st)
1266{
1267	struct device_node *child;
1268	struct device *dev = &st->spi->dev;
1269	int ret = 0, chan = 0, channel_avail_mask = 0;
1270
1271	of_property_read_u32(dev->of_node, "adi,mux-delay-config-us",
1272			     &st->mux_delay_config);
1273
1274	of_property_read_u32(dev->of_node, "adi,filter-notch-freq",
1275			     &st->filter_notch_freq);
1276
1277	st->num_channels = of_get_available_child_count(dev->of_node);
1278	st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
1279				   GFP_KERNEL);
1280	if (!st->sensors)
1281		return -ENOMEM;
1282
1283	st->iio_channels = st->num_channels;
1284	for_each_available_child_of_node(dev->of_node, child) {
1285		struct ltc2983_sensor sensor;
1286
1287		ret = of_property_read_u32(child, "reg", &sensor.chan);
1288		if (ret) {
1289			dev_err(dev, "reg property must given for child nodes\n");
1290			goto put_child;
1291		}
1292
1293		/* check if we have a valid channel */
1294		if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
1295		    sensor.chan > LTC2983_MAX_CHANNELS_NR) {
1296			ret = -EINVAL;
1297			dev_err(dev,
1298				"chan:%d must be from 1 to 20\n", sensor.chan);
1299			goto put_child;
1300		} else if (channel_avail_mask & BIT(sensor.chan)) {
1301			ret = -EINVAL;
1302			dev_err(dev, "chan:%d already in use\n", sensor.chan);
1303			goto put_child;
1304		}
1305
1306		ret = of_property_read_u32(child, "adi,sensor-type",
1307					       &sensor.type);
1308		if (ret) {
1309			dev_err(dev,
1310				"adi,sensor-type property must given for child nodes\n");
1311			goto put_child;
1312		}
1313
1314		dev_dbg(dev, "Create new sensor, type %u, chann %u",
1315								sensor.type,
1316								sensor.chan);
1317
1318		if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
1319		    sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
1320			st->sensors[chan] = ltc2983_thermocouple_new(child, st,
1321								     &sensor);
1322		} else if (sensor.type >= LTC2983_SENSOR_RTD &&
1323			   sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
1324			st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
1325		} else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
1326			   sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
1327			st->sensors[chan] = ltc2983_thermistor_new(child, st,
1328								   &sensor);
1329		} else if (sensor.type == LTC2983_SENSOR_DIODE) {
1330			st->sensors[chan] = ltc2983_diode_new(child, st,
1331							      &sensor);
1332		} else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
1333			st->sensors[chan] = ltc2983_r_sense_new(child, st,
1334								&sensor);
1335			/* don't add rsense to iio */
1336			st->iio_channels--;
1337		} else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
1338			st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
1339		} else {
1340			dev_err(dev, "Unknown sensor type %d\n", sensor.type);
1341			ret = -EINVAL;
1342			goto put_child;
1343		}
1344
1345		if (IS_ERR(st->sensors[chan])) {
1346			dev_err(dev, "Failed to create sensor %ld",
1347				PTR_ERR(st->sensors[chan]));
1348			ret = PTR_ERR(st->sensors[chan]);
1349			goto put_child;
1350		}
1351		/* set generic sensor parameters */
1352		st->sensors[chan]->chan = sensor.chan;
1353		st->sensors[chan]->type = sensor.type;
1354
1355		channel_avail_mask |= BIT(sensor.chan);
1356		chan++;
1357	}
1358
1359	return 0;
1360put_child:
1361	of_node_put(child);
1362	return ret;
1363}
1364
1365static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
1366{
1367	u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
1368	int ret;
1369
1370	/* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
1371	ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
1372				       LTC2983_STATUS_UP(status) == 1, 25000,
1373				       25000 * 10);
1374	if (ret) {
1375		dev_err(&st->spi->dev, "Device startup timed out\n");
1376		return ret;
1377	}
1378
1379	ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
1380				 LTC2983_NOTCH_FREQ_MASK,
1381				 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
1382	if (ret)
1383		return ret;
1384
1385	ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
1386			   st->mux_delay_config);
1387	if (ret)
1388		return ret;
1389
1390	for (chan = 0; chan < st->num_channels; chan++) {
1391		u32 chan_type = 0, *iio_chan;
1392
1393		ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
1394		if (ret)
1395			return ret;
1396		/*
1397		 * The assign_iio flag is necessary for when the device is
1398		 * coming out of sleep. In that case, we just need to
1399		 * re-configure the device channels.
1400		 * We also don't assign iio channels for rsense.
1401		 */
1402		if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
1403		    !assign_iio)
1404			continue;
1405
1406		/* assign iio channel */
1407		if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
1408			chan_type = IIO_TEMP;
1409			iio_chan = &iio_chan_t;
1410		} else {
1411			chan_type = IIO_VOLTAGE;
1412			iio_chan = &iio_chan_v;
1413		}
1414
1415		/*
1416		 * add chan as the iio .address so that, we can directly
1417		 * reference the sensor given the iio_chan_spec
1418		 */
1419		st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
1420						       chan);
1421	}
1422
1423	return 0;
1424}
1425
1426static const struct regmap_range ltc2983_reg_ranges[] = {
1427	regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
1428	regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
1429	regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
1430	regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
1431			 LTC2983_MULT_CHANNEL_END_REG),
1432	regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
1433	regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
1434			 LTC2983_CHAN_ASSIGN_END_REG),
1435	regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
1436			 LTC2983_CUST_SENS_TBL_END_REG),
1437};
1438
1439static const struct regmap_access_table ltc2983_reg_table = {
1440	.yes_ranges = ltc2983_reg_ranges,
1441	.n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
1442};
1443
1444/*
1445 *  The reg_bits are actually 12 but the device needs the first *complete*
1446 *  byte for the command (R/W).
1447 */
1448static const struct regmap_config ltc2983_regmap_config = {
1449	.reg_bits = 24,
1450	.val_bits = 8,
1451	.wr_table = &ltc2983_reg_table,
1452	.rd_table = &ltc2983_reg_table,
1453	.read_flag_mask = GENMASK(1, 0),
1454	.write_flag_mask = BIT(1),
1455};
1456
1457static const struct  iio_info ltc2983_iio_info = {
1458	.read_raw = ltc2983_read_raw,
1459	.debugfs_reg_access = ltc2983_reg_access,
1460};
1461
1462static int ltc2983_probe(struct spi_device *spi)
1463{
1464	struct ltc2983_data *st;
1465	struct iio_dev *indio_dev;
1466	const char *name = spi_get_device_id(spi)->name;
1467	int ret;
1468
1469	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1470	if (!indio_dev)
1471		return -ENOMEM;
1472
1473	st = iio_priv(indio_dev);
1474
1475	st->regmap = devm_regmap_init_spi(spi, &ltc2983_regmap_config);
1476	if (IS_ERR(st->regmap)) {
1477		dev_err(&spi->dev, "Failed to initialize regmap\n");
1478		return PTR_ERR(st->regmap);
1479	}
1480
1481	mutex_init(&st->lock);
1482	init_completion(&st->completion);
1483	st->spi = spi;
1484	spi_set_drvdata(spi, st);
1485
1486	ret = ltc2983_parse_dt(st);
1487	if (ret)
1488		return ret;
1489
1490	st->iio_chan = devm_kzalloc(&spi->dev,
1491				    st->iio_channels * sizeof(*st->iio_chan),
1492				    GFP_KERNEL);
1493	if (!st->iio_chan)
1494		return -ENOMEM;
1495
1496	ret = ltc2983_setup(st, true);
1497	if (ret)
1498		return ret;
1499
1500	ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
1501			       IRQF_TRIGGER_RISING, name, st);
1502	if (ret) {
1503		dev_err(&spi->dev, "failed to request an irq, %d", ret);
1504		return ret;
1505	}
1506
1507	indio_dev->name = name;
1508	indio_dev->num_channels = st->iio_channels;
1509	indio_dev->channels = st->iio_chan;
1510	indio_dev->modes = INDIO_DIRECT_MODE;
1511	indio_dev->info = &ltc2983_iio_info;
1512
1513	return devm_iio_device_register(&spi->dev, indio_dev);
1514}
1515
1516static int __maybe_unused ltc2983_resume(struct device *dev)
1517{
1518	struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1519	int dummy;
1520
1521	/* dummy read to bring the device out of sleep */
1522	regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
1523	/* we need to re-assign the channels */
1524	return ltc2983_setup(st, false);
1525}
1526
1527static int __maybe_unused ltc2983_suspend(struct device *dev)
1528{
1529	struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1530
1531	return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
1532}
1533
1534static SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend, ltc2983_resume);
1535
1536static const struct spi_device_id ltc2983_id_table[] = {
1537	{ "ltc2983" },
1538	{},
1539};
1540MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1541
1542static const struct of_device_id ltc2983_of_match[] = {
1543	{ .compatible = "adi,ltc2983" },
1544	{},
1545};
1546MODULE_DEVICE_TABLE(of, ltc2983_of_match);
1547
1548static struct spi_driver ltc2983_driver = {
1549	.driver = {
1550		.name = "ltc2983",
1551		.of_match_table = ltc2983_of_match,
1552		.pm = &ltc2983_pm_ops,
1553	},
1554	.probe = ltc2983_probe,
1555	.id_table = ltc2983_id_table,
1556};
1557
1558module_spi_driver(ltc2983_driver);
1559
1560MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1561MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1562MODULE_LICENSE("GPL");
1563