boost_manager.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2017 MediaTek Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See http://www.gnu.org/licenses/gpl-2.0.html for more details.
  12. */
  13. #include <linux/debugfs.h>
  14. #include <linux/irq.h>
  15. #include <linux/kernel.h>
  16. #include <linux/of_device.h>
  17. #include <linux/module.h>
  18. #include <linux/pinctrl/consumer.h>
  19. #include <linux/seq_file.h>
  20. #include <linux/uaccess.h>
  21. #if CONFIG_MTK_GAUGE_VERSION == 30
  22. #include <mtk_gauge_time_service.h>
  23. #include <mt-plat/charger_class.h>
  24. #include <linux/alarmtimer.h>
  25. #endif
  26. struct usbotg_boost {
  27. struct platform_device *pdev;
  28. struct charger_device *primary_charger;
  29. #if CONFIG_MTK_GAUGE_VERSION == 30
  30. struct alarm otg_timer;
  31. struct timespec endtime;
  32. struct workqueue_struct *boost_workq;
  33. struct work_struct kick_work;
  34. unsigned int polling_interval;
  35. bool polling_enabled;
  36. #endif
  37. };
  38. static struct usbotg_boost *g_info;
  39. static struct pinctrl *drvvbus;
  40. static struct pinctrl_state *drvvbus_high;
  41. static struct pinctrl_state *drvvbus_low;
  42. #if CONFIG_MTK_GAUGE_VERSION == 30
  43. static void usbotg_alarm_start_timer(struct usbotg_boost *info)
  44. {
  45. struct timespec time, time_now;
  46. ktime_t ktime;
  47. get_monotonic_boottime(&time_now);
  48. time.tv_sec = info->polling_interval;
  49. time.tv_nsec = 0;
  50. info->endtime = timespec_add(time_now, time);
  51. ktime = ktime_set(info->endtime.tv_sec, info->endtime.tv_nsec);
  52. pr_info("%s: alarm timer start\n", __func__);
  53. alarm_start(&info->otg_timer, ktime);
  54. }
  55. static void enable_boost_polling(bool poll_en)
  56. {
  57. if (g_info) {
  58. if (poll_en) {
  59. usbotg_alarm_start_timer(g_info);
  60. g_info->polling_enabled = true;
  61. } else {
  62. g_info->polling_enabled = false;
  63. alarm_try_to_cancel(&g_info->otg_timer);
  64. }
  65. }
  66. }
  67. static void usbotg_boost_kick_work(struct work_struct *work)
  68. {
  69. struct usbotg_boost *usb_boost_manager =
  70. container_of(work, struct usbotg_boost, kick_work);
  71. pr_info("%s\n", __func__);
  72. charger_dev_kick_wdt(usb_boost_manager->primary_charger);
  73. if (usb_boost_manager->polling_enabled == true)
  74. usbotg_alarm_start_timer(usb_boost_manager);
  75. }
  76. static enum alarmtimer_restart
  77. usbotg_alarm_timer_func(struct alarm *alarm, ktime_t now)
  78. {
  79. struct usbotg_boost *usb_boost_manager =
  80. container_of(alarm, struct usbotg_boost, otg_timer);
  81. queue_work(usb_boost_manager->boost_workq,
  82. &usb_boost_manager->kick_work);
  83. return ALARMTIMER_NORESTART;
  84. }
  85. #endif
  86. int usb_otg_set_vbus(int is_on)
  87. {
  88. if (!IS_ERR(drvvbus)) {
  89. if (is_on)
  90. pinctrl_select_state(drvvbus, drvvbus_high);
  91. else
  92. pinctrl_select_state(drvvbus, drvvbus_low);
  93. return 0;
  94. }
  95. if (!g_info)
  96. return -1;
  97. #if CONFIG_MTK_GAUGE_VERSION == 30
  98. if (is_on) {
  99. charger_dev_enable_otg(g_info->primary_charger, true);
  100. charger_dev_set_boost_current_limit(g_info->primary_charger,
  101. 1500000);
  102. charger_dev_kick_wdt(g_info->primary_charger);
  103. enable_boost_polling(true);
  104. } else {
  105. charger_dev_enable_otg(g_info->primary_charger, false);
  106. enable_boost_polling(false);
  107. }
  108. #else
  109. if (is_on) {
  110. charger_dev_enable_otg(g_info->primary_charger, true);
  111. charger_dev_set_boost_current_limit(g_info->primary_charger,
  112. 1500000);
  113. } else {
  114. charger_dev_enable_otg(primary_charger, false);
  115. }
  116. #endif
  117. return 0;
  118. }
  119. static int usbotg_boost_probe(struct platform_device *pdev)
  120. {
  121. struct usbotg_boost *info = NULL;
  122. struct device *dev = &pdev->dev;
  123. struct device_node *node = dev->of_node;
  124. drvvbus = devm_pinctrl_get(dev);
  125. if (IS_ERR(drvvbus)) {
  126. pr_notice("Cannot find usb pinctrl!\n");
  127. } else {
  128. drvvbus_high = pinctrl_lookup_state(drvvbus, "drvvbus_high");
  129. if (IS_ERR(drvvbus_high)) {
  130. pr_notice("Cannot find usb pinctrl drvvbus_high\n");
  131. return -EINVAL;
  132. }
  133. drvvbus_low = pinctrl_lookup_state(drvvbus, "drvvbus_low");
  134. if (IS_ERR(drvvbus_low)) {
  135. pr_notice("Cannot find usb pinctrl drvvbus_low\n");
  136. return -EINVAL;
  137. }
  138. return 0;
  139. }
  140. info = devm_kzalloc(&pdev->dev, sizeof(struct usbotg_boost),
  141. GFP_KERNEL);
  142. if (!info)
  143. return -ENOMEM;
  144. platform_set_drvdata(pdev, info);
  145. info->pdev = pdev;
  146. info->primary_charger = get_charger_by_name("primary_chg");
  147. if (!info->primary_charger) {
  148. pr_info("%s: get primary charger device failed\n", __func__);
  149. return -ENODEV;
  150. }
  151. #if CONFIG_MTK_GAUGE_VERSION == 30
  152. alarm_init(&info->otg_timer, ALARM_BOOTTIME,
  153. usbotg_alarm_timer_func);
  154. if (of_property_read_u32(node, "boost_period",
  155. (u32 *) &info->polling_interval))
  156. return -EINVAL;
  157. info->polling_interval = 30;
  158. info->boost_workq = create_singlethread_workqueue("boost_workq");
  159. INIT_WORK(&info->kick_work, usbotg_boost_kick_work);
  160. #endif
  161. g_info = info;
  162. return 0;
  163. }
  164. static int usbotg_boost_remove(struct platform_device *pdev)
  165. {
  166. return 0;
  167. }
  168. static const struct of_device_id usb_boost_of_match[] = {
  169. {.compatible = "mediatek,usb_boost"},
  170. {},
  171. };
  172. MODULE_DEVICE_TABLE(of, usb_boost_of_match);
  173. static struct platform_driver usb_boost_driver = {
  174. .remove = usbotg_boost_remove,
  175. .probe = usbotg_boost_probe,
  176. .driver = {
  177. .name = "mediatek,usb_boost",
  178. .of_match_table = usb_boost_of_match,
  179. },
  180. };
  181. static int __init usb_boost_init(void)
  182. {
  183. platform_driver_register(&usb_boost_driver);
  184. return 0;
  185. }
  186. late_initcall(usb_boost_init);
  187. static void __exit usb_boost_init_cleanup(void)
  188. {
  189. }
  190. module_exit(usb_boost_init_cleanup);