tilcdc_crtc.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682
  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. uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
  372. /*
  373. * check to see if the width is within the range that
  374. * the LCD Controller physically supports
  375. */
  376. if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
  377. return MODE_VIRTUAL_X;
  378. /* width must be multiple of 16 */
  379. if (mode->hdisplay & 0xf)
  380. return MODE_VIRTUAL_X;
  381. if (mode->vdisplay > 2048)
  382. return MODE_VIRTUAL_Y;
  383. DBG("Processing mode %dx%d@%d with pixel clock %d",
  384. mode->hdisplay, mode->vdisplay,
  385. drm_mode_vrefresh(mode), mode->clock);
  386. hbp = mode->htotal - mode->hsync_end;
  387. hfp = mode->hsync_start - mode->hdisplay;
  388. hsw = mode->hsync_end - mode->hsync_start;
  389. vbp = mode->vtotal - mode->vsync_end;
  390. vfp = mode->vsync_start - mode->vdisplay;
  391. vsw = mode->vsync_end - mode->vsync_start;
  392. if ((hbp-1) & ~0x3ff) {
  393. DBG("Pruning mode: Horizontal Back Porch out of range");
  394. return MODE_HBLANK_WIDE;
  395. }
  396. if ((hfp-1) & ~0x3ff) {
  397. DBG("Pruning mode: Horizontal Front Porch out of range");
  398. return MODE_HBLANK_WIDE;
  399. }
  400. if ((hsw-1) & ~0x3ff) {
  401. DBG("Pruning mode: Horizontal Sync Width out of range");
  402. return MODE_HSYNC_WIDE;
  403. }
  404. if (vbp & ~0xff) {
  405. DBG("Pruning mode: Vertical Back Porch out of range");
  406. return MODE_VBLANK_WIDE;
  407. }
  408. if (vfp & ~0xff) {
  409. DBG("Pruning mode: Vertical Front Porch out of range");
  410. return MODE_VBLANK_WIDE;
  411. }
  412. if ((vsw-1) & ~0x3f) {
  413. DBG("Pruning mode: Vertical Sync Width out of range");
  414. return MODE_VSYNC_WIDE;
  415. }
  416. /*
  417. * some devices have a maximum allowed pixel clock
  418. * configured from the DT
  419. */
  420. if (mode->clock > priv->max_pixelclock) {
  421. DBG("Pruning mode, pixel clock too high");
  422. return MODE_CLOCK_HIGH;
  423. }
  424. /*
  425. * some devices further limit the max horizontal resolution
  426. * configured from the DT
  427. */
  428. if (mode->hdisplay > priv->max_width)
  429. return MODE_BAD_WIDTH;
  430. /* filter out modes that would require too much memory bandwidth: */
  431. bandwidth = mode->hdisplay * mode->vdisplay *
  432. drm_mode_vrefresh(mode);
  433. if (bandwidth > priv->max_bandwidth) {
  434. DBG("Pruning mode, exceeds defined bandwidth limit");
  435. return MODE_BAD;
  436. }
  437. return MODE_OK;
  438. }
  439. void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
  440. const struct tilcdc_panel_info *info)
  441. {
  442. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  443. tilcdc_crtc->info = info;
  444. }
  445. void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
  446. {
  447. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  448. struct drm_device *dev = crtc->dev;
  449. struct tilcdc_drm_private *priv = dev->dev_private;
  450. int dpms = tilcdc_crtc->dpms;
  451. unsigned int lcd_clk, div;
  452. int ret;
  453. pm_runtime_get_sync(dev->dev);
  454. if (dpms == DRM_MODE_DPMS_ON)
  455. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  456. /* in raster mode, minimum divisor is 2: */
  457. ret = clk_set_rate(priv->disp_clk, crtc->mode.clock * 1000 * 2);
  458. if (ret) {
  459. dev_err(dev->dev, "failed to set display clock rate to: %d\n",
  460. crtc->mode.clock);
  461. goto out;
  462. }
  463. lcd_clk = clk_get_rate(priv->clk);
  464. div = lcd_clk / (crtc->mode.clock * 1000);
  465. DBG("lcd_clk=%u, mode clock=%d, div=%u", lcd_clk, crtc->mode.clock, div);
  466. DBG("fck=%lu, dpll_disp_ck=%lu", clk_get_rate(priv->clk), clk_get_rate(priv->disp_clk));
  467. /* Configure the LCD clock divisor. */
  468. tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(div) |
  469. LCDC_RASTER_MODE);
  470. if (priv->rev == 2)
  471. tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
  472. LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
  473. LCDC_V2_CORE_CLK_EN);
  474. if (dpms == DRM_MODE_DPMS_ON)
  475. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  476. out:
  477. pm_runtime_put_sync(dev->dev);
  478. }
  479. irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
  480. {
  481. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  482. struct drm_device *dev = crtc->dev;
  483. struct tilcdc_drm_private *priv = dev->dev_private;
  484. uint32_t stat = tilcdc_read_irqstatus(dev);
  485. if ((stat & LCDC_SYNC_LOST) && (stat & LCDC_FIFO_UNDERFLOW)) {
  486. stop(crtc);
  487. dev_err(dev->dev, "error: %08x\n", stat);
  488. tilcdc_clear_irqstatus(dev, stat);
  489. start(crtc);
  490. } else if (stat & LCDC_PL_LOAD_DONE) {
  491. tilcdc_clear_irqstatus(dev, stat);
  492. } else {
  493. struct drm_pending_vblank_event *event;
  494. unsigned long flags;
  495. uint32_t dirty = tilcdc_crtc->dirty & stat;
  496. tilcdc_clear_irqstatus(dev, stat);
  497. if (dirty & LCDC_END_OF_FRAME0)
  498. set_scanout(crtc, 0);
  499. if (dirty & LCDC_END_OF_FRAME1)
  500. set_scanout(crtc, 1);
  501. drm_handle_vblank(dev, 0);
  502. spin_lock_irqsave(&dev->event_lock, flags);
  503. event = tilcdc_crtc->event;
  504. tilcdc_crtc->event = NULL;
  505. if (event)
  506. drm_send_vblank_event(dev, 0, event);
  507. spin_unlock_irqrestore(&dev->event_lock, flags);
  508. if (dirty && !tilcdc_crtc->dirty)
  509. drm_vblank_put(dev, 0);
  510. }
  511. if (priv->rev == 2) {
  512. if (stat & LCDC_FRAME_DONE) {
  513. tilcdc_crtc->frame_done = true;
  514. wake_up(&tilcdc_crtc->frame_done_wq);
  515. }
  516. tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
  517. }
  518. return IRQ_HANDLED;
  519. }
  520. void tilcdc_crtc_cancel_page_flip(struct drm_crtc *crtc, struct drm_file *file)
  521. {
  522. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  523. struct drm_pending_vblank_event *event;
  524. struct drm_device *dev = crtc->dev;
  525. unsigned long flags;
  526. /* Destroy the pending vertical blanking event associated with the
  527. * pending page flip, if any, and disable vertical blanking interrupts.
  528. */
  529. spin_lock_irqsave(&dev->event_lock, flags);
  530. event = tilcdc_crtc->event;
  531. if (event && event->base.file_priv == file) {
  532. tilcdc_crtc->event = NULL;
  533. event->base.destroy(&event->base);
  534. drm_vblank_put(dev, 0);
  535. }
  536. spin_unlock_irqrestore(&dev->event_lock, flags);
  537. }
  538. struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
  539. {
  540. struct tilcdc_crtc *tilcdc_crtc;
  541. struct drm_crtc *crtc;
  542. int ret;
  543. tilcdc_crtc = kzalloc(sizeof(*tilcdc_crtc), GFP_KERNEL);
  544. if (!tilcdc_crtc) {
  545. dev_err(dev->dev, "allocation failed\n");
  546. return NULL;
  547. }
  548. crtc = &tilcdc_crtc->base;
  549. tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
  550. init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
  551. ret = kfifo_alloc(&tilcdc_crtc->unref_fifo, 16, GFP_KERNEL);
  552. if (ret) {
  553. dev_err(dev->dev, "could not allocate unref FIFO\n");
  554. goto fail;
  555. }
  556. INIT_WORK(&tilcdc_crtc->work, unref_worker);
  557. ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
  558. if (ret < 0)
  559. goto fail;
  560. drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
  561. return crtc;
  562. fail:
  563. tilcdc_crtc_destroy(crtc);
  564. return NULL;
  565. }