phy-samsung-usb2.c 6.2 KB

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