tilcdc_crtc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  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. }
  101. static int tilcdc_verify_fb(struct drm_crtc *crtc, struct drm_framebuffer *fb)
  102. {
  103. struct drm_device *dev = crtc->dev;
  104. unsigned int depth, bpp;
  105. drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
  106. if (fb->pitches[0] != crtc->mode.hdisplay * bpp / 8) {
  107. dev_err(dev->dev,
  108. "Invalid pitch: fb and crtc widths must be the same");
  109. return -EINVAL;
  110. }
  111. return 0;
  112. }
  113. static int tilcdc_crtc_page_flip(struct drm_crtc *crtc,
  114. struct drm_framebuffer *fb,
  115. struct drm_pending_vblank_event *event,
  116. uint32_t page_flip_flags)
  117. {
  118. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  119. struct drm_device *dev = crtc->dev;
  120. int r;
  121. unsigned long flags;
  122. s64 tdiff;
  123. ktime_t next_vblank;
  124. r = tilcdc_verify_fb(crtc, fb);
  125. if (r)
  126. return r;
  127. if (tilcdc_crtc->event) {
  128. dev_err(dev->dev, "already pending page flip!\n");
  129. return -EBUSY;
  130. }
  131. drm_framebuffer_reference(fb);
  132. crtc->primary->fb = fb;
  133. pm_runtime_get_sync(dev->dev);
  134. spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
  135. next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
  136. 1000000 / crtc->hwmode.vrefresh);
  137. tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
  138. if (tdiff >= TILCDC_VBLANK_SAFETY_THRESHOLD_US)
  139. set_scanout(crtc, fb);
  140. else
  141. tilcdc_crtc->next_fb = fb;
  142. tilcdc_crtc->event = event;
  143. spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
  144. pm_runtime_put_sync(dev->dev);
  145. return 0;
  146. }
  147. void tilcdc_crtc_dpms(struct drm_crtc *crtc, int mode)
  148. {
  149. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  150. struct drm_device *dev = crtc->dev;
  151. struct tilcdc_drm_private *priv = dev->dev_private;
  152. /* we really only care about on or off: */
  153. if (mode != DRM_MODE_DPMS_ON)
  154. mode = DRM_MODE_DPMS_OFF;
  155. if (tilcdc_crtc->dpms == mode)
  156. return;
  157. tilcdc_crtc->dpms = mode;
  158. if (mode == DRM_MODE_DPMS_ON) {
  159. pm_runtime_get_sync(dev->dev);
  160. start(crtc);
  161. } else {
  162. tilcdc_crtc->frame_done = false;
  163. stop(crtc);
  164. /*
  165. * if necessary wait for framedone irq which will still come
  166. * before putting things to sleep..
  167. */
  168. if (priv->rev == 2) {
  169. int ret = wait_event_timeout(
  170. tilcdc_crtc->frame_done_wq,
  171. tilcdc_crtc->frame_done,
  172. msecs_to_jiffies(50));
  173. if (ret == 0)
  174. dev_err(dev->dev, "timeout waiting for framedone\n");
  175. }
  176. pm_runtime_put_sync(dev->dev);
  177. if (tilcdc_crtc->next_fb) {
  178. drm_flip_work_queue(&tilcdc_crtc->unref_work,
  179. tilcdc_crtc->next_fb);
  180. tilcdc_crtc->next_fb = NULL;
  181. }
  182. if (tilcdc_crtc->curr_fb) {
  183. drm_flip_work_queue(&tilcdc_crtc->unref_work,
  184. tilcdc_crtc->curr_fb);
  185. tilcdc_crtc->curr_fb = NULL;
  186. }
  187. drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
  188. }
  189. }
  190. int tilcdc_crtc_current_dpms_state(struct drm_crtc *crtc)
  191. {
  192. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  193. return tilcdc_crtc->dpms;
  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. drm_framebuffer_reference(crtc->primary->fb);
  378. set_scanout(crtc, crtc->primary->fb);
  379. tilcdc_crtc_update_clk(crtc);
  380. pm_runtime_put_sync(dev->dev);
  381. return 0;
  382. }
  383. static int tilcdc_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  384. struct drm_framebuffer *old_fb)
  385. {
  386. struct drm_device *dev = crtc->dev;
  387. int r;
  388. r = tilcdc_verify_fb(crtc, crtc->primary->fb);
  389. if (r)
  390. return r;
  391. drm_framebuffer_reference(crtc->primary->fb);
  392. pm_runtime_get_sync(dev->dev);
  393. set_scanout(crtc, crtc->primary->fb);
  394. pm_runtime_put_sync(dev->dev);
  395. return 0;
  396. }
  397. static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
  398. .destroy = tilcdc_crtc_destroy,
  399. .set_config = drm_crtc_helper_set_config,
  400. .page_flip = tilcdc_crtc_page_flip,
  401. };
  402. static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
  403. .dpms = tilcdc_crtc_dpms,
  404. .mode_fixup = tilcdc_crtc_mode_fixup,
  405. .prepare = tilcdc_crtc_prepare,
  406. .commit = tilcdc_crtc_commit,
  407. .mode_set = tilcdc_crtc_mode_set,
  408. .mode_set_base = tilcdc_crtc_mode_set_base,
  409. };
  410. int tilcdc_crtc_max_width(struct drm_crtc *crtc)
  411. {
  412. struct drm_device *dev = crtc->dev;
  413. struct tilcdc_drm_private *priv = dev->dev_private;
  414. int max_width = 0;
  415. if (priv->rev == 1)
  416. max_width = 1024;
  417. else if (priv->rev == 2)
  418. max_width = 2048;
  419. return max_width;
  420. }
  421. int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
  422. {
  423. struct tilcdc_drm_private *priv = crtc->dev->dev_private;
  424. unsigned int bandwidth;
  425. uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
  426. /*
  427. * check to see if the width is within the range that
  428. * the LCD Controller physically supports
  429. */
  430. if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
  431. return MODE_VIRTUAL_X;
  432. /* width must be multiple of 16 */
  433. if (mode->hdisplay & 0xf)
  434. return MODE_VIRTUAL_X;
  435. if (mode->vdisplay > 2048)
  436. return MODE_VIRTUAL_Y;
  437. DBG("Processing mode %dx%d@%d with pixel clock %d",
  438. mode->hdisplay, mode->vdisplay,
  439. drm_mode_vrefresh(mode), mode->clock);
  440. hbp = mode->htotal - mode->hsync_end;
  441. hfp = mode->hsync_start - mode->hdisplay;
  442. hsw = mode->hsync_end - mode->hsync_start;
  443. vbp = mode->vtotal - mode->vsync_end;
  444. vfp = mode->vsync_start - mode->vdisplay;
  445. vsw = mode->vsync_end - mode->vsync_start;
  446. if ((hbp-1) & ~0x3ff) {
  447. DBG("Pruning mode: Horizontal Back Porch out of range");
  448. return MODE_HBLANK_WIDE;
  449. }
  450. if ((hfp-1) & ~0x3ff) {
  451. DBG("Pruning mode: Horizontal Front Porch out of range");
  452. return MODE_HBLANK_WIDE;
  453. }
  454. if ((hsw-1) & ~0x3ff) {
  455. DBG("Pruning mode: Horizontal Sync Width out of range");
  456. return MODE_HSYNC_WIDE;
  457. }
  458. if (vbp & ~0xff) {
  459. DBG("Pruning mode: Vertical Back Porch out of range");
  460. return MODE_VBLANK_WIDE;
  461. }
  462. if (vfp & ~0xff) {
  463. DBG("Pruning mode: Vertical Front Porch out of range");
  464. return MODE_VBLANK_WIDE;
  465. }
  466. if ((vsw-1) & ~0x3f) {
  467. DBG("Pruning mode: Vertical Sync Width out of range");
  468. return MODE_VSYNC_WIDE;
  469. }
  470. /*
  471. * some devices have a maximum allowed pixel clock
  472. * configured from the DT
  473. */
  474. if (mode->clock > priv->max_pixelclock) {
  475. DBG("Pruning mode: pixel clock too high");
  476. return MODE_CLOCK_HIGH;
  477. }
  478. /*
  479. * some devices further limit the max horizontal resolution
  480. * configured from the DT
  481. */
  482. if (mode->hdisplay > priv->max_width)
  483. return MODE_BAD_WIDTH;
  484. /* filter out modes that would require too much memory bandwidth: */
  485. bandwidth = mode->hdisplay * mode->vdisplay *
  486. drm_mode_vrefresh(mode);
  487. if (bandwidth > priv->max_bandwidth) {
  488. DBG("Pruning mode: exceeds defined bandwidth limit");
  489. return MODE_BAD;
  490. }
  491. return MODE_OK;
  492. }
  493. void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
  494. const struct tilcdc_panel_info *info)
  495. {
  496. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  497. tilcdc_crtc->info = info;
  498. }
  499. void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
  500. bool simulate_vesa_sync)
  501. {
  502. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  503. tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
  504. }
  505. void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
  506. {
  507. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  508. struct drm_device *dev = crtc->dev;
  509. struct tilcdc_drm_private *priv = dev->dev_private;
  510. int dpms = tilcdc_crtc->dpms;
  511. unsigned long lcd_clk;
  512. const unsigned clkdiv = 2; /* using a fixed divider of 2 */
  513. int ret;
  514. pm_runtime_get_sync(dev->dev);
  515. if (dpms == DRM_MODE_DPMS_ON)
  516. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  517. /* mode.clock is in KHz, set_rate wants parameter in Hz */
  518. ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
  519. if (ret < 0) {
  520. dev_err(dev->dev, "failed to set display clock rate to: %d\n",
  521. crtc->mode.clock);
  522. goto out;
  523. }
  524. lcd_clk = clk_get_rate(priv->clk);
  525. DBG("lcd_clk=%lu, mode clock=%d, div=%u",
  526. lcd_clk, crtc->mode.clock, clkdiv);
  527. /* Configure the LCD clock divisor. */
  528. tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
  529. LCDC_RASTER_MODE);
  530. if (priv->rev == 2)
  531. tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
  532. LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
  533. LCDC_V2_CORE_CLK_EN);
  534. if (dpms == DRM_MODE_DPMS_ON)
  535. tilcdc_crtc_dpms(crtc, DRM_MODE_DPMS_ON);
  536. out:
  537. pm_runtime_put_sync(dev->dev);
  538. }
  539. #define SYNC_LOST_COUNT_LIMIT 50
  540. irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
  541. {
  542. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  543. struct drm_device *dev = crtc->dev;
  544. struct tilcdc_drm_private *priv = dev->dev_private;
  545. uint32_t stat;
  546. stat = tilcdc_read_irqstatus(dev);
  547. tilcdc_clear_irqstatus(dev, stat);
  548. if (stat & LCDC_END_OF_FRAME0) {
  549. unsigned long flags;
  550. bool skip_event = false;
  551. ktime_t now;
  552. now = ktime_get();
  553. drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
  554. spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
  555. tilcdc_crtc->last_vblank = now;
  556. if (tilcdc_crtc->next_fb) {
  557. set_scanout(crtc, tilcdc_crtc->next_fb);
  558. tilcdc_crtc->next_fb = NULL;
  559. skip_event = true;
  560. }
  561. spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
  562. drm_crtc_handle_vblank(crtc);
  563. if (!skip_event) {
  564. struct drm_pending_vblank_event *event;
  565. spin_lock_irqsave(&dev->event_lock, flags);
  566. event = tilcdc_crtc->event;
  567. tilcdc_crtc->event = NULL;
  568. if (event)
  569. drm_crtc_send_vblank_event(crtc, event);
  570. spin_unlock_irqrestore(&dev->event_lock, flags);
  571. }
  572. if (tilcdc_crtc->frame_intact)
  573. tilcdc_crtc->sync_lost_count = 0;
  574. else
  575. tilcdc_crtc->frame_intact = true;
  576. }
  577. if (priv->rev == 2) {
  578. if (stat & LCDC_FRAME_DONE) {
  579. tilcdc_crtc->frame_done = true;
  580. wake_up(&tilcdc_crtc->frame_done_wq);
  581. }
  582. tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
  583. }
  584. if (stat & LCDC_SYNC_LOST) {
  585. dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
  586. __func__, stat);
  587. tilcdc_crtc->frame_intact = false;
  588. if (tilcdc_crtc->sync_lost_count++ > SYNC_LOST_COUNT_LIMIT) {
  589. dev_err(dev->dev,
  590. "%s(0x%08x): Sync lost flood detected, disabling the interrupt",
  591. __func__, stat);
  592. tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
  593. LCDC_SYNC_LOST);
  594. }
  595. }
  596. if (stat & LCDC_FIFO_UNDERFLOW)
  597. dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
  598. __func__, stat);
  599. return IRQ_HANDLED;
  600. }
  601. struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
  602. {
  603. struct tilcdc_drm_private *priv = dev->dev_private;
  604. struct tilcdc_crtc *tilcdc_crtc;
  605. struct drm_crtc *crtc;
  606. int ret;
  607. tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
  608. if (!tilcdc_crtc) {
  609. dev_err(dev->dev, "allocation failed\n");
  610. return NULL;
  611. }
  612. crtc = &tilcdc_crtc->base;
  613. tilcdc_crtc->dpms = DRM_MODE_DPMS_OFF;
  614. init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
  615. drm_flip_work_init(&tilcdc_crtc->unref_work,
  616. "unref", unref_worker);
  617. spin_lock_init(&tilcdc_crtc->irq_lock);
  618. ret = drm_crtc_init(dev, crtc, &tilcdc_crtc_funcs);
  619. if (ret < 0)
  620. goto fail;
  621. drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
  622. if (priv->is_componentized) {
  623. struct device_node *ports =
  624. of_get_child_by_name(dev->dev->of_node, "ports");
  625. if (ports) {
  626. crtc->port = of_get_child_by_name(ports, "port");
  627. of_node_put(ports);
  628. } else {
  629. crtc->port =
  630. of_get_child_by_name(dev->dev->of_node, "port");
  631. }
  632. if (!crtc->port) { /* This should never happen */
  633. dev_err(dev->dev, "Port node not found in %s\n",
  634. dev->dev->of_node->full_name);
  635. goto fail;
  636. }
  637. }
  638. return crtc;
  639. fail:
  640. tilcdc_crtc_destroy(crtc);
  641. return NULL;
  642. }