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> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/power_supply.h> |
| 17 | #include <linux/pda_power.h> |
| 18 | #include <linux/timer.h> |
| 19 | #include <linux/jiffies.h> |
| 20 | |
| 21 | static inline unsigned int get_irq_flags(struct resource *res) |
| 22 | { |
| 23 | unsigned int flags = IRQF_DISABLED | IRQF_SHARED; |
| 24 | |
| 25 | flags |= res->flags & IRQF_TRIGGER_MASK; |
| 26 | |
| 27 | return flags; |
| 28 | } |
| 29 | |
| 30 | static struct device *dev; |
| 31 | static struct pda_power_pdata *pdata; |
| 32 | static struct resource *ac_irq, *usb_irq; |
| 33 | static struct timer_list charger_timer; |
| 34 | static struct timer_list supply_timer; |
| 35 | |
| 36 | static int pda_power_get_property(struct power_supply *psy, |
| 37 | enum power_supply_property psp, |
| 38 | union power_supply_propval *val) |
| 39 | { |
| 40 | switch (psp) { |
| 41 | case POWER_SUPPLY_PROP_ONLINE: |
| 42 | if (psy->type == POWER_SUPPLY_TYPE_MAINS) |
| 43 | val->intval = pdata->is_ac_online ? |
| 44 | pdata->is_ac_online() : 0; |
| 45 | else |
| 46 | val->intval = pdata->is_usb_online ? |
| 47 | pdata->is_usb_online() : 0; |
| 48 | break; |
| 49 | default: |
| 50 | return -EINVAL; |
| 51 | } |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | static enum power_supply_property pda_power_props[] = { |
| 56 | POWER_SUPPLY_PROP_ONLINE, |
| 57 | }; |
| 58 | |
| 59 | static char *pda_power_supplied_to[] = { |
| 60 | "main-battery", |
| 61 | "backup-battery", |
| 62 | }; |
| 63 | |
| 64 | static struct power_supply pda_power_supplies[] = { |
| 65 | { |
| 66 | .name = "ac", |
| 67 | .type = POWER_SUPPLY_TYPE_MAINS, |
| 68 | .supplied_to = pda_power_supplied_to, |
| 69 | .num_supplicants = ARRAY_SIZE(pda_power_supplied_to), |
| 70 | .properties = pda_power_props, |
| 71 | .num_properties = ARRAY_SIZE(pda_power_props), |
| 72 | .get_property = pda_power_get_property, |
| 73 | }, |
| 74 | { |
| 75 | .name = "usb", |
| 76 | .type = POWER_SUPPLY_TYPE_USB, |
| 77 | .supplied_to = pda_power_supplied_to, |
| 78 | .num_supplicants = ARRAY_SIZE(pda_power_supplied_to), |
| 79 | .properties = pda_power_props, |
| 80 | .num_properties = ARRAY_SIZE(pda_power_props), |
| 81 | .get_property = pda_power_get_property, |
| 82 | }, |
| 83 | }; |
| 84 | |
| 85 | static void update_charger(void) |
| 86 | { |
| 87 | if (!pdata->set_charge) |
| 88 | return; |
| 89 | |
| 90 | if (pdata->is_ac_online && pdata->is_ac_online()) { |
| 91 | dev_dbg(dev, "charger on (AC)\n"); |
| 92 | pdata->set_charge(PDA_POWER_CHARGE_AC); |
| 93 | } else if (pdata->is_usb_online && pdata->is_usb_online()) { |
| 94 | dev_dbg(dev, "charger on (USB)\n"); |
| 95 | pdata->set_charge(PDA_POWER_CHARGE_USB); |
| 96 | } else { |
| 97 | dev_dbg(dev, "charger off\n"); |
| 98 | pdata->set_charge(0); |
| 99 | } |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 100 | } |
| 101 | |
Jeff Garzik | 5ebf6e6 | 2007-07-14 19:12:04 -0400 | [diff] [blame] | 102 | static void supply_timer_func(unsigned long power_supply_ptr) |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 103 | { |
Jeff Garzik | 5ebf6e6 | 2007-07-14 19:12:04 -0400 | [diff] [blame] | 104 | void *power_supply = (void *)power_supply_ptr; |
| 105 | |
| 106 | power_supply_changed(power_supply); |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 107 | } |
| 108 | |
Jeff Garzik | 5ebf6e6 | 2007-07-14 19:12:04 -0400 | [diff] [blame] | 109 | static void charger_timer_func(unsigned long power_supply_ptr) |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 110 | { |
| 111 | update_charger(); |
| 112 | |
| 113 | /* Okay, charger set. Now wait a bit before notifying supplicants, |
| 114 | * charge power should stabilize. */ |
Jeff Garzik | 5ebf6e6 | 2007-07-14 19:12:04 -0400 | [diff] [blame] | 115 | supply_timer.data = power_supply_ptr; |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 116 | mod_timer(&supply_timer, |
| 117 | jiffies + msecs_to_jiffies(pdata->wait_for_charger)); |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 118 | } |
| 119 | |
Jeff Garzik | 5ebf6e6 | 2007-07-14 19:12:04 -0400 | [diff] [blame] | 120 | static irqreturn_t power_changed_isr(int irq, void *power_supply) |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 121 | { |
| 122 | /* Wait a bit before reading ac/usb line status and setting charger, |
| 123 | * because ac/usb status readings may lag from irq. */ |
Jeff Garzik | 5ebf6e6 | 2007-07-14 19:12:04 -0400 | [diff] [blame] | 124 | charger_timer.data = (unsigned long)power_supply; |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 125 | mod_timer(&charger_timer, |
| 126 | jiffies + msecs_to_jiffies(pdata->wait_for_status)); |
| 127 | return IRQ_HANDLED; |
| 128 | } |
| 129 | |
| 130 | static int pda_power_probe(struct platform_device *pdev) |
| 131 | { |
| 132 | int ret = 0; |
| 133 | |
| 134 | dev = &pdev->dev; |
| 135 | |
| 136 | if (pdev->id != -1) { |
| 137 | dev_err(dev, "it's meaningless to register several " |
| 138 | "pda_powers; use id = -1\n"); |
| 139 | ret = -EINVAL; |
| 140 | goto wrongid; |
| 141 | } |
| 142 | |
| 143 | pdata = pdev->dev.platform_data; |
| 144 | |
| 145 | update_charger(); |
| 146 | |
| 147 | if (!pdata->wait_for_status) |
| 148 | pdata->wait_for_status = 500; |
| 149 | |
| 150 | if (!pdata->wait_for_charger) |
| 151 | pdata->wait_for_charger = 500; |
| 152 | |
| 153 | setup_timer(&charger_timer, charger_timer_func, 0); |
| 154 | setup_timer(&supply_timer, supply_timer_func, 0); |
| 155 | |
| 156 | ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac"); |
| 157 | usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb"); |
| 158 | if (!ac_irq && !usb_irq) { |
| 159 | dev_err(dev, "no ac/usb irq specified\n"); |
| 160 | ret = -ENODEV; |
| 161 | goto noirqs; |
| 162 | } |
| 163 | |
| 164 | if (pdata->supplied_to) { |
| 165 | pda_power_supplies[0].supplied_to = pdata->supplied_to; |
| 166 | pda_power_supplies[1].supplied_to = pdata->supplied_to; |
| 167 | pda_power_supplies[0].num_supplicants = pdata->num_supplicants; |
| 168 | pda_power_supplies[1].num_supplicants = pdata->num_supplicants; |
| 169 | } |
| 170 | |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 171 | if (pdata->is_ac_online) { |
| 172 | ret = power_supply_register(&pdev->dev, &pda_power_supplies[0]); |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 173 | if (ret) { |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 174 | dev_err(dev, "failed to register %s power supply\n", |
| 175 | pda_power_supplies[0].name); |
| 176 | goto ac_supply_failed; |
| 177 | } |
| 178 | |
| 179 | if (ac_irq) { |
| 180 | ret = request_irq(ac_irq->start, power_changed_isr, |
| 181 | get_irq_flags(ac_irq), ac_irq->name, |
| 182 | &pda_power_supplies[0]); |
| 183 | if (ret) { |
| 184 | dev_err(dev, "request ac irq failed\n"); |
| 185 | goto ac_irq_failed; |
| 186 | } |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 190 | if (pdata->is_usb_online) { |
| 191 | ret = power_supply_register(&pdev->dev, &pda_power_supplies[1]); |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 192 | if (ret) { |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 193 | dev_err(dev, "failed to register %s power supply\n", |
| 194 | pda_power_supplies[1].name); |
| 195 | goto usb_supply_failed; |
| 196 | } |
| 197 | |
| 198 | if (usb_irq) { |
| 199 | ret = request_irq(usb_irq->start, power_changed_isr, |
| 200 | get_irq_flags(usb_irq), |
| 201 | usb_irq->name, |
| 202 | &pda_power_supplies[1]); |
| 203 | if (ret) { |
| 204 | dev_err(dev, "request usb irq failed\n"); |
| 205 | goto usb_irq_failed; |
| 206 | } |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame^] | 210 | device_init_wakeup(&pdev->dev, 1); |
| 211 | |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 212 | return 0; |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 213 | |
| 214 | usb_irq_failed: |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 215 | if (pdata->is_usb_online) |
| 216 | power_supply_unregister(&pda_power_supplies[1]); |
| 217 | usb_supply_failed: |
| 218 | if (pdata->is_ac_online && ac_irq) |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 219 | free_irq(ac_irq->start, &pda_power_supplies[0]); |
| 220 | ac_irq_failed: |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 221 | if (pdata->is_ac_online) |
| 222 | power_supply_unregister(&pda_power_supplies[0]); |
| 223 | ac_supply_failed: |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 224 | noirqs: |
| 225 | wrongid: |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static int pda_power_remove(struct platform_device *pdev) |
| 230 | { |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 231 | if (pdata->is_usb_online && usb_irq) |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 232 | free_irq(usb_irq->start, &pda_power_supplies[1]); |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 233 | if (pdata->is_ac_online && ac_irq) |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 234 | free_irq(ac_irq->start, &pda_power_supplies[0]); |
| 235 | del_timer_sync(&charger_timer); |
| 236 | del_timer_sync(&supply_timer); |
Dmitry Baryshkov | 9ef4510 | 2008-01-07 04:12:39 +0300 | [diff] [blame] | 237 | if (pdata->is_usb_online) |
| 238 | power_supply_unregister(&pda_power_supplies[1]); |
| 239 | if (pdata->is_ac_online) |
| 240 | power_supply_unregister(&pda_power_supplies[0]); |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 241 | return 0; |
| 242 | } |
| 243 | |
Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame^] | 244 | #ifdef CONFIG_PM |
| 245 | static int pda_power_suspend(struct platform_device *pdev, pm_message_t state) |
| 246 | { |
| 247 | if (device_may_wakeup(&pdev->dev)) { |
| 248 | if (ac_irq) |
| 249 | enable_irq_wake(ac_irq->start); |
| 250 | if (usb_irq) |
| 251 | enable_irq_wake(usb_irq->start); |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int pda_power_resume(struct platform_device *pdev) |
| 258 | { |
| 259 | if (device_may_wakeup(&pdev->dev)) { |
| 260 | if (usb_irq) |
| 261 | disable_irq_wake(usb_irq->start); |
| 262 | if (ac_irq) |
| 263 | disable_irq_wake(ac_irq->start); |
| 264 | } |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | #else |
| 269 | #define pda_power_suspend NULL |
| 270 | #define pda_power_resume NULL |
| 271 | #endif /* CONFIG_PM */ |
| 272 | |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 273 | static struct platform_driver pda_power_pdrv = { |
| 274 | .driver = { |
| 275 | .name = "pda-power", |
| 276 | }, |
| 277 | .probe = pda_power_probe, |
| 278 | .remove = pda_power_remove, |
Dmitry Baryshkov | 8f8e9b3 | 2008-01-13 02:35:43 +0300 | [diff] [blame^] | 279 | .suspend = pda_power_suspend, |
| 280 | .resume = pda_power_resume, |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 281 | }; |
| 282 | |
| 283 | static int __init pda_power_init(void) |
| 284 | { |
| 285 | return platform_driver_register(&pda_power_pdrv); |
| 286 | } |
| 287 | |
| 288 | static void __exit pda_power_exit(void) |
| 289 | { |
| 290 | platform_driver_unregister(&pda_power_pdrv); |
Anton Vorontsov | b299804 | 2007-05-04 00:32:17 +0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | module_init(pda_power_init); |
| 294 | module_exit(pda_power_exit); |
| 295 | MODULE_LICENSE("GPL"); |
| 296 | MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>"); |