rcar_du_crtc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  1. /*
  2. * rcar_du_crtc.c -- R-Car Display Unit CRTCs
  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. #include <linux/clk.h>
  14. #include <linux/mutex.h>
  15. #include <linux/sys_soc.h>
  16. #include <drm/drmP.h>
  17. #include <drm/drm_atomic.h>
  18. #include <drm/drm_atomic_helper.h>
  19. #include <drm/drm_crtc.h>
  20. #include <drm/drm_crtc_helper.h>
  21. #include <drm/drm_fb_cma_helper.h>
  22. #include <drm/drm_gem_cma_helper.h>
  23. #include <drm/drm_plane_helper.h>
  24. #include "rcar_du_crtc.h"
  25. #include "rcar_du_drv.h"
  26. #include "rcar_du_kms.h"
  27. #include "rcar_du_plane.h"
  28. #include "rcar_du_regs.h"
  29. #include "rcar_du_vsp.h"
  30. static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
  31. {
  32. struct rcar_du_device *rcdu = rcrtc->group->dev;
  33. return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
  34. }
  35. static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
  36. {
  37. struct rcar_du_device *rcdu = rcrtc->group->dev;
  38. rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
  39. }
  40. static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
  41. {
  42. struct rcar_du_device *rcdu = rcrtc->group->dev;
  43. rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
  44. rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
  45. }
  46. static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
  47. {
  48. struct rcar_du_device *rcdu = rcrtc->group->dev;
  49. rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
  50. rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
  51. }
  52. static void rcar_du_crtc_clr_set(struct rcar_du_crtc *rcrtc, u32 reg,
  53. u32 clr, u32 set)
  54. {
  55. struct rcar_du_device *rcdu = rcrtc->group->dev;
  56. u32 value = rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
  57. rcar_du_write(rcdu, rcrtc->mmio_offset + reg, (value & ~clr) | set);
  58. }
  59. static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
  60. {
  61. int ret;
  62. ret = clk_prepare_enable(rcrtc->clock);
  63. if (ret < 0)
  64. return ret;
  65. ret = clk_prepare_enable(rcrtc->extclock);
  66. if (ret < 0)
  67. goto error_clock;
  68. ret = rcar_du_group_get(rcrtc->group);
  69. if (ret < 0)
  70. goto error_group;
  71. return 0;
  72. error_group:
  73. clk_disable_unprepare(rcrtc->extclock);
  74. error_clock:
  75. clk_disable_unprepare(rcrtc->clock);
  76. return ret;
  77. }
  78. static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
  79. {
  80. rcar_du_group_put(rcrtc->group);
  81. clk_disable_unprepare(rcrtc->extclock);
  82. clk_disable_unprepare(rcrtc->clock);
  83. }
  84. /* -----------------------------------------------------------------------------
  85. * Hardware Setup
  86. */
  87. struct dpll_info {
  88. unsigned int output;
  89. unsigned int fdpll;
  90. unsigned int n;
  91. unsigned int m;
  92. };
  93. static void rcar_du_dpll_divider(struct rcar_du_crtc *rcrtc,
  94. struct dpll_info *dpll,
  95. unsigned long input,
  96. unsigned long target)
  97. {
  98. unsigned long best_diff = (unsigned long)-1;
  99. unsigned long diff;
  100. unsigned int fdpll;
  101. unsigned int m;
  102. unsigned int n;
  103. /*
  104. * fin fvco fout fclkout
  105. * in --> [1/M] --> |PD| -> [LPF] -> [VCO] -> [1/P] -+-> [1/FDPLL] -> out
  106. * +-> | | |
  107. * | |
  108. * +---------------- [1/N] <------------+
  109. *
  110. * fclkout = fvco / P / FDPLL -- (1)
  111. *
  112. * fin/M = fvco/P/N
  113. *
  114. * fvco = fin * P * N / M -- (2)
  115. *
  116. * (1) + (2) indicates
  117. *
  118. * fclkout = fin * N / M / FDPLL
  119. *
  120. * NOTES
  121. * N : (n + 1)
  122. * M : (m + 1)
  123. * FDPLL : (fdpll + 1)
  124. * P : 2
  125. * 2kHz < fvco < 4096MHz
  126. *
  127. * To minimize the jitter,
  128. * N : as large as possible
  129. * M : as small as possible
  130. */
  131. for (m = 0; m < 4; m++) {
  132. for (n = 119; n > 38; n--) {
  133. /*
  134. * This code only runs on 64-bit architectures, the
  135. * unsigned long type can thus be used for 64-bit
  136. * computation. It will still compile without any
  137. * warning on 32-bit architectures.
  138. *
  139. * To optimize calculations, use fout instead of fvco
  140. * to verify the VCO frequency constraint.
  141. */
  142. unsigned long fout = input * (n + 1) / (m + 1);
  143. if (fout < 1000 || fout > 2048 * 1000 * 1000U)
  144. continue;
  145. for (fdpll = 1; fdpll < 32; fdpll++) {
  146. unsigned long output;
  147. output = fout / (fdpll + 1);
  148. if (output >= 400 * 1000 * 1000)
  149. continue;
  150. diff = abs((long)output - (long)target);
  151. if (best_diff > diff) {
  152. best_diff = diff;
  153. dpll->n = n;
  154. dpll->m = m;
  155. dpll->fdpll = fdpll;
  156. dpll->output = output;
  157. }
  158. if (diff == 0)
  159. goto done;
  160. }
  161. }
  162. }
  163. done:
  164. dev_dbg(rcrtc->group->dev->dev,
  165. "output:%u, fdpll:%u, n:%u, m:%u, diff:%lu\n",
  166. dpll->output, dpll->fdpll, dpll->n, dpll->m,
  167. best_diff);
  168. }
  169. static const struct soc_device_attribute rcar_du_r8a7795_es1[] = {
  170. { .soc_id = "r8a7795", .revision = "ES1.*" },
  171. { /* sentinel */ }
  172. };
  173. static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
  174. {
  175. const struct drm_display_mode *mode = &rcrtc->crtc.state->adjusted_mode;
  176. struct rcar_du_device *rcdu = rcrtc->group->dev;
  177. unsigned long mode_clock = mode->clock * 1000;
  178. unsigned long clk;
  179. u32 value;
  180. u32 escr;
  181. u32 div;
  182. /*
  183. * Compute the clock divisor and select the internal or external dot
  184. * clock based on the requested frequency.
  185. */
  186. clk = clk_get_rate(rcrtc->clock);
  187. div = DIV_ROUND_CLOSEST(clk, mode_clock);
  188. div = clamp(div, 1U, 64U) - 1;
  189. escr = div | ESCR_DCLKSEL_CLKS;
  190. if (rcrtc->extclock) {
  191. struct dpll_info dpll = { 0 };
  192. unsigned long extclk;
  193. unsigned long extrate;
  194. unsigned long rate;
  195. u32 extdiv;
  196. extclk = clk_get_rate(rcrtc->extclock);
  197. if (rcdu->info->dpll_ch & (1 << rcrtc->index)) {
  198. unsigned long target = mode_clock;
  199. /*
  200. * The H3 ES1.x exhibits dot clock duty cycle stability
  201. * issues. We can work around them by configuring the
  202. * DPLL to twice the desired frequency, coupled with a
  203. * /2 post-divider. This isn't needed on other SoCs and
  204. * breaks HDMI output on M3-W for a currently unknown
  205. * reason, so restrict the workaround to H3 ES1.x.
  206. */
  207. if (soc_device_match(rcar_du_r8a7795_es1))
  208. target *= 2;
  209. rcar_du_dpll_divider(rcrtc, &dpll, extclk, target);
  210. extclk = dpll.output;
  211. }
  212. extdiv = DIV_ROUND_CLOSEST(extclk, mode_clock);
  213. extdiv = clamp(extdiv, 1U, 64U) - 1;
  214. rate = clk / (div + 1);
  215. extrate = extclk / (extdiv + 1);
  216. if (abs((long)extrate - (long)mode_clock) <
  217. abs((long)rate - (long)mode_clock)) {
  218. if (rcdu->info->dpll_ch & (1 << rcrtc->index)) {
  219. u32 dpllcr = DPLLCR_CODE | DPLLCR_CLKE
  220. | DPLLCR_FDPLL(dpll.fdpll)
  221. | DPLLCR_N(dpll.n) | DPLLCR_M(dpll.m)
  222. | DPLLCR_STBY;
  223. if (rcrtc->index == 1)
  224. dpllcr |= DPLLCR_PLCS1
  225. | DPLLCR_INCS_DOTCLKIN1;
  226. else
  227. dpllcr |= DPLLCR_PLCS0
  228. | DPLLCR_INCS_DOTCLKIN0;
  229. rcar_du_group_write(rcrtc->group, DPLLCR,
  230. dpllcr);
  231. }
  232. escr = ESCR_DCLKSEL_DCLKIN | extdiv;
  233. }
  234. dev_dbg(rcrtc->group->dev->dev,
  235. "mode clock %lu extrate %lu rate %lu ESCR 0x%08x\n",
  236. mode_clock, extrate, rate, escr);
  237. }
  238. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? ESCR2 : ESCR,
  239. escr);
  240. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? OTAR2 : OTAR, 0);
  241. /* Signal polarities */
  242. value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? DSMR_VSL : 0)
  243. | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? DSMR_HSL : 0)
  244. | DSMR_DIPM_DISP | DSMR_CSPM;
  245. rcar_du_crtc_write(rcrtc, DSMR, value);
  246. /* Display timings */
  247. rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
  248. rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
  249. mode->hdisplay - 19);
  250. rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
  251. mode->hsync_start - 1);
  252. rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
  253. rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
  254. mode->crtc_vsync_end - 2);
  255. rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
  256. mode->crtc_vsync_end +
  257. mode->crtc_vdisplay - 2);
  258. rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
  259. mode->crtc_vsync_end +
  260. mode->crtc_vsync_start - 1);
  261. rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
  262. rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start - 1);
  263. rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
  264. }
  265. void rcar_du_crtc_route_output(struct drm_crtc *crtc,
  266. enum rcar_du_output output)
  267. {
  268. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  269. struct rcar_du_device *rcdu = rcrtc->group->dev;
  270. /*
  271. * Store the route from the CRTC output to the DU output. The DU will be
  272. * configured when starting the CRTC.
  273. */
  274. rcrtc->outputs |= BIT(output);
  275. /*
  276. * Store RGB routing to DPAD0, the hardware will be configured when
  277. * starting the CRTC.
  278. */
  279. if (output == RCAR_DU_OUTPUT_DPAD0)
  280. rcdu->dpad0_source = rcrtc->index;
  281. }
  282. static unsigned int plane_zpos(struct rcar_du_plane *plane)
  283. {
  284. return plane->plane.state->normalized_zpos;
  285. }
  286. static const struct rcar_du_format_info *
  287. plane_format(struct rcar_du_plane *plane)
  288. {
  289. return to_rcar_plane_state(plane->plane.state)->format;
  290. }
  291. static void rcar_du_crtc_update_planes(struct rcar_du_crtc *rcrtc)
  292. {
  293. struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
  294. struct rcar_du_device *rcdu = rcrtc->group->dev;
  295. unsigned int num_planes = 0;
  296. unsigned int dptsr_planes;
  297. unsigned int hwplanes = 0;
  298. unsigned int prio = 0;
  299. unsigned int i;
  300. u32 dspr = 0;
  301. for (i = 0; i < rcrtc->group->num_planes; ++i) {
  302. struct rcar_du_plane *plane = &rcrtc->group->planes[i];
  303. unsigned int j;
  304. if (plane->plane.state->crtc != &rcrtc->crtc ||
  305. !plane->plane.state->visible)
  306. continue;
  307. /* Insert the plane in the sorted planes array. */
  308. for (j = num_planes++; j > 0; --j) {
  309. if (plane_zpos(planes[j-1]) <= plane_zpos(plane))
  310. break;
  311. planes[j] = planes[j-1];
  312. }
  313. planes[j] = plane;
  314. prio += plane_format(plane)->planes * 4;
  315. }
  316. for (i = 0; i < num_planes; ++i) {
  317. struct rcar_du_plane *plane = planes[i];
  318. struct drm_plane_state *state = plane->plane.state;
  319. unsigned int index = to_rcar_plane_state(state)->hwindex;
  320. prio -= 4;
  321. dspr |= (index + 1) << prio;
  322. hwplanes |= 1 << index;
  323. if (plane_format(plane)->planes == 2) {
  324. index = (index + 1) % 8;
  325. prio -= 4;
  326. dspr |= (index + 1) << prio;
  327. hwplanes |= 1 << index;
  328. }
  329. }
  330. /* If VSP+DU integration is enabled the plane assignment is fixed. */
  331. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  332. if (rcdu->info->gen < 3) {
  333. dspr = (rcrtc->index % 2) + 1;
  334. hwplanes = 1 << (rcrtc->index % 2);
  335. } else {
  336. dspr = (rcrtc->index % 2) ? 3 : 1;
  337. hwplanes = 1 << ((rcrtc->index % 2) ? 2 : 0);
  338. }
  339. }
  340. /*
  341. * Update the planes to display timing and dot clock generator
  342. * associations.
  343. *
  344. * Updating the DPTSR register requires restarting the CRTC group,
  345. * resulting in visible flicker. To mitigate the issue only update the
  346. * association if needed by enabled planes. Planes being disabled will
  347. * keep their current association.
  348. */
  349. mutex_lock(&rcrtc->group->lock);
  350. dptsr_planes = rcrtc->index % 2 ? rcrtc->group->dptsr_planes | hwplanes
  351. : rcrtc->group->dptsr_planes & ~hwplanes;
  352. if (dptsr_planes != rcrtc->group->dptsr_planes) {
  353. rcar_du_group_write(rcrtc->group, DPTSR,
  354. (dptsr_planes << 16) | dptsr_planes);
  355. rcrtc->group->dptsr_planes = dptsr_planes;
  356. if (rcrtc->group->used_crtcs)
  357. rcar_du_group_restart(rcrtc->group);
  358. }
  359. /* Restart the group if plane sources have changed. */
  360. if (rcrtc->group->need_restart)
  361. rcar_du_group_restart(rcrtc->group);
  362. mutex_unlock(&rcrtc->group->lock);
  363. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
  364. dspr);
  365. }
  366. /* -----------------------------------------------------------------------------
  367. * Page Flip
  368. */
  369. void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
  370. {
  371. struct drm_pending_vblank_event *event;
  372. struct drm_device *dev = rcrtc->crtc.dev;
  373. unsigned long flags;
  374. spin_lock_irqsave(&dev->event_lock, flags);
  375. event = rcrtc->event;
  376. rcrtc->event = NULL;
  377. spin_unlock_irqrestore(&dev->event_lock, flags);
  378. if (event == NULL)
  379. return;
  380. spin_lock_irqsave(&dev->event_lock, flags);
  381. drm_crtc_send_vblank_event(&rcrtc->crtc, event);
  382. wake_up(&rcrtc->flip_wait);
  383. spin_unlock_irqrestore(&dev->event_lock, flags);
  384. drm_crtc_vblank_put(&rcrtc->crtc);
  385. }
  386. static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
  387. {
  388. struct drm_device *dev = rcrtc->crtc.dev;
  389. unsigned long flags;
  390. bool pending;
  391. spin_lock_irqsave(&dev->event_lock, flags);
  392. pending = rcrtc->event != NULL;
  393. spin_unlock_irqrestore(&dev->event_lock, flags);
  394. return pending;
  395. }
  396. static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
  397. {
  398. struct rcar_du_device *rcdu = rcrtc->group->dev;
  399. if (wait_event_timeout(rcrtc->flip_wait,
  400. !rcar_du_crtc_page_flip_pending(rcrtc),
  401. msecs_to_jiffies(50)))
  402. return;
  403. dev_warn(rcdu->dev, "page flip timeout\n");
  404. rcar_du_crtc_finish_page_flip(rcrtc);
  405. }
  406. /* -----------------------------------------------------------------------------
  407. * Start/Stop and Suspend/Resume
  408. */
  409. static void rcar_du_crtc_setup(struct rcar_du_crtc *rcrtc)
  410. {
  411. /* Set display off and background to black */
  412. rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
  413. rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
  414. /* Configure display timings and output routing */
  415. rcar_du_crtc_set_display_timing(rcrtc);
  416. rcar_du_group_set_routing(rcrtc->group);
  417. /* Start with all planes disabled. */
  418. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
  419. /* Enable the VSP compositor. */
  420. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
  421. rcar_du_vsp_enable(rcrtc);
  422. /* Turn vertical blanking interrupt reporting on. */
  423. drm_crtc_vblank_on(&rcrtc->crtc);
  424. }
  425. static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
  426. {
  427. bool interlaced;
  428. /*
  429. * Select master sync mode. This enables display operation in master
  430. * sync mode (with the HSYNC and VSYNC signals configured as outputs and
  431. * actively driven).
  432. */
  433. interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
  434. rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
  435. (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
  436. DSYSR_TVM_MASTER);
  437. rcar_du_group_start_stop(rcrtc->group, true);
  438. }
  439. static void rcar_du_crtc_disable_planes(struct rcar_du_crtc *rcrtc)
  440. {
  441. struct rcar_du_device *rcdu = rcrtc->group->dev;
  442. struct drm_crtc *crtc = &rcrtc->crtc;
  443. u32 status;
  444. /* Make sure vblank interrupts are enabled. */
  445. drm_crtc_vblank_get(crtc);
  446. /*
  447. * Disable planes and calculate how many vertical blanking interrupts we
  448. * have to wait for. If a vertical blanking interrupt has been triggered
  449. * but not processed yet, we don't know whether it occurred before or
  450. * after the planes got disabled. We thus have to wait for two vblank
  451. * interrupts in that case.
  452. */
  453. spin_lock_irq(&rcrtc->vblank_lock);
  454. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
  455. status = rcar_du_crtc_read(rcrtc, DSSR);
  456. rcrtc->vblank_count = status & DSSR_VBK ? 2 : 1;
  457. spin_unlock_irq(&rcrtc->vblank_lock);
  458. if (!wait_event_timeout(rcrtc->vblank_wait, rcrtc->vblank_count == 0,
  459. msecs_to_jiffies(100)))
  460. dev_warn(rcdu->dev, "vertical blanking timeout\n");
  461. drm_crtc_vblank_put(crtc);
  462. }
  463. static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
  464. {
  465. struct drm_crtc *crtc = &rcrtc->crtc;
  466. /*
  467. * Disable all planes and wait for the change to take effect. This is
  468. * required as the plane enable registers are updated on vblank, and no
  469. * vblank will occur once the CRTC is stopped. Disabling planes when
  470. * starting the CRTC thus wouldn't be enough as it would start scanning
  471. * out immediately from old frame buffers until the next vblank.
  472. *
  473. * This increases the CRTC stop delay, especially when multiple CRTCs
  474. * are stopped in one operation as we now wait for one vblank per CRTC.
  475. * Whether this can be improved needs to be researched.
  476. */
  477. rcar_du_crtc_disable_planes(rcrtc);
  478. /*
  479. * Disable vertical blanking interrupt reporting. We first need to wait
  480. * for page flip completion before stopping the CRTC as userspace
  481. * expects page flips to eventually complete.
  482. */
  483. rcar_du_crtc_wait_page_flip(rcrtc);
  484. drm_crtc_vblank_off(crtc);
  485. /* Disable the VSP compositor. */
  486. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
  487. rcar_du_vsp_disable(rcrtc);
  488. /*
  489. * Select switch sync mode. This stops display operation and configures
  490. * the HSYNC and VSYNC signals as inputs.
  491. */
  492. rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
  493. rcar_du_group_start_stop(rcrtc->group, false);
  494. }
  495. /* -----------------------------------------------------------------------------
  496. * CRTC Functions
  497. */
  498. static void rcar_du_crtc_atomic_enable(struct drm_crtc *crtc,
  499. struct drm_crtc_state *old_state)
  500. {
  501. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  502. /*
  503. * If the CRTC has already been setup by the .atomic_begin() handler we
  504. * can skip the setup stage.
  505. */
  506. if (!rcrtc->initialized) {
  507. rcar_du_crtc_get(rcrtc);
  508. rcar_du_crtc_setup(rcrtc);
  509. rcrtc->initialized = true;
  510. }
  511. rcar_du_crtc_start(rcrtc);
  512. }
  513. static void rcar_du_crtc_atomic_disable(struct drm_crtc *crtc,
  514. struct drm_crtc_state *old_state)
  515. {
  516. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  517. rcar_du_crtc_stop(rcrtc);
  518. rcar_du_crtc_put(rcrtc);
  519. spin_lock_irq(&crtc->dev->event_lock);
  520. if (crtc->state->event) {
  521. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  522. crtc->state->event = NULL;
  523. }
  524. spin_unlock_irq(&crtc->dev->event_lock);
  525. rcrtc->initialized = false;
  526. rcrtc->outputs = 0;
  527. }
  528. static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc,
  529. struct drm_crtc_state *old_crtc_state)
  530. {
  531. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  532. WARN_ON(!crtc->state->enable);
  533. /*
  534. * If a mode set is in progress we can be called with the CRTC disabled.
  535. * We then need to first setup the CRTC in order to configure planes.
  536. * The .atomic_enable() handler will notice and skip the CRTC setup.
  537. */
  538. if (!rcrtc->initialized) {
  539. rcar_du_crtc_get(rcrtc);
  540. rcar_du_crtc_setup(rcrtc);
  541. rcrtc->initialized = true;
  542. }
  543. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
  544. rcar_du_vsp_atomic_begin(rcrtc);
  545. }
  546. static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc,
  547. struct drm_crtc_state *old_crtc_state)
  548. {
  549. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  550. struct drm_device *dev = rcrtc->crtc.dev;
  551. unsigned long flags;
  552. rcar_du_crtc_update_planes(rcrtc);
  553. if (crtc->state->event) {
  554. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  555. spin_lock_irqsave(&dev->event_lock, flags);
  556. rcrtc->event = crtc->state->event;
  557. crtc->state->event = NULL;
  558. spin_unlock_irqrestore(&dev->event_lock, flags);
  559. }
  560. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
  561. rcar_du_vsp_atomic_flush(rcrtc);
  562. }
  563. static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
  564. .atomic_begin = rcar_du_crtc_atomic_begin,
  565. .atomic_flush = rcar_du_crtc_atomic_flush,
  566. .atomic_enable = rcar_du_crtc_atomic_enable,
  567. .atomic_disable = rcar_du_crtc_atomic_disable,
  568. };
  569. static int rcar_du_crtc_enable_vblank(struct drm_crtc *crtc)
  570. {
  571. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  572. rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
  573. rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
  574. rcrtc->vblank_enable = true;
  575. return 0;
  576. }
  577. static void rcar_du_crtc_disable_vblank(struct drm_crtc *crtc)
  578. {
  579. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  580. rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
  581. rcrtc->vblank_enable = false;
  582. }
  583. static const struct drm_crtc_funcs crtc_funcs = {
  584. .reset = drm_atomic_helper_crtc_reset,
  585. .destroy = drm_crtc_cleanup,
  586. .set_config = drm_atomic_helper_set_config,
  587. .page_flip = drm_atomic_helper_page_flip,
  588. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  589. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  590. .enable_vblank = rcar_du_crtc_enable_vblank,
  591. .disable_vblank = rcar_du_crtc_disable_vblank,
  592. };
  593. /* -----------------------------------------------------------------------------
  594. * Interrupt Handling
  595. */
  596. static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
  597. {
  598. struct rcar_du_crtc *rcrtc = arg;
  599. struct rcar_du_device *rcdu = rcrtc->group->dev;
  600. irqreturn_t ret = IRQ_NONE;
  601. u32 status;
  602. spin_lock(&rcrtc->vblank_lock);
  603. status = rcar_du_crtc_read(rcrtc, DSSR);
  604. rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
  605. if (status & DSSR_VBK) {
  606. /*
  607. * Wake up the vblank wait if the counter reaches 0. This must
  608. * be protected by the vblank_lock to avoid races in
  609. * rcar_du_crtc_disable_planes().
  610. */
  611. if (rcrtc->vblank_count) {
  612. if (--rcrtc->vblank_count == 0)
  613. wake_up(&rcrtc->vblank_wait);
  614. }
  615. }
  616. spin_unlock(&rcrtc->vblank_lock);
  617. if (status & DSSR_VBK) {
  618. if (rcdu->info->gen < 3) {
  619. drm_crtc_handle_vblank(&rcrtc->crtc);
  620. rcar_du_crtc_finish_page_flip(rcrtc);
  621. }
  622. ret = IRQ_HANDLED;
  623. }
  624. return ret;
  625. }
  626. /* -----------------------------------------------------------------------------
  627. * Initialization
  628. */
  629. int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
  630. {
  631. static const unsigned int mmio_offsets[] = {
  632. DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET, DU3_REG_OFFSET
  633. };
  634. struct rcar_du_device *rcdu = rgrp->dev;
  635. struct platform_device *pdev = to_platform_device(rcdu->dev);
  636. struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
  637. struct drm_crtc *crtc = &rcrtc->crtc;
  638. struct drm_plane *primary;
  639. unsigned int irqflags;
  640. struct clk *clk;
  641. char clk_name[9];
  642. char *name;
  643. int irq;
  644. int ret;
  645. /* Get the CRTC clock and the optional external clock. */
  646. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
  647. sprintf(clk_name, "du.%u", index);
  648. name = clk_name;
  649. } else {
  650. name = NULL;
  651. }
  652. rcrtc->clock = devm_clk_get(rcdu->dev, name);
  653. if (IS_ERR(rcrtc->clock)) {
  654. dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
  655. return PTR_ERR(rcrtc->clock);
  656. }
  657. sprintf(clk_name, "dclkin.%u", index);
  658. clk = devm_clk_get(rcdu->dev, clk_name);
  659. if (!IS_ERR(clk)) {
  660. rcrtc->extclock = clk;
  661. } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
  662. dev_info(rcdu->dev, "can't get external clock %u\n", index);
  663. return -EPROBE_DEFER;
  664. }
  665. init_waitqueue_head(&rcrtc->flip_wait);
  666. init_waitqueue_head(&rcrtc->vblank_wait);
  667. spin_lock_init(&rcrtc->vblank_lock);
  668. rcrtc->group = rgrp;
  669. rcrtc->mmio_offset = mmio_offsets[index];
  670. rcrtc->index = index;
  671. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
  672. primary = &rcrtc->vsp->planes[rcrtc->vsp_pipe].plane;
  673. else
  674. primary = &rgrp->planes[index % 2].plane;
  675. ret = drm_crtc_init_with_planes(rcdu->ddev, crtc, primary,
  676. NULL, &crtc_funcs, NULL);
  677. if (ret < 0)
  678. return ret;
  679. drm_crtc_helper_add(crtc, &crtc_helper_funcs);
  680. /* Start with vertical blanking interrupt reporting disabled. */
  681. drm_crtc_vblank_off(crtc);
  682. /* Register the interrupt handler. */
  683. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
  684. irq = platform_get_irq(pdev, index);
  685. irqflags = 0;
  686. } else {
  687. irq = platform_get_irq(pdev, 0);
  688. irqflags = IRQF_SHARED;
  689. }
  690. if (irq < 0) {
  691. dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
  692. return irq;
  693. }
  694. ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
  695. dev_name(rcdu->dev), rcrtc);
  696. if (ret < 0) {
  697. dev_err(rcdu->dev,
  698. "failed to register IRQ for CRTC %u\n", index);
  699. return ret;
  700. }
  701. return 0;
  702. }