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