2 * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
4 * Copyright (C) 2009 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.
11 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/hwmon.h>
21 #include <linux/hwmon-sysfs.h>
22 #include <linux/jiffies.h>
24 /* Here are names of the chip's registers (a.k.a. commands) */
26 LTC4215_CONTROL = 0x00, /* rw */
27 LTC4215_ALERT = 0x01, /* rw */
28 LTC4215_STATUS = 0x02, /* ro */
29 LTC4215_FAULT = 0x03, /* rw */
30 LTC4215_SENSE = 0x04, /* rw */
31 LTC4215_SOURCE = 0x05, /* rw */
32 LTC4215_ADIN = 0x06, /* rw */
36 struct device *hwmon_dev;
38 struct mutex update_lock;
40 unsigned long last_updated; /* in jiffies */
46 static struct ltc4215_data *ltc4215_update_device(struct device *dev)
48 struct i2c_client *client = to_i2c_client(dev);
49 struct ltc4215_data *data = i2c_get_clientdata(client);
53 mutex_lock(&data->update_lock);
55 /* The chip's A/D updates 10 times per second */
56 if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
58 dev_dbg(&client->dev, "Starting ltc4215 update\n");
60 /* Read all registers */
61 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
62 val = i2c_smbus_read_byte_data(client, i);
63 if (unlikely(val < 0))
69 data->last_updated = jiffies;
73 mutex_unlock(&data->update_lock);
78 /* Return the voltage from the given register in millivolts */
79 static int ltc4215_get_voltage(struct device *dev, u8 reg)
81 struct ltc4215_data *data = ltc4215_update_device(dev);
82 const u8 regval = data->regs[reg];
87 /* 151 uV per increment */
88 voltage = regval * 151 / 1000;
91 /* 60.5 mV per increment */
92 voltage = regval * 605 / 10;
96 * The ADIN input is divided by 12.5, and has 4.82 mV
97 * per increment, so we have the additional multiply
99 voltage = regval * 482 * 125 / 1000;
102 /* If we get here, the developer messed up */
110 /* Return the current from the sense resistor in mA */
111 static unsigned int ltc4215_get_current(struct device *dev)
113 struct ltc4215_data *data = ltc4215_update_device(dev);
116 * The strange looking conversions that follow are fixed-point
117 * math, since we cannot do floating point in the kernel.
119 * Step 1: convert sense register to microVolts
120 * Step 2: convert voltage to milliAmperes
122 * If you play around with the V=IR equation, you come up with
123 * the following: X uV / Y mOhm == Z mA
125 * With the resistors that are fractions of a milliOhm, we multiply
126 * the voltage and resistance by 10, to shift the decimal point.
127 * Now we can use the normal division operator again.
130 /* Calculate voltage in microVolts (151 uV per increment) */
131 const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
133 /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
134 const unsigned int curr = voltage / 4;
139 static ssize_t ltc4215_show_voltage(struct device *dev,
140 struct device_attribute *da,
143 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
144 const int voltage = ltc4215_get_voltage(dev, attr->index);
146 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
149 static ssize_t ltc4215_show_current(struct device *dev,
150 struct device_attribute *da,
153 const unsigned int curr = ltc4215_get_current(dev);
155 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
158 static ssize_t ltc4215_show_power(struct device *dev,
159 struct device_attribute *da,
162 const unsigned int curr = ltc4215_get_current(dev);
163 const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
165 /* current in mA * voltage in mV == power in uW */
166 const unsigned int power = abs(output_voltage * curr);
168 return snprintf(buf, PAGE_SIZE, "%u\n", power);
171 static ssize_t ltc4215_show_alarm(struct device *dev,
172 struct device_attribute *da,
175 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
176 struct ltc4215_data *data = ltc4215_update_device(dev);
177 const u8 reg = data->regs[LTC4215_STATUS];
178 const u32 mask = attr->index;
180 return snprintf(buf, PAGE_SIZE, "%u\n", !!(reg & mask));
184 * These macros are used below in constructing device attribute objects
185 * for use with sysfs_create_group() to make a sysfs device file
189 /* Construct a sensor_device_attribute structure for each register */
192 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4215_show_current, NULL, 0);
193 static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
196 /* Power (virtual) */
197 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc4215_show_power, NULL, 0);
200 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4215_show_voltage, NULL,
202 static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
204 static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
208 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4215_show_voltage, NULL,
210 static SENSOR_DEVICE_ATTR(in2_min_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
214 * Finally, construct an array of pointers to members of the above objects,
215 * as required for sysfs_create_group()
217 static struct attribute *ltc4215_attributes[] = {
218 &sensor_dev_attr_curr1_input.dev_attr.attr,
219 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
221 &sensor_dev_attr_power1_input.dev_attr.attr,
223 &sensor_dev_attr_in1_input.dev_attr.attr,
224 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
225 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
227 &sensor_dev_attr_in2_input.dev_attr.attr,
228 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
233 static const struct attribute_group ltc4215_group = {
234 .attrs = ltc4215_attributes,
237 static int ltc4215_probe(struct i2c_client *client,
238 const struct i2c_device_id *id)
240 struct i2c_adapter *adapter = client->adapter;
241 struct ltc4215_data *data;
244 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
247 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
251 i2c_set_clientdata(client, data);
252 mutex_init(&data->update_lock);
254 /* Initialize the LTC4215 chip */
255 i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
257 /* Register sysfs hooks */
258 ret = sysfs_create_group(&client->dev.kobj, <c4215_group);
262 data->hwmon_dev = hwmon_device_register(&client->dev);
263 if (IS_ERR(data->hwmon_dev)) {
264 ret = PTR_ERR(data->hwmon_dev);
265 goto out_hwmon_device_register;
270 out_hwmon_device_register:
271 sysfs_remove_group(&client->dev.kobj, <c4215_group);
275 static int ltc4215_remove(struct i2c_client *client)
277 struct ltc4215_data *data = i2c_get_clientdata(client);
279 hwmon_device_unregister(data->hwmon_dev);
280 sysfs_remove_group(&client->dev.kobj, <c4215_group);
285 static const struct i2c_device_id ltc4215_id[] = {
289 MODULE_DEVICE_TABLE(i2c, ltc4215_id);
291 /* This is the driver that will be inserted */
292 static struct i2c_driver ltc4215_driver = {
296 .probe = ltc4215_probe,
297 .remove = ltc4215_remove,
298 .id_table = ltc4215_id,
301 module_i2c_driver(ltc4215_driver);
303 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
304 MODULE_DESCRIPTION("LTC4215 driver");
305 MODULE_LICENSE("GPL");