rcar_du_crtc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  1. /*
  2. * rcar_du_crtc.c -- R-Car Display Unit CRTCs
  3. *
  4. * Copyright (C) 2013-2014 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. static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
  29. {
  30. struct rcar_du_device *rcdu = rcrtc->group->dev;
  31. return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
  32. }
  33. static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
  34. {
  35. struct rcar_du_device *rcdu = rcrtc->group->dev;
  36. rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
  37. }
  38. static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
  39. {
  40. struct rcar_du_device *rcdu = rcrtc->group->dev;
  41. rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
  42. rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
  43. }
  44. static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
  45. {
  46. struct rcar_du_device *rcdu = rcrtc->group->dev;
  47. rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
  48. rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
  49. }
  50. static void rcar_du_crtc_clr_set(struct rcar_du_crtc *rcrtc, u32 reg,
  51. u32 clr, u32 set)
  52. {
  53. struct rcar_du_device *rcdu = rcrtc->group->dev;
  54. u32 value = rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
  55. rcar_du_write(rcdu, rcrtc->mmio_offset + reg, (value & ~clr) | set);
  56. }
  57. static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
  58. {
  59. int ret;
  60. ret = clk_prepare_enable(rcrtc->clock);
  61. if (ret < 0)
  62. return ret;
  63. ret = clk_prepare_enable(rcrtc->extclock);
  64. if (ret < 0)
  65. goto error_clock;
  66. ret = rcar_du_group_get(rcrtc->group);
  67. if (ret < 0)
  68. goto error_group;
  69. return 0;
  70. error_group:
  71. clk_disable_unprepare(rcrtc->extclock);
  72. error_clock:
  73. clk_disable_unprepare(rcrtc->clock);
  74. return ret;
  75. }
  76. static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
  77. {
  78. rcar_du_group_put(rcrtc->group);
  79. clk_disable_unprepare(rcrtc->extclock);
  80. clk_disable_unprepare(rcrtc->clock);
  81. }
  82. /* -----------------------------------------------------------------------------
  83. * Hardware Setup
  84. */
  85. static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
  86. {
  87. const struct drm_display_mode *mode = &rcrtc->crtc.state->adjusted_mode;
  88. unsigned long mode_clock = mode->clock * 1000;
  89. unsigned long clk;
  90. u32 value;
  91. u32 escr;
  92. u32 div;
  93. /* Compute the clock divisor and select the internal or external dot
  94. * clock based on the requested frequency.
  95. */
  96. clk = clk_get_rate(rcrtc->clock);
  97. div = DIV_ROUND_CLOSEST(clk, mode_clock);
  98. div = clamp(div, 1U, 64U) - 1;
  99. escr = div | ESCR_DCLKSEL_CLKS;
  100. if (rcrtc->extclock) {
  101. unsigned long extclk;
  102. unsigned long extrate;
  103. unsigned long rate;
  104. u32 extdiv;
  105. extclk = clk_get_rate(rcrtc->extclock);
  106. extdiv = DIV_ROUND_CLOSEST(extclk, mode_clock);
  107. extdiv = clamp(extdiv, 1U, 64U) - 1;
  108. rate = clk / (div + 1);
  109. extrate = extclk / (extdiv + 1);
  110. if (abs((long)extrate - (long)mode_clock) <
  111. abs((long)rate - (long)mode_clock)) {
  112. dev_dbg(rcrtc->group->dev->dev,
  113. "crtc%u: using external clock\n", rcrtc->index);
  114. escr = extdiv | ESCR_DCLKSEL_DCLKIN;
  115. }
  116. }
  117. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? ESCR2 : ESCR,
  118. escr);
  119. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? OTAR2 : OTAR, 0);
  120. /* Signal polarities */
  121. value = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? 0 : DSMR_VSL)
  122. | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? 0 : DSMR_HSL)
  123. | DSMR_DIPM_DE | DSMR_CSPM;
  124. rcar_du_crtc_write(rcrtc, DSMR, value);
  125. /* Display timings */
  126. rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
  127. rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
  128. mode->hdisplay - 19);
  129. rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
  130. mode->hsync_start - 1);
  131. rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
  132. rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
  133. mode->crtc_vsync_end - 2);
  134. rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
  135. mode->crtc_vsync_end +
  136. mode->crtc_vdisplay - 2);
  137. rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
  138. mode->crtc_vsync_end +
  139. mode->crtc_vsync_start - 1);
  140. rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
  141. rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start);
  142. rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
  143. }
  144. void rcar_du_crtc_route_output(struct drm_crtc *crtc,
  145. enum rcar_du_output output)
  146. {
  147. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  148. struct rcar_du_device *rcdu = rcrtc->group->dev;
  149. /* Store the route from the CRTC output to the DU output. The DU will be
  150. * configured when starting the CRTC.
  151. */
  152. rcrtc->outputs |= BIT(output);
  153. /* Store RGB routing to DPAD0, the hardware will be configured when
  154. * starting the CRTC.
  155. */
  156. if (output == RCAR_DU_OUTPUT_DPAD0)
  157. rcdu->dpad0_source = rcrtc->index;
  158. }
  159. static unsigned int plane_zpos(struct rcar_du_plane *plane)
  160. {
  161. return to_rcar_plane_state(plane->plane.state)->zpos;
  162. }
  163. static const struct rcar_du_format_info *
  164. plane_format(struct rcar_du_plane *plane)
  165. {
  166. return to_rcar_plane_state(plane->plane.state)->format;
  167. }
  168. static void rcar_du_crtc_update_planes(struct rcar_du_crtc *rcrtc)
  169. {
  170. struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
  171. unsigned int num_planes = 0;
  172. unsigned int dptsr_planes;
  173. unsigned int hwplanes = 0;
  174. unsigned int prio = 0;
  175. unsigned int i;
  176. u32 dspr = 0;
  177. for (i = 0; i < rcrtc->group->num_planes; ++i) {
  178. struct rcar_du_plane *plane = &rcrtc->group->planes[i];
  179. unsigned int j;
  180. if (plane->plane.state->crtc != &rcrtc->crtc)
  181. continue;
  182. /* Insert the plane in the sorted planes array. */
  183. for (j = num_planes++; j > 0; --j) {
  184. if (plane_zpos(planes[j-1]) <= plane_zpos(plane))
  185. break;
  186. planes[j] = planes[j-1];
  187. }
  188. planes[j] = plane;
  189. prio += plane_format(plane)->planes * 4;
  190. }
  191. for (i = 0; i < num_planes; ++i) {
  192. struct rcar_du_plane *plane = planes[i];
  193. struct drm_plane_state *state = plane->plane.state;
  194. unsigned int index = to_rcar_plane_state(state)->hwindex;
  195. prio -= 4;
  196. dspr |= (index + 1) << prio;
  197. hwplanes |= 1 << index;
  198. if (plane_format(plane)->planes == 2) {
  199. index = (index + 1) % 8;
  200. prio -= 4;
  201. dspr |= (index + 1) << prio;
  202. hwplanes |= 1 << index;
  203. }
  204. }
  205. /* Update the planes to display timing and dot clock generator
  206. * associations.
  207. *
  208. * Updating the DPTSR register requires restarting the CRTC group,
  209. * resulting in visible flicker. To mitigate the issue only update the
  210. * association if needed by enabled planes. Planes being disabled will
  211. * keep their current association.
  212. */
  213. mutex_lock(&rcrtc->group->lock);
  214. dptsr_planes = rcrtc->index % 2 ? rcrtc->group->dptsr_planes | hwplanes
  215. : rcrtc->group->dptsr_planes & ~hwplanes;
  216. if (dptsr_planes != rcrtc->group->dptsr_planes) {
  217. rcar_du_group_write(rcrtc->group, DPTSR,
  218. (dptsr_planes << 16) | dptsr_planes);
  219. rcrtc->group->dptsr_planes = dptsr_planes;
  220. if (rcrtc->group->used_crtcs)
  221. rcar_du_group_restart(rcrtc->group);
  222. }
  223. mutex_unlock(&rcrtc->group->lock);
  224. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
  225. dspr);
  226. }
  227. /* -----------------------------------------------------------------------------
  228. * Page Flip
  229. */
  230. void rcar_du_crtc_cancel_page_flip(struct rcar_du_crtc *rcrtc,
  231. struct drm_file *file)
  232. {
  233. struct drm_pending_vblank_event *event;
  234. struct drm_device *dev = rcrtc->crtc.dev;
  235. unsigned long flags;
  236. /* Destroy the pending vertical blanking event associated with the
  237. * pending page flip, if any, and disable vertical blanking interrupts.
  238. */
  239. spin_lock_irqsave(&dev->event_lock, flags);
  240. event = rcrtc->event;
  241. if (event && event->base.file_priv == file) {
  242. rcrtc->event = NULL;
  243. event->base.destroy(&event->base);
  244. drm_crtc_vblank_put(&rcrtc->crtc);
  245. }
  246. spin_unlock_irqrestore(&dev->event_lock, flags);
  247. }
  248. static void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
  249. {
  250. struct drm_pending_vblank_event *event;
  251. struct drm_device *dev = rcrtc->crtc.dev;
  252. unsigned long flags;
  253. spin_lock_irqsave(&dev->event_lock, flags);
  254. event = rcrtc->event;
  255. rcrtc->event = NULL;
  256. spin_unlock_irqrestore(&dev->event_lock, flags);
  257. if (event == NULL)
  258. return;
  259. spin_lock_irqsave(&dev->event_lock, flags);
  260. drm_send_vblank_event(dev, rcrtc->index, event);
  261. wake_up(&rcrtc->flip_wait);
  262. spin_unlock_irqrestore(&dev->event_lock, flags);
  263. drm_crtc_vblank_put(&rcrtc->crtc);
  264. }
  265. static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
  266. {
  267. struct drm_device *dev = rcrtc->crtc.dev;
  268. unsigned long flags;
  269. bool pending;
  270. spin_lock_irqsave(&dev->event_lock, flags);
  271. pending = rcrtc->event != NULL;
  272. spin_unlock_irqrestore(&dev->event_lock, flags);
  273. return pending;
  274. }
  275. static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
  276. {
  277. struct rcar_du_device *rcdu = rcrtc->group->dev;
  278. if (wait_event_timeout(rcrtc->flip_wait,
  279. !rcar_du_crtc_page_flip_pending(rcrtc),
  280. msecs_to_jiffies(50)))
  281. return;
  282. dev_warn(rcdu->dev, "page flip timeout\n");
  283. rcar_du_crtc_finish_page_flip(rcrtc);
  284. }
  285. /* -----------------------------------------------------------------------------
  286. * Start/Stop and Suspend/Resume
  287. */
  288. static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
  289. {
  290. struct drm_crtc *crtc = &rcrtc->crtc;
  291. bool interlaced;
  292. if (rcrtc->started)
  293. return;
  294. /* Set display off and background to black */
  295. rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
  296. rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
  297. /* Configure display timings and output routing */
  298. rcar_du_crtc_set_display_timing(rcrtc);
  299. rcar_du_group_set_routing(rcrtc->group);
  300. /* Start with all planes disabled. */
  301. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
  302. /* Select master sync mode. This enables display operation in master
  303. * sync mode (with the HSYNC and VSYNC signals configured as outputs and
  304. * actively driven).
  305. */
  306. interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
  307. rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
  308. (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
  309. DSYSR_TVM_MASTER);
  310. rcar_du_group_start_stop(rcrtc->group, true);
  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. /* Select switch sync mode. This stops display operation and configures
  339. * the HSYNC and VSYNC signals as inputs.
  340. */
  341. rcar_du_crtc_clr_set(rcrtc, DSYSR, DSYSR_TVM_MASK, DSYSR_TVM_SWITCH);
  342. rcar_du_group_start_stop(rcrtc->group, false);
  343. rcrtc->started = false;
  344. }
  345. void rcar_du_crtc_suspend(struct rcar_du_crtc *rcrtc)
  346. {
  347. rcar_du_crtc_stop(rcrtc);
  348. rcar_du_crtc_put(rcrtc);
  349. }
  350. void rcar_du_crtc_resume(struct rcar_du_crtc *rcrtc)
  351. {
  352. unsigned int i;
  353. if (!rcrtc->enabled)
  354. return;
  355. rcar_du_crtc_get(rcrtc);
  356. rcar_du_crtc_start(rcrtc);
  357. /* Commit the planes state. */
  358. for (i = 0; i < rcrtc->group->num_planes; ++i) {
  359. struct rcar_du_plane *plane = &rcrtc->group->planes[i];
  360. if (plane->plane.state->crtc != &rcrtc->crtc)
  361. continue;
  362. rcar_du_plane_setup(plane);
  363. }
  364. rcar_du_crtc_update_planes(rcrtc);
  365. }
  366. /* -----------------------------------------------------------------------------
  367. * CRTC Functions
  368. */
  369. static void rcar_du_crtc_enable(struct drm_crtc *crtc)
  370. {
  371. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  372. if (rcrtc->enabled)
  373. return;
  374. rcar_du_crtc_get(rcrtc);
  375. rcar_du_crtc_start(rcrtc);
  376. rcrtc->enabled = true;
  377. }
  378. static void rcar_du_crtc_disable(struct drm_crtc *crtc)
  379. {
  380. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  381. if (!rcrtc->enabled)
  382. return;
  383. rcar_du_crtc_stop(rcrtc);
  384. rcar_du_crtc_put(rcrtc);
  385. rcrtc->enabled = false;
  386. rcrtc->outputs = 0;
  387. }
  388. static bool rcar_du_crtc_mode_fixup(struct drm_crtc *crtc,
  389. const struct drm_display_mode *mode,
  390. struct drm_display_mode *adjusted_mode)
  391. {
  392. /* TODO Fixup modes */
  393. return true;
  394. }
  395. static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc)
  396. {
  397. struct drm_pending_vblank_event *event = crtc->state->event;
  398. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  399. struct drm_device *dev = rcrtc->crtc.dev;
  400. unsigned long flags;
  401. if (event) {
  402. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  403. spin_lock_irqsave(&dev->event_lock, flags);
  404. rcrtc->event = event;
  405. spin_unlock_irqrestore(&dev->event_lock, flags);
  406. }
  407. }
  408. static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc)
  409. {
  410. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  411. rcar_du_crtc_update_planes(rcrtc);
  412. }
  413. static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
  414. .mode_fixup = rcar_du_crtc_mode_fixup,
  415. .disable = rcar_du_crtc_disable,
  416. .enable = rcar_du_crtc_enable,
  417. .atomic_begin = rcar_du_crtc_atomic_begin,
  418. .atomic_flush = rcar_du_crtc_atomic_flush,
  419. };
  420. static const struct drm_crtc_funcs crtc_funcs = {
  421. .reset = drm_atomic_helper_crtc_reset,
  422. .destroy = drm_crtc_cleanup,
  423. .set_config = drm_atomic_helper_set_config,
  424. .page_flip = drm_atomic_helper_page_flip,
  425. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  426. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  427. };
  428. /* -----------------------------------------------------------------------------
  429. * Interrupt Handling
  430. */
  431. static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
  432. {
  433. struct rcar_du_crtc *rcrtc = arg;
  434. irqreturn_t ret = IRQ_NONE;
  435. u32 status;
  436. status = rcar_du_crtc_read(rcrtc, DSSR);
  437. rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
  438. if (status & DSSR_FRM) {
  439. drm_handle_vblank(rcrtc->crtc.dev, rcrtc->index);
  440. rcar_du_crtc_finish_page_flip(rcrtc);
  441. ret = IRQ_HANDLED;
  442. }
  443. return ret;
  444. }
  445. /* -----------------------------------------------------------------------------
  446. * Initialization
  447. */
  448. int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int index)
  449. {
  450. static const unsigned int mmio_offsets[] = {
  451. DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET
  452. };
  453. struct rcar_du_device *rcdu = rgrp->dev;
  454. struct platform_device *pdev = to_platform_device(rcdu->dev);
  455. struct rcar_du_crtc *rcrtc = &rcdu->crtcs[index];
  456. struct drm_crtc *crtc = &rcrtc->crtc;
  457. unsigned int irqflags;
  458. struct clk *clk;
  459. char clk_name[9];
  460. char *name;
  461. int irq;
  462. int ret;
  463. /* Get the CRTC clock and the optional external clock. */
  464. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
  465. sprintf(clk_name, "du.%u", index);
  466. name = clk_name;
  467. } else {
  468. name = NULL;
  469. }
  470. rcrtc->clock = devm_clk_get(rcdu->dev, name);
  471. if (IS_ERR(rcrtc->clock)) {
  472. dev_err(rcdu->dev, "no clock for CRTC %u\n", index);
  473. return PTR_ERR(rcrtc->clock);
  474. }
  475. sprintf(clk_name, "dclkin.%u", index);
  476. clk = devm_clk_get(rcdu->dev, clk_name);
  477. if (!IS_ERR(clk)) {
  478. rcrtc->extclock = clk;
  479. } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
  480. dev_info(rcdu->dev, "can't get external clock %u\n", index);
  481. return -EPROBE_DEFER;
  482. }
  483. init_waitqueue_head(&rcrtc->flip_wait);
  484. rcrtc->group = rgrp;
  485. rcrtc->mmio_offset = mmio_offsets[index];
  486. rcrtc->index = index;
  487. rcrtc->enabled = false;
  488. ret = drm_crtc_init_with_planes(rcdu->ddev, crtc,
  489. &rgrp->planes[index % 2].plane,
  490. NULL, &crtc_funcs);
  491. if (ret < 0)
  492. return ret;
  493. drm_crtc_helper_add(crtc, &crtc_helper_funcs);
  494. /* Start with vertical blanking interrupt reporting disabled. */
  495. drm_crtc_vblank_off(crtc);
  496. /* Register the interrupt handler. */
  497. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
  498. irq = platform_get_irq(pdev, index);
  499. irqflags = 0;
  500. } else {
  501. irq = platform_get_irq(pdev, 0);
  502. irqflags = IRQF_SHARED;
  503. }
  504. if (irq < 0) {
  505. dev_err(rcdu->dev, "no IRQ for CRTC %u\n", index);
  506. return irq;
  507. }
  508. ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
  509. dev_name(rcdu->dev), rcrtc);
  510. if (ret < 0) {
  511. dev_err(rcdu->dev,
  512. "failed to register IRQ for CRTC %u\n", index);
  513. return ret;
  514. }
  515. return 0;
  516. }
  517. void rcar_du_crtc_enable_vblank(struct rcar_du_crtc *rcrtc, bool enable)
  518. {
  519. if (enable) {
  520. rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
  521. rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
  522. } else {
  523. rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
  524. }
  525. }