12b9ea426SAntoniu Miclaus // SPDX-License-Identifier: GPL-2.0 22b9ea426SAntoniu Miclaus /* 32b9ea426SAntoniu Miclaus * Copyright (C) 2023 Analog Devices, Inc. 42b9ea426SAntoniu Miclaus * Author: Antoniu Miclaus <antoniu.miclaus@analog.com> 52b9ea426SAntoniu Miclaus */ 62b9ea426SAntoniu Miclaus 72b9ea426SAntoniu Miclaus #include <linux/bitops.h> 82b9ea426SAntoniu Miclaus #include <linux/err.h> 92b9ea426SAntoniu Miclaus #include <linux/hwmon.h> 102b9ea426SAntoniu Miclaus #include <linux/i2c.h> 112b9ea426SAntoniu Miclaus #include <linux/kernel.h> 122b9ea426SAntoniu Miclaus #include <linux/module.h> 132b9ea426SAntoniu Miclaus #include <linux/property.h> 142b9ea426SAntoniu Miclaus #include <linux/regmap.h> 152b9ea426SAntoniu Miclaus #include <linux/regulator/consumer.h> 162b9ea426SAntoniu Miclaus 172b9ea426SAntoniu Miclaus #define LTC2991_STATUS_LOW 0x00 182b9ea426SAntoniu Miclaus #define LTC2991_CH_EN_TRIGGER 0x01 192b9ea426SAntoniu Miclaus #define LTC2991_V1_V4_CTRL 0x06 202b9ea426SAntoniu Miclaus #define LTC2991_V5_V8_CTRL 0x07 212b9ea426SAntoniu Miclaus #define LTC2991_PWM_TH_LSB_T_INT 0x08 222b9ea426SAntoniu Miclaus #define LTC2991_PWM_TH_MSB 0x09 232b9ea426SAntoniu Miclaus #define LTC2991_CHANNEL_V_MSB(x) (0x0A + ((x) * 2)) 242b9ea426SAntoniu Miclaus #define LTC2991_CHANNEL_T_MSB(x) (0x0A + ((x) * 4)) 252b9ea426SAntoniu Miclaus #define LTC2991_CHANNEL_C_MSB(x) (0x0C + ((x) * 4)) 262b9ea426SAntoniu Miclaus #define LTC2991_T_INT_MSB 0x1A 272b9ea426SAntoniu Miclaus #define LTC2991_VCC_MSB 0x1C 282b9ea426SAntoniu Miclaus 292b9ea426SAntoniu Miclaus #define LTC2991_V7_V8_EN BIT(7) 302b9ea426SAntoniu Miclaus #define LTC2991_V5_V6_EN BIT(6) 312b9ea426SAntoniu Miclaus #define LTC2991_V3_V4_EN BIT(5) 322b9ea426SAntoniu Miclaus #define LTC2991_V1_V2_EN BIT(4) 332b9ea426SAntoniu Miclaus #define LTC2991_T_INT_VCC_EN BIT(3) 342b9ea426SAntoniu Miclaus 352b9ea426SAntoniu Miclaus #define LTC2991_V3_V4_FILT_EN BIT(7) 362b9ea426SAntoniu Miclaus #define LTC2991_V3_V4_TEMP_EN BIT(5) 372b9ea426SAntoniu Miclaus #define LTC2991_V3_V4_DIFF_EN BIT(4) 382b9ea426SAntoniu Miclaus #define LTC2991_V1_V2_FILT_EN BIT(3) 392b9ea426SAntoniu Miclaus #define LTC2991_V1_V2_TEMP_EN BIT(1) 402b9ea426SAntoniu Miclaus #define LTC2991_V1_V2_DIFF_EN BIT(0) 412b9ea426SAntoniu Miclaus 422b9ea426SAntoniu Miclaus #define LTC2991_V7_V8_FILT_EN BIT(7) 432b9ea426SAntoniu Miclaus #define LTC2991_V7_V8_TEMP_EN BIT(5) 442b9ea426SAntoniu Miclaus #define LTC2991_V7_V8_DIFF_EN BIT(4) 452b9ea426SAntoniu Miclaus #define LTC2991_V5_V6_FILT_EN BIT(7) 462b9ea426SAntoniu Miclaus #define LTC2991_V5_V6_TEMP_EN BIT(5) 472b9ea426SAntoniu Miclaus #define LTC2991_V5_V6_DIFF_EN BIT(4) 482b9ea426SAntoniu Miclaus 492b9ea426SAntoniu Miclaus #define LTC2991_REPEAT_ACQ_EN BIT(4) 502b9ea426SAntoniu Miclaus #define LTC2991_T_INT_FILT_EN BIT(3) 512b9ea426SAntoniu Miclaus 522b9ea426SAntoniu Miclaus #define LTC2991_MAX_CHANNEL 4 532b9ea426SAntoniu Miclaus #define LTC2991_T_INT_CH_NR 4 542b9ea426SAntoniu Miclaus #define LTC2991_VCC_CH_NR 0 552b9ea426SAntoniu Miclaus 562b9ea426SAntoniu Miclaus struct ltc2991_state { 572b9ea426SAntoniu Miclaus struct device *dev; 582b9ea426SAntoniu Miclaus struct regmap *regmap; 592b9ea426SAntoniu Miclaus u32 r_sense_uohm[LTC2991_MAX_CHANNEL]; 602b9ea426SAntoniu Miclaus bool temp_en[LTC2991_MAX_CHANNEL]; 612b9ea426SAntoniu Miclaus }; 622b9ea426SAntoniu Miclaus 632b9ea426SAntoniu Miclaus static int ltc2991_read_reg(struct ltc2991_state *st, u8 addr, u8 reg_len, 642b9ea426SAntoniu Miclaus int *val) 652b9ea426SAntoniu Miclaus { 662b9ea426SAntoniu Miclaus __be16 regvals; 672b9ea426SAntoniu Miclaus int ret; 682b9ea426SAntoniu Miclaus 692b9ea426SAntoniu Miclaus if (reg_len < 2) 702b9ea426SAntoniu Miclaus return regmap_read(st->regmap, addr, val); 712b9ea426SAntoniu Miclaus 722b9ea426SAntoniu Miclaus ret = regmap_bulk_read(st->regmap, addr, ®vals, reg_len); 732b9ea426SAntoniu Miclaus if (ret) 742b9ea426SAntoniu Miclaus return ret; 752b9ea426SAntoniu Miclaus 762b9ea426SAntoniu Miclaus *val = be16_to_cpu(regvals); 772b9ea426SAntoniu Miclaus 782b9ea426SAntoniu Miclaus return 0; 792b9ea426SAntoniu Miclaus } 802b9ea426SAntoniu Miclaus 812b9ea426SAntoniu Miclaus static int ltc2991_get_voltage(struct ltc2991_state *st, u32 reg, long *val) 822b9ea426SAntoniu Miclaus { 832b9ea426SAntoniu Miclaus int reg_val, ret, offset = 0; 842b9ea426SAntoniu Miclaus 852b9ea426SAntoniu Miclaus ret = ltc2991_read_reg(st, reg, 2, ®_val); 862b9ea426SAntoniu Miclaus if (ret) 872b9ea426SAntoniu Miclaus return ret; 882b9ea426SAntoniu Miclaus 892b9ea426SAntoniu Miclaus if (reg == LTC2991_VCC_MSB) 902b9ea426SAntoniu Miclaus /* Vcc 2.5V offset */ 912b9ea426SAntoniu Miclaus offset = 2500; 922b9ea426SAntoniu Miclaus 932b9ea426SAntoniu Miclaus /* Vx, 305.18uV/LSB */ 942b9ea426SAntoniu Miclaus *val = DIV_ROUND_CLOSEST(sign_extend32(reg_val, 14) * 30518, 952b9ea426SAntoniu Miclaus 1000 * 100) + offset; 962b9ea426SAntoniu Miclaus 972b9ea426SAntoniu Miclaus return 0; 982b9ea426SAntoniu Miclaus } 992b9ea426SAntoniu Miclaus 1002b9ea426SAntoniu Miclaus static int ltc2991_read_in(struct device *dev, u32 attr, int channel, long *val) 1012b9ea426SAntoniu Miclaus { 1022b9ea426SAntoniu Miclaus struct ltc2991_state *st = dev_get_drvdata(dev); 1032b9ea426SAntoniu Miclaus u32 reg; 1042b9ea426SAntoniu Miclaus 1052b9ea426SAntoniu Miclaus switch (attr) { 1062b9ea426SAntoniu Miclaus case hwmon_in_input: 1072b9ea426SAntoniu Miclaus if (channel == LTC2991_VCC_CH_NR) 1082b9ea426SAntoniu Miclaus reg = LTC2991_VCC_MSB; 1092b9ea426SAntoniu Miclaus else 1102b9ea426SAntoniu Miclaus reg = LTC2991_CHANNEL_V_MSB(channel - 1); 1112b9ea426SAntoniu Miclaus 1122b9ea426SAntoniu Miclaus return ltc2991_get_voltage(st, reg, val); 1132b9ea426SAntoniu Miclaus default: 1142b9ea426SAntoniu Miclaus return -EOPNOTSUPP; 1152b9ea426SAntoniu Miclaus } 1162b9ea426SAntoniu Miclaus } 1172b9ea426SAntoniu Miclaus 1182b9ea426SAntoniu Miclaus static int ltc2991_get_curr(struct ltc2991_state *st, u32 reg, int channel, 1192b9ea426SAntoniu Miclaus long *val) 1202b9ea426SAntoniu Miclaus { 1212b9ea426SAntoniu Miclaus int reg_val, ret; 1222b9ea426SAntoniu Miclaus 1232b9ea426SAntoniu Miclaus ret = ltc2991_read_reg(st, reg, 2, ®_val); 1242b9ea426SAntoniu Miclaus if (ret) 1252b9ea426SAntoniu Miclaus return ret; 1262b9ea426SAntoniu Miclaus 1272b9ea426SAntoniu Miclaus /* Vx-Vy, 19.075uV/LSB */ 1282b9ea426SAntoniu Miclaus *val = DIV_ROUND_CLOSEST(sign_extend32(reg_val, 14) * 19075, 1292b9ea426SAntoniu Miclaus st->r_sense_uohm[channel]); 1302b9ea426SAntoniu Miclaus 1312b9ea426SAntoniu Miclaus return 0; 1322b9ea426SAntoniu Miclaus } 1332b9ea426SAntoniu Miclaus 1342b9ea426SAntoniu Miclaus static int ltc2991_read_curr(struct device *dev, u32 attr, int channel, 1352b9ea426SAntoniu Miclaus long *val) 1362b9ea426SAntoniu Miclaus { 1372b9ea426SAntoniu Miclaus struct ltc2991_state *st = dev_get_drvdata(dev); 1382b9ea426SAntoniu Miclaus u32 reg; 1392b9ea426SAntoniu Miclaus 1402b9ea426SAntoniu Miclaus switch (attr) { 1412b9ea426SAntoniu Miclaus case hwmon_curr_input: 1422b9ea426SAntoniu Miclaus reg = LTC2991_CHANNEL_C_MSB(channel); 1432b9ea426SAntoniu Miclaus return ltc2991_get_curr(st, reg, channel, val); 1442b9ea426SAntoniu Miclaus default: 1452b9ea426SAntoniu Miclaus return -EOPNOTSUPP; 1462b9ea426SAntoniu Miclaus } 1472b9ea426SAntoniu Miclaus } 1482b9ea426SAntoniu Miclaus 1492b9ea426SAntoniu Miclaus static int ltc2991_get_temp(struct ltc2991_state *st, u32 reg, int channel, 1502b9ea426SAntoniu Miclaus long *val) 1512b9ea426SAntoniu Miclaus { 1522b9ea426SAntoniu Miclaus int reg_val, ret; 1532b9ea426SAntoniu Miclaus 1542b9ea426SAntoniu Miclaus ret = ltc2991_read_reg(st, reg, 2, ®_val); 1552b9ea426SAntoniu Miclaus if (ret) 1562b9ea426SAntoniu Miclaus return ret; 1572b9ea426SAntoniu Miclaus 1582b9ea426SAntoniu Miclaus /* Temp LSB = 0.0625 Degrees */ 1592b9ea426SAntoniu Miclaus *val = DIV_ROUND_CLOSEST(sign_extend32(reg_val, 12) * 1000, 16); 1602b9ea426SAntoniu Miclaus 1612b9ea426SAntoniu Miclaus return 0; 1622b9ea426SAntoniu Miclaus } 1632b9ea426SAntoniu Miclaus 1642b9ea426SAntoniu Miclaus static int ltc2991_read_temp(struct device *dev, u32 attr, int channel, 1652b9ea426SAntoniu Miclaus long *val) 1662b9ea426SAntoniu Miclaus { 1672b9ea426SAntoniu Miclaus struct ltc2991_state *st = dev_get_drvdata(dev); 1682b9ea426SAntoniu Miclaus u32 reg; 1692b9ea426SAntoniu Miclaus 1702b9ea426SAntoniu Miclaus switch (attr) { 1712b9ea426SAntoniu Miclaus case hwmon_temp_input: 1722b9ea426SAntoniu Miclaus if (channel == LTC2991_T_INT_CH_NR) 1732b9ea426SAntoniu Miclaus reg = LTC2991_T_INT_MSB; 1742b9ea426SAntoniu Miclaus else 1752b9ea426SAntoniu Miclaus reg = LTC2991_CHANNEL_T_MSB(channel); 1762b9ea426SAntoniu Miclaus 1772b9ea426SAntoniu Miclaus return ltc2991_get_temp(st, reg, channel, val); 1782b9ea426SAntoniu Miclaus default: 1792b9ea426SAntoniu Miclaus return -EOPNOTSUPP; 1802b9ea426SAntoniu Miclaus } 1812b9ea426SAntoniu Miclaus } 1822b9ea426SAntoniu Miclaus 1832b9ea426SAntoniu Miclaus static int ltc2991_read(struct device *dev, enum hwmon_sensor_types type, 1842b9ea426SAntoniu Miclaus u32 attr, int channel, long *val) 1852b9ea426SAntoniu Miclaus { 1862b9ea426SAntoniu Miclaus switch (type) { 1872b9ea426SAntoniu Miclaus case hwmon_in: 1882b9ea426SAntoniu Miclaus return ltc2991_read_in(dev, attr, channel, val); 1892b9ea426SAntoniu Miclaus case hwmon_curr: 1902b9ea426SAntoniu Miclaus return ltc2991_read_curr(dev, attr, channel, val); 1912b9ea426SAntoniu Miclaus case hwmon_temp: 1922b9ea426SAntoniu Miclaus return ltc2991_read_temp(dev, attr, channel, val); 1932b9ea426SAntoniu Miclaus default: 1942b9ea426SAntoniu Miclaus return -EOPNOTSUPP; 1952b9ea426SAntoniu Miclaus } 1962b9ea426SAntoniu Miclaus } 1972b9ea426SAntoniu Miclaus 1982b9ea426SAntoniu Miclaus static umode_t ltc2991_is_visible(const void *data, 1992b9ea426SAntoniu Miclaus enum hwmon_sensor_types type, u32 attr, 2002b9ea426SAntoniu Miclaus int channel) 2012b9ea426SAntoniu Miclaus { 2022b9ea426SAntoniu Miclaus const struct ltc2991_state *st = data; 2032b9ea426SAntoniu Miclaus 2042b9ea426SAntoniu Miclaus switch (type) { 2052b9ea426SAntoniu Miclaus case hwmon_in: 2062b9ea426SAntoniu Miclaus switch (attr) { 2072b9ea426SAntoniu Miclaus case hwmon_in_input: 2082b9ea426SAntoniu Miclaus if (channel == LTC2991_VCC_CH_NR) 2092b9ea426SAntoniu Miclaus return 0444; 2102b9ea426SAntoniu Miclaus if (st->temp_en[(channel - 1) / 2]) 2112b9ea426SAntoniu Miclaus break; 2122b9ea426SAntoniu Miclaus if (channel % 2) 2132b9ea426SAntoniu Miclaus return 0444; 2142b9ea426SAntoniu Miclaus if (!st->r_sense_uohm[(channel - 1) / 2]) 2152b9ea426SAntoniu Miclaus return 0444; 2162b9ea426SAntoniu Miclaus } 2172b9ea426SAntoniu Miclaus break; 2182b9ea426SAntoniu Miclaus case hwmon_curr: 2192b9ea426SAntoniu Miclaus switch (attr) { 2202b9ea426SAntoniu Miclaus case hwmon_curr_input: 2212b9ea426SAntoniu Miclaus if (st->r_sense_uohm[channel]) 2222b9ea426SAntoniu Miclaus return 0444; 2232b9ea426SAntoniu Miclaus break; 2242b9ea426SAntoniu Miclaus } 2252b9ea426SAntoniu Miclaus break; 2262b9ea426SAntoniu Miclaus case hwmon_temp: 2272b9ea426SAntoniu Miclaus switch (attr) { 2282b9ea426SAntoniu Miclaus case hwmon_temp_input: 2292b9ea426SAntoniu Miclaus if (st->temp_en[channel] || 2302b9ea426SAntoniu Miclaus channel == LTC2991_T_INT_CH_NR) 2312b9ea426SAntoniu Miclaus return 0444; 2322b9ea426SAntoniu Miclaus break; 2332b9ea426SAntoniu Miclaus } 2342b9ea426SAntoniu Miclaus break; 2352b9ea426SAntoniu Miclaus default: 2362b9ea426SAntoniu Miclaus break; 2372b9ea426SAntoniu Miclaus } 2382b9ea426SAntoniu Miclaus 2392b9ea426SAntoniu Miclaus return 0; 2402b9ea426SAntoniu Miclaus } 2412b9ea426SAntoniu Miclaus 2422b9ea426SAntoniu Miclaus static const struct hwmon_ops ltc2991_hwmon_ops = { 2432b9ea426SAntoniu Miclaus .is_visible = ltc2991_is_visible, 2442b9ea426SAntoniu Miclaus .read = ltc2991_read, 2452b9ea426SAntoniu Miclaus }; 2462b9ea426SAntoniu Miclaus 2472b9ea426SAntoniu Miclaus static const struct hwmon_channel_info *ltc2991_info[] = { 2482b9ea426SAntoniu Miclaus HWMON_CHANNEL_INFO(temp, 2492b9ea426SAntoniu Miclaus HWMON_T_INPUT, 2502b9ea426SAntoniu Miclaus HWMON_T_INPUT, 2512b9ea426SAntoniu Miclaus HWMON_T_INPUT, 2522b9ea426SAntoniu Miclaus HWMON_T_INPUT, 2532b9ea426SAntoniu Miclaus HWMON_T_INPUT 2542b9ea426SAntoniu Miclaus ), 2552b9ea426SAntoniu Miclaus HWMON_CHANNEL_INFO(curr, 2562b9ea426SAntoniu Miclaus HWMON_C_INPUT, 2572b9ea426SAntoniu Miclaus HWMON_C_INPUT, 2582b9ea426SAntoniu Miclaus HWMON_C_INPUT, 2592b9ea426SAntoniu Miclaus HWMON_C_INPUT 2602b9ea426SAntoniu Miclaus ), 2612b9ea426SAntoniu Miclaus HWMON_CHANNEL_INFO(in, 2622b9ea426SAntoniu Miclaus HWMON_I_INPUT, 2632b9ea426SAntoniu Miclaus HWMON_I_INPUT, 2642b9ea426SAntoniu Miclaus HWMON_I_INPUT, 2652b9ea426SAntoniu Miclaus HWMON_I_INPUT, 2662b9ea426SAntoniu Miclaus HWMON_I_INPUT, 2672b9ea426SAntoniu Miclaus HWMON_I_INPUT, 2682b9ea426SAntoniu Miclaus HWMON_I_INPUT, 2692b9ea426SAntoniu Miclaus HWMON_I_INPUT, 2702b9ea426SAntoniu Miclaus HWMON_I_INPUT 2712b9ea426SAntoniu Miclaus ), 2722b9ea426SAntoniu Miclaus NULL 2732b9ea426SAntoniu Miclaus }; 2742b9ea426SAntoniu Miclaus 2752b9ea426SAntoniu Miclaus static const struct hwmon_chip_info ltc2991_chip_info = { 2762b9ea426SAntoniu Miclaus .ops = <c2991_hwmon_ops, 2772b9ea426SAntoniu Miclaus .info = ltc2991_info, 2782b9ea426SAntoniu Miclaus }; 2792b9ea426SAntoniu Miclaus 2802b9ea426SAntoniu Miclaus static const struct regmap_config ltc2991_regmap_config = { 2812b9ea426SAntoniu Miclaus .reg_bits = 8, 2822b9ea426SAntoniu Miclaus .val_bits = 8, 2832b9ea426SAntoniu Miclaus .max_register = 0x1D, 2842b9ea426SAntoniu Miclaus }; 2852b9ea426SAntoniu Miclaus 2862b9ea426SAntoniu Miclaus static int ltc2991_init(struct ltc2991_state *st) 2872b9ea426SAntoniu Miclaus { 2882b9ea426SAntoniu Miclaus struct fwnode_handle *child; 2892b9ea426SAntoniu Miclaus int ret; 2902b9ea426SAntoniu Miclaus u32 val, addr; 2912b9ea426SAntoniu Miclaus u8 v5_v8_reg_data = 0, v1_v4_reg_data = 0; 2922b9ea426SAntoniu Miclaus 2932b9ea426SAntoniu Miclaus ret = devm_regulator_get_enable(st->dev, "vcc"); 2942b9ea426SAntoniu Miclaus if (ret) 2952b9ea426SAntoniu Miclaus return dev_err_probe(st->dev, ret, 2962b9ea426SAntoniu Miclaus "failed to enable regulator\n"); 2972b9ea426SAntoniu Miclaus 2982b9ea426SAntoniu Miclaus device_for_each_child_node(st->dev, child) { 2992b9ea426SAntoniu Miclaus ret = fwnode_property_read_u32(child, "reg", &addr); 3002b9ea426SAntoniu Miclaus if (ret < 0) { 3012b9ea426SAntoniu Miclaus fwnode_handle_put(child); 3022b9ea426SAntoniu Miclaus return ret; 3032b9ea426SAntoniu Miclaus } 3042b9ea426SAntoniu Miclaus 3052b9ea426SAntoniu Miclaus if (addr > 3) { 3062b9ea426SAntoniu Miclaus fwnode_handle_put(child); 3072b9ea426SAntoniu Miclaus return -EINVAL; 3082b9ea426SAntoniu Miclaus } 3092b9ea426SAntoniu Miclaus 3102b9ea426SAntoniu Miclaus ret = fwnode_property_read_u32(child, 3112b9ea426SAntoniu Miclaus "shunt-resistor-micro-ohms", 3122b9ea426SAntoniu Miclaus &val); 3132b9ea426SAntoniu Miclaus if (!ret) { 3142b9ea426SAntoniu Miclaus if (!val) 3152b9ea426SAntoniu Miclaus return dev_err_probe(st->dev, -EINVAL, 3162b9ea426SAntoniu Miclaus "shunt resistor value cannot be zero\n"); 3172b9ea426SAntoniu Miclaus 3182b9ea426SAntoniu Miclaus st->r_sense_uohm[addr] = val; 3192b9ea426SAntoniu Miclaus 3202b9ea426SAntoniu Miclaus switch (addr) { 3212b9ea426SAntoniu Miclaus case 0: 3222b9ea426SAntoniu Miclaus v1_v4_reg_data |= LTC2991_V1_V2_DIFF_EN; 3232b9ea426SAntoniu Miclaus break; 3242b9ea426SAntoniu Miclaus case 1: 3252b9ea426SAntoniu Miclaus v1_v4_reg_data |= LTC2991_V3_V4_DIFF_EN; 3262b9ea426SAntoniu Miclaus break; 3272b9ea426SAntoniu Miclaus case 2: 3282b9ea426SAntoniu Miclaus v5_v8_reg_data |= LTC2991_V5_V6_DIFF_EN; 3292b9ea426SAntoniu Miclaus break; 3302b9ea426SAntoniu Miclaus case 3: 3312b9ea426SAntoniu Miclaus v5_v8_reg_data |= LTC2991_V7_V8_DIFF_EN; 3322b9ea426SAntoniu Miclaus break; 3332b9ea426SAntoniu Miclaus default: 3342b9ea426SAntoniu Miclaus break; 3352b9ea426SAntoniu Miclaus } 3362b9ea426SAntoniu Miclaus } 3372b9ea426SAntoniu Miclaus 3382b9ea426SAntoniu Miclaus ret = fwnode_property_read_bool(child, 3392b9ea426SAntoniu Miclaus "adi,temperature-enable"); 3402b9ea426SAntoniu Miclaus if (ret) { 3412b9ea426SAntoniu Miclaus st->temp_en[addr] = ret; 3422b9ea426SAntoniu Miclaus 3432b9ea426SAntoniu Miclaus switch (addr) { 3442b9ea426SAntoniu Miclaus case 0: 3452b9ea426SAntoniu Miclaus v1_v4_reg_data |= LTC2991_V1_V2_TEMP_EN; 3462b9ea426SAntoniu Miclaus break; 3472b9ea426SAntoniu Miclaus case 1: 3482b9ea426SAntoniu Miclaus v1_v4_reg_data |= LTC2991_V3_V4_TEMP_EN; 3492b9ea426SAntoniu Miclaus break; 3502b9ea426SAntoniu Miclaus case 2: 3512b9ea426SAntoniu Miclaus v5_v8_reg_data |= LTC2991_V5_V6_TEMP_EN; 3522b9ea426SAntoniu Miclaus break; 3532b9ea426SAntoniu Miclaus case 3: 3542b9ea426SAntoniu Miclaus v5_v8_reg_data |= LTC2991_V7_V8_TEMP_EN; 3552b9ea426SAntoniu Miclaus break; 3562b9ea426SAntoniu Miclaus default: 3572b9ea426SAntoniu Miclaus break; 3582b9ea426SAntoniu Miclaus } 3592b9ea426SAntoniu Miclaus } 3602b9ea426SAntoniu Miclaus } 3612b9ea426SAntoniu Miclaus 3622b9ea426SAntoniu Miclaus ret = regmap_write(st->regmap, LTC2991_V5_V8_CTRL, v5_v8_reg_data); 3632b9ea426SAntoniu Miclaus if (ret) 3642b9ea426SAntoniu Miclaus return dev_err_probe(st->dev, ret, 3652b9ea426SAntoniu Miclaus "Error: Failed to set V5-V8 CTRL reg.\n"); 3662b9ea426SAntoniu Miclaus 3672b9ea426SAntoniu Miclaus ret = regmap_write(st->regmap, LTC2991_V1_V4_CTRL, v1_v4_reg_data); 3682b9ea426SAntoniu Miclaus if (ret) 3692b9ea426SAntoniu Miclaus return dev_err_probe(st->dev, ret, 3702b9ea426SAntoniu Miclaus "Error: Failed to set V1-V4 CTRL reg.\n"); 3712b9ea426SAntoniu Miclaus 3722b9ea426SAntoniu Miclaus ret = regmap_write(st->regmap, LTC2991_PWM_TH_LSB_T_INT, 3732b9ea426SAntoniu Miclaus LTC2991_REPEAT_ACQ_EN); 3742b9ea426SAntoniu Miclaus if (ret) 3752b9ea426SAntoniu Miclaus return dev_err_probe(st->dev, ret, 376*d9995cd9SColin Ian King "Error: Failed to set continuous mode.\n"); 3772b9ea426SAntoniu Miclaus 3782b9ea426SAntoniu Miclaus /* Enable all channels and trigger conversions */ 3792b9ea426SAntoniu Miclaus return regmap_write(st->regmap, LTC2991_CH_EN_TRIGGER, 3802b9ea426SAntoniu Miclaus LTC2991_V7_V8_EN | LTC2991_V5_V6_EN | 3812b9ea426SAntoniu Miclaus LTC2991_V3_V4_EN | LTC2991_V1_V2_EN | 3822b9ea426SAntoniu Miclaus LTC2991_T_INT_VCC_EN); 3832b9ea426SAntoniu Miclaus } 3842b9ea426SAntoniu Miclaus 3852b9ea426SAntoniu Miclaus static int ltc2991_i2c_probe(struct i2c_client *client) 3862b9ea426SAntoniu Miclaus { 3872b9ea426SAntoniu Miclaus int ret; 3882b9ea426SAntoniu Miclaus struct device *hwmon_dev; 3892b9ea426SAntoniu Miclaus struct ltc2991_state *st; 3902b9ea426SAntoniu Miclaus 3912b9ea426SAntoniu Miclaus st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL); 3922b9ea426SAntoniu Miclaus if (!st) 3932b9ea426SAntoniu Miclaus return -ENOMEM; 3942b9ea426SAntoniu Miclaus 3952b9ea426SAntoniu Miclaus st->dev = &client->dev; 3962b9ea426SAntoniu Miclaus st->regmap = devm_regmap_init_i2c(client, <c2991_regmap_config); 3972b9ea426SAntoniu Miclaus if (IS_ERR(st->regmap)) 3982b9ea426SAntoniu Miclaus return PTR_ERR(st->regmap); 3992b9ea426SAntoniu Miclaus 4002b9ea426SAntoniu Miclaus ret = ltc2991_init(st); 4012b9ea426SAntoniu Miclaus if (ret) 4022b9ea426SAntoniu Miclaus return ret; 4032b9ea426SAntoniu Miclaus 4042b9ea426SAntoniu Miclaus hwmon_dev = devm_hwmon_device_register_with_info(&client->dev, 4052b9ea426SAntoniu Miclaus client->name, st, 4062b9ea426SAntoniu Miclaus <c2991_chip_info, 4072b9ea426SAntoniu Miclaus NULL); 4082b9ea426SAntoniu Miclaus 4092b9ea426SAntoniu Miclaus return PTR_ERR_OR_ZERO(hwmon_dev); 4102b9ea426SAntoniu Miclaus } 4112b9ea426SAntoniu Miclaus 4122b9ea426SAntoniu Miclaus static const struct of_device_id ltc2991_of_match[] = { 4132b9ea426SAntoniu Miclaus { .compatible = "adi,ltc2991" }, 4142b9ea426SAntoniu Miclaus { } 4152b9ea426SAntoniu Miclaus }; 4162b9ea426SAntoniu Miclaus MODULE_DEVICE_TABLE(of, ltc2991_of_match); 4172b9ea426SAntoniu Miclaus 4182b9ea426SAntoniu Miclaus static const struct i2c_device_id ltc2991_i2c_id[] = { 4192b9ea426SAntoniu Miclaus { "ltc2991", 0 }, 4202b9ea426SAntoniu Miclaus {} 4212b9ea426SAntoniu Miclaus }; 4222b9ea426SAntoniu Miclaus MODULE_DEVICE_TABLE(i2c, ltc2991_i2c_id); 4232b9ea426SAntoniu Miclaus 4242b9ea426SAntoniu Miclaus static struct i2c_driver ltc2991_i2c_driver = { 4252b9ea426SAntoniu Miclaus .driver = { 4262b9ea426SAntoniu Miclaus .name = "ltc2991", 4272b9ea426SAntoniu Miclaus .of_match_table = ltc2991_of_match, 4282b9ea426SAntoniu Miclaus }, 4292b9ea426SAntoniu Miclaus .probe = ltc2991_i2c_probe, 4302b9ea426SAntoniu Miclaus .id_table = ltc2991_i2c_id, 4312b9ea426SAntoniu Miclaus }; 4322b9ea426SAntoniu Miclaus 4332b9ea426SAntoniu Miclaus module_i2c_driver(ltc2991_i2c_driver); 4342b9ea426SAntoniu Miclaus 4352b9ea426SAntoniu Miclaus MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>"); 4362b9ea426SAntoniu Miclaus MODULE_DESCRIPTION("Analog Devices LTC2991 HWMON Driver"); 4372b9ea426SAntoniu Miclaus MODULE_LICENSE("GPL"); 438