pda_power.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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 delayed_work charger_work;
  31. static struct delayed_work polling_work;
  32. static struct delayed_work supply_work;
  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_work_func(struct work_struct *work)
  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. cancel_delayed_work(&supply_work);
  146. schedule_delayed_work(&supply_work,
  147. msecs_to_jiffies(pdata->wait_for_charger));
  148. }
  149. static void charger_work_func(struct work_struct *work)
  150. {
  151. update_status();
  152. psy_changed();
  153. }
  154. static irqreturn_t power_changed_isr(int irq, void *power_supply)
  155. {
  156. if (power_supply == pda_psy_ac)
  157. ac_status = PDA_PSY_TO_CHANGE;
  158. else if (power_supply == pda_psy_usb)
  159. usb_status = PDA_PSY_TO_CHANGE;
  160. else
  161. return IRQ_NONE;
  162. /*
  163. * Wait a bit before reading ac/usb line status and setting charger,
  164. * because ac/usb status readings may lag from irq.
  165. */
  166. cancel_delayed_work(&charger_work);
  167. schedule_delayed_work(&charger_work,
  168. msecs_to_jiffies(pdata->wait_for_status));
  169. return IRQ_HANDLED;
  170. }
  171. static void polling_work_func(struct work_struct *work)
  172. {
  173. int changed = 0;
  174. dev_dbg(dev, "polling...\n");
  175. update_status();
  176. if (!ac_irq && new_ac_status != ac_status) {
  177. ac_status = PDA_PSY_TO_CHANGE;
  178. changed = 1;
  179. }
  180. if (!usb_irq && new_usb_status != usb_status) {
  181. usb_status = PDA_PSY_TO_CHANGE;
  182. changed = 1;
  183. }
  184. if (changed)
  185. psy_changed();
  186. cancel_delayed_work(&polling_work);
  187. schedule_delayed_work(&polling_work,
  188. msecs_to_jiffies(pdata->polling_interval));
  189. }
  190. #if IS_ENABLED(CONFIG_USB_PHY)
  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. cancel_delayed_work(&charger_work);
  223. schedule_delayed_work(&charger_work,
  224. msecs_to_jiffies(pdata->wait_for_status));
  225. return NOTIFY_OK;
  226. }
  227. #endif
  228. static int pda_power_probe(struct platform_device *pdev)
  229. {
  230. struct power_supply_config psy_cfg = {};
  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. ac_draw = regulator_get(dev, "ac_draw");
  246. if (IS_ERR(ac_draw)) {
  247. dev_dbg(dev, "couldn't get ac_draw regulator\n");
  248. ac_draw = NULL;
  249. }
  250. update_status();
  251. update_charger();
  252. if (!pdata->wait_for_status)
  253. pdata->wait_for_status = 500;
  254. if (!pdata->wait_for_charger)
  255. pdata->wait_for_charger = 500;
  256. if (!pdata->polling_interval)
  257. pdata->polling_interval = 2000;
  258. if (!pdata->ac_max_uA)
  259. pdata->ac_max_uA = 500000;
  260. INIT_DELAYED_WORK(&charger_work, charger_work_func);
  261. INIT_DELAYED_WORK(&supply_work, supply_work_func);
  262. ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
  263. usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
  264. if (pdata->supplied_to) {
  265. psy_cfg.supplied_to = pdata->supplied_to;
  266. psy_cfg.num_supplicants = pdata->num_supplicants;
  267. } else {
  268. psy_cfg.supplied_to = pda_power_supplied_to;
  269. psy_cfg.num_supplicants = ARRAY_SIZE(pda_power_supplied_to);
  270. }
  271. #if IS_ENABLED(CONFIG_USB_PHY)
  272. transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
  273. if (!IS_ERR_OR_NULL(transceiver)) {
  274. if (!pdata->is_usb_online)
  275. pdata->is_usb_online = otg_is_usb_online;
  276. if (!pdata->is_ac_online)
  277. pdata->is_ac_online = otg_is_ac_online;
  278. }
  279. #endif
  280. if (pdata->is_ac_online) {
  281. pda_psy_ac = power_supply_register(&pdev->dev,
  282. &pda_psy_ac_desc, &psy_cfg);
  283. if (IS_ERR(pda_psy_ac)) {
  284. dev_err(dev, "failed to register %s power supply\n",
  285. pda_psy_ac_desc.name);
  286. ret = PTR_ERR(pda_psy_ac);
  287. goto ac_supply_failed;
  288. }
  289. if (ac_irq) {
  290. ret = request_irq(ac_irq->start, power_changed_isr,
  291. get_irq_flags(ac_irq), ac_irq->name,
  292. pda_psy_ac);
  293. if (ret) {
  294. dev_err(dev, "request ac irq failed\n");
  295. goto ac_irq_failed;
  296. }
  297. } else {
  298. polling = 1;
  299. }
  300. }
  301. if (pdata->is_usb_online) {
  302. pda_psy_usb = power_supply_register(&pdev->dev,
  303. &pda_psy_usb_desc,
  304. &psy_cfg);
  305. if (IS_ERR(pda_psy_usb)) {
  306. dev_err(dev, "failed to register %s power supply\n",
  307. pda_psy_usb_desc.name);
  308. ret = PTR_ERR(pda_psy_usb);
  309. goto usb_supply_failed;
  310. }
  311. if (usb_irq) {
  312. ret = request_irq(usb_irq->start, power_changed_isr,
  313. get_irq_flags(usb_irq),
  314. usb_irq->name, pda_psy_usb);
  315. if (ret) {
  316. dev_err(dev, "request usb irq failed\n");
  317. goto usb_irq_failed;
  318. }
  319. } else {
  320. polling = 1;
  321. }
  322. }
  323. #if IS_ENABLED(CONFIG_USB_PHY)
  324. if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) {
  325. otg_nb.notifier_call = otg_handle_notification;
  326. ret = usb_register_notifier(transceiver, &otg_nb);
  327. if (ret) {
  328. dev_err(dev, "failure to register otg notifier\n");
  329. goto otg_reg_notifier_failed;
  330. }
  331. polling = 0;
  332. }
  333. #endif
  334. if (polling) {
  335. dev_dbg(dev, "will poll for status\n");
  336. INIT_DELAYED_WORK(&polling_work, polling_work_func);
  337. cancel_delayed_work(&polling_work);
  338. schedule_delayed_work(&polling_work,
  339. msecs_to_jiffies(pdata->polling_interval));
  340. }
  341. if (ac_irq || usb_irq)
  342. device_init_wakeup(&pdev->dev, 1);
  343. return 0;
  344. #if IS_ENABLED(CONFIG_USB_PHY)
  345. otg_reg_notifier_failed:
  346. if (pdata->is_usb_online && usb_irq)
  347. free_irq(usb_irq->start, pda_psy_usb);
  348. #endif
  349. usb_irq_failed:
  350. if (pdata->is_usb_online)
  351. power_supply_unregister(pda_psy_usb);
  352. usb_supply_failed:
  353. if (pdata->is_ac_online && ac_irq)
  354. free_irq(ac_irq->start, pda_psy_ac);
  355. #if IS_ENABLED(CONFIG_USB_PHY)
  356. if (!IS_ERR_OR_NULL(transceiver))
  357. usb_put_phy(transceiver);
  358. #endif
  359. ac_irq_failed:
  360. if (pdata->is_ac_online)
  361. power_supply_unregister(pda_psy_ac);
  362. ac_supply_failed:
  363. if (ac_draw) {
  364. regulator_put(ac_draw);
  365. ac_draw = NULL;
  366. }
  367. if (pdata->exit)
  368. pdata->exit(dev);
  369. init_failed:
  370. wrongid:
  371. return ret;
  372. }
  373. static int pda_power_remove(struct platform_device *pdev)
  374. {
  375. if (pdata->is_usb_online && usb_irq)
  376. free_irq(usb_irq->start, pda_psy_usb);
  377. if (pdata->is_ac_online && ac_irq)
  378. free_irq(ac_irq->start, pda_psy_ac);
  379. if (polling)
  380. cancel_delayed_work_sync(&polling_work);
  381. cancel_delayed_work_sync(&charger_work);
  382. cancel_delayed_work_sync(&supply_work);
  383. if (pdata->is_usb_online)
  384. power_supply_unregister(pda_psy_usb);
  385. if (pdata->is_ac_online)
  386. power_supply_unregister(pda_psy_ac);
  387. #if IS_ENABLED(CONFIG_USB_PHY)
  388. if (!IS_ERR_OR_NULL(transceiver))
  389. usb_put_phy(transceiver);
  390. #endif
  391. if (ac_draw) {
  392. regulator_put(ac_draw);
  393. ac_draw = NULL;
  394. }
  395. if (pdata->exit)
  396. pdata->exit(dev);
  397. return 0;
  398. }
  399. #ifdef CONFIG_PM
  400. static int ac_wakeup_enabled;
  401. static int usb_wakeup_enabled;
  402. static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
  403. {
  404. if (pdata->suspend) {
  405. int ret = pdata->suspend(state);
  406. if (ret)
  407. return ret;
  408. }
  409. if (device_may_wakeup(&pdev->dev)) {
  410. if (ac_irq)
  411. ac_wakeup_enabled = !enable_irq_wake(ac_irq->start);
  412. if (usb_irq)
  413. usb_wakeup_enabled = !enable_irq_wake(usb_irq->start);
  414. }
  415. return 0;
  416. }
  417. static int pda_power_resume(struct platform_device *pdev)
  418. {
  419. if (device_may_wakeup(&pdev->dev)) {
  420. if (usb_irq && usb_wakeup_enabled)
  421. disable_irq_wake(usb_irq->start);
  422. if (ac_irq && ac_wakeup_enabled)
  423. disable_irq_wake(ac_irq->start);
  424. }
  425. if (pdata->resume)
  426. return pdata->resume();
  427. return 0;
  428. }
  429. #else
  430. #define pda_power_suspend NULL
  431. #define pda_power_resume NULL
  432. #endif /* CONFIG_PM */
  433. static struct platform_driver pda_power_pdrv = {
  434. .driver = {
  435. .name = "pda-power",
  436. },
  437. .probe = pda_power_probe,
  438. .remove = pda_power_remove,
  439. .suspend = pda_power_suspend,
  440. .resume = pda_power_resume,
  441. };
  442. module_platform_driver(pda_power_pdrv);
  443. MODULE_LICENSE("GPL");
  444. MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
  445. MODULE_ALIAS("platform:pda-power");