phy-da8xx-usb.c 6.3 KB

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