armada_crtc.c 39 KB

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