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_GLOBAL_CONFIG_REG 0x00F0
29 #define LTC2983_MULT_CHANNEL_START_REG 0x00F4
30 #define LTC2983_MULT_CHANNEL_END_REG 0x00F7
31 #define LTC2983_MUX_CONFIG_REG 0x00FF
32 #define LTC2983_CHAN_ASSIGN_START_REG 0x0200
33 #define LTC2983_CHAN_ASSIGN_END_REG 0x024F
34 #define LTC2983_CUST_SENS_TBL_START_REG 0x0250
35 #define LTC2983_CUST_SENS_TBL_END_REG 0x03CF
37 #define LTC2983_DIFFERENTIAL_CHAN_MIN 2
38 #define LTC2983_MAX_CHANNELS_NR 20
39 #define LTC2983_MIN_CHANNELS_NR 1
40 #define LTC2983_SLEEP 0x97
41 #define LTC2983_CUSTOM_STEINHART_SIZE 24
42 #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6
43 #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4
45 #define LTC2983_CHAN_START_ADDR(chan) \
46 (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
47 #define LTC2983_CHAN_RES_ADDR(chan) \
48 (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
49 #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3)
50 #define LTC2983_THERMOCOUPLE_SGL(x) \
51 FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
52 #define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0)
53 #define LTC2983_THERMOCOUPLE_OC_CURR(x) \
54 FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
55 #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2)
56 #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
57 FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
59 #define LTC2983_THERMISTOR_DIFF_MASK BIT(2)
60 #define LTC2983_THERMISTOR_SGL(x) \
61 FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
62 #define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1)
63 #define LTC2983_THERMISTOR_R_SHARE(x) \
64 FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
65 #define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0)
66 #define LTC2983_THERMISTOR_C_ROTATE(x) \
67 FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
69 #define LTC2983_DIODE_DIFF_MASK BIT(2)
70 #define LTC2983_DIODE_SGL(x) \
71 FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
72 #define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1)
73 #define LTC2983_DIODE_3_CONV_CYCLE(x) \
74 FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
75 #define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0)
76 #define LTC2983_DIODE_AVERAGE_ON(x) \
77 FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
79 #define LTC2983_RTD_4_WIRE_MASK BIT(3)
80 #define LTC2983_RTD_ROTATION_MASK BIT(1)
81 #define LTC2983_RTD_C_ROTATE(x) \
82 FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
83 #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2)
84 #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2)
85 #define LTC2983_RTD_N_WIRES(x) \
86 FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
87 #define LTC2983_RTD_R_SHARE_MASK BIT(0)
88 #define LTC2983_RTD_R_SHARE(x) \
89 FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
91 #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30)
92 #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25)
94 #define LTC2983_STATUS_START_MASK BIT(7)
95 #define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x)
96 #define LTC2983_STATUS_UP_MASK GENMASK(7, 6)
97 #define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
99 #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0)
100 #define LTC2983_STATUS_CHAN_SEL(x) \
101 FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
103 #define LTC2983_TEMP_UNITS_MASK BIT(2)
104 #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
106 #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0)
107 #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
109 #define LTC2983_RES_VALID_MASK BIT(24)
110 #define LTC2983_DATA_MASK GENMASK(23, 0)
111 #define LTC2983_DATA_SIGN_BIT 23
113 #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27)
114 #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
116 /* cold junction for thermocouples and rsense for rtd's and thermistor's */
117 #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22)
118 #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
120 #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0)
121 #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
123 #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6)
124 #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
126 #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18)
127 #define LTC2983_THERMOCOUPLE_CFG(x) \
128 FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
129 #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29)
130 #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25)
132 #define LTC2983_RTD_CFG_MASK GENMASK(21, 18)
133 #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
134 #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14)
135 #define LTC2983_RTD_EXC_CURRENT(x) \
136 FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
137 #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12)
138 #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
140 #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19)
141 #define LTC2983_THERMISTOR_CFG(x) \
142 FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
143 #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15)
144 #define LTC2983_THERMISTOR_EXC_CURRENT(x) \
145 FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
147 #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24)
148 #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
149 #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22)
150 #define LTC2983_DIODE_EXC_CURRENT(x) \
151 FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
152 #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0)
153 #define LTC2983_DIODE_IDEAL_FACTOR(x) \
154 FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
156 #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0)
157 #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
159 #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26)
160 #define LTC2983_ADC_SINGLE_ENDED(x) \
161 FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
164 LTC2983_SENSOR_THERMOCOUPLE = 1,
165 LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
166 LTC2983_SENSOR_RTD = 10,
167 LTC2983_SENSOR_RTD_CUSTOM = 18,
168 LTC2983_SENSOR_THERMISTOR = 19,
169 LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
170 LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
171 LTC2983_SENSOR_DIODE = 28,
172 LTC2983_SENSOR_SENSE_RESISTOR = 29,
173 LTC2983_SENSOR_DIRECT_ADC = 30,
176 #define to_thermocouple(_sensor) \
177 container_of(_sensor, struct ltc2983_thermocouple, sensor)
179 #define to_rtd(_sensor) \
180 container_of(_sensor, struct ltc2983_rtd, sensor)
182 #define to_thermistor(_sensor) \
183 container_of(_sensor, struct ltc2983_thermistor, sensor)
185 #define to_diode(_sensor) \
186 container_of(_sensor, struct ltc2983_diode, sensor)
188 #define to_rsense(_sensor) \
189 container_of(_sensor, struct ltc2983_rsense, sensor)
191 #define to_adc(_sensor) \
192 container_of(_sensor, struct ltc2983_adc, sensor)
194 struct ltc2983_data {
195 struct regmap *regmap;
196 struct spi_device *spi;
198 struct completion completion;
199 struct iio_chan_spec *iio_chan;
200 struct ltc2983_sensor **sensors;
201 u32 mux_delay_config;
202 u32 filter_notch_freq;
203 u16 custom_table_size;
207 * DMA (thus cache coherency maintenance) may require the
208 * transfer buffers to live in their own cache lines.
209 * Holds the converted temperature
211 __be32 temp __aligned(IIO_DMA_MINALIGN);
215 struct ltc2983_sensor {
216 int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
217 int (*assign_chan)(struct ltc2983_data *st,
218 const struct ltc2983_sensor *sensor);
219 /* specifies the sensor channel */
225 struct ltc2983_custom_sensor {
226 /* raw table sensor data */
234 struct ltc2983_thermocouple {
235 struct ltc2983_sensor sensor;
236 struct ltc2983_custom_sensor *custom;
238 u32 cold_junction_chan;
242 struct ltc2983_sensor sensor;
243 struct ltc2983_custom_sensor *custom;
246 u32 excitation_current;
250 struct ltc2983_thermistor {
251 struct ltc2983_sensor sensor;
252 struct ltc2983_custom_sensor *custom;
255 u32 excitation_current;
258 struct ltc2983_diode {
259 struct ltc2983_sensor sensor;
261 u32 excitation_current;
262 u32 ideal_factor_value;
265 struct ltc2983_rsense {
266 struct ltc2983_sensor sensor;
271 struct ltc2983_sensor sensor;
276 * Convert to Q format numbers. These number's are integers where
277 * the number of integer and fractional bits are specified. The resolution
278 * is given by 1/@resolution and tell us the number of fractional bits. For
279 * instance a resolution of 2^-10 means we have 10 fractional bits.
281 static u32 __convert_to_raw(const u64 val, const u32 resolution)
283 u64 __res = val * resolution;
285 /* all values are multiplied by 1000000 to remove the fraction */
286 do_div(__res, 1000000);
291 static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
293 s64 __res = -(s32)val;
295 __res = __convert_to_raw(__res, resolution);
300 static int __ltc2983_fault_handler(const struct ltc2983_data *st,
301 const u32 result, const u32 hard_mask,
304 const struct device *dev = &st->spi->dev;
306 if (result & hard_mask) {
307 dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
309 } else if (result & soft_mask) {
310 /* just print a warning */
311 dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
317 static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
318 const struct ltc2983_sensor *sensor,
321 u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
323 chan_val |= LTC2983_CHAN_TYPE(sensor->type);
324 dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
326 st->chan_val = cpu_to_be32(chan_val);
327 return regmap_bulk_write(st->regmap, reg, &st->chan_val,
328 sizeof(st->chan_val));
331 static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
332 struct ltc2983_custom_sensor *custom,
336 u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
337 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
338 const struct device *dev = &st->spi->dev;
340 * custom->size holds the raw size of the table. However, when
341 * configuring the sensor channel, we must write the number of
342 * entries of the table minus 1. For steinhart sensors 0 is written
343 * since the size is constant!
345 const u8 len = custom->is_steinhart ? 0 :
346 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
348 * Check if the offset was assigned already. It should be for steinhart
349 * sensors. When coming from sleep, it should be assigned for all.
351 if (custom->offset < 0) {
353 * This needs to be done again here because, from the moment
354 * when this test was done (successfully) for this custom
355 * sensor, a steinhart sensor might have been added changing
356 * custom_table_size...
358 if (st->custom_table_size + custom->size >
359 (LTC2983_CUST_SENS_TBL_END_REG -
360 LTC2983_CUST_SENS_TBL_START_REG) + 1) {
362 "Not space left(%d) for new custom sensor(%zu)",
363 st->custom_table_size,
368 custom->offset = st->custom_table_size /
369 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
370 st->custom_table_size += custom->size;
373 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
375 *chan_val |= LTC2983_CUSTOM_LEN(len);
376 *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
377 dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
380 /* write custom sensor table */
381 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
384 static struct ltc2983_custom_sensor *
385 __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
386 const char *propname, const bool is_steinhart,
387 const u32 resolution, const bool has_signed)
389 struct ltc2983_custom_sensor *new_custom;
390 struct device *dev = &st->spi->dev;
392 * For custom steinhart, the full u32 is taken. For all the others
393 * the MSB is discarded.
395 const u8 n_size = is_steinhart ? 4 : 3;
400 n_entries = fwnode_property_count_u32(fn, propname);
402 n_entries = fwnode_property_count_u64(fn, propname);
403 /* n_entries must be an even number */
404 if (!n_entries || (n_entries % 2) != 0) {
405 dev_err(dev, "Number of entries either 0 or not even\n");
406 return ERR_PTR(-EINVAL);
409 new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
411 return ERR_PTR(-ENOMEM);
413 new_custom->size = n_entries * n_size;
414 /* check Steinhart size */
415 if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) {
416 dev_err(dev, "Steinhart sensors size(%zu) must be %u\n", new_custom->size,
417 LTC2983_CUSTOM_STEINHART_SIZE);
418 return ERR_PTR(-EINVAL);
420 /* Check space on the table. */
421 if (st->custom_table_size + new_custom->size >
422 (LTC2983_CUST_SENS_TBL_END_REG -
423 LTC2983_CUST_SENS_TBL_START_REG) + 1) {
424 dev_err(dev, "No space left(%d) for new custom sensor(%zu)",
425 st->custom_table_size, new_custom->size);
426 return ERR_PTR(-EINVAL);
429 /* allocate the table */
431 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL);
433 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL);
434 if (!new_custom->table)
435 return ERR_PTR(-ENOMEM);
438 * Steinhart sensors are configured with raw values in the firmware
439 * node. For the other sensors we must convert the value to raw.
440 * The odd index's correspond to temperatures and always have 1/1024
441 * of resolution. Temperatures also come in Kelvin, so signed values
445 ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries);
449 cpu_to_be32_array(new_custom->table, new_custom->table, n_entries);
451 ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries);
455 for (index = 0; index < n_entries; index++) {
456 u64 temp = ((u64 *)new_custom->table)[index];
458 if ((index % 2) != 0)
459 temp = __convert_to_raw(temp, 1024);
460 else if (has_signed && (s64)temp < 0)
461 temp = __convert_to_raw_sign(temp, resolution);
463 temp = __convert_to_raw(temp, resolution);
465 put_unaligned_be24(temp, new_custom->table + index * 3);
469 new_custom->is_steinhart = is_steinhart;
471 * This is done to first add all the steinhart sensors to the table,
472 * in order to maximize the table usage. If we mix adding steinhart
473 * with the other sensors, we might have to do some roundup to make
474 * sure that sensor_addr - 0x250(start address) is a multiple of 4
475 * (for steinhart), and a multiple of 6 for all the other sensors.
476 * Since we have const 24 bytes for steinhart sensors and 24 is
477 * also a multiple of 6, we guarantee that the first non-steinhart
478 * sensor will sit in a correct address without the need of filling
482 new_custom->offset = st->custom_table_size /
483 LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
484 st->custom_table_size += new_custom->size;
486 /* mark as unset. This is checked later on the assign phase */
487 new_custom->offset = -1;
493 static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
496 return __ltc2983_fault_handler(st, result,
497 LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
498 LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
501 static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
504 return __ltc2983_fault_handler(st, result,
505 LTC2983_COMMON_HARD_FAULT_MASK,
506 LTC2983_COMMON_SOFT_FAULT_MASK);
509 static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
510 const struct ltc2983_sensor *sensor)
512 struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
515 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
516 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
518 if (thermo->custom) {
521 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
526 return __ltc2983_chan_assign_common(st, sensor, chan_val);
529 static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
530 const struct ltc2983_sensor *sensor)
532 struct ltc2983_rtd *rtd = to_rtd(sensor);
535 chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
536 chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
537 chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
538 chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
543 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
548 return __ltc2983_chan_assign_common(st, sensor, chan_val);
551 static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
552 const struct ltc2983_sensor *sensor)
554 struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
557 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
558 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
560 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
562 if (thermistor->custom) {
565 ret = __ltc2983_chan_custom_sensor_assign(st,
571 return __ltc2983_chan_assign_common(st, sensor, chan_val);
574 static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
575 const struct ltc2983_sensor *sensor)
577 struct ltc2983_diode *diode = to_diode(sensor);
580 chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
581 chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
582 chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
584 return __ltc2983_chan_assign_common(st, sensor, chan_val);
587 static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
588 const struct ltc2983_sensor *sensor)
590 struct ltc2983_rsense *rsense = to_rsense(sensor);
593 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
595 return __ltc2983_chan_assign_common(st, sensor, chan_val);
598 static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
599 const struct ltc2983_sensor *sensor)
601 struct ltc2983_adc *adc = to_adc(sensor);
604 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
606 return __ltc2983_chan_assign_common(st, sensor, chan_val);
609 static struct ltc2983_sensor *
610 ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st,
611 const struct ltc2983_sensor *sensor)
613 struct ltc2983_thermocouple *thermo;
614 struct fwnode_handle *ref;
618 thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
620 return ERR_PTR(-ENOMEM);
622 if (fwnode_property_read_bool(child, "adi,single-ended"))
623 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
625 ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current);
627 switch (oc_current) {
629 thermo->sensor_config |=
630 LTC2983_THERMOCOUPLE_OC_CURR(0);
633 thermo->sensor_config |=
634 LTC2983_THERMOCOUPLE_OC_CURR(1);
637 thermo->sensor_config |=
638 LTC2983_THERMOCOUPLE_OC_CURR(2);
641 thermo->sensor_config |=
642 LTC2983_THERMOCOUPLE_OC_CURR(3);
645 dev_err(&st->spi->dev,
646 "Invalid open circuit current:%u", oc_current);
647 return ERR_PTR(-EINVAL);
650 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
652 /* validate channel index */
653 if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
654 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
655 dev_err(&st->spi->dev,
656 "Invalid chann:%d for differential thermocouple",
658 return ERR_PTR(-EINVAL);
661 ref = fwnode_find_reference(child, "adi,cold-junction-handle", 0);
665 ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan);
668 * This would be catched later but we can just return
669 * the error right away.
671 dev_err(&st->spi->dev, "Property reg must be given\n");
676 /* check custom sensor */
677 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
678 const char *propname = "adi,custom-thermocouple";
680 thermo->custom = __ltc2983_custom_sensor_new(st, child,
683 if (IS_ERR(thermo->custom)) {
684 ret = PTR_ERR(thermo->custom);
689 /* set common parameters */
690 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
691 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
693 fwnode_handle_put(ref);
694 return &thermo->sensor;
697 fwnode_handle_put(ref);
701 static struct ltc2983_sensor *
702 ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
703 const struct ltc2983_sensor *sensor)
705 struct ltc2983_rtd *rtd;
707 struct device *dev = &st->spi->dev;
708 struct fwnode_handle *ref;
709 u32 excitation_current = 0, n_wires = 0;
711 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
713 return ERR_PTR(-ENOMEM);
715 ref = fwnode_find_reference(child, "adi,rsense-handle", 0);
717 dev_err(dev, "Property adi,rsense-handle missing or invalid");
718 return ERR_CAST(ref);
721 ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan);
723 dev_err(dev, "Property reg must be given\n");
727 ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires);
731 rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
734 rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
737 rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
740 /* 4 wires, Kelvin Rsense */
741 rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
744 dev_err(dev, "Invalid number of wires:%u\n", n_wires);
750 if (fwnode_property_read_bool(child, "adi,rsense-share")) {
751 /* Current rotation is only available with rsense sharing */
752 if (fwnode_property_read_bool(child, "adi,current-rotate")) {
753 if (n_wires == 2 || n_wires == 3) {
755 "Rotation not allowed for 2/3 Wire RTDs");
759 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
761 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
765 * rtd channel indexes are a bit more complicated to validate.
766 * For 4wire RTD with rotation, the channel selection cannot be
767 * >=19 since the chann + 1 is used in this configuration.
768 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
769 * <=1 since chanel - 1 and channel - 2 are used.
771 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
773 u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
774 max = LTC2983_MAX_CHANNELS_NR;
776 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
777 max = LTC2983_MAX_CHANNELS_NR - 1;
779 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
780 == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
781 (rtd->r_sense_chan <= min)) {
784 "Invalid rsense chann:%d to use in kelvin rsense",
791 if (sensor->chan < min || sensor->chan > max) {
792 dev_err(dev, "Invalid chann:%d for the rtd config",
799 /* same as differential case */
800 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
801 dev_err(&st->spi->dev,
802 "Invalid chann:%d for RTD", sensor->chan);
809 /* check custom sensor */
810 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
811 rtd->custom = __ltc2983_custom_sensor_new(st, child,
814 if (IS_ERR(rtd->custom)) {
815 ret = PTR_ERR(rtd->custom);
820 /* set common parameters */
821 rtd->sensor.fault_handler = ltc2983_common_fault_handler;
822 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
824 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
825 &excitation_current);
828 rtd->excitation_current = 1;
830 switch (excitation_current) {
832 rtd->excitation_current = 0x01;
835 rtd->excitation_current = 0x02;
838 rtd->excitation_current = 0x03;
841 rtd->excitation_current = 0x04;
844 rtd->excitation_current = 0x05;
847 rtd->excitation_current = 0x06;
850 rtd->excitation_current = 0x07;
853 rtd->excitation_current = 0x08;
856 dev_err(&st->spi->dev,
857 "Invalid value for excitation current(%u)",
864 fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
866 fwnode_handle_put(ref);
869 fwnode_handle_put(ref);
873 static struct ltc2983_sensor *
874 ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st,
875 const struct ltc2983_sensor *sensor)
877 struct ltc2983_thermistor *thermistor;
878 struct device *dev = &st->spi->dev;
879 struct fwnode_handle *ref;
880 u32 excitation_current = 0;
883 thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
885 return ERR_PTR(-ENOMEM);
887 ref = fwnode_find_reference(child, "adi,rsense-handle", 0);
889 dev_err(dev, "Property adi,rsense-handle missing or invalid");
890 return ERR_CAST(ref);
893 ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan);
895 dev_err(dev, "rsense channel must be configured...\n");
899 if (fwnode_property_read_bool(child, "adi,single-ended")) {
900 thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
901 } else if (fwnode_property_read_bool(child, "adi,rsense-share")) {
902 /* rotation is only possible if sharing rsense */
903 if (fwnode_property_read_bool(child, "adi,current-rotate"))
904 thermistor->sensor_config =
905 LTC2983_THERMISTOR_C_ROTATE(1);
907 thermistor->sensor_config =
908 LTC2983_THERMISTOR_R_SHARE(1);
910 /* validate channel index */
911 if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
912 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
913 dev_err(&st->spi->dev,
914 "Invalid chann:%d for differential thermistor",
920 /* check custom sensor */
921 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
922 bool steinhart = false;
923 const char *propname;
925 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
927 propname = "adi,custom-steinhart";
929 propname = "adi,custom-thermistor";
932 thermistor->custom = __ltc2983_custom_sensor_new(st, child,
936 if (IS_ERR(thermistor->custom)) {
937 ret = PTR_ERR(thermistor->custom);
941 /* set common parameters */
942 thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
943 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
945 ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
946 &excitation_current);
948 /* Auto range is not allowed for custom sensors */
949 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
951 thermistor->excitation_current = 0x03;
953 /* default to auto-range */
954 thermistor->excitation_current = 0x0c;
956 switch (excitation_current) {
960 LTC2983_SENSOR_THERMISTOR_STEINHART) {
961 dev_err(&st->spi->dev,
962 "Auto Range not allowed for custom sensors\n");
966 thermistor->excitation_current = 0x0c;
969 thermistor->excitation_current = 0x01;
972 thermistor->excitation_current = 0x02;
975 thermistor->excitation_current = 0x03;
978 thermistor->excitation_current = 0x04;
981 thermistor->excitation_current = 0x05;
984 thermistor->excitation_current = 0x06;
987 thermistor->excitation_current = 0x07;
990 thermistor->excitation_current = 0x08;
993 thermistor->excitation_current = 0x09;
996 thermistor->excitation_current = 0x0a;
999 thermistor->excitation_current = 0x0b;
1002 dev_err(&st->spi->dev,
1003 "Invalid value for excitation current(%u)",
1004 excitation_current);
1010 fwnode_handle_put(ref);
1011 return &thermistor->sensor;
1013 fwnode_handle_put(ref);
1014 return ERR_PTR(ret);
1017 static struct ltc2983_sensor *
1018 ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st,
1019 const struct ltc2983_sensor *sensor)
1021 struct ltc2983_diode *diode;
1022 u32 temp = 0, excitation_current = 0;
1025 diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
1027 return ERR_PTR(-ENOMEM);
1029 if (fwnode_property_read_bool(child, "adi,single-ended"))
1030 diode->sensor_config = LTC2983_DIODE_SGL(1);
1032 if (fwnode_property_read_bool(child, "adi,three-conversion-cycles"))
1033 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
1035 if (fwnode_property_read_bool(child, "adi,average-on"))
1036 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
1038 /* validate channel index */
1039 if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
1040 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1041 dev_err(&st->spi->dev,
1042 "Invalid chann:%d for differential thermistor",
1044 return ERR_PTR(-EINVAL);
1046 /* set common parameters */
1047 diode->sensor.fault_handler = ltc2983_common_fault_handler;
1048 diode->sensor.assign_chan = ltc2983_diode_assign_chan;
1050 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
1051 &excitation_current);
1053 switch (excitation_current) {
1055 diode->excitation_current = 0x00;
1058 diode->excitation_current = 0x01;
1061 diode->excitation_current = 0x02;
1064 diode->excitation_current = 0x03;
1067 dev_err(&st->spi->dev,
1068 "Invalid value for excitation current(%u)",
1069 excitation_current);
1070 return ERR_PTR(-EINVAL);
1074 fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp);
1076 /* 2^20 resolution */
1077 diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
1079 return &diode->sensor;
1082 static struct ltc2983_sensor *ltc2983_r_sense_new(struct fwnode_handle *child,
1083 struct ltc2983_data *st,
1084 const struct ltc2983_sensor *sensor)
1086 struct ltc2983_rsense *rsense;
1090 rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
1092 return ERR_PTR(-ENOMEM);
1094 /* validate channel index */
1095 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1096 dev_err(&st->spi->dev, "Invalid chann:%d for r_sense",
1098 return ERR_PTR(-EINVAL);
1101 ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
1103 dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n");
1104 return ERR_PTR(-EINVAL);
1107 * Times 1000 because we have milli-ohms and __convert_to_raw
1108 * expects scales of 1000000 which are used for all other
1112 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
1114 /* set common parameters */
1115 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
1117 return &rsense->sensor;
1120 static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child,
1121 struct ltc2983_data *st,
1122 const struct ltc2983_sensor *sensor)
1124 struct ltc2983_adc *adc;
1126 adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
1128 return ERR_PTR(-ENOMEM);
1130 if (fwnode_property_read_bool(child, "adi,single-ended"))
1131 adc->single_ended = true;
1133 if (!adc->single_ended &&
1134 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1135 dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n",
1137 return ERR_PTR(-EINVAL);
1139 /* set common parameters */
1140 adc->sensor.assign_chan = ltc2983_adc_assign_chan;
1141 adc->sensor.fault_handler = ltc2983_common_fault_handler;
1143 return &adc->sensor;
1146 static int ltc2983_chan_read(struct ltc2983_data *st,
1147 const struct ltc2983_sensor *sensor, int *val)
1149 u32 start_conversion = 0;
1153 start_conversion = LTC2983_STATUS_START(true);
1154 start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
1155 dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
1156 sensor->chan, start_conversion);
1157 /* start conversion */
1158 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
1162 reinit_completion(&st->completion);
1164 * wait for conversion to complete.
1165 * 300 ms should be more than enough to complete the conversion.
1166 * Depending on the sensor configuration, there are 2/3 conversions
1169 time = wait_for_completion_timeout(&st->completion,
1170 msecs_to_jiffies(300));
1172 dev_warn(&st->spi->dev, "Conversion timed out\n");
1176 /* read the converted data */
1177 ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
1178 &st->temp, sizeof(st->temp));
1182 *val = __be32_to_cpu(st->temp);
1184 if (!(LTC2983_RES_VALID_MASK & *val)) {
1185 dev_err(&st->spi->dev, "Invalid conversion detected\n");
1189 ret = sensor->fault_handler(st, *val);
1193 *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
1197 static int ltc2983_read_raw(struct iio_dev *indio_dev,
1198 struct iio_chan_spec const *chan,
1199 int *val, int *val2, long mask)
1201 struct ltc2983_data *st = iio_priv(indio_dev);
1205 if (chan->address >= st->num_channels) {
1206 dev_err(&st->spi->dev, "Invalid chan address:%ld",
1212 case IIO_CHAN_INFO_RAW:
1213 mutex_lock(&st->lock);
1214 ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
1215 mutex_unlock(&st->lock);
1216 return ret ?: IIO_VAL_INT;
1217 case IIO_CHAN_INFO_SCALE:
1218 switch (chan->type) {
1220 /* value in milli degrees */
1224 return IIO_VAL_FRACTIONAL;
1226 /* value in millivolt */
1230 return IIO_VAL_FRACTIONAL;
1239 static int ltc2983_reg_access(struct iio_dev *indio_dev,
1241 unsigned int writeval,
1242 unsigned int *readval)
1244 struct ltc2983_data *st = iio_priv(indio_dev);
1247 return regmap_read(st->regmap, reg, readval);
1249 return regmap_write(st->regmap, reg, writeval);
1252 static irqreturn_t ltc2983_irq_handler(int irq, void *data)
1254 struct ltc2983_data *st = data;
1256 complete(&st->completion);
1260 #define LTC2983_CHAN(__type, index, __address) ({ \
1261 struct iio_chan_spec __chan = { \
1265 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1266 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1267 .address = __address, \
1272 static int ltc2983_parse_dt(struct ltc2983_data *st)
1274 struct device *dev = &st->spi->dev;
1275 struct fwnode_handle *child;
1276 int ret = 0, chan = 0, channel_avail_mask = 0;
1278 device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config);
1280 device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq);
1282 st->num_channels = device_get_child_node_count(dev);
1283 if (!st->num_channels) {
1284 dev_err(&st->spi->dev, "At least one channel must be given!");
1288 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
1293 st->iio_channels = st->num_channels;
1294 device_for_each_child_node(dev, child) {
1295 struct ltc2983_sensor sensor;
1297 ret = fwnode_property_read_u32(child, "reg", &sensor.chan);
1299 dev_err(dev, "reg property must given for child nodes\n");
1303 /* check if we have a valid channel */
1304 if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
1305 sensor.chan > LTC2983_MAX_CHANNELS_NR) {
1307 dev_err(dev, "chan:%d must be from %u to %u\n", sensor.chan,
1308 LTC2983_MIN_CHANNELS_NR, LTC2983_MAX_CHANNELS_NR);
1310 } else if (channel_avail_mask & BIT(sensor.chan)) {
1312 dev_err(dev, "chan:%d already in use\n", sensor.chan);
1316 ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type);
1319 "adi,sensor-type property must given for child nodes\n");
1323 dev_dbg(dev, "Create new sensor, type %u, chann %u",
1327 if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
1328 sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
1329 st->sensors[chan] = ltc2983_thermocouple_new(child, st,
1331 } else if (sensor.type >= LTC2983_SENSOR_RTD &&
1332 sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
1333 st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
1334 } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
1335 sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
1336 st->sensors[chan] = ltc2983_thermistor_new(child, st,
1338 } else if (sensor.type == LTC2983_SENSOR_DIODE) {
1339 st->sensors[chan] = ltc2983_diode_new(child, st,
1341 } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
1342 st->sensors[chan] = ltc2983_r_sense_new(child, st,
1344 /* don't add rsense to iio */
1346 } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
1347 st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
1349 dev_err(dev, "Unknown sensor type %d\n", sensor.type);
1354 if (IS_ERR(st->sensors[chan])) {
1355 dev_err(dev, "Failed to create sensor %ld",
1356 PTR_ERR(st->sensors[chan]));
1357 ret = PTR_ERR(st->sensors[chan]);
1360 /* set generic sensor parameters */
1361 st->sensors[chan]->chan = sensor.chan;
1362 st->sensors[chan]->type = sensor.type;
1364 channel_avail_mask |= BIT(sensor.chan);
1370 fwnode_handle_put(child);
1374 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
1376 u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
1379 /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
1380 ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
1381 LTC2983_STATUS_UP(status) == 1, 25000,
1384 dev_err(&st->spi->dev, "Device startup timed out\n");
1388 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
1389 LTC2983_NOTCH_FREQ_MASK,
1390 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
1394 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
1395 st->mux_delay_config);
1399 for (chan = 0; chan < st->num_channels; chan++) {
1400 u32 chan_type = 0, *iio_chan;
1402 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
1406 * The assign_iio flag is necessary for when the device is
1407 * coming out of sleep. In that case, we just need to
1408 * re-configure the device channels.
1409 * We also don't assign iio channels for rsense.
1411 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
1415 /* assign iio channel */
1416 if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
1417 chan_type = IIO_TEMP;
1418 iio_chan = &iio_chan_t;
1420 chan_type = IIO_VOLTAGE;
1421 iio_chan = &iio_chan_v;
1425 * add chan as the iio .address so that, we can directly
1426 * reference the sensor given the iio_chan_spec
1428 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
1435 static const struct regmap_range ltc2983_reg_ranges[] = {
1436 regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
1437 regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
1438 regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
1439 regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
1440 LTC2983_MULT_CHANNEL_END_REG),
1441 regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
1442 regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
1443 LTC2983_CHAN_ASSIGN_END_REG),
1444 regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
1445 LTC2983_CUST_SENS_TBL_END_REG),
1448 static const struct regmap_access_table ltc2983_reg_table = {
1449 .yes_ranges = ltc2983_reg_ranges,
1450 .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
1454 * The reg_bits are actually 12 but the device needs the first *complete*
1455 * byte for the command (R/W).
1457 static const struct regmap_config ltc2983_regmap_config = {
1460 .wr_table = <c2983_reg_table,
1461 .rd_table = <c2983_reg_table,
1462 .read_flag_mask = GENMASK(1, 0),
1463 .write_flag_mask = BIT(1),
1466 static const struct iio_info ltc2983_iio_info = {
1467 .read_raw = ltc2983_read_raw,
1468 .debugfs_reg_access = ltc2983_reg_access,
1471 static int ltc2983_probe(struct spi_device *spi)
1473 struct ltc2983_data *st;
1474 struct iio_dev *indio_dev;
1475 struct gpio_desc *gpio;
1476 const char *name = spi_get_device_id(spi)->name;
1479 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1483 st = iio_priv(indio_dev);
1485 st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config);
1486 if (IS_ERR(st->regmap)) {
1487 dev_err(&spi->dev, "Failed to initialize regmap\n");
1488 return PTR_ERR(st->regmap);
1491 mutex_init(&st->lock);
1492 init_completion(&st->completion);
1494 spi_set_drvdata(spi, st);
1496 ret = ltc2983_parse_dt(st);
1500 gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH);
1502 return PTR_ERR(gpio);
1505 /* bring the device out of reset */
1506 usleep_range(1000, 1200);
1507 gpiod_set_value_cansleep(gpio, 0);
1510 st->iio_chan = devm_kzalloc(&spi->dev,
1511 st->iio_channels * sizeof(*st->iio_chan),
1516 ret = ltc2983_setup(st, true);
1520 ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
1521 IRQF_TRIGGER_RISING, name, st);
1523 dev_err(&spi->dev, "failed to request an irq, %d", ret);
1527 indio_dev->name = name;
1528 indio_dev->num_channels = st->iio_channels;
1529 indio_dev->channels = st->iio_chan;
1530 indio_dev->modes = INDIO_DIRECT_MODE;
1531 indio_dev->info = <c2983_iio_info;
1533 return devm_iio_device_register(&spi->dev, indio_dev);
1536 static int ltc2983_resume(struct device *dev)
1538 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1541 /* dummy read to bring the device out of sleep */
1542 regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
1543 /* we need to re-assign the channels */
1544 return ltc2983_setup(st, false);
1547 static int ltc2983_suspend(struct device *dev)
1549 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1551 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
1554 static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend,
1557 static const struct spi_device_id ltc2983_id_table[] = {
1561 MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1563 static const struct of_device_id ltc2983_of_match[] = {
1564 { .compatible = "adi,ltc2983" },
1567 MODULE_DEVICE_TABLE(of, ltc2983_of_match);
1569 static struct spi_driver ltc2983_driver = {
1572 .of_match_table = ltc2983_of_match,
1573 .pm = pm_sleep_ptr(<c2983_pm_ops),
1575 .probe = ltc2983_probe,
1576 .id_table = ltc2983_id_table,
1579 module_spi_driver(ltc2983_driver);
1581 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1582 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1583 MODULE_LICENSE("GPL");