dwc3-of-simple.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_probe(struct platform_device *pdev)
  37. {
  38. struct dwc3_of_simple *simple;
  39. struct device *dev = &pdev->dev;
  40. struct device_node *np = dev->of_node;
  41. unsigned int count;
  42. int ret;
  43. int i;
  44. simple = devm_kzalloc(dev, sizeof(*simple), GFP_KERNEL);
  45. if (!simple)
  46. return -ENOMEM;
  47. count = of_clk_get_parent_count(np);
  48. if (!count)
  49. return -ENOENT;
  50. simple->num_clocks = count;
  51. simple->clks = devm_kcalloc(dev, simple->num_clocks,
  52. sizeof(struct clk *), GFP_KERNEL);
  53. if (!simple->clks)
  54. return -ENOMEM;
  55. simple->dev = dev;
  56. for (i = 0; i < simple->num_clocks; i++) {
  57. struct clk *clk;
  58. clk = of_clk_get(np, i);
  59. if (IS_ERR(clk)) {
  60. while (--i >= 0)
  61. clk_put(simple->clks[i]);
  62. return PTR_ERR(clk);
  63. }
  64. ret = clk_prepare_enable(clk);
  65. if (ret < 0) {
  66. while (--i >= 0) {
  67. clk_disable_unprepare(simple->clks[i]);
  68. clk_put(simple->clks[i]);
  69. }
  70. clk_put(clk);
  71. return ret;
  72. }
  73. simple->clks[i] = clk;
  74. }
  75. ret = of_platform_populate(np, NULL, NULL, dev);
  76. if (ret) {
  77. for (i = 0; i < simple->num_clocks; i++) {
  78. clk_disable_unprepare(simple->clks[i]);
  79. clk_put(simple->clks[i]);
  80. }
  81. return ret;
  82. }
  83. pm_runtime_set_active(dev);
  84. pm_runtime_enable(dev);
  85. pm_runtime_get_sync(dev);
  86. return 0;
  87. }
  88. static int dwc3_of_simple_remove(struct platform_device *pdev)
  89. {
  90. struct dwc3_of_simple *simple = platform_get_drvdata(pdev);
  91. struct device *dev = &pdev->dev;
  92. int i;
  93. for (i = 0; i < simple->num_clocks; i++) {
  94. clk_unprepare(simple->clks[i]);
  95. clk_put(simple->clks[i]);
  96. }
  97. of_platform_depopulate(dev);
  98. pm_runtime_put_sync(dev);
  99. pm_runtime_disable(dev);
  100. return 0;
  101. }
  102. #ifdef CONFIG_PM
  103. static int dwc3_of_simple_runtime_suspend(struct device *dev)
  104. {
  105. struct dwc3_of_simple *simple = dev_get_drvdata(dev);
  106. int i;
  107. for (i = 0; i < simple->num_clocks; i++)
  108. clk_disable(simple->clks[i]);
  109. return 0;
  110. }
  111. static int dwc3_of_simple_runtime_resume(struct device *dev)
  112. {
  113. struct dwc3_of_simple *simple = dev_get_drvdata(dev);
  114. int ret;
  115. int i;
  116. for (i = 0; i < simple->num_clocks; i++) {
  117. ret = clk_enable(simple->clks[i]);
  118. if (ret < 0) {
  119. while (--i >= 0)
  120. clk_disable(simple->clks[i]);
  121. return ret;
  122. }
  123. }
  124. return 0;
  125. }
  126. #endif
  127. static const struct dev_pm_ops dwc3_of_simple_dev_pm_ops = {
  128. SET_RUNTIME_PM_OPS(dwc3_of_simple_runtime_suspend,
  129. dwc3_of_simple_runtime_resume, NULL)
  130. };
  131. static const struct of_device_id of_dwc3_simple_match[] = {
  132. { .compatible = "qcom,dwc3" },
  133. { .compatible = "xlnx,zynqmp-dwc3" },
  134. { /* Sentinel */ }
  135. };
  136. MODULE_DEVICE_TABLE(of, of_dwc3_simple_match);
  137. static struct platform_driver dwc3_of_simple_driver = {
  138. .probe = dwc3_of_simple_probe,
  139. .remove = dwc3_of_simple_remove,
  140. .driver = {
  141. .name = "dwc3-of-simple",
  142. .of_match_table = of_dwc3_simple_match,
  143. },
  144. };
  145. module_platform_driver(dwc3_of_simple_driver);
  146. MODULE_LICENSE("GPL v2");
  147. MODULE_DESCRIPTION("DesignWare USB3 OF Simple Glue Layer");
  148. MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");