dw_hdmi-rockchip.c 11 KB

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