Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / drivers / power / pda_power.c
blobc8aa55b81fd86458f7c5dbcdcbc486a81c8cb5a7
1 /*
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;
27 return flags;
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;
36 static int polling;
38 enum {
39 PDA_PSY_OFFLINE = 0,
40 PDA_PSY_ONLINE = 1,
41 PDA_PSY_TO_CHANGE,
43 static int new_ac_status = -1;
44 static int new_usb_status = -1;
45 static int ac_status = -1;
46 static int usb_status = -1;
48 static int pda_power_get_property(struct power_supply *psy,
49 enum power_supply_property psp,
50 union power_supply_propval *val)
52 switch (psp) {
53 case POWER_SUPPLY_PROP_ONLINE:
54 if (psy->type == POWER_SUPPLY_TYPE_MAINS)
55 val->intval = pdata->is_ac_online ?
56 pdata->is_ac_online() : 0;
57 else
58 val->intval = pdata->is_usb_online ?
59 pdata->is_usb_online() : 0;
60 break;
61 default:
62 return -EINVAL;
64 return 0;
67 static enum power_supply_property pda_power_props[] = {
68 POWER_SUPPLY_PROP_ONLINE,
71 static char *pda_power_supplied_to[] = {
72 "main-battery",
73 "backup-battery",
76 static struct power_supply pda_psy_ac = {
77 .name = "ac",
78 .type = POWER_SUPPLY_TYPE_MAINS,
79 .supplied_to = pda_power_supplied_to,
80 .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
81 .properties = pda_power_props,
82 .num_properties = ARRAY_SIZE(pda_power_props),
83 .get_property = pda_power_get_property,
86 static struct power_supply pda_psy_usb = {
87 .name = "usb",
88 .type = POWER_SUPPLY_TYPE_USB,
89 .supplied_to = pda_power_supplied_to,
90 .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
91 .properties = pda_power_props,
92 .num_properties = ARRAY_SIZE(pda_power_props),
93 .get_property = pda_power_get_property,
96 static void update_status(void)
98 if (pdata->is_ac_online)
99 new_ac_status = !!pdata->is_ac_online();
101 if (pdata->is_usb_online)
102 new_usb_status = !!pdata->is_usb_online();
105 static void update_charger(void)
107 if (!pdata->set_charge)
108 return;
110 if (new_ac_status > 0) {
111 dev_dbg(dev, "charger on (AC)\n");
112 pdata->set_charge(PDA_POWER_CHARGE_AC);
113 } else if (new_usb_status > 0) {
114 dev_dbg(dev, "charger on (USB)\n");
115 pdata->set_charge(PDA_POWER_CHARGE_USB);
116 } else {
117 dev_dbg(dev, "charger off\n");
118 pdata->set_charge(0);
122 static void supply_timer_func(unsigned long unused)
124 if (ac_status == PDA_PSY_TO_CHANGE) {
125 ac_status = new_ac_status;
126 power_supply_changed(&pda_psy_ac);
129 if (usb_status == PDA_PSY_TO_CHANGE) {
130 usb_status = new_usb_status;
131 power_supply_changed(&pda_psy_usb);
135 static void psy_changed(void)
137 update_charger();
140 * Okay, charger set. Now wait a bit before notifying supplicants,
141 * charge power should stabilize.
143 mod_timer(&supply_timer,
144 jiffies + msecs_to_jiffies(pdata->wait_for_charger));
147 static void charger_timer_func(unsigned long unused)
149 update_status();
150 psy_changed();
153 static irqreturn_t power_changed_isr(int irq, void *power_supply)
155 if (power_supply == &pda_psy_ac)
156 ac_status = PDA_PSY_TO_CHANGE;
157 else if (power_supply == &pda_psy_usb)
158 usb_status = PDA_PSY_TO_CHANGE;
159 else
160 return IRQ_NONE;
163 * Wait a bit before reading ac/usb line status and setting charger,
164 * because ac/usb status readings may lag from irq.
166 mod_timer(&charger_timer,
167 jiffies + msecs_to_jiffies(pdata->wait_for_status));
169 return IRQ_HANDLED;
172 static void polling_timer_func(unsigned long unused)
174 int changed = 0;
176 dev_dbg(dev, "polling...\n");
178 update_status();
180 if (!ac_irq && new_ac_status != ac_status) {
181 ac_status = PDA_PSY_TO_CHANGE;
182 changed = 1;
185 if (!usb_irq && new_usb_status != usb_status) {
186 usb_status = PDA_PSY_TO_CHANGE;
187 changed = 1;
190 if (changed)
191 psy_changed();
193 mod_timer(&polling_timer,
194 jiffies + msecs_to_jiffies(pdata->polling_interval));
197 static int pda_power_probe(struct platform_device *pdev)
199 int ret = 0;
201 dev = &pdev->dev;
203 if (pdev->id != -1) {
204 dev_err(dev, "it's meaningless to register several "
205 "pda_powers; use id = -1\n");
206 ret = -EINVAL;
207 goto wrongid;
210 pdata = pdev->dev.platform_data;
212 update_status();
213 update_charger();
215 if (!pdata->wait_for_status)
216 pdata->wait_for_status = 500;
218 if (!pdata->wait_for_charger)
219 pdata->wait_for_charger = 500;
221 if (!pdata->polling_interval)
222 pdata->polling_interval = 2000;
224 setup_timer(&charger_timer, charger_timer_func, 0);
225 setup_timer(&supply_timer, supply_timer_func, 0);
227 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
228 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
230 if (pdata->supplied_to) {
231 pda_psy_ac.supplied_to = pdata->supplied_to;
232 pda_psy_ac.num_supplicants = pdata->num_supplicants;
233 pda_psy_usb.supplied_to = pdata->supplied_to;
234 pda_psy_usb.num_supplicants = pdata->num_supplicants;
237 if (pdata->is_ac_online) {
238 ret = power_supply_register(&pdev->dev, &pda_psy_ac);
239 if (ret) {
240 dev_err(dev, "failed to register %s power supply\n",
241 pda_psy_ac.name);
242 goto ac_supply_failed;
245 if (ac_irq) {
246 ret = request_irq(ac_irq->start, power_changed_isr,
247 get_irq_flags(ac_irq), ac_irq->name,
248 &pda_psy_ac);
249 if (ret) {
250 dev_err(dev, "request ac irq failed\n");
251 goto ac_irq_failed;
253 } else {
254 polling = 1;
258 if (pdata->is_usb_online) {
259 ret = power_supply_register(&pdev->dev, &pda_psy_usb);
260 if (ret) {
261 dev_err(dev, "failed to register %s power supply\n",
262 pda_psy_usb.name);
263 goto usb_supply_failed;
266 if (usb_irq) {
267 ret = request_irq(usb_irq->start, power_changed_isr,
268 get_irq_flags(usb_irq),
269 usb_irq->name, &pda_psy_usb);
270 if (ret) {
271 dev_err(dev, "request usb irq failed\n");
272 goto usb_irq_failed;
274 } else {
275 polling = 1;
279 if (polling) {
280 dev_dbg(dev, "will poll for status\n");
281 setup_timer(&polling_timer, polling_timer_func, 0);
282 mod_timer(&polling_timer,
283 jiffies + msecs_to_jiffies(pdata->polling_interval));
286 if (ac_irq || usb_irq)
287 device_init_wakeup(&pdev->dev, 1);
289 return 0;
291 usb_irq_failed:
292 if (pdata->is_usb_online)
293 power_supply_unregister(&pda_psy_usb);
294 usb_supply_failed:
295 if (pdata->is_ac_online && ac_irq)
296 free_irq(ac_irq->start, &pda_psy_ac);
297 ac_irq_failed:
298 if (pdata->is_ac_online)
299 power_supply_unregister(&pda_psy_ac);
300 ac_supply_failed:
301 wrongid:
302 return ret;
305 static int pda_power_remove(struct platform_device *pdev)
307 if (pdata->is_usb_online && usb_irq)
308 free_irq(usb_irq->start, &pda_psy_usb);
309 if (pdata->is_ac_online && ac_irq)
310 free_irq(ac_irq->start, &pda_psy_ac);
312 if (polling)
313 del_timer_sync(&polling_timer);
314 del_timer_sync(&charger_timer);
315 del_timer_sync(&supply_timer);
317 if (pdata->is_usb_online)
318 power_supply_unregister(&pda_psy_usb);
319 if (pdata->is_ac_online)
320 power_supply_unregister(&pda_psy_ac);
322 return 0;
325 #ifdef CONFIG_PM
326 static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
328 if (device_may_wakeup(&pdev->dev)) {
329 if (ac_irq)
330 enable_irq_wake(ac_irq->start);
331 if (usb_irq)
332 enable_irq_wake(usb_irq->start);
335 return 0;
338 static int pda_power_resume(struct platform_device *pdev)
340 if (device_may_wakeup(&pdev->dev)) {
341 if (usb_irq)
342 disable_irq_wake(usb_irq->start);
343 if (ac_irq)
344 disable_irq_wake(ac_irq->start);
347 return 0;
349 #else
350 #define pda_power_suspend NULL
351 #define pda_power_resume NULL
352 #endif /* CONFIG_PM */
354 static struct platform_driver pda_power_pdrv = {
355 .driver = {
356 .name = "pda-power",
358 .probe = pda_power_probe,
359 .remove = pda_power_remove,
360 .suspend = pda_power_suspend,
361 .resume = pda_power_resume,
364 static int __init pda_power_init(void)
366 return platform_driver_register(&pda_power_pdrv);
369 static void __exit pda_power_exit(void)
371 platform_driver_unregister(&pda_power_pdrv);
374 module_init(pda_power_init);
375 module_exit(pda_power_exit);
376 MODULE_LICENSE("GPL");
377 MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");