dw_hdmi-rockchip.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/module.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/mfd/syscon.h>
  12. #include <linux/regmap.h>
  13. #include <drm/drm_of.h>
  14. #include <drm/drmP.h>
  15. #include <drm/drm_crtc_helper.h>
  16. #include <drm/drm_edid.h>
  17. #include <drm/bridge/dw_hdmi.h>
  18. #include "rockchip_drm_drv.h"
  19. #include "rockchip_drm_vop.h"
  20. #define GRF_SOC_CON6 0x025c
  21. #define HDMI_SEL_VOP_LIT (1 << 4)
  22. struct rockchip_hdmi {
  23. struct device *dev;
  24. struct regmap *regmap;
  25. struct drm_encoder encoder;
  26. };
  27. #define to_rockchip_hdmi(x) container_of(x, struct rockchip_hdmi, x)
  28. static const struct dw_hdmi_mpll_config rockchip_mpll_cfg[] = {
  29. {
  30. 27000000, {
  31. { 0x00b3, 0x0000},
  32. { 0x2153, 0x0000},
  33. { 0x40f3, 0x0000}
  34. },
  35. }, {
  36. 36000000, {
  37. { 0x00b3, 0x0000},
  38. { 0x2153, 0x0000},
  39. { 0x40f3, 0x0000}
  40. },
  41. }, {
  42. 40000000, {
  43. { 0x00b3, 0x0000},
  44. { 0x2153, 0x0000},
  45. { 0x40f3, 0x0000}
  46. },
  47. }, {
  48. 54000000, {
  49. { 0x0072, 0x0001},
  50. { 0x2142, 0x0001},
  51. { 0x40a2, 0x0001},
  52. },
  53. }, {
  54. 65000000, {
  55. { 0x0072, 0x0001},
  56. { 0x2142, 0x0001},
  57. { 0x40a2, 0x0001},
  58. },
  59. }, {
  60. 66000000, {
  61. { 0x013e, 0x0003},
  62. { 0x217e, 0x0002},
  63. { 0x4061, 0x0002}
  64. },
  65. }, {
  66. 74250000, {
  67. { 0x0072, 0x0001},
  68. { 0x2145, 0x0002},
  69. { 0x4061, 0x0002}
  70. },
  71. }, {
  72. 83500000, {
  73. { 0x0072, 0x0001},
  74. },
  75. }, {
  76. 108000000, {
  77. { 0x0051, 0x0002},
  78. { 0x2145, 0x0002},
  79. { 0x4061, 0x0002}
  80. },
  81. }, {
  82. 106500000, {
  83. { 0x0051, 0x0002},
  84. { 0x2145, 0x0002},
  85. { 0x4061, 0x0002}
  86. },
  87. }, {
  88. 146250000, {
  89. { 0x0051, 0x0002},
  90. { 0x2145, 0x0002},
  91. { 0x4061, 0x0002}
  92. },
  93. }, {
  94. 148500000, {
  95. { 0x0051, 0x0003},
  96. { 0x214c, 0x0003},
  97. { 0x4064, 0x0003}
  98. },
  99. }, {
  100. ~0UL, {
  101. { 0x00a0, 0x000a },
  102. { 0x2001, 0x000f },
  103. { 0x4002, 0x000f },
  104. },
  105. }
  106. };
  107. static const struct dw_hdmi_curr_ctrl rockchip_cur_ctr[] = {
  108. /* pixelclk bpp8 bpp10 bpp12 */
  109. {
  110. 40000000, { 0x0018, 0x0018, 0x0018 },
  111. }, {
  112. 65000000, { 0x0028, 0x0028, 0x0028 },
  113. }, {
  114. 66000000, { 0x0038, 0x0038, 0x0038 },
  115. }, {
  116. 74250000, { 0x0028, 0x0038, 0x0038 },
  117. }, {
  118. 83500000, { 0x0028, 0x0038, 0x0038 },
  119. }, {
  120. 146250000, { 0x0038, 0x0038, 0x0038 },
  121. }, {
  122. 148500000, { 0x0000, 0x0038, 0x0038 },
  123. }, {
  124. ~0UL, { 0x0000, 0x0000, 0x0000},
  125. }
  126. };
  127. static const struct dw_hdmi_phy_config rockchip_phy_config[] = {
  128. /*pixelclk symbol term vlev*/
  129. { 74250000, 0x8009, 0x0004, 0x0272},
  130. { 148500000, 0x802b, 0x0004, 0x028d},
  131. { 297000000, 0x8039, 0x0005, 0x028d},
  132. { ~0UL, 0x0000, 0x0000, 0x0000}
  133. };
  134. static int rockchip_hdmi_parse_dt(struct rockchip_hdmi *hdmi)
  135. {
  136. struct device_node *np = hdmi->dev->of_node;
  137. hdmi->regmap = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  138. if (IS_ERR(hdmi->regmap)) {
  139. dev_err(hdmi->dev, "Unable to get rockchip,grf\n");
  140. return PTR_ERR(hdmi->regmap);
  141. }
  142. return 0;
  143. }
  144. static enum drm_mode_status
  145. dw_hdmi_rockchip_mode_valid(struct drm_connector *connector,
  146. struct drm_display_mode *mode)
  147. {
  148. const struct dw_hdmi_mpll_config *mpll_cfg = rockchip_mpll_cfg;
  149. int pclk = mode->clock * 1000;
  150. bool valid = false;
  151. int i;
  152. for (i = 0; mpll_cfg[i].mpixelclock != (~0UL); i++) {
  153. if (pclk == mpll_cfg[i].mpixelclock) {
  154. valid = true;
  155. break;
  156. }
  157. }
  158. return (valid) ? MODE_OK : MODE_BAD;
  159. }
  160. static const struct drm_encoder_funcs dw_hdmi_rockchip_encoder_funcs = {
  161. .destroy = drm_encoder_cleanup,
  162. };
  163. static void dw_hdmi_rockchip_encoder_disable(struct drm_encoder *encoder)
  164. {
  165. }
  166. static bool
  167. dw_hdmi_rockchip_encoder_mode_fixup(struct drm_encoder *encoder,
  168. const struct drm_display_mode *mode,
  169. struct drm_display_mode *adj_mode)
  170. {
  171. return true;
  172. }
  173. static void dw_hdmi_rockchip_encoder_mode_set(struct drm_encoder *encoder,
  174. struct drm_display_mode *mode,
  175. struct drm_display_mode *adj_mode)
  176. {
  177. }
  178. static void dw_hdmi_rockchip_encoder_enable(struct drm_encoder *encoder)
  179. {
  180. struct rockchip_hdmi *hdmi = to_rockchip_hdmi(encoder);
  181. u32 val;
  182. int mux;
  183. mux = drm_of_encoder_active_endpoint_id(hdmi->dev->of_node, encoder);
  184. if (mux)
  185. val = HDMI_SEL_VOP_LIT | (HDMI_SEL_VOP_LIT << 16);
  186. else
  187. val = HDMI_SEL_VOP_LIT << 16;
  188. regmap_write(hdmi->regmap, GRF_SOC_CON6, val);
  189. dev_dbg(hdmi->dev, "vop %s output to hdmi\n",
  190. (mux) ? "LIT" : "BIG");
  191. }
  192. static int
  193. dw_hdmi_rockchip_encoder_atomic_check(struct drm_encoder *encoder,
  194. struct drm_crtc_state *crtc_state,
  195. struct drm_connector_state *conn_state)
  196. {
  197. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  198. s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
  199. s->output_type = DRM_MODE_CONNECTOR_HDMIA;
  200. return 0;
  201. }
  202. static const struct drm_encoder_helper_funcs dw_hdmi_rockchip_encoder_helper_funcs = {
  203. .mode_fixup = dw_hdmi_rockchip_encoder_mode_fixup,
  204. .mode_set = dw_hdmi_rockchip_encoder_mode_set,
  205. .enable = dw_hdmi_rockchip_encoder_enable,
  206. .disable = dw_hdmi_rockchip_encoder_disable,
  207. .atomic_check = dw_hdmi_rockchip_encoder_atomic_check,
  208. };
  209. static const struct dw_hdmi_plat_data rockchip_hdmi_drv_data = {
  210. .mode_valid = dw_hdmi_rockchip_mode_valid,
  211. .mpll_cfg = rockchip_mpll_cfg,
  212. .cur_ctr = rockchip_cur_ctr,
  213. .phy_config = rockchip_phy_config,
  214. };
  215. static const struct of_device_id dw_hdmi_rockchip_dt_ids[] = {
  216. { .compatible = "rockchip,rk3288-dw-hdmi",
  217. .data = &rockchip_hdmi_drv_data
  218. },
  219. {},
  220. };
  221. MODULE_DEVICE_TABLE(of, dw_hdmi_rockchip_dt_ids);
  222. static int dw_hdmi_rockchip_bind(struct device *dev, struct device *master,
  223. void *data)
  224. {
  225. struct platform_device *pdev = to_platform_device(dev);
  226. const struct dw_hdmi_plat_data *plat_data;
  227. const struct of_device_id *match;
  228. struct drm_device *drm = data;
  229. struct drm_encoder *encoder;
  230. struct rockchip_hdmi *hdmi;
  231. int ret;
  232. if (!pdev->dev.of_node)
  233. return -ENODEV;
  234. hdmi = devm_kzalloc(&pdev->dev, sizeof(*hdmi), GFP_KERNEL);
  235. if (!hdmi)
  236. return -ENOMEM;
  237. match = of_match_node(dw_hdmi_rockchip_dt_ids, pdev->dev.of_node);
  238. plat_data = match->data;
  239. hdmi->dev = &pdev->dev;
  240. encoder = &hdmi->encoder;
  241. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm, dev->of_node);
  242. /*
  243. * If we failed to find the CRTC(s) which this encoder is
  244. * supposed to be connected to, it's because the CRTC has
  245. * not been registered yet. Defer probing, and hope that
  246. * the required CRTC is added later.
  247. */
  248. if (encoder->possible_crtcs == 0)
  249. return -EPROBE_DEFER;
  250. ret = rockchip_hdmi_parse_dt(hdmi);
  251. if (ret) {
  252. dev_err(hdmi->dev, "Unable to parse OF data\n");
  253. return ret;
  254. }
  255. drm_encoder_helper_add(encoder, &dw_hdmi_rockchip_encoder_helper_funcs);
  256. drm_encoder_init(drm, encoder, &dw_hdmi_rockchip_encoder_funcs,
  257. DRM_MODE_ENCODER_TMDS, NULL);
  258. ret = dw_hdmi_bind(pdev, encoder, plat_data);
  259. /*
  260. * If dw_hdmi_bind() fails we'll never call dw_hdmi_unbind(),
  261. * which would have called the encoder cleanup. Do it manually.
  262. */
  263. if (ret)
  264. drm_encoder_cleanup(encoder);
  265. return ret;
  266. }
  267. static void dw_hdmi_rockchip_unbind(struct device *dev, struct device *master,
  268. void *data)
  269. {
  270. return dw_hdmi_unbind(dev);
  271. }
  272. static const struct component_ops dw_hdmi_rockchip_ops = {
  273. .bind = dw_hdmi_rockchip_bind,
  274. .unbind = dw_hdmi_rockchip_unbind,
  275. };
  276. static int dw_hdmi_rockchip_probe(struct platform_device *pdev)
  277. {
  278. return component_add(&pdev->dev, &dw_hdmi_rockchip_ops);
  279. }
  280. static int dw_hdmi_rockchip_remove(struct platform_device *pdev)
  281. {
  282. component_del(&pdev->dev, &dw_hdmi_rockchip_ops);
  283. return 0;
  284. }
  285. struct platform_driver dw_hdmi_rockchip_pltfm_driver = {
  286. .probe = dw_hdmi_rockchip_probe,
  287. .remove = dw_hdmi_rockchip_remove,
  288. .driver = {
  289. .name = "dwhdmi-rockchip",
  290. .of_match_table = dw_hdmi_rockchip_dt_ids,
  291. },
  292. };