ARM: 7989/1: Delete asm/system.h
[linux/fpc-iii.git] / drivers / hwmon / ltc4245.c
blobd4172933ce4f2b67d8696b97527d25e7fd5fab48
1 /*
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.
12 * Datasheet:
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>
24 #include <linux/jiffies.h>
25 #include <linux/i2c/ltc4245.h>
27 /* Here are names of the chip's registers (a.k.a. commands) */
28 enum ltc4245_cmd {
29 LTC4245_STATUS = 0x00, /* readonly */
30 LTC4245_ALERT = 0x01,
31 LTC4245_CONTROL = 0x02,
32 LTC4245_ON = 0x03,
33 LTC4245_FAULT1 = 0x04,
34 LTC4245_FAULT2 = 0x05,
35 LTC4245_GPIO = 0x06,
36 LTC4245_ADCADR = 0x07,
38 LTC4245_12VIN = 0x10,
39 LTC4245_12VSENSE = 0x11,
40 LTC4245_12VOUT = 0x12,
41 LTC4245_5VIN = 0x13,
42 LTC4245_5VSENSE = 0x14,
43 LTC4245_5VOUT = 0x15,
44 LTC4245_3VIN = 0x16,
45 LTC4245_3VSENSE = 0x17,
46 LTC4245_3VOUT = 0x18,
47 LTC4245_VEEIN = 0x19,
48 LTC4245_VEESENSE = 0x1a,
49 LTC4245_VEEOUT = 0x1b,
50 LTC4245_GPIOADC = 0x1c,
53 struct ltc4245_data {
54 struct i2c_client *client;
56 const struct attribute_group *groups[3];
58 struct mutex update_lock;
59 bool valid;
60 unsigned long last_updated; /* in jiffies */
62 /* Control registers */
63 u8 cregs[0x08];
65 /* Voltage registers */
66 u8 vregs[0x0d];
68 /* GPIO ADC registers */
69 bool use_extra_gpios;
70 int gpios[3];
74 * Update the readings from the GPIO pins. If the driver has been configured to
75 * sample all GPIO's as analog voltages, a round-robin sampling method is used.
76 * Otherwise, only the configured GPIO pin is sampled.
78 * LOCKING: must hold data->update_lock
80 static void ltc4245_update_gpios(struct device *dev)
82 struct ltc4245_data *data = dev_get_drvdata(dev);
83 struct i2c_client *client = data->client;
84 u8 gpio_curr, gpio_next, gpio_reg;
85 int i;
87 /* no extra gpio support, we're basically done */
88 if (!data->use_extra_gpios) {
89 data->gpios[0] = data->vregs[LTC4245_GPIOADC - 0x10];
90 return;
94 * If the last reading was too long ago, then we mark all old GPIO
95 * readings as stale by setting them to -EAGAIN
97 if (time_after(jiffies, data->last_updated + 5 * HZ)) {
98 dev_dbg(&client->dev, "Marking GPIOs invalid\n");
99 for (i = 0; i < ARRAY_SIZE(data->gpios); i++)
100 data->gpios[i] = -EAGAIN;
104 * Get the current GPIO pin
106 * The datasheet calls these GPIO[1-3], but we'll calculate the zero
107 * based array index instead, and call them GPIO[0-2]. This is much
108 * easier to think about.
110 gpio_curr = (data->cregs[LTC4245_GPIO] & 0xc0) >> 6;
111 if (gpio_curr > 0)
112 gpio_curr -= 1;
114 /* Read the GPIO voltage from the GPIOADC register */
115 data->gpios[gpio_curr] = data->vregs[LTC4245_GPIOADC - 0x10];
117 /* Find the next GPIO pin to read */
118 gpio_next = (gpio_curr + 1) % ARRAY_SIZE(data->gpios);
121 * Calculate the correct setting for the GPIO register so it will
122 * sample the next GPIO pin
124 gpio_reg = (data->cregs[LTC4245_GPIO] & 0x3f) | ((gpio_next + 1) << 6);
126 /* Update the GPIO register */
127 i2c_smbus_write_byte_data(client, LTC4245_GPIO, gpio_reg);
129 /* Update saved data */
130 data->cregs[LTC4245_GPIO] = gpio_reg;
133 static struct ltc4245_data *ltc4245_update_device(struct device *dev)
135 struct ltc4245_data *data = dev_get_drvdata(dev);
136 struct i2c_client *client = data->client;
137 s32 val;
138 int i;
140 mutex_lock(&data->update_lock);
142 if (time_after(jiffies, data->last_updated + HZ) || !data->valid) {
144 dev_dbg(&client->dev, "Starting ltc4245 update\n");
146 /* Read control registers -- 0x00 to 0x07 */
147 for (i = 0; i < ARRAY_SIZE(data->cregs); i++) {
148 val = i2c_smbus_read_byte_data(client, i);
149 if (unlikely(val < 0))
150 data->cregs[i] = 0;
151 else
152 data->cregs[i] = val;
155 /* Read voltage registers -- 0x10 to 0x1c */
156 for (i = 0; i < ARRAY_SIZE(data->vregs); i++) {
157 val = i2c_smbus_read_byte_data(client, i+0x10);
158 if (unlikely(val < 0))
159 data->vregs[i] = 0;
160 else
161 data->vregs[i] = val;
164 /* Update GPIO readings */
165 ltc4245_update_gpios(dev);
167 data->last_updated = jiffies;
168 data->valid = 1;
171 mutex_unlock(&data->update_lock);
173 return data;
176 /* Return the voltage from the given register in millivolts */
177 static int ltc4245_get_voltage(struct device *dev, u8 reg)
179 struct ltc4245_data *data = ltc4245_update_device(dev);
180 const u8 regval = data->vregs[reg - 0x10];
181 u32 voltage = 0;
183 switch (reg) {
184 case LTC4245_12VIN:
185 case LTC4245_12VOUT:
186 voltage = regval * 55;
187 break;
188 case LTC4245_5VIN:
189 case LTC4245_5VOUT:
190 voltage = regval * 22;
191 break;
192 case LTC4245_3VIN:
193 case LTC4245_3VOUT:
194 voltage = regval * 15;
195 break;
196 case LTC4245_VEEIN:
197 case LTC4245_VEEOUT:
198 voltage = regval * -55;
199 break;
200 case LTC4245_GPIOADC:
201 voltage = regval * 10;
202 break;
203 default:
204 /* If we get here, the developer messed up */
205 WARN_ON_ONCE(1);
206 break;
209 return voltage;
212 /* Return the current in the given sense register in milliAmperes */
213 static unsigned int ltc4245_get_current(struct device *dev, u8 reg)
215 struct ltc4245_data *data = ltc4245_update_device(dev);
216 const u8 regval = data->vregs[reg - 0x10];
217 unsigned int voltage;
218 unsigned int curr;
221 * The strange looking conversions that follow are fixed-point
222 * math, since we cannot do floating point in the kernel.
224 * Step 1: convert sense register to microVolts
225 * Step 2: convert voltage to milliAmperes
227 * If you play around with the V=IR equation, you come up with
228 * the following: X uV / Y mOhm == Z mA
230 * With the resistors that are fractions of a milliOhm, we multiply
231 * the voltage and resistance by 10, to shift the decimal point.
232 * Now we can use the normal division operator again.
235 switch (reg) {
236 case LTC4245_12VSENSE:
237 voltage = regval * 250; /* voltage in uV */
238 curr = voltage / 50; /* sense resistor 50 mOhm */
239 break;
240 case LTC4245_5VSENSE:
241 voltage = regval * 125; /* voltage in uV */
242 curr = (voltage * 10) / 35; /* sense resistor 3.5 mOhm */
243 break;
244 case LTC4245_3VSENSE:
245 voltage = regval * 125; /* voltage in uV */
246 curr = (voltage * 10) / 25; /* sense resistor 2.5 mOhm */
247 break;
248 case LTC4245_VEESENSE:
249 voltage = regval * 250; /* voltage in uV */
250 curr = voltage / 100; /* sense resistor 100 mOhm */
251 break;
252 default:
253 /* If we get here, the developer messed up */
254 WARN_ON_ONCE(1);
255 curr = 0;
256 break;
259 return curr;
262 static ssize_t ltc4245_show_voltage(struct device *dev,
263 struct device_attribute *da,
264 char *buf)
266 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
267 const int voltage = ltc4245_get_voltage(dev, attr->index);
269 return snprintf(buf, PAGE_SIZE, "%d\n", voltage);
272 static ssize_t ltc4245_show_current(struct device *dev,
273 struct device_attribute *da,
274 char *buf)
276 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
277 const unsigned int curr = ltc4245_get_current(dev, attr->index);
279 return snprintf(buf, PAGE_SIZE, "%u\n", curr);
282 static ssize_t ltc4245_show_power(struct device *dev,
283 struct device_attribute *da,
284 char *buf)
286 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
287 const unsigned int curr = ltc4245_get_current(dev, attr->index);
288 const int output_voltage = ltc4245_get_voltage(dev, attr->index+1);
290 /* current in mA * voltage in mV == power in uW */
291 const unsigned int power = abs(output_voltage * curr);
293 return snprintf(buf, PAGE_SIZE, "%u\n", power);
296 static ssize_t ltc4245_show_alarm(struct device *dev,
297 struct device_attribute *da,
298 char *buf)
300 struct sensor_device_attribute_2 *attr = to_sensor_dev_attr_2(da);
301 struct ltc4245_data *data = ltc4245_update_device(dev);
302 const u8 reg = data->cregs[attr->index];
303 const u32 mask = attr->nr;
305 return snprintf(buf, PAGE_SIZE, "%u\n", (reg & mask) ? 1 : 0);
308 static ssize_t ltc4245_show_gpio(struct device *dev,
309 struct device_attribute *da,
310 char *buf)
312 struct sensor_device_attribute *attr = to_sensor_dev_attr(da);
313 struct ltc4245_data *data = ltc4245_update_device(dev);
314 int val = data->gpios[attr->index];
316 /* handle stale GPIO's */
317 if (val < 0)
318 return val;
320 /* Convert to millivolts and print */
321 return snprintf(buf, PAGE_SIZE, "%u\n", val * 10);
324 /* Construct a sensor_device_attribute structure for each register */
326 /* Input voltages */
327 static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, ltc4245_show_voltage, NULL,
328 LTC4245_12VIN);
329 static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, ltc4245_show_voltage, NULL,
330 LTC4245_5VIN);
331 static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, ltc4245_show_voltage, NULL,
332 LTC4245_3VIN);
333 static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, ltc4245_show_voltage, NULL,
334 LTC4245_VEEIN);
336 /* Input undervoltage alarms */
337 static SENSOR_DEVICE_ATTR_2(in1_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
338 1 << 0, LTC4245_FAULT1);
339 static SENSOR_DEVICE_ATTR_2(in2_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
340 1 << 1, LTC4245_FAULT1);
341 static SENSOR_DEVICE_ATTR_2(in3_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
342 1 << 2, LTC4245_FAULT1);
343 static SENSOR_DEVICE_ATTR_2(in4_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
344 1 << 3, LTC4245_FAULT1);
346 /* Currents (via sense resistor) */
347 static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, ltc4245_show_current, NULL,
348 LTC4245_12VSENSE);
349 static SENSOR_DEVICE_ATTR(curr2_input, S_IRUGO, ltc4245_show_current, NULL,
350 LTC4245_5VSENSE);
351 static SENSOR_DEVICE_ATTR(curr3_input, S_IRUGO, ltc4245_show_current, NULL,
352 LTC4245_3VSENSE);
353 static SENSOR_DEVICE_ATTR(curr4_input, S_IRUGO, ltc4245_show_current, NULL,
354 LTC4245_VEESENSE);
356 /* Overcurrent alarms */
357 static SENSOR_DEVICE_ATTR_2(curr1_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
358 1 << 4, LTC4245_FAULT1);
359 static SENSOR_DEVICE_ATTR_2(curr2_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
360 1 << 5, LTC4245_FAULT1);
361 static SENSOR_DEVICE_ATTR_2(curr3_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
362 1 << 6, LTC4245_FAULT1);
363 static SENSOR_DEVICE_ATTR_2(curr4_max_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
364 1 << 7, LTC4245_FAULT1);
366 /* Output voltages */
367 static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, ltc4245_show_voltage, NULL,
368 LTC4245_12VOUT);
369 static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, ltc4245_show_voltage, NULL,
370 LTC4245_5VOUT);
371 static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, ltc4245_show_voltage, NULL,
372 LTC4245_3VOUT);
373 static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, ltc4245_show_voltage, NULL,
374 LTC4245_VEEOUT);
376 /* Power Bad alarms */
377 static SENSOR_DEVICE_ATTR_2(in5_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
378 1 << 0, LTC4245_FAULT2);
379 static SENSOR_DEVICE_ATTR_2(in6_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
380 1 << 1, LTC4245_FAULT2);
381 static SENSOR_DEVICE_ATTR_2(in7_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
382 1 << 2, LTC4245_FAULT2);
383 static SENSOR_DEVICE_ATTR_2(in8_min_alarm, S_IRUGO, ltc4245_show_alarm, NULL,
384 1 << 3, LTC4245_FAULT2);
386 /* GPIO voltages */
387 static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, ltc4245_show_gpio, NULL, 0);
388 static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, ltc4245_show_gpio, NULL, 1);
389 static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, ltc4245_show_gpio, NULL, 2);
391 /* Power Consumption (virtual) */
392 static SENSOR_DEVICE_ATTR(power1_input, S_IRUGO, ltc4245_show_power, NULL,
393 LTC4245_12VSENSE);
394 static SENSOR_DEVICE_ATTR(power2_input, S_IRUGO, ltc4245_show_power, NULL,
395 LTC4245_5VSENSE);
396 static SENSOR_DEVICE_ATTR(power3_input, S_IRUGO, ltc4245_show_power, NULL,
397 LTC4245_3VSENSE);
398 static SENSOR_DEVICE_ATTR(power4_input, S_IRUGO, ltc4245_show_power, NULL,
399 LTC4245_VEESENSE);
402 * Finally, construct an array of pointers to members of the above objects,
403 * as required for sysfs_create_group()
405 static struct attribute *ltc4245_std_attributes[] = {
406 &sensor_dev_attr_in1_input.dev_attr.attr,
407 &sensor_dev_attr_in2_input.dev_attr.attr,
408 &sensor_dev_attr_in3_input.dev_attr.attr,
409 &sensor_dev_attr_in4_input.dev_attr.attr,
411 &sensor_dev_attr_in1_min_alarm.dev_attr.attr,
412 &sensor_dev_attr_in2_min_alarm.dev_attr.attr,
413 &sensor_dev_attr_in3_min_alarm.dev_attr.attr,
414 &sensor_dev_attr_in4_min_alarm.dev_attr.attr,
416 &sensor_dev_attr_curr1_input.dev_attr.attr,
417 &sensor_dev_attr_curr2_input.dev_attr.attr,
418 &sensor_dev_attr_curr3_input.dev_attr.attr,
419 &sensor_dev_attr_curr4_input.dev_attr.attr,
421 &sensor_dev_attr_curr1_max_alarm.dev_attr.attr,
422 &sensor_dev_attr_curr2_max_alarm.dev_attr.attr,
423 &sensor_dev_attr_curr3_max_alarm.dev_attr.attr,
424 &sensor_dev_attr_curr4_max_alarm.dev_attr.attr,
426 &sensor_dev_attr_in5_input.dev_attr.attr,
427 &sensor_dev_attr_in6_input.dev_attr.attr,
428 &sensor_dev_attr_in7_input.dev_attr.attr,
429 &sensor_dev_attr_in8_input.dev_attr.attr,
431 &sensor_dev_attr_in5_min_alarm.dev_attr.attr,
432 &sensor_dev_attr_in6_min_alarm.dev_attr.attr,
433 &sensor_dev_attr_in7_min_alarm.dev_attr.attr,
434 &sensor_dev_attr_in8_min_alarm.dev_attr.attr,
436 &sensor_dev_attr_in9_input.dev_attr.attr,
438 &sensor_dev_attr_power1_input.dev_attr.attr,
439 &sensor_dev_attr_power2_input.dev_attr.attr,
440 &sensor_dev_attr_power3_input.dev_attr.attr,
441 &sensor_dev_attr_power4_input.dev_attr.attr,
443 NULL,
446 static struct attribute *ltc4245_gpio_attributes[] = {
447 &sensor_dev_attr_in10_input.dev_attr.attr,
448 &sensor_dev_attr_in11_input.dev_attr.attr,
449 NULL,
452 static const struct attribute_group ltc4245_std_group = {
453 .attrs = ltc4245_std_attributes,
456 static const struct attribute_group ltc4245_gpio_group = {
457 .attrs = ltc4245_gpio_attributes,
460 static void ltc4245_sysfs_add_groups(struct ltc4245_data *data)
462 /* standard sysfs attributes */
463 data->groups[0] = &ltc4245_std_group;
465 /* if we're using the extra gpio support, register it's attributes */
466 if (data->use_extra_gpios)
467 data->groups[1] = &ltc4245_gpio_group;
470 static bool ltc4245_use_extra_gpios(struct i2c_client *client)
472 struct ltc4245_platform_data *pdata = dev_get_platdata(&client->dev);
473 #ifdef CONFIG_OF
474 struct device_node *np = client->dev.of_node;
475 #endif
477 /* prefer platform data */
478 if (pdata)
479 return pdata->use_extra_gpios;
481 #ifdef CONFIG_OF
482 /* fallback on OF */
483 if (of_find_property(np, "ltc4245,use-extra-gpios", NULL))
484 return true;
485 #endif
487 return false;
490 static int ltc4245_probe(struct i2c_client *client,
491 const struct i2c_device_id *id)
493 struct i2c_adapter *adapter = client->adapter;
494 struct ltc4245_data *data;
495 struct device *hwmon_dev;
497 if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE_DATA))
498 return -ENODEV;
500 data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
501 if (!data)
502 return -ENOMEM;
504 data->client = client;
505 mutex_init(&data->update_lock);
506 data->use_extra_gpios = ltc4245_use_extra_gpios(client);
508 /* Initialize the LTC4245 chip */
509 i2c_smbus_write_byte_data(client, LTC4245_FAULT1, 0x00);
510 i2c_smbus_write_byte_data(client, LTC4245_FAULT2, 0x00);
512 /* Add sysfs hooks */
513 ltc4245_sysfs_add_groups(data);
515 hwmon_dev = hwmon_device_register_with_groups(&client->dev,
516 client->name, data,
517 data->groups);
518 if (IS_ERR(hwmon_dev))
519 return PTR_ERR(hwmon_dev);
521 i2c_set_clientdata(client, hwmon_dev);
523 return 0;
526 static int ltc4245_remove(struct i2c_client *client)
528 struct device *hwmon_dev = i2c_get_clientdata(client);
530 hwmon_device_unregister(hwmon_dev);
532 return 0;
535 static const struct i2c_device_id ltc4245_id[] = {
536 { "ltc4245", 0 },
539 MODULE_DEVICE_TABLE(i2c, ltc4245_id);
541 /* This is the driver that will be inserted */
542 static struct i2c_driver ltc4245_driver = {
543 .driver = {
544 .name = "ltc4245",
546 .probe = ltc4245_probe,
547 .remove = ltc4245_remove,
548 .id_table = ltc4245_id,
551 module_i2c_driver(ltc4245_driver);
553 MODULE_AUTHOR("Ira W. Snyder <iws@ovro.caltech.edu>");
554 MODULE_DESCRIPTION("LTC4245 driver");
555 MODULE_LICENSE("GPL");