armada_crtc.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239
  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. static void armada_drm_crtc_load_lut(struct drm_crtc *crtc)
  544. {
  545. }
  546. /* The mode_config.mutex will be held for this call */
  547. static void armada_drm_crtc_disable(struct drm_crtc *crtc)
  548. {
  549. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  550. armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
  551. armada_drm_crtc_finish_fb(dcrtc, crtc->primary->fb, true);
  552. /* Power down most RAMs and FIFOs */
  553. writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
  554. CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
  555. CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
  556. }
  557. static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
  558. .dpms = armada_drm_crtc_dpms,
  559. .prepare = armada_drm_crtc_prepare,
  560. .commit = armada_drm_crtc_commit,
  561. .mode_fixup = armada_drm_crtc_mode_fixup,
  562. .mode_set = armada_drm_crtc_mode_set,
  563. .mode_set_base = armada_drm_crtc_mode_set_base,
  564. .load_lut = armada_drm_crtc_load_lut,
  565. .disable = armada_drm_crtc_disable,
  566. };
  567. static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
  568. unsigned stride, unsigned width, unsigned height)
  569. {
  570. uint32_t addr;
  571. unsigned y;
  572. addr = SRAM_HWC32_RAM1;
  573. for (y = 0; y < height; y++) {
  574. uint32_t *p = &pix[y * stride];
  575. unsigned x;
  576. for (x = 0; x < width; x++, p++) {
  577. uint32_t val = *p;
  578. val = (val & 0xff00ff00) |
  579. (val & 0x000000ff) << 16 |
  580. (val & 0x00ff0000) >> 16;
  581. writel_relaxed(val,
  582. base + LCD_SPU_SRAM_WRDAT);
  583. writel_relaxed(addr | SRAM_WRITE,
  584. base + LCD_SPU_SRAM_CTRL);
  585. readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
  586. addr += 1;
  587. if ((addr & 0x00ff) == 0)
  588. addr += 0xf00;
  589. if ((addr & 0x30ff) == 0)
  590. addr = SRAM_HWC32_RAM2;
  591. }
  592. }
  593. }
  594. static void armada_drm_crtc_cursor_tran(void __iomem *base)
  595. {
  596. unsigned addr;
  597. for (addr = 0; addr < 256; addr++) {
  598. /* write the default value */
  599. writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
  600. writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
  601. base + LCD_SPU_SRAM_CTRL);
  602. }
  603. }
  604. static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
  605. {
  606. uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
  607. uint32_t yoff, yscr, h = dcrtc->cursor_h;
  608. uint32_t para1;
  609. /*
  610. * Calculate the visible width and height of the cursor,
  611. * screen position, and the position in the cursor bitmap.
  612. */
  613. if (dcrtc->cursor_x < 0) {
  614. xoff = -dcrtc->cursor_x;
  615. xscr = 0;
  616. w -= min(xoff, w);
  617. } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
  618. xoff = 0;
  619. xscr = dcrtc->cursor_x;
  620. w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
  621. } else {
  622. xoff = 0;
  623. xscr = dcrtc->cursor_x;
  624. }
  625. if (dcrtc->cursor_y < 0) {
  626. yoff = -dcrtc->cursor_y;
  627. yscr = 0;
  628. h -= min(yoff, h);
  629. } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
  630. yoff = 0;
  631. yscr = dcrtc->cursor_y;
  632. h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
  633. } else {
  634. yoff = 0;
  635. yscr = dcrtc->cursor_y;
  636. }
  637. /* On interlaced modes, the vertical cursor size must be halved */
  638. s = dcrtc->cursor_w;
  639. if (dcrtc->interlaced) {
  640. s *= 2;
  641. yscr /= 2;
  642. h /= 2;
  643. }
  644. if (!dcrtc->cursor_obj || !h || !w) {
  645. spin_lock_irq(&dcrtc->irq_lock);
  646. armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  647. dcrtc->cursor_update = false;
  648. armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
  649. spin_unlock_irq(&dcrtc->irq_lock);
  650. return 0;
  651. }
  652. para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
  653. armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
  654. dcrtc->base + LCD_SPU_SRAM_PARA1);
  655. /*
  656. * Initialize the transparency if the SRAM was powered down.
  657. * We must also reload the cursor data as well.
  658. */
  659. if (!(para1 & CFG_CSB_256x32)) {
  660. armada_drm_crtc_cursor_tran(dcrtc->base);
  661. reload = true;
  662. }
  663. if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
  664. spin_lock_irq(&dcrtc->irq_lock);
  665. armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  666. dcrtc->cursor_update = false;
  667. armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
  668. spin_unlock_irq(&dcrtc->irq_lock);
  669. reload = true;
  670. }
  671. if (reload) {
  672. struct armada_gem_object *obj = dcrtc->cursor_obj;
  673. uint32_t *pix;
  674. /* Set the top-left corner of the cursor image */
  675. pix = obj->addr;
  676. pix += yoff * s + xoff;
  677. armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
  678. }
  679. /* Reload the cursor position, size and enable in the IRQ handler */
  680. spin_lock_irq(&dcrtc->irq_lock);
  681. dcrtc->cursor_hw_pos = yscr << 16 | xscr;
  682. dcrtc->cursor_hw_sz = h << 16 | w;
  683. dcrtc->cursor_update = true;
  684. armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  685. spin_unlock_irq(&dcrtc->irq_lock);
  686. return 0;
  687. }
  688. static void cursor_update(void *data)
  689. {
  690. armada_drm_crtc_cursor_update(data, true);
  691. }
  692. static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
  693. struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
  694. {
  695. struct drm_device *dev = crtc->dev;
  696. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  697. struct armada_gem_object *obj = NULL;
  698. int ret;
  699. /* If no cursor support, replicate drm's return value */
  700. if (!dcrtc->variant->has_spu_adv_reg)
  701. return -ENXIO;
  702. if (handle && w > 0 && h > 0) {
  703. /* maximum size is 64x32 or 32x64 */
  704. if (w > 64 || h > 64 || (w > 32 && h > 32))
  705. return -ENOMEM;
  706. obj = armada_gem_object_lookup(dev, file, handle);
  707. if (!obj)
  708. return -ENOENT;
  709. /* Must be a kernel-mapped object */
  710. if (!obj->addr) {
  711. drm_gem_object_unreference_unlocked(&obj->obj);
  712. return -EINVAL;
  713. }
  714. if (obj->obj.size < w * h * 4) {
  715. DRM_ERROR("buffer is too small\n");
  716. drm_gem_object_unreference_unlocked(&obj->obj);
  717. return -ENOMEM;
  718. }
  719. }
  720. mutex_lock(&dev->struct_mutex);
  721. if (dcrtc->cursor_obj) {
  722. dcrtc->cursor_obj->update = NULL;
  723. dcrtc->cursor_obj->update_data = NULL;
  724. drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
  725. }
  726. dcrtc->cursor_obj = obj;
  727. dcrtc->cursor_w = w;
  728. dcrtc->cursor_h = h;
  729. ret = armada_drm_crtc_cursor_update(dcrtc, true);
  730. if (obj) {
  731. obj->update_data = dcrtc;
  732. obj->update = cursor_update;
  733. }
  734. mutex_unlock(&dev->struct_mutex);
  735. return ret;
  736. }
  737. static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  738. {
  739. struct drm_device *dev = crtc->dev;
  740. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  741. int ret;
  742. /* If no cursor support, replicate drm's return value */
  743. if (!dcrtc->variant->has_spu_adv_reg)
  744. return -EFAULT;
  745. mutex_lock(&dev->struct_mutex);
  746. dcrtc->cursor_x = x;
  747. dcrtc->cursor_y = y;
  748. ret = armada_drm_crtc_cursor_update(dcrtc, false);
  749. mutex_unlock(&dev->struct_mutex);
  750. return ret;
  751. }
  752. static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
  753. {
  754. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  755. struct armada_private *priv = crtc->dev->dev_private;
  756. if (dcrtc->cursor_obj)
  757. drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
  758. priv->dcrtc[dcrtc->num] = NULL;
  759. drm_crtc_cleanup(&dcrtc->crtc);
  760. if (!IS_ERR(dcrtc->clk))
  761. clk_disable_unprepare(dcrtc->clk);
  762. writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
  763. of_node_put(dcrtc->crtc.port);
  764. kfree(dcrtc);
  765. }
  766. /*
  767. * The mode_config lock is held here, to prevent races between this
  768. * and a mode_set.
  769. */
  770. static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
  771. struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
  772. {
  773. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  774. struct armada_frame_work *work;
  775. struct drm_device *dev = crtc->dev;
  776. unsigned long flags;
  777. unsigned i;
  778. int ret;
  779. /* We don't support changing the pixel format */
  780. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  781. return -EINVAL;
  782. work = kmalloc(sizeof(*work), GFP_KERNEL);
  783. if (!work)
  784. return -ENOMEM;
  785. work->event = event;
  786. work->old_fb = dcrtc->crtc.primary->fb;
  787. i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
  788. dcrtc->interlaced);
  789. armada_reg_queue_end(work->regs, i);
  790. /*
  791. * Ensure that we hold a reference on the new framebuffer.
  792. * This has to match the behaviour in mode_set.
  793. */
  794. drm_framebuffer_reference(fb);
  795. ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
  796. if (ret) {
  797. /* Undo our reference above */
  798. drm_framebuffer_unreference(fb);
  799. kfree(work);
  800. return ret;
  801. }
  802. /*
  803. * Don't take a reference on the new framebuffer;
  804. * drm_mode_page_flip_ioctl() has already grabbed a reference and
  805. * will _not_ drop that reference on successful return from this
  806. * function. Simply mark this new framebuffer as the current one.
  807. */
  808. dcrtc->crtc.primary->fb = fb;
  809. /*
  810. * Finally, if the display is blanked, we won't receive an
  811. * interrupt, so complete it now.
  812. */
  813. if (dpms_blanked(dcrtc->dpms)) {
  814. spin_lock_irqsave(&dev->event_lock, flags);
  815. if (dcrtc->frame_work)
  816. armada_drm_crtc_complete_frame_work(dcrtc);
  817. spin_unlock_irqrestore(&dev->event_lock, flags);
  818. }
  819. return 0;
  820. }
  821. static int
  822. armada_drm_crtc_set_property(struct drm_crtc *crtc,
  823. struct drm_property *property, uint64_t val)
  824. {
  825. struct armada_private *priv = crtc->dev->dev_private;
  826. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  827. bool update_csc = false;
  828. if (property == priv->csc_yuv_prop) {
  829. dcrtc->csc_yuv_mode = val;
  830. update_csc = true;
  831. } else if (property == priv->csc_rgb_prop) {
  832. dcrtc->csc_rgb_mode = val;
  833. update_csc = true;
  834. }
  835. if (update_csc) {
  836. uint32_t val;
  837. val = dcrtc->spu_iopad_ctrl |
  838. armada_drm_crtc_calculate_csc(dcrtc);
  839. writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
  840. }
  841. return 0;
  842. }
  843. static struct drm_crtc_funcs armada_crtc_funcs = {
  844. .cursor_set = armada_drm_crtc_cursor_set,
  845. .cursor_move = armada_drm_crtc_cursor_move,
  846. .destroy = armada_drm_crtc_destroy,
  847. .set_config = drm_crtc_helper_set_config,
  848. .page_flip = armada_drm_crtc_page_flip,
  849. .set_property = armada_drm_crtc_set_property,
  850. };
  851. static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
  852. { CSC_AUTO, "Auto" },
  853. { CSC_YUV_CCIR601, "CCIR601" },
  854. { CSC_YUV_CCIR709, "CCIR709" },
  855. };
  856. static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
  857. { CSC_AUTO, "Auto" },
  858. { CSC_RGB_COMPUTER, "Computer system" },
  859. { CSC_RGB_STUDIO, "Studio" },
  860. };
  861. static int armada_drm_crtc_create_properties(struct drm_device *dev)
  862. {
  863. struct armada_private *priv = dev->dev_private;
  864. if (priv->csc_yuv_prop)
  865. return 0;
  866. priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
  867. "CSC_YUV", armada_drm_csc_yuv_enum_list,
  868. ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
  869. priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
  870. "CSC_RGB", armada_drm_csc_rgb_enum_list,
  871. ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
  872. if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
  873. return -ENOMEM;
  874. return 0;
  875. }
  876. int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
  877. struct resource *res, int irq, const struct armada_variant *variant,
  878. struct device_node *port)
  879. {
  880. struct armada_private *priv = drm->dev_private;
  881. struct armada_crtc *dcrtc;
  882. void __iomem *base;
  883. int ret;
  884. ret = armada_drm_crtc_create_properties(drm);
  885. if (ret)
  886. return ret;
  887. base = devm_ioremap_resource(dev, res);
  888. if (IS_ERR(base))
  889. return PTR_ERR(base);
  890. dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
  891. if (!dcrtc) {
  892. DRM_ERROR("failed to allocate Armada crtc\n");
  893. return -ENOMEM;
  894. }
  895. if (dev != drm->dev)
  896. dev_set_drvdata(dev, dcrtc);
  897. dcrtc->variant = variant;
  898. dcrtc->base = base;
  899. dcrtc->num = drm->mode_config.num_crtc;
  900. dcrtc->clk = ERR_PTR(-EINVAL);
  901. dcrtc->csc_yuv_mode = CSC_AUTO;
  902. dcrtc->csc_rgb_mode = CSC_AUTO;
  903. dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
  904. dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
  905. spin_lock_init(&dcrtc->irq_lock);
  906. dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
  907. INIT_LIST_HEAD(&dcrtc->vbl_list);
  908. init_waitqueue_head(&dcrtc->frame_wait);
  909. /* Initialize some registers which we don't otherwise set */
  910. writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
  911. writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
  912. writel_relaxed(dcrtc->spu_iopad_ctrl,
  913. dcrtc->base + LCD_SPU_IOPAD_CONTROL);
  914. writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
  915. writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
  916. CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
  917. CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
  918. writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
  919. writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
  920. writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
  921. writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
  922. ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
  923. dcrtc);
  924. if (ret < 0) {
  925. kfree(dcrtc);
  926. return ret;
  927. }
  928. if (dcrtc->variant->init) {
  929. ret = dcrtc->variant->init(dcrtc, dev);
  930. if (ret) {
  931. kfree(dcrtc);
  932. return ret;
  933. }
  934. }
  935. /* Ensure AXI pipeline is enabled */
  936. armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
  937. priv->dcrtc[dcrtc->num] = dcrtc;
  938. dcrtc->crtc.port = port;
  939. drm_crtc_init(drm, &dcrtc->crtc, &armada_crtc_funcs);
  940. drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
  941. drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
  942. dcrtc->csc_yuv_mode);
  943. drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
  944. dcrtc->csc_rgb_mode);
  945. return armada_overlay_plane_create(drm, 1 << dcrtc->num);
  946. }
  947. static int
  948. armada_lcd_bind(struct device *dev, struct device *master, void *data)
  949. {
  950. struct platform_device *pdev = to_platform_device(dev);
  951. struct drm_device *drm = data;
  952. struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  953. int irq = platform_get_irq(pdev, 0);
  954. const struct armada_variant *variant;
  955. struct device_node *port = NULL;
  956. if (irq < 0)
  957. return irq;
  958. if (!dev->of_node) {
  959. const struct platform_device_id *id;
  960. id = platform_get_device_id(pdev);
  961. if (!id)
  962. return -ENXIO;
  963. variant = (const struct armada_variant *)id->driver_data;
  964. } else {
  965. const struct of_device_id *match;
  966. struct device_node *np, *parent = dev->of_node;
  967. match = of_match_device(dev->driver->of_match_table, dev);
  968. if (!match)
  969. return -ENXIO;
  970. np = of_get_child_by_name(parent, "ports");
  971. if (np)
  972. parent = np;
  973. port = of_get_child_by_name(parent, "port");
  974. of_node_put(np);
  975. if (!port) {
  976. dev_err(dev, "no port node found in %s\n",
  977. parent->full_name);
  978. return -ENXIO;
  979. }
  980. variant = match->data;
  981. }
  982. return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
  983. }
  984. static void
  985. armada_lcd_unbind(struct device *dev, struct device *master, void *data)
  986. {
  987. struct armada_crtc *dcrtc = dev_get_drvdata(dev);
  988. armada_drm_crtc_destroy(&dcrtc->crtc);
  989. }
  990. static const struct component_ops armada_lcd_ops = {
  991. .bind = armada_lcd_bind,
  992. .unbind = armada_lcd_unbind,
  993. };
  994. static int armada_lcd_probe(struct platform_device *pdev)
  995. {
  996. return component_add(&pdev->dev, &armada_lcd_ops);
  997. }
  998. static int armada_lcd_remove(struct platform_device *pdev)
  999. {
  1000. component_del(&pdev->dev, &armada_lcd_ops);
  1001. return 0;
  1002. }
  1003. static struct of_device_id armada_lcd_of_match[] = {
  1004. {
  1005. .compatible = "marvell,dove-lcd",
  1006. .data = &armada510_ops,
  1007. },
  1008. {}
  1009. };
  1010. MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
  1011. static const struct platform_device_id armada_lcd_platform_ids[] = {
  1012. {
  1013. .name = "armada-lcd",
  1014. .driver_data = (unsigned long)&armada510_ops,
  1015. }, {
  1016. .name = "armada-510-lcd",
  1017. .driver_data = (unsigned long)&armada510_ops,
  1018. },
  1019. { },
  1020. };
  1021. MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
  1022. struct platform_driver armada_lcd_platform_driver = {
  1023. .probe = armada_lcd_probe,
  1024. .remove = armada_lcd_remove,
  1025. .driver = {
  1026. .name = "armada-lcd",
  1027. .owner = THIS_MODULE,
  1028. .of_match_table = armada_lcd_of_match,
  1029. },
  1030. .id_table = armada_lcd_platform_ids,
  1031. };