rk_vop.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. * Copyright 2014 Rockchip Inc.
  5. */
  6. #include <common.h>
  7. #include <clk.h>
  8. #include <display.h>
  9. #include <dm.h>
  10. #include <edid.h>
  11. #include <regmap.h>
  12. #include <syscon.h>
  13. #include <video.h>
  14. #include <asm/gpio.h>
  15. #include <asm/hardware.h>
  16. #include <asm/io.h>
  17. #include <asm/arch/clock.h>
  18. #include <asm/arch/edp_rk3288.h>
  19. #include <asm/arch/vop_rk3288.h>
  20. #include <dm/device-internal.h>
  21. #include <dm/uclass-internal.h>
  22. #include <power/regulator.h>
  23. #include "rk_vop.h"
  24. DECLARE_GLOBAL_DATA_PTR;
  25. enum vop_pol {
  26. HSYNC_POSITIVE = 0,
  27. VSYNC_POSITIVE = 1,
  28. DEN_NEGATIVE = 2,
  29. DCLK_INVERT = 3
  30. };
  31. static void rkvop_enable(struct rk3288_vop *regs, ulong fbbase,
  32. int fb_bits_per_pixel,
  33. const struct display_timing *edid)
  34. {
  35. u32 lb_mode;
  36. u32 rgb_mode;
  37. u32 hactive = edid->hactive.typ;
  38. u32 vactive = edid->vactive.typ;
  39. writel(V_ACT_WIDTH(hactive - 1) | V_ACT_HEIGHT(vactive - 1),
  40. &regs->win0_act_info);
  41. writel(V_DSP_XST(edid->hsync_len.typ + edid->hback_porch.typ) |
  42. V_DSP_YST(edid->vsync_len.typ + edid->vback_porch.typ),
  43. &regs->win0_dsp_st);
  44. writel(V_DSP_WIDTH(hactive - 1) |
  45. V_DSP_HEIGHT(vactive - 1),
  46. &regs->win0_dsp_info);
  47. clrsetbits_le32(&regs->win0_color_key, M_WIN0_KEY_EN | M_WIN0_KEY_COLOR,
  48. V_WIN0_KEY_EN(0) | V_WIN0_KEY_COLOR(0));
  49. switch (fb_bits_per_pixel) {
  50. case 16:
  51. rgb_mode = RGB565;
  52. writel(V_RGB565_VIRWIDTH(hactive), &regs->win0_vir);
  53. break;
  54. case 24:
  55. rgb_mode = RGB888;
  56. writel(V_RGB888_VIRWIDTH(hactive), &regs->win0_vir);
  57. break;
  58. case 32:
  59. default:
  60. rgb_mode = ARGB8888;
  61. writel(V_ARGB888_VIRWIDTH(hactive), &regs->win0_vir);
  62. break;
  63. }
  64. if (hactive > 2560)
  65. lb_mode = LB_RGB_3840X2;
  66. else if (hactive > 1920)
  67. lb_mode = LB_RGB_2560X4;
  68. else if (hactive > 1280)
  69. lb_mode = LB_RGB_1920X5;
  70. else
  71. lb_mode = LB_RGB_1280X8;
  72. clrsetbits_le32(&regs->win0_ctrl0,
  73. M_WIN0_LB_MODE | M_WIN0_DATA_FMT | M_WIN0_EN,
  74. V_WIN0_LB_MODE(lb_mode) | V_WIN0_DATA_FMT(rgb_mode) |
  75. V_WIN0_EN(1));
  76. writel(fbbase, &regs->win0_yrgb_mst);
  77. writel(0x01, &regs->reg_cfg_done); /* enable reg config */
  78. }
  79. static void rkvop_set_pin_polarity(struct udevice *dev,
  80. enum vop_modes mode, u32 polarity)
  81. {
  82. struct rkvop_driverdata *ops =
  83. (struct rkvop_driverdata *)dev_get_driver_data(dev);
  84. if (ops->set_pin_polarity)
  85. ops->set_pin_polarity(dev, mode, polarity);
  86. }
  87. static void rkvop_enable_output(struct udevice *dev, enum vop_modes mode)
  88. {
  89. struct rk_vop_priv *priv = dev_get_priv(dev);
  90. struct rk3288_vop *regs = priv->regs;
  91. /* remove from standby */
  92. clrbits_le32(&regs->sys_ctrl, V_STANDBY_EN(1));
  93. switch (mode) {
  94. case VOP_MODE_HDMI:
  95. clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
  96. V_HDMI_OUT_EN(1));
  97. break;
  98. case VOP_MODE_EDP:
  99. clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
  100. V_EDP_OUT_EN(1));
  101. break;
  102. case VOP_MODE_LVDS:
  103. clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
  104. V_RGB_OUT_EN(1));
  105. break;
  106. case VOP_MODE_MIPI:
  107. clrsetbits_le32(&regs->sys_ctrl, M_ALL_OUT_EN,
  108. V_MIPI_OUT_EN(1));
  109. break;
  110. default:
  111. debug("%s: unsupported output mode %x\n", __func__, mode);
  112. }
  113. }
  114. static void rkvop_mode_set(struct udevice *dev,
  115. const struct display_timing *edid,
  116. enum vop_modes mode)
  117. {
  118. struct rk_vop_priv *priv = dev_get_priv(dev);
  119. struct rk3288_vop *regs = priv->regs;
  120. struct rkvop_driverdata *data =
  121. (struct rkvop_driverdata *)dev_get_driver_data(dev);
  122. u32 hactive = edid->hactive.typ;
  123. u32 vactive = edid->vactive.typ;
  124. u32 hsync_len = edid->hsync_len.typ;
  125. u32 hback_porch = edid->hback_porch.typ;
  126. u32 vsync_len = edid->vsync_len.typ;
  127. u32 vback_porch = edid->vback_porch.typ;
  128. u32 hfront_porch = edid->hfront_porch.typ;
  129. u32 vfront_porch = edid->vfront_porch.typ;
  130. int mode_flags;
  131. u32 pin_polarity;
  132. pin_polarity = BIT(DCLK_INVERT);
  133. if (edid->flags & DISPLAY_FLAGS_HSYNC_HIGH)
  134. pin_polarity |= BIT(HSYNC_POSITIVE);
  135. if (edid->flags & DISPLAY_FLAGS_VSYNC_HIGH)
  136. pin_polarity |= BIT(VSYNC_POSITIVE);
  137. rkvop_set_pin_polarity(dev, mode, pin_polarity);
  138. rkvop_enable_output(dev, mode);
  139. mode_flags = 0; /* RGB888 */
  140. if ((data->features & VOP_FEATURE_OUTPUT_10BIT) &&
  141. (mode == VOP_MODE_HDMI || mode == VOP_MODE_EDP))
  142. mode_flags = 15; /* RGBaaa */
  143. clrsetbits_le32(&regs->dsp_ctrl0, M_DSP_OUT_MODE,
  144. V_DSP_OUT_MODE(mode_flags));
  145. writel(V_HSYNC(hsync_len) |
  146. V_HORPRD(hsync_len + hback_porch + hactive + hfront_porch),
  147. &regs->dsp_htotal_hs_end);
  148. writel(V_HEAP(hsync_len + hback_porch + hactive) |
  149. V_HASP(hsync_len + hback_porch),
  150. &regs->dsp_hact_st_end);
  151. writel(V_VSYNC(vsync_len) |
  152. V_VERPRD(vsync_len + vback_porch + vactive + vfront_porch),
  153. &regs->dsp_vtotal_vs_end);
  154. writel(V_VAEP(vsync_len + vback_porch + vactive)|
  155. V_VASP(vsync_len + vback_porch),
  156. &regs->dsp_vact_st_end);
  157. writel(V_HEAP(hsync_len + hback_porch + hactive) |
  158. V_HASP(hsync_len + hback_porch),
  159. &regs->post_dsp_hact_info);
  160. writel(V_VAEP(vsync_len + vback_porch + vactive)|
  161. V_VASP(vsync_len + vback_porch),
  162. &regs->post_dsp_vact_info);
  163. writel(0x01, &regs->reg_cfg_done); /* enable reg config */
  164. }
  165. /**
  166. * rk_display_init() - Try to enable the given display device
  167. *
  168. * This function performs many steps:
  169. * - Finds the display device being referenced by @ep_node
  170. * - Puts the VOP's ID into its uclass platform data
  171. * - Probes the device to set it up
  172. * - Reads the EDID timing information
  173. * - Sets up the VOP clocks, etc. for the selected pixel clock and display mode
  174. * - Enables the display (the display device handles this and will do different
  175. * things depending on the display type)
  176. * - Tells the uclass about the display resolution so that the console will
  177. * appear correctly
  178. *
  179. * @dev: VOP device that we want to connect to the display
  180. * @fbbase: Frame buffer address
  181. * @ep_node: Device tree node to process - this is the offset of an endpoint
  182. * node within the VOP's 'port' list.
  183. * @return 0 if OK, -ve if something went wrong
  184. */
  185. static int rk_display_init(struct udevice *dev, ulong fbbase, ofnode ep_node)
  186. {
  187. struct video_priv *uc_priv = dev_get_uclass_priv(dev);
  188. struct rk_vop_priv *priv = dev_get_priv(dev);
  189. int vop_id, remote_vop_id;
  190. struct rk3288_vop *regs = priv->regs;
  191. struct display_timing timing;
  192. struct udevice *disp;
  193. int ret;
  194. u32 remote_phandle;
  195. struct display_plat *disp_uc_plat;
  196. struct clk clk;
  197. enum video_log2_bpp l2bpp;
  198. ofnode remote;
  199. debug("%s(%s, %lu, %s)\n", __func__,
  200. dev_read_name(dev), fbbase, ofnode_get_name(ep_node));
  201. vop_id = ofnode_read_s32_default(ep_node, "reg", -1);
  202. debug("vop_id=%d\n", vop_id);
  203. ret = ofnode_read_u32(ep_node, "remote-endpoint", &remote_phandle);
  204. if (ret)
  205. return ret;
  206. remote = ofnode_get_by_phandle(remote_phandle);
  207. if (!ofnode_valid(remote))
  208. return -EINVAL;
  209. remote_vop_id = ofnode_read_u32_default(remote, "reg", -1);
  210. debug("remote vop_id=%d\n", remote_vop_id);
  211. /*
  212. * The remote-endpoint references into a subnode of the encoder
  213. * (i.e. HDMI, MIPI, etc.) with the DTS looking something like
  214. * the following (assume 'hdmi_in_vopl' to be referenced):
  215. *
  216. * hdmi: hdmi@ff940000 {
  217. * ports {
  218. * hdmi_in: port {
  219. * hdmi_in_vopb: endpoint@0 { ... };
  220. * hdmi_in_vopl: endpoint@1 { ... };
  221. * }
  222. * }
  223. * }
  224. *
  225. * The original code had 3 steps of "walking the parent", but
  226. * a much better (as in: less likely to break if the DTS
  227. * changes) way of doing this is to "find the enclosing device
  228. * of UCLASS_DISPLAY".
  229. */
  230. while (ofnode_valid(remote)) {
  231. remote = ofnode_get_parent(remote);
  232. if (!ofnode_valid(remote)) {
  233. debug("%s(%s): no UCLASS_DISPLAY for remote-endpoint\n",
  234. __func__, dev_read_name(dev));
  235. return -EINVAL;
  236. }
  237. uclass_find_device_by_ofnode(UCLASS_DISPLAY, remote, &disp);
  238. if (disp)
  239. break;
  240. };
  241. disp_uc_plat = dev_get_uclass_platdata(disp);
  242. debug("Found device '%s', disp_uc_priv=%p\n", disp->name, disp_uc_plat);
  243. if (display_in_use(disp)) {
  244. debug(" - device in use\n");
  245. return -EBUSY;
  246. }
  247. disp_uc_plat->source_id = remote_vop_id;
  248. disp_uc_plat->src_dev = dev;
  249. ret = device_probe(disp);
  250. if (ret) {
  251. debug("%s: device '%s' display won't probe (ret=%d)\n",
  252. __func__, dev->name, ret);
  253. return ret;
  254. }
  255. ret = display_read_timing(disp, &timing);
  256. if (ret) {
  257. debug("%s: Failed to read timings\n", __func__);
  258. return ret;
  259. }
  260. ret = clk_get_by_index(dev, 1, &clk);
  261. if (!ret)
  262. ret = clk_set_rate(&clk, timing.pixelclock.typ);
  263. if (IS_ERR_VALUE(ret)) {
  264. debug("%s: Failed to set pixel clock: ret=%d\n", __func__, ret);
  265. return ret;
  266. }
  267. /* Set bitwidth for vop display according to vop mode */
  268. switch (vop_id) {
  269. case VOP_MODE_EDP:
  270. case VOP_MODE_LVDS:
  271. l2bpp = VIDEO_BPP16;
  272. break;
  273. case VOP_MODE_HDMI:
  274. case VOP_MODE_MIPI:
  275. l2bpp = VIDEO_BPP32;
  276. break;
  277. default:
  278. l2bpp = VIDEO_BPP16;
  279. }
  280. rkvop_mode_set(dev, &timing, vop_id);
  281. rkvop_enable(regs, fbbase, 1 << l2bpp, &timing);
  282. ret = display_enable(disp, 1 << l2bpp, &timing);
  283. if (ret)
  284. return ret;
  285. uc_priv->xsize = timing.hactive.typ;
  286. uc_priv->ysize = timing.vactive.typ;
  287. uc_priv->bpix = l2bpp;
  288. debug("fb=%lx, size=%d %d\n", fbbase, uc_priv->xsize, uc_priv->ysize);
  289. return 0;
  290. }
  291. void rk_vop_probe_regulators(struct udevice *dev,
  292. const char * const *names, int cnt)
  293. {
  294. int i, ret;
  295. const char *name;
  296. struct udevice *reg;
  297. for (i = 0; i < cnt; ++i) {
  298. name = names[i];
  299. debug("%s: probing regulator '%s'\n", dev->name, name);
  300. ret = regulator_autoset_by_name(name, &reg);
  301. if (!ret)
  302. ret = regulator_set_enable(reg, true);
  303. }
  304. }
  305. int rk_vop_probe(struct udevice *dev)
  306. {
  307. struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
  308. struct rk_vop_priv *priv = dev_get_priv(dev);
  309. int ret = 0;
  310. ofnode port, node;
  311. /* Before relocation we don't need to do anything */
  312. if (!(gd->flags & GD_FLG_RELOC))
  313. return 0;
  314. priv->regs = (struct rk3288_vop *)dev_read_addr(dev);
  315. /*
  316. * Try all the ports until we find one that works. In practice this
  317. * tries EDP first if available, then HDMI.
  318. *
  319. * Note that rockchip_vop_set_clk() always uses NPLL as the source
  320. * clock so it is currently not possible to use more than one display
  321. * device simultaneously.
  322. */
  323. port = dev_read_subnode(dev, "port");
  324. if (!ofnode_valid(port)) {
  325. debug("%s(%s): 'port' subnode not found\n",
  326. __func__, dev_read_name(dev));
  327. return -EINVAL;
  328. }
  329. for (node = ofnode_first_subnode(port);
  330. ofnode_valid(node);
  331. node = dev_read_next_subnode(node)) {
  332. ret = rk_display_init(dev, plat->base, node);
  333. if (ret)
  334. debug("Device failed: ret=%d\n", ret);
  335. if (!ret)
  336. break;
  337. }
  338. video_set_flush_dcache(dev, 1);
  339. return ret;
  340. }
  341. int rk_vop_bind(struct udevice *dev)
  342. {
  343. struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
  344. plat->size = 4 * (CONFIG_VIDEO_ROCKCHIP_MAX_XRES *
  345. CONFIG_VIDEO_ROCKCHIP_MAX_YRES);
  346. return 0;
  347. }