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;
36 static int pda_power_get_property(struct power_supply *psy,
37 enum power_supply_property psp,
38 union power_supply_propval *val)
41 case POWER_SUPPLY_PROP_ONLINE:
42 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
43 val->intval = pdata->is_ac_online ?
44 pdata->is_ac_online() : 0;
46 val->intval = pdata->is_usb_online ?
47 pdata->is_usb_online() : 0;
55 static enum power_supply_property pda_power_props[] = {
56 POWER_SUPPLY_PROP_ONLINE,
59 static char *pda_power_supplied_to[] = {
64 static struct power_supply pda_power_supplies[] = {
67 .type = POWER_SUPPLY_TYPE_MAINS,
68 .supplied_to = pda_power_supplied_to,
69 .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
70 .properties = pda_power_props,
71 .num_properties = ARRAY_SIZE(pda_power_props),
72 .get_property = pda_power_get_property,
76 .type = POWER_SUPPLY_TYPE_USB,
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,
85 static void update_charger(void)
87 if (!pdata->set_charge)
90 if (pdata->is_ac_online && pdata->is_ac_online()) {
91 dev_dbg(dev, "charger on (AC)\n");
92 pdata->set_charge(PDA_POWER_CHARGE_AC);
93 } else if (pdata->is_usb_online && pdata->is_usb_online()) {
94 dev_dbg(dev, "charger on (USB)\n");
95 pdata->set_charge(PDA_POWER_CHARGE_USB);
97 dev_dbg(dev, "charger off\n");
104 static void supply_timer_func(unsigned long power_supply_ptr)
106 void *power_supply = (void *)power_supply_ptr;
108 power_supply_changed(power_supply);
112 static void charger_timer_func(unsigned long power_supply_ptr)
116 /* Okay, charger set. Now wait a bit before notifying supplicants,
117 * charge power should stabilize. */
118 supply_timer.data = power_supply_ptr;
119 mod_timer(&supply_timer,
120 jiffies + msecs_to_jiffies(pdata->wait_for_charger));
124 static irqreturn_t power_changed_isr(int irq, void *power_supply)
126 /* Wait a bit before reading ac/usb line status and setting charger,
127 * because ac/usb status readings may lag from irq. */
128 charger_timer.data = (unsigned long)power_supply;
129 mod_timer(&charger_timer,
130 jiffies + msecs_to_jiffies(pdata->wait_for_status));
134 static int pda_power_probe(struct platform_device *pdev)
140 if (pdev->id != -1) {
141 dev_err(dev, "it's meaningless to register several "
142 "pda_powers; use id = -1\n");
147 pdata = pdev->dev.platform_data;
151 if (!pdata->wait_for_status)
152 pdata->wait_for_status = 500;
154 if (!pdata->wait_for_charger)
155 pdata->wait_for_charger = 500;
157 setup_timer(&charger_timer, charger_timer_func, 0);
158 setup_timer(&supply_timer, supply_timer_func, 0);
160 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
161 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
162 if (!ac_irq && !usb_irq) {
163 dev_err(dev, "no ac/usb irq specified\n");
168 if (pdata->supplied_to) {
169 pda_power_supplies[0].supplied_to = pdata->supplied_to;
170 pda_power_supplies[1].supplied_to = pdata->supplied_to;
171 pda_power_supplies[0].num_supplicants = pdata->num_supplicants;
172 pda_power_supplies[1].num_supplicants = pdata->num_supplicants;
175 ret = power_supply_register(&pdev->dev, &pda_power_supplies[0]);
177 dev_err(dev, "failed to register %s power supply\n",
178 pda_power_supplies[0].name);
182 ret = power_supply_register(&pdev->dev, &pda_power_supplies[1]);
184 dev_err(dev, "failed to register %s power supply\n",
185 pda_power_supplies[1].name);
190 ret = request_irq(ac_irq->start, power_changed_isr,
191 get_irq_flags(ac_irq), ac_irq->name,
192 &pda_power_supplies[0]);
194 dev_err(dev, "request ac irq failed\n");
200 ret = request_irq(usb_irq->start, power_changed_isr,
201 get_irq_flags(usb_irq), usb_irq->name,
202 &pda_power_supplies[1]);
204 dev_err(dev, "request usb irq failed\n");
213 free_irq(ac_irq->start, &pda_power_supplies[0]);
215 power_supply_unregister(&pda_power_supplies[1]);
217 power_supply_unregister(&pda_power_supplies[0]);
225 static int pda_power_remove(struct platform_device *pdev)
228 free_irq(usb_irq->start, &pda_power_supplies[1]);
230 free_irq(ac_irq->start, &pda_power_supplies[0]);
231 del_timer_sync(&charger_timer);
232 del_timer_sync(&supply_timer);
233 power_supply_unregister(&pda_power_supplies[1]);
234 power_supply_unregister(&pda_power_supplies[0]);
238 static struct platform_driver pda_power_pdrv = {
242 .probe = pda_power_probe,
243 .remove = pda_power_remove,
246 static int __init pda_power_init(void)
248 return platform_driver_register(&pda_power_pdrv);
251 static void __exit pda_power_exit(void)
253 platform_driver_unregister(&pda_power_pdrv);
257 module_init(pda_power_init);
258 module_exit(pda_power_exit);
259 MODULE_LICENSE("GPL");
260 MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");