rcar_du_crtc.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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 struct drm_crtc_state *
  570. rcar_du_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
  571. {
  572. struct rcar_du_crtc_state *state;
  573. struct rcar_du_crtc_state *copy;
  574. if (WARN_ON(!crtc->state))
  575. return NULL;
  576. state = to_rcar_crtc_state(crtc->state);
  577. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  578. if (copy == NULL)
  579. return NULL;
  580. __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->state);
  581. return &copy->state;
  582. }
  583. static void rcar_du_crtc_atomic_destroy_state(struct drm_crtc *crtc,
  584. struct drm_crtc_state *state)
  585. {
  586. __drm_atomic_helper_crtc_destroy_state(state);
  587. kfree(to_rcar_crtc_state(state));
  588. }
  589. static void rcar_du_crtc_reset(struct drm_crtc *crtc)
  590. {
  591. struct rcar_du_crtc_state *state;
  592. if (crtc->state) {
  593. rcar_du_crtc_atomic_destroy_state(crtc, crtc->state);
  594. crtc->state = NULL;
  595. }
  596. state = kzalloc(sizeof(*state), GFP_KERNEL);
  597. if (state == NULL)
  598. return;
  599. state->crc.source = VSP1_DU_CRC_NONE;
  600. state->crc.index = 0;
  601. crtc->state = &state->state;
  602. crtc->state->crtc = crtc;
  603. }
  604. static int rcar_du_crtc_enable_vblank(struct drm_crtc *crtc)
  605. {
  606. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  607. rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
  608. rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
  609. rcrtc->vblank_enable = true;
  610. return 0;
  611. }
  612. static void rcar_du_crtc_disable_vblank(struct drm_crtc *crtc)
  613. {
  614. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  615. rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
  616. rcrtc->vblank_enable = false;
  617. }
  618. static int rcar_du_crtc_set_crc_source(struct drm_crtc *crtc,
  619. const char *source_name,
  620. size_t *values_cnt)
  621. {
  622. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  623. struct drm_modeset_acquire_ctx ctx;
  624. struct drm_crtc_state *crtc_state;
  625. struct drm_atomic_state *state;
  626. enum vsp1_du_crc_source source;
  627. unsigned int index = 0;
  628. unsigned int i;
  629. int ret;
  630. /*
  631. * Parse the source name. Supported values are "plane%u" to compute the
  632. * CRC on an input plane (%u is the plane ID), and "auto" to compute the
  633. * CRC on the composer (VSP) output.
  634. */
  635. if (!source_name) {
  636. source = VSP1_DU_CRC_NONE;
  637. } else if (!strcmp(source_name, "auto")) {
  638. source = VSP1_DU_CRC_OUTPUT;
  639. } else if (strstarts(source_name, "plane")) {
  640. source = VSP1_DU_CRC_PLANE;
  641. ret = kstrtouint(source_name + strlen("plane"), 10, &index);
  642. if (ret < 0)
  643. return ret;
  644. for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
  645. if (index == rcrtc->vsp->planes[i].plane.base.id) {
  646. index = i;
  647. break;
  648. }
  649. }
  650. if (i >= rcrtc->vsp->num_planes)
  651. return -EINVAL;
  652. } else {
  653. return -EINVAL;
  654. }
  655. *values_cnt = 1;
  656. /* Perform an atomic commit to set the CRC source. */
  657. drm_modeset_acquire_init(&ctx, 0);
  658. state = drm_atomic_state_alloc(crtc->dev);
  659. if (!state) {
  660. ret = -ENOMEM;
  661. goto unlock;
  662. }
  663. state->acquire_ctx = &ctx;
  664. retry:
  665. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  666. if (!IS_ERR(crtc_state)) {
  667. struct rcar_du_crtc_state *rcrtc_state;
  668. rcrtc_state = to_rcar_crtc_state(crtc_state);
  669. rcrtc_state->crc.source = source;
  670. rcrtc_state->crc.index = index;
  671. ret = drm_atomic_commit(state);
  672. } else {
  673. ret = PTR_ERR(crtc_state);
  674. }
  675. if (ret == -EDEADLK) {
  676. drm_atomic_state_clear(state);
  677. drm_modeset_backoff(&ctx);
  678. goto retry;
  679. }
  680. drm_atomic_state_put(state);
  681. unlock:
  682. drm_modeset_drop_locks(&ctx);
  683. drm_modeset_acquire_fini(&ctx);
  684. return ret;
  685. }
  686. static const struct drm_crtc_funcs crtc_funcs_gen2 = {
  687. .reset = rcar_du_crtc_reset,
  688. .destroy = drm_crtc_cleanup,
  689. .set_config = drm_atomic_helper_set_config,
  690. .page_flip = drm_atomic_helper_page_flip,
  691. .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
  692. .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
  693. .enable_vblank = rcar_du_crtc_enable_vblank,
  694. .disable_vblank = rcar_du_crtc_disable_vblank,
  695. };
  696. static const struct drm_crtc_funcs crtc_funcs_gen3 = {
  697. .reset = rcar_du_crtc_reset,
  698. .destroy = drm_crtc_cleanup,
  699. .set_config = drm_atomic_helper_set_config,
  700. .page_flip = drm_atomic_helper_page_flip,
  701. .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
  702. .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
  703. .enable_vblank = rcar_du_crtc_enable_vblank,
  704. .disable_vblank = rcar_du_crtc_disable_vblank,
  705. .set_crc_source = rcar_du_crtc_set_crc_source,
  706. };
  707. /* -----------------------------------------------------------------------------
  708. * Interrupt Handling
  709. */
  710. static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
  711. {
  712. struct rcar_du_crtc *rcrtc = arg;
  713. struct rcar_du_device *rcdu = rcrtc->group->dev;
  714. irqreturn_t ret = IRQ_NONE;
  715. u32 status;
  716. spin_lock(&rcrtc->vblank_lock);
  717. status = rcar_du_crtc_read(rcrtc, DSSR);
  718. rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
  719. if (status & DSSR_VBK) {
  720. /*
  721. * Wake up the vblank wait if the counter reaches 0. This must
  722. * be protected by the vblank_lock to avoid races in
  723. * rcar_du_crtc_disable_planes().
  724. */
  725. if (rcrtc->vblank_count) {
  726. if (--rcrtc->vblank_count == 0)
  727. wake_up(&rcrtc->vblank_wait);
  728. }
  729. }
  730. spin_unlock(&rcrtc->vblank_lock);
  731. if (status & DSSR_VBK) {
  732. if (rcdu->info->gen < 3) {
  733. drm_crtc_handle_vblank(&rcrtc->crtc);
  734. rcar_du_crtc_finish_page_flip(rcrtc);
  735. }
  736. ret = IRQ_HANDLED;
  737. }
  738. return ret;
  739. }
  740. /* -----------------------------------------------------------------------------
  741. * Initialization
  742. */
  743. int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int swindex,
  744. unsigned int hwindex)
  745. {
  746. static const unsigned int mmio_offsets[] = {
  747. DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET, DU3_REG_OFFSET
  748. };
  749. struct rcar_du_device *rcdu = rgrp->dev;
  750. struct platform_device *pdev = to_platform_device(rcdu->dev);
  751. struct rcar_du_crtc *rcrtc = &rcdu->crtcs[swindex];
  752. struct drm_crtc *crtc = &rcrtc->crtc;
  753. struct drm_plane *primary;
  754. unsigned int irqflags;
  755. struct clk *clk;
  756. char clk_name[9];
  757. char *name;
  758. int irq;
  759. int ret;
  760. /* Get the CRTC clock and the optional external clock. */
  761. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
  762. sprintf(clk_name, "du.%u", hwindex);
  763. name = clk_name;
  764. } else {
  765. name = NULL;
  766. }
  767. rcrtc->clock = devm_clk_get(rcdu->dev, name);
  768. if (IS_ERR(rcrtc->clock)) {
  769. dev_err(rcdu->dev, "no clock for DU channel %u\n", hwindex);
  770. return PTR_ERR(rcrtc->clock);
  771. }
  772. sprintf(clk_name, "dclkin.%u", hwindex);
  773. clk = devm_clk_get(rcdu->dev, clk_name);
  774. if (!IS_ERR(clk)) {
  775. rcrtc->extclock = clk;
  776. } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
  777. dev_info(rcdu->dev, "can't get external clock %u\n", hwindex);
  778. return -EPROBE_DEFER;
  779. }
  780. init_waitqueue_head(&rcrtc->flip_wait);
  781. init_waitqueue_head(&rcrtc->vblank_wait);
  782. spin_lock_init(&rcrtc->vblank_lock);
  783. rcrtc->group = rgrp;
  784. rcrtc->mmio_offset = mmio_offsets[hwindex];
  785. rcrtc->index = hwindex;
  786. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
  787. primary = &rcrtc->vsp->planes[rcrtc->vsp_pipe].plane;
  788. else
  789. primary = &rgrp->planes[swindex % 2].plane;
  790. ret = drm_crtc_init_with_planes(rcdu->ddev, crtc, primary, NULL,
  791. rcdu->info->gen <= 2 ?
  792. &crtc_funcs_gen2 : &crtc_funcs_gen3,
  793. NULL);
  794. if (ret < 0)
  795. return ret;
  796. drm_crtc_helper_add(crtc, &crtc_helper_funcs);
  797. /* Start with vertical blanking interrupt reporting disabled. */
  798. drm_crtc_vblank_off(crtc);
  799. /* Register the interrupt handler. */
  800. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
  801. /* The IRQ's are associated with the CRTC (sw)index. */
  802. irq = platform_get_irq(pdev, swindex);
  803. irqflags = 0;
  804. } else {
  805. irq = platform_get_irq(pdev, 0);
  806. irqflags = IRQF_SHARED;
  807. }
  808. if (irq < 0) {
  809. dev_err(rcdu->dev, "no IRQ for CRTC %u\n", swindex);
  810. return irq;
  811. }
  812. ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
  813. dev_name(rcdu->dev), rcrtc);
  814. if (ret < 0) {
  815. dev_err(rcdu->dev,
  816. "failed to register IRQ for CRTC %u\n", swindex);
  817. return ret;
  818. }
  819. return 0;
  820. }