pda_power.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. update_status();
  178. update_charger();
  179. if (!pdata->wait_for_status)
  180. pdata->wait_for_status = 500;
  181. if (!pdata->wait_for_charger)
  182. pdata->wait_for_charger = 500;
  183. if (!pdata->polling_interval)
  184. pdata->polling_interval = 2000;
  185. setup_timer(&charger_timer, charger_timer_func, 0);
  186. setup_timer(&supply_timer, supply_timer_func, 0);
  187. ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
  188. usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
  189. if (pdata->supplied_to) {
  190. pda_psy_ac.supplied_to = pdata->supplied_to;
  191. pda_psy_ac.num_supplicants = pdata->num_supplicants;
  192. pda_psy_usb.supplied_to = pdata->supplied_to;
  193. pda_psy_usb.num_supplicants = pdata->num_supplicants;
  194. }
  195. if (pdata->is_ac_online) {
  196. ret = power_supply_register(&pdev->dev, &pda_psy_ac);
  197. if (ret) {
  198. dev_err(dev, "failed to register %s power supply\n",
  199. pda_psy_ac.name);
  200. goto ac_supply_failed;
  201. }
  202. if (ac_irq) {
  203. ret = request_irq(ac_irq->start, power_changed_isr,
  204. get_irq_flags(ac_irq), ac_irq->name,
  205. &pda_psy_ac);
  206. if (ret) {
  207. dev_err(dev, "request ac irq failed\n");
  208. goto ac_irq_failed;
  209. }
  210. } else {
  211. polling = 1;
  212. }
  213. }
  214. if (pdata->is_usb_online) {
  215. ret = power_supply_register(&pdev->dev, &pda_psy_usb);
  216. if (ret) {
  217. dev_err(dev, "failed to register %s power supply\n",
  218. pda_psy_usb.name);
  219. goto usb_supply_failed;
  220. }
  221. if (usb_irq) {
  222. ret = request_irq(usb_irq->start, power_changed_isr,
  223. get_irq_flags(usb_irq),
  224. usb_irq->name, &pda_psy_usb);
  225. if (ret) {
  226. dev_err(dev, "request usb irq failed\n");
  227. goto usb_irq_failed;
  228. }
  229. } else {
  230. polling = 1;
  231. }
  232. }
  233. if (polling) {
  234. dev_dbg(dev, "will poll for status\n");
  235. setup_timer(&polling_timer, polling_timer_func, 0);
  236. mod_timer(&polling_timer,
  237. jiffies + msecs_to_jiffies(pdata->polling_interval));
  238. }
  239. if (ac_irq || usb_irq)
  240. device_init_wakeup(&pdev->dev, 1);
  241. return 0;
  242. usb_irq_failed:
  243. if (pdata->is_usb_online)
  244. power_supply_unregister(&pda_psy_usb);
  245. usb_supply_failed:
  246. if (pdata->is_ac_online && ac_irq)
  247. free_irq(ac_irq->start, &pda_psy_ac);
  248. ac_irq_failed:
  249. if (pdata->is_ac_online)
  250. power_supply_unregister(&pda_psy_ac);
  251. ac_supply_failed:
  252. wrongid:
  253. return ret;
  254. }
  255. static int pda_power_remove(struct platform_device *pdev)
  256. {
  257. if (pdata->is_usb_online && usb_irq)
  258. free_irq(usb_irq->start, &pda_psy_usb);
  259. if (pdata->is_ac_online && ac_irq)
  260. free_irq(ac_irq->start, &pda_psy_ac);
  261. if (polling)
  262. del_timer_sync(&polling_timer);
  263. del_timer_sync(&charger_timer);
  264. del_timer_sync(&supply_timer);
  265. if (pdata->is_usb_online)
  266. power_supply_unregister(&pda_psy_usb);
  267. if (pdata->is_ac_online)
  268. power_supply_unregister(&pda_psy_ac);
  269. return 0;
  270. }
  271. #ifdef CONFIG_PM
  272. static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
  273. {
  274. if (device_may_wakeup(&pdev->dev)) {
  275. if (ac_irq)
  276. enable_irq_wake(ac_irq->start);
  277. if (usb_irq)
  278. enable_irq_wake(usb_irq->start);
  279. }
  280. return 0;
  281. }
  282. static int pda_power_resume(struct platform_device *pdev)
  283. {
  284. if (device_may_wakeup(&pdev->dev)) {
  285. if (usb_irq)
  286. disable_irq_wake(usb_irq->start);
  287. if (ac_irq)
  288. disable_irq_wake(ac_irq->start);
  289. }
  290. return 0;
  291. }
  292. #else
  293. #define pda_power_suspend NULL
  294. #define pda_power_resume NULL
  295. #endif /* CONFIG_PM */
  296. static struct platform_driver pda_power_pdrv = {
  297. .driver = {
  298. .name = "pda-power",
  299. },
  300. .probe = pda_power_probe,
  301. .remove = pda_power_remove,
  302. .suspend = pda_power_suspend,
  303. .resume = pda_power_resume,
  304. };
  305. static int __init pda_power_init(void)
  306. {
  307. return platform_driver_register(&pda_power_pdrv);
  308. }
  309. static void __exit pda_power_exit(void)
  310. {
  311. platform_driver_unregister(&pda_power_pdrv);
  312. }
  313. module_init(pda_power_init);
  314. module_exit(pda_power_exit);
  315. MODULE_LICENSE("GPL");
  316. MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");