tilcdc_crtc.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060
  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 <linux/completion.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/of_graph.h>
  26. #include "tilcdc_drv.h"
  27. #include "tilcdc_regs.h"
  28. #define TILCDC_VBLANK_SAFETY_THRESHOLD_US 1000
  29. #define TILCDC_PALETTE_SIZE 32
  30. #define TILCDC_PALETTE_FIRST_ENTRY 0x4000
  31. struct tilcdc_crtc {
  32. struct drm_crtc base;
  33. struct drm_plane primary;
  34. const struct tilcdc_panel_info *info;
  35. struct drm_pending_vblank_event *event;
  36. struct mutex enable_lock;
  37. bool enabled;
  38. bool shutdown;
  39. wait_queue_head_t frame_done_wq;
  40. bool frame_done;
  41. spinlock_t irq_lock;
  42. unsigned int lcd_fck_rate;
  43. ktime_t last_vblank;
  44. struct drm_framebuffer *curr_fb;
  45. struct drm_framebuffer *next_fb;
  46. /* for deferred fb unref's: */
  47. struct drm_flip_work unref_work;
  48. /* Only set if an external encoder is connected */
  49. bool simulate_vesa_sync;
  50. int sync_lost_count;
  51. bool frame_intact;
  52. struct work_struct recover_work;
  53. dma_addr_t palette_dma_handle;
  54. u16 *palette_base;
  55. struct completion palette_loaded;
  56. };
  57. #define to_tilcdc_crtc(x) container_of(x, struct tilcdc_crtc, base)
  58. static void unref_worker(struct drm_flip_work *work, void *val)
  59. {
  60. struct tilcdc_crtc *tilcdc_crtc =
  61. container_of(work, struct tilcdc_crtc, unref_work);
  62. struct drm_device *dev = tilcdc_crtc->base.dev;
  63. mutex_lock(&dev->mode_config.mutex);
  64. drm_framebuffer_unreference(val);
  65. mutex_unlock(&dev->mode_config.mutex);
  66. }
  67. static void set_scanout(struct drm_crtc *crtc, struct drm_framebuffer *fb)
  68. {
  69. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  70. struct drm_device *dev = crtc->dev;
  71. struct tilcdc_drm_private *priv = dev->dev_private;
  72. struct drm_gem_cma_object *gem;
  73. dma_addr_t start, end;
  74. u64 dma_base_and_ceiling;
  75. gem = drm_fb_cma_get_gem_obj(fb, 0);
  76. start = gem->paddr + fb->offsets[0] +
  77. crtc->y * fb->pitches[0] +
  78. crtc->x * fb->format->cpp[0];
  79. end = start + (crtc->mode.vdisplay * fb->pitches[0]);
  80. /* Write LCDC_DMA_FB_BASE_ADDR_0_REG and LCDC_DMA_FB_CEILING_ADDR_0_REG
  81. * with a single insruction, if available. This should make it more
  82. * unlikely that LCDC would fetch the DMA addresses in the middle of
  83. * an update.
  84. */
  85. if (priv->rev == 1)
  86. end -= 1;
  87. dma_base_and_ceiling = (u64)end << 32 | start;
  88. tilcdc_write64(dev, LCDC_DMA_FB_BASE_ADDR_0_REG, dma_base_and_ceiling);
  89. if (tilcdc_crtc->curr_fb)
  90. drm_flip_work_queue(&tilcdc_crtc->unref_work,
  91. tilcdc_crtc->curr_fb);
  92. tilcdc_crtc->curr_fb = fb;
  93. }
  94. /*
  95. * The driver currently only supports only true color formats. For
  96. * true color the palette block is bypassed, but a 32 byte palette
  97. * should still be loaded. The first 16-bit entry must be 0x4000 while
  98. * all other entries must be zeroed.
  99. */
  100. static void tilcdc_crtc_load_palette(struct drm_crtc *crtc)
  101. {
  102. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  103. struct drm_device *dev = crtc->dev;
  104. struct tilcdc_drm_private *priv = dev->dev_private;
  105. int ret;
  106. reinit_completion(&tilcdc_crtc->palette_loaded);
  107. /* Tell the LCDC where the palette is located. */
  108. tilcdc_write(dev, LCDC_DMA_FB_BASE_ADDR_0_REG,
  109. tilcdc_crtc->palette_dma_handle);
  110. tilcdc_write(dev, LCDC_DMA_FB_CEILING_ADDR_0_REG,
  111. (u32) tilcdc_crtc->palette_dma_handle +
  112. TILCDC_PALETTE_SIZE - 1);
  113. /* Set dma load mode for palette loading only. */
  114. tilcdc_write_mask(dev, LCDC_RASTER_CTRL_REG,
  115. LCDC_PALETTE_LOAD_MODE(PALETTE_ONLY),
  116. LCDC_PALETTE_LOAD_MODE_MASK);
  117. /* Enable DMA Palette Loaded Interrupt */
  118. if (priv->rev == 1)
  119. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_PL_INT_ENA);
  120. else
  121. tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG, LCDC_V2_PL_INT_ENA);
  122. /* Enable LCDC DMA and wait for palette to be loaded. */
  123. tilcdc_clear_irqstatus(dev, 0xffffffff);
  124. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  125. ret = wait_for_completion_timeout(&tilcdc_crtc->palette_loaded,
  126. msecs_to_jiffies(50));
  127. if (ret == 0)
  128. dev_err(dev->dev, "%s: Palette loading timeout", __func__);
  129. /* Disable LCDC DMA and DMA Palette Loaded Interrupt. */
  130. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  131. if (priv->rev == 1)
  132. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_V1_PL_INT_ENA);
  133. else
  134. tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG, LCDC_V2_PL_INT_ENA);
  135. }
  136. static void tilcdc_crtc_enable_irqs(struct drm_device *dev)
  137. {
  138. struct tilcdc_drm_private *priv = dev->dev_private;
  139. tilcdc_clear_irqstatus(dev, 0xffffffff);
  140. if (priv->rev == 1) {
  141. tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
  142. LCDC_V1_SYNC_LOST_INT_ENA | LCDC_V1_FRAME_DONE_INT_ENA |
  143. LCDC_V1_UNDERFLOW_INT_ENA);
  144. tilcdc_set(dev, LCDC_DMA_CTRL_REG,
  145. LCDC_V1_END_OF_FRAME_INT_ENA);
  146. } else {
  147. tilcdc_write(dev, LCDC_INT_ENABLE_SET_REG,
  148. LCDC_V2_UNDERFLOW_INT_ENA |
  149. LCDC_V2_END_OF_FRAME0_INT_ENA |
  150. LCDC_FRAME_DONE | LCDC_SYNC_LOST);
  151. }
  152. }
  153. static void tilcdc_crtc_disable_irqs(struct drm_device *dev)
  154. {
  155. struct tilcdc_drm_private *priv = dev->dev_private;
  156. /* disable irqs that we might have enabled: */
  157. if (priv->rev == 1) {
  158. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
  159. LCDC_V1_SYNC_LOST_INT_ENA | LCDC_V1_FRAME_DONE_INT_ENA |
  160. LCDC_V1_UNDERFLOW_INT_ENA | LCDC_V1_PL_INT_ENA);
  161. tilcdc_clear(dev, LCDC_DMA_CTRL_REG,
  162. LCDC_V1_END_OF_FRAME_INT_ENA);
  163. } else {
  164. tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
  165. LCDC_V2_UNDERFLOW_INT_ENA | LCDC_V2_PL_INT_ENA |
  166. LCDC_V2_END_OF_FRAME0_INT_ENA |
  167. LCDC_FRAME_DONE | LCDC_SYNC_LOST);
  168. }
  169. }
  170. static void reset(struct drm_crtc *crtc)
  171. {
  172. struct drm_device *dev = crtc->dev;
  173. struct tilcdc_drm_private *priv = dev->dev_private;
  174. if (priv->rev != 2)
  175. return;
  176. tilcdc_set(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
  177. usleep_range(250, 1000);
  178. tilcdc_clear(dev, LCDC_CLK_RESET_REG, LCDC_CLK_MAIN_RESET);
  179. }
  180. /*
  181. * Calculate the percentage difference between the requested pixel clock rate
  182. * and the effective rate resulting from calculating the clock divider value.
  183. */
  184. static unsigned int tilcdc_pclk_diff(unsigned long rate,
  185. unsigned long real_rate)
  186. {
  187. int r = rate / 100, rr = real_rate / 100;
  188. return (unsigned int)(abs(((rr - r) * 100) / r));
  189. }
  190. static void tilcdc_crtc_set_clk(struct drm_crtc *crtc)
  191. {
  192. struct drm_device *dev = crtc->dev;
  193. struct tilcdc_drm_private *priv = dev->dev_private;
  194. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  195. unsigned long clk_rate, real_rate, req_rate;
  196. unsigned int clkdiv;
  197. int ret;
  198. clkdiv = 2; /* first try using a standard divider of 2 */
  199. /* mode.clock is in KHz, set_rate wants parameter in Hz */
  200. req_rate = crtc->mode.clock * 1000;
  201. ret = clk_set_rate(priv->clk, req_rate * clkdiv);
  202. clk_rate = clk_get_rate(priv->clk);
  203. if (ret < 0) {
  204. /*
  205. * If we fail to set the clock rate (some architectures don't
  206. * use the common clock framework yet and may not implement
  207. * all the clk API calls for every clock), try the next best
  208. * thing: adjusting the clock divider, unless clk_get_rate()
  209. * failed as well.
  210. */
  211. if (!clk_rate) {
  212. /* Nothing more we can do. Just bail out. */
  213. dev_err(dev->dev,
  214. "failed to set the pixel clock - unable to read current lcdc clock rate\n");
  215. return;
  216. }
  217. clkdiv = DIV_ROUND_CLOSEST(clk_rate, req_rate);
  218. /*
  219. * Emit a warning if the real clock rate resulting from the
  220. * calculated divider differs much from the requested rate.
  221. *
  222. * 5% is an arbitrary value - LCDs are usually quite tolerant
  223. * about pixel clock rates.
  224. */
  225. real_rate = clkdiv * req_rate;
  226. if (tilcdc_pclk_diff(clk_rate, real_rate) > 5) {
  227. dev_warn(dev->dev,
  228. "effective pixel clock rate (%luHz) differs from the calculated rate (%luHz)\n",
  229. clk_rate, real_rate);
  230. }
  231. }
  232. tilcdc_crtc->lcd_fck_rate = clk_rate;
  233. DBG("lcd_clk=%u, mode clock=%d, div=%u",
  234. tilcdc_crtc->lcd_fck_rate, crtc->mode.clock, clkdiv);
  235. /* Configure the LCD clock divisor. */
  236. tilcdc_write(dev, LCDC_CTRL_REG, LCDC_CLK_DIVISOR(clkdiv) |
  237. LCDC_RASTER_MODE);
  238. if (priv->rev == 2)
  239. tilcdc_set(dev, LCDC_CLK_ENABLE_REG,
  240. LCDC_V2_DMA_CLK_EN | LCDC_V2_LIDD_CLK_EN |
  241. LCDC_V2_CORE_CLK_EN);
  242. }
  243. static void tilcdc_crtc_set_mode(struct drm_crtc *crtc)
  244. {
  245. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  246. struct drm_device *dev = crtc->dev;
  247. struct tilcdc_drm_private *priv = dev->dev_private;
  248. const struct tilcdc_panel_info *info = tilcdc_crtc->info;
  249. uint32_t reg, hbp, hfp, hsw, vbp, vfp, vsw;
  250. struct drm_display_mode *mode = &crtc->state->adjusted_mode;
  251. struct drm_framebuffer *fb = crtc->primary->state->fb;
  252. if (WARN_ON(!info))
  253. return;
  254. if (WARN_ON(!fb))
  255. return;
  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. dev_err(dev->dev, "invalid burst size\n");
  276. return;
  277. }
  278. reg |= (info->fifo_th << 8);
  279. tilcdc_write(dev, LCDC_DMA_CTRL_REG, reg);
  280. /* Configure timings: */
  281. hbp = mode->htotal - mode->hsync_end;
  282. hfp = mode->hsync_start - mode->hdisplay;
  283. hsw = mode->hsync_end - mode->hsync_start;
  284. vbp = mode->vtotal - mode->vsync_end;
  285. vfp = mode->vsync_start - mode->vdisplay;
  286. vsw = mode->vsync_end - mode->vsync_start;
  287. DBG("%dx%d, hbp=%u, hfp=%u, hsw=%u, vbp=%u, vfp=%u, vsw=%u",
  288. mode->hdisplay, mode->vdisplay, hbp, hfp, hsw, vbp, vfp, vsw);
  289. /* Set AC Bias Period and Number of Transitions per Interrupt: */
  290. reg = tilcdc_read(dev, LCDC_RASTER_TIMING_2_REG) & ~0x000fff00;
  291. reg |= LCDC_AC_BIAS_FREQUENCY(info->ac_bias) |
  292. LCDC_AC_BIAS_TRANSITIONS_PER_INT(info->ac_bias_intrpt);
  293. /*
  294. * subtract one from hfp, hbp, hsw because the hardware uses
  295. * a value of 0 as 1
  296. */
  297. if (priv->rev == 2) {
  298. /* clear bits we're going to set */
  299. reg &= ~0x78000033;
  300. reg |= ((hfp-1) & 0x300) >> 8;
  301. reg |= ((hbp-1) & 0x300) >> 4;
  302. reg |= ((hsw-1) & 0x3c0) << 21;
  303. }
  304. tilcdc_write(dev, LCDC_RASTER_TIMING_2_REG, reg);
  305. reg = (((mode->hdisplay >> 4) - 1) << 4) |
  306. (((hbp-1) & 0xff) << 24) |
  307. (((hfp-1) & 0xff) << 16) |
  308. (((hsw-1) & 0x3f) << 10);
  309. if (priv->rev == 2)
  310. reg |= (((mode->hdisplay >> 4) - 1) & 0x40) >> 3;
  311. tilcdc_write(dev, LCDC_RASTER_TIMING_0_REG, reg);
  312. reg = ((mode->vdisplay - 1) & 0x3ff) |
  313. ((vbp & 0xff) << 24) |
  314. ((vfp & 0xff) << 16) |
  315. (((vsw-1) & 0x3f) << 10);
  316. tilcdc_write(dev, LCDC_RASTER_TIMING_1_REG, reg);
  317. /*
  318. * be sure to set Bit 10 for the V2 LCDC controller,
  319. * otherwise limited to 1024 pixels width, stopping
  320. * 1920x1080 being supported.
  321. */
  322. if (priv->rev == 2) {
  323. if ((mode->vdisplay - 1) & 0x400) {
  324. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG,
  325. LCDC_LPP_B10);
  326. } else {
  327. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG,
  328. LCDC_LPP_B10);
  329. }
  330. }
  331. /* Configure display type: */
  332. reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG) &
  333. ~(LCDC_TFT_MODE | LCDC_MONO_8BIT_MODE | LCDC_MONOCHROME_MODE |
  334. LCDC_V2_TFT_24BPP_MODE | LCDC_V2_TFT_24BPP_UNPACK |
  335. 0x000ff000 /* Palette Loading Delay bits */);
  336. reg |= LCDC_TFT_MODE; /* no monochrome/passive support */
  337. if (info->tft_alt_mode)
  338. reg |= LCDC_TFT_ALT_ENABLE;
  339. if (priv->rev == 2) {
  340. switch (fb->format->format) {
  341. case DRM_FORMAT_BGR565:
  342. case DRM_FORMAT_RGB565:
  343. break;
  344. case DRM_FORMAT_XBGR8888:
  345. case DRM_FORMAT_XRGB8888:
  346. reg |= LCDC_V2_TFT_24BPP_UNPACK;
  347. /* fallthrough */
  348. case DRM_FORMAT_BGR888:
  349. case DRM_FORMAT_RGB888:
  350. reg |= LCDC_V2_TFT_24BPP_MODE;
  351. break;
  352. default:
  353. dev_err(dev->dev, "invalid pixel format\n");
  354. return;
  355. }
  356. }
  357. reg |= info->fdd < 12;
  358. tilcdc_write(dev, LCDC_RASTER_CTRL_REG, reg);
  359. if (info->invert_pxl_clk)
  360. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  361. else
  362. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_PIXEL_CLOCK);
  363. if (info->sync_ctrl)
  364. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  365. else
  366. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_CTRL);
  367. if (info->sync_edge)
  368. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  369. else
  370. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_SYNC_EDGE);
  371. if (mode->flags & DRM_MODE_FLAG_NHSYNC)
  372. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
  373. else
  374. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_HSYNC);
  375. if (mode->flags & DRM_MODE_FLAG_NVSYNC)
  376. tilcdc_set(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
  377. else
  378. tilcdc_clear(dev, LCDC_RASTER_TIMING_2_REG, LCDC_INVERT_VSYNC);
  379. if (info->raster_order)
  380. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
  381. else
  382. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ORDER);
  383. tilcdc_crtc_set_clk(crtc);
  384. tilcdc_crtc_load_palette(crtc);
  385. set_scanout(crtc, fb);
  386. drm_framebuffer_reference(fb);
  387. crtc->hwmode = crtc->state->adjusted_mode;
  388. }
  389. static void tilcdc_crtc_enable(struct drm_crtc *crtc)
  390. {
  391. struct drm_device *dev = crtc->dev;
  392. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  393. unsigned long flags;
  394. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  395. mutex_lock(&tilcdc_crtc->enable_lock);
  396. if (tilcdc_crtc->enabled || tilcdc_crtc->shutdown) {
  397. mutex_unlock(&tilcdc_crtc->enable_lock);
  398. return;
  399. }
  400. pm_runtime_get_sync(dev->dev);
  401. reset(crtc);
  402. tilcdc_crtc_set_mode(crtc);
  403. tilcdc_crtc_enable_irqs(dev);
  404. tilcdc_clear(dev, LCDC_DMA_CTRL_REG, LCDC_DUAL_FRAME_BUFFER_ENABLE);
  405. tilcdc_write_mask(dev, LCDC_RASTER_CTRL_REG,
  406. LCDC_PALETTE_LOAD_MODE(DATA_ONLY),
  407. LCDC_PALETTE_LOAD_MODE_MASK);
  408. /* There is no real chance for a race here as the time stamp
  409. * is taken before the raster DMA is started. The spin-lock is
  410. * taken to have a memory barrier after taking the time-stamp
  411. * and to avoid a context switch between taking the stamp and
  412. * enabling the raster.
  413. */
  414. spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
  415. tilcdc_crtc->last_vblank = ktime_get();
  416. tilcdc_set(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  417. spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
  418. drm_crtc_vblank_on(crtc);
  419. tilcdc_crtc->enabled = true;
  420. mutex_unlock(&tilcdc_crtc->enable_lock);
  421. }
  422. static void tilcdc_crtc_atomic_enable(struct drm_crtc *crtc,
  423. struct drm_crtc_state *old_state)
  424. {
  425. tilcdc_crtc_enable(crtc);
  426. }
  427. static void tilcdc_crtc_off(struct drm_crtc *crtc, bool shutdown)
  428. {
  429. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  430. struct drm_device *dev = crtc->dev;
  431. struct tilcdc_drm_private *priv = dev->dev_private;
  432. int ret;
  433. mutex_lock(&tilcdc_crtc->enable_lock);
  434. if (shutdown)
  435. tilcdc_crtc->shutdown = true;
  436. if (!tilcdc_crtc->enabled) {
  437. mutex_unlock(&tilcdc_crtc->enable_lock);
  438. return;
  439. }
  440. tilcdc_crtc->frame_done = false;
  441. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG, LCDC_RASTER_ENABLE);
  442. /*
  443. * Wait for framedone irq which will still come before putting
  444. * things to sleep..
  445. */
  446. ret = wait_event_timeout(tilcdc_crtc->frame_done_wq,
  447. tilcdc_crtc->frame_done,
  448. msecs_to_jiffies(500));
  449. if (ret == 0)
  450. dev_err(dev->dev, "%s: timeout waiting for framedone\n",
  451. __func__);
  452. drm_crtc_vblank_off(crtc);
  453. tilcdc_crtc_disable_irqs(dev);
  454. pm_runtime_put_sync(dev->dev);
  455. if (tilcdc_crtc->next_fb) {
  456. drm_flip_work_queue(&tilcdc_crtc->unref_work,
  457. tilcdc_crtc->next_fb);
  458. tilcdc_crtc->next_fb = NULL;
  459. }
  460. if (tilcdc_crtc->curr_fb) {
  461. drm_flip_work_queue(&tilcdc_crtc->unref_work,
  462. tilcdc_crtc->curr_fb);
  463. tilcdc_crtc->curr_fb = NULL;
  464. }
  465. drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
  466. tilcdc_crtc->enabled = false;
  467. mutex_unlock(&tilcdc_crtc->enable_lock);
  468. }
  469. static void tilcdc_crtc_disable(struct drm_crtc *crtc)
  470. {
  471. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  472. tilcdc_crtc_off(crtc, false);
  473. }
  474. void tilcdc_crtc_shutdown(struct drm_crtc *crtc)
  475. {
  476. tilcdc_crtc_off(crtc, true);
  477. }
  478. static bool tilcdc_crtc_is_on(struct drm_crtc *crtc)
  479. {
  480. return crtc->state && crtc->state->enable && crtc->state->active;
  481. }
  482. static void tilcdc_crtc_recover_work(struct work_struct *work)
  483. {
  484. struct tilcdc_crtc *tilcdc_crtc =
  485. container_of(work, struct tilcdc_crtc, recover_work);
  486. struct drm_crtc *crtc = &tilcdc_crtc->base;
  487. dev_info(crtc->dev->dev, "%s: Reset CRTC", __func__);
  488. drm_modeset_lock(&crtc->mutex, NULL);
  489. if (!tilcdc_crtc_is_on(crtc))
  490. goto out;
  491. tilcdc_crtc_disable(crtc);
  492. tilcdc_crtc_enable(crtc);
  493. out:
  494. drm_modeset_unlock(&crtc->mutex);
  495. }
  496. static void tilcdc_crtc_destroy(struct drm_crtc *crtc)
  497. {
  498. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  499. struct tilcdc_drm_private *priv = crtc->dev->dev_private;
  500. drm_modeset_lock(&crtc->mutex, NULL);
  501. tilcdc_crtc_disable(crtc);
  502. drm_modeset_unlock(&crtc->mutex);
  503. flush_workqueue(priv->wq);
  504. of_node_put(crtc->port);
  505. drm_crtc_cleanup(crtc);
  506. drm_flip_work_cleanup(&tilcdc_crtc->unref_work);
  507. }
  508. int tilcdc_crtc_update_fb(struct drm_crtc *crtc,
  509. struct drm_framebuffer *fb,
  510. struct drm_pending_vblank_event *event)
  511. {
  512. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  513. struct drm_device *dev = crtc->dev;
  514. WARN_ON(!drm_modeset_is_locked(&crtc->mutex));
  515. if (tilcdc_crtc->event) {
  516. dev_err(dev->dev, "already pending page flip!\n");
  517. return -EBUSY;
  518. }
  519. drm_framebuffer_reference(fb);
  520. crtc->primary->fb = fb;
  521. tilcdc_crtc->event = event;
  522. mutex_lock(&tilcdc_crtc->enable_lock);
  523. if (tilcdc_crtc->enabled) {
  524. unsigned long flags;
  525. ktime_t next_vblank;
  526. s64 tdiff;
  527. spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
  528. next_vblank = ktime_add_us(tilcdc_crtc->last_vblank,
  529. 1000000 / crtc->hwmode.vrefresh);
  530. tdiff = ktime_to_us(ktime_sub(next_vblank, ktime_get()));
  531. if (tdiff < TILCDC_VBLANK_SAFETY_THRESHOLD_US)
  532. tilcdc_crtc->next_fb = fb;
  533. else
  534. set_scanout(crtc, fb);
  535. spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
  536. }
  537. mutex_unlock(&tilcdc_crtc->enable_lock);
  538. return 0;
  539. }
  540. static bool tilcdc_crtc_mode_fixup(struct drm_crtc *crtc,
  541. const struct drm_display_mode *mode,
  542. struct drm_display_mode *adjusted_mode)
  543. {
  544. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  545. if (!tilcdc_crtc->simulate_vesa_sync)
  546. return true;
  547. /*
  548. * tilcdc does not generate VESA-compliant sync but aligns
  549. * VS on the second edge of HS instead of first edge.
  550. * We use adjusted_mode, to fixup sync by aligning both rising
  551. * edges and add HSKEW offset to fix the sync.
  552. */
  553. adjusted_mode->hskew = mode->hsync_end - mode->hsync_start;
  554. adjusted_mode->flags |= DRM_MODE_FLAG_HSKEW;
  555. if (mode->flags & DRM_MODE_FLAG_NHSYNC) {
  556. adjusted_mode->flags |= DRM_MODE_FLAG_PHSYNC;
  557. adjusted_mode->flags &= ~DRM_MODE_FLAG_NHSYNC;
  558. } else {
  559. adjusted_mode->flags |= DRM_MODE_FLAG_NHSYNC;
  560. adjusted_mode->flags &= ~DRM_MODE_FLAG_PHSYNC;
  561. }
  562. return true;
  563. }
  564. static int tilcdc_crtc_atomic_check(struct drm_crtc *crtc,
  565. struct drm_crtc_state *state)
  566. {
  567. struct drm_display_mode *mode = &state->mode;
  568. int ret;
  569. /* If we are not active we don't care */
  570. if (!state->active)
  571. return 0;
  572. if (state->state->planes[0].ptr != crtc->primary ||
  573. state->state->planes[0].state == NULL ||
  574. state->state->planes[0].state->crtc != crtc) {
  575. dev_dbg(crtc->dev->dev, "CRTC primary plane must be present");
  576. return -EINVAL;
  577. }
  578. ret = tilcdc_crtc_mode_valid(crtc, mode);
  579. if (ret) {
  580. dev_dbg(crtc->dev->dev, "Mode \"%s\" not valid", mode->name);
  581. return -EINVAL;
  582. }
  583. return 0;
  584. }
  585. static int tilcdc_crtc_enable_vblank(struct drm_crtc *crtc)
  586. {
  587. return 0;
  588. }
  589. static void tilcdc_crtc_disable_vblank(struct drm_crtc *crtc)
  590. {
  591. }
  592. static const struct drm_crtc_funcs tilcdc_crtc_funcs = {
  593. .destroy = tilcdc_crtc_destroy,
  594. .set_config = drm_atomic_helper_set_config,
  595. .page_flip = drm_atomic_helper_page_flip,
  596. .reset = drm_atomic_helper_crtc_reset,
  597. .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state,
  598. .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state,
  599. .enable_vblank = tilcdc_crtc_enable_vblank,
  600. .disable_vblank = tilcdc_crtc_disable_vblank,
  601. };
  602. static const struct drm_crtc_helper_funcs tilcdc_crtc_helper_funcs = {
  603. .mode_fixup = tilcdc_crtc_mode_fixup,
  604. .disable = tilcdc_crtc_disable,
  605. .atomic_check = tilcdc_crtc_atomic_check,
  606. .atomic_enable = tilcdc_crtc_atomic_enable,
  607. };
  608. int tilcdc_crtc_max_width(struct drm_crtc *crtc)
  609. {
  610. struct drm_device *dev = crtc->dev;
  611. struct tilcdc_drm_private *priv = dev->dev_private;
  612. int max_width = 0;
  613. if (priv->rev == 1)
  614. max_width = 1024;
  615. else if (priv->rev == 2)
  616. max_width = 2048;
  617. return max_width;
  618. }
  619. int tilcdc_crtc_mode_valid(struct drm_crtc *crtc, struct drm_display_mode *mode)
  620. {
  621. struct tilcdc_drm_private *priv = crtc->dev->dev_private;
  622. unsigned int bandwidth;
  623. uint32_t hbp, hfp, hsw, vbp, vfp, vsw;
  624. /*
  625. * check to see if the width is within the range that
  626. * the LCD Controller physically supports
  627. */
  628. if (mode->hdisplay > tilcdc_crtc_max_width(crtc))
  629. return MODE_VIRTUAL_X;
  630. /* width must be multiple of 16 */
  631. if (mode->hdisplay & 0xf)
  632. return MODE_VIRTUAL_X;
  633. if (mode->vdisplay > 2048)
  634. return MODE_VIRTUAL_Y;
  635. DBG("Processing mode %dx%d@%d with pixel clock %d",
  636. mode->hdisplay, mode->vdisplay,
  637. drm_mode_vrefresh(mode), mode->clock);
  638. hbp = mode->htotal - mode->hsync_end;
  639. hfp = mode->hsync_start - mode->hdisplay;
  640. hsw = mode->hsync_end - mode->hsync_start;
  641. vbp = mode->vtotal - mode->vsync_end;
  642. vfp = mode->vsync_start - mode->vdisplay;
  643. vsw = mode->vsync_end - mode->vsync_start;
  644. if ((hbp-1) & ~0x3ff) {
  645. DBG("Pruning mode: Horizontal Back Porch out of range");
  646. return MODE_HBLANK_WIDE;
  647. }
  648. if ((hfp-1) & ~0x3ff) {
  649. DBG("Pruning mode: Horizontal Front Porch out of range");
  650. return MODE_HBLANK_WIDE;
  651. }
  652. if ((hsw-1) & ~0x3ff) {
  653. DBG("Pruning mode: Horizontal Sync Width out of range");
  654. return MODE_HSYNC_WIDE;
  655. }
  656. if (vbp & ~0xff) {
  657. DBG("Pruning mode: Vertical Back Porch out of range");
  658. return MODE_VBLANK_WIDE;
  659. }
  660. if (vfp & ~0xff) {
  661. DBG("Pruning mode: Vertical Front Porch out of range");
  662. return MODE_VBLANK_WIDE;
  663. }
  664. if ((vsw-1) & ~0x3f) {
  665. DBG("Pruning mode: Vertical Sync Width out of range");
  666. return MODE_VSYNC_WIDE;
  667. }
  668. /*
  669. * some devices have a maximum allowed pixel clock
  670. * configured from the DT
  671. */
  672. if (mode->clock > priv->max_pixelclock) {
  673. DBG("Pruning mode: pixel clock too high");
  674. return MODE_CLOCK_HIGH;
  675. }
  676. /*
  677. * some devices further limit the max horizontal resolution
  678. * configured from the DT
  679. */
  680. if (mode->hdisplay > priv->max_width)
  681. return MODE_BAD_WIDTH;
  682. /* filter out modes that would require too much memory bandwidth: */
  683. bandwidth = mode->hdisplay * mode->vdisplay *
  684. drm_mode_vrefresh(mode);
  685. if (bandwidth > priv->max_bandwidth) {
  686. DBG("Pruning mode: exceeds defined bandwidth limit");
  687. return MODE_BAD;
  688. }
  689. return MODE_OK;
  690. }
  691. void tilcdc_crtc_set_panel_info(struct drm_crtc *crtc,
  692. const struct tilcdc_panel_info *info)
  693. {
  694. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  695. tilcdc_crtc->info = info;
  696. }
  697. void tilcdc_crtc_set_simulate_vesa_sync(struct drm_crtc *crtc,
  698. bool simulate_vesa_sync)
  699. {
  700. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  701. tilcdc_crtc->simulate_vesa_sync = simulate_vesa_sync;
  702. }
  703. void tilcdc_crtc_update_clk(struct drm_crtc *crtc)
  704. {
  705. struct drm_device *dev = crtc->dev;
  706. struct tilcdc_drm_private *priv = dev->dev_private;
  707. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  708. drm_modeset_lock(&crtc->mutex, NULL);
  709. if (tilcdc_crtc->lcd_fck_rate != clk_get_rate(priv->clk)) {
  710. if (tilcdc_crtc_is_on(crtc)) {
  711. pm_runtime_get_sync(dev->dev);
  712. tilcdc_crtc_disable(crtc);
  713. tilcdc_crtc_set_clk(crtc);
  714. tilcdc_crtc_enable(crtc);
  715. pm_runtime_put_sync(dev->dev);
  716. }
  717. }
  718. drm_modeset_unlock(&crtc->mutex);
  719. }
  720. #define SYNC_LOST_COUNT_LIMIT 50
  721. irqreturn_t tilcdc_crtc_irq(struct drm_crtc *crtc)
  722. {
  723. struct tilcdc_crtc *tilcdc_crtc = to_tilcdc_crtc(crtc);
  724. struct drm_device *dev = crtc->dev;
  725. struct tilcdc_drm_private *priv = dev->dev_private;
  726. uint32_t stat, reg;
  727. stat = tilcdc_read_irqstatus(dev);
  728. tilcdc_clear_irqstatus(dev, stat);
  729. if (stat & LCDC_END_OF_FRAME0) {
  730. unsigned long flags;
  731. bool skip_event = false;
  732. ktime_t now;
  733. now = ktime_get();
  734. drm_flip_work_commit(&tilcdc_crtc->unref_work, priv->wq);
  735. spin_lock_irqsave(&tilcdc_crtc->irq_lock, flags);
  736. tilcdc_crtc->last_vblank = now;
  737. if (tilcdc_crtc->next_fb) {
  738. set_scanout(crtc, tilcdc_crtc->next_fb);
  739. tilcdc_crtc->next_fb = NULL;
  740. skip_event = true;
  741. }
  742. spin_unlock_irqrestore(&tilcdc_crtc->irq_lock, flags);
  743. drm_crtc_handle_vblank(crtc);
  744. if (!skip_event) {
  745. struct drm_pending_vblank_event *event;
  746. spin_lock_irqsave(&dev->event_lock, flags);
  747. event = tilcdc_crtc->event;
  748. tilcdc_crtc->event = NULL;
  749. if (event)
  750. drm_crtc_send_vblank_event(crtc, event);
  751. spin_unlock_irqrestore(&dev->event_lock, flags);
  752. }
  753. if (tilcdc_crtc->frame_intact)
  754. tilcdc_crtc->sync_lost_count = 0;
  755. else
  756. tilcdc_crtc->frame_intact = true;
  757. }
  758. if (stat & LCDC_FIFO_UNDERFLOW)
  759. dev_err_ratelimited(dev->dev, "%s(0x%08x): FIFO underflow",
  760. __func__, stat);
  761. if (stat & LCDC_PL_LOAD_DONE) {
  762. complete(&tilcdc_crtc->palette_loaded);
  763. if (priv->rev == 1)
  764. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
  765. LCDC_V1_PL_INT_ENA);
  766. else
  767. tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
  768. LCDC_V2_PL_INT_ENA);
  769. }
  770. if (stat & LCDC_SYNC_LOST) {
  771. dev_err_ratelimited(dev->dev, "%s(0x%08x): Sync lost",
  772. __func__, stat);
  773. tilcdc_crtc->frame_intact = false;
  774. if (priv->rev == 1) {
  775. reg = tilcdc_read(dev, LCDC_RASTER_CTRL_REG);
  776. if (reg & LCDC_RASTER_ENABLE) {
  777. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
  778. LCDC_RASTER_ENABLE);
  779. tilcdc_set(dev, LCDC_RASTER_CTRL_REG,
  780. LCDC_RASTER_ENABLE);
  781. }
  782. } else {
  783. if (tilcdc_crtc->sync_lost_count++ >
  784. SYNC_LOST_COUNT_LIMIT) {
  785. dev_err(dev->dev,
  786. "%s(0x%08x): Sync lost flood detected, recovering",
  787. __func__, stat);
  788. queue_work(system_wq,
  789. &tilcdc_crtc->recover_work);
  790. tilcdc_write(dev, LCDC_INT_ENABLE_CLR_REG,
  791. LCDC_SYNC_LOST);
  792. tilcdc_crtc->sync_lost_count = 0;
  793. }
  794. }
  795. }
  796. if (stat & LCDC_FRAME_DONE) {
  797. tilcdc_crtc->frame_done = true;
  798. wake_up(&tilcdc_crtc->frame_done_wq);
  799. /* rev 1 lcdc appears to hang if irq is not disbaled here */
  800. if (priv->rev == 1)
  801. tilcdc_clear(dev, LCDC_RASTER_CTRL_REG,
  802. LCDC_V1_FRAME_DONE_INT_ENA);
  803. }
  804. /* For revision 2 only */
  805. if (priv->rev == 2) {
  806. /* Indicate to LCDC that the interrupt service routine has
  807. * completed, see 13.3.6.1.6 in AM335x TRM.
  808. */
  809. tilcdc_write(dev, LCDC_END_OF_INT_IND_REG, 0);
  810. }
  811. return IRQ_HANDLED;
  812. }
  813. int tilcdc_crtc_create(struct drm_device *dev)
  814. {
  815. struct tilcdc_drm_private *priv = dev->dev_private;
  816. struct tilcdc_crtc *tilcdc_crtc;
  817. struct drm_crtc *crtc;
  818. int ret;
  819. tilcdc_crtc = devm_kzalloc(dev->dev, sizeof(*tilcdc_crtc), GFP_KERNEL);
  820. if (!tilcdc_crtc) {
  821. dev_err(dev->dev, "allocation failed\n");
  822. return -ENOMEM;
  823. }
  824. init_completion(&tilcdc_crtc->palette_loaded);
  825. tilcdc_crtc->palette_base = dmam_alloc_coherent(dev->dev,
  826. TILCDC_PALETTE_SIZE,
  827. &tilcdc_crtc->palette_dma_handle,
  828. GFP_KERNEL | __GFP_ZERO);
  829. if (!tilcdc_crtc->palette_base)
  830. return -ENOMEM;
  831. *tilcdc_crtc->palette_base = TILCDC_PALETTE_FIRST_ENTRY;
  832. crtc = &tilcdc_crtc->base;
  833. ret = tilcdc_plane_init(dev, &tilcdc_crtc->primary);
  834. if (ret < 0)
  835. goto fail;
  836. mutex_init(&tilcdc_crtc->enable_lock);
  837. init_waitqueue_head(&tilcdc_crtc->frame_done_wq);
  838. drm_flip_work_init(&tilcdc_crtc->unref_work,
  839. "unref", unref_worker);
  840. spin_lock_init(&tilcdc_crtc->irq_lock);
  841. INIT_WORK(&tilcdc_crtc->recover_work, tilcdc_crtc_recover_work);
  842. ret = drm_crtc_init_with_planes(dev, crtc,
  843. &tilcdc_crtc->primary,
  844. NULL,
  845. &tilcdc_crtc_funcs,
  846. "tilcdc crtc");
  847. if (ret < 0)
  848. goto fail;
  849. drm_crtc_helper_add(crtc, &tilcdc_crtc_helper_funcs);
  850. if (priv->is_componentized) {
  851. crtc->port = of_graph_get_port_by_id(dev->dev->of_node, 0);
  852. if (!crtc->port) { /* This should never happen */
  853. dev_err(dev->dev, "Port node not found in %s\n",
  854. dev->dev->of_node->full_name);
  855. ret = -EINVAL;
  856. goto fail;
  857. }
  858. }
  859. priv->crtc = crtc;
  860. return 0;
  861. fail:
  862. tilcdc_crtc_destroy(crtc);
  863. return ret;
  864. }