pda_power.c 12 KB

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