armada_crtc.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582
  1. /*
  2. * Copyright (C) 2012 Russell King
  3. * Rewritten from the dovefb driver, and Armada510 manuals.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/component.h>
  11. #include <linux/of_device.h>
  12. #include <linux/platform_device.h>
  13. #include <drm/drmP.h>
  14. #include <drm/drm_crtc_helper.h>
  15. #include <drm/drm_plane_helper.h>
  16. #include <drm/drm_atomic_helper.h>
  17. #include "armada_crtc.h"
  18. #include "armada_drm.h"
  19. #include "armada_fb.h"
  20. #include "armada_gem.h"
  21. #include "armada_hw.h"
  22. #include "armada_trace.h"
  23. enum csc_mode {
  24. CSC_AUTO = 0,
  25. CSC_YUV_CCIR601 = 1,
  26. CSC_YUV_CCIR709 = 2,
  27. CSC_RGB_COMPUTER = 1,
  28. CSC_RGB_STUDIO = 2,
  29. };
  30. static const uint32_t armada_primary_formats[] = {
  31. DRM_FORMAT_UYVY,
  32. DRM_FORMAT_YUYV,
  33. DRM_FORMAT_VYUY,
  34. DRM_FORMAT_YVYU,
  35. DRM_FORMAT_ARGB8888,
  36. DRM_FORMAT_ABGR8888,
  37. DRM_FORMAT_XRGB8888,
  38. DRM_FORMAT_XBGR8888,
  39. DRM_FORMAT_RGB888,
  40. DRM_FORMAT_BGR888,
  41. DRM_FORMAT_ARGB1555,
  42. DRM_FORMAT_ABGR1555,
  43. DRM_FORMAT_RGB565,
  44. DRM_FORMAT_BGR565,
  45. };
  46. /*
  47. * A note about interlacing. Let's consider HDMI 1920x1080i.
  48. * The timing parameters we have from X are:
  49. * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
  50. * 1920 2448 2492 2640 1080 1084 1094 1125
  51. * Which get translated to:
  52. * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
  53. * 1920 2448 2492 2640 540 542 547 562
  54. *
  55. * This is how it is defined by CEA-861-D - line and pixel numbers are
  56. * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
  57. * line: 2640. The odd frame, the first active line is at line 21, and
  58. * the even frame, the first active line is 584.
  59. *
  60. * LN: 560 561 562 563 567 568 569
  61. * DE: ~~~|____________________________//__________________________
  62. * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
  63. * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
  64. * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
  65. *
  66. * LN: 1123 1124 1125 1 5 6 7
  67. * DE: ~~~|____________________________//__________________________
  68. * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
  69. * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
  70. * 23 blanking lines
  71. *
  72. * The Armada LCD Controller line and pixel numbers are, like X timings,
  73. * referenced to the top left of the active frame.
  74. *
  75. * So, translating these to our LCD controller:
  76. * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
  77. * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
  78. * Note: Vsync front porch remains constant!
  79. *
  80. * if (odd_frame) {
  81. * vtotal = mode->crtc_vtotal + 1;
  82. * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
  83. * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
  84. * } else {
  85. * vtotal = mode->crtc_vtotal;
  86. * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
  87. * vhorizpos = mode->crtc_hsync_start;
  88. * }
  89. * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
  90. *
  91. * So, we need to reprogram these registers on each vsync event:
  92. * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
  93. *
  94. * Note: we do not use the frame done interrupts because these appear
  95. * to happen too early, and lead to jitter on the display (presumably
  96. * they occur at the end of the last active line, before the vsync back
  97. * porch, which we're reprogramming.)
  98. */
  99. void
  100. armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
  101. {
  102. while (regs->offset != ~0) {
  103. void __iomem *reg = dcrtc->base + regs->offset;
  104. uint32_t val;
  105. val = regs->mask;
  106. if (val != 0)
  107. val &= readl_relaxed(reg);
  108. writel_relaxed(val | regs->val, reg);
  109. ++regs;
  110. }
  111. }
  112. #define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
  113. static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
  114. {
  115. uint32_t dumb_ctrl;
  116. dumb_ctrl = dcrtc->cfg_dumb_ctrl;
  117. if (!dpms_blanked(dcrtc->dpms))
  118. dumb_ctrl |= CFG_DUMB_ENA;
  119. /*
  120. * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
  121. * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
  122. * force LCD_D[23:0] to output blank color, overriding the GPIO or
  123. * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
  124. */
  125. if (dpms_blanked(dcrtc->dpms) &&
  126. (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
  127. dumb_ctrl &= ~DUMB_MASK;
  128. dumb_ctrl |= DUMB_BLANK;
  129. }
  130. /*
  131. * The documentation doesn't indicate what the normal state of
  132. * the sync signals are. Sebastian Hesselbart kindly probed
  133. * these signals on his board to determine their state.
  134. *
  135. * The non-inverted state of the sync signals is active high.
  136. * Setting these bits makes the appropriate signal active low.
  137. */
  138. if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
  139. dumb_ctrl |= CFG_INV_CSYNC;
  140. if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
  141. dumb_ctrl |= CFG_INV_HSYNC;
  142. if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
  143. dumb_ctrl |= CFG_INV_VSYNC;
  144. if (dcrtc->dumb_ctrl != dumb_ctrl) {
  145. dcrtc->dumb_ctrl = dumb_ctrl;
  146. writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
  147. }
  148. }
  149. void armada_drm_plane_calc_addrs(u32 *addrs, struct drm_framebuffer *fb,
  150. int x, int y)
  151. {
  152. const struct drm_format_info *format = fb->format;
  153. unsigned int num_planes = format->num_planes;
  154. u32 addr = drm_fb_obj(fb)->dev_addr;
  155. int i;
  156. if (num_planes > 3)
  157. num_planes = 3;
  158. addrs[0] = addr + fb->offsets[0] + y * fb->pitches[0] +
  159. x * format->cpp[0];
  160. y /= format->vsub;
  161. x /= format->hsub;
  162. for (i = 1; i < num_planes; i++)
  163. addrs[i] = addr + fb->offsets[i] + y * fb->pitches[i] +
  164. x * format->cpp[i];
  165. for (; i < 3; i++)
  166. addrs[i] = 0;
  167. }
  168. static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
  169. int x, int y, struct armada_regs *regs, bool interlaced)
  170. {
  171. unsigned pitch = fb->pitches[0];
  172. u32 addrs[3], addr_odd, addr_even;
  173. unsigned i = 0;
  174. DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
  175. pitch, x, y, fb->format->cpp[0] * 8);
  176. armada_drm_plane_calc_addrs(addrs, fb, x, y);
  177. addr_odd = addr_even = addrs[0];
  178. if (interlaced) {
  179. addr_even += pitch;
  180. pitch *= 2;
  181. }
  182. /* write offset, base, and pitch */
  183. armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
  184. armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
  185. armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
  186. return i;
  187. }
  188. static void armada_drm_plane_work_call(struct armada_crtc *dcrtc,
  189. struct armada_plane_work *work,
  190. void (*fn)(struct armada_crtc *, struct armada_plane_work *))
  191. {
  192. struct armada_plane *dplane = drm_to_armada_plane(work->plane);
  193. struct drm_pending_vblank_event *event;
  194. struct drm_framebuffer *fb;
  195. if (fn)
  196. fn(dcrtc, work);
  197. drm_crtc_vblank_put(&dcrtc->crtc);
  198. event = work->event;
  199. fb = work->old_fb;
  200. if (event || fb) {
  201. struct drm_device *dev = dcrtc->crtc.dev;
  202. unsigned long flags;
  203. spin_lock_irqsave(&dev->event_lock, flags);
  204. if (event)
  205. drm_crtc_send_vblank_event(&dcrtc->crtc, event);
  206. if (fb)
  207. __armada_drm_queue_unref_work(dev, fb);
  208. spin_unlock_irqrestore(&dev->event_lock, flags);
  209. }
  210. if (work->need_kfree)
  211. kfree(work);
  212. wake_up(&dplane->frame_wait);
  213. }
  214. static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
  215. struct drm_plane *plane)
  216. {
  217. struct armada_plane *dplane = drm_to_armada_plane(plane);
  218. struct armada_plane_work *work = xchg(&dplane->work, NULL);
  219. /* Handle any pending frame work. */
  220. if (work)
  221. armada_drm_plane_work_call(dcrtc, work, work->fn);
  222. }
  223. int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
  224. struct armada_plane_work *work)
  225. {
  226. struct armada_plane *plane = drm_to_armada_plane(work->plane);
  227. int ret;
  228. ret = drm_crtc_vblank_get(&dcrtc->crtc);
  229. if (ret)
  230. return ret;
  231. ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
  232. if (ret)
  233. drm_crtc_vblank_put(&dcrtc->crtc);
  234. return ret;
  235. }
  236. int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
  237. {
  238. return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
  239. }
  240. void armada_drm_plane_work_cancel(struct armada_crtc *dcrtc,
  241. struct armada_plane *dplane)
  242. {
  243. struct armada_plane_work *work = xchg(&dplane->work, NULL);
  244. if (work)
  245. armada_drm_plane_work_call(dcrtc, work, work->cancel);
  246. }
  247. static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
  248. struct armada_plane_work *work)
  249. {
  250. unsigned long flags;
  251. spin_lock_irqsave(&dcrtc->irq_lock, flags);
  252. armada_drm_crtc_update_regs(dcrtc, work->regs);
  253. spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
  254. }
  255. static void armada_drm_crtc_complete_disable_work(struct armada_crtc *dcrtc,
  256. struct armada_plane_work *work)
  257. {
  258. unsigned long flags;
  259. if (dcrtc->plane == work->plane)
  260. dcrtc->plane = NULL;
  261. spin_lock_irqsave(&dcrtc->irq_lock, flags);
  262. armada_drm_crtc_update_regs(dcrtc, work->regs);
  263. spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
  264. }
  265. static struct armada_plane_work *
  266. armada_drm_crtc_alloc_plane_work(struct drm_plane *plane)
  267. {
  268. struct armada_plane_work *work;
  269. int i = 0;
  270. work = kzalloc(sizeof(*work), GFP_KERNEL);
  271. if (!work)
  272. return NULL;
  273. work->plane = plane;
  274. work->fn = armada_drm_crtc_complete_frame_work;
  275. work->need_kfree = true;
  276. armada_reg_queue_end(work->regs, i);
  277. return work;
  278. }
  279. static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
  280. struct drm_framebuffer *fb, bool force)
  281. {
  282. struct armada_plane_work *work;
  283. if (!fb)
  284. return;
  285. if (force) {
  286. /* Display is disabled, so just drop the old fb */
  287. drm_framebuffer_put(fb);
  288. return;
  289. }
  290. work = armada_drm_crtc_alloc_plane_work(dcrtc->crtc.primary);
  291. if (work) {
  292. work->old_fb = fb;
  293. if (armada_drm_plane_work_queue(dcrtc, work) == 0)
  294. return;
  295. kfree(work);
  296. }
  297. /*
  298. * Oops - just drop the reference immediately and hope for
  299. * the best. The worst that will happen is the buffer gets
  300. * reused before it has finished being displayed.
  301. */
  302. drm_framebuffer_put(fb);
  303. }
  304. static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
  305. {
  306. /*
  307. * Tell the DRM core that vblank IRQs aren't going to happen for
  308. * a while. This cleans up any pending vblank events for us.
  309. */
  310. drm_crtc_vblank_off(&dcrtc->crtc);
  311. armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
  312. }
  313. /* The mode_config.mutex will be held for this call */
  314. static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
  315. {
  316. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  317. if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) {
  318. if (dpms_blanked(dpms))
  319. armada_drm_vblank_off(dcrtc);
  320. else if (!IS_ERR(dcrtc->clk))
  321. WARN_ON(clk_prepare_enable(dcrtc->clk));
  322. dcrtc->dpms = dpms;
  323. armada_drm_crtc_update(dcrtc);
  324. if (!dpms_blanked(dpms))
  325. drm_crtc_vblank_on(&dcrtc->crtc);
  326. else if (!IS_ERR(dcrtc->clk))
  327. clk_disable_unprepare(dcrtc->clk);
  328. } else if (dcrtc->dpms != dpms) {
  329. dcrtc->dpms = dpms;
  330. }
  331. }
  332. /*
  333. * Prepare for a mode set. Turn off overlay to ensure that we don't end
  334. * up with the overlay size being bigger than the active screen size.
  335. * We rely upon X refreshing this state after the mode set has completed.
  336. *
  337. * The mode_config.mutex will be held for this call
  338. */
  339. static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
  340. {
  341. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  342. struct drm_plane *plane;
  343. /*
  344. * If we have an overlay plane associated with this CRTC, disable
  345. * it before the modeset to avoid its coordinates being outside
  346. * the new mode parameters.
  347. */
  348. plane = dcrtc->plane;
  349. if (plane) {
  350. drm_plane_force_disable(plane);
  351. WARN_ON(!armada_drm_plane_work_wait(drm_to_armada_plane(plane),
  352. HZ));
  353. }
  354. }
  355. /* The mode_config.mutex will be held for this call */
  356. static void armada_drm_crtc_commit(struct drm_crtc *crtc)
  357. {
  358. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  359. if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
  360. dcrtc->dpms = DRM_MODE_DPMS_ON;
  361. armada_drm_crtc_update(dcrtc);
  362. }
  363. }
  364. /* The mode_config.mutex will be held for this call */
  365. static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  366. const struct drm_display_mode *mode, struct drm_display_mode *adj)
  367. {
  368. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  369. int ret;
  370. /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
  371. if (!dcrtc->variant->has_spu_adv_reg &&
  372. adj->flags & DRM_MODE_FLAG_INTERLACE)
  373. return false;
  374. /* Check whether the display mode is possible */
  375. ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
  376. if (ret)
  377. return false;
  378. return true;
  379. }
  380. /* These are locked by dev->vbl_lock */
  381. static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
  382. {
  383. if (dcrtc->irq_ena & mask) {
  384. dcrtc->irq_ena &= ~mask;
  385. writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
  386. }
  387. }
  388. static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
  389. {
  390. if ((dcrtc->irq_ena & mask) != mask) {
  391. dcrtc->irq_ena |= mask;
  392. writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
  393. if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
  394. writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
  395. }
  396. }
  397. static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
  398. {
  399. void __iomem *base = dcrtc->base;
  400. struct drm_plane *ovl_plane;
  401. if (stat & DMA_FF_UNDERFLOW)
  402. DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
  403. if (stat & GRA_FF_UNDERFLOW)
  404. DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
  405. if (stat & VSYNC_IRQ)
  406. drm_crtc_handle_vblank(&dcrtc->crtc);
  407. ovl_plane = dcrtc->plane;
  408. if (ovl_plane)
  409. armada_drm_plane_work_run(dcrtc, ovl_plane);
  410. spin_lock(&dcrtc->irq_lock);
  411. if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
  412. int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
  413. uint32_t val;
  414. writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
  415. writel_relaxed(dcrtc->v[i].spu_v_h_total,
  416. base + LCD_SPUT_V_H_TOTAL);
  417. val = readl_relaxed(base + LCD_SPU_ADV_REG);
  418. val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
  419. val |= dcrtc->v[i].spu_adv_reg;
  420. writel_relaxed(val, base + LCD_SPU_ADV_REG);
  421. }
  422. if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
  423. writel_relaxed(dcrtc->cursor_hw_pos,
  424. base + LCD_SPU_HWC_OVSA_HPXL_VLN);
  425. writel_relaxed(dcrtc->cursor_hw_sz,
  426. base + LCD_SPU_HWC_HPXL_VLN);
  427. armada_updatel(CFG_HWC_ENA,
  428. CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
  429. base + LCD_SPU_DMA_CTRL0);
  430. dcrtc->cursor_update = false;
  431. armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  432. }
  433. spin_unlock(&dcrtc->irq_lock);
  434. if (stat & GRA_FRAME_IRQ)
  435. armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
  436. }
  437. static irqreturn_t armada_drm_irq(int irq, void *arg)
  438. {
  439. struct armada_crtc *dcrtc = arg;
  440. u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
  441. /*
  442. * This is rediculous - rather than writing bits to clear, we
  443. * have to set the actual status register value. This is racy.
  444. */
  445. writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
  446. trace_armada_drm_irq(&dcrtc->crtc, stat);
  447. /* Mask out those interrupts we haven't enabled */
  448. v = stat & dcrtc->irq_ena;
  449. if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
  450. armada_drm_crtc_irq(dcrtc, stat);
  451. return IRQ_HANDLED;
  452. }
  453. return IRQ_NONE;
  454. }
  455. static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
  456. {
  457. struct drm_display_mode *adj = &dcrtc->crtc.mode;
  458. uint32_t val = 0;
  459. if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
  460. val |= CFG_CSC_YUV_CCIR709;
  461. if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
  462. val |= CFG_CSC_RGB_STUDIO;
  463. /*
  464. * In auto mode, set the colorimetry, based upon the HDMI spec.
  465. * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
  466. * ITU601. It may be more appropriate to set this depending on
  467. * the source - but what if the graphic frame is YUV and the
  468. * video frame is RGB?
  469. */
  470. if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
  471. !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
  472. (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
  473. if (dcrtc->csc_yuv_mode == CSC_AUTO)
  474. val |= CFG_CSC_YUV_CCIR709;
  475. }
  476. /*
  477. * We assume we're connected to a TV-like device, so the YUV->RGB
  478. * conversion should produce a limited range. We should set this
  479. * depending on the connectors attached to this CRTC, and what
  480. * kind of device they report being connected.
  481. */
  482. if (dcrtc->csc_rgb_mode == CSC_AUTO)
  483. val |= CFG_CSC_RGB_STUDIO;
  484. return val;
  485. }
  486. static void armada_drm_gra_plane_regs(struct armada_regs *regs,
  487. struct drm_framebuffer *fb, struct armada_plane_state *state,
  488. int x, int y, bool interlaced)
  489. {
  490. unsigned int i;
  491. u32 ctrl0;
  492. i = armada_drm_crtc_calc_fb(fb, x, y, regs, interlaced);
  493. armada_reg_queue_set(regs, i, state->dst_yx, LCD_SPU_GRA_OVSA_HPXL_VLN);
  494. armada_reg_queue_set(regs, i, state->src_hw, LCD_SPU_GRA_HPXL_VLN);
  495. armada_reg_queue_set(regs, i, state->dst_hw, LCD_SPU_GZM_HPXL_VLN);
  496. ctrl0 = state->ctrl0;
  497. if (interlaced)
  498. ctrl0 |= CFG_GRA_FTOGGLE;
  499. armada_reg_queue_mod(regs, i, ctrl0, CFG_GRAFORMAT |
  500. CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
  501. CFG_SWAPYU | CFG_YUV2RGB) |
  502. CFG_PALETTE_ENA | CFG_GRA_FTOGGLE |
  503. CFG_GRA_HSMOOTH | CFG_GRA_ENA,
  504. LCD_SPU_DMA_CTRL0);
  505. armada_reg_queue_end(regs, i);
  506. }
  507. static void armada_drm_primary_set(struct drm_crtc *crtc,
  508. struct drm_plane *plane, int x, int y)
  509. {
  510. struct armada_plane_state *state = &drm_to_armada_plane(plane)->state;
  511. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  512. struct armada_regs regs[8];
  513. bool interlaced = dcrtc->interlaced;
  514. armada_drm_gra_plane_regs(regs, plane->fb, state, x, y, interlaced);
  515. armada_drm_crtc_update_regs(dcrtc, regs);
  516. }
  517. /* The mode_config.mutex will be held for this call */
  518. static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
  519. struct drm_display_mode *mode, struct drm_display_mode *adj,
  520. int x, int y, struct drm_framebuffer *old_fb)
  521. {
  522. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  523. struct armada_regs regs[17];
  524. uint32_t lm, rm, tm, bm, val, sclk;
  525. unsigned long flags;
  526. unsigned i;
  527. bool interlaced;
  528. drm_framebuffer_get(crtc->primary->fb);
  529. interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
  530. val = CFG_GRA_ENA;
  531. val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
  532. val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
  533. if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
  534. val |= CFG_PALETTE_ENA;
  535. drm_to_armada_plane(crtc->primary)->state.ctrl0 = val;
  536. drm_to_armada_plane(crtc->primary)->state.src_hw =
  537. drm_to_armada_plane(crtc->primary)->state.dst_hw =
  538. adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
  539. drm_to_armada_plane(crtc->primary)->state.dst_yx = 0;
  540. i = 0;
  541. rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
  542. lm = adj->crtc_htotal - adj->crtc_hsync_end;
  543. bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
  544. tm = adj->crtc_vtotal - adj->crtc_vsync_end;
  545. DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
  546. adj->crtc_hdisplay,
  547. adj->crtc_hsync_start,
  548. adj->crtc_hsync_end,
  549. adj->crtc_htotal, lm, rm);
  550. DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
  551. adj->crtc_vdisplay,
  552. adj->crtc_vsync_start,
  553. adj->crtc_vsync_end,
  554. adj->crtc_vtotal, tm, bm);
  555. /* Wait for pending flips to complete */
  556. armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
  557. MAX_SCHEDULE_TIMEOUT);
  558. drm_crtc_vblank_off(crtc);
  559. val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
  560. if (val != dcrtc->dumb_ctrl) {
  561. dcrtc->dumb_ctrl = val;
  562. writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
  563. }
  564. /*
  565. * If we are blanked, we would have disabled the clock. Re-enable
  566. * it so that compute_clock() does the right thing.
  567. */
  568. if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
  569. WARN_ON(clk_prepare_enable(dcrtc->clk));
  570. /* Now compute the divider for real */
  571. dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
  572. armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
  573. if (interlaced ^ dcrtc->interlaced) {
  574. if (adj->flags & DRM_MODE_FLAG_INTERLACE)
  575. drm_crtc_vblank_get(&dcrtc->crtc);
  576. else
  577. drm_crtc_vblank_put(&dcrtc->crtc);
  578. dcrtc->interlaced = interlaced;
  579. }
  580. spin_lock_irqsave(&dcrtc->irq_lock, flags);
  581. /* Ensure graphic fifo is enabled */
  582. armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
  583. /* Even interlaced/progressive frame */
  584. dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
  585. adj->crtc_htotal;
  586. dcrtc->v[1].spu_v_porch = tm << 16 | bm;
  587. val = adj->crtc_hsync_start;
  588. dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
  589. dcrtc->variant->spu_adv_reg;
  590. if (interlaced) {
  591. /* Odd interlaced frame */
  592. dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
  593. (1 << 16);
  594. dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
  595. val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
  596. dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
  597. dcrtc->variant->spu_adv_reg;
  598. } else {
  599. dcrtc->v[0] = dcrtc->v[1];
  600. }
  601. val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
  602. armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
  603. armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
  604. armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
  605. armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
  606. LCD_SPUT_V_H_TOTAL);
  607. if (dcrtc->variant->has_spu_adv_reg) {
  608. armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
  609. ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
  610. ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
  611. }
  612. val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
  613. armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
  614. val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
  615. armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
  616. armada_reg_queue_end(regs, i);
  617. armada_drm_crtc_update_regs(dcrtc, regs);
  618. armada_drm_primary_set(crtc, crtc->primary, x, y);
  619. spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
  620. armada_drm_crtc_update(dcrtc);
  621. drm_crtc_vblank_on(crtc);
  622. armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
  623. return 0;
  624. }
  625. /* The mode_config.mutex will be held for this call */
  626. static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  627. struct drm_framebuffer *old_fb)
  628. {
  629. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  630. struct armada_regs regs[4];
  631. unsigned i;
  632. i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
  633. dcrtc->interlaced);
  634. armada_reg_queue_end(regs, i);
  635. /* Wait for pending flips to complete */
  636. armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
  637. MAX_SCHEDULE_TIMEOUT);
  638. /* Take a reference to the new fb as we're using it */
  639. drm_framebuffer_get(crtc->primary->fb);
  640. /* Update the base in the CRTC */
  641. armada_drm_crtc_update_regs(dcrtc, regs);
  642. /* Drop our previously held reference */
  643. armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
  644. return 0;
  645. }
  646. /* The mode_config.mutex will be held for this call */
  647. static void armada_drm_crtc_disable(struct drm_crtc *crtc)
  648. {
  649. armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  650. /* Disable our primary plane when we disable the CRTC. */
  651. crtc->primary->funcs->disable_plane(crtc->primary, NULL);
  652. }
  653. static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
  654. .dpms = armada_drm_crtc_dpms,
  655. .prepare = armada_drm_crtc_prepare,
  656. .commit = armada_drm_crtc_commit,
  657. .mode_fixup = armada_drm_crtc_mode_fixup,
  658. .mode_set = armada_drm_crtc_mode_set,
  659. .mode_set_base = armada_drm_crtc_mode_set_base,
  660. .disable = armada_drm_crtc_disable,
  661. };
  662. static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
  663. unsigned stride, unsigned width, unsigned height)
  664. {
  665. uint32_t addr;
  666. unsigned y;
  667. addr = SRAM_HWC32_RAM1;
  668. for (y = 0; y < height; y++) {
  669. uint32_t *p = &pix[y * stride];
  670. unsigned x;
  671. for (x = 0; x < width; x++, p++) {
  672. uint32_t val = *p;
  673. val = (val & 0xff00ff00) |
  674. (val & 0x000000ff) << 16 |
  675. (val & 0x00ff0000) >> 16;
  676. writel_relaxed(val,
  677. base + LCD_SPU_SRAM_WRDAT);
  678. writel_relaxed(addr | SRAM_WRITE,
  679. base + LCD_SPU_SRAM_CTRL);
  680. readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
  681. addr += 1;
  682. if ((addr & 0x00ff) == 0)
  683. addr += 0xf00;
  684. if ((addr & 0x30ff) == 0)
  685. addr = SRAM_HWC32_RAM2;
  686. }
  687. }
  688. }
  689. static void armada_drm_crtc_cursor_tran(void __iomem *base)
  690. {
  691. unsigned addr;
  692. for (addr = 0; addr < 256; addr++) {
  693. /* write the default value */
  694. writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
  695. writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
  696. base + LCD_SPU_SRAM_CTRL);
  697. }
  698. }
  699. static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
  700. {
  701. uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
  702. uint32_t yoff, yscr, h = dcrtc->cursor_h;
  703. uint32_t para1;
  704. /*
  705. * Calculate the visible width and height of the cursor,
  706. * screen position, and the position in the cursor bitmap.
  707. */
  708. if (dcrtc->cursor_x < 0) {
  709. xoff = -dcrtc->cursor_x;
  710. xscr = 0;
  711. w -= min(xoff, w);
  712. } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
  713. xoff = 0;
  714. xscr = dcrtc->cursor_x;
  715. w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
  716. } else {
  717. xoff = 0;
  718. xscr = dcrtc->cursor_x;
  719. }
  720. if (dcrtc->cursor_y < 0) {
  721. yoff = -dcrtc->cursor_y;
  722. yscr = 0;
  723. h -= min(yoff, h);
  724. } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
  725. yoff = 0;
  726. yscr = dcrtc->cursor_y;
  727. h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
  728. } else {
  729. yoff = 0;
  730. yscr = dcrtc->cursor_y;
  731. }
  732. /* On interlaced modes, the vertical cursor size must be halved */
  733. s = dcrtc->cursor_w;
  734. if (dcrtc->interlaced) {
  735. s *= 2;
  736. yscr /= 2;
  737. h /= 2;
  738. }
  739. if (!dcrtc->cursor_obj || !h || !w) {
  740. spin_lock_irq(&dcrtc->irq_lock);
  741. armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  742. dcrtc->cursor_update = false;
  743. armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
  744. spin_unlock_irq(&dcrtc->irq_lock);
  745. return 0;
  746. }
  747. spin_lock_irq(&dcrtc->irq_lock);
  748. para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
  749. armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
  750. dcrtc->base + LCD_SPU_SRAM_PARA1);
  751. spin_unlock_irq(&dcrtc->irq_lock);
  752. /*
  753. * Initialize the transparency if the SRAM was powered down.
  754. * We must also reload the cursor data as well.
  755. */
  756. if (!(para1 & CFG_CSB_256x32)) {
  757. armada_drm_crtc_cursor_tran(dcrtc->base);
  758. reload = true;
  759. }
  760. if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
  761. spin_lock_irq(&dcrtc->irq_lock);
  762. armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  763. dcrtc->cursor_update = false;
  764. armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
  765. spin_unlock_irq(&dcrtc->irq_lock);
  766. reload = true;
  767. }
  768. if (reload) {
  769. struct armada_gem_object *obj = dcrtc->cursor_obj;
  770. uint32_t *pix;
  771. /* Set the top-left corner of the cursor image */
  772. pix = obj->addr;
  773. pix += yoff * s + xoff;
  774. armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
  775. }
  776. /* Reload the cursor position, size and enable in the IRQ handler */
  777. spin_lock_irq(&dcrtc->irq_lock);
  778. dcrtc->cursor_hw_pos = yscr << 16 | xscr;
  779. dcrtc->cursor_hw_sz = h << 16 | w;
  780. dcrtc->cursor_update = true;
  781. armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  782. spin_unlock_irq(&dcrtc->irq_lock);
  783. return 0;
  784. }
  785. static void cursor_update(void *data)
  786. {
  787. armada_drm_crtc_cursor_update(data, true);
  788. }
  789. static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
  790. struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
  791. {
  792. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  793. struct armada_gem_object *obj = NULL;
  794. int ret;
  795. /* If no cursor support, replicate drm's return value */
  796. if (!dcrtc->variant->has_spu_adv_reg)
  797. return -ENXIO;
  798. if (handle && w > 0 && h > 0) {
  799. /* maximum size is 64x32 or 32x64 */
  800. if (w > 64 || h > 64 || (w > 32 && h > 32))
  801. return -ENOMEM;
  802. obj = armada_gem_object_lookup(file, handle);
  803. if (!obj)
  804. return -ENOENT;
  805. /* Must be a kernel-mapped object */
  806. if (!obj->addr) {
  807. drm_gem_object_put_unlocked(&obj->obj);
  808. return -EINVAL;
  809. }
  810. if (obj->obj.size < w * h * 4) {
  811. DRM_ERROR("buffer is too small\n");
  812. drm_gem_object_put_unlocked(&obj->obj);
  813. return -ENOMEM;
  814. }
  815. }
  816. if (dcrtc->cursor_obj) {
  817. dcrtc->cursor_obj->update = NULL;
  818. dcrtc->cursor_obj->update_data = NULL;
  819. drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
  820. }
  821. dcrtc->cursor_obj = obj;
  822. dcrtc->cursor_w = w;
  823. dcrtc->cursor_h = h;
  824. ret = armada_drm_crtc_cursor_update(dcrtc, true);
  825. if (obj) {
  826. obj->update_data = dcrtc;
  827. obj->update = cursor_update;
  828. }
  829. return ret;
  830. }
  831. static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  832. {
  833. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  834. int ret;
  835. /* If no cursor support, replicate drm's return value */
  836. if (!dcrtc->variant->has_spu_adv_reg)
  837. return -EFAULT;
  838. dcrtc->cursor_x = x;
  839. dcrtc->cursor_y = y;
  840. ret = armada_drm_crtc_cursor_update(dcrtc, false);
  841. return ret;
  842. }
  843. static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
  844. {
  845. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  846. struct armada_private *priv = crtc->dev->dev_private;
  847. if (dcrtc->cursor_obj)
  848. drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj);
  849. priv->dcrtc[dcrtc->num] = NULL;
  850. drm_crtc_cleanup(&dcrtc->crtc);
  851. if (!IS_ERR(dcrtc->clk))
  852. clk_disable_unprepare(dcrtc->clk);
  853. writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
  854. of_node_put(dcrtc->crtc.port);
  855. kfree(dcrtc);
  856. }
  857. /*
  858. * The mode_config lock is held here, to prevent races between this
  859. * and a mode_set.
  860. */
  861. static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
  862. struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags,
  863. struct drm_modeset_acquire_ctx *ctx)
  864. {
  865. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  866. struct armada_plane_work *work;
  867. unsigned i;
  868. int ret;
  869. /* We don't support changing the pixel format */
  870. if (fb->format != crtc->primary->fb->format)
  871. return -EINVAL;
  872. work = armada_drm_crtc_alloc_plane_work(dcrtc->crtc.primary);
  873. if (!work)
  874. return -ENOMEM;
  875. work->event = event;
  876. work->old_fb = dcrtc->crtc.primary->fb;
  877. i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
  878. dcrtc->interlaced);
  879. armada_reg_queue_end(work->regs, i);
  880. /*
  881. * Ensure that we hold a reference on the new framebuffer.
  882. * This has to match the behaviour in mode_set.
  883. */
  884. drm_framebuffer_get(fb);
  885. ret = armada_drm_plane_work_queue(dcrtc, work);
  886. if (ret) {
  887. /* Undo our reference above */
  888. drm_framebuffer_put(fb);
  889. kfree(work);
  890. return ret;
  891. }
  892. /*
  893. * Don't take a reference on the new framebuffer;
  894. * drm_mode_page_flip_ioctl() has already grabbed a reference and
  895. * will _not_ drop that reference on successful return from this
  896. * function. Simply mark this new framebuffer as the current one.
  897. */
  898. dcrtc->crtc.primary->fb = fb;
  899. /*
  900. * Finally, if the display is blanked, we won't receive an
  901. * interrupt, so complete it now.
  902. */
  903. if (dpms_blanked(dcrtc->dpms))
  904. armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary);
  905. return 0;
  906. }
  907. static int
  908. armada_drm_crtc_set_property(struct drm_crtc *crtc,
  909. struct drm_property *property, uint64_t val)
  910. {
  911. struct armada_private *priv = crtc->dev->dev_private;
  912. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  913. bool update_csc = false;
  914. if (property == priv->csc_yuv_prop) {
  915. dcrtc->csc_yuv_mode = val;
  916. update_csc = true;
  917. } else if (property == priv->csc_rgb_prop) {
  918. dcrtc->csc_rgb_mode = val;
  919. update_csc = true;
  920. }
  921. if (update_csc) {
  922. uint32_t val;
  923. val = dcrtc->spu_iopad_ctrl |
  924. armada_drm_crtc_calculate_csc(dcrtc);
  925. writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
  926. }
  927. return 0;
  928. }
  929. /* These are called under the vbl_lock. */
  930. static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc)
  931. {
  932. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  933. armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA);
  934. return 0;
  935. }
  936. static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc)
  937. {
  938. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  939. armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA);
  940. }
  941. static const struct drm_crtc_funcs armada_crtc_funcs = {
  942. .cursor_set = armada_drm_crtc_cursor_set,
  943. .cursor_move = armada_drm_crtc_cursor_move,
  944. .destroy = armada_drm_crtc_destroy,
  945. .set_config = drm_crtc_helper_set_config,
  946. .page_flip = armada_drm_crtc_page_flip,
  947. .set_property = armada_drm_crtc_set_property,
  948. .enable_vblank = armada_drm_crtc_enable_vblank,
  949. .disable_vblank = armada_drm_crtc_disable_vblank,
  950. };
  951. static void armada_drm_primary_update_state(struct drm_plane_state *state,
  952. struct armada_regs *regs)
  953. {
  954. struct armada_plane *dplane = drm_to_armada_plane(state->plane);
  955. struct armada_crtc *dcrtc = drm_to_armada_crtc(state->crtc);
  956. struct armada_framebuffer *dfb = drm_fb_to_armada_fb(state->fb);
  957. bool was_disabled;
  958. unsigned int idx = 0;
  959. u32 val;
  960. val = CFG_GRA_FMT(dfb->fmt) | CFG_GRA_MOD(dfb->mod);
  961. if (dfb->fmt > CFG_420)
  962. val |= CFG_PALETTE_ENA;
  963. if (state->visible)
  964. val |= CFG_GRA_ENA;
  965. if (drm_rect_width(&state->src) >> 16 != drm_rect_width(&state->dst))
  966. val |= CFG_GRA_HSMOOTH;
  967. was_disabled = !(dplane->state.ctrl0 & CFG_GRA_ENA);
  968. if (was_disabled)
  969. armada_reg_queue_mod(regs, idx,
  970. 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
  971. dplane->state.ctrl0 = val;
  972. dplane->state.src_hw = (drm_rect_height(&state->src) & 0xffff0000) |
  973. drm_rect_width(&state->src) >> 16;
  974. dplane->state.dst_hw = drm_rect_height(&state->dst) << 16 |
  975. drm_rect_width(&state->dst);
  976. dplane->state.dst_yx = state->dst.y1 << 16 | state->dst.x1;
  977. armada_drm_gra_plane_regs(regs + idx, &dfb->fb, &dplane->state,
  978. state->src.x1 >> 16, state->src.y1 >> 16,
  979. dcrtc->interlaced);
  980. dplane->state.vsync_update = !was_disabled;
  981. dplane->state.changed = true;
  982. }
  983. static int armada_drm_primary_update(struct drm_plane *plane,
  984. struct drm_crtc *crtc, struct drm_framebuffer *fb,
  985. int crtc_x, int crtc_y, unsigned int crtc_w, unsigned int crtc_h,
  986. uint32_t src_x, uint32_t src_y, uint32_t src_w, uint32_t src_h,
  987. struct drm_modeset_acquire_ctx *ctx)
  988. {
  989. struct armada_plane *dplane = drm_to_armada_plane(plane);
  990. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  991. struct armada_plane_work *work;
  992. struct drm_plane_state state = {
  993. .plane = plane,
  994. .crtc = crtc,
  995. .fb = fb,
  996. .src_x = src_x,
  997. .src_y = src_y,
  998. .src_w = src_w,
  999. .src_h = src_h,
  1000. .crtc_x = crtc_x,
  1001. .crtc_y = crtc_y,
  1002. .crtc_w = crtc_w,
  1003. .crtc_h = crtc_h,
  1004. .rotation = DRM_MODE_ROTATE_0,
  1005. };
  1006. struct drm_crtc_state crtc_state = {
  1007. .crtc = crtc,
  1008. .enable = crtc->enabled,
  1009. .mode = crtc->mode,
  1010. };
  1011. int ret;
  1012. ret = drm_atomic_helper_check_plane_state(&state, &crtc_state, 0,
  1013. INT_MAX, true, false);
  1014. if (ret)
  1015. return ret;
  1016. work = &dplane->works[dplane->next_work];
  1017. work->fn = armada_drm_crtc_complete_frame_work;
  1018. if (plane->fb != fb) {
  1019. /*
  1020. * Take a reference on the new framebuffer - we want to
  1021. * hold on to it while the hardware is displaying it.
  1022. */
  1023. drm_framebuffer_reference(fb);
  1024. work->old_fb = plane->fb;
  1025. } else {
  1026. work->old_fb = NULL;
  1027. }
  1028. armada_drm_primary_update_state(&state, work->regs);
  1029. if (!dplane->state.changed)
  1030. return 0;
  1031. /* Wait for pending work to complete */
  1032. if (armada_drm_plane_work_wait(dplane, HZ / 10) == 0)
  1033. armada_drm_plane_work_cancel(dcrtc, dplane);
  1034. if (!dplane->state.vsync_update) {
  1035. work->fn(dcrtc, work);
  1036. if (work->old_fb)
  1037. drm_framebuffer_unreference(work->old_fb);
  1038. return 0;
  1039. }
  1040. /* Queue it for update on the next interrupt if we are enabled */
  1041. ret = armada_drm_plane_work_queue(dcrtc, work);
  1042. if (ret) {
  1043. work->fn(dcrtc, work);
  1044. if (work->old_fb)
  1045. drm_framebuffer_unreference(work->old_fb);
  1046. }
  1047. dplane->next_work = !dplane->next_work;
  1048. return 0;
  1049. }
  1050. int armada_drm_plane_disable(struct drm_plane *plane,
  1051. struct drm_modeset_acquire_ctx *ctx)
  1052. {
  1053. struct armada_plane *dplane = drm_to_armada_plane(plane);
  1054. struct armada_crtc *dcrtc;
  1055. struct armada_plane_work *work;
  1056. unsigned int idx = 0;
  1057. u32 sram_para1, enable_mask;
  1058. if (!plane->crtc)
  1059. return 0;
  1060. /*
  1061. * Arrange to power down most RAMs and FIFOs if this is the primary
  1062. * plane, otherwise just the YUV FIFOs for the overlay plane.
  1063. */
  1064. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  1065. sram_para1 = CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
  1066. CFG_PDWN32x32 | CFG_PDWN64x66;
  1067. enable_mask = CFG_GRA_ENA;
  1068. } else {
  1069. sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
  1070. enable_mask = CFG_DMA_ENA;
  1071. }
  1072. dplane->state.ctrl0 &= ~enable_mask;
  1073. dcrtc = drm_to_armada_crtc(plane->crtc);
  1074. /*
  1075. * Try to disable the plane and drop our ref on the framebuffer
  1076. * at the next frame update. If we fail for any reason, disable
  1077. * the plane immediately.
  1078. */
  1079. work = &dplane->works[dplane->next_work];
  1080. work->fn = armada_drm_crtc_complete_disable_work;
  1081. work->cancel = armada_drm_crtc_complete_disable_work;
  1082. work->old_fb = plane->fb;
  1083. armada_reg_queue_mod(work->regs, idx,
  1084. 0, enable_mask, LCD_SPU_DMA_CTRL0);
  1085. armada_reg_queue_mod(work->regs, idx,
  1086. sram_para1, 0, LCD_SPU_SRAM_PARA1);
  1087. armada_reg_queue_end(work->regs, idx);
  1088. /* Wait for any preceding work to complete, but don't wedge */
  1089. if (WARN_ON(!armada_drm_plane_work_wait(dplane, HZ)))
  1090. armada_drm_plane_work_cancel(dcrtc, dplane);
  1091. if (armada_drm_plane_work_queue(dcrtc, work)) {
  1092. work->fn(dcrtc, work);
  1093. if (work->old_fb)
  1094. drm_framebuffer_unreference(work->old_fb);
  1095. }
  1096. dplane->next_work = !dplane->next_work;
  1097. return 0;
  1098. }
  1099. static const struct drm_plane_funcs armada_primary_plane_funcs = {
  1100. .update_plane = armada_drm_primary_update,
  1101. .disable_plane = armada_drm_plane_disable,
  1102. .destroy = drm_primary_helper_destroy,
  1103. };
  1104. int armada_drm_plane_init(struct armada_plane *plane)
  1105. {
  1106. unsigned int i;
  1107. for (i = 0; i < ARRAY_SIZE(plane->works); i++)
  1108. plane->works[i].plane = &plane->base;
  1109. init_waitqueue_head(&plane->frame_wait);
  1110. return 0;
  1111. }
  1112. static const struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
  1113. { CSC_AUTO, "Auto" },
  1114. { CSC_YUV_CCIR601, "CCIR601" },
  1115. { CSC_YUV_CCIR709, "CCIR709" },
  1116. };
  1117. static const struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
  1118. { CSC_AUTO, "Auto" },
  1119. { CSC_RGB_COMPUTER, "Computer system" },
  1120. { CSC_RGB_STUDIO, "Studio" },
  1121. };
  1122. static int armada_drm_crtc_create_properties(struct drm_device *dev)
  1123. {
  1124. struct armada_private *priv = dev->dev_private;
  1125. if (priv->csc_yuv_prop)
  1126. return 0;
  1127. priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
  1128. "CSC_YUV", armada_drm_csc_yuv_enum_list,
  1129. ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
  1130. priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
  1131. "CSC_RGB", armada_drm_csc_rgb_enum_list,
  1132. ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
  1133. if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
  1134. return -ENOMEM;
  1135. return 0;
  1136. }
  1137. static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
  1138. struct resource *res, int irq, const struct armada_variant *variant,
  1139. struct device_node *port)
  1140. {
  1141. struct armada_private *priv = drm->dev_private;
  1142. struct armada_crtc *dcrtc;
  1143. struct armada_plane *primary;
  1144. void __iomem *base;
  1145. int ret;
  1146. ret = armada_drm_crtc_create_properties(drm);
  1147. if (ret)
  1148. return ret;
  1149. base = devm_ioremap_resource(dev, res);
  1150. if (IS_ERR(base))
  1151. return PTR_ERR(base);
  1152. dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
  1153. if (!dcrtc) {
  1154. DRM_ERROR("failed to allocate Armada crtc\n");
  1155. return -ENOMEM;
  1156. }
  1157. if (dev != drm->dev)
  1158. dev_set_drvdata(dev, dcrtc);
  1159. dcrtc->variant = variant;
  1160. dcrtc->base = base;
  1161. dcrtc->num = drm->mode_config.num_crtc;
  1162. dcrtc->clk = ERR_PTR(-EINVAL);
  1163. dcrtc->csc_yuv_mode = CSC_AUTO;
  1164. dcrtc->csc_rgb_mode = CSC_AUTO;
  1165. dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
  1166. dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
  1167. spin_lock_init(&dcrtc->irq_lock);
  1168. dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
  1169. /* Initialize some registers which we don't otherwise set */
  1170. writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
  1171. writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
  1172. writel_relaxed(dcrtc->spu_iopad_ctrl,
  1173. dcrtc->base + LCD_SPU_IOPAD_CONTROL);
  1174. writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
  1175. writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
  1176. CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
  1177. CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
  1178. writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
  1179. writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
  1180. writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
  1181. ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
  1182. dcrtc);
  1183. if (ret < 0)
  1184. goto err_crtc;
  1185. if (dcrtc->variant->init) {
  1186. ret = dcrtc->variant->init(dcrtc, dev);
  1187. if (ret)
  1188. goto err_crtc;
  1189. }
  1190. /* Ensure AXI pipeline is enabled */
  1191. armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
  1192. priv->dcrtc[dcrtc->num] = dcrtc;
  1193. dcrtc->crtc.port = port;
  1194. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  1195. if (!primary) {
  1196. ret = -ENOMEM;
  1197. goto err_crtc;
  1198. }
  1199. ret = armada_drm_plane_init(primary);
  1200. if (ret) {
  1201. kfree(primary);
  1202. goto err_crtc;
  1203. }
  1204. ret = drm_universal_plane_init(drm, &primary->base, 0,
  1205. &armada_primary_plane_funcs,
  1206. armada_primary_formats,
  1207. ARRAY_SIZE(armada_primary_formats),
  1208. NULL,
  1209. DRM_PLANE_TYPE_PRIMARY, NULL);
  1210. if (ret) {
  1211. kfree(primary);
  1212. goto err_crtc;
  1213. }
  1214. ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
  1215. &armada_crtc_funcs, NULL);
  1216. if (ret)
  1217. goto err_crtc_init;
  1218. drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
  1219. drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
  1220. dcrtc->csc_yuv_mode);
  1221. drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
  1222. dcrtc->csc_rgb_mode);
  1223. return armada_overlay_plane_create(drm, 1 << dcrtc->num);
  1224. err_crtc_init:
  1225. primary->base.funcs->destroy(&primary->base);
  1226. err_crtc:
  1227. kfree(dcrtc);
  1228. return ret;
  1229. }
  1230. static int
  1231. armada_lcd_bind(struct device *dev, struct device *master, void *data)
  1232. {
  1233. struct platform_device *pdev = to_platform_device(dev);
  1234. struct drm_device *drm = data;
  1235. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1236. int irq = platform_get_irq(pdev, 0);
  1237. const struct armada_variant *variant;
  1238. struct device_node *port = NULL;
  1239. if (irq < 0)
  1240. return irq;
  1241. if (!dev->of_node) {
  1242. const struct platform_device_id *id;
  1243. id = platform_get_device_id(pdev);
  1244. if (!id)
  1245. return -ENXIO;
  1246. variant = (const struct armada_variant *)id->driver_data;
  1247. } else {
  1248. const struct of_device_id *match;
  1249. struct device_node *np, *parent = dev->of_node;
  1250. match = of_match_device(dev->driver->of_match_table, dev);
  1251. if (!match)
  1252. return -ENXIO;
  1253. np = of_get_child_by_name(parent, "ports");
  1254. if (np)
  1255. parent = np;
  1256. port = of_get_child_by_name(parent, "port");
  1257. of_node_put(np);
  1258. if (!port) {
  1259. dev_err(dev, "no port node found in %pOF\n", parent);
  1260. return -ENXIO;
  1261. }
  1262. variant = match->data;
  1263. }
  1264. return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
  1265. }
  1266. static void
  1267. armada_lcd_unbind(struct device *dev, struct device *master, void *data)
  1268. {
  1269. struct armada_crtc *dcrtc = dev_get_drvdata(dev);
  1270. armada_drm_crtc_destroy(&dcrtc->crtc);
  1271. }
  1272. static const struct component_ops armada_lcd_ops = {
  1273. .bind = armada_lcd_bind,
  1274. .unbind = armada_lcd_unbind,
  1275. };
  1276. static int armada_lcd_probe(struct platform_device *pdev)
  1277. {
  1278. return component_add(&pdev->dev, &armada_lcd_ops);
  1279. }
  1280. static int armada_lcd_remove(struct platform_device *pdev)
  1281. {
  1282. component_del(&pdev->dev, &armada_lcd_ops);
  1283. return 0;
  1284. }
  1285. static const struct of_device_id armada_lcd_of_match[] = {
  1286. {
  1287. .compatible = "marvell,dove-lcd",
  1288. .data = &armada510_ops,
  1289. },
  1290. {}
  1291. };
  1292. MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
  1293. static const struct platform_device_id armada_lcd_platform_ids[] = {
  1294. {
  1295. .name = "armada-lcd",
  1296. .driver_data = (unsigned long)&armada510_ops,
  1297. }, {
  1298. .name = "armada-510-lcd",
  1299. .driver_data = (unsigned long)&armada510_ops,
  1300. },
  1301. { },
  1302. };
  1303. MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
  1304. struct platform_driver armada_lcd_platform_driver = {
  1305. .probe = armada_lcd_probe,
  1306. .remove = armada_lcd_remove,
  1307. .driver = {
  1308. .name = "armada-lcd",
  1309. .owner = THIS_MODULE,
  1310. .of_match_table = armada_lcd_of_match,
  1311. },
  1312. .id_table = armada_lcd_platform_ids,
  1313. };