rcar_du_group.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * rcar_du_group.c -- R-Car Display Unit Channels Pair
  4. *
  5. * Copyright (C) 2013-2015 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. /*
  10. * The R8A7779 DU is split in per-CRTC resources (scan-out engine, blending
  11. * unit, timings generator, ...) and device-global resources (start/stop
  12. * control, planes, ...) shared between the two CRTCs.
  13. *
  14. * The R8A7790 introduced a third CRTC with its own set of global resources.
  15. * This would be modeled as two separate DU device instances if it wasn't for
  16. * a handful or resources that are shared between the three CRTCs (mostly
  17. * related to input and output routing). For this reason the R8A7790 DU must be
  18. * modeled as a single device with three CRTCs, two sets of "semi-global"
  19. * resources, and a few device-global resources.
  20. *
  21. * The rcar_du_group object is a driver specific object, without any real
  22. * counterpart in the DU documentation, that models those semi-global resources.
  23. */
  24. #include <linux/clk.h>
  25. #include <linux/io.h>
  26. #include "rcar_du_drv.h"
  27. #include "rcar_du_group.h"
  28. #include "rcar_du_regs.h"
  29. u32 rcar_du_group_read(struct rcar_du_group *rgrp, u32 reg)
  30. {
  31. return rcar_du_read(rgrp->dev, rgrp->mmio_offset + reg);
  32. }
  33. void rcar_du_group_write(struct rcar_du_group *rgrp, u32 reg, u32 data)
  34. {
  35. rcar_du_write(rgrp->dev, rgrp->mmio_offset + reg, data);
  36. }
  37. static void rcar_du_group_setup_pins(struct rcar_du_group *rgrp)
  38. {
  39. u32 defr6 = DEFR6_CODE;
  40. if (rgrp->channels_mask & BIT(0))
  41. defr6 |= DEFR6_ODPM02_DISP;
  42. if (rgrp->channels_mask & BIT(1))
  43. defr6 |= DEFR6_ODPM12_DISP;
  44. rcar_du_group_write(rgrp, DEFR6, defr6);
  45. }
  46. static void rcar_du_group_setup_defr8(struct rcar_du_group *rgrp)
  47. {
  48. struct rcar_du_device *rcdu = rgrp->dev;
  49. u32 defr8 = DEFR8_CODE;
  50. if (rcdu->info->gen < 3) {
  51. defr8 |= DEFR8_DEFE8;
  52. /*
  53. * On Gen2 the DEFR8 register for the first group also controls
  54. * RGB output routing to DPAD0 and VSPD1 routing to DU0/1/2 for
  55. * DU instances that support it.
  56. */
  57. if (rgrp->index == 0) {
  58. defr8 |= DEFR8_DRGBS_DU(rcdu->dpad0_source);
  59. if (rgrp->dev->vspd1_sink == 2)
  60. defr8 |= DEFR8_VSCS;
  61. }
  62. } else {
  63. /*
  64. * On Gen3 VSPD routing can't be configured, and DPAD routing
  65. * is set in the group corresponding to the DPAD output (no Gen3
  66. * SoC has multiple DPAD sources belonging to separate groups).
  67. */
  68. if (rgrp->index == rcdu->dpad0_source / 2)
  69. defr8 |= DEFR8_DRGBS_DU(rcdu->dpad0_source);
  70. }
  71. rcar_du_group_write(rgrp, DEFR8, defr8);
  72. }
  73. static void rcar_du_group_setup_didsr(struct rcar_du_group *rgrp)
  74. {
  75. struct rcar_du_device *rcdu = rgrp->dev;
  76. struct rcar_du_crtc *rcrtc;
  77. unsigned int num_crtcs = 0;
  78. unsigned int i;
  79. u32 didsr;
  80. /*
  81. * Configure input dot clock routing with a hardcoded configuration. If
  82. * the DU channel can use the LVDS encoder output clock as the dot
  83. * clock, do so. Otherwise route DU_DOTCLKINn signal to DUn.
  84. *
  85. * Each channel can then select between the dot clock configured here
  86. * and the clock provided by the CPG through the ESCR register.
  87. */
  88. if (rcdu->info->gen < 3 && rgrp->index == 0) {
  89. /*
  90. * On Gen2 a single register in the first group controls dot
  91. * clock selection for all channels.
  92. */
  93. rcrtc = rcdu->crtcs;
  94. num_crtcs = rcdu->num_crtcs;
  95. } else if (rcdu->info->gen == 3 && rgrp->num_crtcs > 1) {
  96. /*
  97. * On Gen3 dot clocks are setup through per-group registers,
  98. * only available when the group has two channels.
  99. */
  100. rcrtc = &rcdu->crtcs[rgrp->index * 2];
  101. num_crtcs = rgrp->num_crtcs;
  102. }
  103. if (!num_crtcs)
  104. return;
  105. didsr = DIDSR_CODE;
  106. for (i = 0; i < num_crtcs; ++i, ++rcrtc) {
  107. if (rcdu->info->lvds_clk_mask & BIT(rcrtc->index))
  108. didsr |= DIDSR_LCDS_LVDS0(i)
  109. | DIDSR_PDCS_CLK(i, 0);
  110. else
  111. didsr |= DIDSR_LCDS_DCLKIN(i)
  112. | DIDSR_PDCS_CLK(i, 0);
  113. }
  114. rcar_du_group_write(rgrp, DIDSR, didsr);
  115. }
  116. static void rcar_du_group_setup(struct rcar_du_group *rgrp)
  117. {
  118. struct rcar_du_device *rcdu = rgrp->dev;
  119. /* Enable extended features */
  120. rcar_du_group_write(rgrp, DEFR, DEFR_CODE | DEFR_DEFE);
  121. if (rcdu->info->gen < 3) {
  122. rcar_du_group_write(rgrp, DEFR2, DEFR2_CODE | DEFR2_DEFE2G);
  123. rcar_du_group_write(rgrp, DEFR3, DEFR3_CODE | DEFR3_DEFE3);
  124. rcar_du_group_write(rgrp, DEFR4, DEFR4_CODE);
  125. }
  126. rcar_du_group_write(rgrp, DEFR5, DEFR5_CODE | DEFR5_DEFE5);
  127. rcar_du_group_setup_pins(rgrp);
  128. if (rcar_du_has(rgrp->dev, RCAR_DU_FEATURE_EXT_CTRL_REGS)) {
  129. rcar_du_group_setup_defr8(rgrp);
  130. rcar_du_group_setup_didsr(rgrp);
  131. }
  132. if (rcdu->info->gen >= 3)
  133. rcar_du_group_write(rgrp, DEFR10, DEFR10_CODE | DEFR10_DEFE10);
  134. /*
  135. * Use DS1PR and DS2PR to configure planes priorities and connects the
  136. * superposition 0 to DU0 pins. DU1 pins will be configured dynamically.
  137. */
  138. rcar_du_group_write(rgrp, DORCR, DORCR_PG1D_DS1 | DORCR_DPRS);
  139. /* Apply planes to CRTCs association. */
  140. mutex_lock(&rgrp->lock);
  141. rcar_du_group_write(rgrp, DPTSR, (rgrp->dptsr_planes << 16) |
  142. rgrp->dptsr_planes);
  143. mutex_unlock(&rgrp->lock);
  144. }
  145. /*
  146. * rcar_du_group_get - Acquire a reference to the DU channels group
  147. *
  148. * Acquiring the first reference setups core registers. A reference must be held
  149. * before accessing any hardware registers.
  150. *
  151. * This function must be called with the DRM mode_config lock held.
  152. *
  153. * Return 0 in case of success or a negative error code otherwise.
  154. */
  155. int rcar_du_group_get(struct rcar_du_group *rgrp)
  156. {
  157. if (rgrp->use_count)
  158. goto done;
  159. rcar_du_group_setup(rgrp);
  160. done:
  161. rgrp->use_count++;
  162. return 0;
  163. }
  164. /*
  165. * rcar_du_group_put - Release a reference to the DU
  166. *
  167. * This function must be called with the DRM mode_config lock held.
  168. */
  169. void rcar_du_group_put(struct rcar_du_group *rgrp)
  170. {
  171. --rgrp->use_count;
  172. }
  173. static void __rcar_du_group_start_stop(struct rcar_du_group *rgrp, bool start)
  174. {
  175. struct rcar_du_device *rcdu = rgrp->dev;
  176. /*
  177. * Group start/stop is controlled by the DRES and DEN bits of DSYSR0
  178. * for the first group and DSYSR2 for the second group. On most DU
  179. * instances, this maps to the first CRTC of the group, and we can just
  180. * use rcar_du_crtc_dsysr_clr_set() to access the correct DSYSR. On
  181. * M3-N, however, DU2 doesn't exist, but DSYSR2 does. We thus need to
  182. * access the register directly using group read/write.
  183. */
  184. if (rcdu->info->channels_mask & BIT(rgrp->index * 2)) {
  185. struct rcar_du_crtc *rcrtc = &rgrp->dev->crtcs[rgrp->index * 2];
  186. rcar_du_crtc_dsysr_clr_set(rcrtc, DSYSR_DRES | DSYSR_DEN,
  187. start ? DSYSR_DEN : DSYSR_DRES);
  188. } else {
  189. rcar_du_group_write(rgrp, DSYSR,
  190. start ? DSYSR_DEN : DSYSR_DRES);
  191. }
  192. }
  193. void rcar_du_group_start_stop(struct rcar_du_group *rgrp, bool start)
  194. {
  195. /*
  196. * Many of the configuration bits are only updated when the display
  197. * reset (DRES) bit in DSYSR is set to 1, disabling *both* CRTCs. Some
  198. * of those bits could be pre-configured, but others (especially the
  199. * bits related to plane assignment to display timing controllers) need
  200. * to be modified at runtime.
  201. *
  202. * Restart the display controller if a start is requested. Sorry for the
  203. * flicker. It should be possible to move most of the "DRES-update" bits
  204. * setup to driver initialization time and minimize the number of cases
  205. * when the display controller will have to be restarted.
  206. */
  207. if (start) {
  208. if (rgrp->used_crtcs++ != 0)
  209. __rcar_du_group_start_stop(rgrp, false);
  210. __rcar_du_group_start_stop(rgrp, true);
  211. } else {
  212. if (--rgrp->used_crtcs == 0)
  213. __rcar_du_group_start_stop(rgrp, false);
  214. }
  215. }
  216. void rcar_du_group_restart(struct rcar_du_group *rgrp)
  217. {
  218. rgrp->need_restart = false;
  219. __rcar_du_group_start_stop(rgrp, false);
  220. __rcar_du_group_start_stop(rgrp, true);
  221. }
  222. int rcar_du_set_dpad0_vsp1_routing(struct rcar_du_device *rcdu)
  223. {
  224. struct rcar_du_group *rgrp;
  225. struct rcar_du_crtc *crtc;
  226. unsigned int index;
  227. int ret;
  228. if (!rcar_du_has(rcdu, RCAR_DU_FEATURE_EXT_CTRL_REGS))
  229. return 0;
  230. /*
  231. * RGB output routing to DPAD0 and VSP1D routing to DU0/1/2 are
  232. * configured in the DEFR8 register of the first group on Gen2 and the
  233. * last group on Gen3. As this function can be called with the DU
  234. * channels of the corresponding CRTCs disabled, we need to enable the
  235. * group clock before accessing the register.
  236. */
  237. index = rcdu->info->gen < 3 ? 0 : DIV_ROUND_UP(rcdu->num_crtcs, 2) - 1;
  238. rgrp = &rcdu->groups[index];
  239. crtc = &rcdu->crtcs[index * 2];
  240. ret = clk_prepare_enable(crtc->clock);
  241. if (ret < 0)
  242. return ret;
  243. rcar_du_group_setup_defr8(rgrp);
  244. clk_disable_unprepare(crtc->clock);
  245. return 0;
  246. }
  247. int rcar_du_group_set_routing(struct rcar_du_group *rgrp)
  248. {
  249. struct rcar_du_crtc *crtc0 = &rgrp->dev->crtcs[rgrp->index * 2];
  250. u32 dorcr = rcar_du_group_read(rgrp, DORCR);
  251. dorcr &= ~(DORCR_PG2T | DORCR_DK2S | DORCR_PG2D_MASK);
  252. /*
  253. * Set the DPAD1 pins sources. Select CRTC 0 if explicitly requested and
  254. * CRTC 1 in all other cases to avoid cloning CRTC 0 to DPAD0 and DPAD1
  255. * by default.
  256. */
  257. if (crtc0->outputs & BIT(RCAR_DU_OUTPUT_DPAD1))
  258. dorcr |= DORCR_PG2D_DS1;
  259. else
  260. dorcr |= DORCR_PG2T | DORCR_DK2S | DORCR_PG2D_DS2;
  261. rcar_du_group_write(rgrp, DORCR, dorcr);
  262. return rcar_du_set_dpad0_vsp1_routing(rgrp->dev);
  263. }