blob: 7082301da945baab5a708f594368f97ce1ec2513 [file] [log] [blame]
Mark Brown3961f7c2009-08-10 17:43:53 +01001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Mark Brown3961f7c2009-08-10 17:43:53 +010016
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
22struct wm831x_power {
23 struct wm831x *wm831x;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010024 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;
Mark Brownccf8fa22011-05-31 13:30:11 +010030 char wall_name[20];
31 char usb_name[20];
32 char battery_name[20];
Mark Brown35c3ae52011-08-16 08:37:42 +090033 bool have_battery;
Mark Brown3961f7c2009-08-10 17:43:53 +010034};
35
36static 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
53static 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 *********************************************************************/
69static int wm831x_wall_get_prop(struct power_supply *psy,
70 enum power_supply_property psp,
71 union power_supply_propval *val)
72{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010073 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
Mark Brown3961f7c2009-08-10 17:43:53 +010074 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
92static 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 *********************************************************************/
100static int wm831x_usb_get_prop(struct power_supply *psy,
101 enum power_supply_property psp,
102 union power_supply_propval *val)
103{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100104 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
Mark Brown3961f7c2009-08-10 17:43:53 +0100105 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
123static 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
132struct chg_map {
133 int val;
134 int reg_val;
135};
136
137static 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
144static 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
151static 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
170static 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
181static 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
200static 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
219static 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 |
Mark Brown3961f7c2009-08-10 17:43:53 +0100277 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
297static 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
331static 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
356static 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
395static int wm831x_bat_get_prop(struct power_supply *psy,
396 enum power_supply_property psp,
397 union power_supply_propval *val)
398{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100399 struct wm831x_power *wm831x_power = dev_get_drvdata(psy->dev.parent);
Mark Brown3961f7c2009-08-10 17:43:53 +0100400 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
428static 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
436static 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
447static 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 */
Mark Brown35c3ae52011-08-16 08:37:42 +0900456 if (wm831x_power->have_battery)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100457 power_supply_changed(wm831x_power->battery);
Mark Brown3961f7c2009-08-10 17:43:53 +0100458
459 return IRQ_HANDLED;
460}
461
462
463/*********************************************************************
Mark Brown3961f7c2009-08-10 17:43:53 +0100464 * Initialisation
465 *********************************************************************/
466
467static 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
479static 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
Mark Brownc26964e2009-10-01 15:41:06 +0100486 /* Just notify for everything - little harm in overnotifying. */
Mark Brown35c3ae52011-08-16 08:37:42 +0900487 if (wm831x_power->have_battery)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100488 power_supply_changed(wm831x_power->battery);
489 power_supply_changed(wm831x_power->usb);
490 power_supply_changed(wm831x_power->wall);
Mark Brown3961f7c2009-08-10 17:43:53 +0100491
492 return IRQ_HANDLED;
493}
494
Bill Pembertonc8afa642012-11-19 13:22:23 -0500495static int wm831x_power_probe(struct platform_device *pdev)
Mark Brown3961f7c2009-08-10 17:43:53 +0100496{
497 struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent);
Mark Brownccf8fa22011-05-31 13:30:11 +0100498 struct wm831x_pdata *wm831x_pdata = wm831x->dev->platform_data;
Mark Brown3961f7c2009-08-10 17:43:53 +0100499 struct wm831x_power *power;
Mark Brown3961f7c2009-08-10 17:43:53 +0100500 int ret, irq, i;
501
Mark Brownc01576c2015-08-20 15:12:43 -0700502 power = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_power),
503 GFP_KERNEL);
Mark Brown3961f7c2009-08-10 17:43:53 +0100504 if (power == NULL)
505 return -ENOMEM;
506
507 power->wm831x = wm831x;
508 platform_set_drvdata(pdev, power);
509
Mark Brownccf8fa22011-05-31 13:30:11 +0100510 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
Mark Brown3961f7c2009-08-10 17:43:53 +0100526 /* We ignore configuration failures since we can still read back
Mark Brownc26964e2009-10-01 15:41:06 +0100527 * the status without enabling the charger.
Mark Brown3961f7c2009-08-10 17:43:53 +0100528 */
529 wm831x_config_battery(wm831x);
Mark Brown3961f7c2009-08-10 17:43:53 +0100530
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100531 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);
Mark Brownc01576c2015-08-20 15:12:43 -0700540 goto err;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100541 }
Mark Brown3961f7c2009-08-10 17:43:53 +0100542
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100543 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);
Mark Brown35c3ae52011-08-16 08:37:42 +0900551 goto err_wall;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100552 }
Mark Brown35c3ae52011-08-16 08:37:42 +0900553
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) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100560 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 }
Mark Brown35c3ae52011-08-16 08:37:42 +0900572 }
Mark Brown3961f7c2009-08-10 17:43:53 +0100573
Mark Browncd997582012-05-14 23:14:24 +0200574 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
Mark Brownb5874f32010-03-10 18:27:40 +0000575 ret = request_threaded_irq(irq, NULL, wm831x_syslo_irq,
Valentin Rothberg90adf982015-09-22 19:00:40 +0200576 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "System power low",
Mark Brownb5874f32010-03-10 18:27:40 +0000577 power);
Mark Brown3961f7c2009-08-10 17:43:53 +0100578 if (ret != 0) {
579 dev_err(&pdev->dev, "Failed to request SYSLO IRQ %d: %d\n",
580 irq, ret);
Mark Brown35c3ae52011-08-16 08:37:42 +0900581 goto err_battery;
Mark Brown3961f7c2009-08-10 17:43:53 +0100582 }
583
Mark Browncd997582012-05-14 23:14:24 +0200584 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
Mark Brownb5874f32010-03-10 18:27:40 +0000585 ret = request_threaded_irq(irq, NULL, wm831x_pwr_src_irq,
Valentin Rothberg90adf982015-09-22 19:00:40 +0200586 IRQF_TRIGGER_RISING | IRQF_ONESHOT, "Power source",
Mark Brownb5874f32010-03-10 18:27:40 +0000587 power);
Mark Brown3961f7c2009-08-10 17:43:53 +0100588 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++) {
Mark Browncd997582012-05-14 23:14:24 +0200595 irq = wm831x_irq(wm831x,
596 platform_get_irq_byname(pdev,
597 wm831x_bat_irqs[i]));
Mark Brownb5874f32010-03-10 18:27:40 +0000598 ret = request_threaded_irq(irq, NULL, wm831x_bat_irq,
Valentin Rothberg90adf982015-09-22 19:00:40 +0200599 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
Mark Brownb5874f32010-03-10 18:27:40 +0000600 wm831x_bat_irqs[i],
601 power);
Mark Brown3961f7c2009-08-10 17:43:53 +0100602 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
612err_bat_irq:
Takeshi Yoshimura85a59652015-06-14 20:55:18 +0900613 --i;
Mark Brown3961f7c2009-08-10 17:43:53 +0100614 for (; i >= 0; i--) {
615 irq = platform_get_irq_byname(pdev, wm831x_bat_irqs[i]);
Mark Brownb5874f32010-03-10 18:27:40 +0000616 free_irq(irq, power);
Mark Brown3961f7c2009-08-10 17:43:53 +0100617 }
Mark Browncd997582012-05-14 23:14:24 +0200618 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
Mark Brownb5874f32010-03-10 18:27:40 +0000619 free_irq(irq, power);
Mark Brown3961f7c2009-08-10 17:43:53 +0100620err_syslo:
Mark Browncd997582012-05-14 23:14:24 +0200621 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
Mark Brownb5874f32010-03-10 18:27:40 +0000622 free_irq(irq, power);
Mark Brown35c3ae52011-08-16 08:37:42 +0900623err_battery:
624 if (power->have_battery)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100625 power_supply_unregister(power->battery);
Mark Brown3961f7c2009-08-10 17:43:53 +0100626err_usb:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100627 power_supply_unregister(power->usb);
Mark Brown3961f7c2009-08-10 17:43:53 +0100628err_wall:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100629 power_supply_unregister(power->wall);
Mark Brownc01576c2015-08-20 15:12:43 -0700630err:
Mark Brown3961f7c2009-08-10 17:43:53 +0100631 return ret;
632}
633
Bill Pemberton415ec692012-11-19 13:26:07 -0500634static int wm831x_power_remove(struct platform_device *pdev)
Mark Brown3961f7c2009-08-10 17:43:53 +0100635{
636 struct wm831x_power *wm831x_power = platform_get_drvdata(pdev);
Mark Browncd997582012-05-14 23:14:24 +0200637 struct wm831x *wm831x = wm831x_power->wm831x;
Mark Brown3961f7c2009-08-10 17:43:53 +0100638 int irq, i;
639
640 for (i = 0; i < ARRAY_SIZE(wm831x_bat_irqs); i++) {
Mark Browncd997582012-05-14 23:14:24 +0200641 irq = wm831x_irq(wm831x,
642 platform_get_irq_byname(pdev,
643 wm831x_bat_irqs[i]));
Mark Brownb5874f32010-03-10 18:27:40 +0000644 free_irq(irq, wm831x_power);
Mark Brown3961f7c2009-08-10 17:43:53 +0100645 }
646
Mark Browncd997582012-05-14 23:14:24 +0200647 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "PWR SRC"));
Mark Brownb5874f32010-03-10 18:27:40 +0000648 free_irq(irq, wm831x_power);
Mark Brown3961f7c2009-08-10 17:43:53 +0100649
Mark Browncd997582012-05-14 23:14:24 +0200650 irq = wm831x_irq(wm831x, platform_get_irq_byname(pdev, "SYSLO"));
Mark Brownb5874f32010-03-10 18:27:40 +0000651 free_irq(irq, wm831x_power);
Mark Brown3961f7c2009-08-10 17:43:53 +0100652
Mark Brown35c3ae52011-08-16 08:37:42 +0900653 if (wm831x_power->have_battery)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100654 power_supply_unregister(wm831x_power->battery);
655 power_supply_unregister(wm831x_power->wall);
656 power_supply_unregister(wm831x_power->usb);
Mark Brown3961f7c2009-08-10 17:43:53 +0100657 return 0;
658}
659
660static struct platform_driver wm831x_power_driver = {
661 .probe = wm831x_power_probe,
Bill Pemberton28ea73f2012-11-19 13:20:40 -0500662 .remove = wm831x_power_remove,
Mark Brown3961f7c2009-08-10 17:43:53 +0100663 .driver = {
664 .name = "wm831x-power",
665 },
666};
667
Axel Lin300bac72011-11-26 12:01:10 +0800668module_platform_driver(wm831x_power_driver);
Mark Brown3961f7c2009-08-10 17:43:53 +0100669
670MODULE_DESCRIPTION("Power supply driver for WM831x PMICs");
671MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>");
672MODULE_LICENSE("GPL");
673MODULE_ALIAS("platform:wm831x-power");