tilcdc_crtc.c 19 KB

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