Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for Linear Technology LTC4222 Dual Hot Swap controller
0004  *
0005  * Copyright (c) 2014 Guenter Roeck
0006  */
0007 
0008 #include <linux/kernel.h>
0009 #include <linux/module.h>
0010 #include <linux/err.h>
0011 #include <linux/slab.h>
0012 #include <linux/bitops.h>
0013 #include <linux/i2c.h>
0014 #include <linux/hwmon.h>
0015 #include <linux/hwmon-sysfs.h>
0016 #include <linux/jiffies.h>
0017 #include <linux/regmap.h>
0018 
0019 /* chip registers */
0020 
0021 #define LTC4222_CONTROL1    0xd0
0022 #define LTC4222_ALERT1      0xd1
0023 #define LTC4222_STATUS1     0xd2
0024 #define LTC4222_FAULT1      0xd3
0025 #define LTC4222_CONTROL2    0xd4
0026 #define LTC4222_ALERT2      0xd5
0027 #define LTC4222_STATUS2     0xd6
0028 #define LTC4222_FAULT2      0xd7
0029 #define LTC4222_SOURCE1     0xd8
0030 #define LTC4222_SOURCE2     0xda
0031 #define LTC4222_ADIN1       0xdc
0032 #define LTC4222_ADIN2       0xde
0033 #define LTC4222_SENSE1      0xe0
0034 #define LTC4222_SENSE2      0xe2
0035 #define LTC4222_ADC_CONTROL 0xe4
0036 
0037 /*
0038  * Fault register bits
0039  */
0040 #define FAULT_OV    BIT(0)
0041 #define FAULT_UV    BIT(1)
0042 #define FAULT_OC    BIT(2)
0043 #define FAULT_POWER_BAD BIT(3)
0044 #define FAULT_FET_BAD   BIT(5)
0045 
0046 /* Return the voltage from the given register in mV or mA */
0047 static int ltc4222_get_value(struct device *dev, u8 reg)
0048 {
0049     struct regmap *regmap = dev_get_drvdata(dev);
0050     unsigned int val;
0051     u8 buf[2];
0052     int ret;
0053 
0054     ret = regmap_bulk_read(regmap, reg, buf, 2);
0055     if (ret < 0)
0056         return ret;
0057 
0058     val = ((buf[0] << 8) + buf[1]) >> 6;
0059 
0060     switch (reg) {
0061     case LTC4222_ADIN1:
0062     case LTC4222_ADIN2:
0063         /* 1.25 mV resolution. Convert to mV. */
0064         val = DIV_ROUND_CLOSEST(val * 5, 4);
0065         break;
0066     case LTC4222_SOURCE1:
0067     case LTC4222_SOURCE2:
0068         /* 31.25 mV resolution. Convert to mV. */
0069         val = DIV_ROUND_CLOSEST(val * 125, 4);
0070         break;
0071     case LTC4222_SENSE1:
0072     case LTC4222_SENSE2:
0073         /*
0074          * 62.5 uV resolution. Convert to current as measured with
0075          * an 1 mOhm sense resistor, in mA. If a different sense
0076          * resistor is installed, calculate the actual current by
0077          * dividing the reported current by the sense resistor value
0078          * in mOhm.
0079          */
0080         val = DIV_ROUND_CLOSEST(val * 125, 2);
0081         break;
0082     default:
0083         return -EINVAL;
0084     }
0085     return val;
0086 }
0087 
0088 static ssize_t ltc4222_value_show(struct device *dev,
0089                   struct device_attribute *da, char *buf)
0090 {
0091     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0092     int value;
0093 
0094     value = ltc4222_get_value(dev, attr->index);
0095     if (value < 0)
0096         return value;
0097     return sysfs_emit(buf, "%d\n", value);
0098 }
0099 
0100 static ssize_t ltc4222_bool_show(struct device *dev,
0101                  struct device_attribute *da, char *buf)
0102 {
0103     struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
0104     struct regmap *regmap = dev_get_drvdata(dev);
0105     unsigned int fault;
0106     int ret;
0107 
0108     ret = regmap_read(regmap, attr->nr, &fault);
0109     if (ret < 0)
0110         return ret;
0111     fault &= attr->index;
0112     if (fault)      /* Clear reported faults in chip register */
0113         regmap_update_bits(regmap, attr->nr, attr->index, 0);
0114 
0115     return sysfs_emit(buf, "%d\n", !!fault);
0116 }
0117 
0118 /* Voltages */
0119 static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4222_value, LTC4222_SOURCE1);
0120 static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4222_value, LTC4222_ADIN1);
0121 static SENSOR_DEVICE_ATTR_RO(in3_input, ltc4222_value, LTC4222_SOURCE2);
0122 static SENSOR_DEVICE_ATTR_RO(in4_input, ltc4222_value, LTC4222_ADIN2);
0123 
0124 /*
0125  * Voltage alarms
0126  * UV/OV faults are associated with the input voltage, and power bad and fet
0127  * faults are associated with the output voltage.
0128  */
0129 static SENSOR_DEVICE_ATTR_2_RO(in1_min_alarm, ltc4222_bool, LTC4222_FAULT1,
0130                    FAULT_UV);
0131 static SENSOR_DEVICE_ATTR_2_RO(in1_max_alarm, ltc4222_bool, LTC4222_FAULT1,
0132                    FAULT_OV);
0133 static SENSOR_DEVICE_ATTR_2_RO(in2_alarm, ltc4222_bool, LTC4222_FAULT1,
0134                    FAULT_POWER_BAD | FAULT_FET_BAD);
0135 
0136 static SENSOR_DEVICE_ATTR_2_RO(in3_min_alarm, ltc4222_bool, LTC4222_FAULT2,
0137                    FAULT_UV);
0138 static SENSOR_DEVICE_ATTR_2_RO(in3_max_alarm, ltc4222_bool, LTC4222_FAULT2,
0139                    FAULT_OV);
0140 static SENSOR_DEVICE_ATTR_2_RO(in4_alarm, ltc4222_bool, LTC4222_FAULT2,
0141                    FAULT_POWER_BAD | FAULT_FET_BAD);
0142 
0143 /* Current (via sense resistor) */
0144 static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4222_value, LTC4222_SENSE1);
0145 static SENSOR_DEVICE_ATTR_RO(curr2_input, ltc4222_value, LTC4222_SENSE2);
0146 
0147 /* Overcurrent alarm */
0148 static SENSOR_DEVICE_ATTR_2_RO(curr1_max_alarm, ltc4222_bool, LTC4222_FAULT1,
0149                    FAULT_OC);
0150 static SENSOR_DEVICE_ATTR_2_RO(curr2_max_alarm, ltc4222_bool, LTC4222_FAULT2,
0151                    FAULT_OC);
0152 
0153 static struct attribute *ltc4222_attrs[] = {
0154     &sensor_dev_attr_in1_input.dev_attr.attr,
0155     &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
0156     &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
0157     &sensor_dev_attr_in2_input.dev_attr.attr,
0158     &sensor_dev_attr_in2_alarm.dev_attr.attr,
0159     &sensor_dev_attr_in3_input.dev_attr.attr,
0160     &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
0161     &sensor_dev_attr_in3_max_alarm.dev_attr.attr,
0162     &sensor_dev_attr_in4_input.dev_attr.attr,
0163     &sensor_dev_attr_in4_alarm.dev_attr.attr,
0164 
0165     &sensor_dev_attr_curr1_input.dev_attr.attr,
0166     &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
0167     &sensor_dev_attr_curr2_input.dev_attr.attr,
0168     &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
0169 
0170     NULL,
0171 };
0172 ATTRIBUTE_GROUPS(ltc4222);
0173 
0174 static const struct regmap_config ltc4222_regmap_config = {
0175     .reg_bits = 8,
0176     .val_bits = 8,
0177     .max_register = LTC4222_ADC_CONTROL,
0178 };
0179 
0180 static int ltc4222_probe(struct i2c_client *client)
0181 {
0182     struct device *dev = &client->dev;
0183     struct device *hwmon_dev;
0184     struct regmap *regmap;
0185 
0186     regmap = devm_regmap_init_i2c(client, &ltc4222_regmap_config);
0187     if (IS_ERR(regmap)) {
0188         dev_err(dev, "failed to allocate register map\n");
0189         return PTR_ERR(regmap);
0190     }
0191 
0192     /* Clear faults */
0193     regmap_write(regmap, LTC4222_FAULT1, 0x00);
0194     regmap_write(regmap, LTC4222_FAULT2, 0x00);
0195 
0196     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0197                                regmap,
0198                                ltc4222_groups);
0199     return PTR_ERR_OR_ZERO(hwmon_dev);
0200 }
0201 
0202 static const struct i2c_device_id ltc4222_id[] = {
0203     {"ltc4222", 0},
0204     { }
0205 };
0206 
0207 MODULE_DEVICE_TABLE(i2c, ltc4222_id);
0208 
0209 static struct i2c_driver ltc4222_driver = {
0210     .driver = {
0211            .name = "ltc4222",
0212            },
0213     .probe_new = ltc4222_probe,
0214     .id_table = ltc4222_id,
0215 };
0216 
0217 module_i2c_driver(ltc4222_driver);
0218 
0219 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
0220 MODULE_DESCRIPTION("LTC4222 driver");
0221 MODULE_LICENSE("GPL");