1 /* 2 * Driver for Linear Technology LTC4260 I2C Positive Voltage Hot Swap Controller 3 * 4 * Copyright (c) 2014 Guenter Roeck 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 */ 16 17 #include <linux/kernel.h> 18 #include <linux/module.h> 19 #include <linux/err.h> 20 #include <linux/slab.h> 21 #include <linux/i2c.h> 22 #include <linux/hwmon.h> 23 #include <linux/hwmon-sysfs.h> 24 #include <linux/jiffies.h> 25 #include <linux/regmap.h> 26 27 /* chip registers */ 28 #define LTC4260_CONTROL 0x00 29 #define LTC4260_ALERT 0x01 30 #define LTC4260_STATUS 0x02 31 #define LTC4260_FAULT 0x03 32 #define LTC4260_SENSE 0x04 33 #define LTC4260_SOURCE 0x05 34 #define LTC4260_ADIN 0x06 35 36 /* 37 * Fault register bits 38 */ 39 #define FAULT_OV (1 << 0) 40 #define FAULT_UV (1 << 1) 41 #define FAULT_OC (1 << 2) 42 #define FAULT_POWER_BAD (1 << 3) 43 #define FAULT_FET_SHORT (1 << 5) 44 45 /* Return the voltage from the given register in mV or mA */ 46 static int ltc4260_get_value(struct device *dev, u8 reg) 47 { 48 struct regmap *regmap = dev_get_drvdata(dev); 49 unsigned int val; 50 int ret; 51 52 ret = regmap_read(regmap, reg, &val); 53 if (ret < 0) 54 return ret; 55 56 switch (reg) { 57 case LTC4260_ADIN: 58 /* 10 mV resolution. Convert to mV. */ 59 val = val * 10; 60 break; 61 case LTC4260_SOURCE: 62 /* 400 mV resolution. Convert to mV. */ 63 val = val * 400; 64 break; 65 case LTC4260_SENSE: 66 /* 67 * 300 uV resolution. Convert to current as measured with 68 * an 1 mOhm sense resistor, in mA. If a different sense 69 * resistor is installed, calculate the actual current by 70 * dividing the reported current by the sense resistor value 71 * in mOhm. 72 */ 73 val = val * 300; 74 break; 75 default: 76 return -EINVAL; 77 } 78 79 return val; 80 } 81 82 static ssize_t ltc4260_value_show(struct device *dev, 83 struct device_attribute *da, char *buf) 84 { 85 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 86 int value; 87 88 value = ltc4260_get_value(dev, attr->index); 89 if (value < 0) 90 return value; 91 return snprintf(buf, PAGE_SIZE, "%d\n", value); 92 } 93 94 static ssize_t ltc4260_bool_show(struct device *dev, 95 struct device_attribute *da, char *buf) 96 { 97 struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 98 struct regmap *regmap = dev_get_drvdata(dev); 99 unsigned int fault; 100 int ret; 101 102 ret = regmap_read(regmap, LTC4260_FAULT, &fault); 103 if (ret < 0) 104 return ret; 105 106 fault &= attr->index; 107 if (fault) /* Clear reported faults in chip register */ 108 regmap_update_bits(regmap, LTC4260_FAULT, attr->index, 0); 109 110 return snprintf(buf, PAGE_SIZE, "%d\n", !!fault); 111 } 112 113 /* Voltages */ 114 static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4260_value, LTC4260_SOURCE); 115 static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4260_value, LTC4260_ADIN); 116 117 /* 118 * Voltage alarms 119 * UV/OV faults are associated with the input voltage, and the POWER BAD and 120 * FET SHORT faults are associated with the output voltage. 121 */ 122 static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4260_bool, FAULT_UV); 123 static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4260_bool, FAULT_OV); 124 static SENSOR_DEVICE_ATTR_RO(in2_alarm, ltc4260_bool, 125 FAULT_POWER_BAD | FAULT_FET_SHORT); 126 127 /* Current (via sense resistor) */ 128 static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4260_value, LTC4260_SENSE); 129 130 /* Overcurrent alarm */ 131 static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4260_bool, FAULT_OC); 132 133 static struct attribute *ltc4260_attrs[] = { 134 &sensor_dev_attr_in1_input.dev_attr.attr, 135 &sensor_dev_attr_in1_min_alarm.dev_attr.attr, 136 &sensor_dev_attr_in1_max_alarm.dev_attr.attr, 137 &sensor_dev_attr_in2_input.dev_attr.attr, 138 &sensor_dev_attr_in2_alarm.dev_attr.attr, 139 140 &sensor_dev_attr_curr1_input.dev_attr.attr, 141 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr, 142 143 NULL, 144 }; 145 ATTRIBUTE_GROUPS(ltc4260); 146 147 static const struct regmap_config ltc4260_regmap_config = { 148 .reg_bits = 8, 149 .val_bits = 8, 150 .max_register = LTC4260_ADIN, 151 }; 152 153 static int ltc4260_probe(struct i2c_client *client, 154 const struct i2c_device_id *id) 155 { 156 struct device *dev = &client->dev; 157 struct device *hwmon_dev; 158 struct regmap *regmap; 159 160 regmap = devm_regmap_init_i2c(client, <c4260_regmap_config); 161 if (IS_ERR(regmap)) { 162 dev_err(dev, "failed to allocate register map\n"); 163 return PTR_ERR(regmap); 164 } 165 166 /* Clear faults */ 167 regmap_write(regmap, LTC4260_FAULT, 0x00); 168 169 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 170 regmap, 171 ltc4260_groups); 172 return PTR_ERR_OR_ZERO(hwmon_dev); 173 } 174 175 static const struct i2c_device_id ltc4260_id[] = { 176 {"ltc4260", 0}, 177 { } 178 }; 179 180 MODULE_DEVICE_TABLE(i2c, ltc4260_id); 181 182 static struct i2c_driver ltc4260_driver = { 183 .driver = { 184 .name = "ltc4260", 185 }, 186 .probe = ltc4260_probe, 187 .id_table = ltc4260_id, 188 }; 189 190 module_i2c_driver(ltc4260_driver); 191 192 MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 193 MODULE_DESCRIPTION("LTC4260 driver"); 194 MODULE_LICENSE("GPL"); 195