Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller
0004  *
0005  * Copyright (C) 2010 Ericsson AB.
0006  *
0007  * Derived from:
0008  *
0009  *  Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
0010  *  Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
0011  *
0012  * Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf
0013  */
0014 
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/init.h>
0018 #include <linux/err.h>
0019 #include <linux/slab.h>
0020 #include <linux/i2c.h>
0021 #include <linux/hwmon.h>
0022 #include <linux/hwmon-sysfs.h>
0023 #include <linux/jiffies.h>
0024 
0025 /* chip registers */
0026 #define LTC4261_STATUS  0x00    /* readonly */
0027 #define LTC4261_FAULT   0x01
0028 #define LTC4261_ALERT   0x02
0029 #define LTC4261_CONTROL 0x03
0030 #define LTC4261_SENSE_H 0x04
0031 #define LTC4261_SENSE_L 0x05
0032 #define LTC4261_ADIN2_H 0x06
0033 #define LTC4261_ADIN2_L 0x07
0034 #define LTC4261_ADIN_H  0x08
0035 #define LTC4261_ADIN_L  0x09
0036 
0037 /*
0038  * Fault register bits
0039  */
0040 #define FAULT_OV    (1<<0)
0041 #define FAULT_UV    (1<<1)
0042 #define FAULT_OC    (1<<2)
0043 
0044 struct ltc4261_data {
0045     struct i2c_client *client;
0046 
0047     struct mutex update_lock;
0048     bool valid;
0049     unsigned long last_updated; /* in jiffies */
0050 
0051     /* Registers */
0052     u8 regs[10];
0053 };
0054 
0055 static struct ltc4261_data *ltc4261_update_device(struct device *dev)
0056 {
0057     struct ltc4261_data *data = dev_get_drvdata(dev);
0058     struct i2c_client *client = data->client;
0059     struct ltc4261_data *ret = data;
0060 
0061     mutex_lock(&data->update_lock);
0062 
0063     if (time_after(jiffies, data->last_updated + HZ / 4) || !data->valid) {
0064         int i;
0065 
0066         /* Read registers -- 0x00 to 0x09 */
0067         for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
0068             int val;
0069 
0070             val = i2c_smbus_read_byte_data(client, i);
0071             if (unlikely(val < 0)) {
0072                 dev_dbg(dev,
0073                     "Failed to read ADC value: error %d\n",
0074                     val);
0075                 ret = ERR_PTR(val);
0076                 data->valid = false;
0077                 goto abort;
0078             }
0079             data->regs[i] = val;
0080         }
0081         data->last_updated = jiffies;
0082         data->valid = true;
0083     }
0084 abort:
0085     mutex_unlock(&data->update_lock);
0086     return ret;
0087 }
0088 
0089 /* Return the voltage from the given register in mV or mA */
0090 static int ltc4261_get_value(struct ltc4261_data *data, u8 reg)
0091 {
0092     u32 val;
0093 
0094     val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6);
0095 
0096     switch (reg) {
0097     case LTC4261_ADIN_H:
0098     case LTC4261_ADIN2_H:
0099         /* 2.5mV resolution. Convert to mV. */
0100         val = val * 25 / 10;
0101         break;
0102     case LTC4261_SENSE_H:
0103         /*
0104          * 62.5uV resolution. Convert to current as measured with
0105          * an 1 mOhm sense resistor, in mA. If a different sense
0106          * resistor is installed, calculate the actual current by
0107          * dividing the reported current by the sense resistor value
0108          * in mOhm.
0109          */
0110         val = val * 625 / 10;
0111         break;
0112     default:
0113         /* If we get here, the developer messed up */
0114         WARN_ON_ONCE(1);
0115         val = 0;
0116         break;
0117     }
0118 
0119     return val;
0120 }
0121 
0122 static ssize_t ltc4261_value_show(struct device *dev,
0123                   struct device_attribute *da, char *buf)
0124 {
0125     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0126     struct ltc4261_data *data = ltc4261_update_device(dev);
0127     int value;
0128 
0129     if (IS_ERR(data))
0130         return PTR_ERR(data);
0131 
0132     value = ltc4261_get_value(data, attr->index);
0133     return sysfs_emit(buf, "%d\n", value);
0134 }
0135 
0136 static ssize_t ltc4261_bool_show(struct device *dev,
0137                  struct device_attribute *da, char *buf)
0138 {
0139     struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
0140     struct ltc4261_data *data = ltc4261_update_device(dev);
0141     u8 fault;
0142 
0143     if (IS_ERR(data))
0144         return PTR_ERR(data);
0145 
0146     fault = data->regs[LTC4261_FAULT] & attr->index;
0147     if (fault)      /* Clear reported faults in chip register */
0148         i2c_smbus_write_byte_data(data->client, LTC4261_FAULT, ~fault);
0149 
0150     return sysfs_emit(buf, "%d\n", fault ? 1 : 0);
0151 }
0152 
0153 /*
0154  * Input voltages.
0155  */
0156 static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4261_value, LTC4261_ADIN_H);
0157 static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4261_value, LTC4261_ADIN2_H);
0158 
0159 /*
0160  * Voltage alarms. The chip has only one set of voltage alarm status bits,
0161  * triggered by input voltage alarms. In many designs, those alarms are
0162  * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin
0163  * to the OV pin. ADIN2 is, however, not available on all chip variants.
0164  * To ensure that the alarm condition is reported to the user, report it
0165  * with both voltage sensors.
0166  */
0167 static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4261_bool, FAULT_UV);
0168 static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4261_bool, FAULT_OV);
0169 static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4261_bool, FAULT_UV);
0170 static SENSOR_DEVICE_ATTR_RO(in2_max_alarm, ltc4261_bool, FAULT_OV);
0171 
0172 /* Currents (via sense resistor) */
0173 static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4261_value, LTC4261_SENSE_H);
0174 
0175 /* Overcurrent alarm */
0176 static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4261_bool, FAULT_OC);
0177 
0178 static struct attribute *ltc4261_attrs[] = {
0179     &sensor_dev_attr_in1_input.dev_attr.attr,
0180     &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
0181     &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
0182     &sensor_dev_attr_in2_input.dev_attr.attr,
0183     &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
0184     &sensor_dev_attr_in2_max_alarm.dev_attr.attr,
0185 
0186     &sensor_dev_attr_curr1_input.dev_attr.attr,
0187     &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
0188 
0189     NULL,
0190 };
0191 ATTRIBUTE_GROUPS(ltc4261);
0192 
0193 static int ltc4261_probe(struct i2c_client *client)
0194 {
0195     struct i2c_adapter *adapter = client->adapter;
0196     struct device *dev = &client->dev;
0197     struct ltc4261_data *data;
0198     struct device *hwmon_dev;
0199 
0200     if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
0201         return -ENODEV;
0202 
0203     if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) {
0204         dev_err(dev, "Failed to read status register\n");
0205         return -ENODEV;
0206     }
0207 
0208     data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
0209     if (!data)
0210         return -ENOMEM;
0211 
0212     data->client = client;
0213     mutex_init(&data->update_lock);
0214 
0215     /* Clear faults */
0216     i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00);
0217 
0218     hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
0219                                data,
0220                                ltc4261_groups);
0221     return PTR_ERR_OR_ZERO(hwmon_dev);
0222 }
0223 
0224 static const struct i2c_device_id ltc4261_id[] = {
0225     {"ltc4261", 0},
0226     {}
0227 };
0228 
0229 MODULE_DEVICE_TABLE(i2c, ltc4261_id);
0230 
0231 /* This is the driver that will be inserted */
0232 static struct i2c_driver ltc4261_driver = {
0233     .driver = {
0234            .name = "ltc4261",
0235            },
0236     .probe_new = ltc4261_probe,
0237     .id_table = ltc4261_id,
0238 };
0239 
0240 module_i2c_driver(ltc4261_driver);
0241 
0242 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>");
0243 MODULE_DESCRIPTION("LTC4261 driver");
0244 MODULE_LICENSE("GPL");