phy-fsl-samsung-hdmi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2020 NXP
  4. * Copyright 2022 Pengutronix, Lucas Stach <kernel@pengutronix.de>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/bits.h>
  8. #include <linux/clk.h>
  9. #include <linux/clk-provider.h>
  10. #include <linux/delay.h>
  11. #include <linux/iopoll.h>
  12. #include <linux/module.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/pm_runtime.h>
  15. #define PHY_REG(reg) (reg * 4)
  16. #define REG01_PMS_P_MASK GENMASK(3, 0)
  17. #define REG03_PMS_S_MASK GENMASK(7, 4)
  18. #define REG12_CK_DIV_MASK GENMASK(5, 4)
  19. #define REG13_TG_CODE_LOW_MASK GENMASK(7, 0)
  20. #define REG14_TOL_MASK GENMASK(7, 4)
  21. #define REG14_RP_CODE_MASK GENMASK(3, 1)
  22. #define REG14_TG_CODE_HIGH_MASK GENMASK(0, 0)
  23. #define REG21_SEL_TX_CK_INV BIT(7)
  24. #define REG21_PMS_S_MASK GENMASK(3, 0)
  25. /*
  26. * REG33 does not match the ref manual. According to Sandor Yu from NXP,
  27. * "There is a doc issue on the i.MX8MP latest RM"
  28. * REG33 is being used per guidance from Sandor
  29. */
  30. #define REG33_MODE_SET_DONE BIT(7)
  31. #define REG33_FIX_DA BIT(1)
  32. #define REG34_PHY_READY BIT(7)
  33. #define REG34_PLL_LOCK BIT(6)
  34. #define REG34_PHY_CLK_READY BIT(5)
  35. #ifndef MHZ
  36. #define MHZ (1000UL * 1000UL)
  37. #endif
  38. #define PHY_PLL_DIV_REGS_NUM 7
  39. struct phy_config {
  40. u32 pixclk;
  41. u8 pll_div_regs[PHY_PLL_DIV_REGS_NUM];
  42. };
  43. /*
  44. * The calculated_phy_pll_cfg only handles integer divider for PMS,
  45. * meaning the last four entries will be fixed, but the first three will
  46. * be calculated by the PMS calculator.
  47. */
  48. static struct phy_config calculated_phy_pll_cfg = {
  49. .pixclk = 0,
  50. .pll_div_regs = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x00 },
  51. };
  52. /* The lookup table contains values for which the fractional divder is used */
  53. static const struct phy_config phy_pll_cfg[] = {
  54. {
  55. .pixclk = 22250000,
  56. .pll_div_regs = { 0xd1, 0x4b, 0xf1, 0x89, 0x88, 0x80, 0x40 },
  57. }, {
  58. .pixclk = 23750000,
  59. .pll_div_regs = { 0xd1, 0x50, 0xf1, 0x86, 0x85, 0x80, 0x40 },
  60. }, {
  61. .pixclk = 24024000,
  62. .pll_div_regs = { 0xd1, 0x50, 0xf1, 0x99, 0x02, 0x80, 0x40 },
  63. }, {
  64. .pixclk = 25175000,
  65. .pll_div_regs = { 0xd1, 0x54, 0xfc, 0xcc, 0x91, 0x80, 0x40 },
  66. }, {
  67. .pixclk = 26750000,
  68. .pll_div_regs = { 0xd1, 0x5a, 0xf2, 0x89, 0x88, 0x80, 0x40 },
  69. }, {
  70. .pixclk = 27027000,
  71. .pll_div_regs = { 0xd1, 0x5a, 0xf2, 0xfd, 0x0c, 0x80, 0x40 },
  72. }, {
  73. .pixclk = 29500000,
  74. .pll_div_regs = { 0xd1, 0x62, 0xf4, 0x95, 0x08, 0x80, 0x40 },
  75. }, {
  76. .pixclk = 30750000,
  77. .pll_div_regs = { 0xd1, 0x66, 0xf4, 0x82, 0x01, 0x88, 0x45 },
  78. }, {
  79. .pixclk = 30888000,
  80. .pll_div_regs = { 0xd1, 0x66, 0xf4, 0x99, 0x18, 0x88, 0x45 },
  81. }, {
  82. .pixclk = 33750000,
  83. .pll_div_regs = { 0xd1, 0x70, 0xf4, 0x82, 0x01, 0x80, 0x40 },
  84. }, {
  85. .pixclk = 35000000,
  86. .pll_div_regs = { 0xd1, 0x58, 0xb8, 0x8b, 0x88, 0x80, 0x40 },
  87. }, {
  88. .pixclk = 36036000,
  89. .pll_div_regs = { 0xd1, 0x5a, 0xb2, 0xfd, 0x0c, 0x80, 0x40 },
  90. }, {
  91. .pixclk = 43243200,
  92. .pll_div_regs = { 0xd1, 0x5a, 0x92, 0xfd, 0x0c, 0x80, 0x40 },
  93. }, {
  94. .pixclk = 44500000,
  95. .pll_div_regs = { 0xd1, 0x5c, 0x92, 0x98, 0x11, 0x84, 0x41 },
  96. }, {
  97. .pixclk = 47000000,
  98. .pll_div_regs = { 0xd1, 0x62, 0x94, 0x95, 0x82, 0x80, 0x40 },
  99. }, {
  100. .pixclk = 47500000,
  101. .pll_div_regs = { 0xd1, 0x63, 0x96, 0xa1, 0x82, 0x80, 0x40 },
  102. }, {
  103. .pixclk = 50349650,
  104. .pll_div_regs = { 0xd1, 0x54, 0x7c, 0xc3, 0x8f, 0x80, 0x40 },
  105. }, {
  106. .pixclk = 53250000,
  107. .pll_div_regs = { 0xd1, 0x58, 0x72, 0x84, 0x03, 0x82, 0x41 },
  108. }, {
  109. .pixclk = 53500000,
  110. .pll_div_regs = { 0xd1, 0x5a, 0x72, 0x89, 0x88, 0x80, 0x40 },
  111. }, {
  112. .pixclk = 54054000,
  113. .pll_div_regs = { 0xd1, 0x5a, 0x72, 0xfd, 0x0c, 0x80, 0x40 },
  114. }, {
  115. .pixclk = 59000000,
  116. .pll_div_regs = { 0xd1, 0x62, 0x74, 0x95, 0x08, 0x80, 0x40 },
  117. }, {
  118. .pixclk = 59340659,
  119. .pll_div_regs = { 0xd1, 0x62, 0x74, 0xdb, 0x52, 0x88, 0x47 },
  120. }, {
  121. .pixclk = 61500000,
  122. .pll_div_regs = { 0xd1, 0x66, 0x74, 0x82, 0x01, 0x88, 0x45 },
  123. }, {
  124. .pixclk = 63500000,
  125. .pll_div_regs = { 0xd1, 0x69, 0x74, 0x89, 0x08, 0x80, 0x40 },
  126. }, {
  127. .pixclk = 67500000,
  128. .pll_div_regs = { 0xd1, 0x54, 0x52, 0x87, 0x03, 0x80, 0x40 },
  129. }, {
  130. .pixclk = 70000000,
  131. .pll_div_regs = { 0xd1, 0x58, 0x58, 0x8b, 0x88, 0x80, 0x40 },
  132. }, {
  133. .pixclk = 72072000,
  134. .pll_div_regs = { 0xd1, 0x5a, 0x52, 0xfd, 0x0c, 0x80, 0x40 },
  135. }, {
  136. .pixclk = 74176000,
  137. .pll_div_regs = { 0xd1, 0x5d, 0x58, 0xdb, 0xA2, 0x88, 0x41 },
  138. }, {
  139. .pixclk = 74250000,
  140. .pll_div_regs = { 0xd1, 0x5c, 0x52, 0x90, 0x0d, 0x84, 0x41 },
  141. }, {
  142. .pixclk = 78500000,
  143. .pll_div_regs = { 0xd1, 0x62, 0x54, 0x87, 0x01, 0x80, 0x40 },
  144. }, {
  145. .pixclk = 82000000,
  146. .pll_div_regs = { 0xd1, 0x66, 0x54, 0x82, 0x01, 0x88, 0x45 },
  147. }, {
  148. .pixclk = 82500000,
  149. .pll_div_regs = { 0xd1, 0x67, 0x54, 0x88, 0x01, 0x90, 0x49 },
  150. }, {
  151. .pixclk = 89000000,
  152. .pll_div_regs = { 0xd1, 0x70, 0x54, 0x84, 0x83, 0x80, 0x40 },
  153. }, {
  154. .pixclk = 90000000,
  155. .pll_div_regs = { 0xd1, 0x70, 0x54, 0x82, 0x01, 0x80, 0x40 },
  156. }, {
  157. .pixclk = 94000000,
  158. .pll_div_regs = { 0xd1, 0x4e, 0x32, 0xa7, 0x10, 0x80, 0x40 },
  159. }, {
  160. .pixclk = 95000000,
  161. .pll_div_regs = { 0xd1, 0x50, 0x31, 0x86, 0x85, 0x80, 0x40 },
  162. }, {
  163. .pixclk = 98901099,
  164. .pll_div_regs = { 0xd1, 0x52, 0x3a, 0xdb, 0x4c, 0x88, 0x47 },
  165. }, {
  166. .pixclk = 99000000,
  167. .pll_div_regs = { 0xd1, 0x52, 0x32, 0x82, 0x01, 0x88, 0x47 },
  168. }, {
  169. .pixclk = 100699300,
  170. .pll_div_regs = { 0xd1, 0x54, 0x3c, 0xc3, 0x8f, 0x80, 0x40 },
  171. }, {
  172. .pixclk = 102500000,
  173. .pll_div_regs = { 0xd1, 0x55, 0x32, 0x8c, 0x05, 0x90, 0x4b },
  174. }, {
  175. .pixclk = 104750000,
  176. .pll_div_regs = { 0xd1, 0x57, 0x32, 0x98, 0x07, 0x90, 0x49 },
  177. }, {
  178. .pixclk = 106500000,
  179. .pll_div_regs = { 0xd1, 0x58, 0x32, 0x84, 0x03, 0x82, 0x41 },
  180. }, {
  181. .pixclk = 107000000,
  182. .pll_div_regs = { 0xd1, 0x5a, 0x32, 0x89, 0x88, 0x80, 0x40 },
  183. }, {
  184. .pixclk = 108108000,
  185. .pll_div_regs = { 0xd1, 0x5a, 0x32, 0xfd, 0x0c, 0x80, 0x40 },
  186. }, {
  187. .pixclk = 118000000,
  188. .pll_div_regs = { 0xd1, 0x62, 0x34, 0x95, 0x08, 0x80, 0x40 },
  189. }, {
  190. .pixclk = 123000000,
  191. .pll_div_regs = { 0xd1, 0x66, 0x34, 0x82, 0x01, 0x88, 0x45 },
  192. }, {
  193. .pixclk = 127000000,
  194. .pll_div_regs = { 0xd1, 0x69, 0x34, 0x89, 0x08, 0x80, 0x40 },
  195. }, {
  196. .pixclk = 135000000,
  197. .pll_div_regs = { 0xd1, 0x70, 0x34, 0x82, 0x01, 0x80, 0x40 },
  198. }, {
  199. .pixclk = 135580000,
  200. .pll_div_regs = { 0xd1, 0x71, 0x39, 0xe9, 0x82, 0x9c, 0x5b },
  201. }, {
  202. .pixclk = 137520000,
  203. .pll_div_regs = { 0xd1, 0x72, 0x38, 0x99, 0x10, 0x85, 0x41 },
  204. }, {
  205. .pixclk = 138750000,
  206. .pll_div_regs = { 0xd1, 0x73, 0x35, 0x88, 0x05, 0x90, 0x4d },
  207. }, {
  208. .pixclk = 140000000,
  209. .pll_div_regs = { 0xd1, 0x75, 0x36, 0xa7, 0x90, 0x80, 0x40 },
  210. }, {
  211. .pixclk = 148352000,
  212. .pll_div_regs = { 0xd1, 0x7b, 0x35, 0xdb, 0x39, 0x90, 0x45 },
  213. }, {
  214. .pixclk = 148500000,
  215. .pll_div_regs = { 0xd1, 0x7b, 0x35, 0x84, 0x03, 0x90, 0x45 },
  216. }, {
  217. .pixclk = 154000000,
  218. .pll_div_regs = { 0xd1, 0x40, 0x18, 0x83, 0x01, 0x00, 0x40 },
  219. }, {
  220. .pixclk = 157000000,
  221. .pll_div_regs = { 0xd1, 0x41, 0x11, 0xa7, 0x14, 0x80, 0x40 },
  222. }, {
  223. .pixclk = 160000000,
  224. .pll_div_regs = { 0xd1, 0x42, 0x12, 0xa1, 0x20, 0x80, 0x40 },
  225. }, {
  226. .pixclk = 162000000,
  227. .pll_div_regs = { 0xd1, 0x43, 0x18, 0x8b, 0x08, 0x96, 0x55 },
  228. }, {
  229. .pixclk = 164000000,
  230. .pll_div_regs = { 0xd1, 0x45, 0x11, 0x83, 0x82, 0x90, 0x4b },
  231. }, {
  232. .pixclk = 165000000,
  233. .pll_div_regs = { 0xd1, 0x45, 0x11, 0x84, 0x81, 0x90, 0x4b },
  234. }, {
  235. .pixclk = 185625000,
  236. .pll_div_regs = { 0xd1, 0x4e, 0x12, 0x9a, 0x95, 0x80, 0x40 },
  237. }, {
  238. .pixclk = 188000000,
  239. .pll_div_regs = { 0xd1, 0x4e, 0x12, 0xa7, 0x10, 0x80, 0x40 },
  240. }, {
  241. .pixclk = 198000000,
  242. .pll_div_regs = { 0xd1, 0x52, 0x12, 0x82, 0x01, 0x88, 0x47 },
  243. }, {
  244. .pixclk = 205000000,
  245. .pll_div_regs = { 0xd1, 0x55, 0x12, 0x8c, 0x05, 0x90, 0x4b },
  246. }, {
  247. .pixclk = 209500000,
  248. .pll_div_regs = { 0xd1, 0x57, 0x12, 0x98, 0x07, 0x90, 0x49 },
  249. }, {
  250. .pixclk = 213000000,
  251. .pll_div_regs = { 0xd1, 0x58, 0x12, 0x84, 0x03, 0x82, 0x41 },
  252. }, {
  253. .pixclk = 216216000,
  254. .pll_div_regs = { 0xd1, 0x5a, 0x12, 0xfd, 0x0c, 0x80, 0x40 },
  255. }, {
  256. .pixclk = 254000000,
  257. .pll_div_regs = { 0xd1, 0x69, 0x14, 0x89, 0x08, 0x80, 0x40 },
  258. }, {
  259. .pixclk = 277500000,
  260. .pll_div_regs = { 0xd1, 0x73, 0x15, 0x88, 0x05, 0x90, 0x4d },
  261. }, {
  262. .pixclk = 297000000,
  263. .pll_div_regs = { 0xd1, 0x7b, 0x15, 0x84, 0x03, 0x90, 0x45 },
  264. },
  265. };
  266. struct reg_settings {
  267. u8 reg;
  268. u8 val;
  269. };
  270. static const struct reg_settings common_phy_cfg[] = {
  271. { PHY_REG(0), 0x00 },
  272. /* PHY_REG(1-7) pix clk specific */
  273. { PHY_REG(8), 0x4f }, { PHY_REG(9), 0x30 },
  274. { PHY_REG(10), 0x33 }, { PHY_REG(11), 0x65 },
  275. /* REG12 pixclk specific */
  276. /* REG13 pixclk specific */
  277. /* REG14 pixclk specific */
  278. { PHY_REG(15), 0x80 }, { PHY_REG(16), 0x6c },
  279. { PHY_REG(17), 0xf2 }, { PHY_REG(18), 0x67 },
  280. { PHY_REG(19), 0x00 }, { PHY_REG(20), 0x10 },
  281. /* REG21 pixclk specific */
  282. { PHY_REG(22), 0x30 }, { PHY_REG(23), 0x32 },
  283. { PHY_REG(24), 0x60 }, { PHY_REG(25), 0x8f },
  284. { PHY_REG(26), 0x00 }, { PHY_REG(27), 0x00 },
  285. { PHY_REG(28), 0x08 }, { PHY_REG(29), 0x00 },
  286. { PHY_REG(30), 0x00 }, { PHY_REG(31), 0x00 },
  287. { PHY_REG(32), 0x00 }, { PHY_REG(33), 0x80 },
  288. { PHY_REG(34), 0x00 }, { PHY_REG(35), 0x00 },
  289. { PHY_REG(36), 0x00 }, { PHY_REG(37), 0x00 },
  290. { PHY_REG(38), 0x00 }, { PHY_REG(39), 0x00 },
  291. { PHY_REG(40), 0x00 }, { PHY_REG(41), 0xe0 },
  292. { PHY_REG(42), 0x83 }, { PHY_REG(43), 0x0f },
  293. { PHY_REG(44), 0x3E }, { PHY_REG(45), 0xf8 },
  294. { PHY_REG(46), 0x00 }, { PHY_REG(47), 0x00 }
  295. };
  296. struct fsl_samsung_hdmi_phy {
  297. struct device *dev;
  298. void __iomem *regs;
  299. struct clk *apbclk;
  300. struct clk *refclk;
  301. /* clk provider */
  302. struct clk_hw hw;
  303. const struct phy_config *cur_cfg;
  304. };
  305. static inline struct fsl_samsung_hdmi_phy *
  306. to_fsl_samsung_hdmi_phy(struct clk_hw *hw)
  307. {
  308. return container_of(hw, struct fsl_samsung_hdmi_phy, hw);
  309. }
  310. static int
  311. fsl_samsung_hdmi_phy_configure_pll_lock_det(struct fsl_samsung_hdmi_phy *phy,
  312. const struct phy_config *cfg)
  313. {
  314. u32 pclk = cfg->pixclk;
  315. u32 fld_tg_code;
  316. u32 int_pllclk;
  317. u8 div;
  318. /* Find int_pllclk speed */
  319. for (div = 0; div < 4; div++) {
  320. int_pllclk = pclk / (1 << div);
  321. if (int_pllclk < (50 * MHZ))
  322. break;
  323. }
  324. if (unlikely(div == 4))
  325. return -EINVAL;
  326. writeb(FIELD_PREP(REG12_CK_DIV_MASK, div), phy->regs + PHY_REG(12));
  327. /*
  328. * Calculation for the frequency lock detector target code (fld_tg_code)
  329. * is based on reference manual register description of PHY_REG13
  330. * (13.10.3.1.14.2):
  331. * 1st) Calculate int_pllclk which is determinded by FLD_CK_DIV
  332. * 2nd) Increase resolution to avoid rounding issues
  333. * 3th) Do the div (256 / Freq. of int_pllclk) * 24
  334. * 4th) Reduce the resolution and always round up since the NXP
  335. * settings rounding up always too. TODO: Check if that is
  336. * correct.
  337. */
  338. fld_tg_code = DIV_ROUND_UP(24 * MHZ * 256, int_pllclk);
  339. /* FLD_TOL and FLD_RP_CODE taken from downstream driver */
  340. writeb(FIELD_PREP(REG13_TG_CODE_LOW_MASK, fld_tg_code),
  341. phy->regs + PHY_REG(13));
  342. writeb(FIELD_PREP(REG14_TOL_MASK, 2) |
  343. FIELD_PREP(REG14_RP_CODE_MASK, 2) |
  344. FIELD_PREP(REG14_TG_CODE_HIGH_MASK, fld_tg_code >> 8),
  345. phy->regs + PHY_REG(14));
  346. return 0;
  347. }
  348. static unsigned long fsl_samsung_hdmi_phy_find_pms(unsigned long fout, u8 *p, u16 *m, u8 *s)
  349. {
  350. unsigned long best_freq = 0;
  351. u32 min_delta = 0xffffffff;
  352. u8 _p, best_p;
  353. u16 _m, best_m;
  354. u8 _s, best_s;
  355. /*
  356. * Figure 13-78 of the reference manual states the PLL should be TMDS x 5
  357. * while the TMDS_CLKO should be the PLL / 5. So to calculate the PLL,
  358. * take the pix clock x 5, then return the value of the PLL / 5.
  359. */
  360. fout *= 5;
  361. /* The ref manual states the values of 'P' range from 1 to 11 */
  362. for (_p = 1; _p <= 11; ++_p) {
  363. for (_s = 1; _s <= 16; ++_s) {
  364. u64 tmp;
  365. u32 delta;
  366. /* s must be one or even */
  367. if (_s > 1 && (_s & 0x01) == 1)
  368. _s++;
  369. /* _s cannot be 14 per the TRM */
  370. if (_s == 14)
  371. continue;
  372. /*
  373. * The Ref manual doesn't explicitly state the range of M,
  374. * but it does show it as an 8-bit value, so reject
  375. * any value above 255.
  376. */
  377. tmp = (u64)fout * (_p * _s);
  378. do_div(tmp, 24 * MHZ);
  379. if (tmp > 255)
  380. continue;
  381. _m = tmp;
  382. /*
  383. * Rev 2 of the Ref Manual states the
  384. * VCO can range between 750MHz and
  385. * 3GHz. The VCO is assumed to be
  386. * Fvco = (M * f_ref) / P,
  387. * where f_ref is 24MHz.
  388. */
  389. tmp = div64_ul((u64)_m * 24 * MHZ, _p);
  390. if (tmp < 750 * MHZ ||
  391. tmp > 3000 * MHZ)
  392. continue;
  393. /* Final frequency after post-divider */
  394. do_div(tmp, _s);
  395. delta = abs(fout - tmp);
  396. if (delta < min_delta) {
  397. best_p = _p;
  398. best_s = _s;
  399. best_m = _m;
  400. min_delta = delta;
  401. best_freq = tmp;
  402. }
  403. /* If we have an exact match, stop looking for a better value */
  404. if (!delta)
  405. goto done;
  406. }
  407. }
  408. done:
  409. if (best_freq) {
  410. *p = best_p;
  411. *m = best_m;
  412. *s = best_s;
  413. }
  414. return best_freq / 5;
  415. }
  416. static int fsl_samsung_hdmi_phy_configure(struct fsl_samsung_hdmi_phy *phy,
  417. const struct phy_config *cfg)
  418. {
  419. int i, ret;
  420. u8 val;
  421. phy->cur_cfg = cfg;
  422. /* HDMI PHY init */
  423. writeb(REG33_FIX_DA, phy->regs + PHY_REG(33));
  424. /* common PHY registers */
  425. for (i = 0; i < ARRAY_SIZE(common_phy_cfg); i++)
  426. writeb(common_phy_cfg[i].val, phy->regs + common_phy_cfg[i].reg);
  427. /* set individual PLL registers PHY_REG1 ... PHY_REG7 */
  428. for (i = 0; i < PHY_PLL_DIV_REGS_NUM; i++)
  429. writeb(cfg->pll_div_regs[i], phy->regs + PHY_REG(1) + i * 4);
  430. /* High nibble of PHY_REG3 and low nibble of PHY_REG21 both contain 'S' */
  431. writeb(REG21_SEL_TX_CK_INV | FIELD_PREP(REG21_PMS_S_MASK,
  432. cfg->pll_div_regs[2] >> 4), phy->regs + PHY_REG(21));
  433. ret = fsl_samsung_hdmi_phy_configure_pll_lock_det(phy, cfg);
  434. if (ret) {
  435. dev_err(phy->dev, "pixclock too large\n");
  436. return ret;
  437. }
  438. writeb(REG33_FIX_DA | REG33_MODE_SET_DONE, phy->regs + PHY_REG(33));
  439. ret = readb_poll_timeout(phy->regs + PHY_REG(34), val,
  440. val & REG34_PLL_LOCK, 50, 20000);
  441. if (ret)
  442. dev_err(phy->dev, "PLL failed to lock\n");
  443. return ret;
  444. }
  445. static unsigned long phy_clk_recalc_rate(struct clk_hw *hw,
  446. unsigned long parent_rate)
  447. {
  448. struct fsl_samsung_hdmi_phy *phy = to_fsl_samsung_hdmi_phy(hw);
  449. if (!phy->cur_cfg)
  450. return 74250000;
  451. return phy->cur_cfg->pixclk;
  452. }
  453. /* Helper function to lookup the available fractional-divider rate */
  454. static const struct phy_config *fsl_samsung_hdmi_phy_lookup_rate(unsigned long rate)
  455. {
  456. int i;
  457. /* Search the lookup table */
  458. for (i = ARRAY_SIZE(phy_pll_cfg) - 1; i >= 0; i--)
  459. if (phy_pll_cfg[i].pixclk <= rate)
  460. break;
  461. /* If there is an exact match, or the array has been searched, return the value*/
  462. if (phy_pll_cfg[i].pixclk == rate || i + 1 > ARRAY_SIZE(phy_pll_cfg) - 1)
  463. return &phy_pll_cfg[i];
  464. /* See if the next entry is closer to nominal than this one */
  465. return (abs((long) rate - (long) phy_pll_cfg[i].pixclk) <
  466. abs((long) rate - (long) phy_pll_cfg[i+1].pixclk) ?
  467. &phy_pll_cfg[i] : &phy_pll_cfg[i+1]);
  468. }
  469. static void fsl_samsung_hdmi_calculate_phy(struct phy_config *cal_phy, unsigned long rate,
  470. u8 p, u16 m, u8 s)
  471. {
  472. cal_phy->pixclk = rate;
  473. cal_phy->pll_div_regs[0] = FIELD_PREP(REG01_PMS_P_MASK, p);
  474. cal_phy->pll_div_regs[1] = m;
  475. cal_phy->pll_div_regs[2] = FIELD_PREP(REG03_PMS_S_MASK, s-1);
  476. /* pll_div_regs 3-6 are fixed and pre-defined already */
  477. }
  478. static
  479. const struct phy_config *fsl_samsung_hdmi_phy_find_settings(struct fsl_samsung_hdmi_phy *phy,
  480. unsigned long rate)
  481. {
  482. const struct phy_config *fract_div_phy;
  483. u32 int_div_clk;
  484. u16 m;
  485. u8 p, s;
  486. /* If the clock is out of range return error instead of searching */
  487. if (rate > 297000000 || rate < 22250000)
  488. return NULL;
  489. /* Search the fractional divider lookup table */
  490. fract_div_phy = fsl_samsung_hdmi_phy_lookup_rate(rate);
  491. if (fract_div_phy->pixclk == rate) {
  492. dev_dbg(phy->dev, "fractional divider match = %u\n", fract_div_phy->pixclk);
  493. return fract_div_phy;
  494. }
  495. /* Calculate the integer divider */
  496. int_div_clk = fsl_samsung_hdmi_phy_find_pms(rate, &p, &m, &s);
  497. fsl_samsung_hdmi_calculate_phy(&calculated_phy_pll_cfg, int_div_clk, p, m, s);
  498. if (int_div_clk == rate) {
  499. dev_dbg(phy->dev, "integer divider match = %u\n", calculated_phy_pll_cfg.pixclk);
  500. return &calculated_phy_pll_cfg;
  501. }
  502. /* Calculate the absolute value of the differences and return whichever is closest */
  503. if (abs((long)rate - (long)int_div_clk) <
  504. abs((long)rate - (long)fract_div_phy->pixclk)) {
  505. dev_dbg(phy->dev, "integer divider = %u\n", calculated_phy_pll_cfg.pixclk);
  506. return &calculated_phy_pll_cfg;
  507. }
  508. dev_dbg(phy->dev, "fractional divider = %u\n", phy->cur_cfg->pixclk);
  509. return fract_div_phy;
  510. }
  511. static int fsl_samsung_hdmi_phy_clk_determine_rate(struct clk_hw *hw,
  512. struct clk_rate_request *req)
  513. {
  514. struct fsl_samsung_hdmi_phy *phy = to_fsl_samsung_hdmi_phy(hw);
  515. const struct phy_config *target_settings = fsl_samsung_hdmi_phy_find_settings(phy,
  516. req->rate);
  517. if (target_settings == NULL)
  518. return -EINVAL;
  519. dev_dbg(phy->dev, "round_rate, closest rate = %u\n", target_settings->pixclk);
  520. req->rate = target_settings->pixclk;
  521. return 0;
  522. }
  523. static int fsl_samsung_hdmi_phy_clk_set_rate(struct clk_hw *hw,
  524. unsigned long rate, unsigned long parent_rate)
  525. {
  526. struct fsl_samsung_hdmi_phy *phy = to_fsl_samsung_hdmi_phy(hw);
  527. const struct phy_config *target_settings = fsl_samsung_hdmi_phy_find_settings(phy, rate);
  528. if (target_settings == NULL)
  529. return -EINVAL;
  530. dev_dbg(phy->dev, "set_rate, closest rate = %u\n", target_settings->pixclk);
  531. return fsl_samsung_hdmi_phy_configure(phy, target_settings);
  532. }
  533. static const struct clk_ops phy_clk_ops = {
  534. .recalc_rate = phy_clk_recalc_rate,
  535. .determine_rate = fsl_samsung_hdmi_phy_clk_determine_rate,
  536. .set_rate = fsl_samsung_hdmi_phy_clk_set_rate,
  537. };
  538. static int phy_clk_register(struct fsl_samsung_hdmi_phy *phy)
  539. {
  540. struct device *dev = phy->dev;
  541. struct device_node *np = dev->of_node;
  542. struct clk_init_data init;
  543. const char *parent_name;
  544. struct clk *phyclk;
  545. int ret;
  546. parent_name = __clk_get_name(phy->refclk);
  547. init.parent_names = &parent_name;
  548. init.num_parents = 1;
  549. init.flags = 0;
  550. init.name = "hdmi_pclk";
  551. init.ops = &phy_clk_ops;
  552. phy->hw.init = &init;
  553. phyclk = devm_clk_register(dev, &phy->hw);
  554. if (IS_ERR(phyclk))
  555. return dev_err_probe(dev, PTR_ERR(phyclk),
  556. "failed to register clock\n");
  557. ret = of_clk_add_hw_provider(np, of_clk_hw_simple_get, phyclk);
  558. if (ret)
  559. return dev_err_probe(dev, ret,
  560. "failed to register clock provider\n");
  561. return 0;
  562. }
  563. static int fsl_samsung_hdmi_phy_probe(struct platform_device *pdev)
  564. {
  565. struct fsl_samsung_hdmi_phy *phy;
  566. int ret;
  567. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  568. if (!phy)
  569. return -ENOMEM;
  570. platform_set_drvdata(pdev, phy);
  571. phy->dev = &pdev->dev;
  572. phy->regs = devm_platform_ioremap_resource(pdev, 0);
  573. if (IS_ERR(phy->regs))
  574. return PTR_ERR(phy->regs);
  575. phy->apbclk = devm_clk_get_enabled(phy->dev, "apb");
  576. if (IS_ERR(phy->apbclk))
  577. return dev_err_probe(phy->dev, PTR_ERR(phy->apbclk),
  578. "failed to get apb clk\n");
  579. phy->refclk = devm_clk_get(phy->dev, "ref");
  580. if (IS_ERR(phy->refclk))
  581. return dev_err_probe(phy->dev, PTR_ERR(phy->refclk),
  582. "failed to get ref clk\n");
  583. pm_runtime_get_noresume(phy->dev);
  584. pm_runtime_set_active(phy->dev);
  585. pm_runtime_enable(phy->dev);
  586. ret = phy_clk_register(phy);
  587. if (ret) {
  588. dev_err(&pdev->dev, "register clk failed\n");
  589. goto register_clk_failed;
  590. }
  591. pm_runtime_put(phy->dev);
  592. return 0;
  593. register_clk_failed:
  594. return ret;
  595. }
  596. static void fsl_samsung_hdmi_phy_remove(struct platform_device *pdev)
  597. {
  598. of_clk_del_provider(pdev->dev.of_node);
  599. }
  600. static int __maybe_unused fsl_samsung_hdmi_phy_suspend(struct device *dev)
  601. {
  602. struct fsl_samsung_hdmi_phy *phy = dev_get_drvdata(dev);
  603. clk_disable_unprepare(phy->apbclk);
  604. return 0;
  605. }
  606. static int __maybe_unused fsl_samsung_hdmi_phy_resume(struct device *dev)
  607. {
  608. struct fsl_samsung_hdmi_phy *phy = dev_get_drvdata(dev);
  609. int ret = 0;
  610. ret = clk_prepare_enable(phy->apbclk);
  611. if (ret) {
  612. dev_err(phy->dev, "failed to enable apbclk\n");
  613. return ret;
  614. }
  615. if (phy->cur_cfg)
  616. ret = fsl_samsung_hdmi_phy_configure(phy, phy->cur_cfg);
  617. return ret;
  618. }
  619. static DEFINE_RUNTIME_DEV_PM_OPS(fsl_samsung_hdmi_phy_pm_ops,
  620. fsl_samsung_hdmi_phy_suspend,
  621. fsl_samsung_hdmi_phy_resume, NULL);
  622. static const struct of_device_id fsl_samsung_hdmi_phy_of_match[] = {
  623. {
  624. .compatible = "fsl,imx8mp-hdmi-phy",
  625. }, {
  626. /* sentinel */
  627. }
  628. };
  629. MODULE_DEVICE_TABLE(of, fsl_samsung_hdmi_phy_of_match);
  630. static struct platform_driver fsl_samsung_hdmi_phy_driver = {
  631. .probe = fsl_samsung_hdmi_phy_probe,
  632. .remove = fsl_samsung_hdmi_phy_remove,
  633. .driver = {
  634. .name = "fsl-samsung-hdmi-phy",
  635. .of_match_table = fsl_samsung_hdmi_phy_of_match,
  636. .pm = pm_ptr(&fsl_samsung_hdmi_phy_pm_ops),
  637. },
  638. };
  639. module_platform_driver(fsl_samsung_hdmi_phy_driver);
  640. MODULE_AUTHOR("Sandor Yu <Sandor.yu@nxp.com>");
  641. MODULE_DESCRIPTION("SAMSUNG HDMI 2.0 Transmitter PHY Driver");
  642. MODULE_LICENSE("GPL");