dwc3-of-simple.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * dwc3-of-simple.c - OF glue layer for simple integrations
  4. *
  5. * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Author: Felipe Balbi <balbi@ti.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. * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov
  19. * <iivanov@mm-sol.com> and the original patch adding support for Xilinx' SoC
  20. * by Subbaraya Sundeep Bhatta <subbaraya.sundeep.bhatta@xilinx.com>
  21. */
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/clk.h>
  28. #include <linux/of.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/pm_runtime.h>
  31. #include <linux/reset.h>
  32. struct dwc3_of_simple {
  33. struct device *dev;
  34. struct clk **clks;
  35. int num_clocks;
  36. struct reset_control *resets;
  37. };
  38. static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
  39. {
  40. struct device *dev = simple->dev;
  41. struct device_node *np = dev->of_node;
  42. int i;
  43. simple->num_clocks = count;
  44. if (!count)
  45. return 0;
  46. simple->clks = devm_kcalloc(dev, simple->num_clocks,
  47. sizeof(struct clk *), GFP_KERNEL);
  48. if (!simple->clks)
  49. return -ENOMEM;
  50. for (i = 0; i < simple->num_clocks; i++) {
  51. struct clk *clk;
  52. int ret;
  53. clk = of_clk_get(np, i);
  54. if (IS_ERR(clk)) {
  55. while (--i >= 0)
  56. clk_put(simple->clks[i]);
  57. return PTR_ERR(clk);
  58. }
  59. ret = clk_prepare_enable(clk);
  60. if (ret < 0) {
  61. while (--i >= 0) {
  62. clk_disable_unprepare(simple->clks[i]);
  63. clk_put(simple->clks[i]);
  64. }
  65. clk_put(clk);
  66. return ret;
  67. }
  68. simple->clks[i] = clk;
  69. }
  70. return 0;
  71. }
  72. static int dwc3_of_simple_probe(struct platform_device *pdev)
  73. {
  74. struct dwc3_of_simple *simple;
  75. struct device *dev = &pdev->dev;
  76. struct device_node *np = dev->of_node;
  77. int ret;
  78. int i;
  79. simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
  80. if (!simple)
  81. return -ENOMEM;
  82. platform_set_drvdata(pdev, simple);
  83. simple->dev = dev;
  84. simple->resets = of_reset_control_array_get_optional_exclusive(np);
  85. if (IS_ERR(simple->resets)) {
  86. ret = PTR_ERR(simple->resets);
  87. dev_err(dev, "failed to get device resets, err=%d\n", ret);
  88. return ret;
  89. }
  90. ret = reset_control_deassert(simple->resets);
  91. if (ret)
  92. goto err_resetc_put;
  93. ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
  94. "clocks", "#clock-cells"));
  95. if (ret)
  96. goto err_resetc_assert;
  97. ret = of_platform_populate(np, NULL, NULL, dev);
  98. if (ret) {
  99. for (i = 0; i < simple->num_clocks; i++) {
  100. clk_disable_unprepare(simple->clks[i]);
  101. clk_put(simple->clks[i]);
  102. }
  103. goto err_resetc_assert;
  104. }
  105. pm_runtime_set_active(dev);
  106. pm_runtime_enable(dev);
  107. pm_runtime_get_sync(dev);
  108. return 0;
  109. err_resetc_assert:
  110. reset_control_assert(simple->resets);
  111. err_resetc_put:
  112. reset_control_put(simple->resets);
  113. return ret;
  114. }
  115. static int dwc3_of_simple_remove(struct platform_device *pdev)
  116. {
  117. struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
  118. struct device *dev = &pdev->dev;
  119. int i;
  120. of_platform_depopulate(dev);
  121. for (i = 0; i < simple->num_clocks; i++) {
  122. clk_disable_unprepare(simple->clks[i]);
  123. clk_put(simple->clks[i]);
  124. }
  125. reset_control_assert(simple->resets);
  126. reset_control_put(simple->resets);
  127. pm_runtime_put_sync(dev);
  128. pm_runtime_disable(dev);
  129. return 0;
  130. }
  131. #ifdef CONFIG_PM
  132. static int dwc3_of_simple_runtime_suspend(struct device *dev)
  133. {
  134. struct dwc3_of_simple *simple = dev_get_drvdata(dev);
  135. int i;
  136. for (i = 0; i < simple->num_clocks; i++)
  137. clk_disable(simple->clks[i]);
  138. return 0;
  139. }
  140. static int dwc3_of_simple_runtime_resume(struct device *dev)
  141. {
  142. struct dwc3_of_simple *simple = dev_get_drvdata(dev);
  143. int ret;
  144. int i;
  145. for (i = 0; i < simple->num_clocks; i++) {
  146. ret = clk_enable(simple->clks[i]);
  147. if (ret < 0) {
  148. while (--i >= 0)
  149. clk_disable(simple->clks[i]);
  150. return ret;
  151. }
  152. }
  153. return 0;
  154. }
  155. #endif
  156. static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
  157. SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
  158. dwc3_of_simple_runtime_resume, NULL)
  159. };
  160. static const struct of_device_id of_dwc3_simple_match[] = {
  161. { .compatible = "qcom,dwc3" },
  162. { .compatible = "rockchip,rk3399-dwc3" },
  163. { .compatible = "xlnx,zynqmp-dwc3" },
  164. { .compatible = "cavium,octeon-7130-usb-uctl" },
  165. { .compatible = "sprd,sc9860-dwc3" },
  166. { /* Sentinel */ }
  167. };
  168. MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
  169. static struct platform_driver dwc3_of_simple_driver = {
  170. .probe = dwc3_of_simple_probe,
  171. .remove = dwc3_of_simple_remove,
  172. .driver = {
  173. .name = "dwc3-of-simple",
  174. .of_match_table = of_dwc3_simple_match,
  175. },
  176. };
  177. module_platform_driver(dwc3_of_simple_driver);
  178. MODULE_LICENSE("GPL v2");
  179. MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
  180. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");