tilcdc_crtc.c 21 KB

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