196f60e37SRussell King /* 296f60e37SRussell King * Copyright (C) 2012 Russell King 396f60e37SRussell King * Rewritten from the dovefb driver, and Armada510 manuals. 496f60e37SRussell King * 596f60e37SRussell King * This program is free software; you can redistribute it and/or modify 696f60e37SRussell King * it under the terms of the GNU General Public License version 2 as 796f60e37SRussell King * published by the Free Software Foundation. 896f60e37SRussell King */ 996f60e37SRussell King #include <linux/clk.h> 10d8c96083SRussell King #include <linux/component.h> 11d8c96083SRussell King #include <linux/of_device.h> 12d8c96083SRussell King #include <linux/platform_device.h> 1396f60e37SRussell King #include <drm/drmP.h> 14de503ddfSRussell King #include <drm/drm_atomic.h> 1596f60e37SRussell King #include <drm/drm_crtc_helper.h> 163cb9ae4fSDaniel Vetter #include <drm/drm_plane_helper.h> 17bcd21a47SDave Airlie #include <drm/drm_atomic_helper.h> 1896f60e37SRussell King #include "armada_crtc.h" 1996f60e37SRussell King #include "armada_drm.h" 2096f60e37SRussell King #include "armada_fb.h" 2196f60e37SRussell King #include "armada_gem.h" 2296f60e37SRussell King #include "armada_hw.h" 23d40af7b1SRussell King #include "armada_plane.h" 24c8a220c6SRussell King #include "armada_trace.h" 2596f60e37SRussell King 2696f60e37SRussell King /* 2796f60e37SRussell King * A note about interlacing. Let's consider HDMI 1920x1080i. 2896f60e37SRussell King * The timing parameters we have from X are: 2996f60e37SRussell King * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot 3096f60e37SRussell King * 1920 2448 2492 2640 1080 1084 1094 1125 3196f60e37SRussell King * Which get translated to: 3296f60e37SRussell King * Hact HsyA HsyI Htot Vact VsyA VsyI Vtot 3396f60e37SRussell King * 1920 2448 2492 2640 540 542 547 562 3496f60e37SRussell King * 3596f60e37SRussell King * This is how it is defined by CEA-861-D - line and pixel numbers are 3696f60e37SRussell King * referenced to the rising edge of VSYNC and HSYNC. Total clocks per 3796f60e37SRussell King * line: 2640. The odd frame, the first active line is at line 21, and 3896f60e37SRussell King * the even frame, the first active line is 584. 3996f60e37SRussell King * 4096f60e37SRussell King * LN: 560 561 562 563 567 568 569 4196f60e37SRussell King * DE: ~~~|____________________________//__________________________ 4296f60e37SRussell King * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____ 4396f60e37SRussell King * VSYNC: _________________________|~~~~~~//~~~~~~~~~~~~~~~|__________ 4496f60e37SRussell King * 22 blanking lines. VSYNC at 1320 (referenced to the HSYNC rising edge). 4596f60e37SRussell King * 4696f60e37SRussell King * LN: 1123 1124 1125 1 5 6 7 4796f60e37SRussell King * DE: ~~~|____________________________//__________________________ 4896f60e37SRussell King * HSYNC: ____|~|_____|~|_____|~|_____|~|_//__|~|_____|~|_____|~|_____ 4996f60e37SRussell King * VSYNC: ____________________|~~~~~~~~~~~//~~~~~~~~~~|_______________ 5096f60e37SRussell King * 23 blanking lines 5196f60e37SRussell King * 5296f60e37SRussell King * The Armada LCD Controller line and pixel numbers are, like X timings, 5396f60e37SRussell King * referenced to the top left of the active frame. 5496f60e37SRussell King * 5596f60e37SRussell King * So, translating these to our LCD controller: 5696f60e37SRussell King * Odd frame, 563 total lines, VSYNC at line 543-548, pixel 1128. 5796f60e37SRussell King * Even frame, 562 total lines, VSYNC at line 542-547, pixel 2448. 5896f60e37SRussell King * Note: Vsync front porch remains constant! 5996f60e37SRussell King * 6096f60e37SRussell King * if (odd_frame) { 6196f60e37SRussell King * vtotal = mode->crtc_vtotal + 1; 6296f60e37SRussell King * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay + 1; 6396f60e37SRussell King * vhorizpos = mode->crtc_hsync_start - mode->crtc_htotal / 2 6496f60e37SRussell King * } else { 6596f60e37SRussell King * vtotal = mode->crtc_vtotal; 6696f60e37SRussell King * vbackporch = mode->crtc_vsync_start - mode->crtc_vdisplay; 6796f60e37SRussell King * vhorizpos = mode->crtc_hsync_start; 6896f60e37SRussell King * } 6996f60e37SRussell King * vfrontporch = mode->crtc_vtotal - mode->crtc_vsync_end; 7096f60e37SRussell King * 7196f60e37SRussell King * So, we need to reprogram these registers on each vsync event: 7296f60e37SRussell King * LCD_SPU_V_PORCH, LCD_SPU_ADV_REG, LCD_SPUT_V_H_TOTAL 7396f60e37SRussell King * 7496f60e37SRussell King * Note: we do not use the frame done interrupts because these appear 7596f60e37SRussell King * to happen too early, and lead to jitter on the display (presumably 7696f60e37SRussell King * they occur at the end of the last active line, before the vsync back 7796f60e37SRussell King * porch, which we're reprogramming.) 7896f60e37SRussell King */ 7996f60e37SRussell King 8096f60e37SRussell King void 8196f60e37SRussell King armada_drm_crtc_update_regs(struct armada_crtc *dcrtc, struct armada_regs *regs) 8296f60e37SRussell King { 8396f60e37SRussell King while (regs->offset != ~0) { 8496f60e37SRussell King void __iomem *reg = dcrtc->base + regs->offset; 8596f60e37SRussell King uint32_t val; 8696f60e37SRussell King 8796f60e37SRussell King val = regs->mask; 8896f60e37SRussell King if (val != 0) 8996f60e37SRussell King val &= readl_relaxed(reg); 9096f60e37SRussell King writel_relaxed(val | regs->val, reg); 9196f60e37SRussell King ++regs; 9296f60e37SRussell King } 9396f60e37SRussell King } 9496f60e37SRussell King 9596f60e37SRussell King #define dpms_blanked(dpms) ((dpms) != DRM_MODE_DPMS_ON) 9696f60e37SRussell King 97*a0f75d24SRussell King static void armada_drm_crtc_update(struct armada_crtc *dcrtc, bool enable) 9896f60e37SRussell King { 9996f60e37SRussell King uint32_t dumb_ctrl; 10096f60e37SRussell King 10196f60e37SRussell King dumb_ctrl = dcrtc->cfg_dumb_ctrl; 10296f60e37SRussell King 103*a0f75d24SRussell King if (enable) 10496f60e37SRussell King dumb_ctrl |= CFG_DUMB_ENA; 10596f60e37SRussell King 10696f60e37SRussell King /* 10796f60e37SRussell King * When the dumb interface isn't in DUMB24_RGB888_0 mode, it might 10896f60e37SRussell King * be using SPI or GPIO. If we set this to DUMB_BLANK, we will 10996f60e37SRussell King * force LCD_D[23:0] to output blank color, overriding the GPIO or 11096f60e37SRussell King * SPI usage. So leave it as-is unless in DUMB24_RGB888_0 mode. 11196f60e37SRussell King */ 112*a0f75d24SRussell King if (!enable && (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) { 11396f60e37SRussell King dumb_ctrl &= ~DUMB_MASK; 11496f60e37SRussell King dumb_ctrl |= DUMB_BLANK; 11596f60e37SRussell King } 11696f60e37SRussell King 117155b8290SRussell King armada_updatel(dumb_ctrl, 118155b8290SRussell King ~(CFG_INV_CSYNC | CFG_INV_HSYNC | CFG_INV_VSYNC), 119155b8290SRussell King dcrtc->base + LCD_SPU_DUMB_CTRL); 12096f60e37SRussell King } 12196f60e37SRussell King 1222839d45cSRussell King static void armada_drm_plane_work_call(struct armada_crtc *dcrtc, 1232839d45cSRussell King struct armada_plane_work *work, 1242839d45cSRussell King void (*fn)(struct armada_crtc *, struct armada_plane_work *)) 1252839d45cSRussell King { 1262839d45cSRussell King struct armada_plane *dplane = drm_to_armada_plane(work->plane); 127d924155dSRussell King struct drm_pending_vblank_event *event; 128d924155dSRussell King struct drm_framebuffer *fb; 1292839d45cSRussell King 1302839d45cSRussell King if (fn) 1312839d45cSRussell King fn(dcrtc, work); 1322839d45cSRussell King drm_crtc_vblank_put(&dcrtc->crtc); 1332839d45cSRussell King 134d924155dSRussell King event = work->event; 135d924155dSRussell King fb = work->old_fb; 136eb19be5bSRussell King if (event || fb) { 137eb19be5bSRussell King struct drm_device *dev = dcrtc->crtc.dev; 138eb19be5bSRussell King unsigned long flags; 139eb19be5bSRussell King 140eb19be5bSRussell King spin_lock_irqsave(&dev->event_lock, flags); 141eb19be5bSRussell King if (event) 142eb19be5bSRussell King drm_crtc_send_vblank_event(&dcrtc->crtc, event); 143b972a80fSRussell King if (fb) 144eb19be5bSRussell King __armada_drm_queue_unref_work(dev, fb); 145eb19be5bSRussell King spin_unlock_irqrestore(&dev->event_lock, flags); 146eb19be5bSRussell King } 147b972a80fSRussell King 148d924155dSRussell King if (work->need_kfree) 149d924155dSRussell King kfree(work); 150d924155dSRussell King 1512839d45cSRussell King wake_up(&dplane->frame_wait); 1522839d45cSRussell King } 1532839d45cSRussell King 1544b5dda82SRussell King static void armada_drm_plane_work_run(struct armada_crtc *dcrtc, 155ec6fb159SRussell King struct drm_plane *plane) 1564b5dda82SRussell King { 157ec6fb159SRussell King struct armada_plane *dplane = drm_to_armada_plane(plane); 158ec6fb159SRussell King struct armada_plane_work *work = xchg(&dplane->work, NULL); 1594b5dda82SRussell King 1604b5dda82SRussell King /* Handle any pending frame work. */ 1612839d45cSRussell King if (work) 1622839d45cSRussell King armada_drm_plane_work_call(dcrtc, work, work->fn); 1634b5dda82SRussell King } 1644b5dda82SRussell King 1654b5dda82SRussell King int armada_drm_plane_work_queue(struct armada_crtc *dcrtc, 166eaab0130SRussell King struct armada_plane_work *work) 1674b5dda82SRussell King { 168eaab0130SRussell King struct armada_plane *plane = drm_to_armada_plane(work->plane); 1694b5dda82SRussell King int ret; 1704b5dda82SRussell King 171accbaf6eSGustavo Padovan ret = drm_crtc_vblank_get(&dcrtc->crtc); 172c93dfdcdSRussell King if (ret) 1734b5dda82SRussell King return ret; 1744b5dda82SRussell King 1754b5dda82SRussell King ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0; 1764b5dda82SRussell King if (ret) 177accbaf6eSGustavo Padovan drm_crtc_vblank_put(&dcrtc->crtc); 1784b5dda82SRussell King 1794b5dda82SRussell King return ret; 1804b5dda82SRussell King } 1814b5dda82SRussell King 1824b5dda82SRussell King int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout) 1834b5dda82SRussell King { 1844b5dda82SRussell King return wait_event_timeout(plane->frame_wait, !plane->work, timeout); 1854b5dda82SRussell King } 1864b5dda82SRussell King 187d3b84215SRussell King void armada_drm_plane_work_cancel(struct armada_crtc *dcrtc, 188d3b84215SRussell King struct armada_plane *dplane) 1897c8f7e1aSRussell King { 190d3b84215SRussell King struct armada_plane_work *work = xchg(&dplane->work, NULL); 1917c8f7e1aSRussell King 1924a8506d2SRussell King if (work) 1932839d45cSRussell King armada_drm_plane_work_call(dcrtc, work, work->cancel); 19496f60e37SRussell King } 19596f60e37SRussell King 196709ffd82SRussell King static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc, 19765724a19SRussell King struct armada_plane_work *work) 19896f60e37SRussell King { 199709ffd82SRussell King unsigned long flags; 20096f60e37SRussell King 201709ffd82SRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 202eaa66279SRussell King armada_drm_crtc_update_regs(dcrtc, work->regs); 203709ffd82SRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 204709ffd82SRussell King } 20596f60e37SRussell King 206eaa66279SRussell King static struct armada_plane_work * 207eaa66279SRussell King armada_drm_crtc_alloc_plane_work(struct drm_plane *plane) 208901bb889SRussell King { 209eaa66279SRussell King struct armada_plane_work *work; 210901bb889SRussell King int i = 0; 211901bb889SRussell King 212901bb889SRussell King work = kzalloc(sizeof(*work), GFP_KERNEL); 213901bb889SRussell King if (!work) 214901bb889SRussell King return NULL; 215901bb889SRussell King 216eaa66279SRussell King work->plane = plane; 217eaa66279SRussell King work->fn = armada_drm_crtc_complete_frame_work; 218d924155dSRussell King work->need_kfree = true; 219901bb889SRussell King armada_reg_queue_end(work->regs, i); 220901bb889SRussell King 221901bb889SRussell King return work; 22296f60e37SRussell King } 22396f60e37SRussell King 22496f60e37SRussell King static void armada_drm_vblank_off(struct armada_crtc *dcrtc) 22596f60e37SRussell King { 22696f60e37SRussell King /* 22796f60e37SRussell King * Tell the DRM core that vblank IRQs aren't going to happen for 22896f60e37SRussell King * a while. This cleans up any pending vblank events for us. 22996f60e37SRussell King */ 230178e561fSRussell King drm_crtc_vblank_off(&dcrtc->crtc); 231ec6fb159SRussell King armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary); 23296f60e37SRussell King } 23396f60e37SRussell King 234dbb4ca8aSRussell King static void armada_drm_crtc_queue_state_event(struct drm_crtc *crtc) 235dbb4ca8aSRussell King { 236dbb4ca8aSRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 237dbb4ca8aSRussell King struct drm_pending_vblank_event *event; 238dbb4ca8aSRussell King 239dbb4ca8aSRussell King /* If we have an event, we need vblank events enabled */ 240dbb4ca8aSRussell King event = xchg(&crtc->state->event, NULL); 241dbb4ca8aSRussell King if (event) { 242dbb4ca8aSRussell King WARN_ON(drm_crtc_vblank_get(crtc) != 0); 243dbb4ca8aSRussell King dcrtc->event = event; 244dbb4ca8aSRussell King } 245dbb4ca8aSRussell King } 246dbb4ca8aSRussell King 24796f60e37SRussell King /* The mode_config.mutex will be held for this call */ 24896f60e37SRussell King static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms) 24996f60e37SRussell King { 25096f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 25196f60e37SRussell King 252ea908ba8SRussell King if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) { 25396f60e37SRussell King if (dpms_blanked(dpms)) 25496f60e37SRussell King armada_drm_vblank_off(dcrtc); 255a0fbb35eSRussell King else if (dcrtc->variant->enable) 256a0fbb35eSRussell King dcrtc->variant->enable(dcrtc, &crtc->hwmode); 257ea908ba8SRussell King dcrtc->dpms = dpms; 258*a0f75d24SRussell King armada_drm_crtc_update(dcrtc, !dpms_blanked(dcrtc->dpms)); 259ea908ba8SRussell King if (!dpms_blanked(dpms)) 260178e561fSRussell King drm_crtc_vblank_on(&dcrtc->crtc); 261a0fbb35eSRussell King else if (dcrtc->variant->disable) 262a0fbb35eSRussell King dcrtc->variant->disable(dcrtc); 263ea908ba8SRussell King } else if (dcrtc->dpms != dpms) { 264ea908ba8SRussell King dcrtc->dpms = dpms; 26596f60e37SRussell King } 26696f60e37SRussell King } 26796f60e37SRussell King 26896f60e37SRussell King /* 26996f60e37SRussell King * Prepare for a mode set. Turn off overlay to ensure that we don't end 27096f60e37SRussell King * up with the overlay size being bigger than the active screen size. 27196f60e37SRussell King * We rely upon X refreshing this state after the mode set has completed. 27296f60e37SRussell King * 27396f60e37SRussell King * The mode_config.mutex will be held for this call 27496f60e37SRussell King */ 27596f60e37SRussell King static void armada_drm_crtc_prepare(struct drm_crtc *crtc) 27696f60e37SRussell King { 27796f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 27896f60e37SRussell King struct drm_plane *plane; 27996f60e37SRussell King 28096f60e37SRussell King /* 28196f60e37SRussell King * If we have an overlay plane associated with this CRTC, disable 28296f60e37SRussell King * it before the modeset to avoid its coordinates being outside 283f8e14069SRussell King * the new mode parameters. 28496f60e37SRussell King */ 28596f60e37SRussell King plane = dcrtc->plane; 286890ca8dfSRussell King if (plane) { 287f8e14069SRussell King drm_plane_force_disable(plane); 288890ca8dfSRussell King WARN_ON(!armada_drm_plane_work_wait(drm_to_armada_plane(plane), 289890ca8dfSRussell King HZ)); 290890ca8dfSRussell King } 291f9a13bb3SRussell King 292f9a13bb3SRussell King /* Wait for pending flips to complete */ 293f9a13bb3SRussell King armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary), 294f9a13bb3SRussell King MAX_SCHEDULE_TIMEOUT); 295f9a13bb3SRussell King 296f9a13bb3SRussell King drm_crtc_vblank_off(crtc); 297f9a13bb3SRussell King 298155b8290SRussell King armada_updatel(0, CFG_DUMB_ENA, dcrtc->base + LCD_SPU_DUMB_CTRL); 29996f60e37SRussell King } 30096f60e37SRussell King 30196f60e37SRussell King /* The mode_config.mutex will be held for this call */ 30296f60e37SRussell King static void armada_drm_crtc_commit(struct drm_crtc *crtc) 30396f60e37SRussell King { 30496f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 30596f60e37SRussell King 30696f60e37SRussell King dcrtc->dpms = DRM_MODE_DPMS_ON; 307*a0f75d24SRussell King armada_drm_crtc_update(dcrtc, true); 308f9a13bb3SRussell King drm_crtc_vblank_on(crtc); 309dbb4ca8aSRussell King 310dbb4ca8aSRussell King armada_drm_crtc_queue_state_event(crtc); 31196f60e37SRussell King } 31296f60e37SRussell King 31396f60e37SRussell King /* The mode_config.mutex will be held for this call */ 31496f60e37SRussell King static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc, 31596f60e37SRussell King const struct drm_display_mode *mode, struct drm_display_mode *adj) 31696f60e37SRussell King { 31796f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 31896f60e37SRussell King int ret; 31996f60e37SRussell King 32096f60e37SRussell King /* We can't do interlaced modes if we don't have the SPU_ADV_REG */ 32142e62ba7SRussell King if (!dcrtc->variant->has_spu_adv_reg && 32296f60e37SRussell King adj->flags & DRM_MODE_FLAG_INTERLACE) 32396f60e37SRussell King return false; 32496f60e37SRussell King 32596f60e37SRussell King /* Check whether the display mode is possible */ 32642e62ba7SRussell King ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL); 32796f60e37SRussell King if (ret) 32896f60e37SRussell King return false; 32996f60e37SRussell King 33096f60e37SRussell King return true; 33196f60e37SRussell King } 33296f60e37SRussell King 3335922a7d0SShawn Guo /* These are locked by dev->vbl_lock */ 3345922a7d0SShawn Guo static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask) 3355922a7d0SShawn Guo { 3365922a7d0SShawn Guo if (dcrtc->irq_ena & mask) { 3375922a7d0SShawn Guo dcrtc->irq_ena &= ~mask; 3385922a7d0SShawn Guo writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA); 3395922a7d0SShawn Guo } 3405922a7d0SShawn Guo } 3415922a7d0SShawn Guo 3425922a7d0SShawn Guo static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask) 3435922a7d0SShawn Guo { 3445922a7d0SShawn Guo if ((dcrtc->irq_ena & mask) != mask) { 3455922a7d0SShawn Guo dcrtc->irq_ena |= mask; 3465922a7d0SShawn Guo writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA); 3475922a7d0SShawn Guo if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask) 3485922a7d0SShawn Guo writel(0, dcrtc->base + LCD_SPU_IRQ_ISR); 3495922a7d0SShawn Guo } 3505922a7d0SShawn Guo } 3515922a7d0SShawn Guo 352e5d9ddfbSRussell King static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat) 35396f60e37SRussell King { 354dbb4ca8aSRussell King struct drm_pending_vblank_event *event; 35596f60e37SRussell King void __iomem *base = dcrtc->base; 3564a8506d2SRussell King struct drm_plane *ovl_plane; 35796f60e37SRussell King 35896f60e37SRussell King if (stat & DMA_FF_UNDERFLOW) 35996f60e37SRussell King DRM_ERROR("video underflow on crtc %u\n", dcrtc->num); 36096f60e37SRussell King if (stat & GRA_FF_UNDERFLOW) 36196f60e37SRussell King DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num); 36296f60e37SRussell King 36396f60e37SRussell King if (stat & VSYNC_IRQ) 3640ac28c57SGustavo Padovan drm_crtc_handle_vblank(&dcrtc->crtc); 36596f60e37SRussell King 3664a8506d2SRussell King ovl_plane = dcrtc->plane; 367ec6fb159SRussell King if (ovl_plane) 368ec6fb159SRussell King armada_drm_plane_work_run(dcrtc, ovl_plane); 36996f60e37SRussell King 370a3f6a18fSRussell King spin_lock(&dcrtc->irq_lock); 37196f60e37SRussell King if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) { 37296f60e37SRussell King int i = stat & GRA_FRAME_IRQ0 ? 0 : 1; 37396f60e37SRussell King uint32_t val; 37496f60e37SRussell King 37596f60e37SRussell King writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH); 37696f60e37SRussell King writel_relaxed(dcrtc->v[i].spu_v_h_total, 37796f60e37SRussell King base + LCD_SPUT_V_H_TOTAL); 37896f60e37SRussell King 37996f60e37SRussell King val = readl_relaxed(base + LCD_SPU_ADV_REG); 38096f60e37SRussell King val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN); 38196f60e37SRussell King val |= dcrtc->v[i].spu_adv_reg; 382662af0d8SRussell King writel_relaxed(val, base + LCD_SPU_ADV_REG); 38396f60e37SRussell King } 384662af0d8SRussell King 385662af0d8SRussell King if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) { 386662af0d8SRussell King writel_relaxed(dcrtc->cursor_hw_pos, 387662af0d8SRussell King base + LCD_SPU_HWC_OVSA_HPXL_VLN); 388662af0d8SRussell King writel_relaxed(dcrtc->cursor_hw_sz, 389662af0d8SRussell King base + LCD_SPU_HWC_HPXL_VLN); 390662af0d8SRussell King armada_updatel(CFG_HWC_ENA, 391662af0d8SRussell King CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA, 392662af0d8SRussell King base + LCD_SPU_DMA_CTRL0); 393662af0d8SRussell King dcrtc->cursor_update = false; 394662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 395662af0d8SRussell King } 396662af0d8SRussell King 39796f60e37SRussell King spin_unlock(&dcrtc->irq_lock); 39896f60e37SRussell King 399ec6fb159SRussell King if (stat & GRA_FRAME_IRQ) 400ec6fb159SRussell King armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary); 401dbb4ca8aSRussell King 402dbb4ca8aSRussell King if (stat & VSYNC_IRQ) { 403dbb4ca8aSRussell King event = xchg(&dcrtc->event, NULL); 404dbb4ca8aSRussell King if (event) { 405dbb4ca8aSRussell King spin_lock(&dcrtc->crtc.dev->event_lock); 406dbb4ca8aSRussell King drm_crtc_send_vblank_event(&dcrtc->crtc, event); 407dbb4ca8aSRussell King spin_unlock(&dcrtc->crtc.dev->event_lock); 408dbb4ca8aSRussell King drm_crtc_vblank_put(&dcrtc->crtc); 409dbb4ca8aSRussell King } 410dbb4ca8aSRussell King } 41196f60e37SRussell King } 41296f60e37SRussell King 413e5d9ddfbSRussell King static irqreturn_t armada_drm_irq(int irq, void *arg) 414e5d9ddfbSRussell King { 415e5d9ddfbSRussell King struct armada_crtc *dcrtc = arg; 416e5d9ddfbSRussell King u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR); 417e5d9ddfbSRussell King 418e5d9ddfbSRussell King /* 41992298c1cSRussell King * Reading the ISR appears to clear bits provided CLEAN_SPU_IRQ_ISR 42092298c1cSRussell King * is set. Writing has some other effect to acknowledge the IRQ - 42192298c1cSRussell King * without this, we only get a single IRQ. 422e5d9ddfbSRussell King */ 423e5d9ddfbSRussell King writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR); 424e5d9ddfbSRussell King 425c8a220c6SRussell King trace_armada_drm_irq(&dcrtc->crtc, stat); 426c8a220c6SRussell King 427e5d9ddfbSRussell King /* Mask out those interrupts we haven't enabled */ 428e5d9ddfbSRussell King v = stat & dcrtc->irq_ena; 429e5d9ddfbSRussell King 430e5d9ddfbSRussell King if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) { 431e5d9ddfbSRussell King armada_drm_crtc_irq(dcrtc, stat); 432e5d9ddfbSRussell King return IRQ_HANDLED; 433e5d9ddfbSRussell King } 434e5d9ddfbSRussell King return IRQ_NONE; 435e5d9ddfbSRussell King } 436e5d9ddfbSRussell King 43796f60e37SRussell King /* The mode_config.mutex will be held for this call */ 438c36045e1SRussell King static void armada_drm_crtc_mode_set_nofb(struct drm_crtc *crtc) 43996f60e37SRussell King { 440c36045e1SRussell King struct drm_display_mode *adj = &crtc->state->adjusted_mode; 44196f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 44296f60e37SRussell King struct armada_regs regs[17]; 44396f60e37SRussell King uint32_t lm, rm, tm, bm, val, sclk; 44496f60e37SRussell King unsigned long flags; 44596f60e37SRussell King unsigned i; 446c36045e1SRussell King bool interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE); 44796f60e37SRussell King 44837af35c7SRussell King i = 0; 44996f60e37SRussell King rm = adj->crtc_hsync_start - adj->crtc_hdisplay; 45096f60e37SRussell King lm = adj->crtc_htotal - adj->crtc_hsync_end; 45196f60e37SRussell King bm = adj->crtc_vsync_start - adj->crtc_vdisplay; 45296f60e37SRussell King tm = adj->crtc_vtotal - adj->crtc_vsync_end; 45396f60e37SRussell King 454a61c3922SRussell King DRM_DEBUG_KMS("[CRTC:%d:%s] mode " DRM_MODE_FMT "\n", 455a61c3922SRussell King crtc->base.id, crtc->name, 456a61c3922SRussell King adj->base.id, adj->name, adj->vrefresh, adj->clock, 457a61c3922SRussell King adj->crtc_hdisplay, adj->crtc_hsync_start, 458a61c3922SRussell King adj->crtc_hsync_end, adj->crtc_htotal, 459a61c3922SRussell King adj->crtc_vdisplay, adj->crtc_vsync_start, 460a61c3922SRussell King adj->crtc_vsync_end, adj->crtc_vtotal, 461a61c3922SRussell King adj->type, adj->flags); 462a61c3922SRussell King DRM_DEBUG_KMS("lm %d rm %d tm %d bm %d\n", lm, rm, tm, bm); 46396f60e37SRussell King 46496f60e37SRussell King /* Now compute the divider for real */ 46542e62ba7SRussell King dcrtc->variant->compute_clock(dcrtc, adj, &sclk); 46696f60e37SRussell King 46796f60e37SRussell King armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV); 46896f60e37SRussell King 46996f60e37SRussell King if (interlaced ^ dcrtc->interlaced) { 47096f60e37SRussell King if (adj->flags & DRM_MODE_FLAG_INTERLACE) 471accbaf6eSGustavo Padovan drm_crtc_vblank_get(&dcrtc->crtc); 47296f60e37SRussell King else 473accbaf6eSGustavo Padovan drm_crtc_vblank_put(&dcrtc->crtc); 47496f60e37SRussell King dcrtc->interlaced = interlaced; 47596f60e37SRussell King } 47696f60e37SRussell King 47796f60e37SRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 47896f60e37SRussell King 47996f60e37SRussell King /* Even interlaced/progressive frame */ 48096f60e37SRussell King dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 | 48196f60e37SRussell King adj->crtc_htotal; 48296f60e37SRussell King dcrtc->v[1].spu_v_porch = tm << 16 | bm; 48396f60e37SRussell King val = adj->crtc_hsync_start; 4844e4b3563SRussell King dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN; 48596f60e37SRussell King 48696f60e37SRussell King if (interlaced) { 48796f60e37SRussell King /* Odd interlaced frame */ 4884e4b3563SRussell King val -= adj->crtc_htotal / 2; 4894e4b3563SRussell King dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN; 49096f60e37SRussell King dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total + 49196f60e37SRussell King (1 << 16); 49296f60e37SRussell King dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1; 49396f60e37SRussell King } else { 49496f60e37SRussell King dcrtc->v[0] = dcrtc->v[1]; 49596f60e37SRussell King } 49696f60e37SRussell King 49796f60e37SRussell King val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay; 49896f60e37SRussell King 49996f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE); 50096f60e37SRussell King armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH); 50196f60e37SRussell King armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH); 50296f60e37SRussell King armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total, 50396f60e37SRussell King LCD_SPUT_V_H_TOTAL); 50496f60e37SRussell King 5054e4b3563SRussell King if (dcrtc->variant->has_spu_adv_reg) 50696f60e37SRussell King armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg, 50796f60e37SRussell King ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | 50896f60e37SRussell King ADV_VSYNCOFFEN, LCD_SPU_ADV_REG); 50996f60e37SRussell King 51096f60e37SRussell King val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0; 51196f60e37SRussell King armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1); 512155b8290SRussell King 513155b8290SRussell King /* 514155b8290SRussell King * The documentation doesn't indicate what the normal state of 515155b8290SRussell King * the sync signals are. Sebastian Hesselbart kindly probed 516155b8290SRussell King * these signals on his board to determine their state. 517155b8290SRussell King * 518155b8290SRussell King * The non-inverted state of the sync signals is active high. 519155b8290SRussell King * Setting these bits makes the appropriate signal active low. 520155b8290SRussell King */ 521155b8290SRussell King val = 0; 522155b8290SRussell King if (adj->flags & DRM_MODE_FLAG_NCSYNC) 523155b8290SRussell King val |= CFG_INV_CSYNC; 524155b8290SRussell King if (adj->flags & DRM_MODE_FLAG_NHSYNC) 525155b8290SRussell King val |= CFG_INV_HSYNC; 526155b8290SRussell King if (adj->flags & DRM_MODE_FLAG_NVSYNC) 527155b8290SRussell King val |= CFG_INV_VSYNC; 528155b8290SRussell King armada_reg_queue_mod(regs, i, val, CFG_INV_CSYNC | CFG_INV_HSYNC | 529155b8290SRussell King CFG_INV_VSYNC, LCD_SPU_DUMB_CTRL); 53096f60e37SRussell King armada_reg_queue_end(regs, i); 53196f60e37SRussell King 53296f60e37SRussell King armada_drm_crtc_update_regs(dcrtc, regs); 53396f60e37SRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 53496f60e37SRussell King } 53596f60e37SRussell King 53696f60e37SRussell King /* The mode_config.mutex will be held for this call */ 53796f60e37SRussell King static void armada_drm_crtc_disable(struct drm_crtc *crtc) 53896f60e37SRussell King { 53996f60e37SRussell King armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); 54028b30433SRussell King 54128b30433SRussell King /* Disable our primary plane when we disable the CRTC. */ 54228b30433SRussell King crtc->primary->funcs->disable_plane(crtc->primary, NULL); 54396f60e37SRussell King } 54496f60e37SRussell King 545c36045e1SRussell King static void armada_drm_crtc_atomic_begin(struct drm_crtc *crtc, 546c36045e1SRussell King struct drm_crtc_state *old_crtc_state) 547c36045e1SRussell King { 548c36045e1SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 549c36045e1SRussell King struct armada_plane *dplane; 550c36045e1SRussell King 551c36045e1SRussell King DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name); 552c36045e1SRussell King 553c36045e1SRussell King /* Wait 100ms for any plane works to complete */ 554c36045e1SRussell King dplane = drm_to_armada_plane(crtc->primary); 555c36045e1SRussell King if (WARN_ON(armada_drm_plane_work_wait(dplane, HZ / 10) == 0)) 556c36045e1SRussell King armada_drm_plane_work_cancel(dcrtc, dplane); 557c36045e1SRussell King 558c36045e1SRussell King dcrtc->regs_idx = 0; 559c36045e1SRussell King dcrtc->regs = dcrtc->atomic_regs; 560c36045e1SRussell King } 561c36045e1SRussell King 562c36045e1SRussell King static void armada_drm_crtc_atomic_flush(struct drm_crtc *crtc, 563c36045e1SRussell King struct drm_crtc_state *old_crtc_state) 564c36045e1SRussell King { 565c36045e1SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 566c36045e1SRussell King unsigned long flags; 567c36045e1SRussell King 568c36045e1SRussell King DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name); 569c36045e1SRussell King 570c36045e1SRussell King armada_reg_queue_end(dcrtc->regs, dcrtc->regs_idx); 571c36045e1SRussell King 572c36045e1SRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 573c36045e1SRussell King armada_drm_crtc_update_regs(dcrtc, dcrtc->regs); 574c36045e1SRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 575dbb4ca8aSRussell King 576dbb4ca8aSRussell King /* 577dbb4ca8aSRussell King * If we aren't doing a full modeset, then we need to queue 578dbb4ca8aSRussell King * the event here. 579dbb4ca8aSRussell King */ 580dbb4ca8aSRussell King if (!drm_atomic_crtc_needs_modeset(crtc->state)) 581dbb4ca8aSRussell King armada_drm_crtc_queue_state_event(crtc); 582c36045e1SRussell King } 583c36045e1SRussell King 58496f60e37SRussell King static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = { 58596f60e37SRussell King .dpms = armada_drm_crtc_dpms, 58696f60e37SRussell King .prepare = armada_drm_crtc_prepare, 58796f60e37SRussell King .commit = armada_drm_crtc_commit, 58896f60e37SRussell King .mode_fixup = armada_drm_crtc_mode_fixup, 589c36045e1SRussell King .mode_set = drm_helper_crtc_mode_set, 590c36045e1SRussell King .mode_set_nofb = armada_drm_crtc_mode_set_nofb, 591c36045e1SRussell King .mode_set_base = drm_helper_crtc_mode_set_base, 59296f60e37SRussell King .disable = armada_drm_crtc_disable, 593c36045e1SRussell King .atomic_begin = armada_drm_crtc_atomic_begin, 594c36045e1SRussell King .atomic_flush = armada_drm_crtc_atomic_flush, 59596f60e37SRussell King }; 59696f60e37SRussell King 597662af0d8SRussell King static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix, 598662af0d8SRussell King unsigned stride, unsigned width, unsigned height) 599662af0d8SRussell King { 600662af0d8SRussell King uint32_t addr; 601662af0d8SRussell King unsigned y; 602662af0d8SRussell King 603662af0d8SRussell King addr = SRAM_HWC32_RAM1; 604662af0d8SRussell King for (y = 0; y < height; y++) { 605662af0d8SRussell King uint32_t *p = &pix[y * stride]; 606662af0d8SRussell King unsigned x; 607662af0d8SRussell King 608662af0d8SRussell King for (x = 0; x < width; x++, p++) { 609662af0d8SRussell King uint32_t val = *p; 610662af0d8SRussell King 611662af0d8SRussell King val = (val & 0xff00ff00) | 612662af0d8SRussell King (val & 0x000000ff) << 16 | 613662af0d8SRussell King (val & 0x00ff0000) >> 16; 614662af0d8SRussell King 615662af0d8SRussell King writel_relaxed(val, 616662af0d8SRussell King base + LCD_SPU_SRAM_WRDAT); 617662af0d8SRussell King writel_relaxed(addr | SRAM_WRITE, 618662af0d8SRussell King base + LCD_SPU_SRAM_CTRL); 619c39b0695SRussell King readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN); 620662af0d8SRussell King addr += 1; 621662af0d8SRussell King if ((addr & 0x00ff) == 0) 622662af0d8SRussell King addr += 0xf00; 623662af0d8SRussell King if ((addr & 0x30ff) == 0) 624662af0d8SRussell King addr = SRAM_HWC32_RAM2; 625662af0d8SRussell King } 626662af0d8SRussell King } 627662af0d8SRussell King } 628662af0d8SRussell King 629662af0d8SRussell King static void armada_drm_crtc_cursor_tran(void __iomem *base) 630662af0d8SRussell King { 631662af0d8SRussell King unsigned addr; 632662af0d8SRussell King 633662af0d8SRussell King for (addr = 0; addr < 256; addr++) { 634662af0d8SRussell King /* write the default value */ 635662af0d8SRussell King writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT); 636662af0d8SRussell King writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN, 637662af0d8SRussell King base + LCD_SPU_SRAM_CTRL); 638662af0d8SRussell King } 639662af0d8SRussell King } 640662af0d8SRussell King 641662af0d8SRussell King static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload) 642662af0d8SRussell King { 643662af0d8SRussell King uint32_t xoff, xscr, w = dcrtc->cursor_w, s; 644662af0d8SRussell King uint32_t yoff, yscr, h = dcrtc->cursor_h; 645662af0d8SRussell King uint32_t para1; 646662af0d8SRussell King 647662af0d8SRussell King /* 648662af0d8SRussell King * Calculate the visible width and height of the cursor, 649662af0d8SRussell King * screen position, and the position in the cursor bitmap. 650662af0d8SRussell King */ 651662af0d8SRussell King if (dcrtc->cursor_x < 0) { 652662af0d8SRussell King xoff = -dcrtc->cursor_x; 653662af0d8SRussell King xscr = 0; 654662af0d8SRussell King w -= min(xoff, w); 655662af0d8SRussell King } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) { 656662af0d8SRussell King xoff = 0; 657662af0d8SRussell King xscr = dcrtc->cursor_x; 658662af0d8SRussell King w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0); 659662af0d8SRussell King } else { 660662af0d8SRussell King xoff = 0; 661662af0d8SRussell King xscr = dcrtc->cursor_x; 662662af0d8SRussell King } 663662af0d8SRussell King 664662af0d8SRussell King if (dcrtc->cursor_y < 0) { 665662af0d8SRussell King yoff = -dcrtc->cursor_y; 666662af0d8SRussell King yscr = 0; 667662af0d8SRussell King h -= min(yoff, h); 668662af0d8SRussell King } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) { 669662af0d8SRussell King yoff = 0; 670662af0d8SRussell King yscr = dcrtc->cursor_y; 671662af0d8SRussell King h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0); 672662af0d8SRussell King } else { 673662af0d8SRussell King yoff = 0; 674662af0d8SRussell King yscr = dcrtc->cursor_y; 675662af0d8SRussell King } 676662af0d8SRussell King 677662af0d8SRussell King /* On interlaced modes, the vertical cursor size must be halved */ 678662af0d8SRussell King s = dcrtc->cursor_w; 679662af0d8SRussell King if (dcrtc->interlaced) { 680662af0d8SRussell King s *= 2; 681662af0d8SRussell King yscr /= 2; 682662af0d8SRussell King h /= 2; 683662af0d8SRussell King } 684662af0d8SRussell King 685662af0d8SRussell King if (!dcrtc->cursor_obj || !h || !w) { 686662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 687662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 688662af0d8SRussell King dcrtc->cursor_update = false; 689662af0d8SRussell King armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0); 690662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 691662af0d8SRussell King return 0; 692662af0d8SRussell King } 693662af0d8SRussell King 694214612f9SRussell King spin_lock_irq(&dcrtc->irq_lock); 695662af0d8SRussell King para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1); 696662af0d8SRussell King armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32, 697662af0d8SRussell King dcrtc->base + LCD_SPU_SRAM_PARA1); 698214612f9SRussell King spin_unlock_irq(&dcrtc->irq_lock); 699662af0d8SRussell King 700662af0d8SRussell King /* 701662af0d8SRussell King * Initialize the transparency if the SRAM was powered down. 702662af0d8SRussell King * We must also reload the cursor data as well. 703662af0d8SRussell King */ 704662af0d8SRussell King if (!(para1 & CFG_CSB_256x32)) { 705662af0d8SRussell King armada_drm_crtc_cursor_tran(dcrtc->base); 706662af0d8SRussell King reload = true; 707662af0d8SRussell King } 708662af0d8SRussell King 709662af0d8SRussell King if (dcrtc->cursor_hw_sz != (h << 16 | w)) { 710662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 711662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 712662af0d8SRussell King dcrtc->cursor_update = false; 713662af0d8SRussell King armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0); 714662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 715662af0d8SRussell King reload = true; 716662af0d8SRussell King } 717662af0d8SRussell King if (reload) { 718662af0d8SRussell King struct armada_gem_object *obj = dcrtc->cursor_obj; 719662af0d8SRussell King uint32_t *pix; 720662af0d8SRussell King /* Set the top-left corner of the cursor image */ 721662af0d8SRussell King pix = obj->addr; 722662af0d8SRussell King pix += yoff * s + xoff; 723662af0d8SRussell King armada_load_cursor_argb(dcrtc->base, pix, s, w, h); 724662af0d8SRussell King } 725662af0d8SRussell King 726662af0d8SRussell King /* Reload the cursor position, size and enable in the IRQ handler */ 727662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 728662af0d8SRussell King dcrtc->cursor_hw_pos = yscr << 16 | xscr; 729662af0d8SRussell King dcrtc->cursor_hw_sz = h << 16 | w; 730662af0d8SRussell King dcrtc->cursor_update = true; 731662af0d8SRussell King armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 732662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 733662af0d8SRussell King 734662af0d8SRussell King return 0; 735662af0d8SRussell King } 736662af0d8SRussell King 737662af0d8SRussell King static void cursor_update(void *data) 738662af0d8SRussell King { 739662af0d8SRussell King armada_drm_crtc_cursor_update(data, true); 740662af0d8SRussell King } 741662af0d8SRussell King 742662af0d8SRussell King static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc, 743662af0d8SRussell King struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h) 744662af0d8SRussell King { 745662af0d8SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 746662af0d8SRussell King struct armada_gem_object *obj = NULL; 747662af0d8SRussell King int ret; 748662af0d8SRussell King 749662af0d8SRussell King /* If no cursor support, replicate drm's return value */ 75042e62ba7SRussell King if (!dcrtc->variant->has_spu_adv_reg) 751662af0d8SRussell King return -ENXIO; 752662af0d8SRussell King 753662af0d8SRussell King if (handle && w > 0 && h > 0) { 754662af0d8SRussell King /* maximum size is 64x32 or 32x64 */ 755662af0d8SRussell King if (w > 64 || h > 64 || (w > 32 && h > 32)) 756662af0d8SRussell King return -ENOMEM; 757662af0d8SRussell King 758a8ad0bd8SChris Wilson obj = armada_gem_object_lookup(file, handle); 759662af0d8SRussell King if (!obj) 760662af0d8SRussell King return -ENOENT; 761662af0d8SRussell King 762662af0d8SRussell King /* Must be a kernel-mapped object */ 763662af0d8SRussell King if (!obj->addr) { 7644c3cf375SHaneen Mohammed drm_gem_object_put_unlocked(&obj->obj); 765662af0d8SRussell King return -EINVAL; 766662af0d8SRussell King } 767662af0d8SRussell King 768662af0d8SRussell King if (obj->obj.size < w * h * 4) { 769662af0d8SRussell King DRM_ERROR("buffer is too small\n"); 7704c3cf375SHaneen Mohammed drm_gem_object_put_unlocked(&obj->obj); 771662af0d8SRussell King return -ENOMEM; 772662af0d8SRussell King } 773662af0d8SRussell King } 774662af0d8SRussell King 775662af0d8SRussell King if (dcrtc->cursor_obj) { 776662af0d8SRussell King dcrtc->cursor_obj->update = NULL; 777662af0d8SRussell King dcrtc->cursor_obj->update_data = NULL; 7784c3cf375SHaneen Mohammed drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj); 779662af0d8SRussell King } 780662af0d8SRussell King dcrtc->cursor_obj = obj; 781662af0d8SRussell King dcrtc->cursor_w = w; 782662af0d8SRussell King dcrtc->cursor_h = h; 783662af0d8SRussell King ret = armada_drm_crtc_cursor_update(dcrtc, true); 784662af0d8SRussell King if (obj) { 785662af0d8SRussell King obj->update_data = dcrtc; 786662af0d8SRussell King obj->update = cursor_update; 787662af0d8SRussell King } 788662af0d8SRussell King 789662af0d8SRussell King return ret; 790662af0d8SRussell King } 791662af0d8SRussell King 792662af0d8SRussell King static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y) 793662af0d8SRussell King { 794662af0d8SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 795662af0d8SRussell King int ret; 796662af0d8SRussell King 797662af0d8SRussell King /* If no cursor support, replicate drm's return value */ 79842e62ba7SRussell King if (!dcrtc->variant->has_spu_adv_reg) 799662af0d8SRussell King return -EFAULT; 800662af0d8SRussell King 801662af0d8SRussell King dcrtc->cursor_x = x; 802662af0d8SRussell King dcrtc->cursor_y = y; 803662af0d8SRussell King ret = armada_drm_crtc_cursor_update(dcrtc, false); 804662af0d8SRussell King 805662af0d8SRussell King return ret; 806662af0d8SRussell King } 807662af0d8SRussell King 80896f60e37SRussell King static void armada_drm_crtc_destroy(struct drm_crtc *crtc) 80996f60e37SRussell King { 81096f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 81196f60e37SRussell King struct armada_private *priv = crtc->dev->dev_private; 81296f60e37SRussell King 813662af0d8SRussell King if (dcrtc->cursor_obj) 8144c3cf375SHaneen Mohammed drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj); 815662af0d8SRussell King 81696f60e37SRussell King priv->dcrtc[dcrtc->num] = NULL; 81796f60e37SRussell King drm_crtc_cleanup(&dcrtc->crtc); 81896f60e37SRussell King 819a0fbb35eSRussell King if (dcrtc->variant->disable) 820a0fbb35eSRussell King dcrtc->variant->disable(dcrtc); 82196f60e37SRussell King 822e5d9ddfbSRussell King writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA); 823e5d9ddfbSRussell King 8249611cb93SRussell King of_node_put(dcrtc->crtc.port); 8259611cb93SRussell King 82696f60e37SRussell King kfree(dcrtc); 82796f60e37SRussell King } 82896f60e37SRussell King 82996f60e37SRussell King /* 83096f60e37SRussell King * The mode_config lock is held here, to prevent races between this 83196f60e37SRussell King * and a mode_set. 83296f60e37SRussell King */ 83396f60e37SRussell King static int armada_drm_crtc_page_flip(struct drm_crtc *crtc, 834de503ddfSRussell King struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, 835de503ddfSRussell King uint32_t page_flip_flags, struct drm_modeset_acquire_ctx *ctx) 83696f60e37SRussell King { 83796f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 838de503ddfSRussell King struct drm_plane *plane = crtc->primary; 839de503ddfSRussell King const struct drm_plane_helper_funcs *plane_funcs; 840de503ddfSRussell King struct drm_plane_state *state; 841eaa66279SRussell King struct armada_plane_work *work; 84296f60e37SRussell King int ret; 84396f60e37SRussell King 844de503ddfSRussell King /* Construct new state for the primary plane */ 845de503ddfSRussell King state = drm_atomic_helper_plane_duplicate_state(plane); 846de503ddfSRussell King if (!state) 84796f60e37SRussell King return -ENOMEM; 84896f60e37SRussell King 849de503ddfSRussell King drm_atomic_set_fb_for_plane(state, fb); 85096f60e37SRussell King 851de503ddfSRussell King work = armada_drm_crtc_alloc_plane_work(plane); 852de503ddfSRussell King if (!work) { 853de503ddfSRussell King ret = -ENOMEM; 854de503ddfSRussell King goto put_state; 85596f60e37SRussell King } 85696f60e37SRussell King 857de503ddfSRussell King /* Make sure we can get vblank interrupts */ 858de503ddfSRussell King ret = drm_crtc_vblank_get(crtc); 859de503ddfSRussell King if (ret) 860de503ddfSRussell King goto put_work; 861de503ddfSRussell King 86296f60e37SRussell King /* 863de503ddfSRussell King * If we have another work pending, we can't process this flip. 864de503ddfSRussell King * The modeset locks protect us from another user queuing a work 865de503ddfSRussell King * while we're setting up. 866c36045e1SRussell King */ 867de503ddfSRussell King if (drm_to_armada_plane(plane)->work) { 868de503ddfSRussell King ret = -EBUSY; 869de503ddfSRussell King goto put_vblank; 870de503ddfSRussell King } 871de503ddfSRussell King 872de503ddfSRussell King work->event = event; 873de503ddfSRussell King work->old_fb = plane->state->fb; 874de503ddfSRussell King 875de503ddfSRussell King /* 876de503ddfSRussell King * Hold a ref on the new fb while it's being displayed by the 877de503ddfSRussell King * hardware. The old fb refcount will be released in the worker. 878de503ddfSRussell King */ 879de503ddfSRussell King drm_framebuffer_get(state->fb); 880de503ddfSRussell King 881de503ddfSRussell King /* Point of no return */ 882de503ddfSRussell King swap(plane->state, state); 883de503ddfSRussell King 884de503ddfSRussell King dcrtc->regs_idx = 0; 885de503ddfSRussell King dcrtc->regs = work->regs; 886de503ddfSRussell King 887de503ddfSRussell King plane_funcs = plane->helper_private; 888de503ddfSRussell King plane_funcs->atomic_update(plane, state); 889de503ddfSRussell King armada_reg_queue_end(dcrtc->regs, dcrtc->regs_idx); 890de503ddfSRussell King 891de503ddfSRussell King /* Queue the work - this should never fail */ 892de503ddfSRussell King WARN_ON(armada_drm_plane_work_queue(dcrtc, work)); 893de503ddfSRussell King work = NULL; 894c36045e1SRussell King 895c36045e1SRussell King /* 89696f60e37SRussell King * Finally, if the display is blanked, we won't receive an 89796f60e37SRussell King * interrupt, so complete it now. 89896f60e37SRussell King */ 8994b5dda82SRussell King if (dpms_blanked(dcrtc->dpms)) 900de503ddfSRussell King armada_drm_plane_work_run(dcrtc, plane); 90196f60e37SRussell King 902de503ddfSRussell King put_vblank: 903de503ddfSRussell King drm_crtc_vblank_put(crtc); 904de503ddfSRussell King put_work: 905de503ddfSRussell King kfree(work); 906de503ddfSRussell King put_state: 907de503ddfSRussell King drm_atomic_helper_plane_destroy_state(plane, state); 908de503ddfSRussell King return ret; 90996f60e37SRussell King } 91096f60e37SRussell King 9115922a7d0SShawn Guo /* These are called under the vbl_lock. */ 9125922a7d0SShawn Guo static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc) 9135922a7d0SShawn Guo { 9145922a7d0SShawn Guo struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 91592298c1cSRussell King unsigned long flags; 9165922a7d0SShawn Guo 91792298c1cSRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 9185922a7d0SShawn Guo armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA); 91992298c1cSRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 9205922a7d0SShawn Guo return 0; 9215922a7d0SShawn Guo } 9225922a7d0SShawn Guo 9235922a7d0SShawn Guo static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc) 9245922a7d0SShawn Guo { 9255922a7d0SShawn Guo struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 92692298c1cSRussell King unsigned long flags; 9275922a7d0SShawn Guo 92892298c1cSRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 9295922a7d0SShawn Guo armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA); 93092298c1cSRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 9315922a7d0SShawn Guo } 9325922a7d0SShawn Guo 933a02fb90aSVille Syrjälä static const struct drm_crtc_funcs armada_crtc_funcs = { 934c36045e1SRussell King .reset = drm_atomic_helper_crtc_reset, 935662af0d8SRussell King .cursor_set = armada_drm_crtc_cursor_set, 936662af0d8SRussell King .cursor_move = armada_drm_crtc_cursor_move, 93796f60e37SRussell King .destroy = armada_drm_crtc_destroy, 93896f60e37SRussell King .set_config = drm_crtc_helper_set_config, 93996f60e37SRussell King .page_flip = armada_drm_crtc_page_flip, 940c36045e1SRussell King .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 941c36045e1SRussell King .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 9425922a7d0SShawn Guo .enable_vblank = armada_drm_crtc_enable_vblank, 9435922a7d0SShawn Guo .disable_vblank = armada_drm_crtc_disable_vblank, 94496f60e37SRussell King }; 94596f60e37SRussell King 9460fb2970bSRussell King static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev, 9479611cb93SRussell King struct resource *res, int irq, const struct armada_variant *variant, 9489611cb93SRussell King struct device_node *port) 94996f60e37SRussell King { 950d8c96083SRussell King struct armada_private *priv = drm->dev_private; 95196f60e37SRussell King struct armada_crtc *dcrtc; 952de32301bSRussell King struct armada_plane *primary; 95396f60e37SRussell King void __iomem *base; 95496f60e37SRussell King int ret; 95596f60e37SRussell King 956a7d7a143SLinus Torvalds base = devm_ioremap_resource(dev, res); 957c9d53c0fSJingoo Han if (IS_ERR(base)) 958c9d53c0fSJingoo Han return PTR_ERR(base); 95996f60e37SRussell King 96096f60e37SRussell King dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL); 96196f60e37SRussell King if (!dcrtc) { 96296f60e37SRussell King DRM_ERROR("failed to allocate Armada crtc\n"); 96396f60e37SRussell King return -ENOMEM; 96496f60e37SRussell King } 96596f60e37SRussell King 966d8c96083SRussell King if (dev != drm->dev) 967d8c96083SRussell King dev_set_drvdata(dev, dcrtc); 968d8c96083SRussell King 96942e62ba7SRussell King dcrtc->variant = variant; 97096f60e37SRussell King dcrtc->base = base; 971d8c96083SRussell King dcrtc->num = drm->mode_config.num_crtc; 97296f60e37SRussell King dcrtc->clk = ERR_PTR(-EINVAL); 97396f60e37SRussell King dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0; 97496f60e37SRussell King dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24; 97596f60e37SRussell King spin_lock_init(&dcrtc->irq_lock); 97696f60e37SRussell King dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR; 97796f60e37SRussell King 97896f60e37SRussell King /* Initialize some registers which we don't otherwise set */ 97996f60e37SRussell King writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV); 98096f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR); 98196f60e37SRussell King writel_relaxed(dcrtc->spu_iopad_ctrl, 98296f60e37SRussell King dcrtc->base + LCD_SPU_IOPAD_CONTROL); 98396f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0); 98496f60e37SRussell King writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 | 98596f60e37SRussell King CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 | 98696f60e37SRussell King CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1); 98796f60e37SRussell King writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1); 988e5d9ddfbSRussell King writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA); 98992298c1cSRussell King readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR); 990e5d9ddfbSRussell King writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR); 99196f60e37SRussell King 992e5d9ddfbSRussell King ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc", 993e5d9ddfbSRussell King dcrtc); 99433cd3c07SRussell King if (ret < 0) 99533cd3c07SRussell King goto err_crtc; 99696f60e37SRussell King 99742e62ba7SRussell King if (dcrtc->variant->init) { 998d8c96083SRussell King ret = dcrtc->variant->init(dcrtc, dev); 99933cd3c07SRussell King if (ret) 100033cd3c07SRussell King goto err_crtc; 100196f60e37SRussell King } 100296f60e37SRussell King 100396f60e37SRussell King /* Ensure AXI pipeline is enabled */ 100496f60e37SRussell King armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0); 100596f60e37SRussell King 100696f60e37SRussell King priv->dcrtc[dcrtc->num] = dcrtc; 100796f60e37SRussell King 10089611cb93SRussell King dcrtc->crtc.port = port; 10091c914cecSRussell King 1010de32301bSRussell King primary = kzalloc(sizeof(*primary), GFP_KERNEL); 101133cd3c07SRussell King if (!primary) { 101233cd3c07SRussell King ret = -ENOMEM; 101333cd3c07SRussell King goto err_crtc; 101433cd3c07SRussell King } 10151c914cecSRussell King 1016d40af7b1SRussell King ret = armada_drm_primary_plane_init(drm, primary); 1017de32301bSRussell King if (ret) { 1018de32301bSRussell King kfree(primary); 101933cd3c07SRussell King goto err_crtc; 1020de32301bSRussell King } 1021de32301bSRussell King 1022de32301bSRussell King ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL, 1023f9882876SVille Syrjälä &armada_crtc_funcs, NULL); 10241c914cecSRussell King if (ret) 10251c914cecSRussell King goto err_crtc_init; 10261c914cecSRussell King 102796f60e37SRussell King drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs); 102896f60e37SRussell King 1029d8c96083SRussell King return armada_overlay_plane_create(drm, 1 << dcrtc->num); 10301c914cecSRussell King 10311c914cecSRussell King err_crtc_init: 1032de32301bSRussell King primary->base.funcs->destroy(&primary->base); 103333cd3c07SRussell King err_crtc: 103433cd3c07SRussell King kfree(dcrtc); 103533cd3c07SRussell King 10361c914cecSRussell King return ret; 103796f60e37SRussell King } 1038d8c96083SRussell King 1039d8c96083SRussell King static int 1040d8c96083SRussell King armada_lcd_bind(struct device *dev, struct device *master, void *data) 1041d8c96083SRussell King { 1042d8c96083SRussell King struct platform_device *pdev = to_platform_device(dev); 1043d8c96083SRussell King struct drm_device *drm = data; 1044d8c96083SRussell King struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1045d8c96083SRussell King int irq = platform_get_irq(pdev, 0); 1046d8c96083SRussell King const struct armada_variant *variant; 10479611cb93SRussell King struct device_node *port = NULL; 1048d8c96083SRussell King 1049d8c96083SRussell King if (irq < 0) 1050d8c96083SRussell King return irq; 1051d8c96083SRussell King 1052d8c96083SRussell King if (!dev->of_node) { 1053d8c96083SRussell King const struct platform_device_id *id; 1054d8c96083SRussell King 1055d8c96083SRussell King id = platform_get_device_id(pdev); 1056d8c96083SRussell King if (!id) 1057d8c96083SRussell King return -ENXIO; 1058d8c96083SRussell King 1059d8c96083SRussell King variant = (const struct armada_variant *)id->driver_data; 1060d8c96083SRussell King } else { 1061d8c96083SRussell King const struct of_device_id *match; 10629611cb93SRussell King struct device_node *np, *parent = dev->of_node; 1063d8c96083SRussell King 1064d8c96083SRussell King match = of_match_device(dev->driver->of_match_table, dev); 1065d8c96083SRussell King if (!match) 1066d8c96083SRussell King return -ENXIO; 1067d8c96083SRussell King 10689611cb93SRussell King np = of_get_child_by_name(parent, "ports"); 10699611cb93SRussell King if (np) 10709611cb93SRussell King parent = np; 10719611cb93SRussell King port = of_get_child_by_name(parent, "port"); 10729611cb93SRussell King of_node_put(np); 10739611cb93SRussell King if (!port) { 10744bf99144SRob Herring dev_err(dev, "no port node found in %pOF\n", parent); 10759611cb93SRussell King return -ENXIO; 10769611cb93SRussell King } 10779611cb93SRussell King 1078d8c96083SRussell King variant = match->data; 1079d8c96083SRussell King } 1080d8c96083SRussell King 10819611cb93SRussell King return armada_drm_crtc_create(drm, dev, res, irq, variant, port); 1082d8c96083SRussell King } 1083d8c96083SRussell King 1084d8c96083SRussell King static void 1085d8c96083SRussell King armada_lcd_unbind(struct device *dev, struct device *master, void *data) 1086d8c96083SRussell King { 1087d8c96083SRussell King struct armada_crtc *dcrtc = dev_get_drvdata(dev); 1088d8c96083SRussell King 1089d8c96083SRussell King armada_drm_crtc_destroy(&dcrtc->crtc); 1090d8c96083SRussell King } 1091d8c96083SRussell King 1092d8c96083SRussell King static const struct component_ops armada_lcd_ops = { 1093d8c96083SRussell King .bind = armada_lcd_bind, 1094d8c96083SRussell King .unbind = armada_lcd_unbind, 1095d8c96083SRussell King }; 1096d8c96083SRussell King 1097d8c96083SRussell King static int armada_lcd_probe(struct platform_device *pdev) 1098d8c96083SRussell King { 1099d8c96083SRussell King return component_add(&pdev->dev, &armada_lcd_ops); 1100d8c96083SRussell King } 1101d8c96083SRussell King 1102d8c96083SRussell King static int armada_lcd_remove(struct platform_device *pdev) 1103d8c96083SRussell King { 1104d8c96083SRussell King component_del(&pdev->dev, &armada_lcd_ops); 1105d8c96083SRussell King return 0; 1106d8c96083SRussell King } 1107d8c96083SRussell King 110885909716SArvind Yadav static const struct of_device_id armada_lcd_of_match[] = { 1109d8c96083SRussell King { 1110d8c96083SRussell King .compatible = "marvell,dove-lcd", 1111d8c96083SRussell King .data = &armada510_ops, 1112d8c96083SRussell King }, 1113d8c96083SRussell King {} 1114d8c96083SRussell King }; 1115d8c96083SRussell King MODULE_DEVICE_TABLE(of, armada_lcd_of_match); 1116d8c96083SRussell King 1117d8c96083SRussell King static const struct platform_device_id armada_lcd_platform_ids[] = { 1118d8c96083SRussell King { 1119d8c96083SRussell King .name = "armada-lcd", 1120d8c96083SRussell King .driver_data = (unsigned long)&armada510_ops, 1121d8c96083SRussell King }, { 1122d8c96083SRussell King .name = "armada-510-lcd", 1123d8c96083SRussell King .driver_data = (unsigned long)&armada510_ops, 1124d8c96083SRussell King }, 1125d8c96083SRussell King { }, 1126d8c96083SRussell King }; 1127d8c96083SRussell King MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids); 1128d8c96083SRussell King 1129d8c96083SRussell King struct platform_driver armada_lcd_platform_driver = { 1130d8c96083SRussell King .probe = armada_lcd_probe, 1131d8c96083SRussell King .remove = armada_lcd_remove, 1132d8c96083SRussell King .driver = { 1133d8c96083SRussell King .name = "armada-lcd", 1134d8c96083SRussell King .owner = THIS_MODULE, 1135d8c96083SRussell King .of_match_table = armada_lcd_of_match, 1136d8c96083SRussell King }, 1137d8c96083SRussell King .id_table = armada_lcd_platform_ids, 1138d8c96083SRussell King }; 1139