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 "armada_crtc.h"
  16. #include "armada_drm.h"
  17. #include "armada_fb.h"
  18. #include "armada_gem.h"
  19. #include "armada_hw.h"
  20. struct armada_frame_work {
  21. struct drm_pending_vblank_event *event;
  22. struct armada_regs regs[4];
  23. struct drm_framebuffer *old_fb;
  24. };
  25. enum csc_mode {
  26. CSC_AUTO = 0,
  27. CSC_YUV_CCIR601 = 1,
  28. CSC_YUV_CCIR709 = 2,
  29. CSC_RGB_COMPUTER = 1,
  30. CSC_RGB_STUDIO = 2,
  31. };
  32. /*
  33. * A note about interlacing. Let's consider HDMI 1920x1080i.
  34. * The timing parameters we have from X are:
  35. * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
  36. * 1920 2448 2492 2640 1080 1084 1094 1125
  37. * Which get translated to:
  38. * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
  39. * 1920 2448 2492 2640 540 542 547 562
  40. *
  41. * This is how it is defined by CEA-861-D - line and pixel numbers are
  42. * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
  43. * line: 2640. The odd frame, the first active line is at line 21, and
  44. * the even frame, the first active line is 584.
  45. *
  46. * LN: 560 561 562 563 567 568 569
  47. * DE: ~~~|____________________________//__________________________
  48. * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
  49. * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
  50. * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
  51. *
  52. * LN: 1123 1124 1125 1 5 6 7
  53. * DE: ~~~|____________________________//__________________________
  54. * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
  55. * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
  56. * 23 blanking lines
  57. *
  58. * The Armada LCD Controller line and pixel numbers are, like X timings,
  59. * referenced to the top left of the active frame.
  60. *
  61. * So, translating these to our LCD controller:
  62. * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
  63. * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
  64. * Note: Vsync front porch remains constant!
  65. *
  66. * if (odd_frame) {
  67. * vtotal = mode->crtc_vtotal + 1;
  68. * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
  69. * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
  70. * } else {
  71. * vtotal = mode->crtc_vtotal;
  72. * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
  73. * vhorizpos = mode->crtc_hsync_start;
  74. * }
  75. * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
  76. *
  77. * So, we need to reprogram these registers on each vsync event:
  78. * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
  79. *
  80. * Note: we do not use the frame done interrupts because these appear
  81. * to happen too early, and lead to jitter on the display (presumably
  82. * they occur at the end of the last active line, before the vsync back
  83. * porch, which we're reprogramming.)
  84. */
  85. void
  86. armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
  87. {
  88. while (regs->offset != ~0) {
  89. void __iomem *reg = dcrtc->base + regs->offset;
  90. uint32_t val;
  91. val = regs->mask;
  92. if (val != 0)
  93. val &= readl_relaxed(reg);
  94. writel_relaxed(val | regs->val, reg);
  95. ++regs;
  96. }
  97. }
  98. #define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
  99. static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
  100. {
  101. uint32_t dumb_ctrl;
  102. dumb_ctrl = dcrtc->cfg_dumb_ctrl;
  103. if (!dpms_blanked(dcrtc->dpms))
  104. dumb_ctrl |= CFG_DUMB_ENA;
  105. /*
  106. * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
  107. * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
  108. * force LCD_D[23:0] to output blank color, overriding the GPIO or
  109. * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
  110. */
  111. if (dpms_blanked(dcrtc->dpms) &&
  112. (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
  113. dumb_ctrl &= ~DUMB_MASK;
  114. dumb_ctrl |= DUMB_BLANK;
  115. }
  116. /*
  117. * The documentation doesn't indicate what the normal state of
  118. * the sync signals are. Sebastian Hesselbart kindly probed
  119. * these signals on his board to determine their state.
  120. *
  121. * The non-inverted state of the sync signals is active high.
  122. * Setting these bits makes the appropriate signal active low.
  123. */
  124. if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
  125. dumb_ctrl |= CFG_INV_CSYNC;
  126. if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
  127. dumb_ctrl |= CFG_INV_HSYNC;
  128. if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
  129. dumb_ctrl |= CFG_INV_VSYNC;
  130. if (dcrtc->dumb_ctrl != dumb_ctrl) {
  131. dcrtc->dumb_ctrl = dumb_ctrl;
  132. writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
  133. }
  134. }
  135. static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
  136. int x, int y, struct armada_regs *regs, bool interlaced)
  137. {
  138. struct armada_gem_object *obj = drm_fb_obj(fb);
  139. unsigned pitch = fb->pitches[0];
  140. unsigned offset = y * pitch + x * fb->bits_per_pixel / 8;
  141. uint32_t addr_odd, addr_even;
  142. unsigned i = 0;
  143. DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
  144. pitch, x, y, fb->bits_per_pixel);
  145. addr_odd = addr_even = obj->dev_addr + offset;
  146. if (interlaced) {
  147. addr_even += pitch;
  148. pitch *= 2;
  149. }
  150. /* write offset, base, and pitch */
  151. armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
  152. armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
  153. armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
  154. return i;
  155. }
  156. static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
  157. struct armada_frame_work *work)
  158. {
  159. struct drm_device *dev = dcrtc->crtc.dev;
  160. unsigned long flags;
  161. int ret;
  162. ret = drm_vblank_get(dev, dcrtc->num);
  163. if (ret) {
  164. DRM_ERROR("failed to acquire vblank counter\n");
  165. return ret;
  166. }
  167. spin_lock_irqsave(&dev->event_lock, flags);
  168. if (!dcrtc->frame_work)
  169. dcrtc->frame_work = work;
  170. else
  171. ret = -EBUSY;
  172. spin_unlock_irqrestore(&dev->event_lock, flags);
  173. if (ret)
  174. drm_vblank_put(dev, dcrtc->num);
  175. return ret;
  176. }
  177. static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc)
  178. {
  179. struct drm_device *dev = dcrtc->crtc.dev;
  180. struct armada_frame_work *work = dcrtc->frame_work;
  181. dcrtc->frame_work = NULL;
  182. armada_drm_crtc_update_regs(dcrtc, work->regs);
  183. if (work->event)
  184. drm_send_vblank_event(dev, dcrtc->num, work->event);
  185. drm_vblank_put(dev, dcrtc->num);
  186. /* Finally, queue the process-half of the cleanup. */
  187. __armada_drm_queue_unref_work(dcrtc->crtc.dev, work->old_fb);
  188. kfree(work);
  189. }
  190. static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
  191. struct drm_framebuffer *fb, bool force)
  192. {
  193. struct armada_frame_work *work;
  194. if (!fb)
  195. return;
  196. if (force) {
  197. /* Display is disabled, so just drop the old fb */
  198. drm_framebuffer_unreference(fb);
  199. return;
  200. }
  201. work = kmalloc(sizeof(*work), GFP_KERNEL);
  202. if (work) {
  203. int i = 0;
  204. work->event = NULL;
  205. work->old_fb = fb;
  206. armada_reg_queue_end(work->regs, i);
  207. if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
  208. return;
  209. kfree(work);
  210. }
  211. /*
  212. * Oops - just drop the reference immediately and hope for
  213. * the best. The worst that will happen is the buffer gets
  214. * reused before it has finished being displayed.
  215. */
  216. drm_framebuffer_unreference(fb);
  217. }
  218. static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
  219. {
  220. struct drm_device *dev = dcrtc->crtc.dev;
  221. /*
  222. * Tell the DRM core that vblank IRQs aren't going to happen for
  223. * a while. This cleans up any pending vblank events for us.
  224. */
  225. drm_vblank_off(dev, dcrtc->num);
  226. /* Handle any pending flip event. */
  227. spin_lock_irq(&dev->event_lock);
  228. if (dcrtc->frame_work)
  229. armada_drm_crtc_complete_frame_work(dcrtc);
  230. spin_unlock_irq(&dev->event_lock);
  231. }
  232. void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
  233. int idx)
  234. {
  235. }
  236. void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
  237. int idx)
  238. {
  239. }
  240. /* The mode_config.mutex will be held for this call */
  241. static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
  242. {
  243. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  244. if (dcrtc->dpms != dpms) {
  245. dcrtc->dpms = dpms;
  246. armada_drm_crtc_update(dcrtc);
  247. if (dpms_blanked(dpms))
  248. armada_drm_vblank_off(dcrtc);
  249. }
  250. }
  251. /*
  252. * Prepare for a mode set. Turn off overlay to ensure that we don't end
  253. * up with the overlay size being bigger than the active screen size.
  254. * We rely upon X refreshing this state after the mode set has completed.
  255. *
  256. * The mode_config.mutex will be held for this call
  257. */
  258. static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
  259. {
  260. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  261. struct drm_plane *plane;
  262. /*
  263. * If we have an overlay plane associated with this CRTC, disable
  264. * it before the modeset to avoid its coordinates being outside
  265. * the new mode parameters. DRM doesn't provide help with this.
  266. */
  267. plane = dcrtc->plane;
  268. if (plane) {
  269. struct drm_framebuffer *fb = plane->fb;
  270. plane->funcs->disable_plane(plane);
  271. plane->fb = NULL;
  272. plane->crtc = NULL;
  273. drm_framebuffer_unreference(fb);
  274. }
  275. }
  276. /* The mode_config.mutex will be held for this call */
  277. static void armada_drm_crtc_commit(struct drm_crtc *crtc)
  278. {
  279. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  280. if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
  281. dcrtc->dpms = DRM_MODE_DPMS_ON;
  282. armada_drm_crtc_update(dcrtc);
  283. }
  284. }
  285. /* The mode_config.mutex will be held for this call */
  286. static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
  287. const struct drm_display_mode *mode, struct drm_display_mode *adj)
  288. {
  289. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  290. int ret;
  291. /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
  292. if (!dcrtc->variant->has_spu_adv_reg &&
  293. adj->flags & DRM_MODE_FLAG_INTERLACE)
  294. return false;
  295. /* Check whether the display mode is possible */
  296. ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
  297. if (ret)
  298. return false;
  299. return true;
  300. }
  301. static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
  302. {
  303. struct armada_vbl_event *e, *n;
  304. void __iomem *base = dcrtc->base;
  305. if (stat & DMA_FF_UNDERFLOW)
  306. DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
  307. if (stat & GRA_FF_UNDERFLOW)
  308. DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
  309. if (stat & VSYNC_IRQ)
  310. drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num);
  311. spin_lock(&dcrtc->irq_lock);
  312. list_for_each_entry_safe(e, n, &dcrtc->vbl_list, node) {
  313. list_del_init(&e->node);
  314. drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
  315. e->fn(dcrtc, e->data);
  316. }
  317. if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
  318. int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
  319. uint32_t val;
  320. writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
  321. writel_relaxed(dcrtc->v[i].spu_v_h_total,
  322. base + LCD_SPUT_V_H_TOTAL);
  323. val = readl_relaxed(base + LCD_SPU_ADV_REG);
  324. val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
  325. val |= dcrtc->v[i].spu_adv_reg;
  326. writel_relaxed(val, base + LCD_SPU_ADV_REG);
  327. }
  328. if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
  329. writel_relaxed(dcrtc->cursor_hw_pos,
  330. base + LCD_SPU_HWC_OVSA_HPXL_VLN);
  331. writel_relaxed(dcrtc->cursor_hw_sz,
  332. base + LCD_SPU_HWC_HPXL_VLN);
  333. armada_updatel(CFG_HWC_ENA,
  334. CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
  335. base + LCD_SPU_DMA_CTRL0);
  336. dcrtc->cursor_update = false;
  337. armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  338. }
  339. spin_unlock(&dcrtc->irq_lock);
  340. if (stat & GRA_FRAME_IRQ) {
  341. struct drm_device *dev = dcrtc->crtc.dev;
  342. spin_lock(&dev->event_lock);
  343. if (dcrtc->frame_work)
  344. armada_drm_crtc_complete_frame_work(dcrtc);
  345. spin_unlock(&dev->event_lock);
  346. wake_up(&dcrtc->frame_wait);
  347. }
  348. }
  349. static irqreturn_t armada_drm_irq(int irq, void *arg)
  350. {
  351. struct armada_crtc *dcrtc = arg;
  352. u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
  353. /*
  354. * This is rediculous - rather than writing bits to clear, we
  355. * have to set the actual status register value. This is racy.
  356. */
  357. writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
  358. /* Mask out those interrupts we haven't enabled */
  359. v = stat & dcrtc->irq_ena;
  360. if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
  361. armada_drm_crtc_irq(dcrtc, stat);
  362. return IRQ_HANDLED;
  363. }
  364. return IRQ_NONE;
  365. }
  366. /* These are locked by dev->vbl_lock */
  367. void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
  368. {
  369. if (dcrtc->irq_ena & mask) {
  370. dcrtc->irq_ena &= ~mask;
  371. writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
  372. }
  373. }
  374. void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
  375. {
  376. if ((dcrtc->irq_ena & mask) != mask) {
  377. dcrtc->irq_ena |= mask;
  378. writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
  379. if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
  380. writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
  381. }
  382. }
  383. static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
  384. {
  385. struct drm_display_mode *adj = &dcrtc->crtc.mode;
  386. uint32_t val = 0;
  387. if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
  388. val |= CFG_CSC_YUV_CCIR709;
  389. if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
  390. val |= CFG_CSC_RGB_STUDIO;
  391. /*
  392. * In auto mode, set the colorimetry, based upon the HDMI spec.
  393. * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
  394. * ITU601. It may be more appropriate to set this depending on
  395. * the source - but what if the graphic frame is YUV and the
  396. * video frame is RGB?
  397. */
  398. if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
  399. !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
  400. (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
  401. if (dcrtc->csc_yuv_mode == CSC_AUTO)
  402. val |= CFG_CSC_YUV_CCIR709;
  403. }
  404. /*
  405. * We assume we're connected to a TV-like device, so the YUV->RGB
  406. * conversion should produce a limited range. We should set this
  407. * depending on the connectors attached to this CRTC, and what
  408. * kind of device they report being connected.
  409. */
  410. if (dcrtc->csc_rgb_mode == CSC_AUTO)
  411. val |= CFG_CSC_RGB_STUDIO;
  412. return val;
  413. }
  414. /* The mode_config.mutex will be held for this call */
  415. static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
  416. struct drm_display_mode *mode, struct drm_display_mode *adj,
  417. int x, int y, struct drm_framebuffer *old_fb)
  418. {
  419. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  420. struct armada_regs regs[17];
  421. uint32_t lm, rm, tm, bm, val, sclk;
  422. unsigned long flags;
  423. unsigned i;
  424. bool interlaced;
  425. drm_framebuffer_reference(crtc->primary->fb);
  426. interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
  427. i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
  428. x, y, regs, interlaced);
  429. rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
  430. lm = adj->crtc_htotal - adj->crtc_hsync_end;
  431. bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
  432. tm = adj->crtc_vtotal - adj->crtc_vsync_end;
  433. DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
  434. adj->crtc_hdisplay,
  435. adj->crtc_hsync_start,
  436. adj->crtc_hsync_end,
  437. adj->crtc_htotal, lm, rm);
  438. DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
  439. adj->crtc_vdisplay,
  440. adj->crtc_vsync_start,
  441. adj->crtc_vsync_end,
  442. adj->crtc_vtotal, tm, bm);
  443. /* Wait for pending flips to complete */
  444. wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
  445. drm_vblank_pre_modeset(crtc->dev, dcrtc->num);
  446. crtc->mode = *adj;
  447. val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
  448. if (val != dcrtc->dumb_ctrl) {
  449. dcrtc->dumb_ctrl = val;
  450. writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
  451. }
  452. /* Now compute the divider for real */
  453. dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
  454. /* Ensure graphic fifo is enabled */
  455. armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
  456. armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
  457. if (interlaced ^ dcrtc->interlaced) {
  458. if (adj->flags & DRM_MODE_FLAG_INTERLACE)
  459. drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
  460. else
  461. drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
  462. dcrtc->interlaced = interlaced;
  463. }
  464. spin_lock_irqsave(&dcrtc->irq_lock, flags);
  465. /* Even interlaced/progressive frame */
  466. dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
  467. adj->crtc_htotal;
  468. dcrtc->v[1].spu_v_porch = tm << 16 | bm;
  469. val = adj->crtc_hsync_start;
  470. dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
  471. dcrtc->variant->spu_adv_reg;
  472. if (interlaced) {
  473. /* Odd interlaced frame */
  474. dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
  475. (1 << 16);
  476. dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
  477. val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
  478. dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
  479. dcrtc->variant->spu_adv_reg;
  480. } else {
  481. dcrtc->v[0] = dcrtc->v[1];
  482. }
  483. val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
  484. armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
  485. armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN);
  486. armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN);
  487. armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
  488. armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
  489. armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
  490. LCD_SPUT_V_H_TOTAL);
  491. if (dcrtc->variant->has_spu_adv_reg) {
  492. armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
  493. ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
  494. ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
  495. }
  496. val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
  497. val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
  498. val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
  499. if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
  500. val |= CFG_PALETTE_ENA;
  501. if (interlaced)
  502. val |= CFG_GRA_FTOGGLE;
  503. armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
  504. CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
  505. CFG_SWAPYU | CFG_YUV2RGB) |
  506. CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
  507. LCD_SPU_DMA_CTRL0);
  508. val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
  509. armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
  510. val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
  511. armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
  512. armada_reg_queue_end(regs, i);
  513. armada_drm_crtc_update_regs(dcrtc, regs);
  514. spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
  515. armada_drm_crtc_update(dcrtc);
  516. drm_vblank_post_modeset(crtc->dev, dcrtc->num);
  517. armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
  518. return 0;
  519. }
  520. /* The mode_config.mutex will be held for this call */
  521. static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
  522. struct drm_framebuffer *old_fb)
  523. {
  524. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  525. struct armada_regs regs[4];
  526. unsigned i;
  527. i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
  528. dcrtc->interlaced);
  529. armada_reg_queue_end(regs, i);
  530. /* Wait for pending flips to complete */
  531. wait_event(dcrtc->frame_wait, !dcrtc->frame_work);
  532. /* Take a reference to the new fb as we're using it */
  533. drm_framebuffer_reference(crtc->primary->fb);
  534. /* Update the base in the CRTC */
  535. armada_drm_crtc_update_regs(dcrtc, regs);
  536. /* Drop our previously held reference */
  537. armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
  538. return 0;
  539. }
  540. static void armada_drm_crtc_load_lut(struct drm_crtc *crtc)
  541. {
  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. .load_lut = armada_drm_crtc_load_lut,
  562. .disable = armada_drm_crtc_disable,
  563. };
  564. static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
  565. unsigned stride, unsigned width, unsigned height)
  566. {
  567. uint32_t addr;
  568. unsigned y;
  569. addr = SRAM_HWC32_RAM1;
  570. for (y = 0; y < height; y++) {
  571. uint32_t *p = &pix[y * stride];
  572. unsigned x;
  573. for (x = 0; x < width; x++, p++) {
  574. uint32_t val = *p;
  575. val = (val & 0xff00ff00) |
  576. (val & 0x000000ff) << 16 |
  577. (val & 0x00ff0000) >> 16;
  578. writel_relaxed(val,
  579. base + LCD_SPU_SRAM_WRDAT);
  580. writel_relaxed(addr | SRAM_WRITE,
  581. base + LCD_SPU_SRAM_CTRL);
  582. readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
  583. addr += 1;
  584. if ((addr & 0x00ff) == 0)
  585. addr += 0xf00;
  586. if ((addr & 0x30ff) == 0)
  587. addr = SRAM_HWC32_RAM2;
  588. }
  589. }
  590. }
  591. static void armada_drm_crtc_cursor_tran(void __iomem *base)
  592. {
  593. unsigned addr;
  594. for (addr = 0; addr < 256; addr++) {
  595. /* write the default value */
  596. writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
  597. writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
  598. base + LCD_SPU_SRAM_CTRL);
  599. }
  600. }
  601. static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
  602. {
  603. uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
  604. uint32_t yoff, yscr, h = dcrtc->cursor_h;
  605. uint32_t para1;
  606. /*
  607. * Calculate the visible width and height of the cursor,
  608. * screen position, and the position in the cursor bitmap.
  609. */
  610. if (dcrtc->cursor_x < 0) {
  611. xoff = -dcrtc->cursor_x;
  612. xscr = 0;
  613. w -= min(xoff, w);
  614. } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
  615. xoff = 0;
  616. xscr = dcrtc->cursor_x;
  617. w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
  618. } else {
  619. xoff = 0;
  620. xscr = dcrtc->cursor_x;
  621. }
  622. if (dcrtc->cursor_y < 0) {
  623. yoff = -dcrtc->cursor_y;
  624. yscr = 0;
  625. h -= min(yoff, h);
  626. } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
  627. yoff = 0;
  628. yscr = dcrtc->cursor_y;
  629. h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
  630. } else {
  631. yoff = 0;
  632. yscr = dcrtc->cursor_y;
  633. }
  634. /* On interlaced modes, the vertical cursor size must be halved */
  635. s = dcrtc->cursor_w;
  636. if (dcrtc->interlaced) {
  637. s *= 2;
  638. yscr /= 2;
  639. h /= 2;
  640. }
  641. if (!dcrtc->cursor_obj || !h || !w) {
  642. spin_lock_irq(&dcrtc->irq_lock);
  643. armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  644. dcrtc->cursor_update = false;
  645. armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
  646. spin_unlock_irq(&dcrtc->irq_lock);
  647. return 0;
  648. }
  649. para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
  650. armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
  651. dcrtc->base + LCD_SPU_SRAM_PARA1);
  652. /*
  653. * Initialize the transparency if the SRAM was powered down.
  654. * We must also reload the cursor data as well.
  655. */
  656. if (!(para1 & CFG_CSB_256x32)) {
  657. armada_drm_crtc_cursor_tran(dcrtc->base);
  658. reload = true;
  659. }
  660. if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
  661. spin_lock_irq(&dcrtc->irq_lock);
  662. armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  663. dcrtc->cursor_update = false;
  664. armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
  665. spin_unlock_irq(&dcrtc->irq_lock);
  666. reload = true;
  667. }
  668. if (reload) {
  669. struct armada_gem_object *obj = dcrtc->cursor_obj;
  670. uint32_t *pix;
  671. /* Set the top-left corner of the cursor image */
  672. pix = obj->addr;
  673. pix += yoff * s + xoff;
  674. armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
  675. }
  676. /* Reload the cursor position, size and enable in the IRQ handler */
  677. spin_lock_irq(&dcrtc->irq_lock);
  678. dcrtc->cursor_hw_pos = yscr << 16 | xscr;
  679. dcrtc->cursor_hw_sz = h << 16 | w;
  680. dcrtc->cursor_update = true;
  681. armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
  682. spin_unlock_irq(&dcrtc->irq_lock);
  683. return 0;
  684. }
  685. static void cursor_update(void *data)
  686. {
  687. armada_drm_crtc_cursor_update(data, true);
  688. }
  689. static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
  690. struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
  691. {
  692. struct drm_device *dev = crtc->dev;
  693. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  694. struct armada_gem_object *obj = NULL;
  695. int ret;
  696. /* If no cursor support, replicate drm's return value */
  697. if (!dcrtc->variant->has_spu_adv_reg)
  698. return -ENXIO;
  699. if (handle && w > 0 && h > 0) {
  700. /* maximum size is 64x32 or 32x64 */
  701. if (w > 64 || h > 64 || (w > 32 && h > 32))
  702. return -ENOMEM;
  703. obj = armada_gem_object_lookup(dev, file, handle);
  704. if (!obj)
  705. return -ENOENT;
  706. /* Must be a kernel-mapped object */
  707. if (!obj->addr) {
  708. drm_gem_object_unreference_unlocked(&obj->obj);
  709. return -EINVAL;
  710. }
  711. if (obj->obj.size < w * h * 4) {
  712. DRM_ERROR("buffer is too small\n");
  713. drm_gem_object_unreference_unlocked(&obj->obj);
  714. return -ENOMEM;
  715. }
  716. }
  717. mutex_lock(&dev->struct_mutex);
  718. if (dcrtc->cursor_obj) {
  719. dcrtc->cursor_obj->update = NULL;
  720. dcrtc->cursor_obj->update_data = NULL;
  721. drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
  722. }
  723. dcrtc->cursor_obj = obj;
  724. dcrtc->cursor_w = w;
  725. dcrtc->cursor_h = h;
  726. ret = armada_drm_crtc_cursor_update(dcrtc, true);
  727. if (obj) {
  728. obj->update_data = dcrtc;
  729. obj->update = cursor_update;
  730. }
  731. mutex_unlock(&dev->struct_mutex);
  732. return ret;
  733. }
  734. static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
  735. {
  736. struct drm_device *dev = crtc->dev;
  737. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  738. int ret;
  739. /* If no cursor support, replicate drm's return value */
  740. if (!dcrtc->variant->has_spu_adv_reg)
  741. return -EFAULT;
  742. mutex_lock(&dev->struct_mutex);
  743. dcrtc->cursor_x = x;
  744. dcrtc->cursor_y = y;
  745. ret = armada_drm_crtc_cursor_update(dcrtc, false);
  746. mutex_unlock(&dev->struct_mutex);
  747. return ret;
  748. }
  749. static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
  750. {
  751. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  752. struct armada_private *priv = crtc->dev->dev_private;
  753. if (dcrtc->cursor_obj)
  754. drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
  755. priv->dcrtc[dcrtc->num] = NULL;
  756. drm_crtc_cleanup(&dcrtc->crtc);
  757. if (!IS_ERR(dcrtc->clk))
  758. clk_disable_unprepare(dcrtc->clk);
  759. writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
  760. of_node_put(dcrtc->crtc.port);
  761. kfree(dcrtc);
  762. }
  763. /*
  764. * The mode_config lock is held here, to prevent races between this
  765. * and a mode_set.
  766. */
  767. static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
  768. struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
  769. {
  770. struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
  771. struct armada_frame_work *work;
  772. struct drm_device *dev = crtc->dev;
  773. unsigned long flags;
  774. unsigned i;
  775. int ret;
  776. /* We don't support changing the pixel format */
  777. if (fb->pixel_format != crtc->primary->fb->pixel_format)
  778. return -EINVAL;
  779. work = kmalloc(sizeof(*work), GFP_KERNEL);
  780. if (!work)
  781. return -ENOMEM;
  782. work->event = event;
  783. work->old_fb = dcrtc->crtc.primary->fb;
  784. i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
  785. dcrtc->interlaced);
  786. armada_reg_queue_end(work->regs, i);
  787. /*
  788. * Hold the old framebuffer for the work - DRM appears to drop our
  789. * reference to the old framebuffer in drm_mode_page_flip_ioctl().
  790. */
  791. drm_framebuffer_reference(work->old_fb);
  792. ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
  793. if (ret) {
  794. /*
  795. * Undo our reference above; DRM does not drop the reference
  796. * to this object on error, so that's okay.
  797. */
  798. drm_framebuffer_unreference(work->old_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. };