power_supply: Move run-time configuration to separate structure
[firefly-linux-kernel-4.4.55.git] / drivers / power / pda_power.c
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/err.h>
16 #include <linux/interrupt.h>
17 #include <linux/notifier.h>
18 #include <linux/power_supply.h>
19 #include <linux/pda_power.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/timer.h>
22 #include <linux/jiffies.h>
23 #include <linux/usb/otg.h>
24
25 static inline unsigned int get_irq_flags(struct resource *res)
26 {
27         return IRQF_SHARED | (res->flags & IRQF_TRIGGER_MASK);
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;
35 static struct timer_list polling_timer;
36 static int polling;
37
38 #if IS_ENABLED(CONFIG_USB_PHY)
39 static struct usb_phy *transceiver;
40 static struct notifier_block otg_nb;
41 #endif
42
43 static struct regulator *ac_draw;
44
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
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
83 static struct power_supply pda_psy_ac = {
84         .name = "ac",
85         .type = POWER_SUPPLY_TYPE_MAINS,
86         .properties = pda_power_props,
87         .num_properties = ARRAY_SIZE(pda_power_props),
88         .get_property = pda_power_get_property,
89 };
90
91 static struct power_supply pda_psy_usb = {
92         .name = "usb",
93         .type = POWER_SUPPLY_TYPE_USB,
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
108 static void update_charger(void)
109 {
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");
129                                 WARN_ON(regulator_enable(ac_draw));
130                                 regulator_enabled = 1;
131                         }
132                 } else {
133                         if (regulator_enabled) {
134                                 dev_dbg(dev, "charger off\n");
135                                 WARN_ON(regulator_disable(ac_draw));
136                                 regulator_enabled = 0;
137                         }
138                 }
139         }
140 }
141
142 static void supply_timer_func(unsigned long unused)
143 {
144         if (ac_status == PDA_PSY_TO_CHANGE) {
145                 ac_status = new_ac_status;
146                 power_supply_changed(&pda_psy_ac);
147         }
148
149         if (usb_status == PDA_PSY_TO_CHANGE) {
150                 usb_status = new_usb_status;
151                 power_supply_changed(&pda_psy_usb);
152         }
153 }
154
155 static void psy_changed(void)
156 {
157         update_charger();
158
159         /*
160          * Okay, charger set. Now wait a bit before notifying supplicants,
161          * charge power should stabilize.
162          */
163         mod_timer(&supply_timer,
164                   jiffies + msecs_to_jiffies(pdata->wait_for_charger));
165 }
166
167 static void charger_timer_func(unsigned long unused)
168 {
169         update_status();
170         psy_changed();
171 }
172
173 static irqreturn_t power_changed_isr(int irq, void *power_supply)
174 {
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          */
186         mod_timer(&charger_timer,
187                   jiffies + msecs_to_jiffies(pdata->wait_for_status));
188
189         return IRQ_HANDLED;
190 }
191
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
217 #if IS_ENABLED(CONFIG_USB_PHY)
218 static int otg_is_usb_online(void)
219 {
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;
256 }
257 #endif
258
259 static int pda_power_probe(struct platform_device *pdev)
260 {
261         struct power_supply_config psy_cfg = {};
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
275         if (pdata->init) {
276                 ret = pdata->init(dev);
277                 if (ret < 0)
278                         goto init_failed;
279         }
280
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;
285         }
286
287         update_status();
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
296         if (!pdata->polling_interval)
297                 pdata->polling_interval = 2000;
298
299         if (!pdata->ac_max_uA)
300                 pdata->ac_max_uA = 500000;
301
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");
307
308         if (pdata->supplied_to) {
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);
314         }
315
316 #if IS_ENABLED(CONFIG_USB_PHY)
317         transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
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;
323         }
324 #endif
325
326         if (pdata->is_ac_online) {
327                 ret = power_supply_register(&pdev->dev, &pda_psy_ac, &psy_cfg);
328                 if (ret) {
329                         dev_err(dev, "failed to register %s power supply\n",
330                                 pda_psy_ac.name);
331                         goto ac_supply_failed;
332                 }
333
334                 if (ac_irq) {
335                         ret = request_irq(ac_irq->start, power_changed_isr,
336                                           get_irq_flags(ac_irq), ac_irq->name,
337                                           &pda_psy_ac);
338                         if (ret) {
339                                 dev_err(dev, "request ac irq failed\n");
340                                 goto ac_irq_failed;
341                         }
342                 } else {
343                         polling = 1;
344                 }
345         }
346
347         if (pdata->is_usb_online) {
348                 ret = power_supply_register(&pdev->dev, &pda_psy_usb, &psy_cfg);
349                 if (ret) {
350                         dev_err(dev, "failed to register %s power supply\n",
351                                 pda_psy_usb.name);
352                         goto usb_supply_failed;
353                 }
354
355                 if (usb_irq) {
356                         ret = request_irq(usb_irq->start, power_changed_isr,
357                                           get_irq_flags(usb_irq),
358                                           usb_irq->name, &pda_psy_usb);
359                         if (ret) {
360                                 dev_err(dev, "request usb irq failed\n");
361                                 goto usb_irq_failed;
362                         }
363                 } else {
364                         polling = 1;
365                 }
366         }
367
368 #if IS_ENABLED(CONFIG_USB_PHY)
369         if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) {
370                 otg_nb.notifier_call = otg_handle_notification;
371                 ret = usb_register_notifier(transceiver, &otg_nb);
372                 if (ret) {
373                         dev_err(dev, "failure to register otg notifier\n");
374                         goto otg_reg_notifier_failed;
375                 }
376                 polling = 0;
377         }
378 #endif
379
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);
389
390         return 0;
391
392 #if IS_ENABLED(CONFIG_USB_PHY)
393 otg_reg_notifier_failed:
394         if (pdata->is_usb_online && usb_irq)
395                 free_irq(usb_irq->start, &pda_psy_usb);
396 #endif
397 usb_irq_failed:
398         if (pdata->is_usb_online)
399                 power_supply_unregister(&pda_psy_usb);
400 usb_supply_failed:
401         if (pdata->is_ac_online && ac_irq)
402                 free_irq(ac_irq->start, &pda_psy_ac);
403 #if IS_ENABLED(CONFIG_USB_PHY)
404         if (!IS_ERR_OR_NULL(transceiver))
405                 usb_put_phy(transceiver);
406 #endif
407 ac_irq_failed:
408         if (pdata->is_ac_online)
409                 power_supply_unregister(&pda_psy_ac);
410 ac_supply_failed:
411         if (ac_draw) {
412                 regulator_put(ac_draw);
413                 ac_draw = NULL;
414         }
415         if (pdata->exit)
416                 pdata->exit(dev);
417 init_failed:
418 wrongid:
419         return ret;
420 }
421
422 static int pda_power_remove(struct platform_device *pdev)
423 {
424         if (pdata->is_usb_online && usb_irq)
425                 free_irq(usb_irq->start, &pda_psy_usb);
426         if (pdata->is_ac_online && ac_irq)
427                 free_irq(ac_irq->start, &pda_psy_ac);
428
429         if (polling)
430                 del_timer_sync(&polling_timer);
431         del_timer_sync(&charger_timer);
432         del_timer_sync(&supply_timer);
433
434         if (pdata->is_usb_online)
435                 power_supply_unregister(&pda_psy_usb);
436         if (pdata->is_ac_online)
437                 power_supply_unregister(&pda_psy_ac);
438 #if IS_ENABLED(CONFIG_USB_PHY)
439         if (!IS_ERR_OR_NULL(transceiver))
440                 usb_put_phy(transceiver);
441 #endif
442         if (ac_draw) {
443                 regulator_put(ac_draw);
444                 ac_draw = NULL;
445         }
446         if (pdata->exit)
447                 pdata->exit(dev);
448
449         return 0;
450 }
451
452 #ifdef CONFIG_PM
453 static int ac_wakeup_enabled;
454 static int usb_wakeup_enabled;
455
456 static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
457 {
458         if (pdata->suspend) {
459                 int ret = pdata->suspend(state);
460
461                 if (ret)
462                         return ret;
463         }
464
465         if (device_may_wakeup(&pdev->dev)) {
466                 if (ac_irq)
467                         ac_wakeup_enabled = !enable_irq_wake(ac_irq->start);
468                 if (usb_irq)
469                         usb_wakeup_enabled = !enable_irq_wake(usb_irq->start);
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)) {
478                 if (usb_irq && usb_wakeup_enabled)
479                         disable_irq_wake(usb_irq->start);
480                 if (ac_irq && ac_wakeup_enabled)
481                         disable_irq_wake(ac_irq->start);
482         }
483
484         if (pdata->resume)
485                 return pdata->resume();
486
487         return 0;
488 }
489 #else
490 #define pda_power_suspend NULL
491 #define pda_power_resume NULL
492 #endif /* CONFIG_PM */
493
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,
500         .suspend = pda_power_suspend,
501         .resume = pda_power_resume,
502 };
503
504 module_platform_driver(pda_power_pdrv);
505
506 MODULE_LICENSE("GPL");
507 MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
508 MODULE_ALIAS("platform:pda-power");