sun8i_hdmi_phy.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2018 Jernej Skrabec <jernej.skrabec@siol.net>
  4. */
  5. #include <linux/delay.h>
  6. #include <linux/of_address.h>
  7. #include "sun8i_dw_hdmi.h"
  8. /*
  9. * Address can be actually any value. Here is set to same value as
  10. * it is set in BSP driver.
  11. */
  12. #define I2C_ADDR 0x69
  13. static int sun8i_hdmi_phy_config_a83t(struct dw_hdmi *hdmi,
  14. struct sun8i_hdmi_phy *phy,
  15. unsigned int clk_rate)
  16. {
  17. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
  18. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN,
  19. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN);
  20. /* power down */
  21. dw_hdmi_phy_gen2_txpwron(hdmi, 0);
  22. dw_hdmi_phy_gen2_pddq(hdmi, 1);
  23. dw_hdmi_phy_reset(hdmi);
  24. dw_hdmi_phy_gen2_pddq(hdmi, 0);
  25. dw_hdmi_phy_i2c_set_addr(hdmi, I2C_ADDR);
  26. /*
  27. * Values are taken from BSP HDMI driver. Although AW didn't
  28. * release any documentation, explanation of this values can
  29. * be found in i.MX 6Dual/6Quad Reference Manual.
  30. */
  31. if (clk_rate <= 27000000) {
  32. dw_hdmi_phy_i2c_write(hdmi, 0x01e0, 0x06);
  33. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x15);
  34. dw_hdmi_phy_i2c_write(hdmi, 0x08da, 0x10);
  35. dw_hdmi_phy_i2c_write(hdmi, 0x0007, 0x19);
  36. dw_hdmi_phy_i2c_write(hdmi, 0x0318, 0x0e);
  37. dw_hdmi_phy_i2c_write(hdmi, 0x8009, 0x09);
  38. } else if (clk_rate <= 74250000) {
  39. dw_hdmi_phy_i2c_write(hdmi, 0x0540, 0x06);
  40. dw_hdmi_phy_i2c_write(hdmi, 0x0005, 0x15);
  41. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
  42. dw_hdmi_phy_i2c_write(hdmi, 0x0007, 0x19);
  43. dw_hdmi_phy_i2c_write(hdmi, 0x02b5, 0x0e);
  44. dw_hdmi_phy_i2c_write(hdmi, 0x8009, 0x09);
  45. } else if (clk_rate <= 148500000) {
  46. dw_hdmi_phy_i2c_write(hdmi, 0x04a0, 0x06);
  47. dw_hdmi_phy_i2c_write(hdmi, 0x000a, 0x15);
  48. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
  49. dw_hdmi_phy_i2c_write(hdmi, 0x0002, 0x19);
  50. dw_hdmi_phy_i2c_write(hdmi, 0x0021, 0x0e);
  51. dw_hdmi_phy_i2c_write(hdmi, 0x8029, 0x09);
  52. } else {
  53. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x06);
  54. dw_hdmi_phy_i2c_write(hdmi, 0x000f, 0x15);
  55. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
  56. dw_hdmi_phy_i2c_write(hdmi, 0x0002, 0x19);
  57. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x0e);
  58. dw_hdmi_phy_i2c_write(hdmi, 0x802b, 0x09);
  59. }
  60. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x1e);
  61. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x13);
  62. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x17);
  63. dw_hdmi_phy_gen2_txpwron(hdmi, 1);
  64. return 0;
  65. }
  66. static int sun8i_hdmi_phy_config_h3(struct dw_hdmi *hdmi,
  67. struct sun8i_hdmi_phy *phy,
  68. unsigned int clk_rate)
  69. {
  70. u32 pll_cfg1_init;
  71. u32 pll_cfg2_init;
  72. u32 ana_cfg1_end;
  73. u32 ana_cfg2_init;
  74. u32 ana_cfg3_init;
  75. u32 b_offset = 0;
  76. u32 val;
  77. /* bandwidth / frequency independent settings */
  78. pll_cfg1_init = SUN8I_HDMI_PHY_PLL_CFG1_LDO2_EN |
  79. SUN8I_HDMI_PHY_PLL_CFG1_LDO1_EN |
  80. SUN8I_HDMI_PHY_PLL_CFG1_LDO_VSET(7) |
  81. SUN8I_HDMI_PHY_PLL_CFG1_UNKNOWN(1) |
  82. SUN8I_HDMI_PHY_PLL_CFG1_PLLDBEN |
  83. SUN8I_HDMI_PHY_PLL_CFG1_CS |
  84. SUN8I_HDMI_PHY_PLL_CFG1_CP_S(2) |
  85. SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(63) |
  86. SUN8I_HDMI_PHY_PLL_CFG1_BWS;
  87. pll_cfg2_init = SUN8I_HDMI_PHY_PLL_CFG2_SV_H |
  88. SUN8I_HDMI_PHY_PLL_CFG2_VCOGAIN_EN |
  89. SUN8I_HDMI_PHY_PLL_CFG2_SDIV2;
  90. ana_cfg1_end = SUN8I_HDMI_PHY_ANA_CFG1_REG_SVBH(1) |
  91. SUN8I_HDMI_PHY_ANA_CFG1_AMP_OPT |
  92. SUN8I_HDMI_PHY_ANA_CFG1_EMP_OPT |
  93. SUN8I_HDMI_PHY_ANA_CFG1_AMPCK_OPT |
  94. SUN8I_HDMI_PHY_ANA_CFG1_EMPCK_OPT |
  95. SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL |
  96. SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG |
  97. SUN8I_HDMI_PHY_ANA_CFG1_REG_SCKTMDS |
  98. SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN |
  99. SUN8I_HDMI_PHY_ANA_CFG1_TXEN_MASK |
  100. SUN8I_HDMI_PHY_ANA_CFG1_TXEN_ALL |
  101. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK |
  102. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
  103. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
  104. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
  105. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2 |
  106. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
  107. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
  108. SUN8I_HDMI_PHY_ANA_CFG1_CKEN |
  109. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
  110. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
  111. SUN8I_HDMI_PHY_ANA_CFG1_ENBI;
  112. ana_cfg2_init = SUN8I_HDMI_PHY_ANA_CFG2_M_EN |
  113. SUN8I_HDMI_PHY_ANA_CFG2_REG_DENCK |
  114. SUN8I_HDMI_PHY_ANA_CFG2_REG_DEN |
  115. SUN8I_HDMI_PHY_ANA_CFG2_REG_CKSS(1) |
  116. SUN8I_HDMI_PHY_ANA_CFG2_REG_CSMPS(1);
  117. ana_cfg3_init = SUN8I_HDMI_PHY_ANA_CFG3_REG_WIRE(0x3e0) |
  118. SUN8I_HDMI_PHY_ANA_CFG3_SDAEN |
  119. SUN8I_HDMI_PHY_ANA_CFG3_SCLEN;
  120. /* bandwidth / frequency dependent settings */
  121. if (clk_rate <= 27000000) {
  122. pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
  123. SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
  124. pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
  125. SUN8I_HDMI_PHY_PLL_CFG2_S(4);
  126. ana_cfg1_end |= SUN8I_HDMI_PHY_ANA_CFG1_REG_CALSW;
  127. ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4) |
  128. SUN8I_HDMI_PHY_ANA_CFG2_REG_RESDI(phy->rcal);
  129. ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(3) |
  130. SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(5);
  131. } else if (clk_rate <= 74250000) {
  132. pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
  133. SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
  134. pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
  135. SUN8I_HDMI_PHY_PLL_CFG2_S(5);
  136. ana_cfg1_end |= SUN8I_HDMI_PHY_ANA_CFG1_REG_CALSW;
  137. ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4) |
  138. SUN8I_HDMI_PHY_ANA_CFG2_REG_RESDI(phy->rcal);
  139. ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(5) |
  140. SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(7);
  141. } else if (clk_rate <= 148500000) {
  142. pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
  143. SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
  144. pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
  145. SUN8I_HDMI_PHY_PLL_CFG2_S(6);
  146. ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSWCK |
  147. SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSW |
  148. SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(2);
  149. ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(7) |
  150. SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(9);
  151. } else {
  152. b_offset = 2;
  153. pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(63);
  154. pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(6) |
  155. SUN8I_HDMI_PHY_PLL_CFG2_S(7);
  156. ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSWCK |
  157. SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSW |
  158. SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4);
  159. ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(9) |
  160. SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(13);
  161. }
  162. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  163. SUN8I_HDMI_PHY_ANA_CFG1_TXEN_MASK, 0);
  164. regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG, pll_cfg1_init);
  165. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG2_REG,
  166. (u32)~SUN8I_HDMI_PHY_PLL_CFG2_PREDIV_MSK,
  167. pll_cfg2_init);
  168. usleep_range(10000, 15000);
  169. regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG3_REG,
  170. SUN8I_HDMI_PHY_PLL_CFG3_SOUT_DIV2);
  171. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  172. SUN8I_HDMI_PHY_PLL_CFG1_PLLEN,
  173. SUN8I_HDMI_PHY_PLL_CFG1_PLLEN);
  174. msleep(100);
  175. /* get B value */
  176. regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
  177. val = (val & SUN8I_HDMI_PHY_ANA_STS_B_OUT_MSK) >>
  178. SUN8I_HDMI_PHY_ANA_STS_B_OUT_SHIFT;
  179. val = min(val + b_offset, (u32)0x3f);
  180. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  181. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
  182. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD,
  183. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
  184. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD);
  185. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  186. SUN8I_HDMI_PHY_PLL_CFG1_B_IN_MSK,
  187. val << SUN8I_HDMI_PHY_PLL_CFG1_B_IN_SHIFT);
  188. msleep(100);
  189. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, ana_cfg1_end);
  190. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG2_REG, ana_cfg2_init);
  191. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG, ana_cfg3_init);
  192. return 0;
  193. }
  194. static int sun8i_hdmi_phy_config(struct dw_hdmi *hdmi, void *data,
  195. struct drm_display_mode *mode)
  196. {
  197. struct sun8i_hdmi_phy *phy = (struct sun8i_hdmi_phy *)data;
  198. u32 val = 0;
  199. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  200. val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NHSYNC;
  201. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  202. val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NVSYNC;
  203. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  204. SUN8I_HDMI_PHY_DBG_CTRL_POL_MASK, val);
  205. if (phy->variant->has_phy_clk)
  206. clk_set_rate(phy->clk_phy, mode->crtc_clock * 1000);
  207. return phy->variant->phy_config(hdmi, phy, mode->crtc_clock * 1000);
  208. };
  209. static void sun8i_hdmi_phy_disable_a83t(struct dw_hdmi *hdmi,
  210. struct sun8i_hdmi_phy *phy)
  211. {
  212. dw_hdmi_phy_gen2_txpwron(hdmi, 0);
  213. dw_hdmi_phy_gen2_pddq(hdmi, 1);
  214. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
  215. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN, 0);
  216. }
  217. static void sun8i_hdmi_phy_disable_h3(struct dw_hdmi *hdmi,
  218. struct sun8i_hdmi_phy *phy)
  219. {
  220. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  221. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
  222. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
  223. SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
  224. regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG, 0);
  225. }
  226. static void sun8i_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
  227. {
  228. struct sun8i_hdmi_phy *phy = (struct sun8i_hdmi_phy *)data;
  229. phy->variant->phy_disable(hdmi, phy);
  230. }
  231. static const struct dw_hdmi_phy_ops sun8i_hdmi_phy_ops = {
  232. .init = &sun8i_hdmi_phy_config,
  233. .disable = &sun8i_hdmi_phy_disable,
  234. .read_hpd = &dw_hdmi_phy_read_hpd,
  235. .update_hpd = &dw_hdmi_phy_update_hpd,
  236. .setup_hpd = &dw_hdmi_phy_setup_hpd,
  237. };
  238. static void sun8i_hdmi_phy_init_a83t(struct sun8i_hdmi_phy *phy)
  239. {
  240. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  241. SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK,
  242. SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK);
  243. /*
  244. * Set PHY I2C address. It must match to the address set by
  245. * dw_hdmi_phy_set_slave_addr().
  246. */
  247. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  248. SUN8I_HDMI_PHY_DBG_CTRL_ADDR_MASK,
  249. SUN8I_HDMI_PHY_DBG_CTRL_ADDR(I2C_ADDR));
  250. }
  251. static void sun8i_hdmi_phy_init_h3(struct sun8i_hdmi_phy *phy)
  252. {
  253. unsigned int val;
  254. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, 0);
  255. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  256. SUN8I_HDMI_PHY_ANA_CFG1_ENBI,
  257. SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
  258. udelay(5);
  259. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  260. SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN,
  261. SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN);
  262. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  263. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS,
  264. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS);
  265. usleep_range(10, 20);
  266. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  267. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN,
  268. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN);
  269. udelay(5);
  270. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  271. SUN8I_HDMI_PHY_ANA_CFG1_CKEN,
  272. SUN8I_HDMI_PHY_ANA_CFG1_CKEN);
  273. usleep_range(40, 100);
  274. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  275. SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL,
  276. SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL);
  277. usleep_range(100, 200);
  278. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  279. SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG,
  280. SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG);
  281. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  282. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
  283. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
  284. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2,
  285. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
  286. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
  287. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2);
  288. /* wait for calibration to finish */
  289. regmap_read_poll_timeout(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, val,
  290. (val & SUN8I_HDMI_PHY_ANA_STS_RCALEND2D),
  291. 100, 2000);
  292. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  293. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK,
  294. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK);
  295. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  296. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
  297. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
  298. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
  299. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK,
  300. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
  301. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
  302. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
  303. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK);
  304. /* enable DDC communication */
  305. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG,
  306. SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
  307. SUN8I_HDMI_PHY_ANA_CFG3_SDAEN,
  308. SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
  309. SUN8I_HDMI_PHY_ANA_CFG3_SDAEN);
  310. /* set HW control of CEC pins */
  311. regmap_write(phy->regs, SUN8I_HDMI_PHY_CEC_REG, 0);
  312. /* read calibration data */
  313. regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
  314. phy->rcal = (val & SUN8I_HDMI_PHY_ANA_STS_RCAL_MASK) >> 2;
  315. }
  316. void sun8i_hdmi_phy_init(struct sun8i_hdmi_phy *phy)
  317. {
  318. /* enable read access to HDMI controller */
  319. regmap_write(phy->regs, SUN8I_HDMI_PHY_READ_EN_REG,
  320. SUN8I_HDMI_PHY_READ_EN_MAGIC);
  321. /* unscramble register offsets */
  322. regmap_write(phy->regs, SUN8I_HDMI_PHY_UNSCRAMBLE_REG,
  323. SUN8I_HDMI_PHY_UNSCRAMBLE_MAGIC);
  324. phy->variant->phy_init(phy);
  325. }
  326. const struct dw_hdmi_phy_ops *sun8i_hdmi_phy_get_ops(void)
  327. {
  328. return &sun8i_hdmi_phy_ops;
  329. }
  330. static struct regmap_config sun8i_hdmi_phy_regmap_config = {
  331. .reg_bits = 32,
  332. .val_bits = 32,
  333. .reg_stride = 4,
  334. .max_register = SUN8I_HDMI_PHY_CEC_REG,
  335. .name = "phy"
  336. };
  337. static const struct sun8i_hdmi_phy_variant sun8i_a83t_hdmi_phy = {
  338. .phy_init = &sun8i_hdmi_phy_init_a83t,
  339. .phy_disable = &sun8i_hdmi_phy_disable_a83t,
  340. .phy_config = &sun8i_hdmi_phy_config_a83t,
  341. };
  342. static const struct sun8i_hdmi_phy_variant sun8i_h3_hdmi_phy = {
  343. .has_phy_clk = true,
  344. .phy_init = &sun8i_hdmi_phy_init_h3,
  345. .phy_disable = &sun8i_hdmi_phy_disable_h3,
  346. .phy_config = &sun8i_hdmi_phy_config_h3,
  347. };
  348. static const struct of_device_id sun8i_hdmi_phy_of_table[] = {
  349. {
  350. .compatible = "allwinner,sun8i-a83t-hdmi-phy",
  351. .data = &sun8i_a83t_hdmi_phy,
  352. },
  353. {
  354. .compatible = "allwinner,sun8i-h3-hdmi-phy",
  355. .data = &sun8i_h3_hdmi_phy,
  356. },
  357. { /* sentinel */ }
  358. };
  359. int sun8i_hdmi_phy_probe(struct sun8i_dw_hdmi *hdmi, struct device_node *node)
  360. {
  361. const struct of_device_id *match;
  362. struct device *dev = hdmi->dev;
  363. struct sun8i_hdmi_phy *phy;
  364. struct resource res;
  365. void __iomem *regs;
  366. int ret;
  367. match = of_match_node(sun8i_hdmi_phy_of_table, node);
  368. if (!match) {
  369. dev_err(dev, "Incompatible HDMI PHY\n");
  370. return -EINVAL;
  371. }
  372. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  373. if (!phy)
  374. return -ENOMEM;
  375. phy->variant = (struct sun8i_hdmi_phy_variant *)match->data;
  376. ret = of_address_to_resource(node, 0, &res);
  377. if (ret) {
  378. dev_err(dev, "phy: Couldn't get our resources\n");
  379. return ret;
  380. }
  381. regs = devm_ioremap_resource(dev, &res);
  382. if (IS_ERR(regs)) {
  383. dev_err(dev, "Couldn't map the HDMI PHY registers\n");
  384. return PTR_ERR(regs);
  385. }
  386. phy->regs = devm_regmap_init_mmio(dev, regs,
  387. &sun8i_hdmi_phy_regmap_config);
  388. if (IS_ERR(phy->regs)) {
  389. dev_err(dev, "Couldn't create the HDMI PHY regmap\n");
  390. return PTR_ERR(phy->regs);
  391. }
  392. phy->clk_bus = of_clk_get_by_name(node, "bus");
  393. if (IS_ERR(phy->clk_bus)) {
  394. dev_err(dev, "Could not get bus clock\n");
  395. return PTR_ERR(phy->clk_bus);
  396. }
  397. phy->clk_mod = of_clk_get_by_name(node, "mod");
  398. if (IS_ERR(phy->clk_mod)) {
  399. dev_err(dev, "Could not get mod clock\n");
  400. ret = PTR_ERR(phy->clk_mod);
  401. goto err_put_clk_bus;
  402. }
  403. if (phy->variant->has_phy_clk) {
  404. phy->clk_pll0 = of_clk_get_by_name(node, "pll-0");
  405. if (IS_ERR(phy->clk_pll0)) {
  406. dev_err(dev, "Could not get pll-0 clock\n");
  407. ret = PTR_ERR(phy->clk_pll0);
  408. goto err_put_clk_mod;
  409. }
  410. ret = sun8i_phy_clk_create(phy, dev);
  411. if (ret) {
  412. dev_err(dev, "Couldn't create the PHY clock\n");
  413. goto err_put_clk_pll0;
  414. }
  415. }
  416. phy->rst_phy = of_reset_control_get_shared(node, "phy");
  417. if (IS_ERR(phy->rst_phy)) {
  418. dev_err(dev, "Could not get phy reset control\n");
  419. ret = PTR_ERR(phy->rst_phy);
  420. goto err_put_clk_pll0;
  421. }
  422. ret = reset_control_deassert(phy->rst_phy);
  423. if (ret) {
  424. dev_err(dev, "Cannot deassert phy reset control: %d\n", ret);
  425. goto err_put_rst_phy;
  426. }
  427. ret = clk_prepare_enable(phy->clk_bus);
  428. if (ret) {
  429. dev_err(dev, "Cannot enable bus clock: %d\n", ret);
  430. goto err_deassert_rst_phy;
  431. }
  432. ret = clk_prepare_enable(phy->clk_mod);
  433. if (ret) {
  434. dev_err(dev, "Cannot enable mod clock: %d\n", ret);
  435. goto err_disable_clk_bus;
  436. }
  437. hdmi->phy = phy;
  438. return 0;
  439. err_disable_clk_bus:
  440. clk_disable_unprepare(phy->clk_bus);
  441. err_deassert_rst_phy:
  442. reset_control_assert(phy->rst_phy);
  443. err_put_rst_phy:
  444. reset_control_put(phy->rst_phy);
  445. err_put_clk_pll0:
  446. if (phy->variant->has_phy_clk)
  447. clk_put(phy->clk_pll0);
  448. err_put_clk_mod:
  449. clk_put(phy->clk_mod);
  450. err_put_clk_bus:
  451. clk_put(phy->clk_bus);
  452. return ret;
  453. }
  454. void sun8i_hdmi_phy_remove(struct sun8i_dw_hdmi *hdmi)
  455. {
  456. struct sun8i_hdmi_phy *phy = hdmi->phy;
  457. clk_disable_unprepare(phy->clk_mod);
  458. clk_disable_unprepare(phy->clk_bus);
  459. reset_control_assert(phy->rst_phy);
  460. reset_control_put(phy->rst_phy);
  461. if (phy->variant->has_phy_clk)
  462. clk_put(phy->clk_pll0);
  463. clk_put(phy->clk_mod);
  464. clk_put(phy->clk_bus);
  465. }