conn rcv_lock converted to spinlock, struct cor_sock created, kernel_packet skb_clone...
[cor_2_6_31.git] / drivers / hwmon / ltc4215.c
blob9386e2a39211f75e9a610e899fc43ff755c9dc25
1 /*
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.
10 * Datasheet:
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>
23 static const unsigned short normal_i2c[] = { I2C_CLIENT_END };
25 /* Insmod parameters */
26 I2C_CLIENT_INSMOD_1(ltc4215);
28 /* Here are names of the chip's registers (a.k.a. commands) */
29 enum ltc4215_cmd {
30 LTC4215_CONTROL = 0x00, /* rw */
31 LTC4215_ALERT = 0x01, /* rw */
32 LTC4215_STATUS = 0x02, /* ro */
33 LTC4215_FAULT = 0x03, /* rw */
34 LTC4215_SENSE = 0x04, /* rw */
35 LTC4215_SOURCE = 0x05, /* rw */
36 LTC4215_ADIN = 0x06, /* rw */
39 struct ltc4215_data {
40 struct device *hwmon_dev;
42 struct mutex update_lock;
43 bool valid;
44 unsigned long last_updated; /* in jiffies */
46 /* Registers */
47 u8 regs[7];
50 static struct ltc4215_data *ltc4215_update_device(struct device *dev)
52 struct i2c_client *client = to_i2c_client(dev);
53 struct ltc4215_data *data = i2c_get_clientdata(client);
54 s32 val;
55 int i;
57 mutex_lock(&data->update_lock);
59 /* The chip's A/D updates 10 times per second */
60 if (time_after(jiffies, data->last_updated + HZ / 10) || !data->valid) {
62 dev_dbg(&client->dev, "Starting ltc4215 update\n");
64 /* Read all registers */
65 for (i = 0; i < ARRAY_SIZE(data->regs); i++) {
66 val = i2c_smbus_read_byte_data(client, i);
67 if (unlikely(val < 0))
68 data->regs[i] = 0;
69 else
70 data->regs[i] = val;
73 data->last_updated = jiffies;
74 data->valid = 1;
77 mutex_unlock(&data->update_lock);
79 return data;
82 /* Return the voltage from the given register in millivolts */
83 static int ltc4215_get_voltage(struct device *dev, u8 reg)
85 struct ltc4215_data *data = ltc4215_update_device(dev);
86 const u8 regval = data->regs[reg];
87 u32 voltage = 0;
89 switch (reg) {
90 case LTC4215_SENSE:
91 /* 151 uV per increment */
92 voltage = regval * 151 / 1000;
93 break;
94 case LTC4215_SOURCE:
95 /* 60.5 mV per increment */
96 voltage = regval * 605 / 10;
97 break;
98 case LTC4215_ADIN:
99 /* The ADIN input is divided by 12.5, and has 4.82 mV
100 * per increment, so we have the additional multiply */
101 voltage = regval * 482 * 125 / 1000;
102 break;
103 default:
104 /* If we get here, the developer messed up */
105 WARN_ON_ONCE(1);
106 break;
109 return voltage;
112 /* Return the current from the sense resistor in mA */
113 static unsigned int ltc4215_get_current(struct device *dev)
115 struct ltc4215_data *data = ltc4215_update_device(dev);
117 /* The strange looking conversions that follow are fixed-point
118 * math, since we cannot do floating point in the kernel.
120 * Step 1: convert sense register to microVolts
121 * Step 2: convert voltage to milliAmperes
123 * If you play around with the V=IR equation, you come up with
124 * the following: X uV / Y mOhm == Z mA
126 * With the resistors that are fractions of a milliOhm, we multiply
127 * the voltage and resistance by 10, to shift the decimal point.
128 * Now we can use the normal division operator again.
131 /* Calculate voltage in microVolts (151 uV per increment) */
132 const unsigned int voltage = data->regs[LTC4215_SENSE] * 151;
134 /* Calculate current in milliAmperes (4 milliOhm sense resistor) */
135 const unsigned int curr = voltage / 4;
137 return curr;
140 static ssize_t ltc4215_show_voltage(struct device *dev,
141 struct device_attribute *da,
142 char *buf)
144 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
145 const int voltage = ltc4215_get_voltage(dev, attr->index);
147 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
150 static ssize_t ltc4215_show_current(struct device *dev,
151 struct device_attribute *da,
152 char *buf)
154 const unsigned int curr = ltc4215_get_current(dev);
156 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
159 static ssize_t ltc4215_show_power(struct device *dev,
160 struct device_attribute *da,
161 char *buf)
163 const unsigned int curr = ltc4215_get_current(dev);
164 const int output_voltage = ltc4215_get_voltage(dev, LTC4215_ADIN);
166 /* current in mA * voltage in mV == power in uW */
167 const unsigned int power = abs(output_voltage * curr);
169 return snprintf(buf, PAGE_SIZE, "%u\n", power);
172 static ssize_t ltc4215_show_alarm(struct device *dev,
173 struct device_attribute *da,
174 char *buf)
176 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
177 struct ltc4215_data *data = ltc4215_update_device(dev);
178 const u8 reg = data->regs[attr->index];
179 const u32 mask = attr->nr;
181 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
184 /* These macros are used below in constructing device attribute objects
185 * for use with sysfs_create_group() to make a sysfs device file
186 * for each register.
189 #define LTC4215_VOLTAGE(name, ltc4215_cmd_idx) \
190 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
191 ltc4215_show_voltage, NULL, ltc4215_cmd_idx)
193 #define LTC4215_CURRENT(name) \
194 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
195 ltc4215_show_current, NULL, 0);
197 #define LTC4215_POWER(name) \
198 static SENSOR_DEVICE_ATTR(name, S_IRUGO, \
199 ltc4215_show_power, NULL, 0);
201 #define LTC4215_ALARM(name, mask, reg) \
202 static SENSOR_DEVICE_ATTR_2(name, S_IRUGO, \
203 ltc4215_show_alarm, NULL, (mask), reg)
205 /* Construct a sensor_device_attribute structure for each register */
207 /* Current */
208 LTC4215_CURRENT(curr1_input);
209 LTC4215_ALARM(curr1_max_alarm, (1 << 2), LTC4215_STATUS);
211 /* Power (virtual) */
212 LTC4215_POWER(power1_input);
213 LTC4215_ALARM(power1_alarm, (1 << 3), LTC4215_STATUS);
215 /* Input Voltage */
216 LTC4215_VOLTAGE(in1_input, LTC4215_ADIN);
217 LTC4215_ALARM(in1_max_alarm, (1 << 0), LTC4215_STATUS);
218 LTC4215_ALARM(in1_min_alarm, (1 << 1), LTC4215_STATUS);
220 /* Output Voltage */
221 LTC4215_VOLTAGE(in2_input, LTC4215_SOURCE);
223 /* Finally, construct an array of pointers to members of the above objects,
224 * as required for sysfs_create_group()
226 static struct attribute *ltc4215_attributes[] = {
227 &sensor_dev_attr_curr1_input.dev_attr.attr,
228 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
230 &sensor_dev_attr_power1_input.dev_attr.attr,
231 &sensor_dev_attr_power1_alarm.dev_attr.attr,
233 &sensor_dev_attr_in1_input.dev_attr.attr,
234 &sensor_dev_attr_in1_max_alarm.dev_attr.attr,
235 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
237 &sensor_dev_attr_in2_input.dev_attr.attr,
239 NULL,
242 static const struct attribute_group ltc4215_group = {
243 .attrs = ltc4215_attributes,
246 static int ltc4215_probe(struct i2c_client *client,
247 const struct i2c_device_id *id)
249 struct ltc4215_data *data;
250 int ret;
252 data = kzalloc(sizeof(*data), GFP_KERNEL);
253 if (!data) {
254 ret = -ENOMEM;
255 goto out_kzalloc;
258 i2c_set_clientdata(client, data);
259 mutex_init(&data->update_lock);
261 /* Initialize the LTC4215 chip */
262 /* TODO */
264 /* Register sysfs hooks */
265 ret = sysfs_create_group(&client->dev.kobj, &ltc4215_group);
266 if (ret)
267 goto out_sysfs_create_group;
269 data->hwmon_dev = hwmon_device_register(&client->dev);
270 if (IS_ERR(data->hwmon_dev)) {
271 ret = PTR_ERR(data->hwmon_dev);
272 goto out_hwmon_device_register;
275 return 0;
277 out_hwmon_device_register:
278 sysfs_remove_group(&client->dev.kobj, &ltc4215_group);
279 out_sysfs_create_group:
280 kfree(data);
281 out_kzalloc:
282 return ret;
285 static int ltc4215_remove(struct i2c_client *client)
287 struct ltc4215_data *data = i2c_get_clientdata(client);
289 hwmon_device_unregister(data->hwmon_dev);
290 sysfs_remove_group(&client->dev.kobj, &ltc4215_group);
292 kfree(data);
294 return 0;
297 static int ltc4215_detect(struct i2c_client *client,
298 int kind,
299 struct i2c_board_info *info)
301 struct i2c_adapter *adapter = client->adapter;
303 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
304 return -ENODEV;
306 if (kind < 0) { /* probed detection - check the chip type */
307 s32 v; /* 8 bits from the chip, or -ERRNO */
310 * Register 0x01 bit b7 is reserved, expect 0
311 * Register 0x03 bit b6 and b7 are reserved, expect 0
313 v = i2c_smbus_read_byte_data(client, LTC4215_ALERT);
314 if (v < 0 || (v & (1 << 7)) != 0)
315 return -ENODEV;
317 v = i2c_smbus_read_byte_data(client, LTC4215_FAULT);
318 if (v < 0 || (v & ((1 << 6) | (1 << 7))) != 0)
319 return -ENODEV;
322 strlcpy(info->type, "ltc4215", I2C_NAME_SIZE);
323 dev_info(&adapter->dev, "ltc4215 %s at address 0x%02x\n",
324 kind < 0 ? "probed" : "forced",
325 client->addr);
327 return 0;
330 static const struct i2c_device_id ltc4215_id[] = {
331 { "ltc4215", ltc4215 },
334 MODULE_DEVICE_TABLE(i2c, ltc4215_id);
336 /* This is the driver that will be inserted */
337 static struct i2c_driver ltc4215_driver = {
338 .class = I2C_CLASS_HWMON,
339 .driver = {
340 .name = "ltc4215",
342 .probe = ltc4215_probe,
343 .remove = ltc4215_remove,
344 .id_table = ltc4215_id,
345 .detect = ltc4215_detect,
346 .address_data = &addr_data,
349 static int __init ltc4215_init(void)
351 return i2c_add_driver(&ltc4215_driver);
354 static void __exit ltc4215_exit(void)
356 i2c_del_driver(&ltc4215_driver);
359 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
360 MODULE_DESCRIPTION("LTC4215 driver");
361 MODULE_LICENSE("GPL");
363 module_init(ltc4215_init);
364 module_exit(ltc4215_exit);