2 * Copyright (C) 2016 Marek Vasut <marex@denx.de>
4 * This code is based on drivers/video/fbdev/mxsfb.c :
5 * Copyright (C) 2010 Juergen Beisert, Pengutronix
6 * Copyright (C) 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
7 * Copyright (C) 2008 Embedded Alley Solutions, Inc All Rights Reserved.
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <drm/drm_atomic_helper.h>
21 #include <drm/drm_crtc.h>
22 #include <drm/drm_crtc_helper.h>
23 #include <drm/drm_fb_helper.h>
24 #include <drm/drm_fb_cma_helper.h>
25 #include <drm/drm_gem_cma_helper.h>
26 #include <drm/drm_of.h>
27 #include <drm/drm_plane_helper.h>
28 #include <drm/drm_simple_kms_helper.h>
29 #include <linux/clk.h>
30 #include <linux/iopoll.h>
31 #include <linux/of_graph.h>
32 #include <linux/platform_data/simplefb.h>
33 #include <video/videomode.h>
35 #include "mxsfb_drv.h"
36 #include "mxsfb_regs.h"
38 #define MXS_SET_ADDR 0x4
39 #define MXS_CLR_ADDR 0x8
40 #define MODULE_CLKGATE BIT(30)
41 #define MODULE_SFTRST BIT(31)
42 /* 1 second delay should be plenty of time for block reset */
43 #define RESET_TIMEOUT 1000000
45 static u32 set_hsync_pulse_width(struct mxsfb_drm_private *mxsfb, u32 val)
47 return (val & mxsfb->devdata->hs_wdth_mask) <<
48 mxsfb->devdata->hs_wdth_shift;
51 /* Setup the MXSFB registers for decoding the pixels out of the framebuffer */
52 static int mxsfb_set_pixel_fmt(struct mxsfb_drm_private *mxsfb)
54 struct drm_crtc *crtc = &mxsfb->pipe.crtc;
55 struct drm_device *drm = crtc->dev;
56 const u32 format = crtc->primary->state->fb->format->format;
59 ctrl = CTRL_BYPASS_COUNT | CTRL_MASTER;
62 * WARNING: The bus width, CTRL_SET_BUS_WIDTH(), is configured to
63 * match the selected mode here. This differs from the original
64 * MXSFB driver, which had the option to configure the bus width
65 * to arbitrary value. This limitation should not pose an issue.
68 /* CTRL1 contains IRQ config and status bits, preserve those. */
69 ctrl1 = readl(mxsfb->base + LCDC_CTRL1);
70 ctrl1 &= CTRL1_CUR_FRAME_DONE_IRQ_EN | CTRL1_CUR_FRAME_DONE_IRQ;
73 case DRM_FORMAT_RGB565:
74 dev_dbg(drm->dev, "Setting up RGB565 mode\n");
75 ctrl |= CTRL_SET_WORD_LENGTH(0);
76 ctrl1 |= CTRL1_SET_BYTE_PACKAGING(0xf);
78 case DRM_FORMAT_XRGB8888:
79 dev_dbg(drm->dev, "Setting up XRGB8888 mode\n");
80 ctrl |= CTRL_SET_WORD_LENGTH(3);
81 /* Do not use packed pixels = one pixel per word instead. */
82 ctrl1 |= CTRL1_SET_BYTE_PACKAGING(0x7);
85 dev_err(drm->dev, "Unhandled pixel format %08x\n", format);
89 writel(ctrl1, mxsfb->base + LCDC_CTRL1);
90 writel(ctrl, mxsfb->base + LCDC_CTRL);
95 static void mxsfb_set_bus_fmt(struct mxsfb_drm_private *mxsfb)
97 struct drm_crtc *crtc = &mxsfb->pipe.crtc;
98 struct drm_device *drm = crtc->dev;
99 u32 bus_format = MEDIA_BUS_FMT_RGB888_1X24;
102 reg = readl(mxsfb->base + LCDC_CTRL);
104 if (mxsfb->connector.display_info.num_bus_formats)
105 bus_format = mxsfb->connector.display_info.bus_formats[0];
107 reg &= ~CTRL_BUS_WIDTH_MASK;
108 switch (bus_format) {
109 case MEDIA_BUS_FMT_RGB565_1X16:
110 reg |= CTRL_SET_BUS_WIDTH(STMLCDIF_16BIT);
112 case MEDIA_BUS_FMT_RGB666_1X18:
113 reg |= CTRL_SET_BUS_WIDTH(STMLCDIF_18BIT);
115 case MEDIA_BUS_FMT_RGB888_1X24:
116 reg |= CTRL_SET_BUS_WIDTH(STMLCDIF_24BIT);
119 dev_err(drm->dev, "Unknown media bus format %d\n", bus_format);
122 writel(reg, mxsfb->base + LCDC_CTRL);
125 static void mxsfb_enable_controller(struct mxsfb_drm_private *mxsfb)
129 if (mxsfb->clk_disp_axi)
130 clk_prepare_enable(mxsfb->clk_disp_axi);
131 clk_prepare_enable(mxsfb->clk);
132 mxsfb_enable_axi_clk(mxsfb);
134 /* If it was disabled, re-enable the mode again */
135 writel(CTRL_DOTCLK_MODE, mxsfb->base + LCDC_CTRL + REG_SET);
137 /* Enable the SYNC signals first, then the DMA engine */
138 reg = readl(mxsfb->base + LCDC_VDCTRL4);
139 reg |= VDCTRL4_SYNC_SIGNALS_ON;
140 writel(reg, mxsfb->base + LCDC_VDCTRL4);
142 writel(CTRL_RUN, mxsfb->base + LCDC_CTRL + REG_SET);
145 static void mxsfb_disable_controller(struct mxsfb_drm_private *mxsfb)
150 * Even if we disable the controller here, it will still continue
151 * until its FIFOs are running out of data
153 writel(CTRL_DOTCLK_MODE, mxsfb->base + LCDC_CTRL + REG_CLR);
155 readl_poll_timeout(mxsfb->base + LCDC_CTRL, reg, !(reg & CTRL_RUN),
158 reg = readl(mxsfb->base + LCDC_VDCTRL4);
159 reg &= ~VDCTRL4_SYNC_SIGNALS_ON;
160 writel(reg, mxsfb->base + LCDC_VDCTRL4);
162 mxsfb_disable_axi_clk(mxsfb);
164 clk_disable_unprepare(mxsfb->clk);
165 if (mxsfb->clk_disp_axi)
166 clk_disable_unprepare(mxsfb->clk_disp_axi);
170 * Clear the bit and poll it cleared. This is usually called with
171 * a reset address and mask being either SFTRST(bit 31) or CLKGATE
174 static int clear_poll_bit(void __iomem *addr, u32 mask)
178 writel(mask, addr + MXS_CLR_ADDR);
179 return readl_poll_timeout(addr, reg, !(reg & mask), 0, RESET_TIMEOUT);
182 static int mxsfb_reset_block(void __iomem *reset_addr)
186 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
190 writel(MODULE_CLKGATE, reset_addr + MXS_CLR_ADDR);
192 ret = clear_poll_bit(reset_addr, MODULE_SFTRST);
196 return clear_poll_bit(reset_addr, MODULE_CLKGATE);
199 static void mxsfb_crtc_mode_set_nofb(struct mxsfb_drm_private *mxsfb)
201 struct drm_display_mode *m = &mxsfb->pipe.crtc.state->adjusted_mode;
202 const u32 bus_flags = mxsfb->connector.display_info.bus_flags;
203 u32 vdctrl0, vsync_pulse_len, hsync_pulse_len;
207 * It seems, you can't re-program the controller if it is still
208 * running. This may lead to shifted pictures (FIFO issue?), so
209 * first stop the controller and drain its FIFOs.
211 mxsfb_enable_axi_clk(mxsfb);
213 /* Mandatory eLCDIF reset as per the Reference Manual */
214 err = mxsfb_reset_block(mxsfb->base);
218 /* Clear the FIFOs */
219 writel(CTRL1_FIFO_CLEAR, mxsfb->base + LCDC_CTRL1 + REG_SET);
221 err = mxsfb_set_pixel_fmt(mxsfb);
225 clk_set_rate(mxsfb->clk, m->crtc_clock * 1000);
227 writel(TRANSFER_COUNT_SET_VCOUNT(m->crtc_vdisplay) |
228 TRANSFER_COUNT_SET_HCOUNT(m->crtc_hdisplay),
229 mxsfb->base + mxsfb->devdata->transfer_count);
231 vsync_pulse_len = m->crtc_vsync_end - m->crtc_vsync_start;
233 vdctrl0 = VDCTRL0_ENABLE_PRESENT | /* Always in DOTCLOCK mode */
234 VDCTRL0_VSYNC_PERIOD_UNIT |
235 VDCTRL0_VSYNC_PULSE_WIDTH_UNIT |
236 VDCTRL0_SET_VSYNC_PULSE_WIDTH(vsync_pulse_len);
237 if (m->flags & DRM_MODE_FLAG_PHSYNC)
238 vdctrl0 |= VDCTRL0_HSYNC_ACT_HIGH;
239 if (m->flags & DRM_MODE_FLAG_PVSYNC)
240 vdctrl0 |= VDCTRL0_VSYNC_ACT_HIGH;
241 /* Make sure Data Enable is high active by default */
242 if (!(bus_flags & DRM_BUS_FLAG_DE_LOW))
243 vdctrl0 |= VDCTRL0_ENABLE_ACT_HIGH;
245 * DRM_BUS_FLAG_PIXDATA_ defines are controller centric,
246 * controllers VDCTRL0_DOTCLK is display centric.
247 * Drive on positive edge -> display samples on falling edge
248 * DRM_BUS_FLAG_PIXDATA_POSEDGE -> VDCTRL0_DOTCLK_ACT_FALLING
250 if (bus_flags & DRM_BUS_FLAG_PIXDATA_POSEDGE)
251 vdctrl0 |= VDCTRL0_DOTCLK_ACT_FALLING;
253 writel(vdctrl0, mxsfb->base + LCDC_VDCTRL0);
255 mxsfb_set_bus_fmt(mxsfb);
257 /* Frame length in lines. */
258 writel(m->crtc_vtotal, mxsfb->base + LCDC_VDCTRL1);
260 /* Line length in units of clocks or pixels. */
261 hsync_pulse_len = m->crtc_hsync_end - m->crtc_hsync_start;
262 writel(set_hsync_pulse_width(mxsfb, hsync_pulse_len) |
263 VDCTRL2_SET_HSYNC_PERIOD(m->crtc_htotal),
264 mxsfb->base + LCDC_VDCTRL2);
266 writel(SET_HOR_WAIT_CNT(m->crtc_htotal - m->crtc_hsync_start) |
267 SET_VERT_WAIT_CNT(m->crtc_vtotal - m->crtc_vsync_start),
268 mxsfb->base + LCDC_VDCTRL3);
270 writel(SET_DOTCLK_H_VALID_DATA_CNT(m->hdisplay),
271 mxsfb->base + LCDC_VDCTRL4);
273 mxsfb_disable_axi_clk(mxsfb);
276 void mxsfb_crtc_enable(struct mxsfb_drm_private *mxsfb)
278 mxsfb_crtc_mode_set_nofb(mxsfb);
279 mxsfb_enable_controller(mxsfb);
282 void mxsfb_crtc_disable(struct mxsfb_drm_private *mxsfb)
284 mxsfb_disable_controller(mxsfb);
287 void mxsfb_plane_atomic_update(struct mxsfb_drm_private *mxsfb,
288 struct drm_plane_state *state)
290 struct drm_simple_display_pipe *pipe = &mxsfb->pipe;
291 struct drm_crtc *crtc = &pipe->crtc;
292 struct drm_framebuffer *fb = pipe->plane.state->fb;
293 struct drm_pending_vblank_event *event;
294 struct drm_gem_cma_object *gem;
299 spin_lock_irq(&crtc->dev->event_lock);
300 event = crtc->state->event;
302 crtc->state->event = NULL;
304 if (drm_crtc_vblank_get(crtc) == 0) {
305 drm_crtc_arm_vblank_event(crtc, event);
307 drm_crtc_send_vblank_event(crtc, event);
310 spin_unlock_irq(&crtc->dev->event_lock);
315 gem = drm_fb_cma_get_gem_obj(fb, 0);
317 mxsfb_enable_axi_clk(mxsfb);
318 writel(gem->paddr, mxsfb->base + mxsfb->devdata->next_buf);
319 mxsfb_disable_axi_clk(mxsfb);