pda_power.c 12 KB

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