dp_catalog_v420.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2017-2021, The Linux Foundation. All rights reserved.
  4. * Copyright (c) 2022 Qualcomm Innovation Center, Inc. All rights reserved.
  5. */
  6. #include "dp_catalog.h"
  7. #include "dp_reg.h"
  8. #include "dp_debug.h"
  9. #include "dp_pll.h"
  10. #include <linux/rational.h>
  11. #define dp_catalog_get_priv_v420(x) ({ \
  12. struct dp_catalog *catalog; \
  13. catalog = container_of(x, struct dp_catalog, x); \
  14. container_of(catalog->sub, \
  15. struct dp_catalog_private_v420, sub); \
  16. })
  17. #define dp_read(x) ({ \
  18. catalog->sub.read(catalog->dpc, io_data, x); \
  19. })
  20. #define dp_write(x, y) ({ \
  21. catalog->sub.write(catalog->dpc, io_data, x, y); \
  22. })
  23. #define MAX_VOLTAGE_LEVELS 4
  24. #define MAX_PRE_EMP_LEVELS 4
  25. static u8 const vm_pre_emphasis[MAX_VOLTAGE_LEVELS][MAX_PRE_EMP_LEVELS] = {
  26. {0x00, 0x0E, 0x16, 0xFF}, /* pe0, 0 db */
  27. {0x00, 0x0E, 0x16, 0xFF}, /* pe1, 3.5 db */
  28. {0x00, 0x0E, 0xFF, 0xFF}, /* pe2, 6.0 db */
  29. {0xFF, 0xFF, 0xFF, 0xFF} /* pe3, 9.5 db */
  30. };
  31. /* voltage swing, 0.2v and 1.0v are not support */
  32. static u8 const vm_voltage_swing[MAX_VOLTAGE_LEVELS][MAX_PRE_EMP_LEVELS] = {
  33. {0x07, 0x0F, 0x16, 0xFF}, /* sw0, 0.4v */
  34. {0x11, 0x1E, 0x1F, 0xFF}, /* sw1, 0.6 v */
  35. {0x1A, 0x1F, 0xFF, 0xFF}, /* sw1, 0.8 v */
  36. {0xFF, 0xFF, 0xFF, 0xFF} /* sw1, 1.2 v, optional */
  37. };
  38. static u8 const dp_pre_emp_hbr2_hbr3[MAX_VOLTAGE_LEVELS][MAX_PRE_EMP_LEVELS] = {
  39. {0x00, 0x0C, 0x15, 0x1B}, /* pe0, 0 db */
  40. {0x02, 0x0E, 0x16, 0xFF}, /* pe1, 3.5 db */
  41. {0x02, 0x11, 0xFF, 0xFF}, /* pe2, 6.0 db */
  42. {0x04, 0xFF, 0xFF, 0xFF} /* pe3, 9.5 db */
  43. };
  44. static u8 const dp_swing_hbr2_hbr3[MAX_VOLTAGE_LEVELS][MAX_PRE_EMP_LEVELS] = {
  45. {0x02, 0x12, 0x16, 0x1A}, /* sw0, 0.4v */
  46. {0x09, 0x19, 0x1F, 0xFF}, /* sw1, 0.6v */
  47. {0x10, 0x1F, 0xFF, 0xFF}, /* sw1, 0.8v */
  48. {0x1F, 0xFF, 0xFF, 0xFF} /* sw1, 1.2v */
  49. };
  50. static u8 const dp_pre_emp_hbr_rbr[MAX_VOLTAGE_LEVELS][MAX_PRE_EMP_LEVELS] = {
  51. {0x00, 0x0D, 0x14, 0x1A}, /* pe0, 0 db */
  52. {0x00, 0x0E, 0x15, 0xFF}, /* pe1, 3.5 db */
  53. {0x00, 0x0E, 0xFF, 0xFF}, /* pe2, 6.0 db */
  54. {0x03, 0xFF, 0xFF, 0xFF} /* pe3, 9.5 db */
  55. };
  56. static u8 const dp_swing_hbr_rbr[MAX_VOLTAGE_LEVELS][MAX_PRE_EMP_LEVELS] = {
  57. {0x08, 0x0F, 0x16, 0x1F}, /* sw0, 0.4v */
  58. {0x11, 0x1E, 0x1F, 0xFF}, /* sw1, 0.6v */
  59. {0x16, 0x1F, 0xFF, 0xFF}, /* sw1, 0.8v */
  60. {0x1F, 0xFF, 0xFF, 0xFF} /* sw1, 1.2v */
  61. };
  62. static const u8 dp_pre_emp_hbr_rbr_v600[MAX_VOLTAGE_LEVELS][MAX_PRE_EMP_LEVELS] = {
  63. {0x00, 0x0D, 0x14, 0x1A}, /* pe0, 0 db */
  64. {0x00, 0x0D, 0x15, 0xFF}, /* pe1, 3.5 db */
  65. {0x00, 0x0E, 0xFF, 0xFF}, /* pe2, 6.0 db */
  66. {0x04, 0xFF, 0xFF, 0xFF} /* pe3, 9.5 db */
  67. };
  68. struct dp_catalog_private_v420 {
  69. struct device *dev;
  70. struct dp_catalog_sub sub;
  71. struct dp_catalog_io *io;
  72. struct dp_catalog *dpc;
  73. };
  74. static void dp_catalog_aux_setup_v420(struct dp_catalog_aux *aux,
  75. struct dp_aux_cfg *cfg)
  76. {
  77. struct dp_catalog_private_v420 *catalog;
  78. struct dp_io_data *io_data;
  79. int i = 0;
  80. u32 phy_version;
  81. if (!aux || !cfg) {
  82. DP_ERR("invalid input\n");
  83. return;
  84. }
  85. catalog = dp_catalog_get_priv_v420(aux);
  86. io_data = catalog->io->dp_phy;
  87. dp_write(DP_PHY_PD_CTL, 0x67);
  88. wmb(); /* make sure PD programming happened */
  89. phy_version = dp_catalog_get_dp_phy_version(catalog->dpc);
  90. if (phy_version >= 0x60000000) {
  91. /* Turn on BIAS current for PHY/PLL */
  92. io_data = catalog->io->dp_pll;
  93. dp_write(QSERDES_COM_BIAS_EN_CLKBUFLR_EN_V600, 0x17);
  94. wmb(); /* make sure BIAS programming happened */
  95. } else {
  96. /* Turn on BIAS current for PHY/PLL */
  97. io_data = catalog->io->dp_pll;
  98. dp_write(QSERDES_COM_BIAS_EN_CLKBUFLR_EN, 0x17);
  99. wmb(); /* make sure BIAS programming happened */
  100. }
  101. io_data = catalog->io->dp_phy;
  102. /* DP AUX CFG register programming */
  103. for (i = 0; i < PHY_AUX_CFG_MAX; i++) {
  104. DP_DEBUG("%s: offset=0x%08x, value=0x%08x\n",
  105. dp_phy_aux_config_type_to_string(i),
  106. cfg[i].offset, cfg[i].lut[cfg[i].current_index]);
  107. dp_write(cfg[i].offset, cfg[i].lut[cfg[i].current_index]);
  108. }
  109. wmb(); /* make sure DP AUX CFG programming happened */
  110. dp_write(DP_PHY_AUX_INTERRUPT_MASK_V420, 0x1F);
  111. }
  112. static void dp_catalog_aux_clear_hw_int_v420(struct dp_catalog_aux *aux)
  113. {
  114. struct dp_catalog_private_v420 *catalog;
  115. struct dp_io_data *io_data;
  116. u32 data = 0;
  117. u32 phy_version;
  118. if (!aux) {
  119. DP_ERR("invalid input\n");
  120. return;
  121. }
  122. catalog = dp_catalog_get_priv_v420(aux);
  123. phy_version = dp_catalog_get_dp_phy_version(catalog->dpc);
  124. io_data = catalog->io->dp_phy;
  125. if (phy_version >= 0x60000000)
  126. data = dp_read(DP_PHY_AUX_INTERRUPT_STATUS_V600);
  127. else
  128. data = dp_read(DP_PHY_AUX_INTERRUPT_STATUS_V420);
  129. dp_write(DP_PHY_AUX_INTERRUPT_CLEAR_V420, 0x1f);
  130. wmb(); /* make sure 0x1f is written before next write */
  131. dp_write(DP_PHY_AUX_INTERRUPT_CLEAR_V420, 0x9f);
  132. wmb(); /* make sure 0x9f is written before next write */
  133. dp_write(DP_PHY_AUX_INTERRUPT_CLEAR_V420, 0);
  134. wmb(); /* make sure register is cleared */
  135. }
  136. static void dp_catalog_panel_config_msa_v420(struct dp_catalog_panel *panel,
  137. u32 rate, u32 stream_rate_khz)
  138. {
  139. u32 mvid, nvid, mvid_off = 0, nvid_off = 0;
  140. u32 div, pixel_div = 0, rate_vco_div = 0;
  141. u32 const nvid_fixed = 0x8000;
  142. u32 const link_rate_hbr2 = 540000;
  143. u32 const link_rate_hbr3 = 810000;
  144. struct dp_catalog *dp_catalog;
  145. struct dp_catalog_private_v420 *catalog;
  146. struct dp_io_data *io_data;
  147. unsigned long num, den;
  148. if (!panel || !rate) {
  149. DP_ERR("invalid input\n");
  150. return;
  151. }
  152. if (panel->stream_id >= DP_STREAM_MAX) {
  153. DP_ERR("invalid stream id:%d\n", panel->stream_id);
  154. return;
  155. }
  156. dp_catalog = container_of(panel, struct dp_catalog, panel);
  157. catalog = container_of(dp_catalog->sub, struct dp_catalog_private_v420, sub);
  158. io_data = catalog->io->dp_pll;
  159. div = dp_read(DP_PHY_VCO_DIV);
  160. div &= 0x03;
  161. if (div == 0)
  162. pixel_div = 6;
  163. else if (div == 1)
  164. pixel_div = 2;
  165. else if (div == 2)
  166. pixel_div = 4;
  167. if (!pixel_div) {
  168. DP_ERR("Invalid pixel mux divider, not setting software mvid and nvid\n");
  169. return;
  170. }
  171. rate_vco_div = (rate * 10) / pixel_div;
  172. rational_best_approximation(rate_vco_div, (stream_rate_khz / 2),
  173. (unsigned long)(1 << 16) - 1,
  174. (unsigned long)(1 << 16) - 1, &den, &num);
  175. den = ~(den - num);
  176. den = den & 0xFFFF;
  177. mvid = (num & 0xFFFF) * 5;
  178. nvid = (0xFFFF & (~den)) + (num & 0xFFFF);
  179. if (nvid < nvid_fixed) {
  180. u32 temp;
  181. temp = (nvid_fixed / nvid) * nvid;
  182. mvid = (nvid_fixed / nvid) * mvid;
  183. nvid = temp;
  184. }
  185. DP_DEBUG("rate = %d\n", rate);
  186. if (panel->widebus_en)
  187. mvid <<= 1;
  188. if (link_rate_hbr2 == rate)
  189. nvid *= 2;
  190. if (link_rate_hbr3 == rate)
  191. nvid *= 3;
  192. io_data = catalog->io->dp_link;
  193. if (panel->stream_id == DP_STREAM_1) {
  194. mvid_off = DP1_SOFTWARE_MVID - DP_SOFTWARE_MVID;
  195. nvid_off = DP1_SOFTWARE_NVID - DP_SOFTWARE_NVID;
  196. }
  197. DP_DEBUG("mvid=0x%x, nvid=0x%x\n", mvid, nvid);
  198. dp_write(DP_SOFTWARE_MVID + mvid_off, mvid);
  199. dp_write(DP_SOFTWARE_NVID + nvid_off, nvid);
  200. }
  201. static void dp_catalog_ctrl_phy_lane_cfg_v420(struct dp_catalog_ctrl *ctrl,
  202. bool flipped, u8 ln_cnt)
  203. {
  204. u32 info = 0x0;
  205. struct dp_catalog_private_v420 *catalog;
  206. struct dp_io_data *io_data;
  207. u8 orientation = BIT(!!flipped);
  208. if (!ctrl) {
  209. DP_ERR("invalid input\n");
  210. return;
  211. }
  212. catalog = dp_catalog_get_priv_v420(ctrl);
  213. io_data = catalog->io->dp_phy;
  214. info |= (ln_cnt & 0x0F);
  215. info |= ((orientation & 0x0F) << 4);
  216. DP_DEBUG("Shared Info = 0x%x\n", info);
  217. dp_write(DP_PHY_SPARE0_V420, info);
  218. }
  219. static void dp_catalog_ctrl_update_vx_px_v420(struct dp_catalog_ctrl *ctrl,
  220. u8 v_level, u8 p_level, bool high)
  221. {
  222. struct dp_catalog_private_v420 *catalog;
  223. struct dp_io_data *io_data;
  224. u8 value0, value1;
  225. u32 version;
  226. u32 phy_version;
  227. if (!ctrl || !((v_level < MAX_VOLTAGE_LEVELS)
  228. && (p_level < MAX_PRE_EMP_LEVELS))) {
  229. DP_ERR("invalid input\n");
  230. return;
  231. }
  232. DP_DEBUG("hw: v=%d p=%d, high=%d\n", v_level, p_level, high);
  233. catalog = dp_catalog_get_priv_v420(ctrl);
  234. phy_version = dp_catalog_get_dp_phy_version(catalog->dpc);
  235. io_data = catalog->io->dp_ahb;
  236. version = dp_read(DP_HW_VERSION);
  237. DP_DEBUG("version: 0x%x\n", version);
  238. /*
  239. * For DP controller versions >= 1.2.3
  240. */
  241. if (version >= 0x10020003) {
  242. if (high) {
  243. value0 = dp_swing_hbr2_hbr3[v_level][p_level];
  244. value1 = dp_pre_emp_hbr2_hbr3[v_level][p_level];
  245. } else {
  246. value0 = dp_swing_hbr_rbr[v_level][p_level];
  247. if (phy_version >= 0x60000000)
  248. value1 = dp_pre_emp_hbr_rbr_v600[v_level][p_level];
  249. else
  250. value1 = dp_pre_emp_hbr_rbr[v_level][p_level];
  251. }
  252. } else {
  253. value0 = vm_voltage_swing[v_level][p_level];
  254. value1 = vm_pre_emphasis[v_level][p_level];
  255. }
  256. /* program default setting first */
  257. io_data = catalog->io->dp_ln_tx0;
  258. dp_write(TXn_TX_DRV_LVL_V420, 0x2A);
  259. dp_write(TXn_TX_EMP_POST1_LVL, 0x20);
  260. io_data = catalog->io->dp_ln_tx1;
  261. dp_write(TXn_TX_DRV_LVL_V420, 0x2A);
  262. dp_write(TXn_TX_EMP_POST1_LVL, 0x20);
  263. /* Enable MUX to use Cursor values from these registers */
  264. value0 |= BIT(5);
  265. value1 |= BIT(5);
  266. /* Configure host and panel only if both values are allowed */
  267. if (value0 != 0xFF && value1 != 0xFF) {
  268. io_data = catalog->io->dp_ln_tx0;
  269. dp_write(TXn_TX_DRV_LVL_V420, value0);
  270. dp_write(TXn_TX_EMP_POST1_LVL, value1);
  271. io_data = catalog->io->dp_ln_tx1;
  272. dp_write(TXn_TX_DRV_LVL_V420, value0);
  273. dp_write(TXn_TX_EMP_POST1_LVL, value1);
  274. DP_DEBUG("hw: vx_value=0x%x px_value=0x%x\n",
  275. value0, value1);
  276. } else {
  277. DP_ERR("invalid vx (0x%x=0x%x), px (0x%x=0x%x\n",
  278. v_level, value0, p_level, value1);
  279. }
  280. }
  281. static void dp_catalog_ctrl_lane_pnswap_v420(struct dp_catalog_ctrl *ctrl,
  282. u8 ln_pnswap)
  283. {
  284. struct dp_catalog_private_v420 *catalog;
  285. struct dp_io_data *io_data;
  286. u32 cfg0, cfg1;
  287. catalog = dp_catalog_get_priv_v420(ctrl);
  288. cfg0 = 0x0a;
  289. cfg1 = 0x0a;
  290. cfg0 |= ((ln_pnswap >> 0) & 0x1) << 0;
  291. cfg0 |= ((ln_pnswap >> 1) & 0x1) << 2;
  292. cfg1 |= ((ln_pnswap >> 2) & 0x1) << 0;
  293. cfg1 |= ((ln_pnswap >> 3) & 0x1) << 2;
  294. io_data = catalog->io->dp_ln_tx0;
  295. dp_write(TXn_TX_POL_INV_V420, cfg0);
  296. io_data = catalog->io->dp_ln_tx1;
  297. dp_write(TXn_TX_POL_INV_V420, cfg1);
  298. }
  299. static void dp_catalog_put_v420(struct dp_catalog *catalog)
  300. {
  301. struct dp_catalog_private_v420 *catalog_priv;
  302. if (!catalog)
  303. return;
  304. catalog_priv = container_of(catalog->sub,
  305. struct dp_catalog_private_v420, sub);
  306. devm_kfree(catalog_priv->dev, catalog_priv);
  307. }
  308. struct dp_catalog_sub *dp_catalog_get_v420(struct device *dev,
  309. struct dp_catalog *catalog, struct dp_catalog_io *io)
  310. {
  311. struct dp_catalog_private_v420 *catalog_priv;
  312. if (!dev || !catalog) {
  313. DP_ERR("invalid input\n");
  314. return ERR_PTR(-EINVAL);
  315. }
  316. catalog_priv = devm_kzalloc(dev, sizeof(*catalog_priv), GFP_KERNEL);
  317. if (!catalog_priv)
  318. return ERR_PTR(-ENOMEM);
  319. catalog_priv->dev = dev;
  320. catalog_priv->io = io;
  321. catalog_priv->dpc = catalog;
  322. catalog_priv->sub.put = dp_catalog_put_v420;
  323. catalog->aux.setup = dp_catalog_aux_setup_v420;
  324. catalog->aux.clear_hw_interrupts = dp_catalog_aux_clear_hw_int_v420;
  325. catalog->panel.config_msa = dp_catalog_panel_config_msa_v420;
  326. catalog->ctrl.phy_lane_cfg = dp_catalog_ctrl_phy_lane_cfg_v420;
  327. catalog->ctrl.update_vx_px = dp_catalog_ctrl_update_vx_px_v420;
  328. catalog->ctrl.lane_pnswap = dp_catalog_ctrl_lane_pnswap_v420;
  329. return &catalog_priv->sub;
  330. }