]>
Commit | Line | Data |
---|---|---|
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> | |
5bf2b994 | 15 | #include <linux/err.h> |
b2998049 | 16 | #include <linux/interrupt.h> |
9ad63986 | 17 | #include <linux/notifier.h> |
b2998049 AV |
18 | #include <linux/power_supply.h> |
19 | #include <linux/pda_power.h> | |
5bf2b994 | 20 | #include <linux/regulator/consumer.h> |
b2998049 AV |
21 | #include <linux/timer.h> |
22 | #include <linux/jiffies.h> | |
5bf2b994 | 23 | #include <linux/usb/otg.h> |
b2998049 AV |
24 | |
25 | static inline unsigned int get_irq_flags(struct resource *res) | |
26 | { | |
d554a3f9 | 27 | return IRQF_SHARED | (res->flags & IRQF_TRIGGER_MASK); |
b2998049 AV |
28 | } |
29 | ||
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; | |
c3caebad AV |
35 | static struct timer_list polling_timer; |
36 | static int polling; | |
b2998049 | 37 | |
820d0883 | 38 | #if IS_ENABLED(CONFIG_USB_PHY) |
86753811 | 39 | static struct usb_phy *transceiver; |
9ad63986 | 40 | static struct notifier_block otg_nb; |
1c74529d AL |
41 | #endif |
42 | ||
5bf2b994 PZ |
43 | static struct regulator *ac_draw; |
44 | ||
bfde2662 AV |
45 | enum { |
46 | PDA_PSY_OFFLINE = 0, | |
47 | PDA_PSY_ONLINE = 1, | |
48 | PDA_PSY_TO_CHANGE, | |
49 | }; | |
50 | static int new_ac_status = -1; | |
51 | static int new_usb_status = -1; | |
52 | static int ac_status = -1; | |
53 | static int usb_status = -1; | |
54 | ||
b2998049 AV |
55 | static int pda_power_get_property(struct power_supply *psy, |
56 | enum power_supply_property psp, | |
57 | union power_supply_propval *val) | |
58 | { | |
59 | switch (psp) { | |
60 | case POWER_SUPPLY_PROP_ONLINE: | |
61 | if (psy->type == POWER_SUPPLY_TYPE_MAINS) | |
62 | val->intval = pdata->is_ac_online ? | |
63 | pdata->is_ac_online() : 0; | |
64 | else | |
65 | val->intval = pdata->is_usb_online ? | |
66 | pdata->is_usb_online() : 0; | |
67 | break; | |
68 | default: | |
69 | return -EINVAL; | |
70 | } | |
71 | return 0; | |
72 | } | |
73 | ||
74 | static enum power_supply_property pda_power_props[] = { | |
75 | POWER_SUPPLY_PROP_ONLINE, | |
76 | }; | |
77 | ||
78 | static char *pda_power_supplied_to[] = { | |
79 | "main-battery", | |
80 | "backup-battery", | |
81 | }; | |
82 | ||
bfde2662 AV |
83 | static struct power_supply pda_psy_ac = { |
84 | .name = "ac", | |
85 | .type = POWER_SUPPLY_TYPE_MAINS, | |
bfde2662 AV |
86 | .properties = pda_power_props, |
87 | .num_properties = ARRAY_SIZE(pda_power_props), | |
88 | .get_property = pda_power_get_property, | |
b2998049 AV |
89 | }; |
90 | ||
bfde2662 AV |
91 | static struct power_supply pda_psy_usb = { |
92 | .name = "usb", | |
93 | .type = POWER_SUPPLY_TYPE_USB, | |
bfde2662 AV |
94 | .properties = pda_power_props, |
95 | .num_properties = ARRAY_SIZE(pda_power_props), | |
96 | .get_property = pda_power_get_property, | |
97 | }; | |
98 | ||
99 | static void update_status(void) | |
100 | { | |
101 | if (pdata->is_ac_online) | |
102 | new_ac_status = !!pdata->is_ac_online(); | |
103 | ||
104 | if (pdata->is_usb_online) | |
105 | new_usb_status = !!pdata->is_usb_online(); | |
106 | } | |
107 | ||
b2998049 AV |
108 | static void update_charger(void) |
109 | { | |
5bf2b994 PZ |
110 | static int regulator_enabled; |
111 | int max_uA = pdata->ac_max_uA; | |
112 | ||
113 | if (pdata->set_charge) { | |
114 | if (new_ac_status > 0) { | |
115 | dev_dbg(dev, "charger on (AC)\n"); | |
116 | pdata->set_charge(PDA_POWER_CHARGE_AC); | |
117 | } else if (new_usb_status > 0) { | |
118 | dev_dbg(dev, "charger on (USB)\n"); | |
119 | pdata->set_charge(PDA_POWER_CHARGE_USB); | |
120 | } else { | |
121 | dev_dbg(dev, "charger off\n"); | |
122 | pdata->set_charge(0); | |
123 | } | |
124 | } else if (ac_draw) { | |
125 | if (new_ac_status > 0) { | |
126 | regulator_set_current_limit(ac_draw, max_uA, max_uA); | |
127 | if (!regulator_enabled) { | |
128 | dev_dbg(dev, "charger on (AC)\n"); | |
92311c3c | 129 | WARN_ON(regulator_enable(ac_draw)); |
5bf2b994 PZ |
130 | regulator_enabled = 1; |
131 | } | |
132 | } else { | |
133 | if (regulator_enabled) { | |
134 | dev_dbg(dev, "charger off\n"); | |
92311c3c | 135 | WARN_ON(regulator_disable(ac_draw)); |
5bf2b994 PZ |
136 | regulator_enabled = 0; |
137 | } | |
138 | } | |
b2998049 | 139 | } |
b2998049 AV |
140 | } |
141 | ||
bfde2662 | 142 | static void supply_timer_func(unsigned long unused) |
b2998049 | 143 | { |
bfde2662 AV |
144 | if (ac_status == PDA_PSY_TO_CHANGE) { |
145 | ac_status = new_ac_status; | |
146 | power_supply_changed(&pda_psy_ac); | |
147 | } | |
5ebf6e6a | 148 | |
bfde2662 AV |
149 | if (usb_status == PDA_PSY_TO_CHANGE) { |
150 | usb_status = new_usb_status; | |
151 | power_supply_changed(&pda_psy_usb); | |
152 | } | |
b2998049 AV |
153 | } |
154 | ||
bfde2662 | 155 | static void psy_changed(void) |
b2998049 AV |
156 | { |
157 | update_charger(); | |
158 | ||
bfde2662 AV |
159 | /* |
160 | * Okay, charger set. Now wait a bit before notifying supplicants, | |
161 | * charge power should stabilize. | |
162 | */ | |
b2998049 AV |
163 | mod_timer(&supply_timer, |
164 | jiffies + msecs_to_jiffies(pdata->wait_for_charger)); | |
b2998049 AV |
165 | } |
166 | ||
bfde2662 AV |
167 | static void charger_timer_func(unsigned long unused) |
168 | { | |
169 | update_status(); | |
170 | psy_changed(); | |
171 | } | |
172 | ||
5ebf6e6a | 173 | static irqreturn_t power_changed_isr(int irq, void *power_supply) |
b2998049 | 174 | { |
bfde2662 AV |
175 | if (power_supply == &pda_psy_ac) |
176 | ac_status = PDA_PSY_TO_CHANGE; | |
177 | else if (power_supply == &pda_psy_usb) | |
178 | usb_status = PDA_PSY_TO_CHANGE; | |
179 | else | |
180 | return IRQ_NONE; | |
181 | ||
182 | /* | |
183 | * Wait a bit before reading ac/usb line status and setting charger, | |
184 | * because ac/usb status readings may lag from irq. | |
185 | */ | |
b2998049 AV |
186 | mod_timer(&charger_timer, |
187 | jiffies + msecs_to_jiffies(pdata->wait_for_status)); | |
bfde2662 | 188 | |
b2998049 AV |
189 | return IRQ_HANDLED; |
190 | } | |
191 | ||
c3caebad AV |
192 | static void polling_timer_func(unsigned long unused) |
193 | { | |
194 | int changed = 0; | |
195 | ||
196 | dev_dbg(dev, "polling...\n"); | |
197 | ||
198 | update_status(); | |
199 | ||
200 | if (!ac_irq && new_ac_status != ac_status) { | |
201 | ac_status = PDA_PSY_TO_CHANGE; | |
202 | changed = 1; | |
203 | } | |
204 | ||
205 | if (!usb_irq && new_usb_status != usb_status) { | |
206 | usb_status = PDA_PSY_TO_CHANGE; | |
207 | changed = 1; | |
208 | } | |
209 | ||
210 | if (changed) | |
211 | psy_changed(); | |
212 | ||
213 | mod_timer(&polling_timer, | |
214 | jiffies + msecs_to_jiffies(pdata->polling_interval)); | |
215 | } | |
216 | ||
820d0883 | 217 | #if IS_ENABLED(CONFIG_USB_PHY) |
5bf2b994 PZ |
218 | static int otg_is_usb_online(void) |
219 | { | |
9ad63986 DZ |
220 | return (transceiver->last_event == USB_EVENT_VBUS || |
221 | transceiver->last_event == USB_EVENT_ENUMERATED); | |
222 | } | |
223 | ||
224 | static int otg_is_ac_online(void) | |
225 | { | |
226 | return (transceiver->last_event == USB_EVENT_CHARGER); | |
227 | } | |
228 | ||
229 | static int otg_handle_notification(struct notifier_block *nb, | |
230 | unsigned long event, void *unused) | |
231 | { | |
232 | switch (event) { | |
233 | case USB_EVENT_CHARGER: | |
234 | ac_status = PDA_PSY_TO_CHANGE; | |
235 | break; | |
236 | case USB_EVENT_VBUS: | |
237 | case USB_EVENT_ENUMERATED: | |
238 | usb_status = PDA_PSY_TO_CHANGE; | |
239 | break; | |
240 | case USB_EVENT_NONE: | |
241 | ac_status = PDA_PSY_TO_CHANGE; | |
242 | usb_status = PDA_PSY_TO_CHANGE; | |
243 | break; | |
244 | default: | |
245 | return NOTIFY_OK; | |
246 | } | |
247 | ||
248 | /* | |
249 | * Wait a bit before reading ac/usb line status and setting charger, | |
250 | * because ac/usb status readings may lag from irq. | |
251 | */ | |
252 | mod_timer(&charger_timer, | |
253 | jiffies + msecs_to_jiffies(pdata->wait_for_status)); | |
254 | ||
255 | return NOTIFY_OK; | |
5bf2b994 PZ |
256 | } |
257 | #endif | |
258 | ||
b2998049 AV |
259 | static int pda_power_probe(struct platform_device *pdev) |
260 | { | |
2dc9215d | 261 | struct power_supply_config psy_cfg = {}; |
b2998049 AV |
262 | int ret = 0; |
263 | ||
264 | dev = &pdev->dev; | |
265 | ||
266 | if (pdev->id != -1) { | |
267 | dev_err(dev, "it's meaningless to register several " | |
268 | "pda_powers; use id = -1\n"); | |
269 | ret = -EINVAL; | |
270 | goto wrongid; | |
271 | } | |
272 | ||
273 | pdata = pdev->dev.platform_data; | |
274 | ||
f6b6b180 PZ |
275 | if (pdata->init) { |
276 | ret = pdata->init(dev); | |
277 | if (ret < 0) | |
278 | goto init_failed; | |
279 | } | |
280 | ||
ec60ea5c PP |
281 | ac_draw = regulator_get(dev, "ac_draw"); |
282 | if (IS_ERR(ac_draw)) { | |
283 | dev_dbg(dev, "couldn't get ac_draw regulator\n"); | |
284 | ac_draw = NULL; | |
ec60ea5c PP |
285 | } |
286 | ||
bfde2662 | 287 | update_status(); |
b2998049 AV |
288 | update_charger(); |
289 | ||
290 | if (!pdata->wait_for_status) | |
291 | pdata->wait_for_status = 500; | |
292 | ||
293 | if (!pdata->wait_for_charger) | |
294 | pdata->wait_for_charger = 500; | |
295 | ||
c3caebad AV |
296 | if (!pdata->polling_interval) |
297 | pdata->polling_interval = 2000; | |
298 | ||
5bf2b994 PZ |
299 | if (!pdata->ac_max_uA) |
300 | pdata->ac_max_uA = 500000; | |
301 | ||
b2998049 AV |
302 | setup_timer(&charger_timer, charger_timer_func, 0); |
303 | setup_timer(&supply_timer, supply_timer_func, 0); | |
304 | ||
305 | ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac"); | |
306 | usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb"); | |
b2998049 AV |
307 | |
308 | if (pdata->supplied_to) { | |
2dc9215d KK |
309 | psy_cfg.supplied_to = pdata->supplied_to; |
310 | psy_cfg.num_supplicants = pdata->num_supplicants; | |
311 | } else { | |
312 | psy_cfg.supplied_to = pda_power_supplied_to; | |
313 | psy_cfg.num_supplicants = ARRAY_SIZE(pda_power_supplied_to); | |
b2998049 AV |
314 | } |
315 | ||
820d0883 | 316 | #if IS_ENABLED(CONFIG_USB_PHY) |
662dca54 | 317 | transceiver = usb_get_phy(USB_PHY_TYPE_USB2); |
ded017ee KVA |
318 | if (!IS_ERR_OR_NULL(transceiver)) { |
319 | if (!pdata->is_usb_online) | |
320 | pdata->is_usb_online = otg_is_usb_online; | |
321 | if (!pdata->is_ac_online) | |
322 | pdata->is_ac_online = otg_is_ac_online; | |
9ad63986 | 323 | } |
1c74529d | 324 | #endif |
9ad63986 | 325 | |
9ef45106 | 326 | if (pdata->is_ac_online) { |
2dc9215d | 327 | ret = power_supply_register(&pdev->dev, &pda_psy_ac, &psy_cfg); |
9ef45106 DES |
328 | if (ret) { |
329 | dev_err(dev, "failed to register %s power supply\n", | |
bfde2662 | 330 | pda_psy_ac.name); |
9ef45106 DES |
331 | goto ac_supply_failed; |
332 | } | |
b2998049 | 333 | |
9ef45106 DES |
334 | if (ac_irq) { |
335 | ret = request_irq(ac_irq->start, power_changed_isr, | |
336 | get_irq_flags(ac_irq), ac_irq->name, | |
bfde2662 | 337 | &pda_psy_ac); |
9ef45106 DES |
338 | if (ret) { |
339 | dev_err(dev, "request ac irq failed\n"); | |
340 | goto ac_irq_failed; | |
341 | } | |
c3caebad AV |
342 | } else { |
343 | polling = 1; | |
9ef45106 | 344 | } |
b2998049 AV |
345 | } |
346 | ||
9ef45106 | 347 | if (pdata->is_usb_online) { |
2dc9215d | 348 | ret = power_supply_register(&pdev->dev, &pda_psy_usb, &psy_cfg); |
b2998049 | 349 | if (ret) { |
9ef45106 | 350 | dev_err(dev, "failed to register %s power supply\n", |
bfde2662 | 351 | pda_psy_usb.name); |
9ef45106 | 352 | goto usb_supply_failed; |
b2998049 | 353 | } |
b2998049 | 354 | |
9ef45106 DES |
355 | if (usb_irq) { |
356 | ret = request_irq(usb_irq->start, power_changed_isr, | |
357 | get_irq_flags(usb_irq), | |
bfde2662 | 358 | usb_irq->name, &pda_psy_usb); |
9ef45106 DES |
359 | if (ret) { |
360 | dev_err(dev, "request usb irq failed\n"); | |
361 | goto usb_irq_failed; | |
362 | } | |
c3caebad AV |
363 | } else { |
364 | polling = 1; | |
b2998049 AV |
365 | } |
366 | } | |
367 | ||
820d0883 | 368 | #if IS_ENABLED(CONFIG_USB_PHY) |
ded017ee | 369 | if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) { |
9ad63986 | 370 | otg_nb.notifier_call = otg_handle_notification; |
fcc8ebc9 | 371 | ret = usb_register_notifier(transceiver, &otg_nb); |
9ad63986 DZ |
372 | if (ret) { |
373 | dev_err(dev, "failure to register otg notifier\n"); | |
374 | goto otg_reg_notifier_failed; | |
375 | } | |
376 | polling = 0; | |
377 | } | |
1c74529d | 378 | #endif |
9ad63986 | 379 | |
c3caebad AV |
380 | if (polling) { |
381 | dev_dbg(dev, "will poll for status\n"); | |
382 | setup_timer(&polling_timer, polling_timer_func, 0); | |
383 | mod_timer(&polling_timer, | |
384 | jiffies + msecs_to_jiffies(pdata->polling_interval)); | |
385 | } | |
386 | ||
387 | if (ac_irq || usb_irq) | |
388 | device_init_wakeup(&pdev->dev, 1); | |
8f8e9b38 | 389 | |
9ef45106 | 390 | return 0; |
b2998049 | 391 | |
820d0883 | 392 | #if IS_ENABLED(CONFIG_USB_PHY) |
9ad63986 DZ |
393 | otg_reg_notifier_failed: |
394 | if (pdata->is_usb_online && usb_irq) | |
395 | free_irq(usb_irq->start, &pda_psy_usb); | |
1c74529d | 396 | #endif |
b2998049 | 397 | usb_irq_failed: |
9ef45106 | 398 | if (pdata->is_usb_online) |
bfde2662 | 399 | power_supply_unregister(&pda_psy_usb); |
9ef45106 DES |
400 | usb_supply_failed: |
401 | if (pdata->is_ac_online && ac_irq) | |
bfde2662 | 402 | free_irq(ac_irq->start, &pda_psy_ac); |
820d0883 | 403 | #if IS_ENABLED(CONFIG_USB_PHY) |
ded017ee | 404 | if (!IS_ERR_OR_NULL(transceiver)) |
721002ec | 405 | usb_put_phy(transceiver); |
1c74529d | 406 | #endif |
b2998049 | 407 | ac_irq_failed: |
9ef45106 | 408 | if (pdata->is_ac_online) |
bfde2662 | 409 | power_supply_unregister(&pda_psy_ac); |
9ef45106 | 410 | ac_supply_failed: |
5bf2b994 PZ |
411 | if (ac_draw) { |
412 | regulator_put(ac_draw); | |
413 | ac_draw = NULL; | |
414 | } | |
f6b6b180 PZ |
415 | if (pdata->exit) |
416 | pdata->exit(dev); | |
417 | init_failed: | |
b2998049 | 418 | wrongid: |
b2998049 AV |
419 | return ret; |
420 | } | |
421 | ||
422 | static int pda_power_remove(struct platform_device *pdev) | |
423 | { | |
9ef45106 | 424 | if (pdata->is_usb_online && usb_irq) |
bfde2662 | 425 | free_irq(usb_irq->start, &pda_psy_usb); |
9ef45106 | 426 | if (pdata->is_ac_online && ac_irq) |
bfde2662 AV |
427 | free_irq(ac_irq->start, &pda_psy_ac); |
428 | ||
c3caebad AV |
429 | if (polling) |
430 | del_timer_sync(&polling_timer); | |
b2998049 AV |
431 | del_timer_sync(&charger_timer); |
432 | del_timer_sync(&supply_timer); | |
bfde2662 | 433 | |
9ef45106 | 434 | if (pdata->is_usb_online) |
bfde2662 | 435 | power_supply_unregister(&pda_psy_usb); |
9ef45106 | 436 | if (pdata->is_ac_online) |
bfde2662 | 437 | power_supply_unregister(&pda_psy_ac); |
820d0883 | 438 | #if IS_ENABLED(CONFIG_USB_PHY) |
ded017ee | 439 | if (!IS_ERR_OR_NULL(transceiver)) |
721002ec | 440 | usb_put_phy(transceiver); |
5bf2b994 PZ |
441 | #endif |
442 | if (ac_draw) { | |
443 | regulator_put(ac_draw); | |
444 | ac_draw = NULL; | |
445 | } | |
f6b6b180 PZ |
446 | if (pdata->exit) |
447 | pdata->exit(dev); | |
bfde2662 | 448 | |
b2998049 AV |
449 | return 0; |
450 | } | |
451 | ||
8f8e9b38 | 452 | #ifdef CONFIG_PM |
e82374fd RJ |
453 | static int ac_wakeup_enabled; |
454 | static int usb_wakeup_enabled; | |
455 | ||
8f8e9b38 DES |
456 | static int pda_power_suspend(struct platform_device *pdev, pm_message_t state) |
457 | { | |
a3bcbbee DM |
458 | if (pdata->suspend) { |
459 | int ret = pdata->suspend(state); | |
460 | ||
461 | if (ret) | |
462 | return ret; | |
463 | } | |
464 | ||
8f8e9b38 DES |
465 | if (device_may_wakeup(&pdev->dev)) { |
466 | if (ac_irq) | |
e82374fd | 467 | ac_wakeup_enabled = !enable_irq_wake(ac_irq->start); |
8f8e9b38 | 468 | if (usb_irq) |
e82374fd | 469 | usb_wakeup_enabled = !enable_irq_wake(usb_irq->start); |
8f8e9b38 DES |
470 | } |
471 | ||
472 | return 0; | |
473 | } | |
474 | ||
475 | static int pda_power_resume(struct platform_device *pdev) | |
476 | { | |
477 | if (device_may_wakeup(&pdev->dev)) { | |
e82374fd | 478 | if (usb_irq && usb_wakeup_enabled) |
8f8e9b38 | 479 | disable_irq_wake(usb_irq->start); |
e82374fd | 480 | if (ac_irq && ac_wakeup_enabled) |
8f8e9b38 DES |
481 | disable_irq_wake(ac_irq->start); |
482 | } | |
483 | ||
a3bcbbee DM |
484 | if (pdata->resume) |
485 | return pdata->resume(); | |
486 | ||
8f8e9b38 DES |
487 | return 0; |
488 | } | |
489 | #else | |
490 | #define pda_power_suspend NULL | |
491 | #define pda_power_resume NULL | |
492 | #endif /* CONFIG_PM */ | |
493 | ||
b2998049 AV |
494 | static struct platform_driver pda_power_pdrv = { |
495 | .driver = { | |
496 | .name = "pda-power", | |
497 | }, | |
498 | .probe = pda_power_probe, | |
499 | .remove = pda_power_remove, | |
8f8e9b38 DES |
500 | .suspend = pda_power_suspend, |
501 | .resume = pda_power_resume, | |
b2998049 AV |
502 | }; |
503 | ||
300bac7f | 504 | module_platform_driver(pda_power_pdrv); |
b2998049 | 505 | |
b2998049 AV |
506 | MODULE_LICENSE("GPL"); |
507 | MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>"); | |
300bac7f | 508 | MODULE_ALIAS("platform:pda-power"); |