phy-da8xx-usb.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * phy-da8xx-usb - TI DaVinci DA8xx USB PHY driver
  4. *
  5. * Copyright (C) 2016 David Lechner <david@lechnology.com>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/io.h>
  9. #include <linux/of.h>
  10. #include <linux/mfd/da8xx-cfgchip.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/module.h>
  13. #include <linux/phy/phy.h>
  14. #include <linux/platform_data/phy-da8xx-usb.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_runtime.h>
  17. #include <linux/regmap.h>
  18. #define PHY_INIT_BITS (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
  19. struct da8xx_usb_phy {
  20. struct device *dev;
  21. struct phy_provider *phy_provider;
  22. struct phy *usb11_phy;
  23. struct phy *usb20_phy;
  24. struct clk *usb11_clk;
  25. struct clk *usb20_clk;
  26. struct regmap *regmap;
  27. };
  28. static int da8xx_usb11_phy_power_on(struct phy *phy)
  29. {
  30. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  31. int ret;
  32. ret = clk_prepare_enable(d_phy->usb11_clk);
  33. if (ret)
  34. return ret;
  35. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
  36. CFGCHIP2_USB1SUSPENDM);
  37. /*
  38. * USB1.1 can used USB2.0 output clock as reference clock so this is here to prevent USB2.0
  39. * from shutting PHY's power when USB1.1 might use it
  40. */
  41. pm_runtime_get_sync(d_phy->dev);
  42. return 0;
  43. }
  44. static int da8xx_usb11_phy_power_off(struct phy *phy)
  45. {
  46. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  47. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM, 0);
  48. clk_disable_unprepare(d_phy->usb11_clk);
  49. pm_runtime_put_sync(d_phy->dev);
  50. return 0;
  51. }
  52. static const struct phy_ops da8xx_usb11_phy_ops = {
  53. .power_on = da8xx_usb11_phy_power_on,
  54. .power_off = da8xx_usb11_phy_power_off,
  55. .owner = THIS_MODULE,
  56. };
  57. static int da8xx_usb20_phy_power_on(struct phy *phy)
  58. {
  59. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  60. int ret;
  61. ret = clk_prepare_enable(d_phy->usb20_clk);
  62. if (ret)
  63. return ret;
  64. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
  65. return 0;
  66. }
  67. static int da8xx_usb20_phy_power_off(struct phy *phy)
  68. {
  69. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  70. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN,
  71. CFGCHIP2_OTGPWRDN);
  72. clk_disable_unprepare(d_phy->usb20_clk);
  73. return 0;
  74. }
  75. static int da8xx_usb20_phy_set_mode(struct phy *phy,
  76. enum phy_mode mode, int submode)
  77. {
  78. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  79. u32 val;
  80. switch (mode) {
  81. case PHY_MODE_USB_HOST: /* Force VBUS valid, ID = 0 */
  82. val = CFGCHIP2_OTGMODE_FORCE_HOST;
  83. break;
  84. case PHY_MODE_USB_DEVICE: /* Force VBUS valid, ID = 1 */
  85. val = CFGCHIP2_OTGMODE_FORCE_DEVICE;
  86. break;
  87. case PHY_MODE_USB_OTG: /* Don't override the VBUS/ID comparators */
  88. val = CFGCHIP2_OTGMODE_NO_OVERRIDE;
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
  94. val);
  95. return 0;
  96. }
  97. static const struct phy_ops da8xx_usb20_phy_ops = {
  98. .power_on = da8xx_usb20_phy_power_on,
  99. .power_off = da8xx_usb20_phy_power_off,
  100. .set_mode = da8xx_usb20_phy_set_mode,
  101. .owner = THIS_MODULE,
  102. };
  103. static int __maybe_unused da8xx_runtime_suspend(struct device *dev)
  104. {
  105. struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
  106. dev_dbg(dev, "Suspending ...\n");
  107. regmap_set_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN);
  108. return 0;
  109. }
  110. static int __maybe_unused da8xx_runtime_resume(struct device *dev)
  111. {
  112. u32 mask = CFGCHIP2_RESET | CFGCHIP2_PHYPWRDN | CFGCHIP2_OTGPWRDN | CFGCHIP2_PHY_PLLON;
  113. struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
  114. u32 pll_status;
  115. regmap_update_bits(d_phy->regmap, CFGCHIP(2), mask, CFGCHIP2_PHY_PLLON);
  116. dev_dbg(dev, "Resuming ...\n");
  117. return regmap_read_poll_timeout(d_phy->regmap, CFGCHIP(2), pll_status,
  118. pll_status & CFGCHIP2_PHYCLKGD, 1000, 500000);
  119. }
  120. static const struct dev_pm_ops da8xx_usb_phy_pm_ops = {
  121. SET_RUNTIME_PM_OPS(da8xx_runtime_suspend, da8xx_runtime_resume, NULL)
  122. };
  123. static struct phy *da8xx_usb_phy_of_xlate(struct device *dev,
  124. const struct of_phandle_args *args)
  125. {
  126. struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
  127. if (!d_phy)
  128. return ERR_PTR(-ENODEV);
  129. switch (args->args[0]) {
  130. case 0:
  131. return d_phy->usb20_phy;
  132. case 1:
  133. return d_phy->usb11_phy;
  134. default:
  135. return ERR_PTR(-EINVAL);
  136. }
  137. }
  138. static int da8xx_usb_phy_probe(struct platform_device *pdev)
  139. {
  140. struct device *dev = &pdev->dev;
  141. struct da8xx_usb_phy_platform_data *pdata = dev->platform_data;
  142. struct device_node *node = dev->of_node;
  143. struct da8xx_usb_phy *d_phy;
  144. int ret;
  145. d_phy = devm_kzalloc(dev, sizeof(*d_phy), GFP_KERNEL);
  146. if (!d_phy)
  147. return -ENOMEM;
  148. d_phy->dev = dev;
  149. if (pdata)
  150. d_phy->regmap = pdata->cfgchip;
  151. else
  152. d_phy->regmap = syscon_regmap_lookup_by_compatible(
  153. "ti,da830-cfgchip");
  154. if (IS_ERR(d_phy->regmap)) {
  155. dev_err(dev, "Failed to get syscon\n");
  156. return PTR_ERR(d_phy->regmap);
  157. }
  158. d_phy->usb11_clk = devm_clk_get(dev, "usb1_clk48");
  159. if (IS_ERR(d_phy->usb11_clk)) {
  160. dev_err(dev, "Failed to get usb1_clk48\n");
  161. return PTR_ERR(d_phy->usb11_clk);
  162. }
  163. d_phy->usb20_clk = devm_clk_get(dev, "usb0_clk48");
  164. if (IS_ERR(d_phy->usb20_clk)) {
  165. dev_err(dev, "Failed to get usb0_clk48\n");
  166. return PTR_ERR(d_phy->usb20_clk);
  167. }
  168. d_phy->usb11_phy = devm_phy_create(dev, node, &da8xx_usb11_phy_ops);
  169. if (IS_ERR(d_phy->usb11_phy)) {
  170. dev_err(dev, "Failed to create usb11 phy\n");
  171. return PTR_ERR(d_phy->usb11_phy);
  172. }
  173. d_phy->usb20_phy = devm_phy_create(dev, node, &da8xx_usb20_phy_ops);
  174. if (IS_ERR(d_phy->usb20_phy)) {
  175. dev_err(dev, "Failed to create usb20 phy\n");
  176. return PTR_ERR(d_phy->usb20_phy);
  177. }
  178. platform_set_drvdata(pdev, d_phy);
  179. phy_set_drvdata(d_phy->usb11_phy, d_phy);
  180. phy_set_drvdata(d_phy->usb20_phy, d_phy);
  181. if (node) {
  182. d_phy->phy_provider = devm_of_phy_provider_register(dev,
  183. da8xx_usb_phy_of_xlate);
  184. if (IS_ERR(d_phy->phy_provider)) {
  185. dev_err(dev, "Failed to create phy provider\n");
  186. return PTR_ERR(d_phy->phy_provider);
  187. }
  188. } else {
  189. ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy",
  190. "ohci-da8xx");
  191. if (ret)
  192. dev_warn(dev, "Failed to create usb11 phy lookup\n");
  193. ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy",
  194. "musb-da8xx");
  195. if (ret)
  196. dev_warn(dev, "Failed to create usb20 phy lookup\n");
  197. }
  198. regmap_write_bits(d_phy->regmap, CFGCHIP(2),
  199. PHY_INIT_BITS, PHY_INIT_BITS);
  200. pm_runtime_set_active(dev);
  201. ret = devm_pm_runtime_enable(dev);
  202. if (ret)
  203. return ret;
  204. /*
  205. * Prevent runtime pm from being ON by default. Users can enable
  206. * it using power/control in sysfs.
  207. */
  208. pm_runtime_forbid(dev);
  209. return 0;
  210. }
  211. static void da8xx_usb_phy_remove(struct platform_device *pdev)
  212. {
  213. struct da8xx_usb_phy *d_phy = platform_get_drvdata(pdev);
  214. if (!pdev->dev.of_node) {
  215. phy_remove_lookup(d_phy->usb20_phy, "usb-phy", "musb-da8xx");
  216. phy_remove_lookup(d_phy->usb11_phy, "usb-phy", "ohci-da8xx");
  217. }
  218. }
  219. static const struct of_device_id da8xx_usb_phy_ids[] = {
  220. { .compatible = "ti,da830-usb-phy" },
  221. { }
  222. };
  223. MODULE_DEVICE_TABLE(of, da8xx_usb_phy_ids);
  224. static struct platform_driver da8xx_usb_phy_driver = {
  225. .probe = da8xx_usb_phy_probe,
  226. .remove = da8xx_usb_phy_remove,
  227. .driver = {
  228. .name = "da8xx-usb-phy",
  229. .pm = &da8xx_usb_phy_pm_ops,
  230. .of_match_table = da8xx_usb_phy_ids,
  231. },
  232. };
  233. module_platform_driver(da8xx_usb_phy_driver);
  234. MODULE_ALIAS("platform:da8xx-usb-phy");
  235. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  236. MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
  237. MODULE_LICENSE("GPL v2");