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