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/interrupt.h>
16 #include <linux/power_supply.h>
17 #include <linux/pda_power.h>
18 #include <linux/timer.h>
19 #include <linux/jiffies.h>
21 static inline unsigned int get_irq_flags(struct resource *res)
23 unsigned int flags = IRQF_DISABLED | IRQF_SHARED;
25 flags |= 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;
41 static int new_ac_status = -1;
42 static int new_usb_status = -1;
43 static int ac_status = -1;
44 static int usb_status = -1;
46 static int pda_power_get_property(struct power_supply *psy,
47 enum power_supply_property psp,
48 union power_supply_propval *val)
51 case POWER_SUPPLY_PROP_ONLINE:
52 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
53 val->intval = pdata->is_ac_online ?
54 pdata->is_ac_online() : 0;
56 val->intval = pdata->is_usb_online ?
57 pdata->is_usb_online() : 0;
65 static enum power_supply_property pda_power_props[] = {
66 POWER_SUPPLY_PROP_ONLINE,
69 static char *pda_power_supplied_to[] = {
74 static struct power_supply pda_psy_ac = {
76 .type = POWER_SUPPLY_TYPE_MAINS,
77 .supplied_to = pda_power_supplied_to,
78 .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
79 .properties = pda_power_props,
80 .num_properties = ARRAY_SIZE(pda_power_props),
81 .get_property = pda_power_get_property,
84 static struct power_supply pda_psy_usb = {
86 .type = POWER_SUPPLY_TYPE_USB,
87 .supplied_to = pda_power_supplied_to,
88 .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
89 .properties = pda_power_props,
90 .num_properties = ARRAY_SIZE(pda_power_props),
91 .get_property = pda_power_get_property,
94 static void update_status(void)
96 if (pdata->is_ac_online)
97 new_ac_status = !!pdata->is_ac_online();
99 if (pdata->is_usb_online)
100 new_usb_status = !!pdata->is_usb_online();
103 static void update_charger(void)
105 if (!pdata->set_charge)
108 if (new_ac_status > 0) {
109 dev_dbg(dev, "charger on (AC)\n");
110 pdata->set_charge(PDA_POWER_CHARGE_AC);
111 } else if (new_usb_status > 0) {
112 dev_dbg(dev, "charger on (USB)\n");
113 pdata->set_charge(PDA_POWER_CHARGE_USB);
115 dev_dbg(dev, "charger off\n");
116 pdata->set_charge(0);
120 static void supply_timer_func(unsigned long unused)
122 if (ac_status == PDA_PSY_TO_CHANGE) {
123 ac_status = new_ac_status;
124 power_supply_changed(&pda_psy_ac);
127 if (usb_status == PDA_PSY_TO_CHANGE) {
128 usb_status = new_usb_status;
129 power_supply_changed(&pda_psy_usb);
133 static void psy_changed(void)
138 * Okay, charger set. Now wait a bit before notifying supplicants,
139 * charge power should stabilize.
141 mod_timer(&supply_timer,
142 jiffies + msecs_to_jiffies(pdata->wait_for_charger));
145 static void charger_timer_func(unsigned long unused)
151 static irqreturn_t power_changed_isr(int irq, void *power_supply)
153 if (power_supply == &pda_psy_ac)
154 ac_status = PDA_PSY_TO_CHANGE;
155 else if (power_supply == &pda_psy_usb)
156 usb_status = PDA_PSY_TO_CHANGE;
161 * Wait a bit before reading ac/usb line status and setting charger,
162 * because ac/usb status readings may lag from irq.
164 mod_timer(&charger_timer,
165 jiffies + msecs_to_jiffies(pdata->wait_for_status));
170 static int pda_power_probe(struct platform_device *pdev)
176 if (pdev->id != -1) {
177 dev_err(dev, "it's meaningless to register several "
178 "pda_powers; use id = -1\n");
183 pdata = pdev->dev.platform_data;
188 if (!pdata->wait_for_status)
189 pdata->wait_for_status = 500;
191 if (!pdata->wait_for_charger)
192 pdata->wait_for_charger = 500;
194 setup_timer(&charger_timer, charger_timer_func, 0);
195 setup_timer(&supply_timer, supply_timer_func, 0);
197 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
198 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
200 if (pdata->supplied_to) {
201 pda_psy_ac.supplied_to = pdata->supplied_to;
202 pda_psy_ac.num_supplicants = pdata->num_supplicants;
203 pda_psy_usb.supplied_to = pdata->supplied_to;
204 pda_psy_usb.num_supplicants = pdata->num_supplicants;
207 if (pdata->is_ac_online) {
208 ret = power_supply_register(&pdev->dev, &pda_psy_ac);
210 dev_err(dev, "failed to register %s power supply\n",
212 goto ac_supply_failed;
216 ret = request_irq(ac_irq->start, power_changed_isr,
217 get_irq_flags(ac_irq), ac_irq->name,
220 dev_err(dev, "request ac irq failed\n");
226 if (pdata->is_usb_online) {
227 ret = power_supply_register(&pdev->dev, &pda_psy_usb);
229 dev_err(dev, "failed to register %s power supply\n",
231 goto usb_supply_failed;
235 ret = request_irq(usb_irq->start, power_changed_isr,
236 get_irq_flags(usb_irq),
237 usb_irq->name, &pda_psy_usb);
239 dev_err(dev, "request usb irq failed\n");
245 device_init_wakeup(&pdev->dev, 1);
250 if (pdata->is_usb_online)
251 power_supply_unregister(&pda_psy_usb);
253 if (pdata->is_ac_online && ac_irq)
254 free_irq(ac_irq->start, &pda_psy_ac);
256 if (pdata->is_ac_online)
257 power_supply_unregister(&pda_psy_ac);
263 static int pda_power_remove(struct platform_device *pdev)
265 if (pdata->is_usb_online && usb_irq)
266 free_irq(usb_irq->start, &pda_psy_usb);
267 if (pdata->is_ac_online && ac_irq)
268 free_irq(ac_irq->start, &pda_psy_ac);
270 del_timer_sync(&charger_timer);
271 del_timer_sync(&supply_timer);
273 if (pdata->is_usb_online)
274 power_supply_unregister(&pda_psy_usb);
275 if (pdata->is_ac_online)
276 power_supply_unregister(&pda_psy_ac);
282 static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
284 if (device_may_wakeup(&pdev->dev)) {
286 enable_irq_wake(ac_irq->start);
288 enable_irq_wake(usb_irq->start);
294 static int pda_power_resume(struct platform_device *pdev)
296 if (device_may_wakeup(&pdev->dev)) {
298 disable_irq_wake(usb_irq->start);
300 disable_irq_wake(ac_irq->start);
306 #define pda_power_suspend NULL
307 #define pda_power_resume NULL
308 #endif /* CONFIG_PM */
310 static struct platform_driver pda_power_pdrv = {
314 .probe = pda_power_probe,
315 .remove = pda_power_remove,
316 .suspend = pda_power_suspend,
317 .resume = pda_power_resume,
320 static int __init pda_power_init(void)
322 return platform_driver_register(&pda_power_pdrv);
325 static void __exit pda_power_exit(void)
327 platform_driver_unregister(&pda_power_pdrv);
330 module_init(pda_power_init);
331 module_exit(pda_power_exit);
332 MODULE_LICENSE("GPL");
333 MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");