tilcdc_crtc.c 29 KB

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