phy-lpc18xx-usb-otg.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. return regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
  52. LPC18XX_CREG_CREG0_USB0PHY, 0);
  53. }
  54. static int lpc18xx_usb_otg_phy_power_off(struct phy *phy)
  55. {
  56. struct lpc18xx_usb_otg_phy *lpc = phy_get_drvdata(phy);
  57. int ret;
  58. ret = regmap_update_bits(lpc->reg, LPC18XX_CREG_CREG0,
  59. LPC18XX_CREG_CREG0_USB0PHY,
  60. LPC18XX_CREG_CREG0_USB0PHY);
  61. if (ret)
  62. return ret;
  63. clk_disable(lpc->clk);
  64. return 0;
  65. }
  66. static const struct phy_ops lpc18xx_usb_otg_phy_ops = {
  67. .init = lpc18xx_usb_otg_phy_init,
  68. .exit = lpc18xx_usb_otg_phy_exit,
  69. .power_on = lpc18xx_usb_otg_phy_power_on,
  70. .power_off = lpc18xx_usb_otg_phy_power_off,
  71. .owner = THIS_MODULE,
  72. };
  73. static int lpc18xx_usb_otg_phy_probe(struct platform_device *pdev)
  74. {
  75. struct phy_provider *phy_provider;
  76. struct lpc18xx_usb_otg_phy *lpc;
  77. lpc = devm_kzalloc(&pdev->dev, sizeof(*lpc), GFP_KERNEL);
  78. if (!lpc)
  79. return -ENOMEM;
  80. lpc->reg = syscon_node_to_regmap(pdev->dev.of_node->parent);
  81. if (IS_ERR(lpc->reg)) {
  82. dev_err(&pdev->dev, "failed to get syscon\n");
  83. return PTR_ERR(lpc->reg);
  84. }
  85. lpc->clk = devm_clk_get(&pdev->dev, NULL);
  86. if (IS_ERR(lpc->clk)) {
  87. dev_err(&pdev->dev, "failed to get clock\n");
  88. return PTR_ERR(lpc->clk);
  89. }
  90. lpc->phy = devm_phy_create(&pdev->dev, NULL, &lpc18xx_usb_otg_phy_ops);
  91. if (IS_ERR(lpc->phy)) {
  92. dev_err(&pdev->dev, "failed to create PHY\n");
  93. return PTR_ERR(lpc->phy);
  94. }
  95. phy_set_drvdata(lpc->phy, lpc);
  96. phy_provider = devm_of_phy_provider_register(&pdev->dev,
  97. of_phy_simple_xlate);
  98. return PTR_ERR_OR_ZERO(phy_provider);
  99. }
  100. static const struct of_device_id lpc18xx_usb_otg_phy_match[] = {
  101. { .compatible = "nxp,lpc1850-usb-otg-phy" },
  102. { }
  103. };
  104. MODULE_DEVICE_TABLE(of, lpc18xx_usb_otg_phy_match);
  105. static struct platform_driver lpc18xx_usb_otg_phy_driver = {
  106. .probe = lpc18xx_usb_otg_phy_probe,
  107. .driver = {
  108. .name = "lpc18xx-usb-otg-phy",
  109. .of_match_table = lpc18xx_usb_otg_phy_match,
  110. },
  111. };
  112. module_platform_driver(lpc18xx_usb_otg_phy_driver);
  113. MODULE_AUTHOR("Joachim Eastwood <manabian@gmail.com>");
  114. MODULE_DESCRIPTION("NXP LPC18xx/43xx USB OTG PHY driver");
  115. MODULE_LICENSE("GPL v2");