phy-rockchip-inno-hdmi.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2017 Rockchip Electronics Co. Ltd.
  4. *
  5. * Author: Zheng Yang <zhengyang@rock-chips.com>
  6. * Heiko Stuebner <heiko@sntech.de>
  7. */
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/delay.h>
  11. #include <linux/io.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/nvmem-consumer.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/regmap.h>
  19. #include <linux/phy/phy.h>
  20. #include <linux/slab.h>
  21. #define UPDATE(x, h, l) (((x) << (l)) & GENMASK((h), (l)))
  22. /* REG: 0x00 */
  23. #define RK3228_PRE_PLL_REFCLK_SEL_PCLK BIT(0)
  24. /* REG: 0x01 */
  25. #define RK3228_BYPASS_RXSENSE_EN BIT(2)
  26. #define RK3228_BYPASS_PWRON_EN BIT(1)
  27. #define RK3228_BYPASS_PLLPD_EN BIT(0)
  28. /* REG: 0x02 */
  29. #define RK3228_BYPASS_PDATA_EN BIT(4)
  30. #define RK3228_PDATAEN_DISABLE BIT(0)
  31. /* REG: 0x03 */
  32. #define RK3228_BYPASS_AUTO_TERM_RES_CAL BIT(7)
  33. #define RK3228_AUTO_TERM_RES_CAL_SPEED_14_8(x) UPDATE(x, 6, 0)
  34. /* REG: 0x04 */
  35. #define RK3228_AUTO_TERM_RES_CAL_SPEED_7_0(x) UPDATE(x, 7, 0)
  36. /* REG: 0xaa */
  37. #define RK3228_POST_PLL_CTRL_MANUAL BIT(0)
  38. /* REG: 0xe0 */
  39. #define RK3228_POST_PLL_POWER_DOWN BIT(5)
  40. #define RK3228_PRE_PLL_POWER_DOWN BIT(4)
  41. #define RK3228_RXSENSE_CLK_CH_ENABLE BIT(3)
  42. #define RK3228_RXSENSE_DATA_CH2_ENABLE BIT(2)
  43. #define RK3228_RXSENSE_DATA_CH1_ENABLE BIT(1)
  44. #define RK3228_RXSENSE_DATA_CH0_ENABLE BIT(0)
  45. /* REG: 0xe1 */
  46. #define RK3228_BANDGAP_ENABLE BIT(4)
  47. #define RK3228_TMDS_DRIVER_ENABLE GENMASK(3, 0)
  48. /* REG: 0xe2 */
  49. #define RK3228_PRE_PLL_FB_DIV_8_MASK BIT(7)
  50. #define RK3228_PRE_PLL_FB_DIV_8(x) UPDATE((x) >> 8, 7, 7)
  51. #define RK3228_PCLK_VCO_DIV_5_MASK BIT(5)
  52. #define RK3228_PCLK_VCO_DIV_5(x) UPDATE(x, 5, 5)
  53. #define RK3228_PRE_PLL_PRE_DIV_MASK GENMASK(4, 0)
  54. #define RK3228_PRE_PLL_PRE_DIV(x) UPDATE(x, 4, 0)
  55. /* REG: 0xe3 */
  56. #define RK3228_PRE_PLL_FB_DIV_7_0(x) UPDATE(x, 7, 0)
  57. /* REG: 0xe4 */
  58. #define RK3228_PRE_PLL_PCLK_DIV_B_MASK GENMASK(6, 5)
  59. #define RK3228_PRE_PLL_PCLK_DIV_B_SHIFT 5
  60. #define RK3228_PRE_PLL_PCLK_DIV_B(x) UPDATE(x, 6, 5)
  61. #define RK3228_PRE_PLL_PCLK_DIV_A_MASK GENMASK(4, 0)
  62. #define RK3228_PRE_PLL_PCLK_DIV_A(x) UPDATE(x, 4, 0)
  63. /* REG: 0xe5 */
  64. #define RK3228_PRE_PLL_PCLK_DIV_C_MASK GENMASK(6, 5)
  65. #define RK3228_PRE_PLL_PCLK_DIV_C(x) UPDATE(x, 6, 5)
  66. #define RK3228_PRE_PLL_PCLK_DIV_D_MASK GENMASK(4, 0)
  67. #define RK3228_PRE_PLL_PCLK_DIV_D(x) UPDATE(x, 4, 0)
  68. /* REG: 0xe6 */
  69. #define RK3228_PRE_PLL_TMDSCLK_DIV_C_MASK GENMASK(5, 4)
  70. #define RK3228_PRE_PLL_TMDSCLK_DIV_C(x) UPDATE(x, 5, 4)
  71. #define RK3228_PRE_PLL_TMDSCLK_DIV_A_MASK GENMASK(3, 2)
  72. #define RK3228_PRE_PLL_TMDSCLK_DIV_A(x) UPDATE(x, 3, 2)
  73. #define RK3228_PRE_PLL_TMDSCLK_DIV_B_MASK GENMASK(1, 0)
  74. #define RK3228_PRE_PLL_TMDSCLK_DIV_B(x) UPDATE(x, 1, 0)
  75. /* REG: 0xe8 */
  76. #define RK3228_PRE_PLL_LOCK_STATUS BIT(0)
  77. /* REG: 0xe9 */
  78. #define RK3228_POST_PLL_POST_DIV_ENABLE UPDATE(3, 7, 6)
  79. #define RK3228_POST_PLL_PRE_DIV_MASK GENMASK(4, 0)
  80. #define RK3228_POST_PLL_PRE_DIV(x) UPDATE(x, 4, 0)
  81. /* REG: 0xea */
  82. #define RK3228_POST_PLL_FB_DIV_7_0(x) UPDATE(x, 7, 0)
  83. /* REG: 0xeb */
  84. #define RK3228_POST_PLL_FB_DIV_8_MASK BIT(7)
  85. #define RK3228_POST_PLL_FB_DIV_8(x) UPDATE((x) >> 8, 7, 7)
  86. #define RK3228_POST_PLL_POST_DIV_MASK GENMASK(5, 4)
  87. #define RK3228_POST_PLL_POST_DIV(x) UPDATE(x, 5, 4)
  88. #define RK3228_POST_PLL_LOCK_STATUS BIT(0)
  89. /* REG: 0xee */
  90. #define RK3228_TMDS_CH_TA_ENABLE GENMASK(7, 4)
  91. /* REG: 0xef */
  92. #define RK3228_TMDS_CLK_CH_TA(x) UPDATE(x, 7, 6)
  93. #define RK3228_TMDS_DATA_CH2_TA(x) UPDATE(x, 5, 4)
  94. #define RK3228_TMDS_DATA_CH1_TA(x) UPDATE(x, 3, 2)
  95. #define RK3228_TMDS_DATA_CH0_TA(x) UPDATE(x, 1, 0)
  96. /* REG: 0xf0 */
  97. #define RK3228_TMDS_DATA_CH2_PRE_EMPHASIS_MASK GENMASK(5, 4)
  98. #define RK3228_TMDS_DATA_CH2_PRE_EMPHASIS(x) UPDATE(x, 5, 4)
  99. #define RK3228_TMDS_DATA_CH1_PRE_EMPHASIS_MASK GENMASK(3, 2)
  100. #define RK3228_TMDS_DATA_CH1_PRE_EMPHASIS(x) UPDATE(x, 3, 2)
  101. #define RK3228_TMDS_DATA_CH0_PRE_EMPHASIS_MASK GENMASK(1, 0)
  102. #define RK3228_TMDS_DATA_CH0_PRE_EMPHASIS(x) UPDATE(x, 1, 0)
  103. /* REG: 0xf1 */
  104. #define RK3228_TMDS_CLK_CH_OUTPUT_SWING(x) UPDATE(x, 7, 4)
  105. #define RK3228_TMDS_DATA_CH2_OUTPUT_SWING(x) UPDATE(x, 3, 0)
  106. /* REG: 0xf2 */
  107. #define RK3228_TMDS_DATA_CH1_OUTPUT_SWING(x) UPDATE(x, 7, 4)
  108. #define RK3228_TMDS_DATA_CH0_OUTPUT_SWING(x) UPDATE(x, 3, 0)
  109. /* REG: 0x01 */
  110. #define RK3328_BYPASS_RXSENSE_EN BIT(2)
  111. #define RK3328_BYPASS_POWERON_EN BIT(1)
  112. #define RK3328_BYPASS_PLLPD_EN BIT(0)
  113. /* REG: 0x02 */
  114. #define RK3328_INT_POL_HIGH BIT(7)
  115. #define RK3328_BYPASS_PDATA_EN BIT(4)
  116. #define RK3328_PDATA_EN BIT(0)
  117. /* REG:0x05 */
  118. #define RK3328_INT_TMDS_CLK(x) UPDATE(x, 7, 4)
  119. #define RK3328_INT_TMDS_D2(x) UPDATE(x, 3, 0)
  120. /* REG:0x07 */
  121. #define RK3328_INT_TMDS_D1(x) UPDATE(x, 7, 4)
  122. #define RK3328_INT_TMDS_D0(x) UPDATE(x, 3, 0)
  123. /* for all RK3328_INT_TMDS_*, ESD_DET as defined in 0xc8-0xcb */
  124. #define RK3328_INT_AGND_LOW_PULSE_LOCKED BIT(3)
  125. #define RK3328_INT_RXSENSE_LOW_PULSE_LOCKED BIT(2)
  126. #define RK3328_INT_VSS_AGND_ESD_DET BIT(1)
  127. #define RK3328_INT_AGND_VSS_ESD_DET BIT(0)
  128. /* REG: 0xa0 */
  129. #define RK3328_PCLK_VCO_DIV_5_MASK BIT(1)
  130. #define RK3328_PCLK_VCO_DIV_5(x) UPDATE(x, 1, 1)
  131. #define RK3328_PRE_PLL_POWER_DOWN BIT(0)
  132. /* REG: 0xa1 */
  133. #define RK3328_PRE_PLL_PRE_DIV_MASK GENMASK(5, 0)
  134. #define RK3328_PRE_PLL_PRE_DIV(x) UPDATE(x, 5, 0)
  135. /* REG: 0xa2 */
  136. /* unset means center spread */
  137. #define RK3328_SPREAD_SPECTRUM_MOD_DOWN BIT(7)
  138. #define RK3328_SPREAD_SPECTRUM_MOD_DISABLE BIT(6)
  139. #define RK3328_PRE_PLL_FRAC_DIV_DISABLE UPDATE(3, 5, 4)
  140. #define RK3328_PRE_PLL_FB_DIV_11_8_MASK GENMASK(3, 0)
  141. #define RK3328_PRE_PLL_FB_DIV_11_8(x) UPDATE((x) >> 8, 3, 0)
  142. /* REG: 0xa3 */
  143. #define RK3328_PRE_PLL_FB_DIV_7_0(x) UPDATE(x, 7, 0)
  144. /* REG: 0xa4*/
  145. #define RK3328_PRE_PLL_TMDSCLK_DIV_C_MASK GENMASK(1, 0)
  146. #define RK3328_PRE_PLL_TMDSCLK_DIV_C(x) UPDATE(x, 1, 0)
  147. #define RK3328_PRE_PLL_TMDSCLK_DIV_B_MASK GENMASK(3, 2)
  148. #define RK3328_PRE_PLL_TMDSCLK_DIV_B(x) UPDATE(x, 3, 2)
  149. #define RK3328_PRE_PLL_TMDSCLK_DIV_A_MASK GENMASK(5, 4)
  150. #define RK3328_PRE_PLL_TMDSCLK_DIV_A(x) UPDATE(x, 5, 4)
  151. /* REG: 0xa5 */
  152. #define RK3328_PRE_PLL_PCLK_DIV_B_SHIFT 5
  153. #define RK3328_PRE_PLL_PCLK_DIV_B_MASK GENMASK(6, 5)
  154. #define RK3328_PRE_PLL_PCLK_DIV_B(x) UPDATE(x, 6, 5)
  155. #define RK3328_PRE_PLL_PCLK_DIV_A_MASK GENMASK(4, 0)
  156. #define RK3328_PRE_PLL_PCLK_DIV_A(x) UPDATE(x, 4, 0)
  157. /* REG: 0xa6 */
  158. #define RK3328_PRE_PLL_PCLK_DIV_C_SHIFT 5
  159. #define RK3328_PRE_PLL_PCLK_DIV_C_MASK GENMASK(6, 5)
  160. #define RK3328_PRE_PLL_PCLK_DIV_C(x) UPDATE(x, 6, 5)
  161. #define RK3328_PRE_PLL_PCLK_DIV_D_MASK GENMASK(4, 0)
  162. #define RK3328_PRE_PLL_PCLK_DIV_D(x) UPDATE(x, 4, 0)
  163. /* REG: 0xa9 */
  164. #define RK3328_PRE_PLL_LOCK_STATUS BIT(0)
  165. /* REG: 0xaa */
  166. #define RK3328_POST_PLL_POST_DIV_ENABLE GENMASK(3, 2)
  167. #define RK3328_POST_PLL_REFCLK_SEL_TMDS BIT(1)
  168. #define RK3328_POST_PLL_POWER_DOWN BIT(0)
  169. /* REG:0xab */
  170. #define RK3328_POST_PLL_FB_DIV_8(x) UPDATE((x) >> 8, 7, 7)
  171. #define RK3328_POST_PLL_PRE_DIV(x) UPDATE(x, 4, 0)
  172. /* REG: 0xac */
  173. #define RK3328_POST_PLL_FB_DIV_7_0(x) UPDATE(x, 7, 0)
  174. /* REG: 0xad */
  175. #define RK3328_POST_PLL_POST_DIV_MASK GENMASK(1, 0)
  176. #define RK3328_POST_PLL_POST_DIV_2 0x0
  177. #define RK3328_POST_PLL_POST_DIV_4 0x1
  178. #define RK3328_POST_PLL_POST_DIV_8 0x3
  179. /* REG: 0xaf */
  180. #define RK3328_POST_PLL_LOCK_STATUS BIT(0)
  181. /* REG: 0xb0 */
  182. #define RK3328_BANDGAP_ENABLE BIT(2)
  183. /* REG: 0xb2 */
  184. #define RK3328_TMDS_CLK_DRIVER_EN BIT(3)
  185. #define RK3328_TMDS_D2_DRIVER_EN BIT(2)
  186. #define RK3328_TMDS_D1_DRIVER_EN BIT(1)
  187. #define RK3328_TMDS_D0_DRIVER_EN BIT(0)
  188. #define RK3328_TMDS_DRIVER_ENABLE (RK3328_TMDS_CLK_DRIVER_EN | \
  189. RK3328_TMDS_D2_DRIVER_EN | \
  190. RK3328_TMDS_D1_DRIVER_EN | \
  191. RK3328_TMDS_D0_DRIVER_EN)
  192. /* REG:0xc5 */
  193. #define RK3328_BYPASS_TERM_RESISTOR_CALIB BIT(7)
  194. #define RK3328_TERM_RESISTOR_CALIB_SPEED_14_8(x) UPDATE((x) >> 8, 6, 0)
  195. /* REG:0xc6 */
  196. #define RK3328_TERM_RESISTOR_CALIB_SPEED_7_0(x) UPDATE(x, 7, 0)
  197. /* REG:0xc7 */
  198. #define RK3328_TERM_RESISTOR_50 UPDATE(0, 2, 1)
  199. #define RK3328_TERM_RESISTOR_62_5 UPDATE(1, 2, 1)
  200. #define RK3328_TERM_RESISTOR_75 UPDATE(2, 2, 1)
  201. #define RK3328_TERM_RESISTOR_100 UPDATE(3, 2, 1)
  202. /* REG 0xc8 - 0xcb */
  203. #define RK3328_ESD_DETECT_MASK GENMASK(7, 6)
  204. #define RK3328_ESD_DETECT_340MV (0x0 << 6)
  205. #define RK3328_ESD_DETECT_280MV (0x1 << 6)
  206. #define RK3328_ESD_DETECT_260MV (0x2 << 6)
  207. #define RK3328_ESD_DETECT_240MV (0x3 << 6)
  208. /* resistors can be used in parallel */
  209. #define RK3328_TMDS_TERM_RESIST_MASK GENMASK(5, 0)
  210. #define RK3328_TMDS_TERM_RESIST_75 BIT(5)
  211. #define RK3328_TMDS_TERM_RESIST_150 BIT(4)
  212. #define RK3328_TMDS_TERM_RESIST_300 BIT(3)
  213. #define RK3328_TMDS_TERM_RESIST_600 BIT(2)
  214. #define RK3328_TMDS_TERM_RESIST_1000 BIT(1)
  215. #define RK3328_TMDS_TERM_RESIST_2000 BIT(0)
  216. /* REG: 0xd1 */
  217. #define RK3328_PRE_PLL_FRAC_DIV_23_16(x) UPDATE((x) >> 16, 7, 0)
  218. /* REG: 0xd2 */
  219. #define RK3328_PRE_PLL_FRAC_DIV_15_8(x) UPDATE((x) >> 8, 7, 0)
  220. /* REG: 0xd3 */
  221. #define RK3328_PRE_PLL_FRAC_DIV_7_0(x) UPDATE(x, 7, 0)
  222. struct inno_hdmi_phy_drv_data;
  223. struct inno_hdmi_phy {
  224. struct device *dev;
  225. struct regmap *regmap;
  226. int irq;
  227. struct phy *phy;
  228. struct clk *sysclk;
  229. struct clk *refoclk;
  230. struct clk *refpclk;
  231. /* platform data */
  232. const struct inno_hdmi_phy_drv_data *plat_data;
  233. int chip_version;
  234. /* clk provider */
  235. struct clk_hw hw;
  236. struct clk *phyclk;
  237. unsigned long pixclock;
  238. unsigned long tmdsclock;
  239. };
  240. struct pre_pll_config {
  241. unsigned long pixclock;
  242. unsigned long tmdsclock;
  243. u8 prediv;
  244. u16 fbdiv;
  245. u8 tmds_div_a;
  246. u8 tmds_div_b;
  247. u8 tmds_div_c;
  248. u8 pclk_div_a;
  249. u8 pclk_div_b;
  250. u8 pclk_div_c;
  251. u8 pclk_div_d;
  252. u8 vco_div_5_en;
  253. u32 fracdiv;
  254. };
  255. struct post_pll_config {
  256. unsigned long tmdsclock;
  257. u8 prediv;
  258. u16 fbdiv;
  259. u8 postdiv;
  260. u8 version;
  261. };
  262. struct phy_config {
  263. unsigned long tmdsclock;
  264. u8 regs[14];
  265. };
  266. struct inno_hdmi_phy_ops {
  267. int (*init)(struct inno_hdmi_phy *inno);
  268. int (*power_on)(struct inno_hdmi_phy *inno,
  269. const struct post_pll_config *cfg,
  270. const struct phy_config *phy_cfg);
  271. void (*power_off)(struct inno_hdmi_phy *inno);
  272. };
  273. struct inno_hdmi_phy_drv_data {
  274. const struct inno_hdmi_phy_ops *ops;
  275. const struct clk_ops *clk_ops;
  276. const struct phy_config *phy_cfg_table;
  277. };
  278. static const struct pre_pll_config pre_pll_cfg_table[] = {
  279. { 25175000, 25175000, 3, 125, 3, 1, 1, 1, 3, 3, 4, 0, 0xe00000},
  280. { 25175000, 31468750, 1, 41, 0, 3, 3, 1, 3, 3, 4, 0, 0xf5554f},
  281. { 27000000, 27000000, 1, 36, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  282. { 27000000, 33750000, 1, 45, 0, 3, 3, 1, 3, 3, 4, 0, 0x0},
  283. { 31500000, 31500000, 1, 42, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  284. { 31500000, 39375000, 1, 105, 1, 3, 3, 10, 0, 3, 4, 0, 0x0},
  285. { 33750000, 33750000, 1, 45, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  286. { 33750000, 42187500, 1, 169, 2, 3, 3, 15, 0, 3, 4, 0, 0x0},
  287. { 35500000, 35500000, 1, 71, 2, 2, 2, 6, 0, 3, 4, 0, 0x0},
  288. { 35500000, 44375000, 1, 74, 3, 1, 1, 25, 0, 1, 1, 0, 0x0},
  289. { 36000000, 36000000, 1, 36, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  290. { 36000000, 45000000, 1, 45, 2, 1, 1, 15, 0, 1, 1, 0, 0x0},
  291. { 40000000, 40000000, 1, 40, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  292. { 40000000, 50000000, 1, 50, 2, 1, 1, 15, 0, 1, 1, 0, 0x0},
  293. { 49500000, 49500000, 1, 66, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  294. { 49500000, 61875000, 1, 165, 1, 3, 3, 10, 0, 3, 4, 0, 0x0},
  295. { 50000000, 50000000, 1, 50, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  296. { 50000000, 62500000, 1, 125, 2, 2, 2, 15, 0, 2, 2, 0, 0x0},
  297. { 54000000, 54000000, 1, 36, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  298. { 54000000, 67500000, 1, 45, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  299. { 56250000, 56250000, 1, 75, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  300. { 56250000, 70312500, 1, 117, 3, 1, 1, 25, 0, 1, 1, 0, 0x0},
  301. { 59341000, 59341000, 1, 118, 2, 2, 2, 6, 0, 3, 4, 0, 0xae978d},
  302. { 59341000, 74176250, 2, 148, 2, 1, 1, 15, 0, 1, 1, 0, 0x5a3d70},
  303. { 59400000, 59400000, 1, 99, 3, 1, 1, 1, 3, 3, 4, 0, 0x0},
  304. { 59400000, 74250000, 1, 99, 0, 3, 3, 1, 3, 3, 4, 0, 0x0},
  305. { 65000000, 65000000, 1, 65, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  306. { 65000000, 81250000, 3, 325, 0, 3, 3, 1, 3, 3, 4, 0, 0x0},
  307. { 68250000, 68250000, 1, 91, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  308. { 68250000, 85312500, 1, 142, 3, 1, 1, 25, 0, 1, 1, 0, 0x0},
  309. { 71000000, 71000000, 1, 71, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  310. { 71000000, 88750000, 3, 355, 0, 3, 3, 1, 3, 3, 4, 0, 0x0},
  311. { 72000000, 72000000, 1, 36, 2, 0, 0, 1, 1, 2, 2, 0, 0x0},
  312. { 72000000, 90000000, 1, 60, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  313. { 73250000, 73250000, 3, 293, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  314. { 73250000, 91562500, 1, 61, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  315. { 74176000, 74176000, 1, 37, 2, 0, 0, 1, 1, 2, 2, 0, 0x16872b},
  316. { 74176000, 92720000, 2, 185, 2, 1, 1, 15, 0, 1, 1, 0, 0x70a3d7},
  317. { 74250000, 74250000, 1, 99, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  318. { 74250000, 92812500, 4, 495, 0, 3, 3, 1, 3, 3, 4, 0, 0x0},
  319. { 75000000, 75000000, 1, 50, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  320. { 75000000, 93750000, 1, 125, 0, 3, 3, 1, 3, 3, 4, 0, 0x0},
  321. { 78750000, 78750000, 1, 105, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  322. { 78750000, 98437500, 1, 164, 3, 1, 1, 25, 0, 1, 1, 0, 0x0},
  323. { 79500000, 79500000, 1, 53, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  324. { 79500000, 99375000, 1, 199, 2, 2, 2, 15, 0, 2, 2, 0, 0x0},
  325. { 83500000, 83500000, 2, 167, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  326. { 83500000, 104375000, 1, 104, 2, 1, 1, 15, 0, 1, 1, 0, 0x600000},
  327. { 85500000, 85500000, 1, 57, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  328. { 85500000, 106875000, 1, 178, 3, 1, 1, 25, 0, 1, 1, 0, 0x0},
  329. { 85750000, 85750000, 3, 343, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  330. { 85750000, 107187500, 1, 143, 0, 3, 3, 1, 3, 3, 4, 0, 0x0},
  331. { 88750000, 88750000, 3, 355, 0, 3, 3, 1, 2, 3, 4, 0, 0x0},
  332. { 88750000, 110937500, 1, 110, 2, 1, 1, 15, 0, 1, 1, 0, 0xf00000},
  333. { 94500000, 94500000, 1, 63, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  334. { 94500000, 118125000, 1, 197, 3, 1, 1, 25, 0, 1, 1, 0, 0x0},
  335. {101000000, 101000000, 1, 101, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  336. {101000000, 126250000, 1, 42, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  337. {102250000, 102250000, 4, 409, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  338. {102250000, 127812500, 1, 128, 2, 1, 1, 15, 0, 1, 1, 0, 0x0},
  339. {106500000, 106500000, 1, 71, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  340. {106500000, 133125000, 1, 133, 2, 1, 1, 15, 0, 1, 1, 0, 0x0},
  341. {108000000, 108000000, 1, 36, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  342. {108000000, 135000000, 1, 45, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  343. {115500000, 115500000, 1, 77, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  344. {115500000, 144375000, 1, 48, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  345. {117500000, 117500000, 2, 235, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  346. {117500000, 146875000, 1, 49, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  347. {119000000, 119000000, 1, 119, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  348. {119000000, 148750000, 3, 148, 0, 1, 1, 1, 3, 1, 1, 0, 0xc00000},
  349. {121750000, 121750000, 4, 487, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  350. {121750000, 152187500, 1, 203, 0, 3, 3, 1, 3, 3, 4, 0, 0x0},
  351. {122500000, 122500000, 2, 245, 2, 1, 1, 1, 1, 3, 4, 0, 0x0},
  352. {122500000, 153125000, 1, 51, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  353. {135000000, 135000000, 1, 45, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  354. {135000000, 168750000, 1, 169, 2, 1, 1, 15, 0, 1, 1, 0, 0x0},
  355. {136750000, 136750000, 1, 68, 2, 0, 0, 1, 1, 2, 2, 0, 0x600000},
  356. {136750000, 170937500, 1, 113, 0, 2, 2, 1, 3, 2, 2, 0, 0xf5554f},
  357. {140250000, 140250000, 2, 187, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  358. {140250000, 175312500, 1, 117, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  359. {146250000, 146250000, 2, 195, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  360. {146250000, 182812500, 1, 61, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  361. {148250000, 148250000, 3, 222, 2, 0, 0, 1, 1, 2, 2, 0, 0x600000},
  362. {148250000, 185312500, 1, 123, 0, 2, 2, 1, 3, 2, 2, 0, 0x8aaab0},
  363. {148352000, 148352000, 2, 148, 2, 0, 0, 1, 1, 2, 2, 0, 0x5a1cac},
  364. {148352000, 185440000, 3, 185, 0, 1, 1, 1, 3, 1, 1, 0, 0x70a3d7},
  365. {148500000, 148500000, 1, 99, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  366. {148500000, 185625000, 4, 495, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  367. {154000000, 154000000, 1, 77, 2, 0, 0, 1, 1, 2, 2, 0, 0x0},
  368. {154000000, 192500000, 1, 64, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  369. {156000000, 156000000, 1, 52, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  370. {156000000, 195000000, 1, 65, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  371. {156750000, 156750000, 2, 209, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  372. {156750000, 195937500, 1, 196, 2, 1, 1, 15, 0, 1, 1, 0, 0x0},
  373. {157000000, 157000000, 2, 157, 2, 0, 0, 1, 1, 2, 2, 0, 0x0},
  374. {157000000, 196250000, 1, 131, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  375. {157500000, 157500000, 1, 105, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  376. {157500000, 196875000, 1, 197, 2, 1, 1, 15, 0, 1, 1, 0, 0x0},
  377. {162000000, 162000000, 1, 54, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  378. {162000000, 202500000, 2, 135, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  379. {175500000, 175500000, 1, 117, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  380. {175500000, 219375000, 1, 73, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  381. {179500000, 179500000, 3, 359, 0, 2, 2, 1, 0, 3, 4, 0, 0x0},
  382. {179500000, 224375000, 1, 75, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  383. {182750000, 182750000, 1, 91, 2, 0, 0, 1, 1, 2, 2, 0, 0x600000},
  384. {182750000, 228437500, 1, 152, 0, 2, 2, 1, 3, 2, 2, 0, 0x4aaab0},
  385. {182750000, 228437500, 1, 152, 0, 2, 2, 1, 3, 2, 2, 0, 0x4aaab0},
  386. {187000000, 187000000, 2, 187, 2, 0, 0, 1, 1, 2, 2, 0, 0x0},
  387. {187000000, 233750000, 1, 39, 0, 0, 0, 1, 3, 0, 0, 1, 0x0},
  388. {187250000, 187250000, 3, 280, 2, 0, 0, 1, 1, 2, 2, 0, 0xe00000},
  389. {187250000, 234062500, 1, 156, 0, 2, 2, 1, 3, 2, 2, 0, 0xaaab0},
  390. {189000000, 189000000, 1, 63, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  391. {189000000, 236250000, 1, 79, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  392. {193250000, 193250000, 3, 289, 2, 0, 0, 1, 1, 2, 2, 0, 0xe00000},
  393. {193250000, 241562500, 1, 161, 0, 2, 2, 1, 3, 2, 2, 0, 0xaaab0},
  394. {202500000, 202500000, 2, 135, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  395. {202500000, 253125000, 1, 169, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  396. {204750000, 204750000, 4, 273, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  397. {204750000, 255937500, 1, 171, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  398. {208000000, 208000000, 1, 104, 2, 0, 0, 1, 1, 2, 2, 0, 0x0},
  399. {208000000, 260000000, 1, 173, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  400. {214750000, 214750000, 1, 107, 2, 0, 0, 1, 1, 2, 2, 0, 0x600000},
  401. {214750000, 268437500, 1, 178, 0, 2, 2, 1, 3, 2, 2, 0, 0xf5554f},
  402. {218250000, 218250000, 4, 291, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  403. {218250000, 272812500, 1, 91, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  404. {229500000, 229500000, 2, 153, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  405. {229500000, 286875000, 1, 191, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  406. {234000000, 234000000, 1, 39, 0, 0, 0, 1, 0, 1, 1, 0, 0x0},
  407. {234000000, 292500000, 1, 195, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  408. {241500000, 241500000, 2, 161, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  409. {241500000, 301875000, 1, 201, 0, 2, 2, 1, 3, 2, 2, 0, 0x0},
  410. {245250000, 245250000, 4, 327, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  411. {245250000, 306562500, 1, 51, 0, 0, 0, 1, 3, 0, 0, 1, 0x0},
  412. {245500000, 245500000, 4, 491, 2, 0, 0, 1, 1, 2, 2, 0, 0x0},
  413. {245500000, 306875000, 1, 51, 0, 0, 0, 1, 3, 0, 0, 1, 0x0},
  414. {261000000, 261000000, 1, 87, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  415. {261000000, 326250000, 1, 109, 0, 1, 1, 1, 3, 1, 1, 0, 0x0},
  416. {268250000, 268250000, 9, 402, 0, 0, 0, 1, 0, 1, 1, 0, 0x600000},
  417. {268250000, 335312500, 1, 111, 0, 1, 1, 1, 3, 1, 1, 0, 0xc5554f},
  418. {268500000, 268500000, 2, 179, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  419. {268500000, 335625000, 1, 56, 0, 0, 0, 1, 3, 0, 0, 1, 0x0},
  420. {281250000, 281250000, 4, 375, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  421. {281250000, 351562500, 1, 117, 0, 3, 1, 1, 3, 1, 1, 0, 0x0},
  422. {288000000, 288000000, 1, 48, 0, 0, 0, 1, 0, 1, 1, 0, 0x0},
  423. {288000000, 360000000, 1, 60, 0, 2, 0, 1, 3, 0, 0, 1, 0x0},
  424. {296703000, 296703000, 1, 49, 0, 0, 0, 1, 0, 1, 1, 0, 0x7353f7},
  425. {296703000, 370878750, 1, 123, 0, 3, 1, 1, 3, 1, 1, 0, 0xa051eb},
  426. {297000000, 297000000, 1, 99, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  427. {297000000, 371250000, 4, 495, 0, 3, 1, 1, 3, 1, 1, 0, 0x0},
  428. {312250000, 312250000, 9, 468, 0, 0, 0, 1, 0, 1, 1, 0, 0x600000},
  429. {312250000, 390312500, 1, 130, 0, 3, 1, 1, 3, 1, 1, 0, 0x1aaab0},
  430. {317000000, 317000000, 3, 317, 0, 1, 1, 1, 0, 2, 2, 0, 0x0},
  431. {317000000, 396250000, 1, 66, 0, 2, 0, 1, 3, 0, 0, 1, 0x0},
  432. {319750000, 319750000, 3, 159, 0, 0, 0, 1, 0, 1, 1, 0, 0xe00000},
  433. {319750000, 399687500, 3, 199, 0, 2, 0, 1, 3, 0, 0, 1, 0xd80000},
  434. {333250000, 333250000, 9, 499, 0, 0, 0, 1, 0, 1, 1, 0, 0xe00000},
  435. {333250000, 416562500, 1, 138, 0, 3, 1, 1, 3, 1, 1, 0, 0xdaaab0},
  436. {348500000, 348500000, 9, 522, 0, 2, 0, 1, 0, 1, 1, 0, 0xc00000},
  437. {348500000, 435625000, 1, 145, 0, 3, 1, 1, 3, 1, 1, 0, 0x35554f},
  438. {356500000, 356500000, 9, 534, 0, 2, 0, 1, 0, 1, 1, 0, 0xc00000},
  439. {356500000, 445625000, 1, 148, 0, 3, 1, 1, 3, 1, 1, 0, 0x8aaab0},
  440. {380500000, 380500000, 9, 570, 0, 2, 0, 1, 0, 1, 1, 0, 0xc00000},
  441. {380500000, 475625000, 1, 158, 0, 3, 1, 1, 3, 1, 1, 0, 0x8aaab0},
  442. {443250000, 443250000, 1, 73, 0, 2, 0, 1, 0, 1, 1, 0, 0xe00000},
  443. {443250000, 554062500, 1, 92, 0, 2, 0, 1, 3, 0, 0, 1, 0x580000},
  444. {505250000, 505250000, 9, 757, 0, 2, 0, 1, 0, 1, 1, 0, 0xe00000},
  445. {552750000, 552750000, 3, 276, 0, 2, 0, 1, 0, 1, 1, 0, 0x600000},
  446. {593407000, 296703500, 3, 296, 0, 1, 1, 1, 0, 1, 1, 0, 0xb41893},
  447. {593407000, 370879375, 4, 494, 0, 3, 1, 1, 3, 0, 0, 1, 0x817e4a},
  448. {593407000, 593407000, 3, 296, 0, 2, 0, 1, 0, 1, 1, 0, 0xb41893},
  449. {594000000, 297000000, 1, 99, 0, 1, 1, 1, 0, 1, 1, 0, 0x0},
  450. {594000000, 371250000, 4, 495, 0, 3, 1, 1, 3, 0, 0, 1, 0x0},
  451. {594000000, 594000000, 1, 99, 0, 2, 0, 1, 0, 1, 1, 0, 0x0},
  452. { /* sentinel */ }
  453. };
  454. static const struct post_pll_config post_pll_cfg_table[] = {
  455. {33750000, 1, 40, 8, 1},
  456. {33750000, 1, 80, 8, 2},
  457. {74250000, 1, 40, 8, 1},
  458. {74250000, 18, 80, 8, 2},
  459. {148500000, 2, 40, 4, 3},
  460. {297000000, 4, 40, 2, 3},
  461. {594000000, 8, 40, 1, 3},
  462. { /* sentinel */ }
  463. };
  464. /* phy tuning values for an undocumented set of registers */
  465. static const struct phy_config rk3228_phy_cfg[] = {
  466. { 165000000, {
  467. 0xaa, 0x00, 0x44, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00,
  468. 0x00, 0x00, 0x00, 0x00, 0x00,
  469. },
  470. }, {
  471. 340000000, {
  472. 0xaa, 0x15, 0x6a, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00,
  473. 0x00, 0x00, 0x00, 0x00, 0x00,
  474. },
  475. }, {
  476. 594000000, {
  477. 0xaa, 0x15, 0x7a, 0xaa, 0x00, 0x00, 0x00, 0x00, 0x00,
  478. 0x00, 0x00, 0x00, 0x00, 0x00,
  479. },
  480. }, { /* sentinel */ },
  481. };
  482. /* phy tuning values for an undocumented set of registers */
  483. static const struct phy_config rk3328_phy_cfg[] = {
  484. { 165000000, {
  485. 0x07, 0x0a, 0x0a, 0x0a, 0x00, 0x00, 0x08, 0x08, 0x08,
  486. 0x00, 0xac, 0xcc, 0xcc, 0xcc,
  487. },
  488. }, {
  489. 340000000, {
  490. 0x0b, 0x0d, 0x0d, 0x0d, 0x07, 0x15, 0x08, 0x08, 0x08,
  491. 0x3f, 0xac, 0xcc, 0xcd, 0xdd,
  492. },
  493. }, {
  494. 594000000, {
  495. 0x10, 0x1a, 0x1a, 0x1a, 0x07, 0x15, 0x08, 0x08, 0x08,
  496. 0x00, 0xac, 0xcc, 0xcc, 0xcc,
  497. },
  498. }, { /* sentinel */ },
  499. };
  500. static inline struct inno_hdmi_phy *to_inno_hdmi_phy(struct clk_hw *hw)
  501. {
  502. return container_of(hw, struct inno_hdmi_phy, hw);
  503. }
  504. /*
  505. * The register description of the IP block does not use any distinct names
  506. * but instead the databook simply numbers the registers in one-increments.
  507. * As the registers are obviously 32bit sized, the inno_* functions
  508. * translate the databook register names to the actual registers addresses.
  509. */
  510. static inline void inno_write(struct inno_hdmi_phy *inno, u32 reg, u8 val)
  511. {
  512. regmap_write(inno->regmap, reg * 4, val);
  513. }
  514. static inline u8 inno_read(struct inno_hdmi_phy *inno, u32 reg)
  515. {
  516. u32 val;
  517. regmap_read(inno->regmap, reg * 4, &val);
  518. return val;
  519. }
  520. static inline void inno_update_bits(struct inno_hdmi_phy *inno, u8 reg,
  521. u8 mask, u8 val)
  522. {
  523. regmap_update_bits(inno->regmap, reg * 4, mask, val);
  524. }
  525. #define inno_poll(inno, reg, val, cond, sleep_us, timeout_us) \
  526. regmap_read_poll_timeout((inno)->regmap, (reg) * 4, val, cond, \
  527. sleep_us, timeout_us)
  528. static unsigned long inno_hdmi_phy_get_tmdsclk(struct inno_hdmi_phy *inno,
  529. unsigned long rate)
  530. {
  531. int bus_width = phy_get_bus_width(inno->phy);
  532. switch (bus_width) {
  533. case 4:
  534. case 5:
  535. case 6:
  536. case 10:
  537. case 12:
  538. case 16:
  539. return (u64)rate * bus_width / 8;
  540. default:
  541. return rate;
  542. }
  543. }
  544. static irqreturn_t inno_hdmi_phy_rk3328_hardirq(int irq, void *dev_id)
  545. {
  546. struct inno_hdmi_phy *inno = dev_id;
  547. int intr_stat1, intr_stat2, intr_stat3;
  548. intr_stat1 = inno_read(inno, 0x04);
  549. intr_stat2 = inno_read(inno, 0x06);
  550. intr_stat3 = inno_read(inno, 0x08);
  551. if (intr_stat1)
  552. inno_write(inno, 0x04, intr_stat1);
  553. if (intr_stat2)
  554. inno_write(inno, 0x06, intr_stat2);
  555. if (intr_stat3)
  556. inno_write(inno, 0x08, intr_stat3);
  557. if (intr_stat1 || intr_stat2 || intr_stat3)
  558. return IRQ_WAKE_THREAD;
  559. return IRQ_HANDLED;
  560. }
  561. static irqreturn_t inno_hdmi_phy_rk3328_irq(int irq, void *dev_id)
  562. {
  563. struct inno_hdmi_phy *inno = dev_id;
  564. inno_update_bits(inno, 0x02, RK3328_PDATA_EN, 0);
  565. usleep_range(10, 20);
  566. inno_update_bits(inno, 0x02, RK3328_PDATA_EN, RK3328_PDATA_EN);
  567. return IRQ_HANDLED;
  568. }
  569. static int inno_hdmi_phy_power_on(struct phy *phy)
  570. {
  571. struct inno_hdmi_phy *inno = phy_get_drvdata(phy);
  572. const struct post_pll_config *cfg = post_pll_cfg_table;
  573. const struct phy_config *phy_cfg = inno->plat_data->phy_cfg_table;
  574. unsigned long tmdsclock = inno_hdmi_phy_get_tmdsclk(inno,
  575. inno->pixclock);
  576. int ret;
  577. if (!tmdsclock) {
  578. dev_err(inno->dev, "TMDS clock is zero!\n");
  579. return -EINVAL;
  580. }
  581. if (!inno->plat_data->ops->power_on)
  582. return -EINVAL;
  583. for (; cfg->tmdsclock != 0; cfg++)
  584. if (tmdsclock <= cfg->tmdsclock &&
  585. cfg->version & inno->chip_version)
  586. break;
  587. for (; phy_cfg->tmdsclock != 0; phy_cfg++)
  588. if (tmdsclock <= phy_cfg->tmdsclock)
  589. break;
  590. if (cfg->tmdsclock == 0 || phy_cfg->tmdsclock == 0)
  591. return -EINVAL;
  592. dev_dbg(inno->dev, "Inno HDMI PHY Power On\n");
  593. inno->plat_data->clk_ops->set_rate(&inno->hw, inno->pixclock, 24000000);
  594. ret = clk_prepare_enable(inno->phyclk);
  595. if (ret)
  596. return ret;
  597. ret = inno->plat_data->ops->power_on(inno, cfg, phy_cfg);
  598. if (ret) {
  599. clk_disable_unprepare(inno->phyclk);
  600. return ret;
  601. }
  602. return 0;
  603. }
  604. static int inno_hdmi_phy_power_off(struct phy *phy)
  605. {
  606. struct inno_hdmi_phy *inno = phy_get_drvdata(phy);
  607. if (!inno->plat_data->ops->power_off)
  608. return -EINVAL;
  609. inno->plat_data->ops->power_off(inno);
  610. clk_disable_unprepare(inno->phyclk);
  611. inno->tmdsclock = 0;
  612. dev_dbg(inno->dev, "Inno HDMI PHY Power Off\n");
  613. return 0;
  614. }
  615. static const struct phy_ops inno_hdmi_phy_ops = {
  616. .owner = THIS_MODULE,
  617. .power_on = inno_hdmi_phy_power_on,
  618. .power_off = inno_hdmi_phy_power_off,
  619. };
  620. static const
  621. struct pre_pll_config *inno_hdmi_phy_get_pre_pll_cfg(struct inno_hdmi_phy *inno,
  622. unsigned long rate)
  623. {
  624. const struct pre_pll_config *cfg = pre_pll_cfg_table;
  625. unsigned long tmdsclock = inno_hdmi_phy_get_tmdsclk(inno, rate);
  626. for (; cfg->pixclock != 0; cfg++)
  627. if (cfg->pixclock == rate && cfg->tmdsclock == tmdsclock)
  628. break;
  629. if (cfg->pixclock == 0)
  630. return ERR_PTR(-EINVAL);
  631. return cfg;
  632. }
  633. static int inno_hdmi_phy_rk3228_clk_is_prepared(struct clk_hw *hw)
  634. {
  635. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  636. u8 status;
  637. status = inno_read(inno, 0xe0) & RK3228_PRE_PLL_POWER_DOWN;
  638. return status ? 0 : 1;
  639. }
  640. static int inno_hdmi_phy_rk3228_clk_prepare(struct clk_hw *hw)
  641. {
  642. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  643. inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN, 0);
  644. return 0;
  645. }
  646. static void inno_hdmi_phy_rk3228_clk_unprepare(struct clk_hw *hw)
  647. {
  648. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  649. inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN,
  650. RK3228_PRE_PLL_POWER_DOWN);
  651. }
  652. static
  653. unsigned long inno_hdmi_phy_rk3228_clk_recalc_rate(struct clk_hw *hw,
  654. unsigned long parent_rate)
  655. {
  656. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  657. u8 nd, no_a, no_b, no_d;
  658. u64 vco;
  659. u16 nf;
  660. nd = inno_read(inno, 0xe2) & RK3228_PRE_PLL_PRE_DIV_MASK;
  661. nf = (inno_read(inno, 0xe2) & RK3228_PRE_PLL_FB_DIV_8_MASK) << 1;
  662. nf |= inno_read(inno, 0xe3);
  663. vco = parent_rate * nf;
  664. if (inno_read(inno, 0xe2) & RK3228_PCLK_VCO_DIV_5_MASK) {
  665. do_div(vco, nd * 5);
  666. } else {
  667. no_a = inno_read(inno, 0xe4) & RK3228_PRE_PLL_PCLK_DIV_A_MASK;
  668. if (!no_a)
  669. no_a = 1;
  670. no_b = inno_read(inno, 0xe4) & RK3228_PRE_PLL_PCLK_DIV_B_MASK;
  671. no_b >>= RK3228_PRE_PLL_PCLK_DIV_B_SHIFT;
  672. no_b += 2;
  673. no_d = inno_read(inno, 0xe5) & RK3228_PRE_PLL_PCLK_DIV_D_MASK;
  674. do_div(vco, (nd * (no_a == 1 ? no_b : no_a) * no_d * 2));
  675. }
  676. inno->pixclock = vco;
  677. dev_dbg(inno->dev, "%s rate %lu\n", __func__, inno->pixclock);
  678. return vco;
  679. }
  680. static long inno_hdmi_phy_rk3228_clk_round_rate(struct clk_hw *hw,
  681. unsigned long rate,
  682. unsigned long *parent_rate)
  683. {
  684. const struct pre_pll_config *cfg = pre_pll_cfg_table;
  685. rate = (rate / 1000) * 1000;
  686. for (; cfg->pixclock != 0; cfg++)
  687. if (cfg->pixclock == rate && !cfg->fracdiv)
  688. break;
  689. if (cfg->pixclock == 0)
  690. return -EINVAL;
  691. return cfg->pixclock;
  692. }
  693. static int inno_hdmi_phy_rk3228_clk_set_rate(struct clk_hw *hw,
  694. unsigned long rate,
  695. unsigned long parent_rate)
  696. {
  697. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  698. const struct pre_pll_config *cfg;
  699. unsigned long tmdsclock = inno_hdmi_phy_get_tmdsclk(inno, rate);
  700. u32 v;
  701. int ret;
  702. dev_dbg(inno->dev, "%s rate %lu tmdsclk %lu\n",
  703. __func__, rate, tmdsclock);
  704. if (inno->pixclock == rate && inno->tmdsclock == tmdsclock)
  705. return 0;
  706. cfg = inno_hdmi_phy_get_pre_pll_cfg(inno, rate);
  707. if (IS_ERR(cfg))
  708. return PTR_ERR(cfg);
  709. /* Power down PRE-PLL */
  710. inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN,
  711. RK3228_PRE_PLL_POWER_DOWN);
  712. inno_update_bits(inno, 0xe2, RK3228_PRE_PLL_FB_DIV_8_MASK |
  713. RK3228_PCLK_VCO_DIV_5_MASK |
  714. RK3228_PRE_PLL_PRE_DIV_MASK,
  715. RK3228_PRE_PLL_FB_DIV_8(cfg->fbdiv) |
  716. RK3228_PCLK_VCO_DIV_5(cfg->vco_div_5_en) |
  717. RK3228_PRE_PLL_PRE_DIV(cfg->prediv));
  718. inno_write(inno, 0xe3, RK3228_PRE_PLL_FB_DIV_7_0(cfg->fbdiv));
  719. inno_update_bits(inno, 0xe4, RK3228_PRE_PLL_PCLK_DIV_B_MASK |
  720. RK3228_PRE_PLL_PCLK_DIV_A_MASK,
  721. RK3228_PRE_PLL_PCLK_DIV_B(cfg->pclk_div_b) |
  722. RK3228_PRE_PLL_PCLK_DIV_A(cfg->pclk_div_a));
  723. inno_update_bits(inno, 0xe5, RK3228_PRE_PLL_PCLK_DIV_C_MASK |
  724. RK3228_PRE_PLL_PCLK_DIV_D_MASK,
  725. RK3228_PRE_PLL_PCLK_DIV_C(cfg->pclk_div_c) |
  726. RK3228_PRE_PLL_PCLK_DIV_D(cfg->pclk_div_d));
  727. inno_update_bits(inno, 0xe6, RK3228_PRE_PLL_TMDSCLK_DIV_C_MASK |
  728. RK3228_PRE_PLL_TMDSCLK_DIV_A_MASK |
  729. RK3228_PRE_PLL_TMDSCLK_DIV_B_MASK,
  730. RK3228_PRE_PLL_TMDSCLK_DIV_C(cfg->tmds_div_c) |
  731. RK3228_PRE_PLL_TMDSCLK_DIV_A(cfg->tmds_div_a) |
  732. RK3228_PRE_PLL_TMDSCLK_DIV_B(cfg->tmds_div_b));
  733. /* Power up PRE-PLL */
  734. inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN, 0);
  735. /* Wait for Pre-PLL lock */
  736. ret = inno_poll(inno, 0xe8, v, v & RK3228_PRE_PLL_LOCK_STATUS,
  737. 100, 100000);
  738. if (ret) {
  739. dev_err(inno->dev, "Pre-PLL locking failed\n");
  740. return ret;
  741. }
  742. inno->pixclock = rate;
  743. inno->tmdsclock = tmdsclock;
  744. return 0;
  745. }
  746. static const struct clk_ops inno_hdmi_phy_rk3228_clk_ops = {
  747. .prepare = inno_hdmi_phy_rk3228_clk_prepare,
  748. .unprepare = inno_hdmi_phy_rk3228_clk_unprepare,
  749. .is_prepared = inno_hdmi_phy_rk3228_clk_is_prepared,
  750. .recalc_rate = inno_hdmi_phy_rk3228_clk_recalc_rate,
  751. .round_rate = inno_hdmi_phy_rk3228_clk_round_rate,
  752. .set_rate = inno_hdmi_phy_rk3228_clk_set_rate,
  753. };
  754. static int inno_hdmi_phy_rk3328_clk_is_prepared(struct clk_hw *hw)
  755. {
  756. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  757. u8 status;
  758. status = inno_read(inno, 0xa0) & RK3328_PRE_PLL_POWER_DOWN;
  759. return status ? 0 : 1;
  760. }
  761. static int inno_hdmi_phy_rk3328_clk_prepare(struct clk_hw *hw)
  762. {
  763. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  764. inno_update_bits(inno, 0xa0, RK3328_PRE_PLL_POWER_DOWN, 0);
  765. return 0;
  766. }
  767. static void inno_hdmi_phy_rk3328_clk_unprepare(struct clk_hw *hw)
  768. {
  769. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  770. inno_update_bits(inno, 0xa0, RK3328_PRE_PLL_POWER_DOWN,
  771. RK3328_PRE_PLL_POWER_DOWN);
  772. }
  773. static
  774. unsigned long inno_hdmi_phy_rk3328_clk_recalc_rate(struct clk_hw *hw,
  775. unsigned long parent_rate)
  776. {
  777. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  778. unsigned long frac;
  779. u8 nd, no_a, no_b, no_d;
  780. u64 vco;
  781. u16 nf;
  782. nd = inno_read(inno, 0xa1) & RK3328_PRE_PLL_PRE_DIV_MASK;
  783. nf = ((inno_read(inno, 0xa2) & RK3328_PRE_PLL_FB_DIV_11_8_MASK) << 8);
  784. nf |= inno_read(inno, 0xa3);
  785. vco = parent_rate * nf;
  786. if (!(inno_read(inno, 0xa2) & RK3328_PRE_PLL_FRAC_DIV_DISABLE)) {
  787. frac = inno_read(inno, 0xd3) |
  788. (inno_read(inno, 0xd2) << 8) |
  789. (inno_read(inno, 0xd1) << 16);
  790. vco += DIV_ROUND_CLOSEST(parent_rate * frac, (1 << 24));
  791. }
  792. if (inno_read(inno, 0xa0) & RK3328_PCLK_VCO_DIV_5_MASK) {
  793. do_div(vco, nd * 5);
  794. } else {
  795. no_a = inno_read(inno, 0xa5) & RK3328_PRE_PLL_PCLK_DIV_A_MASK;
  796. no_b = inno_read(inno, 0xa5) & RK3328_PRE_PLL_PCLK_DIV_B_MASK;
  797. no_b >>= RK3328_PRE_PLL_PCLK_DIV_B_SHIFT;
  798. no_b += 2;
  799. no_d = inno_read(inno, 0xa6) & RK3328_PRE_PLL_PCLK_DIV_D_MASK;
  800. do_div(vco, (nd * (no_a == 1 ? no_b : no_a) * no_d * 2));
  801. }
  802. inno->pixclock = DIV_ROUND_CLOSEST((unsigned long)vco, 1000) * 1000;
  803. dev_dbg(inno->dev, "%s rate %lu vco %llu\n",
  804. __func__, inno->pixclock, vco);
  805. return inno->pixclock;
  806. }
  807. static long inno_hdmi_phy_rk3328_clk_round_rate(struct clk_hw *hw,
  808. unsigned long rate,
  809. unsigned long *parent_rate)
  810. {
  811. const struct pre_pll_config *cfg = pre_pll_cfg_table;
  812. rate = (rate / 1000) * 1000;
  813. for (; cfg->pixclock != 0; cfg++)
  814. if (cfg->pixclock == rate)
  815. break;
  816. if (cfg->pixclock == 0)
  817. return -EINVAL;
  818. return cfg->pixclock;
  819. }
  820. static int inno_hdmi_phy_rk3328_clk_set_rate(struct clk_hw *hw,
  821. unsigned long rate,
  822. unsigned long parent_rate)
  823. {
  824. struct inno_hdmi_phy *inno = to_inno_hdmi_phy(hw);
  825. const struct pre_pll_config *cfg;
  826. unsigned long tmdsclock = inno_hdmi_phy_get_tmdsclk(inno, rate);
  827. u32 val;
  828. int ret;
  829. dev_dbg(inno->dev, "%s rate %lu tmdsclk %lu\n",
  830. __func__, rate, tmdsclock);
  831. if (inno->pixclock == rate && inno->tmdsclock == tmdsclock)
  832. return 0;
  833. cfg = inno_hdmi_phy_get_pre_pll_cfg(inno, rate);
  834. if (IS_ERR(cfg))
  835. return PTR_ERR(cfg);
  836. inno_update_bits(inno, 0xa0, RK3328_PRE_PLL_POWER_DOWN,
  837. RK3328_PRE_PLL_POWER_DOWN);
  838. /* Configure pre-pll */
  839. inno_update_bits(inno, 0xa0, RK3328_PCLK_VCO_DIV_5_MASK,
  840. RK3328_PCLK_VCO_DIV_5(cfg->vco_div_5_en));
  841. inno_write(inno, 0xa1, RK3328_PRE_PLL_PRE_DIV(cfg->prediv));
  842. val = RK3328_SPREAD_SPECTRUM_MOD_DISABLE;
  843. if (!cfg->fracdiv)
  844. val |= RK3328_PRE_PLL_FRAC_DIV_DISABLE;
  845. inno_write(inno, 0xa2, RK3328_PRE_PLL_FB_DIV_11_8(cfg->fbdiv) | val);
  846. inno_write(inno, 0xa3, RK3328_PRE_PLL_FB_DIV_7_0(cfg->fbdiv));
  847. inno_write(inno, 0xa5, RK3328_PRE_PLL_PCLK_DIV_A(cfg->pclk_div_a) |
  848. RK3328_PRE_PLL_PCLK_DIV_B(cfg->pclk_div_b));
  849. inno_write(inno, 0xa6, RK3328_PRE_PLL_PCLK_DIV_C(cfg->pclk_div_c) |
  850. RK3328_PRE_PLL_PCLK_DIV_D(cfg->pclk_div_d));
  851. inno_write(inno, 0xa4, RK3328_PRE_PLL_TMDSCLK_DIV_C(cfg->tmds_div_c) |
  852. RK3328_PRE_PLL_TMDSCLK_DIV_A(cfg->tmds_div_a) |
  853. RK3328_PRE_PLL_TMDSCLK_DIV_B(cfg->tmds_div_b));
  854. inno_write(inno, 0xd3, RK3328_PRE_PLL_FRAC_DIV_7_0(cfg->fracdiv));
  855. inno_write(inno, 0xd2, RK3328_PRE_PLL_FRAC_DIV_15_8(cfg->fracdiv));
  856. inno_write(inno, 0xd1, RK3328_PRE_PLL_FRAC_DIV_23_16(cfg->fracdiv));
  857. inno_update_bits(inno, 0xa0, RK3328_PRE_PLL_POWER_DOWN, 0);
  858. /* Wait for Pre-PLL lock */
  859. ret = inno_poll(inno, 0xa9, val, val & RK3328_PRE_PLL_LOCK_STATUS,
  860. 1000, 10000);
  861. if (ret) {
  862. dev_err(inno->dev, "Pre-PLL locking failed\n");
  863. return ret;
  864. }
  865. inno->pixclock = rate;
  866. inno->tmdsclock = tmdsclock;
  867. return 0;
  868. }
  869. static const struct clk_ops inno_hdmi_phy_rk3328_clk_ops = {
  870. .prepare = inno_hdmi_phy_rk3328_clk_prepare,
  871. .unprepare = inno_hdmi_phy_rk3328_clk_unprepare,
  872. .is_prepared = inno_hdmi_phy_rk3328_clk_is_prepared,
  873. .recalc_rate = inno_hdmi_phy_rk3328_clk_recalc_rate,
  874. .round_rate = inno_hdmi_phy_rk3328_clk_round_rate,
  875. .set_rate = inno_hdmi_phy_rk3328_clk_set_rate,
  876. };
  877. static int inno_hdmi_phy_clk_register(struct inno_hdmi_phy *inno)
  878. {
  879. struct device *dev = inno->dev;
  880. struct device_node *np = dev->of_node;
  881. struct clk_init_data init;
  882. const char *parent_name;
  883. int ret;
  884. parent_name = __clk_get_name(inno->refoclk);
  885. init.parent_names = &parent_name;
  886. init.num_parents = 1;
  887. init.flags = 0;
  888. init.name = "pin_hd20_pclk";
  889. init.ops = inno->plat_data->clk_ops;
  890. /* optional override of the clock name */
  891. of_property_read_string(np, "clock-output-names", &init.name);
  892. inno->hw.init = &init;
  893. inno->phyclk = devm_clk_register(dev, &inno->hw);
  894. if (IS_ERR(inno->phyclk)) {
  895. ret = PTR_ERR(inno->phyclk);
  896. dev_err(dev, "failed to register clock: %d\n", ret);
  897. return ret;
  898. }
  899. ret = of_clk_add_provider(np, of_clk_src_simple_get, inno->phyclk);
  900. if (ret) {
  901. dev_err(dev, "failed to register clock provider: %d\n", ret);
  902. return ret;
  903. }
  904. return 0;
  905. }
  906. static int inno_hdmi_phy_rk3228_init(struct inno_hdmi_phy *inno)
  907. {
  908. /*
  909. * Use phy internal register control
  910. * rxsense/poweron/pllpd/pdataen signal.
  911. */
  912. inno_write(inno, 0x01, RK3228_BYPASS_RXSENSE_EN |
  913. RK3228_BYPASS_PWRON_EN |
  914. RK3228_BYPASS_PLLPD_EN);
  915. inno_update_bits(inno, 0x02, RK3228_BYPASS_PDATA_EN,
  916. RK3228_BYPASS_PDATA_EN);
  917. /* manual power down post-PLL */
  918. inno_update_bits(inno, 0xaa, RK3228_POST_PLL_CTRL_MANUAL,
  919. RK3228_POST_PLL_CTRL_MANUAL);
  920. inno->chip_version = 1;
  921. return 0;
  922. }
  923. static int
  924. inno_hdmi_phy_rk3228_power_on(struct inno_hdmi_phy *inno,
  925. const struct post_pll_config *cfg,
  926. const struct phy_config *phy_cfg)
  927. {
  928. int ret;
  929. u32 v;
  930. inno_update_bits(inno, 0x02, RK3228_PDATAEN_DISABLE,
  931. RK3228_PDATAEN_DISABLE);
  932. inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN |
  933. RK3228_POST_PLL_POWER_DOWN,
  934. RK3228_PRE_PLL_POWER_DOWN |
  935. RK3228_POST_PLL_POWER_DOWN);
  936. /* Post-PLL update */
  937. inno_update_bits(inno, 0xe9, RK3228_POST_PLL_PRE_DIV_MASK,
  938. RK3228_POST_PLL_PRE_DIV(cfg->prediv));
  939. inno_update_bits(inno, 0xeb, RK3228_POST_PLL_FB_DIV_8_MASK,
  940. RK3228_POST_PLL_FB_DIV_8(cfg->fbdiv));
  941. inno_write(inno, 0xea, RK3228_POST_PLL_FB_DIV_7_0(cfg->fbdiv));
  942. if (cfg->postdiv == 1) {
  943. inno_update_bits(inno, 0xe9, RK3228_POST_PLL_POST_DIV_ENABLE,
  944. 0);
  945. } else {
  946. int div = cfg->postdiv / 2 - 1;
  947. inno_update_bits(inno, 0xe9, RK3228_POST_PLL_POST_DIV_ENABLE,
  948. RK3228_POST_PLL_POST_DIV_ENABLE);
  949. inno_update_bits(inno, 0xeb, RK3228_POST_PLL_POST_DIV_MASK,
  950. RK3228_POST_PLL_POST_DIV(div));
  951. }
  952. for (v = 0; v < 4; v++)
  953. inno_write(inno, 0xef + v, phy_cfg->regs[v]);
  954. inno_update_bits(inno, 0xe0, RK3228_PRE_PLL_POWER_DOWN |
  955. RK3228_POST_PLL_POWER_DOWN, 0);
  956. inno_update_bits(inno, 0xe1, RK3228_BANDGAP_ENABLE,
  957. RK3228_BANDGAP_ENABLE);
  958. inno_update_bits(inno, 0xe1, RK3228_TMDS_DRIVER_ENABLE,
  959. RK3228_TMDS_DRIVER_ENABLE);
  960. /* Wait for post PLL lock */
  961. ret = inno_poll(inno, 0xeb, v, v & RK3228_POST_PLL_LOCK_STATUS,
  962. 100, 100000);
  963. if (ret) {
  964. dev_err(inno->dev, "Post-PLL locking failed\n");
  965. return ret;
  966. }
  967. if (cfg->tmdsclock > 340000000)
  968. msleep(100);
  969. inno_update_bits(inno, 0x02, RK3228_PDATAEN_DISABLE, 0);
  970. return 0;
  971. }
  972. static void inno_hdmi_phy_rk3228_power_off(struct inno_hdmi_phy *inno)
  973. {
  974. inno_update_bits(inno, 0xe1, RK3228_TMDS_DRIVER_ENABLE, 0);
  975. inno_update_bits(inno, 0xe1, RK3228_BANDGAP_ENABLE, 0);
  976. inno_update_bits(inno, 0xe0, RK3228_POST_PLL_POWER_DOWN,
  977. RK3228_POST_PLL_POWER_DOWN);
  978. }
  979. static const struct inno_hdmi_phy_ops rk3228_hdmi_phy_ops = {
  980. .init = inno_hdmi_phy_rk3228_init,
  981. .power_on = inno_hdmi_phy_rk3228_power_on,
  982. .power_off = inno_hdmi_phy_rk3228_power_off,
  983. };
  984. static int inno_hdmi_phy_rk3328_init(struct inno_hdmi_phy *inno)
  985. {
  986. struct nvmem_cell *cell;
  987. unsigned char *efuse_buf;
  988. size_t len;
  989. /*
  990. * Use phy internal register control
  991. * rxsense/poweron/pllpd/pdataen signal.
  992. */
  993. inno_write(inno, 0x01, RK3328_BYPASS_RXSENSE_EN |
  994. RK3328_BYPASS_POWERON_EN |
  995. RK3328_BYPASS_PLLPD_EN);
  996. inno_write(inno, 0x02, RK3328_INT_POL_HIGH | RK3328_BYPASS_PDATA_EN |
  997. RK3328_PDATA_EN);
  998. /* Disable phy irq */
  999. inno_write(inno, 0x05, 0);
  1000. inno_write(inno, 0x07, 0);
  1001. /* try to read the chip-version */
  1002. inno->chip_version = 1;
  1003. cell = nvmem_cell_get(inno->dev, "cpu-version");
  1004. if (IS_ERR(cell)) {
  1005. if (PTR_ERR(cell) == -EPROBE_DEFER)
  1006. return -EPROBE_DEFER;
  1007. return 0;
  1008. }
  1009. efuse_buf = nvmem_cell_read(cell, &len);
  1010. nvmem_cell_put(cell);
  1011. if (IS_ERR(efuse_buf))
  1012. return 0;
  1013. if (len == 1)
  1014. inno->chip_version = efuse_buf[0] + 1;
  1015. kfree(efuse_buf);
  1016. return 0;
  1017. }
  1018. static int
  1019. inno_hdmi_phy_rk3328_power_on(struct inno_hdmi_phy *inno,
  1020. const struct post_pll_config *cfg,
  1021. const struct phy_config *phy_cfg)
  1022. {
  1023. int ret;
  1024. u32 v;
  1025. inno_update_bits(inno, 0x02, RK3328_PDATA_EN, 0);
  1026. inno_update_bits(inno, 0xaa, RK3328_POST_PLL_POWER_DOWN,
  1027. RK3328_POST_PLL_POWER_DOWN);
  1028. inno_write(inno, 0xac, RK3328_POST_PLL_FB_DIV_7_0(cfg->fbdiv));
  1029. if (cfg->postdiv == 1) {
  1030. inno_write(inno, 0xab, RK3328_POST_PLL_FB_DIV_8(cfg->fbdiv) |
  1031. RK3328_POST_PLL_PRE_DIV(cfg->prediv));
  1032. inno_write(inno, 0xaa, RK3328_POST_PLL_REFCLK_SEL_TMDS |
  1033. RK3328_POST_PLL_POWER_DOWN);
  1034. } else {
  1035. v = (cfg->postdiv / 2) - 1;
  1036. v &= RK3328_POST_PLL_POST_DIV_MASK;
  1037. inno_write(inno, 0xad, v);
  1038. inno_write(inno, 0xab, RK3328_POST_PLL_FB_DIV_8(cfg->fbdiv) |
  1039. RK3328_POST_PLL_PRE_DIV(cfg->prediv));
  1040. inno_write(inno, 0xaa, RK3328_POST_PLL_POST_DIV_ENABLE |
  1041. RK3328_POST_PLL_REFCLK_SEL_TMDS |
  1042. RK3328_POST_PLL_POWER_DOWN);
  1043. }
  1044. for (v = 0; v < 14; v++)
  1045. inno_write(inno, 0xb5 + v, phy_cfg->regs[v]);
  1046. /* set ESD detection threshold for TMDS CLK, D2, D1 and D0 */
  1047. for (v = 0; v < 4; v++)
  1048. inno_update_bits(inno, 0xc8 + v, RK3328_ESD_DETECT_MASK,
  1049. RK3328_ESD_DETECT_340MV);
  1050. if (phy_cfg->tmdsclock > 340000000) {
  1051. /* Set termination resistor to 100ohm */
  1052. v = clk_get_rate(inno->sysclk) / 100000;
  1053. inno_write(inno, 0xc5, RK3328_TERM_RESISTOR_CALIB_SPEED_14_8(v)
  1054. | RK3328_BYPASS_TERM_RESISTOR_CALIB);
  1055. inno_write(inno, 0xc6, RK3328_TERM_RESISTOR_CALIB_SPEED_7_0(v));
  1056. inno_write(inno, 0xc7, RK3328_TERM_RESISTOR_100);
  1057. inno_update_bits(inno, 0xc5,
  1058. RK3328_BYPASS_TERM_RESISTOR_CALIB, 0);
  1059. } else {
  1060. inno_write(inno, 0xc5, RK3328_BYPASS_TERM_RESISTOR_CALIB);
  1061. /* clk termination resistor is 50ohm (parallel resistors) */
  1062. if (phy_cfg->tmdsclock > 165000000)
  1063. inno_update_bits(inno, 0xc8,
  1064. RK3328_TMDS_TERM_RESIST_MASK,
  1065. RK3328_TMDS_TERM_RESIST_75 |
  1066. RK3328_TMDS_TERM_RESIST_150);
  1067. /* data termination resistor for D2, D1 and D0 is 150ohm */
  1068. for (v = 0; v < 3; v++)
  1069. inno_update_bits(inno, 0xc9 + v,
  1070. RK3328_TMDS_TERM_RESIST_MASK,
  1071. RK3328_TMDS_TERM_RESIST_150);
  1072. }
  1073. inno_update_bits(inno, 0xaa, RK3328_POST_PLL_POWER_DOWN, 0);
  1074. inno_update_bits(inno, 0xb0, RK3328_BANDGAP_ENABLE,
  1075. RK3328_BANDGAP_ENABLE);
  1076. inno_update_bits(inno, 0xb2, RK3328_TMDS_DRIVER_ENABLE,
  1077. RK3328_TMDS_DRIVER_ENABLE);
  1078. /* Wait for post PLL lock */
  1079. ret = inno_poll(inno, 0xaf, v, v & RK3328_POST_PLL_LOCK_STATUS,
  1080. 1000, 10000);
  1081. if (ret) {
  1082. dev_err(inno->dev, "Post-PLL locking failed\n");
  1083. return ret;
  1084. }
  1085. if (phy_cfg->tmdsclock > 340000000)
  1086. msleep(100);
  1087. inno_update_bits(inno, 0x02, RK3328_PDATA_EN, RK3328_PDATA_EN);
  1088. /* Enable PHY IRQ */
  1089. inno_write(inno, 0x05, RK3328_INT_TMDS_CLK(RK3328_INT_VSS_AGND_ESD_DET)
  1090. | RK3328_INT_TMDS_D2(RK3328_INT_VSS_AGND_ESD_DET));
  1091. inno_write(inno, 0x07, RK3328_INT_TMDS_D1(RK3328_INT_VSS_AGND_ESD_DET)
  1092. | RK3328_INT_TMDS_D0(RK3328_INT_VSS_AGND_ESD_DET));
  1093. return 0;
  1094. }
  1095. static void inno_hdmi_phy_rk3328_power_off(struct inno_hdmi_phy *inno)
  1096. {
  1097. inno_update_bits(inno, 0xb2, RK3328_TMDS_DRIVER_ENABLE, 0);
  1098. inno_update_bits(inno, 0xb0, RK3328_BANDGAP_ENABLE, 0);
  1099. inno_update_bits(inno, 0xaa, RK3328_POST_PLL_POWER_DOWN,
  1100. RK3328_POST_PLL_POWER_DOWN);
  1101. /* Disable PHY IRQ */
  1102. inno_write(inno, 0x05, 0);
  1103. inno_write(inno, 0x07, 0);
  1104. }
  1105. static const struct inno_hdmi_phy_ops rk3328_hdmi_phy_ops = {
  1106. .init = inno_hdmi_phy_rk3328_init,
  1107. .power_on = inno_hdmi_phy_rk3328_power_on,
  1108. .power_off = inno_hdmi_phy_rk3328_power_off,
  1109. };
  1110. static const struct inno_hdmi_phy_drv_data rk3228_hdmi_phy_drv_data = {
  1111. .ops = &rk3228_hdmi_phy_ops,
  1112. .clk_ops = &inno_hdmi_phy_rk3228_clk_ops,
  1113. .phy_cfg_table = rk3228_phy_cfg,
  1114. };
  1115. static const struct inno_hdmi_phy_drv_data rk3328_hdmi_phy_drv_data = {
  1116. .ops = &rk3328_hdmi_phy_ops,
  1117. .clk_ops = &inno_hdmi_phy_rk3328_clk_ops,
  1118. .phy_cfg_table = rk3328_phy_cfg,
  1119. };
  1120. static const struct regmap_config inno_hdmi_phy_regmap_config = {
  1121. .reg_bits = 32,
  1122. .val_bits = 32,
  1123. .reg_stride = 4,
  1124. .max_register = 0x400,
  1125. };
  1126. static void inno_hdmi_phy_action(void *data)
  1127. {
  1128. struct inno_hdmi_phy *inno = data;
  1129. clk_disable_unprepare(inno->refpclk);
  1130. clk_disable_unprepare(inno->sysclk);
  1131. }
  1132. static int inno_hdmi_phy_probe(struct platform_device *pdev)
  1133. {
  1134. struct inno_hdmi_phy *inno;
  1135. struct phy_provider *phy_provider;
  1136. void __iomem *regs;
  1137. int ret;
  1138. inno = devm_kzalloc(&pdev->dev, sizeof(*inno), GFP_KERNEL);
  1139. if (!inno)
  1140. return -ENOMEM;
  1141. inno->dev = &pdev->dev;
  1142. inno->plat_data = of_device_get_match_data(inno->dev);
  1143. if (!inno->plat_data || !inno->plat_data->ops)
  1144. return -EINVAL;
  1145. regs = devm_platform_ioremap_resource(pdev, 0);
  1146. if (IS_ERR(regs))
  1147. return PTR_ERR(regs);
  1148. inno->sysclk = devm_clk_get(inno->dev, "sysclk");
  1149. if (IS_ERR(inno->sysclk)) {
  1150. ret = PTR_ERR(inno->sysclk);
  1151. dev_err(inno->dev, "failed to get sysclk: %d\n", ret);
  1152. return ret;
  1153. }
  1154. inno->refpclk = devm_clk_get(inno->dev, "refpclk");
  1155. if (IS_ERR(inno->refpclk)) {
  1156. ret = PTR_ERR(inno->refpclk);
  1157. dev_err(inno->dev, "failed to get ref clock: %d\n", ret);
  1158. return ret;
  1159. }
  1160. inno->refoclk = devm_clk_get(inno->dev, "refoclk");
  1161. if (IS_ERR(inno->refoclk)) {
  1162. ret = PTR_ERR(inno->refoclk);
  1163. dev_err(inno->dev, "failed to get oscillator-ref clock: %d\n",
  1164. ret);
  1165. return ret;
  1166. }
  1167. ret = clk_prepare_enable(inno->sysclk);
  1168. if (ret) {
  1169. dev_err(inno->dev, "Cannot enable inno phy sysclk: %d\n", ret);
  1170. return ret;
  1171. }
  1172. /*
  1173. * Refpclk needs to be on, on at least the rk3328 for still
  1174. * unknown reasons.
  1175. */
  1176. ret = clk_prepare_enable(inno->refpclk);
  1177. if (ret) {
  1178. dev_err(inno->dev, "failed to enable refpclk\n");
  1179. clk_disable_unprepare(inno->sysclk);
  1180. return ret;
  1181. }
  1182. ret = devm_add_action_or_reset(inno->dev, inno_hdmi_phy_action,
  1183. inno);
  1184. if (ret)
  1185. return ret;
  1186. inno->regmap = devm_regmap_init_mmio(inno->dev, regs,
  1187. &inno_hdmi_phy_regmap_config);
  1188. if (IS_ERR(inno->regmap))
  1189. return PTR_ERR(inno->regmap);
  1190. /* only the newer rk3328 hdmiphy has an interrupt */
  1191. inno->irq = platform_get_irq(pdev, 0);
  1192. if (inno->irq > 0) {
  1193. ret = devm_request_threaded_irq(inno->dev, inno->irq,
  1194. inno_hdmi_phy_rk3328_hardirq,
  1195. inno_hdmi_phy_rk3328_irq,
  1196. IRQF_SHARED,
  1197. dev_name(inno->dev), inno);
  1198. if (ret)
  1199. return ret;
  1200. }
  1201. inno->phy = devm_phy_create(inno->dev, NULL, &inno_hdmi_phy_ops);
  1202. if (IS_ERR(inno->phy)) {
  1203. dev_err(inno->dev, "failed to create HDMI PHY\n");
  1204. return PTR_ERR(inno->phy);
  1205. }
  1206. phy_set_drvdata(inno->phy, inno);
  1207. phy_set_bus_width(inno->phy, 8);
  1208. if (inno->plat_data->ops->init) {
  1209. ret = inno->plat_data->ops->init(inno);
  1210. if (ret)
  1211. return ret;
  1212. }
  1213. ret = inno_hdmi_phy_clk_register(inno);
  1214. if (ret)
  1215. return ret;
  1216. phy_provider = devm_of_phy_provider_register(inno->dev,
  1217. of_phy_simple_xlate);
  1218. return PTR_ERR_OR_ZERO(phy_provider);
  1219. }
  1220. static void inno_hdmi_phy_remove(struct platform_device *pdev)
  1221. {
  1222. of_clk_del_provider(pdev->dev.of_node);
  1223. }
  1224. static const struct of_device_id inno_hdmi_phy_of_match[] = {
  1225. {
  1226. .compatible = "rockchip,rk3228-hdmi-phy",
  1227. .data = &rk3228_hdmi_phy_drv_data
  1228. }, {
  1229. .compatible = "rockchip,rk3328-hdmi-phy",
  1230. .data = &rk3328_hdmi_phy_drv_data
  1231. }, { /* sentinel */ }
  1232. };
  1233. MODULE_DEVICE_TABLE(of, inno_hdmi_phy_of_match);
  1234. static struct platform_driver inno_hdmi_phy_driver = {
  1235. .probe = inno_hdmi_phy_probe,
  1236. .remove_new = inno_hdmi_phy_remove,
  1237. .driver = {
  1238. .name = "inno-hdmi-phy",
  1239. .of_match_table = inno_hdmi_phy_of_match,
  1240. },
  1241. };
  1242. module_platform_driver(inno_hdmi_phy_driver);
  1243. MODULE_AUTHOR("Zheng Yang <zhengyang@rock-chips.com>");
  1244. MODULE_DESCRIPTION("Innosilion HDMI 2.0 Transmitter PHY Driver");
  1245. MODULE_LICENSE("GPL v2");