| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 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> | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 15 | #include <linux/err.h> | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 16 | #include <linux/interrupt.h> | 
|  | 17 | #include <linux/power_supply.h> | 
|  | 18 | #include <linux/pda_power.h> | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 19 | #include <linux/regulator/consumer.h> | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 20 | #include <linux/timer.h> | 
|  | 21 | #include <linux/jiffies.h> | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 22 | #include <linux/usb/otg.h> | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 23 |  | 
|  | 24 | static inline unsigned int get_irq_flags(struct resource *res) | 
|  | 25 | { | 
| Philipp Zabel | 74194cc | 2009-01-18 14:46:21 +0100 | [diff] [blame] | 26 | unsigned int flags = IRQF_SAMPLE_RANDOM | IRQF_SHARED; | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 27 |  | 
|  | 28 | flags |= res->flags & IRQF_TRIGGER_MASK; | 
|  | 29 |  | 
|  | 30 | return flags; | 
|  | 31 | } | 
|  | 32 |  | 
|  | 33 | static struct device *dev; | 
|  | 34 | static struct pda_power_pdata *pdata; | 
|  | 35 | static struct resource *ac_irq, *usb_irq; | 
|  | 36 | static struct timer_list charger_timer; | 
|  | 37 | static struct timer_list supply_timer; | 
| Anton Vorontsov | c3caeba | 2008-01-13 02:44:20 +0300 | [diff] [blame] | 38 | static struct timer_list polling_timer; | 
|  | 39 | static int polling; | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 40 |  | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 41 | #ifdef CONFIG_USB_OTG_UTILS | 
|  | 42 | static struct otg_transceiver *transceiver; | 
|  | 43 | #endif | 
|  | 44 | static struct regulator *ac_draw; | 
|  | 45 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 46 | enum { | 
|  | 47 | PDA_PSY_OFFLINE = 0, | 
|  | 48 | PDA_PSY_ONLINE = 1, | 
|  | 49 | PDA_PSY_TO_CHANGE, | 
|  | 50 | }; | 
|  | 51 | static int new_ac_status = -1; | 
|  | 52 | static int new_usb_status = -1; | 
|  | 53 | static int ac_status = -1; | 
|  | 54 | static int usb_status = -1; | 
|  | 55 |  | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 56 | static int pda_power_get_property(struct power_supply *psy, | 
|  | 57 | enum power_supply_property psp, | 
|  | 58 | union power_supply_propval *val) | 
|  | 59 | { | 
|  | 60 | switch (psp) { | 
|  | 61 | case POWER_SUPPLY_PROP_ONLINE: | 
|  | 62 | if (psy->type == POWER_SUPPLY_TYPE_MAINS) | 
|  | 63 | val->intval = pdata->is_ac_online ? | 
|  | 64 | pdata->is_ac_online() : 0; | 
|  | 65 | else | 
|  | 66 | val->intval = pdata->is_usb_online ? | 
|  | 67 | pdata->is_usb_online() : 0; | 
|  | 68 | break; | 
|  | 69 | default: | 
|  | 70 | return -EINVAL; | 
|  | 71 | } | 
|  | 72 | return 0; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | static enum power_supply_property pda_power_props[] = { | 
|  | 76 | POWER_SUPPLY_PROP_ONLINE, | 
|  | 77 | }; | 
|  | 78 |  | 
|  | 79 | static char *pda_power_supplied_to[] = { | 
|  | 80 | "main-battery", | 
|  | 81 | "backup-battery", | 
|  | 82 | }; | 
|  | 83 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 84 | static struct power_supply pda_psy_ac = { | 
|  | 85 | .name = "ac", | 
|  | 86 | .type = POWER_SUPPLY_TYPE_MAINS, | 
|  | 87 | .supplied_to = pda_power_supplied_to, | 
|  | 88 | .num_supplicants = ARRAY_SIZE(pda_power_supplied_to), | 
|  | 89 | .properties = pda_power_props, | 
|  | 90 | .num_properties = ARRAY_SIZE(pda_power_props), | 
|  | 91 | .get_property = pda_power_get_property, | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 92 | }; | 
|  | 93 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 94 | static struct power_supply pda_psy_usb = { | 
|  | 95 | .name = "usb", | 
|  | 96 | .type = POWER_SUPPLY_TYPE_USB, | 
|  | 97 | .supplied_to = pda_power_supplied_to, | 
|  | 98 | .num_supplicants = ARRAY_SIZE(pda_power_supplied_to), | 
|  | 99 | .properties = pda_power_props, | 
|  | 100 | .num_properties = ARRAY_SIZE(pda_power_props), | 
|  | 101 | .get_property = pda_power_get_property, | 
|  | 102 | }; | 
|  | 103 |  | 
|  | 104 | static void update_status(void) | 
|  | 105 | { | 
|  | 106 | if (pdata->is_ac_online) | 
|  | 107 | new_ac_status = !!pdata->is_ac_online(); | 
|  | 108 |  | 
|  | 109 | if (pdata->is_usb_online) | 
|  | 110 | new_usb_status = !!pdata->is_usb_online(); | 
|  | 111 | } | 
|  | 112 |  | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 113 | static void update_charger(void) | 
|  | 114 | { | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 115 | static int regulator_enabled; | 
|  | 116 | int max_uA = pdata->ac_max_uA; | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 117 |  | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 118 | if (pdata->set_charge) { | 
|  | 119 | if (new_ac_status > 0) { | 
|  | 120 | dev_dbg(dev, "charger on (AC)\n"); | 
|  | 121 | pdata->set_charge(PDA_POWER_CHARGE_AC); | 
|  | 122 | } else if (new_usb_status > 0) { | 
|  | 123 | dev_dbg(dev, "charger on (USB)\n"); | 
|  | 124 | pdata->set_charge(PDA_POWER_CHARGE_USB); | 
|  | 125 | } else { | 
|  | 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 | } | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 144 | } | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 145 | } | 
|  | 146 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 147 | static void supply_timer_func(unsigned long unused) | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 148 | { | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 149 | if (ac_status == PDA_PSY_TO_CHANGE) { | 
|  | 150 | ac_status = new_ac_status; | 
|  | 151 | power_supply_changed(&pda_psy_ac); | 
|  | 152 | } | 
| Jeff Garzik | 5ebf6e6 | 2007-07-14 19:12:04 -0400 | [diff] [blame] | 153 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 154 | if (usb_status == PDA_PSY_TO_CHANGE) { | 
|  | 155 | usb_status = new_usb_status; | 
|  | 156 | power_supply_changed(&pda_psy_usb); | 
|  | 157 | } | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 158 | } | 
|  | 159 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 160 | static void psy_changed(void) | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 161 | { | 
|  | 162 | update_charger(); | 
|  | 163 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 164 | /* | 
|  | 165 | * Okay, charger set. Now wait a bit before notifying supplicants, | 
|  | 166 | * charge power should stabilize. | 
|  | 167 | */ | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 168 | mod_timer(&supply_timer, | 
|  | 169 | jiffies + msecs_to_jiffies(pdata->wait_for_charger)); | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 170 | } | 
|  | 171 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 172 | static void charger_timer_func(unsigned long unused) | 
|  | 173 | { | 
|  | 174 | update_status(); | 
|  | 175 | psy_changed(); | 
|  | 176 | } | 
|  | 177 |  | 
| Jeff Garzik | 5ebf6e6 | 2007-07-14 19:12:04 -0400 | [diff] [blame] | 178 | static irqreturn_t power_changed_isr(int irq, void *power_supply) | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 179 | { | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 180 | if (power_supply == &pda_psy_ac) | 
|  | 181 | ac_status = PDA_PSY_TO_CHANGE; | 
|  | 182 | else if (power_supply == &pda_psy_usb) | 
|  | 183 | usb_status = PDA_PSY_TO_CHANGE; | 
|  | 184 | else | 
|  | 185 | return IRQ_NONE; | 
|  | 186 |  | 
|  | 187 | /* | 
|  | 188 | * Wait a bit before reading ac/usb line status and setting charger, | 
|  | 189 | * because ac/usb status readings may lag from irq. | 
|  | 190 | */ | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 191 | mod_timer(&charger_timer, | 
|  | 192 | jiffies + msecs_to_jiffies(pdata->wait_for_status)); | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 193 |  | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 194 | return IRQ_HANDLED; | 
|  | 195 | } | 
|  | 196 |  | 
| Anton Vorontsov | c3caeba | 2008-01-13 02:44:20 +0300 | [diff] [blame] | 197 | static void polling_timer_func(unsigned long unused) | 
|  | 198 | { | 
|  | 199 | int changed = 0; | 
|  | 200 |  | 
|  | 201 | dev_dbg(dev, "polling...\n"); | 
|  | 202 |  | 
|  | 203 | update_status(); | 
|  | 204 |  | 
|  | 205 | if (!ac_irq && new_ac_status != ac_status) { | 
|  | 206 | ac_status = PDA_PSY_TO_CHANGE; | 
|  | 207 | changed = 1; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | if (!usb_irq && new_usb_status != usb_status) { | 
|  | 211 | usb_status = PDA_PSY_TO_CHANGE; | 
|  | 212 | changed = 1; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | if (changed) | 
|  | 216 | psy_changed(); | 
|  | 217 |  | 
|  | 218 | mod_timer(&polling_timer, | 
|  | 219 | jiffies + msecs_to_jiffies(pdata->polling_interval)); | 
|  | 220 | } | 
|  | 221 |  | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 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 |  | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 229 | static int pda_power_probe(struct platform_device *pdev) | 
|  | 230 | { | 
|  | 231 | int ret = 0; | 
|  | 232 |  | 
|  | 233 | dev = &pdev->dev; | 
|  | 234 |  | 
|  | 235 | if (pdev->id != -1) { | 
|  | 236 | dev_err(dev, "it's meaningless to register several " | 
|  | 237 | "pda_powers; use id = -1\n"); | 
|  | 238 | ret = -EINVAL; | 
|  | 239 | goto wrongid; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | pdata = pdev->dev.platform_data; | 
|  | 243 |  | 
| Philipp Zabel | f6b6b18 | 2008-04-12 13:47:45 +0200 | [diff] [blame] | 244 | if (pdata->init) { | 
|  | 245 | ret = pdata->init(dev); | 
|  | 246 | if (ret < 0) | 
|  | 247 | goto init_failed; | 
|  | 248 | } | 
|  | 249 |  | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 250 | update_status(); | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 251 | update_charger(); | 
|  | 252 |  | 
|  | 253 | if (!pdata->wait_for_status) | 
|  | 254 | pdata->wait_for_status = 500; | 
|  | 255 |  | 
|  | 256 | if (!pdata->wait_for_charger) | 
|  | 257 | pdata->wait_for_charger = 500; | 
|  | 258 |  | 
| Anton Vorontsov | c3caeba | 2008-01-13 02:44:20 +0300 | [diff] [blame] | 259 | if (!pdata->polling_interval) | 
|  | 260 | pdata->polling_interval = 2000; | 
|  | 261 |  | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 262 | if (!pdata->ac_max_uA) | 
|  | 263 | pdata->ac_max_uA = 500000; | 
|  | 264 |  | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 265 | setup_timer(&charger_timer, charger_timer_func, 0); | 
|  | 266 | setup_timer(&supply_timer, supply_timer_func, 0); | 
|  | 267 |  | 
|  | 268 | ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac"); | 
|  | 269 | usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb"); | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 270 |  | 
|  | 271 | if (pdata->supplied_to) { | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 272 | pda_psy_ac.supplied_to = pdata->supplied_to; | 
|  | 273 | pda_psy_ac.num_supplicants = pdata->num_supplicants; | 
|  | 274 | pda_psy_usb.supplied_to = pdata->supplied_to; | 
|  | 275 | pda_psy_usb.num_supplicants = pdata->num_supplicants; | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 276 | } | 
|  | 277 |  | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 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 |  | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 285 | if (pdata->is_ac_online) { | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 286 | ret = power_supply_register(&pdev->dev, &pda_psy_ac); | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 287 | if (ret) { | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 288 | dev_err(dev, "failed to register %s power supply\n", | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 289 | pda_psy_ac.name); | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 290 | goto ac_supply_failed; | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | if (ac_irq) { | 
|  | 294 | ret = request_irq(ac_irq->start, power_changed_isr, | 
|  | 295 | get_irq_flags(ac_irq), ac_irq->name, | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 296 | &pda_psy_ac); | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 297 | if (ret) { | 
|  | 298 | dev_err(dev, "request ac irq failed\n"); | 
|  | 299 | goto ac_irq_failed; | 
|  | 300 | } | 
| Anton Vorontsov | c3caeba | 2008-01-13 02:44:20 +0300 | [diff] [blame] | 301 | } else { | 
|  | 302 | polling = 1; | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 303 | } | 
|  | 304 | } | 
|  | 305 |  | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 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 |  | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 313 | if (pdata->is_usb_online) { | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 314 | ret = power_supply_register(&pdev->dev, &pda_psy_usb); | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 315 | if (ret) { | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 316 | dev_err(dev, "failed to register %s power supply\n", | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 317 | pda_psy_usb.name); | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 318 | goto usb_supply_failed; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | if (usb_irq) { | 
|  | 322 | ret = request_irq(usb_irq->start, power_changed_isr, | 
|  | 323 | get_irq_flags(usb_irq), | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 324 | usb_irq->name, &pda_psy_usb); | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 325 | if (ret) { | 
|  | 326 | dev_err(dev, "request usb irq failed\n"); | 
|  | 327 | goto usb_irq_failed; | 
|  | 328 | } | 
| Anton Vorontsov | c3caeba | 2008-01-13 02:44:20 +0300 | [diff] [blame] | 329 | } else { | 
|  | 330 | polling = 1; | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 331 | } | 
|  | 332 | } | 
|  | 333 |  | 
| Anton Vorontsov | c3caeba | 2008-01-13 02:44:20 +0300 | [diff] [blame] | 334 | if (polling) { | 
|  | 335 | dev_dbg(dev, "will poll for status\n"); | 
|  | 336 | setup_timer(&polling_timer, polling_timer_func, 0); | 
|  | 337 | mod_timer(&polling_timer, | 
|  | 338 | jiffies + msecs_to_jiffies(pdata->polling_interval)); | 
|  | 339 | } | 
|  | 340 |  | 
|  | 341 | if (ac_irq || usb_irq) | 
|  | 342 | device_init_wakeup(&pdev->dev, 1); | 
| Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame] | 343 |  | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 344 | return 0; | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 345 |  | 
|  | 346 | usb_irq_failed: | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 347 | if (pdata->is_usb_online) | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 348 | power_supply_unregister(&pda_psy_usb); | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 349 | usb_supply_failed: | 
|  | 350 | if (pdata->is_ac_online && ac_irq) | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 351 | free_irq(ac_irq->start, &pda_psy_ac); | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 352 | #ifdef CONFIG_USB_OTG_UTILS | 
|  | 353 | if (transceiver) | 
|  | 354 | otg_put_transceiver(transceiver); | 
|  | 355 | #endif | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 356 | ac_irq_failed: | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 357 | if (pdata->is_ac_online) | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 358 | power_supply_unregister(&pda_psy_ac); | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 359 | ac_supply_failed: | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 360 | if (ac_draw) { | 
|  | 361 | regulator_put(ac_draw); | 
|  | 362 | ac_draw = NULL; | 
|  | 363 | } | 
| Philipp Zabel | f6b6b18 | 2008-04-12 13:47:45 +0200 | [diff] [blame] | 364 | if (pdata->exit) | 
|  | 365 | pdata->exit(dev); | 
|  | 366 | init_failed: | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 367 | wrongid: | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 368 | return ret; | 
|  | 369 | } | 
|  | 370 |  | 
|  | 371 | static int pda_power_remove(struct platform_device *pdev) | 
|  | 372 | { | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 373 | if (pdata->is_usb_online && usb_irq) | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 374 | free_irq(usb_irq->start, &pda_psy_usb); | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 375 | if (pdata->is_ac_online && ac_irq) | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 376 | free_irq(ac_irq->start, &pda_psy_ac); | 
|  | 377 |  | 
| Anton Vorontsov | c3caeba | 2008-01-13 02:44:20 +0300 | [diff] [blame] | 378 | if (polling) | 
|  | 379 | del_timer_sync(&polling_timer); | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 380 | del_timer_sync(&charger_timer); | 
|  | 381 | del_timer_sync(&supply_timer); | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 382 |  | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 383 | if (pdata->is_usb_online) | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 384 | power_supply_unregister(&pda_psy_usb); | 
| Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 385 | if (pdata->is_ac_online) | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 386 | power_supply_unregister(&pda_psy_ac); | 
| Philipp Zabel | 5bf2b99 | 2009-01-18 17:40:27 +0100 | [diff] [blame] | 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 | } | 
| Philipp Zabel | f6b6b18 | 2008-04-12 13:47:45 +0200 | [diff] [blame] | 395 | if (pdata->exit) | 
|  | 396 | pdata->exit(dev); | 
| Anton Vorontsov | bfde266 | 2008-01-13 02:39:17 +0300 | [diff] [blame] | 397 |  | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 398 | return 0; | 
|  | 399 | } | 
|  | 400 |  | 
| Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame] | 401 | #ifdef CONFIG_PM | 
| Robert Jarzmik | e82374f | 2008-08-11 22:22:27 +0200 | [diff] [blame] | 402 | static int ac_wakeup_enabled; | 
|  | 403 | static int usb_wakeup_enabled; | 
|  | 404 |  | 
| Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame] | 405 | static int pda_power_suspend(struct platform_device *pdev, pm_message_t state) | 
|  | 406 | { | 
|  | 407 | if (device_may_wakeup(&pdev->dev)) { | 
|  | 408 | if (ac_irq) | 
| Robert Jarzmik | e82374f | 2008-08-11 22:22:27 +0200 | [diff] [blame] | 409 | ac_wakeup_enabled = !enable_irq_wake(ac_irq->start); | 
| Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame] | 410 | if (usb_irq) | 
| Robert Jarzmik | e82374f | 2008-08-11 22:22:27 +0200 | [diff] [blame] | 411 | usb_wakeup_enabled = !enable_irq_wake(usb_irq->start); | 
| Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame] | 412 | } | 
|  | 413 |  | 
|  | 414 | return 0; | 
|  | 415 | } | 
|  | 416 |  | 
|  | 417 | static int pda_power_resume(struct platform_device *pdev) | 
|  | 418 | { | 
|  | 419 | if (device_may_wakeup(&pdev->dev)) { | 
| Robert Jarzmik | e82374f | 2008-08-11 22:22:27 +0200 | [diff] [blame] | 420 | if (usb_irq && usb_wakeup_enabled) | 
| Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame] | 421 | disable_irq_wake(usb_irq->start); | 
| Robert Jarzmik | e82374f | 2008-08-11 22:22:27 +0200 | [diff] [blame] | 422 | if (ac_irq && ac_wakeup_enabled) | 
| Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame] | 423 | disable_irq_wake(ac_irq->start); | 
|  | 424 | } | 
|  | 425 |  | 
|  | 426 | return 0; | 
|  | 427 | } | 
|  | 428 | #else | 
|  | 429 | #define pda_power_suspend NULL | 
|  | 430 | #define pda_power_resume NULL | 
|  | 431 | #endif /* CONFIG_PM */ | 
|  | 432 |  | 
| Kay Sievers | 2f5a5cf | 2008-07-25 01:45:46 -0700 | [diff] [blame] | 433 | MODULE_ALIAS("platform:pda-power"); | 
|  | 434 |  | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 435 | static struct platform_driver pda_power_pdrv = { | 
|  | 436 | .driver = { | 
|  | 437 | .name = "pda-power", | 
|  | 438 | }, | 
|  | 439 | .probe = pda_power_probe, | 
|  | 440 | .remove = pda_power_remove, | 
| Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame] | 441 | .suspend = pda_power_suspend, | 
|  | 442 | .resume = pda_power_resume, | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 443 | }; | 
|  | 444 |  | 
|  | 445 | static int __init pda_power_init(void) | 
|  | 446 | { | 
|  | 447 | return platform_driver_register(&pda_power_pdrv); | 
|  | 448 | } | 
|  | 449 |  | 
|  | 450 | static void __exit pda_power_exit(void) | 
|  | 451 | { | 
|  | 452 | platform_driver_unregister(&pda_power_pdrv); | 
| Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 453 | } | 
|  | 454 |  | 
|  | 455 | module_init(pda_power_init); | 
|  | 456 | module_exit(pda_power_exit); | 
|  | 457 | MODULE_LICENSE("GPL"); | 
|  | 458 | MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>"); |