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
);
447 of_property_read_u32_index(np
, propname
, index
,
451 for (j
= 0; j
< n_size
; j
++)
452 new_custom
->table
[tbl
++] =
453 temp
>> (8 * (n_size
- j
- 1));
456 new_custom
->is_steinhart
= is_steinhart
;
458 * This is done to first add all the steinhart sensors to the table,
459 * in order to maximize the table usage. If we mix adding steinhart
460 * with the other sensors, we might have to do some roundup to make
461 * sure that sensor_addr - 0x250(start address) is a multiple of 4
462 * (for steinhart), and a multiple of 6 for all the other sensors.
463 * Since we have const 24 bytes for steinhart sensors and 24 is
464 * also a multiple of 6, we guarantee that the first non-steinhart
465 * sensor will sit in a correct address without the need of filling
469 new_custom
->offset
= st
->custom_table_size
/
470 LTC2983_CUSTOM_STEINHART_ENTRY_SZ
;
471 st
->custom_table_size
+= new_custom
->size
;
473 /* mark as unset. This is checked later on the assign phase */
474 new_custom
->offset
= -1;
480 static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data
*st
,
483 return __ltc2983_fault_handler(st
, result
,
484 LTC2983_THERMOCOUPLE_HARD_FAULT_MASK
,
485 LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK
);
488 static int ltc2983_common_fault_handler(const struct ltc2983_data
*st
,
491 return __ltc2983_fault_handler(st
, result
,
492 LTC2983_COMMON_HARD_FAULT_MASK
,
493 LTC2983_COMMON_SOFT_FAULT_MASK
);
496 static int ltc2983_thermocouple_assign_chan(struct ltc2983_data
*st
,
497 const struct ltc2983_sensor
*sensor
)
499 struct ltc2983_thermocouple
*thermo
= to_thermocouple(sensor
);
502 chan_val
= LTC2983_CHAN_ASSIGN(thermo
->cold_junction_chan
);
503 chan_val
|= LTC2983_THERMOCOUPLE_CFG(thermo
->sensor_config
);
505 if (thermo
->custom
) {
508 ret
= __ltc2983_chan_custom_sensor_assign(st
, thermo
->custom
,
513 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
516 static int ltc2983_rtd_assign_chan(struct ltc2983_data
*st
,
517 const struct ltc2983_sensor
*sensor
)
519 struct ltc2983_rtd
*rtd
= to_rtd(sensor
);
522 chan_val
= LTC2983_CHAN_ASSIGN(rtd
->r_sense_chan
);
523 chan_val
|= LTC2983_RTD_CFG(rtd
->sensor_config
);
524 chan_val
|= LTC2983_RTD_EXC_CURRENT(rtd
->excitation_current
);
525 chan_val
|= LTC2983_RTD_CURVE(rtd
->rtd_curve
);
530 ret
= __ltc2983_chan_custom_sensor_assign(st
, rtd
->custom
,
535 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
538 static int ltc2983_thermistor_assign_chan(struct ltc2983_data
*st
,
539 const struct ltc2983_sensor
*sensor
)
541 struct ltc2983_thermistor
*thermistor
= to_thermistor(sensor
);
544 chan_val
= LTC2983_CHAN_ASSIGN(thermistor
->r_sense_chan
);
545 chan_val
|= LTC2983_THERMISTOR_CFG(thermistor
->sensor_config
);
547 LTC2983_THERMISTOR_EXC_CURRENT(thermistor
->excitation_current
);
549 if (thermistor
->custom
) {
552 ret
= __ltc2983_chan_custom_sensor_assign(st
,
558 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
561 static int ltc2983_diode_assign_chan(struct ltc2983_data
*st
,
562 const struct ltc2983_sensor
*sensor
)
564 struct ltc2983_diode
*diode
= to_diode(sensor
);
567 chan_val
= LTC2983_DIODE_CFG(diode
->sensor_config
);
568 chan_val
|= LTC2983_DIODE_EXC_CURRENT(diode
->excitation_current
);
569 chan_val
|= LTC2983_DIODE_IDEAL_FACTOR(diode
->ideal_factor_value
);
571 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
574 static int ltc2983_r_sense_assign_chan(struct ltc2983_data
*st
,
575 const struct ltc2983_sensor
*sensor
)
577 struct ltc2983_rsense
*rsense
= to_rsense(sensor
);
580 chan_val
= LTC2983_R_SENSE_VAL(rsense
->r_sense_val
);
582 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
585 static int ltc2983_adc_assign_chan(struct ltc2983_data
*st
,
586 const struct ltc2983_sensor
*sensor
)
588 struct ltc2983_adc
*adc
= to_adc(sensor
);
591 chan_val
= LTC2983_ADC_SINGLE_ENDED(adc
->single_ended
);
593 return __ltc2983_chan_assign_common(st
, sensor
, chan_val
);
596 static struct ltc2983_sensor
*ltc2983_thermocouple_new(
597 const struct device_node
*child
,
598 struct ltc2983_data
*st
,
599 const struct ltc2983_sensor
*sensor
)
601 struct ltc2983_thermocouple
*thermo
;
602 struct device_node
*phandle
;
606 thermo
= devm_kzalloc(&st
->spi
->dev
, sizeof(*thermo
), GFP_KERNEL
);
608 return ERR_PTR(-ENOMEM
);
610 if (of_property_read_bool(child
, "adi,single-ended"))
611 thermo
->sensor_config
= LTC2983_THERMOCOUPLE_SGL(1);
613 ret
= of_property_read_u32(child
, "adi,sensor-oc-current-microamp",
616 switch (oc_current
) {
618 thermo
->sensor_config
|=
619 LTC2983_THERMOCOUPLE_OC_CURR(0);
622 thermo
->sensor_config
|=
623 LTC2983_THERMOCOUPLE_OC_CURR(1);
626 thermo
->sensor_config
|=
627 LTC2983_THERMOCOUPLE_OC_CURR(2);
630 thermo
->sensor_config
|=
631 LTC2983_THERMOCOUPLE_OC_CURR(3);
634 dev_err(&st
->spi
->dev
,
635 "Invalid open circuit current:%u", oc_current
);
636 return ERR_PTR(-EINVAL
);
639 thermo
->sensor_config
|= LTC2983_THERMOCOUPLE_OC_CHECK(1);
641 /* validate channel index */
642 if (!(thermo
->sensor_config
& LTC2983_THERMOCOUPLE_DIFF_MASK
) &&
643 sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
644 dev_err(&st
->spi
->dev
,
645 "Invalid chann:%d for differential thermocouple",
647 return ERR_PTR(-EINVAL
);
650 phandle
= of_parse_phandle(child
, "adi,cold-junction-handle", 0);
654 ret
= of_property_read_u32(phandle
, "reg",
655 &thermo
->cold_junction_chan
);
658 * This would be catched later but we can just return
659 * the error right away.
661 dev_err(&st
->spi
->dev
, "Property reg must be given\n");
662 of_node_put(phandle
);
663 return ERR_PTR(-EINVAL
);
667 /* check custom sensor */
668 if (sensor
->type
== LTC2983_SENSOR_THERMOCOUPLE_CUSTOM
) {
669 const char *propname
= "adi,custom-thermocouple";
671 thermo
->custom
= __ltc2983_custom_sensor_new(st
, child
,
674 if (IS_ERR(thermo
->custom
)) {
675 of_node_put(phandle
);
676 return ERR_CAST(thermo
->custom
);
680 /* set common parameters */
681 thermo
->sensor
.fault_handler
= ltc2983_thermocouple_fault_handler
;
682 thermo
->sensor
.assign_chan
= ltc2983_thermocouple_assign_chan
;
684 of_node_put(phandle
);
685 return &thermo
->sensor
;
688 static struct ltc2983_sensor
*ltc2983_rtd_new(const struct device_node
*child
,
689 struct ltc2983_data
*st
,
690 const struct ltc2983_sensor
*sensor
)
692 struct ltc2983_rtd
*rtd
;
694 struct device
*dev
= &st
->spi
->dev
;
695 struct device_node
*phandle
;
696 u32 excitation_current
= 0, n_wires
= 0;
698 rtd
= devm_kzalloc(dev
, sizeof(*rtd
), GFP_KERNEL
);
700 return ERR_PTR(-ENOMEM
);
702 phandle
= of_parse_phandle(child
, "adi,rsense-handle", 0);
704 dev_err(dev
, "Property adi,rsense-handle missing or invalid");
705 return ERR_PTR(-EINVAL
);
708 ret
= of_property_read_u32(phandle
, "reg", &rtd
->r_sense_chan
);
710 dev_err(dev
, "Property reg must be given\n");
714 ret
= of_property_read_u32(child
, "adi,number-of-wires", &n_wires
);
718 rtd
->sensor_config
= LTC2983_RTD_N_WIRES(0);
721 rtd
->sensor_config
= LTC2983_RTD_N_WIRES(1);
724 rtd
->sensor_config
= LTC2983_RTD_N_WIRES(2);
727 /* 4 wires, Kelvin Rsense */
728 rtd
->sensor_config
= LTC2983_RTD_N_WIRES(3);
731 dev_err(dev
, "Invalid number of wires:%u\n", n_wires
);
737 if (of_property_read_bool(child
, "adi,rsense-share")) {
738 /* Current rotation is only available with rsense sharing */
739 if (of_property_read_bool(child
, "adi,current-rotate")) {
740 if (n_wires
== 2 || n_wires
== 3) {
742 "Rotation not allowed for 2/3 Wire RTDs");
746 rtd
->sensor_config
|= LTC2983_RTD_C_ROTATE(1);
748 rtd
->sensor_config
|= LTC2983_RTD_R_SHARE(1);
752 * rtd channel indexes are a bit more complicated to validate.
753 * For 4wire RTD with rotation, the channel selection cannot be
754 * >=19 since the chann + 1 is used in this configuration.
755 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
756 * <=1 since chanel - 1 and channel - 2 are used.
758 if (rtd
->sensor_config
& LTC2983_RTD_4_WIRE_MASK
) {
760 u8 min
= LTC2983_DIFFERENTIAL_CHAN_MIN
,
761 max
= LTC2983_MAX_CHANNELS_NR
;
763 if (rtd
->sensor_config
& LTC2983_RTD_ROTATION_MASK
)
764 max
= LTC2983_MAX_CHANNELS_NR
- 1;
766 if (((rtd
->sensor_config
& LTC2983_RTD_KELVIN_R_SENSE_MASK
)
767 == LTC2983_RTD_KELVIN_R_SENSE_MASK
) &&
768 (rtd
->r_sense_chan
<= min
)) {
771 "Invalid rsense chann:%d to use in kelvin rsense",
778 if (sensor
->chan
< min
|| sensor
->chan
> max
) {
779 dev_err(dev
, "Invalid chann:%d for the rtd config",
786 /* same as differential case */
787 if (sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
788 dev_err(&st
->spi
->dev
,
789 "Invalid chann:%d for RTD", sensor
->chan
);
796 /* check custom sensor */
797 if (sensor
->type
== LTC2983_SENSOR_RTD_CUSTOM
) {
798 rtd
->custom
= __ltc2983_custom_sensor_new(st
, child
,
801 if (IS_ERR(rtd
->custom
)) {
802 of_node_put(phandle
);
803 return ERR_CAST(rtd
->custom
);
807 /* set common parameters */
808 rtd
->sensor
.fault_handler
= ltc2983_common_fault_handler
;
809 rtd
->sensor
.assign_chan
= ltc2983_rtd_assign_chan
;
811 ret
= of_property_read_u32(child
, "adi,excitation-current-microamp",
812 &excitation_current
);
815 rtd
->excitation_current
= 1;
817 switch (excitation_current
) {
819 rtd
->excitation_current
= 0x01;
822 rtd
->excitation_current
= 0x02;
825 rtd
->excitation_current
= 0x03;
828 rtd
->excitation_current
= 0x04;
831 rtd
->excitation_current
= 0x05;
834 rtd
->excitation_current
= 0x06;
837 rtd
->excitation_current
= 0x07;
840 rtd
->excitation_current
= 0x08;
843 dev_err(&st
->spi
->dev
,
844 "Invalid value for excitation current(%u)",
851 of_property_read_u32(child
, "adi,rtd-curve", &rtd
->rtd_curve
);
853 of_node_put(phandle
);
856 of_node_put(phandle
);
860 static struct ltc2983_sensor
*ltc2983_thermistor_new(
861 const struct device_node
*child
,
862 struct ltc2983_data
*st
,
863 const struct ltc2983_sensor
*sensor
)
865 struct ltc2983_thermistor
*thermistor
;
866 struct device
*dev
= &st
->spi
->dev
;
867 struct device_node
*phandle
;
868 u32 excitation_current
= 0;
871 thermistor
= devm_kzalloc(dev
, sizeof(*thermistor
), GFP_KERNEL
);
873 return ERR_PTR(-ENOMEM
);
875 phandle
= of_parse_phandle(child
, "adi,rsense-handle", 0);
877 dev_err(dev
, "Property adi,rsense-handle missing or invalid");
878 return ERR_PTR(-EINVAL
);
881 ret
= of_property_read_u32(phandle
, "reg", &thermistor
->r_sense_chan
);
883 dev_err(dev
, "rsense channel must be configured...\n");
887 if (of_property_read_bool(child
, "adi,single-ended")) {
888 thermistor
->sensor_config
= LTC2983_THERMISTOR_SGL(1);
889 } else if (of_property_read_bool(child
, "adi,rsense-share")) {
890 /* rotation is only possible if sharing rsense */
891 if (of_property_read_bool(child
, "adi,current-rotate"))
892 thermistor
->sensor_config
=
893 LTC2983_THERMISTOR_C_ROTATE(1);
895 thermistor
->sensor_config
=
896 LTC2983_THERMISTOR_R_SHARE(1);
898 /* validate channel index */
899 if (!(thermistor
->sensor_config
& LTC2983_THERMISTOR_DIFF_MASK
) &&
900 sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
901 dev_err(&st
->spi
->dev
,
902 "Invalid chann:%d for differential thermistor",
908 /* check custom sensor */
909 if (sensor
->type
>= LTC2983_SENSOR_THERMISTOR_STEINHART
) {
910 bool steinhart
= false;
911 const char *propname
;
913 if (sensor
->type
== LTC2983_SENSOR_THERMISTOR_STEINHART
) {
915 propname
= "adi,custom-steinhart";
917 propname
= "adi,custom-thermistor";
920 thermistor
->custom
= __ltc2983_custom_sensor_new(st
, child
,
924 if (IS_ERR(thermistor
->custom
)) {
925 of_node_put(phandle
);
926 return ERR_CAST(thermistor
->custom
);
929 /* set common parameters */
930 thermistor
->sensor
.fault_handler
= ltc2983_common_fault_handler
;
931 thermistor
->sensor
.assign_chan
= ltc2983_thermistor_assign_chan
;
933 ret
= of_property_read_u32(child
, "adi,excitation-current-nanoamp",
934 &excitation_current
);
936 /* Auto range is not allowed for custom sensors */
937 if (sensor
->type
>= LTC2983_SENSOR_THERMISTOR_STEINHART
)
939 thermistor
->excitation_current
= 0x03;
941 /* default to auto-range */
942 thermistor
->excitation_current
= 0x0c;
944 switch (excitation_current
) {
948 LTC2983_SENSOR_THERMISTOR_STEINHART
) {
949 dev_err(&st
->spi
->dev
,
950 "Auto Range not allowed for custom sensors\n");
954 thermistor
->excitation_current
= 0x0c;
957 thermistor
->excitation_current
= 0x01;
960 thermistor
->excitation_current
= 0x02;
963 thermistor
->excitation_current
= 0x03;
966 thermistor
->excitation_current
= 0x04;
969 thermistor
->excitation_current
= 0x05;
972 thermistor
->excitation_current
= 0x06;
975 thermistor
->excitation_current
= 0x07;
978 thermistor
->excitation_current
= 0x08;
981 thermistor
->excitation_current
= 0x09;
984 thermistor
->excitation_current
= 0x0a;
987 thermistor
->excitation_current
= 0x0b;
990 dev_err(&st
->spi
->dev
,
991 "Invalid value for excitation current(%u)",
998 of_node_put(phandle
);
999 return &thermistor
->sensor
;
1001 of_node_put(phandle
);
1002 return ERR_PTR(ret
);
1005 static struct ltc2983_sensor
*ltc2983_diode_new(
1006 const struct device_node
*child
,
1007 const struct ltc2983_data
*st
,
1008 const struct ltc2983_sensor
*sensor
)
1010 struct ltc2983_diode
*diode
;
1011 u32 temp
= 0, excitation_current
= 0;
1014 diode
= devm_kzalloc(&st
->spi
->dev
, sizeof(*diode
), GFP_KERNEL
);
1016 return ERR_PTR(-ENOMEM
);
1018 if (of_property_read_bool(child
, "adi,single-ended"))
1019 diode
->sensor_config
= LTC2983_DIODE_SGL(1);
1021 if (of_property_read_bool(child
, "adi,three-conversion-cycles"))
1022 diode
->sensor_config
|= LTC2983_DIODE_3_CONV_CYCLE(1);
1024 if (of_property_read_bool(child
, "adi,average-on"))
1025 diode
->sensor_config
|= LTC2983_DIODE_AVERAGE_ON(1);
1027 /* validate channel index */
1028 if (!(diode
->sensor_config
& LTC2983_DIODE_DIFF_MASK
) &&
1029 sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
1030 dev_err(&st
->spi
->dev
,
1031 "Invalid chann:%d for differential thermistor",
1033 return ERR_PTR(-EINVAL
);
1035 /* set common parameters */
1036 diode
->sensor
.fault_handler
= ltc2983_common_fault_handler
;
1037 diode
->sensor
.assign_chan
= ltc2983_diode_assign_chan
;
1039 ret
= of_property_read_u32(child
, "adi,excitation-current-microamp",
1040 &excitation_current
);
1042 switch (excitation_current
) {
1044 diode
->excitation_current
= 0x00;
1047 diode
->excitation_current
= 0x01;
1050 diode
->excitation_current
= 0x02;
1053 diode
->excitation_current
= 0x03;
1056 dev_err(&st
->spi
->dev
,
1057 "Invalid value for excitation current(%u)",
1058 excitation_current
);
1059 return ERR_PTR(-EINVAL
);
1063 of_property_read_u32(child
, "adi,ideal-factor-value", &temp
);
1065 /* 2^20 resolution */
1066 diode
->ideal_factor_value
= __convert_to_raw(temp
, 1048576);
1068 return &diode
->sensor
;
1071 static struct ltc2983_sensor
*ltc2983_r_sense_new(struct device_node
*child
,
1072 struct ltc2983_data
*st
,
1073 const struct ltc2983_sensor
*sensor
)
1075 struct ltc2983_rsense
*rsense
;
1079 rsense
= devm_kzalloc(&st
->spi
->dev
, sizeof(*rsense
), GFP_KERNEL
);
1081 return ERR_PTR(-ENOMEM
);
1083 /* validate channel index */
1084 if (sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
1085 dev_err(&st
->spi
->dev
, "Invalid chann:%d for r_sense",
1087 return ERR_PTR(-EINVAL
);
1090 ret
= of_property_read_u32(child
, "adi,rsense-val-milli-ohms", &temp
);
1092 dev_err(&st
->spi
->dev
, "Property adi,rsense-val-milli-ohms missing\n");
1093 return ERR_PTR(-EINVAL
);
1096 * Times 1000 because we have milli-ohms and __convert_to_raw
1097 * expects scales of 1000000 which are used for all other
1101 rsense
->r_sense_val
= __convert_to_raw((u64
)temp
* 1000, 1024);
1103 /* set common parameters */
1104 rsense
->sensor
.assign_chan
= ltc2983_r_sense_assign_chan
;
1106 return &rsense
->sensor
;
1109 static struct ltc2983_sensor
*ltc2983_adc_new(struct device_node
*child
,
1110 struct ltc2983_data
*st
,
1111 const struct ltc2983_sensor
*sensor
)
1113 struct ltc2983_adc
*adc
;
1115 adc
= devm_kzalloc(&st
->spi
->dev
, sizeof(*adc
), GFP_KERNEL
);
1117 return ERR_PTR(-ENOMEM
);
1119 if (of_property_read_bool(child
, "adi,single-ended"))
1120 adc
->single_ended
= true;
1122 if (!adc
->single_ended
&&
1123 sensor
->chan
< LTC2983_DIFFERENTIAL_CHAN_MIN
) {
1124 dev_err(&st
->spi
->dev
, "Invalid chan:%d for differential adc\n",
1126 return ERR_PTR(-EINVAL
);
1128 /* set common parameters */
1129 adc
->sensor
.assign_chan
= ltc2983_adc_assign_chan
;
1130 adc
->sensor
.fault_handler
= ltc2983_common_fault_handler
;
1132 return &adc
->sensor
;
1135 static int ltc2983_chan_read(struct ltc2983_data
*st
,
1136 const struct ltc2983_sensor
*sensor
, int *val
)
1138 u32 start_conversion
= 0;
1142 start_conversion
= LTC2983_STATUS_START(true);
1143 start_conversion
|= LTC2983_STATUS_CHAN_SEL(sensor
->chan
);
1144 dev_dbg(&st
->spi
->dev
, "Start conversion on chan:%d, status:%02X\n",
1145 sensor
->chan
, start_conversion
);
1146 /* start conversion */
1147 ret
= regmap_write(st
->regmap
, LTC2983_STATUS_REG
, start_conversion
);
1151 reinit_completion(&st
->completion
);
1153 * wait for conversion to complete.
1154 * 300 ms should be more than enough to complete the conversion.
1155 * Depending on the sensor configuration, there are 2/3 conversions
1158 time
= wait_for_completion_timeout(&st
->completion
,
1159 msecs_to_jiffies(300));
1161 dev_warn(&st
->spi
->dev
, "Conversion timed out\n");
1165 /* read the converted data */
1166 ret
= regmap_bulk_read(st
->regmap
, LTC2983_CHAN_RES_ADDR(sensor
->chan
),
1167 &st
->temp
, sizeof(st
->temp
));
1171 *val
= __be32_to_cpu(st
->temp
);
1173 if (!(LTC2983_RES_VALID_MASK
& *val
)) {
1174 dev_err(&st
->spi
->dev
, "Invalid conversion detected\n");
1178 ret
= sensor
->fault_handler(st
, *val
);
1182 *val
= sign_extend32((*val
) & LTC2983_DATA_MASK
, LTC2983_DATA_SIGN_BIT
);
1186 static int ltc2983_read_raw(struct iio_dev
*indio_dev
,
1187 struct iio_chan_spec
const *chan
,
1188 int *val
, int *val2
, long mask
)
1190 struct ltc2983_data
*st
= iio_priv(indio_dev
);
1194 if (chan
->address
>= st
->num_channels
) {
1195 dev_err(&st
->spi
->dev
, "Invalid chan address:%ld",
1201 case IIO_CHAN_INFO_RAW
:
1202 mutex_lock(&st
->lock
);
1203 ret
= ltc2983_chan_read(st
, st
->sensors
[chan
->address
], val
);
1204 mutex_unlock(&st
->lock
);
1205 return ret
?: IIO_VAL_INT
;
1206 case IIO_CHAN_INFO_SCALE
:
1207 switch (chan
->type
) {
1209 /* value in milli degrees */
1213 return IIO_VAL_FRACTIONAL
;
1215 /* value in millivolt */
1219 return IIO_VAL_FRACTIONAL
;
1228 static int ltc2983_reg_access(struct iio_dev
*indio_dev
,
1230 unsigned int writeval
,
1231 unsigned int *readval
)
1233 struct ltc2983_data
*st
= iio_priv(indio_dev
);
1236 return regmap_read(st
->regmap
, reg
, readval
);
1238 return regmap_write(st
->regmap
, reg
, writeval
);
1241 static irqreturn_t
ltc2983_irq_handler(int irq
, void *data
)
1243 struct ltc2983_data
*st
= data
;
1245 complete(&st
->completion
);
1249 #define LTC2983_CHAN(__type, index, __address) ({ \
1250 struct iio_chan_spec __chan = { \
1254 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1255 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1256 .address = __address, \
1261 static int ltc2983_parse_dt(struct ltc2983_data
*st
)
1263 struct device_node
*child
;
1264 struct device
*dev
= &st
->spi
->dev
;
1265 int ret
= 0, chan
= 0, channel_avail_mask
= 0;
1267 of_property_read_u32(dev
->of_node
, "adi,mux-delay-config-us",
1268 &st
->mux_delay_config
);
1270 of_property_read_u32(dev
->of_node
, "adi,filter-notch-freq",
1271 &st
->filter_notch_freq
);
1273 st
->num_channels
= of_get_available_child_count(dev
->of_node
);
1274 st
->sensors
= devm_kcalloc(dev
, st
->num_channels
, sizeof(*st
->sensors
),
1279 st
->iio_channels
= st
->num_channels
;
1280 for_each_available_child_of_node(dev
->of_node
, child
) {
1281 struct ltc2983_sensor sensor
;
1283 ret
= of_property_read_u32(child
, "reg", &sensor
.chan
);
1285 dev_err(dev
, "reg property must given for child nodes\n");
1289 /* check if we have a valid channel */
1290 if (sensor
.chan
< LTC2983_MIN_CHANNELS_NR
||
1291 sensor
.chan
> LTC2983_MAX_CHANNELS_NR
) {
1293 "chan:%d must be from 1 to 20\n", sensor
.chan
);
1295 } else if (channel_avail_mask
& BIT(sensor
.chan
)) {
1296 dev_err(dev
, "chan:%d already in use\n", sensor
.chan
);
1300 ret
= of_property_read_u32(child
, "adi,sensor-type",
1304 "adi,sensor-type property must given for child nodes\n");
1308 dev_dbg(dev
, "Create new sensor, type %u, chann %u",
1312 if (sensor
.type
>= LTC2983_SENSOR_THERMOCOUPLE
&&
1313 sensor
.type
<= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM
) {
1314 st
->sensors
[chan
] = ltc2983_thermocouple_new(child
, st
,
1316 } else if (sensor
.type
>= LTC2983_SENSOR_RTD
&&
1317 sensor
.type
<= LTC2983_SENSOR_RTD_CUSTOM
) {
1318 st
->sensors
[chan
] = ltc2983_rtd_new(child
, st
, &sensor
);
1319 } else if (sensor
.type
>= LTC2983_SENSOR_THERMISTOR
&&
1320 sensor
.type
<= LTC2983_SENSOR_THERMISTOR_CUSTOM
) {
1321 st
->sensors
[chan
] = ltc2983_thermistor_new(child
, st
,
1323 } else if (sensor
.type
== LTC2983_SENSOR_DIODE
) {
1324 st
->sensors
[chan
] = ltc2983_diode_new(child
, st
,
1326 } else if (sensor
.type
== LTC2983_SENSOR_SENSE_RESISTOR
) {
1327 st
->sensors
[chan
] = ltc2983_r_sense_new(child
, st
,
1329 /* don't add rsense to iio */
1331 } else if (sensor
.type
== LTC2983_SENSOR_DIRECT_ADC
) {
1332 st
->sensors
[chan
] = ltc2983_adc_new(child
, st
, &sensor
);
1334 dev_err(dev
, "Unknown sensor type %d\n", sensor
.type
);
1338 if (IS_ERR(st
->sensors
[chan
])) {
1339 dev_err(dev
, "Failed to create sensor %ld",
1340 PTR_ERR(st
->sensors
[chan
]));
1341 return PTR_ERR(st
->sensors
[chan
]);
1343 /* set generic sensor parameters */
1344 st
->sensors
[chan
]->chan
= sensor
.chan
;
1345 st
->sensors
[chan
]->type
= sensor
.type
;
1347 channel_avail_mask
|= BIT(sensor
.chan
);
1354 static int ltc2983_setup(struct ltc2983_data
*st
, bool assign_iio
)
1356 u32 iio_chan_t
= 0, iio_chan_v
= 0, chan
, iio_idx
= 0;
1360 /* make sure the device is up */
1361 time
= wait_for_completion_timeout(&st
->completion
,
1362 msecs_to_jiffies(250));
1365 dev_err(&st
->spi
->dev
, "Device startup timed out\n");
1369 st
->iio_chan
= devm_kzalloc(&st
->spi
->dev
,
1370 st
->iio_channels
* sizeof(*st
->iio_chan
),
1376 ret
= regmap_update_bits(st
->regmap
, LTC2983_GLOBAL_CONFIG_REG
,
1377 LTC2983_NOTCH_FREQ_MASK
,
1378 LTC2983_NOTCH_FREQ(st
->filter_notch_freq
));
1382 ret
= regmap_write(st
->regmap
, LTC2983_MUX_CONFIG_REG
,
1383 st
->mux_delay_config
);
1387 for (chan
= 0; chan
< st
->num_channels
; chan
++) {
1388 u32 chan_type
= 0, *iio_chan
;
1390 ret
= st
->sensors
[chan
]->assign_chan(st
, st
->sensors
[chan
]);
1394 * The assign_iio flag is necessary for when the device is
1395 * coming out of sleep. In that case, we just need to
1396 * re-configure the device channels.
1397 * We also don't assign iio channels for rsense.
1399 if (st
->sensors
[chan
]->type
== LTC2983_SENSOR_SENSE_RESISTOR
||
1403 /* assign iio channel */
1404 if (st
->sensors
[chan
]->type
!= LTC2983_SENSOR_DIRECT_ADC
) {
1405 chan_type
= IIO_TEMP
;
1406 iio_chan
= &iio_chan_t
;
1408 chan_type
= IIO_VOLTAGE
;
1409 iio_chan
= &iio_chan_v
;
1413 * add chan as the iio .address so that, we can directly
1414 * reference the sensor given the iio_chan_spec
1416 st
->iio_chan
[iio_idx
++] = LTC2983_CHAN(chan_type
, (*iio_chan
)++,
1423 static const struct regmap_range ltc2983_reg_ranges
[] = {
1424 regmap_reg_range(LTC2983_STATUS_REG
, LTC2983_STATUS_REG
),
1425 regmap_reg_range(LTC2983_TEMP_RES_START_REG
, LTC2983_TEMP_RES_END_REG
),
1426 regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG
, LTC2983_GLOBAL_CONFIG_REG
),
1427 regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG
,
1428 LTC2983_MULT_CHANNEL_END_REG
),
1429 regmap_reg_range(LTC2983_MUX_CONFIG_REG
, LTC2983_MUX_CONFIG_REG
),
1430 regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG
,
1431 LTC2983_CHAN_ASSIGN_END_REG
),
1432 regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG
,
1433 LTC2983_CUST_SENS_TBL_END_REG
),
1436 static const struct regmap_access_table ltc2983_reg_table
= {
1437 .yes_ranges
= ltc2983_reg_ranges
,
1438 .n_yes_ranges
= ARRAY_SIZE(ltc2983_reg_ranges
),
1442 * The reg_bits are actually 12 but the device needs the first *complete*
1443 * byte for the command (R/W).
1445 static const struct regmap_config ltc2983_regmap_config
= {
1448 .wr_table
= <c2983_reg_table
,
1449 .rd_table
= <c2983_reg_table
,
1450 .read_flag_mask
= GENMASK(1, 0),
1451 .write_flag_mask
= BIT(1),
1454 static const struct iio_info ltc2983_iio_info
= {
1455 .read_raw
= ltc2983_read_raw
,
1456 .debugfs_reg_access
= ltc2983_reg_access
,
1459 static int ltc2983_probe(struct spi_device
*spi
)
1461 struct ltc2983_data
*st
;
1462 struct iio_dev
*indio_dev
;
1463 const char *name
= spi_get_device_id(spi
)->name
;
1466 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
1470 st
= iio_priv(indio_dev
);
1472 st
->regmap
= devm_regmap_init_spi(spi
, <c2983_regmap_config
);
1473 if (IS_ERR(st
->regmap
)) {
1474 dev_err(&spi
->dev
, "Failed to initialize regmap\n");
1475 return PTR_ERR(st
->regmap
);
1478 mutex_init(&st
->lock
);
1479 init_completion(&st
->completion
);
1481 spi_set_drvdata(spi
, st
);
1483 ret
= ltc2983_parse_dt(st
);
1487 * let's request the irq now so it is used to sync the device
1488 * startup in ltc2983_setup()
1490 ret
= devm_request_irq(&spi
->dev
, spi
->irq
, ltc2983_irq_handler
,
1491 IRQF_TRIGGER_RISING
, name
, st
);
1493 dev_err(&spi
->dev
, "failed to request an irq, %d", ret
);
1497 ret
= ltc2983_setup(st
, true);
1501 indio_dev
->dev
.parent
= &spi
->dev
;
1502 indio_dev
->name
= name
;
1503 indio_dev
->num_channels
= st
->iio_channels
;
1504 indio_dev
->channels
= st
->iio_chan
;
1505 indio_dev
->modes
= INDIO_DIRECT_MODE
;
1506 indio_dev
->info
= <c2983_iio_info
;
1508 return devm_iio_device_register(&spi
->dev
, indio_dev
);
1511 static int __maybe_unused
ltc2983_resume(struct device
*dev
)
1513 struct ltc2983_data
*st
= spi_get_drvdata(to_spi_device(dev
));
1516 /* dummy read to bring the device out of sleep */
1517 regmap_read(st
->regmap
, LTC2983_STATUS_REG
, &dummy
);
1518 /* we need to re-assign the channels */
1519 return ltc2983_setup(st
, false);
1522 static int __maybe_unused
ltc2983_suspend(struct device
*dev
)
1524 struct ltc2983_data
*st
= spi_get_drvdata(to_spi_device(dev
));
1526 return regmap_write(st
->regmap
, LTC2983_STATUS_REG
, LTC2983_SLEEP
);
1529 static SIMPLE_DEV_PM_OPS(ltc2983_pm_ops
, ltc2983_suspend
, ltc2983_resume
);
1531 static const struct spi_device_id ltc2983_id_table
[] = {
1535 MODULE_DEVICE_TABLE(spi
, ltc2983_id_table
);
1537 static const struct of_device_id ltc2983_of_match
[] = {
1538 { .compatible
= "adi,ltc2983" },
1541 MODULE_DEVICE_TABLE(of
, ltc2983_of_match
);
1543 static struct spi_driver ltc2983_driver
= {
1546 .of_match_table
= ltc2983_of_match
,
1547 .pm
= <c2983_pm_ops
,
1549 .probe
= ltc2983_probe
,
1550 .id_table
= ltc2983_id_table
,
1553 module_spi_driver(ltc2983_driver
);
1555 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1556 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1557 MODULE_LICENSE("GPL");