blob: 03a37fd6be274433e2e3229aa0fb4fc59c976f92 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Anton Vorontsovb2998042007-05-04 00:32:17 +04002/*
3 * Common power driver for PDAs and phones with one or two external
4 * power supplies (AC/USB) connected to main and backup batteries,
5 * and optional builtin charger.
6 *
7 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
Anton Vorontsovb2998042007-05-04 00:32:17 +04008 */
9
10#include <linux/module.h>
11#include <linux/platform_device.h>
Philipp Zabel5bf2b992009-01-18 17:40:27 +010012#include <linux/err.h>
Anton Vorontsovb2998042007-05-04 00:32:17 +040013#include <linux/interrupt.h>
Dima Zavin9ad63982011-07-10 16:01:15 -070014#include <linux/notifier.h>
Anton Vorontsovb2998042007-05-04 00:32:17 +040015#include <linux/power_supply.h>
16#include <linux/pda_power.h>
Philipp Zabel5bf2b992009-01-18 17:40:27 +010017#include <linux/regulator/consumer.h>
Anton Vorontsovb2998042007-05-04 00:32:17 +040018#include <linux/timer.h>
19#include <linux/jiffies.h>
Philipp Zabel5bf2b992009-01-18 17:40:27 +010020#include <linux/usb/otg.h>
Anton Vorontsovb2998042007-05-04 00:32:17 +040021
22static inline unsigned int get_irq_flags(struct resource *res)
23{
Theodore Ts'od554a3f2012-07-17 13:40:18 -040024 return IRQF_SHARED | (res->flags & IRQF_TRIGGER_MASK);
Anton Vorontsovb2998042007-05-04 00:32:17 +040025}
26
27static struct device *dev;
28static struct pda_power_pdata *pdata;
29static struct resource *ac_irq, *usb_irq;
Michael Trimarchi633e8792017-04-25 15:18:05 +020030static struct delayed_work charger_work;
31static struct delayed_work polling_work;
32static struct delayed_work supply_work;
Anton Vorontsovc3caeba2008-01-13 02:44:20 +030033static int polling;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010034static struct power_supply *pda_psy_ac, *pda_psy_usb;
Anton Vorontsovb2998042007-05-04 00:32:17 +040035
Felipe Balbi820d0882013-03-07 11:23:50 +020036#if IS_ENABLED(CONFIG_USB_PHY)
Heikki Krogerus86753812012-02-13 13:24:02 +020037static struct usb_phy *transceiver;
Dima Zavin9ad63982011-07-10 16:01:15 -070038static struct notifier_block otg_nb;
Axel Lin1c745292011-08-23 20:34:33 +080039#endif
40
Philipp Zabel5bf2b992009-01-18 17:40:27 +010041static struct regulator *ac_draw;
42
Anton Vorontsovbfde2662008-01-13 02:39:17 +030043enum {
44 PDA_PSY_OFFLINE = 0,
45 PDA_PSY_ONLINE = 1,
46 PDA_PSY_TO_CHANGE,
47};
48static int new_ac_status = -1;
49static int new_usb_status = -1;
50static int ac_status = -1;
51static int usb_status = -1;
52
Anton Vorontsovb2998042007-05-04 00:32:17 +040053static int pda_power_get_property(struct power_supply *psy,
54 enum power_supply_property psp,
55 union power_supply_propval *val)
56{
57 switch (psp) {
58 case POWER_SUPPLY_PROP_ONLINE:
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010059 if (psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
Anton Vorontsovb2998042007-05-04 00:32:17 +040060 val->intval = pdata->is_ac_online ?
61 pdata->is_ac_online() : 0;
62 else
63 val->intval = pdata->is_usb_online ?
64 pdata->is_usb_online() : 0;
65 break;
66 default:
67 return -EINVAL;
68 }
69 return 0;
70}
71
72static enum power_supply_property pda_power_props[] = {
73 POWER_SUPPLY_PROP_ONLINE,
74};
75
76static char *pda_power_supplied_to[] = {
77 "main-battery",
78 "backup-battery",
79};
80
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010081static const struct power_supply_desc pda_psy_ac_desc = {
Anton Vorontsovbfde2662008-01-13 02:39:17 +030082 .name = "ac",
83 .type = POWER_SUPPLY_TYPE_MAINS,
Anton Vorontsovbfde2662008-01-13 02:39:17 +030084 .properties = pda_power_props,
85 .num_properties = ARRAY_SIZE(pda_power_props),
86 .get_property = pda_power_get_property,
Anton Vorontsovb2998042007-05-04 00:32:17 +040087};
88
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +010089static const struct power_supply_desc pda_psy_usb_desc = {
Anton Vorontsovbfde2662008-01-13 02:39:17 +030090 .name = "usb",
91 .type = POWER_SUPPLY_TYPE_USB,
Anton Vorontsovbfde2662008-01-13 02:39:17 +030092 .properties = pda_power_props,
93 .num_properties = ARRAY_SIZE(pda_power_props),
94 .get_property = pda_power_get_property,
95};
96
97static void update_status(void)
98{
99 if (pdata->is_ac_online)
100 new_ac_status = !!pdata->is_ac_online();
101
102 if (pdata->is_usb_online)
103 new_usb_status = !!pdata->is_usb_online();
104}
105
Anton Vorontsovb2998042007-05-04 00:32:17 +0400106static void update_charger(void)
107{
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100108 static int regulator_enabled;
109 int max_uA = pdata->ac_max_uA;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400110
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100111 if (pdata->set_charge) {
112 if (new_ac_status > 0) {
113 dev_dbg(dev, "charger on (AC)\n");
114 pdata->set_charge(PDA_POWER_CHARGE_AC);
115 } else if (new_usb_status > 0) {
116 dev_dbg(dev, "charger on (USB)\n");
117 pdata->set_charge(PDA_POWER_CHARGE_USB);
118 } else {
119 dev_dbg(dev, "charger off\n");
120 pdata->set_charge(0);
121 }
122 } else if (ac_draw) {
123 if (new_ac_status > 0) {
124 regulator_set_current_limit(ac_draw, max_uA, max_uA);
125 if (!regulator_enabled) {
126 dev_dbg(dev, "charger on (AC)\n");
Mark Brown92311c32012-06-12 09:46:26 +0800127 WARN_ON(regulator_enable(ac_draw));
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100128 regulator_enabled = 1;
129 }
130 } else {
131 if (regulator_enabled) {
132 dev_dbg(dev, "charger off\n");
Mark Brown92311c32012-06-12 09:46:26 +0800133 WARN_ON(regulator_disable(ac_draw));
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100134 regulator_enabled = 0;
135 }
136 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400137 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400138}
139
Michael Trimarchi633e8792017-04-25 15:18:05 +0200140static void supply_work_func(struct work_struct *work)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400141{
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300142 if (ac_status == PDA_PSY_TO_CHANGE) {
143 ac_status = new_ac_status;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100144 power_supply_changed(pda_psy_ac);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300145 }
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400146
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300147 if (usb_status == PDA_PSY_TO_CHANGE) {
148 usb_status = new_usb_status;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100149 power_supply_changed(pda_psy_usb);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300150 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400151}
152
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300153static void psy_changed(void)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400154{
155 update_charger();
156
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300157 /*
158 * Okay, charger set. Now wait a bit before notifying supplicants,
159 * charge power should stabilize.
160 */
Michael Trimarchi633e8792017-04-25 15:18:05 +0200161 cancel_delayed_work(&supply_work);
162 schedule_delayed_work(&supply_work,
163 msecs_to_jiffies(pdata->wait_for_charger));
Anton Vorontsovb2998042007-05-04 00:32:17 +0400164}
165
Michael Trimarchi633e8792017-04-25 15:18:05 +0200166static void charger_work_func(struct work_struct *work)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300167{
168 update_status();
169 psy_changed();
170}
171
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400172static irqreturn_t power_changed_isr(int irq, void *power_supply)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400173{
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100174 if (power_supply == pda_psy_ac)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300175 ac_status = PDA_PSY_TO_CHANGE;
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100176 else if (power_supply == pda_psy_usb)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300177 usb_status = PDA_PSY_TO_CHANGE;
178 else
179 return IRQ_NONE;
180
181 /*
182 * Wait a bit before reading ac/usb line status and setting charger,
183 * because ac/usb status readings may lag from irq.
184 */
Michael Trimarchi633e8792017-04-25 15:18:05 +0200185 cancel_delayed_work(&charger_work);
186 schedule_delayed_work(&charger_work,
187 msecs_to_jiffies(pdata->wait_for_status));
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300188
Anton Vorontsovb2998042007-05-04 00:32:17 +0400189 return IRQ_HANDLED;
190}
191
Michael Trimarchi633e8792017-04-25 15:18:05 +0200192static void polling_work_func(struct work_struct *work)
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300193{
194 int changed = 0;
195
196 dev_dbg(dev, "polling...\n");
197
198 update_status();
199
200 if (!ac_irq && new_ac_status != ac_status) {
201 ac_status = PDA_PSY_TO_CHANGE;
202 changed = 1;
203 }
204
205 if (!usb_irq && new_usb_status != usb_status) {
206 usb_status = PDA_PSY_TO_CHANGE;
207 changed = 1;
208 }
209
210 if (changed)
211 psy_changed();
212
Michael Trimarchi633e8792017-04-25 15:18:05 +0200213 cancel_delayed_work(&polling_work);
214 schedule_delayed_work(&polling_work,
215 msecs_to_jiffies(pdata->polling_interval));
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300216}
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 */
Michael Trimarchi633e8792017-04-25 15:18:05 +0200253 cancel_delayed_work(&charger_work);
254 schedule_delayed_work(&charger_work,
255 msecs_to_jiffies(pdata->wait_for_status));
Dima Zavin9ad63982011-07-10 16:01:15 -0700256
257 return NOTIFY_OK;
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100258}
259#endif
260
Anton Vorontsovb2998042007-05-04 00:32:17 +0400261static int pda_power_probe(struct platform_device *pdev)
262{
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100263 struct power_supply_config psy_cfg = {};
Anton Vorontsovb2998042007-05-04 00:32:17 +0400264 int ret = 0;
265
266 dev = &pdev->dev;
267
268 if (pdev->id != -1) {
269 dev_err(dev, "it's meaningless to register several "
270 "pda_powers; use id = -1\n");
271 ret = -EINVAL;
272 goto wrongid;
273 }
274
275 pdata = pdev->dev.platform_data;
276
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200277 if (pdata->init) {
278 ret = pdata->init(dev);
279 if (ret < 0)
280 goto init_failed;
281 }
282
Paul Parsonsec60ea52012-09-20 14:26:05 -0700283 ac_draw = regulator_get(dev, "ac_draw");
284 if (IS_ERR(ac_draw)) {
285 dev_dbg(dev, "couldn't get ac_draw regulator\n");
286 ac_draw = NULL;
Paul Parsonsec60ea52012-09-20 14:26:05 -0700287 }
288
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300289 update_status();
Anton Vorontsovb2998042007-05-04 00:32:17 +0400290 update_charger();
291
292 if (!pdata->wait_for_status)
293 pdata->wait_for_status = 500;
294
295 if (!pdata->wait_for_charger)
296 pdata->wait_for_charger = 500;
297
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300298 if (!pdata->polling_interval)
299 pdata->polling_interval = 2000;
300
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100301 if (!pdata->ac_max_uA)
302 pdata->ac_max_uA = 500000;
303
Michael Trimarchi633e8792017-04-25 15:18:05 +0200304 INIT_DELAYED_WORK(&charger_work, charger_work_func);
305 INIT_DELAYED_WORK(&supply_work, supply_work_func);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400306
307 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
308 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
Anton Vorontsovb2998042007-05-04 00:32:17 +0400309
310 if (pdata->supplied_to) {
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100311 psy_cfg.supplied_to = pdata->supplied_to;
312 psy_cfg.num_supplicants = pdata->num_supplicants;
313 } else {
314 psy_cfg.supplied_to = pda_power_supplied_to;
315 psy_cfg.num_supplicants = ARRAY_SIZE(pda_power_supplied_to);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400316 }
317
Felipe Balbi820d0882013-03-07 11:23:50 +0200318#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530319 transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530320 if (!IS_ERR_OR_NULL(transceiver)) {
321 if (!pdata->is_usb_online)
322 pdata->is_usb_online = otg_is_usb_online;
323 if (!pdata->is_ac_online)
324 pdata->is_ac_online = otg_is_ac_online;
Dima Zavin9ad63982011-07-10 16:01:15 -0700325 }
Axel Lin1c745292011-08-23 20:34:33 +0800326#endif
Dima Zavin9ad63982011-07-10 16:01:15 -0700327
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300328 if (pdata->is_ac_online) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100329 pda_psy_ac = power_supply_register(&pdev->dev,
330 &pda_psy_ac_desc, &psy_cfg);
331 if (IS_ERR(pda_psy_ac)) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300332 dev_err(dev, "failed to register %s power supply\n",
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100333 pda_psy_ac_desc.name);
334 ret = PTR_ERR(pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300335 goto ac_supply_failed;
336 }
337
338 if (ac_irq) {
339 ret = request_irq(ac_irq->start, power_changed_isr,
340 get_irq_flags(ac_irq), ac_irq->name,
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100341 pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300342 if (ret) {
343 dev_err(dev, "request ac irq failed\n");
344 goto ac_irq_failed;
345 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300346 } else {
347 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400348 }
349 }
350
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300351 if (pdata->is_usb_online) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100352 pda_psy_usb = power_supply_register(&pdev->dev,
353 &pda_psy_usb_desc,
354 &psy_cfg);
355 if (IS_ERR(pda_psy_usb)) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300356 dev_err(dev, "failed to register %s power supply\n",
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100357 pda_psy_usb_desc.name);
358 ret = PTR_ERR(pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300359 goto usb_supply_failed;
360 }
361
362 if (usb_irq) {
363 ret = request_irq(usb_irq->start, power_changed_isr,
364 get_irq_flags(usb_irq),
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100365 usb_irq->name, pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300366 if (ret) {
367 dev_err(dev, "request usb irq failed\n");
368 goto usb_irq_failed;
369 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300370 } else {
371 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400372 }
373 }
374
Felipe Balbi820d0882013-03-07 11:23:50 +0200375#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530376 if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) {
Dima Zavin9ad63982011-07-10 16:01:15 -0700377 otg_nb.notifier_call = otg_handle_notification;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200378 ret = usb_register_notifier(transceiver, &otg_nb);
Dima Zavin9ad63982011-07-10 16:01:15 -0700379 if (ret) {
380 dev_err(dev, "failure to register otg notifier\n");
381 goto otg_reg_notifier_failed;
382 }
383 polling = 0;
384 }
Axel Lin1c745292011-08-23 20:34:33 +0800385#endif
Dima Zavin9ad63982011-07-10 16:01:15 -0700386
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300387 if (polling) {
388 dev_dbg(dev, "will poll for status\n");
Michael Trimarchi633e8792017-04-25 15:18:05 +0200389 INIT_DELAYED_WORK(&polling_work, polling_work_func);
390 cancel_delayed_work(&polling_work);
391 schedule_delayed_work(&polling_work,
392 msecs_to_jiffies(pdata->polling_interval));
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300393 }
394
395 if (ac_irq || usb_irq)
396 device_init_wakeup(&pdev->dev, 1);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300397
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300398 return 0;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400399
Felipe Balbi820d0882013-03-07 11:23:50 +0200400#if IS_ENABLED(CONFIG_USB_PHY)
Dima Zavin9ad63982011-07-10 16:01:15 -0700401otg_reg_notifier_failed:
402 if (pdata->is_usb_online && usb_irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100403 free_irq(usb_irq->start, pda_psy_usb);
Axel Lin1c745292011-08-23 20:34:33 +0800404#endif
Anton Vorontsovb2998042007-05-04 00:32:17 +0400405usb_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300406 if (pdata->is_usb_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100407 power_supply_unregister(pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300408usb_supply_failed:
409 if (pdata->is_ac_online && ac_irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100410 free_irq(ac_irq->start, pda_psy_ac);
Felipe Balbi820d0882013-03-07 11:23:50 +0200411#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530412 if (!IS_ERR_OR_NULL(transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530413 usb_put_phy(transceiver);
Axel Lin1c745292011-08-23 20:34:33 +0800414#endif
Anton Vorontsovb2998042007-05-04 00:32:17 +0400415ac_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300416 if (pdata->is_ac_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100417 power_supply_unregister(pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300418ac_supply_failed:
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100419 if (ac_draw) {
420 regulator_put(ac_draw);
421 ac_draw = NULL;
422 }
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200423 if (pdata->exit)
424 pdata->exit(dev);
425init_failed:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400426wrongid:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400427 return ret;
428}
429
430static int pda_power_remove(struct platform_device *pdev)
431{
Chuhong Yuandd04def2019-11-15 14:25:15 +0800432#if IS_ENABLED(CONFIG_USB_PHY)
433 if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier)
434 usb_unregister_notifier(transceiver, &otg_nb);
435#endif
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300436 if (pdata->is_usb_online && usb_irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100437 free_irq(usb_irq->start, pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300438 if (pdata->is_ac_online && ac_irq)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100439 free_irq(ac_irq->start, pda_psy_ac);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300440
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300441 if (polling)
Michael Trimarchi633e8792017-04-25 15:18:05 +0200442 cancel_delayed_work_sync(&polling_work);
443 cancel_delayed_work_sync(&charger_work);
444 cancel_delayed_work_sync(&supply_work);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300445
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300446 if (pdata->is_usb_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100447 power_supply_unregister(pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300448 if (pdata->is_ac_online)
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100449 power_supply_unregister(pda_psy_ac);
Felipe Balbi820d0882013-03-07 11:23:50 +0200450#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530451 if (!IS_ERR_OR_NULL(transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530452 usb_put_phy(transceiver);
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100453#endif
454 if (ac_draw) {
455 regulator_put(ac_draw);
456 ac_draw = NULL;
457 }
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200458 if (pdata->exit)
459 pdata->exit(dev);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300460
Anton Vorontsovb2998042007-05-04 00:32:17 +0400461 return 0;
462}
463
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300464#ifdef CONFIG_PM
Robert Jarzmike82374f2008-08-11 22:22:27 +0200465static int ac_wakeup_enabled;
466static int usb_wakeup_enabled;
467
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300468static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
469{
Daniel Macka3bcbbe2010-04-12 23:33:22 +0200470 if (pdata->suspend) {
471 int ret = pdata->suspend(state);
472
473 if (ret)
474 return ret;
475 }
476
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300477 if (device_may_wakeup(&pdev->dev)) {
478 if (ac_irq)
Robert Jarzmike82374f2008-08-11 22:22:27 +0200479 ac_wakeup_enabled = !enable_irq_wake(ac_irq->start);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300480 if (usb_irq)
Robert Jarzmike82374f2008-08-11 22:22:27 +0200481 usb_wakeup_enabled = !enable_irq_wake(usb_irq->start);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300482 }
483
484 return 0;
485}
486
487static int pda_power_resume(struct platform_device *pdev)
488{
489 if (device_may_wakeup(&pdev->dev)) {
Robert Jarzmike82374f2008-08-11 22:22:27 +0200490 if (usb_irq && usb_wakeup_enabled)
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300491 disable_irq_wake(usb_irq->start);
Robert Jarzmike82374f2008-08-11 22:22:27 +0200492 if (ac_irq && ac_wakeup_enabled)
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300493 disable_irq_wake(ac_irq->start);
494 }
495
Daniel Macka3bcbbe2010-04-12 23:33:22 +0200496 if (pdata->resume)
497 return pdata->resume();
498
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300499 return 0;
500}
501#else
502#define pda_power_suspend NULL
503#define pda_power_resume NULL
504#endif /* CONFIG_PM */
505
Anton Vorontsovb2998042007-05-04 00:32:17 +0400506static struct platform_driver pda_power_pdrv = {
507 .driver = {
508 .name = "pda-power",
509 },
510 .probe = pda_power_probe,
511 .remove = pda_power_remove,
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300512 .suspend = pda_power_suspend,
513 .resume = pda_power_resume,
Anton Vorontsovb2998042007-05-04 00:32:17 +0400514};
515
Axel Lin300bac72011-11-26 12:01:10 +0800516module_platform_driver(pda_power_pdrv);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400517
Anton Vorontsovb2998042007-05-04 00:32:17 +0400518MODULE_LICENSE("GPL");
519MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
Axel Lin300bac72011-11-26 12:01:10 +0800520MODULE_ALIAS("platform:pda-power");