tilcdc_crtc.c 18 KB

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