tilcdc_crtc.c 19 KB

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