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