tilcdc_crtc.c 27 KB

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