tilcdc_crtc.c 22 KB

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