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 {
211 unsigned int max_channels_nr;
216 struct ltc2983_data {
217 const struct ltc2983_chip_info *info;
218 struct regmap *regmap;
219 struct spi_device *spi;
221 struct completion completion;
222 struct iio_chan_spec *iio_chan;
223 struct ltc2983_sensor **sensors;
224 u32 mux_delay_config;
225 u32 filter_notch_freq;
226 u16 custom_table_size;
230 * DMA (thus cache coherency maintenance) may require the
231 * transfer buffers to live in their own cache lines.
232 * Holds the converted temperature
234 __be32 temp __aligned(IIO_DMA_MINALIGN);
239 struct ltc2983_sensor {
240 int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
241 int (*assign_chan)(struct ltc2983_data *st,
242 const struct ltc2983_sensor *sensor);
243 /* specifies the sensor channel */
249 struct ltc2983_custom_sensor {
250 /* raw table sensor data */
258 struct ltc2983_thermocouple {
259 struct ltc2983_sensor sensor;
260 struct ltc2983_custom_sensor *custom;
262 u32 cold_junction_chan;
266 struct ltc2983_sensor sensor;
267 struct ltc2983_custom_sensor *custom;
270 u32 excitation_current;
274 struct ltc2983_thermistor {
275 struct ltc2983_sensor sensor;
276 struct ltc2983_custom_sensor *custom;
279 u32 excitation_current;
282 struct ltc2983_diode {
283 struct ltc2983_sensor sensor;
285 u32 excitation_current;
286 u32 ideal_factor_value;
289 struct ltc2983_rsense {
290 struct ltc2983_sensor sensor;
295 struct ltc2983_sensor sensor;
299 struct ltc2983_temp {
300 struct ltc2983_sensor sensor;
301 struct ltc2983_custom_sensor *custom;
306 * Convert to Q format numbers. These number's are integers where
307 * the number of integer and fractional bits are specified. The resolution
308 * is given by 1/@resolution and tell us the number of fractional bits. For
309 * instance a resolution of 2^-10 means we have 10 fractional bits.
311 static u32 __convert_to_raw(const u64 val, const u32 resolution)
313 u64 __res = val * resolution;
315 /* all values are multiplied by 1000000 to remove the fraction */
316 do_div(__res, 1000000);
321 static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
323 s64 __res = -(s32)val;
325 __res = __convert_to_raw(__res, resolution);
330 static int __ltc2983_fault_handler(const struct ltc2983_data *st,
331 const u32 result, const u32 hard_mask,
334 const struct device *dev = &st->spi->dev;
336 if (result & hard_mask) {
337 dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
339 } else if (result & soft_mask) {
340 /* just print a warning */
341 dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
347 static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
348 const struct ltc2983_sensor *sensor,
351 u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
353 chan_val |= LTC2983_CHAN_TYPE(sensor->type);
354 dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
356 st->chan_val = cpu_to_be32(chan_val);
357 return regmap_bulk_write(st->regmap, reg, &st->chan_val,
358 sizeof(st->chan_val));
361 static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
362 struct ltc2983_custom_sensor *custom,
366 u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
367 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
368 const struct device *dev = &st->spi->dev;
370 * custom->size holds the raw size of the table. However, when
371 * configuring the sensor channel, we must write the number of
372 * entries of the table minus 1. For steinhart sensors 0 is written
373 * since the size is constant!
375 const u8 len = custom->is_steinhart ? 0 :
376 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
378 * Check if the offset was assigned already. It should be for steinhart
379 * sensors. When coming from sleep, it should be assigned for all.
381 if (custom->offset < 0) {
383 * This needs to be done again here because, from the moment
384 * when this test was done (successfully) for this custom
385 * sensor, a steinhart sensor might have been added changing
386 * custom_table_size...
388 if (st->custom_table_size + custom->size >
389 (LTC2983_CUST_SENS_TBL_END_REG -
390 LTC2983_CUST_SENS_TBL_START_REG) + 1) {
392 "Not space left(%d) for new custom sensor(%zu)",
393 st->custom_table_size,
398 custom->offset = st->custom_table_size /
399 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
400 st->custom_table_size += custom->size;
403 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
405 *chan_val |= LTC2983_CUSTOM_LEN(len);
406 *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
407 dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
410 /* write custom sensor table */
411 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
414 static struct ltc2983_custom_sensor *
415 __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
416 const char *propname, const bool is_steinhart,
417 const u32 resolution, const bool has_signed)
419 struct ltc2983_custom_sensor *new_custom;
420 struct device *dev = &st->spi->dev;
422 * For custom steinhart, the full u32 is taken. For all the others
423 * the MSB is discarded.
425 const u8 n_size = is_steinhart ? 4 : 3;
430 n_entries = fwnode_property_count_u32(fn, propname);
432 n_entries = fwnode_property_count_u64(fn, propname);
433 /* n_entries must be an even number */
434 if (!n_entries || (n_entries % 2) != 0) {
435 dev_err(dev, "Number of entries either 0 or not even\n");
436 return ERR_PTR(-EINVAL);
439 new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
441 return ERR_PTR(-ENOMEM);
443 new_custom->size = n_entries * n_size;
444 /* check Steinhart size */
445 if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) {
446 dev_err(dev, "Steinhart sensors size(%zu) must be %u\n", new_custom->size,
447 LTC2983_CUSTOM_STEINHART_SIZE);
448 return ERR_PTR(-EINVAL);
450 /* Check space on the table. */
451 if (st->custom_table_size + new_custom->size >
452 (LTC2983_CUST_SENS_TBL_END_REG -
453 LTC2983_CUST_SENS_TBL_START_REG) + 1) {
454 dev_err(dev, "No space left(%d) for new custom sensor(%zu)",
455 st->custom_table_size, new_custom->size);
456 return ERR_PTR(-EINVAL);
459 /* allocate the table */
461 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL);
463 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL);
464 if (!new_custom->table)
465 return ERR_PTR(-ENOMEM);
468 * Steinhart sensors are configured with raw values in the firmware
469 * node. For the other sensors we must convert the value to raw.
470 * The odd index's correspond to temperatures and always have 1/1024
471 * of resolution. Temperatures also come in Kelvin, so signed values
475 ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries);
479 cpu_to_be32_array(new_custom->table, new_custom->table, n_entries);
481 ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries);
485 for (index = 0; index < n_entries; index++) {
486 u64 temp = ((u64 *)new_custom->table)[index];
488 if ((index % 2) != 0)
489 temp = __convert_to_raw(temp, 1024);
490 else if (has_signed && (s64)temp < 0)
491 temp = __convert_to_raw_sign(temp, resolution);
493 temp = __convert_to_raw(temp, resolution);
495 put_unaligned_be24(temp, new_custom->table + index * 3);
499 new_custom->is_steinhart = is_steinhart;
501 * This is done to first add all the steinhart sensors to the table,
502 * in order to maximize the table usage. If we mix adding steinhart
503 * with the other sensors, we might have to do some roundup to make
504 * sure that sensor_addr - 0x250(start address) is a multiple of 4
505 * (for steinhart), and a multiple of 6 for all the other sensors.
506 * Since we have const 24 bytes for steinhart sensors and 24 is
507 * also a multiple of 6, we guarantee that the first non-steinhart
508 * sensor will sit in a correct address without the need of filling
512 new_custom->offset = st->custom_table_size /
513 LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
514 st->custom_table_size += new_custom->size;
516 /* mark as unset. This is checked later on the assign phase */
517 new_custom->offset = -1;
523 static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
526 return __ltc2983_fault_handler(st, result,
527 LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
528 LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
531 static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
534 return __ltc2983_fault_handler(st, result,
535 LTC2983_COMMON_HARD_FAULT_MASK,
536 LTC2983_COMMON_SOFT_FAULT_MASK);
539 static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
540 const struct ltc2983_sensor *sensor)
542 struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
545 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
546 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
548 if (thermo->custom) {
551 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
556 return __ltc2983_chan_assign_common(st, sensor, chan_val);
559 static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
560 const struct ltc2983_sensor *sensor)
562 struct ltc2983_rtd *rtd = to_rtd(sensor);
565 chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
566 chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
567 chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
568 chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
573 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
578 return __ltc2983_chan_assign_common(st, sensor, chan_val);
581 static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
582 const struct ltc2983_sensor *sensor)
584 struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
587 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
588 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
590 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
592 if (thermistor->custom) {
595 ret = __ltc2983_chan_custom_sensor_assign(st,
601 return __ltc2983_chan_assign_common(st, sensor, chan_val);
604 static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
605 const struct ltc2983_sensor *sensor)
607 struct ltc2983_diode *diode = to_diode(sensor);
610 chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
611 chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
612 chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
614 return __ltc2983_chan_assign_common(st, sensor, chan_val);
617 static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
618 const struct ltc2983_sensor *sensor)
620 struct ltc2983_rsense *rsense = to_rsense(sensor);
623 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
625 return __ltc2983_chan_assign_common(st, sensor, chan_val);
628 static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
629 const struct ltc2983_sensor *sensor)
631 struct ltc2983_adc *adc = to_adc(sensor);
634 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
636 return __ltc2983_chan_assign_common(st, sensor, chan_val);
639 static int ltc2983_temp_assign_chan(struct ltc2983_data *st,
640 const struct ltc2983_sensor *sensor)
642 struct ltc2983_temp *temp = to_temp(sensor);
646 chan_val = LTC2983_ADC_SINGLE_ENDED(temp->single_ended);
648 ret = __ltc2983_chan_custom_sensor_assign(st, temp->custom, &chan_val);
652 return __ltc2983_chan_assign_common(st, sensor, chan_val);
655 static struct ltc2983_sensor *
656 ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st,
657 const struct ltc2983_sensor *sensor)
659 struct ltc2983_thermocouple *thermo;
660 struct fwnode_handle *ref;
664 thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
666 return ERR_PTR(-ENOMEM);
668 if (fwnode_property_read_bool(child, "adi,single-ended"))
669 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
671 ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current);
673 switch (oc_current) {
675 thermo->sensor_config |=
676 LTC2983_THERMOCOUPLE_OC_CURR(0);
679 thermo->sensor_config |=
680 LTC2983_THERMOCOUPLE_OC_CURR(1);
683 thermo->sensor_config |=
684 LTC2983_THERMOCOUPLE_OC_CURR(2);
687 thermo->sensor_config |=
688 LTC2983_THERMOCOUPLE_OC_CURR(3);
691 dev_err(&st->spi->dev,
692 "Invalid open circuit current:%u", oc_current);
693 return ERR_PTR(-EINVAL);
696 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
698 /* validate channel index */
699 if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
700 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
701 dev_err(&st->spi->dev,
702 "Invalid chann:%d for differential thermocouple",
704 return ERR_PTR(-EINVAL);
707 ref = fwnode_find_reference(child, "adi,cold-junction-handle", 0);
711 ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan);
714 * This would be catched later but we can just return
715 * the error right away.
717 dev_err(&st->spi->dev, "Property reg must be given\n");
722 /* check custom sensor */
723 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
724 const char *propname = "adi,custom-thermocouple";
726 thermo->custom = __ltc2983_custom_sensor_new(st, child,
729 if (IS_ERR(thermo->custom)) {
730 ret = PTR_ERR(thermo->custom);
735 /* set common parameters */
736 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
737 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
739 fwnode_handle_put(ref);
740 return &thermo->sensor;
743 fwnode_handle_put(ref);
747 static struct ltc2983_sensor *
748 ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
749 const struct ltc2983_sensor *sensor)
751 struct ltc2983_rtd *rtd;
753 struct device *dev = &st->spi->dev;
754 struct fwnode_handle *ref;
755 u32 excitation_current = 0, n_wires = 0;
757 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
759 return ERR_PTR(-ENOMEM);
761 ref = fwnode_find_reference(child, "adi,rsense-handle", 0);
763 dev_err(dev, "Property adi,rsense-handle missing or invalid");
764 return ERR_CAST(ref);
767 ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan);
769 dev_err(dev, "Property reg must be given\n");
773 ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires);
777 rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
780 rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
783 rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
786 /* 4 wires, Kelvin Rsense */
787 rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
790 dev_err(dev, "Invalid number of wires:%u\n", n_wires);
796 if (fwnode_property_read_bool(child, "adi,rsense-share")) {
797 /* Current rotation is only available with rsense sharing */
798 if (fwnode_property_read_bool(child, "adi,current-rotate")) {
799 if (n_wires == 2 || n_wires == 3) {
801 "Rotation not allowed for 2/3 Wire RTDs");
805 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
807 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
811 * rtd channel indexes are a bit more complicated to validate.
812 * For 4wire RTD with rotation, the channel selection cannot be
813 * >=19 since the chann + 1 is used in this configuration.
814 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
815 * <=1 since chanel - 1 and channel - 2 are used.
817 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
819 u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
820 max = st->info->max_channels_nr;
822 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
823 max = st->info->max_channels_nr - 1;
825 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
826 == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
827 (rtd->r_sense_chan <= min)) {
830 "Invalid rsense chann:%d to use in kelvin rsense",
837 if (sensor->chan < min || sensor->chan > max) {
838 dev_err(dev, "Invalid chann:%d for the rtd config",
845 /* same as differential case */
846 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
847 dev_err(&st->spi->dev,
848 "Invalid chann:%d for RTD", sensor->chan);
855 /* check custom sensor */
856 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
857 rtd->custom = __ltc2983_custom_sensor_new(st, child,
860 if (IS_ERR(rtd->custom)) {
861 ret = PTR_ERR(rtd->custom);
866 /* set common parameters */
867 rtd->sensor.fault_handler = ltc2983_common_fault_handler;
868 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
870 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
871 &excitation_current);
874 rtd->excitation_current = 1;
876 switch (excitation_current) {
878 rtd->excitation_current = 0x01;
881 rtd->excitation_current = 0x02;
884 rtd->excitation_current = 0x03;
887 rtd->excitation_current = 0x04;
890 rtd->excitation_current = 0x05;
893 rtd->excitation_current = 0x06;
896 rtd->excitation_current = 0x07;
899 rtd->excitation_current = 0x08;
902 dev_err(&st->spi->dev,
903 "Invalid value for excitation current(%u)",
910 fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
912 fwnode_handle_put(ref);
915 fwnode_handle_put(ref);
919 static struct ltc2983_sensor *
920 ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st,
921 const struct ltc2983_sensor *sensor)
923 struct ltc2983_thermistor *thermistor;
924 struct device *dev = &st->spi->dev;
925 struct fwnode_handle *ref;
926 u32 excitation_current = 0;
929 thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
931 return ERR_PTR(-ENOMEM);
933 ref = fwnode_find_reference(child, "adi,rsense-handle", 0);
935 dev_err(dev, "Property adi,rsense-handle missing or invalid");
936 return ERR_CAST(ref);
939 ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan);
941 dev_err(dev, "rsense channel must be configured...\n");
945 if (fwnode_property_read_bool(child, "adi,single-ended")) {
946 thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
947 } else if (fwnode_property_read_bool(child, "adi,rsense-share")) {
948 /* rotation is only possible if sharing rsense */
949 if (fwnode_property_read_bool(child, "adi,current-rotate"))
950 thermistor->sensor_config =
951 LTC2983_THERMISTOR_C_ROTATE(1);
953 thermistor->sensor_config =
954 LTC2983_THERMISTOR_R_SHARE(1);
956 /* validate channel index */
957 if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
958 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
959 dev_err(&st->spi->dev,
960 "Invalid chann:%d for differential thermistor",
966 /* check custom sensor */
967 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
968 bool steinhart = false;
969 const char *propname;
971 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
973 propname = "adi,custom-steinhart";
975 propname = "adi,custom-thermistor";
978 thermistor->custom = __ltc2983_custom_sensor_new(st, child,
982 if (IS_ERR(thermistor->custom)) {
983 ret = PTR_ERR(thermistor->custom);
987 /* set common parameters */
988 thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
989 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
991 ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
992 &excitation_current);
994 /* Auto range is not allowed for custom sensors */
995 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
997 thermistor->excitation_current = 0x03;
999 /* default to auto-range */
1000 thermistor->excitation_current = 0x0c;
1002 switch (excitation_current) {
1006 LTC2983_SENSOR_THERMISTOR_STEINHART) {
1007 dev_err(&st->spi->dev,
1008 "Auto Range not allowed for custom sensors\n");
1012 thermistor->excitation_current = 0x0c;
1015 thermistor->excitation_current = 0x01;
1018 thermistor->excitation_current = 0x02;
1021 thermistor->excitation_current = 0x03;
1024 thermistor->excitation_current = 0x04;
1027 thermistor->excitation_current = 0x05;
1030 thermistor->excitation_current = 0x06;
1033 thermistor->excitation_current = 0x07;
1036 thermistor->excitation_current = 0x08;
1039 thermistor->excitation_current = 0x09;
1042 thermistor->excitation_current = 0x0a;
1045 thermistor->excitation_current = 0x0b;
1048 dev_err(&st->spi->dev,
1049 "Invalid value for excitation current(%u)",
1050 excitation_current);
1056 fwnode_handle_put(ref);
1057 return &thermistor->sensor;
1059 fwnode_handle_put(ref);
1060 return ERR_PTR(ret);
1063 static struct ltc2983_sensor *
1064 ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st,
1065 const struct ltc2983_sensor *sensor)
1067 struct ltc2983_diode *diode;
1068 u32 temp = 0, excitation_current = 0;
1071 diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
1073 return ERR_PTR(-ENOMEM);
1075 if (fwnode_property_read_bool(child, "adi,single-ended"))
1076 diode->sensor_config = LTC2983_DIODE_SGL(1);
1078 if (fwnode_property_read_bool(child, "adi,three-conversion-cycles"))
1079 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
1081 if (fwnode_property_read_bool(child, "adi,average-on"))
1082 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
1084 /* validate channel index */
1085 if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
1086 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1087 dev_err(&st->spi->dev,
1088 "Invalid chann:%d for differential thermistor",
1090 return ERR_PTR(-EINVAL);
1092 /* set common parameters */
1093 diode->sensor.fault_handler = ltc2983_common_fault_handler;
1094 diode->sensor.assign_chan = ltc2983_diode_assign_chan;
1096 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
1097 &excitation_current);
1099 switch (excitation_current) {
1101 diode->excitation_current = 0x00;
1104 diode->excitation_current = 0x01;
1107 diode->excitation_current = 0x02;
1110 diode->excitation_current = 0x03;
1113 dev_err(&st->spi->dev,
1114 "Invalid value for excitation current(%u)",
1115 excitation_current);
1116 return ERR_PTR(-EINVAL);
1120 fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp);
1122 /* 2^20 resolution */
1123 diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
1125 return &diode->sensor;
1128 static struct ltc2983_sensor *ltc2983_r_sense_new(struct fwnode_handle *child,
1129 struct ltc2983_data *st,
1130 const struct ltc2983_sensor *sensor)
1132 struct ltc2983_rsense *rsense;
1136 rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
1138 return ERR_PTR(-ENOMEM);
1140 /* validate channel index */
1141 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1142 dev_err(&st->spi->dev, "Invalid chann:%d for r_sense",
1144 return ERR_PTR(-EINVAL);
1147 ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
1149 dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n");
1150 return ERR_PTR(-EINVAL);
1153 * Times 1000 because we have milli-ohms and __convert_to_raw
1154 * expects scales of 1000000 which are used for all other
1158 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
1160 /* set common parameters */
1161 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
1163 return &rsense->sensor;
1166 static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child,
1167 struct ltc2983_data *st,
1168 const struct ltc2983_sensor *sensor)
1170 struct ltc2983_adc *adc;
1172 adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
1174 return ERR_PTR(-ENOMEM);
1176 if (fwnode_property_read_bool(child, "adi,single-ended"))
1177 adc->single_ended = true;
1179 if (!adc->single_ended &&
1180 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1181 dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n",
1183 return ERR_PTR(-EINVAL);
1185 /* set common parameters */
1186 adc->sensor.assign_chan = ltc2983_adc_assign_chan;
1187 adc->sensor.fault_handler = ltc2983_common_fault_handler;
1189 return &adc->sensor;
1192 static struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child,
1193 struct ltc2983_data *st,
1194 const struct ltc2983_sensor *sensor)
1196 struct ltc2983_temp *temp;
1198 temp = devm_kzalloc(&st->spi->dev, sizeof(*temp), GFP_KERNEL);
1200 return ERR_PTR(-ENOMEM);
1202 if (fwnode_property_read_bool(child, "adi,single-ended"))
1203 temp->single_ended = true;
1205 if (!temp->single_ended &&
1206 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1207 dev_err(&st->spi->dev, "Invalid chan:%d for differential temp\n",
1209 return ERR_PTR(-EINVAL);
1212 temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp",
1214 if (IS_ERR(temp->custom))
1215 return ERR_CAST(temp->custom);
1217 /* set common parameters */
1218 temp->sensor.assign_chan = ltc2983_temp_assign_chan;
1219 temp->sensor.fault_handler = ltc2983_common_fault_handler;
1221 return &temp->sensor;
1224 static int ltc2983_chan_read(struct ltc2983_data *st,
1225 const struct ltc2983_sensor *sensor, int *val)
1227 u32 start_conversion = 0;
1231 start_conversion = LTC2983_STATUS_START(true);
1232 start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
1233 dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
1234 sensor->chan, start_conversion);
1235 /* start conversion */
1236 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
1240 reinit_completion(&st->completion);
1242 * wait for conversion to complete.
1243 * 300 ms should be more than enough to complete the conversion.
1244 * Depending on the sensor configuration, there are 2/3 conversions
1247 time = wait_for_completion_timeout(&st->completion,
1248 msecs_to_jiffies(300));
1250 dev_warn(&st->spi->dev, "Conversion timed out\n");
1254 /* read the converted data */
1255 ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
1256 &st->temp, sizeof(st->temp));
1260 *val = __be32_to_cpu(st->temp);
1262 if (!(LTC2983_RES_VALID_MASK & *val)) {
1263 dev_err(&st->spi->dev, "Invalid conversion detected\n");
1267 ret = sensor->fault_handler(st, *val);
1271 *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
1275 static int ltc2983_read_raw(struct iio_dev *indio_dev,
1276 struct iio_chan_spec const *chan,
1277 int *val, int *val2, long mask)
1279 struct ltc2983_data *st = iio_priv(indio_dev);
1283 if (chan->address >= st->num_channels) {
1284 dev_err(&st->spi->dev, "Invalid chan address:%ld",
1290 case IIO_CHAN_INFO_RAW:
1291 mutex_lock(&st->lock);
1292 ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
1293 mutex_unlock(&st->lock);
1294 return ret ?: IIO_VAL_INT;
1295 case IIO_CHAN_INFO_SCALE:
1296 switch (chan->type) {
1298 /* value in milli degrees */
1302 return IIO_VAL_FRACTIONAL;
1304 /* value in millivolt */
1308 return IIO_VAL_FRACTIONAL;
1317 static int ltc2983_reg_access(struct iio_dev *indio_dev,
1319 unsigned int writeval,
1320 unsigned int *readval)
1322 struct ltc2983_data *st = iio_priv(indio_dev);
1325 return regmap_read(st->regmap, reg, readval);
1327 return regmap_write(st->regmap, reg, writeval);
1330 static irqreturn_t ltc2983_irq_handler(int irq, void *data)
1332 struct ltc2983_data *st = data;
1334 complete(&st->completion);
1338 #define LTC2983_CHAN(__type, index, __address) ({ \
1339 struct iio_chan_spec __chan = { \
1343 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1344 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1345 .address = __address, \
1350 static int ltc2983_parse_fw(struct ltc2983_data *st)
1352 struct device *dev = &st->spi->dev;
1353 struct fwnode_handle *child;
1354 int ret = 0, chan = 0, channel_avail_mask = 0;
1356 device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config);
1358 device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq);
1360 st->num_channels = device_get_child_node_count(dev);
1361 if (!st->num_channels) {
1362 dev_err(&st->spi->dev, "At least one channel must be given!");
1366 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
1371 st->iio_channels = st->num_channels;
1372 device_for_each_child_node(dev, child) {
1373 struct ltc2983_sensor sensor;
1375 ret = fwnode_property_read_u32(child, "reg", &sensor.chan);
1377 dev_err(dev, "reg property must given for child nodes\n");
1381 /* check if we have a valid channel */
1382 if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
1383 sensor.chan > st->info->max_channels_nr) {
1385 dev_err(dev, "chan:%d must be from %u to %u\n", sensor.chan,
1386 LTC2983_MIN_CHANNELS_NR, st->info->max_channels_nr);
1388 } else if (channel_avail_mask & BIT(sensor.chan)) {
1390 dev_err(dev, "chan:%d already in use\n", sensor.chan);
1394 ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type);
1397 "adi,sensor-type property must given for child nodes\n");
1401 dev_dbg(dev, "Create new sensor, type %u, chann %u",
1405 if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
1406 sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
1407 st->sensors[chan] = ltc2983_thermocouple_new(child, st,
1409 } else if (sensor.type >= LTC2983_SENSOR_RTD &&
1410 sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
1411 st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
1412 } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
1413 sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
1414 st->sensors[chan] = ltc2983_thermistor_new(child, st,
1416 } else if (sensor.type == LTC2983_SENSOR_DIODE) {
1417 st->sensors[chan] = ltc2983_diode_new(child, st,
1419 } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
1420 st->sensors[chan] = ltc2983_r_sense_new(child, st,
1422 /* don't add rsense to iio */
1424 } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
1425 st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
1426 } else if (st->info->has_temp &&
1427 sensor.type == LTC2983_SENSOR_ACTIVE_TEMP) {
1428 st->sensors[chan] = ltc2983_temp_new(child, st, &sensor);
1430 dev_err(dev, "Unknown sensor type %d\n", sensor.type);
1435 if (IS_ERR(st->sensors[chan])) {
1436 dev_err(dev, "Failed to create sensor %ld",
1437 PTR_ERR(st->sensors[chan]));
1438 ret = PTR_ERR(st->sensors[chan]);
1441 /* set generic sensor parameters */
1442 st->sensors[chan]->chan = sensor.chan;
1443 st->sensors[chan]->type = sensor.type;
1445 channel_avail_mask |= BIT(sensor.chan);
1451 fwnode_handle_put(child);
1455 static int ltc2983_eeprom_cmd(struct ltc2983_data *st, unsigned int cmd,
1456 unsigned int wait_time, unsigned int status_reg,
1457 unsigned long status_fail_mask)
1463 ret = regmap_bulk_write(st->regmap, LTC2983_EEPROM_KEY_REG,
1464 &st->eeprom_key, sizeof(st->eeprom_key));
1468 reinit_completion(&st->completion);
1470 ret = regmap_write(st->regmap, LTC2983_STATUS_REG,
1471 LTC2983_STATUS_START(true) | cmd);
1475 time = wait_for_completion_timeout(&st->completion,
1476 msecs_to_jiffies(wait_time));
1478 dev_err(&st->spi->dev, "EEPROM command timed out\n");
1482 ret = regmap_read(st->regmap, status_reg, &val);
1486 if (val & status_fail_mask) {
1487 dev_err(&st->spi->dev, "EEPROM command failed: 0x%02X\n", val);
1494 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
1496 u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
1499 /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
1500 ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
1501 LTC2983_STATUS_UP(status) == 1, 25000,
1504 dev_err(&st->spi->dev, "Device startup timed out\n");
1508 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
1509 LTC2983_NOTCH_FREQ_MASK,
1510 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
1514 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
1515 st->mux_delay_config);
1519 if (st->info->has_eeprom && !assign_iio) {
1520 ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_READ_CMD,
1521 LTC2983_EEPROM_READ_TIME_MS,
1522 LTC2983_EEPROM_READ_STATUS_REG,
1523 LTC2983_EEPROM_READ_FAILURE_MASK);
1528 for (chan = 0; chan < st->num_channels; chan++) {
1529 u32 chan_type = 0, *iio_chan;
1531 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
1535 * The assign_iio flag is necessary for when the device is
1536 * coming out of sleep. In that case, we just need to
1537 * re-configure the device channels.
1538 * We also don't assign iio channels for rsense.
1540 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
1544 /* assign iio channel */
1545 if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
1546 chan_type = IIO_TEMP;
1547 iio_chan = &iio_chan_t;
1549 chan_type = IIO_VOLTAGE;
1550 iio_chan = &iio_chan_v;
1554 * add chan as the iio .address so that, we can directly
1555 * reference the sensor given the iio_chan_spec
1557 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
1564 static const struct regmap_range ltc2983_reg_ranges[] = {
1565 regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
1566 regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
1567 regmap_reg_range(LTC2983_EEPROM_KEY_REG, LTC2983_EEPROM_KEY_REG),
1568 regmap_reg_range(LTC2983_EEPROM_READ_STATUS_REG,
1569 LTC2983_EEPROM_READ_STATUS_REG),
1570 regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
1571 regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
1572 LTC2983_MULT_CHANNEL_END_REG),
1573 regmap_reg_range(LTC2986_EEPROM_STATUS_REG, LTC2986_EEPROM_STATUS_REG),
1574 regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
1575 regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
1576 LTC2983_CHAN_ASSIGN_END_REG),
1577 regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
1578 LTC2983_CUST_SENS_TBL_END_REG),
1581 static const struct regmap_access_table ltc2983_reg_table = {
1582 .yes_ranges = ltc2983_reg_ranges,
1583 .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
1587 * The reg_bits are actually 12 but the device needs the first *complete*
1588 * byte for the command (R/W).
1590 static const struct regmap_config ltc2983_regmap_config = {
1593 .wr_table = <c2983_reg_table,
1594 .rd_table = <c2983_reg_table,
1595 .read_flag_mask = GENMASK(1, 0),
1596 .write_flag_mask = BIT(1),
1599 static const struct iio_info ltc2983_iio_info = {
1600 .read_raw = ltc2983_read_raw,
1601 .debugfs_reg_access = ltc2983_reg_access,
1604 static int ltc2983_probe(struct spi_device *spi)
1606 struct ltc2983_data *st;
1607 struct iio_dev *indio_dev;
1608 struct gpio_desc *gpio;
1611 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1615 st = iio_priv(indio_dev);
1617 st->info = spi_get_device_match_data(spi);
1621 st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config);
1622 if (IS_ERR(st->regmap)) {
1623 dev_err(&spi->dev, "Failed to initialize regmap\n");
1624 return PTR_ERR(st->regmap);
1627 mutex_init(&st->lock);
1628 init_completion(&st->completion);
1630 st->eeprom_key = cpu_to_be32(LTC2983_EEPROM_KEY);
1631 spi_set_drvdata(spi, st);
1633 ret = ltc2983_parse_fw(st);
1637 gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH);
1639 return PTR_ERR(gpio);
1642 /* bring the device out of reset */
1643 usleep_range(1000, 1200);
1644 gpiod_set_value_cansleep(gpio, 0);
1647 st->iio_chan = devm_kzalloc(&spi->dev,
1648 st->iio_channels * sizeof(*st->iio_chan),
1653 ret = ltc2983_setup(st, true);
1657 ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
1658 IRQF_TRIGGER_RISING, st->info->name, st);
1660 dev_err(&spi->dev, "failed to request an irq, %d", ret);
1664 if (st->info->has_eeprom) {
1665 ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_WRITE_CMD,
1666 LTC2983_EEPROM_WRITE_TIME_MS,
1667 LTC2986_EEPROM_STATUS_REG,
1668 LTC2983_EEPROM_STATUS_FAILURE_MASK);
1673 indio_dev->name = st->info->name;
1674 indio_dev->num_channels = st->iio_channels;
1675 indio_dev->channels = st->iio_chan;
1676 indio_dev->modes = INDIO_DIRECT_MODE;
1677 indio_dev->info = <c2983_iio_info;
1679 return devm_iio_device_register(&spi->dev, indio_dev);
1682 static int ltc2983_resume(struct device *dev)
1684 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1687 /* dummy read to bring the device out of sleep */
1688 regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
1689 /* we need to re-assign the channels */
1690 return ltc2983_setup(st, false);
1693 static int ltc2983_suspend(struct device *dev)
1695 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1697 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
1700 static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend,
1703 static const struct ltc2983_chip_info ltc2983_chip_info_data = {
1705 .max_channels_nr = 20,
1708 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 = {
1716 .max_channels_nr = 10,
1721 static const struct ltc2983_chip_info ltm2985_chip_info_data = {
1723 .max_channels_nr = 10,
1728 static const struct spi_device_id ltc2983_id_table[] = {
1729 { "ltc2983", (kernel_ulong_t)<c2983_chip_info_data },
1730 { "ltc2984", (kernel_ulong_t)<c2984_chip_info_data },
1731 { "ltc2986", (kernel_ulong_t)<c2986_chip_info_data },
1732 { "ltm2985", (kernel_ulong_t)<m2985_chip_info_data },
1735 MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1737 static const struct of_device_id ltc2983_of_match[] = {
1738 { .compatible = "adi,ltc2983", .data = <c2983_chip_info_data },
1739 { .compatible = "adi,ltc2984", .data = <c2984_chip_info_data },
1740 { .compatible = "adi,ltc2986", .data = <c2986_chip_info_data },
1741 { .compatible = "adi,ltm2985", .data = <m2985_chip_info_data },
1744 MODULE_DEVICE_TABLE(of, ltc2983_of_match);
1746 static struct spi_driver ltc2983_driver = {
1749 .of_match_table = ltc2983_of_match,
1750 .pm = pm_sleep_ptr(<c2983_pm_ops),
1752 .probe = ltc2983_probe,
1753 .id_table = ltc2983_id_table,
1756 module_spi_driver(ltc2983_driver);
1758 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1759 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1760 MODULE_LICENSE("GPL");