dwc3-of-simple.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * dwc3-of-simple.c - OF glue layer for simple integrations
  3. *
  4. * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Author: Felipe Balbi <balbi@ti.com>
  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 of
  10. * the License as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * This is a combination of the old dwc3-qcom.c by Ivan T. Ivanov
  18. * <iivanov@mm-sol.com> and the original patch adding support for Xilinx' SoC
  19. * by Subbaraya Sundeep Bhatta <subbaraya.sundeep.bhatta@xilinx.com>
  20. */
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/slab.h>
  24. #include <linux/platform_device.h>
  25. #include <linux/dma-mapping.h>
  26. #include <linux/clk.h>
  27. #include <linux/of.h>
  28. #include <linux/of_platform.h>
  29. #include <linux/pm_runtime.h>
  30. struct dwc3_of_simple {
  31. struct device *dev;
  32. struct clk **clks;
  33. int num_clocks;
  34. };
  35. static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
  36. {
  37. struct device *dev = simple->dev;
  38. struct device_node *np = dev->of_node;
  39. int i;
  40. simple->num_clocks = count;
  41. if (!count)
  42. return 0;
  43. simple->clks = devm_kcalloc(dev, simple->num_clocks,
  44. sizeof(struct clk *), GFP_KERNEL);
  45. if (!simple->clks)
  46. return -ENOMEM;
  47. for (i = 0; i < simple->num_clocks; i++) {
  48. struct clk *clk;
  49. int ret;
  50. clk = of_clk_get(np, i);
  51. if (IS_ERR(clk)) {
  52. while (--i >= 0)
  53. clk_put(simple->clks[i]);
  54. return PTR_ERR(clk);
  55. }
  56. ret = clk_prepare_enable(clk);
  57. if (ret < 0) {
  58. while (--i >= 0) {
  59. clk_disable_unprepare(simple->clks[i]);
  60. clk_put(simple->clks[i]);
  61. }
  62. clk_put(clk);
  63. return ret;
  64. }
  65. simple->clks[i] = clk;
  66. }
  67. return 0;
  68. }
  69. static int dwc3_of_simple_probe(struct platform_device *pdev)
  70. {
  71. struct dwc3_of_simple *simple;
  72. struct device *dev = &pdev->dev;
  73. struct device_node *np = dev->of_node;
  74. int ret;
  75. int i;
  76. simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
  77. if (!simple)
  78. return -ENOMEM;
  79. platform_set_drvdata(pdev, simple);
  80. simple->dev = dev;
  81. ret = dwc3_of_simple_clk_init(simple, of_count_phandle_with_args(np,
  82. "clocks", "#clock-cells"));
  83. if (ret)
  84. return ret;
  85. ret = of_platform_populate(np, NULL, NULL, dev);
  86. if (ret) {
  87. for (i = 0; i < simple->num_clocks; i++) {
  88. clk_disable_unprepare(simple->clks[i]);
  89. clk_put(simple->clks[i]);
  90. }
  91. return ret;
  92. }
  93. pm_runtime_set_active(dev);
  94. pm_runtime_enable(dev);
  95. pm_runtime_get_sync(dev);
  96. return 0;
  97. }
  98. static int dwc3_of_simple_remove(struct platform_device *pdev)
  99. {
  100. struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
  101. struct device *dev = &pdev->dev;
  102. int i;
  103. for (i = 0; i < simple->num_clocks; i++) {
  104. clk_disable_unprepare(simple->clks[i]);
  105. clk_put(simple->clks[i]);
  106. }
  107. of_platform_depopulate(dev);
  108. pm_runtime_put_sync(dev);
  109. pm_runtime_disable(dev);
  110. return 0;
  111. }
  112. #ifdef CONFIG_PM
  113. static int dwc3_of_simple_runtime_suspend(struct device *dev)
  114. {
  115. struct dwc3_of_simple *simple = dev_get_drvdata(dev);
  116. int i;
  117. for (i = 0; i < simple->num_clocks; i++)
  118. clk_disable(simple->clks[i]);
  119. return 0;
  120. }
  121. static int dwc3_of_simple_runtime_resume(struct device *dev)
  122. {
  123. struct dwc3_of_simple *simple = dev_get_drvdata(dev);
  124. int ret;
  125. int i;
  126. for (i = 0; i < simple->num_clocks; i++) {
  127. ret = clk_enable(simple->clks[i]);
  128. if (ret < 0) {
  129. while (--i >= 0)
  130. clk_disable(simple->clks[i]);
  131. return ret;
  132. }
  133. }
  134. return 0;
  135. }
  136. #endif
  137. static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
  138. SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
  139. dwc3_of_simple_runtime_resume, NULL)
  140. };
  141. static const struct of_device_id of_dwc3_simple_match[] = {
  142. { .compatible = "qcom,dwc3" },
  143. { .compatible = "rockchip,rk3399-dwc3" },
  144. { .compatible = "xlnx,zynqmp-dwc3" },
  145. { .compatible = "cavium,octeon-7130-usb-uctl" },
  146. { .compatible = "sprd,sc9860-dwc3" },
  147. { /* Sentinel */ }
  148. };
  149. MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
  150. static struct platform_driver dwc3_of_simple_driver = {
  151. .probe = dwc3_of_simple_probe,
  152. .remove = dwc3_of_simple_remove,
  153. .driver = {
  154. .name = "dwc3-of-simple",
  155. .of_match_table = of_dwc3_simple_match,
  156. },
  157. };
  158. module_platform_driver(dwc3_of_simple_driver);
  159. MODULE_LICENSE("GPL v2");
  160. MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
  161. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");