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 9796f60e37SRussell King static void armada_drm_crtc_update(struct armada_crtc *dcrtc) 9896f60e37SRussell King { 9996f60e37SRussell King uint32_t dumb_ctrl; 10096f60e37SRussell King 10196f60e37SRussell King dumb_ctrl = dcrtc->cfg_dumb_ctrl; 10296f60e37SRussell King 10396f60e37SRussell King if (!dpms_blanked(dcrtc->dpms)) 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 */ 11296f60e37SRussell King if (dpms_blanked(dcrtc->dpms) && 11396f60e37SRussell King (dumb_ctrl & DUMB_MASK) == DUMB24_RGB888_0) { 11496f60e37SRussell King dumb_ctrl &= ~DUMB_MASK; 11596f60e37SRussell King dumb_ctrl |= DUMB_BLANK; 11696f60e37SRussell King } 11796f60e37SRussell King 118155b8290SRussell King armada_updatel(dumb_ctrl, 119155b8290SRussell King ~(CFG_INV_CSYNC | CFG_INV_HSYNC | CFG_INV_VSYNC), 120155b8290SRussell King dcrtc->base + LCD_SPU_DUMB_CTRL); 12196f60e37SRussell King } 12296f60e37SRussell King 1232839d45cSRussell King static void armada_drm_plane_work_call(struct armada_crtc *dcrtc, 1242839d45cSRussell King struct armada_plane_work *work, 1252839d45cSRussell King void (*fn)(struct armada_crtc *, struct armada_plane_work *)) 1262839d45cSRussell King { 1272839d45cSRussell King struct armada_plane *dplane = drm_to_armada_plane(work->plane); 128d924155dSRussell King struct drm_pending_vblank_event *event; 129d924155dSRussell King struct drm_framebuffer *fb; 1302839d45cSRussell King 1312839d45cSRussell King if (fn) 1322839d45cSRussell King fn(dcrtc, work); 1332839d45cSRussell King drm_crtc_vblank_put(&dcrtc->crtc); 1342839d45cSRussell King 135d924155dSRussell King event = work->event; 136d924155dSRussell King fb = work->old_fb; 137eb19be5bSRussell King if (event || fb) { 138eb19be5bSRussell King struct drm_device *dev = dcrtc->crtc.dev; 139eb19be5bSRussell King unsigned long flags; 140eb19be5bSRussell King 141eb19be5bSRussell King spin_lock_irqsave(&dev->event_lock, flags); 142eb19be5bSRussell King if (event) 143eb19be5bSRussell King drm_crtc_send_vblank_event(&dcrtc->crtc, event); 144b972a80fSRussell King if (fb) 145eb19be5bSRussell King __armada_drm_queue_unref_work(dev, fb); 146eb19be5bSRussell King spin_unlock_irqrestore(&dev->event_lock, flags); 147eb19be5bSRussell King } 148b972a80fSRussell King 149d924155dSRussell King if (work->need_kfree) 150d924155dSRussell King kfree(work); 151d924155dSRussell King 1522839d45cSRussell King wake_up(&dplane->frame_wait); 1532839d45cSRussell King } 1542839d45cSRussell King 1554b5dda82SRussell King static void armada_drm_plane_work_run(struct armada_crtc *dcrtc, 156ec6fb159SRussell King struct drm_plane *plane) 1574b5dda82SRussell King { 158ec6fb159SRussell King struct armada_plane *dplane = drm_to_armada_plane(plane); 159ec6fb159SRussell King struct armada_plane_work *work = xchg(&dplane->work, NULL); 1604b5dda82SRussell King 1614b5dda82SRussell King /* Handle any pending frame work. */ 1622839d45cSRussell King if (work) 1632839d45cSRussell King armada_drm_plane_work_call(dcrtc, work, work->fn); 1644b5dda82SRussell King } 1654b5dda82SRussell King 1664b5dda82SRussell King int armada_drm_plane_work_queue(struct armada_crtc *dcrtc, 167eaab0130SRussell King struct armada_plane_work *work) 1684b5dda82SRussell King { 169eaab0130SRussell King struct armada_plane *plane = drm_to_armada_plane(work->plane); 1704b5dda82SRussell King int ret; 1714b5dda82SRussell King 172accbaf6eSGustavo Padovan ret = drm_crtc_vblank_get(&dcrtc->crtc); 173c93dfdcdSRussell King if (ret) 1744b5dda82SRussell King return ret; 1754b5dda82SRussell King 1764b5dda82SRussell King ret = cmpxchg(&plane->work, NULL, work) ? -EBUSY : 0; 1774b5dda82SRussell King if (ret) 178accbaf6eSGustavo Padovan drm_crtc_vblank_put(&dcrtc->crtc); 1794b5dda82SRussell King 1804b5dda82SRussell King return ret; 1814b5dda82SRussell King } 1824b5dda82SRussell King 1834b5dda82SRussell King int armada_drm_plane_work_wait(struct armada_plane *plane, long timeout) 1844b5dda82SRussell King { 1854b5dda82SRussell King return wait_event_timeout(plane->frame_wait, !plane->work, timeout); 1864b5dda82SRussell King } 1874b5dda82SRussell King 188d3b84215SRussell King void armada_drm_plane_work_cancel(struct armada_crtc *dcrtc, 189d3b84215SRussell King struct armada_plane *dplane) 1907c8f7e1aSRussell King { 191d3b84215SRussell King struct armada_plane_work *work = xchg(&dplane->work, NULL); 1927c8f7e1aSRussell King 1934a8506d2SRussell King if (work) 1942839d45cSRussell King armada_drm_plane_work_call(dcrtc, work, work->cancel); 19596f60e37SRussell King } 19696f60e37SRussell King 197709ffd82SRussell King static void armada_drm_crtc_complete_frame_work(struct armada_crtc *dcrtc, 19865724a19SRussell King struct armada_plane_work *work) 19996f60e37SRussell King { 200709ffd82SRussell King unsigned long flags; 20196f60e37SRussell King 202709ffd82SRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 203eaa66279SRussell King armada_drm_crtc_update_regs(dcrtc, work->regs); 204709ffd82SRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 205709ffd82SRussell King } 20696f60e37SRussell King 207eaa66279SRussell King static struct armada_plane_work * 208eaa66279SRussell King armada_drm_crtc_alloc_plane_work(struct drm_plane *plane) 209901bb889SRussell King { 210eaa66279SRussell King struct armada_plane_work *work; 211901bb889SRussell King int i = 0; 212901bb889SRussell King 213901bb889SRussell King work = kzalloc(sizeof(*work), GFP_KERNEL); 214901bb889SRussell King if (!work) 215901bb889SRussell King return NULL; 216901bb889SRussell King 217eaa66279SRussell King work->plane = plane; 218eaa66279SRussell King work->fn = armada_drm_crtc_complete_frame_work; 219d924155dSRussell King work->need_kfree = true; 220901bb889SRussell King armada_reg_queue_end(work->regs, i); 221901bb889SRussell King 222901bb889SRussell King return work; 22396f60e37SRussell King } 22496f60e37SRussell King 22596f60e37SRussell King static void armada_drm_vblank_off(struct armada_crtc *dcrtc) 22696f60e37SRussell King { 22796f60e37SRussell King /* 22896f60e37SRussell King * Tell the DRM core that vblank IRQs aren't going to happen for 22996f60e37SRussell King * a while. This cleans up any pending vblank events for us. 23096f60e37SRussell King */ 231178e561fSRussell King drm_crtc_vblank_off(&dcrtc->crtc); 232ec6fb159SRussell King armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary); 23396f60e37SRussell King } 23496f60e37SRussell King 23596f60e37SRussell King /* The mode_config.mutex will be held for this call */ 23696f60e37SRussell King static void armada_drm_crtc_dpms(struct drm_crtc *crtc, int dpms) 23796f60e37SRussell King { 23896f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 23996f60e37SRussell King 240ea908ba8SRussell King if (dpms_blanked(dcrtc->dpms) != dpms_blanked(dpms)) { 24196f60e37SRussell King if (dpms_blanked(dpms)) 24296f60e37SRussell King armada_drm_vblank_off(dcrtc); 243ea908ba8SRussell King else if (!IS_ERR(dcrtc->clk)) 244ea908ba8SRussell King WARN_ON(clk_prepare_enable(dcrtc->clk)); 245ea908ba8SRussell King dcrtc->dpms = dpms; 246ea908ba8SRussell King armada_drm_crtc_update(dcrtc); 247ea908ba8SRussell King if (!dpms_blanked(dpms)) 248178e561fSRussell King drm_crtc_vblank_on(&dcrtc->crtc); 249ea908ba8SRussell King else if (!IS_ERR(dcrtc->clk)) 250ea908ba8SRussell King clk_disable_unprepare(dcrtc->clk); 251ea908ba8SRussell King } else if (dcrtc->dpms != dpms) { 252ea908ba8SRussell King dcrtc->dpms = dpms; 25396f60e37SRussell King } 25496f60e37SRussell King } 25596f60e37SRussell King 25696f60e37SRussell King /* 25796f60e37SRussell King * Prepare for a mode set. Turn off overlay to ensure that we don't end 25896f60e37SRussell King * up with the overlay size being bigger than the active screen size. 25996f60e37SRussell King * We rely upon X refreshing this state after the mode set has completed. 26096f60e37SRussell King * 26196f60e37SRussell King * The mode_config.mutex will be held for this call 26296f60e37SRussell King */ 26396f60e37SRussell King static void armada_drm_crtc_prepare(struct drm_crtc *crtc) 26496f60e37SRussell King { 26596f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 26696f60e37SRussell King struct drm_plane *plane; 26796f60e37SRussell King 26896f60e37SRussell King /* 26996f60e37SRussell King * If we have an overlay plane associated with this CRTC, disable 27096f60e37SRussell King * it before the modeset to avoid its coordinates being outside 271f8e14069SRussell King * the new mode parameters. 27296f60e37SRussell King */ 27396f60e37SRussell King plane = dcrtc->plane; 274890ca8dfSRussell King if (plane) { 275f8e14069SRussell King drm_plane_force_disable(plane); 276890ca8dfSRussell King WARN_ON(!armada_drm_plane_work_wait(drm_to_armada_plane(plane), 277890ca8dfSRussell King HZ)); 278890ca8dfSRussell King } 279f9a13bb3SRussell King 280f9a13bb3SRussell King /* Wait for pending flips to complete */ 281f9a13bb3SRussell King armada_drm_plane_work_wait(drm_to_armada_plane(dcrtc->crtc.primary), 282f9a13bb3SRussell King MAX_SCHEDULE_TIMEOUT); 283f9a13bb3SRussell King 284f9a13bb3SRussell King drm_crtc_vblank_off(crtc); 285f9a13bb3SRussell King 286155b8290SRussell King armada_updatel(0, CFG_DUMB_ENA, dcrtc->base + LCD_SPU_DUMB_CTRL); 28796f60e37SRussell King } 28896f60e37SRussell King 28996f60e37SRussell King /* The mode_config.mutex will be held for this call */ 29096f60e37SRussell King static void armada_drm_crtc_commit(struct drm_crtc *crtc) 29196f60e37SRussell King { 29296f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 29396f60e37SRussell King 29496f60e37SRussell King dcrtc->dpms = DRM_MODE_DPMS_ON; 29596f60e37SRussell King armada_drm_crtc_update(dcrtc); 296f9a13bb3SRussell King drm_crtc_vblank_on(crtc); 29796f60e37SRussell King } 29896f60e37SRussell King 29996f60e37SRussell King /* The mode_config.mutex will be held for this call */ 30096f60e37SRussell King static bool armada_drm_crtc_mode_fixup(struct drm_crtc *crtc, 30196f60e37SRussell King const struct drm_display_mode *mode, struct drm_display_mode *adj) 30296f60e37SRussell King { 30396f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 30496f60e37SRussell King int ret; 30596f60e37SRussell King 30696f60e37SRussell King /* We can't do interlaced modes if we don't have the SPU_ADV_REG */ 30742e62ba7SRussell King if (!dcrtc->variant->has_spu_adv_reg && 30896f60e37SRussell King adj->flags & DRM_MODE_FLAG_INTERLACE) 30996f60e37SRussell King return false; 31096f60e37SRussell King 31196f60e37SRussell King /* Check whether the display mode is possible */ 31242e62ba7SRussell King ret = dcrtc->variant->compute_clock(dcrtc, adj, NULL); 31396f60e37SRussell King if (ret) 31496f60e37SRussell King return false; 31596f60e37SRussell King 31696f60e37SRussell King return true; 31796f60e37SRussell King } 31896f60e37SRussell King 3195922a7d0SShawn Guo /* These are locked by dev->vbl_lock */ 3205922a7d0SShawn Guo static void armada_drm_crtc_disable_irq(struct armada_crtc *dcrtc, u32 mask) 3215922a7d0SShawn Guo { 3225922a7d0SShawn Guo if (dcrtc->irq_ena & mask) { 3235922a7d0SShawn Guo dcrtc->irq_ena &= ~mask; 3245922a7d0SShawn Guo writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA); 3255922a7d0SShawn Guo } 3265922a7d0SShawn Guo } 3275922a7d0SShawn Guo 3285922a7d0SShawn Guo static void armada_drm_crtc_enable_irq(struct armada_crtc *dcrtc, u32 mask) 3295922a7d0SShawn Guo { 3305922a7d0SShawn Guo if ((dcrtc->irq_ena & mask) != mask) { 3315922a7d0SShawn Guo dcrtc->irq_ena |= mask; 3325922a7d0SShawn Guo writel(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA); 3335922a7d0SShawn Guo if (readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR) & mask) 3345922a7d0SShawn Guo writel(0, dcrtc->base + LCD_SPU_IRQ_ISR); 3355922a7d0SShawn Guo } 3365922a7d0SShawn Guo } 3375922a7d0SShawn Guo 338e5d9ddfbSRussell King static void armada_drm_crtc_irq(struct armada_crtc *dcrtc, u32 stat) 33996f60e37SRussell King { 34096f60e37SRussell King void __iomem *base = dcrtc->base; 3414a8506d2SRussell King struct drm_plane *ovl_plane; 34296f60e37SRussell King 34396f60e37SRussell King if (stat & DMA_FF_UNDERFLOW) 34496f60e37SRussell King DRM_ERROR("video underflow on crtc %u\n", dcrtc->num); 34596f60e37SRussell King if (stat & GRA_FF_UNDERFLOW) 34696f60e37SRussell King DRM_ERROR("graphics underflow on crtc %u\n", dcrtc->num); 34796f60e37SRussell King 34896f60e37SRussell King if (stat & VSYNC_IRQ) 3490ac28c57SGustavo Padovan drm_crtc_handle_vblank(&dcrtc->crtc); 35096f60e37SRussell King 3514a8506d2SRussell King ovl_plane = dcrtc->plane; 352ec6fb159SRussell King if (ovl_plane) 353ec6fb159SRussell King armada_drm_plane_work_run(dcrtc, ovl_plane); 35496f60e37SRussell King 355a3f6a18fSRussell King spin_lock(&dcrtc->irq_lock); 35696f60e37SRussell King if (stat & GRA_FRAME_IRQ && dcrtc->interlaced) { 35796f60e37SRussell King int i = stat & GRA_FRAME_IRQ0 ? 0 : 1; 35896f60e37SRussell King uint32_t val; 35996f60e37SRussell King 36096f60e37SRussell King writel_relaxed(dcrtc->v[i].spu_v_porch, base + LCD_SPU_V_PORCH); 36196f60e37SRussell King writel_relaxed(dcrtc->v[i].spu_v_h_total, 36296f60e37SRussell King base + LCD_SPUT_V_H_TOTAL); 36396f60e37SRussell King 36496f60e37SRussell King val = readl_relaxed(base + LCD_SPU_ADV_REG); 36596f60e37SRussell King val &= ~(ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | ADV_VSYNCOFFEN); 36696f60e37SRussell King val |= dcrtc->v[i].spu_adv_reg; 367662af0d8SRussell King writel_relaxed(val, base + LCD_SPU_ADV_REG); 36896f60e37SRussell King } 369662af0d8SRussell King 370662af0d8SRussell King if (stat & DUMB_FRAMEDONE && dcrtc->cursor_update) { 371662af0d8SRussell King writel_relaxed(dcrtc->cursor_hw_pos, 372662af0d8SRussell King base + LCD_SPU_HWC_OVSA_HPXL_VLN); 373662af0d8SRussell King writel_relaxed(dcrtc->cursor_hw_sz, 374662af0d8SRussell King base + LCD_SPU_HWC_HPXL_VLN); 375662af0d8SRussell King armada_updatel(CFG_HWC_ENA, 376662af0d8SRussell King CFG_HWC_ENA | CFG_HWC_1BITMOD | CFG_HWC_1BITENA, 377662af0d8SRussell King base + LCD_SPU_DMA_CTRL0); 378662af0d8SRussell King dcrtc->cursor_update = false; 379662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 380662af0d8SRussell King } 381662af0d8SRussell King 38296f60e37SRussell King spin_unlock(&dcrtc->irq_lock); 38396f60e37SRussell King 384ec6fb159SRussell King if (stat & GRA_FRAME_IRQ) 385ec6fb159SRussell King armada_drm_plane_work_run(dcrtc, dcrtc->crtc.primary); 38696f60e37SRussell King } 38796f60e37SRussell King 388e5d9ddfbSRussell King static irqreturn_t armada_drm_irq(int irq, void *arg) 389e5d9ddfbSRussell King { 390e5d9ddfbSRussell King struct armada_crtc *dcrtc = arg; 391e5d9ddfbSRussell King u32 v, stat = readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR); 392e5d9ddfbSRussell King 393e5d9ddfbSRussell King /* 39492298c1cSRussell King * Reading the ISR appears to clear bits provided CLEAN_SPU_IRQ_ISR 39592298c1cSRussell King * is set. Writing has some other effect to acknowledge the IRQ - 39692298c1cSRussell King * without this, we only get a single IRQ. 397e5d9ddfbSRussell King */ 398e5d9ddfbSRussell King writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR); 399e5d9ddfbSRussell King 400c8a220c6SRussell King trace_armada_drm_irq(&dcrtc->crtc, stat); 401c8a220c6SRussell King 402e5d9ddfbSRussell King /* Mask out those interrupts we haven't enabled */ 403e5d9ddfbSRussell King v = stat & dcrtc->irq_ena; 404e5d9ddfbSRussell King 405e5d9ddfbSRussell King if (v & (VSYNC_IRQ|GRA_FRAME_IRQ|DUMB_FRAMEDONE)) { 406e5d9ddfbSRussell King armada_drm_crtc_irq(dcrtc, stat); 407e5d9ddfbSRussell King return IRQ_HANDLED; 408e5d9ddfbSRussell King } 409e5d9ddfbSRussell King return IRQ_NONE; 410e5d9ddfbSRussell King } 411e5d9ddfbSRussell King 41296f60e37SRussell King /* The mode_config.mutex will be held for this call */ 413c36045e1SRussell King static void armada_drm_crtc_mode_set_nofb(struct drm_crtc *crtc) 41496f60e37SRussell King { 415c36045e1SRussell King struct drm_display_mode *adj = &crtc->state->adjusted_mode; 41696f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 41796f60e37SRussell King struct armada_regs regs[17]; 41896f60e37SRussell King uint32_t lm, rm, tm, bm, val, sclk; 41996f60e37SRussell King unsigned long flags; 42096f60e37SRussell King unsigned i; 421c36045e1SRussell King bool interlaced = !!(adj->flags & DRM_MODE_FLAG_INTERLACE); 42296f60e37SRussell King 42337af35c7SRussell King i = 0; 42496f60e37SRussell King rm = adj->crtc_hsync_start - adj->crtc_hdisplay; 42596f60e37SRussell King lm = adj->crtc_htotal - adj->crtc_hsync_end; 42696f60e37SRussell King bm = adj->crtc_vsync_start - adj->crtc_vdisplay; 42796f60e37SRussell King tm = adj->crtc_vtotal - adj->crtc_vsync_end; 42896f60e37SRussell King 429a61c3922SRussell King DRM_DEBUG_KMS("[CRTC:%d:%s] mode " DRM_MODE_FMT "\n", 430a61c3922SRussell King crtc->base.id, crtc->name, 431a61c3922SRussell King adj->base.id, adj->name, adj->vrefresh, adj->clock, 432a61c3922SRussell King adj->crtc_hdisplay, adj->crtc_hsync_start, 433a61c3922SRussell King adj->crtc_hsync_end, adj->crtc_htotal, 434a61c3922SRussell King adj->crtc_vdisplay, adj->crtc_vsync_start, 435a61c3922SRussell King adj->crtc_vsync_end, adj->crtc_vtotal, 436a61c3922SRussell King adj->type, adj->flags); 437a61c3922SRussell King DRM_DEBUG_KMS("lm %d rm %d tm %d bm %d\n", lm, rm, tm, bm); 43896f60e37SRussell King 439e0ac5e9bSRussell King /* 440e0ac5e9bSRussell King * If we are blanked, we would have disabled the clock. Re-enable 441e0ac5e9bSRussell King * it so that compute_clock() does the right thing. 442e0ac5e9bSRussell King */ 443e0ac5e9bSRussell King if (!IS_ERR(dcrtc->clk) && dpms_blanked(dcrtc->dpms)) 444e0ac5e9bSRussell King WARN_ON(clk_prepare_enable(dcrtc->clk)); 445e0ac5e9bSRussell King 44696f60e37SRussell King /* Now compute the divider for real */ 44742e62ba7SRussell King dcrtc->variant->compute_clock(dcrtc, adj, &sclk); 44896f60e37SRussell King 44996f60e37SRussell King armada_reg_queue_set(regs, i, sclk, LCD_CFG_SCLK_DIV); 45096f60e37SRussell King 45196f60e37SRussell King if (interlaced ^ dcrtc->interlaced) { 45296f60e37SRussell King if (adj->flags & DRM_MODE_FLAG_INTERLACE) 453accbaf6eSGustavo Padovan drm_crtc_vblank_get(&dcrtc->crtc); 45496f60e37SRussell King else 455accbaf6eSGustavo Padovan drm_crtc_vblank_put(&dcrtc->crtc); 45696f60e37SRussell King dcrtc->interlaced = interlaced; 45796f60e37SRussell King } 45896f60e37SRussell King 45996f60e37SRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 46096f60e37SRussell King 46196f60e37SRussell King /* Even interlaced/progressive frame */ 46296f60e37SRussell King dcrtc->v[1].spu_v_h_total = adj->crtc_vtotal << 16 | 46396f60e37SRussell King adj->crtc_htotal; 46496f60e37SRussell King dcrtc->v[1].spu_v_porch = tm << 16 | bm; 46596f60e37SRussell King val = adj->crtc_hsync_start; 466*4e4b3563SRussell King dcrtc->v[1].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN; 46796f60e37SRussell King 46896f60e37SRussell King if (interlaced) { 46996f60e37SRussell King /* Odd interlaced frame */ 470*4e4b3563SRussell King val -= adj->crtc_htotal / 2; 471*4e4b3563SRussell King dcrtc->v[0].spu_adv_reg = val << 20 | val | ADV_VSYNCOFFEN; 47296f60e37SRussell King dcrtc->v[0].spu_v_h_total = dcrtc->v[1].spu_v_h_total + 47396f60e37SRussell King (1 << 16); 47496f60e37SRussell King dcrtc->v[0].spu_v_porch = dcrtc->v[1].spu_v_porch + 1; 47596f60e37SRussell King } else { 47696f60e37SRussell King dcrtc->v[0] = dcrtc->v[1]; 47796f60e37SRussell King } 47896f60e37SRussell King 47996f60e37SRussell King val = adj->crtc_vdisplay << 16 | adj->crtc_hdisplay; 48096f60e37SRussell King 48196f60e37SRussell King armada_reg_queue_set(regs, i, val, LCD_SPU_V_H_ACTIVE); 48296f60e37SRussell King armada_reg_queue_set(regs, i, (lm << 16) | rm, LCD_SPU_H_PORCH); 48396f60e37SRussell King armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_porch, LCD_SPU_V_PORCH); 48496f60e37SRussell King armada_reg_queue_set(regs, i, dcrtc->v[0].spu_v_h_total, 48596f60e37SRussell King LCD_SPUT_V_H_TOTAL); 48696f60e37SRussell King 487*4e4b3563SRussell King if (dcrtc->variant->has_spu_adv_reg) 48896f60e37SRussell King armada_reg_queue_mod(regs, i, dcrtc->v[0].spu_adv_reg, 48996f60e37SRussell King ADV_VSYNC_L_OFF | ADV_VSYNC_H_OFF | 49096f60e37SRussell King ADV_VSYNCOFFEN, LCD_SPU_ADV_REG); 49196f60e37SRussell King 49296f60e37SRussell King val = adj->flags & DRM_MODE_FLAG_NVSYNC ? CFG_VSYNC_INV : 0; 49396f60e37SRussell King armada_reg_queue_mod(regs, i, val, CFG_VSYNC_INV, LCD_SPU_DMA_CTRL1); 494155b8290SRussell King 495155b8290SRussell King /* 496155b8290SRussell King * The documentation doesn't indicate what the normal state of 497155b8290SRussell King * the sync signals are. Sebastian Hesselbart kindly probed 498155b8290SRussell King * these signals on his board to determine their state. 499155b8290SRussell King * 500155b8290SRussell King * The non-inverted state of the sync signals is active high. 501155b8290SRussell King * Setting these bits makes the appropriate signal active low. 502155b8290SRussell King */ 503155b8290SRussell King val = 0; 504155b8290SRussell King if (adj->flags & DRM_MODE_FLAG_NCSYNC) 505155b8290SRussell King val |= CFG_INV_CSYNC; 506155b8290SRussell King if (adj->flags & DRM_MODE_FLAG_NHSYNC) 507155b8290SRussell King val |= CFG_INV_HSYNC; 508155b8290SRussell King if (adj->flags & DRM_MODE_FLAG_NVSYNC) 509155b8290SRussell King val |= CFG_INV_VSYNC; 510155b8290SRussell King armada_reg_queue_mod(regs, i, val, CFG_INV_CSYNC | CFG_INV_HSYNC | 511155b8290SRussell King CFG_INV_VSYNC, LCD_SPU_DUMB_CTRL); 51296f60e37SRussell King armada_reg_queue_end(regs, i); 51396f60e37SRussell King 51496f60e37SRussell King armada_drm_crtc_update_regs(dcrtc, regs); 51596f60e37SRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 51696f60e37SRussell King } 51796f60e37SRussell King 51896f60e37SRussell King /* The mode_config.mutex will be held for this call */ 51996f60e37SRussell King static void armada_drm_crtc_disable(struct drm_crtc *crtc) 52096f60e37SRussell King { 52196f60e37SRussell King armada_drm_crtc_dpms(crtc, DRM_MODE_DPMS_OFF); 52228b30433SRussell King 52328b30433SRussell King /* Disable our primary plane when we disable the CRTC. */ 52428b30433SRussell King crtc->primary->funcs->disable_plane(crtc->primary, NULL); 52596f60e37SRussell King } 52696f60e37SRussell King 527c36045e1SRussell King static void armada_drm_crtc_atomic_begin(struct drm_crtc *crtc, 528c36045e1SRussell King struct drm_crtc_state *old_crtc_state) 529c36045e1SRussell King { 530c36045e1SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 531c36045e1SRussell King struct armada_plane *dplane; 532c36045e1SRussell King 533c36045e1SRussell King DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name); 534c36045e1SRussell King 535c36045e1SRussell King /* Wait 100ms for any plane works to complete */ 536c36045e1SRussell King dplane = drm_to_armada_plane(crtc->primary); 537c36045e1SRussell King if (WARN_ON(armada_drm_plane_work_wait(dplane, HZ / 10) == 0)) 538c36045e1SRussell King armada_drm_plane_work_cancel(dcrtc, dplane); 539c36045e1SRussell King 540c36045e1SRussell King dcrtc->regs_idx = 0; 541c36045e1SRussell King dcrtc->regs = dcrtc->atomic_regs; 542c36045e1SRussell King } 543c36045e1SRussell King 544c36045e1SRussell King static void armada_drm_crtc_atomic_flush(struct drm_crtc *crtc, 545c36045e1SRussell King struct drm_crtc_state *old_crtc_state) 546c36045e1SRussell King { 547c36045e1SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 548c36045e1SRussell King unsigned long flags; 549c36045e1SRussell King 550c36045e1SRussell King DRM_DEBUG_KMS("[CRTC:%d:%s]\n", crtc->base.id, crtc->name); 551c36045e1SRussell King 552c36045e1SRussell King armada_reg_queue_end(dcrtc->regs, dcrtc->regs_idx); 553c36045e1SRussell King 554c36045e1SRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 555c36045e1SRussell King armada_drm_crtc_update_regs(dcrtc, dcrtc->regs); 556c36045e1SRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 557c36045e1SRussell King } 558c36045e1SRussell King 55996f60e37SRussell King static const struct drm_crtc_helper_funcs armada_crtc_helper_funcs = { 56096f60e37SRussell King .dpms = armada_drm_crtc_dpms, 56196f60e37SRussell King .prepare = armada_drm_crtc_prepare, 56296f60e37SRussell King .commit = armada_drm_crtc_commit, 56396f60e37SRussell King .mode_fixup = armada_drm_crtc_mode_fixup, 564c36045e1SRussell King .mode_set = drm_helper_crtc_mode_set, 565c36045e1SRussell King .mode_set_nofb = armada_drm_crtc_mode_set_nofb, 566c36045e1SRussell King .mode_set_base = drm_helper_crtc_mode_set_base, 56796f60e37SRussell King .disable = armada_drm_crtc_disable, 568c36045e1SRussell King .atomic_begin = armada_drm_crtc_atomic_begin, 569c36045e1SRussell King .atomic_flush = armada_drm_crtc_atomic_flush, 57096f60e37SRussell King }; 57196f60e37SRussell King 572662af0d8SRussell King static void armada_load_cursor_argb(void __iomem *base, uint32_t *pix, 573662af0d8SRussell King unsigned stride, unsigned width, unsigned height) 574662af0d8SRussell King { 575662af0d8SRussell King uint32_t addr; 576662af0d8SRussell King unsigned y; 577662af0d8SRussell King 578662af0d8SRussell King addr = SRAM_HWC32_RAM1; 579662af0d8SRussell King for (y = 0; y < height; y++) { 580662af0d8SRussell King uint32_t *p = &pix[y * stride]; 581662af0d8SRussell King unsigned x; 582662af0d8SRussell King 583662af0d8SRussell King for (x = 0; x < width; x++, p++) { 584662af0d8SRussell King uint32_t val = *p; 585662af0d8SRussell King 586662af0d8SRussell King val = (val & 0xff00ff00) | 587662af0d8SRussell King (val & 0x000000ff) << 16 | 588662af0d8SRussell King (val & 0x00ff0000) >> 16; 589662af0d8SRussell King 590662af0d8SRussell King writel_relaxed(val, 591662af0d8SRussell King base + LCD_SPU_SRAM_WRDAT); 592662af0d8SRussell King writel_relaxed(addr | SRAM_WRITE, 593662af0d8SRussell King base + LCD_SPU_SRAM_CTRL); 594c39b0695SRussell King readl_relaxed(base + LCD_SPU_HWC_OVSA_HPXL_VLN); 595662af0d8SRussell King addr += 1; 596662af0d8SRussell King if ((addr & 0x00ff) == 0) 597662af0d8SRussell King addr += 0xf00; 598662af0d8SRussell King if ((addr & 0x30ff) == 0) 599662af0d8SRussell King addr = SRAM_HWC32_RAM2; 600662af0d8SRussell King } 601662af0d8SRussell King } 602662af0d8SRussell King } 603662af0d8SRussell King 604662af0d8SRussell King static void armada_drm_crtc_cursor_tran(void __iomem *base) 605662af0d8SRussell King { 606662af0d8SRussell King unsigned addr; 607662af0d8SRussell King 608662af0d8SRussell King for (addr = 0; addr < 256; addr++) { 609662af0d8SRussell King /* write the default value */ 610662af0d8SRussell King writel_relaxed(0x55555555, base + LCD_SPU_SRAM_WRDAT); 611662af0d8SRussell King writel_relaxed(addr | SRAM_WRITE | SRAM_HWC32_TRAN, 612662af0d8SRussell King base + LCD_SPU_SRAM_CTRL); 613662af0d8SRussell King } 614662af0d8SRussell King } 615662af0d8SRussell King 616662af0d8SRussell King static int armada_drm_crtc_cursor_update(struct armada_crtc *dcrtc, bool reload) 617662af0d8SRussell King { 618662af0d8SRussell King uint32_t xoff, xscr, w = dcrtc->cursor_w, s; 619662af0d8SRussell King uint32_t yoff, yscr, h = dcrtc->cursor_h; 620662af0d8SRussell King uint32_t para1; 621662af0d8SRussell King 622662af0d8SRussell King /* 623662af0d8SRussell King * Calculate the visible width and height of the cursor, 624662af0d8SRussell King * screen position, and the position in the cursor bitmap. 625662af0d8SRussell King */ 626662af0d8SRussell King if (dcrtc->cursor_x < 0) { 627662af0d8SRussell King xoff = -dcrtc->cursor_x; 628662af0d8SRussell King xscr = 0; 629662af0d8SRussell King w -= min(xoff, w); 630662af0d8SRussell King } else if (dcrtc->cursor_x + w > dcrtc->crtc.mode.hdisplay) { 631662af0d8SRussell King xoff = 0; 632662af0d8SRussell King xscr = dcrtc->cursor_x; 633662af0d8SRussell King w = max_t(int, dcrtc->crtc.mode.hdisplay - dcrtc->cursor_x, 0); 634662af0d8SRussell King } else { 635662af0d8SRussell King xoff = 0; 636662af0d8SRussell King xscr = dcrtc->cursor_x; 637662af0d8SRussell King } 638662af0d8SRussell King 639662af0d8SRussell King if (dcrtc->cursor_y < 0) { 640662af0d8SRussell King yoff = -dcrtc->cursor_y; 641662af0d8SRussell King yscr = 0; 642662af0d8SRussell King h -= min(yoff, h); 643662af0d8SRussell King } else if (dcrtc->cursor_y + h > dcrtc->crtc.mode.vdisplay) { 644662af0d8SRussell King yoff = 0; 645662af0d8SRussell King yscr = dcrtc->cursor_y; 646662af0d8SRussell King h = max_t(int, dcrtc->crtc.mode.vdisplay - dcrtc->cursor_y, 0); 647662af0d8SRussell King } else { 648662af0d8SRussell King yoff = 0; 649662af0d8SRussell King yscr = dcrtc->cursor_y; 650662af0d8SRussell King } 651662af0d8SRussell King 652662af0d8SRussell King /* On interlaced modes, the vertical cursor size must be halved */ 653662af0d8SRussell King s = dcrtc->cursor_w; 654662af0d8SRussell King if (dcrtc->interlaced) { 655662af0d8SRussell King s *= 2; 656662af0d8SRussell King yscr /= 2; 657662af0d8SRussell King h /= 2; 658662af0d8SRussell King } 659662af0d8SRussell King 660662af0d8SRussell King if (!dcrtc->cursor_obj || !h || !w) { 661662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 662662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 663662af0d8SRussell King dcrtc->cursor_update = false; 664662af0d8SRussell King armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0); 665662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 666662af0d8SRussell King return 0; 667662af0d8SRussell King } 668662af0d8SRussell King 669214612f9SRussell King spin_lock_irq(&dcrtc->irq_lock); 670662af0d8SRussell King para1 = readl_relaxed(dcrtc->base + LCD_SPU_SRAM_PARA1); 671662af0d8SRussell King armada_updatel(CFG_CSB_256x32, CFG_CSB_256x32 | CFG_PDWN256x32, 672662af0d8SRussell King dcrtc->base + LCD_SPU_SRAM_PARA1); 673214612f9SRussell King spin_unlock_irq(&dcrtc->irq_lock); 674662af0d8SRussell King 675662af0d8SRussell King /* 676662af0d8SRussell King * Initialize the transparency if the SRAM was powered down. 677662af0d8SRussell King * We must also reload the cursor data as well. 678662af0d8SRussell King */ 679662af0d8SRussell King if (!(para1 & CFG_CSB_256x32)) { 680662af0d8SRussell King armada_drm_crtc_cursor_tran(dcrtc->base); 681662af0d8SRussell King reload = true; 682662af0d8SRussell King } 683662af0d8SRussell King 684662af0d8SRussell King if (dcrtc->cursor_hw_sz != (h << 16 | w)) { 685662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 686662af0d8SRussell King armada_drm_crtc_disable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 687662af0d8SRussell King dcrtc->cursor_update = false; 688662af0d8SRussell King armada_updatel(0, CFG_HWC_ENA, dcrtc->base + LCD_SPU_DMA_CTRL0); 689662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 690662af0d8SRussell King reload = true; 691662af0d8SRussell King } 692662af0d8SRussell King if (reload) { 693662af0d8SRussell King struct armada_gem_object *obj = dcrtc->cursor_obj; 694662af0d8SRussell King uint32_t *pix; 695662af0d8SRussell King /* Set the top-left corner of the cursor image */ 696662af0d8SRussell King pix = obj->addr; 697662af0d8SRussell King pix += yoff * s + xoff; 698662af0d8SRussell King armada_load_cursor_argb(dcrtc->base, pix, s, w, h); 699662af0d8SRussell King } 700662af0d8SRussell King 701662af0d8SRussell King /* Reload the cursor position, size and enable in the IRQ handler */ 702662af0d8SRussell King spin_lock_irq(&dcrtc->irq_lock); 703662af0d8SRussell King dcrtc->cursor_hw_pos = yscr << 16 | xscr; 704662af0d8SRussell King dcrtc->cursor_hw_sz = h << 16 | w; 705662af0d8SRussell King dcrtc->cursor_update = true; 706662af0d8SRussell King armada_drm_crtc_enable_irq(dcrtc, DUMB_FRAMEDONE_ENA); 707662af0d8SRussell King spin_unlock_irq(&dcrtc->irq_lock); 708662af0d8SRussell King 709662af0d8SRussell King return 0; 710662af0d8SRussell King } 711662af0d8SRussell King 712662af0d8SRussell King static void cursor_update(void *data) 713662af0d8SRussell King { 714662af0d8SRussell King armada_drm_crtc_cursor_update(data, true); 715662af0d8SRussell King } 716662af0d8SRussell King 717662af0d8SRussell King static int armada_drm_crtc_cursor_set(struct drm_crtc *crtc, 718662af0d8SRussell King struct drm_file *file, uint32_t handle, uint32_t w, uint32_t h) 719662af0d8SRussell King { 720662af0d8SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 721662af0d8SRussell King struct armada_gem_object *obj = NULL; 722662af0d8SRussell King int ret; 723662af0d8SRussell King 724662af0d8SRussell King /* If no cursor support, replicate drm's return value */ 72542e62ba7SRussell King if (!dcrtc->variant->has_spu_adv_reg) 726662af0d8SRussell King return -ENXIO; 727662af0d8SRussell King 728662af0d8SRussell King if (handle && w > 0 && h > 0) { 729662af0d8SRussell King /* maximum size is 64x32 or 32x64 */ 730662af0d8SRussell King if (w > 64 || h > 64 || (w > 32 && h > 32)) 731662af0d8SRussell King return -ENOMEM; 732662af0d8SRussell King 733a8ad0bd8SChris Wilson obj = armada_gem_object_lookup(file, handle); 734662af0d8SRussell King if (!obj) 735662af0d8SRussell King return -ENOENT; 736662af0d8SRussell King 737662af0d8SRussell King /* Must be a kernel-mapped object */ 738662af0d8SRussell King if (!obj->addr) { 7394c3cf375SHaneen Mohammed drm_gem_object_put_unlocked(&obj->obj); 740662af0d8SRussell King return -EINVAL; 741662af0d8SRussell King } 742662af0d8SRussell King 743662af0d8SRussell King if (obj->obj.size < w * h * 4) { 744662af0d8SRussell King DRM_ERROR("buffer is too small\n"); 7454c3cf375SHaneen Mohammed drm_gem_object_put_unlocked(&obj->obj); 746662af0d8SRussell King return -ENOMEM; 747662af0d8SRussell King } 748662af0d8SRussell King } 749662af0d8SRussell King 750662af0d8SRussell King if (dcrtc->cursor_obj) { 751662af0d8SRussell King dcrtc->cursor_obj->update = NULL; 752662af0d8SRussell King dcrtc->cursor_obj->update_data = NULL; 7534c3cf375SHaneen Mohammed drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj); 754662af0d8SRussell King } 755662af0d8SRussell King dcrtc->cursor_obj = obj; 756662af0d8SRussell King dcrtc->cursor_w = w; 757662af0d8SRussell King dcrtc->cursor_h = h; 758662af0d8SRussell King ret = armada_drm_crtc_cursor_update(dcrtc, true); 759662af0d8SRussell King if (obj) { 760662af0d8SRussell King obj->update_data = dcrtc; 761662af0d8SRussell King obj->update = cursor_update; 762662af0d8SRussell King } 763662af0d8SRussell King 764662af0d8SRussell King return ret; 765662af0d8SRussell King } 766662af0d8SRussell King 767662af0d8SRussell King static int armada_drm_crtc_cursor_move(struct drm_crtc *crtc, int x, int y) 768662af0d8SRussell King { 769662af0d8SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 770662af0d8SRussell King int ret; 771662af0d8SRussell King 772662af0d8SRussell King /* If no cursor support, replicate drm's return value */ 77342e62ba7SRussell King if (!dcrtc->variant->has_spu_adv_reg) 774662af0d8SRussell King return -EFAULT; 775662af0d8SRussell King 776662af0d8SRussell King dcrtc->cursor_x = x; 777662af0d8SRussell King dcrtc->cursor_y = y; 778662af0d8SRussell King ret = armada_drm_crtc_cursor_update(dcrtc, false); 779662af0d8SRussell King 780662af0d8SRussell King return ret; 781662af0d8SRussell King } 782662af0d8SRussell King 78396f60e37SRussell King static void armada_drm_crtc_destroy(struct drm_crtc *crtc) 78496f60e37SRussell King { 78596f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 78696f60e37SRussell King struct armada_private *priv = crtc->dev->dev_private; 78796f60e37SRussell King 788662af0d8SRussell King if (dcrtc->cursor_obj) 7894c3cf375SHaneen Mohammed drm_gem_object_put_unlocked(&dcrtc->cursor_obj->obj); 790662af0d8SRussell King 79196f60e37SRussell King priv->dcrtc[dcrtc->num] = NULL; 79296f60e37SRussell King drm_crtc_cleanup(&dcrtc->crtc); 79396f60e37SRussell King 79496f60e37SRussell King if (!IS_ERR(dcrtc->clk)) 79596f60e37SRussell King clk_disable_unprepare(dcrtc->clk); 79696f60e37SRussell King 797e5d9ddfbSRussell King writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ENA); 798e5d9ddfbSRussell King 7999611cb93SRussell King of_node_put(dcrtc->crtc.port); 8009611cb93SRussell King 80196f60e37SRussell King kfree(dcrtc); 80296f60e37SRussell King } 80396f60e37SRussell King 80496f60e37SRussell King /* 80596f60e37SRussell King * The mode_config lock is held here, to prevent races between this 80696f60e37SRussell King * and a mode_set. 80796f60e37SRussell King */ 80896f60e37SRussell King static int armada_drm_crtc_page_flip(struct drm_crtc *crtc, 809de503ddfSRussell King struct drm_framebuffer *fb, struct drm_pending_vblank_event *event, 810de503ddfSRussell King uint32_t page_flip_flags, struct drm_modeset_acquire_ctx *ctx) 81196f60e37SRussell King { 81296f60e37SRussell King struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 813de503ddfSRussell King struct drm_plane *plane = crtc->primary; 814de503ddfSRussell King const struct drm_plane_helper_funcs *plane_funcs; 815de503ddfSRussell King struct drm_plane_state *state; 816eaa66279SRussell King struct armada_plane_work *work; 81796f60e37SRussell King int ret; 81896f60e37SRussell King 819de503ddfSRussell King /* Construct new state for the primary plane */ 820de503ddfSRussell King state = drm_atomic_helper_plane_duplicate_state(plane); 821de503ddfSRussell King if (!state) 82296f60e37SRussell King return -ENOMEM; 82396f60e37SRussell King 824de503ddfSRussell King drm_atomic_set_fb_for_plane(state, fb); 82596f60e37SRussell King 826de503ddfSRussell King work = armada_drm_crtc_alloc_plane_work(plane); 827de503ddfSRussell King if (!work) { 828de503ddfSRussell King ret = -ENOMEM; 829de503ddfSRussell King goto put_state; 83096f60e37SRussell King } 83196f60e37SRussell King 832de503ddfSRussell King /* Make sure we can get vblank interrupts */ 833de503ddfSRussell King ret = drm_crtc_vblank_get(crtc); 834de503ddfSRussell King if (ret) 835de503ddfSRussell King goto put_work; 836de503ddfSRussell King 83796f60e37SRussell King /* 838de503ddfSRussell King * If we have another work pending, we can't process this flip. 839de503ddfSRussell King * The modeset locks protect us from another user queuing a work 840de503ddfSRussell King * while we're setting up. 841c36045e1SRussell King */ 842de503ddfSRussell King if (drm_to_armada_plane(plane)->work) { 843de503ddfSRussell King ret = -EBUSY; 844de503ddfSRussell King goto put_vblank; 845de503ddfSRussell King } 846de503ddfSRussell King 847de503ddfSRussell King work->event = event; 848de503ddfSRussell King work->old_fb = plane->state->fb; 849de503ddfSRussell King 850de503ddfSRussell King /* 851de503ddfSRussell King * Hold a ref on the new fb while it's being displayed by the 852de503ddfSRussell King * hardware. The old fb refcount will be released in the worker. 853de503ddfSRussell King */ 854de503ddfSRussell King drm_framebuffer_get(state->fb); 855de503ddfSRussell King 856de503ddfSRussell King /* Point of no return */ 857de503ddfSRussell King swap(plane->state, state); 858de503ddfSRussell King 859de503ddfSRussell King dcrtc->regs_idx = 0; 860de503ddfSRussell King dcrtc->regs = work->regs; 861de503ddfSRussell King 862de503ddfSRussell King plane_funcs = plane->helper_private; 863de503ddfSRussell King plane_funcs->atomic_update(plane, state); 864de503ddfSRussell King armada_reg_queue_end(dcrtc->regs, dcrtc->regs_idx); 865de503ddfSRussell King 866de503ddfSRussell King /* Queue the work - this should never fail */ 867de503ddfSRussell King WARN_ON(armada_drm_plane_work_queue(dcrtc, work)); 868de503ddfSRussell King work = NULL; 869c36045e1SRussell King 870c36045e1SRussell King /* 87196f60e37SRussell King * Finally, if the display is blanked, we won't receive an 87296f60e37SRussell King * interrupt, so complete it now. 87396f60e37SRussell King */ 8744b5dda82SRussell King if (dpms_blanked(dcrtc->dpms)) 875de503ddfSRussell King armada_drm_plane_work_run(dcrtc, plane); 87696f60e37SRussell King 877de503ddfSRussell King put_vblank: 878de503ddfSRussell King drm_crtc_vblank_put(crtc); 879de503ddfSRussell King put_work: 880de503ddfSRussell King kfree(work); 881de503ddfSRussell King put_state: 882de503ddfSRussell King drm_atomic_helper_plane_destroy_state(plane, state); 883de503ddfSRussell King return ret; 88496f60e37SRussell King } 88596f60e37SRussell King 8865922a7d0SShawn Guo /* These are called under the vbl_lock. */ 8875922a7d0SShawn Guo static int armada_drm_crtc_enable_vblank(struct drm_crtc *crtc) 8885922a7d0SShawn Guo { 8895922a7d0SShawn Guo struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 89092298c1cSRussell King unsigned long flags; 8915922a7d0SShawn Guo 89292298c1cSRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 8935922a7d0SShawn Guo armada_drm_crtc_enable_irq(dcrtc, VSYNC_IRQ_ENA); 89492298c1cSRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 8955922a7d0SShawn Guo return 0; 8965922a7d0SShawn Guo } 8975922a7d0SShawn Guo 8985922a7d0SShawn Guo static void armada_drm_crtc_disable_vblank(struct drm_crtc *crtc) 8995922a7d0SShawn Guo { 9005922a7d0SShawn Guo struct armada_crtc *dcrtc = drm_to_armada_crtc(crtc); 90192298c1cSRussell King unsigned long flags; 9025922a7d0SShawn Guo 90392298c1cSRussell King spin_lock_irqsave(&dcrtc->irq_lock, flags); 9045922a7d0SShawn Guo armada_drm_crtc_disable_irq(dcrtc, VSYNC_IRQ_ENA); 90592298c1cSRussell King spin_unlock_irqrestore(&dcrtc->irq_lock, flags); 9065922a7d0SShawn Guo } 9075922a7d0SShawn Guo 908a02fb90aSVille Syrjälä static const struct drm_crtc_funcs armada_crtc_funcs = { 909c36045e1SRussell King .reset = drm_atomic_helper_crtc_reset, 910662af0d8SRussell King .cursor_set = armada_drm_crtc_cursor_set, 911662af0d8SRussell King .cursor_move = armada_drm_crtc_cursor_move, 91296f60e37SRussell King .destroy = armada_drm_crtc_destroy, 91396f60e37SRussell King .set_config = drm_crtc_helper_set_config, 91496f60e37SRussell King .page_flip = armada_drm_crtc_page_flip, 915c36045e1SRussell King .atomic_duplicate_state = drm_atomic_helper_crtc_duplicate_state, 916c36045e1SRussell King .atomic_destroy_state = drm_atomic_helper_crtc_destroy_state, 9175922a7d0SShawn Guo .enable_vblank = armada_drm_crtc_enable_vblank, 9185922a7d0SShawn Guo .disable_vblank = armada_drm_crtc_disable_vblank, 91996f60e37SRussell King }; 92096f60e37SRussell King 9210fb2970bSRussell King static int armada_drm_crtc_create(struct drm_device *drm, struct device *dev, 9229611cb93SRussell King struct resource *res, int irq, const struct armada_variant *variant, 9239611cb93SRussell King struct device_node *port) 92496f60e37SRussell King { 925d8c96083SRussell King struct armada_private *priv = drm->dev_private; 92696f60e37SRussell King struct armada_crtc *dcrtc; 927de32301bSRussell King struct armada_plane *primary; 92896f60e37SRussell King void __iomem *base; 92996f60e37SRussell King int ret; 93096f60e37SRussell King 931a7d7a143SLinus Torvalds base = devm_ioremap_resource(dev, res); 932c9d53c0fSJingoo Han if (IS_ERR(base)) 933c9d53c0fSJingoo Han return PTR_ERR(base); 93496f60e37SRussell King 93596f60e37SRussell King dcrtc = kzalloc(sizeof(*dcrtc), GFP_KERNEL); 93696f60e37SRussell King if (!dcrtc) { 93796f60e37SRussell King DRM_ERROR("failed to allocate Armada crtc\n"); 93896f60e37SRussell King return -ENOMEM; 93996f60e37SRussell King } 94096f60e37SRussell King 941d8c96083SRussell King if (dev != drm->dev) 942d8c96083SRussell King dev_set_drvdata(dev, dcrtc); 943d8c96083SRussell King 94442e62ba7SRussell King dcrtc->variant = variant; 94596f60e37SRussell King dcrtc->base = base; 946d8c96083SRussell King dcrtc->num = drm->mode_config.num_crtc; 94796f60e37SRussell King dcrtc->clk = ERR_PTR(-EINVAL); 94896f60e37SRussell King dcrtc->cfg_dumb_ctrl = DUMB24_RGB888_0; 94996f60e37SRussell King dcrtc->spu_iopad_ctrl = CFG_VSCALE_LN_EN | CFG_IOPAD_DUMB24; 95096f60e37SRussell King spin_lock_init(&dcrtc->irq_lock); 95196f60e37SRussell King dcrtc->irq_ena = CLEAN_SPU_IRQ_ISR; 95296f60e37SRussell King 95396f60e37SRussell King /* Initialize some registers which we don't otherwise set */ 95496f60e37SRussell King writel_relaxed(0x00000001, dcrtc->base + LCD_CFG_SCLK_DIV); 95596f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_BLANKCOLOR); 95696f60e37SRussell King writel_relaxed(dcrtc->spu_iopad_ctrl, 95796f60e37SRussell King dcrtc->base + LCD_SPU_IOPAD_CONTROL); 95896f60e37SRussell King writel_relaxed(0x00000000, dcrtc->base + LCD_SPU_SRAM_PARA0); 95996f60e37SRussell King writel_relaxed(CFG_PDWN256x32 | CFG_PDWN256x24 | CFG_PDWN256x8 | 96096f60e37SRussell King CFG_PDWN32x32 | CFG_PDWN16x66 | CFG_PDWN32x66 | 96196f60e37SRussell King CFG_PDWN64x66, dcrtc->base + LCD_SPU_SRAM_PARA1); 96296f60e37SRussell King writel_relaxed(0x2032ff81, dcrtc->base + LCD_SPU_DMA_CTRL1); 963e5d9ddfbSRussell King writel_relaxed(dcrtc->irq_ena, dcrtc->base + LCD_SPU_IRQ_ENA); 96492298c1cSRussell King readl_relaxed(dcrtc->base + LCD_SPU_IRQ_ISR); 965e5d9ddfbSRussell King writel_relaxed(0, dcrtc->base + LCD_SPU_IRQ_ISR); 96696f60e37SRussell King 967e5d9ddfbSRussell King ret = devm_request_irq(dev, irq, armada_drm_irq, 0, "armada_drm_crtc", 968e5d9ddfbSRussell King dcrtc); 96933cd3c07SRussell King if (ret < 0) 97033cd3c07SRussell King goto err_crtc; 97196f60e37SRussell King 97242e62ba7SRussell King if (dcrtc->variant->init) { 973d8c96083SRussell King ret = dcrtc->variant->init(dcrtc, dev); 97433cd3c07SRussell King if (ret) 97533cd3c07SRussell King goto err_crtc; 97696f60e37SRussell King } 97796f60e37SRussell King 97896f60e37SRussell King /* Ensure AXI pipeline is enabled */ 97996f60e37SRussell King armada_updatel(CFG_ARBFAST_ENA, 0, dcrtc->base + LCD_SPU_DMA_CTRL0); 98096f60e37SRussell King 98196f60e37SRussell King priv->dcrtc[dcrtc->num] = dcrtc; 98296f60e37SRussell King 9839611cb93SRussell King dcrtc->crtc.port = port; 9841c914cecSRussell King 985de32301bSRussell King primary = kzalloc(sizeof(*primary), GFP_KERNEL); 98633cd3c07SRussell King if (!primary) { 98733cd3c07SRussell King ret = -ENOMEM; 98833cd3c07SRussell King goto err_crtc; 98933cd3c07SRussell King } 9901c914cecSRussell King 991d40af7b1SRussell King ret = armada_drm_primary_plane_init(drm, primary); 992de32301bSRussell King if (ret) { 993de32301bSRussell King kfree(primary); 99433cd3c07SRussell King goto err_crtc; 995de32301bSRussell King } 996de32301bSRussell King 997de32301bSRussell King ret = drm_crtc_init_with_planes(drm, &dcrtc->crtc, &primary->base, NULL, 998f9882876SVille Syrjälä &armada_crtc_funcs, NULL); 9991c914cecSRussell King if (ret) 10001c914cecSRussell King goto err_crtc_init; 10011c914cecSRussell King 100296f60e37SRussell King drm_crtc_helper_add(&dcrtc->crtc, &armada_crtc_helper_funcs); 100396f60e37SRussell King 1004d8c96083SRussell King return armada_overlay_plane_create(drm, 1 << dcrtc->num); 10051c914cecSRussell King 10061c914cecSRussell King err_crtc_init: 1007de32301bSRussell King primary->base.funcs->destroy(&primary->base); 100833cd3c07SRussell King err_crtc: 100933cd3c07SRussell King kfree(dcrtc); 101033cd3c07SRussell King 10111c914cecSRussell King return ret; 101296f60e37SRussell King } 1013d8c96083SRussell King 1014d8c96083SRussell King static int 1015d8c96083SRussell King armada_lcd_bind(struct device *dev, struct device *master, void *data) 1016d8c96083SRussell King { 1017d8c96083SRussell King struct platform_device *pdev = to_platform_device(dev); 1018d8c96083SRussell King struct drm_device *drm = data; 1019d8c96083SRussell King struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1020d8c96083SRussell King int irq = platform_get_irq(pdev, 0); 1021d8c96083SRussell King const struct armada_variant *variant; 10229611cb93SRussell King struct device_node *port = NULL; 1023d8c96083SRussell King 1024d8c96083SRussell King if (irq < 0) 1025d8c96083SRussell King return irq; 1026d8c96083SRussell King 1027d8c96083SRussell King if (!dev->of_node) { 1028d8c96083SRussell King const struct platform_device_id *id; 1029d8c96083SRussell King 1030d8c96083SRussell King id = platform_get_device_id(pdev); 1031d8c96083SRussell King if (!id) 1032d8c96083SRussell King return -ENXIO; 1033d8c96083SRussell King 1034d8c96083SRussell King variant = (const struct armada_variant *)id->driver_data; 1035d8c96083SRussell King } else { 1036d8c96083SRussell King const struct of_device_id *match; 10379611cb93SRussell King struct device_node *np, *parent = dev->of_node; 1038d8c96083SRussell King 1039d8c96083SRussell King match = of_match_device(dev->driver->of_match_table, dev); 1040d8c96083SRussell King if (!match) 1041d8c96083SRussell King return -ENXIO; 1042d8c96083SRussell King 10439611cb93SRussell King np = of_get_child_by_name(parent, "ports"); 10449611cb93SRussell King if (np) 10459611cb93SRussell King parent = np; 10469611cb93SRussell King port = of_get_child_by_name(parent, "port"); 10479611cb93SRussell King of_node_put(np); 10489611cb93SRussell King if (!port) { 10494bf99144SRob Herring dev_err(dev, "no port node found in %pOF\n", parent); 10509611cb93SRussell King return -ENXIO; 10519611cb93SRussell King } 10529611cb93SRussell King 1053d8c96083SRussell King variant = match->data; 1054d8c96083SRussell King } 1055d8c96083SRussell King 10569611cb93SRussell King return armada_drm_crtc_create(drm, dev, res, irq, variant, port); 1057d8c96083SRussell King } 1058d8c96083SRussell King 1059d8c96083SRussell King static void 1060d8c96083SRussell King armada_lcd_unbind(struct device *dev, struct device *master, void *data) 1061d8c96083SRussell King { 1062d8c96083SRussell King struct armada_crtc *dcrtc = dev_get_drvdata(dev); 1063d8c96083SRussell King 1064d8c96083SRussell King armada_drm_crtc_destroy(&dcrtc->crtc); 1065d8c96083SRussell King } 1066d8c96083SRussell King 1067d8c96083SRussell King static const struct component_ops armada_lcd_ops = { 1068d8c96083SRussell King .bind = armada_lcd_bind, 1069d8c96083SRussell King .unbind = armada_lcd_unbind, 1070d8c96083SRussell King }; 1071d8c96083SRussell King 1072d8c96083SRussell King static int armada_lcd_probe(struct platform_device *pdev) 1073d8c96083SRussell King { 1074d8c96083SRussell King return component_add(&pdev->dev, &armada_lcd_ops); 1075d8c96083SRussell King } 1076d8c96083SRussell King 1077d8c96083SRussell King static int armada_lcd_remove(struct platform_device *pdev) 1078d8c96083SRussell King { 1079d8c96083SRussell King component_del(&pdev->dev, &armada_lcd_ops); 1080d8c96083SRussell King return 0; 1081d8c96083SRussell King } 1082d8c96083SRussell King 108385909716SArvind Yadav static const struct of_device_id armada_lcd_of_match[] = { 1084d8c96083SRussell King { 1085d8c96083SRussell King .compatible = "marvell,dove-lcd", 1086d8c96083SRussell King .data = &armada510_ops, 1087d8c96083SRussell King }, 1088d8c96083SRussell King {} 1089d8c96083SRussell King }; 1090d8c96083SRussell King MODULE_DEVICE_TABLE(of, armada_lcd_of_match); 1091d8c96083SRussell King 1092d8c96083SRussell King static const struct platform_device_id armada_lcd_platform_ids[] = { 1093d8c96083SRussell King { 1094d8c96083SRussell King .name = "armada-lcd", 1095d8c96083SRussell King .driver_data = (unsigned long)&armada510_ops, 1096d8c96083SRussell King }, { 1097d8c96083SRussell King .name = "armada-510-lcd", 1098d8c96083SRussell King .driver_data = (unsigned long)&armada510_ops, 1099d8c96083SRussell King }, 1100d8c96083SRussell King { }, 1101d8c96083SRussell King }; 1102d8c96083SRussell King MODULE_DEVICE_TABLE(platform, armada_lcd_platform_ids); 1103d8c96083SRussell King 1104d8c96083SRussell King struct platform_driver armada_lcd_platform_driver = { 1105d8c96083SRussell King .probe = armada_lcd_probe, 1106d8c96083SRussell King .remove = armada_lcd_remove, 1107d8c96083SRussell King .driver = { 1108d8c96083SRussell King .name = "armada-lcd", 1109d8c96083SRussell King .owner = THIS_MODULE, 1110d8c96083SRussell King .of_match_table = armada_lcd_of_match, 1111d8c96083SRussell King }, 1112d8c96083SRussell King .id_table = armada_lcd_platform_ids, 1113d8c96083SRussell King }; 1114