tilcdc_crtc.c 17 KB

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