phy-samsung-usb2.c 6.4 KB

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