blob: fd55fad1d0db91efcbd8c004ed4bc39ccad8f101 [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,
Anton Vorontsovbfde2662008-01-13 02:39:17 +030086 .properties = pda_power_props,
87 .num_properties = ARRAY_SIZE(pda_power_props),
88 .get_property = pda_power_get_property,
Anton Vorontsovb2998042007-05-04 00:32:17 +040089};
90
Anton Vorontsovbfde2662008-01-13 02:39:17 +030091static struct power_supply pda_psy_usb = {
92 .name = "usb",
93 .type = POWER_SUPPLY_TYPE_USB,
Anton Vorontsovbfde2662008-01-13 02:39:17 +030094 .properties = pda_power_props,
95 .num_properties = ARRAY_SIZE(pda_power_props),
96 .get_property = pda_power_get_property,
97};
98
99static void update_status(void)
100{
101 if (pdata->is_ac_online)
102 new_ac_status = !!pdata->is_ac_online();
103
104 if (pdata->is_usb_online)
105 new_usb_status = !!pdata->is_usb_online();
106}
107
Anton Vorontsovb2998042007-05-04 00:32:17 +0400108static void update_charger(void)
109{
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100110 static int regulator_enabled;
111 int max_uA = pdata->ac_max_uA;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400112
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100113 if (pdata->set_charge) {
114 if (new_ac_status > 0) {
115 dev_dbg(dev, "charger on (AC)\n");
116 pdata->set_charge(PDA_POWER_CHARGE_AC);
117 } else if (new_usb_status > 0) {
118 dev_dbg(dev, "charger on (USB)\n");
119 pdata->set_charge(PDA_POWER_CHARGE_USB);
120 } else {
121 dev_dbg(dev, "charger off\n");
122 pdata->set_charge(0);
123 }
124 } else if (ac_draw) {
125 if (new_ac_status > 0) {
126 regulator_set_current_limit(ac_draw, max_uA, max_uA);
127 if (!regulator_enabled) {
128 dev_dbg(dev, "charger on (AC)\n");
Mark Brown92311c32012-06-12 09:46:26 +0800129 WARN_ON(regulator_enable(ac_draw));
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100130 regulator_enabled = 1;
131 }
132 } else {
133 if (regulator_enabled) {
134 dev_dbg(dev, "charger off\n");
Mark Brown92311c32012-06-12 09:46:26 +0800135 WARN_ON(regulator_disable(ac_draw));
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100136 regulator_enabled = 0;
137 }
138 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400139 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400140}
141
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300142static void supply_timer_func(unsigned long unused)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400143{
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300144 if (ac_status == PDA_PSY_TO_CHANGE) {
145 ac_status = new_ac_status;
146 power_supply_changed(&pda_psy_ac);
147 }
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400148
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300149 if (usb_status == PDA_PSY_TO_CHANGE) {
150 usb_status = new_usb_status;
151 power_supply_changed(&pda_psy_usb);
152 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400153}
154
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300155static void psy_changed(void)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400156{
157 update_charger();
158
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300159 /*
160 * Okay, charger set. Now wait a bit before notifying supplicants,
161 * charge power should stabilize.
162 */
Anton Vorontsovb2998042007-05-04 00:32:17 +0400163 mod_timer(&supply_timer,
164 jiffies + msecs_to_jiffies(pdata->wait_for_charger));
Anton Vorontsovb2998042007-05-04 00:32:17 +0400165}
166
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300167static void charger_timer_func(unsigned long unused)
168{
169 update_status();
170 psy_changed();
171}
172
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400173static irqreturn_t power_changed_isr(int irq, void *power_supply)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400174{
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300175 if (power_supply == &pda_psy_ac)
176 ac_status = PDA_PSY_TO_CHANGE;
177 else if (power_supply == &pda_psy_usb)
178 usb_status = PDA_PSY_TO_CHANGE;
179 else
180 return IRQ_NONE;
181
182 /*
183 * Wait a bit before reading ac/usb line status and setting charger,
184 * because ac/usb status readings may lag from irq.
185 */
Anton Vorontsovb2998042007-05-04 00:32:17 +0400186 mod_timer(&charger_timer,
187 jiffies + 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
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300192static void polling_timer_func(unsigned long unused)
193{
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
213 mod_timer(&polling_timer,
214 jiffies + msecs_to_jiffies(pdata->polling_interval));
215}
216
Felipe Balbi820d0882013-03-07 11:23:50 +0200217#if IS_ENABLED(CONFIG_USB_PHY)
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100218static int otg_is_usb_online(void)
219{
Dima Zavin9ad63982011-07-10 16:01:15 -0700220 return (transceiver->last_event == USB_EVENT_VBUS ||
221 transceiver->last_event == USB_EVENT_ENUMERATED);
222}
223
224static int otg_is_ac_online(void)
225{
226 return (transceiver->last_event == USB_EVENT_CHARGER);
227}
228
229static int otg_handle_notification(struct notifier_block *nb,
230 unsigned long event, void *unused)
231{
232 switch (event) {
233 case USB_EVENT_CHARGER:
234 ac_status = PDA_PSY_TO_CHANGE;
235 break;
236 case USB_EVENT_VBUS:
237 case USB_EVENT_ENUMERATED:
238 usb_status = PDA_PSY_TO_CHANGE;
239 break;
240 case USB_EVENT_NONE:
241 ac_status = PDA_PSY_TO_CHANGE;
242 usb_status = PDA_PSY_TO_CHANGE;
243 break;
244 default:
245 return NOTIFY_OK;
246 }
247
248 /*
249 * Wait a bit before reading ac/usb line status and setting charger,
250 * because ac/usb status readings may lag from irq.
251 */
252 mod_timer(&charger_timer,
253 jiffies + msecs_to_jiffies(pdata->wait_for_status));
254
255 return NOTIFY_OK;
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100256}
257#endif
258
Anton Vorontsovb2998042007-05-04 00:32:17 +0400259static int pda_power_probe(struct platform_device *pdev)
260{
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100261 struct power_supply_config psy_cfg = {};
Anton Vorontsovb2998042007-05-04 00:32:17 +0400262 int ret = 0;
263
264 dev = &pdev->dev;
265
266 if (pdev->id != -1) {
267 dev_err(dev, "it's meaningless to register several "
268 "pda_powers; use id = -1\n");
269 ret = -EINVAL;
270 goto wrongid;
271 }
272
273 pdata = pdev->dev.platform_data;
274
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200275 if (pdata->init) {
276 ret = pdata->init(dev);
277 if (ret < 0)
278 goto init_failed;
279 }
280
Paul Parsonsec60ea52012-09-20 14:26:05 -0700281 ac_draw = regulator_get(dev, "ac_draw");
282 if (IS_ERR(ac_draw)) {
283 dev_dbg(dev, "couldn't get ac_draw regulator\n");
284 ac_draw = NULL;
Paul Parsonsec60ea52012-09-20 14:26:05 -0700285 }
286
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300287 update_status();
Anton Vorontsovb2998042007-05-04 00:32:17 +0400288 update_charger();
289
290 if (!pdata->wait_for_status)
291 pdata->wait_for_status = 500;
292
293 if (!pdata->wait_for_charger)
294 pdata->wait_for_charger = 500;
295
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300296 if (!pdata->polling_interval)
297 pdata->polling_interval = 2000;
298
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100299 if (!pdata->ac_max_uA)
300 pdata->ac_max_uA = 500000;
301
Anton Vorontsovb2998042007-05-04 00:32:17 +0400302 setup_timer(&charger_timer, charger_timer_func, 0);
303 setup_timer(&supply_timer, supply_timer_func, 0);
304
305 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
306 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
Anton Vorontsovb2998042007-05-04 00:32:17 +0400307
308 if (pdata->supplied_to) {
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100309 psy_cfg.supplied_to = pdata->supplied_to;
310 psy_cfg.num_supplicants = pdata->num_supplicants;
311 } else {
312 psy_cfg.supplied_to = pda_power_supplied_to;
313 psy_cfg.num_supplicants = ARRAY_SIZE(pda_power_supplied_to);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400314 }
315
Felipe Balbi820d0882013-03-07 11:23:50 +0200316#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530317 transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530318 if (!IS_ERR_OR_NULL(transceiver)) {
319 if (!pdata->is_usb_online)
320 pdata->is_usb_online = otg_is_usb_online;
321 if (!pdata->is_ac_online)
322 pdata->is_ac_online = otg_is_ac_online;
Dima Zavin9ad63982011-07-10 16:01:15 -0700323 }
Axel Lin1c745292011-08-23 20:34:33 +0800324#endif
Dima Zavin9ad63982011-07-10 16:01:15 -0700325
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300326 if (pdata->is_ac_online) {
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100327 ret = power_supply_register(&pdev->dev, &pda_psy_ac, &psy_cfg);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400328 if (ret) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300329 dev_err(dev, "failed to register %s power supply\n",
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300330 pda_psy_ac.name);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300331 goto ac_supply_failed;
332 }
333
334 if (ac_irq) {
335 ret = request_irq(ac_irq->start, power_changed_isr,
336 get_irq_flags(ac_irq), ac_irq->name,
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300337 &pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300338 if (ret) {
339 dev_err(dev, "request ac irq failed\n");
340 goto ac_irq_failed;
341 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300342 } else {
343 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400344 }
345 }
346
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300347 if (pdata->is_usb_online) {
Krzysztof Kozlowski2dc92152015-03-12 08:44:02 +0100348 ret = power_supply_register(&pdev->dev, &pda_psy_usb, &psy_cfg);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400349 if (ret) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300350 dev_err(dev, "failed to register %s power supply\n",
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300351 pda_psy_usb.name);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300352 goto usb_supply_failed;
353 }
354
355 if (usb_irq) {
356 ret = request_irq(usb_irq->start, power_changed_isr,
357 get_irq_flags(usb_irq),
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300358 usb_irq->name, &pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300359 if (ret) {
360 dev_err(dev, "request usb irq failed\n");
361 goto usb_irq_failed;
362 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300363 } else {
364 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400365 }
366 }
367
Felipe Balbi820d0882013-03-07 11:23:50 +0200368#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530369 if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) {
Dima Zavin9ad63982011-07-10 16:01:15 -0700370 otg_nb.notifier_call = otg_handle_notification;
Heikki Krogerusfcc8ebc2012-02-13 13:24:16 +0200371 ret = usb_register_notifier(transceiver, &otg_nb);
Dima Zavin9ad63982011-07-10 16:01:15 -0700372 if (ret) {
373 dev_err(dev, "failure to register otg notifier\n");
374 goto otg_reg_notifier_failed;
375 }
376 polling = 0;
377 }
Axel Lin1c745292011-08-23 20:34:33 +0800378#endif
Dima Zavin9ad63982011-07-10 16:01:15 -0700379
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300380 if (polling) {
381 dev_dbg(dev, "will poll for status\n");
382 setup_timer(&polling_timer, polling_timer_func, 0);
383 mod_timer(&polling_timer,
384 jiffies + msecs_to_jiffies(pdata->polling_interval));
385 }
386
387 if (ac_irq || usb_irq)
388 device_init_wakeup(&pdev->dev, 1);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300389
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300390 return 0;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400391
Felipe Balbi820d0882013-03-07 11:23:50 +0200392#if IS_ENABLED(CONFIG_USB_PHY)
Dima Zavin9ad63982011-07-10 16:01:15 -0700393otg_reg_notifier_failed:
394 if (pdata->is_usb_online && usb_irq)
395 free_irq(usb_irq->start, &pda_psy_usb);
Axel Lin1c745292011-08-23 20:34:33 +0800396#endif
Anton Vorontsovb2998042007-05-04 00:32:17 +0400397usb_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300398 if (pdata->is_usb_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300399 power_supply_unregister(&pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300400usb_supply_failed:
401 if (pdata->is_ac_online && ac_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300402 free_irq(ac_irq->start, &pda_psy_ac);
Felipe Balbi820d0882013-03-07 11:23:50 +0200403#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530404 if (!IS_ERR_OR_NULL(transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530405 usb_put_phy(transceiver);
Axel Lin1c745292011-08-23 20:34:33 +0800406#endif
Anton Vorontsovb2998042007-05-04 00:32:17 +0400407ac_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300408 if (pdata->is_ac_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300409 power_supply_unregister(&pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300410ac_supply_failed:
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100411 if (ac_draw) {
412 regulator_put(ac_draw);
413 ac_draw = NULL;
414 }
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200415 if (pdata->exit)
416 pdata->exit(dev);
417init_failed:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400418wrongid:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400419 return ret;
420}
421
422static int pda_power_remove(struct platform_device *pdev)
423{
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300424 if (pdata->is_usb_online && usb_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300425 free_irq(usb_irq->start, &pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300426 if (pdata->is_ac_online && ac_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300427 free_irq(ac_irq->start, &pda_psy_ac);
428
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300429 if (polling)
430 del_timer_sync(&polling_timer);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400431 del_timer_sync(&charger_timer);
432 del_timer_sync(&supply_timer);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300433
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300434 if (pdata->is_usb_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300435 power_supply_unregister(&pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300436 if (pdata->is_ac_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300437 power_supply_unregister(&pda_psy_ac);
Felipe Balbi820d0882013-03-07 11:23:50 +0200438#if IS_ENABLED(CONFIG_USB_PHY)
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530439 if (!IS_ERR_OR_NULL(transceiver))
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530440 usb_put_phy(transceiver);
Philipp Zabel5bf2b992009-01-18 17:40:27 +0100441#endif
442 if (ac_draw) {
443 regulator_put(ac_draw);
444 ac_draw = NULL;
445 }
Philipp Zabelf6b6b182008-04-12 13:47:45 +0200446 if (pdata->exit)
447 pdata->exit(dev);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300448
Anton Vorontsovb2998042007-05-04 00:32:17 +0400449 return 0;
450}
451
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300452#ifdef CONFIG_PM
Robert Jarzmike82374f2008-08-11 22:22:27 +0200453static int ac_wakeup_enabled;
454static int usb_wakeup_enabled;
455
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300456static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
457{
Daniel Macka3bcbbe2010-04-12 23:33:22 +0200458 if (pdata->suspend) {
459 int ret = pdata->suspend(state);
460
461 if (ret)
462 return ret;
463 }
464
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300465 if (device_may_wakeup(&pdev->dev)) {
466 if (ac_irq)
Robert Jarzmike82374f2008-08-11 22:22:27 +0200467 ac_wakeup_enabled = !enable_irq_wake(ac_irq->start);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300468 if (usb_irq)
Robert Jarzmike82374f2008-08-11 22:22:27 +0200469 usb_wakeup_enabled = !enable_irq_wake(usb_irq->start);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300470 }
471
472 return 0;
473}
474
475static int pda_power_resume(struct platform_device *pdev)
476{
477 if (device_may_wakeup(&pdev->dev)) {
Robert Jarzmike82374f2008-08-11 22:22:27 +0200478 if (usb_irq && usb_wakeup_enabled)
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300479 disable_irq_wake(usb_irq->start);
Robert Jarzmike82374f2008-08-11 22:22:27 +0200480 if (ac_irq && ac_wakeup_enabled)
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300481 disable_irq_wake(ac_irq->start);
482 }
483
Daniel Macka3bcbbe2010-04-12 23:33:22 +0200484 if (pdata->resume)
485 return pdata->resume();
486
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300487 return 0;
488}
489#else
490#define pda_power_suspend NULL
491#define pda_power_resume NULL
492#endif /* CONFIG_PM */
493
Anton Vorontsovb2998042007-05-04 00:32:17 +0400494static struct platform_driver pda_power_pdrv = {
495 .driver = {
496 .name = "pda-power",
497 },
498 .probe = pda_power_probe,
499 .remove = pda_power_remove,
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300500 .suspend = pda_power_suspend,
501 .resume = pda_power_resume,
Anton Vorontsovb2998042007-05-04 00:32:17 +0400502};
503
Axel Lin300bac72011-11-26 12:01:10 +0800504module_platform_driver(pda_power_pdrv);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400505
Anton Vorontsovb2998042007-05-04 00:32:17 +0400506MODULE_LICENSE("GPL");
507MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
Axel Lin300bac72011-11-26 12:01:10 +0800508MODULE_ALIAS("platform:pda-power");