phy-da8xx-usb.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * phy-da8xx-usb - TI DaVinci DA8xx USB PHY driver
  3. *
  4. * Copyright (C) 2016 David Lechner <david@lechnology.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; version 2 of the License.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/io.h>
  17. #include <linux/of.h>
  18. #include <linux/mfd/da8xx-cfgchip.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/module.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/platform_device.h>
  23. #include <linux/regmap.h>
  24. struct da8xx_usb_phy {
  25. struct phy_provider *phy_provider;
  26. struct phy *usb11_phy;
  27. struct phy *usb20_phy;
  28. struct clk *usb11_clk;
  29. struct clk *usb20_clk;
  30. struct regmap *regmap;
  31. };
  32. static int da8xx_usb11_phy_power_on(struct phy *phy)
  33. {
  34. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  35. int ret;
  36. ret = clk_prepare_enable(d_phy->usb11_clk);
  37. if (ret)
  38. return ret;
  39. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM,
  40. CFGCHIP2_USB1SUSPENDM);
  41. return 0;
  42. }
  43. static int da8xx_usb11_phy_power_off(struct phy *phy)
  44. {
  45. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  46. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_USB1SUSPENDM, 0);
  47. clk_disable_unprepare(d_phy->usb11_clk);
  48. return 0;
  49. }
  50. static const struct phy_ops da8xx_usb11_phy_ops = {
  51. .power_on = da8xx_usb11_phy_power_on,
  52. .power_off = da8xx_usb11_phy_power_off,
  53. .owner = THIS_MODULE,
  54. };
  55. static int da8xx_usb20_phy_power_on(struct phy *phy)
  56. {
  57. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  58. int ret;
  59. ret = clk_prepare_enable(d_phy->usb20_clk);
  60. if (ret)
  61. return ret;
  62. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN, 0);
  63. return 0;
  64. }
  65. static int da8xx_usb20_phy_power_off(struct phy *phy)
  66. {
  67. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  68. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGPWRDN,
  69. CFGCHIP2_OTGPWRDN);
  70. clk_disable_unprepare(d_phy->usb20_clk);
  71. return 0;
  72. }
  73. static int da8xx_usb20_phy_set_mode(struct phy *phy, enum phy_mode mode)
  74. {
  75. struct da8xx_usb_phy *d_phy = phy_get_drvdata(phy);
  76. u32 val;
  77. switch (mode) {
  78. case PHY_MODE_USB_HOST: /* Force VBUS valid, ID = 0 */
  79. val = CFGCHIP2_OTGMODE_FORCE_HOST;
  80. break;
  81. case PHY_MODE_USB_DEVICE: /* Force VBUS valid, ID = 1 */
  82. val = CFGCHIP2_OTGMODE_FORCE_DEVICE;
  83. break;
  84. case PHY_MODE_USB_OTG: /* Don't override the VBUS/ID comparators */
  85. val = CFGCHIP2_OTGMODE_NO_OVERRIDE;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. regmap_write_bits(d_phy->regmap, CFGCHIP(2), CFGCHIP2_OTGMODE_MASK,
  91. val);
  92. return 0;
  93. }
  94. static const struct phy_ops da8xx_usb20_phy_ops = {
  95. .power_on = da8xx_usb20_phy_power_on,
  96. .power_off = da8xx_usb20_phy_power_off,
  97. .set_mode = da8xx_usb20_phy_set_mode,
  98. .owner = THIS_MODULE,
  99. };
  100. static struct phy *da8xx_usb_phy_of_xlate(struct device *dev,
  101. struct of_phandle_args *args)
  102. {
  103. struct da8xx_usb_phy *d_phy = dev_get_drvdata(dev);
  104. if (!d_phy)
  105. return ERR_PTR(-ENODEV);
  106. switch (args->args[0]) {
  107. case 0:
  108. return d_phy->usb20_phy;
  109. case 1:
  110. return d_phy->usb11_phy;
  111. default:
  112. return ERR_PTR(-EINVAL);
  113. }
  114. }
  115. static int da8xx_usb_phy_probe(struct platform_device *pdev)
  116. {
  117. struct device *dev = &pdev->dev;
  118. struct device_node *node = dev->of_node;
  119. struct da8xx_usb_phy *d_phy;
  120. d_phy = devm_kzalloc(dev, sizeof(*d_phy), GFP_KERNEL);
  121. if (!d_phy)
  122. return -ENOMEM;
  123. if (node)
  124. d_phy->regmap = syscon_regmap_lookup_by_compatible(
  125. "ti,da830-cfgchip");
  126. else
  127. d_phy->regmap = syscon_regmap_lookup_by_pdevname("syscon");
  128. if (IS_ERR(d_phy->regmap)) {
  129. dev_err(dev, "Failed to get syscon\n");
  130. return PTR_ERR(d_phy->regmap);
  131. }
  132. d_phy->usb11_clk = devm_clk_get(dev, "usb11_phy");
  133. if (IS_ERR(d_phy->usb11_clk)) {
  134. dev_err(dev, "Failed to get usb11_phy clock\n");
  135. return PTR_ERR(d_phy->usb11_clk);
  136. }
  137. d_phy->usb20_clk = devm_clk_get(dev, "usb20_phy");
  138. if (IS_ERR(d_phy->usb20_clk)) {
  139. dev_err(dev, "Failed to get usb20_phy clock\n");
  140. return PTR_ERR(d_phy->usb20_clk);
  141. }
  142. d_phy->usb11_phy = devm_phy_create(dev, node, &da8xx_usb11_phy_ops);
  143. if (IS_ERR(d_phy->usb11_phy)) {
  144. dev_err(dev, "Failed to create usb11 phy\n");
  145. return PTR_ERR(d_phy->usb11_phy);
  146. }
  147. d_phy->usb20_phy = devm_phy_create(dev, node, &da8xx_usb20_phy_ops);
  148. if (IS_ERR(d_phy->usb20_phy)) {
  149. dev_err(dev, "Failed to create usb20 phy\n");
  150. return PTR_ERR(d_phy->usb20_phy);
  151. }
  152. platform_set_drvdata(pdev, d_phy);
  153. phy_set_drvdata(d_phy->usb11_phy, d_phy);
  154. phy_set_drvdata(d_phy->usb20_phy, d_phy);
  155. if (node) {
  156. d_phy->phy_provider = devm_of_phy_provider_register(dev,
  157. da8xx_usb_phy_of_xlate);
  158. if (IS_ERR(d_phy->phy_provider)) {
  159. dev_err(dev, "Failed to create phy provider\n");
  160. return PTR_ERR(d_phy->phy_provider);
  161. }
  162. } else {
  163. int ret;
  164. ret = phy_create_lookup(d_phy->usb11_phy, "usb-phy",
  165. "ohci-da8xx");
  166. if (ret)
  167. dev_warn(dev, "Failed to create usb11 phy lookup\n");
  168. ret = phy_create_lookup(d_phy->usb20_phy, "usb-phy",
  169. "musb-da8xx");
  170. if (ret)
  171. dev_warn(dev, "Failed to create usb20 phy lookup\n");
  172. }
  173. return 0;
  174. }
  175. static int da8xx_usb_phy_remove(struct platform_device *pdev)
  176. {
  177. struct da8xx_usb_phy *d_phy = platform_get_drvdata(pdev);
  178. if (!pdev->dev.of_node) {
  179. phy_remove_lookup(d_phy->usb20_phy, "usb-phy", "musb-da8xx");
  180. phy_remove_lookup(d_phy->usb11_phy, "usb-phy", "ohci-da8xx");
  181. }
  182. return 0;
  183. }
  184. static const struct of_device_id da8xx_usb_phy_ids[] = {
  185. { .compatible = "ti,da830-usb-phy" },
  186. { }
  187. };
  188. MODULE_DEVICE_TABLE(of, da8xx_usb_phy_ids);
  189. static struct platform_driver da8xx_usb_phy_driver = {
  190. .probe = da8xx_usb_phy_probe,
  191. .remove = da8xx_usb_phy_remove,
  192. .driver = {
  193. .name = "da8xx-usb-phy",
  194. .of_match_table = da8xx_usb_phy_ids,
  195. },
  196. };
  197. module_platform_driver(da8xx_usb_phy_driver);
  198. MODULE_ALIAS("platform:da8xx-usb-phy");
  199. MODULE_AUTHOR("David Lechner <david@lechnology.com>");
  200. MODULE_DESCRIPTION("TI DA8xx USB PHY driver");
  201. MODULE_LICENSE("GPL v2");