analogix_dp-rockchip.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Rockchip SoC DP (Display Port) interface driver.
  3. *
  4. * Copyright (C) Fuzhou Rockchip Electronics Co., Ltd.
  5. * Author: Andy Yan <andy.yan@rock-chips.com>
  6. * Yakir Yang <ykk@rock-chips.com>
  7. * Jeff Chen <jeff.chen@rock-chips.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the
  11. * Free Software Foundation; either version 2 of the License, or (at your
  12. * option) any later version.
  13. */
  14. #include <linux/component.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/of_graph.h>
  17. #include <linux/regmap.h>
  18. #include <linux/reset.h>
  19. #include <linux/clk.h>
  20. #include <drm/drmP.h>
  21. #include <drm/drm_crtc_helper.h>
  22. #include <drm/drm_dp_helper.h>
  23. #include <drm/drm_of.h>
  24. #include <drm/drm_panel.h>
  25. #include <video/of_videomode.h>
  26. #include <video/videomode.h>
  27. #include <drm/bridge/analogix_dp.h>
  28. #include "rockchip_drm_drv.h"
  29. #include "rockchip_drm_vop.h"
  30. #define to_dp(nm) container_of(nm, struct rockchip_dp_device, nm)
  31. /* dp grf register offset */
  32. #define GRF_SOC_CON6 0x025c
  33. #define GRF_EDP_LCD_SEL_MASK BIT(5)
  34. #define GRF_EDP_SEL_VOP_LIT BIT(5)
  35. #define GRF_EDP_SEL_VOP_BIG 0
  36. struct rockchip_dp_device {
  37. struct drm_device *drm_dev;
  38. struct device *dev;
  39. struct drm_encoder encoder;
  40. struct drm_display_mode mode;
  41. struct clk *pclk;
  42. struct regmap *grf;
  43. struct reset_control *rst;
  44. struct analogix_dp_plat_data plat_data;
  45. };
  46. static int rockchip_dp_pre_init(struct rockchip_dp_device *dp)
  47. {
  48. reset_control_assert(dp->rst);
  49. usleep_range(10, 20);
  50. reset_control_deassert(dp->rst);
  51. return 0;
  52. }
  53. static int rockchip_dp_poweron(struct analogix_dp_plat_data *plat_data)
  54. {
  55. struct rockchip_dp_device *dp = to_dp(plat_data);
  56. int ret;
  57. ret = clk_prepare_enable(dp->pclk);
  58. if (ret < 0) {
  59. dev_err(dp->dev, "failed to enable pclk %d\n", ret);
  60. return ret;
  61. }
  62. ret = rockchip_dp_pre_init(dp);
  63. if (ret < 0) {
  64. dev_err(dp->dev, "failed to dp pre init %d\n", ret);
  65. return ret;
  66. }
  67. return 0;
  68. }
  69. static int rockchip_dp_powerdown(struct analogix_dp_plat_data *plat_data)
  70. {
  71. struct rockchip_dp_device *dp = to_dp(plat_data);
  72. clk_disable_unprepare(dp->pclk);
  73. return 0;
  74. }
  75. static bool
  76. rockchip_dp_drm_encoder_mode_fixup(struct drm_encoder *encoder,
  77. const struct drm_display_mode *mode,
  78. struct drm_display_mode *adjusted_mode)
  79. {
  80. /* do nothing */
  81. return true;
  82. }
  83. static void rockchip_dp_drm_encoder_mode_set(struct drm_encoder *encoder,
  84. struct drm_display_mode *mode,
  85. struct drm_display_mode *adjusted)
  86. {
  87. /* do nothing */
  88. }
  89. static void rockchip_dp_drm_encoder_enable(struct drm_encoder *encoder)
  90. {
  91. struct rockchip_dp_device *dp = to_dp(encoder);
  92. int ret;
  93. u32 val;
  94. ret = drm_of_encoder_active_endpoint_id(dp->dev->of_node, encoder);
  95. if (ret < 0)
  96. return;
  97. if (ret)
  98. val = GRF_EDP_SEL_VOP_LIT | (GRF_EDP_LCD_SEL_MASK << 16);
  99. else
  100. val = GRF_EDP_SEL_VOP_BIG | (GRF_EDP_LCD_SEL_MASK << 16);
  101. dev_dbg(dp->dev, "vop %s output to dp\n", (ret) ? "LIT" : "BIG");
  102. ret = regmap_write(dp->grf, GRF_SOC_CON6, val);
  103. if (ret != 0) {
  104. dev_err(dp->dev, "Could not write to GRF: %d\n", ret);
  105. return;
  106. }
  107. }
  108. static void rockchip_dp_drm_encoder_nop(struct drm_encoder *encoder)
  109. {
  110. /* do nothing */
  111. }
  112. static int
  113. rockchip_dp_drm_encoder_atomic_check(struct drm_encoder *encoder,
  114. struct drm_crtc_state *crtc_state,
  115. struct drm_connector_state *conn_state)
  116. {
  117. struct rockchip_crtc_state *s = to_rockchip_crtc_state(crtc_state);
  118. /*
  119. * FIXME(Yakir): driver should configure the CRTC output video
  120. * mode with the display information which indicated the monitor
  121. * support colorimetry.
  122. *
  123. * But don't know why the CRTC driver seems could only output the
  124. * RGBaaa rightly. For example, if connect the "innolux,n116bge"
  125. * eDP screen, EDID would indicated that screen only accepted the
  126. * 6bpc mode. But if I configure CRTC to RGB666 output, then eDP
  127. * screen would show a blue picture (RGB888 show a green picture).
  128. * But if I configure CTRC to RGBaaa, and eDP driver still keep
  129. * RGB666 input video mode, then screen would works prefect.
  130. */
  131. s->output_mode = ROCKCHIP_OUT_MODE_AAAA;
  132. s->output_type = DRM_MODE_CONNECTOR_eDP;
  133. return 0;
  134. }
  135. static struct drm_encoder_helper_funcs rockchip_dp_encoder_helper_funcs = {
  136. .mode_fixup = rockchip_dp_drm_encoder_mode_fixup,
  137. .mode_set = rockchip_dp_drm_encoder_mode_set,
  138. .enable = rockchip_dp_drm_encoder_enable,
  139. .disable = rockchip_dp_drm_encoder_nop,
  140. .atomic_check = rockchip_dp_drm_encoder_atomic_check,
  141. };
  142. static void rockchip_dp_drm_encoder_destroy(struct drm_encoder *encoder)
  143. {
  144. drm_encoder_cleanup(encoder);
  145. }
  146. static struct drm_encoder_funcs rockchip_dp_encoder_funcs = {
  147. .destroy = rockchip_dp_drm_encoder_destroy,
  148. };
  149. static int rockchip_dp_init(struct rockchip_dp_device *dp)
  150. {
  151. struct device *dev = dp->dev;
  152. struct device_node *np = dev->of_node;
  153. int ret;
  154. dp->grf = syscon_regmap_lookup_by_phandle(np, "rockchip,grf");
  155. if (IS_ERR(dp->grf)) {
  156. dev_err(dev, "failed to get rockchip,grf property\n");
  157. return PTR_ERR(dp->grf);
  158. }
  159. dp->pclk = devm_clk_get(dev, "pclk");
  160. if (IS_ERR(dp->pclk)) {
  161. dev_err(dev, "failed to get pclk property\n");
  162. return PTR_ERR(dp->pclk);
  163. }
  164. dp->rst = devm_reset_control_get(dev, "dp");
  165. if (IS_ERR(dp->rst)) {
  166. dev_err(dev, "failed to get dp reset control\n");
  167. return PTR_ERR(dp->rst);
  168. }
  169. ret = clk_prepare_enable(dp->pclk);
  170. if (ret < 0) {
  171. dev_err(dp->dev, "failed to enable pclk %d\n", ret);
  172. return ret;
  173. }
  174. ret = rockchip_dp_pre_init(dp);
  175. if (ret < 0) {
  176. dev_err(dp->dev, "failed to pre init %d\n", ret);
  177. return ret;
  178. }
  179. return 0;
  180. }
  181. static int rockchip_dp_drm_create_encoder(struct rockchip_dp_device *dp)
  182. {
  183. struct drm_encoder *encoder = &dp->encoder;
  184. struct drm_device *drm_dev = dp->drm_dev;
  185. struct device *dev = dp->dev;
  186. int ret;
  187. encoder->possible_crtcs = drm_of_find_possible_crtcs(drm_dev,
  188. dev->of_node);
  189. DRM_DEBUG_KMS("possible_crtcs = 0x%x\n", encoder->possible_crtcs);
  190. ret = drm_encoder_init(drm_dev, encoder, &rockchip_dp_encoder_funcs,
  191. DRM_MODE_ENCODER_TMDS, NULL);
  192. if (ret) {
  193. DRM_ERROR("failed to initialize encoder with drm\n");
  194. return ret;
  195. }
  196. drm_encoder_helper_add(encoder, &rockchip_dp_encoder_helper_funcs);
  197. return 0;
  198. }
  199. static int rockchip_dp_bind(struct device *dev, struct device *master,
  200. void *data)
  201. {
  202. struct rockchip_dp_device *dp = dev_get_drvdata(dev);
  203. struct drm_device *drm_dev = data;
  204. int ret;
  205. /*
  206. * Just like the probe function said, we don't need the
  207. * device drvrate anymore, we should leave the charge to
  208. * analogix dp driver, set the device drvdata to NULL.
  209. */
  210. dev_set_drvdata(dev, NULL);
  211. ret = rockchip_dp_init(dp);
  212. if (ret < 0)
  213. return ret;
  214. dp->drm_dev = drm_dev;
  215. ret = rockchip_dp_drm_create_encoder(dp);
  216. if (ret) {
  217. DRM_ERROR("failed to create drm encoder\n");
  218. return ret;
  219. }
  220. dp->plat_data.encoder = &dp->encoder;
  221. dp->plat_data.dev_type = RK3288_DP;
  222. dp->plat_data.power_on = rockchip_dp_poweron;
  223. dp->plat_data.power_off = rockchip_dp_powerdown;
  224. return analogix_dp_bind(dev, dp->drm_dev, &dp->plat_data);
  225. }
  226. static void rockchip_dp_unbind(struct device *dev, struct device *master,
  227. void *data)
  228. {
  229. return analogix_dp_unbind(dev, master, data);
  230. }
  231. static const struct component_ops rockchip_dp_component_ops = {
  232. .bind = rockchip_dp_bind,
  233. .unbind = rockchip_dp_unbind,
  234. };
  235. static int rockchip_dp_probe(struct platform_device *pdev)
  236. {
  237. struct device *dev = &pdev->dev;
  238. struct device_node *panel_node, *port, *endpoint;
  239. struct rockchip_dp_device *dp;
  240. struct drm_panel *panel;
  241. port = of_graph_get_port_by_id(dev->of_node, 1);
  242. if (!port) {
  243. dev_err(dev, "can't find output port\n");
  244. return -EINVAL;
  245. }
  246. endpoint = of_get_child_by_name(port, "endpoint");
  247. of_node_put(port);
  248. if (!endpoint) {
  249. dev_err(dev, "no output endpoint found\n");
  250. return -EINVAL;
  251. }
  252. panel_node = of_graph_get_remote_port_parent(endpoint);
  253. of_node_put(endpoint);
  254. if (!panel_node) {
  255. dev_err(dev, "no output node found\n");
  256. return -EINVAL;
  257. }
  258. panel = of_drm_find_panel(panel_node);
  259. if (!panel) {
  260. DRM_ERROR("failed to find panel\n");
  261. of_node_put(panel_node);
  262. return -EPROBE_DEFER;
  263. }
  264. of_node_put(panel_node);
  265. dp = devm_kzalloc(dev, sizeof(*dp), GFP_KERNEL);
  266. if (!dp)
  267. return -ENOMEM;
  268. dp->dev = dev;
  269. dp->plat_data.panel = panel;
  270. /*
  271. * We just use the drvdata until driver run into component
  272. * add function, and then we would set drvdata to null, so
  273. * that analogix dp driver could take charge of the drvdata.
  274. */
  275. platform_set_drvdata(pdev, dp);
  276. return component_add(dev, &rockchip_dp_component_ops);
  277. }
  278. static int rockchip_dp_remove(struct platform_device *pdev)
  279. {
  280. component_del(&pdev->dev, &rockchip_dp_component_ops);
  281. return 0;
  282. }
  283. #ifdef CONFIG_PM_SLEEP
  284. static int rockchip_dp_suspend(struct device *dev)
  285. {
  286. return analogix_dp_suspend(dev);
  287. }
  288. static int rockchip_dp_resume(struct device *dev)
  289. {
  290. return analogix_dp_resume(dev);
  291. }
  292. #endif
  293. static const struct dev_pm_ops rockchip_dp_pm_ops = {
  294. SET_SYSTEM_SLEEP_PM_OPS(rockchip_dp_suspend, rockchip_dp_resume)
  295. };
  296. static const struct of_device_id rockchip_dp_dt_ids[] = {
  297. {.compatible = "rockchip,rk3288-dp",},
  298. {}
  299. };
  300. MODULE_DEVICE_TABLE(of, rockchip_dp_dt_ids);
  301. static struct platform_driver rockchip_dp_driver = {
  302. .probe = rockchip_dp_probe,
  303. .remove = rockchip_dp_remove,
  304. .driver = {
  305. .name = "rockchip-dp",
  306. .owner = THIS_MODULE,
  307. .pm = &rockchip_dp_pm_ops,
  308. .of_match_table = of_match_ptr(rockchip_dp_dt_ids),
  309. },
  310. };
  311. module_platform_driver(rockchip_dp_driver);
  312. MODULE_AUTHOR("Yakir Yang <ykk@rock-chips.com>");
  313. MODULE_AUTHOR("Jeff chen <jeff.chen@rock-chips.com>");
  314. MODULE_DESCRIPTION("Rockchip Specific Analogix-DP Driver Extension");
  315. MODULE_LICENSE("GPL v2");