blob: c8aa55b81fd86458f7c5dbcdcbc486a81c8cb5a7 [file] [log] [blame]
Anton Vorontsovb2998042007-05-04 00:32:17 +04001/*
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.
5 *
6 * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
7 *
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.
11 */
12
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>
20
21static inline unsigned int get_irq_flags(struct resource *res)
22{
23 unsigned int flags = IRQF_DISABLED | IRQF_SHARED;
24
25 flags |= res->flags & IRQF_TRIGGER_MASK;
26
27 return flags;
28}
29
30static struct device *dev;
31static struct pda_power_pdata *pdata;
32static struct resource *ac_irq, *usb_irq;
33static struct timer_list charger_timer;
34static struct timer_list supply_timer;
Anton Vorontsovc3caeba2008-01-13 02:44:20 +030035static struct timer_list polling_timer;
36static int polling;
Anton Vorontsovb2998042007-05-04 00:32:17 +040037
Anton Vorontsovbfde2662008-01-13 02:39:17 +030038enum {
39 PDA_PSY_OFFLINE = 0,
40 PDA_PSY_ONLINE = 1,
41 PDA_PSY_TO_CHANGE,
42};
43static int new_ac_status = -1;
44static int new_usb_status = -1;
45static int ac_status = -1;
46static int usb_status = -1;
47
Anton Vorontsovb2998042007-05-04 00:32:17 +040048static int pda_power_get_property(struct power_supply *psy,
49 enum power_supply_property psp,
50 union power_supply_propval *val)
51{
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;
63 }
64 return 0;
65}
66
67static enum power_supply_property pda_power_props[] = {
68 POWER_SUPPLY_PROP_ONLINE,
69};
70
71static char *pda_power_supplied_to[] = {
72 "main-battery",
73 "backup-battery",
74};
75
Anton Vorontsovbfde2662008-01-13 02:39:17 +030076static 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,
Anton Vorontsovb2998042007-05-04 00:32:17 +040084};
85
Anton Vorontsovbfde2662008-01-13 02:39:17 +030086static 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,
94};
95
96static void update_status(void)
97{
98 if (pdata->is_ac_online)
99 new_ac_status = !!pdata->is_ac_online();
100
101 if (pdata->is_usb_online)
102 new_usb_status = !!pdata->is_usb_online();
103}
104
Anton Vorontsovb2998042007-05-04 00:32:17 +0400105static void update_charger(void)
106{
107 if (!pdata->set_charge)
108 return;
109
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300110 if (new_ac_status > 0) {
Anton Vorontsovb2998042007-05-04 00:32:17 +0400111 dev_dbg(dev, "charger on (AC)\n");
112 pdata->set_charge(PDA_POWER_CHARGE_AC);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300113 } else if (new_usb_status > 0) {
Anton Vorontsovb2998042007-05-04 00:32:17 +0400114 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);
119 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400120}
121
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300122static void supply_timer_func(unsigned long unused)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400123{
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300124 if (ac_status == PDA_PSY_TO_CHANGE) {
125 ac_status = new_ac_status;
126 power_supply_changed(&pda_psy_ac);
127 }
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400128
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300129 if (usb_status == PDA_PSY_TO_CHANGE) {
130 usb_status = new_usb_status;
131 power_supply_changed(&pda_psy_usb);
132 }
Anton Vorontsovb2998042007-05-04 00:32:17 +0400133}
134
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300135static void psy_changed(void)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400136{
137 update_charger();
138
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300139 /*
140 * Okay, charger set. Now wait a bit before notifying supplicants,
141 * charge power should stabilize.
142 */
Anton Vorontsovb2998042007-05-04 00:32:17 +0400143 mod_timer(&supply_timer,
144 jiffies + msecs_to_jiffies(pdata->wait_for_charger));
Anton Vorontsovb2998042007-05-04 00:32:17 +0400145}
146
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300147static void charger_timer_func(unsigned long unused)
148{
149 update_status();
150 psy_changed();
151}
152
Jeff Garzik5ebf6e62007-07-14 19:12:04 -0400153static irqreturn_t power_changed_isr(int irq, void *power_supply)
Anton Vorontsovb2998042007-05-04 00:32:17 +0400154{
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300155 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;
161
162 /*
163 * Wait a bit before reading ac/usb line status and setting charger,
164 * because ac/usb status readings may lag from irq.
165 */
Anton Vorontsovb2998042007-05-04 00:32:17 +0400166 mod_timer(&charger_timer,
167 jiffies + msecs_to_jiffies(pdata->wait_for_status));
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300168
Anton Vorontsovb2998042007-05-04 00:32:17 +0400169 return IRQ_HANDLED;
170}
171
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300172static void polling_timer_func(unsigned long unused)
173{
174 int changed = 0;
175
176 dev_dbg(dev, "polling...\n");
177
178 update_status();
179
180 if (!ac_irq && new_ac_status != ac_status) {
181 ac_status = PDA_PSY_TO_CHANGE;
182 changed = 1;
183 }
184
185 if (!usb_irq && new_usb_status != usb_status) {
186 usb_status = PDA_PSY_TO_CHANGE;
187 changed = 1;
188 }
189
190 if (changed)
191 psy_changed();
192
193 mod_timer(&polling_timer,
194 jiffies + msecs_to_jiffies(pdata->polling_interval));
195}
196
Anton Vorontsovb2998042007-05-04 00:32:17 +0400197static int pda_power_probe(struct platform_device *pdev)
198{
199 int ret = 0;
200
201 dev = &pdev->dev;
202
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;
208 }
209
210 pdata = pdev->dev.platform_data;
211
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300212 update_status();
Anton Vorontsovb2998042007-05-04 00:32:17 +0400213 update_charger();
214
215 if (!pdata->wait_for_status)
216 pdata->wait_for_status = 500;
217
218 if (!pdata->wait_for_charger)
219 pdata->wait_for_charger = 500;
220
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300221 if (!pdata->polling_interval)
222 pdata->polling_interval = 2000;
223
Anton Vorontsovb2998042007-05-04 00:32:17 +0400224 setup_timer(&charger_timer, charger_timer_func, 0);
225 setup_timer(&supply_timer, supply_timer_func, 0);
226
227 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
228 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
Anton Vorontsovb2998042007-05-04 00:32:17 +0400229
230 if (pdata->supplied_to) {
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300231 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;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400235 }
236
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300237 if (pdata->is_ac_online) {
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300238 ret = power_supply_register(&pdev->dev, &pda_psy_ac);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400239 if (ret) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300240 dev_err(dev, "failed to register %s power supply\n",
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300241 pda_psy_ac.name);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300242 goto ac_supply_failed;
243 }
244
245 if (ac_irq) {
246 ret = request_irq(ac_irq->start, power_changed_isr,
247 get_irq_flags(ac_irq), ac_irq->name,
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300248 &pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300249 if (ret) {
250 dev_err(dev, "request ac irq failed\n");
251 goto ac_irq_failed;
252 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300253 } else {
254 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400255 }
256 }
257
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300258 if (pdata->is_usb_online) {
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300259 ret = power_supply_register(&pdev->dev, &pda_psy_usb);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400260 if (ret) {
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300261 dev_err(dev, "failed to register %s power supply\n",
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300262 pda_psy_usb.name);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300263 goto usb_supply_failed;
264 }
265
266 if (usb_irq) {
267 ret = request_irq(usb_irq->start, power_changed_isr,
268 get_irq_flags(usb_irq),
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300269 usb_irq->name, &pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300270 if (ret) {
271 dev_err(dev, "request usb irq failed\n");
272 goto usb_irq_failed;
273 }
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300274 } else {
275 polling = 1;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400276 }
277 }
278
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300279 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));
284 }
285
286 if (ac_irq || usb_irq)
287 device_init_wakeup(&pdev->dev, 1);
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300288
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300289 return 0;
Anton Vorontsovb2998042007-05-04 00:32:17 +0400290
291usb_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300292 if (pdata->is_usb_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300293 power_supply_unregister(&pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300294usb_supply_failed:
295 if (pdata->is_ac_online && ac_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300296 free_irq(ac_irq->start, &pda_psy_ac);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400297ac_irq_failed:
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300298 if (pdata->is_ac_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300299 power_supply_unregister(&pda_psy_ac);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300300ac_supply_failed:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400301wrongid:
Anton Vorontsovb2998042007-05-04 00:32:17 +0400302 return ret;
303}
304
305static int pda_power_remove(struct platform_device *pdev)
306{
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300307 if (pdata->is_usb_online && usb_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300308 free_irq(usb_irq->start, &pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300309 if (pdata->is_ac_online && ac_irq)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300310 free_irq(ac_irq->start, &pda_psy_ac);
311
Anton Vorontsovc3caeba2008-01-13 02:44:20 +0300312 if (polling)
313 del_timer_sync(&polling_timer);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400314 del_timer_sync(&charger_timer);
315 del_timer_sync(&supply_timer);
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300316
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300317 if (pdata->is_usb_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300318 power_supply_unregister(&pda_psy_usb);
Dmitry Baryshkov9ef45102008-01-07 04:12:39 +0300319 if (pdata->is_ac_online)
Anton Vorontsovbfde2662008-01-13 02:39:17 +0300320 power_supply_unregister(&pda_psy_ac);
321
Anton Vorontsovb2998042007-05-04 00:32:17 +0400322 return 0;
323}
324
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300325#ifdef CONFIG_PM
326static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
327{
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);
333 }
334
335 return 0;
336}
337
338static int pda_power_resume(struct platform_device *pdev)
339{
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);
345 }
346
347 return 0;
348}
349#else
350#define pda_power_suspend NULL
351#define pda_power_resume NULL
352#endif /* CONFIG_PM */
353
Anton Vorontsovb2998042007-05-04 00:32:17 +0400354static struct platform_driver pda_power_pdrv = {
355 .driver = {
356 .name = "pda-power",
357 },
358 .probe = pda_power_probe,
359 .remove = pda_power_remove,
Dmitry Baryshkov8f8e9b32008-01-13 02:35:43 +0300360 .suspend = pda_power_suspend,
361 .resume = pda_power_resume,
Anton Vorontsovb2998042007-05-04 00:32:17 +0400362};
363
364static int __init pda_power_init(void)
365{
366 return platform_driver_register(&pda_power_pdrv);
367}
368
369static void __exit pda_power_exit(void)
370{
371 platform_driver_unregister(&pda_power_pdrv);
Anton Vorontsovb2998042007-05-04 00:32:17 +0400372}
373
374module_init(pda_power_init);
375module_exit(pda_power_exit);
376MODULE_LICENSE("GPL");
377MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");