Commit 5bf2b994bfe11bfe86231050897b2d881ca544d9
Committed by
Anton Vorontsov
1 parent
cc52a29e62
Exists in
master
and in
7 other branches
pda_power: Add optional OTG transceiver and voltage regulator support
This patch allows machines to use an OTG transceiver driver instead of supplying a custom is_usb_online callback to check USB power. Also, in the case that the OTG transceiver handles charger control when connected to USB, a regulator named "ac_draw" can be supplied instead of the custom set_charge callback to control the charger when connected to AC. The check for (transceiver->state == OTG_STATE_B_PERIPHERAL) in otg_is_usb_online is probably too simple, I'm just using this with a peripheral only device and gpio_vbus + bq24022. I'm not sure which other OTG states can supply power. Signed-off-by: Philipp Zabel <philipp.zabel@gmail.com> Signed-off-by: Anton Vorontsov <cbouatmailru@gmail.com>
Showing 2 changed files with 79 additions and 12 deletions Inline Diff
drivers/power/pda_power.c
1 | /* | 1 | /* |
2 | * Common power driver for PDAs and phones with one or two external | 2 | * Common power driver for PDAs and phones with one or two external |
3 | * power supplies (AC/USB) connected to main and backup batteries, | 3 | * power supplies (AC/USB) connected to main and backup batteries, |
4 | * and optional builtin charger. | 4 | * and optional builtin charger. |
5 | * | 5 | * |
6 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | 6 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> |
7 | * | 7 | * |
8 | * This program is free software; you can redistribute it and/or modify | 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 | 9 | * it under the terms of the GNU General Public License version 2 as |
10 | * published by the Free Software Foundation. | 10 | * published by the Free Software Foundation. |
11 | */ | 11 | */ |
12 | 12 | ||
13 | #include <linux/module.h> | 13 | #include <linux/module.h> |
14 | #include <linux/platform_device.h> | 14 | #include <linux/platform_device.h> |
15 | #include <linux/err.h> | ||
15 | #include <linux/interrupt.h> | 16 | #include <linux/interrupt.h> |
16 | #include <linux/power_supply.h> | 17 | #include <linux/power_supply.h> |
17 | #include <linux/pda_power.h> | 18 | #include <linux/pda_power.h> |
19 | #include <linux/regulator/consumer.h> | ||
18 | #include <linux/timer.h> | 20 | #include <linux/timer.h> |
19 | #include <linux/jiffies.h> | 21 | #include <linux/jiffies.h> |
22 | #include <linux/usb/otg.h> | ||
20 | 23 | ||
21 | static inline unsigned int get_irq_flags(struct resource *res) | 24 | static inline unsigned int get_irq_flags(struct resource *res) |
22 | { | 25 | { |
23 | unsigned int flags = IRQF_SAMPLE_RANDOM | IRQF_SHARED; | 26 | unsigned int flags = IRQF_SAMPLE_RANDOM | IRQF_SHARED; |
24 | 27 | ||
25 | flags |= res->flags & IRQF_TRIGGER_MASK; | 28 | flags |= res->flags & IRQF_TRIGGER_MASK; |
26 | 29 | ||
27 | return flags; | 30 | return flags; |
28 | } | 31 | } |
29 | 32 | ||
30 | static struct device *dev; | 33 | static struct device *dev; |
31 | static struct pda_power_pdata *pdata; | 34 | static struct pda_power_pdata *pdata; |
32 | static struct resource *ac_irq, *usb_irq; | 35 | static struct resource *ac_irq, *usb_irq; |
33 | static struct timer_list charger_timer; | 36 | static struct timer_list charger_timer; |
34 | static struct timer_list supply_timer; | 37 | static struct timer_list supply_timer; |
35 | static struct timer_list polling_timer; | 38 | static struct timer_list polling_timer; |
36 | static int polling; | 39 | static int polling; |
37 | 40 | ||
41 | #ifdef CONFIG_USB_OTG_UTILS | ||
42 | static struct otg_transceiver *transceiver; | ||
43 | #endif | ||
44 | static struct regulator *ac_draw; | ||
45 | |||
38 | enum { | 46 | enum { |
39 | PDA_PSY_OFFLINE = 0, | 47 | PDA_PSY_OFFLINE = 0, |
40 | PDA_PSY_ONLINE = 1, | 48 | PDA_PSY_ONLINE = 1, |
41 | PDA_PSY_TO_CHANGE, | 49 | PDA_PSY_TO_CHANGE, |
42 | }; | 50 | }; |
43 | static int new_ac_status = -1; | 51 | static int new_ac_status = -1; |
44 | static int new_usb_status = -1; | 52 | static int new_usb_status = -1; |
45 | static int ac_status = -1; | 53 | static int ac_status = -1; |
46 | static int usb_status = -1; | 54 | static int usb_status = -1; |
47 | 55 | ||
48 | static int pda_power_get_property(struct power_supply *psy, | 56 | static int pda_power_get_property(struct power_supply *psy, |
49 | enum power_supply_property psp, | 57 | enum power_supply_property psp, |
50 | union power_supply_propval *val) | 58 | union power_supply_propval *val) |
51 | { | 59 | { |
52 | switch (psp) { | 60 | switch (psp) { |
53 | case POWER_SUPPLY_PROP_ONLINE: | 61 | case POWER_SUPPLY_PROP_ONLINE: |
54 | if (psy->type == POWER_SUPPLY_TYPE_MAINS) | 62 | if (psy->type == POWER_SUPPLY_TYPE_MAINS) |
55 | val->intval = pdata->is_ac_online ? | 63 | val->intval = pdata->is_ac_online ? |
56 | pdata->is_ac_online() : 0; | 64 | pdata->is_ac_online() : 0; |
57 | else | 65 | else |
58 | val->intval = pdata->is_usb_online ? | 66 | val->intval = pdata->is_usb_online ? |
59 | pdata->is_usb_online() : 0; | 67 | pdata->is_usb_online() : 0; |
60 | break; | 68 | break; |
61 | default: | 69 | default: |
62 | return -EINVAL; | 70 | return -EINVAL; |
63 | } | 71 | } |
64 | return 0; | 72 | return 0; |
65 | } | 73 | } |
66 | 74 | ||
67 | static enum power_supply_property pda_power_props[] = { | 75 | static enum power_supply_property pda_power_props[] = { |
68 | POWER_SUPPLY_PROP_ONLINE, | 76 | POWER_SUPPLY_PROP_ONLINE, |
69 | }; | 77 | }; |
70 | 78 | ||
71 | static char *pda_power_supplied_to[] = { | 79 | static char *pda_power_supplied_to[] = { |
72 | "main-battery", | 80 | "main-battery", |
73 | "backup-battery", | 81 | "backup-battery", |
74 | }; | 82 | }; |
75 | 83 | ||
76 | static struct power_supply pda_psy_ac = { | 84 | static struct power_supply pda_psy_ac = { |
77 | .name = "ac", | 85 | .name = "ac", |
78 | .type = POWER_SUPPLY_TYPE_MAINS, | 86 | .type = POWER_SUPPLY_TYPE_MAINS, |
79 | .supplied_to = pda_power_supplied_to, | 87 | .supplied_to = pda_power_supplied_to, |
80 | .num_supplicants = ARRAY_SIZE(pda_power_supplied_to), | 88 | .num_supplicants = ARRAY_SIZE(pda_power_supplied_to), |
81 | .properties = pda_power_props, | 89 | .properties = pda_power_props, |
82 | .num_properties = ARRAY_SIZE(pda_power_props), | 90 | .num_properties = ARRAY_SIZE(pda_power_props), |
83 | .get_property = pda_power_get_property, | 91 | .get_property = pda_power_get_property, |
84 | }; | 92 | }; |
85 | 93 | ||
86 | static struct power_supply pda_psy_usb = { | 94 | static struct power_supply pda_psy_usb = { |
87 | .name = "usb", | 95 | .name = "usb", |
88 | .type = POWER_SUPPLY_TYPE_USB, | 96 | .type = POWER_SUPPLY_TYPE_USB, |
89 | .supplied_to = pda_power_supplied_to, | 97 | .supplied_to = pda_power_supplied_to, |
90 | .num_supplicants = ARRAY_SIZE(pda_power_supplied_to), | 98 | .num_supplicants = ARRAY_SIZE(pda_power_supplied_to), |
91 | .properties = pda_power_props, | 99 | .properties = pda_power_props, |
92 | .num_properties = ARRAY_SIZE(pda_power_props), | 100 | .num_properties = ARRAY_SIZE(pda_power_props), |
93 | .get_property = pda_power_get_property, | 101 | .get_property = pda_power_get_property, |
94 | }; | 102 | }; |
95 | 103 | ||
96 | static void update_status(void) | 104 | static void update_status(void) |
97 | { | 105 | { |
98 | if (pdata->is_ac_online) | 106 | if (pdata->is_ac_online) |
99 | new_ac_status = !!pdata->is_ac_online(); | 107 | new_ac_status = !!pdata->is_ac_online(); |
100 | 108 | ||
101 | if (pdata->is_usb_online) | 109 | if (pdata->is_usb_online) |
102 | new_usb_status = !!pdata->is_usb_online(); | 110 | new_usb_status = !!pdata->is_usb_online(); |
103 | } | 111 | } |
104 | 112 | ||
105 | static void update_charger(void) | 113 | static void update_charger(void) |
106 | { | 114 | { |
107 | if (!pdata->set_charge) | 115 | static int regulator_enabled; |
108 | return; | 116 | int max_uA = pdata->ac_max_uA; |
109 | 117 | ||
110 | if (new_ac_status > 0) { | 118 | if (pdata->set_charge) { |
111 | dev_dbg(dev, "charger on (AC)\n"); | 119 | if (new_ac_status > 0) { |
112 | pdata->set_charge(PDA_POWER_CHARGE_AC); | 120 | dev_dbg(dev, "charger on (AC)\n"); |
113 | } else if (new_usb_status > 0) { | 121 | pdata->set_charge(PDA_POWER_CHARGE_AC); |
114 | dev_dbg(dev, "charger on (USB)\n"); | 122 | } else if (new_usb_status > 0) { |
115 | pdata->set_charge(PDA_POWER_CHARGE_USB); | 123 | dev_dbg(dev, "charger on (USB)\n"); |
116 | } else { | 124 | pdata->set_charge(PDA_POWER_CHARGE_USB); |
117 | dev_dbg(dev, "charger off\n"); | 125 | } else { |
118 | pdata->set_charge(0); | 126 | dev_dbg(dev, "charger off\n"); |
127 | pdata->set_charge(0); | ||
128 | } | ||
129 | } else if (ac_draw) { | ||
130 | if (new_ac_status > 0) { | ||
131 | regulator_set_current_limit(ac_draw, max_uA, max_uA); | ||
132 | if (!regulator_enabled) { | ||
133 | dev_dbg(dev, "charger on (AC)\n"); | ||
134 | regulator_enable(ac_draw); | ||
135 | regulator_enabled = 1; | ||
136 | } | ||
137 | } else { | ||
138 | if (regulator_enabled) { | ||
139 | dev_dbg(dev, "charger off\n"); | ||
140 | regulator_disable(ac_draw); | ||
141 | regulator_enabled = 0; | ||
142 | } | ||
143 | } | ||
119 | } | 144 | } |
120 | } | 145 | } |
121 | 146 | ||
122 | static void supply_timer_func(unsigned long unused) | 147 | static void supply_timer_func(unsigned long unused) |
123 | { | 148 | { |
124 | if (ac_status == PDA_PSY_TO_CHANGE) { | 149 | if (ac_status == PDA_PSY_TO_CHANGE) { |
125 | ac_status = new_ac_status; | 150 | ac_status = new_ac_status; |
126 | power_supply_changed(&pda_psy_ac); | 151 | power_supply_changed(&pda_psy_ac); |
127 | } | 152 | } |
128 | 153 | ||
129 | if (usb_status == PDA_PSY_TO_CHANGE) { | 154 | if (usb_status == PDA_PSY_TO_CHANGE) { |
130 | usb_status = new_usb_status; | 155 | usb_status = new_usb_status; |
131 | power_supply_changed(&pda_psy_usb); | 156 | power_supply_changed(&pda_psy_usb); |
132 | } | 157 | } |
133 | } | 158 | } |
134 | 159 | ||
135 | static void psy_changed(void) | 160 | static void psy_changed(void) |
136 | { | 161 | { |
137 | update_charger(); | 162 | update_charger(); |
138 | 163 | ||
139 | /* | 164 | /* |
140 | * Okay, charger set. Now wait a bit before notifying supplicants, | 165 | * Okay, charger set. Now wait a bit before notifying supplicants, |
141 | * charge power should stabilize. | 166 | * charge power should stabilize. |
142 | */ | 167 | */ |
143 | mod_timer(&supply_timer, | 168 | mod_timer(&supply_timer, |
144 | jiffies + msecs_to_jiffies(pdata->wait_for_charger)); | 169 | jiffies + msecs_to_jiffies(pdata->wait_for_charger)); |
145 | } | 170 | } |
146 | 171 | ||
147 | static void charger_timer_func(unsigned long unused) | 172 | static void charger_timer_func(unsigned long unused) |
148 | { | 173 | { |
149 | update_status(); | 174 | update_status(); |
150 | psy_changed(); | 175 | psy_changed(); |
151 | } | 176 | } |
152 | 177 | ||
153 | static irqreturn_t power_changed_isr(int irq, void *power_supply) | 178 | static irqreturn_t power_changed_isr(int irq, void *power_supply) |
154 | { | 179 | { |
155 | if (power_supply == &pda_psy_ac) | 180 | if (power_supply == &pda_psy_ac) |
156 | ac_status = PDA_PSY_TO_CHANGE; | 181 | ac_status = PDA_PSY_TO_CHANGE; |
157 | else if (power_supply == &pda_psy_usb) | 182 | else if (power_supply == &pda_psy_usb) |
158 | usb_status = PDA_PSY_TO_CHANGE; | 183 | usb_status = PDA_PSY_TO_CHANGE; |
159 | else | 184 | else |
160 | return IRQ_NONE; | 185 | return IRQ_NONE; |
161 | 186 | ||
162 | /* | 187 | /* |
163 | * Wait a bit before reading ac/usb line status and setting charger, | 188 | * Wait a bit before reading ac/usb line status and setting charger, |
164 | * because ac/usb status readings may lag from irq. | 189 | * because ac/usb status readings may lag from irq. |
165 | */ | 190 | */ |
166 | mod_timer(&charger_timer, | 191 | mod_timer(&charger_timer, |
167 | jiffies + msecs_to_jiffies(pdata->wait_for_status)); | 192 | jiffies + msecs_to_jiffies(pdata->wait_for_status)); |
168 | 193 | ||
169 | return IRQ_HANDLED; | 194 | return IRQ_HANDLED; |
170 | } | 195 | } |
171 | 196 | ||
172 | static void polling_timer_func(unsigned long unused) | 197 | static void polling_timer_func(unsigned long unused) |
173 | { | 198 | { |
174 | int changed = 0; | 199 | int changed = 0; |
175 | 200 | ||
176 | dev_dbg(dev, "polling...\n"); | 201 | dev_dbg(dev, "polling...\n"); |
177 | 202 | ||
178 | update_status(); | 203 | update_status(); |
179 | 204 | ||
180 | if (!ac_irq && new_ac_status != ac_status) { | 205 | if (!ac_irq && new_ac_status != ac_status) { |
181 | ac_status = PDA_PSY_TO_CHANGE; | 206 | ac_status = PDA_PSY_TO_CHANGE; |
182 | changed = 1; | 207 | changed = 1; |
183 | } | 208 | } |
184 | 209 | ||
185 | if (!usb_irq && new_usb_status != usb_status) { | 210 | if (!usb_irq && new_usb_status != usb_status) { |
186 | usb_status = PDA_PSY_TO_CHANGE; | 211 | usb_status = PDA_PSY_TO_CHANGE; |
187 | changed = 1; | 212 | changed = 1; |
188 | } | 213 | } |
189 | 214 | ||
190 | if (changed) | 215 | if (changed) |
191 | psy_changed(); | 216 | psy_changed(); |
192 | 217 | ||
193 | mod_timer(&polling_timer, | 218 | mod_timer(&polling_timer, |
194 | jiffies + msecs_to_jiffies(pdata->polling_interval)); | 219 | jiffies + msecs_to_jiffies(pdata->polling_interval)); |
195 | } | 220 | } |
196 | 221 | ||
222 | #ifdef CONFIG_USB_OTG_UTILS | ||
223 | static int otg_is_usb_online(void) | ||
224 | { | ||
225 | return (transceiver->state == OTG_STATE_B_PERIPHERAL); | ||
226 | } | ||
227 | #endif | ||
228 | |||
197 | static int pda_power_probe(struct platform_device *pdev) | 229 | static int pda_power_probe(struct platform_device *pdev) |
198 | { | 230 | { |
199 | int ret = 0; | 231 | int ret = 0; |
200 | 232 | ||
201 | dev = &pdev->dev; | 233 | dev = &pdev->dev; |
202 | 234 | ||
203 | if (pdev->id != -1) { | 235 | if (pdev->id != -1) { |
204 | dev_err(dev, "it's meaningless to register several " | 236 | dev_err(dev, "it's meaningless to register several " |
205 | "pda_powers; use id = -1\n"); | 237 | "pda_powers; use id = -1\n"); |
206 | ret = -EINVAL; | 238 | ret = -EINVAL; |
207 | goto wrongid; | 239 | goto wrongid; |
208 | } | 240 | } |
209 | 241 | ||
210 | pdata = pdev->dev.platform_data; | 242 | pdata = pdev->dev.platform_data; |
211 | 243 | ||
212 | if (pdata->init) { | 244 | if (pdata->init) { |
213 | ret = pdata->init(dev); | 245 | ret = pdata->init(dev); |
214 | if (ret < 0) | 246 | if (ret < 0) |
215 | goto init_failed; | 247 | goto init_failed; |
216 | } | 248 | } |
217 | 249 | ||
218 | update_status(); | 250 | update_status(); |
219 | update_charger(); | 251 | update_charger(); |
220 | 252 | ||
221 | if (!pdata->wait_for_status) | 253 | if (!pdata->wait_for_status) |
222 | pdata->wait_for_status = 500; | 254 | pdata->wait_for_status = 500; |
223 | 255 | ||
224 | if (!pdata->wait_for_charger) | 256 | if (!pdata->wait_for_charger) |
225 | pdata->wait_for_charger = 500; | 257 | pdata->wait_for_charger = 500; |
226 | 258 | ||
227 | if (!pdata->polling_interval) | 259 | if (!pdata->polling_interval) |
228 | pdata->polling_interval = 2000; | 260 | pdata->polling_interval = 2000; |
229 | 261 | ||
262 | if (!pdata->ac_max_uA) | ||
263 | pdata->ac_max_uA = 500000; | ||
264 | |||
230 | setup_timer(&charger_timer, charger_timer_func, 0); | 265 | setup_timer(&charger_timer, charger_timer_func, 0); |
231 | setup_timer(&supply_timer, supply_timer_func, 0); | 266 | setup_timer(&supply_timer, supply_timer_func, 0); |
232 | 267 | ||
233 | ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac"); | 268 | ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac"); |
234 | usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb"); | 269 | usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb"); |
235 | 270 | ||
236 | if (pdata->supplied_to) { | 271 | if (pdata->supplied_to) { |
237 | pda_psy_ac.supplied_to = pdata->supplied_to; | 272 | pda_psy_ac.supplied_to = pdata->supplied_to; |
238 | pda_psy_ac.num_supplicants = pdata->num_supplicants; | 273 | pda_psy_ac.num_supplicants = pdata->num_supplicants; |
239 | pda_psy_usb.supplied_to = pdata->supplied_to; | 274 | pda_psy_usb.supplied_to = pdata->supplied_to; |
240 | pda_psy_usb.num_supplicants = pdata->num_supplicants; | 275 | pda_psy_usb.num_supplicants = pdata->num_supplicants; |
241 | } | 276 | } |
242 | 277 | ||
278 | ac_draw = regulator_get(dev, "ac_draw"); | ||
279 | if (IS_ERR(ac_draw)) { | ||
280 | dev_dbg(dev, "couldn't get ac_draw regulator\n"); | ||
281 | ac_draw = NULL; | ||
282 | ret = PTR_ERR(ac_draw); | ||
283 | } | ||
284 | |||
243 | if (pdata->is_ac_online) { | 285 | if (pdata->is_ac_online) { |
244 | ret = power_supply_register(&pdev->dev, &pda_psy_ac); | 286 | ret = power_supply_register(&pdev->dev, &pda_psy_ac); |
245 | if (ret) { | 287 | if (ret) { |
246 | dev_err(dev, "failed to register %s power supply\n", | 288 | dev_err(dev, "failed to register %s power supply\n", |
247 | pda_psy_ac.name); | 289 | pda_psy_ac.name); |
248 | goto ac_supply_failed; | 290 | goto ac_supply_failed; |
249 | } | 291 | } |
250 | 292 | ||
251 | if (ac_irq) { | 293 | if (ac_irq) { |
252 | ret = request_irq(ac_irq->start, power_changed_isr, | 294 | ret = request_irq(ac_irq->start, power_changed_isr, |
253 | get_irq_flags(ac_irq), ac_irq->name, | 295 | get_irq_flags(ac_irq), ac_irq->name, |
254 | &pda_psy_ac); | 296 | &pda_psy_ac); |
255 | if (ret) { | 297 | if (ret) { |
256 | dev_err(dev, "request ac irq failed\n"); | 298 | dev_err(dev, "request ac irq failed\n"); |
257 | goto ac_irq_failed; | 299 | goto ac_irq_failed; |
258 | } | 300 | } |
259 | } else { | 301 | } else { |
260 | polling = 1; | 302 | polling = 1; |
261 | } | 303 | } |
262 | } | 304 | } |
263 | 305 | ||
306 | #ifdef CONFIG_USB_OTG_UTILS | ||
307 | transceiver = otg_get_transceiver(); | ||
308 | if (transceiver && !pdata->is_usb_online) { | ||
309 | pdata->is_usb_online = otg_is_usb_online; | ||
310 | } | ||
311 | #endif | ||
312 | |||
264 | if (pdata->is_usb_online) { | 313 | if (pdata->is_usb_online) { |
265 | ret = power_supply_register(&pdev->dev, &pda_psy_usb); | 314 | ret = power_supply_register(&pdev->dev, &pda_psy_usb); |
266 | if (ret) { | 315 | if (ret) { |
267 | dev_err(dev, "failed to register %s power supply\n", | 316 | dev_err(dev, "failed to register %s power supply\n", |
268 | pda_psy_usb.name); | 317 | pda_psy_usb.name); |
269 | goto usb_supply_failed; | 318 | goto usb_supply_failed; |
270 | } | 319 | } |
271 | 320 | ||
272 | if (usb_irq) { | 321 | if (usb_irq) { |
273 | ret = request_irq(usb_irq->start, power_changed_isr, | 322 | ret = request_irq(usb_irq->start, power_changed_isr, |
274 | get_irq_flags(usb_irq), | 323 | get_irq_flags(usb_irq), |
275 | usb_irq->name, &pda_psy_usb); | 324 | usb_irq->name, &pda_psy_usb); |
276 | if (ret) { | 325 | if (ret) { |
277 | dev_err(dev, "request usb irq failed\n"); | 326 | dev_err(dev, "request usb irq failed\n"); |
278 | goto usb_irq_failed; | 327 | goto usb_irq_failed; |
279 | } | 328 | } |
280 | } else { | 329 | } else { |
281 | polling = 1; | 330 | polling = 1; |
282 | } | 331 | } |
283 | } | 332 | } |
284 | 333 | ||
285 | if (polling) { | 334 | if (polling) { |
286 | dev_dbg(dev, "will poll for status\n"); | 335 | dev_dbg(dev, "will poll for status\n"); |
287 | setup_timer(&polling_timer, polling_timer_func, 0); | 336 | setup_timer(&polling_timer, polling_timer_func, 0); |
288 | mod_timer(&polling_timer, | 337 | mod_timer(&polling_timer, |
289 | jiffies + msecs_to_jiffies(pdata->polling_interval)); | 338 | jiffies + msecs_to_jiffies(pdata->polling_interval)); |
290 | } | 339 | } |
291 | 340 | ||
292 | if (ac_irq || usb_irq) | 341 | if (ac_irq || usb_irq) |
293 | device_init_wakeup(&pdev->dev, 1); | 342 | device_init_wakeup(&pdev->dev, 1); |
294 | 343 | ||
295 | return 0; | 344 | return 0; |
296 | 345 | ||
297 | usb_irq_failed: | 346 | usb_irq_failed: |
298 | if (pdata->is_usb_online) | 347 | if (pdata->is_usb_online) |
299 | power_supply_unregister(&pda_psy_usb); | 348 | power_supply_unregister(&pda_psy_usb); |
300 | usb_supply_failed: | 349 | usb_supply_failed: |
301 | if (pdata->is_ac_online && ac_irq) | 350 | if (pdata->is_ac_online && ac_irq) |
302 | free_irq(ac_irq->start, &pda_psy_ac); | 351 | free_irq(ac_irq->start, &pda_psy_ac); |
352 | #ifdef CONFIG_USB_OTG_UTILS | ||
353 | if (transceiver) | ||
354 | otg_put_transceiver(transceiver); | ||
355 | #endif | ||
303 | ac_irq_failed: | 356 | ac_irq_failed: |
304 | if (pdata->is_ac_online) | 357 | if (pdata->is_ac_online) |
305 | power_supply_unregister(&pda_psy_ac); | 358 | power_supply_unregister(&pda_psy_ac); |
306 | ac_supply_failed: | 359 | ac_supply_failed: |
360 | if (ac_draw) { | ||
361 | regulator_put(ac_draw); | ||
362 | ac_draw = NULL; | ||
363 | } | ||
307 | if (pdata->exit) | 364 | if (pdata->exit) |
308 | pdata->exit(dev); | 365 | pdata->exit(dev); |
309 | init_failed: | 366 | init_failed: |
310 | wrongid: | 367 | wrongid: |
311 | return ret; | 368 | return ret; |
312 | } | 369 | } |
313 | 370 | ||
314 | static int pda_power_remove(struct platform_device *pdev) | 371 | static int pda_power_remove(struct platform_device *pdev) |
315 | { | 372 | { |
316 | if (pdata->is_usb_online && usb_irq) | 373 | if (pdata->is_usb_online && usb_irq) |
317 | free_irq(usb_irq->start, &pda_psy_usb); | 374 | free_irq(usb_irq->start, &pda_psy_usb); |
318 | if (pdata->is_ac_online && ac_irq) | 375 | if (pdata->is_ac_online && ac_irq) |
319 | free_irq(ac_irq->start, &pda_psy_ac); | 376 | free_irq(ac_irq->start, &pda_psy_ac); |
320 | 377 | ||
321 | if (polling) | 378 | if (polling) |
322 | del_timer_sync(&polling_timer); | 379 | del_timer_sync(&polling_timer); |
323 | del_timer_sync(&charger_timer); | 380 | del_timer_sync(&charger_timer); |
324 | del_timer_sync(&supply_timer); | 381 | del_timer_sync(&supply_timer); |
325 | 382 | ||
326 | if (pdata->is_usb_online) | 383 | if (pdata->is_usb_online) |
327 | power_supply_unregister(&pda_psy_usb); | 384 | power_supply_unregister(&pda_psy_usb); |
328 | if (pdata->is_ac_online) | 385 | if (pdata->is_ac_online) |
329 | power_supply_unregister(&pda_psy_ac); | 386 | power_supply_unregister(&pda_psy_ac); |
387 | #ifdef CONFIG_USB_OTG_UTILS | ||
388 | if (transceiver) | ||
389 | otg_put_transceiver(transceiver); | ||
390 | #endif | ||
391 | if (ac_draw) { | ||
392 | regulator_put(ac_draw); | ||
393 | ac_draw = NULL; | ||
394 | } | ||
330 | if (pdata->exit) | 395 | if (pdata->exit) |
331 | pdata->exit(dev); | 396 | pdata->exit(dev); |
332 | 397 | ||
333 | return 0; | 398 | return 0; |
334 | } | 399 | } |
335 | 400 | ||
336 | #ifdef CONFIG_PM | 401 | #ifdef CONFIG_PM |
337 | static int ac_wakeup_enabled; | 402 | static int ac_wakeup_enabled; |
338 | static int usb_wakeup_enabled; | 403 | static int usb_wakeup_enabled; |
339 | 404 | ||
340 | static int pda_power_suspend(struct platform_device *pdev, pm_message_t state) | 405 | static int pda_power_suspend(struct platform_device *pdev, pm_message_t state) |
341 | { | 406 | { |
342 | if (device_may_wakeup(&pdev->dev)) { | 407 | if (device_may_wakeup(&pdev->dev)) { |
343 | if (ac_irq) | 408 | if (ac_irq) |
344 | ac_wakeup_enabled = !enable_irq_wake(ac_irq->start); | 409 | ac_wakeup_enabled = !enable_irq_wake(ac_irq->start); |
345 | if (usb_irq) | 410 | if (usb_irq) |
346 | usb_wakeup_enabled = !enable_irq_wake(usb_irq->start); | 411 | usb_wakeup_enabled = !enable_irq_wake(usb_irq->start); |
347 | } | 412 | } |
348 | 413 | ||
349 | return 0; | 414 | return 0; |
350 | } | 415 | } |
351 | 416 | ||
352 | static int pda_power_resume(struct platform_device *pdev) | 417 | static int pda_power_resume(struct platform_device *pdev) |
353 | { | 418 | { |
354 | if (device_may_wakeup(&pdev->dev)) { | 419 | if (device_may_wakeup(&pdev->dev)) { |
355 | if (usb_irq && usb_wakeup_enabled) | 420 | if (usb_irq && usb_wakeup_enabled) |
356 | disable_irq_wake(usb_irq->start); | 421 | disable_irq_wake(usb_irq->start); |
357 | if (ac_irq && ac_wakeup_enabled) | 422 | if (ac_irq && ac_wakeup_enabled) |
358 | disable_irq_wake(ac_irq->start); | 423 | disable_irq_wake(ac_irq->start); |
359 | } | 424 | } |
360 | 425 | ||
361 | return 0; | 426 | return 0; |
362 | } | 427 | } |
363 | #else | 428 | #else |
364 | #define pda_power_suspend NULL | 429 | #define pda_power_suspend NULL |
365 | #define pda_power_resume NULL | 430 | #define pda_power_resume NULL |
366 | #endif /* CONFIG_PM */ | 431 | #endif /* CONFIG_PM */ |
367 | 432 | ||
368 | MODULE_ALIAS("platform:pda-power"); | 433 | MODULE_ALIAS("platform:pda-power"); |
369 | 434 | ||
370 | static struct platform_driver pda_power_pdrv = { | 435 | static struct platform_driver pda_power_pdrv = { |
371 | .driver = { | 436 | .driver = { |
372 | .name = "pda-power", | 437 | .name = "pda-power", |
373 | }, | 438 | }, |
374 | .probe = pda_power_probe, | 439 | .probe = pda_power_probe, |
375 | .remove = pda_power_remove, | 440 | .remove = pda_power_remove, |
376 | .suspend = pda_power_suspend, | 441 | .suspend = pda_power_suspend, |
377 | .resume = pda_power_resume, | 442 | .resume = pda_power_resume, |
378 | }; | 443 | }; |
379 | 444 | ||
380 | static int __init pda_power_init(void) | 445 | static int __init pda_power_init(void) |
381 | { | 446 | { |
382 | return platform_driver_register(&pda_power_pdrv); | 447 | return platform_driver_register(&pda_power_pdrv); |
383 | } | 448 | } |
384 | 449 | ||
385 | static void __exit pda_power_exit(void) | 450 | static void __exit pda_power_exit(void) |
386 | { | 451 | { |
387 | platform_driver_unregister(&pda_power_pdrv); | 452 | platform_driver_unregister(&pda_power_pdrv); |
388 | } | 453 | } |
389 | 454 | ||
390 | module_init(pda_power_init); | 455 | module_init(pda_power_init); |
391 | module_exit(pda_power_exit); | 456 | module_exit(pda_power_exit); |
392 | MODULE_LICENSE("GPL"); | 457 | MODULE_LICENSE("GPL"); |
393 | MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>"); | 458 | MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>"); |
394 | 459 |
include/linux/pda_power.h
1 | /* | 1 | /* |
2 | * Common power driver for PDAs and phones with one or two external | 2 | * Common power driver for PDAs and phones with one or two external |
3 | * power supplies (AC/USB) connected to main and backup batteries, | 3 | * power supplies (AC/USB) connected to main and backup batteries, |
4 | * and optional builtin charger. | 4 | * and optional builtin charger. |
5 | * | 5 | * |
6 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | 6 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> |
7 | * | 7 | * |
8 | * This program is free software; you can redistribute it and/or modify | 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 | 9 | * it under the terms of the GNU General Public License version 2 as |
10 | * published by the Free Software Foundation. | 10 | * published by the Free Software Foundation. |
11 | */ | 11 | */ |
12 | 12 | ||
13 | #ifndef __PDA_POWER_H__ | 13 | #ifndef __PDA_POWER_H__ |
14 | #define __PDA_POWER_H__ | 14 | #define __PDA_POWER_H__ |
15 | 15 | ||
16 | #define PDA_POWER_CHARGE_AC (1 << 0) | 16 | #define PDA_POWER_CHARGE_AC (1 << 0) |
17 | #define PDA_POWER_CHARGE_USB (1 << 1) | 17 | #define PDA_POWER_CHARGE_USB (1 << 1) |
18 | 18 | ||
19 | struct device; | 19 | struct device; |
20 | 20 | ||
21 | struct pda_power_pdata { | 21 | struct pda_power_pdata { |
22 | int (*init)(struct device *dev); | 22 | int (*init)(struct device *dev); |
23 | int (*is_ac_online)(void); | 23 | int (*is_ac_online)(void); |
24 | int (*is_usb_online)(void); | 24 | int (*is_usb_online)(void); |
25 | void (*set_charge)(int flags); | 25 | void (*set_charge)(int flags); |
26 | void (*exit)(struct device *dev); | 26 | void (*exit)(struct device *dev); |
27 | 27 | ||
28 | char **supplied_to; | 28 | char **supplied_to; |
29 | size_t num_supplicants; | 29 | size_t num_supplicants; |
30 | 30 | ||
31 | unsigned int wait_for_status; /* msecs, default is 500 */ | 31 | unsigned int wait_for_status; /* msecs, default is 500 */ |
32 | unsigned int wait_for_charger; /* msecs, default is 500 */ | 32 | unsigned int wait_for_charger; /* msecs, default is 500 */ |
33 | unsigned int polling_interval; /* msecs, default is 2000 */ | 33 | unsigned int polling_interval; /* msecs, default is 2000 */ |
34 | |||
35 | unsigned long ac_max_uA; /* current to draw when on AC */ | ||
34 | }; | 36 | }; |
35 | 37 | ||
36 | #endif /* __PDA_POWER_H__ */ | 38 | #endif /* __PDA_POWER_H__ */ |
37 | 39 |