tilcdc_crtc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /*
  2. * Copyright (C) 2012 Texas Instruments
  3. * Author: Rob Clark <robdclark@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <linux/kfifo.h>
  18. #include "tilcdc_drv.h"
  19. #include "tilcdc_regs.h"
  20. struct tilcdc_crtc {
  21. struct drm_crtc base;
  22. const struct tilcdc_panel_info *info;
  23. uint32_t dirty;
  24. dma_addr_t start, end;
  25. struct drm_pending_vblank_event *event;
  26. int dpms;
  27. wait_queue_head_t frame_done_wq;
  28. bool frame_done;
  29. /* fb currently set to scanout 0/1: */
  30. struct drm_framebuffer *scanout[2];
  31. /* for deferred fb unref's: */
  32. DECLARE_KFIFO_PTR(unref_fifo, struct drm_framebuffer *);
  33. struct work_struct work;
  34. };
  35. #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
  36. static void unref_worker(struct work_struct *work)
  37. {
  38. struct tilcdc_crtc *tilcdc_crtc = container_of(work, struct tilcdc_crtc, work);
  39. struct drm_device *dev = tilcdc_crtc->base.dev;
  40. struct drm_framebuffer *fb;
  41. mutex_lock(&dev->mode_config.mutex);
  42. while (kfifo_get(&tilcdc_crtc->unref_fifo, &fb))
  43. drm_framebuffer_unreference(fb);
  44. mutex_unlock(&dev->mode_config.mutex);
  45. }
  46. static void set_scanout(struct drm_crtc *crtc, int n)
  47. {
  48. static const uint32_t base_reg[] = {
  49. LCDC_DMA_FB_BASE_ADDR_0_REG, LCDC_DMA_FB_BASE_ADDR_1_REG,
  50. };
  51. static const uint32_t ceil_reg[] = {
  52. LCDC_DMA_FB_CEILING_ADDR_0_REG, LCDC_DMA_FB_CEILING_ADDR_1_REG,
  53. };
  54. static const uint32_t stat[] = {
  55. LCDC_END_OF_FRAME0, LCDC_END_OF_FRAME1,
  56. };
  57. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  58. struct drm_device *dev = crtc->dev;
  59. pm_runtime_get_sync(dev->dev);
  60. tilcdc_write(dev, base_reg[n], tilcdc_crtc->start);
  61. tilcdc_write(dev, ceil_reg[n], tilcdc_crtc->end);
  62. if (tilcdc_crtc->scanout[n]) {
  63. if (kfifo_put(&tilcdc_crtc->unref_fifo,
  64. (const struct drm_framebuffer **)&tilcdc_crtc->scanout[n])) {
  65. struct tilcdc_drm_private *priv = dev->dev_private;
  66. queue_work(priv->wq, &tilcdc_crtc->work);
  67. } else {
  68. dev_err(dev->dev, "unref fifo full!\n");
  69. drm_framebuffer_unreference(tilcdc_crtc->scanout[n]);
  70. }
  71. }
  72. tilcdc_crtc->scanout[n] = crtc->fb;
  73. drm_framebuffer_reference(tilcdc_crtc->scanout[n]);
  74. tilcdc_crtc->dirty &= ~stat[n];
  75. pm_runtime_put_sync(dev->dev);
  76. }
  77. static void update_scanout(struct drm_crtc *crtc)
  78. {
  79. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  80. struct drm_device *dev = crtc->dev;
  81. struct drm_framebuffer *fb = crtc->fb;
  82. struct drm_gem_cma_object *gem;
  83. unsigned int depth, bpp;
  84. drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
  85. gem = drm_fb_cma_get_gem_obj(fb, 0);
  86. tilcdc_crtc->start = gem->paddr + fb->offsets[0] +
  87. (crtc->y * fb->pitches[0]) + (crtc->x * bpp/8);
  88. tilcdc_crtc->end = tilcdc_crtc->start +
  89. (crtc->mode.vdisplay * fb->pitches[0]);
  90. if (tilcdc_crtc->dpms == DRM_MODE_DPMS_ON) {
  91. /* already enabled, so just mark the frames that need
  92. * updating and they will be updated on vblank:
  93. */
  94. tilcdc_crtc->dirty |= LCDC_END_OF_FRAME0 | LCDC_END_OF_FRAME1;
  95. drm_vblank_get(dev, 0);
  96. } else {
  97. /* not enabled yet, so update registers immediately: */
  98. set_scanout(crtc, 0);
  99. set_scanout(crtc, 1);
  100. }
  101. }
  102. static void start(struct drm_crtc *crtc)
  103. {
  104. struct drm_device *dev = crtc->dev;
  105. struct tilcdc_drm_private *priv = dev->dev_private;
  106. if (priv->rev == 2) {
  107. tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
  108. msleep(1);
  109. tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
  110. msleep(1);
  111. }
  112. tilcdc_set(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
  113. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
  114. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  115. }
  116. static void stop(struct drm_crtc *crtc)
  117. {
  118. struct drm_device *dev = crtc->dev;
  119. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  120. }
  121. static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
  122. {
  123. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  124. WARN_ON(tilcdc_crtc->dpms == DRM_MODE_DPMS_ON);
  125. drm_crtc_cleanup(crtc);
  126. WARN_ON(!kfifo_is_empty(&tilcdc_crtc->unref_fifo));
  127. kfifo_free(&tilcdc_crtc->unref_fifo);
  128. kfree(tilcdc_crtc);
  129. }
  130. static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
  131. struct drm_framebuffer *fb,
  132. struct drm_pending_vblank_event *event)
  133. {
  134. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  135. struct drm_device *dev = crtc->dev;
  136. if (tilcdc_crtc->event) {
  137. dev_err(dev->dev, "already pending page flip!\n");
  138. return -EBUSY;
  139. }
  140. crtc->fb = fb;
  141. tilcdc_crtc->event = event;
  142. update_scanout(crtc);
  143. return 0;
  144. }
  145. static void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
  146. {
  147. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  148. struct drm_device *dev = crtc->dev;
  149. struct tilcdc_drm_private *priv = dev->dev_private;
  150. /* we really only care about on or off: */
  151. if (mode != DRM_MODE_DPMS_ON)
  152. mode = DRM_MODE_DPMS_OFF;
  153. if (tilcdc_crtc->dpms == mode)
  154. return;
  155. tilcdc_crtc->dpms = mode;
  156. pm_runtime_get_sync(dev->dev);
  157. if (mode == DRM_MODE_DPMS_ON) {
  158. pm_runtime_forbid(dev->dev);
  159. start(crtc);
  160. } else {
  161. tilcdc_crtc->frame_done = false;
  162. stop(crtc);
  163. /* if necessary wait for framedone irq which will still come
  164. * before putting things to sleep..
  165. */
  166. if (priv->rev == 2) {
  167. int ret = wait_event_timeout(
  168. tilcdc_crtc->frame_done_wq,
  169. tilcdc_crtc->frame_done,
  170. msecs_to_jiffies(50));
  171. if (ret == 0)
  172. dev_err(dev->dev, "timeout waiting for framedone\n");
  173. }
  174. pm_runtime_allow(dev->dev);
  175. }
  176. pm_runtime_put_sync(dev->dev);
  177. }
  178. static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
  179. const struct drm_display_mode *mode,
  180. struct drm_display_mode *adjusted_mode)
  181. {
  182. return true;
  183. }
  184. static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
  185. {
  186. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  187. }
  188. static void tilcdc_crtc_commit(struct drm_crtc *crtc)
  189. {
  190. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  191. }
  192. static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
  193. struct drm_display_mode *mode,
  194. struct drm_display_mode *adjusted_mode,
  195. int x, int y,
  196. struct drm_framebuffer *old_fb)
  197. {
  198. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  199. struct drm_device *dev = crtc->dev;
  200. struct tilcdc_drm_private *priv = dev->dev_private;
  201. const struct tilcdc_panel_info *info = tilcdc_crtc->info;
  202. uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
  203. int ret;
  204. ret = tilcdc_crtc_mode_valid(crtc, mode);
  205. if (WARN_ON(ret))
  206. return ret;
  207. if (WARN_ON(!info))
  208. return -EINVAL;
  209. pm_runtime_get_sync(dev->dev);
  210. /* Configure the Burst Size and fifo threshold of DMA: */
  211. reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
  212. switch (info->dma_burst_sz) {
  213. case 1:
  214. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
  215. break;
  216. case 2:
  217. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
  218. break;
  219. case 4:
  220. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
  221. break;
  222. case 8:
  223. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
  224. break;
  225. case 16:
  226. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
  227. break;
  228. default:
  229. return -EINVAL;
  230. }
  231. reg |= (info->fifo_th << 8);
  232. tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
  233. /* Configure timings: */
  234. hbp = mode->htotal - mode->hsync_end;
  235. hfp = mode->hsync_start - mode->hdisplay;
  236. hsw = mode->hsync_end - mode->hsync_start;
  237. vbp = mode->vtotal - mode->vsync_end;
  238. vfp = mode->vsync_start - mode->vdisplay;
  239. vsw = mode->vsync_end - mode->vsync_start;
  240. DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
  241. mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
  242. /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
  243. reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
  244. reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
  245. LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
  246. if (priv->rev == 2) {
  247. reg |= (hfp & 0x300) >> 8;
  248. reg |= (hbp & 0x300) >> 4;
  249. reg |= (hsw & 0x3c0) << 21;
  250. }
  251. tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
  252. reg = (((mode->hdisplay >> 4) - 1) << 4) |
  253. ((hbp & 0xff) << 24) |
  254. ((hfp & 0xff) << 16) |
  255. ((hsw & 0x3f) << 10);
  256. if (priv->rev == 2)
  257. reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
  258. tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
  259. reg = ((mode->vdisplay - 1) & 0x3ff) |
  260. ((vbp & 0xff) << 24) |
  261. ((vfp & 0xff) << 16) |
  262. ((vsw & 0x3f) << 10);
  263. tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
  264. /*
  265. * be sure to set Bit 10 for the V2 LCDC controller,
  266. * otherwise limited to 1024 pixels width, stopping
  267. * 1920x1080 being suppoted.
  268. */
  269. if (priv->rev == 2) {
  270. if ((mode->vdisplay - 1) & 0x400) {
  271. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
  272. LCDC_LPP_B10);
  273. } else {
  274. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
  275. LCDC_LPP_B10);
  276. }
  277. }
  278. /* Configure display type: */
  279. reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
  280. ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
  281. LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
  282. reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
  283. if (info->tft_alt_mode)
  284. reg |= LCDC_TFT_ALT_ENABLE;
  285. if (priv->rev == 2) {
  286. unsigned int depth, bpp;
  287. drm_fb_get_bpp_depth(crtc->fb->pixel_format, &depth, &bpp);
  288. switch (bpp) {
  289. case 16:
  290. break;
  291. case 32:
  292. reg |= LCDC_V2_TFT_24BPP_UNPACK;
  293. /* fallthrough */
  294. case 24:
  295. reg |= LCDC_V2_TFT_24BPP_MODE;
  296. break;
  297. default:
  298. dev_err(dev->dev, "invalid pixel format\n");
  299. return -EINVAL;
  300. }
  301. }
  302. reg |= info->fdd < 12;
  303. tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
  304. if (info->invert_pxl_clk)
  305. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  306. else
  307. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  308. if (info->sync_ctrl)
  309. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  310. else
  311. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  312. if (info->sync_edge)
  313. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  314. else
  315. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  316. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  317. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
  318. else
  319. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
  320. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  321. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
  322. else
  323. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
  324. if (info->raster_order)
  325. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
  326. else
  327. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
  328. update_scanout(crtc);
  329. tilcdc_crtc_update_clk(crtc);
  330. pm_runtime_put_sync(dev->dev);
  331. return 0;
  332. }
  333. static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  334. struct drm_framebuffer *old_fb)
  335. {
  336. update_scanout(crtc);
  337. return 0;
  338. }
  339. static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
  340. .destroy = tilcdc_crtc_destroy,
  341. .set_config = drm_crtc_helper_set_config,
  342. .page_flip = tilcdc_crtc_page_flip,
  343. };
  344. static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
  345. .dpms = tilcdc_crtc_dpms,
  346. .mode_fixup = tilcdc_crtc_mode_fixup,
  347. .prepare = tilcdc_crtc_prepare,
  348. .commit = tilcdc_crtc_commit,
  349. .mode_set = tilcdc_crtc_mode_set,
  350. .mode_set_base = tilcdc_crtc_mode_set_base,
  351. };
  352. int tilcdc_crtc_max_width(struct drm_crtc *crtc)
  353. {
  354. struct drm_device *dev = crtc->dev;
  355. struct tilcdc_drm_private *priv = dev->dev_private;
  356. int max_width = 0;
  357. if (priv->rev == 1)
  358. max_width = 1024;
  359. else if (priv->rev == 2)
  360. max_width = 2048;
  361. return max_width;
  362. }
  363. int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
  364. {
  365. struct tilcdc_drm_private *priv = crtc->dev->dev_private;
  366. unsigned int bandwidth;
  367. if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
  368. return MODE_VIRTUAL_X;
  369. /* width must be multiple of 16 */
  370. if (mode->hdisplay & 0xf)
  371. return MODE_VIRTUAL_X;
  372. if (mode->vdisplay > 2048)
  373. return MODE_VIRTUAL_Y;
  374. /* filter out modes that would require too much memory bandwidth: */
  375. bandwidth = mode->hdisplay * mode->vdisplay * drm_mode_vrefresh(mode);
  376. if (bandwidth > priv->max_bandwidth)
  377. return MODE_BAD;
  378. return MODE_OK;
  379. }
  380. void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
  381. const struct tilcdc_panel_info *info)
  382. {
  383. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  384. tilcdc_crtc->info = info;
  385. }
  386. void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
  387. {
  388. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  389. struct drm_device *dev = crtc->dev;
  390. struct tilcdc_drm_private *priv = dev->dev_private;
  391. int dpms = tilcdc_crtc->dpms;
  392. unsigned int lcd_clk, div;
  393. int ret;
  394. pm_runtime_get_sync(dev->dev);
  395. if (dpms == DRM_MODE_DPMS_ON)
  396. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  397. /* in raster mode, minimum divisor is 2: */
  398. ret = clk_set_rate(priv->disp_clk, crtc->mode.clock * 1000 * 2);
  399. if (ret) {
  400. dev_err(dev->dev, "failed to set display clock rate to: %d\n",
  401. crtc->mode.clock);
  402. goto out;
  403. }
  404. lcd_clk = clk_get_rate(priv->clk);
  405. div = lcd_clk / (crtc->mode.clock * 1000);
  406. DBG("lcd_clk=%u, mode clock=%d, div=%u", lcd_clk, crtc->mode.clock, div);
  407. DBG("fck=%lu, dpll_disp_ck=%lu", clk_get_rate(priv->clk), clk_get_rate(priv->disp_clk));
  408. /* Configure the LCD clock divisor. */
  409. tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(div) |
  410. LCDC_RASTER_MODE);
  411. if (priv->rev == 2)
  412. tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
  413. LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
  414. LCDC_V2_CORE_CLK_EN);
  415. if (dpms == DRM_MODE_DPMS_ON)
  416. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  417. out:
  418. pm_runtime_put_sync(dev->dev);
  419. }
  420. irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
  421. {
  422. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  423. struct drm_device *dev = crtc->dev;
  424. struct tilcdc_drm_private *priv = dev->dev_private;
  425. uint32_t stat = tilcdc_read_irqstatus(dev);
  426. if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) {
  427. stop(crtc);
  428. dev_err(dev->dev, "error: %08x\n", stat);
  429. tilcdc_clear_irqstatus(dev, stat);
  430. start(crtc);
  431. } else if (stat & LCDC_PL_LOAD_DONE) {
  432. tilcdc_clear_irqstatus(dev, stat);
  433. } else {
  434. struct drm_pending_vblank_event *event;
  435. unsigned long flags;
  436. uint32_t dirty = tilcdc_crtc->dirty & stat;
  437. tilcdc_clear_irqstatus(dev, stat);
  438. if (dirty & LCDC_END_OF_FRAME0)
  439. set_scanout(crtc, 0);
  440. if (dirty & LCDC_END_OF_FRAME1)
  441. set_scanout(crtc, 1);
  442. drm_handle_vblank(dev, 0);
  443. spin_lock_irqsave(&dev->event_lock, flags);
  444. event = tilcdc_crtc->event;
  445. tilcdc_crtc->event = NULL;
  446. if (event)
  447. drm_send_vblank_event(dev, 0, event);
  448. spin_unlock_irqrestore(&dev->event_lock, flags);
  449. if (dirty && !tilcdc_crtc->dirty)
  450. drm_vblank_put(dev, 0);
  451. }
  452. if (priv->rev == 2) {
  453. if (stat & LCDC_FRAME_DONE) {
  454. tilcdc_crtc->frame_done = true;
  455. wake_up(&tilcdc_crtc->frame_done_wq);
  456. }
  457. tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
  458. }
  459. return IRQ_HANDLED;
  460. }
  461. void tilcdc_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  462. {
  463. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  464. struct drm_pending_vblank_event *event;
  465. struct drm_device *dev = crtc->dev;
  466. unsigned long flags;
  467. /* Destroy the pending vertical blanking event associated with the
  468. * pending page flip, if any, and disable vertical blanking interrupts.
  469. */
  470. spin_lock_irqsave(&dev->event_lock, flags);
  471. event = tilcdc_crtc->event;
  472. if (event && event->base.file_priv == file) {
  473. tilcdc_crtc->event = NULL;
  474. event->base.destroy(&event->base);
  475. drm_vblank_put(dev, 0);
  476. }
  477. spin_unlock_irqrestore(&dev->event_lock, flags);
  478. }
  479. struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
  480. {
  481. struct tilcdc_crtc *tilcdc_crtc;
  482. struct drm_crtc *crtc;
  483. int ret;
  484. tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
  485. if (!tilcdc_crtc) {
  486. dev_err(dev->dev, "allocation failed\n");
  487. return NULL;
  488. }
  489. crtc = &tilcdc_crtc->base;
  490. tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
  491. init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
  492. ret = kfifo_alloc(&tilcdc_crtc->unref_fifo, 16, GFP_KERNEL);
  493. if (ret) {
  494. dev_err(dev->dev, "could not allocate unref FIFO\n");
  495. goto fail;
  496. }
  497. INIT_WORK(&tilcdc_crtc->work, unref_worker);
  498. ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
  499. if (ret < 0)
  500. goto fail;
  501. drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
  502. return crtc;
  503. fail:
  504. tilcdc_crtc_destroy(crtc);
  505. return NULL;
  506. }