phy-samsung-usb2.c 6.5 KB

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