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/module.h>
16 #include <linux/of_gpio.h>
17 #include <linux/regmap.h>
18 #include <linux/spi/spi.h>
21 #define LTC2983_STATUS_REG 0x0000
22 #define LTC2983_TEMP_RES_START_REG 0x0010
23 #define LTC2983_TEMP_RES_END_REG 0x005F
24 #define LTC2983_GLOBAL_CONFIG_REG 0x00F0
25 #define LTC2983_MULT_CHANNEL_START_REG 0x00F4
26 #define LTC2983_MULT_CHANNEL_END_REG 0x00F7
27 #define LTC2983_MUX_CONFIG_REG 0x00FF
28 #define LTC2983_CHAN_ASSIGN_START_REG 0x0200
29 #define LTC2983_CHAN_ASSIGN_END_REG 0x024F
30 #define LTC2983_CUST_SENS_TBL_START_REG 0x0250
31 #define LTC2983_CUST_SENS_TBL_END_REG 0x03CF
33 #define LTC2983_DIFFERENTIAL_CHAN_MIN 2
34 #define LTC2983_MAX_CHANNELS_NR 20
35 #define LTC2983_MIN_CHANNELS_NR 1
36 #define LTC2983_SLEEP 0x97
37 #define LTC2983_CUSTOM_STEINHART_SIZE 24
38 #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6
39 #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4
41 #define LTC2983_CHAN_START_ADDR(chan) \
42 (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
43 #define LTC2983_CHAN_RES_ADDR(chan) \
44 (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
45 #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3)
46 #define LTC2983_THERMOCOUPLE_SGL(x) \
47 FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
48 #define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0)
49 #define LTC2983_THERMOCOUPLE_OC_CURR(x) \
50 FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
51 #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2)
52 #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
53 FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
55 #define LTC2983_THERMISTOR_DIFF_MASK BIT(2)
56 #define LTC2983_THERMISTOR_SGL(x) \
57 FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
58 #define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1)
59 #define LTC2983_THERMISTOR_R_SHARE(x) \
60 FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
61 #define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0)
62 #define LTC2983_THERMISTOR_C_ROTATE(x) \
63 FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
65 #define LTC2983_DIODE_DIFF_MASK BIT(2)
66 #define LTC2983_DIODE_SGL(x) \
67 FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
68 #define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1)
69 #define LTC2983_DIODE_3_CONV_CYCLE(x) \
70 FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
71 #define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0)
72 #define LTC2983_DIODE_AVERAGE_ON(x) \
73 FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
75 #define LTC2983_RTD_4_WIRE_MASK BIT(3)
76 #define LTC2983_RTD_ROTATION_MASK BIT(1)
77 #define LTC2983_RTD_C_ROTATE(x) \
78 FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
79 #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2)
80 #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2)
81 #define LTC2983_RTD_N_WIRES(x) \
82 FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
83 #define LTC2983_RTD_R_SHARE_MASK BIT(0)
84 #define LTC2983_RTD_R_SHARE(x) \
85 FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
87 #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30)
88 #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25)
90 #define LTC2983_STATUS_START_MASK BIT(7)
91 #define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x)
92 #define LTC2983_STATUS_UP_MASK GENMASK(7, 6)
93 #define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
95 #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0)
96 #define LTC2983_STATUS_CHAN_SEL(x) \
97 FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
99 #define LTC2983_TEMP_UNITS_MASK BIT(2)
100 #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
102 #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0)
103 #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
105 #define LTC2983_RES_VALID_MASK BIT(24)
106 #define LTC2983_DATA_MASK GENMASK(23, 0)
107 #define LTC2983_DATA_SIGN_BIT 23
109 #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27)
110 #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
112 /* cold junction for thermocouples and rsense for rtd's and thermistor's */
113 #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22)
114 #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
116 #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0)
117 #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
119 #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6)
120 #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
122 #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18)
123 #define LTC2983_THERMOCOUPLE_CFG(x) \
124 FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
125 #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29)
126 #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25)
128 #define LTC2983_RTD_CFG_MASK GENMASK(21, 18)
129 #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
130 #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14)
131 #define LTC2983_RTD_EXC_CURRENT(x) \
132 FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
133 #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12)
134 #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
136 #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19)
137 #define LTC2983_THERMISTOR_CFG(x) \
138 FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
139 #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15)
140 #define LTC2983_THERMISTOR_EXC_CURRENT(x) \
141 FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
143 #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24)
144 #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
145 #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22)
146 #define LTC2983_DIODE_EXC_CURRENT(x) \
147 FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
148 #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0)
149 #define LTC2983_DIODE_IDEAL_FACTOR(x) \
150 FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
152 #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0)
153 #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
155 #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26)
156 #define LTC2983_ADC_SINGLE_ENDED(x) \
157 FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
160 LTC2983_SENSOR_THERMOCOUPLE = 1,
161 LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
162 LTC2983_SENSOR_RTD = 10,
163 LTC2983_SENSOR_RTD_CUSTOM = 18,
164 LTC2983_SENSOR_THERMISTOR = 19,
165 LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
166 LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
167 LTC2983_SENSOR_DIODE = 28,
168 LTC2983_SENSOR_SENSE_RESISTOR = 29,
169 LTC2983_SENSOR_DIRECT_ADC = 30,
172 #define to_thermocouple(_sensor) \
173 container_of(_sensor, struct ltc2983_thermocouple, sensor)
175 #define to_rtd(_sensor) \
176 container_of(_sensor, struct ltc2983_rtd, sensor)
178 #define to_thermistor(_sensor) \
179 container_of(_sensor, struct ltc2983_thermistor, sensor)
181 #define to_diode(_sensor) \
182 container_of(_sensor, struct ltc2983_diode, sensor)
184 #define to_rsense(_sensor) \
185 container_of(_sensor, struct ltc2983_rsense, sensor)
187 #define to_adc(_sensor) \
188 container_of(_sensor, struct ltc2983_adc, sensor)
190 struct ltc2983_data {
191 struct regmap *regmap;
192 struct spi_device *spi;
194 struct completion completion;
195 struct iio_chan_spec *iio_chan;
196 struct ltc2983_sensor **sensors;
197 u32 mux_delay_config;
198 u32 filter_notch_freq;
199 u16 custom_table_size;
203 * DMA (thus cache coherency maintenance) requires the
204 * transfer buffers to live in their own cache lines.
205 * Holds the converted temperature
207 __be32 temp ____cacheline_aligned;
211 struct ltc2983_sensor {
212 int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
213 int (*assign_chan)(struct ltc2983_data *st,
214 const struct ltc2983_sensor *sensor);
215 /* specifies the sensor channel */
221 struct ltc2983_custom_sensor {
222 /* raw table sensor data */
230 struct ltc2983_thermocouple {
231 struct ltc2983_sensor sensor;
232 struct ltc2983_custom_sensor *custom;
234 u32 cold_junction_chan;
238 struct ltc2983_sensor sensor;
239 struct ltc2983_custom_sensor *custom;
242 u32 excitation_current;
246 struct ltc2983_thermistor {
247 struct ltc2983_sensor sensor;
248 struct ltc2983_custom_sensor *custom;
251 u32 excitation_current;
254 struct ltc2983_diode {
255 struct ltc2983_sensor sensor;
257 u32 excitation_current;
258 u32 ideal_factor_value;
261 struct ltc2983_rsense {
262 struct ltc2983_sensor sensor;
267 struct ltc2983_sensor sensor;
272 * Convert to Q format numbers. These number's are integers where
273 * the number of integer and fractional bits are specified. The resolution
274 * is given by 1/@resolution and tell us the number of fractional bits. For
275 * instance a resolution of 2^-10 means we have 10 fractional bits.
277 static u32 __convert_to_raw(const u64 val, const u32 resolution)
279 u64 __res = val * resolution;
281 /* all values are multiplied by 1000000 to remove the fraction */
282 do_div(__res, 1000000);
287 static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
289 s64 __res = -(s32)val;
291 __res = __convert_to_raw(__res, resolution);
296 static int __ltc2983_fault_handler(const struct ltc2983_data *st,
297 const u32 result, const u32 hard_mask,
300 const struct device *dev = &st->spi->dev;
302 if (result & hard_mask) {
303 dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
305 } else if (result & soft_mask) {
306 /* just print a warning */
307 dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
313 static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
314 const struct ltc2983_sensor *sensor,
317 u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
319 chan_val |= LTC2983_CHAN_TYPE(sensor->type);
320 dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
322 st->chan_val = cpu_to_be32(chan_val);
323 return regmap_bulk_write(st->regmap, reg, &st->chan_val,
324 sizeof(st->chan_val));
327 static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
328 struct ltc2983_custom_sensor *custom,
332 u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
333 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
334 const struct device *dev = &st->spi->dev;
336 * custom->size holds the raw size of the table. However, when
337 * configuring the sensor channel, we must write the number of
338 * entries of the table minus 1. For steinhart sensors 0 is written
339 * since the size is constant!
341 const u8 len = custom->is_steinhart ? 0 :
342 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
344 * Check if the offset was assigned already. It should be for steinhart
345 * sensors. When coming from sleep, it should be assigned for all.
347 if (custom->offset < 0) {
349 * This needs to be done again here because, from the moment
350 * when this test was done (successfully) for this custom
351 * sensor, a steinhart sensor might have been added changing
352 * custom_table_size...
354 if (st->custom_table_size + custom->size >
355 (LTC2983_CUST_SENS_TBL_END_REG -
356 LTC2983_CUST_SENS_TBL_START_REG) + 1) {
358 "Not space left(%d) for new custom sensor(%zu)",
359 st->custom_table_size,
364 custom->offset = st->custom_table_size /
365 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
366 st->custom_table_size += custom->size;
369 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
371 *chan_val |= LTC2983_CUSTOM_LEN(len);
372 *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
373 dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
376 /* write custom sensor table */
377 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
380 static struct ltc2983_custom_sensor *__ltc2983_custom_sensor_new(
381 struct ltc2983_data *st,
382 const struct device_node *np,
383 const char *propname,
384 const bool is_steinhart,
385 const u32 resolution,
386 const bool has_signed)
388 struct ltc2983_custom_sensor *new_custom;
389 u8 index, n_entries, tbl = 0;
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;
396 const u8 e_size = is_steinhart ? sizeof(u32) : sizeof(u64);
398 n_entries = of_property_count_elems_of_size(np, propname, e_size);
399 /* n_entries must be an even number */
400 if (!n_entries || (n_entries % 2) != 0) {
401 dev_err(dev, "Number of entries either 0 or not even\n");
402 return ERR_PTR(-EINVAL);
405 new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
407 return ERR_PTR(-ENOMEM);
409 new_custom->size = n_entries * n_size;
410 /* check Steinhart size */
411 if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) {
412 dev_err(dev, "Steinhart sensors size(%zu) must be 24",
414 return ERR_PTR(-EINVAL);
416 /* Check space on the table. */
417 if (st->custom_table_size + new_custom->size >
418 (LTC2983_CUST_SENS_TBL_END_REG -
419 LTC2983_CUST_SENS_TBL_START_REG) + 1) {
420 dev_err(dev, "No space left(%d) for new custom sensor(%zu)",
421 st->custom_table_size, new_custom->size);
422 return ERR_PTR(-EINVAL);
425 /* allocate the table */
426 new_custom->table = devm_kzalloc(dev, new_custom->size, GFP_KERNEL);
427 if (!new_custom->table)
428 return ERR_PTR(-ENOMEM);
430 for (index = 0; index < n_entries; index++) {
433 * Steinhart sensors are configured with raw values in the
434 * devicetree. For the other sensors we must convert the
435 * value to raw. The odd index's correspond to temperarures
436 * and always have 1/1024 of resolution. Temperatures also
437 * come in kelvin, so signed values is not possible
440 of_property_read_u64_index(np, propname, index, &temp);
442 if ((index % 2) != 0)
443 temp = __convert_to_raw(temp, 1024);
444 else if (has_signed && (s64)temp < 0)
445 temp = __convert_to_raw_sign(temp, resolution);
447 temp = __convert_to_raw(temp, resolution);
451 of_property_read_u32_index(np, propname, index, &t32);
455 for (j = 0; j < n_size; j++)
456 new_custom->table[tbl++] =
457 temp >> (8 * (n_size - j - 1));
460 new_custom->is_steinhart = is_steinhart;
462 * This is done to first add all the steinhart sensors to the table,
463 * in order to maximize the table usage. If we mix adding steinhart
464 * with the other sensors, we might have to do some roundup to make
465 * sure that sensor_addr - 0x250(start address) is a multiple of 4
466 * (for steinhart), and a multiple of 6 for all the other sensors.
467 * Since we have const 24 bytes for steinhart sensors and 24 is
468 * also a multiple of 6, we guarantee that the first non-steinhart
469 * sensor will sit in a correct address without the need of filling
473 new_custom->offset = st->custom_table_size /
474 LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
475 st->custom_table_size += new_custom->size;
477 /* mark as unset. This is checked later on the assign phase */
478 new_custom->offset = -1;
484 static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
487 return __ltc2983_fault_handler(st, result,
488 LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
489 LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
492 static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
495 return __ltc2983_fault_handler(st, result,
496 LTC2983_COMMON_HARD_FAULT_MASK,
497 LTC2983_COMMON_SOFT_FAULT_MASK);
500 static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
501 const struct ltc2983_sensor *sensor)
503 struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
506 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
507 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
509 if (thermo->custom) {
512 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
517 return __ltc2983_chan_assign_common(st, sensor, chan_val);
520 static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
521 const struct ltc2983_sensor *sensor)
523 struct ltc2983_rtd *rtd = to_rtd(sensor);
526 chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
527 chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
528 chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
529 chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
534 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
539 return __ltc2983_chan_assign_common(st, sensor, chan_val);
542 static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
543 const struct ltc2983_sensor *sensor)
545 struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
548 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
549 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
551 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
553 if (thermistor->custom) {
556 ret = __ltc2983_chan_custom_sensor_assign(st,
562 return __ltc2983_chan_assign_common(st, sensor, chan_val);
565 static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
566 const struct ltc2983_sensor *sensor)
568 struct ltc2983_diode *diode = to_diode(sensor);
571 chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
572 chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
573 chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
575 return __ltc2983_chan_assign_common(st, sensor, chan_val);
578 static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
579 const struct ltc2983_sensor *sensor)
581 struct ltc2983_rsense *rsense = to_rsense(sensor);
584 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
586 return __ltc2983_chan_assign_common(st, sensor, chan_val);
589 static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
590 const struct ltc2983_sensor *sensor)
592 struct ltc2983_adc *adc = to_adc(sensor);
595 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
597 return __ltc2983_chan_assign_common(st, sensor, chan_val);
600 static struct ltc2983_sensor *ltc2983_thermocouple_new(
601 const struct device_node *child,
602 struct ltc2983_data *st,
603 const struct ltc2983_sensor *sensor)
605 struct ltc2983_thermocouple *thermo;
606 struct device_node *phandle;
610 thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
612 return ERR_PTR(-ENOMEM);
614 if (of_property_read_bool(child, "adi,single-ended"))
615 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
617 ret = of_property_read_u32(child, "adi,sensor-oc-current-microamp",
620 switch (oc_current) {
622 thermo->sensor_config |=
623 LTC2983_THERMOCOUPLE_OC_CURR(0);
626 thermo->sensor_config |=
627 LTC2983_THERMOCOUPLE_OC_CURR(1);
630 thermo->sensor_config |=
631 LTC2983_THERMOCOUPLE_OC_CURR(2);
634 thermo->sensor_config |=
635 LTC2983_THERMOCOUPLE_OC_CURR(3);
638 dev_err(&st->spi->dev,
639 "Invalid open circuit current:%u", oc_current);
640 return ERR_PTR(-EINVAL);
643 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
645 /* validate channel index */
646 if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
647 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
648 dev_err(&st->spi->dev,
649 "Invalid chann:%d for differential thermocouple",
651 return ERR_PTR(-EINVAL);
654 phandle = of_parse_phandle(child, "adi,cold-junction-handle", 0);
658 ret = of_property_read_u32(phandle, "reg",
659 &thermo->cold_junction_chan);
662 * This would be catched later but we can just return
663 * the error right away.
665 dev_err(&st->spi->dev, "Property reg must be given\n");
666 of_node_put(phandle);
667 return ERR_PTR(-EINVAL);
671 /* check custom sensor */
672 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
673 const char *propname = "adi,custom-thermocouple";
675 thermo->custom = __ltc2983_custom_sensor_new(st, child,
678 if (IS_ERR(thermo->custom)) {
679 of_node_put(phandle);
680 return ERR_CAST(thermo->custom);
684 /* set common parameters */
685 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
686 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
688 of_node_put(phandle);
689 return &thermo->sensor;
692 static struct ltc2983_sensor *ltc2983_rtd_new(const struct device_node *child,
693 struct ltc2983_data *st,
694 const struct ltc2983_sensor *sensor)
696 struct ltc2983_rtd *rtd;
698 struct device *dev = &st->spi->dev;
699 struct device_node *phandle;
700 u32 excitation_current = 0, n_wires = 0;
702 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
704 return ERR_PTR(-ENOMEM);
706 phandle = of_parse_phandle(child, "adi,rsense-handle", 0);
708 dev_err(dev, "Property adi,rsense-handle missing or invalid");
709 return ERR_PTR(-EINVAL);
712 ret = of_property_read_u32(phandle, "reg", &rtd->r_sense_chan);
714 dev_err(dev, "Property reg must be given\n");
718 ret = of_property_read_u32(child, "adi,number-of-wires", &n_wires);
722 rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
725 rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
728 rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
731 /* 4 wires, Kelvin Rsense */
732 rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
735 dev_err(dev, "Invalid number of wires:%u\n", n_wires);
741 if (of_property_read_bool(child, "adi,rsense-share")) {
742 /* Current rotation is only available with rsense sharing */
743 if (of_property_read_bool(child, "adi,current-rotate")) {
744 if (n_wires == 2 || n_wires == 3) {
746 "Rotation not allowed for 2/3 Wire RTDs");
750 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
752 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
756 * rtd channel indexes are a bit more complicated to validate.
757 * For 4wire RTD with rotation, the channel selection cannot be
758 * >=19 since the chann + 1 is used in this configuration.
759 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
760 * <=1 since chanel - 1 and channel - 2 are used.
762 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
764 u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
765 max = LTC2983_MAX_CHANNELS_NR;
767 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
768 max = LTC2983_MAX_CHANNELS_NR - 1;
770 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
771 == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
772 (rtd->r_sense_chan <= min)) {
775 "Invalid rsense chann:%d to use in kelvin rsense",
782 if (sensor->chan < min || sensor->chan > max) {
783 dev_err(dev, "Invalid chann:%d for the rtd config",
790 /* same as differential case */
791 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
792 dev_err(&st->spi->dev,
793 "Invalid chann:%d for RTD", sensor->chan);
800 /* check custom sensor */
801 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
802 rtd->custom = __ltc2983_custom_sensor_new(st, child,
805 if (IS_ERR(rtd->custom)) {
806 of_node_put(phandle);
807 return ERR_CAST(rtd->custom);
811 /* set common parameters */
812 rtd->sensor.fault_handler = ltc2983_common_fault_handler;
813 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
815 ret = of_property_read_u32(child, "adi,excitation-current-microamp",
816 &excitation_current);
819 rtd->excitation_current = 1;
821 switch (excitation_current) {
823 rtd->excitation_current = 0x01;
826 rtd->excitation_current = 0x02;
829 rtd->excitation_current = 0x03;
832 rtd->excitation_current = 0x04;
835 rtd->excitation_current = 0x05;
838 rtd->excitation_current = 0x06;
841 rtd->excitation_current = 0x07;
844 rtd->excitation_current = 0x08;
847 dev_err(&st->spi->dev,
848 "Invalid value for excitation current(%u)",
855 of_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
857 of_node_put(phandle);
860 of_node_put(phandle);
864 static struct ltc2983_sensor *ltc2983_thermistor_new(
865 const struct device_node *child,
866 struct ltc2983_data *st,
867 const struct ltc2983_sensor *sensor)
869 struct ltc2983_thermistor *thermistor;
870 struct device *dev = &st->spi->dev;
871 struct device_node *phandle;
872 u32 excitation_current = 0;
875 thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
877 return ERR_PTR(-ENOMEM);
879 phandle = of_parse_phandle(child, "adi,rsense-handle", 0);
881 dev_err(dev, "Property adi,rsense-handle missing or invalid");
882 return ERR_PTR(-EINVAL);
885 ret = of_property_read_u32(phandle, "reg", &thermistor->r_sense_chan);
887 dev_err(dev, "rsense channel must be configured...\n");
891 if (of_property_read_bool(child, "adi,single-ended")) {
892 thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
893 } else if (of_property_read_bool(child, "adi,rsense-share")) {
894 /* rotation is only possible if sharing rsense */
895 if (of_property_read_bool(child, "adi,current-rotate"))
896 thermistor->sensor_config =
897 LTC2983_THERMISTOR_C_ROTATE(1);
899 thermistor->sensor_config =
900 LTC2983_THERMISTOR_R_SHARE(1);
902 /* validate channel index */
903 if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
904 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
905 dev_err(&st->spi->dev,
906 "Invalid chann:%d for differential thermistor",
912 /* check custom sensor */
913 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
914 bool steinhart = false;
915 const char *propname;
917 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
919 propname = "adi,custom-steinhart";
921 propname = "adi,custom-thermistor";
924 thermistor->custom = __ltc2983_custom_sensor_new(st, child,
928 if (IS_ERR(thermistor->custom)) {
929 of_node_put(phandle);
930 return ERR_CAST(thermistor->custom);
933 /* set common parameters */
934 thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
935 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
937 ret = of_property_read_u32(child, "adi,excitation-current-nanoamp",
938 &excitation_current);
940 /* Auto range is not allowed for custom sensors */
941 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
943 thermistor->excitation_current = 0x03;
945 /* default to auto-range */
946 thermistor->excitation_current = 0x0c;
948 switch (excitation_current) {
952 LTC2983_SENSOR_THERMISTOR_STEINHART) {
953 dev_err(&st->spi->dev,
954 "Auto Range not allowed for custom sensors\n");
958 thermistor->excitation_current = 0x0c;
961 thermistor->excitation_current = 0x01;
964 thermistor->excitation_current = 0x02;
967 thermistor->excitation_current = 0x03;
970 thermistor->excitation_current = 0x04;
973 thermistor->excitation_current = 0x05;
976 thermistor->excitation_current = 0x06;
979 thermistor->excitation_current = 0x07;
982 thermistor->excitation_current = 0x08;
985 thermistor->excitation_current = 0x09;
988 thermistor->excitation_current = 0x0a;
991 thermistor->excitation_current = 0x0b;
994 dev_err(&st->spi->dev,
995 "Invalid value for excitation current(%u)",
1002 of_node_put(phandle);
1003 return &thermistor->sensor;
1005 of_node_put(phandle);
1006 return ERR_PTR(ret);
1009 static struct ltc2983_sensor *ltc2983_diode_new(
1010 const struct device_node *child,
1011 const struct ltc2983_data *st,
1012 const struct ltc2983_sensor *sensor)
1014 struct ltc2983_diode *diode;
1015 u32 temp = 0, excitation_current = 0;
1018 diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
1020 return ERR_PTR(-ENOMEM);
1022 if (of_property_read_bool(child, "adi,single-ended"))
1023 diode->sensor_config = LTC2983_DIODE_SGL(1);
1025 if (of_property_read_bool(child, "adi,three-conversion-cycles"))
1026 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
1028 if (of_property_read_bool(child, "adi,average-on"))
1029 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
1031 /* validate channel index */
1032 if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
1033 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1034 dev_err(&st->spi->dev,
1035 "Invalid chann:%d for differential thermistor",
1037 return ERR_PTR(-EINVAL);
1039 /* set common parameters */
1040 diode->sensor.fault_handler = ltc2983_common_fault_handler;
1041 diode->sensor.assign_chan = ltc2983_diode_assign_chan;
1043 ret = of_property_read_u32(child, "adi,excitation-current-microamp",
1044 &excitation_current);
1046 switch (excitation_current) {
1048 diode->excitation_current = 0x00;
1051 diode->excitation_current = 0x01;
1054 diode->excitation_current = 0x02;
1057 diode->excitation_current = 0x03;
1060 dev_err(&st->spi->dev,
1061 "Invalid value for excitation current(%u)",
1062 excitation_current);
1063 return ERR_PTR(-EINVAL);
1067 of_property_read_u32(child, "adi,ideal-factor-value", &temp);
1069 /* 2^20 resolution */
1070 diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
1072 return &diode->sensor;
1075 static struct ltc2983_sensor *ltc2983_r_sense_new(struct device_node *child,
1076 struct ltc2983_data *st,
1077 const struct ltc2983_sensor *sensor)
1079 struct ltc2983_rsense *rsense;
1083 rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
1085 return ERR_PTR(-ENOMEM);
1087 /* validate channel index */
1088 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1089 dev_err(&st->spi->dev, "Invalid chann:%d for r_sense",
1091 return ERR_PTR(-EINVAL);
1094 ret = of_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
1096 dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n");
1097 return ERR_PTR(-EINVAL);
1100 * Times 1000 because we have milli-ohms and __convert_to_raw
1101 * expects scales of 1000000 which are used for all other
1105 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
1107 /* set common parameters */
1108 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
1110 return &rsense->sensor;
1113 static struct ltc2983_sensor *ltc2983_adc_new(struct device_node *child,
1114 struct ltc2983_data *st,
1115 const struct ltc2983_sensor *sensor)
1117 struct ltc2983_adc *adc;
1119 adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
1121 return ERR_PTR(-ENOMEM);
1123 if (of_property_read_bool(child, "adi,single-ended"))
1124 adc->single_ended = true;
1126 if (!adc->single_ended &&
1127 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
1128 dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n",
1130 return ERR_PTR(-EINVAL);
1132 /* set common parameters */
1133 adc->sensor.assign_chan = ltc2983_adc_assign_chan;
1134 adc->sensor.fault_handler = ltc2983_common_fault_handler;
1136 return &adc->sensor;
1139 static int ltc2983_chan_read(struct ltc2983_data *st,
1140 const struct ltc2983_sensor *sensor, int *val)
1142 u32 start_conversion = 0;
1146 start_conversion = LTC2983_STATUS_START(true);
1147 start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
1148 dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
1149 sensor->chan, start_conversion);
1150 /* start conversion */
1151 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
1155 reinit_completion(&st->completion);
1157 * wait for conversion to complete.
1158 * 300 ms should be more than enough to complete the conversion.
1159 * Depending on the sensor configuration, there are 2/3 conversions
1162 time = wait_for_completion_timeout(&st->completion,
1163 msecs_to_jiffies(300));
1165 dev_warn(&st->spi->dev, "Conversion timed out\n");
1169 /* read the converted data */
1170 ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
1171 &st->temp, sizeof(st->temp));
1175 *val = __be32_to_cpu(st->temp);
1177 if (!(LTC2983_RES_VALID_MASK & *val)) {
1178 dev_err(&st->spi->dev, "Invalid conversion detected\n");
1182 ret = sensor->fault_handler(st, *val);
1186 *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
1190 static int ltc2983_read_raw(struct iio_dev *indio_dev,
1191 struct iio_chan_spec const *chan,
1192 int *val, int *val2, long mask)
1194 struct ltc2983_data *st = iio_priv(indio_dev);
1198 if (chan->address >= st->num_channels) {
1199 dev_err(&st->spi->dev, "Invalid chan address:%ld",
1205 case IIO_CHAN_INFO_RAW:
1206 mutex_lock(&st->lock);
1207 ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
1208 mutex_unlock(&st->lock);
1209 return ret ?: IIO_VAL_INT;
1210 case IIO_CHAN_INFO_SCALE:
1211 switch (chan->type) {
1213 /* value in milli degrees */
1217 return IIO_VAL_FRACTIONAL;
1219 /* value in millivolt */
1223 return IIO_VAL_FRACTIONAL;
1232 static int ltc2983_reg_access(struct iio_dev *indio_dev,
1234 unsigned int writeval,
1235 unsigned int *readval)
1237 struct ltc2983_data *st = iio_priv(indio_dev);
1240 return regmap_read(st->regmap, reg, readval);
1242 return regmap_write(st->regmap, reg, writeval);
1245 static irqreturn_t ltc2983_irq_handler(int irq, void *data)
1247 struct ltc2983_data *st = data;
1249 complete(&st->completion);
1253 #define LTC2983_CHAN(__type, index, __address) ({ \
1254 struct iio_chan_spec __chan = { \
1258 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1259 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1260 .address = __address, \
1265 static int ltc2983_parse_dt(struct ltc2983_data *st)
1267 struct device_node *child;
1268 struct device *dev = &st->spi->dev;
1269 int ret = 0, chan = 0, channel_avail_mask = 0;
1271 of_property_read_u32(dev->of_node, "adi,mux-delay-config-us",
1272 &st->mux_delay_config);
1274 of_property_read_u32(dev->of_node, "adi,filter-notch-freq",
1275 &st->filter_notch_freq);
1277 st->num_channels = of_get_available_child_count(dev->of_node);
1278 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
1283 st->iio_channels = st->num_channels;
1284 for_each_available_child_of_node(dev->of_node, child) {
1285 struct ltc2983_sensor sensor;
1287 ret = of_property_read_u32(child, "reg", &sensor.chan);
1289 dev_err(dev, "reg property must given for child nodes\n");
1293 /* check if we have a valid channel */
1294 if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
1295 sensor.chan > LTC2983_MAX_CHANNELS_NR) {
1298 "chan:%d must be from 1 to 20\n", sensor.chan);
1300 } else if (channel_avail_mask & BIT(sensor.chan)) {
1302 dev_err(dev, "chan:%d already in use\n", sensor.chan);
1306 ret = of_property_read_u32(child, "adi,sensor-type",
1310 "adi,sensor-type property must given for child nodes\n");
1314 dev_dbg(dev, "Create new sensor, type %u, chann %u",
1318 if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
1319 sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
1320 st->sensors[chan] = ltc2983_thermocouple_new(child, st,
1322 } else if (sensor.type >= LTC2983_SENSOR_RTD &&
1323 sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
1324 st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
1325 } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
1326 sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
1327 st->sensors[chan] = ltc2983_thermistor_new(child, st,
1329 } else if (sensor.type == LTC2983_SENSOR_DIODE) {
1330 st->sensors[chan] = ltc2983_diode_new(child, st,
1332 } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
1333 st->sensors[chan] = ltc2983_r_sense_new(child, st,
1335 /* don't add rsense to iio */
1337 } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
1338 st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
1340 dev_err(dev, "Unknown sensor type %d\n", sensor.type);
1345 if (IS_ERR(st->sensors[chan])) {
1346 dev_err(dev, "Failed to create sensor %ld",
1347 PTR_ERR(st->sensors[chan]));
1348 ret = PTR_ERR(st->sensors[chan]);
1351 /* set generic sensor parameters */
1352 st->sensors[chan]->chan = sensor.chan;
1353 st->sensors[chan]->type = sensor.type;
1355 channel_avail_mask |= BIT(sensor.chan);
1365 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
1367 u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
1370 /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
1371 ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
1372 LTC2983_STATUS_UP(status) == 1, 25000,
1375 dev_err(&st->spi->dev, "Device startup timed out\n");
1379 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
1380 LTC2983_NOTCH_FREQ_MASK,
1381 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
1385 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
1386 st->mux_delay_config);
1390 for (chan = 0; chan < st->num_channels; chan++) {
1391 u32 chan_type = 0, *iio_chan;
1393 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
1397 * The assign_iio flag is necessary for when the device is
1398 * coming out of sleep. In that case, we just need to
1399 * re-configure the device channels.
1400 * We also don't assign iio channels for rsense.
1402 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
1406 /* assign iio channel */
1407 if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
1408 chan_type = IIO_TEMP;
1409 iio_chan = &iio_chan_t;
1411 chan_type = IIO_VOLTAGE;
1412 iio_chan = &iio_chan_v;
1416 * add chan as the iio .address so that, we can directly
1417 * reference the sensor given the iio_chan_spec
1419 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
1426 static const struct regmap_range ltc2983_reg_ranges[] = {
1427 regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
1428 regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
1429 regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
1430 regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
1431 LTC2983_MULT_CHANNEL_END_REG),
1432 regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
1433 regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
1434 LTC2983_CHAN_ASSIGN_END_REG),
1435 regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
1436 LTC2983_CUST_SENS_TBL_END_REG),
1439 static const struct regmap_access_table ltc2983_reg_table = {
1440 .yes_ranges = ltc2983_reg_ranges,
1441 .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
1445 * The reg_bits are actually 12 but the device needs the first *complete*
1446 * byte for the command (R/W).
1448 static const struct regmap_config ltc2983_regmap_config = {
1451 .wr_table = <c2983_reg_table,
1452 .rd_table = <c2983_reg_table,
1453 .read_flag_mask = GENMASK(1, 0),
1454 .write_flag_mask = BIT(1),
1457 static const struct iio_info ltc2983_iio_info = {
1458 .read_raw = ltc2983_read_raw,
1459 .debugfs_reg_access = ltc2983_reg_access,
1462 static int ltc2983_probe(struct spi_device *spi)
1464 struct ltc2983_data *st;
1465 struct iio_dev *indio_dev;
1466 const char *name = spi_get_device_id(spi)->name;
1469 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1473 st = iio_priv(indio_dev);
1475 st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config);
1476 if (IS_ERR(st->regmap)) {
1477 dev_err(&spi->dev, "Failed to initialize regmap\n");
1478 return PTR_ERR(st->regmap);
1481 mutex_init(&st->lock);
1482 init_completion(&st->completion);
1484 spi_set_drvdata(spi, st);
1486 ret = ltc2983_parse_dt(st);
1490 st->iio_chan = devm_kzalloc(&spi->dev,
1491 st->iio_channels * sizeof(*st->iio_chan),
1496 ret = ltc2983_setup(st, true);
1500 ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
1501 IRQF_TRIGGER_RISING, name, st);
1503 dev_err(&spi->dev, "failed to request an irq, %d", ret);
1507 indio_dev->name = name;
1508 indio_dev->num_channels = st->iio_channels;
1509 indio_dev->channels = st->iio_chan;
1510 indio_dev->modes = INDIO_DIRECT_MODE;
1511 indio_dev->info = <c2983_iio_info;
1513 return devm_iio_device_register(&spi->dev, indio_dev);
1516 static int __maybe_unused ltc2983_resume(struct device *dev)
1518 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1521 /* dummy read to bring the device out of sleep */
1522 regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
1523 /* we need to re-assign the channels */
1524 return ltc2983_setup(st, false);
1527 static int __maybe_unused ltc2983_suspend(struct device *dev)
1529 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1531 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
1534 static SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend, ltc2983_resume);
1536 static const struct spi_device_id ltc2983_id_table[] = {
1540 MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1542 static const struct of_device_id ltc2983_of_match[] = {
1543 { .compatible = "adi,ltc2983" },
1546 MODULE_DEVICE_TABLE(of, ltc2983_of_match);
1548 static struct spi_driver ltc2983_driver = {
1551 .of_match_table = ltc2983_of_match,
1552 .pm = <c2983_pm_ops,
1554 .probe = ltc2983_probe,
1555 .id_table = ltc2983_id_table,
1558 module_spi_driver(ltc2983_driver);
1560 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1561 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1562 MODULE_LICENSE("GPL");