blob: dfe1ee89f7c7ad06fc014b3986c240904329e6ba [file] [log] [blame]
Anton Vorontsovb2998042007-05-04 00:32:17 +04001/*
2 * Common power driver for PDAs and phones with one or two external
3 * power supplies (AC/USB) connected to main and backup batteries,
4 * and optional builtin charger.
5 *
6 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 */
12
13#include <linux/module.h>
14#include <linux/platform_device.h>
Philipp Zabel5bf2b992009-01-18 17:40:27 +010015#include <linux/err.h>
Anton Vorontsovb2998042007-05-04 00:32:17 +040016#include <linux/interrupt.h>
Dima Zavin9ad63982011-07-10 16:01:15 -070017#include <linux/notifier.h>
Anton Vorontsovb2998042007-05-04 00:32:17 +040018#include <linux/power_supply.h>
19#include <linux/pda_power.h>
Philipp Zabel5bf2b992009-01-18 17:40:27 +010020#include <linux/regulator/consumer.h>
Anton Vorontsovb2998042007-05-04 00:32:17 +040021#include <linux/timer.h>
22#include <linux/jiffies.h>
Philipp Zabel5bf2b992009-01-18 17:40:27 +010023#include <linux/usb/otg.h>
Anton Vorontsovb2998042007-05-04 00:32:17 +040024
25static inline unsigned int get_irq_flags(struct resource *res)
26{
Theodore Ts'od554a3f2012-07-17 13:40:18 -040027 return IRQF_SHARED | (res->flags & IRQF_TRIGGER_MASK);
Anton Vorontsovb2998042007-05-04 00:32:17 +040028}
29
30static struct device *dev;
31static struct pda_power_pdata *pdata;
32static struct resource *ac_irq, *usb_irq;
33static struct timer_list charger_timer;
34static struct timer_list supply_timer;
Anton Vorontsovc3caeba2008-01-13 02:44:20 +030035static struct timer_list polling_timer;
36static int polling;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010037static struct power_supply *pda_psy_ac, *pda_psy_usb;
Anton Vorontsovb2998042007-05-04 00:32:17 +040038
Felipe Balbi820d0882013-03-07 11:23:50 +020039#if IS_ENABLED(CONFIG_USB_PHY)
Heikki Krogerus86753812012-02-13 13:24:02 +020040static struct usb_phy *transceiver;
Dima Zavin9ad63982011-07-10 16:01:15 -070041static struct notifier_block otg_nb;
Axel Lin1c745292011-08-23 20:34:33 +080042#endif
43
Philipp Zabel5bf2b992009-01-18 17:40:27 +010044static struct regulator *ac_draw;
45
Anton Vorontsovbfde2662008-01-13 02:39:17 +030046enum {
47 PDA_PSY_OFFLINE = 0,
48 PDA_PSY_ONLINE = 1,
49 PDA_PSY_TO_CHANGE,
50};
51static int new_ac_status = -1;
52static int new_usb_status = -1;
53static int ac_status = -1;
54static int usb_status = -1;
55
Anton Vorontsovb2998042007-05-04 00:32:17 +040056static int pda_power_get_property(struct power_supply *psy,
57 enum power_supply_property psp,
58 union power_supply_propval *val)
59{
60 switch (psp) {
61 case POWER_SUPPLY_PROP_ONLINE:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010062 if (psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
Anton Vorontsovb2998042007-05-04 00:32:17 +040063 val->intval = pdata->is_ac_online ?
64 pdata->is_ac_online() : 0;
65 else
66 val->intval = pdata->is_usb_online ?
67 pdata->is_usb_online() : 0;
68 break;
69 default:
70 return -EINVAL;
71 }
72 return 0;
73}
74
75static enum power_supply_property pda_power_props[] = {
76 POWER_SUPPLY_PROP_ONLINE,
77};
78
79static char *pda_power_supplied_to[] = {
80 "main-battery",
81 "backup-battery",
82};
83
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010084static const struct power_supply_desc pda_psy_ac_desc = {
Anton Vorontsovbfde2662008-01-13 02:39:17 +030085 .name = "ac",
86 .type = POWER_SUPPLY_TYPE_MAINS,
Anton Vorontsovbfde2662008-01-13 02:39:17 +030087 .properties = pda_power_props,
88 .num_properties = ARRAY_SIZE(pda_power_props),
89 .get_property = pda_power_get_property,
Anton Vorontsovb2998042007-05-04 00:32:17 +040090};
91
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010092static const struct power_supply_desc pda_psy_usb_desc = {
Anton Vorontsovbfde2662008-01-13 02:39:17 +030093 .name = "usb",
94 .type = POWER_SUPPLY_TYPE_USB,
Anton Vorontsovbfde2662008-01-13 02:39:17 +030095 .properties = pda_power_props,
96 .num_properties = ARRAY_SIZE(pda_power_props),
97 .get_property = pda_power_get_property,
98};
99
100static void update_status(void)
101{
102 if (pdata->is_ac_online)
103 new_ac_status = !!pdata->is_ac_online();
104
105 if (pdata->is_usb_online)
106 new_usb_status = !!pdata->is_usb_online();
107}
108
Anton Vorontsovb2998042007-05-04 00:32:17 +0400109static void update_charger(void)
110{
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100111 static int regulator_enabled;
112 int max_uA = pdata->ac_max_uA;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400113
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100114 if (pdata->set_charge) {
115 if (new_ac_status > 0) {
116 dev_dbg(dev, "charger on (AC)\n");
117 pdata->set_charge(PDA_POWER_CHARGE_AC);
118 } else if (new_usb_status > 0) {
119 dev_dbg(dev, "charger on (USB)\n");
120 pdata->set_charge(PDA_POWER_CHARGE_USB);
121 } else {
122 dev_dbg(dev, "charger off\n");
123 pdata->set_charge(0);
124 }
125 } else if (ac_draw) {
126 if (new_ac_status > 0) {
127 regulator_set_current_limit(ac_draw, max_uA, max_uA);
128 if (!regulator_enabled) {
129 dev_dbg(dev, "charger on (AC)\n");
Mark Brown92311c32012-06-12 09:46:26 +0800130 WARN_ON(regulator_enable(ac_draw));
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100131 regulator_enabled = 1;
132 }
133 } else {
134 if (regulator_enabled) {
135 dev_dbg(dev, "charger off\n");
Mark Brown92311c32012-06-12 09:46:26 +0800136 WARN_ON(regulator_disable(ac_draw));
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100137 regulator_enabled = 0;
138 }
139 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400140 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400141}
142
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300143static void supply_timer_func(unsigned long unused)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400144{
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300145 if (ac_status == PDA_PSY_TO_CHANGE) {
146 ac_status = new_ac_status;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100147 power_supply_changed(pda_psy_ac);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300148 }
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400149
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300150 if (usb_status == PDA_PSY_TO_CHANGE) {
151 usb_status = new_usb_status;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100152 power_supply_changed(pda_psy_usb);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300153 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400154}
155
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300156static void psy_changed(void)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400157{
158 update_charger();
159
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300160 /*
161 * Okay, charger set. Now wait a bit before notifying supplicants,
162 * charge power should stabilize.
163 */
Anton Vorontsovb2998042007-05-04 00:32:17 +0400164 mod_timer(&supply_timer,
165 jiffies + msecs_to_jiffies(pdata->wait_for_charger));
Anton Vorontsovb2998042007-05-04 00:32:17 +0400166}
167
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300168static void charger_timer_func(unsigned long unused)
169{
170 update_status();
171 psy_changed();
172}
173
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400174static irqreturn_t power_changed_isr(int irq, void *power_supply)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400175{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100176 if (power_supply == pda_psy_ac)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300177 ac_status = PDA_PSY_TO_CHANGE;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100178 else if (power_supply == pda_psy_usb)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300179 usb_status = PDA_PSY_TO_CHANGE;
180 else
181 return IRQ_NONE;
182
183 /*
184 * Wait a bit before reading ac/usb line status and setting charger,
185 * because ac/usb status readings may lag from irq.
186 */
Anton Vorontsovb2998042007-05-04 00:32:17 +0400187 mod_timer(&charger_timer,
188 jiffies + msecs_to_jiffies(pdata->wait_for_status));
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300189
Anton Vorontsovb2998042007-05-04 00:32:17 +0400190 return IRQ_HANDLED;
191}
192
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300193static void polling_timer_func(unsigned long unused)
194{
195 int changed = 0;
196
197 dev_dbg(dev, "polling...\n");
198
199 update_status();
200
201 if (!ac_irq && new_ac_status != ac_status) {
202 ac_status = PDA_PSY_TO_CHANGE;
203 changed = 1;
204 }
205
206 if (!usb_irq && new_usb_status != usb_status) {
207 usb_status = PDA_PSY_TO_CHANGE;
208 changed = 1;
209 }
210
211 if (changed)
212 psy_changed();
213
214 mod_timer(&polling_timer,
215 jiffies + msecs_to_jiffies(pdata->polling_interval));
216}
217
Felipe Balbi820d0882013-03-07 11:23:50 +0200218#if IS_ENABLED(CONFIG_USB_PHY)
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100219static int otg_is_usb_online(void)
220{
Dima Zavin9ad63982011-07-10 16:01:15 -0700221 return (transceiver->last_event == USB_EVENT_VBUS ||
222 transceiver->last_event == USB_EVENT_ENUMERATED);
223}
224
225static int otg_is_ac_online(void)
226{
227 return (transceiver->last_event == USB_EVENT_CHARGER);
228}
229
230static int otg_handle_notification(struct notifier_block *nb,
231 unsigned long event, void *unused)
232{
233 switch (event) {
234 case USB_EVENT_CHARGER:
235 ac_status = PDA_PSY_TO_CHANGE;
236 break;
237 case USB_EVENT_VBUS:
238 case USB_EVENT_ENUMERATED:
239 usb_status = PDA_PSY_TO_CHANGE;
240 break;
241 case USB_EVENT_NONE:
242 ac_status = PDA_PSY_TO_CHANGE;
243 usb_status = PDA_PSY_TO_CHANGE;
244 break;
245 default:
246 return NOTIFY_OK;
247 }
248
249 /*
250 * Wait a bit before reading ac/usb line status and setting charger,
251 * because ac/usb status readings may lag from irq.
252 */
253 mod_timer(&charger_timer,
254 jiffies + msecs_to_jiffies(pdata->wait_for_status));
255
256 return NOTIFY_OK;
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100257}
258#endif
259
Anton Vorontsovb2998042007-05-04 00:32:17 +0400260static int pda_power_probe(struct platform_device *pdev)
261{
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100262 struct power_supply_config psy_cfg = {};
Anton Vorontsovb2998042007-05-04 00:32:17 +0400263 int ret = 0;
264
265 dev = &pdev->dev;
266
267 if (pdev->id != -1) {
268 dev_err(dev, "it's meaningless to register several "
269 "pda_powers; use id = -1\n");
270 ret = -EINVAL;
271 goto wrongid;
272 }
273
274 pdata = pdev->dev.platform_data;
275
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200276 if (pdata->init) {
277 ret = pdata->init(dev);
278 if (ret < 0)
279 goto init_failed;
280 }
281
Paul Parsonsec60ea52012-09-20 14:26:05 -0700282 ac_draw = regulator_get(dev, "ac_draw");
283 if (IS_ERR(ac_draw)) {
284 dev_dbg(dev, "couldn't get ac_draw regulator\n");
285 ac_draw = NULL;
Paul Parsonsec60ea52012-09-20 14:26:05 -0700286 }
287
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300288 update_status();
Anton Vorontsovb2998042007-05-04 00:32:17 +0400289 update_charger();
290
291 if (!pdata->wait_for_status)
292 pdata->wait_for_status = 500;
293
294 if (!pdata->wait_for_charger)
295 pdata->wait_for_charger = 500;
296
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300297 if (!pdata->polling_interval)
298 pdata->polling_interval = 2000;
299
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100300 if (!pdata->ac_max_uA)
301 pdata->ac_max_uA = 500000;
302
Anton Vorontsovb2998042007-05-04 00:32:17 +0400303 setup_timer(&charger_timer, charger_timer_func, 0);
304 setup_timer(&supply_timer, supply_timer_func, 0);
305
306 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
307 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
Anton Vorontsovb2998042007-05-04 00:32:17 +0400308
309 if (pdata->supplied_to) {
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100310 psy_cfg.supplied_to = pdata->supplied_to;
311 psy_cfg.num_supplicants = pdata->num_supplicants;
312 } else {
313 psy_cfg.supplied_to = pda_power_supplied_to;
314 psy_cfg.num_supplicants = ARRAY_SIZE(pda_power_supplied_to);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400315 }
316
Felipe Balbi820d0882013-03-07 11:23:50 +0200317#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530318 transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530319 if (!IS_ERR_OR_NULL(transceiver)) {
320 if (!pdata->is_usb_online)
321 pdata->is_usb_online = otg_is_usb_online;
322 if (!pdata->is_ac_online)
323 pdata->is_ac_online = otg_is_ac_online;
Dima Zavin9ad63982011-07-10 16:01:15 -0700324 }
Axel Lin1c745292011-08-23 20:34:33 +0800325#endif
Dima Zavin9ad63982011-07-10 16:01:15 -0700326
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300327 if (pdata->is_ac_online) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100328 pda_psy_ac = power_supply_register(&pdev->dev,
329 &pda_psy_ac_desc, &psy_cfg);
330 if (IS_ERR(pda_psy_ac)) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300331 dev_err(dev, "failed to register %s power supply\n",
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100332 pda_psy_ac_desc.name);
333 ret = PTR_ERR(pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300334 goto ac_supply_failed;
335 }
336
337 if (ac_irq) {
338 ret = request_irq(ac_irq->start, power_changed_isr,
339 get_irq_flags(ac_irq), ac_irq->name,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100340 pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300341 if (ret) {
342 dev_err(dev, "request ac irq failed\n");
343 goto ac_irq_failed;
344 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300345 } else {
346 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400347 }
348 }
349
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300350 if (pdata->is_usb_online) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100351 pda_psy_usb = power_supply_register(&pdev->dev,
352 &pda_psy_usb_desc,
353 &psy_cfg);
354 if (IS_ERR(pda_psy_usb)) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300355 dev_err(dev, "failed to register %s power supply\n",
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100356 pda_psy_usb_desc.name);
357 ret = PTR_ERR(pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300358 goto usb_supply_failed;
359 }
360
361 if (usb_irq) {
362 ret = request_irq(usb_irq->start, power_changed_isr,
363 get_irq_flags(usb_irq),
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100364 usb_irq->name, pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300365 if (ret) {
366 dev_err(dev, "request usb irq failed\n");
367 goto usb_irq_failed;
368 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300369 } else {
370 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400371 }
372 }
373
Felipe Balbi820d0882013-03-07 11:23:50 +0200374#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530375 if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) {
Dima Zavin9ad63982011-07-10 16:01:15 -0700376 otg_nb.notifier_call = otg_handle_notification;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200377 ret = usb_register_notifier(transceiver, &otg_nb);
Dima Zavin9ad63982011-07-10 16:01:15 -0700378 if (ret) {
379 dev_err(dev, "failure to register otg notifier\n");
380 goto otg_reg_notifier_failed;
381 }
382 polling = 0;
383 }
Axel Lin1c745292011-08-23 20:34:33 +0800384#endif
Dima Zavin9ad63982011-07-10 16:01:15 -0700385
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300386 if (polling) {
387 dev_dbg(dev, "will poll for status\n");
388 setup_timer(&polling_timer, polling_timer_func, 0);
389 mod_timer(&polling_timer,
390 jiffies + msecs_to_jiffies(pdata->polling_interval));
391 }
392
393 if (ac_irq || usb_irq)
394 device_init_wakeup(&pdev->dev, 1);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300395
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300396 return 0;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400397
Felipe Balbi820d0882013-03-07 11:23:50 +0200398#if IS_ENABLED(CONFIG_USB_PHY)
Dima Zavin9ad63982011-07-10 16:01:15 -0700399otg_reg_notifier_failed:
400 if (pdata->is_usb_online && usb_irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100401 free_irq(usb_irq->start, pda_psy_usb);
Axel Lin1c745292011-08-23 20:34:33 +0800402#endif
Anton Vorontsovb2998042007-05-04 00:32:17 +0400403usb_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300404 if (pdata->is_usb_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100405 power_supply_unregister(pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300406usb_supply_failed:
407 if (pdata->is_ac_online && ac_irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100408 free_irq(ac_irq->start, pda_psy_ac);
Felipe Balbi820d0882013-03-07 11:23:50 +0200409#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530410 if (!IS_ERR_OR_NULL(transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530411 usb_put_phy(transceiver);
Axel Lin1c745292011-08-23 20:34:33 +0800412#endif
Anton Vorontsovb2998042007-05-04 00:32:17 +0400413ac_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300414 if (pdata->is_ac_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100415 power_supply_unregister(pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300416ac_supply_failed:
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100417 if (ac_draw) {
418 regulator_put(ac_draw);
419 ac_draw = NULL;
420 }
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200421 if (pdata->exit)
422 pdata->exit(dev);
423init_failed:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400424wrongid:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400425 return ret;
426}
427
428static int pda_power_remove(struct platform_device *pdev)
429{
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300430 if (pdata->is_usb_online && usb_irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100431 free_irq(usb_irq->start, pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300432 if (pdata->is_ac_online && ac_irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100433 free_irq(ac_irq->start, pda_psy_ac);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300434
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300435 if (polling)
436 del_timer_sync(&polling_timer);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400437 del_timer_sync(&charger_timer);
438 del_timer_sync(&supply_timer);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300439
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300440 if (pdata->is_usb_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100441 power_supply_unregister(pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300442 if (pdata->is_ac_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100443 power_supply_unregister(pda_psy_ac);
Felipe Balbi820d0882013-03-07 11:23:50 +0200444#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530445 if (!IS_ERR_OR_NULL(transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530446 usb_put_phy(transceiver);
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100447#endif
448 if (ac_draw) {
449 regulator_put(ac_draw);
450 ac_draw = NULL;
451 }
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200452 if (pdata->exit)
453 pdata->exit(dev);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300454
Anton Vorontsovb2998042007-05-04 00:32:17 +0400455 return 0;
456}
457
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300458#ifdef CONFIG_PM
Robert Jarzmike82374f2008-08-11 22:22:27 +0200459static int ac_wakeup_enabled;
460static int usb_wakeup_enabled;
461
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300462static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
463{
Daniel Macka3bcbbe2010-04-12 23:33:22 +0200464 if (pdata->suspend) {
465 int ret = pdata->suspend(state);
466
467 if (ret)
468 return ret;
469 }
470
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300471 if (device_may_wakeup(&pdev->dev)) {
472 if (ac_irq)
Robert Jarzmike82374f2008-08-11 22:22:27 +0200473 ac_wakeup_enabled = !enable_irq_wake(ac_irq->start);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300474 if (usb_irq)
Robert Jarzmike82374f2008-08-11 22:22:27 +0200475 usb_wakeup_enabled = !enable_irq_wake(usb_irq->start);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300476 }
477
478 return 0;
479}
480
481static int pda_power_resume(struct platform_device *pdev)
482{
483 if (device_may_wakeup(&pdev->dev)) {
Robert Jarzmike82374f2008-08-11 22:22:27 +0200484 if (usb_irq && usb_wakeup_enabled)
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300485 disable_irq_wake(usb_irq->start);
Robert Jarzmike82374f2008-08-11 22:22:27 +0200486 if (ac_irq && ac_wakeup_enabled)
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300487 disable_irq_wake(ac_irq->start);
488 }
489
Daniel Macka3bcbbe2010-04-12 23:33:22 +0200490 if (pdata->resume)
491 return pdata->resume();
492
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300493 return 0;
494}
495#else
496#define pda_power_suspend NULL
497#define pda_power_resume NULL
498#endif /* CONFIG_PM */
499
Anton Vorontsovb2998042007-05-04 00:32:17 +0400500static struct platform_driver pda_power_pdrv = {
501 .driver = {
502 .name = "pda-power",
503 },
504 .probe = pda_power_probe,
505 .remove = pda_power_remove,
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300506 .suspend = pda_power_suspend,
507 .resume = pda_power_resume,
Anton Vorontsovb2998042007-05-04 00:32:17 +0400508};
509
Axel Lin300bac72011-11-26 12:01:10 +0800510module_platform_driver(pda_power_pdrv);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400511
Anton Vorontsovb2998042007-05-04 00:32:17 +0400512MODULE_LICENSE("GPL");
513MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
Axel Lin300bac72011-11-26 12:01:10 +0800514MODULE_ALIAS("platform:pda-power");