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.
6 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
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.
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/err.h>
16 #include <linux/interrupt.h>
17 #include <linux/notifier.h>
18 #include <linux/power_supply.h>
19 #include <linux/pda_power.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/timer.h>
22 #include <linux/jiffies.h>
23 #include <linux/usb/otg.h>
25 static inline unsigned int get_irq_flags(struct resource
*res
)
27 unsigned int flags
= IRQF_SAMPLE_RANDOM
| IRQF_SHARED
;
29 flags
|= res
->flags
& IRQF_TRIGGER_MASK
;
34 static struct device
*dev
;
35 static struct pda_power_pdata
*pdata
;
36 static struct resource
*ac_irq
, *usb_irq
;
37 static struct timer_list charger_timer
;
38 static struct timer_list supply_timer
;
39 static struct timer_list polling_timer
;
42 #ifdef CONFIG_USB_OTG_UTILS
43 static struct otg_transceiver
*transceiver
;
44 static struct notifier_block otg_nb
;
47 static struct regulator
*ac_draw
;
54 static int new_ac_status
= -1;
55 static int new_usb_status
= -1;
56 static int ac_status
= -1;
57 static int usb_status
= -1;
59 static int pda_power_get_property(struct power_supply
*psy
,
60 enum power_supply_property psp
,
61 union power_supply_propval
*val
)
64 case POWER_SUPPLY_PROP_ONLINE
:
65 if (psy
->type
== POWER_SUPPLY_TYPE_MAINS
)
66 val
->intval
= pdata
->is_ac_online
?
67 pdata
->is_ac_online() : 0;
69 val
->intval
= pdata
->is_usb_online
?
70 pdata
->is_usb_online() : 0;
78 static enum power_supply_property pda_power_props
[] = {
79 POWER_SUPPLY_PROP_ONLINE
,
82 static char *pda_power_supplied_to
[] = {
87 static struct power_supply pda_psy_ac
= {
89 .type
= POWER_SUPPLY_TYPE_MAINS
,
90 .supplied_to
= pda_power_supplied_to
,
91 .num_supplicants
= ARRAY_SIZE(pda_power_supplied_to
),
92 .properties
= pda_power_props
,
93 .num_properties
= ARRAY_SIZE(pda_power_props
),
94 .get_property
= pda_power_get_property
,
97 static struct power_supply pda_psy_usb
= {
99 .type
= POWER_SUPPLY_TYPE_USB
,
100 .supplied_to
= pda_power_supplied_to
,
101 .num_supplicants
= ARRAY_SIZE(pda_power_supplied_to
),
102 .properties
= pda_power_props
,
103 .num_properties
= ARRAY_SIZE(pda_power_props
),
104 .get_property
= pda_power_get_property
,
107 static void update_status(void)
109 if (pdata
->is_ac_online
)
110 new_ac_status
= !!pdata
->is_ac_online();
112 if (pdata
->is_usb_online
)
113 new_usb_status
= !!pdata
->is_usb_online();
116 static void update_charger(void)
118 static int regulator_enabled
;
119 int max_uA
= pdata
->ac_max_uA
;
121 if (pdata
->set_charge
) {
122 if (new_ac_status
> 0) {
123 dev_dbg(dev
, "charger on (AC)\n");
124 pdata
->set_charge(PDA_POWER_CHARGE_AC
);
125 } else if (new_usb_status
> 0) {
126 dev_dbg(dev
, "charger on (USB)\n");
127 pdata
->set_charge(PDA_POWER_CHARGE_USB
);
129 dev_dbg(dev
, "charger off\n");
130 pdata
->set_charge(0);
132 } else if (ac_draw
) {
133 if (new_ac_status
> 0) {
134 regulator_set_current_limit(ac_draw
, max_uA
, max_uA
);
135 if (!regulator_enabled
) {
136 dev_dbg(dev
, "charger on (AC)\n");
137 regulator_enable(ac_draw
);
138 regulator_enabled
= 1;
141 if (regulator_enabled
) {
142 dev_dbg(dev
, "charger off\n");
143 regulator_disable(ac_draw
);
144 regulator_enabled
= 0;
150 static void supply_timer_func(unsigned long unused
)
152 if (ac_status
== PDA_PSY_TO_CHANGE
) {
153 ac_status
= new_ac_status
;
154 power_supply_changed(&pda_psy_ac
);
157 if (usb_status
== PDA_PSY_TO_CHANGE
) {
158 usb_status
= new_usb_status
;
159 power_supply_changed(&pda_psy_usb
);
163 static void psy_changed(void)
168 * Okay, charger set. Now wait a bit before notifying supplicants,
169 * charge power should stabilize.
171 mod_timer(&supply_timer
,
172 jiffies
+ msecs_to_jiffies(pdata
->wait_for_charger
));
175 static void charger_timer_func(unsigned long unused
)
181 static irqreturn_t
power_changed_isr(int irq
, void *power_supply
)
183 if (power_supply
== &pda_psy_ac
)
184 ac_status
= PDA_PSY_TO_CHANGE
;
185 else if (power_supply
== &pda_psy_usb
)
186 usb_status
= PDA_PSY_TO_CHANGE
;
191 * Wait a bit before reading ac/usb line status and setting charger,
192 * because ac/usb status readings may lag from irq.
194 mod_timer(&charger_timer
,
195 jiffies
+ msecs_to_jiffies(pdata
->wait_for_status
));
200 static void polling_timer_func(unsigned long unused
)
204 dev_dbg(dev
, "polling...\n");
208 if (!ac_irq
&& new_ac_status
!= ac_status
) {
209 ac_status
= PDA_PSY_TO_CHANGE
;
213 if (!usb_irq
&& new_usb_status
!= usb_status
) {
214 usb_status
= PDA_PSY_TO_CHANGE
;
221 mod_timer(&polling_timer
,
222 jiffies
+ msecs_to_jiffies(pdata
->polling_interval
));
225 #ifdef CONFIG_USB_OTG_UTILS
226 static int otg_is_usb_online(void)
228 return (transceiver
->last_event
== USB_EVENT_VBUS
||
229 transceiver
->last_event
== USB_EVENT_ENUMERATED
);
232 static int otg_is_ac_online(void)
234 return (transceiver
->last_event
== USB_EVENT_CHARGER
);
237 static int otg_handle_notification(struct notifier_block
*nb
,
238 unsigned long event
, void *unused
)
241 case USB_EVENT_CHARGER
:
242 ac_status
= PDA_PSY_TO_CHANGE
;
245 case USB_EVENT_ENUMERATED
:
246 usb_status
= PDA_PSY_TO_CHANGE
;
249 ac_status
= PDA_PSY_TO_CHANGE
;
250 usb_status
= PDA_PSY_TO_CHANGE
;
257 * Wait a bit before reading ac/usb line status and setting charger,
258 * because ac/usb status readings may lag from irq.
260 mod_timer(&charger_timer
,
261 jiffies
+ msecs_to_jiffies(pdata
->wait_for_status
));
267 static int pda_power_probe(struct platform_device
*pdev
)
273 if (pdev
->id
!= -1) {
274 dev_err(dev
, "it's meaningless to register several "
275 "pda_powers; use id = -1\n");
280 pdata
= pdev
->dev
.platform_data
;
283 ret
= pdata
->init(dev
);
291 if (!pdata
->wait_for_status
)
292 pdata
->wait_for_status
= 500;
294 if (!pdata
->wait_for_charger
)
295 pdata
->wait_for_charger
= 500;
297 if (!pdata
->polling_interval
)
298 pdata
->polling_interval
= 2000;
300 if (!pdata
->ac_max_uA
)
301 pdata
->ac_max_uA
= 500000;
303 setup_timer(&charger_timer
, charger_timer_func
, 0);
304 setup_timer(&supply_timer
, supply_timer_func
, 0);
306 ac_irq
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "ac");
307 usb_irq
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "usb");
309 if (pdata
->supplied_to
) {
310 pda_psy_ac
.supplied_to
= pdata
->supplied_to
;
311 pda_psy_ac
.num_supplicants
= pdata
->num_supplicants
;
312 pda_psy_usb
.supplied_to
= pdata
->supplied_to
;
313 pda_psy_usb
.num_supplicants
= pdata
->num_supplicants
;
316 ac_draw
= regulator_get(dev
, "ac_draw");
317 if (IS_ERR(ac_draw
)) {
318 dev_dbg(dev
, "couldn't get ac_draw regulator\n");
320 ret
= PTR_ERR(ac_draw
);
323 #ifdef CONFIG_USB_OTG_UTILS
324 transceiver
= otg_get_transceiver();
325 if (transceiver
&& !pdata
->is_usb_online
) {
326 pdata
->is_usb_online
= otg_is_usb_online
;
328 if (transceiver
&& !pdata
->is_ac_online
) {
329 pdata
->is_ac_online
= otg_is_ac_online
;
333 if (pdata
->is_ac_online
) {
334 ret
= power_supply_register(&pdev
->dev
, &pda_psy_ac
);
336 dev_err(dev
, "failed to register %s power supply\n",
338 goto ac_supply_failed
;
342 ret
= request_irq(ac_irq
->start
, power_changed_isr
,
343 get_irq_flags(ac_irq
), ac_irq
->name
,
346 dev_err(dev
, "request ac irq failed\n");
354 if (pdata
->is_usb_online
) {
355 ret
= power_supply_register(&pdev
->dev
, &pda_psy_usb
);
357 dev_err(dev
, "failed to register %s power supply\n",
359 goto usb_supply_failed
;
363 ret
= request_irq(usb_irq
->start
, power_changed_isr
,
364 get_irq_flags(usb_irq
),
365 usb_irq
->name
, &pda_psy_usb
);
367 dev_err(dev
, "request usb irq failed\n");
375 #ifdef CONFIG_USB_OTG_UTILS
376 if (transceiver
&& pdata
->use_otg_notifier
) {
377 otg_nb
.notifier_call
= otg_handle_notification
;
378 ret
= otg_register_notifier(transceiver
, &otg_nb
);
380 dev_err(dev
, "failure to register otg notifier\n");
381 goto otg_reg_notifier_failed
;
388 dev_dbg(dev
, "will poll for status\n");
389 setup_timer(&polling_timer
, polling_timer_func
, 0);
390 mod_timer(&polling_timer
,
391 jiffies
+ msecs_to_jiffies(pdata
->polling_interval
));
394 if (ac_irq
|| usb_irq
)
395 device_init_wakeup(&pdev
->dev
, 1);
399 #ifdef CONFIG_USB_OTG_UTILS
400 otg_reg_notifier_failed
:
401 if (pdata
->is_usb_online
&& usb_irq
)
402 free_irq(usb_irq
->start
, &pda_psy_usb
);
405 if (pdata
->is_usb_online
)
406 power_supply_unregister(&pda_psy_usb
);
408 if (pdata
->is_ac_online
&& ac_irq
)
409 free_irq(ac_irq
->start
, &pda_psy_ac
);
410 #ifdef CONFIG_USB_OTG_UTILS
412 otg_put_transceiver(transceiver
);
415 if (pdata
->is_ac_online
)
416 power_supply_unregister(&pda_psy_ac
);
419 regulator_put(ac_draw
);
429 static int pda_power_remove(struct platform_device
*pdev
)
431 if (pdata
->is_usb_online
&& usb_irq
)
432 free_irq(usb_irq
->start
, &pda_psy_usb
);
433 if (pdata
->is_ac_online
&& ac_irq
)
434 free_irq(ac_irq
->start
, &pda_psy_ac
);
437 del_timer_sync(&polling_timer
);
438 del_timer_sync(&charger_timer
);
439 del_timer_sync(&supply_timer
);
441 if (pdata
->is_usb_online
)
442 power_supply_unregister(&pda_psy_usb
);
443 if (pdata
->is_ac_online
)
444 power_supply_unregister(&pda_psy_ac
);
445 #ifdef CONFIG_USB_OTG_UTILS
447 otg_put_transceiver(transceiver
);
450 regulator_put(ac_draw
);
460 static int ac_wakeup_enabled
;
461 static int usb_wakeup_enabled
;
463 static int pda_power_suspend(struct platform_device
*pdev
, pm_message_t state
)
465 if (pdata
->suspend
) {
466 int ret
= pdata
->suspend(state
);
472 if (device_may_wakeup(&pdev
->dev
)) {
474 ac_wakeup_enabled
= !enable_irq_wake(ac_irq
->start
);
476 usb_wakeup_enabled
= !enable_irq_wake(usb_irq
->start
);
482 static int pda_power_resume(struct platform_device
*pdev
)
484 if (device_may_wakeup(&pdev
->dev
)) {
485 if (usb_irq
&& usb_wakeup_enabled
)
486 disable_irq_wake(usb_irq
->start
);
487 if (ac_irq
&& ac_wakeup_enabled
)
488 disable_irq_wake(ac_irq
->start
);
492 return pdata
->resume();
497 #define pda_power_suspend NULL
498 #define pda_power_resume NULL
499 #endif /* CONFIG_PM */
501 MODULE_ALIAS("platform:pda-power");
503 static struct platform_driver pda_power_pdrv
= {
507 .probe
= pda_power_probe
,
508 .remove
= pda_power_remove
,
509 .suspend
= pda_power_suspend
,
510 .resume
= pda_power_resume
,
513 static int __init
pda_power_init(void)
515 return platform_driver_register(&pda_power_pdrv
);
518 static void __exit
pda_power_exit(void)
520 platform_driver_unregister(&pda_power_pdrv
);
523 module_init(pda_power_init
);
524 module_exit(pda_power_exit
);
525 MODULE_LICENSE("GPL");
526 MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");