pda_power.c 12 KB

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