drm/modes: Fix drm_mode_vrefres() docs
[drm/drm-misc.git] / drivers / iio / temperature / ltc2983.c
blobf8ea2219ab48b31c18d2ff22dcf103fb4d114f09
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
4 * driver
6 * Copyright 2019 Analog Devices Inc.
7 */
8 #include <linux/bitfield.h>
9 #include <linux/completion.h>
10 #include <linux/device.h>
11 #include <linux/err.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/iio/iio.h>
15 #include <linux/interrupt.h>
16 #include <linux/list.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/property.h>
20 #include <linux/regmap.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/spi/spi.h>
24 #include <asm/byteorder.h>
25 #include <linux/unaligned.h>
27 /* register map */
28 #define LTC2983_STATUS_REG 0x0000
29 #define LTC2983_TEMP_RES_START_REG 0x0010
30 #define LTC2983_TEMP_RES_END_REG 0x005F
31 #define LTC2983_EEPROM_KEY_REG 0x00B0
32 #define LTC2983_EEPROM_READ_STATUS_REG 0x00D0
33 #define LTC2983_GLOBAL_CONFIG_REG 0x00F0
34 #define LTC2983_MULT_CHANNEL_START_REG 0x00F4
35 #define LTC2983_MULT_CHANNEL_END_REG 0x00F7
36 #define LTC2986_EEPROM_STATUS_REG 0x00F9
37 #define LTC2983_MUX_CONFIG_REG 0x00FF
38 #define LTC2983_CHAN_ASSIGN_START_REG 0x0200
39 #define LTC2983_CHAN_ASSIGN_END_REG 0x024F
40 #define LTC2983_CUST_SENS_TBL_START_REG 0x0250
41 #define LTC2983_CUST_SENS_TBL_END_REG 0x03CF
43 #define LTC2983_DIFFERENTIAL_CHAN_MIN 2
44 #define LTC2983_MIN_CHANNELS_NR 1
45 #define LTC2983_SLEEP 0x97
46 #define LTC2983_CUSTOM_STEINHART_SIZE 24
47 #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6
48 #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4
50 #define LTC2983_EEPROM_KEY 0xA53C0F5A
51 #define LTC2983_EEPROM_WRITE_CMD 0x15
52 #define LTC2983_EEPROM_READ_CMD 0x16
53 #define LTC2983_EEPROM_STATUS_FAILURE_MASK GENMASK(3, 1)
54 #define LTC2983_EEPROM_READ_FAILURE_MASK GENMASK(7, 0)
56 #define LTC2983_EEPROM_WRITE_TIME_MS 2600
57 #define LTC2983_EEPROM_READ_TIME_MS 20
59 #define LTC2983_CHAN_START_ADDR(chan) \
60 (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
61 #define LTC2983_CHAN_RES_ADDR(chan) \
62 (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
63 #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3)
64 #define LTC2983_THERMOCOUPLE_SGL(x) \
65 FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
66 #define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0)
67 #define LTC2983_THERMOCOUPLE_OC_CURR(x) \
68 FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
69 #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2)
70 #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
71 FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
73 #define LTC2983_THERMISTOR_DIFF_MASK BIT(2)
74 #define LTC2983_THERMISTOR_SGL(x) \
75 FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
76 #define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1)
77 #define LTC2983_THERMISTOR_R_SHARE(x) \
78 FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
79 #define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0)
80 #define LTC2983_THERMISTOR_C_ROTATE(x) \
81 FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
83 #define LTC2983_DIODE_DIFF_MASK BIT(2)
84 #define LTC2983_DIODE_SGL(x) \
85 FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
86 #define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1)
87 #define LTC2983_DIODE_3_CONV_CYCLE(x) \
88 FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
89 #define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0)
90 #define LTC2983_DIODE_AVERAGE_ON(x) \
91 FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
93 #define LTC2983_RTD_4_WIRE_MASK BIT(3)
94 #define LTC2983_RTD_ROTATION_MASK BIT(1)
95 #define LTC2983_RTD_C_ROTATE(x) \
96 FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
97 #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2)
98 #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2)
99 #define LTC2983_RTD_N_WIRES(x) \
100 FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
101 #define LTC2983_RTD_R_SHARE_MASK BIT(0)
102 #define LTC2983_RTD_R_SHARE(x) \
103 FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
105 #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30)
106 #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25)
108 #define LTC2983_STATUS_START_MASK BIT(7)
109 #define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x)
110 #define LTC2983_STATUS_UP_MASK GENMASK(7, 6)
111 #define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
113 #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0)
114 #define LTC2983_STATUS_CHAN_SEL(x) \
115 FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
117 #define LTC2983_TEMP_UNITS_MASK BIT(2)
118 #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
120 #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0)
121 #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
123 #define LTC2983_RES_VALID_MASK BIT(24)
124 #define LTC2983_DATA_MASK GENMASK(23, 0)
125 #define LTC2983_DATA_SIGN_BIT 23
127 #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27)
128 #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
130 /* cold junction for thermocouples and rsense for rtd's and thermistor's */
131 #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22)
132 #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
134 #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0)
135 #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
137 #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6)
138 #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
140 #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18)
141 #define LTC2983_THERMOCOUPLE_CFG(x) \
142 FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
143 #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29)
144 #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25)
146 #define LTC2983_RTD_CFG_MASK GENMASK(21, 18)
147 #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
148 #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14)
149 #define LTC2983_RTD_EXC_CURRENT(x) \
150 FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
151 #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12)
152 #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
154 #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19)
155 #define LTC2983_THERMISTOR_CFG(x) \
156 FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
157 #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15)
158 #define LTC2983_THERMISTOR_EXC_CURRENT(x) \
159 FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
161 #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24)
162 #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
163 #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22)
164 #define LTC2983_DIODE_EXC_CURRENT(x) \
165 FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
166 #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0)
167 #define LTC2983_DIODE_IDEAL_FACTOR(x) \
168 FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
170 #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0)
171 #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
173 #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26)
174 #define LTC2983_ADC_SINGLE_ENDED(x) \
175 FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
177 enum {
178 LTC2983_SENSOR_THERMOCOUPLE = 1,
179 LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
180 LTC2983_SENSOR_RTD = 10,
181 LTC2983_SENSOR_RTD_CUSTOM = 18,
182 LTC2983_SENSOR_THERMISTOR = 19,
183 LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
184 LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
185 LTC2983_SENSOR_DIODE = 28,
186 LTC2983_SENSOR_SENSE_RESISTOR = 29,
187 LTC2983_SENSOR_DIRECT_ADC = 30,
188 LTC2983_SENSOR_ACTIVE_TEMP = 31,
191 #define to_thermocouple(_sensor) \
192 container_of(_sensor, struct ltc2983_thermocouple, sensor)
194 #define to_rtd(_sensor) \
195 container_of(_sensor, struct ltc2983_rtd, sensor)
197 #define to_thermistor(_sensor) \
198 container_of(_sensor, struct ltc2983_thermistor, sensor)
200 #define to_diode(_sensor) \
201 container_of(_sensor, struct ltc2983_diode, sensor)
203 #define to_rsense(_sensor) \
204 container_of(_sensor, struct ltc2983_rsense, sensor)
206 #define to_adc(_sensor) \
207 container_of(_sensor, struct ltc2983_adc, sensor)
209 #define to_temp(_sensor) \
210 container_of(_sensor, struct ltc2983_temp, sensor)
212 struct ltc2983_chip_info {
213 const char *name;
214 unsigned int max_channels_nr;
215 bool has_temp;
216 bool has_eeprom;
219 struct ltc2983_data {
220 const struct ltc2983_chip_info *info;
221 struct regmap *regmap;
222 struct spi_device *spi;
223 struct mutex lock;
224 struct completion completion;
225 struct iio_chan_spec *iio_chan;
226 struct ltc2983_sensor **sensors;
227 u32 mux_delay_config;
228 u32 filter_notch_freq;
229 u16 custom_table_size;
230 u8 num_channels;
231 u8 iio_channels;
233 * DMA (thus cache coherency maintenance) may require the
234 * transfer buffers to live in their own cache lines.
235 * Holds the converted temperature
237 __be32 temp __aligned(IIO_DMA_MINALIGN);
238 __be32 chan_val;
239 __be32 eeprom_key;
242 struct ltc2983_sensor {
243 int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
244 int (*assign_chan)(struct ltc2983_data *st,
245 const struct ltc2983_sensor *sensor);
246 /* specifies the sensor channel */
247 u32 chan;
248 /* sensor type */
249 u32 type;
252 struct ltc2983_custom_sensor {
253 /* raw table sensor data */
254 void *table;
255 size_t size;
256 /* address offset */
257 s8 offset;
258 bool is_steinhart;
261 struct ltc2983_thermocouple {
262 struct ltc2983_sensor sensor;
263 struct ltc2983_custom_sensor *custom;
264 u32 sensor_config;
265 u32 cold_junction_chan;
268 struct ltc2983_rtd {
269 struct ltc2983_sensor sensor;
270 struct ltc2983_custom_sensor *custom;
271 u32 sensor_config;
272 u32 r_sense_chan;
273 u32 excitation_current;
274 u32 rtd_curve;
277 struct ltc2983_thermistor {
278 struct ltc2983_sensor sensor;
279 struct ltc2983_custom_sensor *custom;
280 u32 sensor_config;
281 u32 r_sense_chan;
282 u32 excitation_current;
285 struct ltc2983_diode {
286 struct ltc2983_sensor sensor;
287 u32 sensor_config;
288 u32 excitation_current;
289 u32 ideal_factor_value;
292 struct ltc2983_rsense {
293 struct ltc2983_sensor sensor;
294 u32 r_sense_val;
297 struct ltc2983_adc {
298 struct ltc2983_sensor sensor;
299 bool single_ended;
302 struct ltc2983_temp {
303 struct ltc2983_sensor sensor;
304 struct ltc2983_custom_sensor *custom;
305 bool single_ended;
309 * Convert to Q format numbers. These number's are integers where
310 * the number of integer and fractional bits are specified. The resolution
311 * is given by 1/@resolution and tell us the number of fractional bits. For
312 * instance a resolution of 2^-10 means we have 10 fractional bits.
314 static u32 __convert_to_raw(const u64 val, const u32 resolution)
316 u64 __res = val * resolution;
318 /* all values are multiplied by 1000000 to remove the fraction */
319 do_div(__res, 1000000);
321 return __res;
324 static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
326 s64 __res = -(s32)val;
328 __res = __convert_to_raw(__res, resolution);
330 return (u32)-__res;
333 static int __ltc2983_fault_handler(const struct ltc2983_data *st,
334 const u32 result, const u32 hard_mask,
335 const u32 soft_mask)
337 const struct device *dev = &st->spi->dev;
339 if (result & hard_mask) {
340 dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
341 return -EIO;
342 } else if (result & soft_mask) {
343 /* just print a warning */
344 dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
347 return 0;
350 static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
351 const struct ltc2983_sensor *sensor,
352 u32 chan_val)
354 u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
356 chan_val |= LTC2983_CHAN_TYPE(sensor->type);
357 dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
358 chan_val);
359 st->chan_val = cpu_to_be32(chan_val);
360 return regmap_bulk_write(st->regmap, reg, &st->chan_val,
361 sizeof(st->chan_val));
364 static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
365 struct ltc2983_custom_sensor *custom,
366 u32 *chan_val)
368 u32 reg;
369 u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
370 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
371 const struct device *dev = &st->spi->dev;
373 * custom->size holds the raw size of the table. However, when
374 * configuring the sensor channel, we must write the number of
375 * entries of the table minus 1. For steinhart sensors 0 is written
376 * since the size is constant!
378 const u8 len = custom->is_steinhart ? 0 :
379 (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
381 * Check if the offset was assigned already. It should be for steinhart
382 * sensors. When coming from sleep, it should be assigned for all.
384 if (custom->offset < 0) {
386 * This needs to be done again here because, from the moment
387 * when this test was done (successfully) for this custom
388 * sensor, a steinhart sensor might have been added changing
389 * custom_table_size...
391 if (st->custom_table_size + custom->size >
392 (LTC2983_CUST_SENS_TBL_END_REG -
393 LTC2983_CUST_SENS_TBL_START_REG) + 1) {
394 dev_err(dev,
395 "Not space left(%d) for new custom sensor(%zu)",
396 st->custom_table_size,
397 custom->size);
398 return -EINVAL;
401 custom->offset = st->custom_table_size /
402 LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
403 st->custom_table_size += custom->size;
406 reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
408 *chan_val |= LTC2983_CUSTOM_LEN(len);
409 *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
410 dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
411 reg, custom->offset,
412 custom->size);
413 /* write custom sensor table */
414 return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
417 static struct ltc2983_custom_sensor *
418 __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
419 const char *propname, const bool is_steinhart,
420 const u32 resolution, const bool has_signed)
422 struct ltc2983_custom_sensor *new_custom;
423 struct device *dev = &st->spi->dev;
425 * For custom steinhart, the full u32 is taken. For all the others
426 * the MSB is discarded.
428 const u8 n_size = is_steinhart ? 4 : 3;
429 u8 index, n_entries;
430 int ret;
432 if (is_steinhart)
433 n_entries = fwnode_property_count_u32(fn, propname);
434 else
435 n_entries = fwnode_property_count_u64(fn, propname);
436 /* n_entries must be an even number */
437 if (!n_entries || (n_entries % 2) != 0)
438 return dev_err_ptr_probe(dev, -EINVAL,
439 "Number of entries either 0 or not even\n");
441 new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
442 if (!new_custom)
443 return ERR_PTR(-ENOMEM);
445 new_custom->size = n_entries * n_size;
446 /* check Steinhart size */
447 if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE)
448 return dev_err_ptr_probe(dev, -EINVAL,
449 "Steinhart sensors size(%zu) must be %u\n",
450 new_custom->size, LTC2983_CUSTOM_STEINHART_SIZE);
452 /* Check space on the table. */
453 if (st->custom_table_size + new_custom->size >
454 (LTC2983_CUST_SENS_TBL_END_REG - LTC2983_CUST_SENS_TBL_START_REG) + 1)
455 return dev_err_ptr_probe(dev, -EINVAL,
456 "No space left(%d) for new custom sensor(%zu)\n",
457 st->custom_table_size, new_custom->size);
459 /* allocate the table */
460 if (is_steinhart)
461 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL);
462 else
463 new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL);
464 if (!new_custom->table)
465 return ERR_PTR(-ENOMEM);
468 * Steinhart sensors are configured with raw values in the firmware
469 * node. For the other sensors we must convert the value to raw.
470 * The odd index's correspond to temperatures and always have 1/1024
471 * of resolution. Temperatures also come in Kelvin, so signed values
472 * are not possible.
474 if (is_steinhart) {
475 ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries);
476 if (ret < 0)
477 return ERR_PTR(ret);
479 cpu_to_be32_array(new_custom->table, new_custom->table, n_entries);
480 } else {
481 ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries);
482 if (ret < 0)
483 return ERR_PTR(ret);
485 for (index = 0; index < n_entries; index++) {
486 u64 temp = ((u64 *)new_custom->table)[index];
488 if ((index % 2) != 0)
489 temp = __convert_to_raw(temp, 1024);
490 else if (has_signed && (s64)temp < 0)
491 temp = __convert_to_raw_sign(temp, resolution);
492 else
493 temp = __convert_to_raw(temp, resolution);
495 put_unaligned_be24(temp, new_custom->table + index * 3);
499 new_custom->is_steinhart = is_steinhart;
501 * This is done to first add all the steinhart sensors to the table,
502 * in order to maximize the table usage. If we mix adding steinhart
503 * with the other sensors, we might have to do some roundup to make
504 * sure that sensor_addr - 0x250(start address) is a multiple of 4
505 * (for steinhart), and a multiple of 6 for all the other sensors.
506 * Since we have const 24 bytes for steinhart sensors and 24 is
507 * also a multiple of 6, we guarantee that the first non-steinhart
508 * sensor will sit in a correct address without the need of filling
509 * addresses.
511 if (is_steinhart) {
512 new_custom->offset = st->custom_table_size /
513 LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
514 st->custom_table_size += new_custom->size;
515 } else {
516 /* mark as unset. This is checked later on the assign phase */
517 new_custom->offset = -1;
520 return new_custom;
523 static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
524 const u32 result)
526 return __ltc2983_fault_handler(st, result,
527 LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
528 LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
531 static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
532 const u32 result)
534 return __ltc2983_fault_handler(st, result,
535 LTC2983_COMMON_HARD_FAULT_MASK,
536 LTC2983_COMMON_SOFT_FAULT_MASK);
539 static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
540 const struct ltc2983_sensor *sensor)
542 struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
543 u32 chan_val;
545 chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
546 chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
548 if (thermo->custom) {
549 int ret;
551 ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
552 &chan_val);
553 if (ret)
554 return ret;
556 return __ltc2983_chan_assign_common(st, sensor, chan_val);
559 static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
560 const struct ltc2983_sensor *sensor)
562 struct ltc2983_rtd *rtd = to_rtd(sensor);
563 u32 chan_val;
565 chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
566 chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
567 chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
568 chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
570 if (rtd->custom) {
571 int ret;
573 ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
574 &chan_val);
575 if (ret)
576 return ret;
578 return __ltc2983_chan_assign_common(st, sensor, chan_val);
581 static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
582 const struct ltc2983_sensor *sensor)
584 struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
585 u32 chan_val;
587 chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
588 chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
589 chan_val |=
590 LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
592 if (thermistor->custom) {
593 int ret;
595 ret = __ltc2983_chan_custom_sensor_assign(st,
596 thermistor->custom,
597 &chan_val);
598 if (ret)
599 return ret;
601 return __ltc2983_chan_assign_common(st, sensor, chan_val);
604 static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
605 const struct ltc2983_sensor *sensor)
607 struct ltc2983_diode *diode = to_diode(sensor);
608 u32 chan_val;
610 chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
611 chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
612 chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
614 return __ltc2983_chan_assign_common(st, sensor, chan_val);
617 static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
618 const struct ltc2983_sensor *sensor)
620 struct ltc2983_rsense *rsense = to_rsense(sensor);
621 u32 chan_val;
623 chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
625 return __ltc2983_chan_assign_common(st, sensor, chan_val);
628 static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
629 const struct ltc2983_sensor *sensor)
631 struct ltc2983_adc *adc = to_adc(sensor);
632 u32 chan_val;
634 chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
636 return __ltc2983_chan_assign_common(st, sensor, chan_val);
639 static int ltc2983_temp_assign_chan(struct ltc2983_data *st,
640 const struct ltc2983_sensor *sensor)
642 struct ltc2983_temp *temp = to_temp(sensor);
643 u32 chan_val;
644 int ret;
646 chan_val = LTC2983_ADC_SINGLE_ENDED(temp->single_ended);
648 ret = __ltc2983_chan_custom_sensor_assign(st, temp->custom, &chan_val);
649 if (ret)
650 return ret;
652 return __ltc2983_chan_assign_common(st, sensor, chan_val);
655 static struct ltc2983_sensor *
656 ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st,
657 const struct ltc2983_sensor *sensor)
659 struct ltc2983_thermocouple *thermo;
660 u32 oc_current;
661 int ret;
663 thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
664 if (!thermo)
665 return ERR_PTR(-ENOMEM);
667 if (fwnode_property_read_bool(child, "adi,single-ended"))
668 thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
670 ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current);
671 if (!ret) {
672 switch (oc_current) {
673 case 10:
674 thermo->sensor_config |=
675 LTC2983_THERMOCOUPLE_OC_CURR(0);
676 break;
677 case 100:
678 thermo->sensor_config |=
679 LTC2983_THERMOCOUPLE_OC_CURR(1);
680 break;
681 case 500:
682 thermo->sensor_config |=
683 LTC2983_THERMOCOUPLE_OC_CURR(2);
684 break;
685 case 1000:
686 thermo->sensor_config |=
687 LTC2983_THERMOCOUPLE_OC_CURR(3);
688 break;
689 default:
690 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
691 "Invalid open circuit current:%u\n",
692 oc_current);
695 thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
697 /* validate channel index */
698 if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
699 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
700 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
701 "Invalid chann:%d for differential thermocouple\n",
702 sensor->chan);
704 struct fwnode_handle *ref __free(fwnode_handle) =
705 fwnode_find_reference(child, "adi,cold-junction-handle", 0);
706 if (IS_ERR(ref)) {
707 ref = NULL;
708 } else {
709 ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan);
710 if (ret)
712 * This would be catched later but we can just return
713 * the error right away.
715 return dev_err_ptr_probe(&st->spi->dev, ret,
716 "Property reg must be given\n");
719 /* check custom sensor */
720 if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
721 const char *propname = "adi,custom-thermocouple";
723 thermo->custom = __ltc2983_custom_sensor_new(st, child,
724 propname, false,
725 16384, true);
726 if (IS_ERR(thermo->custom))
727 return ERR_CAST(thermo->custom);
730 /* set common parameters */
731 thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
732 thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
734 return &thermo->sensor;
737 static struct ltc2983_sensor *
738 ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
739 const struct ltc2983_sensor *sensor)
741 struct ltc2983_rtd *rtd;
742 int ret = 0;
743 struct device *dev = &st->spi->dev;
744 u32 excitation_current = 0, n_wires = 0;
746 rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
747 if (!rtd)
748 return ERR_PTR(-ENOMEM);
750 struct fwnode_handle *ref __free(fwnode_handle) =
751 fwnode_find_reference(child, "adi,rsense-handle", 0);
752 if (IS_ERR(ref))
753 return dev_err_cast_probe(dev, ref,
754 "Property adi,rsense-handle missing or invalid\n");
756 ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan);
757 if (ret)
758 return dev_err_ptr_probe(dev, ret,
759 "Property reg must be given\n");
761 ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires);
762 if (!ret) {
763 switch (n_wires) {
764 case 2:
765 rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
766 break;
767 case 3:
768 rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
769 break;
770 case 4:
771 rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
772 break;
773 case 5:
774 /* 4 wires, Kelvin Rsense */
775 rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
776 break;
777 default:
778 return dev_err_ptr_probe(dev, -EINVAL,
779 "Invalid number of wires:%u\n",
780 n_wires);
784 if (fwnode_property_read_bool(child, "adi,rsense-share")) {
785 /* Current rotation is only available with rsense sharing */
786 if (fwnode_property_read_bool(child, "adi,current-rotate")) {
787 if (n_wires == 2 || n_wires == 3)
788 return dev_err_ptr_probe(dev, -EINVAL,
789 "Rotation not allowed for 2/3 Wire RTDs\n");
791 rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
792 } else {
793 rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
797 * rtd channel indexes are a bit more complicated to validate.
798 * For 4wire RTD with rotation, the channel selection cannot be
799 * >=19 since the chann + 1 is used in this configuration.
800 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
801 * <=1 since chanel - 1 and channel - 2 are used.
803 if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
804 /* 4-wire */
805 u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
806 max = st->info->max_channels_nr;
808 if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
809 max = st->info->max_channels_nr - 1;
811 if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
812 == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
813 (rtd->r_sense_chan <= min))
814 /* kelvin rsense*/
815 return dev_err_ptr_probe(dev, -EINVAL,
816 "Invalid rsense chann:%d to use in kelvin rsense\n",
817 rtd->r_sense_chan);
819 if (sensor->chan < min || sensor->chan > max)
820 return dev_err_ptr_probe(dev, -EINVAL,
821 "Invalid chann:%d for the rtd config\n",
822 sensor->chan);
823 } else {
824 /* same as differential case */
825 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
826 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
827 "Invalid chann:%d for RTD\n",
828 sensor->chan);
831 /* check custom sensor */
832 if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
833 rtd->custom = __ltc2983_custom_sensor_new(st, child,
834 "adi,custom-rtd",
835 false, 2048, false);
836 if (IS_ERR(rtd->custom))
837 return ERR_CAST(rtd->custom);
840 /* set common parameters */
841 rtd->sensor.fault_handler = ltc2983_common_fault_handler;
842 rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
844 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
845 &excitation_current);
846 if (ret) {
847 /* default to 5uA */
848 rtd->excitation_current = 1;
849 } else {
850 switch (excitation_current) {
851 case 5:
852 rtd->excitation_current = 0x01;
853 break;
854 case 10:
855 rtd->excitation_current = 0x02;
856 break;
857 case 25:
858 rtd->excitation_current = 0x03;
859 break;
860 case 50:
861 rtd->excitation_current = 0x04;
862 break;
863 case 100:
864 rtd->excitation_current = 0x05;
865 break;
866 case 250:
867 rtd->excitation_current = 0x06;
868 break;
869 case 500:
870 rtd->excitation_current = 0x07;
871 break;
872 case 1000:
873 rtd->excitation_current = 0x08;
874 break;
875 default:
876 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
877 "Invalid value for excitation current(%u)\n",
878 excitation_current);
882 fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
884 return &rtd->sensor;
887 static struct ltc2983_sensor *
888 ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st,
889 const struct ltc2983_sensor *sensor)
891 struct ltc2983_thermistor *thermistor;
892 struct device *dev = &st->spi->dev;
893 u32 excitation_current = 0;
894 int ret = 0;
896 thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
897 if (!thermistor)
898 return ERR_PTR(-ENOMEM);
900 struct fwnode_handle *ref __free(fwnode_handle) =
901 fwnode_find_reference(child, "adi,rsense-handle", 0);
902 if (IS_ERR(ref))
903 return dev_err_cast_probe(dev, ref,
904 "Property adi,rsense-handle missing or invalid\n");
906 ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan);
907 if (ret)
908 return dev_err_ptr_probe(dev, ret,
909 "rsense channel must be configured...\n");
911 if (fwnode_property_read_bool(child, "adi,single-ended")) {
912 thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
913 } else if (fwnode_property_read_bool(child, "adi,rsense-share")) {
914 /* rotation is only possible if sharing rsense */
915 if (fwnode_property_read_bool(child, "adi,current-rotate"))
916 thermistor->sensor_config =
917 LTC2983_THERMISTOR_C_ROTATE(1);
918 else
919 thermistor->sensor_config =
920 LTC2983_THERMISTOR_R_SHARE(1);
922 /* validate channel index */
923 if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
924 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
925 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
926 "Invalid chann:%d for differential thermistor\n",
927 sensor->chan);
929 /* check custom sensor */
930 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
931 bool steinhart = false;
932 const char *propname;
934 if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
935 steinhart = true;
936 propname = "adi,custom-steinhart";
937 } else {
938 propname = "adi,custom-thermistor";
941 thermistor->custom = __ltc2983_custom_sensor_new(st, child,
942 propname,
943 steinhart,
944 64, false);
945 if (IS_ERR(thermistor->custom))
946 return ERR_CAST(thermistor->custom);
948 /* set common parameters */
949 thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
950 thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
952 ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
953 &excitation_current);
954 if (ret) {
955 /* Auto range is not allowed for custom sensors */
956 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
957 /* default to 1uA */
958 thermistor->excitation_current = 0x03;
959 else
960 /* default to auto-range */
961 thermistor->excitation_current = 0x0c;
962 } else {
963 switch (excitation_current) {
964 case 0:
965 /* auto range */
966 if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
967 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
968 "Auto Range not allowed for custom sensors\n");
970 thermistor->excitation_current = 0x0c;
971 break;
972 case 250:
973 thermistor->excitation_current = 0x01;
974 break;
975 case 500:
976 thermistor->excitation_current = 0x02;
977 break;
978 case 1000:
979 thermistor->excitation_current = 0x03;
980 break;
981 case 5000:
982 thermistor->excitation_current = 0x04;
983 break;
984 case 10000:
985 thermistor->excitation_current = 0x05;
986 break;
987 case 25000:
988 thermistor->excitation_current = 0x06;
989 break;
990 case 50000:
991 thermistor->excitation_current = 0x07;
992 break;
993 case 100000:
994 thermistor->excitation_current = 0x08;
995 break;
996 case 250000:
997 thermistor->excitation_current = 0x09;
998 break;
999 case 500000:
1000 thermistor->excitation_current = 0x0a;
1001 break;
1002 case 1000000:
1003 thermistor->excitation_current = 0x0b;
1004 break;
1005 default:
1006 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1007 "Invalid value for excitation current(%u)\n",
1008 excitation_current);
1012 return &thermistor->sensor;
1015 static struct ltc2983_sensor *
1016 ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st,
1017 const struct ltc2983_sensor *sensor)
1019 struct ltc2983_diode *diode;
1020 u32 temp = 0, excitation_current = 0;
1021 int ret;
1023 diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
1024 if (!diode)
1025 return ERR_PTR(-ENOMEM);
1027 if (fwnode_property_read_bool(child, "adi,single-ended"))
1028 diode->sensor_config = LTC2983_DIODE_SGL(1);
1030 if (fwnode_property_read_bool(child, "adi,three-conversion-cycles"))
1031 diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
1033 if (fwnode_property_read_bool(child, "adi,average-on"))
1034 diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
1036 /* validate channel index */
1037 if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
1038 sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
1039 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1040 "Invalid chann:%d for differential thermistor\n",
1041 sensor->chan);
1043 /* set common parameters */
1044 diode->sensor.fault_handler = ltc2983_common_fault_handler;
1045 diode->sensor.assign_chan = ltc2983_diode_assign_chan;
1047 ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
1048 &excitation_current);
1049 if (!ret) {
1050 switch (excitation_current) {
1051 case 10:
1052 diode->excitation_current = 0x00;
1053 break;
1054 case 20:
1055 diode->excitation_current = 0x01;
1056 break;
1057 case 40:
1058 diode->excitation_current = 0x02;
1059 break;
1060 case 80:
1061 diode->excitation_current = 0x03;
1062 break;
1063 default:
1064 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1065 "Invalid value for excitation current(%u)\n",
1066 excitation_current);
1070 fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp);
1072 /* 2^20 resolution */
1073 diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
1075 return &diode->sensor;
1078 static struct ltc2983_sensor *ltc2983_r_sense_new(struct fwnode_handle *child,
1079 struct ltc2983_data *st,
1080 const struct ltc2983_sensor *sensor)
1082 struct ltc2983_rsense *rsense;
1083 int ret;
1084 u32 temp;
1086 rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
1087 if (!rsense)
1088 return ERR_PTR(-ENOMEM);
1090 /* validate channel index */
1091 if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
1092 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1093 "Invalid chann:%d for r_sense\n",
1094 sensor->chan);
1096 ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
1097 if (ret)
1098 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1099 "Property adi,rsense-val-milli-ohms missing\n");
1101 * Times 1000 because we have milli-ohms and __convert_to_raw
1102 * expects scales of 1000000 which are used for all other
1103 * properties.
1104 * 2^10 resolution
1106 rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
1108 /* set common parameters */
1109 rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
1111 return &rsense->sensor;
1114 static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child,
1115 struct ltc2983_data *st,
1116 const struct ltc2983_sensor *sensor)
1118 struct ltc2983_adc *adc;
1120 adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
1121 if (!adc)
1122 return ERR_PTR(-ENOMEM);
1124 if (fwnode_property_read_bool(child, "adi,single-ended"))
1125 adc->single_ended = true;
1127 if (!adc->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
1128 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1129 "Invalid chan:%d for differential adc\n",
1130 sensor->chan);
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 struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child,
1140 struct ltc2983_data *st,
1141 const struct ltc2983_sensor *sensor)
1143 struct ltc2983_temp *temp;
1145 temp = devm_kzalloc(&st->spi->dev, sizeof(*temp), GFP_KERNEL);
1146 if (!temp)
1147 return ERR_PTR(-ENOMEM);
1149 if (fwnode_property_read_bool(child, "adi,single-ended"))
1150 temp->single_ended = true;
1152 if (!temp->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
1153 return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1154 "Invalid chan:%d for differential temp\n",
1155 sensor->chan);
1157 temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp",
1158 false, 4096, true);
1159 if (IS_ERR(temp->custom))
1160 return ERR_CAST(temp->custom);
1162 /* set common parameters */
1163 temp->sensor.assign_chan = ltc2983_temp_assign_chan;
1164 temp->sensor.fault_handler = ltc2983_common_fault_handler;
1166 return &temp->sensor;
1169 static int ltc2983_chan_read(struct ltc2983_data *st,
1170 const struct ltc2983_sensor *sensor, int *val)
1172 u32 start_conversion = 0;
1173 int ret;
1174 unsigned long time;
1176 start_conversion = LTC2983_STATUS_START(true);
1177 start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
1178 dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
1179 sensor->chan, start_conversion);
1180 /* start conversion */
1181 ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
1182 if (ret)
1183 return ret;
1185 reinit_completion(&st->completion);
1187 * wait for conversion to complete.
1188 * 300 ms should be more than enough to complete the conversion.
1189 * Depending on the sensor configuration, there are 2/3 conversions
1190 * cycles of 82ms.
1192 time = wait_for_completion_timeout(&st->completion,
1193 msecs_to_jiffies(300));
1194 if (!time) {
1195 dev_warn(&st->spi->dev, "Conversion timed out\n");
1196 return -ETIMEDOUT;
1199 /* read the converted data */
1200 ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
1201 &st->temp, sizeof(st->temp));
1202 if (ret)
1203 return ret;
1205 *val = __be32_to_cpu(st->temp);
1207 if (!(LTC2983_RES_VALID_MASK & *val)) {
1208 dev_err(&st->spi->dev, "Invalid conversion detected\n");
1209 return -EIO;
1212 ret = sensor->fault_handler(st, *val);
1213 if (ret)
1214 return ret;
1216 *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
1217 return 0;
1220 static int ltc2983_read_raw(struct iio_dev *indio_dev,
1221 struct iio_chan_spec const *chan,
1222 int *val, int *val2, long mask)
1224 struct ltc2983_data *st = iio_priv(indio_dev);
1225 int ret;
1227 /* sanity check */
1228 if (chan->address >= st->num_channels) {
1229 dev_err(&st->spi->dev, "Invalid chan address:%ld",
1230 chan->address);
1231 return -EINVAL;
1234 switch (mask) {
1235 case IIO_CHAN_INFO_RAW:
1236 mutex_lock(&st->lock);
1237 ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
1238 mutex_unlock(&st->lock);
1239 return ret ?: IIO_VAL_INT;
1240 case IIO_CHAN_INFO_SCALE:
1241 switch (chan->type) {
1242 case IIO_TEMP:
1243 /* value in milli degrees */
1244 *val = 1000;
1245 /* 2^10 */
1246 *val2 = 1024;
1247 return IIO_VAL_FRACTIONAL;
1248 case IIO_VOLTAGE:
1249 /* value in millivolt */
1250 *val = 1000;
1251 /* 2^21 */
1252 *val2 = 2097152;
1253 return IIO_VAL_FRACTIONAL;
1254 default:
1255 return -EINVAL;
1259 return -EINVAL;
1262 static int ltc2983_reg_access(struct iio_dev *indio_dev,
1263 unsigned int reg,
1264 unsigned int writeval,
1265 unsigned int *readval)
1267 struct ltc2983_data *st = iio_priv(indio_dev);
1269 if (readval)
1270 return regmap_read(st->regmap, reg, readval);
1272 return regmap_write(st->regmap, reg, writeval);
1275 static irqreturn_t ltc2983_irq_handler(int irq, void *data)
1277 struct ltc2983_data *st = data;
1279 complete(&st->completion);
1280 return IRQ_HANDLED;
1283 #define LTC2983_CHAN(__type, index, __address) ({ \
1284 struct iio_chan_spec __chan = { \
1285 .type = __type, \
1286 .indexed = 1, \
1287 .channel = index, \
1288 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1289 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1290 .address = __address, \
1291 }; \
1292 __chan; \
1295 static int ltc2983_parse_fw(struct ltc2983_data *st)
1297 struct device *dev = &st->spi->dev;
1298 int ret, chan = 0, channel_avail_mask = 0;
1300 device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config);
1302 device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq);
1304 st->num_channels = device_get_child_node_count(dev);
1305 if (!st->num_channels)
1306 return dev_err_probe(&st->spi->dev, -EINVAL,
1307 "At least one channel must be given!\n");
1309 st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
1310 GFP_KERNEL);
1311 if (!st->sensors)
1312 return -ENOMEM;
1314 st->iio_channels = st->num_channels;
1315 device_for_each_child_node_scoped(dev, child) {
1316 struct ltc2983_sensor sensor;
1318 ret = fwnode_property_read_u32(child, "reg", &sensor.chan);
1319 if (ret)
1320 return dev_err_probe(dev, ret,
1321 "reg property must given for child nodes\n");
1323 /* check if we have a valid channel */
1324 if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
1325 sensor.chan > st->info->max_channels_nr)
1326 return dev_err_probe(dev, -EINVAL,
1327 "chan:%d must be from %u to %u\n",
1328 sensor.chan,
1329 LTC2983_MIN_CHANNELS_NR,
1330 st->info->max_channels_nr);
1332 if (channel_avail_mask & BIT(sensor.chan))
1333 return dev_err_probe(dev, -EINVAL,
1334 "chan:%d already in use\n",
1335 sensor.chan);
1337 ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type);
1338 if (ret)
1339 return dev_err_probe(dev, ret,
1340 "adi,sensor-type property must given for child nodes\n");
1342 dev_dbg(dev, "Create new sensor, type %u, chann %u",
1343 sensor.type, sensor.chan);
1345 if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
1346 sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
1347 st->sensors[chan] = ltc2983_thermocouple_new(child, st,
1348 &sensor);
1349 } else if (sensor.type >= LTC2983_SENSOR_RTD &&
1350 sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
1351 st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
1352 } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
1353 sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
1354 st->sensors[chan] = ltc2983_thermistor_new(child, st,
1355 &sensor);
1356 } else if (sensor.type == LTC2983_SENSOR_DIODE) {
1357 st->sensors[chan] = ltc2983_diode_new(child, st,
1358 &sensor);
1359 } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
1360 st->sensors[chan] = ltc2983_r_sense_new(child, st,
1361 &sensor);
1362 /* don't add rsense to iio */
1363 st->iio_channels--;
1364 } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
1365 st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
1366 } else if (st->info->has_temp &&
1367 sensor.type == LTC2983_SENSOR_ACTIVE_TEMP) {
1368 st->sensors[chan] = ltc2983_temp_new(child, st, &sensor);
1369 } else {
1370 return dev_err_probe(dev, -EINVAL,
1371 "Unknown sensor type %d\n",
1372 sensor.type);
1375 if (IS_ERR(st->sensors[chan]))
1376 return dev_err_probe(dev, PTR_ERR(st->sensors[chan]),
1377 "Failed to create sensor\n");
1379 /* set generic sensor parameters */
1380 st->sensors[chan]->chan = sensor.chan;
1381 st->sensors[chan]->type = sensor.type;
1383 channel_avail_mask |= BIT(sensor.chan);
1384 chan++;
1387 return 0;
1390 static int ltc2983_eeprom_cmd(struct ltc2983_data *st, unsigned int cmd,
1391 unsigned int wait_time, unsigned int status_reg,
1392 unsigned long status_fail_mask)
1394 unsigned long time;
1395 unsigned int val;
1396 int ret;
1398 ret = regmap_bulk_write(st->regmap, LTC2983_EEPROM_KEY_REG,
1399 &st->eeprom_key, sizeof(st->eeprom_key));
1400 if (ret)
1401 return ret;
1403 reinit_completion(&st->completion);
1405 ret = regmap_write(st->regmap, LTC2983_STATUS_REG,
1406 LTC2983_STATUS_START(true) | cmd);
1407 if (ret)
1408 return ret;
1410 time = wait_for_completion_timeout(&st->completion,
1411 msecs_to_jiffies(wait_time));
1412 if (!time)
1413 return dev_err_probe(&st->spi->dev, -ETIMEDOUT,
1414 "EEPROM command timed out\n");
1416 ret = regmap_read(st->regmap, status_reg, &val);
1417 if (ret)
1418 return ret;
1420 if (val & status_fail_mask)
1421 return dev_err_probe(&st->spi->dev, -EINVAL,
1422 "EEPROM command failed: 0x%02X\n", val);
1424 return 0;
1427 static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
1429 u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
1430 int ret;
1432 /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
1433 ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
1434 LTC2983_STATUS_UP(status) == 1, 25000,
1435 25000 * 10);
1436 if (ret)
1437 return dev_err_probe(&st->spi->dev, ret,
1438 "Device startup timed out\n");
1440 ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
1441 LTC2983_NOTCH_FREQ_MASK,
1442 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
1443 if (ret)
1444 return ret;
1446 ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
1447 st->mux_delay_config);
1448 if (ret)
1449 return ret;
1451 if (st->info->has_eeprom && !assign_iio) {
1452 ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_READ_CMD,
1453 LTC2983_EEPROM_READ_TIME_MS,
1454 LTC2983_EEPROM_READ_STATUS_REG,
1455 LTC2983_EEPROM_READ_FAILURE_MASK);
1456 if (!ret)
1457 return 0;
1460 for (chan = 0; chan < st->num_channels; chan++) {
1461 u32 chan_type = 0, *iio_chan;
1463 ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
1464 if (ret)
1465 return ret;
1467 * The assign_iio flag is necessary for when the device is
1468 * coming out of sleep. In that case, we just need to
1469 * re-configure the device channels.
1470 * We also don't assign iio channels for rsense.
1472 if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
1473 !assign_iio)
1474 continue;
1476 /* assign iio channel */
1477 if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
1478 chan_type = IIO_TEMP;
1479 iio_chan = &iio_chan_t;
1480 } else {
1481 chan_type = IIO_VOLTAGE;
1482 iio_chan = &iio_chan_v;
1486 * add chan as the iio .address so that, we can directly
1487 * reference the sensor given the iio_chan_spec
1489 st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
1490 chan);
1493 return 0;
1496 static const struct regmap_range ltc2983_reg_ranges[] = {
1497 regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
1498 regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
1499 regmap_reg_range(LTC2983_EEPROM_KEY_REG, LTC2983_EEPROM_KEY_REG),
1500 regmap_reg_range(LTC2983_EEPROM_READ_STATUS_REG,
1501 LTC2983_EEPROM_READ_STATUS_REG),
1502 regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
1503 regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
1504 LTC2983_MULT_CHANNEL_END_REG),
1505 regmap_reg_range(LTC2986_EEPROM_STATUS_REG, LTC2986_EEPROM_STATUS_REG),
1506 regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
1507 regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
1508 LTC2983_CHAN_ASSIGN_END_REG),
1509 regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
1510 LTC2983_CUST_SENS_TBL_END_REG),
1513 static const struct regmap_access_table ltc2983_reg_table = {
1514 .yes_ranges = ltc2983_reg_ranges,
1515 .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
1519 * The reg_bits are actually 12 but the device needs the first *complete*
1520 * byte for the command (R/W).
1522 static const struct regmap_config ltc2983_regmap_config = {
1523 .reg_bits = 24,
1524 .val_bits = 8,
1525 .wr_table = &ltc2983_reg_table,
1526 .rd_table = &ltc2983_reg_table,
1527 .read_flag_mask = GENMASK(1, 0),
1528 .write_flag_mask = BIT(1),
1531 static const struct iio_info ltc2983_iio_info = {
1532 .read_raw = ltc2983_read_raw,
1533 .debugfs_reg_access = ltc2983_reg_access,
1536 static int ltc2983_probe(struct spi_device *spi)
1538 struct ltc2983_data *st;
1539 struct iio_dev *indio_dev;
1540 struct gpio_desc *gpio;
1541 int ret;
1543 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1544 if (!indio_dev)
1545 return -ENOMEM;
1547 st = iio_priv(indio_dev);
1549 st->info = spi_get_device_match_data(spi);
1550 if (!st->info)
1551 return -ENODEV;
1553 st->regmap = devm_regmap_init_spi(spi, &ltc2983_regmap_config);
1554 if (IS_ERR(st->regmap))
1555 return dev_err_probe(&spi->dev, PTR_ERR(st->regmap),
1556 "Failed to initialize regmap\n");
1558 mutex_init(&st->lock);
1559 init_completion(&st->completion);
1560 st->spi = spi;
1561 st->eeprom_key = cpu_to_be32(LTC2983_EEPROM_KEY);
1562 spi_set_drvdata(spi, st);
1564 ret = ltc2983_parse_fw(st);
1565 if (ret)
1566 return ret;
1568 ret = devm_regulator_get_enable(&spi->dev, "vdd");
1569 if (ret)
1570 return ret;
1572 gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH);
1573 if (IS_ERR(gpio))
1574 return PTR_ERR(gpio);
1576 if (gpio) {
1577 /* bring the device out of reset */
1578 usleep_range(1000, 1200);
1579 gpiod_set_value_cansleep(gpio, 0);
1582 st->iio_chan = devm_kzalloc(&spi->dev,
1583 st->iio_channels * sizeof(*st->iio_chan),
1584 GFP_KERNEL);
1585 if (!st->iio_chan)
1586 return -ENOMEM;
1588 ret = ltc2983_setup(st, true);
1589 if (ret)
1590 return ret;
1592 ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
1593 IRQF_TRIGGER_RISING, st->info->name, st);
1594 if (ret)
1595 return dev_err_probe(&spi->dev, ret,
1596 "failed to request an irq\n");
1598 if (st->info->has_eeprom) {
1599 ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_WRITE_CMD,
1600 LTC2983_EEPROM_WRITE_TIME_MS,
1601 LTC2986_EEPROM_STATUS_REG,
1602 LTC2983_EEPROM_STATUS_FAILURE_MASK);
1603 if (ret)
1604 return ret;
1607 indio_dev->name = st->info->name;
1608 indio_dev->num_channels = st->iio_channels;
1609 indio_dev->channels = st->iio_chan;
1610 indio_dev->modes = INDIO_DIRECT_MODE;
1611 indio_dev->info = &ltc2983_iio_info;
1613 return devm_iio_device_register(&spi->dev, indio_dev);
1616 static int ltc2983_resume(struct device *dev)
1618 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1619 int dummy;
1621 /* dummy read to bring the device out of sleep */
1622 regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
1623 /* we need to re-assign the channels */
1624 return ltc2983_setup(st, false);
1627 static int ltc2983_suspend(struct device *dev)
1629 struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1631 return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
1634 static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend,
1635 ltc2983_resume);
1637 static const struct ltc2983_chip_info ltc2983_chip_info_data = {
1638 .name = "ltc2983",
1639 .max_channels_nr = 20,
1642 static const struct ltc2983_chip_info ltc2984_chip_info_data = {
1643 .name = "ltc2984",
1644 .max_channels_nr = 20,
1645 .has_eeprom = true,
1648 static const struct ltc2983_chip_info ltc2986_chip_info_data = {
1649 .name = "ltc2986",
1650 .max_channels_nr = 10,
1651 .has_temp = true,
1652 .has_eeprom = true,
1655 static const struct ltc2983_chip_info ltm2985_chip_info_data = {
1656 .name = "ltm2985",
1657 .max_channels_nr = 10,
1658 .has_temp = true,
1659 .has_eeprom = true,
1662 static const struct spi_device_id ltc2983_id_table[] = {
1663 { "ltc2983", (kernel_ulong_t)&ltc2983_chip_info_data },
1664 { "ltc2984", (kernel_ulong_t)&ltc2984_chip_info_data },
1665 { "ltc2986", (kernel_ulong_t)&ltc2986_chip_info_data },
1666 { "ltm2985", (kernel_ulong_t)&ltm2985_chip_info_data },
1669 MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1671 static const struct of_device_id ltc2983_of_match[] = {
1672 { .compatible = "adi,ltc2983", .data = &ltc2983_chip_info_data },
1673 { .compatible = "adi,ltc2984", .data = &ltc2984_chip_info_data },
1674 { .compatible = "adi,ltc2986", .data = &ltc2986_chip_info_data },
1675 { .compatible = "adi,ltm2985", .data = &ltm2985_chip_info_data },
1678 MODULE_DEVICE_TABLE(of, ltc2983_of_match);
1680 static struct spi_driver ltc2983_driver = {
1681 .driver = {
1682 .name = "ltc2983",
1683 .of_match_table = ltc2983_of_match,
1684 .pm = pm_sleep_ptr(&ltc2983_pm_ops),
1686 .probe = ltc2983_probe,
1687 .id_table = ltc2983_id_table,
1690 module_spi_driver(ltc2983_driver);
1692 MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1693 MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1694 MODULE_LICENSE("GPL");