tilcdc_crtc.c 18 KB

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