rcar_du_crtc.c 20 KB

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