Blame view
drivers/hwmon/ltc4261.c
6.28 KB
74ba9207e treewide: Replace... |
1 |
// SPDX-License-Identifier: GPL-2.0-or-later |
e5f5c99a3 hwmon: LTC4261 Ha... |
2 3 4 5 6 7 8 9 10 11 12 |
/* * Driver for Linear Technology LTC4261 I2C Negative Voltage Hot Swap Controller * * Copyright (C) 2010 Ericsson AB. * * Derived from: * * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu> * * Datasheet: http://cds.linear.com/docs/Datasheet/42612fb.pdf |
e5f5c99a3 hwmon: LTC4261 Ha... |
13 14 15 16 17 18 19 20 21 22 |
*/ #include <linux/kernel.h> #include <linux/module.h> #include <linux/init.h> #include <linux/err.h> #include <linux/slab.h> #include <linux/i2c.h> #include <linux/hwmon.h> #include <linux/hwmon-sysfs.h> |
dcd8f3923 hwmon: Add missin... |
23 |
#include <linux/jiffies.h> |
e5f5c99a3 hwmon: LTC4261 Ha... |
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
/* chip registers */ #define LTC4261_STATUS 0x00 /* readonly */ #define LTC4261_FAULT 0x01 #define LTC4261_ALERT 0x02 #define LTC4261_CONTROL 0x03 #define LTC4261_SENSE_H 0x04 #define LTC4261_SENSE_L 0x05 #define LTC4261_ADIN2_H 0x06 #define LTC4261_ADIN2_L 0x07 #define LTC4261_ADIN_H 0x08 #define LTC4261_ADIN_L 0x09 /* * Fault register bits */ #define FAULT_OV (1<<0) #define FAULT_UV (1<<1) #define FAULT_OC (1<<2) struct ltc4261_data { |
38f150fe3 hwmon: (ltc4261) ... |
45 |
struct i2c_client *client; |
e5f5c99a3 hwmon: LTC4261 Ha... |
46 47 48 49 50 51 52 53 54 55 56 |
struct mutex update_lock; bool valid; unsigned long last_updated; /* in jiffies */ /* Registers */ u8 regs[10]; }; static struct ltc4261_data *ltc4261_update_device(struct device *dev) { |
38f150fe3 hwmon: (ltc4261) ... |
57 58 |
struct ltc4261_data *data = dev_get_drvdata(dev); struct i2c_client *client = data->client; |
e5f5c99a3 hwmon: LTC4261 Ha... |
59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
struct ltc4261_data *ret = data; mutex_lock(&data->update_lock); if (time_after(jiffies, data->last_updated + HZ / 4) || !data->valid) { int i; /* Read registers -- 0x00 to 0x09 */ for (i = 0; i < ARRAY_SIZE(data->regs); i++) { int val; val = i2c_smbus_read_byte_data(client, i); if (unlikely(val < 0)) { dev_dbg(dev, |
69f8b7419 hwmon: (ltc4261) ... |
73 74 |
"Failed to read ADC value: error %d ", |
e5f5c99a3 hwmon: LTC4261 Ha... |
75 76 |
val); ret = ERR_PTR(val); |
aac9fe9b4 hwmon: (ltc4261) ... |
77 |
data->valid = 0; |
e5f5c99a3 hwmon: LTC4261 Ha... |
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
goto abort; } data->regs[i] = val; } data->last_updated = jiffies; data->valid = 1; } abort: mutex_unlock(&data->update_lock); return ret; } /* Return the voltage from the given register in mV or mA */ static int ltc4261_get_value(struct ltc4261_data *data, u8 reg) { u32 val; val = (data->regs[reg] << 2) + (data->regs[reg + 1] >> 6); switch (reg) { case LTC4261_ADIN_H: case LTC4261_ADIN2_H: /* 2.5mV resolution. Convert to mV. */ val = val * 25 / 10; break; case LTC4261_SENSE_H: /* * 62.5uV resolution. Convert to current as measured with * an 1 mOhm sense resistor, in mA. If a different sense * resistor is installed, calculate the actual current by * dividing the reported current by the sense resistor value * in mOhm. */ val = val * 625 / 10; break; default: /* If we get here, the developer messed up */ WARN_ON_ONCE(1); val = 0; break; } return val; } |
decb23dc6 hwmon: (ltc4261) ... |
122 |
static ssize_t ltc4261_value_show(struct device *dev, |
e5f5c99a3 hwmon: LTC4261 Ha... |
123 124 125 126 127 128 129 130 131 132 133 134 135 |
struct device_attribute *da, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); struct ltc4261_data *data = ltc4261_update_device(dev); int value; if (IS_ERR(data)) return PTR_ERR(data); value = ltc4261_get_value(data, attr->index); return snprintf(buf, PAGE_SIZE, "%d ", value); } |
decb23dc6 hwmon: (ltc4261) ... |
136 |
static ssize_t ltc4261_bool_show(struct device *dev, |
e5f5c99a3 hwmon: LTC4261 Ha... |
137 138 139 |
struct device_attribute *da, char *buf) { struct sensor_device_attribute *attr = to_sensor_dev_attr(da); |
e5f5c99a3 hwmon: LTC4261 Ha... |
140 141 142 143 144 145 146 147 |
struct ltc4261_data *data = ltc4261_update_device(dev); u8 fault; if (IS_ERR(data)) return PTR_ERR(data); fault = data->regs[LTC4261_FAULT] & attr->index; if (fault) /* Clear reported faults in chip register */ |
38f150fe3 hwmon: (ltc4261) ... |
148 |
i2c_smbus_write_byte_data(data->client, LTC4261_FAULT, ~fault); |
e5f5c99a3 hwmon: LTC4261 Ha... |
149 150 151 152 153 154 |
return snprintf(buf, PAGE_SIZE, "%d ", fault ? 1 : 0); } /* |
e5f5c99a3 hwmon: LTC4261 Ha... |
155 156 |
* Input voltages. */ |
decb23dc6 hwmon: (ltc4261) ... |
157 158 |
static SENSOR_DEVICE_ATTR_RO(in1_input, ltc4261_value, LTC4261_ADIN_H); static SENSOR_DEVICE_ATTR_RO(in2_input, ltc4261_value, LTC4261_ADIN2_H); |
e5f5c99a3 hwmon: LTC4261 Ha... |
159 160 161 162 163 164 165 166 167 |
/* * Voltage alarms. The chip has only one set of voltage alarm status bits, * triggered by input voltage alarms. In many designs, those alarms are * associated with the ADIN2 sensor, due to the proximity of the ADIN2 pin * to the OV pin. ADIN2 is, however, not available on all chip variants. * To ensure that the alarm condition is reported to the user, report it * with both voltage sensors. */ |
decb23dc6 hwmon: (ltc4261) ... |
168 169 170 171 |
static SENSOR_DEVICE_ATTR_RO(in1_min_alarm, ltc4261_bool, FAULT_UV); static SENSOR_DEVICE_ATTR_RO(in1_max_alarm, ltc4261_bool, FAULT_OV); static SENSOR_DEVICE_ATTR_RO(in2_min_alarm, ltc4261_bool, FAULT_UV); static SENSOR_DEVICE_ATTR_RO(in2_max_alarm, ltc4261_bool, FAULT_OV); |
e5f5c99a3 hwmon: LTC4261 Ha... |
172 173 |
/* Currents (via sense resistor) */ |
decb23dc6 hwmon: (ltc4261) ... |
174 |
static SENSOR_DEVICE_ATTR_RO(curr1_input, ltc4261_value, LTC4261_SENSE_H); |
e5f5c99a3 hwmon: LTC4261 Ha... |
175 176 |
/* Overcurrent alarm */ |
decb23dc6 hwmon: (ltc4261) ... |
177 |
static SENSOR_DEVICE_ATTR_RO(curr1_max_alarm, ltc4261_bool, FAULT_OC); |
e5f5c99a3 hwmon: LTC4261 Ha... |
178 |
|
38f150fe3 hwmon: (ltc4261) ... |
179 |
static struct attribute *ltc4261_attrs[] = { |
e5f5c99a3 hwmon: LTC4261 Ha... |
180 181 182 183 184 185 186 187 188 189 190 191 |
&sensor_dev_attr_in1_input.dev_attr.attr, &sensor_dev_attr_in1_min_alarm.dev_attr.attr, &sensor_dev_attr_in1_max_alarm.dev_attr.attr, &sensor_dev_attr_in2_input.dev_attr.attr, &sensor_dev_attr_in2_min_alarm.dev_attr.attr, &sensor_dev_attr_in2_max_alarm.dev_attr.attr, &sensor_dev_attr_curr1_input.dev_attr.attr, &sensor_dev_attr_curr1_max_alarm.dev_attr.attr, NULL, }; |
38f150fe3 hwmon: (ltc4261) ... |
192 |
ATTRIBUTE_GROUPS(ltc4261); |
e5f5c99a3 hwmon: LTC4261 Ha... |
193 |
|
674870385 hwmon: use simple... |
194 |
static int ltc4261_probe(struct i2c_client *client) |
e5f5c99a3 hwmon: LTC4261 Ha... |
195 196 |
{ struct i2c_adapter *adapter = client->adapter; |
38f150fe3 hwmon: (ltc4261) ... |
197 |
struct device *dev = &client->dev; |
e5f5c99a3 hwmon: LTC4261 Ha... |
198 |
struct ltc4261_data *data; |
38f150fe3 hwmon: (ltc4261) ... |
199 |
struct device *hwmon_dev; |
e5f5c99a3 hwmon: LTC4261 Ha... |
200 201 202 203 204 |
if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA)) return -ENODEV; if (i2c_smbus_read_byte_data(client, LTC4261_STATUS) < 0) { |
38f150fe3 hwmon: (ltc4261) ... |
205 206 |
dev_err(dev, "Failed to read status register "); |
e5f5c99a3 hwmon: LTC4261 Ha... |
207 208 |
return -ENODEV; } |
38f150fe3 hwmon: (ltc4261) ... |
209 |
data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL); |
368392872 hwmon: (ltc4261) ... |
210 211 |
if (!data) return -ENOMEM; |
e5f5c99a3 hwmon: LTC4261 Ha... |
212 |
|
38f150fe3 hwmon: (ltc4261) ... |
213 |
data->client = client; |
e5f5c99a3 hwmon: LTC4261 Ha... |
214 215 216 217 |
mutex_init(&data->update_lock); /* Clear faults */ i2c_smbus_write_byte_data(client, LTC4261_FAULT, 0x00); |
38f150fe3 hwmon: (ltc4261) ... |
218 219 220 |
hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name, data, ltc4261_groups); |
2a253c478 hwmon: (ltc4261) ... |
221 |
return PTR_ERR_OR_ZERO(hwmon_dev); |
e5f5c99a3 hwmon: LTC4261 Ha... |
222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
} static const struct i2c_device_id ltc4261_id[] = { {"ltc4261", 0}, {} }; MODULE_DEVICE_TABLE(i2c, ltc4261_id); /* This is the driver that will be inserted */ static struct i2c_driver ltc4261_driver = { .driver = { .name = "ltc4261", }, |
674870385 hwmon: use simple... |
236 |
.probe_new = ltc4261_probe, |
e5f5c99a3 hwmon: LTC4261 Ha... |
237 238 |
.id_table = ltc4261_id, }; |
f0967eea8 hwmon: convert dr... |
239 |
module_i2c_driver(ltc4261_driver); |
e5f5c99a3 hwmon: LTC4261 Ha... |
240 |
|
bb9a80e57 hwmon: Update my ... |
241 |
MODULE_AUTHOR("Guenter Roeck <linux@roeck-us.net>"); |
e5f5c99a3 hwmon: LTC4261 Ha... |
242 243 |
MODULE_DESCRIPTION("LTC4261 driver"); MODULE_LICENSE("GPL"); |