rcar_du_group.c 7.8 KB

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