tilcdc_crtc.c 20 KB

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