tilcdc_crtc.c 28 KB

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