phy-lpc18xx-usb-otg.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * PHY driver for NXP LPC18xx/43xx internal USB OTG PHY
  3. *
  4. * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.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 version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/err.h>
  13. #include <linux/mfd/syscon.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. /* USB OTG PHY register offset and bit in CREG */
  20. #define LPC18XX_CREG_CREG0 0x004
  21. #define LPC18XX_CREG_CREG0_USB0PHY BIT(5)
  22. struct lpc18xx_usb_otg_phy {
  23. struct phy *phy;
  24. struct clk *clk;
  25. struct regmap *reg;
  26. };
  27. static int lpc18xx_usb_otg_phy_init(struct phy *phy)
  28. {
  29. struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
  30. int ret;
  31. /* The PHY must be clocked at 480 MHz */
  32. ret = clk_set_rate(lpc->clk, 480000000);
  33. if (ret)
  34. return ret;
  35. return clk_prepare(lpc->clk);
  36. }
  37. static int lpc18xx_usb_otg_phy_exit(struct phy *phy)
  38. {
  39. struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
  40. clk_unprepare(lpc->clk);
  41. return 0;
  42. }
  43. static int lpc18xx_usb_otg_phy_power_on(struct phy *phy)
  44. {
  45. struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
  46. int ret;
  47. ret = clk_enable(lpc->clk);
  48. if (ret)
  49. return ret;
  50. /* The bit in CREG is cleared to enable the PHY */
  51. ret = regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
  52. LPC18XX_CREG_CREG0_USB0PHY, 0);
  53. if (ret) {
  54. clk_disable(lpc->clk);
  55. return ret;
  56. }
  57. return 0;
  58. }
  59. static int lpc18xx_usb_otg_phy_power_off(struct phy *phy)
  60. {
  61. struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
  62. int ret;
  63. ret = regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
  64. LPC18XX_CREG_CREG0_USB0PHY,
  65. LPC18XX_CREG_CREG0_USB0PHY);
  66. if (ret)
  67. return ret;
  68. clk_disable(lpc->clk);
  69. return 0;
  70. }
  71. static const struct phy_ops lpc18xx_usb_otg_phy_ops = {
  72. .init = lpc18xx_usb_otg_phy_init,
  73. .exit = lpc18xx_usb_otg_phy_exit,
  74. .power_on = lpc18xx_usb_otg_phy_power_on,
  75. .power_off = lpc18xx_usb_otg_phy_power_off,
  76. .owner = THIS_MODULE,
  77. };
  78. static int lpc18xx_usb_otg_phy_probe(struct platform_device *pdev)
  79. {
  80. struct phy_provider *phy_provider;
  81. struct lpc18xx_usb_otg_phy *lpc;
  82. lpc = devm_kzalloc(&pdev->dev, sizeof(*lpc), GFP_KERNEL);
  83. if (!lpc)
  84. return -ENOMEM;
  85. lpc->reg = syscon_node_to_regmap(pdev->dev.of_node->parent);
  86. if (IS_ERR(lpc->reg)) {
  87. dev_err(&pdev->dev, "failed to get syscon\n");
  88. return PTR_ERR(lpc->reg);
  89. }
  90. lpc->clk = devm_clk_get(&pdev->dev, NULL);
  91. if (IS_ERR(lpc->clk)) {
  92. dev_err(&pdev->dev, "failed to get clock\n");
  93. return PTR_ERR(lpc->clk);
  94. }
  95. lpc->phy = devm_phy_create(&pdev->dev, NULL, &lpc18xx_usb_otg_phy_ops);
  96. if (IS_ERR(lpc->phy)) {
  97. dev_err(&pdev->dev, "failed to create PHY\n");
  98. return PTR_ERR(lpc->phy);
  99. }
  100. phy_set_drvdata(lpc->phy, lpc);
  101. phy_provider = devm_of_phy_provider_register(&pdev->dev,
  102. of_phy_simple_xlate);
  103. return PTR_ERR_OR_ZERO(phy_provider);
  104. }
  105. static const struct of_device_id lpc18xx_usb_otg_phy_match[] = {
  106. { .compatible = "nxp,lpc1850-usb-otg-phy" },
  107. { }
  108. };
  109. MODULE_DEVICE_TABLE(of, lpc18xx_usb_otg_phy_match);
  110. static struct platform_driver lpc18xx_usb_otg_phy_driver = {
  111. .probe = lpc18xx_usb_otg_phy_probe,
  112. .driver = {
  113. .name = "lpc18xx-usb-otg-phy",
  114. .of_match_table = lpc18xx_usb_otg_phy_match,
  115. },
  116. };
  117. module_platform_driver(lpc18xx_usb_otg_phy_driver);
  118. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  119. MODULE_DESCRIPTION("NXP LPC18xx/43xx USB OTG PHY driver");
  120. MODULE_LICENSE("GPL v2");