armada_crtc.c 34 KB

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