dwc3-exynos.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * dwc3-exynos.c - Samsung EXYNOS DWC3 Specific Glue layer
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com
  6. *
  7. * Author: Anton Tikhomirov <av.tikhomirov@samsung.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/clk.h>
  23. #include <linux/usb/otg.h>
  24. #include <linux/usb/usb_phy_generic.h>
  25. #include <linux/of.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/regulator/consumer.h>
  28. struct dwc3_exynos {
  29. struct platform_device *usb2_phy;
  30. struct platform_device *usb3_phy;
  31. struct device *dev;
  32. struct clk *clk;
  33. struct clk *susp_clk;
  34. struct clk *axius_clk;
  35. struct regulator *vdd33;
  36. struct regulator *vdd10;
  37. };
  38. static int dwc3_exynos_register_phys(struct dwc3_exynos *exynos)
  39. {
  40. struct usb_phy_generic_platform_data pdata;
  41. struct platform_device *pdev;
  42. int ret;
  43. memset(&pdata, 0x00, sizeof(pdata));
  44. pdev = platform_device_alloc("usb_phy_generic", PLATFORM_DEVID_AUTO);
  45. if (!pdev)
  46. return -ENOMEM;
  47. exynos->usb2_phy = pdev;
  48. pdata.type = USB_PHY_TYPE_USB2;
  49. pdata.gpio_reset = -1;
  50. ret = platform_device_add_data(exynos->usb2_phy, &pdata, sizeof(pdata));
  51. if (ret)
  52. goto err1;
  53. pdev = platform_device_alloc("usb_phy_generic", PLATFORM_DEVID_AUTO);
  54. if (!pdev) {
  55. ret = -ENOMEM;
  56. goto err1;
  57. }
  58. exynos->usb3_phy = pdev;
  59. pdata.type = USB_PHY_TYPE_USB3;
  60. ret = platform_device_add_data(exynos->usb3_phy, &pdata, sizeof(pdata));
  61. if (ret)
  62. goto err2;
  63. ret = platform_device_add(exynos->usb2_phy);
  64. if (ret)
  65. goto err2;
  66. ret = platform_device_add(exynos->usb3_phy);
  67. if (ret)
  68. goto err3;
  69. return 0;
  70. err3:
  71. platform_device_del(exynos->usb2_phy);
  72. err2:
  73. platform_device_put(exynos->usb3_phy);
  74. err1:
  75. platform_device_put(exynos->usb2_phy);
  76. return ret;
  77. }
  78. static int dwc3_exynos_remove_child(struct device *dev, void *unused)
  79. {
  80. struct platform_device *pdev = to_platform_device(dev);
  81. platform_device_unregister(pdev);
  82. return 0;
  83. }
  84. static int dwc3_exynos_probe(struct platform_device *pdev)
  85. {
  86. struct dwc3_exynos *exynos;
  87. struct device *dev = &pdev->dev;
  88. struct device_node *node = dev->of_node;
  89. int ret;
  90. exynos = devm_kzalloc(dev, sizeof(*exynos), GFP_KERNEL);
  91. if (!exynos)
  92. return -ENOMEM;
  93. platform_set_drvdata(pdev, exynos);
  94. exynos->dev = dev;
  95. exynos->clk = devm_clk_get(dev, "usbdrd30");
  96. if (IS_ERR(exynos->clk)) {
  97. dev_err(dev, "couldn't get clock\n");
  98. return -EINVAL;
  99. }
  100. ret = clk_prepare_enable(exynos->clk);
  101. if (ret)
  102. return ret;
  103. exynos->susp_clk = devm_clk_get(dev, "usbdrd30_susp_clk");
  104. if (IS_ERR(exynos->susp_clk))
  105. exynos->susp_clk = NULL;
  106. ret = clk_prepare_enable(exynos->susp_clk);
  107. if (ret)
  108. goto susp_clk_err;
  109. if (of_device_is_compatible(node, "samsung,exynos7-dwusb3")) {
  110. exynos->axius_clk = devm_clk_get(dev, "usbdrd30_axius_clk");
  111. if (IS_ERR(exynos->axius_clk)) {
  112. dev_err(dev, "no AXI UpScaler clk specified\n");
  113. ret = -ENODEV;
  114. goto axius_clk_err;
  115. }
  116. ret = clk_prepare_enable(exynos->axius_clk);
  117. if (ret)
  118. goto axius_clk_err;
  119. } else {
  120. exynos->axius_clk = NULL;
  121. }
  122. exynos->vdd33 = devm_regulator_get(dev, "vdd33");
  123. if (IS_ERR(exynos->vdd33)) {
  124. ret = PTR_ERR(exynos->vdd33);
  125. goto vdd33_err;
  126. }
  127. ret = regulator_enable(exynos->vdd33);
  128. if (ret) {
  129. dev_err(dev, "Failed to enable VDD33 supply\n");
  130. goto vdd33_err;
  131. }
  132. exynos->vdd10 = devm_regulator_get(dev, "vdd10");
  133. if (IS_ERR(exynos->vdd10)) {
  134. ret = PTR_ERR(exynos->vdd10);
  135. goto vdd10_err;
  136. }
  137. ret = regulator_enable(exynos->vdd10);
  138. if (ret) {
  139. dev_err(dev, "Failed to enable VDD10 supply\n");
  140. goto vdd10_err;
  141. }
  142. ret = dwc3_exynos_register_phys(exynos);
  143. if (ret) {
  144. dev_err(dev, "couldn't register PHYs\n");
  145. goto phys_err;
  146. }
  147. if (node) {
  148. ret = of_platform_populate(node, NULL, NULL, dev);
  149. if (ret) {
  150. dev_err(dev, "failed to add dwc3 core\n");
  151. goto populate_err;
  152. }
  153. } else {
  154. dev_err(dev, "no device node, failed to add dwc3 core\n");
  155. ret = -ENODEV;
  156. goto populate_err;
  157. }
  158. return 0;
  159. populate_err:
  160. platform_device_unregister(exynos->usb2_phy);
  161. platform_device_unregister(exynos->usb3_phy);
  162. phys_err:
  163. regulator_disable(exynos->vdd10);
  164. vdd10_err:
  165. regulator_disable(exynos->vdd33);
  166. vdd33_err:
  167. clk_disable_unprepare(exynos->axius_clk);
  168. axius_clk_err:
  169. clk_disable_unprepare(exynos->susp_clk);
  170. susp_clk_err:
  171. clk_disable_unprepare(exynos->clk);
  172. return ret;
  173. }
  174. static int dwc3_exynos_remove(struct platform_device *pdev)
  175. {
  176. struct dwc3_exynos *exynos = platform_get_drvdata(pdev);
  177. device_for_each_child(&pdev->dev, NULL, dwc3_exynos_remove_child);
  178. platform_device_unregister(exynos->usb2_phy);
  179. platform_device_unregister(exynos->usb3_phy);
  180. clk_disable_unprepare(exynos->axius_clk);
  181. clk_disable_unprepare(exynos->susp_clk);
  182. clk_disable_unprepare(exynos->clk);
  183. regulator_disable(exynos->vdd33);
  184. regulator_disable(exynos->vdd10);
  185. return 0;
  186. }
  187. static const struct of_device_id exynos_dwc3_match[] = {
  188. { .compatible = "samsung,exynos5250-dwusb3" },
  189. { .compatible = "samsung,exynos7-dwusb3" },
  190. {},
  191. };
  192. MODULE_DEVICE_TABLE(of, exynos_dwc3_match);
  193. #ifdef CONFIG_PM_SLEEP
  194. static int dwc3_exynos_suspend(struct device *dev)
  195. {
  196. struct dwc3_exynos *exynos = dev_get_drvdata(dev);
  197. clk_disable(exynos->axius_clk);
  198. clk_disable(exynos->clk);
  199. regulator_disable(exynos->vdd33);
  200. regulator_disable(exynos->vdd10);
  201. return 0;
  202. }
  203. static int dwc3_exynos_resume(struct device *dev)
  204. {
  205. struct dwc3_exynos *exynos = dev_get_drvdata(dev);
  206. int ret;
  207. ret = regulator_enable(exynos->vdd33);
  208. if (ret) {
  209. dev_err(dev, "Failed to enable VDD33 supply\n");
  210. return ret;
  211. }
  212. ret = regulator_enable(exynos->vdd10);
  213. if (ret) {
  214. dev_err(dev, "Failed to enable VDD10 supply\n");
  215. return ret;
  216. }
  217. clk_enable(exynos->clk);
  218. clk_enable(exynos->axius_clk);
  219. /* runtime set active to reflect active state. */
  220. pm_runtime_disable(dev);
  221. pm_runtime_set_active(dev);
  222. pm_runtime_enable(dev);
  223. return 0;
  224. }
  225. static const struct dev_pm_ops dwc3_exynos_dev_pm_ops = {
  226. SET_SYSTEM_SLEEP_PM_OPS(dwc3_exynos_suspend, dwc3_exynos_resume)
  227. };
  228. #define DEV_PM_OPS (&dwc3_exynos_dev_pm_ops)
  229. #else
  230. #define DEV_PM_OPS NULL
  231. #endif /* CONFIG_PM_SLEEP */
  232. static struct platform_driver dwc3_exynos_driver = {
  233. .probe = dwc3_exynos_probe,
  234. .remove = dwc3_exynos_remove,
  235. .driver = {
  236. .name = "exynos-dwc3",
  237. .of_match_table = exynos_dwc3_match,
  238. .pm = DEV_PM_OPS,
  239. },
  240. };
  241. module_platform_driver(dwc3_exynos_driver);
  242. MODULE_AUTHOR("Anton Tikhomirov <av.tikhomirov@samsung.com>");
  243. MODULE_LICENSE("GPL v2");
  244. MODULE_DESCRIPTION("DesignWare USB3 EXYNOS Glue Layer");