dwc3-of-simple.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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/clk-provider.h>
  28. #include <linux/of.h>
  29. #include <linux/of_platform.h>
  30. #include <linux/pm_runtime.h>
  31. struct dwc3_of_simple {
  32. struct device *dev;
  33. struct clk **clks;
  34. int num_clocks;
  35. };
  36. static int dwc3_of_simple_clk_init(struct dwc3_of_simple *simple, int count)
  37. {
  38. struct device *dev = simple->dev;
  39. struct device_node *np = dev->of_node;
  40. int i;
  41. simple->num_clocks = count;
  42. if (!count)
  43. return 0;
  44. simple->clks = devm_kcalloc(dev, simple->num_clocks,
  45. sizeof(struct clk *), GFP_KERNEL);
  46. if (!simple->clks)
  47. return -ENOMEM;
  48. for (i = 0; i < simple->num_clocks; i++) {
  49. struct clk *clk;
  50. int ret;
  51. clk = of_clk_get(np, i);
  52. if (IS_ERR(clk)) {
  53. while (--i >= 0) {
  54. clk_disable_unprepare(simple->clks[i]);
  55. clk_put(simple->clks[i]);
  56. }
  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. ret = dwc3_of_simple_clk_init(simple, of_clk_get_parent_count(np));
  85. if (ret)
  86. return ret;
  87. ret = of_platform_populate(np, NULL, NULL, dev);
  88. if (ret) {
  89. for (i = 0; i < simple->num_clocks; i++) {
  90. clk_disable_unprepare(simple->clks[i]);
  91. clk_put(simple->clks[i]);
  92. }
  93. return ret;
  94. }
  95. pm_runtime_set_active(dev);
  96. pm_runtime_enable(dev);
  97. pm_runtime_get_sync(dev);
  98. return 0;
  99. }
  100. static int dwc3_of_simple_remove(struct platform_device *pdev)
  101. {
  102. struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
  103. struct device *dev = &pdev->dev;
  104. int i;
  105. for (i = 0; i < simple->num_clocks; i++) {
  106. clk_disable_unprepare(simple->clks[i]);
  107. clk_put(simple->clks[i]);
  108. }
  109. of_platform_depopulate(dev);
  110. pm_runtime_put_sync(dev);
  111. pm_runtime_disable(dev);
  112. return 0;
  113. }
  114. #ifdef CONFIG_PM
  115. static int dwc3_of_simple_runtime_suspend(struct device *dev)
  116. {
  117. struct dwc3_of_simple *simple = dev_get_drvdata(dev);
  118. int i;
  119. for (i = 0; i < simple->num_clocks; i++)
  120. clk_disable(simple->clks[i]);
  121. return 0;
  122. }
  123. static int dwc3_of_simple_runtime_resume(struct device *dev)
  124. {
  125. struct dwc3_of_simple *simple = dev_get_drvdata(dev);
  126. int ret;
  127. int i;
  128. for (i = 0; i < simple->num_clocks; i++) {
  129. ret = clk_enable(simple->clks[i]);
  130. if (ret < 0) {
  131. while (--i >= 0)
  132. clk_disable(simple->clks[i]);
  133. return ret;
  134. }
  135. }
  136. return 0;
  137. }
  138. #endif
  139. static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
  140. SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
  141. dwc3_of_simple_runtime_resume, NULL)
  142. };
  143. static const struct of_device_id of_dwc3_simple_match[] = {
  144. { .compatible = "qcom,dwc3" },
  145. { .compatible = "rockchip,rk3399-dwc3" },
  146. { .compatible = "xlnx,zynqmp-dwc3" },
  147. { .compatible = "cavium,octeon-7130-usb-uctl" },
  148. { /* Sentinel */ }
  149. };
  150. MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
  151. static struct platform_driver dwc3_of_simple_driver = {
  152. .probe = dwc3_of_simple_probe,
  153. .remove = dwc3_of_simple_remove,
  154. .driver = {
  155. .name = "dwc3-of-simple",
  156. .of_match_table = of_dwc3_simple_match,
  157. },
  158. };
  159. module_platform_driver(dwc3_of_simple_driver);
  160. MODULE_LICENSE("GPL v2");
  161. MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
  162. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");