2 * Copyright (C) 2012 Russell King
3 * Rewritten from the dovefb driver, and Armada510 manuals.
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.
10 #include <linux/component.h>
11 #include <linux/of_device.h>
12 #include <linux/platform_device.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"
22 struct armada_frame_work {
23 struct armada_plane_work work;
24 struct drm_pending_vblank_event *event;
25 struct armada_regs regs[4];
26 struct drm_framebuffer *old_fb;
37 static const uint32_t armada_primary_formats[] = {
55 * A note about interlacing. Let's consider HDMI 1920x1080i.
56 * The timing parameters we have from X are:
57 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
58 * 1920 2448 2492 2640 1080 1084 1094 1125
59 * Which get translated to:
60 * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot
61 * 1920 2448 2492 2640 540 542 547 562
63 * This is how it is defined by CEA-861-D - line and pixel numbers are
64 * referenced to the rising edge of VSYNC and HSYNC. Total clocks per
65 * line: 2640. The odd frame, the first active line is at line 21, and
66 * the even frame, the first active line is 584.
68 * LN: 560 561 562 563 567 568 569
69 * DE: ~~~|____________________________//__________________________
70 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
71 * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________
72 * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge).
74 * LN: 1123 1124 1125 1 5 6 7
75 * DE: ~~~|____________________________//__________________________
76 * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____
77 * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________
80 * The Armada LCD Controller line and pixel numbers are, like X timings,
81 * referenced to the top left of the active frame.
83 * So, translating these to our LCD controller:
84 * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128.
85 * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448.
86 * Note: Vsync front porch remains constant!
89 * vtotal = mode->crtc_vtotal + 1;
90 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1;
91 * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2
93 * vtotal = mode->crtc_vtotal;
94 * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay;
95 * vhorizpos = mode->crtc_hsync_start;
97 * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end;
99 * So, we need to reprogram these registers on each vsync event:
100 * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL
102 * Note: we do not use the frame done interrupts because these appear
103 * to happen too early, and lead to jitter on the display (presumably
104 * they occur at the end of the last active line, before the vsync back
105 * porch, which we're reprogramming.)
109 armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs)
111 while (regs->offset != ~0) {
112 void __iomem *reg = dcrtc->base + regs->offset;
117 val &= readl_relaxed(reg);
118 writel_relaxed(val | regs->val, reg);
123 #define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON)
125 static void armada_drm_crtc_update(struct armada_crtc *dcrtc)
129 dumb_ctrl = dcrtc->cfg_dumb_ctrl;
131 if (!dpms_blanked(dcrtc->dpms))
132 dumb_ctrl |= CFG_DUMB_ENA;
135 * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might
136 * be using SPI or GPIO. If we set this to DUMB_BLANK, we will
137 * force LCD_D[23:0] to output blank color, overriding the GPIO or
138 * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode.
140 if (dpms_blanked(dcrtc->dpms) &&
141 (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) {
142 dumb_ctrl &= ~DUMB_MASK;
143 dumb_ctrl |= DUMB_BLANK;
147 * The documentation doesn't indicate what the normal state of
148 * the sync signals are. Sebastian Hesselbart kindly probed
149 * these signals on his board to determine their state.
151 * The non-inverted state of the sync signals is active high.
152 * Setting these bits makes the appropriate signal active low.
154 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NCSYNC)
155 dumb_ctrl |= CFG_INV_CSYNC;
156 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NHSYNC)
157 dumb_ctrl |= CFG_INV_HSYNC;
158 if (dcrtc->crtc.mode.flags & DRM_MODE_FLAG_NVSYNC)
159 dumb_ctrl |= CFG_INV_VSYNC;
161 if (dcrtc->dumb_ctrl != dumb_ctrl) {
162 dcrtc->dumb_ctrl = dumb_ctrl;
163 writel_relaxed(dumb_ctrl, dcrtc->base + LCD_SPU_DUMB_CTRL);
167 static unsigned armada_drm_crtc_calc_fb(struct drm_framebuffer *fb,
168 int x, int y, struct armada_regs *regs, bool interlaced)
170 struct armada_gem_object *obj = drm_fb_obj(fb);
171 unsigned pitch = fb->pitches[0];
172 unsigned offset = y * pitch + x * fb->bits_per_pixel / 8;
173 uint32_t addr_odd, addr_even;
176 DRM_DEBUG_DRIVER("pitch %u x %d y %d bpp %d\n",
177 pitch, x, y, fb->bits_per_pixel);
179 addr_odd = addr_even = obj->dev_addr + offset;
186 /* write offset, base, and pitch */
187 armada_reg_queue_set(regs, i, addr_odd, LCD_CFG_GRA_START_ADDR0);
188 armada_reg_queue_set(regs, i, addr_even, LCD_CFG_GRA_START_ADDR1);
189 armada_reg_queue_mod(regs, i, pitch, 0xffff, LCD_CFG_GRA_PITCH);
194 static void armada_drm_plane_work_run(struct armada_crtc *dcrtc,
195 struct armada_plane *plane)
197 struct armada_plane_work *work = xchg(&plane->work, NULL);
199 /* Handle any pending frame work. */
201 work->fn(dcrtc, plane, work);
202 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
206 int armada_drm_plane_work_queue(struct armada_crtc *dcrtc,
207 struct armada_plane *plane, struct armada_plane_work *work)
211 ret = drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
213 DRM_ERROR("failed to acquire vblank counter\n");
217 ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0;
219 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
224 int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout)
226 return wait_event_timeout(plane->frame_wait, !plane->work, timeout);
229 void armada_drm_vbl_event_add(struct armada_crtc *dcrtc,
230 struct armada_vbl_event *evt)
235 WARN_ON(drm_vblank_get(dcrtc->crtc.dev, dcrtc->num));
237 spin_lock_irqsave(&dcrtc->irq_lock, flags);
238 not_on_list = list_empty(&evt->node);
240 list_add_tail(&evt->node, &dcrtc->vbl_list);
241 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
244 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
247 void armada_drm_vbl_event_remove(struct armada_crtc *dcrtc,
248 struct armada_vbl_event *evt)
250 spin_lock_irq(&dcrtc->irq_lock);
251 if (!list_empty(&evt->node)) {
252 list_del_init(&evt->node);
253 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
255 spin_unlock_irq(&dcrtc->irq_lock);
258 static void armada_drm_vbl_event_run(struct armada_crtc *dcrtc)
260 struct armada_vbl_event *e, *n;
262 list_for_each_entry_safe(e, n, &dcrtc->vbl_list, node) {
263 list_del_init(&e->node);
264 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
265 e->fn(dcrtc, e->data);
269 static int armada_drm_crtc_queue_frame_work(struct armada_crtc *dcrtc,
270 struct armada_frame_work *work)
272 struct armada_plane *plane = drm_to_armada_plane(dcrtc->crtc.primary);
274 return armada_drm_plane_work_queue(dcrtc, plane, &work->work);
277 static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc,
278 struct armada_plane *plane, struct armada_plane_work *work)
280 struct armada_frame_work *fwork = container_of(work, struct armada_frame_work, work);
281 struct drm_device *dev = dcrtc->crtc.dev;
284 spin_lock_irqsave(&dcrtc->irq_lock, flags);
285 armada_drm_crtc_update_regs(dcrtc, fwork->regs);
286 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
289 spin_lock_irqsave(&dev->event_lock, flags);
290 drm_send_vblank_event(dev, dcrtc->num, fwork->event);
291 spin_unlock_irqrestore(&dev->event_lock, flags);
294 /* Finally, queue the process-half of the cleanup. */
295 __armada_drm_queue_unref_work(dcrtc->crtc.dev, fwork->old_fb);
299 static void armada_drm_crtc_finish_fb(struct armada_crtc *dcrtc,
300 struct drm_framebuffer *fb, bool force)
302 struct armada_frame_work *work;
308 /* Display is disabled, so just drop the old fb */
309 drm_framebuffer_unreference(fb);
313 work = kmalloc(sizeof(*work), GFP_KERNEL);
316 work->work.fn = armada_drm_crtc_complete_frame_work;
319 armada_reg_queue_end(work->regs, i);
321 if (armada_drm_crtc_queue_frame_work(dcrtc, work) == 0)
328 * Oops - just drop the reference immediately and hope for
329 * the best. The worst that will happen is the buffer gets
330 * reused before it has finished being displayed.
332 drm_framebuffer_unreference(fb);
335 static void armada_drm_vblank_off(struct armada_crtc *dcrtc)
337 struct armada_plane *plane = drm_to_armada_plane(dcrtc->crtc.primary);
340 * Tell the DRM core that vblank IRQs aren't going to happen for
341 * a while. This cleans up any pending vblank events for us.
343 drm_crtc_vblank_off(&dcrtc->crtc);
344 armada_drm_plane_work_run(dcrtc, plane);
347 void armada_drm_crtc_gamma_set(struct drm_crtc *crtc, u16 r, u16 g, u16 b,
352 void armada_drm_crtc_gamma_get(struct drm_crtc *crtc, u16 *r, u16 *g, u16 *b,
357 /* The mode_config.mutex will be held for this call */
358 static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms)
360 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
362 if (dcrtc->dpms != dpms) {
364 if (!IS_ERR(dcrtc->clk) && !dpms_blanked(dpms))
365 WARN_ON(clk_prepare_enable(dcrtc->clk));
366 armada_drm_crtc_update(dcrtc);
367 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dpms))
368 clk_disable_unprepare(dcrtc->clk);
369 if (dpms_blanked(dpms))
370 armada_drm_vblank_off(dcrtc);
372 drm_crtc_vblank_on(&dcrtc->crtc);
377 * Prepare for a mode set. Turn off overlay to ensure that we don't end
378 * up with the overlay size being bigger than the active screen size.
379 * We rely upon X refreshing this state after the mode set has completed.
381 * The mode_config.mutex will be held for this call
383 static void armada_drm_crtc_prepare(struct drm_crtc *crtc)
385 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
386 struct drm_plane *plane;
389 * If we have an overlay plane associated with this CRTC, disable
390 * it before the modeset to avoid its coordinates being outside
391 * the new mode parameters.
393 plane = dcrtc->plane;
395 drm_plane_force_disable(plane);
398 /* The mode_config.mutex will be held for this call */
399 static void armada_drm_crtc_commit(struct drm_crtc *crtc)
401 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
403 if (dcrtc->dpms != DRM_MODE_DPMS_ON) {
404 dcrtc->dpms = DRM_MODE_DPMS_ON;
405 armada_drm_crtc_update(dcrtc);
409 /* The mode_config.mutex will be held for this call */
410 static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc,
411 const struct drm_display_mode *mode, struct drm_display_mode *adj)
413 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
416 /* We can't do interlaced modes if we don't have the SPU_ADV_REG */
417 if (!dcrtc->variant->has_spu_adv_reg &&
418 adj->flags & DRM_MODE_FLAG_INTERLACE)
421 /* Check whether the display mode is possible */
422 ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL);
429 static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat)
431 void __iomem *base = dcrtc->base;
433 if (stat & DMA_FF_UNDERFLOW)
434 DRM_ERROR("video underflow on crtc %u\n", dcrtc->num);
435 if (stat & GRA_FF_UNDERFLOW)
436 DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num);
438 if (stat & VSYNC_IRQ)
439 drm_handle_vblank(dcrtc->crtc.dev, dcrtc->num);
441 spin_lock(&dcrtc->irq_lock);
442 armada_drm_vbl_event_run(dcrtc);
444 if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) {
445 int i = stat & GRA_FRAME_IRQ0 ? 0 : 1;
448 writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH);
449 writel_relaxed(dcrtc->v[i].spu_v_h_total,
450 base + LCD_SPUT_V_H_TOTAL);
452 val = readl_relaxed(base + LCD_SPU_ADV_REG);
453 val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN);
454 val |= dcrtc->v[i].spu_adv_reg;
455 writel_relaxed(val, base + LCD_SPU_ADV_REG);
458 if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) {
459 writel_relaxed(dcrtc->cursor_hw_pos,
460 base + LCD_SPU_HWC_OVSA_HPXL_VLN);
461 writel_relaxed(dcrtc->cursor_hw_sz,
462 base + LCD_SPU_HWC_HPXL_VLN);
463 armada_updatel(CFG_HWC_ENA,
464 CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA,
465 base + LCD_SPU_DMA_CTRL0);
466 dcrtc->cursor_update = false;
467 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
470 spin_unlock(&dcrtc->irq_lock);
472 if (stat & GRA_FRAME_IRQ) {
473 struct armada_plane *plane = drm_to_armada_plane(dcrtc->crtc.primary);
474 armada_drm_plane_work_run(dcrtc, plane);
475 wake_up(&plane->frame_wait);
479 static irqreturn_t armada_drm_irq(int irq, void *arg)
481 struct armada_crtc *dcrtc = arg;
482 u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR);
485 * This is rediculous - rather than writing bits to clear, we
486 * have to set the actual status register value. This is racy.
488 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
490 /* Mask out those interrupts we haven't enabled */
491 v = stat & dcrtc->irq_ena;
493 if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) {
494 armada_drm_crtc_irq(dcrtc, stat);
500 /* These are locked by dev->vbl_lock */
501 void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask)
503 if (dcrtc->irq_ena & mask) {
504 dcrtc->irq_ena &= ~mask;
505 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
509 void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask)
511 if ((dcrtc->irq_ena & mask) != mask) {
512 dcrtc->irq_ena |= mask;
513 writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
514 if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask)
515 writel(0, dcrtc->base + LCD_SPU_IRQ_ISR);
519 static uint32_t armada_drm_crtc_calculate_csc(struct armada_crtc *dcrtc)
521 struct drm_display_mode *adj = &dcrtc->crtc.mode;
524 if (dcrtc->csc_yuv_mode == CSC_YUV_CCIR709)
525 val |= CFG_CSC_YUV_CCIR709;
526 if (dcrtc->csc_rgb_mode == CSC_RGB_STUDIO)
527 val |= CFG_CSC_RGB_STUDIO;
530 * In auto mode, set the colorimetry, based upon the HDMI spec.
531 * 1280x720p, 1920x1080p and 1920x1080i use ITU709, others use
532 * ITU601. It may be more appropriate to set this depending on
533 * the source - but what if the graphic frame is YUV and the
534 * video frame is RGB?
536 if ((adj->hdisplay == 1280 && adj->vdisplay == 720 &&
537 !(adj->flags & DRM_MODE_FLAG_INTERLACE)) ||
538 (adj->hdisplay == 1920 && adj->vdisplay == 1080)) {
539 if (dcrtc->csc_yuv_mode == CSC_AUTO)
540 val |= CFG_CSC_YUV_CCIR709;
544 * We assume we're connected to a TV-like device, so the YUV->RGB
545 * conversion should produce a limited range. We should set this
546 * depending on the connectors attached to this CRTC, and what
547 * kind of device they report being connected.
549 if (dcrtc->csc_rgb_mode == CSC_AUTO)
550 val |= CFG_CSC_RGB_STUDIO;
555 /* The mode_config.mutex will be held for this call */
556 static int armada_drm_crtc_mode_set(struct drm_crtc *crtc,
557 struct drm_display_mode *mode, struct drm_display_mode *adj,
558 int x, int y, struct drm_framebuffer *old_fb)
560 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
561 struct armada_regs regs[17];
562 uint32_t lm, rm, tm, bm, val, sclk;
567 drm_framebuffer_reference(crtc->primary->fb);
569 interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE);
571 i = armada_drm_crtc_calc_fb(dcrtc->crtc.primary->fb,
572 x, y, regs, interlaced);
574 rm = adj->crtc_hsync_start - adj->crtc_hdisplay;
575 lm = adj->crtc_htotal - adj->crtc_hsync_end;
576 bm = adj->crtc_vsync_start - adj->crtc_vdisplay;
577 tm = adj->crtc_vtotal - adj->crtc_vsync_end;
579 DRM_DEBUG_DRIVER("H: %d %d %d %d lm %d rm %d\n",
581 adj->crtc_hsync_start,
583 adj->crtc_htotal, lm, rm);
584 DRM_DEBUG_DRIVER("V: %d %d %d %d tm %d bm %d\n",
586 adj->crtc_vsync_start,
588 adj->crtc_vtotal, tm, bm);
590 /* Wait for pending flips to complete */
591 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
592 MAX_SCHEDULE_TIMEOUT);
594 drm_crtc_vblank_off(crtc);
596 val = dcrtc->dumb_ctrl & ~CFG_DUMB_ENA;
597 if (val != dcrtc->dumb_ctrl) {
598 dcrtc->dumb_ctrl = val;
599 writel_relaxed(val, dcrtc->base + LCD_SPU_DUMB_CTRL);
603 * If we are blanked, we would have disabled the clock. Re-enable
604 * it so that compute_clock() does the right thing.
606 if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms))
607 WARN_ON(clk_prepare_enable(dcrtc->clk));
609 /* Now compute the divider for real */
610 dcrtc->variant->compute_clock(dcrtc, adj, &sclk);
612 /* Ensure graphic fifo is enabled */
613 armada_reg_queue_mod(regs, i, 0, CFG_PDWN64x66, LCD_SPU_SRAM_PARA1);
614 armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV);
616 if (interlaced ^ dcrtc->interlaced) {
617 if (adj->flags & DRM_MODE_FLAG_INTERLACE)
618 drm_vblank_get(dcrtc->crtc.dev, dcrtc->num);
620 drm_vblank_put(dcrtc->crtc.dev, dcrtc->num);
621 dcrtc->interlaced = interlaced;
624 spin_lock_irqsave(&dcrtc->irq_lock, flags);
626 /* Even interlaced/progressive frame */
627 dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 |
629 dcrtc->v[1].spu_v_porch = tm << 16 | bm;
630 val = adj->crtc_hsync_start;
631 dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
632 dcrtc->variant->spu_adv_reg;
635 /* Odd interlaced frame */
636 dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total +
638 dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1;
639 val = adj->crtc_hsync_start - adj->crtc_htotal / 2;
640 dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN |
641 dcrtc->variant->spu_adv_reg;
643 dcrtc->v[0] = dcrtc->v[1];
646 val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay;
648 armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE);
649 armada_reg_queue_set(regs, i, val, LCD_SPU_GRA_HPXL_VLN);
650 armada_reg_queue_set(regs, i, val, LCD_SPU_GZM_HPXL_VLN);
651 armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH);
652 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH);
653 armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total,
656 if (dcrtc->variant->has_spu_adv_reg) {
657 armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg,
658 ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF |
659 ADV_VSYNCOFFEN, LCD_SPU_ADV_REG);
662 val = CFG_GRA_ENA | CFG_GRA_HSMOOTH;
663 val |= CFG_GRA_FMT(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt);
664 val |= CFG_GRA_MOD(drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->mod);
666 if (drm_fb_to_armada_fb(dcrtc->crtc.primary->fb)->fmt > CFG_420)
667 val |= CFG_PALETTE_ENA;
670 val |= CFG_GRA_FTOGGLE;
672 armada_reg_queue_mod(regs, i, val, CFG_GRAFORMAT |
673 CFG_GRA_MOD(CFG_SWAPRB | CFG_SWAPUV |
674 CFG_SWAPYU | CFG_YUV2RGB) |
675 CFG_PALETTE_ENA | CFG_GRA_FTOGGLE,
678 val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0;
679 armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1);
681 val = dcrtc->spu_iopad_ctrl | armada_drm_crtc_calculate_csc(dcrtc);
682 armada_reg_queue_set(regs, i, val, LCD_SPU_IOPAD_CONTROL);
683 armada_reg_queue_end(regs, i);
685 armada_drm_crtc_update_regs(dcrtc, regs);
686 spin_unlock_irqrestore(&dcrtc->irq_lock, flags);
688 armada_drm_crtc_update(dcrtc);
690 drm_crtc_vblank_on(crtc);
691 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
696 /* The mode_config.mutex will be held for this call */
697 static int armada_drm_crtc_mode_set_base(struct drm_crtc *crtc, int x, int y,
698 struct drm_framebuffer *old_fb)
700 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
701 struct armada_regs regs[4];
704 i = armada_drm_crtc_calc_fb(crtc->primary->fb, crtc->x, crtc->y, regs,
706 armada_reg_queue_end(regs, i);
708 /* Wait for pending flips to complete */
709 armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary),
710 MAX_SCHEDULE_TIMEOUT);
712 /* Take a reference to the new fb as we're using it */
713 drm_framebuffer_reference(crtc->primary->fb);
715 /* Update the base in the CRTC */
716 armada_drm_crtc_update_regs(dcrtc, regs);
718 /* Drop our previously held reference */
719 armada_drm_crtc_finish_fb(dcrtc, old_fb, dpms_blanked(dcrtc->dpms));
724 void armada_drm_crtc_plane_disable(struct armada_crtc *dcrtc,
725 struct drm_plane *plane)
727 u32 sram_para1, dma_ctrl0_mask;
730 * Drop our reference on any framebuffer attached to this plane.
731 * We don't need to NULL this out as drm_plane_force_disable(),
732 * and __setplane_internal() will do so for an overlay plane, and
733 * __drm_helper_disable_unused_functions() will do so for the
737 drm_framebuffer_unreference(plane->fb);
739 /* Power down the Y/U/V FIFOs */
740 sram_para1 = CFG_PDWN16x66 | CFG_PDWN32x66;
742 /* Power down most RAMs and FIFOs if this is the primary plane */
743 if (plane->type == DRM_PLANE_TYPE_PRIMARY) {
744 sram_para1 |= CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
745 CFG_PDWN32x32 | CFG_PDWN64x66;
746 dma_ctrl0_mask = CFG_GRA_ENA;
748 dma_ctrl0_mask = CFG_DMA_ENA;
751 spin_lock_irq(&dcrtc->irq_lock);
752 armada_updatel(0, dma_ctrl0_mask, dcrtc->base + LCD_SPU_DMA_CTRL0);
753 spin_unlock_irq(&dcrtc->irq_lock);
755 armada_updatel(sram_para1, 0, dcrtc->base + LCD_SPU_SRAM_PARA1);
758 /* The mode_config.mutex will be held for this call */
759 static void armada_drm_crtc_disable(struct drm_crtc *crtc)
761 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
763 armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF);
764 armada_drm_crtc_plane_disable(dcrtc, crtc->primary);
767 static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = {
768 .dpms = armada_drm_crtc_dpms,
769 .prepare = armada_drm_crtc_prepare,
770 .commit = armada_drm_crtc_commit,
771 .mode_fixup = armada_drm_crtc_mode_fixup,
772 .mode_set = armada_drm_crtc_mode_set,
773 .mode_set_base = armada_drm_crtc_mode_set_base,
774 .disable = armada_drm_crtc_disable,
777 static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix,
778 unsigned stride, unsigned width, unsigned height)
783 addr = SRAM_HWC32_RAM1;
784 for (y = 0; y < height; y++) {
785 uint32_t *p = &pix[y * stride];
788 for (x = 0; x < width; x++, p++) {
791 val = (val & 0xff00ff00) |
792 (val & 0x000000ff) << 16 |
793 (val & 0x00ff0000) >> 16;
796 base + LCD_SPU_SRAM_WRDAT);
797 writel_relaxed(addr | SRAM_WRITE,
798 base + LCD_SPU_SRAM_CTRL);
799 readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN);
801 if ((addr & 0x00ff) == 0)
803 if ((addr & 0x30ff) == 0)
804 addr = SRAM_HWC32_RAM2;
809 static void armada_drm_crtc_cursor_tran(void __iomem *base)
813 for (addr = 0; addr < 256; addr++) {
814 /* write the default value */
815 writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT);
816 writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN,
817 base + LCD_SPU_SRAM_CTRL);
821 static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload)
823 uint32_t xoff, xscr, w = dcrtc->cursor_w, s;
824 uint32_t yoff, yscr, h = dcrtc->cursor_h;
828 * Calculate the visible width and height of the cursor,
829 * screen position, and the position in the cursor bitmap.
831 if (dcrtc->cursor_x < 0) {
832 xoff = -dcrtc->cursor_x;
835 } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) {
837 xscr = dcrtc->cursor_x;
838 w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0);
841 xscr = dcrtc->cursor_x;
844 if (dcrtc->cursor_y < 0) {
845 yoff = -dcrtc->cursor_y;
848 } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) {
850 yscr = dcrtc->cursor_y;
851 h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0);
854 yscr = dcrtc->cursor_y;
857 /* On interlaced modes, the vertical cursor size must be halved */
859 if (dcrtc->interlaced) {
865 if (!dcrtc->cursor_obj || !h || !w) {
866 spin_lock_irq(&dcrtc->irq_lock);
867 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
868 dcrtc->cursor_update = false;
869 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
870 spin_unlock_irq(&dcrtc->irq_lock);
874 para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1);
875 armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32,
876 dcrtc->base + LCD_SPU_SRAM_PARA1);
879 * Initialize the transparency if the SRAM was powered down.
880 * We must also reload the cursor data as well.
882 if (!(para1 & CFG_CSB_256x32)) {
883 armada_drm_crtc_cursor_tran(dcrtc->base);
887 if (dcrtc->cursor_hw_sz != (h << 16 | w)) {
888 spin_lock_irq(&dcrtc->irq_lock);
889 armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
890 dcrtc->cursor_update = false;
891 armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0);
892 spin_unlock_irq(&dcrtc->irq_lock);
896 struct armada_gem_object *obj = dcrtc->cursor_obj;
898 /* Set the top-left corner of the cursor image */
900 pix += yoff * s + xoff;
901 armada_load_cursor_argb(dcrtc->base, pix, s, w, h);
904 /* Reload the cursor position, size and enable in the IRQ handler */
905 spin_lock_irq(&dcrtc->irq_lock);
906 dcrtc->cursor_hw_pos = yscr << 16 | xscr;
907 dcrtc->cursor_hw_sz = h << 16 | w;
908 dcrtc->cursor_update = true;
909 armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA);
910 spin_unlock_irq(&dcrtc->irq_lock);
915 static void cursor_update(void *data)
917 armada_drm_crtc_cursor_update(data, true);
920 static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc,
921 struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h)
923 struct drm_device *dev = crtc->dev;
924 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
925 struct armada_gem_object *obj = NULL;
928 /* If no cursor support, replicate drm's return value */
929 if (!dcrtc->variant->has_spu_adv_reg)
932 if (handle && w > 0 && h > 0) {
933 /* maximum size is 64x32 or 32x64 */
934 if (w > 64 || h > 64 || (w > 32 && h > 32))
937 obj = armada_gem_object_lookup(dev, file, handle);
941 /* Must be a kernel-mapped object */
943 drm_gem_object_unreference_unlocked(&obj->obj);
947 if (obj->obj.size < w * h * 4) {
948 DRM_ERROR("buffer is too small\n");
949 drm_gem_object_unreference_unlocked(&obj->obj);
954 mutex_lock(&dev->struct_mutex);
955 if (dcrtc->cursor_obj) {
956 dcrtc->cursor_obj->update = NULL;
957 dcrtc->cursor_obj->update_data = NULL;
958 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
960 dcrtc->cursor_obj = obj;
963 ret = armada_drm_crtc_cursor_update(dcrtc, true);
965 obj->update_data = dcrtc;
966 obj->update = cursor_update;
968 mutex_unlock(&dev->struct_mutex);
973 static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
975 struct drm_device *dev = crtc->dev;
976 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
979 /* If no cursor support, replicate drm's return value */
980 if (!dcrtc->variant->has_spu_adv_reg)
983 mutex_lock(&dev->struct_mutex);
986 ret = armada_drm_crtc_cursor_update(dcrtc, false);
987 mutex_unlock(&dev->struct_mutex);
992 static void armada_drm_crtc_destroy(struct drm_crtc *crtc)
994 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
995 struct armada_private *priv = crtc->dev->dev_private;
997 if (dcrtc->cursor_obj)
998 drm_gem_object_unreference(&dcrtc->cursor_obj->obj);
1000 priv->dcrtc[dcrtc->num] = NULL;
1001 drm_crtc_cleanup(&dcrtc->crtc);
1003 if (!IS_ERR(dcrtc->clk))
1004 clk_disable_unprepare(dcrtc->clk);
1006 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA);
1008 of_node_put(dcrtc->crtc.port);
1014 * The mode_config lock is held here, to prevent races between this
1017 static int armada_drm_crtc_page_flip(struct drm_crtc *crtc,
1018 struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, uint32_t page_flip_flags)
1020 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1021 struct armada_frame_work *work;
1025 /* We don't support changing the pixel format */
1026 if (fb->pixel_format != crtc->primary->fb->pixel_format)
1029 work = kmalloc(sizeof(*work), GFP_KERNEL);
1033 work->work.fn = armada_drm_crtc_complete_frame_work;
1034 work->event = event;
1035 work->old_fb = dcrtc->crtc.primary->fb;
1037 i = armada_drm_crtc_calc_fb(fb, crtc->x, crtc->y, work->regs,
1039 armada_reg_queue_end(work->regs, i);
1042 * Ensure that we hold a reference on the new framebuffer.
1043 * This has to match the behaviour in mode_set.
1045 drm_framebuffer_reference(fb);
1047 ret = armada_drm_crtc_queue_frame_work(dcrtc, work);
1049 /* Undo our reference above */
1050 drm_framebuffer_unreference(fb);
1056 * Don't take a reference on the new framebuffer;
1057 * drm_mode_page_flip_ioctl() has already grabbed a reference and
1058 * will _not_ drop that reference on successful return from this
1059 * function. Simply mark this new framebuffer as the current one.
1061 dcrtc->crtc.primary->fb = fb;
1064 * Finally, if the display is blanked, we won't receive an
1065 * interrupt, so complete it now.
1067 if (dpms_blanked(dcrtc->dpms))
1068 armada_drm_plane_work_run(dcrtc, drm_to_armada_plane(dcrtc->crtc.primary));
1074 armada_drm_crtc_set_property(struct drm_crtc *crtc,
1075 struct drm_property *property, uint64_t val)
1077 struct armada_private *priv = crtc->dev->dev_private;
1078 struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc);
1079 bool update_csc = false;
1081 if (property == priv->csc_yuv_prop) {
1082 dcrtc->csc_yuv_mode = val;
1084 } else if (property == priv->csc_rgb_prop) {
1085 dcrtc->csc_rgb_mode = val;
1092 val = dcrtc->spu_iopad_ctrl |
1093 armada_drm_crtc_calculate_csc(dcrtc);
1094 writel_relaxed(val, dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1100 static struct drm_crtc_funcs armada_crtc_funcs = {
1101 .cursor_set = armada_drm_crtc_cursor_set,
1102 .cursor_move = armada_drm_crtc_cursor_move,
1103 .destroy = armada_drm_crtc_destroy,
1104 .set_config = drm_crtc_helper_set_config,
1105 .page_flip = armada_drm_crtc_page_flip,
1106 .set_property = armada_drm_crtc_set_property,
1109 static const struct drm_plane_funcs armada_primary_plane_funcs = {
1110 .update_plane = drm_primary_helper_update,
1111 .disable_plane = drm_primary_helper_disable,
1112 .destroy = drm_primary_helper_destroy,
1115 int armada_drm_plane_init(struct armada_plane *plane)
1117 init_waitqueue_head(&plane->frame_wait);
1122 static struct drm_prop_enum_list armada_drm_csc_yuv_enum_list[] = {
1123 { CSC_AUTO, "Auto" },
1124 { CSC_YUV_CCIR601, "CCIR601" },
1125 { CSC_YUV_CCIR709, "CCIR709" },
1128 static struct drm_prop_enum_list armada_drm_csc_rgb_enum_list[] = {
1129 { CSC_AUTO, "Auto" },
1130 { CSC_RGB_COMPUTER, "Computer system" },
1131 { CSC_RGB_STUDIO, "Studio" },
1134 static int armada_drm_crtc_create_properties(struct drm_device *dev)
1136 struct armada_private *priv = dev->dev_private;
1138 if (priv->csc_yuv_prop)
1141 priv->csc_yuv_prop = drm_property_create_enum(dev, 0,
1142 "CSC_YUV", armada_drm_csc_yuv_enum_list,
1143 ARRAY_SIZE(armada_drm_csc_yuv_enum_list));
1144 priv->csc_rgb_prop = drm_property_create_enum(dev, 0,
1145 "CSC_RGB", armada_drm_csc_rgb_enum_list,
1146 ARRAY_SIZE(armada_drm_csc_rgb_enum_list));
1148 if (!priv->csc_yuv_prop || !priv->csc_rgb_prop)
1154 static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev,
1155 struct resource *res, int irq, const struct armada_variant *variant,
1156 struct device_node *port)
1158 struct armada_private *priv = drm->dev_private;
1159 struct armada_crtc *dcrtc;
1160 struct armada_plane *primary;
1164 ret = armada_drm_crtc_create_properties(drm);
1168 base = devm_ioremap_resource(dev, res);
1170 return PTR_ERR(base);
1172 dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL);
1174 DRM_ERROR("failed to allocate Armada crtc\n");
1178 if (dev != drm->dev)
1179 dev_set_drvdata(dev, dcrtc);
1181 dcrtc->variant = variant;
1183 dcrtc->num = drm->mode_config.num_crtc;
1184 dcrtc->clk = ERR_PTR(-EINVAL);
1185 dcrtc->csc_yuv_mode = CSC_AUTO;
1186 dcrtc->csc_rgb_mode = CSC_AUTO;
1187 dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0;
1188 dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24;
1189 spin_lock_init(&dcrtc->irq_lock);
1190 dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR;
1191 INIT_LIST_HEAD(&dcrtc->vbl_list);
1193 /* Initialize some registers which we don't otherwise set */
1194 writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV);
1195 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR);
1196 writel_relaxed(dcrtc->spu_iopad_ctrl,
1197 dcrtc->base + LCD_SPU_IOPAD_CONTROL);
1198 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0);
1199 writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 |
1200 CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 |
1201 CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1);
1202 writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1);
1203 writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_GRA_OVSA_HPXL_VLN);
1204 writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA);
1205 writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR);
1207 ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc",
1214 if (dcrtc->variant->init) {
1215 ret = dcrtc->variant->init(dcrtc, dev);
1222 /* Ensure AXI pipeline is enabled */
1223 armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0);
1225 priv->dcrtc[dcrtc->num] = dcrtc;
1227 dcrtc->crtc.port = port;
1229 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
1233 ret = armada_drm_plane_init(primary);
1239 ret = drm_universal_plane_init(drm, &primary->base, 0,
1240 &armada_primary_plane_funcs,
1241 armada_primary_formats,
1242 ARRAY_SIZE(armada_primary_formats),
1243 DRM_PLANE_TYPE_PRIMARY);
1249 ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL,
1250 &armada_crtc_funcs);
1254 drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs);
1256 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_yuv_prop,
1257 dcrtc->csc_yuv_mode);
1258 drm_object_attach_property(&dcrtc->crtc.base, priv->csc_rgb_prop,
1259 dcrtc->csc_rgb_mode);
1261 return armada_overlay_plane_create(drm, 1 << dcrtc->num);
1264 primary->base.funcs->destroy(&primary->base);
1269 armada_lcd_bind(struct device *dev, struct device *master, void *data)
1271 struct platform_device *pdev = to_platform_device(dev);
1272 struct drm_device *drm = data;
1273 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1274 int irq = platform_get_irq(pdev, 0);
1275 const struct armada_variant *variant;
1276 struct device_node *port = NULL;
1281 if (!dev->of_node) {
1282 const struct platform_device_id *id;
1284 id = platform_get_device_id(pdev);
1288 variant = (const struct armada_variant *)id->driver_data;
1290 const struct of_device_id *match;
1291 struct device_node *np, *parent = dev->of_node;
1293 match = of_match_device(dev->driver->of_match_table, dev);
1297 np = of_get_child_by_name(parent, "ports");
1300 port = of_get_child_by_name(parent, "port");
1303 dev_err(dev, "no port node found in %s\n",
1308 variant = match->data;
1311 return armada_drm_crtc_create(drm, dev, res, irq, variant, port);
1315 armada_lcd_unbind(struct device *dev, struct device *master, void *data)
1317 struct armada_crtc *dcrtc = dev_get_drvdata(dev);
1319 armada_drm_crtc_destroy(&dcrtc->crtc);
1322 static const struct component_ops armada_lcd_ops = {
1323 .bind = armada_lcd_bind,
1324 .unbind = armada_lcd_unbind,
1327 static int armada_lcd_probe(struct platform_device *pdev)
1329 return component_add(&pdev->dev, &armada_lcd_ops);
1332 static int armada_lcd_remove(struct platform_device *pdev)
1334 component_del(&pdev->dev, &armada_lcd_ops);
1338 static struct of_device_id armada_lcd_of_match[] = {
1340 .compatible = "marvell,dove-lcd",
1341 .data = &armada510_ops,
1345 MODULE_DEVICE_TABLE(of, armada_lcd_of_match);
1347 static const struct platform_device_id armada_lcd_platform_ids[] = {
1349 .name = "armada-lcd",
1350 .driver_data = (unsigned long)&armada510_ops,
1352 .name = "armada-510-lcd",
1353 .driver_data = (unsigned long)&armada510_ops,
1357 MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids);
1359 struct platform_driver armada_lcd_platform_driver = {
1360 .probe = armada_lcd_probe,
1361 .remove = armada_lcd_remove,
1363 .name = "armada-lcd",
1364 .owner = THIS_MODULE,
1365 .of_match_table = armada_lcd_of_match,
1367 .id_table = armada_lcd_platform_ids,