pda_power.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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/err.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/notifier.h>
  17. #include <linux/power_supply.h>
  18. #include <linux/pda_power.h>
  19. #include <linux/regulator/consumer.h>
  20. #include <linux/timer.h>
  21. #include <linux/jiffies.h>
  22. #include <linux/usb/otg.h>
  23. static inline unsigned int get_irq_flags(struct resource *res)
  24. {
  25. return IRQF_SHARED | (res->flags & IRQF_TRIGGER_MASK);
  26. }
  27. static struct device *dev;
  28. static struct pda_power_pdata *pdata;
  29. static struct resource *ac_irq, *usb_irq;
  30. static struct timer_list charger_timer;
  31. static struct timer_list supply_timer;
  32. static struct timer_list polling_timer;
  33. static int polling;
  34. #ifdef CONFIG_USB_OTG_UTILS
  35. static struct usb_phy *transceiver;
  36. static struct notifier_block otg_nb;
  37. #endif
  38. static struct regulator *ac_draw;
  39. enum {
  40. PDA_PSY_OFFLINE = 0,
  41. PDA_PSY_ONLINE = 1,
  42. PDA_PSY_TO_CHANGE,
  43. };
  44. static int new_ac_status = -1;
  45. static int new_usb_status = -1;
  46. static int ac_status = -1;
  47. static int usb_status = -1;
  48. static int pda_power_get_property(struct power_supply *psy,
  49. enum power_supply_property psp,
  50. union power_supply_propval *val)
  51. {
  52. switch (psp) {
  53. case POWER_SUPPLY_PROP_ONLINE:
  54. if (psy->type == POWER_SUPPLY_TYPE_MAINS)
  55. val->intval = pdata->is_ac_online ?
  56. pdata->is_ac_online() : 0;
  57. else
  58. val->intval = pdata->is_usb_online ?
  59. pdata->is_usb_online() : 0;
  60. break;
  61. default:
  62. return -EINVAL;
  63. }
  64. return 0;
  65. }
  66. static enum power_supply_property pda_power_props[] = {
  67. POWER_SUPPLY_PROP_ONLINE,
  68. };
  69. static char *pda_power_supplied_to[] = {
  70. "main-battery",
  71. "backup-battery",
  72. };
  73. static struct power_supply pda_psy_ac = {
  74. .name = "ac",
  75. .type = POWER_SUPPLY_TYPE_MAINS,
  76. .supplied_to = pda_power_supplied_to,
  77. .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
  78. .properties = pda_power_props,
  79. .num_properties = ARRAY_SIZE(pda_power_props),
  80. .get_property = pda_power_get_property,
  81. };
  82. static struct power_supply pda_psy_usb = {
  83. .name = "usb",
  84. .type = POWER_SUPPLY_TYPE_USB,
  85. .supplied_to = pda_power_supplied_to,
  86. .num_supplicants = ARRAY_SIZE(pda_power_supplied_to),
  87. .properties = pda_power_props,
  88. .num_properties = ARRAY_SIZE(pda_power_props),
  89. .get_property = pda_power_get_property,
  90. };
  91. static void update_status(void)
  92. {
  93. if (pdata->is_ac_online)
  94. new_ac_status = !!pdata->is_ac_online();
  95. if (pdata->is_usb_online)
  96. new_usb_status = !!pdata->is_usb_online();
  97. }
  98. static void update_charger(void)
  99. {
  100. static int regulator_enabled;
  101. int max_uA = pdata->ac_max_uA;
  102. if (pdata->set_charge) {
  103. if (new_ac_status > 0) {
  104. dev_dbg(dev, "charger on (AC)\n");
  105. pdata->set_charge(PDA_POWER_CHARGE_AC);
  106. } else if (new_usb_status > 0) {
  107. dev_dbg(dev, "charger on (USB)\n");
  108. pdata->set_charge(PDA_POWER_CHARGE_USB);
  109. } else {
  110. dev_dbg(dev, "charger off\n");
  111. pdata->set_charge(0);
  112. }
  113. } else if (ac_draw) {
  114. if (new_ac_status > 0) {
  115. regulator_set_current_limit(ac_draw, max_uA, max_uA);
  116. if (!regulator_enabled) {
  117. dev_dbg(dev, "charger on (AC)\n");
  118. WARN_ON(regulator_enable(ac_draw));
  119. regulator_enabled = 1;
  120. }
  121. } else {
  122. if (regulator_enabled) {
  123. dev_dbg(dev, "charger off\n");
  124. WARN_ON(regulator_disable(ac_draw));
  125. regulator_enabled = 0;
  126. }
  127. }
  128. }
  129. }
  130. static void supply_timer_func(unsigned long unused)
  131. {
  132. if (ac_status == PDA_PSY_TO_CHANGE) {
  133. ac_status = new_ac_status;
  134. power_supply_changed(&pda_psy_ac);
  135. }
  136. if (usb_status == PDA_PSY_TO_CHANGE) {
  137. usb_status = new_usb_status;
  138. power_supply_changed(&pda_psy_usb);
  139. }
  140. }
  141. static void psy_changed(void)
  142. {
  143. update_charger();
  144. /*
  145. * Okay, charger set. Now wait a bit before notifying supplicants,
  146. * charge power should stabilize.
  147. */
  148. mod_timer(&supply_timer,
  149. jiffies + msecs_to_jiffies(pdata->wait_for_charger));
  150. }
  151. static void charger_timer_func(unsigned long unused)
  152. {
  153. update_status();
  154. psy_changed();
  155. }
  156. static irqreturn_t power_changed_isr(int irq, void *power_supply)
  157. {
  158. if (power_supply == &pda_psy_ac)
  159. ac_status = PDA_PSY_TO_CHANGE;
  160. else if (power_supply == &pda_psy_usb)
  161. usb_status = PDA_PSY_TO_CHANGE;
  162. else
  163. return IRQ_NONE;
  164. /*
  165. * Wait a bit before reading ac/usb line status and setting charger,
  166. * because ac/usb status readings may lag from irq.
  167. */
  168. mod_timer(&charger_timer,
  169. jiffies + msecs_to_jiffies(pdata->wait_for_status));
  170. return IRQ_HANDLED;
  171. }
  172. static void polling_timer_func(unsigned long unused)
  173. {
  174. int changed = 0;
  175. dev_dbg(dev, "polling...\n");
  176. update_status();
  177. if (!ac_irq && new_ac_status != ac_status) {
  178. ac_status = PDA_PSY_TO_CHANGE;
  179. changed = 1;
  180. }
  181. if (!usb_irq && new_usb_status != usb_status) {
  182. usb_status = PDA_PSY_TO_CHANGE;
  183. changed = 1;
  184. }
  185. if (changed)
  186. psy_changed();
  187. mod_timer(&polling_timer,
  188. jiffies + msecs_to_jiffies(pdata->polling_interval));
  189. }
  190. #ifdef CONFIG_USB_OTG_UTILS
  191. static int otg_is_usb_online(void)
  192. {
  193. return (transceiver->last_event == USB_EVENT_VBUS ||
  194. transceiver->last_event == USB_EVENT_ENUMERATED);
  195. }
  196. static int otg_is_ac_online(void)
  197. {
  198. return (transceiver->last_event == USB_EVENT_CHARGER);
  199. }
  200. static int otg_handle_notification(struct notifier_block *nb,
  201. unsigned long event, void *unused)
  202. {
  203. switch (event) {
  204. case USB_EVENT_CHARGER:
  205. ac_status = PDA_PSY_TO_CHANGE;
  206. break;
  207. case USB_EVENT_VBUS:
  208. case USB_EVENT_ENUMERATED:
  209. usb_status = PDA_PSY_TO_CHANGE;
  210. break;
  211. case USB_EVENT_NONE:
  212. ac_status = PDA_PSY_TO_CHANGE;
  213. usb_status = PDA_PSY_TO_CHANGE;
  214. break;
  215. default:
  216. return NOTIFY_OK;
  217. }
  218. /*
  219. * Wait a bit before reading ac/usb line status and setting charger,
  220. * because ac/usb status readings may lag from irq.
  221. */
  222. mod_timer(&charger_timer,
  223. jiffies + msecs_to_jiffies(pdata->wait_for_status));
  224. return NOTIFY_OK;
  225. }
  226. #endif
  227. static int pda_power_probe(struct platform_device *pdev)
  228. {
  229. int ret = 0;
  230. dev = &pdev->dev;
  231. if (pdev->id != -1) {
  232. dev_err(dev, "it's meaningless to register several "
  233. "pda_powers; use id = -1\n");
  234. ret = -EINVAL;
  235. goto wrongid;
  236. }
  237. pdata = pdev->dev.platform_data;
  238. if (pdata->init) {
  239. ret = pdata->init(dev);
  240. if (ret < 0)
  241. goto init_failed;
  242. }
  243. update_status();
  244. update_charger();
  245. if (!pdata->wait_for_status)
  246. pdata->wait_for_status = 500;
  247. if (!pdata->wait_for_charger)
  248. pdata->wait_for_charger = 500;
  249. if (!pdata->polling_interval)
  250. pdata->polling_interval = 2000;
  251. if (!pdata->ac_max_uA)
  252. pdata->ac_max_uA = 500000;
  253. setup_timer(&charger_timer, charger_timer_func, 0);
  254. setup_timer(&supply_timer, supply_timer_func, 0);
  255. ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
  256. usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
  257. if (pdata->supplied_to) {
  258. pda_psy_ac.supplied_to = pdata->supplied_to;
  259. pda_psy_ac.num_supplicants = pdata->num_supplicants;
  260. pda_psy_usb.supplied_to = pdata->supplied_to;
  261. pda_psy_usb.num_supplicants = pdata->num_supplicants;
  262. }
  263. ac_draw = regulator_get(dev, "ac_draw");
  264. if (IS_ERR(ac_draw)) {
  265. dev_dbg(dev, "couldn't get ac_draw regulator\n");
  266. ac_draw = NULL;
  267. ret = PTR_ERR(ac_draw);
  268. }
  269. #ifdef CONFIG_USB_OTG_UTILS
  270. transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  271. if (!IS_ERR_OR_NULL(transceiver)) {
  272. if (!pdata->is_usb_online)
  273. pdata->is_usb_online = otg_is_usb_online;
  274. if (!pdata->is_ac_online)
  275. pdata->is_ac_online = otg_is_ac_online;
  276. }
  277. #endif
  278. if (pdata->is_ac_online) {
  279. ret = power_supply_register(&pdev->dev, &pda_psy_ac);
  280. if (ret) {
  281. dev_err(dev, "failed to register %s power supply\n",
  282. pda_psy_ac.name);
  283. goto ac_supply_failed;
  284. }
  285. if (ac_irq) {
  286. ret = request_irq(ac_irq->start, power_changed_isr,
  287. get_irq_flags(ac_irq), ac_irq->name,
  288. &pda_psy_ac);
  289. if (ret) {
  290. dev_err(dev, "request ac irq failed\n");
  291. goto ac_irq_failed;
  292. }
  293. } else {
  294. polling = 1;
  295. }
  296. }
  297. if (pdata->is_usb_online) {
  298. ret = power_supply_register(&pdev->dev, &pda_psy_usb);
  299. if (ret) {
  300. dev_err(dev, "failed to register %s power supply\n",
  301. pda_psy_usb.name);
  302. goto usb_supply_failed;
  303. }
  304. if (usb_irq) {
  305. ret = request_irq(usb_irq->start, power_changed_isr,
  306. get_irq_flags(usb_irq),
  307. usb_irq->name, &pda_psy_usb);
  308. if (ret) {
  309. dev_err(dev, "request usb irq failed\n");
  310. goto usb_irq_failed;
  311. }
  312. } else {
  313. polling = 1;
  314. }
  315. }
  316. #ifdef CONFIG_USB_OTG_UTILS
  317. if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) {
  318. otg_nb.notifier_call = otg_handle_notification;
  319. ret = usb_register_notifier(transceiver, &otg_nb);
  320. if (ret) {
  321. dev_err(dev, "failure to register otg notifier\n");
  322. goto otg_reg_notifier_failed;
  323. }
  324. polling = 0;
  325. }
  326. #endif
  327. if (polling) {
  328. dev_dbg(dev, "will poll for status\n");
  329. setup_timer(&polling_timer, polling_timer_func, 0);
  330. mod_timer(&polling_timer,
  331. jiffies + msecs_to_jiffies(pdata->polling_interval));
  332. }
  333. if (ac_irq || usb_irq)
  334. device_init_wakeup(&pdev->dev, 1);
  335. return 0;
  336. #ifdef CONFIG_USB_OTG_UTILS
  337. otg_reg_notifier_failed:
  338. if (pdata->is_usb_online && usb_irq)
  339. free_irq(usb_irq->start, &pda_psy_usb);
  340. #endif
  341. usb_irq_failed:
  342. if (pdata->is_usb_online)
  343. power_supply_unregister(&pda_psy_usb);
  344. usb_supply_failed:
  345. if (pdata->is_ac_online && ac_irq)
  346. free_irq(ac_irq->start, &pda_psy_ac);
  347. #ifdef CONFIG_USB_OTG_UTILS
  348. if (!IS_ERR_OR_NULL(transceiver))
  349. usb_put_phy(transceiver);
  350. #endif
  351. ac_irq_failed:
  352. if (pdata->is_ac_online)
  353. power_supply_unregister(&pda_psy_ac);
  354. ac_supply_failed:
  355. if (ac_draw) {
  356. regulator_put(ac_draw);
  357. ac_draw = NULL;
  358. }
  359. if (pdata->exit)
  360. pdata->exit(dev);
  361. init_failed:
  362. wrongid:
  363. return ret;
  364. }
  365. static int pda_power_remove(struct platform_device *pdev)
  366. {
  367. if (pdata->is_usb_online && usb_irq)
  368. free_irq(usb_irq->start, &pda_psy_usb);
  369. if (pdata->is_ac_online && ac_irq)
  370. free_irq(ac_irq->start, &pda_psy_ac);
  371. if (polling)
  372. del_timer_sync(&polling_timer);
  373. del_timer_sync(&charger_timer);
  374. del_timer_sync(&supply_timer);
  375. if (pdata->is_usb_online)
  376. power_supply_unregister(&pda_psy_usb);
  377. if (pdata->is_ac_online)
  378. power_supply_unregister(&pda_psy_ac);
  379. #ifdef CONFIG_USB_OTG_UTILS
  380. if (!IS_ERR_OR_NULL(transceiver))
  381. usb_put_phy(transceiver);
  382. #endif
  383. if (ac_draw) {
  384. regulator_put(ac_draw);
  385. ac_draw = NULL;
  386. }
  387. if (pdata->exit)
  388. pdata->exit(dev);
  389. return 0;
  390. }
  391. #ifdef CONFIG_PM
  392. static int ac_wakeup_enabled;
  393. static int usb_wakeup_enabled;
  394. static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
  395. {
  396. if (pdata->suspend) {
  397. int ret = pdata->suspend(state);
  398. if (ret)
  399. return ret;
  400. }
  401. if (device_may_wakeup(&pdev->dev)) {
  402. if (ac_irq)
  403. ac_wakeup_enabled = !enable_irq_wake(ac_irq->start);
  404. if (usb_irq)
  405. usb_wakeup_enabled = !enable_irq_wake(usb_irq->start);
  406. }
  407. return 0;
  408. }
  409. static int pda_power_resume(struct platform_device *pdev)
  410. {
  411. if (device_may_wakeup(&pdev->dev)) {
  412. if (usb_irq && usb_wakeup_enabled)
  413. disable_irq_wake(usb_irq->start);
  414. if (ac_irq && ac_wakeup_enabled)
  415. disable_irq_wake(ac_irq->start);
  416. }
  417. if (pdata->resume)
  418. return pdata->resume();
  419. return 0;
  420. }
  421. #else
  422. #define pda_power_suspend NULL
  423. #define pda_power_resume NULL
  424. #endif /* CONFIG_PM */
  425. static struct platform_driver pda_power_pdrv = {
  426. .driver = {
  427. .name = "pda-power",
  428. },
  429. .probe = pda_power_probe,
  430. .remove = pda_power_remove,
  431. .suspend = pda_power_suspend,
  432. .resume = pda_power_resume,
  433. };
  434. module_platform_driver(pda_power_pdrv);
  435. MODULE_LICENSE("GPL");
  436. MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
  437. MODULE_ALIAS("platform:pda-power");