sun8i_hdmi_phy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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.h>
  7. #include <linux/of_platform.h>
  8. #include <linux/platform_device.h>
  9. #include "sun8i_dw_hdmi.h"
  10. /*
  11. * Address can be actually any value. Here is set to same value as
  12. * it is set in BSP driver.
  13. */
  14. #define I2C_ADDR 0x69
  15. static const struct dw_hdmi_mpll_config sun50i_h6_mpll_cfg[] = {
  16. {
  17. 30666000, {
  18. { 0x00b3, 0x0000 },
  19. { 0x2153, 0x0000 },
  20. { 0x40f3, 0x0000 },
  21. },
  22. }, {
  23. 36800000, {
  24. { 0x00b3, 0x0000 },
  25. { 0x2153, 0x0000 },
  26. { 0x40a2, 0x0001 },
  27. },
  28. }, {
  29. 46000000, {
  30. { 0x00b3, 0x0000 },
  31. { 0x2142, 0x0001 },
  32. { 0x40a2, 0x0001 },
  33. },
  34. }, {
  35. 61333000, {
  36. { 0x0072, 0x0001 },
  37. { 0x2142, 0x0001 },
  38. { 0x40a2, 0x0001 },
  39. },
  40. }, {
  41. 73600000, {
  42. { 0x0072, 0x0001 },
  43. { 0x2142, 0x0001 },
  44. { 0x4061, 0x0002 },
  45. },
  46. }, {
  47. 92000000, {
  48. { 0x0072, 0x0001 },
  49. { 0x2145, 0x0002 },
  50. { 0x4061, 0x0002 },
  51. },
  52. }, {
  53. 122666000, {
  54. { 0x0051, 0x0002 },
  55. { 0x2145, 0x0002 },
  56. { 0x4061, 0x0002 },
  57. },
  58. }, {
  59. 147200000, {
  60. { 0x0051, 0x0002 },
  61. { 0x2145, 0x0002 },
  62. { 0x4064, 0x0003 },
  63. },
  64. }, {
  65. 184000000, {
  66. { 0x0051, 0x0002 },
  67. { 0x214c, 0x0003 },
  68. { 0x4064, 0x0003 },
  69. },
  70. }, {
  71. 226666000, {
  72. { 0x0040, 0x0003 },
  73. { 0x214c, 0x0003 },
  74. { 0x4064, 0x0003 },
  75. },
  76. }, {
  77. 272000000, {
  78. { 0x0040, 0x0003 },
  79. { 0x214c, 0x0003 },
  80. { 0x5a64, 0x0003 },
  81. },
  82. }, {
  83. 340000000, {
  84. { 0x0040, 0x0003 },
  85. { 0x3b4c, 0x0003 },
  86. { 0x5a64, 0x0003 },
  87. },
  88. }, {
  89. 594000000, {
  90. { 0x1a40, 0x0003 },
  91. { 0x3b4c, 0x0003 },
  92. { 0x5a64, 0x0003 },
  93. },
  94. }, {
  95. ~0UL, {
  96. { 0x0000, 0x0000 },
  97. { 0x0000, 0x0000 },
  98. { 0x0000, 0x0000 },
  99. },
  100. }
  101. };
  102. static const struct dw_hdmi_curr_ctrl sun50i_h6_cur_ctr[] = {
  103. /* pixelclk bpp8 bpp10 bpp12 */
  104. { 27000000, { 0x0012, 0x0000, 0x0000 }, },
  105. { 74250000, { 0x0013, 0x001a, 0x001b }, },
  106. { 148500000, { 0x0019, 0x0033, 0x0034 }, },
  107. { 297000000, { 0x0019, 0x001b, 0x001b }, },
  108. { 594000000, { 0x0010, 0x001b, 0x001b }, },
  109. { ~0UL, { 0x0000, 0x0000, 0x0000 }, }
  110. };
  111. static const struct dw_hdmi_phy_config sun50i_h6_phy_config[] = {
  112. /*pixelclk symbol term vlev*/
  113. { 27000000, 0x8009, 0x0007, 0x02b0 },
  114. { 74250000, 0x8009, 0x0006, 0x022d },
  115. { 148500000, 0x8029, 0x0006, 0x0270 },
  116. { 297000000, 0x8039, 0x0005, 0x01ab },
  117. { 594000000, 0x8029, 0x0000, 0x008a },
  118. { ~0UL, 0x0000, 0x0000, 0x0000}
  119. };
  120. static void sun8i_hdmi_phy_set_polarity(struct sun8i_hdmi_phy *phy,
  121. const struct drm_display_mode *mode)
  122. {
  123. u32 val = 0;
  124. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  125. val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NHSYNC;
  126. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  127. val |= SUN8I_HDMI_PHY_DBG_CTRL_POL_NVSYNC;
  128. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  129. SUN8I_HDMI_PHY_DBG_CTRL_POL_MASK, val);
  130. };
  131. static int sun8i_a83t_hdmi_phy_config(struct dw_hdmi *hdmi, void *data,
  132. const struct drm_display_info *display,
  133. const struct drm_display_mode *mode)
  134. {
  135. unsigned int clk_rate = mode->crtc_clock * 1000;
  136. struct sun8i_hdmi_phy *phy = data;
  137. sun8i_hdmi_phy_set_polarity(phy, mode);
  138. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
  139. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN,
  140. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN);
  141. /* power down */
  142. dw_hdmi_phy_gen2_txpwron(hdmi, 0);
  143. dw_hdmi_phy_gen2_pddq(hdmi, 1);
  144. dw_hdmi_phy_gen2_reset(hdmi);
  145. dw_hdmi_phy_gen2_pddq(hdmi, 0);
  146. dw_hdmi_phy_i2c_set_addr(hdmi, I2C_ADDR);
  147. /*
  148. * Values are taken from BSP HDMI driver. Although AW didn't
  149. * release any documentation, explanation of this values can
  150. * be found in i.MX 6Dual/6Quad Reference Manual.
  151. */
  152. if (clk_rate <= 27000000) {
  153. dw_hdmi_phy_i2c_write(hdmi, 0x01e0, 0x06);
  154. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x15);
  155. dw_hdmi_phy_i2c_write(hdmi, 0x08da, 0x10);
  156. dw_hdmi_phy_i2c_write(hdmi, 0x0007, 0x19);
  157. dw_hdmi_phy_i2c_write(hdmi, 0x0318, 0x0e);
  158. dw_hdmi_phy_i2c_write(hdmi, 0x8009, 0x09);
  159. } else if (clk_rate <= 74250000) {
  160. dw_hdmi_phy_i2c_write(hdmi, 0x0540, 0x06);
  161. dw_hdmi_phy_i2c_write(hdmi, 0x0005, 0x15);
  162. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
  163. dw_hdmi_phy_i2c_write(hdmi, 0x0007, 0x19);
  164. dw_hdmi_phy_i2c_write(hdmi, 0x02b5, 0x0e);
  165. dw_hdmi_phy_i2c_write(hdmi, 0x8009, 0x09);
  166. } else if (clk_rate <= 148500000) {
  167. dw_hdmi_phy_i2c_write(hdmi, 0x04a0, 0x06);
  168. dw_hdmi_phy_i2c_write(hdmi, 0x000a, 0x15);
  169. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
  170. dw_hdmi_phy_i2c_write(hdmi, 0x0002, 0x19);
  171. dw_hdmi_phy_i2c_write(hdmi, 0x0021, 0x0e);
  172. dw_hdmi_phy_i2c_write(hdmi, 0x8029, 0x09);
  173. } else {
  174. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x06);
  175. dw_hdmi_phy_i2c_write(hdmi, 0x000f, 0x15);
  176. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x10);
  177. dw_hdmi_phy_i2c_write(hdmi, 0x0002, 0x19);
  178. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x0e);
  179. dw_hdmi_phy_i2c_write(hdmi, 0x802b, 0x09);
  180. }
  181. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x1e);
  182. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x13);
  183. dw_hdmi_phy_i2c_write(hdmi, 0x0000, 0x17);
  184. dw_hdmi_phy_gen2_txpwron(hdmi, 1);
  185. return 0;
  186. }
  187. static void sun8i_a83t_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
  188. {
  189. struct sun8i_hdmi_phy *phy = data;
  190. dw_hdmi_phy_gen2_txpwron(hdmi, 0);
  191. dw_hdmi_phy_gen2_pddq(hdmi, 1);
  192. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
  193. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN, 0);
  194. }
  195. static const struct dw_hdmi_phy_ops sun8i_a83t_hdmi_phy_ops = {
  196. .init = sun8i_a83t_hdmi_phy_config,
  197. .disable = sun8i_a83t_hdmi_phy_disable,
  198. .read_hpd = dw_hdmi_phy_read_hpd,
  199. .update_hpd = dw_hdmi_phy_update_hpd,
  200. .setup_hpd = dw_hdmi_phy_setup_hpd,
  201. };
  202. static int sun8i_h3_hdmi_phy_config(struct dw_hdmi *hdmi, void *data,
  203. const struct drm_display_info *display,
  204. const struct drm_display_mode *mode)
  205. {
  206. unsigned int clk_rate = mode->crtc_clock * 1000;
  207. struct sun8i_hdmi_phy *phy = data;
  208. u32 pll_cfg1_init;
  209. u32 pll_cfg2_init;
  210. u32 ana_cfg1_end;
  211. u32 ana_cfg2_init;
  212. u32 ana_cfg3_init;
  213. u32 b_offset = 0;
  214. u32 val;
  215. if (phy->variant->has_phy_clk)
  216. clk_set_rate(phy->clk_phy, clk_rate);
  217. sun8i_hdmi_phy_set_polarity(phy, mode);
  218. /* bandwidth / frequency independent settings */
  219. pll_cfg1_init = SUN8I_HDMI_PHY_PLL_CFG1_LDO2_EN |
  220. SUN8I_HDMI_PHY_PLL_CFG1_LDO1_EN |
  221. SUN8I_HDMI_PHY_PLL_CFG1_LDO_VSET(7) |
  222. SUN8I_HDMI_PHY_PLL_CFG1_UNKNOWN(1) |
  223. SUN8I_HDMI_PHY_PLL_CFG1_PLLDBEN |
  224. SUN8I_HDMI_PHY_PLL_CFG1_CS |
  225. SUN8I_HDMI_PHY_PLL_CFG1_CP_S(2) |
  226. SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(63) |
  227. SUN8I_HDMI_PHY_PLL_CFG1_BWS;
  228. pll_cfg2_init = SUN8I_HDMI_PHY_PLL_CFG2_SV_H |
  229. SUN8I_HDMI_PHY_PLL_CFG2_VCOGAIN_EN |
  230. SUN8I_HDMI_PHY_PLL_CFG2_SDIV2;
  231. ana_cfg1_end = SUN8I_HDMI_PHY_ANA_CFG1_REG_SVBH(1) |
  232. SUN8I_HDMI_PHY_ANA_CFG1_AMP_OPT |
  233. SUN8I_HDMI_PHY_ANA_CFG1_EMP_OPT |
  234. SUN8I_HDMI_PHY_ANA_CFG1_AMPCK_OPT |
  235. SUN8I_HDMI_PHY_ANA_CFG1_EMPCK_OPT |
  236. SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL |
  237. SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG |
  238. SUN8I_HDMI_PHY_ANA_CFG1_REG_SCKTMDS |
  239. SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN |
  240. SUN8I_HDMI_PHY_ANA_CFG1_TXEN_MASK |
  241. SUN8I_HDMI_PHY_ANA_CFG1_TXEN_ALL |
  242. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK |
  243. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
  244. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
  245. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
  246. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2 |
  247. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
  248. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
  249. SUN8I_HDMI_PHY_ANA_CFG1_CKEN |
  250. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
  251. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
  252. SUN8I_HDMI_PHY_ANA_CFG1_ENBI;
  253. ana_cfg2_init = SUN8I_HDMI_PHY_ANA_CFG2_M_EN |
  254. SUN8I_HDMI_PHY_ANA_CFG2_REG_DENCK |
  255. SUN8I_HDMI_PHY_ANA_CFG2_REG_DEN |
  256. SUN8I_HDMI_PHY_ANA_CFG2_REG_CKSS(1) |
  257. SUN8I_HDMI_PHY_ANA_CFG2_REG_CSMPS(1);
  258. ana_cfg3_init = SUN8I_HDMI_PHY_ANA_CFG3_REG_WIRE(0x3e0) |
  259. SUN8I_HDMI_PHY_ANA_CFG3_SDAEN |
  260. SUN8I_HDMI_PHY_ANA_CFG3_SCLEN;
  261. /* bandwidth / frequency dependent settings */
  262. if (clk_rate <= 27000000) {
  263. pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
  264. SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
  265. pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
  266. SUN8I_HDMI_PHY_PLL_CFG2_S(4);
  267. ana_cfg1_end |= SUN8I_HDMI_PHY_ANA_CFG1_REG_CALSW;
  268. ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4) |
  269. SUN8I_HDMI_PHY_ANA_CFG2_REG_RESDI(phy->rcal);
  270. ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(3) |
  271. SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(5);
  272. } else if (clk_rate <= 74250000) {
  273. pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
  274. SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
  275. pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
  276. SUN8I_HDMI_PHY_PLL_CFG2_S(5);
  277. ana_cfg1_end |= SUN8I_HDMI_PHY_ANA_CFG1_REG_CALSW;
  278. ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4) |
  279. SUN8I_HDMI_PHY_ANA_CFG2_REG_RESDI(phy->rcal);
  280. ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(5) |
  281. SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(7);
  282. } else if (clk_rate <= 148500000) {
  283. pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_HV_IS_33 |
  284. SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(32);
  285. pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(4) |
  286. SUN8I_HDMI_PHY_PLL_CFG2_S(6);
  287. ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSWCK |
  288. SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSW |
  289. SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(2);
  290. ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(7) |
  291. SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(9);
  292. } else {
  293. b_offset = 2;
  294. pll_cfg1_init |= SUN8I_HDMI_PHY_PLL_CFG1_CNT_INT(63);
  295. pll_cfg2_init |= SUN8I_HDMI_PHY_PLL_CFG2_VCO_S(6) |
  296. SUN8I_HDMI_PHY_PLL_CFG2_S(7);
  297. ana_cfg2_init |= SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSWCK |
  298. SUN8I_HDMI_PHY_ANA_CFG2_REG_BIGSW |
  299. SUN8I_HDMI_PHY_ANA_CFG2_REG_SLV(4);
  300. ana_cfg3_init |= SUN8I_HDMI_PHY_ANA_CFG3_REG_AMPCK(9) |
  301. SUN8I_HDMI_PHY_ANA_CFG3_REG_AMP(13) |
  302. SUN8I_HDMI_PHY_ANA_CFG3_REG_EMP(3);
  303. }
  304. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  305. SUN8I_HDMI_PHY_ANA_CFG1_TXEN_MASK, 0);
  306. /*
  307. * NOTE: We have to be careful not to overwrite PHY parent
  308. * clock selection bit and clock divider.
  309. */
  310. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  311. (u32)~SUN8I_HDMI_PHY_PLL_CFG1_CKIN_SEL_MSK,
  312. pll_cfg1_init);
  313. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG2_REG,
  314. (u32)~SUN8I_HDMI_PHY_PLL_CFG2_PREDIV_MSK,
  315. pll_cfg2_init);
  316. usleep_range(10000, 15000);
  317. regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG3_REG,
  318. SUN8I_HDMI_PHY_PLL_CFG3_SOUT_DIV2);
  319. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  320. SUN8I_HDMI_PHY_PLL_CFG1_PLLEN,
  321. SUN8I_HDMI_PHY_PLL_CFG1_PLLEN);
  322. msleep(100);
  323. /* get B value */
  324. regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
  325. val = (val & SUN8I_HDMI_PHY_ANA_STS_B_OUT_MSK) >>
  326. SUN8I_HDMI_PHY_ANA_STS_B_OUT_SHIFT;
  327. val = min(val + b_offset, (u32)0x3f);
  328. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  329. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
  330. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD,
  331. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD1 |
  332. SUN8I_HDMI_PHY_PLL_CFG1_REG_OD);
  333. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  334. SUN8I_HDMI_PHY_PLL_CFG1_B_IN_MSK,
  335. val << SUN8I_HDMI_PHY_PLL_CFG1_B_IN_SHIFT);
  336. msleep(100);
  337. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, ana_cfg1_end);
  338. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG2_REG, ana_cfg2_init);
  339. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG, ana_cfg3_init);
  340. return 0;
  341. }
  342. static void sun8i_h3_hdmi_phy_disable(struct dw_hdmi *hdmi, void *data)
  343. {
  344. struct sun8i_hdmi_phy *phy = data;
  345. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  346. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN |
  347. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS |
  348. SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
  349. regmap_write(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG, 0);
  350. }
  351. static const struct dw_hdmi_phy_ops sun8i_h3_hdmi_phy_ops = {
  352. .init = sun8i_h3_hdmi_phy_config,
  353. .disable = sun8i_h3_hdmi_phy_disable,
  354. .read_hpd = dw_hdmi_phy_read_hpd,
  355. .update_hpd = dw_hdmi_phy_update_hpd,
  356. .setup_hpd = dw_hdmi_phy_setup_hpd,
  357. };
  358. static void sun8i_hdmi_phy_unlock(struct sun8i_hdmi_phy *phy)
  359. {
  360. /* enable read access to HDMI controller */
  361. regmap_write(phy->regs, SUN8I_HDMI_PHY_READ_EN_REG,
  362. SUN8I_HDMI_PHY_READ_EN_MAGIC);
  363. /* unscramble register offsets */
  364. regmap_write(phy->regs, SUN8I_HDMI_PHY_UNSCRAMBLE_REG,
  365. SUN8I_HDMI_PHY_UNSCRAMBLE_MAGIC);
  366. }
  367. static void sun50i_hdmi_phy_init_h6(struct sun8i_hdmi_phy *phy)
  368. {
  369. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
  370. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN,
  371. SUN8I_HDMI_PHY_REXT_CTRL_REXT_EN);
  372. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_REXT_CTRL_REG,
  373. 0xffff0000, 0x80c00000);
  374. }
  375. static void sun8i_hdmi_phy_init_a83t(struct sun8i_hdmi_phy *phy)
  376. {
  377. sun8i_hdmi_phy_unlock(phy);
  378. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  379. SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK,
  380. SUN8I_HDMI_PHY_DBG_CTRL_PX_LOCK);
  381. /*
  382. * Set PHY I2C address. It must match to the address set by
  383. * dw_hdmi_phy_set_slave_addr().
  384. */
  385. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_DBG_CTRL_REG,
  386. SUN8I_HDMI_PHY_DBG_CTRL_ADDR_MASK,
  387. SUN8I_HDMI_PHY_DBG_CTRL_ADDR(I2C_ADDR));
  388. }
  389. static void sun8i_hdmi_phy_init_h3(struct sun8i_hdmi_phy *phy)
  390. {
  391. unsigned int val;
  392. sun8i_hdmi_phy_unlock(phy);
  393. regmap_write(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG, 0);
  394. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  395. SUN8I_HDMI_PHY_ANA_CFG1_ENBI,
  396. SUN8I_HDMI_PHY_ANA_CFG1_ENBI);
  397. udelay(5);
  398. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  399. SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN,
  400. SUN8I_HDMI_PHY_ANA_CFG1_TMDSCLK_EN);
  401. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  402. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS,
  403. SUN8I_HDMI_PHY_ANA_CFG1_ENVBS);
  404. usleep_range(10, 20);
  405. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  406. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN,
  407. SUN8I_HDMI_PHY_ANA_CFG1_LDOEN);
  408. udelay(5);
  409. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  410. SUN8I_HDMI_PHY_ANA_CFG1_CKEN,
  411. SUN8I_HDMI_PHY_ANA_CFG1_CKEN);
  412. usleep_range(40, 100);
  413. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  414. SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL,
  415. SUN8I_HDMI_PHY_ANA_CFG1_ENRCAL);
  416. usleep_range(100, 200);
  417. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  418. SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG,
  419. SUN8I_HDMI_PHY_ANA_CFG1_ENCALOG);
  420. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  421. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
  422. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
  423. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2,
  424. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS0 |
  425. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS1 |
  426. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDS2);
  427. /* wait for calibration to finish */
  428. regmap_read_poll_timeout(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, val,
  429. (val & SUN8I_HDMI_PHY_ANA_STS_RCALEND2D),
  430. 100, 2000);
  431. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  432. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK,
  433. SUN8I_HDMI_PHY_ANA_CFG1_ENP2S_TMDSCLK);
  434. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG1_REG,
  435. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
  436. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
  437. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
  438. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK,
  439. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS0 |
  440. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS1 |
  441. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDS2 |
  442. SUN8I_HDMI_PHY_ANA_CFG1_BIASEN_TMDSCLK);
  443. /* enable DDC communication */
  444. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_ANA_CFG3_REG,
  445. SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
  446. SUN8I_HDMI_PHY_ANA_CFG3_SDAEN,
  447. SUN8I_HDMI_PHY_ANA_CFG3_SCLEN |
  448. SUN8I_HDMI_PHY_ANA_CFG3_SDAEN);
  449. /* reset PHY PLL clock parent */
  450. regmap_update_bits(phy->regs, SUN8I_HDMI_PHY_PLL_CFG1_REG,
  451. SUN8I_HDMI_PHY_PLL_CFG1_CKIN_SEL_MSK, 0);
  452. /* set HW control of CEC pins */
  453. regmap_write(phy->regs, SUN8I_HDMI_PHY_CEC_REG, 0);
  454. /* read calibration data */
  455. regmap_read(phy->regs, SUN8I_HDMI_PHY_ANA_STS_REG, &val);
  456. phy->rcal = (val & SUN8I_HDMI_PHY_ANA_STS_RCAL_MASK) >> 2;
  457. }
  458. int sun8i_hdmi_phy_init(struct sun8i_hdmi_phy *phy)
  459. {
  460. int ret;
  461. ret = reset_control_deassert(phy->rst_phy);
  462. if (ret) {
  463. dev_err(phy->dev, "Cannot deassert phy reset control: %d\n", ret);
  464. return ret;
  465. }
  466. ret = clk_prepare_enable(phy->clk_bus);
  467. if (ret) {
  468. dev_err(phy->dev, "Cannot enable bus clock: %d\n", ret);
  469. goto err_assert_rst_phy;
  470. }
  471. ret = clk_prepare_enable(phy->clk_mod);
  472. if (ret) {
  473. dev_err(phy->dev, "Cannot enable mod clock: %d\n", ret);
  474. goto err_disable_clk_bus;
  475. }
  476. if (phy->variant->has_phy_clk) {
  477. ret = sun8i_phy_clk_create(phy, phy->dev,
  478. phy->variant->has_second_pll);
  479. if (ret) {
  480. dev_err(phy->dev, "Couldn't create the PHY clock\n");
  481. goto err_disable_clk_mod;
  482. }
  483. clk_prepare_enable(phy->clk_phy);
  484. }
  485. phy->variant->phy_init(phy);
  486. return 0;
  487. err_disable_clk_mod:
  488. clk_disable_unprepare(phy->clk_mod);
  489. err_disable_clk_bus:
  490. clk_disable_unprepare(phy->clk_bus);
  491. err_assert_rst_phy:
  492. reset_control_assert(phy->rst_phy);
  493. return ret;
  494. }
  495. void sun8i_hdmi_phy_deinit(struct sun8i_hdmi_phy *phy)
  496. {
  497. clk_disable_unprepare(phy->clk_mod);
  498. clk_disable_unprepare(phy->clk_bus);
  499. clk_disable_unprepare(phy->clk_phy);
  500. reset_control_assert(phy->rst_phy);
  501. }
  502. void sun8i_hdmi_phy_set_ops(struct sun8i_hdmi_phy *phy,
  503. struct dw_hdmi_plat_data *plat_data)
  504. {
  505. const struct sun8i_hdmi_phy_variant *variant = phy->variant;
  506. if (variant->phy_ops) {
  507. plat_data->phy_ops = variant->phy_ops;
  508. plat_data->phy_name = "sun8i_dw_hdmi_phy";
  509. plat_data->phy_data = phy;
  510. } else {
  511. plat_data->mpll_cfg = variant->mpll_cfg;
  512. plat_data->cur_ctr = variant->cur_ctr;
  513. plat_data->phy_config = variant->phy_cfg;
  514. }
  515. }
  516. static const struct regmap_config sun8i_hdmi_phy_regmap_config = {
  517. .reg_bits = 32,
  518. .val_bits = 32,
  519. .reg_stride = 4,
  520. .max_register = SUN8I_HDMI_PHY_CEC_REG,
  521. .name = "phy"
  522. };
  523. static const struct sun8i_hdmi_phy_variant sun8i_a83t_hdmi_phy = {
  524. .phy_ops = &sun8i_a83t_hdmi_phy_ops,
  525. .phy_init = &sun8i_hdmi_phy_init_a83t,
  526. };
  527. static const struct sun8i_hdmi_phy_variant sun8i_h3_hdmi_phy = {
  528. .has_phy_clk = true,
  529. .phy_ops = &sun8i_h3_hdmi_phy_ops,
  530. .phy_init = &sun8i_hdmi_phy_init_h3,
  531. };
  532. static const struct sun8i_hdmi_phy_variant sun8i_r40_hdmi_phy = {
  533. .has_phy_clk = true,
  534. .has_second_pll = true,
  535. .phy_ops = &sun8i_h3_hdmi_phy_ops,
  536. .phy_init = &sun8i_hdmi_phy_init_h3,
  537. };
  538. static const struct sun8i_hdmi_phy_variant sun50i_a64_hdmi_phy = {
  539. .has_phy_clk = true,
  540. .phy_ops = &sun8i_h3_hdmi_phy_ops,
  541. .phy_init = &sun8i_hdmi_phy_init_h3,
  542. };
  543. static const struct sun8i_hdmi_phy_variant sun50i_h6_hdmi_phy = {
  544. .cur_ctr = sun50i_h6_cur_ctr,
  545. .mpll_cfg = sun50i_h6_mpll_cfg,
  546. .phy_cfg = sun50i_h6_phy_config,
  547. .phy_init = &sun50i_hdmi_phy_init_h6,
  548. };
  549. static const struct of_device_id sun8i_hdmi_phy_of_table[] = {
  550. {
  551. .compatible = "allwinner,sun8i-a83t-hdmi-phy",
  552. .data = &sun8i_a83t_hdmi_phy,
  553. },
  554. {
  555. .compatible = "allwinner,sun8i-h3-hdmi-phy",
  556. .data = &sun8i_h3_hdmi_phy,
  557. },
  558. {
  559. .compatible = "allwinner,sun8i-r40-hdmi-phy",
  560. .data = &sun8i_r40_hdmi_phy,
  561. },
  562. {
  563. .compatible = "allwinner,sun50i-a64-hdmi-phy",
  564. .data = &sun50i_a64_hdmi_phy,
  565. },
  566. {
  567. .compatible = "allwinner,sun50i-h6-hdmi-phy",
  568. .data = &sun50i_h6_hdmi_phy,
  569. },
  570. { /* sentinel */ }
  571. };
  572. int sun8i_hdmi_phy_get(struct sun8i_dw_hdmi *hdmi, struct device_node *node)
  573. {
  574. struct platform_device *pdev = of_find_device_by_node(node);
  575. struct sun8i_hdmi_phy *phy;
  576. if (!pdev)
  577. return -EPROBE_DEFER;
  578. phy = platform_get_drvdata(pdev);
  579. if (!phy) {
  580. put_device(&pdev->dev);
  581. return -EPROBE_DEFER;
  582. }
  583. hdmi->phy = phy;
  584. put_device(&pdev->dev);
  585. return 0;
  586. }
  587. static int sun8i_hdmi_phy_probe(struct platform_device *pdev)
  588. {
  589. struct device *dev = &pdev->dev;
  590. struct sun8i_hdmi_phy *phy;
  591. void __iomem *regs;
  592. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  593. if (!phy)
  594. return -ENOMEM;
  595. phy->variant = of_device_get_match_data(dev);
  596. phy->dev = dev;
  597. regs = devm_platform_ioremap_resource(pdev, 0);
  598. if (IS_ERR(regs))
  599. return dev_err_probe(dev, PTR_ERR(regs),
  600. "Couldn't map the HDMI PHY registers\n");
  601. phy->regs = devm_regmap_init_mmio(dev, regs,
  602. &sun8i_hdmi_phy_regmap_config);
  603. if (IS_ERR(phy->regs))
  604. return dev_err_probe(dev, PTR_ERR(phy->regs),
  605. "Couldn't create the HDMI PHY regmap\n");
  606. phy->clk_bus = devm_clk_get(dev, "bus");
  607. if (IS_ERR(phy->clk_bus))
  608. return dev_err_probe(dev, PTR_ERR(phy->clk_bus),
  609. "Could not get bus clock\n");
  610. phy->clk_mod = devm_clk_get(dev, "mod");
  611. if (IS_ERR(phy->clk_mod))
  612. return dev_err_probe(dev, PTR_ERR(phy->clk_mod),
  613. "Could not get mod clock\n");
  614. if (phy->variant->has_phy_clk) {
  615. phy->clk_pll0 = devm_clk_get(dev, "pll-0");
  616. if (IS_ERR(phy->clk_pll0))
  617. return dev_err_probe(dev, PTR_ERR(phy->clk_pll0),
  618. "Could not get pll-0 clock\n");
  619. if (phy->variant->has_second_pll) {
  620. phy->clk_pll1 = devm_clk_get(dev, "pll-1");
  621. if (IS_ERR(phy->clk_pll1))
  622. return dev_err_probe(dev, PTR_ERR(phy->clk_pll1),
  623. "Could not get pll-1 clock\n");
  624. }
  625. }
  626. phy->rst_phy = devm_reset_control_get_shared(dev, "phy");
  627. if (IS_ERR(phy->rst_phy))
  628. return dev_err_probe(dev, PTR_ERR(phy->rst_phy),
  629. "Could not get phy reset control\n");
  630. platform_set_drvdata(pdev, phy);
  631. return 0;
  632. }
  633. struct platform_driver sun8i_hdmi_phy_driver = {
  634. .probe = sun8i_hdmi_phy_probe,
  635. .driver = {
  636. .name = "sun8i-hdmi-phy",
  637. .of_match_table = sun8i_hdmi_phy_of_table,
  638. },
  639. };