OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / power / wm831x_power.c
blob987332b71d8de3ddfbfab813800cac880e48e53d
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;
27 char wall_name[20];
28 char usb_name[20];
29 char battery_name[20];
30 bool have_battery;
33 static int wm831x_power_check_online(struct wm831x *wm831x, int supply,
34 union power_supply_propval *val)
36 int ret;
38 ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
39 if (ret < 0)
40 return ret;
42 if (ret & supply)
43 val->intval = 1;
44 else
45 val->intval = 0;
47 return 0;
50 static int wm831x_power_read_voltage(struct wm831x *wm831x,
51 enum wm831x_auxadc src,
52 union power_supply_propval *val)
54 int ret;
56 ret = wm831x_auxadc_read_uv(wm831x, src);
57 if (ret >= 0)
58 val->intval = ret;
60 return ret;
63 /*********************************************************************
64 * WALL Power
65 *********************************************************************/
66 static int wm831x_wall_get_prop(struct power_supply *psy,
67 enum power_supply_property psp,
68 union power_supply_propval *val)
70 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent);
71 struct wm831x *wm831x = wm831x_power->wm831x;
72 int ret = 0;
74 switch (psp) {
75 case POWER_SUPPLY_PROP_ONLINE:
76 ret = wm831x_power_check_online(wm831x, WM831X_PWR_WALL, val);
77 break;
78 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
79 ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_WALL, val);
80 break;
81 default:
82 ret = -EINVAL;
83 break;
86 return ret;
89 static enum power_supply_property wm831x_wall_props[] = {
90 POWER_SUPPLY_PROP_ONLINE,
91 POWER_SUPPLY_PROP_VOLTAGE_NOW,
94 /*********************************************************************
95 * USB Power
96 *********************************************************************/
97 static int wm831x_usb_get_prop(struct power_supply *psy,
98 enum power_supply_property psp,
99 union power_supply_propval *val)
101 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent);
102 struct wm831x *wm831x = wm831x_power->wm831x;
103 int ret = 0;
105 switch (psp) {
106 case POWER_SUPPLY_PROP_ONLINE:
107 ret = wm831x_power_check_online(wm831x, WM831X_PWR_USB, val);
108 break;
109 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
110 ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_USB, val);
111 break;
112 default:
113 ret = -EINVAL;
114 break;
117 return ret;
120 static enum power_supply_property wm831x_usb_props[] = {
121 POWER_SUPPLY_PROP_ONLINE,
122 POWER_SUPPLY_PROP_VOLTAGE_NOW,
125 /*********************************************************************
126 * Battery properties
127 *********************************************************************/
129 struct chg_map {
130 int val;
131 int reg_val;
134 static struct chg_map trickle_ilims[] = {
135 { 50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT },
136 { 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT },
137 { 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT },
138 { 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT },
141 static struct chg_map vsels[] = {
142 { 4050, 0 << WM831X_CHG_VSEL_SHIFT },
143 { 4100, 1 << WM831X_CHG_VSEL_SHIFT },
144 { 4150, 2 << WM831X_CHG_VSEL_SHIFT },
145 { 4200, 3 << WM831X_CHG_VSEL_SHIFT },
148 static struct chg_map fast_ilims[] = {
149 { 0, 0 << WM831X_CHG_FAST_ILIM_SHIFT },
150 { 50, 1 << WM831X_CHG_FAST_ILIM_SHIFT },
151 { 100, 2 << WM831X_CHG_FAST_ILIM_SHIFT },
152 { 150, 3 << WM831X_CHG_FAST_ILIM_SHIFT },
153 { 200, 4 << WM831X_CHG_FAST_ILIM_SHIFT },
154 { 250, 5 << WM831X_CHG_FAST_ILIM_SHIFT },
155 { 300, 6 << WM831X_CHG_FAST_ILIM_SHIFT },
156 { 350, 7 << WM831X_CHG_FAST_ILIM_SHIFT },
157 { 400, 8 << WM831X_CHG_FAST_ILIM_SHIFT },
158 { 450, 9 << WM831X_CHG_FAST_ILIM_SHIFT },
159 { 500, 10 << WM831X_CHG_FAST_ILIM_SHIFT },
160 { 600, 11 << WM831X_CHG_FAST_ILIM_SHIFT },
161 { 700, 12 << WM831X_CHG_FAST_ILIM_SHIFT },
162 { 800, 13 << WM831X_CHG_FAST_ILIM_SHIFT },
163 { 900, 14 << WM831X_CHG_FAST_ILIM_SHIFT },
164 { 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT },
167 static struct chg_map eoc_iterms[] = {
168 { 20, 0 << WM831X_CHG_ITERM_SHIFT },
169 { 30, 1 << WM831X_CHG_ITERM_SHIFT },
170 { 40, 2 << WM831X_CHG_ITERM_SHIFT },
171 { 50, 3 << WM831X_CHG_ITERM_SHIFT },
172 { 60, 4 << WM831X_CHG_ITERM_SHIFT },
173 { 70, 5 << WM831X_CHG_ITERM_SHIFT },
174 { 80, 6 << WM831X_CHG_ITERM_SHIFT },
175 { 90, 7 << WM831X_CHG_ITERM_SHIFT },
178 static struct chg_map chg_times[] = {
179 { 60, 0 << WM831X_CHG_TIME_SHIFT },
180 { 90, 1 << WM831X_CHG_TIME_SHIFT },
181 { 120, 2 << WM831X_CHG_TIME_SHIFT },
182 { 150, 3 << WM831X_CHG_TIME_SHIFT },
183 { 180, 4 << WM831X_CHG_TIME_SHIFT },
184 { 210, 5 << WM831X_CHG_TIME_SHIFT },
185 { 240, 6 << WM831X_CHG_TIME_SHIFT },
186 { 270, 7 << WM831X_CHG_TIME_SHIFT },
187 { 300, 8 << WM831X_CHG_TIME_SHIFT },
188 { 330, 9 << WM831X_CHG_TIME_SHIFT },
189 { 360, 10 << WM831X_CHG_TIME_SHIFT },
190 { 390, 11 << WM831X_CHG_TIME_SHIFT },
191 { 420, 12 << WM831X_CHG_TIME_SHIFT },
192 { 450, 13 << WM831X_CHG_TIME_SHIFT },
193 { 480, 14 << WM831X_CHG_TIME_SHIFT },
194 { 510, 15 << WM831X_CHG_TIME_SHIFT },
197 static void wm831x_battey_apply_config(struct wm831x *wm831x,
198 struct chg_map *map, int count, int val,
199 int *reg, const char *name,
200 const char *units)
202 int i;
204 for (i = 0; i < count; i++)
205 if (val == map[i].val)
206 break;
207 if (i == count) {
208 dev_err(wm831x->dev, "Invalid %s %d%s\n",
209 name, val, units);
210 } else {
211 *reg |= map[i].reg_val;
212 dev_dbg(wm831x->dev, "Set %s of %d%s\n", name, val, units);
216 static void wm831x_config_battery(struct wm831x *wm831x)
218 struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
219 struct wm831x_battery_pdata *pdata;
220 int ret, reg1, reg2;
222 if (!wm831x_pdata || !wm831x_pdata->battery) {
223 dev_warn(wm831x->dev,
224 "No battery charger configuration\n");
225 return;
228 pdata = wm831x_pdata->battery;
230 reg1 = 0;
231 reg2 = 0;
233 if (!pdata->enable) {
234 dev_info(wm831x->dev, "Battery charger disabled\n");
235 return;
238 reg1 |= WM831X_CHG_ENA;
239 if (pdata->off_mask)
240 reg2 |= WM831X_CHG_OFF_MSK;
241 if (pdata->fast_enable)
242 reg1 |= WM831X_CHG_FAST;
244 wm831x_battey_apply_config(wm831x, trickle_ilims,
245 ARRAY_SIZE(trickle_ilims),
246 pdata->trickle_ilim, &reg2,
247 "trickle charge current limit", "mA");
249 wm831x_battey_apply_config(wm831x, vsels, ARRAY_SIZE(vsels),
250 pdata->vsel, &reg2,
251 "target voltage", "mV");
253 wm831x_battey_apply_config(wm831x, fast_ilims, ARRAY_SIZE(fast_ilims),
254 pdata->fast_ilim, &reg2,
255 "fast charge current limit", "mA");
257 wm831x_battey_apply_config(wm831x, eoc_iterms, ARRAY_SIZE(eoc_iterms),
258 pdata->eoc_iterm, &reg1,
259 "end of charge current threshold", "mA");
261 wm831x_battey_apply_config(wm831x, chg_times, ARRAY_SIZE(chg_times),
262 pdata->timeout, &reg2,
263 "charger timeout", "min");
265 ret = wm831x_reg_unlock(wm831x);
266 if (ret != 0) {
267 dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret);
268 return;
271 ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_1,
272 WM831X_CHG_ENA_MASK |
273 WM831X_CHG_FAST_MASK |
274 WM831X_CHG_ITERM_MASK,
275 reg1);
276 if (ret != 0)
277 dev_err(wm831x->dev, "Failed to set charger control 1: %d\n",
278 ret);
280 ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_2,
281 WM831X_CHG_OFF_MSK |
282 WM831X_CHG_TIME_MASK |
283 WM831X_CHG_FAST_ILIM_MASK |
284 WM831X_CHG_TRKL_ILIM_MASK |
285 WM831X_CHG_VSEL_MASK,
286 reg2);
287 if (ret != 0)
288 dev_err(wm831x->dev, "Failed to set charger control 2: %d\n",
289 ret);
291 wm831x_reg_lock(wm831x);
294 static int wm831x_bat_check_status(struct wm831x *wm831x, int *status)
296 int ret;
298 ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
299 if (ret < 0)
300 return ret;
302 if (ret & WM831X_PWR_SRC_BATT) {
303 *status = POWER_SUPPLY_STATUS_DISCHARGING;
304 return 0;
307 ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
308 if (ret < 0)
309 return ret;
311 switch (ret & WM831X_CHG_STATE_MASK) {
312 case WM831X_CHG_STATE_OFF:
313 *status = POWER_SUPPLY_STATUS_NOT_CHARGING;
314 break;
315 case WM831X_CHG_STATE_TRICKLE:
316 case WM831X_CHG_STATE_FAST:
317 *status = POWER_SUPPLY_STATUS_CHARGING;
318 break;
320 default:
321 *status = POWER_SUPPLY_STATUS_UNKNOWN;
322 break;
325 return 0;
328 static int wm831x_bat_check_type(struct wm831x *wm831x, int *type)
330 int ret;
332 ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
333 if (ret < 0)
334 return ret;
336 switch (ret & WM831X_CHG_STATE_MASK) {
337 case WM831X_CHG_STATE_TRICKLE:
338 case WM831X_CHG_STATE_TRICKLE_OT:
339 *type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
340 break;
341 case WM831X_CHG_STATE_FAST:
342 case WM831X_CHG_STATE_FAST_OT:
343 *type = POWER_SUPPLY_CHARGE_TYPE_FAST;
344 break;
345 default:
346 *type = POWER_SUPPLY_CHARGE_TYPE_NONE;
347 break;
350 return 0;
353 static int wm831x_bat_check_health(struct wm831x *wm831x, int *health)
355 int ret;
357 ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
358 if (ret < 0)
359 return ret;
361 if (ret & WM831X_BATT_HOT_STS) {
362 *health = POWER_SUPPLY_HEALTH_OVERHEAT;
363 return 0;
366 if (ret & WM831X_BATT_COLD_STS) {
367 *health = POWER_SUPPLY_HEALTH_COLD;
368 return 0;
371 if (ret & WM831X_BATT_OV_STS) {
372 *health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
373 return 0;
376 switch (ret & WM831X_CHG_STATE_MASK) {
377 case WM831X_CHG_STATE_TRICKLE_OT:
378 case WM831X_CHG_STATE_FAST_OT:
379 *health = POWER_SUPPLY_HEALTH_OVERHEAT;
380 break;
381 case WM831X_CHG_STATE_DEFECTIVE:
382 *health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
383 break;
384 default:
385 *health = POWER_SUPPLY_HEALTH_GOOD;
386 break;
389 return 0;
392 static int wm831x_bat_get_prop(struct power_supply *psy,
393 enum power_supply_property psp,
394 union power_supply_propval *val)
396 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev->parent);
397 struct wm831x *wm831x = wm831x_power->wm831x;
398 int ret = 0;
400 switch (psp) {
401 case POWER_SUPPLY_PROP_STATUS:
402 ret = wm831x_bat_check_status(wm831x, &val->intval);
403 break;
404 case POWER_SUPPLY_PROP_ONLINE:
405 ret = wm831x_power_check_online(wm831x, WM831X_PWR_SRC_BATT,
406 val);
407 break;
408 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
409 ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_BATT, val);
410 break;
411 case POWER_SUPPLY_PROP_HEALTH:
412 ret = wm831x_bat_check_health(wm831x, &val->intval);
413 break;
414 case POWER_SUPPLY_PROP_CHARGE_TYPE:
415 ret = wm831x_bat_check_type(wm831x, &val->intval);
416 break;
417 default:
418 ret = -EINVAL;
419 break;
422 return ret;
425 static enum power_supply_property wm831x_bat_props[] = {
426 POWER_SUPPLY_PROP_STATUS,
427 POWER_SUPPLY_PROP_ONLINE,
428 POWER_SUPPLY_PROP_VOLTAGE_NOW,
429 POWER_SUPPLY_PROP_HEALTH,
430 POWER_SUPPLY_PROP_CHARGE_TYPE,
433 static const char *wm831x_bat_irqs[] = {
434 "BATT HOT",
435 "BATT COLD",
436 "BATT FAIL",
437 "OV",
438 "END",
439 "TO",
440 "MODE",
441 "START",
444 static irqreturn_t wm831x_bat_irq(int irq, void *data)
446 struct wm831x_power *wm831x_power = data;
447 struct wm831x *wm831x = wm831x_power->wm831x;
449 dev_dbg(wm831x->dev, "Battery status changed: %d\n", irq);
451 /* The battery charger is autonomous so we don't need to do
452 * anything except kick user space */
453 if (wm831x_power->have_battery)
454 power_supply_changed(&wm831x_power->battery);
456 return IRQ_HANDLED;
460 /*********************************************************************
461 * Initialisation
462 *********************************************************************/
464 static irqreturn_t wm831x_syslo_irq(int irq, void *data)
466 struct wm831x_power *wm831x_power = data;
467 struct wm831x *wm831x = wm831x_power->wm831x;
469 /* Not much we can actually *do* but tell people for
470 * posterity, we're probably about to run out of power. */
471 dev_crit(wm831x->dev, "SYSVDD under voltage\n");
473 return IRQ_HANDLED;
476 static irqreturn_t wm831x_pwr_src_irq(int irq, void *data)
478 struct wm831x_power *wm831x_power = data;
479 struct wm831x *wm831x = wm831x_power->wm831x;
481 dev_dbg(wm831x->dev, "Power source changed\n");
483 /* Just notify for everything - little harm in overnotifying. */
484 if (wm831x_power->have_battery)
485 power_supply_changed(&wm831x_power->battery);
486 power_supply_changed(&wm831x_power->usb);
487 power_supply_changed(&wm831x_power->wall);
489 return IRQ_HANDLED;
492 static __devinit int wm831x_power_probe(struct platform_device *pdev)
494 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
495 struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
496 struct wm831x_power *power;
497 struct power_supply *usb;
498 struct power_supply *battery;
499 struct power_supply *wall;
500 int ret, irq, i;
502 power = kzalloc(sizeof(struct wm831x_power), GFP_KERNEL);
503 if (power == NULL)
504 return -ENOMEM;
506 power->wm831x = wm831x;
507 platform_set_drvdata(pdev, power);
509 usb = &power->usb;
510 battery = &power->battery;
511 wall = &power->wall;
513 if (wm831x_pdata && wm831x_pdata->wm831x_num) {
514 snprintf(power->wall_name, sizeof(power->wall_name),
515 "wm831x-wall.%d", wm831x_pdata->wm831x_num);
516 snprintf(power->battery_name, sizeof(power->wall_name),
517 "wm831x-battery.%d", wm831x_pdata->wm831x_num);
518 snprintf(power->usb_name, sizeof(power->wall_name),
519 "wm831x-usb.%d", wm831x_pdata->wm831x_num);
520 } else {
521 snprintf(power->wall_name, sizeof(power->wall_name),
522 "wm831x-wall");
523 snprintf(power->battery_name, sizeof(power->wall_name),
524 "wm831x-battery");
525 snprintf(power->usb_name, sizeof(power->wall_name),
526 "wm831x-usb");
529 /* We ignore configuration failures since we can still read back
530 * the status without enabling the charger.
532 wm831x_config_battery(wm831x);
534 wall->name = power->wall_name;
535 wall->type = POWER_SUPPLY_TYPE_MAINS;
536 wall->properties = wm831x_wall_props;
537 wall->num_properties = ARRAY_SIZE(wm831x_wall_props);
538 wall->get_property = wm831x_wall_get_prop;
539 ret = power_supply_register(&pdev->dev, wall);
540 if (ret)
541 goto err_kmalloc;
543 usb->name = power->usb_name,
544 usb->type = POWER_SUPPLY_TYPE_USB;
545 usb->properties = wm831x_usb_props;
546 usb->num_properties = ARRAY_SIZE(wm831x_usb_props);
547 usb->get_property = wm831x_usb_get_prop;
548 ret = power_supply_register(&pdev->dev, usb);
549 if (ret)
550 goto err_wall;
552 ret = wm831x_reg_read(wm831x, WM831X_CHARGER_CONTROL_1);
553 if (ret < 0)
554 goto err_wall;
555 power->have_battery = ret & WM831X_CHG_ENA;
557 if (power->have_battery) {
558 battery->name = power->battery_name;
559 battery->properties = wm831x_bat_props;
560 battery->num_properties = ARRAY_SIZE(wm831x_bat_props);
561 battery->get_property = wm831x_bat_get_prop;
562 battery->use_for_apm = 1;
563 ret = power_supply_register(&pdev->dev, battery);
564 if (ret)
565 goto err_usb;
568 irq = platform_get_irq_byname(pdev, "SYSLO");
569 ret = request_threaded_irq(irq, NULL, wm831x_syslo_irq,
570 IRQF_TRIGGER_RISING, "System power low",
571 power);
572 if (ret != 0) {
573 dev_err(&pdev->dev, "Failed to request SYSLO IRQ %d: %d\n",
574 irq, ret);
575 goto err_battery;
578 irq = platform_get_irq_byname(pdev, "PWR SRC");
579 ret = request_threaded_irq(irq, NULL, wm831x_pwr_src_irq,
580 IRQF_TRIGGER_RISING, "Power source",
581 power);
582 if (ret != 0) {
583 dev_err(&pdev->dev, "Failed to request PWR SRC IRQ %d: %d\n",
584 irq, ret);
585 goto err_syslo;
588 for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
589 irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
590 ret = request_threaded_irq(irq, NULL, wm831x_bat_irq,
591 IRQF_TRIGGER_RISING,
592 wm831x_bat_irqs[i],
593 power);
594 if (ret != 0) {
595 dev_err(&pdev->dev,
596 "Failed to request %s IRQ %d: %d\n",
597 wm831x_bat_irqs[i], irq, ret);
598 goto err_bat_irq;
602 return ret;
604 err_bat_irq:
605 for (; i >= 0; i--) {
606 irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
607 free_irq(irq, power);
609 irq = platform_get_irq_byname(pdev, "PWR SRC");
610 free_irq(irq, power);
611 err_syslo:
612 irq = platform_get_irq_byname(pdev, "SYSLO");
613 free_irq(irq, power);
614 err_battery:
615 if (power->have_battery)
616 power_supply_unregister(battery);
617 err_usb:
618 power_supply_unregister(usb);
619 err_wall:
620 power_supply_unregister(wall);
621 err_kmalloc:
622 kfree(power);
623 return ret;
626 static __devexit int wm831x_power_remove(struct platform_device *pdev)
628 struct wm831x_power *wm831x_power = platform_get_drvdata(pdev);
629 int irq, i;
631 for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
632 irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
633 free_irq(irq, wm831x_power);
636 irq = platform_get_irq_byname(pdev, "PWR SRC");
637 free_irq(irq, wm831x_power);
639 irq = platform_get_irq_byname(pdev, "SYSLO");
640 free_irq(irq, wm831x_power);
642 if (wm831x_power->have_battery)
643 power_supply_unregister(&wm831x_power->battery);
644 power_supply_unregister(&wm831x_power->wall);
645 power_supply_unregister(&wm831x_power->usb);
646 kfree(wm831x_power);
647 return 0;
650 static struct platform_driver wm831x_power_driver = {
651 .probe = wm831x_power_probe,
652 .remove = __devexit_p(wm831x_power_remove),
653 .driver = {
654 .name = "wm831x-power",
658 module_platform_driver(wm831x_power_driver);
660 MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
661 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
662 MODULE_LICENSE("GPL");
663 MODULE_ALIAS("platform:wm831x-power");