dw_hdmi-rockchip.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/mfd/syscon.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/regmap.h>
  14. #include <drm/drm_of.h>
  15. #include <drm/drmP.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_edid.h>
  18. #include <drm/bridge/dw_hdmi.h>
  19. #include "rockchip_drm_drv.h"
  20. #include "rockchip_drm_vop.h"
  21. #define RK3288_GRF_SOC_CON6 0x025C
  22. #define RK3288_HDMI_LCDC_SEL BIT(4)
  23. #define RK3399_GRF_SOC_CON20 0x6250
  24. #define RK3399_HDMI_LCDC_SEL BIT(6)
  25. #define HIWORD_UPDATE(val, mask) (val | (mask) << 16)
  26. /**
  27. * struct rockchip_hdmi_chip_data - splite the grf setting of kind of chips
  28. * @lcdsel_grf_reg: grf register offset of lcdc select
  29. * @lcdsel_big: reg value of selecting vop big for HDMI
  30. * @lcdsel_lit: reg value of selecting vop little for HDMI
  31. */
  32. struct rockchip_hdmi_chip_data {
  33. u32 lcdsel_grf_reg;
  34. u32 lcdsel_big;
  35. u32 lcdsel_lit;
  36. };
  37. struct rockchip_hdmi {
  38. struct device *dev;
  39. struct regmap *regmap;
  40. struct drm_encoder encoder;
  41. const struct rockchip_hdmi_chip_data *chip_data;
  42. struct clk *vpll_clk;
  43. struct clk *grf_clk;
  44. };
  45. #define to_rockchip_hdmi(x) container_of(x, struct rockchip_hdmi, x)
  46. static const struct dw_hdmi_mpll_config rockchip_mpll_cfg[] = {
  47. {
  48. 27000000, {
  49. { 0x00b3, 0x0000},
  50. { 0x2153, 0x0000},
  51. { 0x40f3, 0x0000}
  52. },
  53. }, {
  54. 36000000, {
  55. { 0x00b3, 0x0000},
  56. { 0x2153, 0x0000},
  57. { 0x40f3, 0x0000}
  58. },
  59. }, {
  60. 40000000, {
  61. { 0x00b3, 0x0000},
  62. { 0x2153, 0x0000},
  63. { 0x40f3, 0x0000}
  64. },
  65. }, {
  66. 54000000, {
  67. { 0x0072, 0x0001},
  68. { 0x2142, 0x0001},
  69. { 0x40a2, 0x0001},
  70. },
  71. }, {
  72. 65000000, {
  73. { 0x0072, 0x0001},
  74. { 0x2142, 0x0001},
  75. { 0x40a2, 0x0001},
  76. },
  77. }, {
  78. 66000000, {
  79. { 0x013e, 0x0003},
  80. { 0x217e, 0x0002},
  81. { 0x4061, 0x0002}
  82. },
  83. }, {
  84. 74250000, {
  85. { 0x0072, 0x0001},
  86. { 0x2145, 0x0002},
  87. { 0x4061, 0x0002}
  88. },
  89. }, {
  90. 83500000, {
  91. { 0x0072, 0x0001},
  92. },
  93. }, {
  94. 108000000, {
  95. { 0x0051, 0x0002},
  96. { 0x2145, 0x0002},
  97. { 0x4061, 0x0002}
  98. },
  99. }, {
  100. 106500000, {
  101. { 0x0051, 0x0002},
  102. { 0x2145, 0x0002},
  103. { 0x4061, 0x0002}
  104. },
  105. }, {
  106. 146250000, {
  107. { 0x0051, 0x0002},
  108. { 0x2145, 0x0002},
  109. { 0x4061, 0x0002}
  110. },
  111. }, {
  112. 148500000, {
  113. { 0x0051, 0x0003},
  114. { 0x214c, 0x0003},
  115. { 0x4064, 0x0003}
  116. },
  117. }, {
  118. ~0UL, {
  119. { 0x00a0, 0x000a },
  120. { 0x2001, 0x000f },
  121. { 0x4002, 0x000f },
  122. },
  123. }
  124. };
  125. static const struct dw_hdmi_curr_ctrl rockchip_cur_ctr[] = {
  126. /* pixelclk bpp8 bpp10 bpp12 */
  127. {
  128. 40000000, { 0x0018, 0x0018, 0x0018 },
  129. }, {
  130. 65000000, { 0x0028, 0x0028, 0x0028 },
  131. }, {
  132. 66000000, { 0x0038, 0x0038, 0x0038 },
  133. }, {
  134. 74250000, { 0x0028, 0x0038, 0x0038 },
  135. }, {
  136. 83500000, { 0x0028, 0x0038, 0x0038 },
  137. }, {
  138. 146250000, { 0x0038, 0x0038, 0x0038 },
  139. }, {
  140. 148500000, { 0x0000, 0x0038, 0x0038 },
  141. }, {
  142. ~0UL, { 0x0000, 0x0000, 0x0000},
  143. }
  144. };
  145. static const struct dw_hdmi_phy_config rockchip_phy_config[] = {
  146. /*pixelclk symbol term vlev*/
  147. { 74250000, 0x8009, 0x0004, 0x0272},
  148. { 148500000, 0x802b, 0x0004, 0x028d},
  149. { 297000000, 0x8039, 0x0005, 0x028d},
  150. { ~0UL, 0x0000, 0x0000, 0x0000}
  151. };
  152. static int rockchip_hdmi_parse_dt(struct rockchip_hdmi *hdmi)
  153. {
  154. struct device_node *np = hdmi->dev->of_node;
  155. int ret;
  156. hdmi->regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  157. if (IS_ERR(hdmi->regmap)) {
  158. dev_err(hdmi->dev, "Unable to get rockchip,grf\n");
  159. return PTR_ERR(hdmi->regmap);
  160. }
  161. hdmi->vpll_clk = devm_clk_get(hdmi->dev, "vpll");
  162. if (PTR_ERR(hdmi->vpll_clk) == -ENOENT) {
  163. hdmi->vpll_clk = NULL;
  164. } else if (PTR_ERR(hdmi->vpll_clk) == -EPROBE_DEFER) {
  165. return -EPROBE_DEFER;
  166. } else if (IS_ERR(hdmi->vpll_clk)) {
  167. dev_err(hdmi->dev, "failed to get grf clock\n");
  168. return PTR_ERR(hdmi->vpll_clk);
  169. }
  170. hdmi->grf_clk = devm_clk_get(hdmi->dev, "grf");
  171. if (PTR_ERR(hdmi->grf_clk) == -ENOENT) {
  172. hdmi->grf_clk = NULL;
  173. } else if (PTR_ERR(hdmi->grf_clk) == -EPROBE_DEFER) {
  174. return -EPROBE_DEFER;
  175. } else if (IS_ERR(hdmi->grf_clk)) {
  176. dev_err(hdmi->dev, "failed to get grf clock\n");
  177. return PTR_ERR(hdmi->grf_clk);
  178. }
  179. ret = clk_prepare_enable(hdmi->vpll_clk);
  180. if (ret) {
  181. dev_err(hdmi->dev, "Failed to enable HDMI vpll: %d\n", ret);
  182. return ret;
  183. }
  184. return 0;
  185. }
  186. static enum drm_mode_status
  187. dw_hdmi_rockchip_mode_valid(struct drm_connector *connector,
  188. const struct drm_display_mode *mode)
  189. {
  190. const struct dw_hdmi_mpll_config *mpll_cfg = rockchip_mpll_cfg;
  191. int pclk = mode->clock * 1000;
  192. bool valid = false;
  193. int i;
  194. for (i = 0; mpll_cfg[i].mpixelclock != (~0UL); i++) {
  195. if (pclk == mpll_cfg[i].mpixelclock) {
  196. valid = true;
  197. break;
  198. }
  199. }
  200. return (valid) ? MODE_OK : MODE_BAD;
  201. }
  202. static const struct drm_encoder_funcs dw_hdmi_rockchip_encoder_funcs = {
  203. .destroy = drm_encoder_cleanup,
  204. };
  205. static void dw_hdmi_rockchip_encoder_disable(struct drm_encoder *encoder)
  206. {
  207. }
  208. static bool
  209. dw_hdmi_rockchip_encoder_mode_fixup(struct drm_encoder *encoder,
  210. const struct drm_display_mode *mode,
  211. struct drm_display_mode *adj_mode)
  212. {
  213. return true;
  214. }
  215. static void dw_hdmi_rockchip_encoder_mode_set(struct drm_encoder *encoder,
  216. struct drm_display_mode *mode,
  217. struct drm_display_mode *adj_mode)
  218. {
  219. struct rockchip_hdmi *hdmi = to_rockchip_hdmi(encoder);
  220. clk_set_rate(hdmi->vpll_clk, adj_mode->clock * 1000);
  221. }
  222. static void dw_hdmi_rockchip_encoder_enable(struct drm_encoder *encoder)
  223. {
  224. struct rockchip_hdmi *hdmi = to_rockchip_hdmi(encoder);
  225. u32 val;
  226. int ret;
  227. ret = drm_of_encoder_active_endpoint_id(hdmi->dev->of_node, encoder);
  228. if (ret)
  229. val = hdmi->chip_data->lcdsel_lit;
  230. else
  231. val = hdmi->chip_data->lcdsel_big;
  232. ret = clk_prepare_enable(hdmi->grf_clk);
  233. if (ret < 0) {
  234. dev_err(hdmi->dev, "failed to enable grfclk %d\n", ret);
  235. return;
  236. }
  237. ret = regmap_write(hdmi->regmap, hdmi->chip_data->lcdsel_grf_reg, val);
  238. if (ret != 0)
  239. dev_err(hdmi->dev, "Could not write to GRF: %d\n", ret);
  240. clk_disable_unprepare(hdmi->grf_clk);
  241. dev_dbg(hdmi->dev, "vop %s output to hdmi\n",
  242. ret ? "LIT" : "BIG");
  243. }
  244. static int
  245. dw_hdmi_rockchip_encoder_atomic_check(struct drm_encoder *encoder,
  246. struct drm_crtc_state *crtc_state,
  247. struct drm_connector_state *conn_state)
  248. {
  249. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  250. s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
  251. s->output_type = DRM_MODE_CONNECTOR_HDMIA;
  252. return 0;
  253. }
  254. static const struct drm_encoder_helper_funcs dw_hdmi_rockchip_encoder_helper_funcs = {
  255. .mode_fixup = dw_hdmi_rockchip_encoder_mode_fixup,
  256. .mode_set = dw_hdmi_rockchip_encoder_mode_set,
  257. .enable = dw_hdmi_rockchip_encoder_enable,
  258. .disable = dw_hdmi_rockchip_encoder_disable,
  259. .atomic_check = dw_hdmi_rockchip_encoder_atomic_check,
  260. };
  261. static struct rockchip_hdmi_chip_data rk3288_chip_data = {
  262. .lcdsel_grf_reg = RK3288_GRF_SOC_CON6,
  263. .lcdsel_big = HIWORD_UPDATE(0, RK3288_HDMI_LCDC_SEL),
  264. .lcdsel_lit = HIWORD_UPDATE(RK3288_HDMI_LCDC_SEL, RK3288_HDMI_LCDC_SEL),
  265. };
  266. static const struct dw_hdmi_plat_data rk3288_hdmi_drv_data = {
  267. .mode_valid = dw_hdmi_rockchip_mode_valid,
  268. .mpll_cfg = rockchip_mpll_cfg,
  269. .cur_ctr = rockchip_cur_ctr,
  270. .phy_config = rockchip_phy_config,
  271. .phy_data = &rk3288_chip_data,
  272. };
  273. static struct rockchip_hdmi_chip_data rk3399_chip_data = {
  274. .lcdsel_grf_reg = RK3399_GRF_SOC_CON20,
  275. .lcdsel_big = HIWORD_UPDATE(0, RK3399_HDMI_LCDC_SEL),
  276. .lcdsel_lit = HIWORD_UPDATE(RK3399_HDMI_LCDC_SEL, RK3399_HDMI_LCDC_SEL),
  277. };
  278. static const struct dw_hdmi_plat_data rk3399_hdmi_drv_data = {
  279. .mode_valid = dw_hdmi_rockchip_mode_valid,
  280. .mpll_cfg = rockchip_mpll_cfg,
  281. .cur_ctr = rockchip_cur_ctr,
  282. .phy_config = rockchip_phy_config,
  283. .phy_data = &rk3399_chip_data,
  284. };
  285. static const struct of_device_id dw_hdmi_rockchip_dt_ids[] = {
  286. { .compatible = "rockchip,rk3288-dw-hdmi",
  287. .data = &rk3288_hdmi_drv_data
  288. },
  289. { .compatible = "rockchip,rk3399-dw-hdmi",
  290. .data = &rk3399_hdmi_drv_data
  291. },
  292. {},
  293. };
  294. MODULE_DEVICE_TABLE(of, dw_hdmi_rockchip_dt_ids);
  295. static int dw_hdmi_rockchip_bind(struct device *dev, struct device *master,
  296. void *data)
  297. {
  298. struct platform_device *pdev = to_platform_device(dev);
  299. const struct dw_hdmi_plat_data *plat_data;
  300. const struct of_device_id *match;
  301. struct drm_device *drm = data;
  302. struct drm_encoder *encoder;
  303. struct rockchip_hdmi *hdmi;
  304. int ret;
  305. if (!pdev->dev.of_node)
  306. return -ENODEV;
  307. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  308. if (!hdmi)
  309. return -ENOMEM;
  310. match = of_match_node(dw_hdmi_rockchip_dt_ids, pdev->dev.of_node);
  311. plat_data = match->data;
  312. hdmi->dev = &pdev->dev;
  313. hdmi->chip_data = plat_data->phy_data;
  314. encoder = &hdmi->encoder;
  315. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
  316. /*
  317. * If we failed to find the CRTC(s) which this encoder is
  318. * supposed to be connected to, it's because the CRTC has
  319. * not been registered yet. Defer probing, and hope that
  320. * the required CRTC is added later.
  321. */
  322. if (encoder->possible_crtcs == 0)
  323. return -EPROBE_DEFER;
  324. ret = rockchip_hdmi_parse_dt(hdmi);
  325. if (ret) {
  326. dev_err(hdmi->dev, "Unable to parse OF data\n");
  327. return ret;
  328. }
  329. drm_encoder_helper_add(encoder, &dw_hdmi_rockchip_encoder_helper_funcs);
  330. drm_encoder_init(drm, encoder, &dw_hdmi_rockchip_encoder_funcs,
  331. DRM_MODE_ENCODER_TMDS, NULL);
  332. ret = dw_hdmi_bind(pdev, encoder, plat_data);
  333. /*
  334. * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
  335. * which would have called the encoder cleanup. Do it manually.
  336. */
  337. if (ret)
  338. drm_encoder_cleanup(encoder);
  339. return ret;
  340. }
  341. static void dw_hdmi_rockchip_unbind(struct device *dev, struct device *master,
  342. void *data)
  343. {
  344. return dw_hdmi_unbind(dev);
  345. }
  346. static const struct component_ops dw_hdmi_rockchip_ops = {
  347. .bind = dw_hdmi_rockchip_bind,
  348. .unbind = dw_hdmi_rockchip_unbind,
  349. };
  350. static int dw_hdmi_rockchip_probe(struct platform_device *pdev)
  351. {
  352. return component_add(&pdev->dev, &dw_hdmi_rockchip_ops);
  353. }
  354. static int dw_hdmi_rockchip_remove(struct platform_device *pdev)
  355. {
  356. component_del(&pdev->dev, &dw_hdmi_rockchip_ops);
  357. return 0;
  358. }
  359. struct platform_driver dw_hdmi_rockchip_pltfm_driver = {
  360. .probe = dw_hdmi_rockchip_probe,
  361. .remove = dw_hdmi_rockchip_remove,
  362. .driver = {
  363. .name = "dwhdmi-rockchip",
  364. .of_match_table = dw_hdmi_rockchip_dt_ids,
  365. },
  366. };