Merge branch 'merge' of git://git.kernel.org/pub/scm/linux/kernel/git/benh/powerpc
[linux-block.git] / drivers / power / pda_power.c
CommitLineData
b2998049
AV
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.
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;
c3caebad
AV
35static struct timer_list polling_timer;
36static int polling;
b2998049 37
bfde2662
AV
38enum {
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
b2998049
AV
48static 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
bfde2662
AV
76static 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,
b2998049
AV
84};
85
bfde2662
AV
86static 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
b2998049
AV
105static void update_charger(void)
106{
107 if (!pdata->set_charge)
108 return;
109
bfde2662 110 if (new_ac_status > 0) {
b2998049
AV
111 dev_dbg(dev, "charger on (AC)\n");
112 pdata->set_charge(PDA_POWER_CHARGE_AC);
bfde2662 113 } else if (new_usb_status > 0) {
b2998049
AV
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);
119 }
b2998049
AV
120}
121
bfde2662 122static void supply_timer_func(unsigned long unused)
b2998049 123{
bfde2662
AV
124 if (ac_status == PDA_PSY_TO_CHANGE) {
125 ac_status = new_ac_status;
126 power_supply_changed(&pda_psy_ac);
127 }
5ebf6e6a 128
bfde2662
AV
129 if (usb_status == PDA_PSY_TO_CHANGE) {
130 usb_status = new_usb_status;
131 power_supply_changed(&pda_psy_usb);
132 }
b2998049
AV
133}
134
bfde2662 135static void psy_changed(void)
b2998049
AV
136{
137 update_charger();
138
bfde2662
AV
139 /*
140 * Okay, charger set. Now wait a bit before notifying supplicants,
141 * charge power should stabilize.
142 */
b2998049
AV
143 mod_timer(&supply_timer,
144 jiffies + msecs_to_jiffies(pdata->wait_for_charger));
b2998049
AV
145}
146
bfde2662
AV
147static void charger_timer_func(unsigned long unused)
148{
149 update_status();
150 psy_changed();
151}
152
5ebf6e6a 153static irqreturn_t power_changed_isr(int irq, void *power_supply)
b2998049 154{
bfde2662
AV
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;
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 */
b2998049
AV
166 mod_timer(&charger_timer,
167 jiffies + msecs_to_jiffies(pdata->wait_for_status));
bfde2662 168
b2998049
AV
169 return IRQ_HANDLED;
170}
171
c3caebad
AV
172static 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
b2998049
AV
197static 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
f6b6b180
PZ
212 if (pdata->init) {
213 ret = pdata->init(dev);
214 if (ret < 0)
215 goto init_failed;
216 }
217
bfde2662 218 update_status();
b2998049
AV
219 update_charger();
220
221 if (!pdata->wait_for_status)
222 pdata->wait_for_status = 500;
223
224 if (!pdata->wait_for_charger)
225 pdata->wait_for_charger = 500;
226
c3caebad
AV
227 if (!pdata->polling_interval)
228 pdata->polling_interval = 2000;
229
b2998049
AV
230 setup_timer(&charger_timer, charger_timer_func, 0);
231 setup_timer(&supply_timer, supply_timer_func, 0);
232
233 ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
234 usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
b2998049
AV
235
236 if (pdata->supplied_to) {
bfde2662
AV
237 pda_psy_ac.supplied_to = pdata->supplied_to;
238 pda_psy_ac.num_supplicants = pdata->num_supplicants;
239 pda_psy_usb.supplied_to = pdata->supplied_to;
240 pda_psy_usb.num_supplicants = pdata->num_supplicants;
b2998049
AV
241 }
242
9ef45106 243 if (pdata->is_ac_online) {
bfde2662 244 ret = power_supply_register(&pdev->dev, &pda_psy_ac);
9ef45106
DB
245 if (ret) {
246 dev_err(dev, "failed to register %s power supply\n",
bfde2662 247 pda_psy_ac.name);
9ef45106
DB
248 goto ac_supply_failed;
249 }
b2998049 250
9ef45106
DB
251 if (ac_irq) {
252 ret = request_irq(ac_irq->start, power_changed_isr,
253 get_irq_flags(ac_irq), ac_irq->name,
bfde2662 254 &pda_psy_ac);
9ef45106
DB
255 if (ret) {
256 dev_err(dev, "request ac irq failed\n");
257 goto ac_irq_failed;
258 }
c3caebad
AV
259 } else {
260 polling = 1;
9ef45106 261 }
b2998049
AV
262 }
263
9ef45106 264 if (pdata->is_usb_online) {
bfde2662 265 ret = power_supply_register(&pdev->dev, &pda_psy_usb);
b2998049 266 if (ret) {
9ef45106 267 dev_err(dev, "failed to register %s power supply\n",
bfde2662 268 pda_psy_usb.name);
9ef45106 269 goto usb_supply_failed;
b2998049 270 }
b2998049 271
9ef45106
DB
272 if (usb_irq) {
273 ret = request_irq(usb_irq->start, power_changed_isr,
274 get_irq_flags(usb_irq),
bfde2662 275 usb_irq->name, &pda_psy_usb);
9ef45106
DB
276 if (ret) {
277 dev_err(dev, "request usb irq failed\n");
278 goto usb_irq_failed;
279 }
c3caebad
AV
280 } else {
281 polling = 1;
b2998049
AV
282 }
283 }
284
c3caebad
AV
285 if (polling) {
286 dev_dbg(dev, "will poll for status\n");
287 setup_timer(&polling_timer, polling_timer_func, 0);
288 mod_timer(&polling_timer,
289 jiffies + msecs_to_jiffies(pdata->polling_interval));
290 }
291
292 if (ac_irq || usb_irq)
293 device_init_wakeup(&pdev->dev, 1);
8f8e9b38 294
9ef45106 295 return 0;
b2998049
AV
296
297usb_irq_failed:
9ef45106 298 if (pdata->is_usb_online)
bfde2662 299 power_supply_unregister(&pda_psy_usb);
9ef45106
DB
300usb_supply_failed:
301 if (pdata->is_ac_online && ac_irq)
bfde2662 302 free_irq(ac_irq->start, &pda_psy_ac);
b2998049 303ac_irq_failed:
9ef45106 304 if (pdata->is_ac_online)
bfde2662 305 power_supply_unregister(&pda_psy_ac);
9ef45106 306ac_supply_failed:
f6b6b180
PZ
307 if (pdata->exit)
308 pdata->exit(dev);
309init_failed:
b2998049 310wrongid:
b2998049
AV
311 return ret;
312}
313
314static int pda_power_remove(struct platform_device *pdev)
315{
9ef45106 316 if (pdata->is_usb_online && usb_irq)
bfde2662 317 free_irq(usb_irq->start, &pda_psy_usb);
9ef45106 318 if (pdata->is_ac_online && ac_irq)
bfde2662
AV
319 free_irq(ac_irq->start, &pda_psy_ac);
320
c3caebad
AV
321 if (polling)
322 del_timer_sync(&polling_timer);
b2998049
AV
323 del_timer_sync(&charger_timer);
324 del_timer_sync(&supply_timer);
bfde2662 325
9ef45106 326 if (pdata->is_usb_online)
bfde2662 327 power_supply_unregister(&pda_psy_usb);
9ef45106 328 if (pdata->is_ac_online)
bfde2662 329 power_supply_unregister(&pda_psy_ac);
f6b6b180
PZ
330 if (pdata->exit)
331 pdata->exit(dev);
bfde2662 332
b2998049
AV
333 return 0;
334}
335
8f8e9b38
DB
336#ifdef CONFIG_PM
337static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
338{
339 if (device_may_wakeup(&pdev->dev)) {
340 if (ac_irq)
341 enable_irq_wake(ac_irq->start);
342 if (usb_irq)
343 enable_irq_wake(usb_irq->start);
344 }
345
346 return 0;
347}
348
349static int pda_power_resume(struct platform_device *pdev)
350{
351 if (device_may_wakeup(&pdev->dev)) {
352 if (usb_irq)
353 disable_irq_wake(usb_irq->start);
354 if (ac_irq)
355 disable_irq_wake(ac_irq->start);
356 }
357
358 return 0;
359}
360#else
361#define pda_power_suspend NULL
362#define pda_power_resume NULL
363#endif /* CONFIG_PM */
364
2f5a5cf9
KS
365MODULE_ALIAS("platform:pda-power");
366
b2998049
AV
367static struct platform_driver pda_power_pdrv = {
368 .driver = {
369 .name = "pda-power",
370 },
371 .probe = pda_power_probe,
372 .remove = pda_power_remove,
8f8e9b38
DB
373 .suspend = pda_power_suspend,
374 .resume = pda_power_resume,
b2998049
AV
375};
376
377static int __init pda_power_init(void)
378{
379 return platform_driver_register(&pda_power_pdrv);
380}
381
382static void __exit pda_power_exit(void)
383{
384 platform_driver_unregister(&pda_power_pdrv);
b2998049
AV
385}
386
387module_init(pda_power_init);
388module_exit(pda_power_exit);
389MODULE_LICENSE("GPL");
390MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");