cdns-dphy-rx.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2022 Texas Instruments Incorporated - https://www.ti.com/
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bitops.h>
  7. #include <linux/io.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/module.h>
  10. #include <linux/phy/phy.h>
  11. #include <linux/phy/phy-mipi-dphy.h>
  12. #include <linux/platform_device.h>
  13. #define DPHY_PMA_CMN(reg) (reg)
  14. #define DPHY_PCS(reg) (0xb00 + (reg))
  15. #define DPHY_ISO(reg) (0xc00 + (reg))
  16. #define DPHY_CMN_SSM DPHY_PMA_CMN(0x20)
  17. #define DPHY_CMN_RX_MODE_EN BIT(10)
  18. #define DPHY_CMN_RX_BANDGAP_TIMER_MASK GENMASK(8, 1)
  19. #define DPHY_CMN_SSM_EN BIT(0)
  20. #define DPHY_CMN_RX_BANDGAP_TIMER 0x14
  21. #define DPHY_BAND_CFG DPHY_PCS(0x0)
  22. #define DPHY_BAND_CFG_RIGHT_BAND GENMASK(9, 5)
  23. #define DPHY_BAND_CFG_LEFT_BAND GENMASK(4, 0)
  24. #define DPHY_POWER_ISLAND_EN_DATA DPHY_PCS(0x8)
  25. #define DPHY_POWER_ISLAND_EN_DATA_VAL 0xaaaaaaaa
  26. #define DPHY_POWER_ISLAND_EN_CLK DPHY_PCS(0xc)
  27. #define DPHY_POWER_ISLAND_EN_CLK_VAL 0xaa
  28. #define DPHY_ISO_CL_CTRL_L DPHY_ISO(0x10)
  29. #define DPHY_ISO_DL_CTRL_L0 DPHY_ISO(0x14)
  30. #define DPHY_ISO_DL_CTRL_L1 DPHY_ISO(0x20)
  31. #define DPHY_ISO_DL_CTRL_L2 DPHY_ISO(0x30)
  32. #define DPHY_ISO_DL_CTRL_L3 DPHY_ISO(0x3c)
  33. #define DPHY_ISO_LANE_READY_BIT 0
  34. #define DPHY_ISO_LANE_READY_TIMEOUT_MS 100UL
  35. #define DPHY_LANES_MIN 1
  36. #define DPHY_LANES_MAX 4
  37. struct cdns_dphy_rx {
  38. void __iomem *regs;
  39. struct device *dev;
  40. struct phy *phy;
  41. };
  42. struct cdns_dphy_rx_band {
  43. /* Rates are in Mbps. */
  44. unsigned int min_rate;
  45. unsigned int max_rate;
  46. };
  47. /* Order of bands is important since the index is the band number. */
  48. static const struct cdns_dphy_rx_band bands[] = {
  49. { 80, 100 }, { 100, 120 }, { 120, 160 }, { 160, 200 }, { 200, 240 },
  50. { 240, 280 }, { 280, 320 }, { 320, 360 }, { 360, 400 }, { 400, 480 },
  51. { 480, 560 }, { 560, 640 }, { 640, 720 }, { 720, 800 }, { 800, 880 },
  52. { 880, 1040 }, { 1040, 1200 }, { 1200, 1350 }, { 1350, 1500 },
  53. { 1500, 1750 }, { 1750, 2000 }, { 2000, 2250 }, { 2250, 2500 }
  54. };
  55. static int cdns_dphy_rx_power_on(struct phy *phy)
  56. {
  57. struct cdns_dphy_rx *dphy = phy_get_drvdata(phy);
  58. /* Start RX state machine. */
  59. writel(DPHY_CMN_SSM_EN | DPHY_CMN_RX_MODE_EN |
  60. FIELD_PREP(DPHY_CMN_RX_BANDGAP_TIMER_MASK,
  61. DPHY_CMN_RX_BANDGAP_TIMER),
  62. dphy->regs + DPHY_CMN_SSM);
  63. return 0;
  64. }
  65. static int cdns_dphy_rx_power_off(struct phy *phy)
  66. {
  67. struct cdns_dphy_rx *dphy = phy_get_drvdata(phy);
  68. writel(0, dphy->regs + DPHY_CMN_SSM);
  69. return 0;
  70. }
  71. static int cdns_dphy_rx_get_band_ctrl(unsigned long hs_clk_rate)
  72. {
  73. unsigned int rate, i;
  74. rate = hs_clk_rate / 1000000UL;
  75. /* Since CSI-2 clock is DDR, the bit rate is twice the clock rate. */
  76. rate *= 2;
  77. if (rate < bands[0].min_rate)
  78. return -EOPNOTSUPP;
  79. for (i = 0; i < ARRAY_SIZE(bands); i++)
  80. if (rate < bands[i].max_rate)
  81. return i;
  82. return -EOPNOTSUPP;
  83. }
  84. static inline int cdns_dphy_rx_wait_for_bit(void __iomem *addr,
  85. unsigned int bit)
  86. {
  87. u32 val;
  88. return readl_relaxed_poll_timeout(addr, val, val & BIT(bit), 10,
  89. DPHY_ISO_LANE_READY_TIMEOUT_MS * 1000);
  90. }
  91. static int cdns_dphy_rx_wait_lane_ready(struct cdns_dphy_rx *dphy,
  92. unsigned int lanes)
  93. {
  94. static const u32 data_lane_ctrl[] = {DPHY_ISO_DL_CTRL_L0,
  95. DPHY_ISO_DL_CTRL_L1,
  96. DPHY_ISO_DL_CTRL_L2,
  97. DPHY_ISO_DL_CTRL_L3};
  98. void __iomem *reg = dphy->regs;
  99. unsigned int i;
  100. int ret;
  101. /* Clock lane */
  102. ret = cdns_dphy_rx_wait_for_bit(reg + DPHY_ISO_CL_CTRL_L,
  103. DPHY_ISO_LANE_READY_BIT);
  104. if (ret)
  105. return ret;
  106. for (i = 0; i < lanes; i++) {
  107. ret = cdns_dphy_rx_wait_for_bit(reg + data_lane_ctrl[i],
  108. DPHY_ISO_LANE_READY_BIT);
  109. if (ret)
  110. return ret;
  111. }
  112. return 0;
  113. }
  114. static int cdns_dphy_rx_configure(struct phy *phy,
  115. union phy_configure_opts *opts)
  116. {
  117. struct cdns_dphy_rx *dphy = phy_get_drvdata(phy);
  118. unsigned int reg, lanes = opts->mipi_dphy.lanes;
  119. int band_ctrl, ret;
  120. /* Data lanes. Minimum one lane is mandatory. */
  121. if (lanes < DPHY_LANES_MIN || lanes > DPHY_LANES_MAX)
  122. return -EINVAL;
  123. band_ctrl = cdns_dphy_rx_get_band_ctrl(opts->mipi_dphy.hs_clk_rate);
  124. if (band_ctrl < 0)
  125. return band_ctrl;
  126. reg = FIELD_PREP(DPHY_BAND_CFG_LEFT_BAND, band_ctrl) |
  127. FIELD_PREP(DPHY_BAND_CFG_RIGHT_BAND, band_ctrl);
  128. writel(reg, dphy->regs + DPHY_BAND_CFG);
  129. /*
  130. * Set the required power island phase 2 time. This is mandated by DPHY
  131. * specs.
  132. */
  133. reg = DPHY_POWER_ISLAND_EN_DATA_VAL;
  134. writel(reg, dphy->regs + DPHY_POWER_ISLAND_EN_DATA);
  135. reg = DPHY_POWER_ISLAND_EN_CLK_VAL;
  136. writel(reg, dphy->regs + DPHY_POWER_ISLAND_EN_CLK);
  137. ret = cdns_dphy_rx_wait_lane_ready(dphy, lanes);
  138. if (ret) {
  139. dev_err(dphy->dev, "DPHY wait for lane ready timeout\n");
  140. return ret;
  141. }
  142. return 0;
  143. }
  144. static int cdns_dphy_rx_validate(struct phy *phy, enum phy_mode mode,
  145. int submode, union phy_configure_opts *opts)
  146. {
  147. int ret;
  148. if (mode != PHY_MODE_MIPI_DPHY)
  149. return -EINVAL;
  150. ret = cdns_dphy_rx_get_band_ctrl(opts->mipi_dphy.hs_clk_rate);
  151. if (ret < 0)
  152. return ret;
  153. return phy_mipi_dphy_config_validate(&opts->mipi_dphy);
  154. }
  155. static const struct phy_ops cdns_dphy_rx_ops = {
  156. .power_on = cdns_dphy_rx_power_on,
  157. .power_off = cdns_dphy_rx_power_off,
  158. .configure = cdns_dphy_rx_configure,
  159. .validate = cdns_dphy_rx_validate,
  160. };
  161. static int cdns_dphy_rx_probe(struct platform_device *pdev)
  162. {
  163. struct device *dev = &pdev->dev;
  164. struct phy_provider *provider;
  165. struct cdns_dphy_rx *dphy;
  166. dphy = devm_kzalloc(dev, sizeof(*dphy), GFP_KERNEL);
  167. if (!dphy)
  168. return -ENOMEM;
  169. dev_set_drvdata(dev, dphy);
  170. dphy->dev = dev;
  171. dphy->regs = devm_platform_ioremap_resource(pdev, 0);
  172. if (IS_ERR(dphy->regs))
  173. return PTR_ERR(dphy->regs);
  174. dphy->phy = devm_phy_create(dev, NULL, &cdns_dphy_rx_ops);
  175. if (IS_ERR(dphy->phy)) {
  176. dev_err(dev, "Failed to create PHY: %ld\n", PTR_ERR(dphy->phy));
  177. return PTR_ERR(dphy->phy);
  178. }
  179. phy_set_drvdata(dphy->phy, dphy);
  180. provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  181. if (IS_ERR(provider)) {
  182. dev_err(dev, "Failed to register PHY provider: %ld\n",
  183. PTR_ERR(provider));
  184. return PTR_ERR(provider);
  185. }
  186. return 0;
  187. }
  188. static const struct of_device_id cdns_dphy_rx_of_match[] = {
  189. { .compatible = "cdns,dphy-rx" },
  190. { /* sentinel */ },
  191. };
  192. MODULE_DEVICE_TABLE(of, cdns_dphy_rx_of_match);
  193. static struct platform_driver cdns_dphy_rx_platform_driver = {
  194. .probe = cdns_dphy_rx_probe,
  195. .driver = {
  196. .name = "cdns-mipi-dphy-rx",
  197. .of_match_table = cdns_dphy_rx_of_match,
  198. },
  199. };
  200. module_platform_driver(cdns_dphy_rx_platform_driver);
  201. MODULE_AUTHOR("Pratyush Yadav <[email protected]>");
  202. MODULE_DESCRIPTION("Cadence D-PHY Rx Driver");
  203. MODULE_LICENSE("GPL");