tilcdc_crtc.c 28 KB

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