cdns-dphy-rx.c 7.3 KB

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