blob: 0c52e2a0d90cdba166a5cdfda72f70d86f5a2572 [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;
Anton Vorontsovb2998042007-05-04 00:32:17 +040037
Felipe Balbi820d0882013-03-07 11:23:50 +020038#if IS_ENABLED(CONFIG_USB_PHY)
Heikki Krogerus86753812012-02-13 13:24:02 +020039static struct usb_phy *transceiver;
Dima Zavin9ad63982011-07-10 16:01:15 -070040static struct notifier_block otg_nb;
Axel Lin1c745292011-08-23 20:34:33 +080041#endif
42
Philipp Zabel5bf2b992009-01-18 17:40:27 +010043static struct regulator *ac_draw;
44
Anton Vorontsovbfde2662008-01-13 02:39:17 +030045enum {
46 PDA_PSY_OFFLINE = 0,
47 PDA_PSY_ONLINE = 1,
48 PDA_PSY_TO_CHANGE,
49};
50static int new_ac_status = -1;
51static int new_usb_status = -1;
52static int ac_status = -1;
53static int usb_status = -1;
54
Anton Vorontsovb2998042007-05-04 00:32:17 +040055static int pda_power_get_property(struct power_supply *psy,
56 enum power_supply_property psp,
57 union power_supply_propval *val)
58{
59 switch (psp) {
60 case POWER_SUPPLY_PROP_ONLINE:
61 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
62 val->intval = pdata->is_ac_online ?
63 pdata->is_ac_online() : 0;
64 else
65 val->intval = pdata->is_usb_online ?
66 pdata->is_usb_online() : 0;
67 break;
68 default:
69 return -EINVAL;
70 }
71 return 0;
72}
73
74static enum power_supply_property pda_power_props[] = {
75 POWER_SUPPLY_PROP_ONLINE,
76};
77
78static char *pda_power_supplied_to[] = {
79 "main-battery",
80 "backup-battery",
81};
82
Anton Vorontsovbfde2662008-01-13 02:39:17 +030083static struct power_supply pda_psy_ac = {
84 .name = "ac",
85 .type = POWER_SUPPLY_TYPE_MAINS,
86 .supplied_to = pda_power_supplied_to,
87 .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
88 .properties = pda_power_props,
89 .num_properties = ARRAY_SIZE(pda_power_props),
90 .get_property = pda_power_get_property,
Anton Vorontsovb2998042007-05-04 00:32:17 +040091};
92
Anton Vorontsovbfde2662008-01-13 02:39:17 +030093static struct power_supply pda_psy_usb = {
94 .name = "usb",
95 .type = POWER_SUPPLY_TYPE_USB,
96 .supplied_to = pda_power_supplied_to,
97 .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
98 .properties = pda_power_props,
99 .num_properties = ARRAY_SIZE(pda_power_props),
100 .get_property = pda_power_get_property,
101};
102
103static void update_status(void)
104{
105 if (pdata->is_ac_online)
106 new_ac_status = !!pdata->is_ac_online();
107
108 if (pdata->is_usb_online)
109 new_usb_status = !!pdata->is_usb_online();
110}
111
Anton Vorontsovb2998042007-05-04 00:32:17 +0400112static void update_charger(void)
113{
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100114 static int regulator_enabled;
115 int max_uA = pdata->ac_max_uA;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400116
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100117 if (pdata->set_charge) {
118 if (new_ac_status > 0) {
119 dev_dbg(dev, "charger on (AC)\n");
120 pdata->set_charge(PDA_POWER_CHARGE_AC);
121 } else if (new_usb_status > 0) {
122 dev_dbg(dev, "charger on (USB)\n");
123 pdata->set_charge(PDA_POWER_CHARGE_USB);
124 } else {
125 dev_dbg(dev, "charger off\n");
126 pdata->set_charge(0);
127 }
128 } else if (ac_draw) {
129 if (new_ac_status > 0) {
130 regulator_set_current_limit(ac_draw, max_uA, max_uA);
131 if (!regulator_enabled) {
132 dev_dbg(dev, "charger on (AC)\n");
Mark Brown92311c32012-06-12 09:46:26 +0800133 WARN_ON(regulator_enable(ac_draw));
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100134 regulator_enabled = 1;
135 }
136 } else {
137 if (regulator_enabled) {
138 dev_dbg(dev, "charger off\n");
Mark Brown92311c32012-06-12 09:46:26 +0800139 WARN_ON(regulator_disable(ac_draw));
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100140 regulator_enabled = 0;
141 }
142 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400143 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400144}
145
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300146static void supply_timer_func(unsigned long unused)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400147{
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300148 if (ac_status == PDA_PSY_TO_CHANGE) {
149 ac_status = new_ac_status;
150 power_supply_changed(&pda_psy_ac);
151 }
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400152
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300153 if (usb_status == PDA_PSY_TO_CHANGE) {
154 usb_status = new_usb_status;
155 power_supply_changed(&pda_psy_usb);
156 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400157}
158
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300159static void psy_changed(void)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400160{
161 update_charger();
162
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300163 /*
164 * Okay, charger set. Now wait a bit before notifying supplicants,
165 * charge power should stabilize.
166 */
Anton Vorontsovb2998042007-05-04 00:32:17 +0400167 mod_timer(&supply_timer,
168 jiffies + msecs_to_jiffies(pdata->wait_for_charger));
Anton Vorontsovb2998042007-05-04 00:32:17 +0400169}
170
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300171static void charger_timer_func(unsigned long unused)
172{
173 update_status();
174 psy_changed();
175}
176
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400177static irqreturn_t power_changed_isr(int irq, void *power_supply)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400178{
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300179 if (power_supply == &pda_psy_ac)
180 ac_status = PDA_PSY_TO_CHANGE;
181 else if (power_supply == &pda_psy_usb)
182 usb_status = PDA_PSY_TO_CHANGE;
183 else
184 return IRQ_NONE;
185
186 /*
187 * Wait a bit before reading ac/usb line status and setting charger,
188 * because ac/usb status readings may lag from irq.
189 */
Anton Vorontsovb2998042007-05-04 00:32:17 +0400190 mod_timer(&charger_timer,
191 jiffies + msecs_to_jiffies(pdata->wait_for_status));
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300192
Anton Vorontsovb2998042007-05-04 00:32:17 +0400193 return IRQ_HANDLED;
194}
195
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300196static void polling_timer_func(unsigned long unused)
197{
198 int changed = 0;
199
200 dev_dbg(dev, "polling...\n");
201
202 update_status();
203
204 if (!ac_irq && new_ac_status != ac_status) {
205 ac_status = PDA_PSY_TO_CHANGE;
206 changed = 1;
207 }
208
209 if (!usb_irq && new_usb_status != usb_status) {
210 usb_status = PDA_PSY_TO_CHANGE;
211 changed = 1;
212 }
213
214 if (changed)
215 psy_changed();
216
217 mod_timer(&polling_timer,
218 jiffies + msecs_to_jiffies(pdata->polling_interval));
219}
220
Felipe Balbi820d0882013-03-07 11:23:50 +0200221#if IS_ENABLED(CONFIG_USB_PHY)
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100222static int otg_is_usb_online(void)
223{
Dima Zavin9ad63982011-07-10 16:01:15 -0700224 return (transceiver->last_event == USB_EVENT_VBUS ||
225 transceiver->last_event == USB_EVENT_ENUMERATED);
226}
227
228static int otg_is_ac_online(void)
229{
230 return (transceiver->last_event == USB_EVENT_CHARGER);
231}
232
233static int otg_handle_notification(struct notifier_block *nb,
234 unsigned long event, void *unused)
235{
236 switch (event) {
237 case USB_EVENT_CHARGER:
238 ac_status = PDA_PSY_TO_CHANGE;
239 break;
240 case USB_EVENT_VBUS:
241 case USB_EVENT_ENUMERATED:
242 usb_status = PDA_PSY_TO_CHANGE;
243 break;
244 case USB_EVENT_NONE:
245 ac_status = PDA_PSY_TO_CHANGE;
246 usb_status = PDA_PSY_TO_CHANGE;
247 break;
248 default:
249 return NOTIFY_OK;
250 }
251
252 /*
253 * Wait a bit before reading ac/usb line status and setting charger,
254 * because ac/usb status readings may lag from irq.
255 */
256 mod_timer(&charger_timer,
257 jiffies + msecs_to_jiffies(pdata->wait_for_status));
258
259 return NOTIFY_OK;
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100260}
261#endif
262
Anton Vorontsovb2998042007-05-04 00:32:17 +0400263static int pda_power_probe(struct platform_device *pdev)
264{
265 int ret = 0;
266
267 dev = &pdev->dev;
268
269 if (pdev->id != -1) {
270 dev_err(dev, "it's meaningless to register several "
271 "pda_powers; use id = -1\n");
272 ret = -EINVAL;
273 goto wrongid;
274 }
275
276 pdata = pdev->dev.platform_data;
277
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200278 if (pdata->init) {
279 ret = pdata->init(dev);
280 if (ret < 0)
281 goto init_failed;
282 }
283
Paul Parsonsec60ea52012-09-20 14:26:05 -0700284 ac_draw = regulator_get(dev, "ac_draw");
285 if (IS_ERR(ac_draw)) {
286 dev_dbg(dev, "couldn't get ac_draw regulator\n");
287 ac_draw = NULL;
Paul Parsonsec60ea52012-09-20 14:26:05 -0700288 }
289
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300290 update_status();
Anton Vorontsovb2998042007-05-04 00:32:17 +0400291 update_charger();
292
293 if (!pdata->wait_for_status)
294 pdata->wait_for_status = 500;
295
296 if (!pdata->wait_for_charger)
297 pdata->wait_for_charger = 500;
298
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300299 if (!pdata->polling_interval)
300 pdata->polling_interval = 2000;
301
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100302 if (!pdata->ac_max_uA)
303 pdata->ac_max_uA = 500000;
304
Anton Vorontsovb2998042007-05-04 00:32:17 +0400305 setup_timer(&charger_timer, charger_timer_func, 0);
306 setup_timer(&supply_timer, supply_timer_func, 0);
307
308 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
309 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
Anton Vorontsovb2998042007-05-04 00:32:17 +0400310
311 if (pdata->supplied_to) {
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300312 pda_psy_ac.supplied_to = pdata->supplied_to;
313 pda_psy_ac.num_supplicants = pdata->num_supplicants;
314 pda_psy_usb.supplied_to = pdata->supplied_to;
315 pda_psy_usb.num_supplicants = pdata->num_supplicants;
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) {
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300329 ret = power_supply_register(&pdev->dev, &pda_psy_ac);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400330 if (ret) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300331 dev_err(dev, "failed to register %s power supply\n",
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300332 pda_psy_ac.name);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300333 goto ac_supply_failed;
334 }
335
336 if (ac_irq) {
337 ret = request_irq(ac_irq->start, power_changed_isr,
338 get_irq_flags(ac_irq), ac_irq->name,
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300339 &pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300340 if (ret) {
341 dev_err(dev, "request ac irq failed\n");
342 goto ac_irq_failed;
343 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300344 } else {
345 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400346 }
347 }
348
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300349 if (pdata->is_usb_online) {
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300350 ret = power_supply_register(&pdev->dev, &pda_psy_usb);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400351 if (ret) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300352 dev_err(dev, "failed to register %s power supply\n",
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300353 pda_psy_usb.name);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300354 goto usb_supply_failed;
355 }
356
357 if (usb_irq) {
358 ret = request_irq(usb_irq->start, power_changed_isr,
359 get_irq_flags(usb_irq),
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300360 usb_irq->name, &pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300361 if (ret) {
362 dev_err(dev, "request usb irq failed\n");
363 goto usb_irq_failed;
364 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300365 } else {
366 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400367 }
368 }
369
Felipe Balbi820d0882013-03-07 11:23:50 +0200370#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530371 if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) {
Dima Zavin9ad63982011-07-10 16:01:15 -0700372 otg_nb.notifier_call = otg_handle_notification;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200373 ret = usb_register_notifier(transceiver, &otg_nb);
Dima Zavin9ad63982011-07-10 16:01:15 -0700374 if (ret) {
375 dev_err(dev, "failure to register otg notifier\n");
376 goto otg_reg_notifier_failed;
377 }
378 polling = 0;
379 }
Axel Lin1c745292011-08-23 20:34:33 +0800380#endif
Dima Zavin9ad63982011-07-10 16:01:15 -0700381
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300382 if (polling) {
383 dev_dbg(dev, "will poll for status\n");
384 setup_timer(&polling_timer, polling_timer_func, 0);
385 mod_timer(&polling_timer,
386 jiffies + msecs_to_jiffies(pdata->polling_interval));
387 }
388
389 if (ac_irq || usb_irq)
390 device_init_wakeup(&pdev->dev, 1);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300391
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300392 return 0;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400393
Felipe Balbi820d0882013-03-07 11:23:50 +0200394#if IS_ENABLED(CONFIG_USB_PHY)
Dima Zavin9ad63982011-07-10 16:01:15 -0700395otg_reg_notifier_failed:
396 if (pdata->is_usb_online && usb_irq)
397 free_irq(usb_irq->start, &pda_psy_usb);
Axel Lin1c745292011-08-23 20:34:33 +0800398#endif
Anton Vorontsovb2998042007-05-04 00:32:17 +0400399usb_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300400 if (pdata->is_usb_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300401 power_supply_unregister(&pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300402usb_supply_failed:
403 if (pdata->is_ac_online && ac_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300404 free_irq(ac_irq->start, &pda_psy_ac);
Felipe Balbi820d0882013-03-07 11:23:50 +0200405#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530406 if (!IS_ERR_OR_NULL(transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530407 usb_put_phy(transceiver);
Axel Lin1c745292011-08-23 20:34:33 +0800408#endif
Anton Vorontsovb2998042007-05-04 00:32:17 +0400409ac_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300410 if (pdata->is_ac_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300411 power_supply_unregister(&pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300412ac_supply_failed:
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100413 if (ac_draw) {
414 regulator_put(ac_draw);
415 ac_draw = NULL;
416 }
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200417 if (pdata->exit)
418 pdata->exit(dev);
419init_failed:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400420wrongid:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400421 return ret;
422}
423
424static int pda_power_remove(struct platform_device *pdev)
425{
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300426 if (pdata->is_usb_online && usb_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300427 free_irq(usb_irq->start, &pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300428 if (pdata->is_ac_online && ac_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300429 free_irq(ac_irq->start, &pda_psy_ac);
430
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300431 if (polling)
432 del_timer_sync(&polling_timer);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400433 del_timer_sync(&charger_timer);
434 del_timer_sync(&supply_timer);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300435
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300436 if (pdata->is_usb_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300437 power_supply_unregister(&pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300438 if (pdata->is_ac_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300439 power_supply_unregister(&pda_psy_ac);
Felipe Balbi820d0882013-03-07 11:23:50 +0200440#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530441 if (!IS_ERR_OR_NULL(transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530442 usb_put_phy(transceiver);
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100443#endif
444 if (ac_draw) {
445 regulator_put(ac_draw);
446 ac_draw = NULL;
447 }
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200448 if (pdata->exit)
449 pdata->exit(dev);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300450
Anton Vorontsovb2998042007-05-04 00:32:17 +0400451 return 0;
452}
453
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300454#ifdef CONFIG_PM
Robert Jarzmike82374f2008-08-11 22:22:27 +0200455static int ac_wakeup_enabled;
456static int usb_wakeup_enabled;
457
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300458static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
459{
Daniel Macka3bcbbe2010-04-12 23:33:22 +0200460 if (pdata->suspend) {
461 int ret = pdata->suspend(state);
462
463 if (ret)
464 return ret;
465 }
466
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300467 if (device_may_wakeup(&pdev->dev)) {
468 if (ac_irq)
Robert Jarzmike82374f2008-08-11 22:22:27 +0200469 ac_wakeup_enabled = !enable_irq_wake(ac_irq->start);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300470 if (usb_irq)
Robert Jarzmike82374f2008-08-11 22:22:27 +0200471 usb_wakeup_enabled = !enable_irq_wake(usb_irq->start);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300472 }
473
474 return 0;
475}
476
477static int pda_power_resume(struct platform_device *pdev)
478{
479 if (device_may_wakeup(&pdev->dev)) {
Robert Jarzmike82374f2008-08-11 22:22:27 +0200480 if (usb_irq && usb_wakeup_enabled)
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300481 disable_irq_wake(usb_irq->start);
Robert Jarzmike82374f2008-08-11 22:22:27 +0200482 if (ac_irq && ac_wakeup_enabled)
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300483 disable_irq_wake(ac_irq->start);
484 }
485
Daniel Macka3bcbbe2010-04-12 23:33:22 +0200486 if (pdata->resume)
487 return pdata->resume();
488
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300489 return 0;
490}
491#else
492#define pda_power_suspend NULL
493#define pda_power_resume NULL
494#endif /* CONFIG_PM */
495
Anton Vorontsovb2998042007-05-04 00:32:17 +0400496static struct platform_driver pda_power_pdrv = {
497 .driver = {
498 .name = "pda-power",
499 },
500 .probe = pda_power_probe,
501 .remove = pda_power_remove,
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300502 .suspend = pda_power_suspend,
503 .resume = pda_power_resume,
Anton Vorontsovb2998042007-05-04 00:32:17 +0400504};
505
Axel Lin300bac72011-11-26 12:01:10 +0800506module_platform_driver(pda_power_pdrv);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400507
Anton Vorontsovb2998042007-05-04 00:32:17 +0400508MODULE_LICENSE("GPL");
509MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
Axel Lin300bac72011-11-26 12:01:10 +0800510MODULE_ALIAS("platform:pda-power");