1 /*
2  * PMU driver for Wolfson Microelectronics wm831x PMICs
3  *
4  * Copyright 2009 Wolfson Microelectronics PLC.
5  *
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  */
10 
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>
16 
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>
21 
22 struct wm831x_power {
23 	struct wm831x *wm831x;
24 	struct power_supply *wall;
25 	struct power_supply *usb;
26 	struct power_supply *battery;
27 	struct power_supply_desc wall_desc;
28 	struct power_supply_desc usb_desc;
29 	struct power_supply_desc battery_desc;
30 	char wall_name[20];
31 	char usb_name[20];
32 	char battery_name[20];
33 	bool have_battery;
34 };
35 
36 static int wm831x_power_check_online(struct wm831x *wm831x, int supply,
37 				     union power_supply_propval *val)
38 {
39 	int ret;
40 
41 	ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
42 	if (ret < 0)
43 		return ret;
44 
45 	if (ret & supply)
46 		val->intval = 1;
47 	else
48 		val->intval = 0;
49 
50 	return 0;
51 }
52 
53 static int wm831x_power_read_voltage(struct wm831x *wm831x,
54 				     enum wm831x_auxadc src,
55 				     union power_supply_propval *val)
56 {
57 	int ret;
58 
59 	ret = wm831x_auxadc_read_uv(wm831x, src);
60 	if (ret >= 0)
61 		val->intval = ret;
62 
63 	return ret;
64 }
65 
66 /*********************************************************************
67  *		WALL Power
68  *********************************************************************/
69 static int wm831x_wall_get_prop(struct power_supply *psy,
70 				enum power_supply_property psp,
71 				union power_supply_propval *val)
72 {
73 	struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
74 	struct wm831x *wm831x = wm831x_power->wm831x;
75 	int ret = 0;
76 
77 	switch (psp) {
78 	case POWER_SUPPLY_PROP_ONLINE:
79 		ret = wm831x_power_check_online(wm831x, WM831X_PWR_WALL, val);
80 		break;
81 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
82 		ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_WALL, val);
83 		break;
84 	default:
85 		ret = -EINVAL;
86 		break;
87 	}
88 
89 	return ret;
90 }
91 
92 static enum power_supply_property wm831x_wall_props[] = {
93 	POWER_SUPPLY_PROP_ONLINE,
94 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
95 };
96 
97 /*********************************************************************
98  *		USB Power
99  *********************************************************************/
100 static int wm831x_usb_get_prop(struct power_supply *psy,
101 			       enum power_supply_property psp,
102 			       union power_supply_propval *val)
103 {
104 	struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
105 	struct wm831x *wm831x = wm831x_power->wm831x;
106 	int ret = 0;
107 
108 	switch (psp) {
109 	case POWER_SUPPLY_PROP_ONLINE:
110 		ret = wm831x_power_check_online(wm831x, WM831X_PWR_USB, val);
111 		break;
112 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
113 		ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_USB, val);
114 		break;
115 	default:
116 		ret = -EINVAL;
117 		break;
118 	}
119 
120 	return ret;
121 }
122 
123 static enum power_supply_property wm831x_usb_props[] = {
124 	POWER_SUPPLY_PROP_ONLINE,
125 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
126 };
127 
128 /*********************************************************************
129  *		Battery properties
130  *********************************************************************/
131 
132 struct chg_map {
133 	int val;
134 	int reg_val;
135 };
136 
137 static struct chg_map trickle_ilims[] = {
138 	{  50, 0 << WM831X_CHG_TRKL_ILIM_SHIFT },
139 	{ 100, 1 << WM831X_CHG_TRKL_ILIM_SHIFT },
140 	{ 150, 2 << WM831X_CHG_TRKL_ILIM_SHIFT },
141 	{ 200, 3 << WM831X_CHG_TRKL_ILIM_SHIFT },
142 };
143 
144 static struct chg_map vsels[] = {
145 	{ 4050, 0 << WM831X_CHG_VSEL_SHIFT },
146 	{ 4100, 1 << WM831X_CHG_VSEL_SHIFT },
147 	{ 4150, 2 << WM831X_CHG_VSEL_SHIFT },
148 	{ 4200, 3 << WM831X_CHG_VSEL_SHIFT },
149 };
150 
151 static struct chg_map fast_ilims[] = {
152 	{    0,  0 << WM831X_CHG_FAST_ILIM_SHIFT },
153 	{   50,  1 << WM831X_CHG_FAST_ILIM_SHIFT },
154 	{  100,  2 << WM831X_CHG_FAST_ILIM_SHIFT },
155 	{  150,  3 << WM831X_CHG_FAST_ILIM_SHIFT },
156 	{  200,  4 << WM831X_CHG_FAST_ILIM_SHIFT },
157 	{  250,  5 << WM831X_CHG_FAST_ILIM_SHIFT },
158 	{  300,  6 << WM831X_CHG_FAST_ILIM_SHIFT },
159 	{  350,  7 << WM831X_CHG_FAST_ILIM_SHIFT },
160 	{  400,  8 << WM831X_CHG_FAST_ILIM_SHIFT },
161 	{  450,  9 << WM831X_CHG_FAST_ILIM_SHIFT },
162 	{  500, 10 << WM831X_CHG_FAST_ILIM_SHIFT },
163 	{  600, 11 << WM831X_CHG_FAST_ILIM_SHIFT },
164 	{  700, 12 << WM831X_CHG_FAST_ILIM_SHIFT },
165 	{  800, 13 << WM831X_CHG_FAST_ILIM_SHIFT },
166 	{  900, 14 << WM831X_CHG_FAST_ILIM_SHIFT },
167 	{ 1000, 15 << WM831X_CHG_FAST_ILIM_SHIFT },
168 };
169 
170 static struct chg_map eoc_iterms[] = {
171 	{ 20, 0 << WM831X_CHG_ITERM_SHIFT },
172 	{ 30, 1 << WM831X_CHG_ITERM_SHIFT },
173 	{ 40, 2 << WM831X_CHG_ITERM_SHIFT },
174 	{ 50, 3 << WM831X_CHG_ITERM_SHIFT },
175 	{ 60, 4 << WM831X_CHG_ITERM_SHIFT },
176 	{ 70, 5 << WM831X_CHG_ITERM_SHIFT },
177 	{ 80, 6 << WM831X_CHG_ITERM_SHIFT },
178 	{ 90, 7 << WM831X_CHG_ITERM_SHIFT },
179 };
180 
181 static struct chg_map chg_times[] = {
182 	{  60,  0 << WM831X_CHG_TIME_SHIFT },
183 	{  90,  1 << WM831X_CHG_TIME_SHIFT },
184 	{ 120,  2 << WM831X_CHG_TIME_SHIFT },
185 	{ 150,  3 << WM831X_CHG_TIME_SHIFT },
186 	{ 180,  4 << WM831X_CHG_TIME_SHIFT },
187 	{ 210,  5 << WM831X_CHG_TIME_SHIFT },
188 	{ 240,  6 << WM831X_CHG_TIME_SHIFT },
189 	{ 270,  7 << WM831X_CHG_TIME_SHIFT },
190 	{ 300,  8 << WM831X_CHG_TIME_SHIFT },
191 	{ 330,  9 << WM831X_CHG_TIME_SHIFT },
192 	{ 360, 10 << WM831X_CHG_TIME_SHIFT },
193 	{ 390, 11 << WM831X_CHG_TIME_SHIFT },
194 	{ 420, 12 << WM831X_CHG_TIME_SHIFT },
195 	{ 450, 13 << WM831X_CHG_TIME_SHIFT },
196 	{ 480, 14 << WM831X_CHG_TIME_SHIFT },
197 	{ 510, 15 << WM831X_CHG_TIME_SHIFT },
198 };
199 
200 static void wm831x_battey_apply_config(struct wm831x *wm831x,
201 				       struct chg_map *map, int count, int val,
202 				       int *reg, const char *name,
203 				       const char *units)
204 {
205 	int i;
206 
207 	for (i = 0; i < count; i++)
208 		if (val == map[i].val)
209 			break;
210 	if (i == count) {
211 		dev_err(wm831x->dev, "Invalid %s %d%s\n",
212 			name, val, units);
213 	} else {
214 		*reg |= map[i].reg_val;
215 		dev_dbg(wm831x->dev, "Set %s of %d%s\n", name, val, units);
216 	}
217 }
218 
219 static void wm831x_config_battery(struct wm831x *wm831x)
220 {
221 	struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
222 	struct wm831x_battery_pdata *pdata;
223 	int ret, reg1, reg2;
224 
225 	if (!wm831x_pdata || !wm831x_pdata->battery) {
226 		dev_warn(wm831x->dev,
227 			 "No battery charger configuration\n");
228 		return;
229 	}
230 
231 	pdata = wm831x_pdata->battery;
232 
233 	reg1 = 0;
234 	reg2 = 0;
235 
236 	if (!pdata->enable) {
237 		dev_info(wm831x->dev, "Battery charger disabled\n");
238 		return;
239 	}
240 
241 	reg1 |= WM831X_CHG_ENA;
242 	if (pdata->off_mask)
243 		reg2 |= WM831X_CHG_OFF_MSK;
244 	if (pdata->fast_enable)
245 		reg1 |= WM831X_CHG_FAST;
246 
247 	wm831x_battey_apply_config(wm831x, trickle_ilims,
248 				   ARRAY_SIZE(trickle_ilims),
249 				   pdata->trickle_ilim, &reg2,
250 				   "trickle charge current limit", "mA");
251 
252 	wm831x_battey_apply_config(wm831x, vsels, ARRAY_SIZE(vsels),
253 				   pdata->vsel, &reg2,
254 				   "target voltage", "mV");
255 
256 	wm831x_battey_apply_config(wm831x, fast_ilims, ARRAY_SIZE(fast_ilims),
257 				   pdata->fast_ilim, &reg2,
258 				   "fast charge current limit", "mA");
259 
260 	wm831x_battey_apply_config(wm831x, eoc_iterms, ARRAY_SIZE(eoc_iterms),
261 				   pdata->eoc_iterm, &reg1,
262 				   "end of charge current threshold", "mA");
263 
264 	wm831x_battey_apply_config(wm831x, chg_times, ARRAY_SIZE(chg_times),
265 				   pdata->timeout, &reg2,
266 				   "charger timeout", "min");
267 
268 	ret = wm831x_reg_unlock(wm831x);
269 	if (ret != 0) {
270 		dev_err(wm831x->dev, "Failed to unlock registers: %d\n", ret);
271 		return;
272 	}
273 
274 	ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_1,
275 			      WM831X_CHG_ENA_MASK |
276 			      WM831X_CHG_FAST_MASK |
277 			      WM831X_CHG_ITERM_MASK,
278 			      reg1);
279 	if (ret != 0)
280 		dev_err(wm831x->dev, "Failed to set charger control 1: %d\n",
281 			ret);
282 
283 	ret = wm831x_set_bits(wm831x, WM831X_CHARGER_CONTROL_2,
284 			      WM831X_CHG_OFF_MSK |
285 			      WM831X_CHG_TIME_MASK |
286 			      WM831X_CHG_FAST_ILIM_MASK |
287 			      WM831X_CHG_TRKL_ILIM_MASK |
288 			      WM831X_CHG_VSEL_MASK,
289 			      reg2);
290 	if (ret != 0)
291 		dev_err(wm831x->dev, "Failed to set charger control 2: %d\n",
292 			ret);
293 
294 	wm831x_reg_lock(wm831x);
295 }
296 
297 static int wm831x_bat_check_status(struct wm831x *wm831x, int *status)
298 {
299 	int ret;
300 
301 	ret = wm831x_reg_read(wm831x, WM831X_SYSTEM_STATUS);
302 	if (ret < 0)
303 		return ret;
304 
305 	if (ret & WM831X_PWR_SRC_BATT) {
306 		*status = POWER_SUPPLY_STATUS_DISCHARGING;
307 		return 0;
308 	}
309 
310 	ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
311 	if (ret < 0)
312 		return ret;
313 
314 	switch (ret & WM831X_CHG_STATE_MASK) {
315 	case WM831X_CHG_STATE_OFF:
316 		*status = POWER_SUPPLY_STATUS_NOT_CHARGING;
317 		break;
318 	case WM831X_CHG_STATE_TRICKLE:
319 	case WM831X_CHG_STATE_FAST:
320 		*status = POWER_SUPPLY_STATUS_CHARGING;
321 		break;
322 
323 	default:
324 		*status = POWER_SUPPLY_STATUS_UNKNOWN;
325 		break;
326 	}
327 
328 	return 0;
329 }
330 
331 static int wm831x_bat_check_type(struct wm831x *wm831x, int *type)
332 {
333 	int ret;
334 
335 	ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
336 	if (ret < 0)
337 		return ret;
338 
339 	switch (ret & WM831X_CHG_STATE_MASK) {
340 	case WM831X_CHG_STATE_TRICKLE:
341 	case WM831X_CHG_STATE_TRICKLE_OT:
342 		*type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
343 		break;
344 	case WM831X_CHG_STATE_FAST:
345 	case WM831X_CHG_STATE_FAST_OT:
346 		*type = POWER_SUPPLY_CHARGE_TYPE_FAST;
347 		break;
348 	default:
349 		*type = POWER_SUPPLY_CHARGE_TYPE_NONE;
350 		break;
351 	}
352 
353 	return 0;
354 }
355 
356 static int wm831x_bat_check_health(struct wm831x *wm831x, int *health)
357 {
358 	int ret;
359 
360 	ret = wm831x_reg_read(wm831x, WM831X_CHARGER_STATUS);
361 	if (ret < 0)
362 		return ret;
363 
364 	if (ret & WM831X_BATT_HOT_STS) {
365 		*health = POWER_SUPPLY_HEALTH_OVERHEAT;
366 		return 0;
367 	}
368 
369 	if (ret & WM831X_BATT_COLD_STS) {
370 		*health = POWER_SUPPLY_HEALTH_COLD;
371 		return 0;
372 	}
373 
374 	if (ret & WM831X_BATT_OV_STS) {
375 		*health = POWER_SUPPLY_HEALTH_OVERVOLTAGE;
376 		return 0;
377 	}
378 
379 	switch (ret & WM831X_CHG_STATE_MASK) {
380 	case WM831X_CHG_STATE_TRICKLE_OT:
381 	case WM831X_CHG_STATE_FAST_OT:
382 		*health = POWER_SUPPLY_HEALTH_OVERHEAT;
383 		break;
384 	case WM831X_CHG_STATE_DEFECTIVE:
385 		*health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
386 		break;
387 	default:
388 		*health = POWER_SUPPLY_HEALTH_GOOD;
389 		break;
390 	}
391 
392 	return 0;
393 }
394 
395 static int wm831x_bat_get_prop(struct power_supply *psy,
396 			       enum power_supply_property psp,
397 			       union power_supply_propval *val)
398 {
399 	struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
400 	struct wm831x *wm831x = wm831x_power->wm831x;
401 	int ret = 0;
402 
403 	switch (psp) {
404 	case POWER_SUPPLY_PROP_STATUS:
405 		ret = wm831x_bat_check_status(wm831x, &val->intval);
406 		break;
407 	case POWER_SUPPLY_PROP_ONLINE:
408 		ret = wm831x_power_check_online(wm831x, WM831X_PWR_SRC_BATT,
409 						val);
410 		break;
411 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
412 		ret = wm831x_power_read_voltage(wm831x, WM831X_AUX_BATT, val);
413 		break;
414 	case POWER_SUPPLY_PROP_HEALTH:
415 		ret = wm831x_bat_check_health(wm831x, &val->intval);
416 		break;
417 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
418 		ret = wm831x_bat_check_type(wm831x, &val->intval);
419 		break;
420 	default:
421 		ret = -EINVAL;
422 		break;
423 	}
424 
425 	return ret;
426 }
427 
428 static enum power_supply_property wm831x_bat_props[] = {
429 	POWER_SUPPLY_PROP_STATUS,
430 	POWER_SUPPLY_PROP_ONLINE,
431 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
432 	POWER_SUPPLY_PROP_HEALTH,
433 	POWER_SUPPLY_PROP_CHARGE_TYPE,
434 };
435 
436 static const char *wm831x_bat_irqs[] = {
437 	"BATT HOT",
438 	"BATT COLD",
439 	"BATT FAIL",
440 	"OV",
441 	"END",
442 	"TO",
443 	"MODE",
444 	"START",
445 };
446 
447 static irqreturn_t wm831x_bat_irq(int irq, void *data)
448 {
449 	struct wm831x_power *wm831x_power = data;
450 	struct wm831x *wm831x = wm831x_power->wm831x;
451 
452 	dev_dbg(wm831x->dev, "Battery status changed: %d\n", irq);
453 
454 	/* The battery charger is autonomous so we don't need to do
455 	 * anything except kick user space */
456 	if (wm831x_power->have_battery)
457 		power_supply_changed(wm831x_power->battery);
458 
459 	return IRQ_HANDLED;
460 }
461 
462 
463 /*********************************************************************
464  *		Initialisation
465  *********************************************************************/
466 
467 static irqreturn_t wm831x_syslo_irq(int irq, void *data)
468 {
469 	struct wm831x_power *wm831x_power = data;
470 	struct wm831x *wm831x = wm831x_power->wm831x;
471 
472 	/* Not much we can actually *do* but tell people for
473 	 * posterity, we're probably about to run out of power. */
474 	dev_crit(wm831x->dev, "SYSVDD under voltage\n");
475 
476 	return IRQ_HANDLED;
477 }
478 
479 static irqreturn_t wm831x_pwr_src_irq(int irq, void *data)
480 {
481 	struct wm831x_power *wm831x_power = data;
482 	struct wm831x *wm831x = wm831x_power->wm831x;
483 
484 	dev_dbg(wm831x->dev, "Power source changed\n");
485 
486 	/* Just notify for everything - little harm in overnotifying. */
487 	if (wm831x_power->have_battery)
488 		power_supply_changed(wm831x_power->battery);
489 	power_supply_changed(wm831x_power->usb);
490 	power_supply_changed(wm831x_power->wall);
491 
492 	return IRQ_HANDLED;
493 }
494 
495 static int wm831x_power_probe(struct platform_device *pdev)
496 {
497 	struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
498 	struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
499 	struct wm831x_power *power;
500 	int ret, irq, i;
501 
502 	power = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_power),
503 			     GFP_KERNEL);
504 	if (power == NULL)
505 		return -ENOMEM;
506 
507 	power->wm831x = wm831x;
508 	platform_set_drvdata(pdev, power);
509 
510 	if (wm831x_pdata && wm831x_pdata->wm831x_num) {
511 		snprintf(power->wall_name, sizeof(power->wall_name),
512 			 "wm831x-wall.%d", wm831x_pdata->wm831x_num);
513 		snprintf(power->battery_name, sizeof(power->wall_name),
514 			 "wm831x-battery.%d", wm831x_pdata->wm831x_num);
515 		snprintf(power->usb_name, sizeof(power->wall_name),
516 			 "wm831x-usb.%d", wm831x_pdata->wm831x_num);
517 	} else {
518 		snprintf(power->wall_name, sizeof(power->wall_name),
519 			 "wm831x-wall");
520 		snprintf(power->battery_name, sizeof(power->wall_name),
521 			 "wm831x-battery");
522 		snprintf(power->usb_name, sizeof(power->wall_name),
523 			 "wm831x-usb");
524 	}
525 
526 	/* We ignore configuration failures since we can still read back
527 	 * the status without enabling the charger.
528 	 */
529 	wm831x_config_battery(wm831x);
530 
531 	power->wall_desc.name = power->wall_name;
532 	power->wall_desc.type = POWER_SUPPLY_TYPE_MAINS;
533 	power->wall_desc.properties = wm831x_wall_props;
534 	power->wall_desc.num_properties = ARRAY_SIZE(wm831x_wall_props);
535 	power->wall_desc.get_property = wm831x_wall_get_prop;
536 	power->wall = power_supply_register(&pdev->dev, &power->wall_desc,
537 					    NULL);
538 	if (IS_ERR(power->wall)) {
539 		ret = PTR_ERR(power->wall);
540 		goto err;
541 	}
542 
543 	power->usb_desc.name = power->usb_name,
544 	power->usb_desc.type = POWER_SUPPLY_TYPE_USB;
545 	power->usb_desc.properties = wm831x_usb_props;
546 	power->usb_desc.num_properties = ARRAY_SIZE(wm831x_usb_props);
547 	power->usb_desc.get_property = wm831x_usb_get_prop;
548 	power->usb = power_supply_register(&pdev->dev, &power->usb_desc, NULL);
549 	if (IS_ERR(power->usb)) {
550 		ret = PTR_ERR(power->usb);
551 		goto err_wall;
552 	}
553 
554 	ret = wm831x_reg_read(wm831x, WM831X_CHARGER_CONTROL_1);
555 	if (ret < 0)
556 		goto err_wall;
557 	power->have_battery = ret & WM831X_CHG_ENA;
558 
559 	if (power->have_battery) {
560 		power->battery_desc.name = power->battery_name;
561 		power->battery_desc.properties = wm831x_bat_props;
562 		power->battery_desc.num_properties = ARRAY_SIZE(wm831x_bat_props);
563 		power->battery_desc.get_property = wm831x_bat_get_prop;
564 		power->battery_desc.use_for_apm = 1;
565 		power->battery = power_supply_register(&pdev->dev,
566 						       &power->battery_desc,
567 						       NULL);
568 		if (IS_ERR(power->battery)) {
569 			ret = PTR_ERR(power->battery);
570 			goto err_usb;
571 		}
572 	}
573 
574 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
575 	ret = request_threaded_irq(irq, NULL, wm831x_syslo_irq,
576 				   IRQF_TRIGGER_RISING | IRQF_ONESHOT, "System power low",
577 				   power);
578 	if (ret != 0) {
579 		dev_err(&pdev->dev, "Failed to request SYSLO IRQ %d: %d\n",
580 			irq, ret);
581 		goto err_battery;
582 	}
583 
584 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
585 	ret = request_threaded_irq(irq, NULL, wm831x_pwr_src_irq,
586 				   IRQF_TRIGGER_RISING | IRQF_ONESHOT, "Power source",
587 				   power);
588 	if (ret != 0) {
589 		dev_err(&pdev->dev, "Failed to request PWR SRC IRQ %d: %d\n",
590 			irq, ret);
591 		goto err_syslo;
592 	}
593 
594 	for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
595 		irq = wm831x_irq(wm831x,
596 				 platform_get_irq_byname(pdev,
597 							 wm831x_bat_irqs[i]));
598 		ret = request_threaded_irq(irq, NULL, wm831x_bat_irq,
599 					   IRQF_TRIGGER_RISING | IRQF_ONESHOT,
600 					   wm831x_bat_irqs[i],
601 					   power);
602 		if (ret != 0) {
603 			dev_err(&pdev->dev,
604 				"Failed to request %s IRQ %d: %d\n",
605 				wm831x_bat_irqs[i], irq, ret);
606 			goto err_bat_irq;
607 		}
608 	}
609 
610 	return ret;
611 
612 err_bat_irq:
613 	--i;
614 	for (; i >= 0; i--) {
615 		irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
616 		free_irq(irq, power);
617 	}
618 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
619 	free_irq(irq, power);
620 err_syslo:
621 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
622 	free_irq(irq, power);
623 err_battery:
624 	if (power->have_battery)
625 		power_supply_unregister(power->battery);
626 err_usb:
627 	power_supply_unregister(power->usb);
628 err_wall:
629 	power_supply_unregister(power->wall);
630 err:
631 	return ret;
632 }
633 
634 static int wm831x_power_remove(struct platform_device *pdev)
635 {
636 	struct wm831x_power *wm831x_power = platform_get_drvdata(pdev);
637 	struct wm831x *wm831x = wm831x_power->wm831x;
638 	int irq, i;
639 
640 	for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
641 		irq = wm831x_irq(wm831x,
642 				 platform_get_irq_byname(pdev,
643 							 wm831x_bat_irqs[i]));
644 		free_irq(irq, wm831x_power);
645 	}
646 
647 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
648 	free_irq(irq, wm831x_power);
649 
650 	irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
651 	free_irq(irq, wm831x_power);
652 
653 	if (wm831x_power->have_battery)
654 		power_supply_unregister(wm831x_power->battery);
655 	power_supply_unregister(wm831x_power->wall);
656 	power_supply_unregister(wm831x_power->usb);
657 	return 0;
658 }
659 
660 static struct platform_driver wm831x_power_driver = {
661 	.probe = wm831x_power_probe,
662 	.remove = wm831x_power_remove,
663 	.driver = {
664 		.name = "wm831x-power",
665 	},
666 };
667 
668 module_platform_driver(wm831x_power_driver);
669 
670 MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
671 MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
672 MODULE_LICENSE("GPL");
673 MODULE_ALIAS("platform:wm831x-power");
674