1 // SPDX-License-Identifier: GPL-2.0
3 * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
6 * Copyright 2019 Analog Devices Inc.
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>
21 #include <asm/byteorder.h>
22 #include <asm/unaligned.h>
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
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
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)
53 #define LTC2983_EEPROM_WRITE_TIME_MS 2600
54 #define LTC2983_EEPROM_READ_TIME_MS 20
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)
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)
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)
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)
102 #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30)
103 #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25)
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)
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)
114 #define LTC2983_TEMP_UNITS_MASK BIT(2)
115 #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
117 #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0)
118 #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
120 #define LTC2983_RES_VALID_MASK BIT(24)
121 #define LTC2983_DATA_MASK GENMASK(23, 0)
122 #define LTC2983_DATA_SIGN_BIT 23
124 #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27)
125 #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
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)
131 #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0)
132 #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
134 #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6)
135 #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
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)
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)
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)
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)
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)
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)
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,
188 #define to_thermocouple(_sensor) \
189 container_of(_sensor, struct ltc2983_thermocouple, sensor)
191 #define to_rtd(_sensor) \
192 container_of(_sensor, struct ltc2983_rtd, sensor)
194 #define to_thermistor(_sensor) \
195 container_of(_sensor, struct ltc2983_thermistor, sensor)
197 #define to_diode(_sensor) \
198 container_of(_sensor, struct ltc2983_diode, sensor)
200 #define to_rsense(_sensor) \
201 container_of(_sensor, struct ltc2983_rsense, sensor)
203 #define to_adc(_sensor) \
204 container_of(_sensor, struct ltc2983_adc, sensor)
206 #define to_temp(_sensor) \
207 container_of(_sensor, struct ltc2983_temp, sensor)
209 struct ltc2983_chip_info {
210 unsigned int max_channels_nr;
215 struct ltc2983_data {
216 const struct ltc2983_chip_info *info;
217 struct regmap *regmap;
218 struct spi_device *spi;
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;
229 * DMA (thus cache coherency maintenance) may require the
230 * transfer buffers to live in their own cache lines.
231 * Holds the converted temperature
233 __be32 temp __aligned(IIO_DMA_MINALIGN);
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 */
248 struct ltc2983_custom_sensor {
249 /* raw table sensor data */
257 struct ltc2983_thermocouple {
258 struct ltc2983_sensor sensor;
259 struct ltc2983_custom_sensor *custom;
261 u32 cold_junction_chan;
265 struct ltc2983_sensor sensor;
266 struct ltc2983_custom_sensor *custom;
269 u32 excitation_current;
273 struct ltc2983_thermistor {
274 struct ltc2983_sensor sensor;
275 struct ltc2983_custom_sensor *custom;
278 u32 excitation_current;
281 struct ltc2983_diode {
282 struct ltc2983_sensor sensor;
284 u32 excitation_current;
285 u32 ideal_factor_value;
288 struct ltc2983_rsense {
289 struct ltc2983_sensor sensor;
294 struct ltc2983_sensor sensor;
298 struct ltc2983_temp {
299 struct ltc2983_sensor sensor;
300 struct ltc2983_custom_sensor *custom;
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.
310 static u32 __convert_to_raw(const u64 val, const u32 resolution)
312 u64 __res = val * resolution;
314 /* all values are multiplied by 1000000 to remove the fraction */
315 do_div(__res, 1000000);
320 static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
322 s64 __res = -(s32)val;
324 __res = __convert_to_raw(__res, resolution);
329 static int __ltc2983_fault_handler(const struct ltc2983_data *st,
330 const u32 result, const u32 hard_mask,
333 const struct device *dev = &st->spi->dev;
335 if (result & hard_mask) {
336 dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
338 } else if (result & soft_mask) {
339 /* just print a warning */
340 dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
346 static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
347 const struct ltc2983_sensor *sensor,
350 u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
352 chan_val |= LTC2983_CHAN_TYPE(sensor->type);
353 dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
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));
360 static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
361 struct ltc2983_custom_sensor *custom,
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;
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!
374 const u8 len = custom->is_steinhart ? 0 :
375 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
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.
380 if (custom->offset < 0) {
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...
387 if (st->custom_table_size + custom->size >
388 (LTC2983_CUST_SENS_TBL_END_REG -
389 LTC2983_CUST_SENS_TBL_START_REG) + 1) {
391 "Not space left(%d) for new custom sensor(%zu)",
392 st->custom_table_size,
397 custom->offset = st->custom_table_size /
398 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
399 st->custom_table_size += custom->size;
402 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
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",
409 /* write custom sensor table */
410 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
413 static struct ltc2983_custom_sensor *
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)
418 struct ltc2983_custom_sensor *new_custom;
419 struct device *dev = &st->spi->dev;
421 * For custom steinhart, the full u32 is taken. For all the others
422 * the MSB is discarded.
424 const u8 n_size = is_steinhart ? 4 : 3;
429 n_entries = fwnode_property_count_u32(fn, propname);
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);
438 new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
440 return ERR_PTR(-ENOMEM);
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);
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);
458 /* allocate the table */
460 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL);
462 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL);
463 if (!new_custom->table)
464 return ERR_PTR(-ENOMEM);
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
474 ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries);
478 cpu_to_be32_array(new_custom->table, new_custom->table, n_entries);
480 ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries);
484 for (index = 0; index < n_entries; index++) {
485 u64 temp = ((u64 *)new_custom->table)[index];
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);
492 temp = __convert_to_raw(temp, resolution);
494 put_unaligned_be24(temp, new_custom->table + index * 3);
498 new_custom->is_steinhart = is_steinhart;
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
511 new_custom->offset = st->custom_table_size /
512 LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
513 st->custom_table_size += new_custom->size;
515 /* mark as unset. This is checked later on the assign phase */
516 new_custom->offset = -1;
522 static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
525 return __ltc2983_fault_handler(st, result,
526 LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
527 LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
530 static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
533 return __ltc2983_fault_handler(st, result,
534 LTC2983_COMMON_HARD_FAULT_MASK,
535 LTC2983_COMMON_SOFT_FAULT_MASK);
538 static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
539 const struct ltc2983_sensor *sensor)
541 struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
544 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
545 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
547 if (thermo->custom) {
550 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
555 return __ltc2983_chan_assign_common(st, sensor, chan_val);
558 static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
559 const struct ltc2983_sensor *sensor)
561 struct ltc2983_rtd *rtd = to_rtd(sensor);
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);
572 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
577 return __ltc2983_chan_assign_common(st, sensor, chan_val);
580 static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
581 const struct ltc2983_sensor *sensor)
583 struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
586 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
587 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
589 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
591 if (thermistor->custom) {
594 ret = __ltc2983_chan_custom_sensor_assign(st,
600 return __ltc2983_chan_assign_common(st, sensor, chan_val);
603 static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
604 const struct ltc2983_sensor *sensor)
606 struct ltc2983_diode *diode = to_diode(sensor);
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);
613 return __ltc2983_chan_assign_common(st, sensor, chan_val);
616 static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
617 const struct ltc2983_sensor *sensor)
619 struct ltc2983_rsense *rsense = to_rsense(sensor);
622 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
624 return __ltc2983_chan_assign_common(st, sensor, chan_val);
627 static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
628 const struct ltc2983_sensor *sensor)
630 struct ltc2983_adc *adc = to_adc(sensor);
633 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
635 return __ltc2983_chan_assign_common(st, sensor, chan_val);
638 static int ltc2983_temp_assign_chan(struct ltc2983_data *st,
639 const struct ltc2983_sensor *sensor)
641 struct ltc2983_temp *temp = to_temp(sensor);
645 chan_val = LTC2983_ADC_SINGLE_ENDED(temp->single_ended);
647 ret = __ltc2983_chan_custom_sensor_assign(st, temp->custom, &chan_val);
651 return __ltc2983_chan_assign_common(st, sensor, chan_val);
654 static struct ltc2983_sensor *
655 ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st,
656 const struct ltc2983_sensor *sensor)
658 struct ltc2983_thermocouple *thermo;
659 struct fwnode_handle *ref;
663 thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
665 return ERR_PTR(-ENOMEM);
667 if (fwnode_property_read_bool(child, "adi,single-ended"))
668 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
670 ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current);
672 switch (oc_current) {
674 thermo->sensor_config |=
675 LTC2983_THERMOCOUPLE_OC_CURR(0);
678 thermo->sensor_config |=
679 LTC2983_THERMOCOUPLE_OC_CURR(1);
682 thermo->sensor_config |=
683 LTC2983_THERMOCOUPLE_OC_CURR(2);
686 thermo->sensor_config |=
687 LTC2983_THERMOCOUPLE_OC_CURR(3);
690 dev_err(&st->spi->dev,
691 "Invalid open circuit current:%u", oc_current);
692 return ERR_PTR(-EINVAL);
695 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
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",
703 return ERR_PTR(-EINVAL);
706 ref = fwnode_find_reference(child, "adi,cold-junction-handle", 0);
710 ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan);
713 * This would be catched later but we can just return
714 * the error right away.
716 dev_err(&st->spi->dev, "Property reg must be given\n");
721 /* check custom sensor */
722 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
723 const char *propname = "adi,custom-thermocouple";
725 thermo->custom = __ltc2983_custom_sensor_new(st, child,
728 if (IS_ERR(thermo->custom)) {
729 ret = PTR_ERR(thermo->custom);
734 /* set common parameters */
735 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
736 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
738 fwnode_handle_put(ref);
739 return &thermo->sensor;
742 fwnode_handle_put(ref);
746 static struct ltc2983_sensor *
747 ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
748 const struct ltc2983_sensor *sensor)
750 struct ltc2983_rtd *rtd;
752 struct device *dev = &st->spi->dev;
753 struct fwnode_handle *ref;
754 u32 excitation_current = 0, n_wires = 0;
756 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
758 return ERR_PTR(-ENOMEM);
760 ref = fwnode_find_reference(child, "adi,rsense-handle", 0);
762 dev_err(dev, "Property adi,rsense-handle missing or invalid");
763 return ERR_CAST(ref);
766 ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan);
768 dev_err(dev, "Property reg must be given\n");
772 ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires);
776 rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
779 rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
782 rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
785 /* 4 wires, Kelvin Rsense */
786 rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
789 dev_err(dev, "Invalid number of wires:%u\n", n_wires);
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) {
800 "Rotation not allowed for 2/3 Wire RTDs");
804 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
806 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
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.
816 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
818 u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
819 max = st->info->max_channels_nr;
821 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
822 max = st->info->max_channels_nr - 1;
824 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
825 == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
826 (rtd->r_sense_chan <= min)) {
829 "Invalid rsense chann:%d to use in kelvin rsense",
836 if (sensor->chan < min || sensor->chan > max) {
837 dev_err(dev, "Invalid chann:%d for the rtd config",
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);
854 /* check custom sensor */
855 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
856 rtd->custom = __ltc2983_custom_sensor_new(st, child,
859 if (IS_ERR(rtd->custom)) {
860 ret = PTR_ERR(rtd->custom);
865 /* set common parameters */
866 rtd->sensor.fault_handler = ltc2983_common_fault_handler;
867 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
869 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
870 &excitation_current);
873 rtd->excitation_current = 1;
875 switch (excitation_current) {
877 rtd->excitation_current = 0x01;
880 rtd->excitation_current = 0x02;
883 rtd->excitation_current = 0x03;
886 rtd->excitation_current = 0x04;
889 rtd->excitation_current = 0x05;
892 rtd->excitation_current = 0x06;
895 rtd->excitation_current = 0x07;
898 rtd->excitation_current = 0x08;
901 dev_err(&st->spi->dev,
902 "Invalid value for excitation current(%u)",
909 fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
911 fwnode_handle_put(ref);
914 fwnode_handle_put(ref);
918 static struct ltc2983_sensor *
919 ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st,
920 const struct ltc2983_sensor *sensor)
922 struct ltc2983_thermistor *thermistor;
923 struct device *dev = &st->spi->dev;
924 struct fwnode_handle *ref;
925 u32 excitation_current = 0;
928 thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
930 return ERR_PTR(-ENOMEM);
932 ref = fwnode_find_reference(child, "adi,rsense-handle", 0);
934 dev_err(dev, "Property adi,rsense-handle missing or invalid");
935 return ERR_CAST(ref);
938 ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan);
940 dev_err(dev, "rsense channel must be configured...\n");
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);
952 thermistor->sensor_config =
953 LTC2983_THERMISTOR_R_SHARE(1);
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",
965 /* check custom sensor */
966 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
967 bool steinhart = false;
968 const char *propname;
970 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
972 propname = "adi,custom-steinhart";
974 propname = "adi,custom-thermistor";
977 thermistor->custom = __ltc2983_custom_sensor_new(st, child,
981 if (IS_ERR(thermistor->custom)) {
982 ret = PTR_ERR(thermistor->custom);
986 /* set common parameters */
987 thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
988 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
990 ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
991 &excitation_current);
993 /* Auto range is not allowed for custom sensors */
994 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
996 thermistor->excitation_current = 0x03;
998 /* default to auto-range */
999 thermistor->excitation_current = 0x0c;
1001 switch (excitation_current) {
1005 LTC2983_SENSOR_THERMISTOR_STEINHART) {
1006 dev_err(&st->spi->dev,
1007 "Auto Range not allowed for custom sensors\n");
1011 thermistor->excitation_current = 0x0c;
1014 thermistor->excitation_current = 0x01;
1017 thermistor->excitation_current = 0x02;
1020 thermistor->excitation_current = 0x03;
1023 thermistor->excitation_current = 0x04;
1026 thermistor->excitation_current = 0x05;
1029 thermistor->excitation_current = 0x06;
1032 thermistor->excitation_current = 0x07;
1035 thermistor->excitation_current = 0x08;
1038 thermistor->excitation_current = 0x09;
1041 thermistor->excitation_current = 0x0a;
1044 thermistor->excitation_current = 0x0b;
1047 dev_err(&st->spi->dev,
1048 "Invalid value for excitation current(%u)",
1049 excitation_current);
1055 fwnode_handle_put(ref);
1056 return &thermistor->sensor;
1058 fwnode_handle_put(ref);
1059 return ERR_PTR(ret);
1062 static struct ltc2983_sensor *
1063 ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st,
1064 const struct ltc2983_sensor *sensor)
1066 struct ltc2983_diode *diode;
1067 u32 temp = 0, excitation_current = 0;
1070 diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
1072 return ERR_PTR(-ENOMEM);
1074 if (fwnode_property_read_bool(child, "adi,single-ended"))
1075 diode->sensor_config = LTC2983_DIODE_SGL(1);
1077 if (fwnode_property_read_bool(child, "adi,three-conversion-cycles"))
1078 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
1080 if (fwnode_property_read_bool(child, "adi,average-on"))
1081 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
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",
1089 return ERR_PTR(-EINVAL);
1091 /* set common parameters */
1092 diode->sensor.fault_handler = ltc2983_common_fault_handler;
1093 diode->sensor.assign_chan = ltc2983_diode_assign_chan;
1095 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
1096 &excitation_current);
1098 switch (excitation_current) {
1100 diode->excitation_current = 0x00;
1103 diode->excitation_current = 0x01;
1106 diode->excitation_current = 0x02;
1109 diode->excitation_current = 0x03;
1112 dev_err(&st->spi->dev,
1113 "Invalid value for excitation current(%u)",
1114 excitation_current);
1115 return ERR_PTR(-EINVAL);
1119 fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp);
1121 /* 2^20 resolution */
1122 diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
1124 return &diode->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)
1131 struct ltc2983_rsense *rsense;
1135 rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
1137 return ERR_PTR(-ENOMEM);
1139 /* validate channel index */
1140 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1141 dev_err(&st->spi->dev, "Invalid chann:%d for r_sense",
1143 return ERR_PTR(-EINVAL);
1146 ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
1148 dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n");
1149 return ERR_PTR(-EINVAL);
1152 * Times 1000 because we have milli-ohms and __convert_to_raw
1153 * expects scales of 1000000 which are used for all other
1157 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
1159 /* set common parameters */
1160 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
1162 return &rsense->sensor;
1165 static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child,
1166 struct ltc2983_data *st,
1167 const struct ltc2983_sensor *sensor)
1169 struct ltc2983_adc *adc;
1171 adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
1173 return ERR_PTR(-ENOMEM);
1175 if (fwnode_property_read_bool(child, "adi,single-ended"))
1176 adc->single_ended = true;
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",
1182 return ERR_PTR(-EINVAL);
1184 /* set common parameters */
1185 adc->sensor.assign_chan = ltc2983_adc_assign_chan;
1186 adc->sensor.fault_handler = ltc2983_common_fault_handler;
1188 return &adc->sensor;
1191 static struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child,
1192 struct ltc2983_data *st,
1193 const struct ltc2983_sensor *sensor)
1195 struct ltc2983_temp *temp;
1197 temp = devm_kzalloc(&st->spi->dev, sizeof(*temp), GFP_KERNEL);
1199 return ERR_PTR(-ENOMEM);
1201 if (fwnode_property_read_bool(child, "adi,single-ended"))
1202 temp->single_ended = true;
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",
1208 return ERR_PTR(-EINVAL);
1211 temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp",
1213 if (IS_ERR(temp->custom))
1214 return ERR_CAST(temp->custom);
1216 /* set common parameters */
1217 temp->sensor.assign_chan = ltc2983_temp_assign_chan;
1218 temp->sensor.fault_handler = ltc2983_common_fault_handler;
1220 return &temp->sensor;
1223 static int ltc2983_chan_read(struct ltc2983_data *st,
1224 const struct ltc2983_sensor *sensor, int *val)
1226 u32 start_conversion = 0;
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);
1239 reinit_completion(&st->completion);
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
1246 time = wait_for_completion_timeout(&st->completion,
1247 msecs_to_jiffies(300));
1249 dev_warn(&st->spi->dev, "Conversion timed out\n");
1253 /* read the converted data */
1254 ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
1255 &st->temp, sizeof(st->temp));
1259 *val = __be32_to_cpu(st->temp);
1261 if (!(LTC2983_RES_VALID_MASK & *val)) {
1262 dev_err(&st->spi->dev, "Invalid conversion detected\n");
1266 ret = sensor->fault_handler(st, *val);
1270 *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
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)
1278 struct ltc2983_data *st = iio_priv(indio_dev);
1282 if (chan->address >= st->num_channels) {
1283 dev_err(&st->spi->dev, "Invalid chan address:%ld",
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) {
1297 /* value in milli degrees */
1301 return IIO_VAL_FRACTIONAL;
1303 /* value in millivolt */
1307 return IIO_VAL_FRACTIONAL;
1316 static int ltc2983_reg_access(struct iio_dev *indio_dev,
1318 unsigned int writeval,
1319 unsigned int *readval)
1321 struct ltc2983_data *st = iio_priv(indio_dev);
1324 return regmap_read(st->regmap, reg, readval);
1326 return regmap_write(st->regmap, reg, writeval);
1329 static irqreturn_t ltc2983_irq_handler(int irq, void *data)
1331 struct ltc2983_data *st = data;
1333 complete(&st->completion);
1337 #define LTC2983_CHAN(__type, index, __address) ({ \
1338 struct iio_chan_spec __chan = { \
1342 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1343 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1344 .address = __address, \
1349 static int ltc2983_parse_dt(struct ltc2983_data *st)
1351 struct device *dev = &st->spi->dev;
1352 struct fwnode_handle *child;
1353 int ret = 0, chan = 0, channel_avail_mask = 0;
1355 device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config);
1357 device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq);
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!");
1365 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
1370 st->iio_channels = st->num_channels;
1371 device_for_each_child_node(dev, child) {
1372 struct ltc2983_sensor sensor;
1374 ret = fwnode_property_read_u32(child, "reg", &sensor.chan);
1376 dev_err(dev, "reg property must given for child nodes\n");
1380 /* check if we have a valid channel */
1381 if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
1382 sensor.chan > st->info->max_channels_nr) {
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);
1387 } else if (channel_avail_mask & BIT(sensor.chan)) {
1389 dev_err(dev, "chan:%d already in use\n", sensor.chan);
1393 ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type);
1396 "adi,sensor-type property must given for child nodes\n");
1400 dev_dbg(dev, "Create new sensor, type %u, chann %u",
1404 if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
1405 sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
1406 st->sensors[chan] = ltc2983_thermocouple_new(child, st,
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,
1415 } else if (sensor.type == LTC2983_SENSOR_DIODE) {
1416 st->sensors[chan] = ltc2983_diode_new(child, st,
1418 } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
1419 st->sensors[chan] = ltc2983_r_sense_new(child, st,
1421 /* don't add rsense to iio */
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);
1429 dev_err(dev, "Unknown sensor type %d\n", sensor.type);
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]);
1440 /* set generic sensor parameters */
1441 st->sensors[chan]->chan = sensor.chan;
1442 st->sensors[chan]->type = sensor.type;
1444 channel_avail_mask |= BIT(sensor.chan);
1450 fwnode_handle_put(child);
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)
1462 ret = regmap_bulk_write(st->regmap, LTC2983_EEPROM_KEY_REG,
1463 &st->eeprom_key, sizeof(st->eeprom_key));
1467 reinit_completion(&st->completion);
1469 ret = regmap_write(st->regmap, LTC2983_STATUS_REG,
1470 LTC2983_STATUS_START(true) | cmd);
1474 time = wait_for_completion_timeout(&st->completion,
1475 msecs_to_jiffies(wait_time));
1477 dev_err(&st->spi->dev, "EEPROM command timed out\n");
1481 ret = regmap_read(st->regmap, status_reg, &val);
1485 if (val & status_fail_mask) {
1486 dev_err(&st->spi->dev, "EEPROM command failed: 0x%02X\n", val);
1493 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
1495 u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
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,
1503 dev_err(&st->spi->dev, "Device startup timed out\n");
1507 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
1508 LTC2983_NOTCH_FREQ_MASK,
1509 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
1513 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
1514 st->mux_delay_config);
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);
1527 for (chan = 0; chan < st->num_channels; chan++) {
1528 u32 chan_type = 0, *iio_chan;
1530 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
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.
1539 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
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;
1548 chan_type = IIO_VOLTAGE;
1549 iio_chan = &iio_chan_v;
1553 * add chan as the iio .address so that, we can directly
1554 * reference the sensor given the iio_chan_spec
1556 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
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),
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),
1586 * The reg_bits are actually 12 but the device needs the first *complete*
1587 * byte for the command (R/W).
1589 static const struct regmap_config ltc2983_regmap_config = {
1592 .wr_table = <c2983_reg_table,
1593 .rd_table = <c2983_reg_table,
1594 .read_flag_mask = GENMASK(1, 0),
1595 .write_flag_mask = BIT(1),
1598 static const struct iio_info ltc2983_iio_info = {
1599 .read_raw = ltc2983_read_raw,
1600 .debugfs_reg_access = ltc2983_reg_access,
1603 static int ltc2983_probe(struct spi_device *spi)
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;
1611 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1615 st = iio_priv(indio_dev);
1617 st->info = device_get_match_data(&spi->dev);
1619 st->info = (void *)spi_get_device_id(spi)->driver_data;
1623 st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config);
1624 if (IS_ERR(st->regmap)) {
1625 dev_err(&spi->dev, "Failed to initialize regmap\n");
1626 return PTR_ERR(st->regmap);
1629 mutex_init(&st->lock);
1630 init_completion(&st->completion);
1632 st->eeprom_key = cpu_to_be32(LTC2983_EEPROM_KEY);
1633 spi_set_drvdata(spi, st);
1635 ret = ltc2983_parse_dt(st);
1639 gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH);
1641 return PTR_ERR(gpio);
1644 /* bring the device out of reset */
1645 usleep_range(1000, 1200);
1646 gpiod_set_value_cansleep(gpio, 0);
1649 st->iio_chan = devm_kzalloc(&spi->dev,
1650 st->iio_channels * sizeof(*st->iio_chan),
1655 ret = ltc2983_setup(st, true);
1659 ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
1660 IRQF_TRIGGER_RISING, name, st);
1662 dev_err(&spi->dev, "failed to request an irq, %d", ret);
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);
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 = <c2983_iio_info;
1681 return devm_iio_device_register(&spi->dev, indio_dev);
1684 static int ltc2983_resume(struct device *dev)
1686 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
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);
1695 static int ltc2983_suspend(struct device *dev)
1697 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1699 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
1702 static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend,
1705 static const struct ltc2983_chip_info ltc2983_chip_info_data = {
1706 .max_channels_nr = 20,
1709 static const struct ltc2983_chip_info ltc2984_chip_info_data = {
1710 .max_channels_nr = 20,
1714 static const struct ltc2983_chip_info ltc2986_chip_info_data = {
1715 .max_channels_nr = 10,
1720 static const struct spi_device_id ltc2983_id_table[] = {
1721 { "ltc2983", (kernel_ulong_t)<c2983_chip_info_data },
1722 { "ltc2984", (kernel_ulong_t)<c2984_chip_info_data },
1723 { "ltc2986", (kernel_ulong_t)<c2986_chip_info_data },
1724 { "ltm2985", (kernel_ulong_t)<c2986_chip_info_data },
1727 MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1729 static const struct of_device_id ltc2983_of_match[] = {
1730 { .compatible = "adi,ltc2983", .data = <c2983_chip_info_data },
1731 { .compatible = "adi,ltc2984", .data = <c2984_chip_info_data },
1732 { .compatible = "adi,ltc2986", .data = <c2986_chip_info_data },
1733 { .compatible = "adi,ltm2985", .data = <c2986_chip_info_data },
1736 MODULE_DEVICE_TABLE(of, ltc2983_of_match);
1738 static struct spi_driver ltc2983_driver = {
1741 .of_match_table = ltc2983_of_match,
1742 .pm = pm_sleep_ptr(<c2983_pm_ops),
1744 .probe = ltc2983_probe,
1745 .id_table = ltc2983_id_table,
1748 module_spi_driver(ltc2983_driver);
1750 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1751 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1752 MODULE_LICENSE("GPL");