tilcdc_crtc.c 20 KB

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