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