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