blob: c8a9bd9b050ffb78a3ee2da6bf35b2cdeec5e600 [file] [log] [blame]
Ira Snyder72f5de92009-03-31 15:24:29 -07001/*
2 * Driver for Linear Technology LTC4215 I2C Hot Swap Controller
3 *
4 * Copyright (C) 2009 Ira W. Snyder <iws@ovro.caltech.edu>
5 *
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.
9 *
10 * Datasheet:
11 * http://www.linear.com/pc/downloadDocument.do?navId=H0,C1,C1003,C1006,C1163,P17572,D12697
12 */
13
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>
Jean Delvaredcd8f392012-10-10 15:25:56 +020022#include <linux/jiffies.h>
Ira Snyder72f5de92009-03-31 15:24:29 -070023
Ira Snyder72f5de92009-03-31 15:24:29 -070024/* Here are names of the chip's registers (a.k.a. commands) */
25enum ltc4215_cmd {
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 */
33};
34
35struct ltc4215_data {
Guenter Roeck6c588b42014-02-15 22:05:17 -080036 struct i2c_client *client;
Ira Snyder72f5de92009-03-31 15:24:29 -070037
38 struct mutex update_lock;
39 bool valid;
40 unsigned long last_updated; /* in jiffies */
41
42 /* Registers */
43 u8 regs[7];
44};
45
46static struct ltc4215_data *ltc4215_update_device(struct device *dev)
47{
Guenter Roeck6c588b42014-02-15 22:05:17 -080048 struct ltc4215_data *data = dev_get_drvdata(dev);
49 struct i2c_client *client = data->client;
Ira Snyder72f5de92009-03-31 15:24:29 -070050 s32 val;
51 int i;
52
53 mutex_lock(&data->update_lock);
54
55 /* The chip's A/D updates 10 times per second */
56 if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
57
58 dev_dbg(&client->dev, "Starting ltc4215 update\n");
59
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))
64 data->regs[i] = 0;
65 else
66 data->regs[i] = val;
67 }
68
69 data->last_updated = jiffies;
70 data->valid = 1;
71 }
72
73 mutex_unlock(&data->update_lock);
74
75 return data;
76}
77
78/* Return the voltage from the given register in millivolts */
79static int ltc4215_get_voltage(struct device *dev, u8 reg)
80{
81 struct ltc4215_data *data = ltc4215_update_device(dev);
82 const u8 regval = data->regs[reg];
83 u32 voltage = 0;
84
85 switch (reg) {
86 case LTC4215_SENSE:
87 /* 151 uV per increment */
88 voltage = regval * 151 / 1000;
89 break;
90 case LTC4215_SOURCE:
91 /* 60.5 mV per increment */
92 voltage = regval * 605 / 10;
93 break;
94 case LTC4215_ADIN:
Guenter Roeck790fa382012-01-19 11:02:21 -080095 /*
96 * The ADIN input is divided by 12.5, and has 4.82 mV
97 * per increment, so we have the additional multiply
98 */
Ira Snyder72f5de92009-03-31 15:24:29 -070099 voltage = regval * 482 * 125 / 1000;
100 break;
101 default:
102 /* If we get here, the developer messed up */
103 WARN_ON_ONCE(1);
104 break;
105 }
106
107 return voltage;
108}
109
110/* Return the current from the sense resistor in mA */
111static unsigned int ltc4215_get_current(struct device *dev)
112{
113 struct ltc4215_data *data = ltc4215_update_device(dev);
114
Guenter Roeck790fa382012-01-19 11:02:21 -0800115 /*
116 * The strange looking conversions that follow are fixed-point
Ira Snyder72f5de92009-03-31 15:24:29 -0700117 * math, since we cannot do floating point in the kernel.
118 *
119 * Step 1: convert sense register to microVolts
120 * Step 2: convert voltage to milliAmperes
121 *
122 * If you play around with the V=IR equation, you come up with
123 * the following: X uV / Y mOhm == Z mA
124 *
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.
128 */
129
130 /* Calculate voltage in microVolts (151 uV per increment) */
131 const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
132
133 /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
134 const unsigned int curr = voltage / 4;
135
136 return curr;
137}
138
139static ssize_t ltc4215_show_voltage(struct device *dev,
140 struct device_attribute *da,
141 char *buf)
142{
143 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
144 const int voltage = ltc4215_get_voltage(dev, attr->index);
145
146 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
147}
148
149static ssize_t ltc4215_show_current(struct device *dev,
150 struct device_attribute *da,
151 char *buf)
152{
153 const unsigned int curr = ltc4215_get_current(dev);
154
155 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
156}
157
158static ssize_t ltc4215_show_power(struct device *dev,
159 struct device_attribute *da,
160 char *buf)
161{
162 const unsigned int curr = ltc4215_get_current(dev);
163 const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
164
165 /* current in mA * voltage in mV == power in uW */
166 const unsigned int power = abs(output_voltage * curr);
167
168 return snprintf(buf, PAGE_SIZE, "%u\n", power);
169}
170
171static ssize_t ltc4215_show_alarm(struct device *dev,
172 struct device_attribute *da,
173 char *buf)
174{
Guenter Roeckb5f0f1e2013-01-10 10:30:31 -0800175 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
Ira Snyder72f5de92009-03-31 15:24:29 -0700176 struct ltc4215_data *data = ltc4215_update_device(dev);
Guenter Roeckb5f0f1e2013-01-10 10:30:31 -0800177 const u8 reg = data->regs[LTC4215_STATUS];
178 const u32 mask = attr->index;
Ira Snyder72f5de92009-03-31 15:24:29 -0700179
Guenter Roeckb5f0f1e2013-01-10 10:30:31 -0800180 return snprintf(buf, PAGE_SIZE, "%u\n", !!(reg & mask));
Ira Snyder72f5de92009-03-31 15:24:29 -0700181}
182
Guenter Roeck790fa382012-01-19 11:02:21 -0800183/*
184 * These macros are used below in constructing device attribute objects
Ira Snyder72f5de92009-03-31 15:24:29 -0700185 * for use with sysfs_create_group() to make a sysfs device file
186 * for each register.
187 */
188
Ira Snyder72f5de92009-03-31 15:24:29 -0700189/* Construct a sensor_device_attribute structure for each register */
190
191/* Current */
Guenter Roeckb5f0f1e2013-01-10 10:30:31 -0800192static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4215_show_current, NULL, 0);
193static SENSOR_DEVICE_ATTR(curr1_max_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
194 1 << 2);
Ira Snyder72f5de92009-03-31 15:24:29 -0700195
196/* Power (virtual) */
Guenter Roeckb5f0f1e2013-01-10 10:30:31 -0800197static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc4215_show_power, NULL, 0);
Ira Snyder72f5de92009-03-31 15:24:29 -0700198
199/* Input Voltage */
Guenter Roeckb5f0f1e2013-01-10 10:30:31 -0800200static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4215_show_voltage, NULL,
201 LTC4215_ADIN);
202static SENSOR_DEVICE_ATTR(in1_max_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
203 1 << 0);
204static SENSOR_DEVICE_ATTR(in1_min_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
205 1 << 1);
Ira Snyder72f5de92009-03-31 15:24:29 -0700206
207/* Output Voltage */
Guenter Roeckb5f0f1e2013-01-10 10:30:31 -0800208static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4215_show_voltage, NULL,
209 LTC4215_SOURCE);
210static SENSOR_DEVICE_ATTR(in2_min_alarm, S_IRUGO, ltc4215_show_alarm, NULL,
211 1 << 3);
Ira Snyder72f5de92009-03-31 15:24:29 -0700212
Guenter Roeck790fa382012-01-19 11:02:21 -0800213/*
214 * Finally, construct an array of pointers to members of the above objects,
Ira Snyder72f5de92009-03-31 15:24:29 -0700215 * as required for sysfs_create_group()
216 */
Guenter Roeck6c588b42014-02-15 22:05:17 -0800217static struct attribute *ltc4215_attrs[] = {
Ira Snyder72f5de92009-03-31 15:24:29 -0700218 &sensor_dev_attr_curr1_input.dev_attr.attr,
219 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
220
221 &sensor_dev_attr_power1_input.dev_attr.attr,
Ira Snyder72f5de92009-03-31 15:24:29 -0700222
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,
226
227 &sensor_dev_attr_in2_input.dev_attr.attr,
Ira W. Snyder0a6bf652010-12-13 11:42:30 -0500228 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
Ira Snyder72f5de92009-03-31 15:24:29 -0700229
230 NULL,
231};
Guenter Roeck6c588b42014-02-15 22:05:17 -0800232ATTRIBUTE_GROUPS(ltc4215);
Ira Snyder72f5de92009-03-31 15:24:29 -0700233
234static int ltc4215_probe(struct i2c_client *client,
235 const struct i2c_device_id *id)
236{
Jean Delvare2d2a7cf2009-10-04 22:53:42 +0200237 struct i2c_adapter *adapter = client->adapter;
Guenter Roeck6c588b42014-02-15 22:05:17 -0800238 struct device *dev = &client->dev;
Ira Snyder72f5de92009-03-31 15:24:29 -0700239 struct ltc4215_data *data;
Guenter Roeck6c588b42014-02-15 22:05:17 -0800240 struct device *hwmon_dev;
Ira Snyder72f5de92009-03-31 15:24:29 -0700241
Jean Delvare2d2a7cf2009-10-04 22:53:42 +0200242 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
243 return -ENODEV;
244
Guenter Roeck6c588b42014-02-15 22:05:17 -0800245 data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
Guenter Roeckd2b95e52012-06-02 10:35:52 -0700246 if (!data)
247 return -ENOMEM;
Ira Snyder72f5de92009-03-31 15:24:29 -0700248
Guenter Roeck6c588b42014-02-15 22:05:17 -0800249 data->client = client;
Ira Snyder72f5de92009-03-31 15:24:29 -0700250 mutex_init(&data->update_lock);
251
252 /* Initialize the LTC4215 chip */
Ira W. Snyderb6b9d692009-09-23 22:59:43 +0200253 i2c_smbus_write_byte_data(client, LTC4215_FAULT, 0x00);
Ira Snyder72f5de92009-03-31 15:24:29 -0700254
Guenter Roeck6c588b42014-02-15 22:05:17 -0800255 hwmon_dev = devm_hwmon_device_register_with_groups(dev, client->name,
256 data,
257 ltc4215_groups);
258 return PTR_ERR_OR_ZERO(hwmon_dev);
Ira Snyder72f5de92009-03-31 15:24:29 -0700259}
260
Ira Snyder72f5de92009-03-31 15:24:29 -0700261static const struct i2c_device_id ltc4215_id[] = {
Jean Delvare2d2a7cf2009-10-04 22:53:42 +0200262 { "ltc4215", 0 },
Ira Snyder72f5de92009-03-31 15:24:29 -0700263 { }
264};
265MODULE_DEVICE_TABLE(i2c, ltc4215_id);
266
267/* This is the driver that will be inserted */
268static struct i2c_driver ltc4215_driver = {
Ira Snyder72f5de92009-03-31 15:24:29 -0700269 .driver = {
270 .name = "ltc4215",
271 },
272 .probe = ltc4215_probe,
Ira Snyder72f5de92009-03-31 15:24:29 -0700273 .id_table = ltc4215_id,
Ira Snyder72f5de92009-03-31 15:24:29 -0700274};
275
Axel Linf0967ee2012-01-20 15:38:18 +0800276module_i2c_driver(ltc4215_driver);
Ira Snyder72f5de92009-03-31 15:24:29 -0700277
278MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
279MODULE_DESCRIPTION("LTC4215 driver");
280MODULE_LICENSE("GPL");