dwc3-of-simple.c 4.1 KB

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