tilcdc_crtc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 <drm/drm_atomic_helper.h>
  20. #include "tilcdc_drv.h"
  21. #include "tilcdc_regs.h"
  22. #define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
  23. struct tilcdc_crtc {
  24. struct drm_crtc base;
  25. struct drm_plane primary;
  26. const struct tilcdc_panel_info *info;
  27. struct drm_pending_vblank_event *event;
  28. bool enabled;
  29. wait_queue_head_t frame_done_wq;
  30. bool frame_done;
  31. spinlock_t irq_lock;
  32. ktime_t last_vblank;
  33. struct drm_framebuffer *curr_fb;
  34. struct drm_framebuffer *next_fb;
  35. /* for deferred fb unref's: */
  36. struct drm_flip_work unref_work;
  37. /* Only set if an external encoder is connected */
  38. bool simulate_vesa_sync;
  39. int sync_lost_count;
  40. bool frame_intact;
  41. };
  42. #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
  43. static void unref_worker(struct drm_flip_work *work, void *val)
  44. {
  45. struct tilcdc_crtc *tilcdc_crtc =
  46. container_of(work, struct tilcdc_crtc, unref_work);
  47. struct drm_device *dev = tilcdc_crtc->base.dev;
  48. mutex_lock(&dev->mode_config.mutex);
  49. drm_framebuffer_unreference(val);
  50. mutex_unlock(&dev->mode_config.mutex);
  51. }
  52. static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
  53. {
  54. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  55. struct drm_device *dev = crtc->dev;
  56. struct drm_gem_cma_object *gem;
  57. unsigned int depth, bpp;
  58. dma_addr_t start, end;
  59. u64 dma_base_and_ceiling;
  60. drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
  61. gem = drm_fb_cma_get_gem_obj(fb, 0);
  62. start = gem->paddr + fb->offsets[0] +
  63. crtc->y * fb->pitches[0] +
  64. crtc->x * bpp / 8;
  65. end = start + (crtc->mode.vdisplay * fb->pitches[0]);
  66. /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
  67. * with a single insruction, if available. This should make it more
  68. * unlikely that LCDC would fetch the DMA addresses in the middle of
  69. * an update.
  70. */
  71. dma_base_and_ceiling = (u64)(end - 1) << 32 | start;
  72. tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
  73. if (tilcdc_crtc->curr_fb)
  74. drm_flip_work_queue(&tilcdc_crtc->unref_work,
  75. tilcdc_crtc->curr_fb);
  76. tilcdc_crtc->curr_fb = fb;
  77. }
  78. static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
  79. {
  80. struct tilcdc_drm_private *priv = dev->dev_private;
  81. tilcdc_clear_irqstatus(dev, 0xffffffff);
  82. if (priv->rev == 1) {
  83. tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
  84. LCDC_V1_UNDERFLOW_INT_ENA);
  85. tilcdc_set(dev, LCDC_DMA_CTRL_REG,
  86. LCDC_V1_END_OF_FRAME_INT_ENA);
  87. } else {
  88. tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
  89. LCDC_V2_UNDERFLOW_INT_ENA |
  90. LCDC_V2_END_OF_FRAME0_INT_ENA |
  91. LCDC_FRAME_DONE | LCDC_SYNC_LOST);
  92. }
  93. }
  94. static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
  95. {
  96. struct tilcdc_drm_private *priv = dev->dev_private;
  97. /* disable irqs that we might have enabled: */
  98. if (priv->rev == 1) {
  99. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
  100. LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
  101. tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
  102. LCDC_V1_END_OF_FRAME_INT_ENA);
  103. } else {
  104. tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
  105. LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
  106. LCDC_V2_END_OF_FRAME0_INT_ENA |
  107. LCDC_FRAME_DONE | LCDC_SYNC_LOST);
  108. }
  109. }
  110. static void reset(struct drm_crtc *crtc)
  111. {
  112. struct drm_device *dev = crtc->dev;
  113. struct tilcdc_drm_private *priv = dev->dev_private;
  114. if (priv->rev != 2)
  115. return;
  116. tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
  117. usleep_range(250, 1000);
  118. tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
  119. }
  120. static void tilcdc_crtc_enable(struct drm_crtc *crtc)
  121. {
  122. struct drm_device *dev = crtc->dev;
  123. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  124. if (tilcdc_crtc->enabled)
  125. return;
  126. pm_runtime_get_sync(dev->dev);
  127. reset(crtc);
  128. tilcdc_crtc_enable_irqs(dev);
  129. tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
  130. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_PALETTE_LOAD_MODE(DATA_ONLY));
  131. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  132. drm_crtc_vblank_on(crtc);
  133. tilcdc_crtc->enabled = true;
  134. }
  135. void tilcdc_crtc_disable(struct drm_crtc *crtc)
  136. {
  137. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  138. struct drm_device *dev = crtc->dev;
  139. struct tilcdc_drm_private *priv = dev->dev_private;
  140. if (!tilcdc_crtc->enabled)
  141. return;
  142. tilcdc_crtc->frame_done = false;
  143. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  144. /*
  145. * if necessary wait for framedone irq which will still come
  146. * before putting things to sleep..
  147. */
  148. if (priv->rev == 2) {
  149. int ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
  150. tilcdc_crtc->frame_done,
  151. msecs_to_jiffies(500));
  152. if (ret == 0)
  153. dev_err(dev->dev, "%s: timeout waiting for framedone\n",
  154. __func__);
  155. }
  156. drm_crtc_vblank_off(crtc);
  157. tilcdc_crtc_disable_irqs(dev);
  158. pm_runtime_put_sync(dev->dev);
  159. if (tilcdc_crtc->next_fb) {
  160. drm_flip_work_queue(&tilcdc_crtc->unref_work,
  161. tilcdc_crtc->next_fb);
  162. tilcdc_crtc->next_fb = NULL;
  163. }
  164. if (tilcdc_crtc->curr_fb) {
  165. drm_flip_work_queue(&tilcdc_crtc->unref_work,
  166. tilcdc_crtc->curr_fb);
  167. tilcdc_crtc->curr_fb = NULL;
  168. }
  169. drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
  170. tilcdc_crtc->last_vblank = ktime_set(0, 0);
  171. tilcdc_crtc->enabled = false;
  172. }
  173. static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
  174. {
  175. return crtc->state && crtc->state->enable && crtc->state->active;
  176. }
  177. static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
  178. {
  179. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  180. tilcdc_crtc_disable(crtc);
  181. of_node_put(crtc->port);
  182. drm_crtc_cleanup(crtc);
  183. drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
  184. }
  185. int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
  186. struct drm_framebuffer *fb,
  187. struct drm_pending_vblank_event *event)
  188. {
  189. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  190. struct drm_device *dev = crtc->dev;
  191. unsigned long flags;
  192. if (tilcdc_crtc->event) {
  193. dev_err(dev->dev, "already pending page flip!\n");
  194. return -EBUSY;
  195. }
  196. drm_framebuffer_reference(fb);
  197. crtc->primary->fb = fb;
  198. spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
  199. if (crtc->hwmode.vrefresh && ktime_to_ns(tilcdc_crtc->last_vblank)) {
  200. ktime_t next_vblank;
  201. s64 tdiff;
  202. next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
  203. 1000000 / crtc->hwmode.vrefresh);
  204. tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
  205. if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
  206. tilcdc_crtc->next_fb = fb;
  207. }
  208. if (tilcdc_crtc->next_fb != fb)
  209. set_scanout(crtc, fb);
  210. tilcdc_crtc->event = event;
  211. spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
  212. return 0;
  213. }
  214. static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
  215. const struct drm_display_mode *mode,
  216. struct drm_display_mode *adjusted_mode)
  217. {
  218. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  219. if (!tilcdc_crtc->simulate_vesa_sync)
  220. return true;
  221. /*
  222. * tilcdc does not generate VESA-compliant sync but aligns
  223. * VS on the second edge of HS instead of first edge.
  224. * We use adjusted_mode, to fixup sync by aligning both rising
  225. * edges and add HSKEW offset to fix the sync.
  226. */
  227. adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
  228. adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
  229. if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
  230. adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
  231. adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
  232. } else {
  233. adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
  234. adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
  235. }
  236. return true;
  237. }
  238. static void tilcdc_crtc_mode_set_nofb(struct drm_crtc *crtc)
  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. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  246. struct drm_framebuffer *fb = crtc->primary->state->fb;
  247. if (WARN_ON(!info))
  248. return;
  249. if (WARN_ON(!fb))
  250. return;
  251. /* Configure the Burst Size and fifo threshold of DMA: */
  252. reg = tilcdc_read(dev, LCDC_DMA_CTRL_REG) & ~0x00000770;
  253. switch (info->dma_burst_sz) {
  254. case 1:
  255. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_1);
  256. break;
  257. case 2:
  258. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_2);
  259. break;
  260. case 4:
  261. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_4);
  262. break;
  263. case 8:
  264. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_8);
  265. break;
  266. case 16:
  267. reg |= LCDC_DMA_BURST_SIZE(LCDC_DMA_BURST_16);
  268. break;
  269. default:
  270. dev_err(dev->dev, "invalid burst size\n");
  271. return;
  272. }
  273. reg |= (info->fifo_th << 8);
  274. tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
  275. /* Configure timings: */
  276. hbp = mode->htotal - mode->hsync_end;
  277. hfp = mode->hsync_start - mode->hdisplay;
  278. hsw = mode->hsync_end - mode->hsync_start;
  279. vbp = mode->vtotal - mode->vsync_end;
  280. vfp = mode->vsync_start - mode->vdisplay;
  281. vsw = mode->vsync_end - mode->vsync_start;
  282. DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
  283. mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
  284. /* Set AC Bias Period and Number of Transitions per Interrupt: */
  285. reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
  286. reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
  287. LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
  288. /*
  289. * subtract one from hfp, hbp, hsw because the hardware uses
  290. * a value of 0 as 1
  291. */
  292. if (priv->rev == 2) {
  293. /* clear bits we're going to set */
  294. reg &= ~0x78000033;
  295. reg |= ((hfp-1) & 0x300) >> 8;
  296. reg |= ((hbp-1) & 0x300) >> 4;
  297. reg |= ((hsw-1) & 0x3c0) << 21;
  298. }
  299. tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
  300. reg = (((mode->hdisplay >> 4) - 1) << 4) |
  301. (((hbp-1) & 0xff) << 24) |
  302. (((hfp-1) & 0xff) << 16) |
  303. (((hsw-1) & 0x3f) << 10);
  304. if (priv->rev == 2)
  305. reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
  306. tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
  307. reg = ((mode->vdisplay - 1) & 0x3ff) |
  308. ((vbp & 0xff) << 24) |
  309. ((vfp & 0xff) << 16) |
  310. (((vsw-1) & 0x3f) << 10);
  311. tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
  312. /*
  313. * be sure to set Bit 10 for the V2 LCDC controller,
  314. * otherwise limited to 1024 pixels width, stopping
  315. * 1920x1080 being supported.
  316. */
  317. if (priv->rev == 2) {
  318. if ((mode->vdisplay - 1) & 0x400) {
  319. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
  320. LCDC_LPP_B10);
  321. } else {
  322. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
  323. LCDC_LPP_B10);
  324. }
  325. }
  326. /* Configure display type: */
  327. reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
  328. ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
  329. LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
  330. 0x000ff000 /* Palette Loading Delay bits */);
  331. reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
  332. if (info->tft_alt_mode)
  333. reg |= LCDC_TFT_ALT_ENABLE;
  334. if (priv->rev == 2) {
  335. unsigned int depth, bpp;
  336. drm_fb_get_bpp_depth(fb->pixel_format, &depth, &bpp);
  337. switch (bpp) {
  338. case 16:
  339. break;
  340. case 32:
  341. reg |= LCDC_V2_TFT_24BPP_UNPACK;
  342. /* fallthrough */
  343. case 24:
  344. reg |= LCDC_V2_TFT_24BPP_MODE;
  345. break;
  346. default:
  347. dev_err(dev->dev, "invalid pixel format\n");
  348. return;
  349. }
  350. }
  351. reg |= info->fdd < 12;
  352. tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
  353. if (info->invert_pxl_clk)
  354. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  355. else
  356. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  357. if (info->sync_ctrl)
  358. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  359. else
  360. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  361. if (info->sync_edge)
  362. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  363. else
  364. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  365. if (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(fb);
  378. set_scanout(crtc, fb);
  379. tilcdc_crtc_update_clk(crtc);
  380. crtc->hwmode = crtc->state->adjusted_mode;
  381. }
  382. static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
  383. struct drm_crtc_state *state)
  384. {
  385. struct drm_display_mode *mode = &state->mode;
  386. int ret;
  387. /* If we are not active we don't care */
  388. if (!state->active)
  389. return 0;
  390. if (state->state->planes[0].ptr != crtc->primary ||
  391. state->state->planes[0].state == NULL ||
  392. state->state->planes[0].state->crtc != crtc) {
  393. dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
  394. return -EINVAL;
  395. }
  396. ret = tilcdc_crtc_mode_valid(crtc, mode);
  397. if (ret) {
  398. dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
  399. return -EINVAL;
  400. }
  401. return 0;
  402. }
  403. static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
  404. .destroy = tilcdc_crtc_destroy,
  405. .set_config = drm_atomic_helper_set_config,
  406. .page_flip = drm_atomic_helper_page_flip,
  407. .reset = drm_atomic_helper_crtc_reset,
  408. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  409. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  410. };
  411. static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
  412. .mode_fixup = tilcdc_crtc_mode_fixup,
  413. .enable = tilcdc_crtc_enable,
  414. .disable = tilcdc_crtc_disable,
  415. .atomic_check = tilcdc_crtc_atomic_check,
  416. .mode_set_nofb = tilcdc_crtc_mode_set_nofb,
  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 drm_device *dev = crtc->dev;
  516. struct tilcdc_drm_private *priv = dev->dev_private;
  517. unsigned long lcd_clk;
  518. const unsigned clkdiv = 2; /* using a fixed divider of 2 */
  519. int ret;
  520. pm_runtime_get_sync(dev->dev);
  521. tilcdc_crtc_disable(crtc);
  522. /* mode.clock is in KHz, set_rate wants parameter in Hz */
  523. ret = clk_set_rate(priv->clk, crtc->mode.clock * 1000 * clkdiv);
  524. if (ret < 0) {
  525. dev_err(dev->dev, "failed to set display clock rate to: %d\n",
  526. crtc->mode.clock);
  527. goto out;
  528. }
  529. lcd_clk = clk_get_rate(priv->clk);
  530. DBG("lcd_clk=%lu, mode clock=%d, div=%u",
  531. lcd_clk, crtc->mode.clock, clkdiv);
  532. /* Configure the LCD clock divisor. */
  533. tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
  534. LCDC_RASTER_MODE);
  535. if (priv->rev == 2)
  536. tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
  537. LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
  538. LCDC_V2_CORE_CLK_EN);
  539. if (tilcdc_crtc_is_on(crtc))
  540. tilcdc_crtc_enable(crtc);
  541. out:
  542. pm_runtime_put_sync(dev->dev);
  543. }
  544. #define SYNC_LOST_COUNT_LIMIT 50
  545. irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
  546. {
  547. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  548. struct drm_device *dev = crtc->dev;
  549. struct tilcdc_drm_private *priv = dev->dev_private;
  550. uint32_t stat;
  551. stat = tilcdc_read_irqstatus(dev);
  552. tilcdc_clear_irqstatus(dev, stat);
  553. if (stat & LCDC_END_OF_FRAME0) {
  554. unsigned long flags;
  555. bool skip_event = false;
  556. ktime_t now;
  557. now = ktime_get();
  558. drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
  559. spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
  560. tilcdc_crtc->last_vblank = now;
  561. if (tilcdc_crtc->next_fb) {
  562. set_scanout(crtc, tilcdc_crtc->next_fb);
  563. tilcdc_crtc->next_fb = NULL;
  564. skip_event = true;
  565. }
  566. spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
  567. drm_crtc_handle_vblank(crtc);
  568. if (!skip_event) {
  569. struct drm_pending_vblank_event *event;
  570. spin_lock_irqsave(&dev->event_lock, flags);
  571. event = tilcdc_crtc->event;
  572. tilcdc_crtc->event = NULL;
  573. if (event)
  574. drm_crtc_send_vblank_event(crtc, event);
  575. spin_unlock_irqrestore(&dev->event_lock, flags);
  576. }
  577. if (tilcdc_crtc->frame_intact)
  578. tilcdc_crtc->sync_lost_count = 0;
  579. else
  580. tilcdc_crtc->frame_intact = true;
  581. }
  582. if (stat & LCDC_FIFO_UNDERFLOW)
  583. dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underfow",
  584. __func__, stat);
  585. /* For revision 2 only */
  586. if (priv->rev == 2) {
  587. if (stat & LCDC_FRAME_DONE) {
  588. tilcdc_crtc->frame_done = true;
  589. wake_up(&tilcdc_crtc->frame_done_wq);
  590. }
  591. if (stat & LCDC_SYNC_LOST) {
  592. dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
  593. __func__, stat);
  594. tilcdc_crtc->frame_intact = false;
  595. if (tilcdc_crtc->sync_lost_count++ >
  596. SYNC_LOST_COUNT_LIMIT) {
  597. dev_err(dev->dev, "%s(0x%08x): Sync lost flood detected, disabling the interrupt", __func__, stat);
  598. tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
  599. LCDC_SYNC_LOST);
  600. }
  601. }
  602. /* Indicate to LCDC that the interrupt service routine has
  603. * completed, see 13.3.6.1.6 in AM335x TRM.
  604. */
  605. tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
  606. }
  607. return IRQ_HANDLED;
  608. }
  609. struct drm_crtc *tilcdc_crtc_create(struct drm_device *dev)
  610. {
  611. struct tilcdc_drm_private *priv = dev->dev_private;
  612. struct tilcdc_crtc *tilcdc_crtc;
  613. struct drm_crtc *crtc;
  614. int ret;
  615. tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
  616. if (!tilcdc_crtc) {
  617. dev_err(dev->dev, "allocation failed\n");
  618. return NULL;
  619. }
  620. crtc = &tilcdc_crtc->base;
  621. ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
  622. if (ret < 0)
  623. goto fail;
  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_with_planes(dev, crtc,
  629. &tilcdc_crtc->primary,
  630. NULL,
  631. &tilcdc_crtc_funcs,
  632. "tilcdc crtc");
  633. if (ret < 0)
  634. goto fail;
  635. drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
  636. if (priv->is_componentized) {
  637. struct device_node *ports =
  638. of_get_child_by_name(dev->dev->of_node, "ports");
  639. if (ports) {
  640. crtc->port = of_get_child_by_name(ports, "port");
  641. of_node_put(ports);
  642. } else {
  643. crtc->port =
  644. of_get_child_by_name(dev->dev->of_node, "port");
  645. }
  646. if (!crtc->port) { /* This should never happen */
  647. dev_err(dev->dev, "Port node not found in %s\n",
  648. dev->dev->of_node->full_name);
  649. goto fail;
  650. }
  651. }
  652. return crtc;
  653. fail:
  654. tilcdc_crtc_destroy(crtc);
  655. return NULL;
  656. }