aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/power/wm8350_power.c
diff options
context:
space:
mode:
authorMark Brown <broonie@opensource.wolfsonmicro.com>2008-11-16 14:16:47 -0500
committerSamuel Ortiz <samuel@sortiz.org>2009-01-04 06:17:38 -0500
commit14431aa0c5a443d13d24e6f865a8838f97dab973 (patch)
treeb0494cf2787cf039f7541439ef9b1f6ee5c3b294 /drivers/power/wm8350_power.c
parent3fba19ec1ae5b460c73a7f32efed8d3b3300b246 (diff)
power_supply: Add support for WM8350 PMU
This patch adds support for the PMU provided by the WM8350 which implements battery, line and USB supplies including a battery charger. The hardware functions largely autonomously, with minimal software control required to initiate fast charging. Support for configuration of the USB supply is not yet implemented. This means that the hardware will remain in the mode configured at startup, by default limiting the current drawn from USB to 100mA. This driver was originally written by Liam Girdwood with subsequent updates for submission by Mark Brown. Signed-off-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Acked-by: Anton Vorontsov <cbouatmailru@gmail.com> Signed-off-by: Samuel Ortiz <sameo@openedhand.com>
Diffstat (limited to 'drivers/power/wm8350_power.c')
-rw-r--r--drivers/power/wm8350_power.c515
1 files changed, 515 insertions, 0 deletions
diff --git a/drivers/power/wm8350_power.c b/drivers/power/wm8350_power.c
new file mode 100644
index 000000000000..9c0a847a1139
--- /dev/null
+++ b/drivers/power/wm8350_power.c
@@ -0,0 +1,515 @@
1/*
2 * Battery driver for wm8350 PMIC
3 *
4 * Copyright 2007, 2008 Wolfson Microelectronics PLC.
5 *
6 * Based on OLPC Battery Driver
7 *
8 * Copyright 2006 David Woodhouse <dwmw2@infradead.org>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/err.h>
17#include <linux/platform_device.h>
18#include <linux/power_supply.h>
19#include <linux/mfd/wm8350/supply.h>
20#include <linux/mfd/wm8350/core.h>
21#include <linux/mfd/wm8350/comparator.h>
22
23static int wm8350_read_battery_uvolts(struct wm8350 *wm8350)
24{
25 return wm8350_read_auxadc(wm8350, WM8350_AUXADC_BATT, 0, 0)
26 * WM8350_AUX_COEFF;
27}
28
29static int wm8350_read_line_uvolts(struct wm8350 *wm8350)
30{
31 return wm8350_read_auxadc(wm8350, WM8350_AUXADC_LINE, 0, 0)
32 * WM8350_AUX_COEFF;
33}
34
35static int wm8350_read_usb_uvolts(struct wm8350 *wm8350)
36{
37 return wm8350_read_auxadc(wm8350, WM8350_AUXADC_USB, 0, 0)
38 * WM8350_AUX_COEFF;
39}
40
41#define WM8350_BATT_SUPPLY 1
42#define WM8350_USB_SUPPLY 2
43#define WM8350_LINE_SUPPLY 4
44
45static inline int wm8350_charge_time_min(struct wm8350 *wm8350, int min)
46{
47 if (wm8350->rev < WM8350_REV_G)
48 return (((min - 30) / 15) & 0xf) << 8;
49 else
50 return (((min - 30) / 30) & 0xf) << 8;
51}
52
53static int wm8350_get_supplies(struct wm8350 *wm8350)
54{
55 u16 sm, ov, co, chrg;
56 int supplies = 0;
57
58 sm = wm8350_reg_read(wm8350, WM8350_STATE_MACHINE_STATUS);
59 ov = wm8350_reg_read(wm8350, WM8350_MISC_OVERRIDES);
60 co = wm8350_reg_read(wm8350, WM8350_COMPARATOR_OVERRIDES);
61 chrg = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2);
62
63 /* USB_SM */
64 sm = (sm & WM8350_USB_SM_MASK) >> WM8350_USB_SM_SHIFT;
65
66 /* CHG_ISEL */
67 chrg &= WM8350_CHG_ISEL_MASK;
68
69 /* If the USB state machine is active then we're using that with or
70 * without battery, otherwise check for wall supply */
71 if (((sm == WM8350_USB_SM_100_SLV) ||
72 (sm == WM8350_USB_SM_500_SLV) ||
73 (sm == WM8350_USB_SM_STDBY_SLV))
74 && !(ov & WM8350_USB_LIMIT_OVRDE))
75 supplies = WM8350_USB_SUPPLY;
76 else if (((sm == WM8350_USB_SM_100_SLV) ||
77 (sm == WM8350_USB_SM_500_SLV) ||
78 (sm == WM8350_USB_SM_STDBY_SLV))
79 && (ov & WM8350_USB_LIMIT_OVRDE) && (chrg == 0))
80 supplies = WM8350_USB_SUPPLY | WM8350_BATT_SUPPLY;
81 else if (co & WM8350_WALL_FB_OVRDE)
82 supplies = WM8350_LINE_SUPPLY;
83 else
84 supplies = WM8350_BATT_SUPPLY;
85
86 return supplies;
87}
88
89static int wm8350_charger_config(struct wm8350 *wm8350,
90 struct wm8350_charger_policy *policy)
91{
92 u16 reg, eoc_mA, fast_limit_mA;
93
94 if (!policy) {
95 dev_warn(wm8350->dev,
96 "No charger policy, charger not configured.\n");
97 return -EINVAL;
98 }
99
100 /* make sure USB fast charge current is not > 500mA */
101 if (policy->fast_limit_USB_mA > 500) {
102 dev_err(wm8350->dev, "USB fast charge > 500mA\n");
103 return -EINVAL;
104 }
105
106 eoc_mA = WM8350_CHG_EOC_mA(policy->eoc_mA);
107
108 wm8350_reg_unlock(wm8350);
109
110 reg = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1)
111 & WM8350_CHG_ENA_R168;
112 wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1,
113 reg | eoc_mA | policy->trickle_start_mV |
114 WM8350_CHG_TRICKLE_TEMP_CHOKE |
115 WM8350_CHG_TRICKLE_USB_CHOKE |
116 WM8350_CHG_FAST_USB_THROTTLE);
117
118 if (wm8350_get_supplies(wm8350) & WM8350_USB_SUPPLY) {
119 fast_limit_mA =
120 WM8350_CHG_FAST_LIMIT_mA(policy->fast_limit_USB_mA);
121 wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2,
122 policy->charge_mV | policy->trickle_charge_USB_mA |
123 fast_limit_mA | wm8350_charge_time_min(wm8350,
124 policy->charge_timeout));
125
126 } else {
127 fast_limit_mA =
128 WM8350_CHG_FAST_LIMIT_mA(policy->fast_limit_mA);
129 wm8350_reg_write(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2,
130 policy->charge_mV | policy->trickle_charge_mA |
131 fast_limit_mA | wm8350_charge_time_min(wm8350,
132 policy->charge_timeout));
133 }
134
135 wm8350_reg_lock(wm8350);
136 return 0;
137}
138
139static int wm8350_batt_status(struct wm8350 *wm8350)
140{
141 u16 state;
142
143 state = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2);
144 state &= WM8350_CHG_STS_MASK;
145
146 switch (state) {
147 case WM8350_CHG_STS_OFF:
148 return POWER_SUPPLY_STATUS_DISCHARGING;
149
150 case WM8350_CHG_STS_TRICKLE:
151 case WM8350_CHG_STS_FAST:
152 return POWER_SUPPLY_STATUS_CHARGING;
153
154 default:
155 return POWER_SUPPLY_STATUS_UNKNOWN;
156 }
157}
158
159static ssize_t charger_state_show(struct device *dev,
160 struct device_attribute *attr, char *buf)
161{
162 struct wm8350 *wm8350 = dev_get_drvdata(dev);
163 char *charge;
164 int state;
165
166 state = wm8350_reg_read(wm8350, WM8350_BATTERY_CHARGER_CONTROL_2) &
167 WM8350_CHG_STS_MASK;
168 switch (state) {
169 case WM8350_CHG_STS_OFF:
170 charge = "Charger Off";
171 break;
172 case WM8350_CHG_STS_TRICKLE:
173 charge = "Trickle Charging";
174 break;
175 case WM8350_CHG_STS_FAST:
176 charge = "Fast Charging";
177 break;
178 default:
179 return 0;
180 }
181
182 return sprintf(buf, "%s\n", charge);
183}
184
185static DEVICE_ATTR(charger_state, 0444, charger_state_show, NULL);
186
187static void wm8350_charger_handler(struct wm8350 *wm8350, int irq, void *data)
188{
189 struct wm8350_power *power = &wm8350->power;
190 struct wm8350_charger_policy *policy = power->policy;
191
192 switch (irq) {
193 case WM8350_IRQ_CHG_BAT_HOT:
194 dev_err(wm8350->dev, "battery too hot\n");
195 break;
196 case WM8350_IRQ_CHG_BAT_COLD:
197 dev_err(wm8350->dev, "battery too cold\n");
198 break;
199 case WM8350_IRQ_CHG_BAT_FAIL:
200 dev_err(wm8350->dev, "battery failed\n");
201 break;
202 case WM8350_IRQ_CHG_TO:
203 dev_err(wm8350->dev, "charger timeout\n");
204 break;
205 case WM8350_IRQ_CHG_END:
206 power_supply_changed(&power->battery);
207 break;
208 case WM8350_IRQ_CHG_START:
209 power_supply_changed(&power->battery);
210 break;
211
212 case WM8350_IRQ_CHG_FAST_RDY:
213 dev_dbg(wm8350->dev, "fast charger ready\n");
214 wm8350_charger_config(wm8350, policy);
215 wm8350_reg_unlock(wm8350);
216 wm8350_set_bits(wm8350, WM8350_BATTERY_CHARGER_CONTROL_1,
217 WM8350_CHG_FAST);
218 wm8350_reg_lock(wm8350);
219 break;
220
221 case WM8350_IRQ_CHG_VBATT_LT_3P9:
222 dev_warn(wm8350->dev, "battery < 3.9V\n");
223 break;
224 case WM8350_IRQ_CHG_VBATT_LT_3P1:
225 dev_warn(wm8350->dev, "battery < 3.1V\n");
226 break;
227 case WM8350_IRQ_CHG_VBATT_LT_2P85:
228 dev_warn(wm8350->dev, "battery < 2.85V\n");
229 break;
230
231 /* Supply change. We will overnotify but it should do
232 * no harm. */
233 case WM8350_IRQ_EXT_USB_FB:
234 case WM8350_IRQ_EXT_WALL_FB:
235 wm8350_charger_config(wm8350, policy);
236 case WM8350_IRQ_EXT_BAT_FB: /* Fall through */
237 power_supply_changed(&power->battery);
238 power_supply_changed(&power->usb);
239 power_supply_changed(&power->ac);
240 break;
241
242 default:
243 dev_err(wm8350->dev, "Unknown interrupt %d\n", irq);
244 }
245}
246
247/*********************************************************************
248 * AC Power
249 *********************************************************************/
250static int wm8350_ac_get_prop(struct power_supply *psy,
251 enum power_supply_property psp,
252 union power_supply_propval *val)
253{
254 struct wm8350 *wm8350 = dev_get_drvdata(psy->dev->parent);
255 int ret = 0;
256
257 switch (psp) {
258 case POWER_SUPPLY_PROP_ONLINE:
259 val->intval = !!(wm8350_get_supplies(wm8350) &
260 WM8350_LINE_SUPPLY);
261 break;
262 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
263 val->intval = wm8350_read_line_uvolts(wm8350);
264 break;
265 default:
266 ret = -EINVAL;
267 break;
268 }
269 return ret;
270}
271
272static enum power_supply_property wm8350_ac_props[] = {
273 POWER_SUPPLY_PROP_ONLINE,
274 POWER_SUPPLY_PROP_VOLTAGE_NOW,
275};
276
277/*********************************************************************
278 * USB Power
279 *********************************************************************/
280static int wm8350_usb_get_prop(struct power_supply *psy,
281 enum power_supply_property psp,
282 union power_supply_propval *val)
283{
284 struct wm8350 *wm8350 = dev_get_drvdata(psy->dev->parent);
285 int ret = 0;
286
287 switch (psp) {
288 case POWER_SUPPLY_PROP_ONLINE:
289 val->intval = !!(wm8350_get_supplies(wm8350) &
290 WM8350_USB_SUPPLY);
291 break;
292 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
293 val->intval = wm8350_read_usb_uvolts(wm8350);
294 break;
295 default:
296 ret = -EINVAL;
297 break;
298 }
299 return ret;
300}
301
302static enum power_supply_property wm8350_usb_props[] = {
303 POWER_SUPPLY_PROP_ONLINE,
304 POWER_SUPPLY_PROP_VOLTAGE_NOW,
305};
306
307/*********************************************************************
308 * Battery properties
309 *********************************************************************/
310
311static int wm8350_bat_get_property(struct power_supply *psy,
312 enum power_supply_property psp,
313 union power_supply_propval *val)
314{
315 struct wm8350 *wm8350 = dev_get_drvdata(psy->dev->parent);
316 int ret = 0;
317
318 switch (psp) {
319 case POWER_SUPPLY_PROP_STATUS:
320 val->intval = wm8350_batt_status(wm8350);
321 break;
322 case POWER_SUPPLY_PROP_ONLINE:
323 val->intval = !!(wm8350_get_supplies(wm8350) &
324 WM8350_BATT_SUPPLY);
325 break;
326 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
327 val->intval = wm8350_read_battery_uvolts(wm8350);
328 break;
329 default:
330 ret = -EINVAL;
331 break;
332 }
333
334 return ret;
335}
336
337static enum power_supply_property wm8350_bat_props[] = {
338 POWER_SUPPLY_PROP_STATUS,
339 POWER_SUPPLY_PROP_ONLINE,
340 POWER_SUPPLY_PROP_VOLTAGE_NOW,
341};
342
343/*********************************************************************
344 * Initialisation
345 *********************************************************************/
346
347static void wm8350_init_charger(struct wm8350 *wm8350)
348{
349 /* register our interest in charger events */
350 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_HOT,
351 wm8350_charger_handler, NULL);
352 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_BAT_HOT);
353 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_COLD,
354 wm8350_charger_handler, NULL);
355 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_BAT_COLD);
356 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_BAT_FAIL,
357 wm8350_charger_handler, NULL);
358 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_BAT_FAIL);
359 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_TO,
360 wm8350_charger_handler, NULL);
361 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_TO);
362 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_END,
363 wm8350_charger_handler, NULL);
364 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_END);
365 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_START,
366 wm8350_charger_handler, NULL);
367 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_START);
368 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_FAST_RDY,
369 wm8350_charger_handler, NULL);
370 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_FAST_RDY);
371 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P9,
372 wm8350_charger_handler, NULL);
373 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P9);
374 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P1,
375 wm8350_charger_handler, NULL);
376 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P1);
377 wm8350_register_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_2P85,
378 wm8350_charger_handler, NULL);
379 wm8350_unmask_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_2P85);
380
381 /* and supply change events */
382 wm8350_register_irq(wm8350, WM8350_IRQ_EXT_USB_FB,
383 wm8350_charger_handler, NULL);
384 wm8350_unmask_irq(wm8350, WM8350_IRQ_EXT_USB_FB);
385 wm8350_register_irq(wm8350, WM8350_IRQ_EXT_WALL_FB,
386 wm8350_charger_handler, NULL);
387 wm8350_unmask_irq(wm8350, WM8350_IRQ_EXT_WALL_FB);
388 wm8350_register_irq(wm8350, WM8350_IRQ_EXT_BAT_FB,
389 wm8350_charger_handler, NULL);
390 wm8350_unmask_irq(wm8350, WM8350_IRQ_EXT_BAT_FB);
391}
392
393static void free_charger_irq(struct wm8350 *wm8350)
394{
395 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_BAT_HOT);
396 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_HOT);
397 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_BAT_COLD);
398 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_COLD);
399 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_BAT_FAIL);
400 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_BAT_FAIL);
401 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_TO);
402 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_TO);
403 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_END);
404 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_END);
405 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_START);
406 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_START);
407 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P9);
408 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P9);
409 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P1);
410 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_3P1);
411 wm8350_mask_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_2P85);
412 wm8350_free_irq(wm8350, WM8350_IRQ_CHG_VBATT_LT_2P85);
413 wm8350_mask_irq(wm8350, WM8350_IRQ_EXT_USB_FB);
414 wm8350_free_irq(wm8350, WM8350_IRQ_EXT_USB_FB);
415 wm8350_mask_irq(wm8350, WM8350_IRQ_EXT_WALL_FB);
416 wm8350_free_irq(wm8350, WM8350_IRQ_EXT_WALL_FB);
417 wm8350_mask_irq(wm8350, WM8350_IRQ_EXT_BAT_FB);
418 wm8350_free_irq(wm8350, WM8350_IRQ_EXT_BAT_FB);
419}
420
421static __devinit int wm8350_power_probe(struct platform_device *pdev)
422{
423 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
424 struct wm8350_power *power = &wm8350->power;
425 struct wm8350_charger_policy *policy = power->policy;
426 struct power_supply *usb = &power->usb;
427 struct power_supply *battery = &power->battery;
428 struct power_supply *ac = &power->ac;
429 int ret;
430
431 ac->name = "wm8350-ac";
432 ac->type = POWER_SUPPLY_TYPE_MAINS;
433 ac->properties = wm8350_ac_props;
434 ac->num_properties = ARRAY_SIZE(wm8350_ac_props);
435 ac->get_property = wm8350_ac_get_prop;
436 ret = power_supply_register(&pdev->dev, ac);
437 if (ret)
438 return ret;
439
440 battery->name = "wm8350-battery";
441 battery->properties = wm8350_bat_props;
442 battery->num_properties = ARRAY_SIZE(wm8350_bat_props);
443 battery->get_property = wm8350_bat_get_property;
444 battery->use_for_apm = 1;
445 ret = power_supply_register(&pdev->dev, battery);
446 if (ret)
447 goto battery_failed;
448
449 usb->name = "wm8350-usb",
450 usb->type = POWER_SUPPLY_TYPE_USB;
451 usb->properties = wm8350_usb_props;
452 usb->num_properties = ARRAY_SIZE(wm8350_usb_props);
453 usb->get_property = wm8350_usb_get_prop;
454 ret = power_supply_register(&pdev->dev, usb);
455 if (ret)
456 goto usb_failed;
457
458 ret = device_create_file(&pdev->dev, &dev_attr_charger_state);
459 if (ret < 0)
460 dev_warn(wm8350->dev, "failed to add charge sysfs: %d\n", ret);
461 ret = 0;
462
463 wm8350_init_charger(wm8350);
464 if (wm8350_charger_config(wm8350, policy) == 0) {
465 wm8350_reg_unlock(wm8350);
466 wm8350_set_bits(wm8350, WM8350_POWER_MGMT_5, WM8350_CHG_ENA);
467 wm8350_reg_lock(wm8350);
468 }
469
470 return ret;
471
472usb_failed:
473 power_supply_unregister(battery);
474battery_failed:
475 power_supply_unregister(ac);
476
477 return ret;
478}
479
480static __devexit int wm8350_power_remove(struct platform_device *pdev)
481{
482 struct wm8350 *wm8350 = platform_get_drvdata(pdev);
483 struct wm8350_power *power = &wm8350->power;
484
485 free_charger_irq(wm8350);
486 device_remove_file(&pdev->dev, &dev_attr_charger_state);
487 power_supply_unregister(&power->battery);
488 power_supply_unregister(&power->ac);
489 power_supply_unregister(&power->usb);
490 return 0;
491}
492
493static struct platform_driver wm8350_power_driver = {
494 .probe = wm8350_power_probe,
495 .remove = __devexit_p(wm8350_power_remove),
496 .driver = {
497 .name = "wm8350-power",
498 },
499};
500
501static int __init wm8350_power_init(void)
502{
503 return platform_driver_register(&wm8350_power_driver);
504}
505module_init(wm8350_power_init);
506
507static void __exit wm8350_power_exit(void)
508{
509 platform_driver_unregister(&wm8350_power_driver);
510}
511module_exit(wm8350_power_exit);
512
513MODULE_LICENSE("GPL");
514MODULE_DESCRIPTION("Power supply driver for WM8350");
515MODULE_ALIAS("platform:wm8350-power");