1e5f5c99aSGuenter Roeck /* 2e5f5c99aSGuenter Roeck * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller 3e5f5c99aSGuenter Roeck * 4e5f5c99aSGuenter Roeck * Copyright (C) 2010 Ericsson AB. 5e5f5c99aSGuenter Roeck * 6e5f5c99aSGuenter Roeck * Derived from: 7e5f5c99aSGuenter Roeck * 8e5f5c99aSGuenter Roeck * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller 9e5f5c99aSGuenter Roeck * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu> 10e5f5c99aSGuenter Roeck * 11e5f5c99aSGuenter Roeck * Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf 12e5f5c99aSGuenter Roeck * 13e5f5c99aSGuenter Roeck * This program is free software; you can redistribute it and/or modify 14e5f5c99aSGuenter Roeck * it under the terms of the GNU General Public License as published by 15e5f5c99aSGuenter Roeck * the Free Software Foundation; either version 2 of the License, or 16e5f5c99aSGuenter Roeck * (at your option) any later version. 17e5f5c99aSGuenter Roeck * 18e5f5c99aSGuenter Roeck * This program is distributed in the hope that it will be useful, 19e5f5c99aSGuenter Roeck * but WITHOUT ANY WARRANTY; without even the implied warranty of 20e5f5c99aSGuenter Roeck * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 21e5f5c99aSGuenter Roeck * GNU General Public License for more details. 22e5f5c99aSGuenter Roeck * 23e5f5c99aSGuenter Roeck * You should have received a copy of the GNU General Public License 24e5f5c99aSGuenter Roeck * along with this program; if not, write to the Free Software 25e5f5c99aSGuenter Roeck * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 26e5f5c99aSGuenter Roeck */ 27e5f5c99aSGuenter Roeck 28e5f5c99aSGuenter Roeck #include <linux/kernel.h> 29e5f5c99aSGuenter Roeck #include <linux/module.h> 30e5f5c99aSGuenter Roeck #include <linux/init.h> 31e5f5c99aSGuenter Roeck #include <linux/err.h> 32e5f5c99aSGuenter Roeck #include <linux/slab.h> 33e5f5c99aSGuenter Roeck #include <linux/i2c.h> 34e5f5c99aSGuenter Roeck #include <linux/hwmon.h> 35e5f5c99aSGuenter Roeck #include <linux/hwmon-sysfs.h> 36dcd8f392SJean Delvare #include <linux/jiffies.h> 37e5f5c99aSGuenter Roeck 38e5f5c99aSGuenter Roeck /* chip registers */ 39e5f5c99aSGuenter Roeck #define LTC4261_STATUS 0x00 /* readonly */ 40e5f5c99aSGuenter Roeck #define LTC4261_FAULT 0x01 41e5f5c99aSGuenter Roeck #define LTC4261_ALERT 0x02 42e5f5c99aSGuenter Roeck #define LTC4261_CONTROL 0x03 43e5f5c99aSGuenter Roeck #define LTC4261_SENSE_H 0x04 44e5f5c99aSGuenter Roeck #define LTC4261_SENSE_L 0x05 45e5f5c99aSGuenter Roeck #define LTC4261_ADIN2_H 0x06 46e5f5c99aSGuenter Roeck #define LTC4261_ADIN2_L 0x07 47e5f5c99aSGuenter Roeck #define LTC4261_ADIN_H 0x08 48e5f5c99aSGuenter Roeck #define LTC4261_ADIN_L 0x09 49e5f5c99aSGuenter Roeck 50e5f5c99aSGuenter Roeck /* 51e5f5c99aSGuenter Roeck * Fault register bits 52e5f5c99aSGuenter Roeck */ 53e5f5c99aSGuenter Roeck #define FAULT_OV (1<<0) 54e5f5c99aSGuenter Roeck #define FAULT_UV (1<<1) 55e5f5c99aSGuenter Roeck #define FAULT_OC (1<<2) 56e5f5c99aSGuenter Roeck 57e5f5c99aSGuenter Roeck struct ltc4261_data { 5838f150feSGuenter Roeck struct i2c_client *client; 59e5f5c99aSGuenter Roeck 60e5f5c99aSGuenter Roeck struct mutex update_lock; 61e5f5c99aSGuenter Roeck bool valid; 62e5f5c99aSGuenter Roeck unsigned long last_updated; /* in jiffies */ 63e5f5c99aSGuenter Roeck 64e5f5c99aSGuenter Roeck /* Registers */ 65e5f5c99aSGuenter Roeck u8 regs[10]; 66e5f5c99aSGuenter Roeck }; 67e5f5c99aSGuenter Roeck 68e5f5c99aSGuenter Roeck static struct ltc4261_data *ltc4261_update_device(struct device *dev) 69e5f5c99aSGuenter Roeck { 7038f150feSGuenter Roeck struct ltc4261_data *data = dev_get_drvdata(dev); 7138f150feSGuenter Roeck struct i2c_client *client = data->client; 72e5f5c99aSGuenter Roeck struct ltc4261_data *ret = data; 73e5f5c99aSGuenter Roeck 74e5f5c99aSGuenter Roeck mutex_lock(&data->update_lock); 75e5f5c99aSGuenter Roeck 76e5f5c99aSGuenter Roeck if (time_after(jiffies, data->last_updated + HZ / 4) || !data->valid) { 77e5f5c99aSGuenter Roeck int i; 78e5f5c99aSGuenter Roeck 79e5f5c99aSGuenter Roeck /* Read registers -- 0x00 to 0x09 */ 80e5f5c99aSGuenter Roeck for (i = 0; i < ARRAY_SIZE(data->regs); i++) { 81e5f5c99aSGuenter Roeck int val; 82e5f5c99aSGuenter Roeck 83e5f5c99aSGuenter Roeck val = i2c_smbus_read_byte_data(client, i); 84e5f5c99aSGuenter Roeck if (unlikely(val < 0)) { 85e5f5c99aSGuenter Roeck dev_dbg(dev, 8669f8b741SGuenter Roeck "Failed to read ADC value: error %d\n", 87e5f5c99aSGuenter Roeck val); 88e5f5c99aSGuenter Roeck ret = ERR_PTR(val); 89aac9fe9bSFrans Meulenbroeks data->valid = 0; 90e5f5c99aSGuenter Roeck goto abort; 91e5f5c99aSGuenter Roeck } 92e5f5c99aSGuenter Roeck data->regs[i] = val; 93e5f5c99aSGuenter Roeck } 94e5f5c99aSGuenter Roeck data->last_updated = jiffies; 95e5f5c99aSGuenter Roeck data->valid = 1; 96e5f5c99aSGuenter Roeck } 97e5f5c99aSGuenter Roeck abort: 98e5f5c99aSGuenter Roeck mutex_unlock(&data->update_lock); 99e5f5c99aSGuenter Roeck return ret; 100e5f5c99aSGuenter Roeck } 101e5f5c99aSGuenter Roeck 102e5f5c99aSGuenter Roeck /* Return the voltage from the given register in mV or mA */ 103e5f5c99aSGuenter Roeck static int ltc4261_get_value(struct ltc4261_data *data, u8 reg) 104e5f5c99aSGuenter Roeck { 105e5f5c99aSGuenter Roeck u32 val; 106e5f5c99aSGuenter Roeck 107e5f5c99aSGuenter Roeck val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6); 108e5f5c99aSGuenter Roeck 109e5f5c99aSGuenter Roeck switch (reg) { 110e5f5c99aSGuenter Roeck case LTC4261_ADIN_H: 111e5f5c99aSGuenter Roeck case LTC4261_ADIN2_H: 112e5f5c99aSGuenter Roeck /* 2.5mV resolution. Convert to mV. */ 113e5f5c99aSGuenter Roeck val = val * 25 / 10; 114e5f5c99aSGuenter Roeck break; 115e5f5c99aSGuenter Roeck case LTC4261_SENSE_H: 116e5f5c99aSGuenter Roeck /* 117e5f5c99aSGuenter Roeck * 62.5uV resolution. Convert to current as measured with 118e5f5c99aSGuenter Roeck * an 1 mOhm sense resistor, in mA. If a different sense 119e5f5c99aSGuenter Roeck * resistor is installed, calculate the actual current by 120e5f5c99aSGuenter Roeck * dividing the reported current by the sense resistor value 121e5f5c99aSGuenter Roeck * in mOhm. 122e5f5c99aSGuenter Roeck */ 123e5f5c99aSGuenter Roeck val = val * 625 / 10; 124e5f5c99aSGuenter Roeck break; 125e5f5c99aSGuenter Roeck default: 126e5f5c99aSGuenter Roeck /* If we get here, the developer messed up */ 127e5f5c99aSGuenter Roeck WARN_ON_ONCE(1); 128e5f5c99aSGuenter Roeck val = 0; 129e5f5c99aSGuenter Roeck break; 130e5f5c99aSGuenter Roeck } 131e5f5c99aSGuenter Roeck 132e5f5c99aSGuenter Roeck return val; 133e5f5c99aSGuenter Roeck } 134e5f5c99aSGuenter Roeck 135*decb23dcSGuenter Roeck static ssize_t ltc4261_value_show(struct device *dev, 136e5f5c99aSGuenter Roeck struct device_attribute *da, char *buf) 137e5f5c99aSGuenter Roeck { 138e5f5c99aSGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 139e5f5c99aSGuenter Roeck struct ltc4261_data *data = ltc4261_update_device(dev); 140e5f5c99aSGuenter Roeck int value; 141e5f5c99aSGuenter Roeck 142e5f5c99aSGuenter Roeck if (IS_ERR(data)) 143e5f5c99aSGuenter Roeck return PTR_ERR(data); 144e5f5c99aSGuenter Roeck 145e5f5c99aSGuenter Roeck value = ltc4261_get_value(data, attr->index); 146e5f5c99aSGuenter Roeck return snprintf(buf, PAGE_SIZE, "%d\n", value); 147e5f5c99aSGuenter Roeck } 148e5f5c99aSGuenter Roeck 149*decb23dcSGuenter Roeck static ssize_t ltc4261_bool_show(struct device *dev, 150e5f5c99aSGuenter Roeck struct device_attribute *da, char *buf) 151e5f5c99aSGuenter Roeck { 152e5f5c99aSGuenter Roeck struct sensor_device_attribute *attr = to_sensor_dev_attr(da); 153e5f5c99aSGuenter Roeck struct ltc4261_data *data = ltc4261_update_device(dev); 154e5f5c99aSGuenter Roeck u8 fault; 155e5f5c99aSGuenter Roeck 156e5f5c99aSGuenter Roeck if (IS_ERR(data)) 157e5f5c99aSGuenter Roeck return PTR_ERR(data); 158e5f5c99aSGuenter Roeck 159e5f5c99aSGuenter Roeck fault = data->regs[LTC4261_FAULT] & attr->index; 160e5f5c99aSGuenter Roeck if (fault) /* Clear reported faults in chip register */ 16138f150feSGuenter Roeck i2c_smbus_write_byte_data(data->client, LTC4261_FAULT, ~fault); 162e5f5c99aSGuenter Roeck 163e5f5c99aSGuenter Roeck return snprintf(buf, PAGE_SIZE, "%d\n", fault ? 1 : 0); 164e5f5c99aSGuenter Roeck } 165e5f5c99aSGuenter Roeck 166e5f5c99aSGuenter Roeck /* 167e5f5c99aSGuenter Roeck * Input voltages. 168e5f5c99aSGuenter Roeck */ 169*decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4261_value, LTC4261_ADIN_H); 170*decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4261_value, LTC4261_ADIN2_H); 171e5f5c99aSGuenter Roeck 172e5f5c99aSGuenter Roeck /* 173e5f5c99aSGuenter Roeck * Voltage alarms. The chip has only one set of voltage alarm status bits, 174e5f5c99aSGuenter Roeck * triggered by input voltage alarms. In many designs, those alarms are 175e5f5c99aSGuenter Roeck * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin 176e5f5c99aSGuenter Roeck * to the OV pin. ADIN2 is, however, not available on all chip variants. 177e5f5c99aSGuenter Roeck * To ensure that the alarm condition is reported to the user, report it 178e5f5c99aSGuenter Roeck * with both voltage sensors. 179e5f5c99aSGuenter Roeck */ 180*decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4261_bool, FAULT_UV); 181*decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4261_bool, FAULT_OV); 182*decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4261_bool, FAULT_UV); 183*decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(in2_max_alarm, ltc4261_bool, FAULT_OV); 184e5f5c99aSGuenter Roeck 185e5f5c99aSGuenter Roeck /* Currents (via sense resistor) */ 186*decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4261_value, LTC4261_SENSE_H); 187e5f5c99aSGuenter Roeck 188e5f5c99aSGuenter Roeck /* Overcurrent alarm */ 189*decb23dcSGuenter Roeck static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4261_bool, FAULT_OC); 190e5f5c99aSGuenter Roeck 19138f150feSGuenter Roeck static struct attribute *ltc4261_attrs[] = { 192e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_input.dev_attr.attr, 193e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_min_alarm.dev_attr.attr, 194e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_max_alarm.dev_attr.attr, 195e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_input.dev_attr.attr, 196e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_min_alarm.dev_attr.attr, 197e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_max_alarm.dev_attr.attr, 198e5f5c99aSGuenter Roeck 199e5f5c99aSGuenter Roeck &sensor_dev_attr_curr1_input.dev_attr.attr, 200e5f5c99aSGuenter Roeck &sensor_dev_attr_curr1_max_alarm.dev_attr.attr, 201e5f5c99aSGuenter Roeck 202e5f5c99aSGuenter Roeck NULL, 203e5f5c99aSGuenter Roeck }; 20438f150feSGuenter Roeck ATTRIBUTE_GROUPS(ltc4261); 205e5f5c99aSGuenter Roeck 206e5f5c99aSGuenter Roeck static int ltc4261_probe(struct i2c_client *client, 207e5f5c99aSGuenter Roeck const struct i2c_device_id *id) 208e5f5c99aSGuenter Roeck { 209e5f5c99aSGuenter Roeck struct i2c_adapter *adapter = client->adapter; 21038f150feSGuenter Roeck struct device *dev = &client->dev; 211e5f5c99aSGuenter Roeck struct ltc4261_data *data; 21238f150feSGuenter Roeck struct device *hwmon_dev; 213e5f5c99aSGuenter Roeck 214e5f5c99aSGuenter Roeck if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 215e5f5c99aSGuenter Roeck return -ENODEV; 216e5f5c99aSGuenter Roeck 217e5f5c99aSGuenter Roeck if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) { 21838f150feSGuenter Roeck dev_err(dev, "Failed to read status register\n"); 219e5f5c99aSGuenter Roeck return -ENODEV; 220e5f5c99aSGuenter Roeck } 221e5f5c99aSGuenter Roeck 22238f150feSGuenter Roeck data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); 22336839287SGuenter Roeck if (!data) 22436839287SGuenter Roeck return -ENOMEM; 225e5f5c99aSGuenter Roeck 22638f150feSGuenter Roeck data->client = client; 227e5f5c99aSGuenter Roeck mutex_init(&data->update_lock); 228e5f5c99aSGuenter Roeck 229e5f5c99aSGuenter Roeck /* Clear faults */ 230e5f5c99aSGuenter Roeck i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00); 231e5f5c99aSGuenter Roeck 23238f150feSGuenter Roeck hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, 23338f150feSGuenter Roeck data, 23438f150feSGuenter Roeck ltc4261_groups); 2352a253c47SFengguang Wu return PTR_ERR_OR_ZERO(hwmon_dev); 236e5f5c99aSGuenter Roeck } 237e5f5c99aSGuenter Roeck 238e5f5c99aSGuenter Roeck static const struct i2c_device_id ltc4261_id[] = { 239e5f5c99aSGuenter Roeck {"ltc4261", 0}, 240e5f5c99aSGuenter Roeck {} 241e5f5c99aSGuenter Roeck }; 242e5f5c99aSGuenter Roeck 243e5f5c99aSGuenter Roeck MODULE_DEVICE_TABLE(i2c, ltc4261_id); 244e5f5c99aSGuenter Roeck 245e5f5c99aSGuenter Roeck /* This is the driver that will be inserted */ 246e5f5c99aSGuenter Roeck static struct i2c_driver ltc4261_driver = { 247e5f5c99aSGuenter Roeck .driver = { 248e5f5c99aSGuenter Roeck .name = "ltc4261", 249e5f5c99aSGuenter Roeck }, 250e5f5c99aSGuenter Roeck .probe = ltc4261_probe, 251e5f5c99aSGuenter Roeck .id_table = ltc4261_id, 252e5f5c99aSGuenter Roeck }; 253e5f5c99aSGuenter Roeck 254f0967eeaSAxel Lin module_i2c_driver(ltc4261_driver); 255e5f5c99aSGuenter Roeck 256bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 257e5f5c99aSGuenter Roeck MODULE_DESCRIPTION("LTC4261 driver"); 258e5f5c99aSGuenter Roeck MODULE_LICENSE("GPL"); 259