phy-da8xx-usb.c 6.2 KB

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