18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Analog Devices LTC2983 Multi-Sensor Digital Temperature Measurement System 48c2ecf20Sopenharmony_ci * driver 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * Copyright 2019 Analog Devices Inc. 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#include <linux/bitfield.h> 98c2ecf20Sopenharmony_ci#include <linux/completion.h> 108c2ecf20Sopenharmony_ci#include <linux/device.h> 118c2ecf20Sopenharmony_ci#include <linux/kernel.h> 128c2ecf20Sopenharmony_ci#include <linux/iio/iio.h> 138c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 148c2ecf20Sopenharmony_ci#include <linux/list.h> 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci#include <linux/of_gpio.h> 178c2ecf20Sopenharmony_ci#include <linux/regmap.h> 188c2ecf20Sopenharmony_ci#include <linux/spi/spi.h> 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci/* register map */ 218c2ecf20Sopenharmony_ci#define LTC2983_STATUS_REG 0x0000 228c2ecf20Sopenharmony_ci#define LTC2983_TEMP_RES_START_REG 0x0010 238c2ecf20Sopenharmony_ci#define LTC2983_TEMP_RES_END_REG 0x005F 248c2ecf20Sopenharmony_ci#define LTC2983_GLOBAL_CONFIG_REG 0x00F0 258c2ecf20Sopenharmony_ci#define LTC2983_MULT_CHANNEL_START_REG 0x00F4 268c2ecf20Sopenharmony_ci#define LTC2983_MULT_CHANNEL_END_REG 0x00F7 278c2ecf20Sopenharmony_ci#define LTC2983_MUX_CONFIG_REG 0x00FF 288c2ecf20Sopenharmony_ci#define LTC2983_CHAN_ASSIGN_START_REG 0x0200 298c2ecf20Sopenharmony_ci#define LTC2983_CHAN_ASSIGN_END_REG 0x024F 308c2ecf20Sopenharmony_ci#define LTC2983_CUST_SENS_TBL_START_REG 0x0250 318c2ecf20Sopenharmony_ci#define LTC2983_CUST_SENS_TBL_END_REG 0x03CF 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define LTC2983_DIFFERENTIAL_CHAN_MIN 2 348c2ecf20Sopenharmony_ci#define LTC2983_MAX_CHANNELS_NR 20 358c2ecf20Sopenharmony_ci#define LTC2983_MIN_CHANNELS_NR 1 368c2ecf20Sopenharmony_ci#define LTC2983_SLEEP 0x97 378c2ecf20Sopenharmony_ci#define LTC2983_CUSTOM_STEINHART_SIZE 24 388c2ecf20Sopenharmony_ci#define LTC2983_CUSTOM_SENSOR_ENTRY_SZ 6 398c2ecf20Sopenharmony_ci#define LTC2983_CUSTOM_STEINHART_ENTRY_SZ 4 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define LTC2983_CHAN_START_ADDR(chan) \ 428c2ecf20Sopenharmony_ci (((chan - 1) * 4) + LTC2983_CHAN_ASSIGN_START_REG) 438c2ecf20Sopenharmony_ci#define LTC2983_CHAN_RES_ADDR(chan) \ 448c2ecf20Sopenharmony_ci (((chan - 1) * 4) + LTC2983_TEMP_RES_START_REG) 458c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_DIFF_MASK BIT(3) 468c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_SGL(x) \ 478c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMOCOUPLE_DIFF_MASK, x) 488c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_OC_CURR_MASK GENMASK(1, 0) 498c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_OC_CURR(x) \ 508c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CURR_MASK, x) 518c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_OC_CHECK_MASK BIT(2) 528c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_OC_CHECK(x) \ 538c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMOCOUPLE_OC_CHECK_MASK, x) 548c2ecf20Sopenharmony_ci 558c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_DIFF_MASK BIT(2) 568c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_SGL(x) \ 578c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMISTOR_DIFF_MASK, x) 588c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_R_SHARE_MASK BIT(1) 598c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_R_SHARE(x) \ 608c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMISTOR_R_SHARE_MASK, x) 618c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_C_ROTATE_MASK BIT(0) 628c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_C_ROTATE(x) \ 638c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMISTOR_C_ROTATE_MASK, x) 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci#define LTC2983_DIODE_DIFF_MASK BIT(2) 668c2ecf20Sopenharmony_ci#define LTC2983_DIODE_SGL(x) \ 678c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_DIODE_DIFF_MASK, x) 688c2ecf20Sopenharmony_ci#define LTC2983_DIODE_3_CONV_CYCLE_MASK BIT(1) 698c2ecf20Sopenharmony_ci#define LTC2983_DIODE_3_CONV_CYCLE(x) \ 708c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_DIODE_3_CONV_CYCLE_MASK, x) 718c2ecf20Sopenharmony_ci#define LTC2983_DIODE_AVERAGE_ON_MASK BIT(0) 728c2ecf20Sopenharmony_ci#define LTC2983_DIODE_AVERAGE_ON(x) \ 738c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_DIODE_AVERAGE_ON_MASK, x) 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci#define LTC2983_RTD_4_WIRE_MASK BIT(3) 768c2ecf20Sopenharmony_ci#define LTC2983_RTD_ROTATION_MASK BIT(1) 778c2ecf20Sopenharmony_ci#define LTC2983_RTD_C_ROTATE(x) \ 788c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_RTD_ROTATION_MASK, x) 798c2ecf20Sopenharmony_ci#define LTC2983_RTD_KELVIN_R_SENSE_MASK GENMASK(3, 2) 808c2ecf20Sopenharmony_ci#define LTC2983_RTD_N_WIRES_MASK GENMASK(3, 2) 818c2ecf20Sopenharmony_ci#define LTC2983_RTD_N_WIRES(x) \ 828c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_RTD_N_WIRES_MASK, x) 838c2ecf20Sopenharmony_ci#define LTC2983_RTD_R_SHARE_MASK BIT(0) 848c2ecf20Sopenharmony_ci#define LTC2983_RTD_R_SHARE(x) \ 858c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_RTD_R_SHARE_MASK, 1) 868c2ecf20Sopenharmony_ci 878c2ecf20Sopenharmony_ci#define LTC2983_COMMON_HARD_FAULT_MASK GENMASK(31, 30) 888c2ecf20Sopenharmony_ci#define LTC2983_COMMON_SOFT_FAULT_MASK GENMASK(27, 25) 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#define LTC2983_STATUS_START_MASK BIT(7) 918c2ecf20Sopenharmony_ci#define LTC2983_STATUS_START(x) FIELD_PREP(LTC2983_STATUS_START_MASK, x) 928c2ecf20Sopenharmony_ci#define LTC2983_STATUS_UP_MASK GENMASK(7, 6) 938c2ecf20Sopenharmony_ci#define LTC2983_STATUS_UP(reg) FIELD_GET(LTC2983_STATUS_UP_MASK, reg) 948c2ecf20Sopenharmony_ci 958c2ecf20Sopenharmony_ci#define LTC2983_STATUS_CHAN_SEL_MASK GENMASK(4, 0) 968c2ecf20Sopenharmony_ci#define LTC2983_STATUS_CHAN_SEL(x) \ 978c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_STATUS_CHAN_SEL_MASK, x) 988c2ecf20Sopenharmony_ci 998c2ecf20Sopenharmony_ci#define LTC2983_TEMP_UNITS_MASK BIT(2) 1008c2ecf20Sopenharmony_ci#define LTC2983_TEMP_UNITS(x) FIELD_PREP(LTC2983_TEMP_UNITS_MASK, x) 1018c2ecf20Sopenharmony_ci 1028c2ecf20Sopenharmony_ci#define LTC2983_NOTCH_FREQ_MASK GENMASK(1, 0) 1038c2ecf20Sopenharmony_ci#define LTC2983_NOTCH_FREQ(x) FIELD_PREP(LTC2983_NOTCH_FREQ_MASK, x) 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci#define LTC2983_RES_VALID_MASK BIT(24) 1068c2ecf20Sopenharmony_ci#define LTC2983_DATA_MASK GENMASK(23, 0) 1078c2ecf20Sopenharmony_ci#define LTC2983_DATA_SIGN_BIT 23 1088c2ecf20Sopenharmony_ci 1098c2ecf20Sopenharmony_ci#define LTC2983_CHAN_TYPE_MASK GENMASK(31, 27) 1108c2ecf20Sopenharmony_ci#define LTC2983_CHAN_TYPE(x) FIELD_PREP(LTC2983_CHAN_TYPE_MASK, x) 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci/* cold junction for thermocouples and rsense for rtd's and thermistor's */ 1138c2ecf20Sopenharmony_ci#define LTC2983_CHAN_ASSIGN_MASK GENMASK(26, 22) 1148c2ecf20Sopenharmony_ci#define LTC2983_CHAN_ASSIGN(x) FIELD_PREP(LTC2983_CHAN_ASSIGN_MASK, x) 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci#define LTC2983_CUSTOM_LEN_MASK GENMASK(5, 0) 1178c2ecf20Sopenharmony_ci#define LTC2983_CUSTOM_LEN(x) FIELD_PREP(LTC2983_CUSTOM_LEN_MASK, x) 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci#define LTC2983_CUSTOM_ADDR_MASK GENMASK(11, 6) 1208c2ecf20Sopenharmony_ci#define LTC2983_CUSTOM_ADDR(x) FIELD_PREP(LTC2983_CUSTOM_ADDR_MASK, x) 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_CFG_MASK GENMASK(21, 18) 1238c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_CFG(x) \ 1248c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMOCOUPLE_CFG_MASK, x) 1258c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_HARD_FAULT_MASK GENMASK(31, 29) 1268c2ecf20Sopenharmony_ci#define LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK GENMASK(28, 25) 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci#define LTC2983_RTD_CFG_MASK GENMASK(21, 18) 1298c2ecf20Sopenharmony_ci#define LTC2983_RTD_CFG(x) FIELD_PREP(LTC2983_RTD_CFG_MASK, x) 1308c2ecf20Sopenharmony_ci#define LTC2983_RTD_EXC_CURRENT_MASK GENMASK(17, 14) 1318c2ecf20Sopenharmony_ci#define LTC2983_RTD_EXC_CURRENT(x) \ 1328c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_RTD_EXC_CURRENT_MASK, x) 1338c2ecf20Sopenharmony_ci#define LTC2983_RTD_CURVE_MASK GENMASK(13, 12) 1348c2ecf20Sopenharmony_ci#define LTC2983_RTD_CURVE(x) FIELD_PREP(LTC2983_RTD_CURVE_MASK, x) 1358c2ecf20Sopenharmony_ci 1368c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_CFG_MASK GENMASK(21, 19) 1378c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_CFG(x) \ 1388c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMISTOR_CFG_MASK, x) 1398c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_EXC_CURRENT_MASK GENMASK(18, 15) 1408c2ecf20Sopenharmony_ci#define LTC2983_THERMISTOR_EXC_CURRENT(x) \ 1418c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_THERMISTOR_EXC_CURRENT_MASK, x) 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci#define LTC2983_DIODE_CFG_MASK GENMASK(26, 24) 1448c2ecf20Sopenharmony_ci#define LTC2983_DIODE_CFG(x) FIELD_PREP(LTC2983_DIODE_CFG_MASK, x) 1458c2ecf20Sopenharmony_ci#define LTC2983_DIODE_EXC_CURRENT_MASK GENMASK(23, 22) 1468c2ecf20Sopenharmony_ci#define LTC2983_DIODE_EXC_CURRENT(x) \ 1478c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_DIODE_EXC_CURRENT_MASK, x) 1488c2ecf20Sopenharmony_ci#define LTC2983_DIODE_IDEAL_FACTOR_MASK GENMASK(21, 0) 1498c2ecf20Sopenharmony_ci#define LTC2983_DIODE_IDEAL_FACTOR(x) \ 1508c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_DIODE_IDEAL_FACTOR_MASK, x) 1518c2ecf20Sopenharmony_ci 1528c2ecf20Sopenharmony_ci#define LTC2983_R_SENSE_VAL_MASK GENMASK(26, 0) 1538c2ecf20Sopenharmony_ci#define LTC2983_R_SENSE_VAL(x) FIELD_PREP(LTC2983_R_SENSE_VAL_MASK, x) 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci#define LTC2983_ADC_SINGLE_ENDED_MASK BIT(26) 1568c2ecf20Sopenharmony_ci#define LTC2983_ADC_SINGLE_ENDED(x) \ 1578c2ecf20Sopenharmony_ci FIELD_PREP(LTC2983_ADC_SINGLE_ENDED_MASK, x) 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cienum { 1608c2ecf20Sopenharmony_ci LTC2983_SENSOR_THERMOCOUPLE = 1, 1618c2ecf20Sopenharmony_ci LTC2983_SENSOR_THERMOCOUPLE_CUSTOM = 9, 1628c2ecf20Sopenharmony_ci LTC2983_SENSOR_RTD = 10, 1638c2ecf20Sopenharmony_ci LTC2983_SENSOR_RTD_CUSTOM = 18, 1648c2ecf20Sopenharmony_ci LTC2983_SENSOR_THERMISTOR = 19, 1658c2ecf20Sopenharmony_ci LTC2983_SENSOR_THERMISTOR_STEINHART = 26, 1668c2ecf20Sopenharmony_ci LTC2983_SENSOR_THERMISTOR_CUSTOM = 27, 1678c2ecf20Sopenharmony_ci LTC2983_SENSOR_DIODE = 28, 1688c2ecf20Sopenharmony_ci LTC2983_SENSOR_SENSE_RESISTOR = 29, 1698c2ecf20Sopenharmony_ci LTC2983_SENSOR_DIRECT_ADC = 30, 1708c2ecf20Sopenharmony_ci}; 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci#define to_thermocouple(_sensor) \ 1738c2ecf20Sopenharmony_ci container_of(_sensor, struct ltc2983_thermocouple, sensor) 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci#define to_rtd(_sensor) \ 1768c2ecf20Sopenharmony_ci container_of(_sensor, struct ltc2983_rtd, sensor) 1778c2ecf20Sopenharmony_ci 1788c2ecf20Sopenharmony_ci#define to_thermistor(_sensor) \ 1798c2ecf20Sopenharmony_ci container_of(_sensor, struct ltc2983_thermistor, sensor) 1808c2ecf20Sopenharmony_ci 1818c2ecf20Sopenharmony_ci#define to_diode(_sensor) \ 1828c2ecf20Sopenharmony_ci container_of(_sensor, struct ltc2983_diode, sensor) 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci#define to_rsense(_sensor) \ 1858c2ecf20Sopenharmony_ci container_of(_sensor, struct ltc2983_rsense, sensor) 1868c2ecf20Sopenharmony_ci 1878c2ecf20Sopenharmony_ci#define to_adc(_sensor) \ 1888c2ecf20Sopenharmony_ci container_of(_sensor, struct ltc2983_adc, sensor) 1898c2ecf20Sopenharmony_ci 1908c2ecf20Sopenharmony_cistruct ltc2983_data { 1918c2ecf20Sopenharmony_ci struct regmap *regmap; 1928c2ecf20Sopenharmony_ci struct spi_device *spi; 1938c2ecf20Sopenharmony_ci struct mutex lock; 1948c2ecf20Sopenharmony_ci struct completion completion; 1958c2ecf20Sopenharmony_ci struct iio_chan_spec *iio_chan; 1968c2ecf20Sopenharmony_ci struct ltc2983_sensor **sensors; 1978c2ecf20Sopenharmony_ci u32 mux_delay_config; 1988c2ecf20Sopenharmony_ci u32 filter_notch_freq; 1998c2ecf20Sopenharmony_ci u16 custom_table_size; 2008c2ecf20Sopenharmony_ci u8 num_channels; 2018c2ecf20Sopenharmony_ci u8 iio_channels; 2028c2ecf20Sopenharmony_ci /* 2038c2ecf20Sopenharmony_ci * DMA (thus cache coherency maintenance) requires the 2048c2ecf20Sopenharmony_ci * transfer buffers to live in their own cache lines. 2058c2ecf20Sopenharmony_ci * Holds the converted temperature 2068c2ecf20Sopenharmony_ci */ 2078c2ecf20Sopenharmony_ci __be32 temp ____cacheline_aligned; 2088c2ecf20Sopenharmony_ci __be32 chan_val; 2098c2ecf20Sopenharmony_ci}; 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_cistruct ltc2983_sensor { 2128c2ecf20Sopenharmony_ci int (*fault_handler)(const struct ltc2983_data *st, const u32 result); 2138c2ecf20Sopenharmony_ci int (*assign_chan)(struct ltc2983_data *st, 2148c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor); 2158c2ecf20Sopenharmony_ci /* specifies the sensor channel */ 2168c2ecf20Sopenharmony_ci u32 chan; 2178c2ecf20Sopenharmony_ci /* sensor type */ 2188c2ecf20Sopenharmony_ci u32 type; 2198c2ecf20Sopenharmony_ci}; 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_cistruct ltc2983_custom_sensor { 2228c2ecf20Sopenharmony_ci /* raw table sensor data */ 2238c2ecf20Sopenharmony_ci u8 *table; 2248c2ecf20Sopenharmony_ci size_t size; 2258c2ecf20Sopenharmony_ci /* address offset */ 2268c2ecf20Sopenharmony_ci s8 offset; 2278c2ecf20Sopenharmony_ci bool is_steinhart; 2288c2ecf20Sopenharmony_ci}; 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_cistruct ltc2983_thermocouple { 2318c2ecf20Sopenharmony_ci struct ltc2983_sensor sensor; 2328c2ecf20Sopenharmony_ci struct ltc2983_custom_sensor *custom; 2338c2ecf20Sopenharmony_ci u32 sensor_config; 2348c2ecf20Sopenharmony_ci u32 cold_junction_chan; 2358c2ecf20Sopenharmony_ci}; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_cistruct ltc2983_rtd { 2388c2ecf20Sopenharmony_ci struct ltc2983_sensor sensor; 2398c2ecf20Sopenharmony_ci struct ltc2983_custom_sensor *custom; 2408c2ecf20Sopenharmony_ci u32 sensor_config; 2418c2ecf20Sopenharmony_ci u32 r_sense_chan; 2428c2ecf20Sopenharmony_ci u32 excitation_current; 2438c2ecf20Sopenharmony_ci u32 rtd_curve; 2448c2ecf20Sopenharmony_ci}; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistruct ltc2983_thermistor { 2478c2ecf20Sopenharmony_ci struct ltc2983_sensor sensor; 2488c2ecf20Sopenharmony_ci struct ltc2983_custom_sensor *custom; 2498c2ecf20Sopenharmony_ci u32 sensor_config; 2508c2ecf20Sopenharmony_ci u32 r_sense_chan; 2518c2ecf20Sopenharmony_ci u32 excitation_current; 2528c2ecf20Sopenharmony_ci}; 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_cistruct ltc2983_diode { 2558c2ecf20Sopenharmony_ci struct ltc2983_sensor sensor; 2568c2ecf20Sopenharmony_ci u32 sensor_config; 2578c2ecf20Sopenharmony_ci u32 excitation_current; 2588c2ecf20Sopenharmony_ci u32 ideal_factor_value; 2598c2ecf20Sopenharmony_ci}; 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistruct ltc2983_rsense { 2628c2ecf20Sopenharmony_ci struct ltc2983_sensor sensor; 2638c2ecf20Sopenharmony_ci u32 r_sense_val; 2648c2ecf20Sopenharmony_ci}; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_cistruct ltc2983_adc { 2678c2ecf20Sopenharmony_ci struct ltc2983_sensor sensor; 2688c2ecf20Sopenharmony_ci bool single_ended; 2698c2ecf20Sopenharmony_ci}; 2708c2ecf20Sopenharmony_ci 2718c2ecf20Sopenharmony_ci/* 2728c2ecf20Sopenharmony_ci * Convert to Q format numbers. These number's are integers where 2738c2ecf20Sopenharmony_ci * the number of integer and fractional bits are specified. The resolution 2748c2ecf20Sopenharmony_ci * is given by 1/@resolution and tell us the number of fractional bits. For 2758c2ecf20Sopenharmony_ci * instance a resolution of 2^-10 means we have 10 fractional bits. 2768c2ecf20Sopenharmony_ci */ 2778c2ecf20Sopenharmony_cistatic u32 __convert_to_raw(const u64 val, const u32 resolution) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci u64 __res = val * resolution; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci /* all values are multiplied by 1000000 to remove the fraction */ 2828c2ecf20Sopenharmony_ci do_div(__res, 1000000); 2838c2ecf20Sopenharmony_ci 2848c2ecf20Sopenharmony_ci return __res; 2858c2ecf20Sopenharmony_ci} 2868c2ecf20Sopenharmony_ci 2878c2ecf20Sopenharmony_cistatic u32 __convert_to_raw_sign(const u64 val, const u32 resolution) 2888c2ecf20Sopenharmony_ci{ 2898c2ecf20Sopenharmony_ci s64 __res = -(s32)val; 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_ci __res = __convert_to_raw(__res, resolution); 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_ci return (u32)-__res; 2948c2ecf20Sopenharmony_ci} 2958c2ecf20Sopenharmony_ci 2968c2ecf20Sopenharmony_cistatic int __ltc2983_fault_handler(const struct ltc2983_data *st, 2978c2ecf20Sopenharmony_ci const u32 result, const u32 hard_mask, 2988c2ecf20Sopenharmony_ci const u32 soft_mask) 2998c2ecf20Sopenharmony_ci{ 3008c2ecf20Sopenharmony_ci const struct device *dev = &st->spi->dev; 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci if (result & hard_mask) { 3038c2ecf20Sopenharmony_ci dev_err(dev, "Invalid conversion: Sensor HARD fault\n"); 3048c2ecf20Sopenharmony_ci return -EIO; 3058c2ecf20Sopenharmony_ci } else if (result & soft_mask) { 3068c2ecf20Sopenharmony_ci /* just print a warning */ 3078c2ecf20Sopenharmony_ci dev_warn(dev, "Suspicious conversion: Sensor SOFT fault\n"); 3088c2ecf20Sopenharmony_ci } 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_ci return 0; 3118c2ecf20Sopenharmony_ci} 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_cistatic int __ltc2983_chan_assign_common(struct ltc2983_data *st, 3148c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor, 3158c2ecf20Sopenharmony_ci u32 chan_val) 3168c2ecf20Sopenharmony_ci{ 3178c2ecf20Sopenharmony_ci u32 reg = LTC2983_CHAN_START_ADDR(sensor->chan); 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci chan_val |= LTC2983_CHAN_TYPE(sensor->type); 3208c2ecf20Sopenharmony_ci dev_dbg(&st->spi->dev, "Assign reg:0x%04X, val:0x%08X\n", reg, 3218c2ecf20Sopenharmony_ci chan_val); 3228c2ecf20Sopenharmony_ci st->chan_val = cpu_to_be32(chan_val); 3238c2ecf20Sopenharmony_ci return regmap_bulk_write(st->regmap, reg, &st->chan_val, 3248c2ecf20Sopenharmony_ci sizeof(st->chan_val)); 3258c2ecf20Sopenharmony_ci} 3268c2ecf20Sopenharmony_ci 3278c2ecf20Sopenharmony_cistatic int __ltc2983_chan_custom_sensor_assign(struct ltc2983_data *st, 3288c2ecf20Sopenharmony_ci struct ltc2983_custom_sensor *custom, 3298c2ecf20Sopenharmony_ci u32 *chan_val) 3308c2ecf20Sopenharmony_ci{ 3318c2ecf20Sopenharmony_ci u32 reg; 3328c2ecf20Sopenharmony_ci u8 mult = custom->is_steinhart ? LTC2983_CUSTOM_STEINHART_ENTRY_SZ : 3338c2ecf20Sopenharmony_ci LTC2983_CUSTOM_SENSOR_ENTRY_SZ; 3348c2ecf20Sopenharmony_ci const struct device *dev = &st->spi->dev; 3358c2ecf20Sopenharmony_ci /* 3368c2ecf20Sopenharmony_ci * custom->size holds the raw size of the table. However, when 3378c2ecf20Sopenharmony_ci * configuring the sensor channel, we must write the number of 3388c2ecf20Sopenharmony_ci * entries of the table minus 1. For steinhart sensors 0 is written 3398c2ecf20Sopenharmony_ci * since the size is constant! 3408c2ecf20Sopenharmony_ci */ 3418c2ecf20Sopenharmony_ci const u8 len = custom->is_steinhart ? 0 : 3428c2ecf20Sopenharmony_ci (custom->size / LTC2983_CUSTOM_SENSOR_ENTRY_SZ) - 1; 3438c2ecf20Sopenharmony_ci /* 3448c2ecf20Sopenharmony_ci * Check if the offset was assigned already. It should be for steinhart 3458c2ecf20Sopenharmony_ci * sensors. When coming from sleep, it should be assigned for all. 3468c2ecf20Sopenharmony_ci */ 3478c2ecf20Sopenharmony_ci if (custom->offset < 0) { 3488c2ecf20Sopenharmony_ci /* 3498c2ecf20Sopenharmony_ci * This needs to be done again here because, from the moment 3508c2ecf20Sopenharmony_ci * when this test was done (successfully) for this custom 3518c2ecf20Sopenharmony_ci * sensor, a steinhart sensor might have been added changing 3528c2ecf20Sopenharmony_ci * custom_table_size... 3538c2ecf20Sopenharmony_ci */ 3548c2ecf20Sopenharmony_ci if (st->custom_table_size + custom->size > 3558c2ecf20Sopenharmony_ci (LTC2983_CUST_SENS_TBL_END_REG - 3568c2ecf20Sopenharmony_ci LTC2983_CUST_SENS_TBL_START_REG) + 1) { 3578c2ecf20Sopenharmony_ci dev_err(dev, 3588c2ecf20Sopenharmony_ci "Not space left(%d) for new custom sensor(%zu)", 3598c2ecf20Sopenharmony_ci st->custom_table_size, 3608c2ecf20Sopenharmony_ci custom->size); 3618c2ecf20Sopenharmony_ci return -EINVAL; 3628c2ecf20Sopenharmony_ci } 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci custom->offset = st->custom_table_size / 3658c2ecf20Sopenharmony_ci LTC2983_CUSTOM_SENSOR_ENTRY_SZ; 3668c2ecf20Sopenharmony_ci st->custom_table_size += custom->size; 3678c2ecf20Sopenharmony_ci } 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci reg = (custom->offset * mult) + LTC2983_CUST_SENS_TBL_START_REG; 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci *chan_val |= LTC2983_CUSTOM_LEN(len); 3728c2ecf20Sopenharmony_ci *chan_val |= LTC2983_CUSTOM_ADDR(custom->offset); 3738c2ecf20Sopenharmony_ci dev_dbg(dev, "Assign custom sensor, reg:0x%04X, off:%d, sz:%zu", 3748c2ecf20Sopenharmony_ci reg, custom->offset, 3758c2ecf20Sopenharmony_ci custom->size); 3768c2ecf20Sopenharmony_ci /* write custom sensor table */ 3778c2ecf20Sopenharmony_ci return regmap_bulk_write(st->regmap, reg, custom->table, custom->size); 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_cistatic struct ltc2983_custom_sensor *__ltc2983_custom_sensor_new( 3818c2ecf20Sopenharmony_ci struct ltc2983_data *st, 3828c2ecf20Sopenharmony_ci const struct device_node *np, 3838c2ecf20Sopenharmony_ci const char *propname, 3848c2ecf20Sopenharmony_ci const bool is_steinhart, 3858c2ecf20Sopenharmony_ci const u32 resolution, 3868c2ecf20Sopenharmony_ci const bool has_signed) 3878c2ecf20Sopenharmony_ci{ 3888c2ecf20Sopenharmony_ci struct ltc2983_custom_sensor *new_custom; 3898c2ecf20Sopenharmony_ci u8 index, n_entries, tbl = 0; 3908c2ecf20Sopenharmony_ci struct device *dev = &st->spi->dev; 3918c2ecf20Sopenharmony_ci /* 3928c2ecf20Sopenharmony_ci * For custom steinhart, the full u32 is taken. For all the others 3938c2ecf20Sopenharmony_ci * the MSB is discarded. 3948c2ecf20Sopenharmony_ci */ 3958c2ecf20Sopenharmony_ci const u8 n_size = is_steinhart ? 4 : 3; 3968c2ecf20Sopenharmony_ci const u8 e_size = is_steinhart ? sizeof(u32) : sizeof(u64); 3978c2ecf20Sopenharmony_ci 3988c2ecf20Sopenharmony_ci n_entries = of_property_count_elems_of_size(np, propname, e_size); 3998c2ecf20Sopenharmony_ci /* n_entries must be an even number */ 4008c2ecf20Sopenharmony_ci if (!n_entries || (n_entries % 2) != 0) { 4018c2ecf20Sopenharmony_ci dev_err(dev, "Number of entries either 0 or not even\n"); 4028c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 4038c2ecf20Sopenharmony_ci } 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci new_custom = devm_kzalloc(dev, sizeof(*new_custom), GFP_KERNEL); 4068c2ecf20Sopenharmony_ci if (!new_custom) 4078c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4088c2ecf20Sopenharmony_ci 4098c2ecf20Sopenharmony_ci new_custom->size = n_entries * n_size; 4108c2ecf20Sopenharmony_ci /* check Steinhart size */ 4118c2ecf20Sopenharmony_ci if (is_steinhart && new_custom->size != LTC2983_CUSTOM_STEINHART_SIZE) { 4128c2ecf20Sopenharmony_ci dev_err(dev, "Steinhart sensors size(%zu) must be 24", 4138c2ecf20Sopenharmony_ci new_custom->size); 4148c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 4158c2ecf20Sopenharmony_ci } 4168c2ecf20Sopenharmony_ci /* Check space on the table. */ 4178c2ecf20Sopenharmony_ci if (st->custom_table_size + new_custom->size > 4188c2ecf20Sopenharmony_ci (LTC2983_CUST_SENS_TBL_END_REG - 4198c2ecf20Sopenharmony_ci LTC2983_CUST_SENS_TBL_START_REG) + 1) { 4208c2ecf20Sopenharmony_ci dev_err(dev, "No space left(%d) for new custom sensor(%zu)", 4218c2ecf20Sopenharmony_ci st->custom_table_size, new_custom->size); 4228c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 4238c2ecf20Sopenharmony_ci } 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_ci /* allocate the table */ 4268c2ecf20Sopenharmony_ci new_custom->table = devm_kzalloc(dev, new_custom->size, GFP_KERNEL); 4278c2ecf20Sopenharmony_ci if (!new_custom->table) 4288c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ci for (index = 0; index < n_entries; index++) { 4318c2ecf20Sopenharmony_ci u64 temp = 0, j; 4328c2ecf20Sopenharmony_ci /* 4338c2ecf20Sopenharmony_ci * Steinhart sensors are configured with raw values in the 4348c2ecf20Sopenharmony_ci * devicetree. For the other sensors we must convert the 4358c2ecf20Sopenharmony_ci * value to raw. The odd index's correspond to temperarures 4368c2ecf20Sopenharmony_ci * and always have 1/1024 of resolution. Temperatures also 4378c2ecf20Sopenharmony_ci * come in kelvin, so signed values is not possible 4388c2ecf20Sopenharmony_ci */ 4398c2ecf20Sopenharmony_ci if (!is_steinhart) { 4408c2ecf20Sopenharmony_ci of_property_read_u64_index(np, propname, index, &temp); 4418c2ecf20Sopenharmony_ci 4428c2ecf20Sopenharmony_ci if ((index % 2) != 0) 4438c2ecf20Sopenharmony_ci temp = __convert_to_raw(temp, 1024); 4448c2ecf20Sopenharmony_ci else if (has_signed && (s64)temp < 0) 4458c2ecf20Sopenharmony_ci temp = __convert_to_raw_sign(temp, resolution); 4468c2ecf20Sopenharmony_ci else 4478c2ecf20Sopenharmony_ci temp = __convert_to_raw(temp, resolution); 4488c2ecf20Sopenharmony_ci } else { 4498c2ecf20Sopenharmony_ci u32 t32; 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci of_property_read_u32_index(np, propname, index, &t32); 4528c2ecf20Sopenharmony_ci temp = t32; 4538c2ecf20Sopenharmony_ci } 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci for (j = 0; j < n_size; j++) 4568c2ecf20Sopenharmony_ci new_custom->table[tbl++] = 4578c2ecf20Sopenharmony_ci temp >> (8 * (n_size - j - 1)); 4588c2ecf20Sopenharmony_ci } 4598c2ecf20Sopenharmony_ci 4608c2ecf20Sopenharmony_ci new_custom->is_steinhart = is_steinhart; 4618c2ecf20Sopenharmony_ci /* 4628c2ecf20Sopenharmony_ci * This is done to first add all the steinhart sensors to the table, 4638c2ecf20Sopenharmony_ci * in order to maximize the table usage. If we mix adding steinhart 4648c2ecf20Sopenharmony_ci * with the other sensors, we might have to do some roundup to make 4658c2ecf20Sopenharmony_ci * sure that sensor_addr - 0x250(start address) is a multiple of 4 4668c2ecf20Sopenharmony_ci * (for steinhart), and a multiple of 6 for all the other sensors. 4678c2ecf20Sopenharmony_ci * Since we have const 24 bytes for steinhart sensors and 24 is 4688c2ecf20Sopenharmony_ci * also a multiple of 6, we guarantee that the first non-steinhart 4698c2ecf20Sopenharmony_ci * sensor will sit in a correct address without the need of filling 4708c2ecf20Sopenharmony_ci * addresses. 4718c2ecf20Sopenharmony_ci */ 4728c2ecf20Sopenharmony_ci if (is_steinhart) { 4738c2ecf20Sopenharmony_ci new_custom->offset = st->custom_table_size / 4748c2ecf20Sopenharmony_ci LTC2983_CUSTOM_STEINHART_ENTRY_SZ; 4758c2ecf20Sopenharmony_ci st->custom_table_size += new_custom->size; 4768c2ecf20Sopenharmony_ci } else { 4778c2ecf20Sopenharmony_ci /* mark as unset. This is checked later on the assign phase */ 4788c2ecf20Sopenharmony_ci new_custom->offset = -1; 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci return new_custom; 4828c2ecf20Sopenharmony_ci} 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistatic int ltc2983_thermocouple_fault_handler(const struct ltc2983_data *st, 4858c2ecf20Sopenharmony_ci const u32 result) 4868c2ecf20Sopenharmony_ci{ 4878c2ecf20Sopenharmony_ci return __ltc2983_fault_handler(st, result, 4888c2ecf20Sopenharmony_ci LTC2983_THERMOCOUPLE_HARD_FAULT_MASK, 4898c2ecf20Sopenharmony_ci LTC2983_THERMOCOUPLE_SOFT_FAULT_MASK); 4908c2ecf20Sopenharmony_ci} 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic int ltc2983_common_fault_handler(const struct ltc2983_data *st, 4938c2ecf20Sopenharmony_ci const u32 result) 4948c2ecf20Sopenharmony_ci{ 4958c2ecf20Sopenharmony_ci return __ltc2983_fault_handler(st, result, 4968c2ecf20Sopenharmony_ci LTC2983_COMMON_HARD_FAULT_MASK, 4978c2ecf20Sopenharmony_ci LTC2983_COMMON_SOFT_FAULT_MASK); 4988c2ecf20Sopenharmony_ci} 4998c2ecf20Sopenharmony_ci 5008c2ecf20Sopenharmony_cistatic int ltc2983_thermocouple_assign_chan(struct ltc2983_data *st, 5018c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 5028c2ecf20Sopenharmony_ci{ 5038c2ecf20Sopenharmony_ci struct ltc2983_thermocouple *thermo = to_thermocouple(sensor); 5048c2ecf20Sopenharmony_ci u32 chan_val; 5058c2ecf20Sopenharmony_ci 5068c2ecf20Sopenharmony_ci chan_val = LTC2983_CHAN_ASSIGN(thermo->cold_junction_chan); 5078c2ecf20Sopenharmony_ci chan_val |= LTC2983_THERMOCOUPLE_CFG(thermo->sensor_config); 5088c2ecf20Sopenharmony_ci 5098c2ecf20Sopenharmony_ci if (thermo->custom) { 5108c2ecf20Sopenharmony_ci int ret; 5118c2ecf20Sopenharmony_ci 5128c2ecf20Sopenharmony_ci ret = __ltc2983_chan_custom_sensor_assign(st, thermo->custom, 5138c2ecf20Sopenharmony_ci &chan_val); 5148c2ecf20Sopenharmony_ci if (ret) 5158c2ecf20Sopenharmony_ci return ret; 5168c2ecf20Sopenharmony_ci } 5178c2ecf20Sopenharmony_ci return __ltc2983_chan_assign_common(st, sensor, chan_val); 5188c2ecf20Sopenharmony_ci} 5198c2ecf20Sopenharmony_ci 5208c2ecf20Sopenharmony_cistatic int ltc2983_rtd_assign_chan(struct ltc2983_data *st, 5218c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 5228c2ecf20Sopenharmony_ci{ 5238c2ecf20Sopenharmony_ci struct ltc2983_rtd *rtd = to_rtd(sensor); 5248c2ecf20Sopenharmony_ci u32 chan_val; 5258c2ecf20Sopenharmony_ci 5268c2ecf20Sopenharmony_ci chan_val = LTC2983_CHAN_ASSIGN(rtd->r_sense_chan); 5278c2ecf20Sopenharmony_ci chan_val |= LTC2983_RTD_CFG(rtd->sensor_config); 5288c2ecf20Sopenharmony_ci chan_val |= LTC2983_RTD_EXC_CURRENT(rtd->excitation_current); 5298c2ecf20Sopenharmony_ci chan_val |= LTC2983_RTD_CURVE(rtd->rtd_curve); 5308c2ecf20Sopenharmony_ci 5318c2ecf20Sopenharmony_ci if (rtd->custom) { 5328c2ecf20Sopenharmony_ci int ret; 5338c2ecf20Sopenharmony_ci 5348c2ecf20Sopenharmony_ci ret = __ltc2983_chan_custom_sensor_assign(st, rtd->custom, 5358c2ecf20Sopenharmony_ci &chan_val); 5368c2ecf20Sopenharmony_ci if (ret) 5378c2ecf20Sopenharmony_ci return ret; 5388c2ecf20Sopenharmony_ci } 5398c2ecf20Sopenharmony_ci return __ltc2983_chan_assign_common(st, sensor, chan_val); 5408c2ecf20Sopenharmony_ci} 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_cistatic int ltc2983_thermistor_assign_chan(struct ltc2983_data *st, 5438c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 5448c2ecf20Sopenharmony_ci{ 5458c2ecf20Sopenharmony_ci struct ltc2983_thermistor *thermistor = to_thermistor(sensor); 5468c2ecf20Sopenharmony_ci u32 chan_val; 5478c2ecf20Sopenharmony_ci 5488c2ecf20Sopenharmony_ci chan_val = LTC2983_CHAN_ASSIGN(thermistor->r_sense_chan); 5498c2ecf20Sopenharmony_ci chan_val |= LTC2983_THERMISTOR_CFG(thermistor->sensor_config); 5508c2ecf20Sopenharmony_ci chan_val |= 5518c2ecf20Sopenharmony_ci LTC2983_THERMISTOR_EXC_CURRENT(thermistor->excitation_current); 5528c2ecf20Sopenharmony_ci 5538c2ecf20Sopenharmony_ci if (thermistor->custom) { 5548c2ecf20Sopenharmony_ci int ret; 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_ci ret = __ltc2983_chan_custom_sensor_assign(st, 5578c2ecf20Sopenharmony_ci thermistor->custom, 5588c2ecf20Sopenharmony_ci &chan_val); 5598c2ecf20Sopenharmony_ci if (ret) 5608c2ecf20Sopenharmony_ci return ret; 5618c2ecf20Sopenharmony_ci } 5628c2ecf20Sopenharmony_ci return __ltc2983_chan_assign_common(st, sensor, chan_val); 5638c2ecf20Sopenharmony_ci} 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_cistatic int ltc2983_diode_assign_chan(struct ltc2983_data *st, 5668c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 5678c2ecf20Sopenharmony_ci{ 5688c2ecf20Sopenharmony_ci struct ltc2983_diode *diode = to_diode(sensor); 5698c2ecf20Sopenharmony_ci u32 chan_val; 5708c2ecf20Sopenharmony_ci 5718c2ecf20Sopenharmony_ci chan_val = LTC2983_DIODE_CFG(diode->sensor_config); 5728c2ecf20Sopenharmony_ci chan_val |= LTC2983_DIODE_EXC_CURRENT(diode->excitation_current); 5738c2ecf20Sopenharmony_ci chan_val |= LTC2983_DIODE_IDEAL_FACTOR(diode->ideal_factor_value); 5748c2ecf20Sopenharmony_ci 5758c2ecf20Sopenharmony_ci return __ltc2983_chan_assign_common(st, sensor, chan_val); 5768c2ecf20Sopenharmony_ci} 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_cistatic int ltc2983_r_sense_assign_chan(struct ltc2983_data *st, 5798c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 5808c2ecf20Sopenharmony_ci{ 5818c2ecf20Sopenharmony_ci struct ltc2983_rsense *rsense = to_rsense(sensor); 5828c2ecf20Sopenharmony_ci u32 chan_val; 5838c2ecf20Sopenharmony_ci 5848c2ecf20Sopenharmony_ci chan_val = LTC2983_R_SENSE_VAL(rsense->r_sense_val); 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci return __ltc2983_chan_assign_common(st, sensor, chan_val); 5878c2ecf20Sopenharmony_ci} 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_cistatic int ltc2983_adc_assign_chan(struct ltc2983_data *st, 5908c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 5918c2ecf20Sopenharmony_ci{ 5928c2ecf20Sopenharmony_ci struct ltc2983_adc *adc = to_adc(sensor); 5938c2ecf20Sopenharmony_ci u32 chan_val; 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci chan_val = LTC2983_ADC_SINGLE_ENDED(adc->single_ended); 5968c2ecf20Sopenharmony_ci 5978c2ecf20Sopenharmony_ci return __ltc2983_chan_assign_common(st, sensor, chan_val); 5988c2ecf20Sopenharmony_ci} 5998c2ecf20Sopenharmony_ci 6008c2ecf20Sopenharmony_cistatic struct ltc2983_sensor *ltc2983_thermocouple_new( 6018c2ecf20Sopenharmony_ci const struct device_node *child, 6028c2ecf20Sopenharmony_ci struct ltc2983_data *st, 6038c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 6048c2ecf20Sopenharmony_ci{ 6058c2ecf20Sopenharmony_ci struct ltc2983_thermocouple *thermo; 6068c2ecf20Sopenharmony_ci struct device_node *phandle; 6078c2ecf20Sopenharmony_ci u32 oc_current; 6088c2ecf20Sopenharmony_ci int ret; 6098c2ecf20Sopenharmony_ci 6108c2ecf20Sopenharmony_ci thermo = devm_kzalloc(&st->spi->dev, sizeof(*thermo), GFP_KERNEL); 6118c2ecf20Sopenharmony_ci if (!thermo) 6128c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 6138c2ecf20Sopenharmony_ci 6148c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,single-ended")) 6158c2ecf20Sopenharmony_ci thermo->sensor_config = LTC2983_THERMOCOUPLE_SGL(1); 6168c2ecf20Sopenharmony_ci 6178c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "adi,sensor-oc-current-microamp", 6188c2ecf20Sopenharmony_ci &oc_current); 6198c2ecf20Sopenharmony_ci if (!ret) { 6208c2ecf20Sopenharmony_ci switch (oc_current) { 6218c2ecf20Sopenharmony_ci case 10: 6228c2ecf20Sopenharmony_ci thermo->sensor_config |= 6238c2ecf20Sopenharmony_ci LTC2983_THERMOCOUPLE_OC_CURR(0); 6248c2ecf20Sopenharmony_ci break; 6258c2ecf20Sopenharmony_ci case 100: 6268c2ecf20Sopenharmony_ci thermo->sensor_config |= 6278c2ecf20Sopenharmony_ci LTC2983_THERMOCOUPLE_OC_CURR(1); 6288c2ecf20Sopenharmony_ci break; 6298c2ecf20Sopenharmony_ci case 500: 6308c2ecf20Sopenharmony_ci thermo->sensor_config |= 6318c2ecf20Sopenharmony_ci LTC2983_THERMOCOUPLE_OC_CURR(2); 6328c2ecf20Sopenharmony_ci break; 6338c2ecf20Sopenharmony_ci case 1000: 6348c2ecf20Sopenharmony_ci thermo->sensor_config |= 6358c2ecf20Sopenharmony_ci LTC2983_THERMOCOUPLE_OC_CURR(3); 6368c2ecf20Sopenharmony_ci break; 6378c2ecf20Sopenharmony_ci default: 6388c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 6398c2ecf20Sopenharmony_ci "Invalid open circuit current:%u", oc_current); 6408c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 6418c2ecf20Sopenharmony_ci } 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_ci thermo->sensor_config |= LTC2983_THERMOCOUPLE_OC_CHECK(1); 6448c2ecf20Sopenharmony_ci } 6458c2ecf20Sopenharmony_ci /* validate channel index */ 6468c2ecf20Sopenharmony_ci if (!(thermo->sensor_config & LTC2983_THERMOCOUPLE_DIFF_MASK) && 6478c2ecf20Sopenharmony_ci sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { 6488c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 6498c2ecf20Sopenharmony_ci "Invalid chann:%d for differential thermocouple", 6508c2ecf20Sopenharmony_ci sensor->chan); 6518c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 6528c2ecf20Sopenharmony_ci } 6538c2ecf20Sopenharmony_ci 6548c2ecf20Sopenharmony_ci phandle = of_parse_phandle(child, "adi,cold-junction-handle", 0); 6558c2ecf20Sopenharmony_ci if (phandle) { 6568c2ecf20Sopenharmony_ci int ret; 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_ci ret = of_property_read_u32(phandle, "reg", 6598c2ecf20Sopenharmony_ci &thermo->cold_junction_chan); 6608c2ecf20Sopenharmony_ci if (ret) { 6618c2ecf20Sopenharmony_ci /* 6628c2ecf20Sopenharmony_ci * This would be catched later but we can just return 6638c2ecf20Sopenharmony_ci * the error right away. 6648c2ecf20Sopenharmony_ci */ 6658c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, "Property reg must be given\n"); 6668c2ecf20Sopenharmony_ci of_node_put(phandle); 6678c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 6688c2ecf20Sopenharmony_ci } 6698c2ecf20Sopenharmony_ci } 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_ci /* check custom sensor */ 6728c2ecf20Sopenharmony_ci if (sensor->type == LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { 6738c2ecf20Sopenharmony_ci const char *propname = "adi,custom-thermocouple"; 6748c2ecf20Sopenharmony_ci 6758c2ecf20Sopenharmony_ci thermo->custom = __ltc2983_custom_sensor_new(st, child, 6768c2ecf20Sopenharmony_ci propname, false, 6778c2ecf20Sopenharmony_ci 16384, true); 6788c2ecf20Sopenharmony_ci if (IS_ERR(thermo->custom)) { 6798c2ecf20Sopenharmony_ci of_node_put(phandle); 6808c2ecf20Sopenharmony_ci return ERR_CAST(thermo->custom); 6818c2ecf20Sopenharmony_ci } 6828c2ecf20Sopenharmony_ci } 6838c2ecf20Sopenharmony_ci 6848c2ecf20Sopenharmony_ci /* set common parameters */ 6858c2ecf20Sopenharmony_ci thermo->sensor.fault_handler = ltc2983_thermocouple_fault_handler; 6868c2ecf20Sopenharmony_ci thermo->sensor.assign_chan = ltc2983_thermocouple_assign_chan; 6878c2ecf20Sopenharmony_ci 6888c2ecf20Sopenharmony_ci of_node_put(phandle); 6898c2ecf20Sopenharmony_ci return &thermo->sensor; 6908c2ecf20Sopenharmony_ci} 6918c2ecf20Sopenharmony_ci 6928c2ecf20Sopenharmony_cistatic struct ltc2983_sensor *ltc2983_rtd_new(const struct device_node *child, 6938c2ecf20Sopenharmony_ci struct ltc2983_data *st, 6948c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 6958c2ecf20Sopenharmony_ci{ 6968c2ecf20Sopenharmony_ci struct ltc2983_rtd *rtd; 6978c2ecf20Sopenharmony_ci int ret = 0; 6988c2ecf20Sopenharmony_ci struct device *dev = &st->spi->dev; 6998c2ecf20Sopenharmony_ci struct device_node *phandle; 7008c2ecf20Sopenharmony_ci u32 excitation_current = 0, n_wires = 0; 7018c2ecf20Sopenharmony_ci 7028c2ecf20Sopenharmony_ci rtd = devm_kzalloc(dev, sizeof(*rtd), GFP_KERNEL); 7038c2ecf20Sopenharmony_ci if (!rtd) 7048c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 7058c2ecf20Sopenharmony_ci 7068c2ecf20Sopenharmony_ci phandle = of_parse_phandle(child, "adi,rsense-handle", 0); 7078c2ecf20Sopenharmony_ci if (!phandle) { 7088c2ecf20Sopenharmony_ci dev_err(dev, "Property adi,rsense-handle missing or invalid"); 7098c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 7108c2ecf20Sopenharmony_ci } 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci ret = of_property_read_u32(phandle, "reg", &rtd->r_sense_chan); 7138c2ecf20Sopenharmony_ci if (ret) { 7148c2ecf20Sopenharmony_ci dev_err(dev, "Property reg must be given\n"); 7158c2ecf20Sopenharmony_ci goto fail; 7168c2ecf20Sopenharmony_ci } 7178c2ecf20Sopenharmony_ci 7188c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "adi,number-of-wires", &n_wires); 7198c2ecf20Sopenharmony_ci if (!ret) { 7208c2ecf20Sopenharmony_ci switch (n_wires) { 7218c2ecf20Sopenharmony_ci case 2: 7228c2ecf20Sopenharmony_ci rtd->sensor_config = LTC2983_RTD_N_WIRES(0); 7238c2ecf20Sopenharmony_ci break; 7248c2ecf20Sopenharmony_ci case 3: 7258c2ecf20Sopenharmony_ci rtd->sensor_config = LTC2983_RTD_N_WIRES(1); 7268c2ecf20Sopenharmony_ci break; 7278c2ecf20Sopenharmony_ci case 4: 7288c2ecf20Sopenharmony_ci rtd->sensor_config = LTC2983_RTD_N_WIRES(2); 7298c2ecf20Sopenharmony_ci break; 7308c2ecf20Sopenharmony_ci case 5: 7318c2ecf20Sopenharmony_ci /* 4 wires, Kelvin Rsense */ 7328c2ecf20Sopenharmony_ci rtd->sensor_config = LTC2983_RTD_N_WIRES(3); 7338c2ecf20Sopenharmony_ci break; 7348c2ecf20Sopenharmony_ci default: 7358c2ecf20Sopenharmony_ci dev_err(dev, "Invalid number of wires:%u\n", n_wires); 7368c2ecf20Sopenharmony_ci ret = -EINVAL; 7378c2ecf20Sopenharmony_ci goto fail; 7388c2ecf20Sopenharmony_ci } 7398c2ecf20Sopenharmony_ci } 7408c2ecf20Sopenharmony_ci 7418c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,rsense-share")) { 7428c2ecf20Sopenharmony_ci /* Current rotation is only available with rsense sharing */ 7438c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,current-rotate")) { 7448c2ecf20Sopenharmony_ci if (n_wires == 2 || n_wires == 3) { 7458c2ecf20Sopenharmony_ci dev_err(dev, 7468c2ecf20Sopenharmony_ci "Rotation not allowed for 2/3 Wire RTDs"); 7478c2ecf20Sopenharmony_ci ret = -EINVAL; 7488c2ecf20Sopenharmony_ci goto fail; 7498c2ecf20Sopenharmony_ci } 7508c2ecf20Sopenharmony_ci rtd->sensor_config |= LTC2983_RTD_C_ROTATE(1); 7518c2ecf20Sopenharmony_ci } else { 7528c2ecf20Sopenharmony_ci rtd->sensor_config |= LTC2983_RTD_R_SHARE(1); 7538c2ecf20Sopenharmony_ci } 7548c2ecf20Sopenharmony_ci } 7558c2ecf20Sopenharmony_ci /* 7568c2ecf20Sopenharmony_ci * rtd channel indexes are a bit more complicated to validate. 7578c2ecf20Sopenharmony_ci * For 4wire RTD with rotation, the channel selection cannot be 7588c2ecf20Sopenharmony_ci * >=19 since the chann + 1 is used in this configuration. 7598c2ecf20Sopenharmony_ci * For 4wire RTDs with kelvin rsense, the rsense channel cannot be 7608c2ecf20Sopenharmony_ci * <=1 since chanel - 1 and channel - 2 are used. 7618c2ecf20Sopenharmony_ci */ 7628c2ecf20Sopenharmony_ci if (rtd->sensor_config & LTC2983_RTD_4_WIRE_MASK) { 7638c2ecf20Sopenharmony_ci /* 4-wire */ 7648c2ecf20Sopenharmony_ci u8 min = LTC2983_DIFFERENTIAL_CHAN_MIN, 7658c2ecf20Sopenharmony_ci max = LTC2983_MAX_CHANNELS_NR; 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_ci if (rtd->sensor_config & LTC2983_RTD_ROTATION_MASK) 7688c2ecf20Sopenharmony_ci max = LTC2983_MAX_CHANNELS_NR - 1; 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci if (((rtd->sensor_config & LTC2983_RTD_KELVIN_R_SENSE_MASK) 7718c2ecf20Sopenharmony_ci == LTC2983_RTD_KELVIN_R_SENSE_MASK) && 7728c2ecf20Sopenharmony_ci (rtd->r_sense_chan <= min)) { 7738c2ecf20Sopenharmony_ci /* kelvin rsense*/ 7748c2ecf20Sopenharmony_ci dev_err(dev, 7758c2ecf20Sopenharmony_ci "Invalid rsense chann:%d to use in kelvin rsense", 7768c2ecf20Sopenharmony_ci rtd->r_sense_chan); 7778c2ecf20Sopenharmony_ci 7788c2ecf20Sopenharmony_ci ret = -EINVAL; 7798c2ecf20Sopenharmony_ci goto fail; 7808c2ecf20Sopenharmony_ci } 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_ci if (sensor->chan < min || sensor->chan > max) { 7838c2ecf20Sopenharmony_ci dev_err(dev, "Invalid chann:%d for the rtd config", 7848c2ecf20Sopenharmony_ci sensor->chan); 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_ci ret = -EINVAL; 7878c2ecf20Sopenharmony_ci goto fail; 7888c2ecf20Sopenharmony_ci } 7898c2ecf20Sopenharmony_ci } else { 7908c2ecf20Sopenharmony_ci /* same as differential case */ 7918c2ecf20Sopenharmony_ci if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { 7928c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 7938c2ecf20Sopenharmony_ci "Invalid chann:%d for RTD", sensor->chan); 7948c2ecf20Sopenharmony_ci 7958c2ecf20Sopenharmony_ci ret = -EINVAL; 7968c2ecf20Sopenharmony_ci goto fail; 7978c2ecf20Sopenharmony_ci } 7988c2ecf20Sopenharmony_ci } 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_ci /* check custom sensor */ 8018c2ecf20Sopenharmony_ci if (sensor->type == LTC2983_SENSOR_RTD_CUSTOM) { 8028c2ecf20Sopenharmony_ci rtd->custom = __ltc2983_custom_sensor_new(st, child, 8038c2ecf20Sopenharmony_ci "adi,custom-rtd", 8048c2ecf20Sopenharmony_ci false, 2048, false); 8058c2ecf20Sopenharmony_ci if (IS_ERR(rtd->custom)) { 8068c2ecf20Sopenharmony_ci of_node_put(phandle); 8078c2ecf20Sopenharmony_ci return ERR_CAST(rtd->custom); 8088c2ecf20Sopenharmony_ci } 8098c2ecf20Sopenharmony_ci } 8108c2ecf20Sopenharmony_ci 8118c2ecf20Sopenharmony_ci /* set common parameters */ 8128c2ecf20Sopenharmony_ci rtd->sensor.fault_handler = ltc2983_common_fault_handler; 8138c2ecf20Sopenharmony_ci rtd->sensor.assign_chan = ltc2983_rtd_assign_chan; 8148c2ecf20Sopenharmony_ci 8158c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "adi,excitation-current-microamp", 8168c2ecf20Sopenharmony_ci &excitation_current); 8178c2ecf20Sopenharmony_ci if (ret) { 8188c2ecf20Sopenharmony_ci /* default to 5uA */ 8198c2ecf20Sopenharmony_ci rtd->excitation_current = 1; 8208c2ecf20Sopenharmony_ci } else { 8218c2ecf20Sopenharmony_ci switch (excitation_current) { 8228c2ecf20Sopenharmony_ci case 5: 8238c2ecf20Sopenharmony_ci rtd->excitation_current = 0x01; 8248c2ecf20Sopenharmony_ci break; 8258c2ecf20Sopenharmony_ci case 10: 8268c2ecf20Sopenharmony_ci rtd->excitation_current = 0x02; 8278c2ecf20Sopenharmony_ci break; 8288c2ecf20Sopenharmony_ci case 25: 8298c2ecf20Sopenharmony_ci rtd->excitation_current = 0x03; 8308c2ecf20Sopenharmony_ci break; 8318c2ecf20Sopenharmony_ci case 50: 8328c2ecf20Sopenharmony_ci rtd->excitation_current = 0x04; 8338c2ecf20Sopenharmony_ci break; 8348c2ecf20Sopenharmony_ci case 100: 8358c2ecf20Sopenharmony_ci rtd->excitation_current = 0x05; 8368c2ecf20Sopenharmony_ci break; 8378c2ecf20Sopenharmony_ci case 250: 8388c2ecf20Sopenharmony_ci rtd->excitation_current = 0x06; 8398c2ecf20Sopenharmony_ci break; 8408c2ecf20Sopenharmony_ci case 500: 8418c2ecf20Sopenharmony_ci rtd->excitation_current = 0x07; 8428c2ecf20Sopenharmony_ci break; 8438c2ecf20Sopenharmony_ci case 1000: 8448c2ecf20Sopenharmony_ci rtd->excitation_current = 0x08; 8458c2ecf20Sopenharmony_ci break; 8468c2ecf20Sopenharmony_ci default: 8478c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 8488c2ecf20Sopenharmony_ci "Invalid value for excitation current(%u)", 8498c2ecf20Sopenharmony_ci excitation_current); 8508c2ecf20Sopenharmony_ci ret = -EINVAL; 8518c2ecf20Sopenharmony_ci goto fail; 8528c2ecf20Sopenharmony_ci } 8538c2ecf20Sopenharmony_ci } 8548c2ecf20Sopenharmony_ci 8558c2ecf20Sopenharmony_ci of_property_read_u32(child, "adi,rtd-curve", &rtd->rtd_curve); 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_ci of_node_put(phandle); 8588c2ecf20Sopenharmony_ci return &rtd->sensor; 8598c2ecf20Sopenharmony_cifail: 8608c2ecf20Sopenharmony_ci of_node_put(phandle); 8618c2ecf20Sopenharmony_ci return ERR_PTR(ret); 8628c2ecf20Sopenharmony_ci} 8638c2ecf20Sopenharmony_ci 8648c2ecf20Sopenharmony_cistatic struct ltc2983_sensor *ltc2983_thermistor_new( 8658c2ecf20Sopenharmony_ci const struct device_node *child, 8668c2ecf20Sopenharmony_ci struct ltc2983_data *st, 8678c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 8688c2ecf20Sopenharmony_ci{ 8698c2ecf20Sopenharmony_ci struct ltc2983_thermistor *thermistor; 8708c2ecf20Sopenharmony_ci struct device *dev = &st->spi->dev; 8718c2ecf20Sopenharmony_ci struct device_node *phandle; 8728c2ecf20Sopenharmony_ci u32 excitation_current = 0; 8738c2ecf20Sopenharmony_ci int ret = 0; 8748c2ecf20Sopenharmony_ci 8758c2ecf20Sopenharmony_ci thermistor = devm_kzalloc(dev, sizeof(*thermistor), GFP_KERNEL); 8768c2ecf20Sopenharmony_ci if (!thermistor) 8778c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 8788c2ecf20Sopenharmony_ci 8798c2ecf20Sopenharmony_ci phandle = of_parse_phandle(child, "adi,rsense-handle", 0); 8808c2ecf20Sopenharmony_ci if (!phandle) { 8818c2ecf20Sopenharmony_ci dev_err(dev, "Property adi,rsense-handle missing or invalid"); 8828c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 8838c2ecf20Sopenharmony_ci } 8848c2ecf20Sopenharmony_ci 8858c2ecf20Sopenharmony_ci ret = of_property_read_u32(phandle, "reg", &thermistor->r_sense_chan); 8868c2ecf20Sopenharmony_ci if (ret) { 8878c2ecf20Sopenharmony_ci dev_err(dev, "rsense channel must be configured...\n"); 8888c2ecf20Sopenharmony_ci goto fail; 8898c2ecf20Sopenharmony_ci } 8908c2ecf20Sopenharmony_ci 8918c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,single-ended")) { 8928c2ecf20Sopenharmony_ci thermistor->sensor_config = LTC2983_THERMISTOR_SGL(1); 8938c2ecf20Sopenharmony_ci } else if (of_property_read_bool(child, "adi,rsense-share")) { 8948c2ecf20Sopenharmony_ci /* rotation is only possible if sharing rsense */ 8958c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,current-rotate")) 8968c2ecf20Sopenharmony_ci thermistor->sensor_config = 8978c2ecf20Sopenharmony_ci LTC2983_THERMISTOR_C_ROTATE(1); 8988c2ecf20Sopenharmony_ci else 8998c2ecf20Sopenharmony_ci thermistor->sensor_config = 9008c2ecf20Sopenharmony_ci LTC2983_THERMISTOR_R_SHARE(1); 9018c2ecf20Sopenharmony_ci } 9028c2ecf20Sopenharmony_ci /* validate channel index */ 9038c2ecf20Sopenharmony_ci if (!(thermistor->sensor_config & LTC2983_THERMISTOR_DIFF_MASK) && 9048c2ecf20Sopenharmony_ci sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { 9058c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 9068c2ecf20Sopenharmony_ci "Invalid chann:%d for differential thermistor", 9078c2ecf20Sopenharmony_ci sensor->chan); 9088c2ecf20Sopenharmony_ci ret = -EINVAL; 9098c2ecf20Sopenharmony_ci goto fail; 9108c2ecf20Sopenharmony_ci } 9118c2ecf20Sopenharmony_ci 9128c2ecf20Sopenharmony_ci /* check custom sensor */ 9138c2ecf20Sopenharmony_ci if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) { 9148c2ecf20Sopenharmony_ci bool steinhart = false; 9158c2ecf20Sopenharmony_ci const char *propname; 9168c2ecf20Sopenharmony_ci 9178c2ecf20Sopenharmony_ci if (sensor->type == LTC2983_SENSOR_THERMISTOR_STEINHART) { 9188c2ecf20Sopenharmony_ci steinhart = true; 9198c2ecf20Sopenharmony_ci propname = "adi,custom-steinhart"; 9208c2ecf20Sopenharmony_ci } else { 9218c2ecf20Sopenharmony_ci propname = "adi,custom-thermistor"; 9228c2ecf20Sopenharmony_ci } 9238c2ecf20Sopenharmony_ci 9248c2ecf20Sopenharmony_ci thermistor->custom = __ltc2983_custom_sensor_new(st, child, 9258c2ecf20Sopenharmony_ci propname, 9268c2ecf20Sopenharmony_ci steinhart, 9278c2ecf20Sopenharmony_ci 64, false); 9288c2ecf20Sopenharmony_ci if (IS_ERR(thermistor->custom)) { 9298c2ecf20Sopenharmony_ci of_node_put(phandle); 9308c2ecf20Sopenharmony_ci return ERR_CAST(thermistor->custom); 9318c2ecf20Sopenharmony_ci } 9328c2ecf20Sopenharmony_ci } 9338c2ecf20Sopenharmony_ci /* set common parameters */ 9348c2ecf20Sopenharmony_ci thermistor->sensor.fault_handler = ltc2983_common_fault_handler; 9358c2ecf20Sopenharmony_ci thermistor->sensor.assign_chan = ltc2983_thermistor_assign_chan; 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "adi,excitation-current-nanoamp", 9388c2ecf20Sopenharmony_ci &excitation_current); 9398c2ecf20Sopenharmony_ci if (ret) { 9408c2ecf20Sopenharmony_ci /* Auto range is not allowed for custom sensors */ 9418c2ecf20Sopenharmony_ci if (sensor->type >= LTC2983_SENSOR_THERMISTOR_STEINHART) 9428c2ecf20Sopenharmony_ci /* default to 1uA */ 9438c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x03; 9448c2ecf20Sopenharmony_ci else 9458c2ecf20Sopenharmony_ci /* default to auto-range */ 9468c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x0c; 9478c2ecf20Sopenharmony_ci } else { 9488c2ecf20Sopenharmony_ci switch (excitation_current) { 9498c2ecf20Sopenharmony_ci case 0: 9508c2ecf20Sopenharmony_ci /* auto range */ 9518c2ecf20Sopenharmony_ci if (sensor->type >= 9528c2ecf20Sopenharmony_ci LTC2983_SENSOR_THERMISTOR_STEINHART) { 9538c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 9548c2ecf20Sopenharmony_ci "Auto Range not allowed for custom sensors\n"); 9558c2ecf20Sopenharmony_ci ret = -EINVAL; 9568c2ecf20Sopenharmony_ci goto fail; 9578c2ecf20Sopenharmony_ci } 9588c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x0c; 9598c2ecf20Sopenharmony_ci break; 9608c2ecf20Sopenharmony_ci case 250: 9618c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x01; 9628c2ecf20Sopenharmony_ci break; 9638c2ecf20Sopenharmony_ci case 500: 9648c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x02; 9658c2ecf20Sopenharmony_ci break; 9668c2ecf20Sopenharmony_ci case 1000: 9678c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x03; 9688c2ecf20Sopenharmony_ci break; 9698c2ecf20Sopenharmony_ci case 5000: 9708c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x04; 9718c2ecf20Sopenharmony_ci break; 9728c2ecf20Sopenharmony_ci case 10000: 9738c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x05; 9748c2ecf20Sopenharmony_ci break; 9758c2ecf20Sopenharmony_ci case 25000: 9768c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x06; 9778c2ecf20Sopenharmony_ci break; 9788c2ecf20Sopenharmony_ci case 50000: 9798c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x07; 9808c2ecf20Sopenharmony_ci break; 9818c2ecf20Sopenharmony_ci case 100000: 9828c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x08; 9838c2ecf20Sopenharmony_ci break; 9848c2ecf20Sopenharmony_ci case 250000: 9858c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x09; 9868c2ecf20Sopenharmony_ci break; 9878c2ecf20Sopenharmony_ci case 500000: 9888c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x0a; 9898c2ecf20Sopenharmony_ci break; 9908c2ecf20Sopenharmony_ci case 1000000: 9918c2ecf20Sopenharmony_ci thermistor->excitation_current = 0x0b; 9928c2ecf20Sopenharmony_ci break; 9938c2ecf20Sopenharmony_ci default: 9948c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 9958c2ecf20Sopenharmony_ci "Invalid value for excitation current(%u)", 9968c2ecf20Sopenharmony_ci excitation_current); 9978c2ecf20Sopenharmony_ci ret = -EINVAL; 9988c2ecf20Sopenharmony_ci goto fail; 9998c2ecf20Sopenharmony_ci } 10008c2ecf20Sopenharmony_ci } 10018c2ecf20Sopenharmony_ci 10028c2ecf20Sopenharmony_ci of_node_put(phandle); 10038c2ecf20Sopenharmony_ci return &thermistor->sensor; 10048c2ecf20Sopenharmony_cifail: 10058c2ecf20Sopenharmony_ci of_node_put(phandle); 10068c2ecf20Sopenharmony_ci return ERR_PTR(ret); 10078c2ecf20Sopenharmony_ci} 10088c2ecf20Sopenharmony_ci 10098c2ecf20Sopenharmony_cistatic struct ltc2983_sensor *ltc2983_diode_new( 10108c2ecf20Sopenharmony_ci const struct device_node *child, 10118c2ecf20Sopenharmony_ci const struct ltc2983_data *st, 10128c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 10138c2ecf20Sopenharmony_ci{ 10148c2ecf20Sopenharmony_ci struct ltc2983_diode *diode; 10158c2ecf20Sopenharmony_ci u32 temp = 0, excitation_current = 0; 10168c2ecf20Sopenharmony_ci int ret; 10178c2ecf20Sopenharmony_ci 10188c2ecf20Sopenharmony_ci diode = devm_kzalloc(&st->spi->dev, sizeof(*diode), GFP_KERNEL); 10198c2ecf20Sopenharmony_ci if (!diode) 10208c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,single-ended")) 10238c2ecf20Sopenharmony_ci diode->sensor_config = LTC2983_DIODE_SGL(1); 10248c2ecf20Sopenharmony_ci 10258c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,three-conversion-cycles")) 10268c2ecf20Sopenharmony_ci diode->sensor_config |= LTC2983_DIODE_3_CONV_CYCLE(1); 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,average-on")) 10298c2ecf20Sopenharmony_ci diode->sensor_config |= LTC2983_DIODE_AVERAGE_ON(1); 10308c2ecf20Sopenharmony_ci 10318c2ecf20Sopenharmony_ci /* validate channel index */ 10328c2ecf20Sopenharmony_ci if (!(diode->sensor_config & LTC2983_DIODE_DIFF_MASK) && 10338c2ecf20Sopenharmony_ci sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { 10348c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 10358c2ecf20Sopenharmony_ci "Invalid chann:%d for differential thermistor", 10368c2ecf20Sopenharmony_ci sensor->chan); 10378c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 10388c2ecf20Sopenharmony_ci } 10398c2ecf20Sopenharmony_ci /* set common parameters */ 10408c2ecf20Sopenharmony_ci diode->sensor.fault_handler = ltc2983_common_fault_handler; 10418c2ecf20Sopenharmony_ci diode->sensor.assign_chan = ltc2983_diode_assign_chan; 10428c2ecf20Sopenharmony_ci 10438c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "adi,excitation-current-microamp", 10448c2ecf20Sopenharmony_ci &excitation_current); 10458c2ecf20Sopenharmony_ci if (!ret) { 10468c2ecf20Sopenharmony_ci switch (excitation_current) { 10478c2ecf20Sopenharmony_ci case 10: 10488c2ecf20Sopenharmony_ci diode->excitation_current = 0x00; 10498c2ecf20Sopenharmony_ci break; 10508c2ecf20Sopenharmony_ci case 20: 10518c2ecf20Sopenharmony_ci diode->excitation_current = 0x01; 10528c2ecf20Sopenharmony_ci break; 10538c2ecf20Sopenharmony_ci case 40: 10548c2ecf20Sopenharmony_ci diode->excitation_current = 0x02; 10558c2ecf20Sopenharmony_ci break; 10568c2ecf20Sopenharmony_ci case 80: 10578c2ecf20Sopenharmony_ci diode->excitation_current = 0x03; 10588c2ecf20Sopenharmony_ci break; 10598c2ecf20Sopenharmony_ci default: 10608c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, 10618c2ecf20Sopenharmony_ci "Invalid value for excitation current(%u)", 10628c2ecf20Sopenharmony_ci excitation_current); 10638c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 10648c2ecf20Sopenharmony_ci } 10658c2ecf20Sopenharmony_ci } 10668c2ecf20Sopenharmony_ci 10678c2ecf20Sopenharmony_ci of_property_read_u32(child, "adi,ideal-factor-value", &temp); 10688c2ecf20Sopenharmony_ci 10698c2ecf20Sopenharmony_ci /* 2^20 resolution */ 10708c2ecf20Sopenharmony_ci diode->ideal_factor_value = __convert_to_raw(temp, 1048576); 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_ci return &diode->sensor; 10738c2ecf20Sopenharmony_ci} 10748c2ecf20Sopenharmony_ci 10758c2ecf20Sopenharmony_cistatic struct ltc2983_sensor *ltc2983_r_sense_new(struct device_node *child, 10768c2ecf20Sopenharmony_ci struct ltc2983_data *st, 10778c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 10788c2ecf20Sopenharmony_ci{ 10798c2ecf20Sopenharmony_ci struct ltc2983_rsense *rsense; 10808c2ecf20Sopenharmony_ci int ret; 10818c2ecf20Sopenharmony_ci u32 temp; 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_ci rsense = devm_kzalloc(&st->spi->dev, sizeof(*rsense), GFP_KERNEL); 10848c2ecf20Sopenharmony_ci if (!rsense) 10858c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 10868c2ecf20Sopenharmony_ci 10878c2ecf20Sopenharmony_ci /* validate channel index */ 10888c2ecf20Sopenharmony_ci if (sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { 10898c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, "Invalid chann:%d for r_sense", 10908c2ecf20Sopenharmony_ci sensor->chan); 10918c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 10928c2ecf20Sopenharmony_ci } 10938c2ecf20Sopenharmony_ci 10948c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "adi,rsense-val-milli-ohms", &temp); 10958c2ecf20Sopenharmony_ci if (ret) { 10968c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, "Property adi,rsense-val-milli-ohms missing\n"); 10978c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 10988c2ecf20Sopenharmony_ci } 10998c2ecf20Sopenharmony_ci /* 11008c2ecf20Sopenharmony_ci * Times 1000 because we have milli-ohms and __convert_to_raw 11018c2ecf20Sopenharmony_ci * expects scales of 1000000 which are used for all other 11028c2ecf20Sopenharmony_ci * properties. 11038c2ecf20Sopenharmony_ci * 2^10 resolution 11048c2ecf20Sopenharmony_ci */ 11058c2ecf20Sopenharmony_ci rsense->r_sense_val = __convert_to_raw((u64)temp * 1000, 1024); 11068c2ecf20Sopenharmony_ci 11078c2ecf20Sopenharmony_ci /* set common parameters */ 11088c2ecf20Sopenharmony_ci rsense->sensor.assign_chan = ltc2983_r_sense_assign_chan; 11098c2ecf20Sopenharmony_ci 11108c2ecf20Sopenharmony_ci return &rsense->sensor; 11118c2ecf20Sopenharmony_ci} 11128c2ecf20Sopenharmony_ci 11138c2ecf20Sopenharmony_cistatic struct ltc2983_sensor *ltc2983_adc_new(struct device_node *child, 11148c2ecf20Sopenharmony_ci struct ltc2983_data *st, 11158c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor) 11168c2ecf20Sopenharmony_ci{ 11178c2ecf20Sopenharmony_ci struct ltc2983_adc *adc; 11188c2ecf20Sopenharmony_ci 11198c2ecf20Sopenharmony_ci adc = devm_kzalloc(&st->spi->dev, sizeof(*adc), GFP_KERNEL); 11208c2ecf20Sopenharmony_ci if (!adc) 11218c2ecf20Sopenharmony_ci return ERR_PTR(-ENOMEM); 11228c2ecf20Sopenharmony_ci 11238c2ecf20Sopenharmony_ci if (of_property_read_bool(child, "adi,single-ended")) 11248c2ecf20Sopenharmony_ci adc->single_ended = true; 11258c2ecf20Sopenharmony_ci 11268c2ecf20Sopenharmony_ci if (!adc->single_ended && 11278c2ecf20Sopenharmony_ci sensor->chan < LTC2983_DIFFERENTIAL_CHAN_MIN) { 11288c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, "Invalid chan:%d for differential adc\n", 11298c2ecf20Sopenharmony_ci sensor->chan); 11308c2ecf20Sopenharmony_ci return ERR_PTR(-EINVAL); 11318c2ecf20Sopenharmony_ci } 11328c2ecf20Sopenharmony_ci /* set common parameters */ 11338c2ecf20Sopenharmony_ci adc->sensor.assign_chan = ltc2983_adc_assign_chan; 11348c2ecf20Sopenharmony_ci adc->sensor.fault_handler = ltc2983_common_fault_handler; 11358c2ecf20Sopenharmony_ci 11368c2ecf20Sopenharmony_ci return &adc->sensor; 11378c2ecf20Sopenharmony_ci} 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_cistatic int ltc2983_chan_read(struct ltc2983_data *st, 11408c2ecf20Sopenharmony_ci const struct ltc2983_sensor *sensor, int *val) 11418c2ecf20Sopenharmony_ci{ 11428c2ecf20Sopenharmony_ci u32 start_conversion = 0; 11438c2ecf20Sopenharmony_ci int ret; 11448c2ecf20Sopenharmony_ci unsigned long time; 11458c2ecf20Sopenharmony_ci 11468c2ecf20Sopenharmony_ci start_conversion = LTC2983_STATUS_START(true); 11478c2ecf20Sopenharmony_ci start_conversion |= LTC2983_STATUS_CHAN_SEL(sensor->chan); 11488c2ecf20Sopenharmony_ci dev_dbg(&st->spi->dev, "Start conversion on chan:%d, status:%02X\n", 11498c2ecf20Sopenharmony_ci sensor->chan, start_conversion); 11508c2ecf20Sopenharmony_ci /* start conversion */ 11518c2ecf20Sopenharmony_ci ret = regmap_write(st->regmap, LTC2983_STATUS_REG, start_conversion); 11528c2ecf20Sopenharmony_ci if (ret) 11538c2ecf20Sopenharmony_ci return ret; 11548c2ecf20Sopenharmony_ci 11558c2ecf20Sopenharmony_ci reinit_completion(&st->completion); 11568c2ecf20Sopenharmony_ci /* 11578c2ecf20Sopenharmony_ci * wait for conversion to complete. 11588c2ecf20Sopenharmony_ci * 300 ms should be more than enough to complete the conversion. 11598c2ecf20Sopenharmony_ci * Depending on the sensor configuration, there are 2/3 conversions 11608c2ecf20Sopenharmony_ci * cycles of 82ms. 11618c2ecf20Sopenharmony_ci */ 11628c2ecf20Sopenharmony_ci time = wait_for_completion_timeout(&st->completion, 11638c2ecf20Sopenharmony_ci msecs_to_jiffies(300)); 11648c2ecf20Sopenharmony_ci if (!time) { 11658c2ecf20Sopenharmony_ci dev_warn(&st->spi->dev, "Conversion timed out\n"); 11668c2ecf20Sopenharmony_ci return -ETIMEDOUT; 11678c2ecf20Sopenharmony_ci } 11688c2ecf20Sopenharmony_ci 11698c2ecf20Sopenharmony_ci /* read the converted data */ 11708c2ecf20Sopenharmony_ci ret = regmap_bulk_read(st->regmap, LTC2983_CHAN_RES_ADDR(sensor->chan), 11718c2ecf20Sopenharmony_ci &st->temp, sizeof(st->temp)); 11728c2ecf20Sopenharmony_ci if (ret) 11738c2ecf20Sopenharmony_ci return ret; 11748c2ecf20Sopenharmony_ci 11758c2ecf20Sopenharmony_ci *val = __be32_to_cpu(st->temp); 11768c2ecf20Sopenharmony_ci 11778c2ecf20Sopenharmony_ci if (!(LTC2983_RES_VALID_MASK & *val)) { 11788c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, "Invalid conversion detected\n"); 11798c2ecf20Sopenharmony_ci return -EIO; 11808c2ecf20Sopenharmony_ci } 11818c2ecf20Sopenharmony_ci 11828c2ecf20Sopenharmony_ci ret = sensor->fault_handler(st, *val); 11838c2ecf20Sopenharmony_ci if (ret) 11848c2ecf20Sopenharmony_ci return ret; 11858c2ecf20Sopenharmony_ci 11868c2ecf20Sopenharmony_ci *val = sign_extend32((*val) & LTC2983_DATA_MASK, LTC2983_DATA_SIGN_BIT); 11878c2ecf20Sopenharmony_ci return 0; 11888c2ecf20Sopenharmony_ci} 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_cistatic int ltc2983_read_raw(struct iio_dev *indio_dev, 11918c2ecf20Sopenharmony_ci struct iio_chan_spec const *chan, 11928c2ecf20Sopenharmony_ci int *val, int *val2, long mask) 11938c2ecf20Sopenharmony_ci{ 11948c2ecf20Sopenharmony_ci struct ltc2983_data *st = iio_priv(indio_dev); 11958c2ecf20Sopenharmony_ci int ret; 11968c2ecf20Sopenharmony_ci 11978c2ecf20Sopenharmony_ci /* sanity check */ 11988c2ecf20Sopenharmony_ci if (chan->address >= st->num_channels) { 11998c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, "Invalid chan address:%ld", 12008c2ecf20Sopenharmony_ci chan->address); 12018c2ecf20Sopenharmony_ci return -EINVAL; 12028c2ecf20Sopenharmony_ci } 12038c2ecf20Sopenharmony_ci 12048c2ecf20Sopenharmony_ci switch (mask) { 12058c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_RAW: 12068c2ecf20Sopenharmony_ci mutex_lock(&st->lock); 12078c2ecf20Sopenharmony_ci ret = ltc2983_chan_read(st, st->sensors[chan->address], val); 12088c2ecf20Sopenharmony_ci mutex_unlock(&st->lock); 12098c2ecf20Sopenharmony_ci return ret ?: IIO_VAL_INT; 12108c2ecf20Sopenharmony_ci case IIO_CHAN_INFO_SCALE: 12118c2ecf20Sopenharmony_ci switch (chan->type) { 12128c2ecf20Sopenharmony_ci case IIO_TEMP: 12138c2ecf20Sopenharmony_ci /* value in milli degrees */ 12148c2ecf20Sopenharmony_ci *val = 1000; 12158c2ecf20Sopenharmony_ci /* 2^10 */ 12168c2ecf20Sopenharmony_ci *val2 = 1024; 12178c2ecf20Sopenharmony_ci return IIO_VAL_FRACTIONAL; 12188c2ecf20Sopenharmony_ci case IIO_VOLTAGE: 12198c2ecf20Sopenharmony_ci /* value in millivolt */ 12208c2ecf20Sopenharmony_ci *val = 1000; 12218c2ecf20Sopenharmony_ci /* 2^21 */ 12228c2ecf20Sopenharmony_ci *val2 = 2097152; 12238c2ecf20Sopenharmony_ci return IIO_VAL_FRACTIONAL; 12248c2ecf20Sopenharmony_ci default: 12258c2ecf20Sopenharmony_ci return -EINVAL; 12268c2ecf20Sopenharmony_ci } 12278c2ecf20Sopenharmony_ci } 12288c2ecf20Sopenharmony_ci 12298c2ecf20Sopenharmony_ci return -EINVAL; 12308c2ecf20Sopenharmony_ci} 12318c2ecf20Sopenharmony_ci 12328c2ecf20Sopenharmony_cistatic int ltc2983_reg_access(struct iio_dev *indio_dev, 12338c2ecf20Sopenharmony_ci unsigned int reg, 12348c2ecf20Sopenharmony_ci unsigned int writeval, 12358c2ecf20Sopenharmony_ci unsigned int *readval) 12368c2ecf20Sopenharmony_ci{ 12378c2ecf20Sopenharmony_ci struct ltc2983_data *st = iio_priv(indio_dev); 12388c2ecf20Sopenharmony_ci 12398c2ecf20Sopenharmony_ci if (readval) 12408c2ecf20Sopenharmony_ci return regmap_read(st->regmap, reg, readval); 12418c2ecf20Sopenharmony_ci else 12428c2ecf20Sopenharmony_ci return regmap_write(st->regmap, reg, writeval); 12438c2ecf20Sopenharmony_ci} 12448c2ecf20Sopenharmony_ci 12458c2ecf20Sopenharmony_cistatic irqreturn_t ltc2983_irq_handler(int irq, void *data) 12468c2ecf20Sopenharmony_ci{ 12478c2ecf20Sopenharmony_ci struct ltc2983_data *st = data; 12488c2ecf20Sopenharmony_ci 12498c2ecf20Sopenharmony_ci complete(&st->completion); 12508c2ecf20Sopenharmony_ci return IRQ_HANDLED; 12518c2ecf20Sopenharmony_ci} 12528c2ecf20Sopenharmony_ci 12538c2ecf20Sopenharmony_ci#define LTC2983_CHAN(__type, index, __address) ({ \ 12548c2ecf20Sopenharmony_ci struct iio_chan_spec __chan = { \ 12558c2ecf20Sopenharmony_ci .type = __type, \ 12568c2ecf20Sopenharmony_ci .indexed = 1, \ 12578c2ecf20Sopenharmony_ci .channel = index, \ 12588c2ecf20Sopenharmony_ci .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \ 12598c2ecf20Sopenharmony_ci .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \ 12608c2ecf20Sopenharmony_ci .address = __address, \ 12618c2ecf20Sopenharmony_ci }; \ 12628c2ecf20Sopenharmony_ci __chan; \ 12638c2ecf20Sopenharmony_ci}) 12648c2ecf20Sopenharmony_ci 12658c2ecf20Sopenharmony_cistatic int ltc2983_parse_dt(struct ltc2983_data *st) 12668c2ecf20Sopenharmony_ci{ 12678c2ecf20Sopenharmony_ci struct device_node *child; 12688c2ecf20Sopenharmony_ci struct device *dev = &st->spi->dev; 12698c2ecf20Sopenharmony_ci int ret = 0, chan = 0, channel_avail_mask = 0; 12708c2ecf20Sopenharmony_ci 12718c2ecf20Sopenharmony_ci of_property_read_u32(dev->of_node, "adi,mux-delay-config-us", 12728c2ecf20Sopenharmony_ci &st->mux_delay_config); 12738c2ecf20Sopenharmony_ci 12748c2ecf20Sopenharmony_ci of_property_read_u32(dev->of_node, "adi,filter-notch-freq", 12758c2ecf20Sopenharmony_ci &st->filter_notch_freq); 12768c2ecf20Sopenharmony_ci 12778c2ecf20Sopenharmony_ci st->num_channels = of_get_available_child_count(dev->of_node); 12788c2ecf20Sopenharmony_ci st->sensors = devm_kcalloc(dev, st->num_channels, sizeof(*st->sensors), 12798c2ecf20Sopenharmony_ci GFP_KERNEL); 12808c2ecf20Sopenharmony_ci if (!st->sensors) 12818c2ecf20Sopenharmony_ci return -ENOMEM; 12828c2ecf20Sopenharmony_ci 12838c2ecf20Sopenharmony_ci st->iio_channels = st->num_channels; 12848c2ecf20Sopenharmony_ci for_each_available_child_of_node(dev->of_node, child) { 12858c2ecf20Sopenharmony_ci struct ltc2983_sensor sensor; 12868c2ecf20Sopenharmony_ci 12878c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "reg", &sensor.chan); 12888c2ecf20Sopenharmony_ci if (ret) { 12898c2ecf20Sopenharmony_ci dev_err(dev, "reg property must given for child nodes\n"); 12908c2ecf20Sopenharmony_ci goto put_child; 12918c2ecf20Sopenharmony_ci } 12928c2ecf20Sopenharmony_ci 12938c2ecf20Sopenharmony_ci /* check if we have a valid channel */ 12948c2ecf20Sopenharmony_ci if (sensor.chan < LTC2983_MIN_CHANNELS_NR || 12958c2ecf20Sopenharmony_ci sensor.chan > LTC2983_MAX_CHANNELS_NR) { 12968c2ecf20Sopenharmony_ci ret = -EINVAL; 12978c2ecf20Sopenharmony_ci dev_err(dev, 12988c2ecf20Sopenharmony_ci "chan:%d must be from 1 to 20\n", sensor.chan); 12998c2ecf20Sopenharmony_ci goto put_child; 13008c2ecf20Sopenharmony_ci } else if (channel_avail_mask & BIT(sensor.chan)) { 13018c2ecf20Sopenharmony_ci ret = -EINVAL; 13028c2ecf20Sopenharmony_ci dev_err(dev, "chan:%d already in use\n", sensor.chan); 13038c2ecf20Sopenharmony_ci goto put_child; 13048c2ecf20Sopenharmony_ci } 13058c2ecf20Sopenharmony_ci 13068c2ecf20Sopenharmony_ci ret = of_property_read_u32(child, "adi,sensor-type", 13078c2ecf20Sopenharmony_ci &sensor.type); 13088c2ecf20Sopenharmony_ci if (ret) { 13098c2ecf20Sopenharmony_ci dev_err(dev, 13108c2ecf20Sopenharmony_ci "adi,sensor-type property must given for child nodes\n"); 13118c2ecf20Sopenharmony_ci goto put_child; 13128c2ecf20Sopenharmony_ci } 13138c2ecf20Sopenharmony_ci 13148c2ecf20Sopenharmony_ci dev_dbg(dev, "Create new sensor, type %u, chann %u", 13158c2ecf20Sopenharmony_ci sensor.type, 13168c2ecf20Sopenharmony_ci sensor.chan); 13178c2ecf20Sopenharmony_ci 13188c2ecf20Sopenharmony_ci if (sensor.type >= LTC2983_SENSOR_THERMOCOUPLE && 13198c2ecf20Sopenharmony_ci sensor.type <= LTC2983_SENSOR_THERMOCOUPLE_CUSTOM) { 13208c2ecf20Sopenharmony_ci st->sensors[chan] = ltc2983_thermocouple_new(child, st, 13218c2ecf20Sopenharmony_ci &sensor); 13228c2ecf20Sopenharmony_ci } else if (sensor.type >= LTC2983_SENSOR_RTD && 13238c2ecf20Sopenharmony_ci sensor.type <= LTC2983_SENSOR_RTD_CUSTOM) { 13248c2ecf20Sopenharmony_ci st->sensors[chan] = ltc2983_rtd_new(child, st, &sensor); 13258c2ecf20Sopenharmony_ci } else if (sensor.type >= LTC2983_SENSOR_THERMISTOR && 13268c2ecf20Sopenharmony_ci sensor.type <= LTC2983_SENSOR_THERMISTOR_CUSTOM) { 13278c2ecf20Sopenharmony_ci st->sensors[chan] = ltc2983_thermistor_new(child, st, 13288c2ecf20Sopenharmony_ci &sensor); 13298c2ecf20Sopenharmony_ci } else if (sensor.type == LTC2983_SENSOR_DIODE) { 13308c2ecf20Sopenharmony_ci st->sensors[chan] = ltc2983_diode_new(child, st, 13318c2ecf20Sopenharmony_ci &sensor); 13328c2ecf20Sopenharmony_ci } else if (sensor.type == LTC2983_SENSOR_SENSE_RESISTOR) { 13338c2ecf20Sopenharmony_ci st->sensors[chan] = ltc2983_r_sense_new(child, st, 13348c2ecf20Sopenharmony_ci &sensor); 13358c2ecf20Sopenharmony_ci /* don't add rsense to iio */ 13368c2ecf20Sopenharmony_ci st->iio_channels--; 13378c2ecf20Sopenharmony_ci } else if (sensor.type == LTC2983_SENSOR_DIRECT_ADC) { 13388c2ecf20Sopenharmony_ci st->sensors[chan] = ltc2983_adc_new(child, st, &sensor); 13398c2ecf20Sopenharmony_ci } else { 13408c2ecf20Sopenharmony_ci dev_err(dev, "Unknown sensor type %d\n", sensor.type); 13418c2ecf20Sopenharmony_ci ret = -EINVAL; 13428c2ecf20Sopenharmony_ci goto put_child; 13438c2ecf20Sopenharmony_ci } 13448c2ecf20Sopenharmony_ci 13458c2ecf20Sopenharmony_ci if (IS_ERR(st->sensors[chan])) { 13468c2ecf20Sopenharmony_ci dev_err(dev, "Failed to create sensor %ld", 13478c2ecf20Sopenharmony_ci PTR_ERR(st->sensors[chan])); 13488c2ecf20Sopenharmony_ci ret = PTR_ERR(st->sensors[chan]); 13498c2ecf20Sopenharmony_ci goto put_child; 13508c2ecf20Sopenharmony_ci } 13518c2ecf20Sopenharmony_ci /* set generic sensor parameters */ 13528c2ecf20Sopenharmony_ci st->sensors[chan]->chan = sensor.chan; 13538c2ecf20Sopenharmony_ci st->sensors[chan]->type = sensor.type; 13548c2ecf20Sopenharmony_ci 13558c2ecf20Sopenharmony_ci channel_avail_mask |= BIT(sensor.chan); 13568c2ecf20Sopenharmony_ci chan++; 13578c2ecf20Sopenharmony_ci } 13588c2ecf20Sopenharmony_ci 13598c2ecf20Sopenharmony_ci return 0; 13608c2ecf20Sopenharmony_ciput_child: 13618c2ecf20Sopenharmony_ci of_node_put(child); 13628c2ecf20Sopenharmony_ci return ret; 13638c2ecf20Sopenharmony_ci} 13648c2ecf20Sopenharmony_ci 13658c2ecf20Sopenharmony_cistatic int ltc2983_setup(struct ltc2983_data *st, bool assign_iio) 13668c2ecf20Sopenharmony_ci{ 13678c2ecf20Sopenharmony_ci u32 iio_chan_t = 0, iio_chan_v = 0, chan, iio_idx = 0, status; 13688c2ecf20Sopenharmony_ci int ret; 13698c2ecf20Sopenharmony_ci 13708c2ecf20Sopenharmony_ci /* make sure the device is up: start bit (7) is 0 and done bit (6) is 1 */ 13718c2ecf20Sopenharmony_ci ret = regmap_read_poll_timeout(st->regmap, LTC2983_STATUS_REG, status, 13728c2ecf20Sopenharmony_ci LTC2983_STATUS_UP(status) == 1, 25000, 13738c2ecf20Sopenharmony_ci 25000 * 10); 13748c2ecf20Sopenharmony_ci if (ret) { 13758c2ecf20Sopenharmony_ci dev_err(&st->spi->dev, "Device startup timed out\n"); 13768c2ecf20Sopenharmony_ci return ret; 13778c2ecf20Sopenharmony_ci } 13788c2ecf20Sopenharmony_ci 13798c2ecf20Sopenharmony_ci ret = regmap_update_bits(st->regmap, LTC2983_GLOBAL_CONFIG_REG, 13808c2ecf20Sopenharmony_ci LTC2983_NOTCH_FREQ_MASK, 13818c2ecf20Sopenharmony_ci LTC2983_NOTCH_FREQ(st->filter_notch_freq)); 13828c2ecf20Sopenharmony_ci if (ret) 13838c2ecf20Sopenharmony_ci return ret; 13848c2ecf20Sopenharmony_ci 13858c2ecf20Sopenharmony_ci ret = regmap_write(st->regmap, LTC2983_MUX_CONFIG_REG, 13868c2ecf20Sopenharmony_ci st->mux_delay_config); 13878c2ecf20Sopenharmony_ci if (ret) 13888c2ecf20Sopenharmony_ci return ret; 13898c2ecf20Sopenharmony_ci 13908c2ecf20Sopenharmony_ci for (chan = 0; chan < st->num_channels; chan++) { 13918c2ecf20Sopenharmony_ci u32 chan_type = 0, *iio_chan; 13928c2ecf20Sopenharmony_ci 13938c2ecf20Sopenharmony_ci ret = st->sensors[chan]->assign_chan(st, st->sensors[chan]); 13948c2ecf20Sopenharmony_ci if (ret) 13958c2ecf20Sopenharmony_ci return ret; 13968c2ecf20Sopenharmony_ci /* 13978c2ecf20Sopenharmony_ci * The assign_iio flag is necessary for when the device is 13988c2ecf20Sopenharmony_ci * coming out of sleep. In that case, we just need to 13998c2ecf20Sopenharmony_ci * re-configure the device channels. 14008c2ecf20Sopenharmony_ci * We also don't assign iio channels for rsense. 14018c2ecf20Sopenharmony_ci */ 14028c2ecf20Sopenharmony_ci if (st->sensors[chan]->type == LTC2983_SENSOR_SENSE_RESISTOR || 14038c2ecf20Sopenharmony_ci !assign_iio) 14048c2ecf20Sopenharmony_ci continue; 14058c2ecf20Sopenharmony_ci 14068c2ecf20Sopenharmony_ci /* assign iio channel */ 14078c2ecf20Sopenharmony_ci if (st->sensors[chan]->type != LTC2983_SENSOR_DIRECT_ADC) { 14088c2ecf20Sopenharmony_ci chan_type = IIO_TEMP; 14098c2ecf20Sopenharmony_ci iio_chan = &iio_chan_t; 14108c2ecf20Sopenharmony_ci } else { 14118c2ecf20Sopenharmony_ci chan_type = IIO_VOLTAGE; 14128c2ecf20Sopenharmony_ci iio_chan = &iio_chan_v; 14138c2ecf20Sopenharmony_ci } 14148c2ecf20Sopenharmony_ci 14158c2ecf20Sopenharmony_ci /* 14168c2ecf20Sopenharmony_ci * add chan as the iio .address so that, we can directly 14178c2ecf20Sopenharmony_ci * reference the sensor given the iio_chan_spec 14188c2ecf20Sopenharmony_ci */ 14198c2ecf20Sopenharmony_ci st->iio_chan[iio_idx++] = LTC2983_CHAN(chan_type, (*iio_chan)++, 14208c2ecf20Sopenharmony_ci chan); 14218c2ecf20Sopenharmony_ci } 14228c2ecf20Sopenharmony_ci 14238c2ecf20Sopenharmony_ci return 0; 14248c2ecf20Sopenharmony_ci} 14258c2ecf20Sopenharmony_ci 14268c2ecf20Sopenharmony_cistatic const struct regmap_range ltc2983_reg_ranges[] = { 14278c2ecf20Sopenharmony_ci regmap_reg_range(LTC2983_STATUS_REG, LTC2983_STATUS_REG), 14288c2ecf20Sopenharmony_ci regmap_reg_range(LTC2983_TEMP_RES_START_REG, LTC2983_TEMP_RES_END_REG), 14298c2ecf20Sopenharmony_ci regmap_reg_range(LTC2983_GLOBAL_CONFIG_REG, LTC2983_GLOBAL_CONFIG_REG), 14308c2ecf20Sopenharmony_ci regmap_reg_range(LTC2983_MULT_CHANNEL_START_REG, 14318c2ecf20Sopenharmony_ci LTC2983_MULT_CHANNEL_END_REG), 14328c2ecf20Sopenharmony_ci regmap_reg_range(LTC2983_MUX_CONFIG_REG, LTC2983_MUX_CONFIG_REG), 14338c2ecf20Sopenharmony_ci regmap_reg_range(LTC2983_CHAN_ASSIGN_START_REG, 14348c2ecf20Sopenharmony_ci LTC2983_CHAN_ASSIGN_END_REG), 14358c2ecf20Sopenharmony_ci regmap_reg_range(LTC2983_CUST_SENS_TBL_START_REG, 14368c2ecf20Sopenharmony_ci LTC2983_CUST_SENS_TBL_END_REG), 14378c2ecf20Sopenharmony_ci}; 14388c2ecf20Sopenharmony_ci 14398c2ecf20Sopenharmony_cistatic const struct regmap_access_table ltc2983_reg_table = { 14408c2ecf20Sopenharmony_ci .yes_ranges = ltc2983_reg_ranges, 14418c2ecf20Sopenharmony_ci .n_yes_ranges = ARRAY_SIZE(ltc2983_reg_ranges), 14428c2ecf20Sopenharmony_ci}; 14438c2ecf20Sopenharmony_ci 14448c2ecf20Sopenharmony_ci/* 14458c2ecf20Sopenharmony_ci * The reg_bits are actually 12 but the device needs the first *complete* 14468c2ecf20Sopenharmony_ci * byte for the command (R/W). 14478c2ecf20Sopenharmony_ci */ 14488c2ecf20Sopenharmony_cistatic const struct regmap_config ltc2983_regmap_config = { 14498c2ecf20Sopenharmony_ci .reg_bits = 24, 14508c2ecf20Sopenharmony_ci .val_bits = 8, 14518c2ecf20Sopenharmony_ci .wr_table = <c2983_reg_table, 14528c2ecf20Sopenharmony_ci .rd_table = <c2983_reg_table, 14538c2ecf20Sopenharmony_ci .read_flag_mask = GENMASK(1, 0), 14548c2ecf20Sopenharmony_ci .write_flag_mask = BIT(1), 14558c2ecf20Sopenharmony_ci}; 14568c2ecf20Sopenharmony_ci 14578c2ecf20Sopenharmony_cistatic const struct iio_info ltc2983_iio_info = { 14588c2ecf20Sopenharmony_ci .read_raw = ltc2983_read_raw, 14598c2ecf20Sopenharmony_ci .debugfs_reg_access = ltc2983_reg_access, 14608c2ecf20Sopenharmony_ci}; 14618c2ecf20Sopenharmony_ci 14628c2ecf20Sopenharmony_cistatic int ltc2983_probe(struct spi_device *spi) 14638c2ecf20Sopenharmony_ci{ 14648c2ecf20Sopenharmony_ci struct ltc2983_data *st; 14658c2ecf20Sopenharmony_ci struct iio_dev *indio_dev; 14668c2ecf20Sopenharmony_ci const char *name = spi_get_device_id(spi)->name; 14678c2ecf20Sopenharmony_ci int ret; 14688c2ecf20Sopenharmony_ci 14698c2ecf20Sopenharmony_ci indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st)); 14708c2ecf20Sopenharmony_ci if (!indio_dev) 14718c2ecf20Sopenharmony_ci return -ENOMEM; 14728c2ecf20Sopenharmony_ci 14738c2ecf20Sopenharmony_ci st = iio_priv(indio_dev); 14748c2ecf20Sopenharmony_ci 14758c2ecf20Sopenharmony_ci st->regmap = devm_regmap_init_spi(spi, <c2983_regmap_config); 14768c2ecf20Sopenharmony_ci if (IS_ERR(st->regmap)) { 14778c2ecf20Sopenharmony_ci dev_err(&spi->dev, "Failed to initialize regmap\n"); 14788c2ecf20Sopenharmony_ci return PTR_ERR(st->regmap); 14798c2ecf20Sopenharmony_ci } 14808c2ecf20Sopenharmony_ci 14818c2ecf20Sopenharmony_ci mutex_init(&st->lock); 14828c2ecf20Sopenharmony_ci init_completion(&st->completion); 14838c2ecf20Sopenharmony_ci st->spi = spi; 14848c2ecf20Sopenharmony_ci spi_set_drvdata(spi, st); 14858c2ecf20Sopenharmony_ci 14868c2ecf20Sopenharmony_ci ret = ltc2983_parse_dt(st); 14878c2ecf20Sopenharmony_ci if (ret) 14888c2ecf20Sopenharmony_ci return ret; 14898c2ecf20Sopenharmony_ci 14908c2ecf20Sopenharmony_ci st->iio_chan = devm_kzalloc(&spi->dev, 14918c2ecf20Sopenharmony_ci st->iio_channels * sizeof(*st->iio_chan), 14928c2ecf20Sopenharmony_ci GFP_KERNEL); 14938c2ecf20Sopenharmony_ci if (!st->iio_chan) 14948c2ecf20Sopenharmony_ci return -ENOMEM; 14958c2ecf20Sopenharmony_ci 14968c2ecf20Sopenharmony_ci ret = ltc2983_setup(st, true); 14978c2ecf20Sopenharmony_ci if (ret) 14988c2ecf20Sopenharmony_ci return ret; 14998c2ecf20Sopenharmony_ci 15008c2ecf20Sopenharmony_ci ret = devm_request_irq(&spi->dev, spi->irq, ltc2983_irq_handler, 15018c2ecf20Sopenharmony_ci IRQF_TRIGGER_RISING, name, st); 15028c2ecf20Sopenharmony_ci if (ret) { 15038c2ecf20Sopenharmony_ci dev_err(&spi->dev, "failed to request an irq, %d", ret); 15048c2ecf20Sopenharmony_ci return ret; 15058c2ecf20Sopenharmony_ci } 15068c2ecf20Sopenharmony_ci 15078c2ecf20Sopenharmony_ci indio_dev->name = name; 15088c2ecf20Sopenharmony_ci indio_dev->num_channels = st->iio_channels; 15098c2ecf20Sopenharmony_ci indio_dev->channels = st->iio_chan; 15108c2ecf20Sopenharmony_ci indio_dev->modes = INDIO_DIRECT_MODE; 15118c2ecf20Sopenharmony_ci indio_dev->info = <c2983_iio_info; 15128c2ecf20Sopenharmony_ci 15138c2ecf20Sopenharmony_ci return devm_iio_device_register(&spi->dev, indio_dev); 15148c2ecf20Sopenharmony_ci} 15158c2ecf20Sopenharmony_ci 15168c2ecf20Sopenharmony_cistatic int __maybe_unused ltc2983_resume(struct device *dev) 15178c2ecf20Sopenharmony_ci{ 15188c2ecf20Sopenharmony_ci struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev)); 15198c2ecf20Sopenharmony_ci int dummy; 15208c2ecf20Sopenharmony_ci 15218c2ecf20Sopenharmony_ci /* dummy read to bring the device out of sleep */ 15228c2ecf20Sopenharmony_ci regmap_read(st->regmap, LTC2983_STATUS_REG, &dummy); 15238c2ecf20Sopenharmony_ci /* we need to re-assign the channels */ 15248c2ecf20Sopenharmony_ci return ltc2983_setup(st, false); 15258c2ecf20Sopenharmony_ci} 15268c2ecf20Sopenharmony_ci 15278c2ecf20Sopenharmony_cistatic int __maybe_unused ltc2983_suspend(struct device *dev) 15288c2ecf20Sopenharmony_ci{ 15298c2ecf20Sopenharmony_ci struct ltc2983_data *st = spi_get_drvdata(to_spi_device(dev)); 15308c2ecf20Sopenharmony_ci 15318c2ecf20Sopenharmony_ci return regmap_write(st->regmap, LTC2983_STATUS_REG, LTC2983_SLEEP); 15328c2ecf20Sopenharmony_ci} 15338c2ecf20Sopenharmony_ci 15348c2ecf20Sopenharmony_cistatic SIMPLE_DEV_PM_OPS(ltc2983_pm_ops, ltc2983_suspend, ltc2983_resume); 15358c2ecf20Sopenharmony_ci 15368c2ecf20Sopenharmony_cistatic const struct spi_device_id ltc2983_id_table[] = { 15378c2ecf20Sopenharmony_ci { "ltc2983" }, 15388c2ecf20Sopenharmony_ci {}, 15398c2ecf20Sopenharmony_ci}; 15408c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(spi, ltc2983_id_table); 15418c2ecf20Sopenharmony_ci 15428c2ecf20Sopenharmony_cistatic const struct of_device_id ltc2983_of_match[] = { 15438c2ecf20Sopenharmony_ci { .compatible = "adi,ltc2983" }, 15448c2ecf20Sopenharmony_ci {}, 15458c2ecf20Sopenharmony_ci}; 15468c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, ltc2983_of_match); 15478c2ecf20Sopenharmony_ci 15488c2ecf20Sopenharmony_cistatic struct spi_driver ltc2983_driver = { 15498c2ecf20Sopenharmony_ci .driver = { 15508c2ecf20Sopenharmony_ci .name = "ltc2983", 15518c2ecf20Sopenharmony_ci .of_match_table = ltc2983_of_match, 15528c2ecf20Sopenharmony_ci .pm = <c2983_pm_ops, 15538c2ecf20Sopenharmony_ci }, 15548c2ecf20Sopenharmony_ci .probe = ltc2983_probe, 15558c2ecf20Sopenharmony_ci .id_table = ltc2983_id_table, 15568c2ecf20Sopenharmony_ci}; 15578c2ecf20Sopenharmony_ci 15588c2ecf20Sopenharmony_cimodule_spi_driver(ltc2983_driver); 15598c2ecf20Sopenharmony_ci 15608c2ecf20Sopenharmony_ciMODULE_AUTHOR("Nuno Sa <nuno.sa@analog.com>"); 15618c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Analog Devices LTC2983 SPI Temperature sensors"); 15628c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 1563