tilcdc_crtc.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  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. /* Configure display type: */
  265. reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
  266. ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
  267. LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
  268. reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
  269. if (info->tft_alt_mode)
  270. reg |= LCDC_TFT_ALT_ENABLE;
  271. if (priv->rev == 2) {
  272. unsigned int depth, bpp;
  273. drm_fb_get_bpp_depth(crtc->fb->pixel_format, &depth, &bpp);
  274. switch (bpp) {
  275. case 16:
  276. break;
  277. case 32:
  278. reg |= LCDC_V2_TFT_24BPP_UNPACK;
  279. /* fallthrough */
  280. case 24:
  281. reg |= LCDC_V2_TFT_24BPP_MODE;
  282. break;
  283. default:
  284. dev_err(dev->dev, "invalid pixel format\n");
  285. return -EINVAL;
  286. }
  287. }
  288. reg |= info->fdd < 12;
  289. tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
  290. if (info->invert_pxl_clk)
  291. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  292. else
  293. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  294. if (info->sync_ctrl)
  295. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  296. else
  297. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  298. if (info->sync_edge)
  299. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  300. else
  301. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  302. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  303. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
  304. else
  305. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
  306. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  307. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
  308. else
  309. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
  310. if (info->raster_order)
  311. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
  312. else
  313. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
  314. update_scanout(crtc);
  315. tilcdc_crtc_update_clk(crtc);
  316. pm_runtime_put_sync(dev->dev);
  317. return 0;
  318. }
  319. static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  320. struct drm_framebuffer *old_fb)
  321. {
  322. update_scanout(crtc);
  323. return 0;
  324. }
  325. static void tilcdc_crtc_load_lut(struct drm_crtc *crtc)
  326. {
  327. }
  328. static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
  329. .destroy = tilcdc_crtc_destroy,
  330. .set_config = drm_crtc_helper_set_config,
  331. .page_flip = tilcdc_crtc_page_flip,
  332. };
  333. static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
  334. .dpms = tilcdc_crtc_dpms,
  335. .mode_fixup = tilcdc_crtc_mode_fixup,
  336. .prepare = tilcdc_crtc_prepare,
  337. .commit = tilcdc_crtc_commit,
  338. .mode_set = tilcdc_crtc_mode_set,
  339. .mode_set_base = tilcdc_crtc_mode_set_base,
  340. .load_lut = tilcdc_crtc_load_lut,
  341. };
  342. int tilcdc_crtc_max_width(struct drm_crtc *crtc)
  343. {
  344. struct drm_device *dev = crtc->dev;
  345. struct tilcdc_drm_private *priv = dev->dev_private;
  346. int max_width = 0;
  347. if (priv->rev == 1)
  348. max_width = 1024;
  349. else if (priv->rev == 2)
  350. max_width = 2048;
  351. return max_width;
  352. }
  353. int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
  354. {
  355. struct tilcdc_drm_private *priv = crtc->dev->dev_private;
  356. unsigned int bandwidth;
  357. if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
  358. return MODE_VIRTUAL_X;
  359. /* width must be multiple of 16 */
  360. if (mode->hdisplay & 0xf)
  361. return MODE_VIRTUAL_X;
  362. if (mode->vdisplay > 2048)
  363. return MODE_VIRTUAL_Y;
  364. /* filter out modes that would require too much memory bandwidth: */
  365. bandwidth = mode->hdisplay * mode->vdisplay * drm_mode_vrefresh(mode);
  366. if (bandwidth > priv->max_bandwidth)
  367. return MODE_BAD;
  368. return MODE_OK;
  369. }
  370. void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
  371. const struct tilcdc_panel_info *info)
  372. {
  373. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  374. tilcdc_crtc->info = info;
  375. }
  376. void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
  377. {
  378. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  379. struct drm_device *dev = crtc->dev;
  380. struct tilcdc_drm_private *priv = dev->dev_private;
  381. int dpms = tilcdc_crtc->dpms;
  382. unsigned int lcd_clk, div;
  383. int ret;
  384. pm_runtime_get_sync(dev->dev);
  385. if (dpms == DRM_MODE_DPMS_ON)
  386. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  387. /* in raster mode, minimum divisor is 2: */
  388. ret = clk_set_rate(priv->disp_clk, crtc->mode.clock * 1000 * 2);
  389. if (ret) {
  390. dev_err(dev->dev, "failed to set display clock rate to: %d\n",
  391. crtc->mode.clock);
  392. goto out;
  393. }
  394. lcd_clk = clk_get_rate(priv->clk);
  395. div = lcd_clk / (crtc->mode.clock * 1000);
  396. DBG("lcd_clk=%u, mode clock=%d, div=%u", lcd_clk, crtc->mode.clock, div);
  397. DBG("fck=%lu, dpll_disp_ck=%lu", clk_get_rate(priv->clk), clk_get_rate(priv->disp_clk));
  398. /* Configure the LCD clock divisor. */
  399. tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(div) |
  400. LCDC_RASTER_MODE);
  401. if (priv->rev == 2)
  402. tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
  403. LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
  404. LCDC_V2_CORE_CLK_EN);
  405. if (dpms == DRM_MODE_DPMS_ON)
  406. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  407. out:
  408. pm_runtime_put_sync(dev->dev);
  409. }
  410. irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
  411. {
  412. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  413. struct drm_device *dev = crtc->dev;
  414. struct tilcdc_drm_private *priv = dev->dev_private;
  415. uint32_t stat = tilcdc_read_irqstatus(dev);
  416. if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) {
  417. stop(crtc);
  418. dev_err(dev->dev, "error: %08x\n", stat);
  419. tilcdc_clear_irqstatus(dev, stat);
  420. start(crtc);
  421. } else if (stat & LCDC_PL_LOAD_DONE) {
  422. tilcdc_clear_irqstatus(dev, stat);
  423. } else {
  424. struct drm_pending_vblank_event *event;
  425. unsigned long flags;
  426. uint32_t dirty = tilcdc_crtc->dirty & stat;
  427. tilcdc_clear_irqstatus(dev, stat);
  428. if (dirty & LCDC_END_OF_FRAME0)
  429. set_scanout(crtc, 0);
  430. if (dirty & LCDC_END_OF_FRAME1)
  431. set_scanout(crtc, 1);
  432. drm_handle_vblank(dev, 0);
  433. spin_lock_irqsave(&dev->event_lock, flags);
  434. event = tilcdc_crtc->event;
  435. tilcdc_crtc->event = NULL;
  436. if (event)
  437. drm_send_vblank_event(dev, 0, event);
  438. spin_unlock_irqrestore(&dev->event_lock, flags);
  439. if (dirty && !tilcdc_crtc->dirty)
  440. drm_vblank_put(dev, 0);
  441. }
  442. if (priv->rev == 2) {
  443. if (stat & LCDC_FRAME_DONE) {
  444. tilcdc_crtc->frame_done = true;
  445. wake_up(&tilcdc_crtc->frame_done_wq);
  446. }
  447. tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
  448. }
  449. return IRQ_HANDLED;
  450. }
  451. void tilcdc_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  452. {
  453. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  454. struct drm_pending_vblank_event *event;
  455. struct drm_device *dev = crtc->dev;
  456. unsigned long flags;
  457. /* Destroy the pending vertical blanking event associated with the
  458. * pending page flip, if any, and disable vertical blanking interrupts.
  459. */
  460. spin_lock_irqsave(&dev->event_lock, flags);
  461. event = tilcdc_crtc->event;
  462. if (event && event->base.file_priv == file) {
  463. tilcdc_crtc->event = NULL;
  464. event->base.destroy(&event->base);
  465. drm_vblank_put(dev, 0);
  466. }
  467. spin_unlock_irqrestore(&dev->event_lock, flags);
  468. }
  469. struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
  470. {
  471. struct tilcdc_crtc *tilcdc_crtc;
  472. struct drm_crtc *crtc;
  473. int ret;
  474. tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
  475. if (!tilcdc_crtc) {
  476. dev_err(dev->dev, "allocation failed\n");
  477. return NULL;
  478. }
  479. crtc = &tilcdc_crtc->base;
  480. tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
  481. init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
  482. ret = kfifo_alloc(&tilcdc_crtc->unref_fifo, 16, GFP_KERNEL);
  483. if (ret) {
  484. dev_err(dev->dev, "could not allocate unref FIFO\n");
  485. goto fail;
  486. }
  487. INIT_WORK(&tilcdc_crtc->work, unref_worker);
  488. ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
  489. if (ret < 0)
  490. goto fail;
  491. drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
  492. return crtc;
  493. fail:
  494. tilcdc_crtc_destroy(crtc);
  495. return NULL;
  496. }