tilcdc_crtc.c 28 KB

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