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 return IRQF_SHARED
| (res
->flags
& IRQF_TRIGGER_MASK
);
30 static struct device
*dev
;
31 static struct pda_power_pdata
*pdata
;
32 static struct resource
*ac_irq
, *usb_irq
;
33 static struct timer_list charger_timer
;
34 static struct timer_list supply_timer
;
35 static struct timer_list polling_timer
;
38 #if IS_ENABLED(CONFIG_USB_PHY)
39 static struct usb_phy
*transceiver
;
40 static struct notifier_block otg_nb
;
43 static struct regulator
*ac_draw
;
50 static int new_ac_status
= -1;
51 static int new_usb_status
= -1;
52 static int ac_status
= -1;
53 static int usb_status
= -1;
55 static int pda_power_get_property(struct power_supply
*psy
,
56 enum power_supply_property psp
,
57 union power_supply_propval
*val
)
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;
65 val
->intval
= pdata
->is_usb_online
?
66 pdata
->is_usb_online() : 0;
74 static enum power_supply_property pda_power_props
[] = {
75 POWER_SUPPLY_PROP_ONLINE
,
78 static char *pda_power_supplied_to
[] = {
83 static struct power_supply pda_psy_ac
= {
85 .type
= POWER_SUPPLY_TYPE_MAINS
,
86 .properties
= pda_power_props
,
87 .num_properties
= ARRAY_SIZE(pda_power_props
),
88 .get_property
= pda_power_get_property
,
91 static struct power_supply pda_psy_usb
= {
93 .type
= POWER_SUPPLY_TYPE_USB
,
94 .properties
= pda_power_props
,
95 .num_properties
= ARRAY_SIZE(pda_power_props
),
96 .get_property
= pda_power_get_property
,
99 static void update_status(void)
101 if (pdata
->is_ac_online
)
102 new_ac_status
= !!pdata
->is_ac_online();
104 if (pdata
->is_usb_online
)
105 new_usb_status
= !!pdata
->is_usb_online();
108 static void update_charger(void)
110 static int regulator_enabled
;
111 int max_uA
= pdata
->ac_max_uA
;
113 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
);
121 dev_dbg(dev
, "charger off\n");
122 pdata
->set_charge(0);
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");
129 WARN_ON(regulator_enable(ac_draw
));
130 regulator_enabled
= 1;
133 if (regulator_enabled
) {
134 dev_dbg(dev
, "charger off\n");
135 WARN_ON(regulator_disable(ac_draw
));
136 regulator_enabled
= 0;
142 static void supply_timer_func(unsigned long unused
)
144 if (ac_status
== PDA_PSY_TO_CHANGE
) {
145 ac_status
= new_ac_status
;
146 power_supply_changed(&pda_psy_ac
);
149 if (usb_status
== PDA_PSY_TO_CHANGE
) {
150 usb_status
= new_usb_status
;
151 power_supply_changed(&pda_psy_usb
);
155 static void psy_changed(void)
160 * Okay, charger set. Now wait a bit before notifying supplicants,
161 * charge power should stabilize.
163 mod_timer(&supply_timer
,
164 jiffies
+ msecs_to_jiffies(pdata
->wait_for_charger
));
167 static void charger_timer_func(unsigned long unused
)
173 static irqreturn_t
power_changed_isr(int irq
, void *power_supply
)
175 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
;
183 * Wait a bit before reading ac/usb line status and setting charger,
184 * because ac/usb status readings may lag from irq.
186 mod_timer(&charger_timer
,
187 jiffies
+ msecs_to_jiffies(pdata
->wait_for_status
));
192 static void polling_timer_func(unsigned long unused
)
196 dev_dbg(dev
, "polling...\n");
200 if (!ac_irq
&& new_ac_status
!= ac_status
) {
201 ac_status
= PDA_PSY_TO_CHANGE
;
205 if (!usb_irq
&& new_usb_status
!= usb_status
) {
206 usb_status
= PDA_PSY_TO_CHANGE
;
213 mod_timer(&polling_timer
,
214 jiffies
+ msecs_to_jiffies(pdata
->polling_interval
));
217 #if IS_ENABLED(CONFIG_USB_PHY)
218 static int otg_is_usb_online(void)
220 return (transceiver
->last_event
== USB_EVENT_VBUS
||
221 transceiver
->last_event
== USB_EVENT_ENUMERATED
);
224 static int otg_is_ac_online(void)
226 return (transceiver
->last_event
== USB_EVENT_CHARGER
);
229 static int otg_handle_notification(struct notifier_block
*nb
,
230 unsigned long event
, void *unused
)
233 case USB_EVENT_CHARGER
:
234 ac_status
= PDA_PSY_TO_CHANGE
;
237 case USB_EVENT_ENUMERATED
:
238 usb_status
= PDA_PSY_TO_CHANGE
;
241 ac_status
= PDA_PSY_TO_CHANGE
;
242 usb_status
= PDA_PSY_TO_CHANGE
;
249 * Wait a bit before reading ac/usb line status and setting charger,
250 * because ac/usb status readings may lag from irq.
252 mod_timer(&charger_timer
,
253 jiffies
+ msecs_to_jiffies(pdata
->wait_for_status
));
259 static int pda_power_probe(struct platform_device
*pdev
)
261 struct power_supply_config psy_cfg
= {};
266 if (pdev
->id
!= -1) {
267 dev_err(dev
, "it's meaningless to register several "
268 "pda_powers; use id = -1\n");
273 pdata
= pdev
->dev
.platform_data
;
276 ret
= pdata
->init(dev
);
281 ac_draw
= regulator_get(dev
, "ac_draw");
282 if (IS_ERR(ac_draw
)) {
283 dev_dbg(dev
, "couldn't get ac_draw regulator\n");
290 if (!pdata
->wait_for_status
)
291 pdata
->wait_for_status
= 500;
293 if (!pdata
->wait_for_charger
)
294 pdata
->wait_for_charger
= 500;
296 if (!pdata
->polling_interval
)
297 pdata
->polling_interval
= 2000;
299 if (!pdata
->ac_max_uA
)
300 pdata
->ac_max_uA
= 500000;
302 setup_timer(&charger_timer
, charger_timer_func
, 0);
303 setup_timer(&supply_timer
, supply_timer_func
, 0);
305 ac_irq
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "ac");
306 usb_irq
= platform_get_resource_byname(pdev
, IORESOURCE_IRQ
, "usb");
308 if (pdata
->supplied_to
) {
309 psy_cfg
.supplied_to
= pdata
->supplied_to
;
310 psy_cfg
.num_supplicants
= pdata
->num_supplicants
;
312 psy_cfg
.supplied_to
= pda_power_supplied_to
;
313 psy_cfg
.num_supplicants
= ARRAY_SIZE(pda_power_supplied_to
);
316 #if IS_ENABLED(CONFIG_USB_PHY)
317 transceiver
= usb_get_phy(USB_PHY_TYPE_USB2
);
318 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
;
326 if (pdata
->is_ac_online
) {
327 ret
= power_supply_register(&pdev
->dev
, &pda_psy_ac
, &psy_cfg
);
329 dev_err(dev
, "failed to register %s power supply\n",
331 goto ac_supply_failed
;
335 ret
= request_irq(ac_irq
->start
, power_changed_isr
,
336 get_irq_flags(ac_irq
), ac_irq
->name
,
339 dev_err(dev
, "request ac irq failed\n");
347 if (pdata
->is_usb_online
) {
348 ret
= power_supply_register(&pdev
->dev
, &pda_psy_usb
, &psy_cfg
);
350 dev_err(dev
, "failed to register %s power supply\n",
352 goto usb_supply_failed
;
356 ret
= request_irq(usb_irq
->start
, power_changed_isr
,
357 get_irq_flags(usb_irq
),
358 usb_irq
->name
, &pda_psy_usb
);
360 dev_err(dev
, "request usb irq failed\n");
368 #if IS_ENABLED(CONFIG_USB_PHY)
369 if (!IS_ERR_OR_NULL(transceiver
) && pdata
->use_otg_notifier
) {
370 otg_nb
.notifier_call
= otg_handle_notification
;
371 ret
= usb_register_notifier(transceiver
, &otg_nb
);
373 dev_err(dev
, "failure to register otg notifier\n");
374 goto otg_reg_notifier_failed
;
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
));
387 if (ac_irq
|| usb_irq
)
388 device_init_wakeup(&pdev
->dev
, 1);
392 #if IS_ENABLED(CONFIG_USB_PHY)
393 otg_reg_notifier_failed
:
394 if (pdata
->is_usb_online
&& usb_irq
)
395 free_irq(usb_irq
->start
, &pda_psy_usb
);
398 if (pdata
->is_usb_online
)
399 power_supply_unregister(&pda_psy_usb
);
401 if (pdata
->is_ac_online
&& ac_irq
)
402 free_irq(ac_irq
->start
, &pda_psy_ac
);
403 #if IS_ENABLED(CONFIG_USB_PHY)
404 if (!IS_ERR_OR_NULL(transceiver
))
405 usb_put_phy(transceiver
);
408 if (pdata
->is_ac_online
)
409 power_supply_unregister(&pda_psy_ac
);
412 regulator_put(ac_draw
);
422 static int pda_power_remove(struct platform_device
*pdev
)
424 if (pdata
->is_usb_online
&& usb_irq
)
425 free_irq(usb_irq
->start
, &pda_psy_usb
);
426 if (pdata
->is_ac_online
&& ac_irq
)
427 free_irq(ac_irq
->start
, &pda_psy_ac
);
430 del_timer_sync(&polling_timer
);
431 del_timer_sync(&charger_timer
);
432 del_timer_sync(&supply_timer
);
434 if (pdata
->is_usb_online
)
435 power_supply_unregister(&pda_psy_usb
);
436 if (pdata
->is_ac_online
)
437 power_supply_unregister(&pda_psy_ac
);
438 #if IS_ENABLED(CONFIG_USB_PHY)
439 if (!IS_ERR_OR_NULL(transceiver
))
440 usb_put_phy(transceiver
);
443 regulator_put(ac_draw
);
453 static int ac_wakeup_enabled
;
454 static int usb_wakeup_enabled
;
456 static int pda_power_suspend(struct platform_device
*pdev
, pm_message_t state
)
458 if (pdata
->suspend
) {
459 int ret
= pdata
->suspend(state
);
465 if (device_may_wakeup(&pdev
->dev
)) {
467 ac_wakeup_enabled
= !enable_irq_wake(ac_irq
->start
);
469 usb_wakeup_enabled
= !enable_irq_wake(usb_irq
->start
);
475 static int pda_power_resume(struct platform_device
*pdev
)
477 if (device_may_wakeup(&pdev
->dev
)) {
478 if (usb_irq
&& usb_wakeup_enabled
)
479 disable_irq_wake(usb_irq
->start
);
480 if (ac_irq
&& ac_wakeup_enabled
)
481 disable_irq_wake(ac_irq
->start
);
485 return pdata
->resume();
490 #define pda_power_suspend NULL
491 #define pda_power_resume NULL
492 #endif /* CONFIG_PM */
494 static struct platform_driver pda_power_pdrv
= {
498 .probe
= pda_power_probe
,
499 .remove
= pda_power_remove
,
500 .suspend
= pda_power_suspend
,
501 .resume
= pda_power_resume
,
504 module_platform_driver(pda_power_pdrv
);
506 MODULE_LICENSE("GPL");
507 MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
508 MODULE_ALIAS("platform:pda-power");