phy-da8xx-usb.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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/regmap.h>
  17. #define PHY_INIT_BITS (CFGCHIP2_SESENDEN | CFGCHIP2_VBDTCTEN)
  18. struct da8xx_usb_phy {
  19. struct phy_provider *phy_provider;
  20. struct phy *usb11_phy;
  21. struct phy *usb20_phy;
  22. struct clk *usb11_clk;
  23. struct clk *usb20_clk;
  24. struct regmap *regmap;
  25. };
  26. static int da8xx_usb11_phy_power_on(struct phy *phy)
  27. {
  28. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  29. int ret;
  30. ret = clk_prepare_enable(d_phy->usb11_clk);
  31. if (ret)
  32. return ret;
  33. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
  34. CFGCHIP2_USB1SUSPENDM);
  35. return 0;
  36. }
  37. static int da8xx_usb11_phy_power_off(struct phy *phy)
  38. {
  39. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  40. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM, 0);
  41. clk_disable_unprepare(d_phy->usb11_clk);
  42. return 0;
  43. }
  44. static const struct phy_ops da8xx_usb11_phy_ops = {
  45. .power_on = da8xx_usb11_phy_power_on,
  46. .power_off = da8xx_usb11_phy_power_off,
  47. .owner = THIS_MODULE,
  48. };
  49. static int da8xx_usb20_phy_power_on(struct phy *phy)
  50. {
  51. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  52. int ret;
  53. ret = clk_prepare_enable(d_phy->usb20_clk);
  54. if (ret)
  55. return ret;
  56. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
  57. return 0;
  58. }
  59. static int da8xx_usb20_phy_power_off(struct phy *phy)
  60. {
  61. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  62. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN,
  63. CFGCHIP2_OTGPWRDN);
  64. clk_disable_unprepare(d_phy->usb20_clk);
  65. return 0;
  66. }
  67. static int da8xx_usb20_phy_set_mode(struct phy *phy,
  68. enum phy_mode mode, int submode)
  69. {
  70. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  71. u32 val;
  72. switch (mode) {
  73. case PHY_MODE_USB_HOST: /* Force VBUS valid, ID = 0 */
  74. val = CFGCHIP2_OTGMODE_FORCE_HOST;
  75. break;
  76. case PHY_MODE_USB_DEVICE: /* Force VBUS valid, ID = 1 */
  77. val = CFGCHIP2_OTGMODE_FORCE_DEVICE;
  78. break;
  79. case PHY_MODE_USB_OTG: /* Don't override the VBUS/ID comparators */
  80. val = CFGCHIP2_OTGMODE_NO_OVERRIDE;
  81. break;
  82. default:
  83. return -EINVAL;
  84. }
  85. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
  86. val);
  87. return 0;
  88. }
  89. static const struct phy_ops da8xx_usb20_phy_ops = {
  90. .power_on = da8xx_usb20_phy_power_on,
  91. .power_off = da8xx_usb20_phy_power_off,
  92. .set_mode = da8xx_usb20_phy_set_mode,
  93. .owner = THIS_MODULE,
  94. };
  95. static struct phy *da8xx_usb_phy_of_xlate(struct device *dev,
  96. struct of_phandle_args *args)
  97. {
  98. struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
  99. if (!d_phy)
  100. return ERR_PTR(-ENODEV);
  101. switch (args->args[0]) {
  102. case 0:
  103. return d_phy->usb20_phy;
  104. case 1:
  105. return d_phy->usb11_phy;
  106. default:
  107. return ERR_PTR(-EINVAL);
  108. }
  109. }
  110. static int da8xx_usb_phy_probe(struct platform_device *pdev)
  111. {
  112. struct device *dev = &pdev->dev;
  113. struct da8xx_usb_phy_platform_data *pdata = dev->platform_data;
  114. struct device_node *node = dev->of_node;
  115. struct da8xx_usb_phy *d_phy;
  116. d_phy = devm_kzalloc(dev, sizeof(*d_phy), GFP_KERNEL);
  117. if (!d_phy)
  118. return -ENOMEM;
  119. if (pdata)
  120. d_phy->regmap = pdata->cfgchip;
  121. else
  122. d_phy->regmap = syscon_regmap_lookup_by_compatible(
  123. "ti,da830-cfgchip");
  124. if (IS_ERR(d_phy->regmap)) {
  125. dev_err(dev, "Failed to get syscon\n");
  126. return PTR_ERR(d_phy->regmap);
  127. }
  128. d_phy->usb11_clk = devm_clk_get(dev, "usb1_clk48");
  129. if (IS_ERR(d_phy->usb11_clk)) {
  130. dev_err(dev, "Failed to get usb1_clk48\n");
  131. return PTR_ERR(d_phy->usb11_clk);
  132. }
  133. d_phy->usb20_clk = devm_clk_get(dev, "usb0_clk48");
  134. if (IS_ERR(d_phy->usb20_clk)) {
  135. dev_err(dev, "Failed to get usb0_clk48\n");
  136. return PTR_ERR(d_phy->usb20_clk);
  137. }
  138. d_phy->usb11_phy = devm_phy_create(dev, node, &da8xx_usb11_phy_ops);
  139. if (IS_ERR(d_phy->usb11_phy)) {
  140. dev_err(dev, "Failed to create usb11 phy\n");
  141. return PTR_ERR(d_phy->usb11_phy);
  142. }
  143. d_phy->usb20_phy = devm_phy_create(dev, node, &da8xx_usb20_phy_ops);
  144. if (IS_ERR(d_phy->usb20_phy)) {
  145. dev_err(dev, "Failed to create usb20 phy\n");
  146. return PTR_ERR(d_phy->usb20_phy);
  147. }
  148. platform_set_drvdata(pdev, d_phy);
  149. phy_set_drvdata(d_phy->usb11_phy, d_phy);
  150. phy_set_drvdata(d_phy->usb20_phy, d_phy);
  151. if (node) {
  152. d_phy->phy_provider = devm_of_phy_provider_register(dev,
  153. da8xx_usb_phy_of_xlate);
  154. if (IS_ERR(d_phy->phy_provider)) {
  155. dev_err(dev, "Failed to create phy provider\n");
  156. return PTR_ERR(d_phy->phy_provider);
  157. }
  158. } else {
  159. int ret;
  160. ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy",
  161. "ohci-da8xx");
  162. if (ret)
  163. dev_warn(dev, "Failed to create usb11 phy lookup\n");
  164. ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy",
  165. "musb-da8xx");
  166. if (ret)
  167. dev_warn(dev, "Failed to create usb20 phy lookup\n");
  168. }
  169. regmap_write_bits(d_phy->regmap, CFGCHIP(2),
  170. PHY_INIT_BITS, PHY_INIT_BITS);
  171. return 0;
  172. }
  173. static int da8xx_usb_phy_remove(struct platform_device *pdev)
  174. {
  175. struct da8xx_usb_phy *d_phy = platform_get_drvdata(pdev);
  176. if (!pdev->dev.of_node) {
  177. phy_remove_lookup(d_phy->usb20_phy, "usb-phy", "musb-da8xx");
  178. phy_remove_lookup(d_phy->usb11_phy, "usb-phy", "ohci-da8xx");
  179. }
  180. return 0;
  181. }
  182. static const struct of_device_id da8xx_usb_phy_ids[] = {
  183. { .compatible = "ti,da830-usb-phy" },
  184. { }
  185. };
  186. MODULE_DEVICE_TABLE(of, da8xx_usb_phy_ids);
  187. static struct platform_driver da8xx_usb_phy_driver = {
  188. .probe = da8xx_usb_phy_probe,
  189. .remove = da8xx_usb_phy_remove,
  190. .driver = {
  191. .name = "da8xx-usb-phy",
  192. .of_match_table = da8xx_usb_phy_ids,
  193. },
  194. };
  195. module_platform_driver(da8xx_usb_phy_driver);
  196. MODULE_ALIAS("platform:da8xx-usb-phy");
  197. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  198. MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
  199. MODULE_LICENSE("GPL v2");