armada_crtc.c 37 KB

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