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> 36*dcd8f392SJean 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 { 58e5f5c99aSGuenter Roeck struct device *hwmon_dev; 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 { 70e5f5c99aSGuenter Roeck struct i2c_client *client = to_i2c_client(dev); 71e5f5c99aSGuenter Roeck struct ltc4261_data *data = i2c_get_clientdata(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 135e5f5c99aSGuenter Roeck static ssize_t ltc4261_show_value(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 149e5f5c99aSGuenter Roeck static ssize_t ltc4261_show_bool(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 i2c_client *client = to_i2c_client(dev); 154e5f5c99aSGuenter Roeck struct ltc4261_data *data = ltc4261_update_device(dev); 155e5f5c99aSGuenter Roeck u8 fault; 156e5f5c99aSGuenter Roeck 157e5f5c99aSGuenter Roeck if (IS_ERR(data)) 158e5f5c99aSGuenter Roeck return PTR_ERR(data); 159e5f5c99aSGuenter Roeck 160e5f5c99aSGuenter Roeck fault = data->regs[LTC4261_FAULT] & attr->index; 161e5f5c99aSGuenter Roeck if (fault) /* Clear reported faults in chip register */ 162e5f5c99aSGuenter Roeck i2c_smbus_write_byte_data(client, LTC4261_FAULT, ~fault); 163e5f5c99aSGuenter Roeck 164e5f5c99aSGuenter Roeck return snprintf(buf, PAGE_SIZE, "%d\n", fault ? 1 : 0); 165e5f5c99aSGuenter Roeck } 166e5f5c99aSGuenter Roeck 167e5f5c99aSGuenter Roeck /* 168e5f5c99aSGuenter Roeck * These macros are used below in constructing device attribute objects 169e5f5c99aSGuenter Roeck * for use with sysfs_create_group() to make a sysfs device file 170e5f5c99aSGuenter Roeck * for each register. 171e5f5c99aSGuenter Roeck */ 172e5f5c99aSGuenter Roeck 173e5f5c99aSGuenter Roeck #define LTC4261_VALUE(name, ltc4261_cmd_idx) \ 174e5f5c99aSGuenter Roeck static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ 175e5f5c99aSGuenter Roeck ltc4261_show_value, NULL, ltc4261_cmd_idx) 176e5f5c99aSGuenter Roeck 177e5f5c99aSGuenter Roeck #define LTC4261_BOOL(name, mask) \ 178e5f5c99aSGuenter Roeck static SENSOR_DEVICE_ATTR(name, S_IRUGO, \ 179e5f5c99aSGuenter Roeck ltc4261_show_bool, NULL, (mask)) 180e5f5c99aSGuenter Roeck 181e5f5c99aSGuenter Roeck /* 182e5f5c99aSGuenter Roeck * Input voltages. 183e5f5c99aSGuenter Roeck */ 184e5f5c99aSGuenter Roeck LTC4261_VALUE(in1_input, LTC4261_ADIN_H); 185e5f5c99aSGuenter Roeck LTC4261_VALUE(in2_input, LTC4261_ADIN2_H); 186e5f5c99aSGuenter Roeck 187e5f5c99aSGuenter Roeck /* 188e5f5c99aSGuenter Roeck * Voltage alarms. The chip has only one set of voltage alarm status bits, 189e5f5c99aSGuenter Roeck * triggered by input voltage alarms. In many designs, those alarms are 190e5f5c99aSGuenter Roeck * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin 191e5f5c99aSGuenter Roeck * to the OV pin. ADIN2 is, however, not available on all chip variants. 192e5f5c99aSGuenter Roeck * To ensure that the alarm condition is reported to the user, report it 193e5f5c99aSGuenter Roeck * with both voltage sensors. 194e5f5c99aSGuenter Roeck */ 195e5f5c99aSGuenter Roeck LTC4261_BOOL(in1_min_alarm, FAULT_UV); 196e5f5c99aSGuenter Roeck LTC4261_BOOL(in1_max_alarm, FAULT_OV); 197e5f5c99aSGuenter Roeck LTC4261_BOOL(in2_min_alarm, FAULT_UV); 198e5f5c99aSGuenter Roeck LTC4261_BOOL(in2_max_alarm, FAULT_OV); 199e5f5c99aSGuenter Roeck 200e5f5c99aSGuenter Roeck /* Currents (via sense resistor) */ 201e5f5c99aSGuenter Roeck LTC4261_VALUE(curr1_input, LTC4261_SENSE_H); 202e5f5c99aSGuenter Roeck 203e5f5c99aSGuenter Roeck /* Overcurrent alarm */ 204e5f5c99aSGuenter Roeck LTC4261_BOOL(curr1_max_alarm, FAULT_OC); 205e5f5c99aSGuenter Roeck 206e5f5c99aSGuenter Roeck static struct attribute *ltc4261_attributes[] = { 207e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_input.dev_attr.attr, 208e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_min_alarm.dev_attr.attr, 209e5f5c99aSGuenter Roeck &sensor_dev_attr_in1_max_alarm.dev_attr.attr, 210e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_input.dev_attr.attr, 211e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_min_alarm.dev_attr.attr, 212e5f5c99aSGuenter Roeck &sensor_dev_attr_in2_max_alarm.dev_attr.attr, 213e5f5c99aSGuenter Roeck 214e5f5c99aSGuenter Roeck &sensor_dev_attr_curr1_input.dev_attr.attr, 215e5f5c99aSGuenter Roeck &sensor_dev_attr_curr1_max_alarm.dev_attr.attr, 216e5f5c99aSGuenter Roeck 217e5f5c99aSGuenter Roeck NULL, 218e5f5c99aSGuenter Roeck }; 219e5f5c99aSGuenter Roeck 220e5f5c99aSGuenter Roeck static const struct attribute_group ltc4261_group = { 221e5f5c99aSGuenter Roeck .attrs = ltc4261_attributes, 222e5f5c99aSGuenter Roeck }; 223e5f5c99aSGuenter Roeck 224e5f5c99aSGuenter Roeck static int ltc4261_probe(struct i2c_client *client, 225e5f5c99aSGuenter Roeck const struct i2c_device_id *id) 226e5f5c99aSGuenter Roeck { 227e5f5c99aSGuenter Roeck struct i2c_adapter *adapter = client->adapter; 228e5f5c99aSGuenter Roeck struct ltc4261_data *data; 229e5f5c99aSGuenter Roeck int ret; 230e5f5c99aSGuenter Roeck 231e5f5c99aSGuenter Roeck if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) 232e5f5c99aSGuenter Roeck return -ENODEV; 233e5f5c99aSGuenter Roeck 234e5f5c99aSGuenter Roeck if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) { 235475200c0SJean Delvare dev_err(&client->dev, "Failed to read status register\n"); 236e5f5c99aSGuenter Roeck return -ENODEV; 237e5f5c99aSGuenter Roeck } 238e5f5c99aSGuenter Roeck 23936839287SGuenter Roeck data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL); 24036839287SGuenter Roeck if (!data) 24136839287SGuenter Roeck return -ENOMEM; 242e5f5c99aSGuenter Roeck 243e5f5c99aSGuenter Roeck i2c_set_clientdata(client, data); 244e5f5c99aSGuenter Roeck mutex_init(&data->update_lock); 245e5f5c99aSGuenter Roeck 246e5f5c99aSGuenter Roeck /* Clear faults */ 247e5f5c99aSGuenter Roeck i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00); 248e5f5c99aSGuenter Roeck 249e5f5c99aSGuenter Roeck /* Register sysfs hooks */ 250e5f5c99aSGuenter Roeck ret = sysfs_create_group(&client->dev.kobj, <c4261_group); 251e5f5c99aSGuenter Roeck if (ret) 25236839287SGuenter Roeck return ret; 253e5f5c99aSGuenter Roeck 254e5f5c99aSGuenter Roeck data->hwmon_dev = hwmon_device_register(&client->dev); 255e5f5c99aSGuenter Roeck if (IS_ERR(data->hwmon_dev)) { 256e5f5c99aSGuenter Roeck ret = PTR_ERR(data->hwmon_dev); 257e5f5c99aSGuenter Roeck goto out_hwmon_device_register; 258e5f5c99aSGuenter Roeck } 259e5f5c99aSGuenter Roeck 260e5f5c99aSGuenter Roeck return 0; 261e5f5c99aSGuenter Roeck 262e5f5c99aSGuenter Roeck out_hwmon_device_register: 263e5f5c99aSGuenter Roeck sysfs_remove_group(&client->dev.kobj, <c4261_group); 264e5f5c99aSGuenter Roeck return ret; 265e5f5c99aSGuenter Roeck } 266e5f5c99aSGuenter Roeck 267e5f5c99aSGuenter Roeck static int ltc4261_remove(struct i2c_client *client) 268e5f5c99aSGuenter Roeck { 269e5f5c99aSGuenter Roeck struct ltc4261_data *data = i2c_get_clientdata(client); 270e5f5c99aSGuenter Roeck 271e5f5c99aSGuenter Roeck hwmon_device_unregister(data->hwmon_dev); 272e5f5c99aSGuenter Roeck sysfs_remove_group(&client->dev.kobj, <c4261_group); 273e5f5c99aSGuenter Roeck 274e5f5c99aSGuenter Roeck return 0; 275e5f5c99aSGuenter Roeck } 276e5f5c99aSGuenter Roeck 277e5f5c99aSGuenter Roeck static const struct i2c_device_id ltc4261_id[] = { 278e5f5c99aSGuenter Roeck {"ltc4261", 0}, 279e5f5c99aSGuenter Roeck {} 280e5f5c99aSGuenter Roeck }; 281e5f5c99aSGuenter Roeck 282e5f5c99aSGuenter Roeck MODULE_DEVICE_TABLE(i2c, ltc4261_id); 283e5f5c99aSGuenter Roeck 284e5f5c99aSGuenter Roeck /* This is the driver that will be inserted */ 285e5f5c99aSGuenter Roeck static struct i2c_driver ltc4261_driver = { 286e5f5c99aSGuenter Roeck .driver = { 287e5f5c99aSGuenter Roeck .name = "ltc4261", 288e5f5c99aSGuenter Roeck }, 289e5f5c99aSGuenter Roeck .probe = ltc4261_probe, 290e5f5c99aSGuenter Roeck .remove = ltc4261_remove, 291e5f5c99aSGuenter Roeck .id_table = ltc4261_id, 292e5f5c99aSGuenter Roeck }; 293e5f5c99aSGuenter Roeck 294f0967eeaSAxel Lin module_i2c_driver(ltc4261_driver); 295e5f5c99aSGuenter Roeck 296bb9a80e5SGuenter Roeck MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); 297e5f5c99aSGuenter Roeck MODULE_DESCRIPTION("LTC4261 driver"); 298e5f5c99aSGuenter Roeck MODULE_LICENSE("GPL"); 299