rcar_du_crtc.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * rcar_du_crtc.c -- R-Car Display Unit CRTCs
  4. *
  5. * Copyright (C) 2013-2015 Renesas Electronics Corporation
  6. *
  7. * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/mutex.h>
  11. #include <linux/sys_soc.h>
  12. #include <drm/drmP.h>
  13. #include <drm/drm_atomic.h>
  14. #include <drm/drm_atomic_helper.h>
  15. #include <drm/drm_crtc.h>
  16. #include <drm/drm_crtc_helper.h>
  17. #include <drm/drm_fb_cma_helper.h>
  18. #include <drm/drm_gem_cma_helper.h>
  19. #include <drm/drm_plane_helper.h>
  20. #include "rcar_du_crtc.h"
  21. #include "rcar_du_drv.h"
  22. #include "rcar_du_kms.h"
  23. #include "rcar_du_plane.h"
  24. #include "rcar_du_regs.h"
  25. #include "rcar_du_vsp.h"
  26. static u32 rcar_du_crtc_read(struct rcar_du_crtc *rcrtc, u32 reg)
  27. {
  28. struct rcar_du_device *rcdu = rcrtc->group->dev;
  29. return rcar_du_read(rcdu, rcrtc->mmio_offset + reg);
  30. }
  31. static void rcar_du_crtc_write(struct rcar_du_crtc *rcrtc, u32 reg, u32 data)
  32. {
  33. struct rcar_du_device *rcdu = rcrtc->group->dev;
  34. rcar_du_write(rcdu, rcrtc->mmio_offset + reg, data);
  35. }
  36. static void rcar_du_crtc_clr(struct rcar_du_crtc *rcrtc, u32 reg, u32 clr)
  37. {
  38. struct rcar_du_device *rcdu = rcrtc->group->dev;
  39. rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
  40. rcar_du_read(rcdu, rcrtc->mmio_offset + reg) & ~clr);
  41. }
  42. static void rcar_du_crtc_set(struct rcar_du_crtc *rcrtc, u32 reg, u32 set)
  43. {
  44. struct rcar_du_device *rcdu = rcrtc->group->dev;
  45. rcar_du_write(rcdu, rcrtc->mmio_offset + reg,
  46. rcar_du_read(rcdu, rcrtc->mmio_offset + reg) | set);
  47. }
  48. void rcar_du_crtc_dsysr_clr_set(struct rcar_du_crtc *rcrtc, u32 clr, u32 set)
  49. {
  50. struct rcar_du_device *rcdu = rcrtc->group->dev;
  51. rcrtc->dsysr = (rcrtc->dsysr & ~clr) | set;
  52. rcar_du_write(rcdu, rcrtc->mmio_offset + DSYSR, rcrtc->dsysr);
  53. }
  54. /* -----------------------------------------------------------------------------
  55. * Hardware Setup
  56. */
  57. struct dpll_info {
  58. unsigned int output;
  59. unsigned int fdpll;
  60. unsigned int n;
  61. unsigned int m;
  62. };
  63. static void rcar_du_dpll_divider(struct rcar_du_crtc *rcrtc,
  64. struct dpll_info *dpll,
  65. unsigned long input,
  66. unsigned long target)
  67. {
  68. unsigned long best_diff = (unsigned long)-1;
  69. unsigned long diff;
  70. unsigned int fdpll;
  71. unsigned int m;
  72. unsigned int n;
  73. /*
  74. * fin fvco fout fclkout
  75. * in --> [1/M] --> |PD| -> [LPF] -> [VCO] -> [1/P] -+-> [1/FDPLL] -> out
  76. * +-> | | |
  77. * | |
  78. * +---------------- [1/N] <------------+
  79. *
  80. * fclkout = fvco / P / FDPLL -- (1)
  81. *
  82. * fin/M = fvco/P/N
  83. *
  84. * fvco = fin * P * N / M -- (2)
  85. *
  86. * (1) + (2) indicates
  87. *
  88. * fclkout = fin * N / M / FDPLL
  89. *
  90. * NOTES
  91. * N : (n + 1)
  92. * M : (m + 1)
  93. * FDPLL : (fdpll + 1)
  94. * P : 2
  95. * 2kHz < fvco < 4096MHz
  96. *
  97. * To minimize the jitter,
  98. * N : as large as possible
  99. * M : as small as possible
  100. */
  101. for (m = 0; m < 4; m++) {
  102. for (n = 119; n > 38; n--) {
  103. /*
  104. * This code only runs on 64-bit architectures, the
  105. * unsigned long type can thus be used for 64-bit
  106. * computation. It will still compile without any
  107. * warning on 32-bit architectures.
  108. *
  109. * To optimize calculations, use fout instead of fvco
  110. * to verify the VCO frequency constraint.
  111. */
  112. unsigned long fout = input * (n + 1) / (m + 1);
  113. if (fout < 1000 || fout > 2048 * 1000 * 1000U)
  114. continue;
  115. for (fdpll = 1; fdpll < 32; fdpll++) {
  116. unsigned long output;
  117. output = fout / (fdpll + 1);
  118. if (output >= 400 * 1000 * 1000)
  119. continue;
  120. diff = abs((long)output - (long)target);
  121. if (best_diff > diff) {
  122. best_diff = diff;
  123. dpll->n = n;
  124. dpll->m = m;
  125. dpll->fdpll = fdpll;
  126. dpll->output = output;
  127. }
  128. if (diff == 0)
  129. goto done;
  130. }
  131. }
  132. }
  133. done:
  134. dev_dbg(rcrtc->group->dev->dev,
  135. "output:%u, fdpll:%u, n:%u, m:%u, diff:%lu\n",
  136. dpll->output, dpll->fdpll, dpll->n, dpll->m,
  137. best_diff);
  138. }
  139. struct du_clk_params {
  140. struct clk *clk;
  141. unsigned long rate;
  142. unsigned long diff;
  143. u32 escr;
  144. };
  145. static void rcar_du_escr_divider(struct clk *clk, unsigned long target,
  146. u32 escr, struct du_clk_params *params)
  147. {
  148. unsigned long rate;
  149. unsigned long diff;
  150. u32 div;
  151. /*
  152. * If the target rate has already been achieved perfectly we can't do
  153. * better.
  154. */
  155. if (params->diff == 0)
  156. return;
  157. /*
  158. * Compute the input clock rate and internal divisor values to obtain
  159. * the clock rate closest to the target frequency.
  160. */
  161. rate = clk_round_rate(clk, target);
  162. div = clamp(DIV_ROUND_CLOSEST(rate, target), 1UL, 64UL) - 1;
  163. diff = abs(rate / (div + 1) - target);
  164. /*
  165. * Store the parameters if the resulting frequency is better than any
  166. * previously calculated value.
  167. */
  168. if (diff < params->diff) {
  169. params->clk = clk;
  170. params->rate = rate;
  171. params->diff = diff;
  172. params->escr = escr | div;
  173. }
  174. }
  175. static const struct soc_device_attribute rcar_du_r8a7795_es1[] = {
  176. { .soc_id = "r8a7795", .revision = "ES1.*" },
  177. { /* sentinel */ }
  178. };
  179. static void rcar_du_crtc_set_display_timing(struct rcar_du_crtc *rcrtc)
  180. {
  181. const struct drm_display_mode *mode = &rcrtc->crtc.state->adjusted_mode;
  182. struct rcar_du_device *rcdu = rcrtc->group->dev;
  183. unsigned long mode_clock = mode->clock * 1000;
  184. u32 dsmr;
  185. u32 escr;
  186. if (rcdu->info->dpll_mask & (1 << rcrtc->index)) {
  187. unsigned long target = mode_clock;
  188. struct dpll_info dpll = { 0 };
  189. unsigned long extclk;
  190. u32 dpllcr;
  191. u32 div = 0;
  192. /*
  193. * DU channels that have a display PLL can't use the internal
  194. * system clock, and have no internal clock divider.
  195. */
  196. if (WARN_ON(!rcrtc->extclock))
  197. return;
  198. /*
  199. * The H3 ES1.x exhibits dot clock duty cycle stability issues.
  200. * We can work around them by configuring the DPLL to twice the
  201. * desired frequency, coupled with a /2 post-divider. Restrict
  202. * the workaround to H3 ES1.x as ES2.0 and all other SoCs have
  203. * no post-divider when a display PLL is present (as shown by
  204. * the workaround breaking HDMI output on M3-W during testing).
  205. */
  206. if (soc_device_match(rcar_du_r8a7795_es1)) {
  207. target *= 2;
  208. div = 1;
  209. }
  210. extclk = clk_get_rate(rcrtc->extclock);
  211. rcar_du_dpll_divider(rcrtc, &dpll, extclk, target);
  212. dpllcr = DPLLCR_CODE | DPLLCR_CLKE
  213. | DPLLCR_FDPLL(dpll.fdpll)
  214. | DPLLCR_N(dpll.n) | DPLLCR_M(dpll.m)
  215. | DPLLCR_STBY;
  216. if (rcrtc->index == 1)
  217. dpllcr |= DPLLCR_PLCS1
  218. | DPLLCR_INCS_DOTCLKIN1;
  219. else
  220. dpllcr |= DPLLCR_PLCS0
  221. | DPLLCR_INCS_DOTCLKIN0;
  222. rcar_du_group_write(rcrtc->group, DPLLCR, dpllcr);
  223. escr = ESCR_DCLKSEL_DCLKIN | div;
  224. } else if (rcdu->info->lvds_clk_mask & BIT(rcrtc->index)) {
  225. /*
  226. * Use the LVDS PLL output as the dot clock when outputting to
  227. * the LVDS encoder on an SoC that supports this clock routing
  228. * option. We use the clock directly in that case, without any
  229. * additional divider.
  230. */
  231. escr = ESCR_DCLKSEL_DCLKIN;
  232. } else {
  233. struct du_clk_params params = { .diff = (unsigned long)-1 };
  234. rcar_du_escr_divider(rcrtc->clock, mode_clock,
  235. ESCR_DCLKSEL_CLKS, &params);
  236. if (rcrtc->extclock)
  237. rcar_du_escr_divider(rcrtc->extclock, mode_clock,
  238. ESCR_DCLKSEL_DCLKIN, &params);
  239. dev_dbg(rcrtc->group->dev->dev, "mode clock %lu %s rate %lu\n",
  240. mode_clock, params.clk == rcrtc->clock ? "cpg" : "ext",
  241. params.rate);
  242. clk_set_rate(params.clk, params.rate);
  243. escr = params.escr;
  244. }
  245. dev_dbg(rcrtc->group->dev->dev, "%s: ESCR 0x%08x\n", __func__, escr);
  246. rcar_du_crtc_write(rcrtc, rcrtc->index % 2 ? ESCR13 : ESCR02, escr);
  247. rcar_du_crtc_write(rcrtc, rcrtc->index % 2 ? OTAR13 : OTAR02, 0);
  248. /* Signal polarities */
  249. dsmr = ((mode->flags & DRM_MODE_FLAG_PVSYNC) ? DSMR_VSL : 0)
  250. | ((mode->flags & DRM_MODE_FLAG_PHSYNC) ? DSMR_HSL : 0)
  251. | ((mode->flags & DRM_MODE_FLAG_INTERLACE) ? DSMR_ODEV : 0)
  252. | DSMR_DIPM_DISP | DSMR_CSPM;
  253. rcar_du_crtc_write(rcrtc, DSMR, dsmr);
  254. /* Display timings */
  255. rcar_du_crtc_write(rcrtc, HDSR, mode->htotal - mode->hsync_start - 19);
  256. rcar_du_crtc_write(rcrtc, HDER, mode->htotal - mode->hsync_start +
  257. mode->hdisplay - 19);
  258. rcar_du_crtc_write(rcrtc, HSWR, mode->hsync_end -
  259. mode->hsync_start - 1);
  260. rcar_du_crtc_write(rcrtc, HCR, mode->htotal - 1);
  261. rcar_du_crtc_write(rcrtc, VDSR, mode->crtc_vtotal -
  262. mode->crtc_vsync_end - 2);
  263. rcar_du_crtc_write(rcrtc, VDER, mode->crtc_vtotal -
  264. mode->crtc_vsync_end +
  265. mode->crtc_vdisplay - 2);
  266. rcar_du_crtc_write(rcrtc, VSPR, mode->crtc_vtotal -
  267. mode->crtc_vsync_end +
  268. mode->crtc_vsync_start - 1);
  269. rcar_du_crtc_write(rcrtc, VCR, mode->crtc_vtotal - 1);
  270. rcar_du_crtc_write(rcrtc, DESR, mode->htotal - mode->hsync_start - 1);
  271. rcar_du_crtc_write(rcrtc, DEWR, mode->hdisplay);
  272. }
  273. void rcar_du_crtc_route_output(struct drm_crtc *crtc,
  274. enum rcar_du_output output)
  275. {
  276. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  277. struct rcar_du_device *rcdu = rcrtc->group->dev;
  278. /*
  279. * Store the route from the CRTC output to the DU output. The DU will be
  280. * configured when starting the CRTC.
  281. */
  282. rcrtc->outputs |= BIT(output);
  283. /*
  284. * Store RGB routing to DPAD0, the hardware will be configured when
  285. * starting the CRTC.
  286. */
  287. if (output == RCAR_DU_OUTPUT_DPAD0)
  288. rcdu->dpad0_source = rcrtc->index;
  289. }
  290. static unsigned int plane_zpos(struct rcar_du_plane *plane)
  291. {
  292. return plane->plane.state->normalized_zpos;
  293. }
  294. static const struct rcar_du_format_info *
  295. plane_format(struct rcar_du_plane *plane)
  296. {
  297. return to_rcar_plane_state(plane->plane.state)->format;
  298. }
  299. static void rcar_du_crtc_update_planes(struct rcar_du_crtc *rcrtc)
  300. {
  301. struct rcar_du_plane *planes[RCAR_DU_NUM_HW_PLANES];
  302. struct rcar_du_device *rcdu = rcrtc->group->dev;
  303. unsigned int num_planes = 0;
  304. unsigned int dptsr_planes;
  305. unsigned int hwplanes = 0;
  306. unsigned int prio = 0;
  307. unsigned int i;
  308. u32 dspr = 0;
  309. for (i = 0; i < rcrtc->group->num_planes; ++i) {
  310. struct rcar_du_plane *plane = &rcrtc->group->planes[i];
  311. unsigned int j;
  312. if (plane->plane.state->crtc != &rcrtc->crtc ||
  313. !plane->plane.state->visible)
  314. continue;
  315. /* Insert the plane in the sorted planes array. */
  316. for (j = num_planes++; j > 0; --j) {
  317. if (plane_zpos(planes[j-1]) <= plane_zpos(plane))
  318. break;
  319. planes[j] = planes[j-1];
  320. }
  321. planes[j] = plane;
  322. prio += plane_format(plane)->planes * 4;
  323. }
  324. for (i = 0; i < num_planes; ++i) {
  325. struct rcar_du_plane *plane = planes[i];
  326. struct drm_plane_state *state = plane->plane.state;
  327. unsigned int index = to_rcar_plane_state(state)->hwindex;
  328. prio -= 4;
  329. dspr |= (index + 1) << prio;
  330. hwplanes |= 1 << index;
  331. if (plane_format(plane)->planes == 2) {
  332. index = (index + 1) % 8;
  333. prio -= 4;
  334. dspr |= (index + 1) << prio;
  335. hwplanes |= 1 << index;
  336. }
  337. }
  338. /* If VSP+DU integration is enabled the plane assignment is fixed. */
  339. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE)) {
  340. if (rcdu->info->gen < 3) {
  341. dspr = (rcrtc->index % 2) + 1;
  342. hwplanes = 1 << (rcrtc->index % 2);
  343. } else {
  344. dspr = (rcrtc->index % 2) ? 3 : 1;
  345. hwplanes = 1 << ((rcrtc->index % 2) ? 2 : 0);
  346. }
  347. }
  348. /*
  349. * Update the planes to display timing and dot clock generator
  350. * associations.
  351. *
  352. * Updating the DPTSR register requires restarting the CRTC group,
  353. * resulting in visible flicker. To mitigate the issue only update the
  354. * association if needed by enabled planes. Planes being disabled will
  355. * keep their current association.
  356. */
  357. mutex_lock(&rcrtc->group->lock);
  358. dptsr_planes = rcrtc->index % 2 ? rcrtc->group->dptsr_planes | hwplanes
  359. : rcrtc->group->dptsr_planes & ~hwplanes;
  360. if (dptsr_planes != rcrtc->group->dptsr_planes) {
  361. rcar_du_group_write(rcrtc->group, DPTSR,
  362. (dptsr_planes << 16) | dptsr_planes);
  363. rcrtc->group->dptsr_planes = dptsr_planes;
  364. if (rcrtc->group->used_crtcs)
  365. rcar_du_group_restart(rcrtc->group);
  366. }
  367. /* Restart the group if plane sources have changed. */
  368. if (rcrtc->group->need_restart)
  369. rcar_du_group_restart(rcrtc->group);
  370. mutex_unlock(&rcrtc->group->lock);
  371. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR,
  372. dspr);
  373. }
  374. /* -----------------------------------------------------------------------------
  375. * Page Flip
  376. */
  377. void rcar_du_crtc_finish_page_flip(struct rcar_du_crtc *rcrtc)
  378. {
  379. struct drm_pending_vblank_event *event;
  380. struct drm_device *dev = rcrtc->crtc.dev;
  381. unsigned long flags;
  382. spin_lock_irqsave(&dev->event_lock, flags);
  383. event = rcrtc->event;
  384. rcrtc->event = NULL;
  385. spin_unlock_irqrestore(&dev->event_lock, flags);
  386. if (event == NULL)
  387. return;
  388. spin_lock_irqsave(&dev->event_lock, flags);
  389. drm_crtc_send_vblank_event(&rcrtc->crtc, event);
  390. wake_up(&rcrtc->flip_wait);
  391. spin_unlock_irqrestore(&dev->event_lock, flags);
  392. drm_crtc_vblank_put(&rcrtc->crtc);
  393. }
  394. static bool rcar_du_crtc_page_flip_pending(struct rcar_du_crtc *rcrtc)
  395. {
  396. struct drm_device *dev = rcrtc->crtc.dev;
  397. unsigned long flags;
  398. bool pending;
  399. spin_lock_irqsave(&dev->event_lock, flags);
  400. pending = rcrtc->event != NULL;
  401. spin_unlock_irqrestore(&dev->event_lock, flags);
  402. return pending;
  403. }
  404. static void rcar_du_crtc_wait_page_flip(struct rcar_du_crtc *rcrtc)
  405. {
  406. struct rcar_du_device *rcdu = rcrtc->group->dev;
  407. if (wait_event_timeout(rcrtc->flip_wait,
  408. !rcar_du_crtc_page_flip_pending(rcrtc),
  409. msecs_to_jiffies(50)))
  410. return;
  411. dev_warn(rcdu->dev, "page flip timeout\n");
  412. rcar_du_crtc_finish_page_flip(rcrtc);
  413. }
  414. /* -----------------------------------------------------------------------------
  415. * Start/Stop and Suspend/Resume
  416. */
  417. static void rcar_du_crtc_setup(struct rcar_du_crtc *rcrtc)
  418. {
  419. /* Set display off and background to black */
  420. rcar_du_crtc_write(rcrtc, DOOR, DOOR_RGB(0, 0, 0));
  421. rcar_du_crtc_write(rcrtc, BPOR, BPOR_RGB(0, 0, 0));
  422. /* Configure display timings and output routing */
  423. rcar_du_crtc_set_display_timing(rcrtc);
  424. rcar_du_group_set_routing(rcrtc->group);
  425. /* Start with all planes disabled. */
  426. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
  427. /* Enable the VSP compositor. */
  428. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
  429. rcar_du_vsp_enable(rcrtc);
  430. /* Turn vertical blanking interrupt reporting on. */
  431. drm_crtc_vblank_on(&rcrtc->crtc);
  432. }
  433. static int rcar_du_crtc_get(struct rcar_du_crtc *rcrtc)
  434. {
  435. int ret;
  436. /*
  437. * Guard against double-get, as the function is called from both the
  438. * .atomic_enable() and .atomic_begin() handlers.
  439. */
  440. if (rcrtc->initialized)
  441. return 0;
  442. ret = clk_prepare_enable(rcrtc->clock);
  443. if (ret < 0)
  444. return ret;
  445. ret = clk_prepare_enable(rcrtc->extclock);
  446. if (ret < 0)
  447. goto error_clock;
  448. ret = rcar_du_group_get(rcrtc->group);
  449. if (ret < 0)
  450. goto error_group;
  451. rcar_du_crtc_setup(rcrtc);
  452. rcrtc->initialized = true;
  453. return 0;
  454. error_group:
  455. clk_disable_unprepare(rcrtc->extclock);
  456. error_clock:
  457. clk_disable_unprepare(rcrtc->clock);
  458. return ret;
  459. }
  460. static void rcar_du_crtc_put(struct rcar_du_crtc *rcrtc)
  461. {
  462. rcar_du_group_put(rcrtc->group);
  463. clk_disable_unprepare(rcrtc->extclock);
  464. clk_disable_unprepare(rcrtc->clock);
  465. rcrtc->initialized = false;
  466. }
  467. static void rcar_du_crtc_start(struct rcar_du_crtc *rcrtc)
  468. {
  469. bool interlaced;
  470. /*
  471. * Select master sync mode. This enables display operation in master
  472. * sync mode (with the HSYNC and VSYNC signals configured as outputs and
  473. * actively driven).
  474. */
  475. interlaced = rcrtc->crtc.mode.flags & DRM_MODE_FLAG_INTERLACE;
  476. rcar_du_crtc_dsysr_clr_set(rcrtc, DSYSR_TVM_MASK | DSYSR_SCM_MASK,
  477. (interlaced ? DSYSR_SCM_INT_VIDEO : 0) |
  478. DSYSR_TVM_MASTER);
  479. rcar_du_group_start_stop(rcrtc->group, true);
  480. }
  481. static void rcar_du_crtc_disable_planes(struct rcar_du_crtc *rcrtc)
  482. {
  483. struct rcar_du_device *rcdu = rcrtc->group->dev;
  484. struct drm_crtc *crtc = &rcrtc->crtc;
  485. u32 status;
  486. /* Make sure vblank interrupts are enabled. */
  487. drm_crtc_vblank_get(crtc);
  488. /*
  489. * Disable planes and calculate how many vertical blanking interrupts we
  490. * have to wait for. If a vertical blanking interrupt has been triggered
  491. * but not processed yet, we don't know whether it occurred before or
  492. * after the planes got disabled. We thus have to wait for two vblank
  493. * interrupts in that case.
  494. */
  495. spin_lock_irq(&rcrtc->vblank_lock);
  496. rcar_du_group_write(rcrtc->group, rcrtc->index % 2 ? DS2PR : DS1PR, 0);
  497. status = rcar_du_crtc_read(rcrtc, DSSR);
  498. rcrtc->vblank_count = status & DSSR_VBK ? 2 : 1;
  499. spin_unlock_irq(&rcrtc->vblank_lock);
  500. if (!wait_event_timeout(rcrtc->vblank_wait, rcrtc->vblank_count == 0,
  501. msecs_to_jiffies(100)))
  502. dev_warn(rcdu->dev, "vertical blanking timeout\n");
  503. drm_crtc_vblank_put(crtc);
  504. }
  505. static void rcar_du_crtc_stop(struct rcar_du_crtc *rcrtc)
  506. {
  507. struct drm_crtc *crtc = &rcrtc->crtc;
  508. /*
  509. * Disable all planes and wait for the change to take effect. This is
  510. * required as the plane enable registers are updated on vblank, and no
  511. * vblank will occur once the CRTC is stopped. Disabling planes when
  512. * starting the CRTC thus wouldn't be enough as it would start scanning
  513. * out immediately from old frame buffers until the next vblank.
  514. *
  515. * This increases the CRTC stop delay, especially when multiple CRTCs
  516. * are stopped in one operation as we now wait for one vblank per CRTC.
  517. * Whether this can be improved needs to be researched.
  518. */
  519. rcar_du_crtc_disable_planes(rcrtc);
  520. /*
  521. * Disable vertical blanking interrupt reporting. We first need to wait
  522. * for page flip completion before stopping the CRTC as userspace
  523. * expects page flips to eventually complete.
  524. */
  525. rcar_du_crtc_wait_page_flip(rcrtc);
  526. drm_crtc_vblank_off(crtc);
  527. /* Disable the VSP compositor. */
  528. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
  529. rcar_du_vsp_disable(rcrtc);
  530. /*
  531. * Select switch sync mode. This stops display operation and configures
  532. * the HSYNC and VSYNC signals as inputs.
  533. *
  534. * TODO: Find another way to stop the display for DUs that don't support
  535. * TVM sync.
  536. */
  537. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_TVM_SYNC))
  538. rcar_du_crtc_dsysr_clr_set(rcrtc, DSYSR_TVM_MASK,
  539. DSYSR_TVM_SWITCH);
  540. rcar_du_group_start_stop(rcrtc->group, false);
  541. }
  542. /* -----------------------------------------------------------------------------
  543. * CRTC Functions
  544. */
  545. static void rcar_du_crtc_atomic_enable(struct drm_crtc *crtc,
  546. struct drm_crtc_state *old_state)
  547. {
  548. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  549. rcar_du_crtc_get(rcrtc);
  550. rcar_du_crtc_start(rcrtc);
  551. }
  552. static void rcar_du_crtc_atomic_disable(struct drm_crtc *crtc,
  553. struct drm_crtc_state *old_state)
  554. {
  555. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  556. rcar_du_crtc_stop(rcrtc);
  557. rcar_du_crtc_put(rcrtc);
  558. spin_lock_irq(&crtc->dev->event_lock);
  559. if (crtc->state->event) {
  560. drm_crtc_send_vblank_event(crtc, crtc->state->event);
  561. crtc->state->event = NULL;
  562. }
  563. spin_unlock_irq(&crtc->dev->event_lock);
  564. rcrtc->outputs = 0;
  565. }
  566. static void rcar_du_crtc_atomic_begin(struct drm_crtc *crtc,
  567. struct drm_crtc_state *old_crtc_state)
  568. {
  569. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  570. WARN_ON(!crtc->state->enable);
  571. /*
  572. * If a mode set is in progress we can be called with the CRTC disabled.
  573. * We thus need to first get and setup the CRTC in order to configure
  574. * planes. We must *not* put the CRTC in .atomic_flush(), as it must be
  575. * kept awake until the .atomic_enable() call that will follow. The get
  576. * operation in .atomic_enable() will in that case be a no-op, and the
  577. * CRTC will be put later in .atomic_disable().
  578. *
  579. * If a mode set is not in progress the CRTC is enabled, and the
  580. * following get call will be a no-op. There is thus no need to belance
  581. * it in .atomic_flush() either.
  582. */
  583. rcar_du_crtc_get(rcrtc);
  584. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
  585. rcar_du_vsp_atomic_begin(rcrtc);
  586. }
  587. static void rcar_du_crtc_atomic_flush(struct drm_crtc *crtc,
  588. struct drm_crtc_state *old_crtc_state)
  589. {
  590. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  591. struct drm_device *dev = rcrtc->crtc.dev;
  592. unsigned long flags;
  593. rcar_du_crtc_update_planes(rcrtc);
  594. if (crtc->state->event) {
  595. WARN_ON(drm_crtc_vblank_get(crtc) != 0);
  596. spin_lock_irqsave(&dev->event_lock, flags);
  597. rcrtc->event = crtc->state->event;
  598. crtc->state->event = NULL;
  599. spin_unlock_irqrestore(&dev->event_lock, flags);
  600. }
  601. if (rcar_du_has(rcrtc->group->dev, RCAR_DU_FEATURE_VSP1_SOURCE))
  602. rcar_du_vsp_atomic_flush(rcrtc);
  603. }
  604. enum drm_mode_status rcar_du_crtc_mode_valid(struct drm_crtc *crtc,
  605. const struct drm_display_mode *mode)
  606. {
  607. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  608. struct rcar_du_device *rcdu = rcrtc->group->dev;
  609. bool interlaced = mode->flags & DRM_MODE_FLAG_INTERLACE;
  610. if (interlaced && !rcar_du_has(rcdu, RCAR_DU_FEATURE_INTERLACED))
  611. return MODE_NO_INTERLACE;
  612. return MODE_OK;
  613. }
  614. static const struct drm_crtc_helper_funcs crtc_helper_funcs = {
  615. .atomic_begin = rcar_du_crtc_atomic_begin,
  616. .atomic_flush = rcar_du_crtc_atomic_flush,
  617. .atomic_enable = rcar_du_crtc_atomic_enable,
  618. .atomic_disable = rcar_du_crtc_atomic_disable,
  619. .mode_valid = rcar_du_crtc_mode_valid,
  620. };
  621. static void rcar_du_crtc_crc_init(struct rcar_du_crtc *rcrtc)
  622. {
  623. struct rcar_du_device *rcdu = rcrtc->group->dev;
  624. const char **sources;
  625. unsigned int count;
  626. int i = -1;
  627. /* CRC available only on Gen3 HW. */
  628. if (rcdu->info->gen < 3)
  629. return;
  630. /* Reserve 1 for "auto" source. */
  631. count = rcrtc->vsp->num_planes + 1;
  632. sources = kmalloc_array(count, sizeof(*sources), GFP_KERNEL);
  633. if (!sources)
  634. return;
  635. sources[0] = kstrdup("auto", GFP_KERNEL);
  636. if (!sources[0])
  637. goto error;
  638. for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
  639. struct drm_plane *plane = &rcrtc->vsp->planes[i].plane;
  640. char name[16];
  641. sprintf(name, "plane%u", plane->base.id);
  642. sources[i + 1] = kstrdup(name, GFP_KERNEL);
  643. if (!sources[i + 1])
  644. goto error;
  645. }
  646. rcrtc->sources = sources;
  647. rcrtc->sources_count = count;
  648. return;
  649. error:
  650. while (i >= 0) {
  651. kfree(sources[i]);
  652. i--;
  653. }
  654. kfree(sources);
  655. }
  656. static void rcar_du_crtc_crc_cleanup(struct rcar_du_crtc *rcrtc)
  657. {
  658. unsigned int i;
  659. if (!rcrtc->sources)
  660. return;
  661. for (i = 0; i < rcrtc->sources_count; i++)
  662. kfree(rcrtc->sources[i]);
  663. kfree(rcrtc->sources);
  664. rcrtc->sources = NULL;
  665. rcrtc->sources_count = 0;
  666. }
  667. static struct drm_crtc_state *
  668. rcar_du_crtc_atomic_duplicate_state(struct drm_crtc *crtc)
  669. {
  670. struct rcar_du_crtc_state *state;
  671. struct rcar_du_crtc_state *copy;
  672. if (WARN_ON(!crtc->state))
  673. return NULL;
  674. state = to_rcar_crtc_state(crtc->state);
  675. copy = kmemdup(state, sizeof(*state), GFP_KERNEL);
  676. if (copy == NULL)
  677. return NULL;
  678. __drm_atomic_helper_crtc_duplicate_state(crtc, &copy->state);
  679. return &copy->state;
  680. }
  681. static void rcar_du_crtc_atomic_destroy_state(struct drm_crtc *crtc,
  682. struct drm_crtc_state *state)
  683. {
  684. __drm_atomic_helper_crtc_destroy_state(state);
  685. kfree(to_rcar_crtc_state(state));
  686. }
  687. static void rcar_du_crtc_cleanup(struct drm_crtc *crtc)
  688. {
  689. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  690. rcar_du_crtc_crc_cleanup(rcrtc);
  691. return drm_crtc_cleanup(crtc);
  692. }
  693. static void rcar_du_crtc_reset(struct drm_crtc *crtc)
  694. {
  695. struct rcar_du_crtc_state *state;
  696. if (crtc->state) {
  697. rcar_du_crtc_atomic_destroy_state(crtc, crtc->state);
  698. crtc->state = NULL;
  699. }
  700. state = kzalloc(sizeof(*state), GFP_KERNEL);
  701. if (state == NULL)
  702. return;
  703. state->crc.source = VSP1_DU_CRC_NONE;
  704. state->crc.index = 0;
  705. crtc->state = &state->state;
  706. crtc->state->crtc = crtc;
  707. }
  708. static int rcar_du_crtc_enable_vblank(struct drm_crtc *crtc)
  709. {
  710. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  711. rcar_du_crtc_write(rcrtc, DSRCR, DSRCR_VBCL);
  712. rcar_du_crtc_set(rcrtc, DIER, DIER_VBE);
  713. rcrtc->vblank_enable = true;
  714. return 0;
  715. }
  716. static void rcar_du_crtc_disable_vblank(struct drm_crtc *crtc)
  717. {
  718. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  719. rcar_du_crtc_clr(rcrtc, DIER, DIER_VBE);
  720. rcrtc->vblank_enable = false;
  721. }
  722. static int rcar_du_crtc_parse_crc_source(struct rcar_du_crtc *rcrtc,
  723. const char *source_name,
  724. enum vsp1_du_crc_source *source)
  725. {
  726. unsigned int index;
  727. int ret;
  728. /*
  729. * Parse the source name. Supported values are "plane%u" to compute the
  730. * CRC on an input plane (%u is the plane ID), and "auto" to compute the
  731. * CRC on the composer (VSP) output.
  732. */
  733. if (!source_name) {
  734. *source = VSP1_DU_CRC_NONE;
  735. return 0;
  736. } else if (!strcmp(source_name, "auto")) {
  737. *source = VSP1_DU_CRC_OUTPUT;
  738. return 0;
  739. } else if (strstarts(source_name, "plane")) {
  740. unsigned int i;
  741. *source = VSP1_DU_CRC_PLANE;
  742. ret = kstrtouint(source_name + strlen("plane"), 10, &index);
  743. if (ret < 0)
  744. return ret;
  745. for (i = 0; i < rcrtc->vsp->num_planes; ++i) {
  746. if (index == rcrtc->vsp->planes[i].plane.base.id)
  747. return i;
  748. }
  749. }
  750. return -EINVAL;
  751. }
  752. static int rcar_du_crtc_verify_crc_source(struct drm_crtc *crtc,
  753. const char *source_name,
  754. size_t *values_cnt)
  755. {
  756. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  757. enum vsp1_du_crc_source source;
  758. if (rcar_du_crtc_parse_crc_source(rcrtc, source_name, &source) < 0) {
  759. DRM_DEBUG_DRIVER("unknown source %s\n", source_name);
  760. return -EINVAL;
  761. }
  762. *values_cnt = 1;
  763. return 0;
  764. }
  765. const char *const *rcar_du_crtc_get_crc_sources(struct drm_crtc *crtc,
  766. size_t *count)
  767. {
  768. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  769. *count = rcrtc->sources_count;
  770. return rcrtc->sources;
  771. }
  772. static int rcar_du_crtc_set_crc_source(struct drm_crtc *crtc,
  773. const char *source_name)
  774. {
  775. struct rcar_du_crtc *rcrtc = to_rcar_crtc(crtc);
  776. struct drm_modeset_acquire_ctx ctx;
  777. struct drm_crtc_state *crtc_state;
  778. struct drm_atomic_state *state;
  779. enum vsp1_du_crc_source source;
  780. unsigned int index;
  781. int ret;
  782. ret = rcar_du_crtc_parse_crc_source(rcrtc, source_name, &source);
  783. if (ret < 0)
  784. return ret;
  785. index = ret;
  786. /* Perform an atomic commit to set the CRC source. */
  787. drm_modeset_acquire_init(&ctx, 0);
  788. state = drm_atomic_state_alloc(crtc->dev);
  789. if (!state) {
  790. ret = -ENOMEM;
  791. goto unlock;
  792. }
  793. state->acquire_ctx = &ctx;
  794. retry:
  795. crtc_state = drm_atomic_get_crtc_state(state, crtc);
  796. if (!IS_ERR(crtc_state)) {
  797. struct rcar_du_crtc_state *rcrtc_state;
  798. rcrtc_state = to_rcar_crtc_state(crtc_state);
  799. rcrtc_state->crc.source = source;
  800. rcrtc_state->crc.index = index;
  801. ret = drm_atomic_commit(state);
  802. } else {
  803. ret = PTR_ERR(crtc_state);
  804. }
  805. if (ret == -EDEADLK) {
  806. drm_atomic_state_clear(state);
  807. drm_modeset_backoff(&ctx);
  808. goto retry;
  809. }
  810. drm_atomic_state_put(state);
  811. unlock:
  812. drm_modeset_drop_locks(&ctx);
  813. drm_modeset_acquire_fini(&ctx);
  814. return 0;
  815. }
  816. static const struct drm_crtc_funcs crtc_funcs_gen2 = {
  817. .reset = rcar_du_crtc_reset,
  818. .destroy = drm_crtc_cleanup,
  819. .set_config = drm_atomic_helper_set_config,
  820. .page_flip = drm_atomic_helper_page_flip,
  821. .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
  822. .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
  823. .enable_vblank = rcar_du_crtc_enable_vblank,
  824. .disable_vblank = rcar_du_crtc_disable_vblank,
  825. };
  826. static const struct drm_crtc_funcs crtc_funcs_gen3 = {
  827. .reset = rcar_du_crtc_reset,
  828. .destroy = rcar_du_crtc_cleanup,
  829. .set_config = drm_atomic_helper_set_config,
  830. .page_flip = drm_atomic_helper_page_flip,
  831. .atomic_duplicate_state = rcar_du_crtc_atomic_duplicate_state,
  832. .atomic_destroy_state = rcar_du_crtc_atomic_destroy_state,
  833. .enable_vblank = rcar_du_crtc_enable_vblank,
  834. .disable_vblank = rcar_du_crtc_disable_vblank,
  835. .set_crc_source = rcar_du_crtc_set_crc_source,
  836. .verify_crc_source = rcar_du_crtc_verify_crc_source,
  837. .get_crc_sources = rcar_du_crtc_get_crc_sources,
  838. };
  839. /* -----------------------------------------------------------------------------
  840. * Interrupt Handling
  841. */
  842. static irqreturn_t rcar_du_crtc_irq(int irq, void *arg)
  843. {
  844. struct rcar_du_crtc *rcrtc = arg;
  845. struct rcar_du_device *rcdu = rcrtc->group->dev;
  846. irqreturn_t ret = IRQ_NONE;
  847. u32 status;
  848. spin_lock(&rcrtc->vblank_lock);
  849. status = rcar_du_crtc_read(rcrtc, DSSR);
  850. rcar_du_crtc_write(rcrtc, DSRCR, status & DSRCR_MASK);
  851. if (status & DSSR_VBK) {
  852. /*
  853. * Wake up the vblank wait if the counter reaches 0. This must
  854. * be protected by the vblank_lock to avoid races in
  855. * rcar_du_crtc_disable_planes().
  856. */
  857. if (rcrtc->vblank_count) {
  858. if (--rcrtc->vblank_count == 0)
  859. wake_up(&rcrtc->vblank_wait);
  860. }
  861. }
  862. spin_unlock(&rcrtc->vblank_lock);
  863. if (status & DSSR_VBK) {
  864. if (rcdu->info->gen < 3) {
  865. drm_crtc_handle_vblank(&rcrtc->crtc);
  866. rcar_du_crtc_finish_page_flip(rcrtc);
  867. }
  868. ret = IRQ_HANDLED;
  869. }
  870. return ret;
  871. }
  872. /* -----------------------------------------------------------------------------
  873. * Initialization
  874. */
  875. int rcar_du_crtc_create(struct rcar_du_group *rgrp, unsigned int swindex,
  876. unsigned int hwindex)
  877. {
  878. static const unsigned int mmio_offsets[] = {
  879. DU0_REG_OFFSET, DU1_REG_OFFSET, DU2_REG_OFFSET, DU3_REG_OFFSET
  880. };
  881. struct rcar_du_device *rcdu = rgrp->dev;
  882. struct platform_device *pdev = to_platform_device(rcdu->dev);
  883. struct rcar_du_crtc *rcrtc = &rcdu->crtcs[swindex];
  884. struct drm_crtc *crtc = &rcrtc->crtc;
  885. struct drm_plane *primary;
  886. unsigned int irqflags;
  887. struct clk *clk;
  888. char clk_name[9];
  889. char *name;
  890. int irq;
  891. int ret;
  892. /* Get the CRTC clock and the optional external clock. */
  893. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
  894. sprintf(clk_name, "du.%u", hwindex);
  895. name = clk_name;
  896. } else {
  897. name = NULL;
  898. }
  899. rcrtc->clock = devm_clk_get(rcdu->dev, name);
  900. if (IS_ERR(rcrtc->clock)) {
  901. dev_err(rcdu->dev, "no clock for DU channel %u\n", hwindex);
  902. return PTR_ERR(rcrtc->clock);
  903. }
  904. sprintf(clk_name, "dclkin.%u", hwindex);
  905. clk = devm_clk_get(rcdu->dev, clk_name);
  906. if (!IS_ERR(clk)) {
  907. rcrtc->extclock = clk;
  908. } else if (PTR_ERR(rcrtc->clock) == -EPROBE_DEFER) {
  909. dev_info(rcdu->dev, "can't get external clock %u\n", hwindex);
  910. return -EPROBE_DEFER;
  911. }
  912. init_waitqueue_head(&rcrtc->flip_wait);
  913. init_waitqueue_head(&rcrtc->vblank_wait);
  914. spin_lock_init(&rcrtc->vblank_lock);
  915. rcrtc->group = rgrp;
  916. rcrtc->mmio_offset = mmio_offsets[hwindex];
  917. rcrtc->index = hwindex;
  918. rcrtc->dsysr = (rcrtc->index % 2 ? 0 : DSYSR_DRES) | DSYSR_TVM_TVSYNC;
  919. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_VSP1_SOURCE))
  920. primary = &rcrtc->vsp->planes[rcrtc->vsp_pipe].plane;
  921. else
  922. primary = &rgrp->planes[swindex % 2].plane;
  923. ret = drm_crtc_init_with_planes(rcdu->ddev, crtc, primary, NULL,
  924. rcdu->info->gen <= 2 ?
  925. &crtc_funcs_gen2 : &crtc_funcs_gen3,
  926. NULL);
  927. if (ret < 0)
  928. return ret;
  929. drm_crtc_helper_add(crtc, &crtc_helper_funcs);
  930. /* Start with vertical blanking interrupt reporting disabled. */
  931. drm_crtc_vblank_off(crtc);
  932. /* Register the interrupt handler. */
  933. if (rcar_du_has(rcdu, RCAR_DU_FEATURE_CRTC_IRQ_CLOCK)) {
  934. /* The IRQ's are associated with the CRTC (sw)index. */
  935. irq = platform_get_irq(pdev, swindex);
  936. irqflags = 0;
  937. } else {
  938. irq = platform_get_irq(pdev, 0);
  939. irqflags = IRQF_SHARED;
  940. }
  941. if (irq < 0) {
  942. dev_err(rcdu->dev, "no IRQ for CRTC %u\n", swindex);
  943. return irq;
  944. }
  945. ret = devm_request_irq(rcdu->dev, irq, rcar_du_crtc_irq, irqflags,
  946. dev_name(rcdu->dev), rcrtc);
  947. if (ret < 0) {
  948. dev_err(rcdu->dev,
  949. "failed to register IRQ for CRTC %u\n", swindex);
  950. return ret;
  951. }
  952. rcar_du_crtc_crc_init(rcrtc);
  953. return 0;
  954. }