dwc3-of-simple.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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_put(simple->clks[i]);
  55. return PTR_ERR(clk);
  56. }
  57. ret = clk_prepare_enable(clk);
  58. if (ret < 0) {
  59. while (--i >= 0) {
  60. clk_disable_unprepare(simple->clks[i]);
  61. clk_put(simple->clks[i]);
  62. }
  63. clk_put(clk);
  64. return ret;
  65. }
  66. simple->clks[i] = clk;
  67. }
  68. return 0;
  69. }
  70. static int dwc3_of_simple_probe(struct platform_device *pdev)
  71. {
  72. struct dwc3_of_simple *simple;
  73. struct device *dev = &pdev->dev;
  74. struct device_node *np = dev->of_node;
  75. int ret;
  76. int i;
  77. simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
  78. if (!simple)
  79. return -ENOMEM;
  80. platform_set_drvdata(pdev, simple);
  81. simple->dev = dev;
  82. ret = dwc3_of_simple_clk_init(simple, of_clk_get_parent_count(np));
  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. { /* Sentinel */ }
  147. };
  148. MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
  149. static struct platform_driver dwc3_of_simple_driver = {
  150. .probe = dwc3_of_simple_probe,
  151. .remove = dwc3_of_simple_remove,
  152. .driver = {
  153. .name = "dwc3-of-simple",
  154. .of_match_table = of_dwc3_simple_match,
  155. },
  156. };
  157. module_platform_driver(dwc3_of_simple_driver);
  158. MODULE_LICENSE("GPL v2");
  159. MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
  160. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");