pda_power.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 int pda_power_get_property(struct power_supply *psy,
  31. enum power_supply_property psp,
  32. union power_supply_propval *val)
  33. {
  34. switch (psp) {
  35. case POWER_SUPPLY_PROP_ONLINE:
  36. if (psy->type == POWER_SUPPLY_TYPE_MAINS)
  37. val->intval = pdata->is_ac_online ?
  38. pdata->is_ac_online() : 0;
  39. else
  40. val->intval = pdata->is_usb_online ?
  41. pdata->is_usb_online() : 0;
  42. break;
  43. default:
  44. return -EINVAL;
  45. }
  46. return 0;
  47. }
  48. static enum power_supply_property pda_power_props[] = {
  49. POWER_SUPPLY_PROP_ONLINE,
  50. };
  51. static char *pda_power_supplied_to[] = {
  52. "main-battery",
  53. "backup-battery",
  54. };
  55. static struct power_supply pda_power_supplies[] = {
  56. {
  57. .name = "ac",
  58. .type = POWER_SUPPLY_TYPE_MAINS,
  59. .supplied_to = pda_power_supplied_to,
  60. .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
  61. .properties = pda_power_props,
  62. .num_properties = ARRAY_SIZE(pda_power_props),
  63. .get_property = pda_power_get_property,
  64. },
  65. {
  66. .name = "usb",
  67. .type = POWER_SUPPLY_TYPE_USB,
  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. static void update_charger(void)
  76. {
  77. if (!pdata->set_charge)
  78. return;
  79. if (pdata->is_ac_online && pdata->is_ac_online()) {
  80. dev_dbg(dev, "charger on (AC)\n");
  81. pdata->set_charge(PDA_POWER_CHARGE_AC);
  82. } else if (pdata->is_usb_online && pdata->is_usb_online()) {
  83. dev_dbg(dev, "charger on (USB)\n");
  84. pdata->set_charge(PDA_POWER_CHARGE_USB);
  85. } else {
  86. dev_dbg(dev, "charger off\n");
  87. pdata->set_charge(0);
  88. }
  89. }
  90. static void supply_timer_func(unsigned long power_supply_ptr)
  91. {
  92. void *power_supply = (void *)power_supply_ptr;
  93. power_supply_changed(power_supply);
  94. }
  95. static void charger_timer_func(unsigned long power_supply_ptr)
  96. {
  97. update_charger();
  98. /* Okay, charger set. Now wait a bit before notifying supplicants,
  99. * charge power should stabilize. */
  100. supply_timer.data = power_supply_ptr;
  101. mod_timer(&supply_timer,
  102. jiffies + msecs_to_jiffies(pdata->wait_for_charger));
  103. }
  104. static irqreturn_t power_changed_isr(int irq, void *power_supply)
  105. {
  106. /* Wait a bit before reading ac/usb line status and setting charger,
  107. * because ac/usb status readings may lag from irq. */
  108. charger_timer.data = (unsigned long)power_supply;
  109. mod_timer(&charger_timer,
  110. jiffies + msecs_to_jiffies(pdata->wait_for_status));
  111. return IRQ_HANDLED;
  112. }
  113. static int pda_power_probe(struct platform_device *pdev)
  114. {
  115. int ret = 0;
  116. dev = &pdev->dev;
  117. if (pdev->id != -1) {
  118. dev_err(dev, "it's meaningless to register several "
  119. "pda_powers; use id = -1\n");
  120. ret = -EINVAL;
  121. goto wrongid;
  122. }
  123. pdata = pdev->dev.platform_data;
  124. update_charger();
  125. if (!pdata->wait_for_status)
  126. pdata->wait_for_status = 500;
  127. if (!pdata->wait_for_charger)
  128. pdata->wait_for_charger = 500;
  129. setup_timer(&charger_timer, charger_timer_func, 0);
  130. setup_timer(&supply_timer, supply_timer_func, 0);
  131. ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
  132. usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
  133. if (!ac_irq && !usb_irq) {
  134. dev_err(dev, "no ac/usb irq specified\n");
  135. ret = -ENODEV;
  136. goto noirqs;
  137. }
  138. if (pdata->supplied_to) {
  139. pda_power_supplies[0].supplied_to = pdata->supplied_to;
  140. pda_power_supplies[1].supplied_to = pdata->supplied_to;
  141. pda_power_supplies[0].num_supplicants = pdata->num_supplicants;
  142. pda_power_supplies[1].num_supplicants = pdata->num_supplicants;
  143. }
  144. if (pdata->is_ac_online) {
  145. ret = power_supply_register(&pdev->dev, &pda_power_supplies[0]);
  146. if (ret) {
  147. dev_err(dev, "failed to register %s power supply\n",
  148. pda_power_supplies[0].name);
  149. goto ac_supply_failed;
  150. }
  151. if (ac_irq) {
  152. ret = request_irq(ac_irq->start, power_changed_isr,
  153. get_irq_flags(ac_irq), ac_irq->name,
  154. &pda_power_supplies[0]);
  155. if (ret) {
  156. dev_err(dev, "request ac irq failed\n");
  157. goto ac_irq_failed;
  158. }
  159. }
  160. }
  161. if (pdata->is_usb_online) {
  162. ret = power_supply_register(&pdev->dev, &pda_power_supplies[1]);
  163. if (ret) {
  164. dev_err(dev, "failed to register %s power supply\n",
  165. pda_power_supplies[1].name);
  166. goto usb_supply_failed;
  167. }
  168. if (usb_irq) {
  169. ret = request_irq(usb_irq->start, power_changed_isr,
  170. get_irq_flags(usb_irq),
  171. usb_irq->name,
  172. &pda_power_supplies[1]);
  173. if (ret) {
  174. dev_err(dev, "request usb irq failed\n");
  175. goto usb_irq_failed;
  176. }
  177. }
  178. }
  179. return 0;
  180. usb_irq_failed:
  181. if (pdata->is_usb_online)
  182. power_supply_unregister(&pda_power_supplies[1]);
  183. usb_supply_failed:
  184. if (pdata->is_ac_online && ac_irq)
  185. free_irq(ac_irq->start, &pda_power_supplies[0]);
  186. ac_irq_failed:
  187. if (pdata->is_ac_online)
  188. power_supply_unregister(&pda_power_supplies[0]);
  189. ac_supply_failed:
  190. noirqs:
  191. wrongid:
  192. return ret;
  193. }
  194. static int pda_power_remove(struct platform_device *pdev)
  195. {
  196. if (pdata->is_usb_online && usb_irq)
  197. free_irq(usb_irq->start, &pda_power_supplies[1]);
  198. if (pdata->is_ac_online && ac_irq)
  199. free_irq(ac_irq->start, &pda_power_supplies[0]);
  200. del_timer_sync(&charger_timer);
  201. del_timer_sync(&supply_timer);
  202. if (pdata->is_usb_online)
  203. power_supply_unregister(&pda_power_supplies[1]);
  204. if (pdata->is_ac_online)
  205. power_supply_unregister(&pda_power_supplies[0]);
  206. return 0;
  207. }
  208. static struct platform_driver pda_power_pdrv = {
  209. .driver = {
  210. .name = "pda-power",
  211. },
  212. .probe = pda_power_probe,
  213. .remove = pda_power_remove,
  214. };
  215. static int __init pda_power_init(void)
  216. {
  217. return platform_driver_register(&pda_power_pdrv);
  218. }
  219. static void __exit pda_power_exit(void)
  220. {
  221. platform_driver_unregister(&pda_power_pdrv);
  222. }
  223. module_init(pda_power_init);
  224. module_exit(pda_power_exit);
  225. MODULE_LICENSE("GPL");
  226. MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");