1f110f318SNuno Sá // SPDX-License-Identifier: GPL-2.0
2f110f318SNuno Sá /*
3f110f318SNuno Sá * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
4f110f318SNuno Sá * driver
5f110f318SNuno Sá *
6f110f318SNuno Sá * Copyright 2019 Analog Devices Inc.
7f110f318SNuno Sá */
8f110f318SNuno Sá #include <linux/bitfield.h>
9f110f318SNuno Sá #include <linux/completion.h>
10f110f318SNuno Sá #include <linux/device.h>
11a00838caSNuno Sa #include <linux/err.h>
12a00838caSNuno Sa #include <linux/errno.h>
13f110f318SNuno Sá #include <linux/kernel.h>
14f110f318SNuno Sá #include <linux/iio/iio.h>
15f110f318SNuno Sá #include <linux/interrupt.h>
16f110f318SNuno Sá #include <linux/list.h>
17bc4c9499SAndy Shevchenko #include <linux/mod_devicetable.h>
18f110f318SNuno Sá #include <linux/module.h>
19bc4c9499SAndy Shevchenko #include <linux/property.h>
20f110f318SNuno Sá #include <linux/regmap.h>
2147ef0501SNuno Sa #include <linux/regulator/consumer.h>
22f110f318SNuno Sá #include <linux/spi/spi.h>
23f110f318SNuno Sá
24bc4c9499SAndy Shevchenko #include <asm/byteorder.h>
25*5f60d5f6SAl Viro #include <linux/unaligned.h>
26bc4c9499SAndy Shevchenko
27f110f318SNuno Sá /* register map */
28f110f318SNuno Sá #define LTC2983_STATUS_REG 0x0000
29f110f318SNuno Sá #define LTC2983_TEMP_RES_START_REG 0x0010
30f110f318SNuno Sá #define LTC2983_TEMP_RES_END_REG 0x005F
316f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_KEY_REG 0x00B0
326f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_READ_STATUS_REG 0x00D0
33f110f318SNuno Sá #define LTC2983_GLOBAL_CONFIG_REG 0x00F0
34f110f318SNuno Sá #define LTC2983_MULT_CHANNEL_START_REG 0x00F4
35f110f318SNuno Sá #define LTC2983_MULT_CHANNEL_END_REG 0x00F7
366f7cadcfSCosmin Tanislav #define LTC2986_EEPROM_STATUS_REG 0x00F9
37f110f318SNuno Sá #define LTC2983_MUX_CONFIG_REG 0x00FF
38f110f318SNuno Sá #define LTC2983_CHAN_ASSIGN_START_REG 0x0200
39f110f318SNuno Sá #define LTC2983_CHAN_ASSIGN_END_REG 0x024F
40f110f318SNuno Sá #define LTC2983_CUST_SENS_TBL_START_REG 0x0250
41f110f318SNuno Sá #define LTC2983_CUST_SENS_TBL_END_REG 0x03CF
42f110f318SNuno Sá
43f110f318SNuno Sá #define LTC2983_DIFFERENTIAL_CHAN_MIN 2
44f110f318SNuno Sá #define LTC2983_MIN_CHANNELS_NR 1
45f110f318SNuno Sá #define LTC2983_SLEEP 0x97
46f110f318SNuno Sá #define LTC2983_CUSTOM_STEINHART_SIZE 24
47f110f318SNuno Sá #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6
48f110f318SNuno Sá #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4
49f110f318SNuno Sá
506f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_KEY 0xA53C0F5A
516f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_WRITE_CMD 0x15
526f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_READ_CMD 0x16
536f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_STATUS_FAILURE_MASK GENMASK(3, 1)
546f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_READ_FAILURE_MASK GENMASK(7, 0)
556f7cadcfSCosmin Tanislav
566f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_WRITE_TIME_MS 2600
576f7cadcfSCosmin Tanislav #define LTC2983_EEPROM_READ_TIME_MS 20
586f7cadcfSCosmin Tanislav
59f110f318SNuno Sá #define LTC2983_CHAN_START_ADDR(chan) \
60f110f318SNuno Sá (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
61f110f318SNuno Sá #define LTC2983_CHAN_RES_ADDR(chan) \
62f110f318SNuno Sá (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
63f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3)
64f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_SGL(x) \
65f110f318SNuno Sá FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
66f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0)
67f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_OC_CURR(x) \
68f110f318SNuno Sá FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
69f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2)
70f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
71f110f318SNuno Sá FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
72f110f318SNuno Sá
73f110f318SNuno Sá #define LTC2983_THERMISTOR_DIFF_MASK BIT(2)
74f110f318SNuno Sá #define LTC2983_THERMISTOR_SGL(x) \
75f110f318SNuno Sá FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
76f110f318SNuno Sá #define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1)
77f110f318SNuno Sá #define LTC2983_THERMISTOR_R_SHARE(x) \
78f110f318SNuno Sá FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
79f110f318SNuno Sá #define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0)
80f110f318SNuno Sá #define LTC2983_THERMISTOR_C_ROTATE(x) \
81f110f318SNuno Sá FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
82f110f318SNuno Sá
83f110f318SNuno Sá #define LTC2983_DIODE_DIFF_MASK BIT(2)
84f110f318SNuno Sá #define LTC2983_DIODE_SGL(x) \
85f110f318SNuno Sá FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
86f110f318SNuno Sá #define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1)
87f110f318SNuno Sá #define LTC2983_DIODE_3_CONV_CYCLE(x) \
88f110f318SNuno Sá FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
89f110f318SNuno Sá #define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0)
90f110f318SNuno Sá #define LTC2983_DIODE_AVERAGE_ON(x) \
91f110f318SNuno Sá FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
92f110f318SNuno Sá
93f110f318SNuno Sá #define LTC2983_RTD_4_WIRE_MASK BIT(3)
94f110f318SNuno Sá #define LTC2983_RTD_ROTATION_MASK BIT(1)
95f110f318SNuno Sá #define LTC2983_RTD_C_ROTATE(x) \
96f110f318SNuno Sá FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
97f110f318SNuno Sá #define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2)
98f110f318SNuno Sá #define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2)
99f110f318SNuno Sá #define LTC2983_RTD_N_WIRES(x) \
100f110f318SNuno Sá FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
101f110f318SNuno Sá #define LTC2983_RTD_R_SHARE_MASK BIT(0)
102f110f318SNuno Sá #define LTC2983_RTD_R_SHARE(x) \
103f110f318SNuno Sá FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
104f110f318SNuno Sá
105f110f318SNuno Sá #define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30)
106f110f318SNuno Sá #define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25)
107f110f318SNuno Sá
108f110f318SNuno Sá #define LTC2983_STATUS_START_MASK BIT(7)
109f110f318SNuno Sá #define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x)
110b76d26d6SNuno Sá #define LTC2983_STATUS_UP_MASK GENMASK(7, 6)
111b76d26d6SNuno Sá #define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
112f110f318SNuno Sá
113f110f318SNuno Sá #define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0)
114f110f318SNuno Sá #define LTC2983_STATUS_CHAN_SEL(x) \
115f110f318SNuno Sá FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
116f110f318SNuno Sá
117f110f318SNuno Sá #define LTC2983_TEMP_UNITS_MASK BIT(2)
118f110f318SNuno Sá #define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
119f110f318SNuno Sá
120f110f318SNuno Sá #define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0)
121f110f318SNuno Sá #define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
122f110f318SNuno Sá
123f110f318SNuno Sá #define LTC2983_RES_VALID_MASK BIT(24)
124f110f318SNuno Sá #define LTC2983_DATA_MASK GENMASK(23, 0)
125f110f318SNuno Sá #define LTC2983_DATA_SIGN_BIT 23
126f110f318SNuno Sá
127f110f318SNuno Sá #define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27)
128f110f318SNuno Sá #define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
129f110f318SNuno Sá
130f110f318SNuno Sá /* cold junction for thermocouples and rsense for rtd's and thermistor's */
131f110f318SNuno Sá #define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22)
132f110f318SNuno Sá #define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
133f110f318SNuno Sá
134f110f318SNuno Sá #define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0)
135f110f318SNuno Sá #define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
136f110f318SNuno Sá
137f110f318SNuno Sá #define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6)
138f110f318SNuno Sá #define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
139f110f318SNuno Sá
140f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18)
141f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_CFG(x) \
142f110f318SNuno Sá FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
143f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29)
144f110f318SNuno Sá #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25)
145f110f318SNuno Sá
146f110f318SNuno Sá #define LTC2983_RTD_CFG_MASK GENMASK(21, 18)
147f110f318SNuno Sá #define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
148f110f318SNuno Sá #define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14)
149f110f318SNuno Sá #define LTC2983_RTD_EXC_CURRENT(x) \
150f110f318SNuno Sá FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
151f110f318SNuno Sá #define LTC2983_RTD_CURVE_MASK GENMASK(13, 12)
152f110f318SNuno Sá #define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
153f110f318SNuno Sá
154f110f318SNuno Sá #define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19)
155f110f318SNuno Sá #define LTC2983_THERMISTOR_CFG(x) \
156f110f318SNuno Sá FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
157f110f318SNuno Sá #define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15)
158f110f318SNuno Sá #define LTC2983_THERMISTOR_EXC_CURRENT(x) \
159f110f318SNuno Sá FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
160f110f318SNuno Sá
161f110f318SNuno Sá #define LTC2983_DIODE_CFG_MASK GENMASK(26, 24)
162f110f318SNuno Sá #define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
163f110f318SNuno Sá #define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22)
164f110f318SNuno Sá #define LTC2983_DIODE_EXC_CURRENT(x) \
165f110f318SNuno Sá FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
166f110f318SNuno Sá #define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0)
167f110f318SNuno Sá #define LTC2983_DIODE_IDEAL_FACTOR(x) \
168f110f318SNuno Sá FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
169f110f318SNuno Sá
170f110f318SNuno Sá #define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0)
171f110f318SNuno Sá #define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
172f110f318SNuno Sá
173f110f318SNuno Sá #define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26)
174f110f318SNuno Sá #define LTC2983_ADC_SINGLE_ENDED(x) \
175f110f318SNuno Sá FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
176f110f318SNuno Sá
177f110f318SNuno Sá enum {
178f110f318SNuno Sá LTC2983_SENSOR_THERMOCOUPLE = 1,
179f110f318SNuno Sá LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
180f110f318SNuno Sá LTC2983_SENSOR_RTD = 10,
181f110f318SNuno Sá LTC2983_SENSOR_RTD_CUSTOM = 18,
182f110f318SNuno Sá LTC2983_SENSOR_THERMISTOR = 19,
183f110f318SNuno Sá LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
184f110f318SNuno Sá LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
185f110f318SNuno Sá LTC2983_SENSOR_DIODE = 28,
186f110f318SNuno Sá LTC2983_SENSOR_SENSE_RESISTOR = 29,
187f110f318SNuno Sá LTC2983_SENSOR_DIRECT_ADC = 30,
1886f7cadcfSCosmin Tanislav LTC2983_SENSOR_ACTIVE_TEMP = 31,
189f110f318SNuno Sá };
190f110f318SNuno Sá
191f110f318SNuno Sá #define to_thermocouple(_sensor) \
192f110f318SNuno Sá container_of(_sensor, struct ltc2983_thermocouple, sensor)
193f110f318SNuno Sá
194f110f318SNuno Sá #define to_rtd(_sensor) \
195f110f318SNuno Sá container_of(_sensor, struct ltc2983_rtd, sensor)
196f110f318SNuno Sá
197f110f318SNuno Sá #define to_thermistor(_sensor) \
198f110f318SNuno Sá container_of(_sensor, struct ltc2983_thermistor, sensor)
199f110f318SNuno Sá
200f110f318SNuno Sá #define to_diode(_sensor) \
201f110f318SNuno Sá container_of(_sensor, struct ltc2983_diode, sensor)
202f110f318SNuno Sá
203f110f318SNuno Sá #define to_rsense(_sensor) \
204f110f318SNuno Sá container_of(_sensor, struct ltc2983_rsense, sensor)
205f110f318SNuno Sá
206f110f318SNuno Sá #define to_adc(_sensor) \
207f110f318SNuno Sá container_of(_sensor, struct ltc2983_adc, sensor)
208f110f318SNuno Sá
2096f7cadcfSCosmin Tanislav #define to_temp(_sensor) \
2106f7cadcfSCosmin Tanislav container_of(_sensor, struct ltc2983_temp, sensor)
2116f7cadcfSCosmin Tanislav
2126f7cadcfSCosmin Tanislav struct ltc2983_chip_info {
2135cad30abSNuno Sa const char *name;
2146f7cadcfSCosmin Tanislav unsigned int max_channels_nr;
2156f7cadcfSCosmin Tanislav bool has_temp;
2166f7cadcfSCosmin Tanislav bool has_eeprom;
2176f7cadcfSCosmin Tanislav };
2186f7cadcfSCosmin Tanislav
219f110f318SNuno Sá struct ltc2983_data {
2206f7cadcfSCosmin Tanislav const struct ltc2983_chip_info *info;
221f110f318SNuno Sá struct regmap *regmap;
222f110f318SNuno Sá struct spi_device *spi;
223f110f318SNuno Sá struct mutex lock;
224f110f318SNuno Sá struct completion completion;
225f110f318SNuno Sá struct iio_chan_spec *iio_chan;
226f110f318SNuno Sá struct ltc2983_sensor **sensors;
227f110f318SNuno Sá u32 mux_delay_config;
228f110f318SNuno Sá u32 filter_notch_freq;
229f110f318SNuno Sá u16 custom_table_size;
230f110f318SNuno Sá u8 num_channels;
231f110f318SNuno Sá u8 iio_channels;
232f110f318SNuno Sá /*
233732f2cb2SJonathan Cameron * DMA (thus cache coherency maintenance) may require the
234f110f318SNuno Sá * transfer buffers to live in their own cache lines.
235f110f318SNuno Sá * Holds the converted temperature
236f110f318SNuno Sá */
237732f2cb2SJonathan Cameron __be32 temp __aligned(IIO_DMA_MINALIGN);
2385e017621SCosmin Tanislav __be32 chan_val;
2396f7cadcfSCosmin Tanislav __be32 eeprom_key;
240f110f318SNuno Sá };
241f110f318SNuno Sá
242f110f318SNuno Sá struct ltc2983_sensor {
243f110f318SNuno Sá int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
244f110f318SNuno Sá int (*assign_chan)(struct ltc2983_data *st,
245f110f318SNuno Sá const struct ltc2983_sensor *sensor);
246f110f318SNuno Sá /* specifies the sensor channel */
247f110f318SNuno Sá u32 chan;
248f110f318SNuno Sá /* sensor type */
249f110f318SNuno Sá u32 type;
250f110f318SNuno Sá };
251f110f318SNuno Sá
252f110f318SNuno Sá struct ltc2983_custom_sensor {
253f110f318SNuno Sá /* raw table sensor data */
254bc4c9499SAndy Shevchenko void *table;
255f110f318SNuno Sá size_t size;
256f110f318SNuno Sá /* address offset */
257f110f318SNuno Sá s8 offset;
258f110f318SNuno Sá bool is_steinhart;
259f110f318SNuno Sá };
260f110f318SNuno Sá
261f110f318SNuno Sá struct ltc2983_thermocouple {
262f110f318SNuno Sá struct ltc2983_sensor sensor;
263f110f318SNuno Sá struct ltc2983_custom_sensor *custom;
264f110f318SNuno Sá u32 sensor_config;
265f110f318SNuno Sá u32 cold_junction_chan;
266f110f318SNuno Sá };
267f110f318SNuno Sá
268f110f318SNuno Sá struct ltc2983_rtd {
269f110f318SNuno Sá struct ltc2983_sensor sensor;
270f110f318SNuno Sá struct ltc2983_custom_sensor *custom;
271f110f318SNuno Sá u32 sensor_config;
272f110f318SNuno Sá u32 r_sense_chan;
273f110f318SNuno Sá u32 excitation_current;
274f110f318SNuno Sá u32 rtd_curve;
275f110f318SNuno Sá };
276f110f318SNuno Sá
277f110f318SNuno Sá struct ltc2983_thermistor {
278f110f318SNuno Sá struct ltc2983_sensor sensor;
279f110f318SNuno Sá struct ltc2983_custom_sensor *custom;
280f110f318SNuno Sá u32 sensor_config;
281f110f318SNuno Sá u32 r_sense_chan;
282f110f318SNuno Sá u32 excitation_current;
283f110f318SNuno Sá };
284f110f318SNuno Sá
285f110f318SNuno Sá struct ltc2983_diode {
286f110f318SNuno Sá struct ltc2983_sensor sensor;
287f110f318SNuno Sá u32 sensor_config;
288f110f318SNuno Sá u32 excitation_current;
289f110f318SNuno Sá u32 ideal_factor_value;
290f110f318SNuno Sá };
291f110f318SNuno Sá
292f110f318SNuno Sá struct ltc2983_rsense {
293f110f318SNuno Sá struct ltc2983_sensor sensor;
294f110f318SNuno Sá u32 r_sense_val;
295f110f318SNuno Sá };
296f110f318SNuno Sá
297f110f318SNuno Sá struct ltc2983_adc {
298f110f318SNuno Sá struct ltc2983_sensor sensor;
299f110f318SNuno Sá bool single_ended;
300f110f318SNuno Sá };
301f110f318SNuno Sá
3026f7cadcfSCosmin Tanislav struct ltc2983_temp {
3036f7cadcfSCosmin Tanislav struct ltc2983_sensor sensor;
3046f7cadcfSCosmin Tanislav struct ltc2983_custom_sensor *custom;
3056f7cadcfSCosmin Tanislav bool single_ended;
3066f7cadcfSCosmin Tanislav };
3076f7cadcfSCosmin Tanislav
308f110f318SNuno Sá /*
309f110f318SNuno Sá * Convert to Q format numbers. These number's are integers where
310f110f318SNuno Sá * the number of integer and fractional bits are specified. The resolution
311f110f318SNuno Sá * is given by 1/@resolution and tell us the number of fractional bits. For
312f110f318SNuno Sá * instance a resolution of 2^-10 means we have 10 fractional bits.
313f110f318SNuno Sá */
__convert_to_raw(const u64 val,const u32 resolution)314f110f318SNuno Sá static u32 __convert_to_raw(const u64 val, const u32 resolution)
315f110f318SNuno Sá {
316f110f318SNuno Sá u64 __res = val * resolution;
317f110f318SNuno Sá
318f110f318SNuno Sá /* all values are multiplied by 1000000 to remove the fraction */
319f110f318SNuno Sá do_div(__res, 1000000);
320f110f318SNuno Sá
321f110f318SNuno Sá return __res;
322f110f318SNuno Sá }
323f110f318SNuno Sá
__convert_to_raw_sign(const u64 val,const u32 resolution)324f110f318SNuno Sá static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
325f110f318SNuno Sá {
326f110f318SNuno Sá s64 __res = -(s32)val;
327f110f318SNuno Sá
328f110f318SNuno Sá __res = __convert_to_raw(__res, resolution);
329f110f318SNuno Sá
330f110f318SNuno Sá return (u32)-__res;
331f110f318SNuno Sá }
332f110f318SNuno Sá
__ltc2983_fault_handler(const struct ltc2983_data * st,const u32 result,const u32 hard_mask,const u32 soft_mask)333f110f318SNuno Sá static int __ltc2983_fault_handler(const struct ltc2983_data *st,
334f110f318SNuno Sá const u32 result, const u32 hard_mask,
335f110f318SNuno Sá const u32 soft_mask)
336f110f318SNuno Sá {
337f110f318SNuno Sá const struct device *dev = &st->spi->dev;
338f110f318SNuno Sá
339f110f318SNuno Sá if (result & hard_mask) {
340f110f318SNuno Sá dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
341f110f318SNuno Sá return -EIO;
342f110f318SNuno Sá } else if (result & soft_mask) {
343f110f318SNuno Sá /* just print a warning */
344f110f318SNuno Sá dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
345f110f318SNuno Sá }
346f110f318SNuno Sá
347f110f318SNuno Sá return 0;
348f110f318SNuno Sá }
349f110f318SNuno Sá
__ltc2983_chan_assign_common(struct ltc2983_data * st,const struct ltc2983_sensor * sensor,u32 chan_val)3505e017621SCosmin Tanislav static int __ltc2983_chan_assign_common(struct ltc2983_data *st,
351f110f318SNuno Sá const struct ltc2983_sensor *sensor,
352f110f318SNuno Sá u32 chan_val)
353f110f318SNuno Sá {
354f110f318SNuno Sá u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
355f110f318SNuno Sá
356f110f318SNuno Sá chan_val |= LTC2983_CHAN_TYPE(sensor->type);
357f110f318SNuno Sá dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
358f110f318SNuno Sá chan_val);
3595e017621SCosmin Tanislav st->chan_val = cpu_to_be32(chan_val);
3605e017621SCosmin Tanislav return regmap_bulk_write(st->regmap, reg, &st->chan_val,
3615e017621SCosmin Tanislav sizeof(st->chan_val));
362f110f318SNuno Sá }
363f110f318SNuno Sá
__ltc2983_chan_custom_sensor_assign(struct ltc2983_data * st,struct ltc2983_custom_sensor * custom,u32 * chan_val)364f110f318SNuno Sá static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
365f110f318SNuno Sá struct ltc2983_custom_sensor *custom,
366f110f318SNuno Sá u32 *chan_val)
367f110f318SNuno Sá {
368f110f318SNuno Sá u32 reg;
369f110f318SNuno Sá u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
370f110f318SNuno Sá LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
371f110f318SNuno Sá const struct device *dev = &st->spi->dev;
372f110f318SNuno Sá /*
373f110f318SNuno Sá * custom->size holds the raw size of the table. However, when
374f110f318SNuno Sá * configuring the sensor channel, we must write the number of
375f110f318SNuno Sá * entries of the table minus 1. For steinhart sensors 0 is written
376f110f318SNuno Sá * since the size is constant!
377f110f318SNuno Sá */
378f110f318SNuno Sá const u8 len = custom->is_steinhart ? 0 :
379f110f318SNuno Sá (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
380f110f318SNuno Sá /*
381f110f318SNuno Sá * Check if the offset was assigned already. It should be for steinhart
382f110f318SNuno Sá * sensors. When coming from sleep, it should be assigned for all.
383f110f318SNuno Sá */
384f110f318SNuno Sá if (custom->offset < 0) {
385f110f318SNuno Sá /*
386f110f318SNuno Sá * This needs to be done again here because, from the moment
387f110f318SNuno Sá * when this test was done (successfully) for this custom
388f110f318SNuno Sá * sensor, a steinhart sensor might have been added changing
389f110f318SNuno Sá * custom_table_size...
390f110f318SNuno Sá */
391f110f318SNuno Sá if (st->custom_table_size + custom->size >
392f110f318SNuno Sá (LTC2983_CUST_SENS_TBL_END_REG -
393f110f318SNuno Sá LTC2983_CUST_SENS_TBL_START_REG) + 1) {
394f110f318SNuno Sá dev_err(dev,
395f110f318SNuno Sá "Not space left(%d) for new custom sensor(%zu)",
396f110f318SNuno Sá st->custom_table_size,
397f110f318SNuno Sá custom->size);
398f110f318SNuno Sá return -EINVAL;
399f110f318SNuno Sá }
400f110f318SNuno Sá
401f110f318SNuno Sá custom->offset = st->custom_table_size /
402f110f318SNuno Sá LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
403f110f318SNuno Sá st->custom_table_size += custom->size;
404f110f318SNuno Sá }
405f110f318SNuno Sá
406f110f318SNuno Sá reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
407f110f318SNuno Sá
408f110f318SNuno Sá *chan_val |= LTC2983_CUSTOM_LEN(len);
409f110f318SNuno Sá *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
410f110f318SNuno Sá dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
411f110f318SNuno Sá reg, custom->offset,
412f110f318SNuno Sá custom->size);
413f110f318SNuno Sá /* write custom sensor table */
414f110f318SNuno Sá return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
415f110f318SNuno Sá }
416f110f318SNuno Sá
417bc4c9499SAndy Shevchenko static struct ltc2983_custom_sensor *
__ltc2983_custom_sensor_new(struct ltc2983_data * st,const struct fwnode_handle * fn,const char * propname,const bool is_steinhart,const u32 resolution,const bool has_signed)418bc4c9499SAndy Shevchenko __ltc2983_custom_sensor_new(struct ltc2983_data *st, const struct fwnode_handle *fn,
419bc4c9499SAndy Shevchenko const char *propname, const bool is_steinhart,
420bc4c9499SAndy Shevchenko const u32 resolution, const bool has_signed)
421f110f318SNuno Sá {
422f110f318SNuno Sá struct ltc2983_custom_sensor *new_custom;
423f110f318SNuno Sá struct device *dev = &st->spi->dev;
424f110f318SNuno Sá /*
425f110f318SNuno Sá * For custom steinhart, the full u32 is taken. For all the others
426f110f318SNuno Sá * the MSB is discarded.
427f110f318SNuno Sá */
4285a464c6eSRohit Sarkar const u8 n_size = is_steinhart ? 4 : 3;
429bc4c9499SAndy Shevchenko u8 index, n_entries;
430bc4c9499SAndy Shevchenko int ret;
431f110f318SNuno Sá
432bc4c9499SAndy Shevchenko if (is_steinhart)
433bc4c9499SAndy Shevchenko n_entries = fwnode_property_count_u32(fn, propname);
434bc4c9499SAndy Shevchenko else
435bc4c9499SAndy Shevchenko n_entries = fwnode_property_count_u64(fn, propname);
436f110f318SNuno Sá /* n_entries must be an even number */
437a00838caSNuno Sa if (!n_entries || (n_entries % 2) != 0)
438a00838caSNuno Sa return dev_err_ptr_probe(dev, -EINVAL,
439a00838caSNuno Sa "Number of entries either 0 or not even\n");
440f110f318SNuno Sá
441f110f318SNuno Sá new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
442f110f318SNuno Sá if (!new_custom)
443f110f318SNuno Sá return ERR_PTR(-ENOMEM);
444f110f318SNuno Sá
445f110f318SNuno Sá new_custom->size = n_entries * n_size;
446f110f318SNuno Sá /* check Steinhart size */
447a00838caSNuno Sa if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE)
448a00838caSNuno Sa return dev_err_ptr_probe(dev, -EINVAL,
449a00838caSNuno Sa "Steinhart sensors size(%zu) must be %u\n",
450a00838caSNuno Sa new_custom->size, LTC2983_CUSTOM_STEINHART_SIZE);
451a00838caSNuno Sa
452f110f318SNuno Sá /* Check space on the table. */
453f110f318SNuno Sá if (st->custom_table_size + new_custom->size >
454a00838caSNuno Sa (LTC2983_CUST_SENS_TBL_END_REG - LTC2983_CUST_SENS_TBL_START_REG) + 1)
455a00838caSNuno Sa return dev_err_ptr_probe(dev, -EINVAL,
456a00838caSNuno Sa "No space left(%d) for new custom sensor(%zu)\n",
457f110f318SNuno Sá st->custom_table_size, new_custom->size);
458f110f318SNuno Sá
459f110f318SNuno Sá /* allocate the table */
460bc4c9499SAndy Shevchenko if (is_steinhart)
461bc4c9499SAndy Shevchenko new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u32), GFP_KERNEL);
462bc4c9499SAndy Shevchenko else
463bc4c9499SAndy Shevchenko new_custom->table = devm_kcalloc(dev, n_entries, sizeof(u64), GFP_KERNEL);
464f110f318SNuno Sá if (!new_custom->table)
465f110f318SNuno Sá return ERR_PTR(-ENOMEM);
466f110f318SNuno Sá
467f110f318SNuno Sá /*
468bc4c9499SAndy Shevchenko * Steinhart sensors are configured with raw values in the firmware
469bc4c9499SAndy Shevchenko * node. For the other sensors we must convert the value to raw.
470bc4c9499SAndy Shevchenko * The odd index's correspond to temperatures and always have 1/1024
471bc4c9499SAndy Shevchenko * of resolution. Temperatures also come in Kelvin, so signed values
472bc4c9499SAndy Shevchenko * are not possible.
473f110f318SNuno Sá */
474bc4c9499SAndy Shevchenko if (is_steinhart) {
475bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32_array(fn, propname, new_custom->table, n_entries);
476bc4c9499SAndy Shevchenko if (ret < 0)
477bc4c9499SAndy Shevchenko return ERR_PTR(ret);
478bc4c9499SAndy Shevchenko
479bc4c9499SAndy Shevchenko cpu_to_be32_array(new_custom->table, new_custom->table, n_entries);
480bc4c9499SAndy Shevchenko } else {
481bc4c9499SAndy Shevchenko ret = fwnode_property_read_u64_array(fn, propname, new_custom->table, n_entries);
482bc4c9499SAndy Shevchenko if (ret < 0)
483bc4c9499SAndy Shevchenko return ERR_PTR(ret);
484bc4c9499SAndy Shevchenko
485bc4c9499SAndy Shevchenko for (index = 0; index < n_entries; index++) {
486bc4c9499SAndy Shevchenko u64 temp = ((u64 *)new_custom->table)[index];
487f110f318SNuno Sá
488f110f318SNuno Sá if ((index % 2) != 0)
489f110f318SNuno Sá temp = __convert_to_raw(temp, 1024);
490f110f318SNuno Sá else if (has_signed && (s64)temp < 0)
491f110f318SNuno Sá temp = __convert_to_raw_sign(temp, resolution);
492f110f318SNuno Sá else
493f110f318SNuno Sá temp = __convert_to_raw(temp, resolution);
4942e19b6c3SColin Ian King
495bc4c9499SAndy Shevchenko put_unaligned_be24(temp, new_custom->table + index * 3);
496f110f318SNuno Sá }
497f110f318SNuno Sá }
498f110f318SNuno Sá
499f110f318SNuno Sá new_custom->is_steinhart = is_steinhart;
500f110f318SNuno Sá /*
501f110f318SNuno Sá * This is done to first add all the steinhart sensors to the table,
502f110f318SNuno Sá * in order to maximize the table usage. If we mix adding steinhart
503f110f318SNuno Sá * with the other sensors, we might have to do some roundup to make
504f110f318SNuno Sá * sure that sensor_addr - 0x250(start address) is a multiple of 4
505f110f318SNuno Sá * (for steinhart), and a multiple of 6 for all the other sensors.
506f110f318SNuno Sá * Since we have const 24 bytes for steinhart sensors and 24 is
507f110f318SNuno Sá * also a multiple of 6, we guarantee that the first non-steinhart
508f110f318SNuno Sá * sensor will sit in a correct address without the need of filling
509f110f318SNuno Sá * addresses.
510f110f318SNuno Sá */
511f110f318SNuno Sá if (is_steinhart) {
512f110f318SNuno Sá new_custom->offset = st->custom_table_size /
513f110f318SNuno Sá LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
514f110f318SNuno Sá st->custom_table_size += new_custom->size;
515f110f318SNuno Sá } else {
516f110f318SNuno Sá /* mark as unset. This is checked later on the assign phase */
517f110f318SNuno Sá new_custom->offset = -1;
518f110f318SNuno Sá }
519f110f318SNuno Sá
520f110f318SNuno Sá return new_custom;
521f110f318SNuno Sá }
522f110f318SNuno Sá
ltc2983_thermocouple_fault_handler(const struct ltc2983_data * st,const u32 result)523f110f318SNuno Sá static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
524f110f318SNuno Sá const u32 result)
525f110f318SNuno Sá {
526f110f318SNuno Sá return __ltc2983_fault_handler(st, result,
527f110f318SNuno Sá LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
528f110f318SNuno Sá LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
529f110f318SNuno Sá }
530f110f318SNuno Sá
ltc2983_common_fault_handler(const struct ltc2983_data * st,const u32 result)531f110f318SNuno Sá static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
532f110f318SNuno Sá const u32 result)
533f110f318SNuno Sá {
534f110f318SNuno Sá return __ltc2983_fault_handler(st, result,
535f110f318SNuno Sá LTC2983_COMMON_HARD_FAULT_MASK,
536f110f318SNuno Sá LTC2983_COMMON_SOFT_FAULT_MASK);
537f110f318SNuno Sá }
538f110f318SNuno Sá
ltc2983_thermocouple_assign_chan(struct ltc2983_data * st,const struct ltc2983_sensor * sensor)539f110f318SNuno Sá static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
540f110f318SNuno Sá const struct ltc2983_sensor *sensor)
541f110f318SNuno Sá {
542f110f318SNuno Sá struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
543f110f318SNuno Sá u32 chan_val;
544f110f318SNuno Sá
545f110f318SNuno Sá chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
546f110f318SNuno Sá chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
547f110f318SNuno Sá
548f110f318SNuno Sá if (thermo->custom) {
549f110f318SNuno Sá int ret;
550f110f318SNuno Sá
551f110f318SNuno Sá ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
552f110f318SNuno Sá &chan_val);
553f110f318SNuno Sá if (ret)
554f110f318SNuno Sá return ret;
555f110f318SNuno Sá }
556f110f318SNuno Sá return __ltc2983_chan_assign_common(st, sensor, chan_val);
557f110f318SNuno Sá }
558f110f318SNuno Sá
ltc2983_rtd_assign_chan(struct ltc2983_data * st,const struct ltc2983_sensor * sensor)559f110f318SNuno Sá static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
560f110f318SNuno Sá const struct ltc2983_sensor *sensor)
561f110f318SNuno Sá {
562f110f318SNuno Sá struct ltc2983_rtd *rtd = to_rtd(sensor);
563f110f318SNuno Sá u32 chan_val;
564f110f318SNuno Sá
565f110f318SNuno Sá chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
566f110f318SNuno Sá chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
567f110f318SNuno Sá chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
568f110f318SNuno Sá chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
569f110f318SNuno Sá
570f110f318SNuno Sá if (rtd->custom) {
571f110f318SNuno Sá int ret;
572f110f318SNuno Sá
573f110f318SNuno Sá ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
574f110f318SNuno Sá &chan_val);
575f110f318SNuno Sá if (ret)
576f110f318SNuno Sá return ret;
577f110f318SNuno Sá }
578f110f318SNuno Sá return __ltc2983_chan_assign_common(st, sensor, chan_val);
579f110f318SNuno Sá }
580f110f318SNuno Sá
ltc2983_thermistor_assign_chan(struct ltc2983_data * st,const struct ltc2983_sensor * sensor)581f110f318SNuno Sá static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
582f110f318SNuno Sá const struct ltc2983_sensor *sensor)
583f110f318SNuno Sá {
584f110f318SNuno Sá struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
585f110f318SNuno Sá u32 chan_val;
586f110f318SNuno Sá
587f110f318SNuno Sá chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
588f110f318SNuno Sá chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
589f110f318SNuno Sá chan_val |=
590f110f318SNuno Sá LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
591f110f318SNuno Sá
592f110f318SNuno Sá if (thermistor->custom) {
593f110f318SNuno Sá int ret;
594f110f318SNuno Sá
595f110f318SNuno Sá ret = __ltc2983_chan_custom_sensor_assign(st,
596f110f318SNuno Sá thermistor->custom,
597f110f318SNuno Sá &chan_val);
598f110f318SNuno Sá if (ret)
599f110f318SNuno Sá return ret;
600f110f318SNuno Sá }
601f110f318SNuno Sá return __ltc2983_chan_assign_common(st, sensor, chan_val);
602f110f318SNuno Sá }
603f110f318SNuno Sá
ltc2983_diode_assign_chan(struct ltc2983_data * st,const struct ltc2983_sensor * sensor)604f110f318SNuno Sá static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
605f110f318SNuno Sá const struct ltc2983_sensor *sensor)
606f110f318SNuno Sá {
607f110f318SNuno Sá struct ltc2983_diode *diode = to_diode(sensor);
608f110f318SNuno Sá u32 chan_val;
609f110f318SNuno Sá
610f110f318SNuno Sá chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
611f110f318SNuno Sá chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
612f110f318SNuno Sá chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
613f110f318SNuno Sá
614f110f318SNuno Sá return __ltc2983_chan_assign_common(st, sensor, chan_val);
615f110f318SNuno Sá }
616f110f318SNuno Sá
ltc2983_r_sense_assign_chan(struct ltc2983_data * st,const struct ltc2983_sensor * sensor)617f110f318SNuno Sá static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
618f110f318SNuno Sá const struct ltc2983_sensor *sensor)
619f110f318SNuno Sá {
620f110f318SNuno Sá struct ltc2983_rsense *rsense = to_rsense(sensor);
621f110f318SNuno Sá u32 chan_val;
622f110f318SNuno Sá
623f110f318SNuno Sá chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
624f110f318SNuno Sá
625f110f318SNuno Sá return __ltc2983_chan_assign_common(st, sensor, chan_val);
626f110f318SNuno Sá }
627f110f318SNuno Sá
ltc2983_adc_assign_chan(struct ltc2983_data * st,const struct ltc2983_sensor * sensor)628f110f318SNuno Sá static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
629f110f318SNuno Sá const struct ltc2983_sensor *sensor)
630f110f318SNuno Sá {
631f110f318SNuno Sá struct ltc2983_adc *adc = to_adc(sensor);
632f110f318SNuno Sá u32 chan_val;
633f110f318SNuno Sá
634f110f318SNuno Sá chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
635f110f318SNuno Sá
636f110f318SNuno Sá return __ltc2983_chan_assign_common(st, sensor, chan_val);
637f110f318SNuno Sá }
638f110f318SNuno Sá
ltc2983_temp_assign_chan(struct ltc2983_data * st,const struct ltc2983_sensor * sensor)6396f7cadcfSCosmin Tanislav static int ltc2983_temp_assign_chan(struct ltc2983_data *st,
6406f7cadcfSCosmin Tanislav const struct ltc2983_sensor *sensor)
6416f7cadcfSCosmin Tanislav {
6426f7cadcfSCosmin Tanislav struct ltc2983_temp *temp = to_temp(sensor);
6436f7cadcfSCosmin Tanislav u32 chan_val;
6446f7cadcfSCosmin Tanislav int ret;
6456f7cadcfSCosmin Tanislav
6466f7cadcfSCosmin Tanislav chan_val = LTC2983_ADC_SINGLE_ENDED(temp->single_ended);
6476f7cadcfSCosmin Tanislav
6486f7cadcfSCosmin Tanislav ret = __ltc2983_chan_custom_sensor_assign(st, temp->custom, &chan_val);
6496f7cadcfSCosmin Tanislav if (ret)
6506f7cadcfSCosmin Tanislav return ret;
6516f7cadcfSCosmin Tanislav
6526f7cadcfSCosmin Tanislav return __ltc2983_chan_assign_common(st, sensor, chan_val);
6536f7cadcfSCosmin Tanislav }
6546f7cadcfSCosmin Tanislav
655bc4c9499SAndy Shevchenko static struct ltc2983_sensor *
ltc2983_thermocouple_new(const struct fwnode_handle * child,struct ltc2983_data * st,const struct ltc2983_sensor * sensor)656bc4c9499SAndy Shevchenko ltc2983_thermocouple_new(const struct fwnode_handle *child, struct ltc2983_data *st,
657f110f318SNuno Sá const struct ltc2983_sensor *sensor)
658f110f318SNuno Sá {
659f110f318SNuno Sá struct ltc2983_thermocouple *thermo;
660f110f318SNuno Sá u32 oc_current;
661f110f318SNuno Sá int ret;
662f110f318SNuno Sá
663f110f318SNuno Sá thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
664f110f318SNuno Sá if (!thermo)
665f110f318SNuno Sá return ERR_PTR(-ENOMEM);
666f110f318SNuno Sá
667bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,single-ended"))
668f110f318SNuno Sá thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
669f110f318SNuno Sá
670bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(child, "adi,sensor-oc-current-microamp", &oc_current);
671f110f318SNuno Sá if (!ret) {
672f110f318SNuno Sá switch (oc_current) {
673f110f318SNuno Sá case 10:
674f110f318SNuno Sá thermo->sensor_config |=
675f110f318SNuno Sá LTC2983_THERMOCOUPLE_OC_CURR(0);
676f110f318SNuno Sá break;
677f110f318SNuno Sá case 100:
678f110f318SNuno Sá thermo->sensor_config |=
679f110f318SNuno Sá LTC2983_THERMOCOUPLE_OC_CURR(1);
680f110f318SNuno Sá break;
681f110f318SNuno Sá case 500:
682f110f318SNuno Sá thermo->sensor_config |=
683f110f318SNuno Sá LTC2983_THERMOCOUPLE_OC_CURR(2);
684f110f318SNuno Sá break;
685f110f318SNuno Sá case 1000:
686f110f318SNuno Sá thermo->sensor_config |=
687f110f318SNuno Sá LTC2983_THERMOCOUPLE_OC_CURR(3);
688f110f318SNuno Sá break;
689f110f318SNuno Sá default:
690a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
691a00838caSNuno Sa "Invalid open circuit current:%u\n",
692a00838caSNuno Sa oc_current);
693f110f318SNuno Sá }
694f110f318SNuno Sá
695f110f318SNuno Sá thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
696f110f318SNuno Sá }
697f110f318SNuno Sá /* validate channel index */
698f110f318SNuno Sá if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
699a00838caSNuno Sa sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
700a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
701a00838caSNuno Sa "Invalid chann:%d for differential thermocouple\n",
702f110f318SNuno Sá sensor->chan);
703f110f318SNuno Sá
7048ca555bdSJonathan Cameron struct fwnode_handle *ref __free(fwnode_handle) =
7058ca555bdSJonathan Cameron fwnode_find_reference(child, "adi,cold-junction-handle", 0);
706bc4c9499SAndy Shevchenko if (IS_ERR(ref)) {
707bc4c9499SAndy Shevchenko ref = NULL;
708bc4c9499SAndy Shevchenko } else {
709bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(ref, "reg", &thermo->cold_junction_chan);
710a00838caSNuno Sa if (ret)
711f110f318SNuno Sá /*
712f110f318SNuno Sá * This would be catched later but we can just return
713f110f318SNuno Sá * the error right away.
714f110f318SNuno Sá */
715a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, ret,
716a00838caSNuno Sa "Property reg must be given\n");
717f110f318SNuno Sá }
718f110f318SNuno Sá
719f110f318SNuno Sá /* check custom sensor */
720f110f318SNuno Sá if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
721f110f318SNuno Sá const char *propname = "adi,custom-thermocouple";
722f110f318SNuno Sá
723f110f318SNuno Sá thermo->custom = __ltc2983_custom_sensor_new(st, child,
724f110f318SNuno Sá propname, false,
725f110f318SNuno Sá 16384, true);
7268ca555bdSJonathan Cameron if (IS_ERR(thermo->custom))
7278ca555bdSJonathan Cameron return ERR_CAST(thermo->custom);
728f110f318SNuno Sá }
729f110f318SNuno Sá
730f110f318SNuno Sá /* set common parameters */
731f110f318SNuno Sá thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
732f110f318SNuno Sá thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
733f110f318SNuno Sá
734f110f318SNuno Sá return &thermo->sensor;
735f110f318SNuno Sá }
736f110f318SNuno Sá
737bc4c9499SAndy Shevchenko static struct ltc2983_sensor *
ltc2983_rtd_new(const struct fwnode_handle * child,struct ltc2983_data * st,const struct ltc2983_sensor * sensor)738bc4c9499SAndy Shevchenko ltc2983_rtd_new(const struct fwnode_handle *child, struct ltc2983_data *st,
739f110f318SNuno Sá const struct ltc2983_sensor *sensor)
740f110f318SNuno Sá {
741f110f318SNuno Sá struct ltc2983_rtd *rtd;
742f110f318SNuno Sá int ret = 0;
743f110f318SNuno Sá struct device *dev = &st->spi->dev;
744f110f318SNuno Sá u32 excitation_current = 0, n_wires = 0;
745f110f318SNuno Sá
746f110f318SNuno Sá rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
747f110f318SNuno Sá if (!rtd)
748f110f318SNuno Sá return ERR_PTR(-ENOMEM);
749f110f318SNuno Sá
7508ca555bdSJonathan Cameron struct fwnode_handle *ref __free(fwnode_handle) =
7518ca555bdSJonathan Cameron fwnode_find_reference(child, "adi,rsense-handle", 0);
752a00838caSNuno Sa if (IS_ERR(ref))
753a00838caSNuno Sa return dev_err_cast_probe(dev, ref,
754a00838caSNuno Sa "Property adi,rsense-handle missing or invalid\n");
755f110f318SNuno Sá
756bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(ref, "reg", &rtd->r_sense_chan);
757a00838caSNuno Sa if (ret)
758a00838caSNuno Sa return dev_err_ptr_probe(dev, ret,
759a00838caSNuno Sa "Property reg must be given\n");
760f110f318SNuno Sá
761bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(child, "adi,number-of-wires", &n_wires);
762f110f318SNuno Sá if (!ret) {
763f110f318SNuno Sá switch (n_wires) {
764f110f318SNuno Sá case 2:
765f110f318SNuno Sá rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
766f110f318SNuno Sá break;
767f110f318SNuno Sá case 3:
768f110f318SNuno Sá rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
769f110f318SNuno Sá break;
770f110f318SNuno Sá case 4:
771f110f318SNuno Sá rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
772f110f318SNuno Sá break;
773f110f318SNuno Sá case 5:
774f110f318SNuno Sá /* 4 wires, Kelvin Rsense */
775f110f318SNuno Sá rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
776f110f318SNuno Sá break;
777f110f318SNuno Sá default:
778a00838caSNuno Sa return dev_err_ptr_probe(dev, -EINVAL,
779a00838caSNuno Sa "Invalid number of wires:%u\n",
780a00838caSNuno Sa n_wires);
781f110f318SNuno Sá }
782f110f318SNuno Sá }
783f110f318SNuno Sá
784bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,rsense-share")) {
785f110f318SNuno Sá /* Current rotation is only available with rsense sharing */
786bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,current-rotate")) {
787a00838caSNuno Sa if (n_wires == 2 || n_wires == 3)
788a00838caSNuno Sa return dev_err_ptr_probe(dev, -EINVAL,
789a00838caSNuno Sa "Rotation not allowed for 2/3 Wire RTDs\n");
790a00838caSNuno Sa
791f110f318SNuno Sá rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
792f110f318SNuno Sá } else {
793f110f318SNuno Sá rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
794f110f318SNuno Sá }
795f110f318SNuno Sá }
796f110f318SNuno Sá /*
797f110f318SNuno Sá * rtd channel indexes are a bit more complicated to validate.
798f110f318SNuno Sá * For 4wire RTD with rotation, the channel selection cannot be
799f110f318SNuno Sá * >=19 since the chann + 1 is used in this configuration.
800f110f318SNuno Sá * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
801f110f318SNuno Sá * <=1 since chanel - 1 and channel - 2 are used.
802f110f318SNuno Sá */
803f110f318SNuno Sá if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
804f110f318SNuno Sá /* 4-wire */
805f110f318SNuno Sá u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
8066f7cadcfSCosmin Tanislav max = st->info->max_channels_nr;
807f110f318SNuno Sá
808f110f318SNuno Sá if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
8096f7cadcfSCosmin Tanislav max = st->info->max_channels_nr - 1;
810f110f318SNuno Sá
811f110f318SNuno Sá if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
812f110f318SNuno Sá == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
813a00838caSNuno Sa (rtd->r_sense_chan <= min))
814f110f318SNuno Sá /* kelvin rsense*/
815a00838caSNuno Sa return dev_err_ptr_probe(dev, -EINVAL,
816a00838caSNuno Sa "Invalid rsense chann:%d to use in kelvin rsense\n",
817f110f318SNuno Sá rtd->r_sense_chan);
818f110f318SNuno Sá
819a00838caSNuno Sa if (sensor->chan < min || sensor->chan > max)
820a00838caSNuno Sa return dev_err_ptr_probe(dev, -EINVAL,
821a00838caSNuno Sa "Invalid chann:%d for the rtd config\n",
822f110f318SNuno Sá sensor->chan);
823f110f318SNuno Sá } else {
824f110f318SNuno Sá /* same as differential case */
825a00838caSNuno Sa if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
826a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
827a00838caSNuno Sa "Invalid chann:%d for RTD\n",
828a00838caSNuno Sa sensor->chan);
829f110f318SNuno Sá }
830f110f318SNuno Sá
831f110f318SNuno Sá /* check custom sensor */
832f110f318SNuno Sá if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
833f110f318SNuno Sá rtd->custom = __ltc2983_custom_sensor_new(st, child,
834f110f318SNuno Sá "adi,custom-rtd",
835f110f318SNuno Sá false, 2048, false);
8368ca555bdSJonathan Cameron if (IS_ERR(rtd->custom))
8378ca555bdSJonathan Cameron return ERR_CAST(rtd->custom);
838f110f318SNuno Sá }
839f110f318SNuno Sá
840f110f318SNuno Sá /* set common parameters */
841f110f318SNuno Sá rtd->sensor.fault_handler = ltc2983_common_fault_handler;
842f110f318SNuno Sá rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
843f110f318SNuno Sá
844bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
845f110f318SNuno Sá &excitation_current);
846f110f318SNuno Sá if (ret) {
847f110f318SNuno Sá /* default to 5uA */
848f110f318SNuno Sá rtd->excitation_current = 1;
849f110f318SNuno Sá } else {
850f110f318SNuno Sá switch (excitation_current) {
851f110f318SNuno Sá case 5:
852f110f318SNuno Sá rtd->excitation_current = 0x01;
853f110f318SNuno Sá break;
854f110f318SNuno Sá case 10:
855f110f318SNuno Sá rtd->excitation_current = 0x02;
856f110f318SNuno Sá break;
857f110f318SNuno Sá case 25:
858f110f318SNuno Sá rtd->excitation_current = 0x03;
859f110f318SNuno Sá break;
860f110f318SNuno Sá case 50:
861f110f318SNuno Sá rtd->excitation_current = 0x04;
862f110f318SNuno Sá break;
863f110f318SNuno Sá case 100:
864f110f318SNuno Sá rtd->excitation_current = 0x05;
865f110f318SNuno Sá break;
866f110f318SNuno Sá case 250:
867f110f318SNuno Sá rtd->excitation_current = 0x06;
868f110f318SNuno Sá break;
869f110f318SNuno Sá case 500:
870f110f318SNuno Sá rtd->excitation_current = 0x07;
871f110f318SNuno Sá break;
872f110f318SNuno Sá case 1000:
873f110f318SNuno Sá rtd->excitation_current = 0x08;
874f110f318SNuno Sá break;
875f110f318SNuno Sá default:
876a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
877a00838caSNuno Sa "Invalid value for excitation current(%u)\n",
878f110f318SNuno Sá excitation_current);
879f110f318SNuno Sá }
880f110f318SNuno Sá }
881f110f318SNuno Sá
882bc4c9499SAndy Shevchenko fwnode_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
883f110f318SNuno Sá
884f110f318SNuno Sá return &rtd->sensor;
885f110f318SNuno Sá }
886f110f318SNuno Sá
887bc4c9499SAndy Shevchenko static struct ltc2983_sensor *
ltc2983_thermistor_new(const struct fwnode_handle * child,struct ltc2983_data * st,const struct ltc2983_sensor * sensor)888bc4c9499SAndy Shevchenko ltc2983_thermistor_new(const struct fwnode_handle *child, struct ltc2983_data *st,
889f110f318SNuno Sá const struct ltc2983_sensor *sensor)
890f110f318SNuno Sá {
891f110f318SNuno Sá struct ltc2983_thermistor *thermistor;
892f110f318SNuno Sá struct device *dev = &st->spi->dev;
893f110f318SNuno Sá u32 excitation_current = 0;
894f110f318SNuno Sá int ret = 0;
895f110f318SNuno Sá
896f110f318SNuno Sá thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
897f110f318SNuno Sá if (!thermistor)
898f110f318SNuno Sá return ERR_PTR(-ENOMEM);
899f110f318SNuno Sá
9008ca555bdSJonathan Cameron struct fwnode_handle *ref __free(fwnode_handle) =
9018ca555bdSJonathan Cameron fwnode_find_reference(child, "adi,rsense-handle", 0);
902a00838caSNuno Sa if (IS_ERR(ref))
903a00838caSNuno Sa return dev_err_cast_probe(dev, ref,
904a00838caSNuno Sa "Property adi,rsense-handle missing or invalid\n");
905f110f318SNuno Sá
906bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(ref, "reg", &thermistor->r_sense_chan);
907a00838caSNuno Sa if (ret)
908a00838caSNuno Sa return dev_err_ptr_probe(dev, ret,
909a00838caSNuno Sa "rsense channel must be configured...\n");
910f110f318SNuno Sá
911bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,single-ended")) {
912f110f318SNuno Sá thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
913bc4c9499SAndy Shevchenko } else if (fwnode_property_read_bool(child, "adi,rsense-share")) {
914f110f318SNuno Sá /* rotation is only possible if sharing rsense */
915bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,current-rotate"))
916f110f318SNuno Sá thermistor->sensor_config =
917f110f318SNuno Sá LTC2983_THERMISTOR_C_ROTATE(1);
918f110f318SNuno Sá else
919f110f318SNuno Sá thermistor->sensor_config =
920f110f318SNuno Sá LTC2983_THERMISTOR_R_SHARE(1);
921f110f318SNuno Sá }
922f110f318SNuno Sá /* validate channel index */
923f110f318SNuno Sá if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
924a00838caSNuno Sa sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
925a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
926a00838caSNuno Sa "Invalid chann:%d for differential thermistor\n",
927f110f318SNuno Sá sensor->chan);
928f110f318SNuno Sá
929f110f318SNuno Sá /* check custom sensor */
930f110f318SNuno Sá if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
931f110f318SNuno Sá bool steinhart = false;
932f110f318SNuno Sá const char *propname;
933f110f318SNuno Sá
934f110f318SNuno Sá if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
935f110f318SNuno Sá steinhart = true;
936f110f318SNuno Sá propname = "adi,custom-steinhart";
937f110f318SNuno Sá } else {
938f110f318SNuno Sá propname = "adi,custom-thermistor";
939f110f318SNuno Sá }
940f110f318SNuno Sá
941f110f318SNuno Sá thermistor->custom = __ltc2983_custom_sensor_new(st, child,
942f110f318SNuno Sá propname,
943f110f318SNuno Sá steinhart,
944f110f318SNuno Sá 64, false);
9458ca555bdSJonathan Cameron if (IS_ERR(thermistor->custom))
9468ca555bdSJonathan Cameron return ERR_CAST(thermistor->custom);
947f110f318SNuno Sá }
948f110f318SNuno Sá /* set common parameters */
949f110f318SNuno Sá thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
950f110f318SNuno Sá thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
951f110f318SNuno Sá
952bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(child, "adi,excitation-current-nanoamp",
953f110f318SNuno Sá &excitation_current);
954f110f318SNuno Sá if (ret) {
955f110f318SNuno Sá /* Auto range is not allowed for custom sensors */
956f110f318SNuno Sá if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
957f110f318SNuno Sá /* default to 1uA */
958f110f318SNuno Sá thermistor->excitation_current = 0x03;
959f110f318SNuno Sá else
960f110f318SNuno Sá /* default to auto-range */
961f110f318SNuno Sá thermistor->excitation_current = 0x0c;
962f110f318SNuno Sá } else {
963f110f318SNuno Sá switch (excitation_current) {
964f110f318SNuno Sá case 0:
965f110f318SNuno Sá /* auto range */
966a00838caSNuno Sa if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
967a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
968f110f318SNuno Sá "Auto Range not allowed for custom sensors\n");
969a00838caSNuno Sa
970f110f318SNuno Sá thermistor->excitation_current = 0x0c;
971f110f318SNuno Sá break;
972f110f318SNuno Sá case 250:
973f110f318SNuno Sá thermistor->excitation_current = 0x01;
974f110f318SNuno Sá break;
975f110f318SNuno Sá case 500:
976f110f318SNuno Sá thermistor->excitation_current = 0x02;
977f110f318SNuno Sá break;
978f110f318SNuno Sá case 1000:
979f110f318SNuno Sá thermistor->excitation_current = 0x03;
980f110f318SNuno Sá break;
981f110f318SNuno Sá case 5000:
982f110f318SNuno Sá thermistor->excitation_current = 0x04;
983f110f318SNuno Sá break;
984f110f318SNuno Sá case 10000:
985f110f318SNuno Sá thermistor->excitation_current = 0x05;
986f110f318SNuno Sá break;
987f110f318SNuno Sá case 25000:
988f110f318SNuno Sá thermistor->excitation_current = 0x06;
989f110f318SNuno Sá break;
990f110f318SNuno Sá case 50000:
991f110f318SNuno Sá thermistor->excitation_current = 0x07;
992f110f318SNuno Sá break;
993f110f318SNuno Sá case 100000:
994f110f318SNuno Sá thermistor->excitation_current = 0x08;
995f110f318SNuno Sá break;
996f110f318SNuno Sá case 250000:
997f110f318SNuno Sá thermistor->excitation_current = 0x09;
998f110f318SNuno Sá break;
999f110f318SNuno Sá case 500000:
1000f110f318SNuno Sá thermistor->excitation_current = 0x0a;
1001f110f318SNuno Sá break;
1002f110f318SNuno Sá case 1000000:
1003f110f318SNuno Sá thermistor->excitation_current = 0x0b;
1004f110f318SNuno Sá break;
1005f110f318SNuno Sá default:
1006a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1007a00838caSNuno Sa "Invalid value for excitation current(%u)\n",
1008f110f318SNuno Sá excitation_current);
1009f110f318SNuno Sá }
1010f110f318SNuno Sá }
1011f110f318SNuno Sá
1012f110f318SNuno Sá return &thermistor->sensor;
1013f110f318SNuno Sá }
1014f110f318SNuno Sá
1015bc4c9499SAndy Shevchenko static struct ltc2983_sensor *
ltc2983_diode_new(const struct fwnode_handle * child,const struct ltc2983_data * st,const struct ltc2983_sensor * sensor)1016bc4c9499SAndy Shevchenko ltc2983_diode_new(const struct fwnode_handle *child, const struct ltc2983_data *st,
1017f110f318SNuno Sá const struct ltc2983_sensor *sensor)
1018f110f318SNuno Sá {
1019f110f318SNuno Sá struct ltc2983_diode *diode;
1020f110f318SNuno Sá u32 temp = 0, excitation_current = 0;
1021f110f318SNuno Sá int ret;
1022f110f318SNuno Sá
1023f110f318SNuno Sá diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
1024f110f318SNuno Sá if (!diode)
1025f110f318SNuno Sá return ERR_PTR(-ENOMEM);
1026f110f318SNuno Sá
1027bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,single-ended"))
1028f110f318SNuno Sá diode->sensor_config = LTC2983_DIODE_SGL(1);
1029f110f318SNuno Sá
1030bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,three-conversion-cycles"))
1031f110f318SNuno Sá diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
1032f110f318SNuno Sá
1033bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,average-on"))
1034f110f318SNuno Sá diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
1035f110f318SNuno Sá
1036f110f318SNuno Sá /* validate channel index */
1037f110f318SNuno Sá if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
1038a00838caSNuno Sa sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
1039a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1040a00838caSNuno Sa "Invalid chann:%d for differential thermistor\n",
1041f110f318SNuno Sá sensor->chan);
1042a00838caSNuno Sa
1043f110f318SNuno Sá /* set common parameters */
1044f110f318SNuno Sá diode->sensor.fault_handler = ltc2983_common_fault_handler;
1045f110f318SNuno Sá diode->sensor.assign_chan = ltc2983_diode_assign_chan;
1046f110f318SNuno Sá
1047bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(child, "adi,excitation-current-microamp",
1048f110f318SNuno Sá &excitation_current);
1049f110f318SNuno Sá if (!ret) {
1050f110f318SNuno Sá switch (excitation_current) {
1051f110f318SNuno Sá case 10:
1052f110f318SNuno Sá diode->excitation_current = 0x00;
1053f110f318SNuno Sá break;
1054f110f318SNuno Sá case 20:
1055f110f318SNuno Sá diode->excitation_current = 0x01;
1056f110f318SNuno Sá break;
1057f110f318SNuno Sá case 40:
1058f110f318SNuno Sá diode->excitation_current = 0x02;
1059f110f318SNuno Sá break;
1060f110f318SNuno Sá case 80:
1061f110f318SNuno Sá diode->excitation_current = 0x03;
1062f110f318SNuno Sá break;
1063f110f318SNuno Sá default:
1064a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1065a00838caSNuno Sa "Invalid value for excitation current(%u)\n",
1066f110f318SNuno Sá excitation_current);
1067f110f318SNuno Sá }
1068f110f318SNuno Sá }
1069f110f318SNuno Sá
1070bc4c9499SAndy Shevchenko fwnode_property_read_u32(child, "adi,ideal-factor-value", &temp);
1071f110f318SNuno Sá
1072f110f318SNuno Sá /* 2^20 resolution */
1073f110f318SNuno Sá diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
1074f110f318SNuno Sá
1075f110f318SNuno Sá return &diode->sensor;
1076f110f318SNuno Sá }
1077f110f318SNuno Sá
ltc2983_r_sense_new(struct fwnode_handle * child,struct ltc2983_data * st,const struct ltc2983_sensor * sensor)1078bc4c9499SAndy Shevchenko static struct ltc2983_sensor *ltc2983_r_sense_new(struct fwnode_handle *child,
1079f110f318SNuno Sá struct ltc2983_data *st,
1080f110f318SNuno Sá const struct ltc2983_sensor *sensor)
1081f110f318SNuno Sá {
1082f110f318SNuno Sá struct ltc2983_rsense *rsense;
1083f110f318SNuno Sá int ret;
1084f110f318SNuno Sá u32 temp;
1085f110f318SNuno Sá
1086f110f318SNuno Sá rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
1087f110f318SNuno Sá if (!rsense)
1088f110f318SNuno Sá return ERR_PTR(-ENOMEM);
1089f110f318SNuno Sá
1090f110f318SNuno Sá /* validate channel index */
1091a00838caSNuno Sa if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
1092a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1093a00838caSNuno Sa "Invalid chann:%d for r_sense\n",
1094f110f318SNuno Sá sensor->chan);
1095f110f318SNuno Sá
1096bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
1097a00838caSNuno Sa if (ret)
1098a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1099a00838caSNuno Sa "Property adi,rsense-val-milli-ohms missing\n");
1100f110f318SNuno Sá /*
1101f110f318SNuno Sá * Times 1000 because we have milli-ohms and __convert_to_raw
1102f110f318SNuno Sá * expects scales of 1000000 which are used for all other
1103f110f318SNuno Sá * properties.
1104f110f318SNuno Sá * 2^10 resolution
1105f110f318SNuno Sá */
1106f110f318SNuno Sá rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
1107f110f318SNuno Sá
1108f110f318SNuno Sá /* set common parameters */
1109f110f318SNuno Sá rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
1110f110f318SNuno Sá
1111f110f318SNuno Sá return &rsense->sensor;
1112f110f318SNuno Sá }
1113f110f318SNuno Sá
ltc2983_adc_new(struct fwnode_handle * child,struct ltc2983_data * st,const struct ltc2983_sensor * sensor)1114bc4c9499SAndy Shevchenko static struct ltc2983_sensor *ltc2983_adc_new(struct fwnode_handle *child,
1115f110f318SNuno Sá struct ltc2983_data *st,
1116f110f318SNuno Sá const struct ltc2983_sensor *sensor)
1117f110f318SNuno Sá {
1118f110f318SNuno Sá struct ltc2983_adc *adc;
1119f110f318SNuno Sá
1120f110f318SNuno Sá adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
1121f110f318SNuno Sá if (!adc)
1122f110f318SNuno Sá return ERR_PTR(-ENOMEM);
1123f110f318SNuno Sá
1124bc4c9499SAndy Shevchenko if (fwnode_property_read_bool(child, "adi,single-ended"))
1125f110f318SNuno Sá adc->single_ended = true;
1126f110f318SNuno Sá
1127a00838caSNuno Sa if (!adc->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
1128a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1129a00838caSNuno Sa "Invalid chan:%d for differential adc\n",
1130f110f318SNuno Sá sensor->chan);
1131a00838caSNuno Sa
1132f110f318SNuno Sá /* set common parameters */
1133f110f318SNuno Sá adc->sensor.assign_chan = ltc2983_adc_assign_chan;
1134f110f318SNuno Sá adc->sensor.fault_handler = ltc2983_common_fault_handler;
1135f110f318SNuno Sá
1136f110f318SNuno Sá return &adc->sensor;
1137f110f318SNuno Sá }
1138f110f318SNuno Sá
ltc2983_temp_new(struct fwnode_handle * child,struct ltc2983_data * st,const struct ltc2983_sensor * sensor)11396f7cadcfSCosmin Tanislav static struct ltc2983_sensor *ltc2983_temp_new(struct fwnode_handle *child,
11406f7cadcfSCosmin Tanislav struct ltc2983_data *st,
11416f7cadcfSCosmin Tanislav const struct ltc2983_sensor *sensor)
11426f7cadcfSCosmin Tanislav {
11436f7cadcfSCosmin Tanislav struct ltc2983_temp *temp;
11446f7cadcfSCosmin Tanislav
11456f7cadcfSCosmin Tanislav temp = devm_kzalloc(&st->spi->dev, sizeof(*temp), GFP_KERNEL);
11466f7cadcfSCosmin Tanislav if (!temp)
11476f7cadcfSCosmin Tanislav return ERR_PTR(-ENOMEM);
11486f7cadcfSCosmin Tanislav
11496f7cadcfSCosmin Tanislav if (fwnode_property_read_bool(child, "adi,single-ended"))
11506f7cadcfSCosmin Tanislav temp->single_ended = true;
11516f7cadcfSCosmin Tanislav
1152a00838caSNuno Sa if (!temp->single_ended && sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN)
1153a00838caSNuno Sa return dev_err_ptr_probe(&st->spi->dev, -EINVAL,
1154a00838caSNuno Sa "Invalid chan:%d for differential temp\n",
11556f7cadcfSCosmin Tanislav sensor->chan);
11566f7cadcfSCosmin Tanislav
11576f7cadcfSCosmin Tanislav temp->custom = __ltc2983_custom_sensor_new(st, child, "adi,custom-temp",
11586f7cadcfSCosmin Tanislav false, 4096, true);
11596f7cadcfSCosmin Tanislav if (IS_ERR(temp->custom))
11606f7cadcfSCosmin Tanislav return ERR_CAST(temp->custom);
11616f7cadcfSCosmin Tanislav
11626f7cadcfSCosmin Tanislav /* set common parameters */
11636f7cadcfSCosmin Tanislav temp->sensor.assign_chan = ltc2983_temp_assign_chan;
11646f7cadcfSCosmin Tanislav temp->sensor.fault_handler = ltc2983_common_fault_handler;
11656f7cadcfSCosmin Tanislav
11666f7cadcfSCosmin Tanislav return &temp->sensor;
11676f7cadcfSCosmin Tanislav }
11686f7cadcfSCosmin Tanislav
ltc2983_chan_read(struct ltc2983_data * st,const struct ltc2983_sensor * sensor,int * val)1169f110f318SNuno Sá static int ltc2983_chan_read(struct ltc2983_data *st,
1170f110f318SNuno Sá const struct ltc2983_sensor *sensor, int *val)
1171f110f318SNuno Sá {
1172f110f318SNuno Sá u32 start_conversion = 0;
1173f110f318SNuno Sá int ret;
1174f110f318SNuno Sá unsigned long time;
1175f110f318SNuno Sá
1176f110f318SNuno Sá start_conversion = LTC2983_STATUS_START(true);
1177f110f318SNuno Sá start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
1178f110f318SNuno Sá dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
1179f110f318SNuno Sá sensor->chan, start_conversion);
1180f110f318SNuno Sá /* start conversion */
1181f110f318SNuno Sá ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
1182f110f318SNuno Sá if (ret)
1183f110f318SNuno Sá return ret;
1184f110f318SNuno Sá
1185f110f318SNuno Sá reinit_completion(&st->completion);
1186f110f318SNuno Sá /*
1187f110f318SNuno Sá * wait for conversion to complete.
1188f110f318SNuno Sá * 300 ms should be more than enough to complete the conversion.
1189f110f318SNuno Sá * Depending on the sensor configuration, there are 2/3 conversions
1190f110f318SNuno Sá * cycles of 82ms.
1191f110f318SNuno Sá */
1192f110f318SNuno Sá time = wait_for_completion_timeout(&st->completion,
1193f110f318SNuno Sá msecs_to_jiffies(300));
1194f110f318SNuno Sá if (!time) {
1195f110f318SNuno Sá dev_warn(&st->spi->dev, "Conversion timed out\n");
1196f110f318SNuno Sá return -ETIMEDOUT;
1197f110f318SNuno Sá }
1198f110f318SNuno Sá
1199f110f318SNuno Sá /* read the converted data */
1200f110f318SNuno Sá ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
1201f110f318SNuno Sá &st->temp, sizeof(st->temp));
1202f110f318SNuno Sá if (ret)
1203f110f318SNuno Sá return ret;
1204f110f318SNuno Sá
1205f110f318SNuno Sá *val = __be32_to_cpu(st->temp);
1206f110f318SNuno Sá
1207f110f318SNuno Sá if (!(LTC2983_RES_VALID_MASK & *val)) {
1208f110f318SNuno Sá dev_err(&st->spi->dev, "Invalid conversion detected\n");
1209f110f318SNuno Sá return -EIO;
1210f110f318SNuno Sá }
1211f110f318SNuno Sá
1212f110f318SNuno Sá ret = sensor->fault_handler(st, *val);
1213f110f318SNuno Sá if (ret)
1214f110f318SNuno Sá return ret;
1215f110f318SNuno Sá
1216f110f318SNuno Sá *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
1217f110f318SNuno Sá return 0;
1218f110f318SNuno Sá }
1219f110f318SNuno Sá
ltc2983_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)1220f110f318SNuno Sá static int ltc2983_read_raw(struct iio_dev *indio_dev,
1221f110f318SNuno Sá struct iio_chan_spec const *chan,
1222f110f318SNuno Sá int *val, int *val2, long mask)
1223f110f318SNuno Sá {
1224f110f318SNuno Sá struct ltc2983_data *st = iio_priv(indio_dev);
1225f110f318SNuno Sá int ret;
1226f110f318SNuno Sá
1227f110f318SNuno Sá /* sanity check */
1228f110f318SNuno Sá if (chan->address >= st->num_channels) {
1229f110f318SNuno Sá dev_err(&st->spi->dev, "Invalid chan address:%ld",
1230f110f318SNuno Sá chan->address);
1231f110f318SNuno Sá return -EINVAL;
1232f110f318SNuno Sá }
1233f110f318SNuno Sá
1234f110f318SNuno Sá switch (mask) {
1235f110f318SNuno Sá case IIO_CHAN_INFO_RAW:
1236f110f318SNuno Sá mutex_lock(&st->lock);
1237f110f318SNuno Sá ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
1238f110f318SNuno Sá mutex_unlock(&st->lock);
1239f110f318SNuno Sá return ret ?: IIO_VAL_INT;
1240f110f318SNuno Sá case IIO_CHAN_INFO_SCALE:
1241f110f318SNuno Sá switch (chan->type) {
1242f110f318SNuno Sá case IIO_TEMP:
1243f110f318SNuno Sá /* value in milli degrees */
1244f110f318SNuno Sá *val = 1000;
1245f110f318SNuno Sá /* 2^10 */
1246f110f318SNuno Sá *val2 = 1024;
1247f110f318SNuno Sá return IIO_VAL_FRACTIONAL;
1248f110f318SNuno Sá case IIO_VOLTAGE:
1249f110f318SNuno Sá /* value in millivolt */
1250f110f318SNuno Sá *val = 1000;
1251f110f318SNuno Sá /* 2^21 */
1252f110f318SNuno Sá *val2 = 2097152;
1253f110f318SNuno Sá return IIO_VAL_FRACTIONAL;
1254f110f318SNuno Sá default:
1255f110f318SNuno Sá return -EINVAL;
1256f110f318SNuno Sá }
1257f110f318SNuno Sá }
1258f110f318SNuno Sá
1259f110f318SNuno Sá return -EINVAL;
1260f110f318SNuno Sá }
1261f110f318SNuno Sá
ltc2983_reg_access(struct iio_dev * indio_dev,unsigned int reg,unsigned int writeval,unsigned int * readval)1262f110f318SNuno Sá static int ltc2983_reg_access(struct iio_dev *indio_dev,
1263f110f318SNuno Sá unsigned int reg,
1264f110f318SNuno Sá unsigned int writeval,
1265f110f318SNuno Sá unsigned int *readval)
1266f110f318SNuno Sá {
1267f110f318SNuno Sá struct ltc2983_data *st = iio_priv(indio_dev);
1268f110f318SNuno Sá
1269f110f318SNuno Sá if (readval)
1270f110f318SNuno Sá return regmap_read(st->regmap, reg, readval);
1271a00838caSNuno Sa
1272f110f318SNuno Sá return regmap_write(st->regmap, reg, writeval);
1273f110f318SNuno Sá }
1274f110f318SNuno Sá
ltc2983_irq_handler(int irq,void * data)1275f110f318SNuno Sá static irqreturn_t ltc2983_irq_handler(int irq, void *data)
1276f110f318SNuno Sá {
1277f110f318SNuno Sá struct ltc2983_data *st = data;
1278f110f318SNuno Sá
1279f110f318SNuno Sá complete(&st->completion);
1280f110f318SNuno Sá return IRQ_HANDLED;
1281f110f318SNuno Sá }
1282f110f318SNuno Sá
1283f110f318SNuno Sá #define LTC2983_CHAN(__type, index, __address) ({ \
1284f110f318SNuno Sá struct iio_chan_spec __chan = { \
1285f110f318SNuno Sá .type = __type, \
1286f110f318SNuno Sá .indexed = 1, \
1287f110f318SNuno Sá .channel = index, \
1288f110f318SNuno Sá .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
1289f110f318SNuno Sá .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
1290f110f318SNuno Sá .address = __address, \
1291f110f318SNuno Sá }; \
1292f110f318SNuno Sá __chan; \
1293f110f318SNuno Sá })
1294f110f318SNuno Sá
ltc2983_parse_fw(struct ltc2983_data * st)1295dccdff35SNuno Sa static int ltc2983_parse_fw(struct ltc2983_data *st)
1296f110f318SNuno Sá {
1297f110f318SNuno Sá struct device *dev = &st->spi->dev;
12988ca555bdSJonathan Cameron int ret, chan = 0, channel_avail_mask = 0;
1299f110f318SNuno Sá
1300bc4c9499SAndy Shevchenko device_property_read_u32(dev, "adi,mux-delay-config-us", &st->mux_delay_config);
1301f110f318SNuno Sá
1302bc4c9499SAndy Shevchenko device_property_read_u32(dev, "adi,filter-notch-freq", &st->filter_notch_freq);
1303f110f318SNuno Sá
1304bc4c9499SAndy Shevchenko st->num_channels = device_get_child_node_count(dev);
1305a00838caSNuno Sa if (!st->num_channels)
1306a00838caSNuno Sa return dev_err_probe(&st->spi->dev, -EINVAL,
1307a00838caSNuno Sa "At least one channel must be given!\n");
130825d4abbfSNuno Sá
1309f110f318SNuno Sá st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
1310f110f318SNuno Sá GFP_KERNEL);
1311f110f318SNuno Sá if (!st->sensors)
1312f110f318SNuno Sá return -ENOMEM;
1313f110f318SNuno Sá
1314f110f318SNuno Sá st->iio_channels = st->num_channels;
13158ca555bdSJonathan Cameron device_for_each_child_node_scoped(dev, child) {
1316f110f318SNuno Sá struct ltc2983_sensor sensor;
1317f110f318SNuno Sá
1318bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(child, "reg", &sensor.chan);
13198ca555bdSJonathan Cameron if (ret)
13208ca555bdSJonathan Cameron return dev_err_probe(dev, ret,
13218ca555bdSJonathan Cameron "reg property must given for child nodes\n");
1322f110f318SNuno Sá
1323f110f318SNuno Sá /* check if we have a valid channel */
1324f110f318SNuno Sá if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
13258ca555bdSJonathan Cameron sensor.chan > st->info->max_channels_nr)
13268ca555bdSJonathan Cameron return dev_err_probe(dev, -EINVAL,
13278ca555bdSJonathan Cameron "chan:%d must be from %u to %u\n",
13288ca555bdSJonathan Cameron sensor.chan,
13298ca555bdSJonathan Cameron LTC2983_MIN_CHANNELS_NR,
13308ca555bdSJonathan Cameron st->info->max_channels_nr);
13318ca555bdSJonathan Cameron
13328ca555bdSJonathan Cameron if (channel_avail_mask & BIT(sensor.chan))
13338ca555bdSJonathan Cameron return dev_err_probe(dev, -EINVAL,
13348ca555bdSJonathan Cameron "chan:%d already in use\n",
13358ca555bdSJonathan Cameron sensor.chan);
1336f110f318SNuno Sá
1337bc4c9499SAndy Shevchenko ret = fwnode_property_read_u32(child, "adi,sensor-type", &sensor.type);
13388ca555bdSJonathan Cameron if (ret)
13398ca555bdSJonathan Cameron return dev_err_probe(dev, ret,
1340f110f318SNuno Sá "adi,sensor-type property must given for child nodes\n");
1341f110f318SNuno Sá
1342f110f318SNuno Sá dev_dbg(dev, "Create new sensor, type %u, chann %u",
13438ca555bdSJonathan Cameron sensor.type, sensor.chan);
1344f110f318SNuno Sá
1345f110f318SNuno Sá if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
1346f110f318SNuno Sá sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
1347f110f318SNuno Sá st->sensors[chan] = ltc2983_thermocouple_new(child, st,
1348f110f318SNuno Sá &sensor);
1349f110f318SNuno Sá } else if (sensor.type >= LTC2983_SENSOR_RTD &&
1350f110f318SNuno Sá sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
1351f110f318SNuno Sá st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
1352f110f318SNuno Sá } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
1353f110f318SNuno Sá sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
1354f110f318SNuno Sá st->sensors[chan] = ltc2983_thermistor_new(child, st,
1355f110f318SNuno Sá &sensor);
1356f110f318SNuno Sá } else if (sensor.type == LTC2983_SENSOR_DIODE) {
1357f110f318SNuno Sá st->sensors[chan] = ltc2983_diode_new(child, st,
1358f110f318SNuno Sá &sensor);
1359f110f318SNuno Sá } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
1360f110f318SNuno Sá st->sensors[chan] = ltc2983_r_sense_new(child, st,
1361f110f318SNuno Sá &sensor);
1362f110f318SNuno Sá /* don't add rsense to iio */
1363f110f318SNuno Sá st->iio_channels--;
1364f110f318SNuno Sá } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
1365f110f318SNuno Sá st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
13666f7cadcfSCosmin Tanislav } else if (st->info->has_temp &&
13676f7cadcfSCosmin Tanislav sensor.type == LTC2983_SENSOR_ACTIVE_TEMP) {
13686f7cadcfSCosmin Tanislav st->sensors[chan] = ltc2983_temp_new(child, st, &sensor);
1369f110f318SNuno Sá } else {
13708ca555bdSJonathan Cameron return dev_err_probe(dev, -EINVAL,
13718ca555bdSJonathan Cameron "Unknown sensor type %d\n",
13728ca555bdSJonathan Cameron sensor.type);
1373f110f318SNuno Sá }
1374f110f318SNuno Sá
13758ca555bdSJonathan Cameron if (IS_ERR(st->sensors[chan]))
13768ca555bdSJonathan Cameron return dev_err_probe(dev, PTR_ERR(st->sensors[chan]),
13778ca555bdSJonathan Cameron "Failed to create sensor\n");
13788ca555bdSJonathan Cameron
1379f110f318SNuno Sá /* set generic sensor parameters */
1380f110f318SNuno Sá st->sensors[chan]->chan = sensor.chan;
1381f110f318SNuno Sá st->sensors[chan]->type = sensor.type;
1382f110f318SNuno Sá
1383f110f318SNuno Sá channel_avail_mask |= BIT(sensor.chan);
1384f110f318SNuno Sá chan++;
1385f110f318SNuno Sá }
1386f110f318SNuno Sá
1387f110f318SNuno Sá return 0;
1388f110f318SNuno Sá }
1389f110f318SNuno Sá
ltc2983_eeprom_cmd(struct ltc2983_data * st,unsigned int cmd,unsigned int wait_time,unsigned int status_reg,unsigned long status_fail_mask)13906f7cadcfSCosmin Tanislav static int ltc2983_eeprom_cmd(struct ltc2983_data *st, unsigned int cmd,
13916f7cadcfSCosmin Tanislav unsigned int wait_time, unsigned int status_reg,
13926f7cadcfSCosmin Tanislav unsigned long status_fail_mask)
13936f7cadcfSCosmin Tanislav {
13946f7cadcfSCosmin Tanislav unsigned long time;
13956f7cadcfSCosmin Tanislav unsigned int val;
13966f7cadcfSCosmin Tanislav int ret;
13976f7cadcfSCosmin Tanislav
13986f7cadcfSCosmin Tanislav ret = regmap_bulk_write(st->regmap, LTC2983_EEPROM_KEY_REG,
13996f7cadcfSCosmin Tanislav &st->eeprom_key, sizeof(st->eeprom_key));
14006f7cadcfSCosmin Tanislav if (ret)
14016f7cadcfSCosmin Tanislav return ret;
14026f7cadcfSCosmin Tanislav
14036f7cadcfSCosmin Tanislav reinit_completion(&st->completion);
14046f7cadcfSCosmin Tanislav
14056f7cadcfSCosmin Tanislav ret = regmap_write(st->regmap, LTC2983_STATUS_REG,
14066f7cadcfSCosmin Tanislav LTC2983_STATUS_START(true) | cmd);
14076f7cadcfSCosmin Tanislav if (ret)
14086f7cadcfSCosmin Tanislav return ret;
14096f7cadcfSCosmin Tanislav
14106f7cadcfSCosmin Tanislav time = wait_for_completion_timeout(&st->completion,
14116f7cadcfSCosmin Tanislav msecs_to_jiffies(wait_time));
1412a00838caSNuno Sa if (!time)
1413a00838caSNuno Sa return dev_err_probe(&st->spi->dev, -ETIMEDOUT,
1414a00838caSNuno Sa "EEPROM command timed out\n");
14156f7cadcfSCosmin Tanislav
14166f7cadcfSCosmin Tanislav ret = regmap_read(st->regmap, status_reg, &val);
14176f7cadcfSCosmin Tanislav if (ret)
14186f7cadcfSCosmin Tanislav return ret;
14196f7cadcfSCosmin Tanislav
1420a00838caSNuno Sa if (val & status_fail_mask)
1421a00838caSNuno Sa return dev_err_probe(&st->spi->dev, -EINVAL,
1422a00838caSNuno Sa "EEPROM command failed: 0x%02X\n", val);
14236f7cadcfSCosmin Tanislav
14246f7cadcfSCosmin Tanislav return 0;
14256f7cadcfSCosmin Tanislav }
14266f7cadcfSCosmin Tanislav
ltc2983_setup(struct ltc2983_data * st,bool assign_iio)1427f110f318SNuno Sá static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
1428f110f318SNuno Sá {
1429b76d26d6SNuno Sá u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
1430f110f318SNuno Sá int ret;
1431f110f318SNuno Sá
1432b76d26d6SNuno Sá /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
1433b76d26d6SNuno Sá ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
1434b76d26d6SNuno Sá LTC2983_STATUS_UP(status) == 1, 25000,
1435b76d26d6SNuno Sá 25000 * 10);
1436a00838caSNuno Sa if (ret)
1437a00838caSNuno Sa return dev_err_probe(&st->spi->dev, ret,
1438a00838caSNuno Sa "Device startup timed out\n");
1439f110f318SNuno Sá
1440f110f318SNuno Sá ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
1441f110f318SNuno Sá LTC2983_NOTCH_FREQ_MASK,
1442f110f318SNuno Sá LTC2983_NOTCH_FREQ(st->filter_notch_freq));
1443f110f318SNuno Sá if (ret)
1444f110f318SNuno Sá return ret;
1445f110f318SNuno Sá
1446f110f318SNuno Sá ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
1447f110f318SNuno Sá st->mux_delay_config);
1448f110f318SNuno Sá if (ret)
1449f110f318SNuno Sá return ret;
1450f110f318SNuno Sá
14516f7cadcfSCosmin Tanislav if (st->info->has_eeprom && !assign_iio) {
14526f7cadcfSCosmin Tanislav ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_READ_CMD,
14536f7cadcfSCosmin Tanislav LTC2983_EEPROM_READ_TIME_MS,
14546f7cadcfSCosmin Tanislav LTC2983_EEPROM_READ_STATUS_REG,
14556f7cadcfSCosmin Tanislav LTC2983_EEPROM_READ_FAILURE_MASK);
14566f7cadcfSCosmin Tanislav if (!ret)
14576f7cadcfSCosmin Tanislav return 0;
14586f7cadcfSCosmin Tanislav }
14596f7cadcfSCosmin Tanislav
1460f110f318SNuno Sá for (chan = 0; chan < st->num_channels; chan++) {
1461f110f318SNuno Sá u32 chan_type = 0, *iio_chan;
1462f110f318SNuno Sá
1463f110f318SNuno Sá ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
1464f110f318SNuno Sá if (ret)
1465f110f318SNuno Sá return ret;
1466f110f318SNuno Sá /*
1467f110f318SNuno Sá * The assign_iio flag is necessary for when the device is
1468f110f318SNuno Sá * coming out of sleep. In that case, we just need to
1469f110f318SNuno Sá * re-configure the device channels.
1470f110f318SNuno Sá * We also don't assign iio channels for rsense.
1471f110f318SNuno Sá */
1472f110f318SNuno Sá if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
1473f110f318SNuno Sá !assign_iio)
1474f110f318SNuno Sá continue;
1475f110f318SNuno Sá
1476f110f318SNuno Sá /* assign iio channel */
1477f110f318SNuno Sá if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
1478f110f318SNuno Sá chan_type = IIO_TEMP;
1479f110f318SNuno Sá iio_chan = &iio_chan_t;
1480f110f318SNuno Sá } else {
1481f110f318SNuno Sá chan_type = IIO_VOLTAGE;
1482f110f318SNuno Sá iio_chan = &iio_chan_v;
1483f110f318SNuno Sá }
1484f110f318SNuno Sá
1485f110f318SNuno Sá /*
1486f110f318SNuno Sá * add chan as the iio .address so that, we can directly
1487f110f318SNuno Sá * reference the sensor given the iio_chan_spec
1488f110f318SNuno Sá */
1489f110f318SNuno Sá st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
1490f110f318SNuno Sá chan);
1491f110f318SNuno Sá }
1492f110f318SNuno Sá
1493f110f318SNuno Sá return 0;
1494f110f318SNuno Sá }
1495f110f318SNuno Sá
1496f110f318SNuno Sá static const struct regmap_range ltc2983_reg_ranges[] = {
1497f110f318SNuno Sá regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
1498f110f318SNuno Sá regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
14996f7cadcfSCosmin Tanislav regmap_reg_range(LTC2983_EEPROM_KEY_REG, LTC2983_EEPROM_KEY_REG),
15006f7cadcfSCosmin Tanislav regmap_reg_range(LTC2983_EEPROM_READ_STATUS_REG,
15016f7cadcfSCosmin Tanislav LTC2983_EEPROM_READ_STATUS_REG),
1502f110f318SNuno Sá regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
1503f110f318SNuno Sá regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
1504f110f318SNuno Sá LTC2983_MULT_CHANNEL_END_REG),
15056f7cadcfSCosmin Tanislav regmap_reg_range(LTC2986_EEPROM_STATUS_REG, LTC2986_EEPROM_STATUS_REG),
1506f110f318SNuno Sá regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
1507f110f318SNuno Sá regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
1508f110f318SNuno Sá LTC2983_CHAN_ASSIGN_END_REG),
1509f110f318SNuno Sá regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
1510f110f318SNuno Sá LTC2983_CUST_SENS_TBL_END_REG),
1511f110f318SNuno Sá };
1512f110f318SNuno Sá
1513f110f318SNuno Sá static const struct regmap_access_table ltc2983_reg_table = {
1514f110f318SNuno Sá .yes_ranges = ltc2983_reg_ranges,
1515f110f318SNuno Sá .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
1516f110f318SNuno Sá };
1517f110f318SNuno Sá
1518f110f318SNuno Sá /*
1519f110f318SNuno Sá * The reg_bits are actually 12 but the device needs the first *complete*
1520f110f318SNuno Sá * byte for the command (R/W).
1521f110f318SNuno Sá */
1522f110f318SNuno Sá static const struct regmap_config ltc2983_regmap_config = {
1523f110f318SNuno Sá .reg_bits = 24,
1524f110f318SNuno Sá .val_bits = 8,
1525f110f318SNuno Sá .wr_table = <c2983_reg_table,
1526f110f318SNuno Sá .rd_table = <c2983_reg_table,
1527f110f318SNuno Sá .read_flag_mask = GENMASK(1, 0),
1528f110f318SNuno Sá .write_flag_mask = BIT(1),
1529f110f318SNuno Sá };
1530f110f318SNuno Sá
1531f110f318SNuno Sá static const struct iio_info ltc2983_iio_info = {
1532f110f318SNuno Sá .read_raw = ltc2983_read_raw,
1533f110f318SNuno Sá .debugfs_reg_access = ltc2983_reg_access,
1534f110f318SNuno Sá };
1535f110f318SNuno Sá
ltc2983_probe(struct spi_device * spi)1536f110f318SNuno Sá static int ltc2983_probe(struct spi_device *spi)
1537f110f318SNuno Sá {
1538f110f318SNuno Sá struct ltc2983_data *st;
1539f110f318SNuno Sá struct iio_dev *indio_dev;
1540919726c9SNuno Sá struct gpio_desc *gpio;
1541f110f318SNuno Sá int ret;
1542f110f318SNuno Sá
1543f110f318SNuno Sá indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
1544f110f318SNuno Sá if (!indio_dev)
1545f110f318SNuno Sá return -ENOMEM;
1546f110f318SNuno Sá
1547f110f318SNuno Sá st = iio_priv(indio_dev);
1548f110f318SNuno Sá
15493bdb96c9SNuno Sa st->info = spi_get_device_match_data(spi);
15506f7cadcfSCosmin Tanislav if (!st->info)
15516f7cadcfSCosmin Tanislav return -ENODEV;
15526f7cadcfSCosmin Tanislav
1553f110f318SNuno Sá st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config);
1554a00838caSNuno Sa if (IS_ERR(st->regmap))
1555a00838caSNuno Sa return dev_err_probe(&spi->dev, PTR_ERR(st->regmap),
1556a00838caSNuno Sa "Failed to initialize regmap\n");
1557f110f318SNuno Sá
1558f110f318SNuno Sá mutex_init(&st->lock);
1559f110f318SNuno Sá init_completion(&st->completion);
1560f110f318SNuno Sá st->spi = spi;
15616f7cadcfSCosmin Tanislav st->eeprom_key = cpu_to_be32(LTC2983_EEPROM_KEY);
1562f110f318SNuno Sá spi_set_drvdata(spi, st);
1563f110f318SNuno Sá
1564dccdff35SNuno Sa ret = ltc2983_parse_fw(st);
1565f110f318SNuno Sá if (ret)
1566f110f318SNuno Sá return ret;
1567b76d26d6SNuno Sá
156847ef0501SNuno Sa ret = devm_regulator_get_enable(&spi->dev, "vdd");
156947ef0501SNuno Sa if (ret)
157047ef0501SNuno Sa return ret;
157147ef0501SNuno Sa
1572919726c9SNuno Sá gpio = devm_gpiod_get_optional(&st->spi->dev, "reset", GPIOD_OUT_HIGH);
1573919726c9SNuno Sá if (IS_ERR(gpio))
1574919726c9SNuno Sá return PTR_ERR(gpio);
1575919726c9SNuno Sá
1576919726c9SNuno Sá if (gpio) {
1577919726c9SNuno Sá /* bring the device out of reset */
1578919726c9SNuno Sá usleep_range(1000, 1200);
1579919726c9SNuno Sá gpiod_set_value_cansleep(gpio, 0);
1580919726c9SNuno Sá }
1581919726c9SNuno Sá
15824132f191SCosmin Tanislav st->iio_chan = devm_kzalloc(&spi->dev,
15834132f191SCosmin Tanislav st->iio_channels * sizeof(*st->iio_chan),
15844132f191SCosmin Tanislav GFP_KERNEL);
15854132f191SCosmin Tanislav if (!st->iio_chan)
15864132f191SCosmin Tanislav return -ENOMEM;
15874132f191SCosmin Tanislav
1588b76d26d6SNuno Sá ret = ltc2983_setup(st, true);
1589b76d26d6SNuno Sá if (ret)
1590b76d26d6SNuno Sá return ret;
1591b76d26d6SNuno Sá
1592f110f318SNuno Sá ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
15935cad30abSNuno Sa IRQF_TRIGGER_RISING, st->info->name, st);
1594a00838caSNuno Sa if (ret)
1595a00838caSNuno Sa return dev_err_probe(&spi->dev, ret,
1596a00838caSNuno Sa "failed to request an irq\n");
1597f110f318SNuno Sá
15986f7cadcfSCosmin Tanislav if (st->info->has_eeprom) {
15996f7cadcfSCosmin Tanislav ret = ltc2983_eeprom_cmd(st, LTC2983_EEPROM_WRITE_CMD,
16006f7cadcfSCosmin Tanislav LTC2983_EEPROM_WRITE_TIME_MS,
16016f7cadcfSCosmin Tanislav LTC2986_EEPROM_STATUS_REG,
16026f7cadcfSCosmin Tanislav LTC2983_EEPROM_STATUS_FAILURE_MASK);
16036f7cadcfSCosmin Tanislav if (ret)
16046f7cadcfSCosmin Tanislav return ret;
16056f7cadcfSCosmin Tanislav }
16066f7cadcfSCosmin Tanislav
16075cad30abSNuno Sa indio_dev->name = st->info->name;
1608f110f318SNuno Sá indio_dev->num_channels = st->iio_channels;
1609f110f318SNuno Sá indio_dev->channels = st->iio_chan;
1610f110f318SNuno Sá indio_dev->modes = INDIO_DIRECT_MODE;
1611f110f318SNuno Sá indio_dev->info = <c2983_iio_info;
1612f110f318SNuno Sá
1613f110f318SNuno Sá return devm_iio_device_register(&spi->dev, indio_dev);
1614f110f318SNuno Sá }
1615f110f318SNuno Sá
ltc2983_resume(struct device * dev)1616fb4e8e2dSJonathan Cameron static int ltc2983_resume(struct device *dev)
1617f110f318SNuno Sá {
1618f110f318SNuno Sá struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1619f110f318SNuno Sá int dummy;
1620f110f318SNuno Sá
1621f110f318SNuno Sá /* dummy read to bring the device out of sleep */
1622f110f318SNuno Sá regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
1623f110f318SNuno Sá /* we need to re-assign the channels */
1624f110f318SNuno Sá return ltc2983_setup(st, false);
1625f110f318SNuno Sá }
1626f110f318SNuno Sá
ltc2983_suspend(struct device * dev)1627fb4e8e2dSJonathan Cameron static int ltc2983_suspend(struct device *dev)
1628f110f318SNuno Sá {
1629f110f318SNuno Sá struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
1630f110f318SNuno Sá
1631f110f318SNuno Sá return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
1632f110f318SNuno Sá }
1633f110f318SNuno Sá
1634fb4e8e2dSJonathan Cameron static DEFINE_SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend,
1635fb4e8e2dSJonathan Cameron ltc2983_resume);
1636f110f318SNuno Sá
16376f7cadcfSCosmin Tanislav static const struct ltc2983_chip_info ltc2983_chip_info_data = {
16385cad30abSNuno Sa .name = "ltc2983",
16396f7cadcfSCosmin Tanislav .max_channels_nr = 20,
16406f7cadcfSCosmin Tanislav };
16416f7cadcfSCosmin Tanislav
16426f7cadcfSCosmin Tanislav static const struct ltc2983_chip_info ltc2984_chip_info_data = {
16435cad30abSNuno Sa .name = "ltc2984",
16446f7cadcfSCosmin Tanislav .max_channels_nr = 20,
16456f7cadcfSCosmin Tanislav .has_eeprom = true,
16466f7cadcfSCosmin Tanislav };
16476f7cadcfSCosmin Tanislav
16486f7cadcfSCosmin Tanislav static const struct ltc2983_chip_info ltc2986_chip_info_data = {
16495cad30abSNuno Sa .name = "ltc2986",
16505cad30abSNuno Sa .max_channels_nr = 10,
16515cad30abSNuno Sa .has_temp = true,
16525cad30abSNuno Sa .has_eeprom = true,
16535cad30abSNuno Sa };
16545cad30abSNuno Sa
16555cad30abSNuno Sa static const struct ltc2983_chip_info ltm2985_chip_info_data = {
16565cad30abSNuno Sa .name = "ltm2985",
16576f7cadcfSCosmin Tanislav .max_channels_nr = 10,
16586f7cadcfSCosmin Tanislav .has_temp = true,
16596f7cadcfSCosmin Tanislav .has_eeprom = true,
16606f7cadcfSCosmin Tanislav };
16616f7cadcfSCosmin Tanislav
1662f110f318SNuno Sá static const struct spi_device_id ltc2983_id_table[] = {
16636f7cadcfSCosmin Tanislav { "ltc2983", (kernel_ulong_t)<c2983_chip_info_data },
16646f7cadcfSCosmin Tanislav { "ltc2984", (kernel_ulong_t)<c2984_chip_info_data },
16656f7cadcfSCosmin Tanislav { "ltc2986", (kernel_ulong_t)<c2986_chip_info_data },
16665cad30abSNuno Sa { "ltm2985", (kernel_ulong_t)<m2985_chip_info_data },
1667f110f318SNuno Sá {},
1668f110f318SNuno Sá };
1669f110f318SNuno Sá MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
1670f110f318SNuno Sá
1671f110f318SNuno Sá static const struct of_device_id ltc2983_of_match[] = {
16726f7cadcfSCosmin Tanislav { .compatible = "adi,ltc2983", .data = <c2983_chip_info_data },
16736f7cadcfSCosmin Tanislav { .compatible = "adi,ltc2984", .data = <c2984_chip_info_data },
16746f7cadcfSCosmin Tanislav { .compatible = "adi,ltc2986", .data = <c2986_chip_info_data },
16755cad30abSNuno Sa { .compatible = "adi,ltm2985", .data = <m2985_chip_info_data },
1676f110f318SNuno Sá {},
1677f110f318SNuno Sá };
1678f110f318SNuno Sá MODULE_DEVICE_TABLE(of, ltc2983_of_match);
1679f110f318SNuno Sá
1680f110f318SNuno Sá static struct spi_driver ltc2983_driver = {
1681f110f318SNuno Sá .driver = {
1682f110f318SNuno Sá .name = "ltc2983",
1683f110f318SNuno Sá .of_match_table = ltc2983_of_match,
1684fb4e8e2dSJonathan Cameron .pm = pm_sleep_ptr(<c2983_pm_ops),
1685f110f318SNuno Sá },
1686f110f318SNuno Sá .probe = ltc2983_probe,
1687f110f318SNuno Sá .id_table = ltc2983_id_table,
1688f110f318SNuno Sá };
1689f110f318SNuno Sá
1690f110f318SNuno Sá module_spi_driver(ltc2983_driver);
1691f110f318SNuno Sá
1692f110f318SNuno Sá MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
1693f110f318SNuno Sá MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
1694f110f318SNuno Sá MODULE_LICENSE("GPL");
1695