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)
93 #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0)
94 #define LTC2983_STATUS_CHAN_SEL(x) \
95 FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
97 #define LTC2983_TEMP_UNITS_MASK BIT(2)
98 #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
100 #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0)
101 #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
103 #define LTC2983_RES_VALID_MASK BIT(24)
104 #define LTC2983_DATA_MASK GENMASK(23, 0)
105 #define LTC2983_DATA_SIGN_BIT 23
107 #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27)
108 #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
110 /* cold junction for thermocouples and rsense for rtd's and thermistor's */
111 #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22)
112 #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
114 #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0)
115 #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
117 #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6)
118 #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
120 #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18)
121 #define LTC2983_THERMOCOUPLE_CFG(x) \
122 FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
123 #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29)
124 #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25)
126 #define LTC2983_RTD_CFG_MASK GENMASK(21, 18)
127 #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
128 #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14)
129 #define LTC2983_RTD_EXC_CURRENT(x) \
130 FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
131 #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12)
132 #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
134 #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19)
135 #define LTC2983_THERMISTOR_CFG(x) \
136 FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
137 #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15)
138 #define LTC2983_THERMISTOR_EXC_CURRENT(x) \
139 FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
141 #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24)
142 #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
143 #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22)
144 #define LTC2983_DIODE_EXC_CURRENT(x) \
145 FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
146 #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0)
147 #define LTC2983_DIODE_IDEAL_FACTOR(x) \
148 FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
150 #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0)
151 #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
153 #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26)
154 #define LTC2983_ADC_SINGLE_ENDED(x) \
155 FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
158 LTC2983_SENSOR_THERMOCOUPLE
= 1,
159 LTC2983_SENSOR_THERMOCOUPLE_CUSTOM
= 9,
160 LTC2983_SENSOR_RTD
= 10,
161 LTC2983_SENSOR_RTD_CUSTOM
= 18,
162 LTC2983_SENSOR_THERMISTOR
= 19,
163 LTC2983_SENSOR_THERMISTOR_STEINHART
= 26,
164 LTC2983_SENSOR_THERMISTOR_CUSTOM
= 27,
165 LTC2983_SENSOR_DIODE
= 28,
166 LTC2983_SENSOR_SENSE_RESISTOR
= 29,
167 LTC2983_SENSOR_DIRECT_ADC
= 30,
170 #define to_thermocouple(_sensor) \
171 container_of(_sensor, struct ltc2983_thermocouple, sensor)
173 #define to_rtd(_sensor) \
174 container_of(_sensor, struct ltc2983_rtd, sensor)
176 #define to_thermistor(_sensor) \
177 container_of(_sensor, struct ltc2983_thermistor, sensor)
179 #define to_diode(_sensor) \
180 container_of(_sensor, struct ltc2983_diode, sensor)
182 #define to_rsense(_sensor) \
183 container_of(_sensor, struct ltc2983_rsense, sensor)
185 #define to_adc(_sensor) \
186 container_of(_sensor, struct ltc2983_adc, sensor)
188 struct ltc2983_data
{
189 struct regmap
*regmap
;
190 struct spi_device
*spi
;
192 struct completion completion
;
193 struct iio_chan_spec
*iio_chan
;
194 struct ltc2983_sensor
**sensors
;
195 u32 mux_delay_config
;
196 u32 filter_notch_freq
;
197 u16 custom_table_size
;
201 * DMA (thus cache coherency maintenance) requires the
202 * transfer buffers to live in their own cache lines.
203 * Holds the converted temperature
205 __be32 temp ____cacheline_aligned
;
208 struct ltc2983_sensor
{
209 int (*fault_handler
)(const struct ltc2983_data
*st
, const u32 result
);
210 int (*assign_chan
)(struct ltc2983_data
*st
,
211 const struct ltc2983_sensor
*sensor
);
212 /* specifies the sensor channel */
218 struct ltc2983_custom_sensor
{
219 /* raw table sensor data */
227 struct ltc2983_thermocouple
{
228 struct ltc2983_sensor sensor
;
229 struct ltc2983_custom_sensor
*custom
;
231 u32 cold_junction_chan
;
235 struct ltc2983_sensor sensor
;
236 struct ltc2983_custom_sensor
*custom
;
239 u32 excitation_current
;
243 struct ltc2983_thermistor
{
244 struct ltc2983_sensor sensor
;
245 struct ltc2983_custom_sensor
*custom
;
248 u32 excitation_current
;
251 struct ltc2983_diode
{
252 struct ltc2983_sensor sensor
;
254 u32 excitation_current
;
255 u32 ideal_factor_value
;
258 struct ltc2983_rsense
{
259 struct ltc2983_sensor sensor
;
264 struct ltc2983_sensor sensor
;
269 * Convert to Q format numbers. These number's are integers where
270 * the number of integer and fractional bits are specified. The resolution
271 * is given by 1/@resolution and tell us the number of fractional bits. For
272 * instance a resolution of 2^-10 means we have 10 fractional bits.
274 static u32
__convert_to_raw(const u64 val
, const u32 resolution
)
276 u64 __res
= val
* resolution
;
278 /* all values are multiplied by 1000000 to remove the fraction */
279 do_div(__res
, 1000000);
284 static u32
__convert_to_raw_sign(const u64 val
, const u32 resolution
)
286 s64 __res
= -(s32
)val
;
288 __res
= __convert_to_raw(__res
, resolution
);
293 static int __ltc2983_fault_handler(const struct ltc2983_data
*st
,
294 const u32 result
, const u32 hard_mask
,
297 const struct device
*dev
= &st
->spi
->dev
;
299 if (result
& hard_mask
) {
300 dev_err(dev
, "Invalid conversion: Sensor HARD fault\n");
302 } else if (result
& soft_mask
) {
303 /* just print a warning */
304 dev_warn(dev
, "Suspicious conversion: Sensor SOFT fault\n");
310 static int __ltc2983_chan_assign_common(const struct ltc2983_data
*st
,
311 const struct ltc2983_sensor
*sensor
,
314 u32 reg
= LTC2983_CHAN_START_ADDR(sensor
->chan
);
317 chan_val
|= LTC2983_CHAN_TYPE(sensor
->type
);
318 dev_dbg(&st
->spi
->dev
, "Assign reg:0x%04X, val:0x%08X\n", reg
,
320 __chan_val
= cpu_to_be32(chan_val
);
321 return regmap_bulk_write(st
->regmap
, reg
, &__chan_val
,
325 static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data
*st
,
326 struct ltc2983_custom_sensor
*custom
,
330 u8 mult
= custom
->is_steinhart
? LTC2983_CUSTOM_STEINHART_ENTRY_SZ
:
331 LTC2983_CUSTOM_SENSOR_ENTRY_SZ
;
332 const struct device
*dev
= &st
->spi
->dev
;
334 * custom->size holds the raw size of the table. However, when
335 * configuring the sensor channel, we must write the number of
336 * entries of the table minus 1. For steinhart sensors 0 is written
337 * since the size is constant!
339 const u8 len
= custom
->is_steinhart
? 0 :
340 (custom
->size
/ LTC2983_CUSTOM_SENSOR_ENTRY_SZ
) - 1;
342 * Check if the offset was assigned already. It should be for steinhart
343 * sensors. When coming from sleep, it should be assigned for all.
345 if (custom
->offset
< 0) {
347 * This needs to be done again here because, from the moment
348 * when this test was done (successfully) for this custom
349 * sensor, a steinhart sensor might have been added changing
350 * custom_table_size...
352 if (st
->custom_table_size
+ custom
->size
>
353 (LTC2983_CUST_SENS_TBL_END_REG
-
354 LTC2983_CUST_SENS_TBL_START_REG
) + 1) {
356 "Not space left(%d) for new custom sensor(%zu)",
357 st
->custom_table_size
,
362 custom
->offset
= st
->custom_table_size
/
363 LTC2983_CUSTOM_SENSOR_ENTRY_SZ
;
364 st
->custom_table_size
+= custom
->size
;
367 reg
= (custom
->offset
* mult
) + LTC2983_CUST_SENS_TBL_START_REG
;
369 *chan_val
|= LTC2983_CUSTOM_LEN(len
);
370 *chan_val
|= LTC2983_CUSTOM_ADDR(custom
->offset
);
371 dev_dbg(dev
, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
374 /* write custom sensor table */
375 return regmap_bulk_write(st
->regmap
, reg
, custom
->table
, custom
->size
);
378 static struct ltc2983_custom_sensor
*__ltc2983_custom_sensor_new(
379 struct ltc2983_data
*st
,
380 const struct device_node
*np
,
381 const char *propname
,
382 const bool is_steinhart
,
383 const u32 resolution
,
384 const bool has_signed
)
386 struct ltc2983_custom_sensor
*new_custom
;
387 u8 index
, n_entries
, tbl
= 0;
388 struct device
*dev
= &st
->spi
->dev
;
390 * For custom steinhart, the full u32 is taken. For all the others
391 * the MSB is discarded.
393 const u8 n_size
= (is_steinhart
== true) ? 4 : 3;
394 const u8 e_size
= (is_steinhart
== true) ? sizeof(u32
) : sizeof(u64
);
396 n_entries
= of_property_count_elems_of_size(np
, propname
, e_size
);
397 /* n_entries must be an even number */
398 if (!n_entries
|| (n_entries
% 2) != 0) {
399 dev_err(dev
, "Number of entries either 0 or not even\n");
400 return ERR_PTR(-EINVAL
);
403 new_custom
= devm_kzalloc(dev
, sizeof(*new_custom
), GFP_KERNEL
);
405 return ERR_PTR(-ENOMEM
);
407 new_custom
->size
= n_entries
* n_size
;
408 /* check Steinhart size */
409 if (is_steinhart
&& new_custom
->size
!= LTC2983_CUSTOM_STEINHART_SIZE
) {
410 dev_err(dev
, "Steinhart sensors size(%zu) must be 24",
412 return ERR_PTR(-EINVAL
);
414 /* Check space on the table. */
415 if (st
->custom_table_size
+ new_custom
->size
>
416 (LTC2983_CUST_SENS_TBL_END_REG
-
417 LTC2983_CUST_SENS_TBL_START_REG
) + 1) {
418 dev_err(dev
, "No space left(%d) for new custom sensor(%zu)",
419 st
->custom_table_size
, new_custom
->size
);
420 return ERR_PTR(-EINVAL
);
423 /* allocate the table */
424 new_custom
->table
= devm_kzalloc(dev
, new_custom
->size
, GFP_KERNEL
);
425 if (!new_custom
->table
)
426 return ERR_PTR(-ENOMEM
);
428 for (index
= 0; index
< n_entries
; index
++) {
431 * Steinhart sensors are configured with raw values in the
432 * devicetree. For the other sensors we must convert the
433 * value to raw. The odd index's correspond to temperarures
434 * and always have 1/1024 of resolution. Temperatures also
435 * come in kelvin, so signed values is not possible
438 of_property_read_u64_index(np
, propname
, index
, &temp
);
440 if ((index
% 2) != 0)
441 temp
= __convert_to_raw(temp
, 1024);
442 else if (has_signed
&& (s64
)temp
< 0)
443 temp
= __convert_to_raw_sign(temp
, resolution
);
445 temp
= __convert_to_raw(temp
, resolution
);
449 of_property_read_u32_index(np
, propname
, index
, &t32
);
453 for (j
= 0; j
< n_size
; j
++)
454 new_custom
->table
[tbl
++] =
455 temp
>> (8 * (n_size
- j
- 1));
458 new_custom
->is_steinhart
= is_steinhart
;
460 * This is done to first add all the steinhart sensors to the table,
461 * in order to maximize the table usage. If we mix adding steinhart
462 * with the other sensors, we might have to do some roundup to make
463 * sure that sensor_addr - 0x250(start address) is a multiple of 4
464 * (for steinhart), and a multiple of 6 for all the other sensors.
465 * Since we have const 24 bytes for steinhart sensors and 24 is
466 * also a multiple of 6, we guarantee that the first non-steinhart
467 * sensor will sit in a correct address without the need of filling
471 new_custom
->offset
= st
->custom_table_size
/
472 LTC2983_CUSTOM_STEINHART_ENTRY_SZ
;
473 st
->custom_table_size
+= new_custom
->size
;
475 /* mark as unset. This is checked later on the assign phase */
476 new_custom
->offset
= -1;
482 static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data
*st
,
485 return __ltc2983_fault_handler(st
, result
,
486 LTC2983_THERMOCOUPLE_HARD_FAULT_MASK
,
487 LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK
);
490 static int ltc2983_common_fault_handler(const struct ltc2983_data
*st
,
493 return __ltc2983_fault_handler(st
, result
,
494 LTC2983_COMMON_HARD_FAULT_MASK
,
495 LTC2983_COMMON_SOFT_FAULT_MASK
);
498 static int ltc2983_thermocouple_assign_chan(struct ltc2983_data
*st
,
499 const struct ltc2983_sensor
*sensor
)
501 struct ltc2983_thermocouple
*thermo
= to_thermocouple(sensor
);
504 chan_val
= LTC2983_CHAN_ASSIGN(thermo
->cold_junction_chan
);
505 chan_val
|= LTC2983_THERMOCOUPLE_CFG(thermo
->sensor_config
);
507 if (thermo
->custom
) {
510 ret
= __ltc2983_chan_custom_sensor_assign(st
, thermo
->custom
,
515 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
518 static int ltc2983_rtd_assign_chan(struct ltc2983_data
*st
,
519 const struct ltc2983_sensor
*sensor
)
521 struct ltc2983_rtd
*rtd
= to_rtd(sensor
);
524 chan_val
= LTC2983_CHAN_ASSIGN(rtd
->r_sense_chan
);
525 chan_val
|= LTC2983_RTD_CFG(rtd
->sensor_config
);
526 chan_val
|= LTC2983_RTD_EXC_CURRENT(rtd
->excitation_current
);
527 chan_val
|= LTC2983_RTD_CURVE(rtd
->rtd_curve
);
532 ret
= __ltc2983_chan_custom_sensor_assign(st
, rtd
->custom
,
537 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
540 static int ltc2983_thermistor_assign_chan(struct ltc2983_data
*st
,
541 const struct ltc2983_sensor
*sensor
)
543 struct ltc2983_thermistor
*thermistor
= to_thermistor(sensor
);
546 chan_val
= LTC2983_CHAN_ASSIGN(thermistor
->r_sense_chan
);
547 chan_val
|= LTC2983_THERMISTOR_CFG(thermistor
->sensor_config
);
549 LTC2983_THERMISTOR_EXC_CURRENT(thermistor
->excitation_current
);
551 if (thermistor
->custom
) {
554 ret
= __ltc2983_chan_custom_sensor_assign(st
,
560 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
563 static int ltc2983_diode_assign_chan(struct ltc2983_data
*st
,
564 const struct ltc2983_sensor
*sensor
)
566 struct ltc2983_diode
*diode
= to_diode(sensor
);
569 chan_val
= LTC2983_DIODE_CFG(diode
->sensor_config
);
570 chan_val
|= LTC2983_DIODE_EXC_CURRENT(diode
->excitation_current
);
571 chan_val
|= LTC2983_DIODE_IDEAL_FACTOR(diode
->ideal_factor_value
);
573 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
576 static int ltc2983_r_sense_assign_chan(struct ltc2983_data
*st
,
577 const struct ltc2983_sensor
*sensor
)
579 struct ltc2983_rsense
*rsense
= to_rsense(sensor
);
582 chan_val
= LTC2983_R_SENSE_VAL(rsense
->r_sense_val
);
584 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
587 static int ltc2983_adc_assign_chan(struct ltc2983_data
*st
,
588 const struct ltc2983_sensor
*sensor
)
590 struct ltc2983_adc
*adc
= to_adc(sensor
);
593 chan_val
= LTC2983_ADC_SINGLE_ENDED(adc
->single_ended
);
595 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
598 static struct ltc2983_sensor
*ltc2983_thermocouple_new(
599 const struct device_node
*child
,
600 struct ltc2983_data
*st
,
601 const struct ltc2983_sensor
*sensor
)
603 struct ltc2983_thermocouple
*thermo
;
604 struct device_node
*phandle
;
608 thermo
= devm_kzalloc(&st
->spi
->dev
, sizeof(*thermo
), GFP_KERNEL
);
610 return ERR_PTR(-ENOMEM
);
612 if (of_property_read_bool(child
, "adi,single-ended"))
613 thermo
->sensor_config
= LTC2983_THERMOCOUPLE_SGL(1);
615 ret
= of_property_read_u32(child
, "adi,sensor-oc-current-microamp",
618 switch (oc_current
) {
620 thermo
->sensor_config
|=
621 LTC2983_THERMOCOUPLE_OC_CURR(0);
624 thermo
->sensor_config
|=
625 LTC2983_THERMOCOUPLE_OC_CURR(1);
628 thermo
->sensor_config
|=
629 LTC2983_THERMOCOUPLE_OC_CURR(2);
632 thermo
->sensor_config
|=
633 LTC2983_THERMOCOUPLE_OC_CURR(3);
636 dev_err(&st
->spi
->dev
,
637 "Invalid open circuit current:%u", oc_current
);
638 return ERR_PTR(-EINVAL
);
641 thermo
->sensor_config
|= LTC2983_THERMOCOUPLE_OC_CHECK(1);
643 /* validate channel index */
644 if (!(thermo
->sensor_config
& LTC2983_THERMOCOUPLE_DIFF_MASK
) &&
645 sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
646 dev_err(&st
->spi
->dev
,
647 "Invalid chann:%d for differential thermocouple",
649 return ERR_PTR(-EINVAL
);
652 phandle
= of_parse_phandle(child
, "adi,cold-junction-handle", 0);
656 ret
= of_property_read_u32(phandle
, "reg",
657 &thermo
->cold_junction_chan
);
660 * This would be catched later but we can just return
661 * the error right away.
663 dev_err(&st
->spi
->dev
, "Property reg must be given\n");
664 of_node_put(phandle
);
665 return ERR_PTR(-EINVAL
);
669 /* check custom sensor */
670 if (sensor
->type
== LTC2983_SENSOR_THERMOCOUPLE_CUSTOM
) {
671 const char *propname
= "adi,custom-thermocouple";
673 thermo
->custom
= __ltc2983_custom_sensor_new(st
, child
,
676 if (IS_ERR(thermo
->custom
)) {
677 of_node_put(phandle
);
678 return ERR_CAST(thermo
->custom
);
682 /* set common parameters */
683 thermo
->sensor
.fault_handler
= ltc2983_thermocouple_fault_handler
;
684 thermo
->sensor
.assign_chan
= ltc2983_thermocouple_assign_chan
;
686 of_node_put(phandle
);
687 return &thermo
->sensor
;
690 static struct ltc2983_sensor
*ltc2983_rtd_new(const struct device_node
*child
,
691 struct ltc2983_data
*st
,
692 const struct ltc2983_sensor
*sensor
)
694 struct ltc2983_rtd
*rtd
;
696 struct device
*dev
= &st
->spi
->dev
;
697 struct device_node
*phandle
;
698 u32 excitation_current
= 0, n_wires
= 0;
700 rtd
= devm_kzalloc(dev
, sizeof(*rtd
), GFP_KERNEL
);
702 return ERR_PTR(-ENOMEM
);
704 phandle
= of_parse_phandle(child
, "adi,rsense-handle", 0);
706 dev_err(dev
, "Property adi,rsense-handle missing or invalid");
707 return ERR_PTR(-EINVAL
);
710 ret
= of_property_read_u32(phandle
, "reg", &rtd
->r_sense_chan
);
712 dev_err(dev
, "Property reg must be given\n");
716 ret
= of_property_read_u32(child
, "adi,number-of-wires", &n_wires
);
720 rtd
->sensor_config
= LTC2983_RTD_N_WIRES(0);
723 rtd
->sensor_config
= LTC2983_RTD_N_WIRES(1);
726 rtd
->sensor_config
= LTC2983_RTD_N_WIRES(2);
729 /* 4 wires, Kelvin Rsense */
730 rtd
->sensor_config
= LTC2983_RTD_N_WIRES(3);
733 dev_err(dev
, "Invalid number of wires:%u\n", n_wires
);
739 if (of_property_read_bool(child
, "adi,rsense-share")) {
740 /* Current rotation is only available with rsense sharing */
741 if (of_property_read_bool(child
, "adi,current-rotate")) {
742 if (n_wires
== 2 || n_wires
== 3) {
744 "Rotation not allowed for 2/3 Wire RTDs");
748 rtd
->sensor_config
|= LTC2983_RTD_C_ROTATE(1);
750 rtd
->sensor_config
|= LTC2983_RTD_R_SHARE(1);
754 * rtd channel indexes are a bit more complicated to validate.
755 * For 4wire RTD with rotation, the channel selection cannot be
756 * >=19 since the chann + 1 is used in this configuration.
757 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
758 * <=1 since chanel - 1 and channel - 2 are used.
760 if (rtd
->sensor_config
& LTC2983_RTD_4_WIRE_MASK
) {
762 u8 min
= LTC2983_DIFFERENTIAL_CHAN_MIN
,
763 max
= LTC2983_MAX_CHANNELS_NR
;
765 if (rtd
->sensor_config
& LTC2983_RTD_ROTATION_MASK
)
766 max
= LTC2983_MAX_CHANNELS_NR
- 1;
768 if (((rtd
->sensor_config
& LTC2983_RTD_KELVIN_R_SENSE_MASK
)
769 == LTC2983_RTD_KELVIN_R_SENSE_MASK
) &&
770 (rtd
->r_sense_chan
<= min
)) {
773 "Invalid rsense chann:%d to use in kelvin rsense",
780 if (sensor
->chan
< min
|| sensor
->chan
> max
) {
781 dev_err(dev
, "Invalid chann:%d for the rtd config",
788 /* same as differential case */
789 if (sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
790 dev_err(&st
->spi
->dev
,
791 "Invalid chann:%d for RTD", sensor
->chan
);
798 /* check custom sensor */
799 if (sensor
->type
== LTC2983_SENSOR_RTD_CUSTOM
) {
800 rtd
->custom
= __ltc2983_custom_sensor_new(st
, child
,
803 if (IS_ERR(rtd
->custom
)) {
804 of_node_put(phandle
);
805 return ERR_CAST(rtd
->custom
);
809 /* set common parameters */
810 rtd
->sensor
.fault_handler
= ltc2983_common_fault_handler
;
811 rtd
->sensor
.assign_chan
= ltc2983_rtd_assign_chan
;
813 ret
= of_property_read_u32(child
, "adi,excitation-current-microamp",
814 &excitation_current
);
817 rtd
->excitation_current
= 1;
819 switch (excitation_current
) {
821 rtd
->excitation_current
= 0x01;
824 rtd
->excitation_current
= 0x02;
827 rtd
->excitation_current
= 0x03;
830 rtd
->excitation_current
= 0x04;
833 rtd
->excitation_current
= 0x05;
836 rtd
->excitation_current
= 0x06;
839 rtd
->excitation_current
= 0x07;
842 rtd
->excitation_current
= 0x08;
845 dev_err(&st
->spi
->dev
,
846 "Invalid value for excitation current(%u)",
853 of_property_read_u32(child
, "adi,rtd-curve", &rtd
->rtd_curve
);
855 of_node_put(phandle
);
858 of_node_put(phandle
);
862 static struct ltc2983_sensor
*ltc2983_thermistor_new(
863 const struct device_node
*child
,
864 struct ltc2983_data
*st
,
865 const struct ltc2983_sensor
*sensor
)
867 struct ltc2983_thermistor
*thermistor
;
868 struct device
*dev
= &st
->spi
->dev
;
869 struct device_node
*phandle
;
870 u32 excitation_current
= 0;
873 thermistor
= devm_kzalloc(dev
, sizeof(*thermistor
), GFP_KERNEL
);
875 return ERR_PTR(-ENOMEM
);
877 phandle
= of_parse_phandle(child
, "adi,rsense-handle", 0);
879 dev_err(dev
, "Property adi,rsense-handle missing or invalid");
880 return ERR_PTR(-EINVAL
);
883 ret
= of_property_read_u32(phandle
, "reg", &thermistor
->r_sense_chan
);
885 dev_err(dev
, "rsense channel must be configured...\n");
889 if (of_property_read_bool(child
, "adi,single-ended")) {
890 thermistor
->sensor_config
= LTC2983_THERMISTOR_SGL(1);
891 } else if (of_property_read_bool(child
, "adi,rsense-share")) {
892 /* rotation is only possible if sharing rsense */
893 if (of_property_read_bool(child
, "adi,current-rotate"))
894 thermistor
->sensor_config
=
895 LTC2983_THERMISTOR_C_ROTATE(1);
897 thermistor
->sensor_config
=
898 LTC2983_THERMISTOR_R_SHARE(1);
900 /* validate channel index */
901 if (!(thermistor
->sensor_config
& LTC2983_THERMISTOR_DIFF_MASK
) &&
902 sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
903 dev_err(&st
->spi
->dev
,
904 "Invalid chann:%d for differential thermistor",
910 /* check custom sensor */
911 if (sensor
->type
>= LTC2983_SENSOR_THERMISTOR_STEINHART
) {
912 bool steinhart
= false;
913 const char *propname
;
915 if (sensor
->type
== LTC2983_SENSOR_THERMISTOR_STEINHART
) {
917 propname
= "adi,custom-steinhart";
919 propname
= "adi,custom-thermistor";
922 thermistor
->custom
= __ltc2983_custom_sensor_new(st
, child
,
926 if (IS_ERR(thermistor
->custom
)) {
927 of_node_put(phandle
);
928 return ERR_CAST(thermistor
->custom
);
931 /* set common parameters */
932 thermistor
->sensor
.fault_handler
= ltc2983_common_fault_handler
;
933 thermistor
->sensor
.assign_chan
= ltc2983_thermistor_assign_chan
;
935 ret
= of_property_read_u32(child
, "adi,excitation-current-nanoamp",
936 &excitation_current
);
938 /* Auto range is not allowed for custom sensors */
939 if (sensor
->type
>= LTC2983_SENSOR_THERMISTOR_STEINHART
)
941 thermistor
->excitation_current
= 0x03;
943 /* default to auto-range */
944 thermistor
->excitation_current
= 0x0c;
946 switch (excitation_current
) {
950 LTC2983_SENSOR_THERMISTOR_STEINHART
) {
951 dev_err(&st
->spi
->dev
,
952 "Auto Range not allowed for custom sensors\n");
956 thermistor
->excitation_current
= 0x0c;
959 thermistor
->excitation_current
= 0x01;
962 thermistor
->excitation_current
= 0x02;
965 thermistor
->excitation_current
= 0x03;
968 thermistor
->excitation_current
= 0x04;
971 thermistor
->excitation_current
= 0x05;
974 thermistor
->excitation_current
= 0x06;
977 thermistor
->excitation_current
= 0x07;
980 thermistor
->excitation_current
= 0x08;
983 thermistor
->excitation_current
= 0x09;
986 thermistor
->excitation_current
= 0x0a;
989 thermistor
->excitation_current
= 0x0b;
992 dev_err(&st
->spi
->dev
,
993 "Invalid value for excitation current(%u)",
1000 of_node_put(phandle
);
1001 return &thermistor
->sensor
;
1003 of_node_put(phandle
);
1004 return ERR_PTR(ret
);
1007 static struct ltc2983_sensor
*ltc2983_diode_new(
1008 const struct device_node
*child
,
1009 const struct ltc2983_data
*st
,
1010 const struct ltc2983_sensor
*sensor
)
1012 struct ltc2983_diode
*diode
;
1013 u32 temp
= 0, excitation_current
= 0;
1016 diode
= devm_kzalloc(&st
->spi
->dev
, sizeof(*diode
), GFP_KERNEL
);
1018 return ERR_PTR(-ENOMEM
);
1020 if (of_property_read_bool(child
, "adi,single-ended"))
1021 diode
->sensor_config
= LTC2983_DIODE_SGL(1);
1023 if (of_property_read_bool(child
, "adi,three-conversion-cycles"))
1024 diode
->sensor_config
|= LTC2983_DIODE_3_CONV_CYCLE(1);
1026 if (of_property_read_bool(child
, "adi,average-on"))
1027 diode
->sensor_config
|= LTC2983_DIODE_AVERAGE_ON(1);
1029 /* validate channel index */
1030 if (!(diode
->sensor_config
& LTC2983_DIODE_DIFF_MASK
) &&
1031 sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
1032 dev_err(&st
->spi
->dev
,
1033 "Invalid chann:%d for differential thermistor",
1035 return ERR_PTR(-EINVAL
);
1037 /* set common parameters */
1038 diode
->sensor
.fault_handler
= ltc2983_common_fault_handler
;
1039 diode
->sensor
.assign_chan
= ltc2983_diode_assign_chan
;
1041 ret
= of_property_read_u32(child
, "adi,excitation-current-microamp",
1042 &excitation_current
);
1044 switch (excitation_current
) {
1046 diode
->excitation_current
= 0x00;
1049 diode
->excitation_current
= 0x01;
1052 diode
->excitation_current
= 0x02;
1055 diode
->excitation_current
= 0x03;
1058 dev_err(&st
->spi
->dev
,
1059 "Invalid value for excitation current(%u)",
1060 excitation_current
);
1061 return ERR_PTR(-EINVAL
);
1065 of_property_read_u32(child
, "adi,ideal-factor-value", &temp
);
1067 /* 2^20 resolution */
1068 diode
->ideal_factor_value
= __convert_to_raw(temp
, 1048576);
1070 return &diode
->sensor
;
1073 static struct ltc2983_sensor
*ltc2983_r_sense_new(struct device_node
*child
,
1074 struct ltc2983_data
*st
,
1075 const struct ltc2983_sensor
*sensor
)
1077 struct ltc2983_rsense
*rsense
;
1081 rsense
= devm_kzalloc(&st
->spi
->dev
, sizeof(*rsense
), GFP_KERNEL
);
1083 return ERR_PTR(-ENOMEM
);
1085 /* validate channel index */
1086 if (sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
1087 dev_err(&st
->spi
->dev
, "Invalid chann:%d for r_sense",
1089 return ERR_PTR(-EINVAL
);
1092 ret
= of_property_read_u32(child
, "adi,rsense-val-milli-ohms", &temp
);
1094 dev_err(&st
->spi
->dev
, "Property adi,rsense-val-milli-ohms missing\n");
1095 return ERR_PTR(-EINVAL
);
1098 * Times 1000 because we have milli-ohms and __convert_to_raw
1099 * expects scales of 1000000 which are used for all other
1103 rsense
->r_sense_val
= __convert_to_raw((u64
)temp
* 1000, 1024);
1105 /* set common parameters */
1106 rsense
->sensor
.assign_chan
= ltc2983_r_sense_assign_chan
;
1108 return &rsense
->sensor
;
1111 static struct ltc2983_sensor
*ltc2983_adc_new(struct device_node
*child
,
1112 struct ltc2983_data
*st
,
1113 const struct ltc2983_sensor
*sensor
)
1115 struct ltc2983_adc
*adc
;
1117 adc
= devm_kzalloc(&st
->spi
->dev
, sizeof(*adc
), GFP_KERNEL
);
1119 return ERR_PTR(-ENOMEM
);
1121 if (of_property_read_bool(child
, "adi,single-ended"))
1122 adc
->single_ended
= true;
1124 if (!adc
->single_ended
&&
1125 sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
1126 dev_err(&st
->spi
->dev
, "Invalid chan:%d for differential adc\n",
1128 return ERR_PTR(-EINVAL
);
1130 /* set common parameters */
1131 adc
->sensor
.assign_chan
= ltc2983_adc_assign_chan
;
1132 adc
->sensor
.fault_handler
= ltc2983_common_fault_handler
;
1134 return &adc
->sensor
;
1137 static int ltc2983_chan_read(struct ltc2983_data
*st
,
1138 const struct ltc2983_sensor
*sensor
, int *val
)
1140 u32 start_conversion
= 0;
1144 start_conversion
= LTC2983_STATUS_START(true);
1145 start_conversion
|= LTC2983_STATUS_CHAN_SEL(sensor
->chan
);
1146 dev_dbg(&st
->spi
->dev
, "Start conversion on chan:%d, status:%02X\n",
1147 sensor
->chan
, start_conversion
);
1148 /* start conversion */
1149 ret
= regmap_write(st
->regmap
, LTC2983_STATUS_REG
, start_conversion
);
1153 reinit_completion(&st
->completion
);
1155 * wait for conversion to complete.
1156 * 300 ms should be more than enough to complete the conversion.
1157 * Depending on the sensor configuration, there are 2/3 conversions
1160 time
= wait_for_completion_timeout(&st
->completion
,
1161 msecs_to_jiffies(300));
1163 dev_warn(&st
->spi
->dev
, "Conversion timed out\n");
1167 /* read the converted data */
1168 ret
= regmap_bulk_read(st
->regmap
, LTC2983_CHAN_RES_ADDR(sensor
->chan
),
1169 &st
->temp
, sizeof(st
->temp
));
1173 *val
= __be32_to_cpu(st
->temp
);
1175 if (!(LTC2983_RES_VALID_MASK
& *val
)) {
1176 dev_err(&st
->spi
->dev
, "Invalid conversion detected\n");
1180 ret
= sensor
->fault_handler(st
, *val
);
1184 *val
= sign_extend32((*val
) & LTC2983_DATA_MASK
, LTC2983_DATA_SIGN_BIT
);
1188 static int ltc2983_read_raw(struct iio_dev
*indio_dev
,
1189 struct iio_chan_spec
const *chan
,
1190 int *val
, int *val2
, long mask
)
1192 struct ltc2983_data
*st
= iio_priv(indio_dev
);
1196 if (chan
->address
>= st
->num_channels
) {
1197 dev_err(&st
->spi
->dev
, "Invalid chan address:%ld",
1203 case IIO_CHAN_INFO_RAW
:
1204 mutex_lock(&st
->lock
);
1205 ret
= ltc2983_chan_read(st
, st
->sensors
[chan
->address
], val
);
1206 mutex_unlock(&st
->lock
);
1207 return ret
?: IIO_VAL_INT
;
1208 case IIO_CHAN_INFO_SCALE
:
1209 switch (chan
->type
) {
1211 /* value in milli degrees */
1215 return IIO_VAL_FRACTIONAL
;
1217 /* value in millivolt */
1221 return IIO_VAL_FRACTIONAL
;
1230 static int ltc2983_reg_access(struct iio_dev
*indio_dev
,
1232 unsigned int writeval
,
1233 unsigned int *readval
)
1235 struct ltc2983_data
*st
= iio_priv(indio_dev
);
1238 return regmap_read(st
->regmap
, reg
, readval
);
1240 return regmap_write(st
->regmap
, reg
, writeval
);
1243 static irqreturn_t
ltc2983_irq_handler(int irq
, void *data
)
1245 struct ltc2983_data
*st
= data
;
1247 complete(&st
->completion
);
1251 #define LTC2983_CHAN(__type, index, __address) ({ \
1252 struct iio_chan_spec __chan = { \
1256 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1257 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1258 .address = __address, \
1263 static int ltc2983_parse_dt(struct ltc2983_data
*st
)
1265 struct device_node
*child
;
1266 struct device
*dev
= &st
->spi
->dev
;
1267 int ret
= 0, chan
= 0, channel_avail_mask
= 0;
1269 of_property_read_u32(dev
->of_node
, "adi,mux-delay-config-us",
1270 &st
->mux_delay_config
);
1272 of_property_read_u32(dev
->of_node
, "adi,filter-notch-freq",
1273 &st
->filter_notch_freq
);
1275 st
->num_channels
= of_get_available_child_count(dev
->of_node
);
1276 st
->sensors
= devm_kcalloc(dev
, st
->num_channels
, sizeof(*st
->sensors
),
1281 st
->iio_channels
= st
->num_channels
;
1282 for_each_available_child_of_node(dev
->of_node
, child
) {
1283 struct ltc2983_sensor sensor
;
1285 ret
= of_property_read_u32(child
, "reg", &sensor
.chan
);
1287 dev_err(dev
, "reg property must given for child nodes\n");
1291 /* check if we have a valid channel */
1292 if (sensor
.chan
< LTC2983_MIN_CHANNELS_NR
||
1293 sensor
.chan
> LTC2983_MAX_CHANNELS_NR
) {
1295 "chan:%d must be from 1 to 20\n", sensor
.chan
);
1297 } else if (channel_avail_mask
& BIT(sensor
.chan
)) {
1298 dev_err(dev
, "chan:%d already in use\n", sensor
.chan
);
1302 ret
= of_property_read_u32(child
, "adi,sensor-type",
1306 "adi,sensor-type property must given for child nodes\n");
1310 dev_dbg(dev
, "Create new sensor, type %u, chann %u",
1314 if (sensor
.type
>= LTC2983_SENSOR_THERMOCOUPLE
&&
1315 sensor
.type
<= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM
) {
1316 st
->sensors
[chan
] = ltc2983_thermocouple_new(child
, st
,
1318 } else if (sensor
.type
>= LTC2983_SENSOR_RTD
&&
1319 sensor
.type
<= LTC2983_SENSOR_RTD_CUSTOM
) {
1320 st
->sensors
[chan
] = ltc2983_rtd_new(child
, st
, &sensor
);
1321 } else if (sensor
.type
>= LTC2983_SENSOR_THERMISTOR
&&
1322 sensor
.type
<= LTC2983_SENSOR_THERMISTOR_CUSTOM
) {
1323 st
->sensors
[chan
] = ltc2983_thermistor_new(child
, st
,
1325 } else if (sensor
.type
== LTC2983_SENSOR_DIODE
) {
1326 st
->sensors
[chan
] = ltc2983_diode_new(child
, st
,
1328 } else if (sensor
.type
== LTC2983_SENSOR_SENSE_RESISTOR
) {
1329 st
->sensors
[chan
] = ltc2983_r_sense_new(child
, st
,
1331 /* don't add rsense to iio */
1333 } else if (sensor
.type
== LTC2983_SENSOR_DIRECT_ADC
) {
1334 st
->sensors
[chan
] = ltc2983_adc_new(child
, st
, &sensor
);
1336 dev_err(dev
, "Unknown sensor type %d\n", sensor
.type
);
1340 if (IS_ERR(st
->sensors
[chan
])) {
1341 dev_err(dev
, "Failed to create sensor %ld",
1342 PTR_ERR(st
->sensors
[chan
]));
1343 return PTR_ERR(st
->sensors
[chan
]);
1345 /* set generic sensor parameters */
1346 st
->sensors
[chan
]->chan
= sensor
.chan
;
1347 st
->sensors
[chan
]->type
= sensor
.type
;
1349 channel_avail_mask
|= BIT(sensor
.chan
);
1356 static int ltc2983_setup(struct ltc2983_data
*st
, bool assign_iio
)
1358 u32 iio_chan_t
= 0, iio_chan_v
= 0, chan
, iio_idx
= 0;
1362 /* make sure the device is up */
1363 time
= wait_for_completion_timeout(&st
->completion
,
1364 msecs_to_jiffies(250));
1367 dev_err(&st
->spi
->dev
, "Device startup timed out\n");
1371 st
->iio_chan
= devm_kzalloc(&st
->spi
->dev
,
1372 st
->iio_channels
* sizeof(*st
->iio_chan
),
1378 ret
= regmap_update_bits(st
->regmap
, LTC2983_GLOBAL_CONFIG_REG
,
1379 LTC2983_NOTCH_FREQ_MASK
,
1380 LTC2983_NOTCH_FREQ(st
->filter_notch_freq
));
1384 ret
= regmap_write(st
->regmap
, LTC2983_MUX_CONFIG_REG
,
1385 st
->mux_delay_config
);
1389 for (chan
= 0; chan
< st
->num_channels
; chan
++) {
1390 u32 chan_type
= 0, *iio_chan
;
1392 ret
= st
->sensors
[chan
]->assign_chan(st
, st
->sensors
[chan
]);
1396 * The assign_iio flag is necessary for when the device is
1397 * coming out of sleep. In that case, we just need to
1398 * re-configure the device channels.
1399 * We also don't assign iio channels for rsense.
1401 if (st
->sensors
[chan
]->type
== LTC2983_SENSOR_SENSE_RESISTOR
||
1405 /* assign iio channel */
1406 if (st
->sensors
[chan
]->type
!= LTC2983_SENSOR_DIRECT_ADC
) {
1407 chan_type
= IIO_TEMP
;
1408 iio_chan
= &iio_chan_t
;
1410 chan_type
= IIO_VOLTAGE
;
1411 iio_chan
= &iio_chan_v
;
1415 * add chan as the iio .address so that, we can directly
1416 * reference the sensor given the iio_chan_spec
1418 st
->iio_chan
[iio_idx
++] = LTC2983_CHAN(chan_type
, (*iio_chan
)++,
1425 static const struct regmap_range ltc2983_reg_ranges
[] = {
1426 regmap_reg_range(LTC2983_STATUS_REG
, LTC2983_STATUS_REG
),
1427 regmap_reg_range(LTC2983_TEMP_RES_START_REG
, LTC2983_TEMP_RES_END_REG
),
1428 regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG
, LTC2983_GLOBAL_CONFIG_REG
),
1429 regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG
,
1430 LTC2983_MULT_CHANNEL_END_REG
),
1431 regmap_reg_range(LTC2983_MUX_CONFIG_REG
, LTC2983_MUX_CONFIG_REG
),
1432 regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG
,
1433 LTC2983_CHAN_ASSIGN_END_REG
),
1434 regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG
,
1435 LTC2983_CUST_SENS_TBL_END_REG
),
1438 static const struct regmap_access_table ltc2983_reg_table
= {
1439 .yes_ranges
= ltc2983_reg_ranges
,
1440 .n_yes_ranges
= ARRAY_SIZE(ltc2983_reg_ranges
),
1444 * The reg_bits are actually 12 but the device needs the first *complete*
1445 * byte for the command (R/W).
1447 static const struct regmap_config ltc2983_regmap_config
= {
1450 .wr_table
= <c2983_reg_table
,
1451 .rd_table
= <c2983_reg_table
,
1452 .read_flag_mask
= GENMASK(1, 0),
1453 .write_flag_mask
= BIT(1),
1456 static const struct iio_info ltc2983_iio_info
= {
1457 .read_raw
= ltc2983_read_raw
,
1458 .debugfs_reg_access
= ltc2983_reg_access
,
1461 static int ltc2983_probe(struct spi_device
*spi
)
1463 struct ltc2983_data
*st
;
1464 struct iio_dev
*indio_dev
;
1465 const char *name
= spi_get_device_id(spi
)->name
;
1468 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
1472 st
= iio_priv(indio_dev
);
1474 st
->regmap
= devm_regmap_init_spi(spi
, <c2983_regmap_config
);
1475 if (IS_ERR(st
->regmap
)) {
1476 dev_err(&spi
->dev
, "Failed to initialize regmap\n");
1477 return PTR_ERR(st
->regmap
);
1480 mutex_init(&st
->lock
);
1481 init_completion(&st
->completion
);
1483 spi_set_drvdata(spi
, st
);
1485 ret
= ltc2983_parse_dt(st
);
1489 * let's request the irq now so it is used to sync the device
1490 * startup in ltc2983_setup()
1492 ret
= devm_request_irq(&spi
->dev
, spi
->irq
, ltc2983_irq_handler
,
1493 IRQF_TRIGGER_RISING
, name
, st
);
1495 dev_err(&spi
->dev
, "failed to request an irq, %d", ret
);
1499 ret
= ltc2983_setup(st
, true);
1503 indio_dev
->dev
.parent
= &spi
->dev
;
1504 indio_dev
->name
= name
;
1505 indio_dev
->num_channels
= st
->iio_channels
;
1506 indio_dev
->channels
= st
->iio_chan
;
1507 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1508 indio_dev
->info
= <c2983_iio_info
;
1510 return devm_iio_device_register(&spi
->dev
, indio_dev
);
1513 static int __maybe_unused
ltc2983_resume(struct device
*dev
)
1515 struct ltc2983_data
*st
= spi_get_drvdata(to_spi_device(dev
));
1518 /* dummy read to bring the device out of sleep */
1519 regmap_read(st
->regmap
, LTC2983_STATUS_REG
, &dummy
);
1520 /* we need to re-assign the channels */
1521 return ltc2983_setup(st
, false);
1524 static int __maybe_unused
ltc2983_suspend(struct device
*dev
)
1526 struct ltc2983_data
*st
= spi_get_drvdata(to_spi_device(dev
));
1528 return regmap_write(st
->regmap
, LTC2983_STATUS_REG
, LTC2983_SLEEP
);
1531 static SIMPLE_DEV_PM_OPS(ltc2983_pm_ops
, ltc2983_suspend
, ltc2983_resume
);
1533 static const struct spi_device_id ltc2983_id_table
[] = {
1537 MODULE_DEVICE_TABLE(spi
, ltc2983_id_table
);
1539 static const struct of_device_id ltc2983_of_match
[] = {
1540 { .compatible
= "adi,ltc2983" },
1543 MODULE_DEVICE_TABLE(of
, ltc2983_of_match
);
1545 static struct spi_driver ltc2983_driver
= {
1548 .of_match_table
= ltc2983_of_match
,
1549 .pm
= <c2983_pm_ops
,
1551 .probe
= ltc2983_probe
,
1552 .id_table
= ltc2983_id_table
,
1555 module_spi_driver(ltc2983_driver
);
1557 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1558 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1559 MODULE_LICENSE("GPL");