tilcdc_crtc.c 19 KB

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