tilcdc_crtc.c 21 KB

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