phy-samsung-usb2.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Samsung SoC USB 1.1/2.0 PHY driver
  3. *
  4. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  5. * Author: Kamil Debski <k.debski@samsung.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/mfd/syscon.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/spinlock.h>
  19. #include "phy-samsung-usb2.h"
  20. static int samsung_usb2_phy_power_on(struct phy *phy)
  21. {
  22. struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
  23. struct samsung_usb2_phy_driver *drv = inst->drv;
  24. int ret;
  25. dev_dbg(drv->dev, "Request to power_on \"%s\" usb phy\n",
  26. inst->cfg->label);
  27. ret = clk_prepare_enable(drv->clk);
  28. if (ret)
  29. goto err_main_clk;
  30. ret = clk_prepare_enable(drv->ref_clk);
  31. if (ret)
  32. goto err_instance_clk;
  33. if (inst->cfg->power_on) {
  34. spin_lock(&drv->lock);
  35. ret = inst->cfg->power_on(inst);
  36. spin_unlock(&drv->lock);
  37. }
  38. return 0;
  39. err_instance_clk:
  40. clk_disable_unprepare(drv->clk);
  41. err_main_clk:
  42. return ret;
  43. }
  44. static int samsung_usb2_phy_power_off(struct phy *phy)
  45. {
  46. struct samsung_usb2_phy_instance *inst = phy_get_drvdata(phy);
  47. struct samsung_usb2_phy_driver *drv = inst->drv;
  48. int ret = 0;
  49. dev_dbg(drv->dev, "Request to power_off \"%s\" usb phy\n",
  50. inst->cfg->label);
  51. if (inst->cfg->power_off) {
  52. spin_lock(&drv->lock);
  53. ret = inst->cfg->power_off(inst);
  54. spin_unlock(&drv->lock);
  55. }
  56. clk_disable_unprepare(drv->ref_clk);
  57. clk_disable_unprepare(drv->clk);
  58. return ret;
  59. }
  60. static struct phy_ops samsung_usb2_phy_ops = {
  61. .power_on = samsung_usb2_phy_power_on,
  62. .power_off = samsung_usb2_phy_power_off,
  63. .owner = THIS_MODULE,
  64. };
  65. static struct phy *samsung_usb2_phy_xlate(struct device *dev,
  66. struct of_phandle_args *args)
  67. {
  68. struct samsung_usb2_phy_driver *drv;
  69. drv = dev_get_drvdata(dev);
  70. if (!drv)
  71. return ERR_PTR(-EINVAL);
  72. if (WARN_ON(args->args[0] >= drv->cfg->num_phys))
  73. return ERR_PTR(-ENODEV);
  74. return drv->instances[args->args[0]].phy;
  75. }
  76. static const struct of_device_id samsung_usb2_phy_of_match[] = {
  77. #ifdef CONFIG_PHY_EXYNOS4X12_USB2
  78. {
  79. .compatible = "samsung,exynos3250-usb2-phy",
  80. .data = &exynos3250_usb2_phy_config,
  81. },
  82. #endif
  83. #ifdef CONFIG_PHY_EXYNOS4210_USB2
  84. {
  85. .compatible = "samsung,exynos4210-usb2-phy",
  86. .data = &exynos4210_usb2_phy_config,
  87. },
  88. #endif
  89. #ifdef CONFIG_PHY_EXYNOS4X12_USB2
  90. {
  91. .compatible = "samsung,exynos4x12-usb2-phy",
  92. .data = &exynos4x12_usb2_phy_config,
  93. },
  94. #endif
  95. #ifdef CONFIG_PHY_EXYNOS5250_USB2
  96. {
  97. .compatible = "samsung,exynos5250-usb2-phy",
  98. .data = &exynos5250_usb2_phy_config,
  99. },
  100. #endif
  101. #ifdef CONFIG_PHY_S5PV210_USB2
  102. {
  103. .compatible = "samsung,s5pv210-usb2-phy",
  104. .data = &s5pv210_usb2_phy_config,
  105. },
  106. #endif
  107. { },
  108. };
  109. MODULE_DEVICE_TABLE(of, samsung_usb2_phy_of_match);
  110. static int samsung_usb2_phy_probe(struct platform_device *pdev)
  111. {
  112. const struct of_device_id *match;
  113. const struct samsung_usb2_phy_config *cfg;
  114. struct device *dev = &pdev->dev;
  115. struct phy_provider *phy_provider;
  116. struct resource *mem;
  117. struct samsung_usb2_phy_driver *drv;
  118. int i, ret;
  119. if (!pdev->dev.of_node) {
  120. dev_err(dev, "This driver is required to be instantiated from device tree\n");
  121. return -EINVAL;
  122. }
  123. match = of_match_node(samsung_usb2_phy_of_match, pdev->dev.of_node);
  124. if (!match) {
  125. dev_err(dev, "of_match_node() failed\n");
  126. return -EINVAL;
  127. }
  128. cfg = match->data;
  129. drv = devm_kzalloc(dev, sizeof(struct samsung_usb2_phy_driver) +
  130. cfg->num_phys * sizeof(struct samsung_usb2_phy_instance),
  131. GFP_KERNEL);
  132. if (!drv)
  133. return -ENOMEM;
  134. dev_set_drvdata(dev, drv);
  135. spin_lock_init(&drv->lock);
  136. drv->cfg = cfg;
  137. drv->dev = dev;
  138. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  139. drv->reg_phy = devm_ioremap_resource(dev, mem);
  140. if (IS_ERR(drv->reg_phy)) {
  141. dev_err(dev, "Failed to map register memory (phy)\n");
  142. return PTR_ERR(drv->reg_phy);
  143. }
  144. drv->reg_pmu = syscon_regmap_lookup_by_phandle(pdev->dev.of_node,
  145. "samsung,pmureg-phandle");
  146. if (IS_ERR(drv->reg_pmu)) {
  147. dev_err(dev, "Failed to map PMU registers (via syscon)\n");
  148. return PTR_ERR(drv->reg_pmu);
  149. }
  150. if (drv->cfg->has_mode_switch) {
  151. drv->reg_sys = syscon_regmap_lookup_by_phandle(
  152. pdev->dev.of_node, "samsung,sysreg-phandle");
  153. if (IS_ERR(drv->reg_sys)) {
  154. dev_err(dev, "Failed to map system registers (via syscon)\n");
  155. return PTR_ERR(drv->reg_sys);
  156. }
  157. }
  158. drv->clk = devm_clk_get(dev, "phy");
  159. if (IS_ERR(drv->clk)) {
  160. dev_err(dev, "Failed to get clock of phy controller\n");
  161. return PTR_ERR(drv->clk);
  162. }
  163. drv->ref_clk = devm_clk_get(dev, "ref");
  164. if (IS_ERR(drv->ref_clk)) {
  165. dev_err(dev, "Failed to get reference clock for the phy controller\n");
  166. return PTR_ERR(drv->ref_clk);
  167. }
  168. drv->ref_rate = clk_get_rate(drv->ref_clk);
  169. if (drv->cfg->rate_to_clk) {
  170. ret = drv->cfg->rate_to_clk(drv->ref_rate, &drv->ref_reg_val);
  171. if (ret)
  172. return ret;
  173. }
  174. for (i = 0; i < drv->cfg->num_phys; i++) {
  175. char *label = drv->cfg->phys[i].label;
  176. struct samsung_usb2_phy_instance *p = &drv->instances[i];
  177. dev_dbg(dev, "Creating phy \"%s\"\n", label);
  178. p->phy = devm_phy_create(dev, NULL, &samsung_usb2_phy_ops);
  179. if (IS_ERR(p->phy)) {
  180. dev_err(drv->dev, "Failed to create usb2_phy \"%s\"\n",
  181. label);
  182. return PTR_ERR(p->phy);
  183. }
  184. p->cfg = &drv->cfg->phys[i];
  185. p->drv = drv;
  186. phy_set_bus_width(p->phy, 8);
  187. phy_set_drvdata(p->phy, p);
  188. }
  189. phy_provider = devm_of_phy_provider_register(dev,
  190. samsung_usb2_phy_xlate);
  191. if (IS_ERR(phy_provider)) {
  192. dev_err(drv->dev, "Failed to register phy provider\n");
  193. return PTR_ERR(phy_provider);
  194. }
  195. return 0;
  196. }
  197. static struct platform_driver samsung_usb2_phy_driver = {
  198. .probe = samsung_usb2_phy_probe,
  199. .driver = {
  200. .of_match_table = samsung_usb2_phy_of_match,
  201. .name = "samsung-usb2-phy",
  202. }
  203. };
  204. module_platform_driver(samsung_usb2_phy_driver);
  205. MODULE_DESCRIPTION("Samsung S5P/EXYNOS SoC USB PHY driver");
  206. MODULE_AUTHOR("Kamil Debski <k.debski@samsung.com>");
  207. MODULE_LICENSE("GPL v2");
  208. MODULE_ALIAS("platform:samsung-usb2-phy");