tilcdc_crtc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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 "drm_flip_work.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. struct drm_flip_work unref_work;
  33. };
  34. #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
  35. static void unref_worker(struct drm_flip_work *work, void *val)
  36. {
  37. struct tilcdc_crtc *tilcdc_crtc =
  38. container_of(work, struct tilcdc_crtc, unref_work);
  39. struct drm_device *dev = tilcdc_crtc->base.dev;
  40. mutex_lock(&dev->mode_config.mutex);
  41. drm_framebuffer_unreference(val);
  42. mutex_unlock(&dev->mode_config.mutex);
  43. }
  44. static void set_scanout(struct drm_crtc *crtc, int n)
  45. {
  46. static const uint32_t base_reg[] = {
  47. LCDC_DMA_FB_BASE_ADDR_0_REG,
  48. LCDC_DMA_FB_BASE_ADDR_1_REG,
  49. };
  50. static const uint32_t ceil_reg[] = {
  51. LCDC_DMA_FB_CEILING_ADDR_0_REG,
  52. 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. struct tilcdc_drm_private *priv = dev->dev_private;
  60. pm_runtime_get_sync(dev->dev);
  61. tilcdc_write(dev, base_reg[n], tilcdc_crtc->start);
  62. tilcdc_write(dev, ceil_reg[n], tilcdc_crtc->end);
  63. if (tilcdc_crtc->scanout[n]) {
  64. drm_flip_work_queue(&tilcdc_crtc->unref_work, tilcdc_crtc->scanout[n]);
  65. drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
  66. }
  67. tilcdc_crtc->scanout[n] = crtc->fb;
  68. drm_framebuffer_reference(tilcdc_crtc->scanout[n]);
  69. tilcdc_crtc->dirty &= ~stat[n];
  70. pm_runtime_put_sync(dev->dev);
  71. }
  72. static void update_scanout(struct drm_crtc *crtc)
  73. {
  74. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  75. struct drm_device *dev = crtc->dev;
  76. struct drm_framebuffer *fb = crtc->fb;
  77. struct drm_gem_cma_object *gem;
  78. unsigned int depth, bpp;
  79. drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
  80. gem = drm_fb_cma_get_gem_obj(fb, 0);
  81. tilcdc_crtc->start = gem->paddr + fb->offsets[0] +
  82. (crtc->y * fb->pitches[0]) + (crtc->x * bpp/8);
  83. tilcdc_crtc->end = tilcdc_crtc->start +
  84. (crtc->mode.vdisplay * fb->pitches[0]);
  85. if (tilcdc_crtc->dpms == DRM_MODE_DPMS_ON) {
  86. /* already enabled, so just mark the frames that need
  87. * updating and they will be updated on vblank:
  88. */
  89. tilcdc_crtc->dirty |= LCDC_END_OF_FRAME0 | LCDC_END_OF_FRAME1;
  90. drm_vblank_get(dev, 0);
  91. } else {
  92. /* not enabled yet, so update registers immediately: */
  93. set_scanout(crtc, 0);
  94. set_scanout(crtc, 1);
  95. }
  96. }
  97. static void start(struct drm_crtc *crtc)
  98. {
  99. struct drm_device *dev = crtc->dev;
  100. struct tilcdc_drm_private *priv = dev->dev_private;
  101. if (priv->rev == 2) {
  102. tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
  103. msleep(1);
  104. tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
  105. msleep(1);
  106. }
  107. tilcdc_set(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
  108. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
  109. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  110. }
  111. static void stop(struct drm_crtc *crtc)
  112. {
  113. struct drm_device *dev = crtc->dev;
  114. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  115. }
  116. static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
  117. {
  118. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  119. WARN_ON(tilcdc_crtc->dpms == DRM_MODE_DPMS_ON);
  120. drm_crtc_cleanup(crtc);
  121. drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
  122. kfree(tilcdc_crtc);
  123. }
  124. static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
  125. struct drm_framebuffer *fb,
  126. struct drm_pending_vblank_event *event,
  127. uint32_t page_flip_flags)
  128. {
  129. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  130. struct drm_device *dev = crtc->dev;
  131. if (tilcdc_crtc->event) {
  132. dev_err(dev->dev, "already pending page flip!\n");
  133. return -EBUSY;
  134. }
  135. crtc->fb = fb;
  136. tilcdc_crtc->event = event;
  137. update_scanout(crtc);
  138. return 0;
  139. }
  140. static void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
  141. {
  142. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  143. struct drm_device *dev = crtc->dev;
  144. struct tilcdc_drm_private *priv = dev->dev_private;
  145. /* we really only care about on or off: */
  146. if (mode != DRM_MODE_DPMS_ON)
  147. mode = DRM_MODE_DPMS_OFF;
  148. if (tilcdc_crtc->dpms == mode)
  149. return;
  150. tilcdc_crtc->dpms = mode;
  151. pm_runtime_get_sync(dev->dev);
  152. if (mode == DRM_MODE_DPMS_ON) {
  153. pm_runtime_forbid(dev->dev);
  154. start(crtc);
  155. } else {
  156. tilcdc_crtc->frame_done = false;
  157. stop(crtc);
  158. /*
  159. * if necessary wait for framedone irq which will still come
  160. * before putting things to sleep..
  161. */
  162. if (priv->rev == 2) {
  163. int ret = wait_event_timeout(
  164. tilcdc_crtc->frame_done_wq,
  165. tilcdc_crtc->frame_done,
  166. msecs_to_jiffies(50));
  167. if (ret == 0)
  168. dev_err(dev->dev, "timeout waiting for framedone\n");
  169. }
  170. pm_runtime_allow(dev->dev);
  171. }
  172. pm_runtime_put_sync(dev->dev);
  173. }
  174. static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
  175. const struct drm_display_mode *mode,
  176. struct drm_display_mode *adjusted_mode)
  177. {
  178. return true;
  179. }
  180. static void tilcdc_crtc_prepare(struct drm_crtc *crtc)
  181. {
  182. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  183. }
  184. static void tilcdc_crtc_commit(struct drm_crtc *crtc)
  185. {
  186. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  187. }
  188. static int tilcdc_crtc_mode_set(struct drm_crtc *crtc,
  189. struct drm_display_mode *mode,
  190. struct drm_display_mode *adjusted_mode,
  191. int x, int y,
  192. struct drm_framebuffer *old_fb)
  193. {
  194. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  195. struct drm_device *dev = crtc->dev;
  196. struct tilcdc_drm_private *priv = dev->dev_private;
  197. const struct tilcdc_panel_info *info = tilcdc_crtc->info;
  198. uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
  199. int ret;
  200. ret = tilcdc_crtc_mode_valid(crtc, mode);
  201. if (WARN_ON(ret))
  202. return ret;
  203. if (WARN_ON(!info))
  204. return -EINVAL;
  205. pm_runtime_get_sync(dev->dev);
  206. /* Configure the Burst Size and fifo threshold of DMA: */
  207. reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
  208. switch (info->dma_burst_sz) {
  209. case 1:
  210. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
  211. break;
  212. case 2:
  213. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
  214. break;
  215. case 4:
  216. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
  217. break;
  218. case 8:
  219. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
  220. break;
  221. case 16:
  222. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
  223. break;
  224. default:
  225. return -EINVAL;
  226. }
  227. reg |= (info->fifo_th << 8);
  228. tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
  229. /* Configure timings: */
  230. hbp = mode->htotal - mode->hsync_end;
  231. hfp = mode->hsync_start - mode->hdisplay;
  232. hsw = mode->hsync_end - mode->hsync_start;
  233. vbp = mode->vtotal - mode->vsync_end;
  234. vfp = mode->vsync_start - mode->vdisplay;
  235. vsw = mode->vsync_end - mode->vsync_start;
  236. DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
  237. mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
  238. /* Configure the AC Bias Period and Number of Transitions per Interrupt: */
  239. reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
  240. reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
  241. LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
  242. /*
  243. * subtract one from hfp, hbp, hsw because the hardware uses
  244. * a value of 0 as 1
  245. */
  246. if (priv->rev == 2) {
  247. /* clear bits we're going to set */
  248. reg &= ~0x78000033;
  249. reg |= ((hfp-1) & 0x300) >> 8;
  250. reg |= ((hbp-1) & 0x300) >> 4;
  251. reg |= ((hsw-1) & 0x3c0) << 21;
  252. }
  253. tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
  254. reg = (((mode->hdisplay >> 4) - 1) << 4) |
  255. (((hbp-1) & 0xff) << 24) |
  256. (((hfp-1) & 0xff) << 16) |
  257. (((hsw-1) & 0x3f) << 10);
  258. if (priv->rev == 2)
  259. reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
  260. tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
  261. reg = ((mode->vdisplay - 1) & 0x3ff) |
  262. ((vbp & 0xff) << 24) |
  263. ((vfp & 0xff) << 16) |
  264. (((vsw-1) & 0x3f) << 10);
  265. tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
  266. /*
  267. * be sure to set Bit 10 for the V2 LCDC controller,
  268. * otherwise limited to 1024 pixels width, stopping
  269. * 1920x1080 being suppoted.
  270. */
  271. if (priv->rev == 2) {
  272. if ((mode->vdisplay - 1) & 0x400) {
  273. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
  274. LCDC_LPP_B10);
  275. } else {
  276. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
  277. LCDC_LPP_B10);
  278. }
  279. }
  280. /* Configure display type: */
  281. reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
  282. ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
  283. LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK | 0x000ff000);
  284. reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
  285. if (info->tft_alt_mode)
  286. reg |= LCDC_TFT_ALT_ENABLE;
  287. if (priv->rev == 2) {
  288. unsigned int depth, bpp;
  289. drm_fb_get_bpp_depth(crtc->fb->pixel_format, &depth, &bpp);
  290. switch (bpp) {
  291. case 16:
  292. break;
  293. case 32:
  294. reg |= LCDC_V2_TFT_24BPP_UNPACK;
  295. /* fallthrough */
  296. case 24:
  297. reg |= LCDC_V2_TFT_24BPP_MODE;
  298. break;
  299. default:
  300. dev_err(dev->dev, "invalid pixel format\n");
  301. return -EINVAL;
  302. }
  303. }
  304. reg |= info->fdd < 12;
  305. tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
  306. if (info->invert_pxl_clk)
  307. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  308. else
  309. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  310. if (info->sync_ctrl)
  311. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  312. else
  313. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  314. if (info->sync_edge)
  315. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  316. else
  317. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  318. /*
  319. * use value from adjusted_mode here as this might have been
  320. * changed as part of the fixup for slave encoders to solve the
  321. * issue where tilcdc timings are not VESA compliant
  322. */
  323. if (adjusted_mode->flags & DRM_MODE_FLAG_NHSYNC)
  324. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
  325. else
  326. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
  327. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  328. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
  329. else
  330. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
  331. if (info->raster_order)
  332. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
  333. else
  334. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
  335. update_scanout(crtc);
  336. tilcdc_crtc_update_clk(crtc);
  337. pm_runtime_put_sync(dev->dev);
  338. return 0;
  339. }
  340. static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  341. struct drm_framebuffer *old_fb)
  342. {
  343. update_scanout(crtc);
  344. return 0;
  345. }
  346. static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
  347. .destroy = tilcdc_crtc_destroy,
  348. .set_config = drm_crtc_helper_set_config,
  349. .page_flip = tilcdc_crtc_page_flip,
  350. };
  351. static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
  352. .dpms = tilcdc_crtc_dpms,
  353. .mode_fixup = tilcdc_crtc_mode_fixup,
  354. .prepare = tilcdc_crtc_prepare,
  355. .commit = tilcdc_crtc_commit,
  356. .mode_set = tilcdc_crtc_mode_set,
  357. .mode_set_base = tilcdc_crtc_mode_set_base,
  358. };
  359. int tilcdc_crtc_max_width(struct drm_crtc *crtc)
  360. {
  361. struct drm_device *dev = crtc->dev;
  362. struct tilcdc_drm_private *priv = dev->dev_private;
  363. int max_width = 0;
  364. if (priv->rev == 1)
  365. max_width = 1024;
  366. else if (priv->rev == 2)
  367. max_width = 2048;
  368. return max_width;
  369. }
  370. int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
  371. {
  372. struct tilcdc_drm_private *priv = crtc->dev->dev_private;
  373. unsigned int bandwidth;
  374. uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
  375. /*
  376. * check to see if the width is within the range that
  377. * the LCD Controller physically supports
  378. */
  379. if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
  380. return MODE_VIRTUAL_X;
  381. /* width must be multiple of 16 */
  382. if (mode->hdisplay & 0xf)
  383. return MODE_VIRTUAL_X;
  384. if (mode->vdisplay > 2048)
  385. return MODE_VIRTUAL_Y;
  386. DBG("Processing mode %dx%d@%d with pixel clock %d",
  387. mode->hdisplay, mode->vdisplay,
  388. drm_mode_vrefresh(mode), mode->clock);
  389. hbp = mode->htotal - mode->hsync_end;
  390. hfp = mode->hsync_start - mode->hdisplay;
  391. hsw = mode->hsync_end - mode->hsync_start;
  392. vbp = mode->vtotal - mode->vsync_end;
  393. vfp = mode->vsync_start - mode->vdisplay;
  394. vsw = mode->vsync_end - mode->vsync_start;
  395. if ((hbp-1) & ~0x3ff) {
  396. DBG("Pruning mode: Horizontal Back Porch out of range");
  397. return MODE_HBLANK_WIDE;
  398. }
  399. if ((hfp-1) & ~0x3ff) {
  400. DBG("Pruning mode: Horizontal Front Porch out of range");
  401. return MODE_HBLANK_WIDE;
  402. }
  403. if ((hsw-1) & ~0x3ff) {
  404. DBG("Pruning mode: Horizontal Sync Width out of range");
  405. return MODE_HSYNC_WIDE;
  406. }
  407. if (vbp & ~0xff) {
  408. DBG("Pruning mode: Vertical Back Porch out of range");
  409. return MODE_VBLANK_WIDE;
  410. }
  411. if (vfp & ~0xff) {
  412. DBG("Pruning mode: Vertical Front Porch out of range");
  413. return MODE_VBLANK_WIDE;
  414. }
  415. if ((vsw-1) & ~0x3f) {
  416. DBG("Pruning mode: Vertical Sync Width out of range");
  417. return MODE_VSYNC_WIDE;
  418. }
  419. /*
  420. * some devices have a maximum allowed pixel clock
  421. * configured from the DT
  422. */
  423. if (mode->clock > priv->max_pixelclock) {
  424. DBG("Pruning mode: pixel clock too high");
  425. return MODE_CLOCK_HIGH;
  426. }
  427. /*
  428. * some devices further limit the max horizontal resolution
  429. * configured from the DT
  430. */
  431. if (mode->hdisplay > priv->max_width)
  432. return MODE_BAD_WIDTH;
  433. /* filter out modes that would require too much memory bandwidth: */
  434. bandwidth = mode->hdisplay * mode->vdisplay *
  435. drm_mode_vrefresh(mode);
  436. if (bandwidth > priv->max_bandwidth) {
  437. DBG("Pruning mode: exceeds defined bandwidth limit");
  438. return MODE_BAD;
  439. }
  440. return MODE_OK;
  441. }
  442. void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
  443. const struct tilcdc_panel_info *info)
  444. {
  445. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  446. tilcdc_crtc->info = info;
  447. }
  448. void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
  449. {
  450. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  451. struct drm_device *dev = crtc->dev;
  452. struct tilcdc_drm_private *priv = dev->dev_private;
  453. int dpms = tilcdc_crtc->dpms;
  454. unsigned int lcd_clk, div;
  455. int ret;
  456. pm_runtime_get_sync(dev->dev);
  457. if (dpms == DRM_MODE_DPMS_ON)
  458. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  459. /* in raster mode, minimum divisor is 2: */
  460. ret = clk_set_rate(priv->disp_clk, crtc->mode.clock * 1000 * 2);
  461. if (ret) {
  462. dev_err(dev->dev, "failed to set display clock rate to: %d\n",
  463. crtc->mode.clock);
  464. goto out;
  465. }
  466. lcd_clk = clk_get_rate(priv->clk);
  467. div = lcd_clk / (crtc->mode.clock * 1000);
  468. DBG("lcd_clk=%u, mode clock=%d, div=%u", lcd_clk, crtc->mode.clock, div);
  469. DBG("fck=%lu, dpll_disp_ck=%lu", clk_get_rate(priv->clk), clk_get_rate(priv->disp_clk));
  470. /* Configure the LCD clock divisor. */
  471. tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(div) |
  472. LCDC_RASTER_MODE);
  473. if (priv->rev == 2)
  474. tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
  475. LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
  476. LCDC_V2_CORE_CLK_EN);
  477. if (dpms == DRM_MODE_DPMS_ON)
  478. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  479. out:
  480. pm_runtime_put_sync(dev->dev);
  481. }
  482. irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
  483. {
  484. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  485. struct drm_device *dev = crtc->dev;
  486. struct tilcdc_drm_private *priv = dev->dev_private;
  487. uint32_t stat = tilcdc_read_irqstatus(dev);
  488. if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) {
  489. stop(crtc);
  490. dev_err(dev->dev, "error: %08x\n", stat);
  491. tilcdc_clear_irqstatus(dev, stat);
  492. start(crtc);
  493. } else if (stat & LCDC_PL_LOAD_DONE) {
  494. tilcdc_clear_irqstatus(dev, stat);
  495. } else {
  496. struct drm_pending_vblank_event *event;
  497. unsigned long flags;
  498. uint32_t dirty = tilcdc_crtc->dirty & stat;
  499. tilcdc_clear_irqstatus(dev, stat);
  500. if (dirty & LCDC_END_OF_FRAME0)
  501. set_scanout(crtc, 0);
  502. if (dirty & LCDC_END_OF_FRAME1)
  503. set_scanout(crtc, 1);
  504. drm_handle_vblank(dev, 0);
  505. spin_lock_irqsave(&dev->event_lock, flags);
  506. event = tilcdc_crtc->event;
  507. tilcdc_crtc->event = NULL;
  508. if (event)
  509. drm_send_vblank_event(dev, 0, event);
  510. spin_unlock_irqrestore(&dev->event_lock, flags);
  511. if (dirty && !tilcdc_crtc->dirty)
  512. drm_vblank_put(dev, 0);
  513. }
  514. if (priv->rev == 2) {
  515. if (stat & LCDC_FRAME_DONE) {
  516. tilcdc_crtc->frame_done = true;
  517. wake_up(&tilcdc_crtc->frame_done_wq);
  518. }
  519. tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
  520. }
  521. return IRQ_HANDLED;
  522. }
  523. void tilcdc_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  524. {
  525. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  526. struct drm_pending_vblank_event *event;
  527. struct drm_device *dev = crtc->dev;
  528. unsigned long flags;
  529. /* Destroy the pending vertical blanking event associated with the
  530. * pending page flip, if any, and disable vertical blanking interrupts.
  531. */
  532. spin_lock_irqsave(&dev->event_lock, flags);
  533. event = tilcdc_crtc->event;
  534. if (event && event->base.file_priv == file) {
  535. tilcdc_crtc->event = NULL;
  536. event->base.destroy(&event->base);
  537. drm_vblank_put(dev, 0);
  538. }
  539. spin_unlock_irqrestore(&dev->event_lock, flags);
  540. }
  541. struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
  542. {
  543. struct tilcdc_crtc *tilcdc_crtc;
  544. struct drm_crtc *crtc;
  545. int ret;
  546. tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
  547. if (!tilcdc_crtc) {
  548. dev_err(dev->dev, "allocation failed\n");
  549. return NULL;
  550. }
  551. crtc = &tilcdc_crtc->base;
  552. tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
  553. init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
  554. ret = drm_flip_work_init(&tilcdc_crtc->unref_work, 16,
  555. "unref", unref_worker);
  556. if (ret) {
  557. dev_err(dev->dev, "could not allocate unref FIFO\n");
  558. goto fail;
  559. }
  560. ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
  561. if (ret < 0)
  562. goto fail;
  563. drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
  564. return crtc;
  565. fail:
  566. tilcdc_crtc_destroy(crtc);
  567. return NULL;
  568. }