Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  * driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  * Copyright 2019 Analog Devices Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/bitfield.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/completion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include <linux/iio/iio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <linux/list.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <linux/spi/spi.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) /* register map */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #define LTC2983_STATUS_REG			0x0000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #define LTC2983_TEMP_RES_START_REG		0x0010
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #define LTC2983_TEMP_RES_END_REG		0x005F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #define LTC2983_GLOBAL_CONFIG_REG		0x00F0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #define LTC2983_MULT_CHANNEL_START_REG		0x00F4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #define LTC2983_MULT_CHANNEL_END_REG		0x00F7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) #define LTC2983_MUX_CONFIG_REG			0x00FF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) #define LTC2983_CHAN_ASSIGN_START_REG		0x0200
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define LTC2983_CHAN_ASSIGN_END_REG		0x024F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) #define LTC2983_CUST_SENS_TBL_START_REG		0x0250
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) #define LTC2983_CUST_SENS_TBL_END_REG		0x03CF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define LTC2983_DIFFERENTIAL_CHAN_MIN		2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) #define LTC2983_MAX_CHANNELS_NR			20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) #define LTC2983_MIN_CHANNELS_NR			1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define LTC2983_SLEEP				0x97
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) #define LTC2983_CUSTOM_STEINHART_SIZE		24
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) #define LTC2983_CUSTOM_SENSOR_ENTRY_SZ		6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) #define LTC2983_CUSTOM_STEINHART_ENTRY_SZ	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) #define LTC2983_CHAN_START_ADDR(chan) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 			(((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) #define LTC2983_CHAN_RES_ADDR(chan) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) 			(((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) #define LTC2983_THERMOCOUPLE_DIFF_MASK		BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) #define LTC2983_THERMOCOUPLE_SGL(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 				FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #define LTC2983_THERMOCOUPLE_OC_CURR_MASK	GENMASK(1, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) #define LTC2983_THERMOCOUPLE_OC_CURR(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 				FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define LTC2983_THERMOCOUPLE_OC_CHECK_MASK	BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define LTC2983_THERMOCOUPLE_OC_CHECK(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 			FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define LTC2983_THERMISTOR_DIFF_MASK		BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) #define LTC2983_THERMISTOR_SGL(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 				FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #define LTC2983_THERMISTOR_R_SHARE_MASK		BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) #define LTC2983_THERMISTOR_R_SHARE(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 				FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define LTC2983_THERMISTOR_C_ROTATE_MASK	BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define LTC2983_THERMISTOR_C_ROTATE(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 				FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define LTC2983_DIODE_DIFF_MASK			BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) #define LTC2983_DIODE_SGL(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 			FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define LTC2983_DIODE_3_CONV_CYCLE_MASK		BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) #define LTC2983_DIODE_3_CONV_CYCLE(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 				FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define LTC2983_DIODE_AVERAGE_ON_MASK		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) #define LTC2983_DIODE_AVERAGE_ON(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 				FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) #define LTC2983_RTD_4_WIRE_MASK			BIT(3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) #define LTC2983_RTD_ROTATION_MASK		BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) #define LTC2983_RTD_C_ROTATE(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 			FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) #define LTC2983_RTD_KELVIN_R_SENSE_MASK		GENMASK(3, 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) #define LTC2983_RTD_N_WIRES_MASK		GENMASK(3, 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) #define LTC2983_RTD_N_WIRES(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 			FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) #define LTC2983_RTD_R_SHARE_MASK		BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) #define LTC2983_RTD_R_SHARE(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 			FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) #define LTC2983_COMMON_HARD_FAULT_MASK	GENMASK(31, 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) #define LTC2983_COMMON_SOFT_FAULT_MASK	GENMASK(27, 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) #define	LTC2983_STATUS_START_MASK	BIT(7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) #define	LTC2983_STATUS_START(x)		FIELD_PREP(LTC2983_STATUS_START_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) #define	LTC2983_STATUS_UP_MASK		GENMASK(7, 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) #define	LTC2983_STATUS_UP(reg)		FIELD_GET(LTC2983_STATUS_UP_MASK, reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) #define	LTC2983_STATUS_CHAN_SEL_MASK	GENMASK(4, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) #define	LTC2983_STATUS_CHAN_SEL(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 				FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) #define LTC2983_TEMP_UNITS_MASK		BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) #define LTC2983_TEMP_UNITS(x)		FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) #define LTC2983_NOTCH_FREQ_MASK		GENMASK(1, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) #define LTC2983_NOTCH_FREQ(x)		FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) #define LTC2983_RES_VALID_MASK		BIT(24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) #define LTC2983_DATA_MASK		GENMASK(23, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) #define LTC2983_DATA_SIGN_BIT		23
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) #define LTC2983_CHAN_TYPE_MASK		GENMASK(31, 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) #define LTC2983_CHAN_TYPE(x)		FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) /* cold junction for thermocouples and rsense for rtd's and thermistor's */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) #define LTC2983_CHAN_ASSIGN_MASK	GENMASK(26, 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define LTC2983_CHAN_ASSIGN(x)		FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define LTC2983_CUSTOM_LEN_MASK		GENMASK(5, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define LTC2983_CUSTOM_LEN(x)		FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) #define LTC2983_CUSTOM_ADDR_MASK	GENMASK(11, 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #define LTC2983_CUSTOM_ADDR(x)		FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define LTC2983_THERMOCOUPLE_CFG_MASK	GENMASK(21, 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) #define LTC2983_THERMOCOUPLE_CFG(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 				FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) #define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK	GENMASK(31, 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK	GENMASK(28, 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) #define LTC2983_RTD_CFG_MASK		GENMASK(21, 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define LTC2983_RTD_CFG(x)		FIELD_PREP(LTC2983_RTD_CFG_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) #define LTC2983_RTD_EXC_CURRENT_MASK	GENMASK(17, 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) #define LTC2983_RTD_EXC_CURRENT(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) 				FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) #define LTC2983_RTD_CURVE_MASK		GENMASK(13, 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) #define LTC2983_RTD_CURVE(x)		FIELD_PREP(LTC2983_RTD_CURVE_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) #define LTC2983_THERMISTOR_CFG_MASK	GENMASK(21, 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) #define LTC2983_THERMISTOR_CFG(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 				FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) #define LTC2983_THERMISTOR_EXC_CURRENT_MASK	GENMASK(18, 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) #define LTC2983_THERMISTOR_EXC_CURRENT(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 			FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) #define LTC2983_DIODE_CFG_MASK		GENMASK(26, 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) #define LTC2983_DIODE_CFG(x)		FIELD_PREP(LTC2983_DIODE_CFG_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) #define LTC2983_DIODE_EXC_CURRENT_MASK	GENMASK(23, 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) #define LTC2983_DIODE_EXC_CURRENT(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) 				FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) #define LTC2983_DIODE_IDEAL_FACTOR_MASK	GENMASK(21, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) #define LTC2983_DIODE_IDEAL_FACTOR(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 				FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) #define LTC2983_R_SENSE_VAL_MASK	GENMASK(26, 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) #define LTC2983_R_SENSE_VAL(x)		FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) #define LTC2983_ADC_SINGLE_ENDED_MASK	BIT(26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) #define LTC2983_ADC_SINGLE_ENDED(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 				FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	LTC2983_SENSOR_THERMOCOUPLE = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 	LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 	LTC2983_SENSOR_RTD = 10,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	LTC2983_SENSOR_RTD_CUSTOM = 18,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	LTC2983_SENSOR_THERMISTOR = 19,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	LTC2983_SENSOR_THERMISTOR_STEINHART = 26,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	LTC2983_SENSOR_THERMISTOR_CUSTOM = 27,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	LTC2983_SENSOR_DIODE = 28,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	LTC2983_SENSOR_SENSE_RESISTOR = 29,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 	LTC2983_SENSOR_DIRECT_ADC = 30,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) #define to_thermocouple(_sensor) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		container_of(_sensor, struct ltc2983_thermocouple, sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) #define to_rtd(_sensor) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		container_of(_sensor, struct ltc2983_rtd, sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) #define to_thermistor(_sensor) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 		container_of(_sensor, struct ltc2983_thermistor, sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) #define to_diode(_sensor) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		container_of(_sensor, struct ltc2983_diode, sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) #define to_rsense(_sensor) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 		container_of(_sensor, struct ltc2983_rsense, sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) #define to_adc(_sensor) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		container_of(_sensor, struct ltc2983_adc, sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) struct ltc2983_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	struct regmap *regmap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 	struct spi_device *spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	struct mutex lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 	struct completion completion;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	struct iio_chan_spec *iio_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 	struct ltc2983_sensor **sensors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	u32 mux_delay_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 	u32 filter_notch_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 	u16 custom_table_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	u8 num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 	u8 iio_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 	 * DMA (thus cache coherency maintenance) requires the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 	 * transfer buffers to live in their own cache lines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 	 * Holds the converted temperature
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	__be32 temp ____cacheline_aligned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) struct ltc2983_sensor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	int (*fault_handler)(const struct ltc2983_data *st, const u32 result);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	int (*assign_chan)(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			   const struct ltc2983_sensor *sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	/* specifies the sensor channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	u32 chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 	/* sensor type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 	u32 type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) struct ltc2983_custom_sensor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 	/* raw table sensor data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	u8 *table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 	size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	/* address offset */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 	s8 offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 	bool is_steinhart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) struct ltc2983_thermocouple {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	struct ltc2983_sensor sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	struct ltc2983_custom_sensor *custom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	u32 sensor_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	u32 cold_junction_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) struct ltc2983_rtd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 	struct ltc2983_sensor sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	struct ltc2983_custom_sensor *custom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 	u32 sensor_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	u32 r_sense_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 	u32 excitation_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	u32 rtd_curve;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) struct ltc2983_thermistor {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	struct ltc2983_sensor sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	struct ltc2983_custom_sensor *custom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	u32 sensor_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 	u32 r_sense_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	u32 excitation_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) struct ltc2983_diode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	struct ltc2983_sensor sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	u32 sensor_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	u32 excitation_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	u32 ideal_factor_value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) struct ltc2983_rsense {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	struct ltc2983_sensor sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	u32 r_sense_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) struct ltc2983_adc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct ltc2983_sensor sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 	bool single_ended;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271)  * Convert to Q format numbers. These number's are integers where
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272)  * the number of integer and fractional bits are specified. The resolution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273)  * is given by 1/@resolution and tell us the number of fractional bits. For
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274)  * instance a resolution of 2^-10 means we have 10 fractional bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) static u32 __convert_to_raw(const u64 val, const u32 resolution)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	u64 __res = val * resolution;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	/* all values are multiplied by 1000000 to remove the fraction */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	do_div(__res, 1000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 	return __res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) static u32 __convert_to_raw_sign(const u64 val, const u32 resolution)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	s64 __res = -(s32)val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	__res = __convert_to_raw(__res, resolution);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	return (u32)-__res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) static int __ltc2983_fault_handler(const struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 				   const u32 result, const u32 hard_mask,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 				   const u32 soft_mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	const struct device *dev = &st->spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	if (result & hard_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		dev_err(dev, "Invalid conversion: Sensor HARD fault\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	} else if (result & soft_mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		/* just print a warning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) static int __ltc2983_chan_assign_common(const struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 					const struct ltc2983_sensor *sensor,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 					u32 chan_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 	__be32 __chan_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 	chan_val |= LTC2983_CHAN_TYPE(sensor->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 		chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	__chan_val = cpu_to_be32(chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 	return regmap_bulk_write(st->regmap, reg, &__chan_val,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 				 sizeof(__chan_val));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) static int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 					  struct ltc2983_custom_sensor *custom,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 					  u32 *chan_val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 	u32 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 	const struct device *dev = &st->spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 	 * custom->size holds the raw size of the table. However, when
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 	 * configuring the sensor channel, we must write the number of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	 * entries of the table minus 1. For steinhart sensors 0 is written
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	 * since the size is constant!
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	const u8 len = custom->is_steinhart ? 0 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 		(custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 	 * Check if the offset was assigned already. It should be for steinhart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	 * sensors. When coming from sleep, it should be assigned for all.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	if (custom->offset < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		 * This needs to be done again here because, from the moment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		 * when this test was done (successfully) for this custom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		 * sensor, a steinhart sensor might have been added changing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		 * custom_table_size...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		if (st->custom_table_size + custom->size >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		    (LTC2983_CUST_SENS_TBL_END_REG -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		     LTC2983_CUST_SENS_TBL_START_REG) + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 			dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 				"Not space left(%d) for new custom sensor(%zu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 				st->custom_table_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 				custom->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 		custom->offset = st->custom_table_size /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 					LTC2983_CUSTOM_SENSOR_ENTRY_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 		st->custom_table_size += custom->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	*chan_val |= LTC2983_CUSTOM_LEN(len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	*chan_val |= LTC2983_CUSTOM_ADDR(custom->offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 	dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		reg, custom->offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		custom->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	/* write custom sensor table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	return regmap_bulk_write(st->regmap, reg, custom->table, custom->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) static struct ltc2983_custom_sensor *__ltc2983_custom_sensor_new(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 						struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 						const struct device_node *np,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 						const char *propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 						const bool is_steinhart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 						const u32 resolution,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 						const bool has_signed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	struct ltc2983_custom_sensor *new_custom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 	u8 index, n_entries, tbl = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	struct device *dev = &st->spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 	 * For custom steinhart, the full u32 is taken. For all the others
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	 * the MSB is discarded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	const u8 n_size = is_steinhart ? 4 : 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 	const u8 e_size = is_steinhart ? sizeof(u32) : sizeof(u64);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 	n_entries = of_property_count_elems_of_size(np, propname, e_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	/* n_entries must be an even number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	if (!n_entries || (n_entries % 2) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		dev_err(dev, "Number of entries either 0 or not even\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 	new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	if (!new_custom)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 	new_custom->size = n_entries * n_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	/* check Steinhart size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 	if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 		dev_err(dev, "Steinhart sensors size(%zu) must be 24",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 							new_custom->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 	/* Check space on the table. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 	if (st->custom_table_size + new_custom->size >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 	    (LTC2983_CUST_SENS_TBL_END_REG -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 	     LTC2983_CUST_SENS_TBL_START_REG) + 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		dev_err(dev, "No space left(%d) for new custom sensor(%zu)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 				st->custom_table_size, new_custom->size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	/* allocate the table */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	new_custom->table = devm_kzalloc(dev, new_custom->size, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	if (!new_custom->table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	for (index = 0; index < n_entries; index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		u64 temp = 0, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		 * Steinhart sensors are configured with raw values in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		 * devicetree. For the other sensors we must convert the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 		 * value to raw. The odd index's correspond to temperarures
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 		 * and always have 1/1024 of resolution. Temperatures also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		 * come in kelvin, so signed values is not possible
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 		if (!is_steinhart) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 			of_property_read_u64_index(np, propname, index, &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 			if ((index % 2) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 				temp = __convert_to_raw(temp, 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			else if (has_signed && (s64)temp < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 				temp = __convert_to_raw_sign(temp, resolution);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 				temp = __convert_to_raw(temp, resolution);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 			u32 t32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 			of_property_read_u32_index(np, propname, index, &t32);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 			temp = t32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		for (j = 0; j < n_size; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			new_custom->table[tbl++] =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 				temp >> (8 * (n_size - j - 1));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	new_custom->is_steinhart = is_steinhart;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	 * This is done to first add all the steinhart sensors to the table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 	 * in order to maximize the table usage. If we mix adding steinhart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	 * with the other sensors, we might have to do some roundup to make
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	 * sure that sensor_addr - 0x250(start address) is a multiple of 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	 * (for steinhart), and a multiple of 6 for all the other sensors.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	 * Since we have const 24 bytes for steinhart sensors and 24 is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	 * also a multiple of 6, we guarantee that the first non-steinhart
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	 * sensor will sit in a correct address without the need of filling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	 * addresses.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	if (is_steinhart) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 		new_custom->offset = st->custom_table_size /
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 					LTC2983_CUSTOM_STEINHART_ENTRY_SZ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		st->custom_table_size += new_custom->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		/* mark as unset. This is checked later on the assign phase */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		new_custom->offset = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 	return new_custom;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) static int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 					      const u32 result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 	return __ltc2983_fault_handler(st, result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 				       LTC2983_THERMOCOUPLE_HARD_FAULT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 				       LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) static int ltc2983_common_fault_handler(const struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 					const u32 result)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 	return __ltc2983_fault_handler(st, result,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 				       LTC2983_COMMON_HARD_FAULT_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 				       LTC2983_COMMON_SOFT_FAULT_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) static int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 				const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 	struct ltc2983_thermocouple *thermo = to_thermocouple(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 	u32 chan_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 	chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 	chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 	if (thermo->custom) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 		ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 							  &chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 	return __ltc2983_chan_assign_common(st, sensor, chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) static int ltc2983_rtd_assign_chan(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 				   const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 	struct ltc2983_rtd *rtd = to_rtd(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	u32 chan_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 	chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 	chan_val |= LTC2983_RTD_CFG(rtd->sensor_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 	chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	if (rtd->custom) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 							  &chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	return __ltc2983_chan_assign_common(st, sensor, chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) static int ltc2983_thermistor_assign_chan(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 					  const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	struct ltc2983_thermistor *thermistor = to_thermistor(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	u32 chan_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	chan_val |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	if (thermistor->custom) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 		ret = __ltc2983_chan_custom_sensor_assign(st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 							  thermistor->custom,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 							  &chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 	return __ltc2983_chan_assign_common(st, sensor, chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) static int ltc2983_diode_assign_chan(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 				     const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	struct ltc2983_diode *diode = to_diode(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	u32 chan_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 	chan_val = LTC2983_DIODE_CFG(diode->sensor_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 	return __ltc2983_chan_assign_common(st, sensor, chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) static int ltc2983_r_sense_assign_chan(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 				       const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 	struct ltc2983_rsense *rsense = to_rsense(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	u32 chan_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	return __ltc2983_chan_assign_common(st, sensor, chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) static int ltc2983_adc_assign_chan(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 				   const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	struct ltc2983_adc *adc = to_adc(sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	u32 chan_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	return __ltc2983_chan_assign_common(st, sensor, chan_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) static struct ltc2983_sensor *ltc2983_thermocouple_new(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 					const struct device_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 					struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 					const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	struct ltc2983_thermocouple *thermo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 	struct device_node *phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 	u32 oc_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	if (!thermo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	if (of_property_read_bool(child, "adi,single-ended"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 	ret = of_property_read_u32(child, "adi,sensor-oc-current-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 				   &oc_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 		switch (oc_current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 		case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 			thermo->sensor_config |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 					LTC2983_THERMOCOUPLE_OC_CURR(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		case 100:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 			thermo->sensor_config |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 					LTC2983_THERMOCOUPLE_OC_CURR(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 		case 500:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 			thermo->sensor_config |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 					LTC2983_THERMOCOUPLE_OC_CURR(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 		case 1000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 			thermo->sensor_config |=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 					LTC2983_THERMOCOUPLE_OC_CURR(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 			dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 				"Invalid open circuit current:%u", oc_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 			return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 		thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	/* validate channel index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 	if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 			"Invalid chann:%d for differential thermocouple",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 			sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	phandle = of_parse_phandle(child, "adi,cold-junction-handle", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	if (phandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 		int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 		ret = of_property_read_u32(phandle, "reg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 					   &thermo->cold_junction_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			 * This would be catched later but we can just return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 			 * the error right away.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 			dev_err(&st->spi->dev, "Property reg must be given\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 			of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 			return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	/* check custom sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		const char *propname = "adi,custom-thermocouple";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 		thermo->custom = __ltc2983_custom_sensor_new(st, child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 							     propname, false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 							     16384, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 		if (IS_ERR(thermo->custom)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 			of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			return ERR_CAST(thermo->custom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	/* set common parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 	of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	return &thermo->sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) static struct ltc2983_sensor *ltc2983_rtd_new(const struct device_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 					  struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 					  const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 	struct ltc2983_rtd *rtd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 	struct device *dev = &st->spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	struct device_node *phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	u32 excitation_current = 0, n_wires = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	if (!rtd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	phandle = of_parse_phandle(child, "adi,rsense-handle", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	if (!phandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 		dev_err(dev, "Property adi,rsense-handle missing or invalid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	ret = of_property_read_u32(phandle, "reg", &rtd->r_sense_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 		dev_err(dev, "Property reg must be given\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	ret = of_property_read_u32(child, "adi,number-of-wires", &n_wires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		switch (n_wires) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		case 2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 			rtd->sensor_config = LTC2983_RTD_N_WIRES(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		case 3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 			rtd->sensor_config = LTC2983_RTD_N_WIRES(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		case 4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 			rtd->sensor_config = LTC2983_RTD_N_WIRES(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 		case 5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 			/* 4 wires, Kelvin Rsense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 			rtd->sensor_config = LTC2983_RTD_N_WIRES(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 			dev_err(dev, "Invalid number of wires:%u\n", n_wires);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) 	if (of_property_read_bool(child, "adi,rsense-share")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) 		/* Current rotation is only available with rsense sharing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 		if (of_property_read_bool(child, "adi,current-rotate")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 			if (n_wires == 2 || n_wires == 3) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 				dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 					"Rotation not allowed for 2/3 Wire RTDs");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 				ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 			rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			rtd->sensor_config |= LTC2983_RTD_R_SHARE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	 * rtd channel indexes are a bit more complicated to validate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 	 * For 4wire RTD with rotation, the channel selection cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	 * >=19 since the chann + 1 is used in this configuration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 	 * For 4wire RTDs with kelvin rsense, the rsense channel cannot be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 	 * <=1 since chanel - 1 and channel - 2 are used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		/* 4-wire */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 		u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 			max = LTC2983_MAX_CHANNELS_NR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 		if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 			max = LTC2983_MAX_CHANNELS_NR - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		     == LTC2983_RTD_KELVIN_R_SENSE_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 		    (rtd->r_sense_chan <=  min)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 			/* kelvin rsense*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 			dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 				"Invalid rsense chann:%d to use in kelvin rsense",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 				rtd->r_sense_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 		if (sensor->chan < min || sensor->chan > max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 			dev_err(dev, "Invalid chann:%d for the rtd config",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 				sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 		/* same as differential case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 		if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 			dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 				"Invalid chann:%d for RTD", sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	/* check custom sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		rtd->custom = __ltc2983_custom_sensor_new(st, child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 							  "adi,custom-rtd",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 							  false, 2048, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		if (IS_ERR(rtd->custom)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 			of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 			return ERR_CAST(rtd->custom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	/* set common parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	rtd->sensor.fault_handler = ltc2983_common_fault_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	rtd->sensor.assign_chan = ltc2983_rtd_assign_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 	ret = of_property_read_u32(child, "adi,excitation-current-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 				   &excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 		/* default to 5uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 		rtd->excitation_current = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		switch (excitation_current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 		case 5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 			rtd->excitation_current = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 		case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			rtd->excitation_current = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 		case 25:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 			rtd->excitation_current = 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		case 50:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 			rtd->excitation_current = 0x04;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 		case 100:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 			rtd->excitation_current = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		case 250:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			rtd->excitation_current = 0x06;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		case 500:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 			rtd->excitation_current = 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 		case 1000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 			rtd->excitation_current = 0x08;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 			dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 				"Invalid value for excitation current(%u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 				excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	of_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 	of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	return &rtd->sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) static struct ltc2983_sensor *ltc2983_thermistor_new(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 					const struct device_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 					struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 					const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	struct ltc2983_thermistor *thermistor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	struct device *dev = &st->spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	struct device_node *phandle;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	u32 excitation_current = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 	if (!thermistor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	phandle = of_parse_phandle(child, "adi,rsense-handle", 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	if (!phandle) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		dev_err(dev, "Property adi,rsense-handle missing or invalid");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	ret = of_property_read_u32(phandle, "reg", &thermistor->r_sense_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		dev_err(dev, "rsense channel must be configured...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 	if (of_property_read_bool(child, "adi,single-ended")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 		thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	} else if (of_property_read_bool(child, "adi,rsense-share")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		/* rotation is only possible if sharing rsense */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		if (of_property_read_bool(child, "adi,current-rotate"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 			thermistor->sensor_config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 						LTC2983_THERMISTOR_C_ROTATE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 			thermistor->sensor_config =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 						LTC2983_THERMISTOR_R_SHARE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	/* validate channel index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 		dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 			"Invalid chann:%d for differential thermistor",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 			sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 		ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 	/* check custom sensor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 	if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 		bool steinhart = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 		const char *propname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 		if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 			steinhart = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 			propname = "adi,custom-steinhart";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 			propname = "adi,custom-thermistor";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 		thermistor->custom = __ltc2983_custom_sensor_new(st, child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 								 propname,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 								 steinhart,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 								 64, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		if (IS_ERR(thermistor->custom)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 			return ERR_CAST(thermistor->custom);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 	/* set common parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	thermistor->sensor.fault_handler = ltc2983_common_fault_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	ret = of_property_read_u32(child, "adi,excitation-current-nanoamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 				   &excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		/* Auto range is not allowed for custom sensors */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 			/* default to 1uA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			thermistor->excitation_current = 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 			/* default to auto-range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 			thermistor->excitation_current = 0x0c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 		switch (excitation_current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 		case 0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 			/* auto range */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 			if (sensor->type >=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 			    LTC2983_SENSOR_THERMISTOR_STEINHART) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 				dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 					"Auto Range not allowed for custom sensors\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 				ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 				goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 			thermistor->excitation_current = 0x0c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 		case 250:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 			thermistor->excitation_current = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 		case 500:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 			thermistor->excitation_current = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		case 1000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 			thermistor->excitation_current = 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 		case 5000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 			thermistor->excitation_current = 0x04;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 		case 10000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 			thermistor->excitation_current = 0x05;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		case 25000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 			thermistor->excitation_current = 0x06;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		case 50000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 			thermistor->excitation_current = 0x07;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 		case 100000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 			thermistor->excitation_current = 0x08;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		case 250000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 			thermistor->excitation_current = 0x09;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		case 500000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			thermistor->excitation_current = 0x0a;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 		case 1000000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			thermistor->excitation_current = 0x0b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 			dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 				"Invalid value for excitation current(%u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 				excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	return &thermistor->sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	of_node_put(phandle);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	return ERR_PTR(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) static struct ltc2983_sensor *ltc2983_diode_new(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 					const struct device_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 					const struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 					const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	struct ltc2983_diode *diode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	u32 temp = 0, excitation_current = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 	diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	if (!diode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	if (of_property_read_bool(child, "adi,single-ended"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 		diode->sensor_config = LTC2983_DIODE_SGL(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) 	if (of_property_read_bool(child, "adi,three-conversion-cycles"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 		diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	if (of_property_read_bool(child, "adi,average-on"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 		diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 	/* validate channel index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			"Invalid chann:%d for differential thermistor",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	/* set common parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	diode->sensor.fault_handler = ltc2983_common_fault_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 	diode->sensor.assign_chan = ltc2983_diode_assign_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	ret = of_property_read_u32(child, "adi,excitation-current-microamp",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 				   &excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	if (!ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 		switch (excitation_current) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 		case 10:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			diode->excitation_current = 0x00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 		case 20:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 			diode->excitation_current = 0x01;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 		case 40:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 			diode->excitation_current = 0x02;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		case 80:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 			diode->excitation_current = 0x03;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 			dev_err(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 				"Invalid value for excitation current(%u)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 				excitation_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 			return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	of_property_read_u32(child, "adi,ideal-factor-value", &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	/* 2^20 resolution */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	diode->ideal_factor_value = __convert_to_raw(temp, 1048576);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 	return &diode->sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) static struct ltc2983_sensor *ltc2983_r_sense_new(struct device_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 					struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 					const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	struct ltc2983_rsense *rsense;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 	u32 temp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 	if (!rsense)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	/* validate channel index */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 		dev_err(&st->spi->dev, "Invalid chann:%d for r_sense",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 			sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	ret = of_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 		dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 	 * Times 1000 because we have milli-ohms and __convert_to_raw
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 	 * expects scales of 1000000 which are used for all other
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	 * properties.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	 * 2^10 resolution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	/* set common parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 	return &rsense->sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) static struct ltc2983_sensor *ltc2983_adc_new(struct device_node *child,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) 					 struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 					 const struct ltc2983_sensor *sensor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 	struct ltc2983_adc *adc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) 	adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 	if (!adc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	if (of_property_read_bool(child, "adi,single-ended"))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 		adc->single_ended = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	if (!adc->single_ended &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	    sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 		dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 			sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 		return ERR_PTR(-EINVAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	/* set common parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	adc->sensor.assign_chan = ltc2983_adc_assign_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	adc->sensor.fault_handler = ltc2983_common_fault_handler;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	return &adc->sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) static int ltc2983_chan_read(struct ltc2983_data *st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 			const struct ltc2983_sensor *sensor, int *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 	u32 start_conversion = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 	unsigned long time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 	start_conversion = LTC2983_STATUS_START(true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 	start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 	dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		sensor->chan, start_conversion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 	/* start conversion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	reinit_completion(&st->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 	 * wait for conversion to complete.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 	 * 300 ms should be more than enough to complete the conversion.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 	 * Depending on the sensor configuration, there are 2/3 conversions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 	 * cycles of 82ms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	time = wait_for_completion_timeout(&st->completion,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 					   msecs_to_jiffies(300));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 	if (!time) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 		dev_warn(&st->spi->dev, "Conversion timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		return -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 	/* read the converted data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 	ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 			       &st->temp, sizeof(st->temp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	*val = __be32_to_cpu(st->temp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 	if (!(LTC2983_RES_VALID_MASK & *val)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		dev_err(&st->spi->dev, "Invalid conversion detected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 	ret = sensor->fault_handler(st, *val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	*val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) static int ltc2983_read_raw(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 			    struct iio_chan_spec const *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			    int *val, int *val2, long mask)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 	struct ltc2983_data *st = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	/* sanity check */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	if (chan->address >= st->num_channels) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 		dev_err(&st->spi->dev, "Invalid chan address:%ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 			chan->address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) 	switch (mask) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 	case IIO_CHAN_INFO_RAW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) 		mutex_lock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) 		ret = ltc2983_chan_read(st, st->sensors[chan->address], val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 		mutex_unlock(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) 		return ret ?: IIO_VAL_INT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	case IIO_CHAN_INFO_SCALE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) 		switch (chan->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 		case IIO_TEMP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 			/* value in milli degrees */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 			*val = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 			/* 2^10 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 			*val2 = 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 			return IIO_VAL_FRACTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 		case IIO_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 			/* value in millivolt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 			*val = 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 			/* 2^21 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 			*val2 = 2097152;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 			return IIO_VAL_FRACTIONAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) static int ltc2983_reg_access(struct iio_dev *indio_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 			      unsigned int reg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 			      unsigned int writeval,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 			      unsigned int *readval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	struct ltc2983_data *st = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	if (readval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 		return regmap_read(st->regmap, reg, readval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 		return regmap_write(st->regmap, reg, writeval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) static irqreturn_t ltc2983_irq_handler(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	struct ltc2983_data *st = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 	complete(&st->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 	return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) #define LTC2983_CHAN(__type, index, __address) ({ \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 	struct iio_chan_spec __chan = { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 		.type = __type, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 		.indexed = 1, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 		.channel = index, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 		.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 		.address = __address, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	}; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 	__chan; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) })
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) static int ltc2983_parse_dt(struct ltc2983_data *st)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 	struct device_node *child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	struct device *dev = &st->spi->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	int ret = 0, chan = 0, channel_avail_mask = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 	of_property_read_u32(dev->of_node, "adi,mux-delay-config-us",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 			     &st->mux_delay_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	of_property_read_u32(dev->of_node, "adi,filter-notch-freq",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 			     &st->filter_notch_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	st->num_channels = of_get_available_child_count(dev->of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 	st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 				   GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 	if (!st->sensors)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	st->iio_channels = st->num_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) 	for_each_available_child_of_node(dev->of_node, child) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 		struct ltc2983_sensor sensor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 		ret = of_property_read_u32(child, "reg", &sensor.chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 			dev_err(dev, "reg property must given for child nodes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 			goto put_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 		/* check if we have a valid channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		if (sensor.chan < LTC2983_MIN_CHANNELS_NR ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		    sensor.chan > LTC2983_MAX_CHANNELS_NR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 			dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 				"chan:%d must be from 1 to 20\n", sensor.chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 			goto put_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 		} else if (channel_avail_mask & BIT(sensor.chan)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 			dev_err(dev, "chan:%d already in use\n", sensor.chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			goto put_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		ret = of_property_read_u32(child, "adi,sensor-type",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 					       &sensor.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 		if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 			dev_err(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 				"adi,sensor-type property must given for child nodes\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 			goto put_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 		dev_dbg(dev, "Create new sensor, type %u, chann %u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 								sensor.type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 								sensor.chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		    sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 			st->sensors[chan] = ltc2983_thermocouple_new(child, st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 								     &sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 		} else if (sensor.type >= LTC2983_SENSOR_RTD &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 			   sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 			st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		} else if (sensor.type >= LTC2983_SENSOR_THERMISTOR &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 			   sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			st->sensors[chan] = ltc2983_thermistor_new(child, st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 								   &sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 		} else if (sensor.type == LTC2983_SENSOR_DIODE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 			st->sensors[chan] = ltc2983_diode_new(child, st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 							      &sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 		} else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 			st->sensors[chan] = ltc2983_r_sense_new(child, st,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 								&sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 			/* don't add rsense to iio */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 			st->iio_channels--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 		} else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 			st->sensors[chan] = ltc2983_adc_new(child, st, &sensor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 			dev_err(dev, "Unknown sensor type %d\n", sensor.type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 			ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 			goto put_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		if (IS_ERR(st->sensors[chan])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 			dev_err(dev, "Failed to create sensor %ld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 				PTR_ERR(st->sensors[chan]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			ret = PTR_ERR(st->sensors[chan]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			goto put_child;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 		/* set generic sensor parameters */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		st->sensors[chan]->chan = sensor.chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 		st->sensors[chan]->type = sensor.type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 		channel_avail_mask |= BIT(sensor.chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 		chan++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) put_child:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 	of_node_put(child);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) static int ltc2983_setup(struct ltc2983_data *st, bool assign_iio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	/* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 				       LTC2983_STATUS_UP(status) == 1, 25000,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 				       25000 * 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		dev_err(&st->spi->dev, "Device startup timed out\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 	st->iio_chan = devm_kzalloc(&st->spi->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 				    st->iio_channels * sizeof(*st->iio_chan),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 				    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	if (!st->iio_chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 				 LTC2983_NOTCH_FREQ_MASK,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 				 LTC2983_NOTCH_FREQ(st->filter_notch_freq));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 			   st->mux_delay_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) 	for (chan = 0; chan < st->num_channels; chan++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 		u32 chan_type = 0, *iio_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) 		ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) 		 * The assign_iio flag is necessary for when the device is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 		 * coming out of sleep. In that case, we just need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 		 * re-configure the device channels.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 		 * We also don't assign iio channels for rsense.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 		    !assign_iio)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 		/* assign iio channel */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 		if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 			chan_type = IIO_TEMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 			iio_chan = &iio_chan_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 			chan_type = IIO_VOLTAGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 			iio_chan = &iio_chan_v;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 		 * add chan as the iio .address so that, we can directly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		 * reference the sensor given the iio_chan_spec
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 		st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 						       chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) static const struct regmap_range ltc2983_reg_ranges[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 			 LTC2983_MULT_CHANNEL_END_REG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) 	regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 			 LTC2983_CHAN_ASSIGN_END_REG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) 	regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) 			 LTC2983_CUST_SENS_TBL_END_REG),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) static const struct regmap_access_table ltc2983_reg_table = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 	.yes_ranges = ltc2983_reg_ranges,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	.n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452)  *  The reg_bits are actually 12 but the device needs the first *complete*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453)  *  byte for the command (R/W).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) static const struct regmap_config ltc2983_regmap_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 	.reg_bits = 24,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 	.val_bits = 8,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	.wr_table = &ltc2983_reg_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 	.rd_table = &ltc2983_reg_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 	.read_flag_mask = GENMASK(1, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	.write_flag_mask = BIT(1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) static const struct  iio_info ltc2983_iio_info = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	.read_raw = ltc2983_read_raw,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 	.debugfs_reg_access = ltc2983_reg_access,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) static int ltc2983_probe(struct spi_device *spi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	struct ltc2983_data *st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 	struct iio_dev *indio_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 	const char *name = spi_get_device_id(spi)->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	if (!indio_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	st = iio_priv(indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	st->regmap = devm_regmap_init_spi(spi, &ltc2983_regmap_config);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 	if (IS_ERR(st->regmap)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 		dev_err(&spi->dev, "Failed to initialize regmap\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 		return PTR_ERR(st->regmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 	mutex_init(&st->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	init_completion(&st->completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 	st->spi = spi;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	spi_set_drvdata(spi, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) 	ret = ltc2983_parse_dt(st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) 	ret = ltc2983_setup(st, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) 	ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 			       IRQF_TRIGGER_RISING, name, st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) 		dev_err(&spi->dev, "failed to request an irq, %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	indio_dev->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) 	indio_dev->num_channels = st->iio_channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	indio_dev->channels = st->iio_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	indio_dev->modes = INDIO_DIRECT_MODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	indio_dev->info = &ltc2983_iio_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	return devm_iio_device_register(&spi->dev, indio_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) static int __maybe_unused ltc2983_resume(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 	int dummy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	/* dummy read to bring the device out of sleep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	/* we need to re-assign the channels */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 	return ltc2983_setup(st, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) static int __maybe_unused ltc2983_suspend(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 	return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) static SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend, ltc2983_resume);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) static const struct spi_device_id ltc2983_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	{ "ltc2983" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) MODULE_DEVICE_TABLE(spi, ltc2983_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) static const struct of_device_id ltc2983_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 	{ .compatible = "adi,ltc2983" },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	{},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) MODULE_DEVICE_TABLE(of, ltc2983_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) static struct spi_driver ltc2983_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 		.name = "ltc2983",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 		.of_match_table = ltc2983_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 		.pm = &ltc2983_pm_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 	.probe = ltc2983_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 	.id_table = ltc2983_id_table,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) module_spi_driver(ltc2983_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) MODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) MODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) MODULE_LICENSE("GPL");