phy-samsung-usb2.c 6.3 KB

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