sun8i_hdmi_phy.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. /*
  165. * NOTE: We have to be careful not to overwrite PHY parent
  166. * clock selection bit and clock divider.
  167. */
  168. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  169. (u32)~SUN8I_HDMI_PHY_PLL_CFG1_CKIN_SEL_MSK,
  170. pll_cfg1_init);
  171. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG2_REG,
  172. (u32)~SUN8I_HDMI_PHY_PLL_CFG2_PREDIV_MSK,
  173. pll_cfg2_init);
  174. usleep_range(10000, 15000);
  175. regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG3_REG,
  176. SUN8I_HDMI_PHY_PLL_CFG3_SOUT_DIV2);
  177. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  178. SUN8I_HDMI_PHY_PLL_CFG1_PLLEN,
  179. SUN8I_HDMI_PHY_PLL_CFG1_PLLEN);
  180. msleep(100);
  181. /* get B value */
  182. regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
  183. val = (val & SUN8I_HDMI_PHY_ANA_STS_B_OUT_MSK) >>
  184. SUN8I_HDMI_PHY_ANA_STS_B_OUT_SHIFT;
  185. val = min(val + b_offset, (u32)0x3f);
  186. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  187. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
  188. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD,
  189. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
  190. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD);
  191. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  192. SUN8I_HDMI_PHY_PLL_CFG1_B_IN_MSK,
  193. val << SUN8I_HDMI_PHY_PLL_CFG1_B_IN_SHIFT);
  194. msleep(100);
  195. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, ana_cfg1_end);
  196. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG2_REG, ana_cfg2_init);
  197. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG, ana_cfg3_init);
  198. return 0;
  199. }
  200. static int sun8i_hdmi_phy_config(struct dw_hdmi *hdmi, void *data,
  201. struct drm_display_mode *mode)
  202. {
  203. struct sun8i_hdmi_phy *phy = (struct sun8i_hdmi_phy *)data;
  204. u32 val = 0;
  205. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  206. val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NHSYNC;
  207. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  208. val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NVSYNC;
  209. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  210. SUN8I_HDMI_PHY_DBG_CTRL_POL_MASK, val);
  211. if (phy->variant->has_phy_clk)
  212. clk_set_rate(phy->clk_phy, mode->crtc_clock * 1000);
  213. return phy->variant->phy_config(hdmi, phy, mode->crtc_clock * 1000);
  214. };
  215. static void sun8i_hdmi_phy_disable_a83t(struct dw_hdmi *hdmi,
  216. struct sun8i_hdmi_phy *phy)
  217. {
  218. dw_hdmi_phy_gen2_txpwron(hdmi, 0);
  219. dw_hdmi_phy_gen2_pddq(hdmi, 1);
  220. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
  221. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN, 0);
  222. }
  223. static void sun8i_hdmi_phy_disable_h3(struct dw_hdmi *hdmi,
  224. struct sun8i_hdmi_phy *phy)
  225. {
  226. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  227. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
  228. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
  229. SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
  230. regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG, 0);
  231. }
  232. static void sun8i_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
  233. {
  234. struct sun8i_hdmi_phy *phy = (struct sun8i_hdmi_phy *)data;
  235. phy->variant->phy_disable(hdmi, phy);
  236. }
  237. static const struct dw_hdmi_phy_ops sun8i_hdmi_phy_ops = {
  238. .init = &sun8i_hdmi_phy_config,
  239. .disable = &sun8i_hdmi_phy_disable,
  240. .read_hpd = &dw_hdmi_phy_read_hpd,
  241. .update_hpd = &dw_hdmi_phy_update_hpd,
  242. .setup_hpd = &dw_hdmi_phy_setup_hpd,
  243. };
  244. static void sun8i_hdmi_phy_init_a83t(struct sun8i_hdmi_phy *phy)
  245. {
  246. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  247. SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK,
  248. SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK);
  249. /*
  250. * Set PHY I2C address. It must match to the address set by
  251. * dw_hdmi_phy_set_slave_addr().
  252. */
  253. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  254. SUN8I_HDMI_PHY_DBG_CTRL_ADDR_MASK,
  255. SUN8I_HDMI_PHY_DBG_CTRL_ADDR(I2C_ADDR));
  256. }
  257. static void sun8i_hdmi_phy_init_h3(struct sun8i_hdmi_phy *phy)
  258. {
  259. unsigned int val;
  260. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, 0);
  261. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  262. SUN8I_HDMI_PHY_ANA_CFG1_ENBI,
  263. SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
  264. udelay(5);
  265. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  266. SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN,
  267. SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN);
  268. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  269. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS,
  270. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS);
  271. usleep_range(10, 20);
  272. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  273. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN,
  274. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN);
  275. udelay(5);
  276. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  277. SUN8I_HDMI_PHY_ANA_CFG1_CKEN,
  278. SUN8I_HDMI_PHY_ANA_CFG1_CKEN);
  279. usleep_range(40, 100);
  280. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  281. SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL,
  282. SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL);
  283. usleep_range(100, 200);
  284. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  285. SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG,
  286. SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG);
  287. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  288. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
  289. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
  290. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2,
  291. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
  292. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
  293. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2);
  294. /* wait for calibration to finish */
  295. regmap_read_poll_timeout(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, val,
  296. (val & SUN8I_HDMI_PHY_ANA_STS_RCALEND2D),
  297. 100, 2000);
  298. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  299. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK,
  300. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK);
  301. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  302. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
  303. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
  304. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
  305. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK,
  306. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
  307. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
  308. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
  309. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK);
  310. /* enable DDC communication */
  311. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG,
  312. SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
  313. SUN8I_HDMI_PHY_ANA_CFG3_SDAEN,
  314. SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
  315. SUN8I_HDMI_PHY_ANA_CFG3_SDAEN);
  316. /* reset PHY PLL clock parent */
  317. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  318. SUN8I_HDMI_PHY_PLL_CFG1_CKIN_SEL_MSK, 0);
  319. /* set HW control of CEC pins */
  320. regmap_write(phy->regs, SUN8I_HDMI_PHY_CEC_REG, 0);
  321. /* read calibration data */
  322. regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
  323. phy->rcal = (val & SUN8I_HDMI_PHY_ANA_STS_RCAL_MASK) >> 2;
  324. }
  325. void sun8i_hdmi_phy_init(struct sun8i_hdmi_phy *phy)
  326. {
  327. /* enable read access to HDMI controller */
  328. regmap_write(phy->regs, SUN8I_HDMI_PHY_READ_EN_REG,
  329. SUN8I_HDMI_PHY_READ_EN_MAGIC);
  330. /* unscramble register offsets */
  331. regmap_write(phy->regs, SUN8I_HDMI_PHY_UNSCRAMBLE_REG,
  332. SUN8I_HDMI_PHY_UNSCRAMBLE_MAGIC);
  333. phy->variant->phy_init(phy);
  334. }
  335. const struct dw_hdmi_phy_ops *sun8i_hdmi_phy_get_ops(void)
  336. {
  337. return &sun8i_hdmi_phy_ops;
  338. }
  339. static struct regmap_config sun8i_hdmi_phy_regmap_config = {
  340. .reg_bits = 32,
  341. .val_bits = 32,
  342. .reg_stride = 4,
  343. .max_register = SUN8I_HDMI_PHY_CEC_REG,
  344. .name = "phy"
  345. };
  346. static const struct sun8i_hdmi_phy_variant sun50i_a64_hdmi_phy = {
  347. .has_phy_clk = true,
  348. .phy_init = &sun8i_hdmi_phy_init_h3,
  349. .phy_disable = &sun8i_hdmi_phy_disable_h3,
  350. .phy_config = &sun8i_hdmi_phy_config_h3,
  351. };
  352. static const struct sun8i_hdmi_phy_variant sun8i_a83t_hdmi_phy = {
  353. .phy_init = &sun8i_hdmi_phy_init_a83t,
  354. .phy_disable = &sun8i_hdmi_phy_disable_a83t,
  355. .phy_config = &sun8i_hdmi_phy_config_a83t,
  356. };
  357. static const struct sun8i_hdmi_phy_variant sun8i_h3_hdmi_phy = {
  358. .has_phy_clk = true,
  359. .phy_init = &sun8i_hdmi_phy_init_h3,
  360. .phy_disable = &sun8i_hdmi_phy_disable_h3,
  361. .phy_config = &sun8i_hdmi_phy_config_h3,
  362. };
  363. static const struct sun8i_hdmi_phy_variant sun8i_r40_hdmi_phy = {
  364. .has_phy_clk = true,
  365. .has_second_pll = true,
  366. .phy_init = &sun8i_hdmi_phy_init_h3,
  367. .phy_disable = &sun8i_hdmi_phy_disable_h3,
  368. .phy_config = &sun8i_hdmi_phy_config_h3,
  369. };
  370. static const struct of_device_id sun8i_hdmi_phy_of_table[] = {
  371. {
  372. .compatible = "allwinner,sun50i-a64-hdmi-phy",
  373. .data = &sun50i_a64_hdmi_phy,
  374. },
  375. {
  376. .compatible = "allwinner,sun8i-a83t-hdmi-phy",
  377. .data = &sun8i_a83t_hdmi_phy,
  378. },
  379. {
  380. .compatible = "allwinner,sun8i-h3-hdmi-phy",
  381. .data = &sun8i_h3_hdmi_phy,
  382. },
  383. {
  384. .compatible = "allwinner,sun8i-r40-hdmi-phy",
  385. .data = &sun8i_r40_hdmi_phy,
  386. },
  387. { /* sentinel */ }
  388. };
  389. int sun8i_hdmi_phy_probe(struct sun8i_dw_hdmi *hdmi, struct device_node *node)
  390. {
  391. const struct of_device_id *match;
  392. struct device *dev = hdmi->dev;
  393. struct sun8i_hdmi_phy *phy;
  394. struct resource res;
  395. void __iomem *regs;
  396. int ret;
  397. match = of_match_node(sun8i_hdmi_phy_of_table, node);
  398. if (!match) {
  399. dev_err(dev, "Incompatible HDMI PHY\n");
  400. return -EINVAL;
  401. }
  402. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  403. if (!phy)
  404. return -ENOMEM;
  405. phy->variant = (struct sun8i_hdmi_phy_variant *)match->data;
  406. ret = of_address_to_resource(node, 0, &res);
  407. if (ret) {
  408. dev_err(dev, "phy: Couldn't get our resources\n");
  409. return ret;
  410. }
  411. regs = devm_ioremap_resource(dev, &res);
  412. if (IS_ERR(regs)) {
  413. dev_err(dev, "Couldn't map the HDMI PHY registers\n");
  414. return PTR_ERR(regs);
  415. }
  416. phy->regs = devm_regmap_init_mmio(dev, regs,
  417. &sun8i_hdmi_phy_regmap_config);
  418. if (IS_ERR(phy->regs)) {
  419. dev_err(dev, "Couldn't create the HDMI PHY regmap\n");
  420. return PTR_ERR(phy->regs);
  421. }
  422. phy->clk_bus = of_clk_get_by_name(node, "bus");
  423. if (IS_ERR(phy->clk_bus)) {
  424. dev_err(dev, "Could not get bus clock\n");
  425. return PTR_ERR(phy->clk_bus);
  426. }
  427. phy->clk_mod = of_clk_get_by_name(node, "mod");
  428. if (IS_ERR(phy->clk_mod)) {
  429. dev_err(dev, "Could not get mod clock\n");
  430. ret = PTR_ERR(phy->clk_mod);
  431. goto err_put_clk_bus;
  432. }
  433. if (phy->variant->has_phy_clk) {
  434. phy->clk_pll0 = of_clk_get_by_name(node, "pll-0");
  435. if (IS_ERR(phy->clk_pll0)) {
  436. dev_err(dev, "Could not get pll-0 clock\n");
  437. ret = PTR_ERR(phy->clk_pll0);
  438. goto err_put_clk_mod;
  439. }
  440. if (phy->variant->has_second_pll) {
  441. phy->clk_pll1 = of_clk_get_by_name(node, "pll-1");
  442. if (IS_ERR(phy->clk_pll1)) {
  443. dev_err(dev, "Could not get pll-1 clock\n");
  444. ret = PTR_ERR(phy->clk_pll1);
  445. goto err_put_clk_pll0;
  446. }
  447. }
  448. ret = sun8i_phy_clk_create(phy, dev,
  449. phy->variant->has_second_pll);
  450. if (ret) {
  451. dev_err(dev, "Couldn't create the PHY clock\n");
  452. goto err_put_clk_pll1;
  453. }
  454. clk_prepare_enable(phy->clk_phy);
  455. }
  456. phy->rst_phy = of_reset_control_get_shared(node, "phy");
  457. if (IS_ERR(phy->rst_phy)) {
  458. dev_err(dev, "Could not get phy reset control\n");
  459. ret = PTR_ERR(phy->rst_phy);
  460. goto err_disable_clk_phy;
  461. }
  462. ret = reset_control_deassert(phy->rst_phy);
  463. if (ret) {
  464. dev_err(dev, "Cannot deassert phy reset control: %d\n", ret);
  465. goto err_put_rst_phy;
  466. }
  467. ret = clk_prepare_enable(phy->clk_bus);
  468. if (ret) {
  469. dev_err(dev, "Cannot enable bus clock: %d\n", ret);
  470. goto err_deassert_rst_phy;
  471. }
  472. ret = clk_prepare_enable(phy->clk_mod);
  473. if (ret) {
  474. dev_err(dev, "Cannot enable mod clock: %d\n", ret);
  475. goto err_disable_clk_bus;
  476. }
  477. hdmi->phy = phy;
  478. return 0;
  479. err_disable_clk_bus:
  480. clk_disable_unprepare(phy->clk_bus);
  481. err_deassert_rst_phy:
  482. reset_control_assert(phy->rst_phy);
  483. err_put_rst_phy:
  484. reset_control_put(phy->rst_phy);
  485. err_disable_clk_phy:
  486. clk_disable_unprepare(phy->clk_phy);
  487. err_put_clk_pll1:
  488. clk_put(phy->clk_pll1);
  489. err_put_clk_pll0:
  490. clk_put(phy->clk_pll0);
  491. err_put_clk_mod:
  492. clk_put(phy->clk_mod);
  493. err_put_clk_bus:
  494. clk_put(phy->clk_bus);
  495. return ret;
  496. }
  497. void sun8i_hdmi_phy_remove(struct sun8i_dw_hdmi *hdmi)
  498. {
  499. struct sun8i_hdmi_phy *phy = hdmi->phy;
  500. clk_disable_unprepare(phy->clk_mod);
  501. clk_disable_unprepare(phy->clk_bus);
  502. clk_disable_unprepare(phy->clk_phy);
  503. reset_control_assert(phy->rst_phy);
  504. reset_control_put(phy->rst_phy);
  505. clk_put(phy->clk_pll0);
  506. clk_put(phy->clk_pll1);
  507. clk_put(phy->clk_mod);
  508. clk_put(phy->clk_bus);
  509. }