cdns-dphy.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright: 2017-2018 Cadence Design Systems, Inc.
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bitops.h>
  7. #include <linux/clk.h>
  8. #include <linux/io.h>
  9. #include <linux/iopoll.h>
  10. #include <linux/module.h>
  11. #include <linux/of.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/reset.h>
  14. #include <linux/phy/phy.h>
  15. #include <linux/phy/phy-mipi-dphy.h>
  16. #define REG_WAKEUP_TIME_NS 800
  17. #define DPHY_PLL_RATE_HZ 108000000
  18. #define POLL_TIMEOUT_US 1000
  19. /* DPHY registers */
  20. #define DPHY_PMA_CMN(reg) (reg)
  21. #define DPHY_PMA_LCLK(reg) (0x100 + (reg))
  22. #define DPHY_PMA_LDATA(lane, reg) (0x200 + ((lane) * 0x100) + (reg))
  23. #define DPHY_PMA_RCLK(reg) (0x600 + (reg))
  24. #define DPHY_PMA_RDATA(lane, reg) (0x700 + ((lane) * 0x100) + (reg))
  25. #define DPHY_PCS(reg) (0xb00 + (reg))
  26. #define DPHY_CMN_SSM DPHY_PMA_CMN(0x20)
  27. #define DPHY_CMN_SSM_EN BIT(0)
  28. #define DPHY_CMN_TX_MODE_EN BIT(9)
  29. #define DPHY_CMN_PWM DPHY_PMA_CMN(0x40)
  30. #define DPHY_CMN_PWM_DIV(x) ((x) << 20)
  31. #define DPHY_CMN_PWM_LOW(x) ((x) << 10)
  32. #define DPHY_CMN_PWM_HIGH(x) (x)
  33. #define DPHY_CMN_FBDIV DPHY_PMA_CMN(0x4c)
  34. #define DPHY_CMN_FBDIV_VAL(low, high) (((high) << 11) | ((low) << 22))
  35. #define DPHY_CMN_FBDIV_FROM_REG (BIT(10) | BIT(21))
  36. #define DPHY_CMN_OPIPDIV DPHY_PMA_CMN(0x50)
  37. #define DPHY_CMN_IPDIV_FROM_REG BIT(0)
  38. #define DPHY_CMN_IPDIV(x) ((x) << 1)
  39. #define DPHY_CMN_OPDIV_FROM_REG BIT(6)
  40. #define DPHY_CMN_OPDIV(x) ((x) << 7)
  41. #define DPHY_BAND_CFG DPHY_PCS(0x0)
  42. #define DPHY_BAND_CFG_LEFT_BAND GENMASK(4, 0)
  43. #define DPHY_BAND_CFG_RIGHT_BAND GENMASK(9, 5)
  44. #define DPHY_PSM_CFG DPHY_PCS(0x4)
  45. #define DPHY_PSM_CFG_FROM_REG BIT(0)
  46. #define DPHY_PSM_CLK_DIV(x) ((x) << 1)
  47. #define DSI_HBP_FRAME_OVERHEAD 12
  48. #define DSI_HSA_FRAME_OVERHEAD 14
  49. #define DSI_HFP_FRAME_OVERHEAD 6
  50. #define DSI_HSS_VSS_VSE_FRAME_OVERHEAD 4
  51. #define DSI_BLANKING_FRAME_OVERHEAD 6
  52. #define DSI_NULL_FRAME_OVERHEAD 6
  53. #define DSI_EOT_PKT_SIZE 4
  54. #define DPHY_TX_J721E_WIZ_PLL_CTRL 0xF04
  55. #define DPHY_TX_J721E_WIZ_STATUS 0xF08
  56. #define DPHY_TX_J721E_WIZ_RST_CTRL 0xF0C
  57. #define DPHY_TX_J721E_WIZ_PSM_FREQ 0xF10
  58. #define DPHY_TX_J721E_WIZ_IPDIV GENMASK(4, 0)
  59. #define DPHY_TX_J721E_WIZ_OPDIV GENMASK(13, 8)
  60. #define DPHY_TX_J721E_WIZ_FBDIV GENMASK(25, 16)
  61. #define DPHY_TX_J721E_WIZ_LANE_RSTB BIT(31)
  62. #define DPHY_TX_WIZ_PLL_LOCK BIT(31)
  63. #define DPHY_TX_WIZ_O_CMN_READY BIT(31)
  64. struct cdns_dphy_cfg {
  65. u8 pll_ipdiv;
  66. u8 pll_opdiv;
  67. u16 pll_fbdiv;
  68. unsigned int nlanes;
  69. };
  70. enum cdns_dphy_clk_lane_cfg {
  71. DPHY_CLK_CFG_LEFT_DRIVES_ALL = 0,
  72. DPHY_CLK_CFG_LEFT_DRIVES_RIGHT = 1,
  73. DPHY_CLK_CFG_LEFT_DRIVES_LEFT = 2,
  74. DPHY_CLK_CFG_RIGHT_DRIVES_ALL = 3,
  75. };
  76. struct cdns_dphy;
  77. struct cdns_dphy_ops {
  78. int (*probe)(struct cdns_dphy *dphy);
  79. void (*remove)(struct cdns_dphy *dphy);
  80. void (*set_psm_div)(struct cdns_dphy *dphy, u8 div);
  81. void (*set_clk_lane_cfg)(struct cdns_dphy *dphy,
  82. enum cdns_dphy_clk_lane_cfg cfg);
  83. void (*set_pll_cfg)(struct cdns_dphy *dphy,
  84. const struct cdns_dphy_cfg *cfg);
  85. unsigned long (*get_wakeup_time_ns)(struct cdns_dphy *dphy);
  86. };
  87. struct cdns_dphy {
  88. struct cdns_dphy_cfg cfg;
  89. void __iomem *regs;
  90. struct clk *psm_clk;
  91. struct clk *pll_ref_clk;
  92. const struct cdns_dphy_ops *ops;
  93. struct phy *phy;
  94. };
  95. /* Order of bands is important since the index is the band number. */
  96. static const unsigned int tx_bands[] = {
  97. 80, 100, 120, 160, 200, 240, 320, 390, 450, 510, 560, 640, 690, 770,
  98. 870, 950, 1000, 1200, 1400, 1600, 1800, 2000, 2200, 2500
  99. };
  100. static int cdns_dsi_get_dphy_pll_cfg(struct cdns_dphy *dphy,
  101. struct cdns_dphy_cfg *cfg,
  102. struct phy_configure_opts_mipi_dphy *opts,
  103. unsigned int *dsi_hfp_ext)
  104. {
  105. unsigned long pll_ref_hz = clk_get_rate(dphy->pll_ref_clk);
  106. u64 dlane_bps;
  107. memset(cfg, 0, sizeof(*cfg));
  108. if (pll_ref_hz < 9600000 || pll_ref_hz >= 150000000)
  109. return -EINVAL;
  110. else if (pll_ref_hz < 19200000)
  111. cfg->pll_ipdiv = 1;
  112. else if (pll_ref_hz < 38400000)
  113. cfg->pll_ipdiv = 2;
  114. else if (pll_ref_hz < 76800000)
  115. cfg->pll_ipdiv = 4;
  116. else
  117. cfg->pll_ipdiv = 8;
  118. dlane_bps = opts->hs_clk_rate;
  119. if (dlane_bps > 2500000000UL || dlane_bps < 160000000UL)
  120. return -EINVAL;
  121. else if (dlane_bps >= 1250000000)
  122. cfg->pll_opdiv = 1;
  123. else if (dlane_bps >= 630000000)
  124. cfg->pll_opdiv = 2;
  125. else if (dlane_bps >= 320000000)
  126. cfg->pll_opdiv = 4;
  127. else if (dlane_bps >= 160000000)
  128. cfg->pll_opdiv = 8;
  129. cfg->pll_fbdiv = DIV_ROUND_UP_ULL(dlane_bps * 2 * cfg->pll_opdiv *
  130. cfg->pll_ipdiv,
  131. pll_ref_hz);
  132. return 0;
  133. }
  134. static int cdns_dphy_setup_psm(struct cdns_dphy *dphy)
  135. {
  136. unsigned long psm_clk_hz = clk_get_rate(dphy->psm_clk);
  137. unsigned long psm_div;
  138. if (!psm_clk_hz || psm_clk_hz > 100000000)
  139. return -EINVAL;
  140. psm_div = DIV_ROUND_CLOSEST(psm_clk_hz, 1000000);
  141. if (dphy->ops->set_psm_div)
  142. dphy->ops->set_psm_div(dphy, psm_div);
  143. return 0;
  144. }
  145. static void cdns_dphy_set_clk_lane_cfg(struct cdns_dphy *dphy,
  146. enum cdns_dphy_clk_lane_cfg cfg)
  147. {
  148. if (dphy->ops->set_clk_lane_cfg)
  149. dphy->ops->set_clk_lane_cfg(dphy, cfg);
  150. }
  151. static void cdns_dphy_set_pll_cfg(struct cdns_dphy *dphy,
  152. const struct cdns_dphy_cfg *cfg)
  153. {
  154. if (dphy->ops->set_pll_cfg)
  155. dphy->ops->set_pll_cfg(dphy, cfg);
  156. }
  157. static unsigned long cdns_dphy_get_wakeup_time_ns(struct cdns_dphy *dphy)
  158. {
  159. return dphy->ops->get_wakeup_time_ns(dphy);
  160. }
  161. static unsigned long cdns_dphy_ref_get_wakeup_time_ns(struct cdns_dphy *dphy)
  162. {
  163. /* Default wakeup time is 800 ns (in a simulated environment). */
  164. return 800;
  165. }
  166. static void cdns_dphy_ref_set_pll_cfg(struct cdns_dphy *dphy,
  167. const struct cdns_dphy_cfg *cfg)
  168. {
  169. u32 fbdiv_low, fbdiv_high;
  170. fbdiv_low = (cfg->pll_fbdiv / 4) - 2;
  171. fbdiv_high = cfg->pll_fbdiv - fbdiv_low - 2;
  172. writel(DPHY_CMN_IPDIV_FROM_REG | DPHY_CMN_OPDIV_FROM_REG |
  173. DPHY_CMN_IPDIV(cfg->pll_ipdiv) |
  174. DPHY_CMN_OPDIV(cfg->pll_opdiv),
  175. dphy->regs + DPHY_CMN_OPIPDIV);
  176. writel(DPHY_CMN_FBDIV_FROM_REG |
  177. DPHY_CMN_FBDIV_VAL(fbdiv_low, fbdiv_high),
  178. dphy->regs + DPHY_CMN_FBDIV);
  179. writel(DPHY_CMN_PWM_HIGH(6) | DPHY_CMN_PWM_LOW(0x101) |
  180. DPHY_CMN_PWM_DIV(0x8),
  181. dphy->regs + DPHY_CMN_PWM);
  182. }
  183. static void cdns_dphy_ref_set_psm_div(struct cdns_dphy *dphy, u8 div)
  184. {
  185. writel(DPHY_PSM_CFG_FROM_REG | DPHY_PSM_CLK_DIV(div),
  186. dphy->regs + DPHY_PSM_CFG);
  187. }
  188. static unsigned long cdns_dphy_j721e_get_wakeup_time_ns(struct cdns_dphy *dphy)
  189. {
  190. /* Minimum wakeup time as per MIPI D-PHY spec v1.2 */
  191. return 1000000;
  192. }
  193. static void cdns_dphy_j721e_set_pll_cfg(struct cdns_dphy *dphy,
  194. const struct cdns_dphy_cfg *cfg)
  195. {
  196. u32 status;
  197. /*
  198. * set the PWM and PLL Byteclk divider settings to recommended values
  199. * which is same as that of in ref ops
  200. */
  201. writel(DPHY_CMN_PWM_HIGH(6) | DPHY_CMN_PWM_LOW(0x101) |
  202. DPHY_CMN_PWM_DIV(0x8),
  203. dphy->regs + DPHY_CMN_PWM);
  204. writel((FIELD_PREP(DPHY_TX_J721E_WIZ_IPDIV, cfg->pll_ipdiv) |
  205. FIELD_PREP(DPHY_TX_J721E_WIZ_OPDIV, cfg->pll_opdiv) |
  206. FIELD_PREP(DPHY_TX_J721E_WIZ_FBDIV, cfg->pll_fbdiv)),
  207. dphy->regs + DPHY_TX_J721E_WIZ_PLL_CTRL);
  208. writel(DPHY_TX_J721E_WIZ_LANE_RSTB,
  209. dphy->regs + DPHY_TX_J721E_WIZ_RST_CTRL);
  210. readl_poll_timeout(dphy->regs + DPHY_TX_J721E_WIZ_PLL_CTRL, status,
  211. (status & DPHY_TX_WIZ_PLL_LOCK), 0, POLL_TIMEOUT_US);
  212. readl_poll_timeout(dphy->regs + DPHY_TX_J721E_WIZ_STATUS, status,
  213. (status & DPHY_TX_WIZ_O_CMN_READY), 0,
  214. POLL_TIMEOUT_US);
  215. }
  216. static void cdns_dphy_j721e_set_psm_div(struct cdns_dphy *dphy, u8 div)
  217. {
  218. writel(div, dphy->regs + DPHY_TX_J721E_WIZ_PSM_FREQ);
  219. }
  220. /*
  221. * This is the reference implementation of DPHY hooks. Specific integration of
  222. * this IP may have to re-implement some of them depending on how they decided
  223. * to wire things in the SoC.
  224. */
  225. static const struct cdns_dphy_ops ref_dphy_ops = {
  226. .get_wakeup_time_ns = cdns_dphy_ref_get_wakeup_time_ns,
  227. .set_pll_cfg = cdns_dphy_ref_set_pll_cfg,
  228. .set_psm_div = cdns_dphy_ref_set_psm_div,
  229. };
  230. static const struct cdns_dphy_ops j721e_dphy_ops = {
  231. .get_wakeup_time_ns = cdns_dphy_j721e_get_wakeup_time_ns,
  232. .set_pll_cfg = cdns_dphy_j721e_set_pll_cfg,
  233. .set_psm_div = cdns_dphy_j721e_set_psm_div,
  234. };
  235. static int cdns_dphy_config_from_opts(struct phy *phy,
  236. struct phy_configure_opts_mipi_dphy *opts,
  237. struct cdns_dphy_cfg *cfg)
  238. {
  239. struct cdns_dphy *dphy = phy_get_drvdata(phy);
  240. unsigned int dsi_hfp_ext = 0;
  241. int ret;
  242. ret = phy_mipi_dphy_config_validate(opts);
  243. if (ret)
  244. return ret;
  245. ret = cdns_dsi_get_dphy_pll_cfg(dphy, cfg,
  246. opts, &dsi_hfp_ext);
  247. if (ret)
  248. return ret;
  249. opts->wakeup = cdns_dphy_get_wakeup_time_ns(dphy) / 1000;
  250. return 0;
  251. }
  252. static int cdns_dphy_tx_get_band_ctrl(unsigned long hs_clk_rate)
  253. {
  254. unsigned int rate;
  255. int i;
  256. rate = hs_clk_rate / 1000000UL;
  257. if (rate < tx_bands[0])
  258. return -EOPNOTSUPP;
  259. for (i = 0; i < ARRAY_SIZE(tx_bands) - 1; i++) {
  260. if (rate >= tx_bands[i] && rate < tx_bands[i + 1])
  261. return i;
  262. }
  263. return -EOPNOTSUPP;
  264. }
  265. static int cdns_dphy_validate(struct phy *phy, enum phy_mode mode, int submode,
  266. union phy_configure_opts *opts)
  267. {
  268. struct cdns_dphy_cfg cfg = { 0 };
  269. if (mode != PHY_MODE_MIPI_DPHY)
  270. return -EINVAL;
  271. return cdns_dphy_config_from_opts(phy, &opts->mipi_dphy, &cfg);
  272. }
  273. static int cdns_dphy_configure(struct phy *phy, union phy_configure_opts *opts)
  274. {
  275. struct cdns_dphy *dphy = phy_get_drvdata(phy);
  276. struct cdns_dphy_cfg cfg = { 0 };
  277. int ret, band_ctrl;
  278. unsigned int reg;
  279. ret = cdns_dphy_config_from_opts(phy, &opts->mipi_dphy, &cfg);
  280. if (ret)
  281. return ret;
  282. /*
  283. * Configure the internal PSM clk divider so that the DPHY has a
  284. * 1MHz clk (or something close).
  285. */
  286. ret = cdns_dphy_setup_psm(dphy);
  287. if (ret)
  288. return ret;
  289. /*
  290. * Configure attach clk lanes to data lanes: the DPHY has 2 clk lanes
  291. * and 8 data lanes, each clk lane can be attache different set of
  292. * data lanes. The 2 groups are named 'left' and 'right', so here we
  293. * just say that we want the 'left' clk lane to drive the 'left' data
  294. * lanes.
  295. */
  296. cdns_dphy_set_clk_lane_cfg(dphy, DPHY_CLK_CFG_LEFT_DRIVES_LEFT);
  297. /*
  298. * Configure the DPHY PLL that will be used to generate the TX byte
  299. * clk.
  300. */
  301. cdns_dphy_set_pll_cfg(dphy, &cfg);
  302. band_ctrl = cdns_dphy_tx_get_band_ctrl(opts->mipi_dphy.hs_clk_rate);
  303. if (band_ctrl < 0)
  304. return band_ctrl;
  305. reg = FIELD_PREP(DPHY_BAND_CFG_LEFT_BAND, band_ctrl) |
  306. FIELD_PREP(DPHY_BAND_CFG_RIGHT_BAND, band_ctrl);
  307. writel(reg, dphy->regs + DPHY_BAND_CFG);
  308. return 0;
  309. }
  310. static int cdns_dphy_power_on(struct phy *phy)
  311. {
  312. struct cdns_dphy *dphy = phy_get_drvdata(phy);
  313. clk_prepare_enable(dphy->psm_clk);
  314. clk_prepare_enable(dphy->pll_ref_clk);
  315. /* Start TX state machine. */
  316. writel(DPHY_CMN_SSM_EN | DPHY_CMN_TX_MODE_EN,
  317. dphy->regs + DPHY_CMN_SSM);
  318. return 0;
  319. }
  320. static int cdns_dphy_power_off(struct phy *phy)
  321. {
  322. struct cdns_dphy *dphy = phy_get_drvdata(phy);
  323. clk_disable_unprepare(dphy->pll_ref_clk);
  324. clk_disable_unprepare(dphy->psm_clk);
  325. return 0;
  326. }
  327. static const struct phy_ops cdns_dphy_ops = {
  328. .configure = cdns_dphy_configure,
  329. .validate = cdns_dphy_validate,
  330. .power_on = cdns_dphy_power_on,
  331. .power_off = cdns_dphy_power_off,
  332. };
  333. static int cdns_dphy_probe(struct platform_device *pdev)
  334. {
  335. struct phy_provider *phy_provider;
  336. struct cdns_dphy *dphy;
  337. int ret;
  338. dphy = devm_kzalloc(&pdev->dev, sizeof(*dphy), GFP_KERNEL);
  339. if (!dphy)
  340. return -ENOMEM;
  341. dev_set_drvdata(&pdev->dev, dphy);
  342. dphy->ops = of_device_get_match_data(&pdev->dev);
  343. if (!dphy->ops)
  344. return -EINVAL;
  345. dphy->regs = devm_platform_ioremap_resource(pdev, 0);
  346. if (IS_ERR(dphy->regs))
  347. return PTR_ERR(dphy->regs);
  348. dphy->psm_clk = devm_clk_get(&pdev->dev, "psm");
  349. if (IS_ERR(dphy->psm_clk))
  350. return PTR_ERR(dphy->psm_clk);
  351. dphy->pll_ref_clk = devm_clk_get(&pdev->dev, "pll_ref");
  352. if (IS_ERR(dphy->pll_ref_clk))
  353. return PTR_ERR(dphy->pll_ref_clk);
  354. if (dphy->ops->probe) {
  355. ret = dphy->ops->probe(dphy);
  356. if (ret)
  357. return ret;
  358. }
  359. dphy->phy = devm_phy_create(&pdev->dev, NULL, &cdns_dphy_ops);
  360. if (IS_ERR(dphy->phy)) {
  361. dev_err(&pdev->dev, "failed to create PHY\n");
  362. if (dphy->ops->remove)
  363. dphy->ops->remove(dphy);
  364. return PTR_ERR(dphy->phy);
  365. }
  366. phy_set_drvdata(dphy->phy, dphy);
  367. phy_provider = devm_of_phy_provider_register(&pdev->dev,
  368. of_phy_simple_xlate);
  369. return PTR_ERR_OR_ZERO(phy_provider);
  370. }
  371. static void cdns_dphy_remove(struct platform_device *pdev)
  372. {
  373. struct cdns_dphy *dphy = dev_get_drvdata(&pdev->dev);
  374. if (dphy->ops->remove)
  375. dphy->ops->remove(dphy);
  376. }
  377. static const struct of_device_id cdns_dphy_of_match[] = {
  378. { .compatible = "cdns,dphy", .data = &ref_dphy_ops },
  379. { .compatible = "ti,j721e-dphy", .data = &j721e_dphy_ops },
  380. { /* sentinel */ },
  381. };
  382. MODULE_DEVICE_TABLE(of, cdns_dphy_of_match);
  383. static struct platform_driver cdns_dphy_platform_driver = {
  384. .probe = cdns_dphy_probe,
  385. .remove_new = cdns_dphy_remove,
  386. .driver = {
  387. .name = "cdns-mipi-dphy",
  388. .of_match_table = cdns_dphy_of_match,
  389. },
  390. };
  391. module_platform_driver(cdns_dphy_platform_driver);
  392. MODULE_AUTHOR("Maxime Ripard <maxime.ripard@bootlin.com>");
  393. MODULE_DESCRIPTION("Cadence MIPI D-PHY Driver");
  394. MODULE_LICENSE("GPL");