USB: add missing "{}" in map_urb_for_dma
[linux/fpc-iii.git] / drivers / power / wm831x_power.c
blob875c4d0f776b90570058eef2e41328488f3f0b51
1 /*
2 * PMU driver for Wolfson Microelectronics wm831x PMICs
4 * Copyright 2009 Wolfson Microelectronics PLC.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
11 #include <linux/module.h>
12 #include <linux/err.h>
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
15 #include <linux/slab.h>
17 #include <linux/mfd/wm831x/core.h>
18 #include <linux/mfd/wm831x/auxadc.h>
19 #include <linux/mfd/wm831x/pmu.h>
20 #include <linux/mfd/wm831x/pdata.h>
22 struct wm831x_power {
23 struct wm831x *wm831x;
24 struct power_supply wall;
25 struct power_supply usb;
26 struct power_supply battery;
29 static int wm831x_power_check_online(struct wm831x *wm831x, int supply,
30 union power_supply_propval *val)
32 int ret;
34 ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
35 if (ret < 0)
36 return ret;
38 if (ret & supply)
39 val->intval = 1;
40 else
41 val->intval = 0;
43 return 0;
46 static int wm831x_power_read_voltage(struct wm831x *wm831x,
47 enum wm831x_auxadc src,
48 union power_supply_propval *val)
50 int ret;
52 ret = wm831x_auxadc_read_uv(wm831x, src);
53 if (ret >= 0)
54 val->intval = ret;
56 return ret;
59 /*********************************************************************
60 * WALL Power
61 *********************************************************************/
62 static int wm831x_wall_get_prop(struct power_supply *psy,
63 enum power_supply_property psp,
64 union power_supply_propval *val)
66 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent);
67 struct wm831x *wm831x = wm831x_power->wm831x;
68 int ret = 0;
70 switch (psp) {
71 case POWER_SUPPLY_PROP_ONLINE:
72 ret = wm831x_power_check_online(wm831x, WM831X_PWR_WALL, val);
73 break;
74 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
75 ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_WALL, val);
76 break;
77 default:
78 ret = -EINVAL;
79 break;
82 return ret;
85 static enum power_supply_property wm831x_wall_props[] = {
86 POWER_SUPPLY_PROP_ONLINE,
87 POWER_SUPPLY_PROP_VOLTAGE_NOW,
90 /*********************************************************************
91 * USB Power
92 *********************************************************************/
93 static int wm831x_usb_get_prop(struct power_supply *psy,
94 enum power_supply_property psp,
95 union power_supply_propval *val)
97 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent);
98 struct wm831x *wm831x = wm831x_power->wm831x;
99 int ret = 0;
101 switch (psp) {
102 case POWER_SUPPLY_PROP_ONLINE:
103 ret = wm831x_power_check_online(wm831x, WM831X_PWR_USB, val);
104 break;
105 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
106 ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_USB, val);
107 break;
108 default:
109 ret = -EINVAL;
110 break;
113 return ret;
116 static enum power_supply_property wm831x_usb_props[] = {
117 POWER_SUPPLY_PROP_ONLINE,
118 POWER_SUPPLY_PROP_VOLTAGE_NOW,
121 /*********************************************************************
122 * Battery properties
123 *********************************************************************/
125 struct chg_map {
126 int val;
127 int reg_val;
130 static struct chg_map trickle_ilims[] = {
131 { 50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT },
132 { 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT },
133 { 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT },
134 { 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT },
137 static struct chg_map vsels[] = {
138 { 4050, 0 << WM831X_CHG_VSEL_SHIFT },
139 { 4100, 1 << WM831X_CHG_VSEL_SHIFT },
140 { 4150, 2 << WM831X_CHG_VSEL_SHIFT },
141 { 4200, 3 << WM831X_CHG_VSEL_SHIFT },
144 static struct chg_map fast_ilims[] = {
145 { 0, 0 << WM831X_CHG_FAST_ILIM_SHIFT },
146 { 50, 1 << WM831X_CHG_FAST_ILIM_SHIFT },
147 { 100, 2 << WM831X_CHG_FAST_ILIM_SHIFT },
148 { 150, 3 << WM831X_CHG_FAST_ILIM_SHIFT },
149 { 200, 4 << WM831X_CHG_FAST_ILIM_SHIFT },
150 { 250, 5 << WM831X_CHG_FAST_ILIM_SHIFT },
151 { 300, 6 << WM831X_CHG_FAST_ILIM_SHIFT },
152 { 350, 7 << WM831X_CHG_FAST_ILIM_SHIFT },
153 { 400, 8 << WM831X_CHG_FAST_ILIM_SHIFT },
154 { 450, 9 << WM831X_CHG_FAST_ILIM_SHIFT },
155 { 500, 10 << WM831X_CHG_FAST_ILIM_SHIFT },
156 { 600, 11 << WM831X_CHG_FAST_ILIM_SHIFT },
157 { 700, 12 << WM831X_CHG_FAST_ILIM_SHIFT },
158 { 800, 13 << WM831X_CHG_FAST_ILIM_SHIFT },
159 { 900, 14 << WM831X_CHG_FAST_ILIM_SHIFT },
160 { 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT },
163 static struct chg_map eoc_iterms[] = {
164 { 20, 0 << WM831X_CHG_ITERM_SHIFT },
165 { 30, 1 << WM831X_CHG_ITERM_SHIFT },
166 { 40, 2 << WM831X_CHG_ITERM_SHIFT },
167 { 50, 3 << WM831X_CHG_ITERM_SHIFT },
168 { 60, 4 << WM831X_CHG_ITERM_SHIFT },
169 { 70, 5 << WM831X_CHG_ITERM_SHIFT },
170 { 80, 6 << WM831X_CHG_ITERM_SHIFT },
171 { 90, 7 << WM831X_CHG_ITERM_SHIFT },
174 static struct chg_map chg_times[] = {
175 { 60, 0 << WM831X_CHG_TIME_SHIFT },
176 { 90, 1 << WM831X_CHG_TIME_SHIFT },
177 { 120, 2 << WM831X_CHG_TIME_SHIFT },
178 { 150, 3 << WM831X_CHG_TIME_SHIFT },
179 { 180, 4 << WM831X_CHG_TIME_SHIFT },
180 { 210, 5 << WM831X_CHG_TIME_SHIFT },
181 { 240, 6 << WM831X_CHG_TIME_SHIFT },
182 { 270, 7 << WM831X_CHG_TIME_SHIFT },
183 { 300, 8 << WM831X_CHG_TIME_SHIFT },
184 { 330, 9 << WM831X_CHG_TIME_SHIFT },
185 { 360, 10 << WM831X_CHG_TIME_SHIFT },
186 { 390, 11 << WM831X_CHG_TIME_SHIFT },
187 { 420, 12 << WM831X_CHG_TIME_SHIFT },
188 { 450, 13 << WM831X_CHG_TIME_SHIFT },
189 { 480, 14 << WM831X_CHG_TIME_SHIFT },
190 { 510, 15 << WM831X_CHG_TIME_SHIFT },
193 static void wm831x_battey_apply_config(struct wm831x *wm831x,
194 struct chg_map *map, int count, int val,
195 int *reg, const char *name,
196 const char *units)
198 int i;
200 for (i = 0; i < count; i++)
201 if (val == map[i].val)
202 break;
203 if (i == count) {
204 dev_err(wm831x->dev, "Invalid %s %d%s\n",
205 name, val, units);
206 } else {
207 *reg |= map[i].reg_val;
208 dev_dbg(wm831x->dev, "Set %s of %d%s\n", name, val, units);
212 static void wm831x_config_battery(struct wm831x *wm831x)
214 struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
215 struct wm831x_battery_pdata *pdata;
216 int ret, reg1, reg2;
218 if (!wm831x_pdata || !wm831x_pdata->battery) {
219 dev_warn(wm831x->dev,
220 "No battery charger configuration\n");
221 return;
224 pdata = wm831x_pdata->battery;
226 reg1 = 0;
227 reg2 = 0;
229 if (!pdata->enable) {
230 dev_info(wm831x->dev, "Battery charger disabled\n");
231 return;
234 reg1 |= WM831X_CHG_ENA;
235 if (pdata->off_mask)
236 reg2 |= WM831X_CHG_OFF_MSK;
237 if (pdata->fast_enable)
238 reg1 |= WM831X_CHG_FAST;
240 wm831x_battey_apply_config(wm831x, trickle_ilims,
241 ARRAY_SIZE(trickle_ilims),
242 pdata->trickle_ilim, &reg2,
243 "trickle charge current limit", "mA");
245 wm831x_battey_apply_config(wm831x, vsels, ARRAY_SIZE(vsels),
246 pdata->vsel, &reg2,
247 "target voltage", "mV");
249 wm831x_battey_apply_config(wm831x, fast_ilims, ARRAY_SIZE(fast_ilims),
250 pdata->fast_ilim, &reg2,
251 "fast charge current limit", "mA");
253 wm831x_battey_apply_config(wm831x, eoc_iterms, ARRAY_SIZE(eoc_iterms),
254 pdata->eoc_iterm, &reg1,
255 "end of charge current threshold", "mA");
257 wm831x_battey_apply_config(wm831x, chg_times, ARRAY_SIZE(chg_times),
258 pdata->timeout, &reg2,
259 "charger timeout", "min");
261 ret = wm831x_reg_unlock(wm831x);
262 if (ret != 0) {
263 dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret);
264 return;
267 ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_1,
268 WM831X_CHG_ENA_MASK |
269 WM831X_CHG_FAST_MASK |
270 WM831X_CHG_ITERM_MASK |
271 WM831X_CHG_ITERM_MASK,
272 reg1);
273 if (ret != 0)
274 dev_err(wm831x->dev, "Failed to set charger control 1: %d\n",
275 ret);
277 ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_2,
278 WM831X_CHG_OFF_MSK |
279 WM831X_CHG_TIME_MASK |
280 WM831X_CHG_FAST_ILIM_MASK |
281 WM831X_CHG_TRKL_ILIM_MASK |
282 WM831X_CHG_VSEL_MASK,
283 reg2);
284 if (ret != 0)
285 dev_err(wm831x->dev, "Failed to set charger control 2: %d\n",
286 ret);
288 wm831x_reg_lock(wm831x);
291 static int wm831x_bat_check_status(struct wm831x *wm831x, int *status)
293 int ret;
295 ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
296 if (ret < 0)
297 return ret;
299 if (ret & WM831X_PWR_SRC_BATT) {
300 *status = POWER_SUPPLY_STATUS_DISCHARGING;
301 return 0;
304 ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
305 if (ret < 0)
306 return ret;
308 switch (ret & WM831X_CHG_STATE_MASK) {
309 case WM831X_CHG_STATE_OFF:
310 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
311 break;
312 case WM831X_CHG_STATE_TRICKLE:
313 case WM831X_CHG_STATE_FAST:
314 *status = POWER_SUPPLY_STATUS_CHARGING;
315 break;
317 default:
318 *status = POWER_SUPPLY_STATUS_UNKNOWN;
319 break;
322 return 0;
325 static int wm831x_bat_check_type(struct wm831x *wm831x, int *type)
327 int ret;
329 ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
330 if (ret < 0)
331 return ret;
333 switch (ret & WM831X_CHG_STATE_MASK) {
334 case WM831X_CHG_STATE_TRICKLE:
335 case WM831X_CHG_STATE_TRICKLE_OT:
336 *type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
337 break;
338 case WM831X_CHG_STATE_FAST:
339 case WM831X_CHG_STATE_FAST_OT:
340 *type = POWER_SUPPLY_CHARGE_TYPE_FAST;
341 break;
342 default:
343 *type = POWER_SUPPLY_CHARGE_TYPE_NONE;
344 break;
347 return 0;
350 static int wm831x_bat_check_health(struct wm831x *wm831x, int *health)
352 int ret;
354 ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
355 if (ret < 0)
356 return ret;
358 if (ret & WM831X_BATT_HOT_STS) {
359 *health = POWER_SUPPLY_HEALTH_OVERHEAT;
360 return 0;
363 if (ret & WM831X_BATT_COLD_STS) {
364 *health = POWER_SUPPLY_HEALTH_COLD;
365 return 0;
368 if (ret & WM831X_BATT_OV_STS) {
369 *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
370 return 0;
373 switch (ret & WM831X_CHG_STATE_MASK) {
374 case WM831X_CHG_STATE_TRICKLE_OT:
375 case WM831X_CHG_STATE_FAST_OT:
376 *health = POWER_SUPPLY_HEALTH_OVERHEAT;
377 break;
378 case WM831X_CHG_STATE_DEFECTIVE:
379 *health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
380 break;
381 default:
382 *health = POWER_SUPPLY_HEALTH_GOOD;
383 break;
386 return 0;
389 static int wm831x_bat_get_prop(struct power_supply *psy,
390 enum power_supply_property psp,
391 union power_supply_propval *val)
393 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent);
394 struct wm831x *wm831x = wm831x_power->wm831x;
395 int ret = 0;
397 switch (psp) {
398 case POWER_SUPPLY_PROP_STATUS:
399 ret = wm831x_bat_check_status(wm831x, &val->intval);
400 break;
401 case POWER_SUPPLY_PROP_ONLINE:
402 ret = wm831x_power_check_online(wm831x, WM831X_PWR_SRC_BATT,
403 val);
404 break;
405 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
406 ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_BATT, val);
407 break;
408 case POWER_SUPPLY_PROP_HEALTH:
409 ret = wm831x_bat_check_health(wm831x, &val->intval);
410 break;
411 case POWER_SUPPLY_PROP_CHARGE_TYPE:
412 ret = wm831x_bat_check_type(wm831x, &val->intval);
413 break;
414 default:
415 ret = -EINVAL;
416 break;
419 return ret;
422 static enum power_supply_property wm831x_bat_props[] = {
423 POWER_SUPPLY_PROP_STATUS,
424 POWER_SUPPLY_PROP_ONLINE,
425 POWER_SUPPLY_PROP_VOLTAGE_NOW,
426 POWER_SUPPLY_PROP_HEALTH,
427 POWER_SUPPLY_PROP_CHARGE_TYPE,
430 static const char *wm831x_bat_irqs[] = {
431 "BATT HOT",
432 "BATT COLD",
433 "BATT FAIL",
434 "OV",
435 "END",
436 "TO",
437 "MODE",
438 "START",
441 static irqreturn_t wm831x_bat_irq(int irq, void *data)
443 struct wm831x_power *wm831x_power = data;
444 struct wm831x *wm831x = wm831x_power->wm831x;
446 dev_dbg(wm831x->dev, "Battery status changed: %d\n", irq);
448 /* The battery charger is autonomous so we don't need to do
449 * anything except kick user space */
450 power_supply_changed(&wm831x_power->battery);
452 return IRQ_HANDLED;
456 /*********************************************************************
457 * Initialisation
458 *********************************************************************/
460 static irqreturn_t wm831x_syslo_irq(int irq, void *data)
462 struct wm831x_power *wm831x_power = data;
463 struct wm831x *wm831x = wm831x_power->wm831x;
465 /* Not much we can actually *do* but tell people for
466 * posterity, we're probably about to run out of power. */
467 dev_crit(wm831x->dev, "SYSVDD under voltage\n");
469 return IRQ_HANDLED;
472 static irqreturn_t wm831x_pwr_src_irq(int irq, void *data)
474 struct wm831x_power *wm831x_power = data;
475 struct wm831x *wm831x = wm831x_power->wm831x;
477 dev_dbg(wm831x->dev, "Power source changed\n");
479 /* Just notify for everything - little harm in overnotifying. */
480 power_supply_changed(&wm831x_power->battery);
481 power_supply_changed(&wm831x_power->usb);
482 power_supply_changed(&wm831x_power->wall);
484 return IRQ_HANDLED;
487 static __devinit int wm831x_power_probe(struct platform_device *pdev)
489 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
490 struct wm831x_power *power;
491 struct power_supply *usb;
492 struct power_supply *battery;
493 struct power_supply *wall;
494 int ret, irq, i;
496 power = kzalloc(sizeof(struct wm831x_power), GFP_KERNEL);
497 if (power == NULL)
498 return -ENOMEM;
500 power->wm831x = wm831x;
501 platform_set_drvdata(pdev, power);
503 usb = &power->usb;
504 battery = &power->battery;
505 wall = &power->wall;
507 /* We ignore configuration failures since we can still read back
508 * the status without enabling the charger.
510 wm831x_config_battery(wm831x);
512 wall->name = "wm831x-wall";
513 wall->type = POWER_SUPPLY_TYPE_MAINS;
514 wall->properties = wm831x_wall_props;
515 wall->num_properties = ARRAY_SIZE(wm831x_wall_props);
516 wall->get_property = wm831x_wall_get_prop;
517 ret = power_supply_register(&pdev->dev, wall);
518 if (ret)
519 goto err_kmalloc;
521 battery->name = "wm831x-battery";
522 battery->properties = wm831x_bat_props;
523 battery->num_properties = ARRAY_SIZE(wm831x_bat_props);
524 battery->get_property = wm831x_bat_get_prop;
525 battery->use_for_apm = 1;
526 ret = power_supply_register(&pdev->dev, battery);
527 if (ret)
528 goto err_wall;
530 usb->name = "wm831x-usb",
531 usb->type = POWER_SUPPLY_TYPE_USB;
532 usb->properties = wm831x_usb_props;
533 usb->num_properties = ARRAY_SIZE(wm831x_usb_props);
534 usb->get_property = wm831x_usb_get_prop;
535 ret = power_supply_register(&pdev->dev, usb);
536 if (ret)
537 goto err_battery;
539 irq = platform_get_irq_byname(pdev, "SYSLO");
540 ret = wm831x_request_irq(wm831x, irq, wm831x_syslo_irq,
541 IRQF_TRIGGER_RISING, "SYSLO",
542 power);
543 if (ret != 0) {
544 dev_err(&pdev->dev, "Failed to request SYSLO IRQ %d: %d\n",
545 irq, ret);
546 goto err_usb;
549 irq = platform_get_irq_byname(pdev, "PWR SRC");
550 ret = wm831x_request_irq(wm831x, irq, wm831x_pwr_src_irq,
551 IRQF_TRIGGER_RISING, "Power source",
552 power);
553 if (ret != 0) {
554 dev_err(&pdev->dev, "Failed to request PWR SRC IRQ %d: %d\n",
555 irq, ret);
556 goto err_syslo;
559 for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
560 irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
561 ret = wm831x_request_irq(wm831x, irq, wm831x_bat_irq,
562 IRQF_TRIGGER_RISING,
563 wm831x_bat_irqs[i],
564 power);
565 if (ret != 0) {
566 dev_err(&pdev->dev,
567 "Failed to request %s IRQ %d: %d\n",
568 wm831x_bat_irqs[i], irq, ret);
569 goto err_bat_irq;
573 return ret;
575 err_bat_irq:
576 for (; i >= 0; i--) {
577 irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
578 wm831x_free_irq(wm831x, irq, power);
580 irq = platform_get_irq_byname(pdev, "PWR SRC");
581 wm831x_free_irq(wm831x, irq, power);
582 err_syslo:
583 irq = platform_get_irq_byname(pdev, "SYSLO");
584 wm831x_free_irq(wm831x, irq, power);
585 err_usb:
586 power_supply_unregister(usb);
587 err_battery:
588 power_supply_unregister(battery);
589 err_wall:
590 power_supply_unregister(wall);
591 err_kmalloc:
592 kfree(power);
593 return ret;
596 static __devexit int wm831x_power_remove(struct platform_device *pdev)
598 struct wm831x_power *wm831x_power = platform_get_drvdata(pdev);
599 struct wm831x *wm831x = wm831x_power->wm831x;
600 int irq, i;
602 for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
603 irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
604 wm831x_free_irq(wm831x, irq, wm831x_power);
607 irq = platform_get_irq_byname(pdev, "PWR SRC");
608 wm831x_free_irq(wm831x, irq, wm831x_power);
610 irq = platform_get_irq_byname(pdev, "SYSLO");
611 wm831x_free_irq(wm831x, irq, wm831x_power);
613 power_supply_unregister(&wm831x_power->battery);
614 power_supply_unregister(&wm831x_power->wall);
615 power_supply_unregister(&wm831x_power->usb);
616 return 0;
619 static struct platform_driver wm831x_power_driver = {
620 .probe = wm831x_power_probe,
621 .remove = __devexit_p(wm831x_power_remove),
622 .driver = {
623 .name = "wm831x-power",
627 static int __init wm831x_power_init(void)
629 return platform_driver_register(&wm831x_power_driver);
631 module_init(wm831x_power_init);
633 static void __exit wm831x_power_exit(void)
635 platform_driver_unregister(&wm831x_power_driver);
637 module_exit(wm831x_power_exit);
639 MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
640 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
641 MODULE_LICENSE("GPL");
642 MODULE_ALIAS("platform:wm831x-power");