tilcdc_crtc.c 25 KB

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