pda_power.c 8.6 KB

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