dsi_phy_hw_v3_0.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "dsi-phy-hw:" fmt
  6. #include <linux/math64.h>
  7. #include <linux/delay.h>
  8. #include <linux/iopoll.h>
  9. #include "dsi_hw.h"
  10. #include "dsi_phy_hw.h"
  11. #include "dsi_catalog.h"
  12. #define DSIPHY_CMN_CLK_CFG0 0x010
  13. #define DSIPHY_CMN_CLK_CFG1 0x014
  14. #define DSIPHY_CMN_GLBL_CTRL 0x018
  15. #define DSIPHY_CMN_RBUF_CTRL 0x01C
  16. #define DSIPHY_CMN_VREG_CTRL 0x020
  17. #define DSIPHY_CMN_CTRL_0 0x024
  18. #define DSIPHY_CMN_CTRL_1 0x028
  19. #define DSIPHY_CMN_CTRL_2 0x02C
  20. #define DSIPHY_CMN_LANE_CFG0 0x030
  21. #define DSIPHY_CMN_LANE_CFG1 0x034
  22. #define DSIPHY_CMN_PLL_CNTRL 0x038
  23. #define DSIPHY_CMN_LANE_CTRL0 0x098
  24. #define DSIPHY_CMN_LANE_CTRL1 0x09C
  25. #define DSIPHY_CMN_LANE_CTRL2 0x0A0
  26. #define DSIPHY_CMN_LANE_CTRL3 0x0A4
  27. #define DSIPHY_CMN_LANE_CTRL4 0x0A8
  28. #define DSIPHY_CMN_TIMING_CTRL_0 0x0AC
  29. #define DSIPHY_CMN_TIMING_CTRL_1 0x0B0
  30. #define DSIPHY_CMN_TIMING_CTRL_2 0x0B4
  31. #define DSIPHY_CMN_TIMING_CTRL_3 0x0B8
  32. #define DSIPHY_CMN_TIMING_CTRL_4 0x0BC
  33. #define DSIPHY_CMN_TIMING_CTRL_5 0x0C0
  34. #define DSIPHY_CMN_TIMING_CTRL_6 0x0C4
  35. #define DSIPHY_CMN_TIMING_CTRL_7 0x0C8
  36. #define DSIPHY_CMN_TIMING_CTRL_8 0x0CC
  37. #define DSIPHY_CMN_TIMING_CTRL_9 0x0D0
  38. #define DSIPHY_CMN_TIMING_CTRL_10 0x0D4
  39. #define DSIPHY_CMN_TIMING_CTRL_11 0x0D8
  40. #define DSIPHY_CMN_PHY_STATUS 0x0EC
  41. #define DSIPHY_CMN_LANE_STATUS0 0x0F4
  42. #define DSIPHY_CMN_LANE_STATUS1 0x0F8
  43. /* n = 0..3 for data lanes and n = 4 for clock lane */
  44. #define DSIPHY_LNX_CFG0(n) (0x200 + (0x80 * (n)))
  45. #define DSIPHY_LNX_CFG1(n) (0x204 + (0x80 * (n)))
  46. #define DSIPHY_LNX_CFG2(n) (0x208 + (0x80 * (n)))
  47. #define DSIPHY_LNX_CFG3(n) (0x20C + (0x80 * (n)))
  48. #define DSIPHY_LNX_TEST_DATAPATH(n) (0x210 + (0x80 * (n)))
  49. #define DSIPHY_LNX_PIN_SWAP(n) (0x214 + (0x80 * (n)))
  50. #define DSIPHY_LNX_HSTX_STR_CTRL(n) (0x218 + (0x80 * (n)))
  51. #define DSIPHY_LNX_OFFSET_TOP_CTRL(n) (0x21C + (0x80 * (n)))
  52. #define DSIPHY_LNX_OFFSET_BOT_CTRL(n) (0x220 + (0x80 * (n)))
  53. #define DSIPHY_LNX_LPTX_STR_CTRL(n) (0x224 + (0x80 * (n)))
  54. #define DSIPHY_LNX_LPRX_CTRL(n) (0x228 + (0x80 * (n)))
  55. #define DSIPHY_LNX_TX_DCTRL(n) (0x22C + (0x80 * (n)))
  56. /**
  57. * regulator_enable() - enable regulators for DSI PHY
  58. * @phy: Pointer to DSI PHY hardware object.
  59. * @reg_cfg: Regulator configuration for all DSI lanes.
  60. */
  61. void dsi_phy_hw_v3_0_regulator_enable(struct dsi_phy_hw *phy,
  62. struct dsi_phy_per_lane_cfgs *reg_cfg)
  63. {
  64. pr_debug("[DSI_%d] Phy regulators enabled\n", phy->index);
  65. /* Nothing to be done for DSI PHY regulator enable */
  66. }
  67. /**
  68. * regulator_disable() - disable regulators
  69. * @phy: Pointer to DSI PHY hardware object.
  70. */
  71. void dsi_phy_hw_v3_0_regulator_disable(struct dsi_phy_hw *phy)
  72. {
  73. pr_debug("[DSI_%d] Phy regulators disabled\n", phy->index);
  74. /* Nothing to be done for DSI PHY regulator disable */
  75. }
  76. void dsi_phy_hw_v3_0_toggle_resync_fifo(struct dsi_phy_hw *phy)
  77. {
  78. DSI_W32(phy, DSIPHY_CMN_RBUF_CTRL, 0x00);
  79. /* ensure that the FIFO is off */
  80. wmb();
  81. DSI_W32(phy, DSIPHY_CMN_RBUF_CTRL, 0x1);
  82. /* ensure that the FIFO is toggled back on */
  83. wmb();
  84. }
  85. static int dsi_phy_hw_v3_0_is_pll_on(struct dsi_phy_hw *phy)
  86. {
  87. u32 data = 0;
  88. data = DSI_R32(phy, DSIPHY_CMN_PLL_CNTRL);
  89. mb(); /*make sure read happened */
  90. return (data & BIT(0));
  91. }
  92. static void dsi_phy_hw_v3_0_config_lpcdrx(struct dsi_phy_hw *phy,
  93. struct dsi_phy_cfg *cfg, bool enable)
  94. {
  95. int phy_lane_0 = dsi_phy_conv_logical_to_phy_lane(&cfg->lane_map,
  96. DSI_LOGICAL_LANE_0);
  97. /*
  98. * LPRX and CDRX need to enabled only for physical data lane
  99. * corresponding to the logical data lane 0
  100. */
  101. if (enable)
  102. DSI_W32(phy, DSIPHY_LNX_LPRX_CTRL(phy_lane_0),
  103. cfg->strength.lane[phy_lane_0][1]);
  104. else
  105. DSI_W32(phy, DSIPHY_LNX_LPRX_CTRL(phy_lane_0), 0);
  106. }
  107. static void dsi_phy_hw_v3_0_lane_swap_config(struct dsi_phy_hw *phy,
  108. struct dsi_lane_map *lane_map)
  109. {
  110. DSI_W32(phy, DSIPHY_CMN_LANE_CFG0,
  111. (lane_map->lane_map_v2[DSI_LOGICAL_LANE_0] |
  112. (lane_map->lane_map_v2[DSI_LOGICAL_LANE_1] << 4)));
  113. DSI_W32(phy, DSIPHY_CMN_LANE_CFG1,
  114. (lane_map->lane_map_v2[DSI_LOGICAL_LANE_2] |
  115. (lane_map->lane_map_v2[DSI_LOGICAL_LANE_3] << 4)));
  116. }
  117. static void dsi_phy_hw_v3_0_lane_settings(struct dsi_phy_hw *phy,
  118. struct dsi_phy_cfg *cfg)
  119. {
  120. int i;
  121. u8 tx_dctrl[] = {0x00, 0x00, 0x00, 0x04, 0x01};
  122. /* Strength ctrl settings */
  123. for (i = DSI_LOGICAL_LANE_0; i < DSI_LANE_MAX; i++) {
  124. DSI_W32(phy, DSIPHY_LNX_LPTX_STR_CTRL(i),
  125. cfg->strength.lane[i][0]);
  126. /*
  127. * Disable LPRX and CDRX for all lanes. And later on, it will
  128. * be only enabled for the physical data lane corresponding
  129. * to the logical data lane 0
  130. */
  131. DSI_W32(phy, DSIPHY_LNX_LPRX_CTRL(i), 0);
  132. DSI_W32(phy, DSIPHY_LNX_PIN_SWAP(i), 0x0);
  133. DSI_W32(phy, DSIPHY_LNX_HSTX_STR_CTRL(i), 0x88);
  134. }
  135. dsi_phy_hw_v3_0_config_lpcdrx(phy, cfg, true);
  136. /* other settings */
  137. for (i = DSI_LOGICAL_LANE_0; i < DSI_LANE_MAX; i++) {
  138. DSI_W32(phy, DSIPHY_LNX_CFG0(i), cfg->lanecfg.lane[i][0]);
  139. DSI_W32(phy, DSIPHY_LNX_CFG1(i), cfg->lanecfg.lane[i][1]);
  140. DSI_W32(phy, DSIPHY_LNX_CFG2(i), cfg->lanecfg.lane[i][2]);
  141. DSI_W32(phy, DSIPHY_LNX_CFG3(i), cfg->lanecfg.lane[i][3]);
  142. DSI_W32(phy, DSIPHY_LNX_OFFSET_TOP_CTRL(i), 0x0);
  143. DSI_W32(phy, DSIPHY_LNX_OFFSET_BOT_CTRL(i), 0x0);
  144. DSI_W32(phy, DSIPHY_LNX_TX_DCTRL(i), tx_dctrl[i]);
  145. }
  146. }
  147. void dsi_phy_hw_v3_0_clamp_ctrl(struct dsi_phy_hw *phy, bool enable)
  148. {
  149. u32 reg;
  150. pr_debug("enable=%s\n", enable ? "true" : "false");
  151. /*
  152. * DSI PHY lane clamps, also referred to as PHY FreezeIO is
  153. * enalbed by default as part of the initialization sequnce.
  154. * This would get triggered anytime the chip FreezeIO is asserted.
  155. */
  156. if (enable)
  157. return;
  158. /*
  159. * Toggle BIT 0 to exlplictly release PHY freeze I/0 to disable
  160. * the clamps.
  161. */
  162. reg = DSI_R32(phy, DSIPHY_LNX_TX_DCTRL(3));
  163. DSI_W32(phy, DSIPHY_LNX_TX_DCTRL(3), reg | BIT(0));
  164. wmb(); /* Ensure that the freezeio bit is toggled */
  165. DSI_W32(phy, DSIPHY_LNX_TX_DCTRL(3), reg & ~BIT(0));
  166. wmb(); /* Ensure that the freezeio bit is toggled */
  167. }
  168. /**
  169. * enable() - Enable PHY hardware
  170. * @phy: Pointer to DSI PHY hardware object.
  171. * @cfg: Per lane configurations for timing, strength and lane
  172. * configurations.
  173. */
  174. void dsi_phy_hw_v3_0_enable(struct dsi_phy_hw *phy,
  175. struct dsi_phy_cfg *cfg)
  176. {
  177. int rc = 0;
  178. u32 status;
  179. u32 const delay_us = 5;
  180. u32 const timeout_us = 1000;
  181. struct dsi_phy_per_lane_cfgs *timing = &cfg->timing;
  182. u32 data;
  183. if (dsi_phy_hw_v3_0_is_pll_on(phy))
  184. pr_warn("PLL turned on before configuring PHY\n");
  185. /* wait for REFGEN READY */
  186. rc = readl_poll_timeout_atomic(phy->base + DSIPHY_CMN_PHY_STATUS,
  187. status, (status & BIT(0)), delay_us, timeout_us);
  188. if (rc) {
  189. pr_err("Ref gen not ready. Aborting\n");
  190. return;
  191. }
  192. /* de-assert digital and pll power down */
  193. data = BIT(6) | BIT(5);
  194. DSI_W32(phy, DSIPHY_CMN_CTRL_0, data);
  195. /* Assert PLL core reset */
  196. DSI_W32(phy, DSIPHY_CMN_PLL_CNTRL, 0x00);
  197. /* turn off resync FIFO */
  198. DSI_W32(phy, DSIPHY_CMN_RBUF_CTRL, 0x00);
  199. /* Select MS1 byte-clk */
  200. DSI_W32(phy, DSIPHY_CMN_GLBL_CTRL, 0x10);
  201. /* Enable LDO */
  202. DSI_W32(phy, DSIPHY_CMN_VREG_CTRL, 0x59);
  203. /* Configure PHY lane swap */
  204. dsi_phy_hw_v3_0_lane_swap_config(phy, &cfg->lane_map);
  205. /* DSI PHY timings */
  206. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_0, timing->lane_v3[0]);
  207. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_1, timing->lane_v3[1]);
  208. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_2, timing->lane_v3[2]);
  209. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_3, timing->lane_v3[3]);
  210. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_4, timing->lane_v3[4]);
  211. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_5, timing->lane_v3[5]);
  212. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_6, timing->lane_v3[6]);
  213. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_7, timing->lane_v3[7]);
  214. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_8, timing->lane_v3[8]);
  215. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_9, timing->lane_v3[9]);
  216. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_10, timing->lane_v3[10]);
  217. DSI_W32(phy, DSIPHY_CMN_TIMING_CTRL_11, timing->lane_v3[11]);
  218. /* Remove power down from all blocks */
  219. DSI_W32(phy, DSIPHY_CMN_CTRL_0, 0x7f);
  220. /*power up lanes */
  221. data = DSI_R32(phy, DSIPHY_CMN_CTRL_0);
  222. /* TODO: only power up lanes that are used */
  223. data |= 0x1F;
  224. DSI_W32(phy, DSIPHY_CMN_CTRL_0, data);
  225. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL0, 0x1F);
  226. /* Select full-rate mode */
  227. DSI_W32(phy, DSIPHY_CMN_CTRL_2, 0x40);
  228. switch (cfg->pll_source) {
  229. case DSI_PLL_SOURCE_STANDALONE:
  230. case DSI_PLL_SOURCE_NATIVE:
  231. data = 0x0; /* internal PLL */
  232. break;
  233. case DSI_PLL_SOURCE_NON_NATIVE:
  234. data = 0x1; /* external PLL */
  235. break;
  236. default:
  237. break;
  238. }
  239. DSI_W32(phy, DSIPHY_CMN_CLK_CFG1, (data << 2)); /* set PLL src */
  240. /* DSI lane settings */
  241. dsi_phy_hw_v3_0_lane_settings(phy, cfg);
  242. pr_debug("[DSI_%d]Phy enabled\n", phy->index);
  243. }
  244. /**
  245. * disable() - Disable PHY hardware
  246. * @phy: Pointer to DSI PHY hardware object.
  247. */
  248. void dsi_phy_hw_v3_0_disable(struct dsi_phy_hw *phy,
  249. struct dsi_phy_cfg *cfg)
  250. {
  251. u32 data = 0;
  252. if (dsi_phy_hw_v3_0_is_pll_on(phy))
  253. pr_warn("Turning OFF PHY while PLL is on\n");
  254. dsi_phy_hw_v3_0_config_lpcdrx(phy, cfg, false);
  255. data = DSI_R32(phy, DSIPHY_CMN_CTRL_0);
  256. /* disable all lanes */
  257. data &= ~0x1F;
  258. DSI_W32(phy, DSIPHY_CMN_CTRL_0, data);
  259. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL0, 0);
  260. /* Turn off all PHY blocks */
  261. DSI_W32(phy, DSIPHY_CMN_CTRL_0, 0x00);
  262. /* make sure phy is turned off */
  263. wmb();
  264. pr_debug("[DSI_%d]Phy disabled\n", phy->index);
  265. }
  266. int dsi_phy_hw_v3_0_wait_for_lane_idle(
  267. struct dsi_phy_hw *phy, u32 lanes)
  268. {
  269. int rc = 0, val = 0;
  270. u32 stop_state_mask = 0;
  271. u32 const sleep_us = 10;
  272. u32 const timeout_us = 100;
  273. stop_state_mask = BIT(4); /* clock lane */
  274. if (lanes & DSI_DATA_LANE_0)
  275. stop_state_mask |= BIT(0);
  276. if (lanes & DSI_DATA_LANE_1)
  277. stop_state_mask |= BIT(1);
  278. if (lanes & DSI_DATA_LANE_2)
  279. stop_state_mask |= BIT(2);
  280. if (lanes & DSI_DATA_LANE_3)
  281. stop_state_mask |= BIT(3);
  282. pr_debug("%s: polling for lanes to be in stop state, mask=0x%08x\n",
  283. __func__, stop_state_mask);
  284. rc = readl_poll_timeout(phy->base + DSIPHY_CMN_LANE_STATUS1, val,
  285. ((val & stop_state_mask) == stop_state_mask),
  286. sleep_us, timeout_us);
  287. if (rc) {
  288. pr_err("%s: lanes not in stop state, LANE_STATUS=0x%08x\n",
  289. __func__, val);
  290. return rc;
  291. }
  292. return 0;
  293. }
  294. void dsi_phy_hw_v3_0_ulps_request(struct dsi_phy_hw *phy,
  295. struct dsi_phy_cfg *cfg, u32 lanes)
  296. {
  297. u32 reg = 0;
  298. if (lanes & DSI_CLOCK_LANE)
  299. reg = BIT(4);
  300. if (lanes & DSI_DATA_LANE_0)
  301. reg |= BIT(0);
  302. if (lanes & DSI_DATA_LANE_1)
  303. reg |= BIT(1);
  304. if (lanes & DSI_DATA_LANE_2)
  305. reg |= BIT(2);
  306. if (lanes & DSI_DATA_LANE_3)
  307. reg |= BIT(3);
  308. /*
  309. * ULPS entry request. Wait for short time to make sure
  310. * that the lanes enter ULPS. Recommended as per HPG.
  311. */
  312. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL1, reg);
  313. usleep_range(100, 110);
  314. /* disable LPRX and CDRX */
  315. dsi_phy_hw_v3_0_config_lpcdrx(phy, cfg, false);
  316. /* disable lane LDOs */
  317. DSI_W32(phy, DSIPHY_CMN_VREG_CTRL, 0x19);
  318. pr_debug("[DSI_PHY%d] ULPS requested for lanes 0x%x\n", phy->index,
  319. lanes);
  320. }
  321. int dsi_phy_hw_v3_0_lane_reset(struct dsi_phy_hw *phy)
  322. {
  323. int ret = 0, loop = 10, u_dly = 200;
  324. u32 ln_status = 0;
  325. while ((ln_status != 0x1f) && loop) {
  326. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL3, 0x1f);
  327. wmb(); /* ensure register is committed */
  328. loop--;
  329. udelay(u_dly);
  330. ln_status = DSI_R32(phy, DSIPHY_CMN_LANE_STATUS1);
  331. pr_debug("trial no: %d\n", loop);
  332. }
  333. if (!loop)
  334. pr_debug("could not reset phy lanes\n");
  335. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL3, 0x0);
  336. wmb(); /* ensure register is committed */
  337. return ret;
  338. }
  339. void dsi_phy_hw_v3_0_ulps_exit(struct dsi_phy_hw *phy,
  340. struct dsi_phy_cfg *cfg, u32 lanes)
  341. {
  342. u32 reg = 0;
  343. if (lanes & DSI_CLOCK_LANE)
  344. reg = BIT(4);
  345. if (lanes & DSI_DATA_LANE_0)
  346. reg |= BIT(0);
  347. if (lanes & DSI_DATA_LANE_1)
  348. reg |= BIT(1);
  349. if (lanes & DSI_DATA_LANE_2)
  350. reg |= BIT(2);
  351. if (lanes & DSI_DATA_LANE_3)
  352. reg |= BIT(3);
  353. /* enable lane LDOs */
  354. DSI_W32(phy, DSIPHY_CMN_VREG_CTRL, 0x59);
  355. /* enable LPRX and CDRX */
  356. dsi_phy_hw_v3_0_config_lpcdrx(phy, cfg, true);
  357. /* ULPS exit request */
  358. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL2, reg);
  359. usleep_range(1000, 1010);
  360. /* Clear ULPS request flags on all lanes */
  361. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL1, 0);
  362. /* Clear ULPS exit flags on all lanes */
  363. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL2, 0);
  364. /*
  365. * Sometimes when exiting ULPS, it is possible that some DSI
  366. * lanes are not in the stop state which could lead to DSI
  367. * commands not going through. To avoid this, force the lanes
  368. * to be in stop state.
  369. */
  370. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL3, reg);
  371. DSI_W32(phy, DSIPHY_CMN_LANE_CTRL3, 0);
  372. usleep_range(100, 110);
  373. }
  374. u32 dsi_phy_hw_v3_0_get_lanes_in_ulps(struct dsi_phy_hw *phy)
  375. {
  376. u32 lanes = 0;
  377. lanes = DSI_R32(phy, DSIPHY_CMN_LANE_STATUS0);
  378. pr_debug("[DSI_PHY%d] lanes in ulps = 0x%x\n", phy->index, lanes);
  379. return lanes;
  380. }
  381. bool dsi_phy_hw_v3_0_is_lanes_in_ulps(u32 lanes, u32 ulps_lanes)
  382. {
  383. if (lanes & ulps_lanes)
  384. return false;
  385. return true;
  386. }
  387. int dsi_phy_hw_timing_val_v3_0(struct dsi_phy_per_lane_cfgs *timing_cfg,
  388. u32 *timing_val, u32 size)
  389. {
  390. int i = 0;
  391. if (size != DSI_PHY_TIMING_V3_SIZE) {
  392. pr_err("Unexpected timing array size %d\n", size);
  393. return -EINVAL;
  394. }
  395. for (i = 0; i < size; i++)
  396. timing_cfg->lane_v3[i] = timing_val[i];
  397. return 0;
  398. }