phy-stih41x-usb.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2014 STMicroelectronics
  3. *
  4. * STMicroelectronics PHY driver for STiH41x USB.
  5. *
  6. * Author: Maxime Coquelin <maxime.coquelin@st.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2, as
  10. * published by the Free Software Foundation.
  11. *
  12. */
  13. #include <linux/platform_device.h>
  14. #include <linux/io.h>
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/clk.h>
  20. #include <linux/phy/phy.h>
  21. #include <linux/regmap.h>
  22. #include <linux/mfd/syscon.h>
  23. #define SYSCFG332 0x80
  24. #define SYSCFG2520 0x820
  25. /**
  26. * struct stih41x_usb_cfg - SoC specific PHY register mapping
  27. * @syscfg: Offset in syscfg registers bank
  28. * @cfg_mask: Bits mask for PHY configuration
  29. * @cfg: Static configuration value for PHY
  30. * @oscok: Notify the PHY oscillator clock is ready
  31. * Setting this bit enable the PHY
  32. */
  33. struct stih41x_usb_cfg {
  34. u32 syscfg;
  35. u32 cfg_mask;
  36. u32 cfg;
  37. u32 oscok;
  38. };
  39. /**
  40. * struct stih41x_usb_phy - Private data for the PHY
  41. * @dev: device for this controller
  42. * @regmap: Syscfg registers bank in which PHY is configured
  43. * @cfg: SoC specific PHY register mapping
  44. * @clk: Oscillator used by the PHY
  45. */
  46. struct stih41x_usb_phy {
  47. struct device *dev;
  48. struct regmap *regmap;
  49. const struct stih41x_usb_cfg *cfg;
  50. struct clk *clk;
  51. };
  52. static struct stih41x_usb_cfg stih415_usb_phy_cfg = {
  53. .syscfg = SYSCFG332,
  54. .cfg_mask = 0x3f,
  55. .cfg = 0x38,
  56. .oscok = BIT(6),
  57. };
  58. static struct stih41x_usb_cfg stih416_usb_phy_cfg = {
  59. .syscfg = SYSCFG2520,
  60. .cfg_mask = 0x33f,
  61. .cfg = 0x238,
  62. .oscok = BIT(6),
  63. };
  64. static int stih41x_usb_phy_init(struct phy *phy)
  65. {
  66. struct stih41x_usb_phy *phy_dev = phy_get_drvdata(phy);
  67. return regmap_update_bits(phy_dev->regmap, phy_dev->cfg->syscfg,
  68. phy_dev->cfg->cfg_mask, phy_dev->cfg->cfg);
  69. }
  70. static int stih41x_usb_phy_power_on(struct phy *phy)
  71. {
  72. struct stih41x_usb_phy *phy_dev = phy_get_drvdata(phy);
  73. int ret;
  74. ret = clk_prepare_enable(phy_dev->clk);
  75. if (ret) {
  76. dev_err(phy_dev->dev, "Failed to enable osc_phy clock\n");
  77. return ret;
  78. }
  79. ret = regmap_update_bits(phy_dev->regmap, phy_dev->cfg->syscfg,
  80. phy_dev->cfg->oscok, phy_dev->cfg->oscok);
  81. if (ret)
  82. clk_disable_unprepare(phy_dev->clk);
  83. return ret;
  84. }
  85. static int stih41x_usb_phy_power_off(struct phy *phy)
  86. {
  87. struct stih41x_usb_phy *phy_dev = phy_get_drvdata(phy);
  88. int ret;
  89. ret = regmap_update_bits(phy_dev->regmap, phy_dev->cfg->syscfg,
  90. phy_dev->cfg->oscok, 0);
  91. if (ret) {
  92. dev_err(phy_dev->dev, "Failed to clear oscok bit\n");
  93. return ret;
  94. }
  95. clk_disable_unprepare(phy_dev->clk);
  96. return 0;
  97. }
  98. static const struct phy_ops stih41x_usb_phy_ops = {
  99. .init = stih41x_usb_phy_init,
  100. .power_on = stih41x_usb_phy_power_on,
  101. .power_off = stih41x_usb_phy_power_off,
  102. .owner = THIS_MODULE,
  103. };
  104. static const struct of_device_id stih41x_usb_phy_of_match[];
  105. static int stih41x_usb_phy_probe(struct platform_device *pdev)
  106. {
  107. struct device_node *np = pdev->dev.of_node;
  108. const struct of_device_id *match;
  109. struct stih41x_usb_phy *phy_dev;
  110. struct device *dev = &pdev->dev;
  111. struct phy_provider *phy_provider;
  112. struct phy *phy;
  113. phy_dev = devm_kzalloc(dev, sizeof(*phy_dev), GFP_KERNEL);
  114. if (!phy_dev)
  115. return -ENOMEM;
  116. match = of_match_device(stih41x_usb_phy_of_match, &pdev->dev);
  117. if (!match)
  118. return -ENODEV;
  119. phy_dev->cfg = match->data;
  120. phy_dev->regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  121. if (IS_ERR(phy_dev->regmap)) {
  122. dev_err(dev, "No syscfg phandle specified\n");
  123. return PTR_ERR(phy_dev->regmap);
  124. }
  125. phy_dev->clk = devm_clk_get(dev, "osc_phy");
  126. if (IS_ERR(phy_dev->clk)) {
  127. dev_err(dev, "osc_phy clk not found\n");
  128. return PTR_ERR(phy_dev->clk);
  129. }
  130. phy = devm_phy_create(dev, NULL, &stih41x_usb_phy_ops);
  131. if (IS_ERR(phy)) {
  132. dev_err(dev, "failed to create phy\n");
  133. return PTR_ERR(phy);
  134. }
  135. phy_dev->dev = dev;
  136. phy_set_drvdata(phy, phy_dev);
  137. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  138. return PTR_ERR_OR_ZERO(phy_provider);
  139. }
  140. static const struct of_device_id stih41x_usb_phy_of_match[] = {
  141. { .compatible = "st,stih415-usb-phy", .data = &stih415_usb_phy_cfg },
  142. { .compatible = "st,stih416-usb-phy", .data = &stih416_usb_phy_cfg },
  143. { /* sentinel */ },
  144. };
  145. MODULE_DEVICE_TABLE(of, stih41x_usb_phy_of_match);
  146. static struct platform_driver stih41x_usb_phy_driver = {
  147. .probe = stih41x_usb_phy_probe,
  148. .driver = {
  149. .name = "stih41x-usb-phy",
  150. .of_match_table = stih41x_usb_phy_of_match,
  151. }
  152. };
  153. module_platform_driver(stih41x_usb_phy_driver);
  154. MODULE_AUTHOR("Maxime Coquelin <maxime.coquelin@st.com>");
  155. MODULE_DESCRIPTION("STMicroelectronics USB PHY driver for STiH41x series");
  156. MODULE_LICENSE("GPL v2");