rcar_du_crtc.c 18 KB

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