2 * Driver for Linear Technology LTC4245 I2C Multiple Supply Hot Swap Controller
4 * Copyright (C) 2008 Ira W. Snyder <iws@ovro.caltech.edu>
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; version 2 of the License.
10 * This driver is based on the ds1621 and ina209 drivers.
13 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1140,P19392,D13517
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.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>
25 /* Here are names of the chip's registers (a.k.a. commands) */
27 LTC4245_STATUS = 0x00, /* readonly */
29 LTC4245_CONTROL = 0x02,
31 LTC4245_FAULT1 = 0x04,
32 LTC4245_FAULT2 = 0x05,
34 LTC4245_ADCADR = 0x07,
37 LTC4245_12VSENSE = 0x11,
38 LTC4245_12VOUT = 0x12,
40 LTC4245_5VSENSE = 0x14,
43 LTC4245_3VSENSE = 0x17,
46 LTC4245_VEESENSE = 0x1a,
47 LTC4245_VEEOUT = 0x1b,
48 LTC4245_GPIOADC1 = 0x1c,
49 LTC4245_GPIOADC2 = 0x1d,
50 LTC4245_GPIOADC3 = 0x1e,
54 struct device *hwmon_dev;
56 struct mutex update_lock;
58 unsigned long last_updated; /* in jiffies */
60 /* Control registers */
63 /* Voltage registers */
67 static struct ltc4245_data *ltc4245_update_device(struct device *dev)
69 struct i2c_client *client = to_i2c_client(dev);
70 struct ltc4245_data *data = i2c_get_clientdata(client);
74 mutex_lock(&data->update_lock);
76 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
78 dev_dbg(&client->dev, "Starting ltc4245 update\n");
80 /* Read control registers -- 0x00 to 0x07 */
81 for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
82 val = i2c_smbus_read_byte_data(client, i);
83 if (unlikely(val < 0))
89 /* Read voltage registers -- 0x10 to 0x1f */
90 for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
91 val = i2c_smbus_read_byte_data(client, i+0x10);
92 if (unlikely(val < 0))
98 data->last_updated = jiffies;
102 mutex_unlock(&data->update_lock);
107 /* Return the voltage from the given register in millivolts */
108 static int ltc4245_get_voltage(struct device *dev, u8 reg)
110 struct ltc4245_data *data = ltc4245_update_device(dev);
111 const u8 regval = data->vregs[reg - 0x10];
117 voltage = regval * 55;
121 voltage = regval * 22;
125 voltage = regval * 15;
129 voltage = regval * -55;
131 case LTC4245_GPIOADC1:
132 case LTC4245_GPIOADC2:
133 case LTC4245_GPIOADC3:
134 voltage = regval * 10;
137 /* If we get here, the developer messed up */
145 /* Return the current in the given sense register in milliAmperes */
146 static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
148 struct ltc4245_data *data = ltc4245_update_device(dev);
149 const u8 regval = data->vregs[reg - 0x10];
150 unsigned int voltage;
153 /* The strange looking conversions that follow are fixed-point
154 * math, since we cannot do floating point in the kernel.
156 * Step 1: convert sense register to microVolts
157 * Step 2: convert voltage to milliAmperes
159 * If you play around with the V=IR equation, you come up with
160 * the following: X uV / Y mOhm == Z mA
162 * With the resistors that are fractions of a milliOhm, we multiply
163 * the voltage and resistance by 10, to shift the decimal point.
164 * Now we can use the normal division operator again.
168 case LTC4245_12VSENSE:
169 voltage = regval * 250; /* voltage in uV */
170 curr = voltage / 50; /* sense resistor 50 mOhm */
172 case LTC4245_5VSENSE:
173 voltage = regval * 125; /* voltage in uV */
174 curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
176 case LTC4245_3VSENSE:
177 voltage = regval * 125; /* voltage in uV */
178 curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
180 case LTC4245_VEESENSE:
181 voltage = regval * 250; /* voltage in uV */
182 curr = voltage / 100; /* sense resistor 100 mOhm */
185 /* If we get here, the developer messed up */
194 static ssize_t ltc4245_show_voltage(struct device *dev,
195 struct device_attribute *da,
198 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
199 const int voltage = ltc4245_get_voltage(dev, attr->index);
201 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
204 static ssize_t ltc4245_show_current(struct device *dev,
205 struct device_attribute *da,
208 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
209 const unsigned int curr = ltc4245_get_current(dev, attr->index);
211 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
214 static ssize_t ltc4245_show_power(struct device *dev,
215 struct device_attribute *da,
218 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
219 const unsigned int curr = ltc4245_get_current(dev, attr->index);
220 const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
222 /* current in mA * voltage in mV == power in uW */
223 const unsigned int power = abs(output_voltage * curr);
225 return snprintf(buf, PAGE_SIZE, "%u\n", power);
228 static ssize_t ltc4245_show_alarm(struct device *dev,
229 struct device_attribute *da,
232 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
233 struct ltc4245_data *data = ltc4245_update_device(dev);
234 const u8 reg = data->cregs[attr->index];
235 const u32 mask = attr->nr;
237 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
240 /* These macros are used below in constructing device attribute objects
241 * for use with sysfs_create_group() to make a sysfs device file
245 #define LTC4245_VOLTAGE(name, ltc4245_cmd_idx) \
246 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
247 ltc4245_show_voltage, NULL, ltc4245_cmd_idx)
249 #define LTC4245_CURRENT(name, ltc4245_cmd_idx) \
250 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
251 ltc4245_show_current, NULL, ltc4245_cmd_idx)
253 #define LTC4245_POWER(name, ltc4245_cmd_idx) \
254 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
255 ltc4245_show_power, NULL, ltc4245_cmd_idx)
257 #define LTC4245_ALARM(name, mask, reg) \
258 static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
259 ltc4245_show_alarm, NULL, (mask), reg)
261 /* Construct a sensor_device_attribute structure for each register */
264 LTC4245_VOLTAGE(in1_input, LTC4245_12VIN);
265 LTC4245_VOLTAGE(in2_input, LTC4245_5VIN);
266 LTC4245_VOLTAGE(in3_input, LTC4245_3VIN);
267 LTC4245_VOLTAGE(in4_input, LTC4245_VEEIN);
269 /* Input undervoltage alarms */
270 LTC4245_ALARM(in1_min_alarm, (1 << 0), LTC4245_FAULT1);
271 LTC4245_ALARM(in2_min_alarm, (1 << 1), LTC4245_FAULT1);
272 LTC4245_ALARM(in3_min_alarm, (1 << 2), LTC4245_FAULT1);
273 LTC4245_ALARM(in4_min_alarm, (1 << 3), LTC4245_FAULT1);
275 /* Currents (via sense resistor) */
276 LTC4245_CURRENT(curr1_input, LTC4245_12VSENSE);
277 LTC4245_CURRENT(curr2_input, LTC4245_5VSENSE);
278 LTC4245_CURRENT(curr3_input, LTC4245_3VSENSE);
279 LTC4245_CURRENT(curr4_input, LTC4245_VEESENSE);
281 /* Overcurrent alarms */
282 LTC4245_ALARM(curr1_max_alarm, (1 << 4), LTC4245_FAULT1);
283 LTC4245_ALARM(curr2_max_alarm, (1 << 5), LTC4245_FAULT1);
284 LTC4245_ALARM(curr3_max_alarm, (1 << 6), LTC4245_FAULT1);
285 LTC4245_ALARM(curr4_max_alarm, (1 << 7), LTC4245_FAULT1);
287 /* Output voltages */
288 LTC4245_VOLTAGE(in5_input, LTC4245_12VOUT);
289 LTC4245_VOLTAGE(in6_input, LTC4245_5VOUT);
290 LTC4245_VOLTAGE(in7_input, LTC4245_3VOUT);
291 LTC4245_VOLTAGE(in8_input, LTC4245_VEEOUT);
293 /* Power Bad alarms */
294 LTC4245_ALARM(in5_min_alarm, (1 << 0), LTC4245_FAULT2);
295 LTC4245_ALARM(in6_min_alarm, (1 << 1), LTC4245_FAULT2);
296 LTC4245_ALARM(in7_min_alarm, (1 << 2), LTC4245_FAULT2);
297 LTC4245_ALARM(in8_min_alarm, (1 << 3), LTC4245_FAULT2);
300 LTC4245_VOLTAGE(in9_input, LTC4245_GPIOADC1);
301 LTC4245_VOLTAGE(in10_input, LTC4245_GPIOADC2);
302 LTC4245_VOLTAGE(in11_input, LTC4245_GPIOADC3);
304 /* Power Consumption (virtual) */
305 LTC4245_POWER(power1_input, LTC4245_12VSENSE);
306 LTC4245_POWER(power2_input, LTC4245_5VSENSE);
307 LTC4245_POWER(power3_input, LTC4245_3VSENSE);
308 LTC4245_POWER(power4_input, LTC4245_VEESENSE);
310 /* Finally, construct an array of pointers to members of the above objects,
311 * as required for sysfs_create_group()
313 static struct attribute *ltc4245_attributes[] = {
314 &sensor_dev_attr_in1_input.dev_attr.attr,
315 &sensor_dev_attr_in2_input.dev_attr.attr,
316 &sensor_dev_attr_in3_input.dev_attr.attr,
317 &sensor_dev_attr_in4_input.dev_attr.attr,
319 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
320 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
321 &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
322 &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
324 &sensor_dev_attr_curr1_input.dev_attr.attr,
325 &sensor_dev_attr_curr2_input.dev_attr.attr,
326 &sensor_dev_attr_curr3_input.dev_attr.attr,
327 &sensor_dev_attr_curr4_input.dev_attr.attr,
329 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
330 &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
331 &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
332 &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
334 &sensor_dev_attr_in5_input.dev_attr.attr,
335 &sensor_dev_attr_in6_input.dev_attr.attr,
336 &sensor_dev_attr_in7_input.dev_attr.attr,
337 &sensor_dev_attr_in8_input.dev_attr.attr,
339 &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
340 &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
341 &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
342 &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
344 &sensor_dev_attr_in9_input.dev_attr.attr,
345 &sensor_dev_attr_in10_input.dev_attr.attr,
346 &sensor_dev_attr_in11_input.dev_attr.attr,
348 &sensor_dev_attr_power1_input.dev_attr.attr,
349 &sensor_dev_attr_power2_input.dev_attr.attr,
350 &sensor_dev_attr_power3_input.dev_attr.attr,
351 &sensor_dev_attr_power4_input.dev_attr.attr,
356 static const struct attribute_group ltc4245_group = {
357 .attrs = ltc4245_attributes,
360 static int ltc4245_probe(struct i2c_client *client,
361 const struct i2c_device_id *id)
363 struct i2c_adapter *adapter = client->adapter;
364 struct ltc4245_data *data;
367 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
370 data = kzalloc(sizeof(*data), GFP_KERNEL);
376 i2c_set_clientdata(client, data);
377 mutex_init(&data->update_lock);
379 /* Initialize the LTC4245 chip */
380 i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
381 i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
383 /* Register sysfs hooks */
384 ret = sysfs_create_group(&client->dev.kobj, <c4245_group);
386 goto out_sysfs_create_group;
388 data->hwmon_dev = hwmon_device_register(&client->dev);
389 if (IS_ERR(data->hwmon_dev)) {
390 ret = PTR_ERR(data->hwmon_dev);
391 goto out_hwmon_device_register;
396 out_hwmon_device_register:
397 sysfs_remove_group(&client->dev.kobj, <c4245_group);
398 out_sysfs_create_group:
404 static int ltc4245_remove(struct i2c_client *client)
406 struct ltc4245_data *data = i2c_get_clientdata(client);
408 hwmon_device_unregister(data->hwmon_dev);
409 sysfs_remove_group(&client->dev.kobj, <c4245_group);
416 static const struct i2c_device_id ltc4245_id[] = {
420 MODULE_DEVICE_TABLE(i2c, ltc4245_id);
422 /* This is the driver that will be inserted */
423 static struct i2c_driver ltc4245_driver = {
427 .probe = ltc4245_probe,
428 .remove = ltc4245_remove,
429 .id_table = ltc4245_id,
432 static int __init ltc4245_init(void)
434 return i2c_add_driver(<c4245_driver);
437 static void __exit ltc4245_exit(void)
439 i2c_del_driver(<c4245_driver);
442 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
443 MODULE_DESCRIPTION("LTC4245 driver");
444 MODULE_LICENSE("GPL");
446 module_init(ltc4245_init);
447 module_exit(ltc4245_exit);