armada_crtc.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581
  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. const struct drm_rect clip = {
  1007. .x2 = crtc->mode.hdisplay,
  1008. .y2 = crtc->mode.vdisplay,
  1009. };
  1010. int ret;
  1011. ret = drm_atomic_helper_check_plane_state(&state, crtc->state, &clip, 0,
  1012. INT_MAX, true, false);
  1013. if (ret)
  1014. return ret;
  1015. work = &dplane->works[dplane->next_work];
  1016. work->fn = armada_drm_crtc_complete_frame_work;
  1017. if (plane->fb != fb) {
  1018. /*
  1019. * Take a reference on the new framebuffer - we want to
  1020. * hold on to it while the hardware is displaying it.
  1021. */
  1022. drm_framebuffer_reference(fb);
  1023. work->old_fb = plane->fb;
  1024. } else {
  1025. work->old_fb = NULL;
  1026. }
  1027. armada_drm_primary_update_state(&state, work->regs);
  1028. if (!dplane->state.changed)
  1029. return 0;
  1030. /* Wait for pending work to complete */
  1031. if (armada_drm_plane_work_wait(dplane, HZ / 10) == 0)
  1032. armada_drm_plane_work_cancel(dcrtc, dplane);
  1033. if (!dplane->state.vsync_update) {
  1034. work->fn(dcrtc, work);
  1035. if (work->old_fb)
  1036. drm_framebuffer_unreference(work->old_fb);
  1037. return 0;
  1038. }
  1039. /* Queue it for update on the next interrupt if we are enabled */
  1040. ret = armada_drm_plane_work_queue(dcrtc, work);
  1041. if (ret) {
  1042. work->fn(dcrtc, work);
  1043. if (work->old_fb)
  1044. drm_framebuffer_unreference(work->old_fb);
  1045. }
  1046. dplane->next_work = !dplane->next_work;
  1047. return 0;
  1048. }
  1049. int armada_drm_plane_disable(struct drm_plane *plane,
  1050. struct drm_modeset_acquire_ctx *ctx)
  1051. {
  1052. struct armada_plane *dplane = drm_to_armada_plane(plane);
  1053. struct armada_crtc *dcrtc;
  1054. struct armada_plane_work *work;
  1055. unsigned int idx = 0;
  1056. u32 sram_para1, enable_mask;
  1057. if (!plane->crtc)
  1058. return 0;
  1059. /*
  1060. * Arrange to power down most RAMs and FIFOs if this is the primary
  1061. * plane, otherwise just the YUV FIFOs for the overlay plane.
  1062. */
  1063. if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
  1064. sram_para1 = CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
  1065. CFG_PDWN32x32 | CFG_PDWN64x66;
  1066. enable_mask = CFG_GRA_ENA;
  1067. } else {
  1068. sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
  1069. enable_mask = CFG_DMA_ENA;
  1070. }
  1071. dplane->state.ctrl0 &= ~enable_mask;
  1072. dcrtc = drm_to_armada_crtc(plane->crtc);
  1073. /*
  1074. * Try to disable the plane and drop our ref on the framebuffer
  1075. * at the next frame update. If we fail for any reason, disable
  1076. * the plane immediately.
  1077. */
  1078. work = &dplane->works[dplane->next_work];
  1079. work->fn = armada_drm_crtc_complete_disable_work;
  1080. work->cancel = armada_drm_crtc_complete_disable_work;
  1081. work->old_fb = plane->fb;
  1082. armada_reg_queue_mod(work->regs, idx,
  1083. 0, enable_mask, LCD_SPU_DMA_CTRL0);
  1084. armada_reg_queue_mod(work->regs, idx,
  1085. sram_para1, 0, LCD_SPU_SRAM_PARA1);
  1086. armada_reg_queue_end(work->regs, idx);
  1087. /* Wait for any preceding work to complete, but don't wedge */
  1088. if (WARN_ON(!armada_drm_plane_work_wait(dplane, HZ)))
  1089. armada_drm_plane_work_cancel(dcrtc, dplane);
  1090. if (armada_drm_plane_work_queue(dcrtc, work)) {
  1091. work->fn(dcrtc, work);
  1092. if (work->old_fb)
  1093. drm_framebuffer_unreference(work->old_fb);
  1094. }
  1095. dplane->next_work = !dplane->next_work;
  1096. return 0;
  1097. }
  1098. static const struct drm_plane_funcs armada_primary_plane_funcs = {
  1099. .update_plane = armada_drm_primary_update,
  1100. .disable_plane = armada_drm_plane_disable,
  1101. .destroy = drm_primary_helper_destroy,
  1102. };
  1103. int armada_drm_plane_init(struct armada_plane *plane)
  1104. {
  1105. unsigned int i;
  1106. for (i = 0; i < ARRAY_SIZE(plane->works); i++)
  1107. plane->works[i].plane = &plane->base;
  1108. init_waitqueue_head(&plane->frame_wait);
  1109. return 0;
  1110. }
  1111. static const struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
  1112. { CSC_AUTO, "Auto" },
  1113. { CSC_YUV_CCIR601, "CCIR601" },
  1114. { CSC_YUV_CCIR709, "CCIR709" },
  1115. };
  1116. static const struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
  1117. { CSC_AUTO, "Auto" },
  1118. { CSC_RGB_COMPUTER, "Computer system" },
  1119. { CSC_RGB_STUDIO, "Studio" },
  1120. };
  1121. static int armada_drm_crtc_create_properties(struct drm_device *dev)
  1122. {
  1123. struct armada_private *priv = dev->dev_private;
  1124. if (priv->csc_yuv_prop)
  1125. return 0;
  1126. priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
  1127. "CSC_YUV", armada_drm_csc_yuv_enum_list,
  1128. ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
  1129. priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
  1130. "CSC_RGB", armada_drm_csc_rgb_enum_list,
  1131. ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
  1132. if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
  1133. return -ENOMEM;
  1134. return 0;
  1135. }
  1136. static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
  1137. struct resource *res, int irq, const struct armada_variant *variant,
  1138. struct device_node *port)
  1139. {
  1140. struct armada_private *priv = drm->dev_private;
  1141. struct armada_crtc *dcrtc;
  1142. struct armada_plane *primary;
  1143. void __iomem *base;
  1144. int ret;
  1145. ret = armada_drm_crtc_create_properties(drm);
  1146. if (ret)
  1147. return ret;
  1148. base = devm_ioremap_resource(dev, res);
  1149. if (IS_ERR(base))
  1150. return PTR_ERR(base);
  1151. dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
  1152. if (!dcrtc) {
  1153. DRM_ERROR("failed to allocate Armada crtc\n");
  1154. return -ENOMEM;
  1155. }
  1156. if (dev != drm->dev)
  1157. dev_set_drvdata(dev, dcrtc);
  1158. dcrtc->variant = variant;
  1159. dcrtc->base = base;
  1160. dcrtc->num = drm->mode_config.num_crtc;
  1161. dcrtc->clk = ERR_PTR(-EINVAL);
  1162. dcrtc->csc_yuv_mode = CSC_AUTO;
  1163. dcrtc->csc_rgb_mode = CSC_AUTO;
  1164. dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
  1165. dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
  1166. spin_lock_init(&dcrtc->irq_lock);
  1167. dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
  1168. /* Initialize some registers which we don't otherwise set */
  1169. writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
  1170. writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
  1171. writel_relaxed(dcrtc->spu_iopad_ctrl,
  1172. dcrtc->base + LCD_SPU_IOPAD_CONTROL);
  1173. writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
  1174. writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
  1175. CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
  1176. CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
  1177. writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
  1178. writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
  1179. writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
  1180. ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
  1181. dcrtc);
  1182. if (ret < 0)
  1183. goto err_crtc;
  1184. if (dcrtc->variant->init) {
  1185. ret = dcrtc->variant->init(dcrtc, dev);
  1186. if (ret)
  1187. goto err_crtc;
  1188. }
  1189. /* Ensure AXI pipeline is enabled */
  1190. armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
  1191. priv->dcrtc[dcrtc->num] = dcrtc;
  1192. dcrtc->crtc.port = port;
  1193. primary = kzalloc(sizeof(*primary), GFP_KERNEL);
  1194. if (!primary) {
  1195. ret = -ENOMEM;
  1196. goto err_crtc;
  1197. }
  1198. ret = armada_drm_plane_init(primary);
  1199. if (ret) {
  1200. kfree(primary);
  1201. goto err_crtc;
  1202. }
  1203. ret = drm_universal_plane_init(drm, &primary->base, 0,
  1204. &armada_primary_plane_funcs,
  1205. armada_primary_formats,
  1206. ARRAY_SIZE(armada_primary_formats),
  1207. NULL,
  1208. DRM_PLANE_TYPE_PRIMARY, NULL);
  1209. if (ret) {
  1210. kfree(primary);
  1211. goto err_crtc;
  1212. }
  1213. ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
  1214. &armada_crtc_funcs, NULL);
  1215. if (ret)
  1216. goto err_crtc_init;
  1217. drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
  1218. drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
  1219. dcrtc->csc_yuv_mode);
  1220. drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
  1221. dcrtc->csc_rgb_mode);
  1222. return armada_overlay_plane_create(drm, 1 << dcrtc->num);
  1223. err_crtc_init:
  1224. primary->base.funcs->destroy(&primary->base);
  1225. err_crtc:
  1226. kfree(dcrtc);
  1227. return ret;
  1228. }
  1229. static int
  1230. armada_lcd_bind(struct device *dev, struct device *master, void *data)
  1231. {
  1232. struct platform_device *pdev = to_platform_device(dev);
  1233. struct drm_device *drm = data;
  1234. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1235. int irq = platform_get_irq(pdev, 0);
  1236. const struct armada_variant *variant;
  1237. struct device_node *port = NULL;
  1238. if (irq < 0)
  1239. return irq;
  1240. if (!dev->of_node) {
  1241. const struct platform_device_id *id;
  1242. id = platform_get_device_id(pdev);
  1243. if (!id)
  1244. return -ENXIO;
  1245. variant = (const struct armada_variant *)id->driver_data;
  1246. } else {
  1247. const struct of_device_id *match;
  1248. struct device_node *np, *parent = dev->of_node;
  1249. match = of_match_device(dev->driver->of_match_table, dev);
  1250. if (!match)
  1251. return -ENXIO;
  1252. np = of_get_child_by_name(parent, "ports");
  1253. if (np)
  1254. parent = np;
  1255. port = of_get_child_by_name(parent, "port");
  1256. of_node_put(np);
  1257. if (!port) {
  1258. dev_err(dev, "no port node found in %pOF\n", parent);
  1259. return -ENXIO;
  1260. }
  1261. variant = match->data;
  1262. }
  1263. return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
  1264. }
  1265. static void
  1266. armada_lcd_unbind(struct device *dev, struct device *master, void *data)
  1267. {
  1268. struct armada_crtc *dcrtc = dev_get_drvdata(dev);
  1269. armada_drm_crtc_destroy(&dcrtc->crtc);
  1270. }
  1271. static const struct component_ops armada_lcd_ops = {
  1272. .bind = armada_lcd_bind,
  1273. .unbind = armada_lcd_unbind,
  1274. };
  1275. static int armada_lcd_probe(struct platform_device *pdev)
  1276. {
  1277. return component_add(&pdev->dev, &armada_lcd_ops);
  1278. }
  1279. static int armada_lcd_remove(struct platform_device *pdev)
  1280. {
  1281. component_del(&pdev->dev, &armada_lcd_ops);
  1282. return 0;
  1283. }
  1284. static const struct of_device_id armada_lcd_of_match[] = {
  1285. {
  1286. .compatible = "marvell,dove-lcd",
  1287. .data = &armada510_ops,
  1288. },
  1289. {}
  1290. };
  1291. MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
  1292. static const struct platform_device_id armada_lcd_platform_ids[] = {
  1293. {
  1294. .name = "armada-lcd",
  1295. .driver_data = (unsigned long)&armada510_ops,
  1296. }, {
  1297. .name = "armada-510-lcd",
  1298. .driver_data = (unsigned long)&armada510_ops,
  1299. },
  1300. { },
  1301. };
  1302. MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
  1303. struct platform_driver armada_lcd_platform_driver = {
  1304. .probe = armada_lcd_probe,
  1305. .remove = armada_lcd_remove,
  1306. .driver = {
  1307. .name = "armada-lcd",
  1308. .owner = THIS_MODULE,
  1309. .of_match_table = armada_lcd_of_match,
  1310. },
  1311. .id_table = armada_lcd_platform_ids,
  1312. };