armada_crtc.c 37 KB

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