cdns-dphy.c 13 KB

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