1 // SPDX-License-Identifier: GPL-2.0-only
3 * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved.
5 * Parts of this file were based on sources as follows:
7 * Copyright (c) 2006-2008 Intel Corporation
9 * Copyright (C) 2011 Texas Instruments
12 #include <linux/clk.h>
13 #include <linux/delay.h>
14 #include <linux/dma-buf.h>
15 #include <linux/of_graph.h>
17 #include <drm/drm_fb_cma_helper.h>
18 #include <drm/drm_fourcc.h>
19 #include <drm/drm_gem_atomic_helper.h>
20 #include <drm/drm_gem_cma_helper.h>
21 #include <drm/drm_vblank.h>
23 #include "pl111_drm.h"
25 irqreturn_t pl111_irq(int irq, void *data)
27 struct pl111_drm_dev_private *priv = data;
29 irqreturn_t status = IRQ_NONE;
31 irq_stat = readl(priv->regs + CLCD_PL111_MIS);
36 if (irq_stat & CLCD_IRQ_NEXTBASE_UPDATE) {
37 drm_crtc_handle_vblank(&priv->pipe.crtc);
42 /* Clear the interrupt once done */
43 writel(irq_stat, priv->regs + CLCD_PL111_ICR);
48 static enum drm_mode_status
49 pl111_mode_valid(struct drm_simple_display_pipe *pipe,
50 const struct drm_display_mode *mode)
52 struct drm_device *drm = pipe->crtc.dev;
53 struct pl111_drm_dev_private *priv = drm->dev_private;
54 u32 cpp = priv->variant->fb_bpp / 8;
58 * We use the pixelclock to also account for interlaced modes, the
59 * resulting bandwidth is in bytes per second.
61 bw = mode->clock * 1000ULL; /* In Hz */
62 bw = bw * mode->hdisplay * mode->vdisplay * cpp;
63 bw = div_u64(bw, mode->htotal * mode->vtotal);
66 * If no bandwidth constraints, anything goes, else
67 * check if we are too fast.
69 if (priv->memory_bw && (bw > priv->memory_bw)) {
70 DRM_DEBUG_KMS("%d x %d @ %d Hz, %d cpp, bw %llu too fast\n",
71 mode->hdisplay, mode->vdisplay,
72 mode->clock * 1000, cpp, bw);
76 DRM_DEBUG_KMS("%d x %d @ %d Hz, %d cpp, bw %llu bytes/s OK\n",
77 mode->hdisplay, mode->vdisplay,
78 mode->clock * 1000, cpp, bw);
83 static int pl111_display_check(struct drm_simple_display_pipe *pipe,
84 struct drm_plane_state *pstate,
85 struct drm_crtc_state *cstate)
87 const struct drm_display_mode *mode = &cstate->mode;
88 struct drm_framebuffer *old_fb = pipe->plane.state->fb;
89 struct drm_framebuffer *fb = pstate->fb;
91 if (mode->hdisplay % 16)
95 u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
97 /* FB base address must be dword aligned. */
101 /* There's no pitch register -- the mode's hdisplay
104 if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0])
107 /* We can't change the FB format in a flicker-free
108 * manner (and only update it during CRTC enable).
110 if (old_fb && old_fb->format != fb->format)
111 cstate->mode_changed = true;
117 static void pl111_display_enable(struct drm_simple_display_pipe *pipe,
118 struct drm_crtc_state *cstate,
119 struct drm_plane_state *plane_state)
121 struct drm_crtc *crtc = &pipe->crtc;
122 struct drm_plane *plane = &pipe->plane;
123 struct drm_device *drm = crtc->dev;
124 struct pl111_drm_dev_private *priv = drm->dev_private;
125 const struct drm_display_mode *mode = &cstate->mode;
126 struct drm_framebuffer *fb = plane->state->fb;
127 struct drm_connector *connector = priv->connector;
128 struct drm_bridge *bridge = priv->bridge;
129 bool grayscale = false;
131 u32 ppl, hsw, hfp, hbp;
132 u32 lpp, vsw, vfp, vbp;
136 ret = clk_set_rate(priv->clk, mode->clock * 1000);
139 "Failed to set pixel clock rate to %d: %d\n",
140 mode->clock * 1000, ret);
143 clk_prepare_enable(priv->clk);
145 ppl = (mode->hdisplay / 16) - 1;
146 hsw = mode->hsync_end - mode->hsync_start - 1;
147 hfp = mode->hsync_start - mode->hdisplay - 1;
148 hbp = mode->htotal - mode->hsync_end - 1;
150 lpp = mode->vdisplay - 1;
151 vsw = mode->vsync_end - mode->vsync_start - 1;
152 vfp = mode->vsync_start - mode->vdisplay;
153 vbp = mode->vtotal - mode->vsync_end;
155 cpl = mode->hdisplay - 1;
161 priv->regs + CLCD_TIM0);
166 priv->regs + CLCD_TIM1);
168 spin_lock(&priv->tim2_lock);
170 tim2 = readl(priv->regs + CLCD_TIM2);
171 tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
173 if (priv->variant->broken_clockdivider)
176 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
179 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
183 if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
186 if (connector->display_info.bus_flags &
187 DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
190 if (connector->display_info.num_bus_formats == 1 &&
191 connector->display_info.bus_formats[0] ==
192 MEDIA_BUS_FMT_Y8_1X8)
196 * The AC pin bias frequency is set to max count when using
197 * grayscale so at least once in a while we will reverse
198 * polarity and get rid of any DC built up that could
199 * damage the display.
202 tim2 |= TIM2_ACB_MASK;
206 const struct drm_bridge_timings *btimings = bridge->timings;
209 * Here is when things get really fun. Sometimes the bridge
210 * timings are such that the signal out from PL11x is not
211 * stable before the receiving bridge (such as a dumb VGA DAC
212 * or similar) samples it. If that happens, we compensate by
213 * the only method we have: output the data on the opposite
214 * edge of the clock so it is for sure stable when it gets
217 * The PL111 manual does not contain proper timining diagrams
218 * or data for these details, but we know from experiments
219 * that the setup time is more than 3000 picoseconds (3 ns).
220 * If we have a bridge that requires the signal to be stable
221 * earlier than 3000 ps before the clock pulse, we have to
222 * output the data on the opposite edge to avoid flicker.
224 if (btimings && btimings->setup_time_ps >= 3000)
229 writel(tim2, priv->regs + CLCD_TIM2);
230 spin_unlock(&priv->tim2_lock);
232 writel(0, priv->regs + CLCD_TIM3);
235 * Detect grayscale bus format. We do not support a grayscale mode
236 * toward userspace, instead we expose an RGB24 buffer and then the
237 * hardware will activate its grayscaler to convert to the grayscale
241 cntl = CNTL_LCDEN | CNTL_LCDMONO8;
243 /* Else we assume TFT display */
244 cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDVCOMP(1);
246 /* On the ST Micro variant, assume all 24 bits are connected */
247 if (priv->variant->st_bitmux_control)
248 cntl |= CNTL_ST_CDWID_24;
251 * Note that the the ARM hardware's format reader takes 'r' from
252 * the low bit, while DRM formats list channels from high bit
253 * to low bit as you read left to right. The ST Micro version of
254 * the PL110 (LCDC) however uses the standard DRM format.
256 switch (fb->format->format) {
257 case DRM_FORMAT_BGR888:
258 /* Only supported on the ST Micro variant */
259 if (priv->variant->st_bitmux_control)
260 cntl |= CNTL_ST_LCDBPP24_PACKED | CNTL_BGR;
262 case DRM_FORMAT_RGB888:
263 /* Only supported on the ST Micro variant */
264 if (priv->variant->st_bitmux_control)
265 cntl |= CNTL_ST_LCDBPP24_PACKED;
267 case DRM_FORMAT_ABGR8888:
268 case DRM_FORMAT_XBGR8888:
269 if (priv->variant->st_bitmux_control)
270 cntl |= CNTL_LCDBPP24 | CNTL_BGR;
272 cntl |= CNTL_LCDBPP24;
274 case DRM_FORMAT_ARGB8888:
275 case DRM_FORMAT_XRGB8888:
276 if (priv->variant->st_bitmux_control)
277 cntl |= CNTL_LCDBPP24;
279 cntl |= CNTL_LCDBPP24 | CNTL_BGR;
281 case DRM_FORMAT_BGR565:
282 if (priv->variant->is_pl110)
283 cntl |= CNTL_LCDBPP16;
284 else if (priv->variant->st_bitmux_control)
285 cntl |= CNTL_LCDBPP16 | CNTL_ST_1XBPP_565 | CNTL_BGR;
287 cntl |= CNTL_LCDBPP16_565;
289 case DRM_FORMAT_RGB565:
290 if (priv->variant->is_pl110)
291 cntl |= CNTL_LCDBPP16 | CNTL_BGR;
292 else if (priv->variant->st_bitmux_control)
293 cntl |= CNTL_LCDBPP16 | CNTL_ST_1XBPP_565;
295 cntl |= CNTL_LCDBPP16_565 | CNTL_BGR;
297 case DRM_FORMAT_ABGR1555:
298 case DRM_FORMAT_XBGR1555:
299 cntl |= CNTL_LCDBPP16;
300 if (priv->variant->st_bitmux_control)
301 cntl |= CNTL_ST_1XBPP_5551 | CNTL_BGR;
303 case DRM_FORMAT_ARGB1555:
304 case DRM_FORMAT_XRGB1555:
305 cntl |= CNTL_LCDBPP16;
306 if (priv->variant->st_bitmux_control)
307 cntl |= CNTL_ST_1XBPP_5551;
311 case DRM_FORMAT_ABGR4444:
312 case DRM_FORMAT_XBGR4444:
313 cntl |= CNTL_LCDBPP16_444;
314 if (priv->variant->st_bitmux_control)
315 cntl |= CNTL_ST_1XBPP_444 | CNTL_BGR;
317 case DRM_FORMAT_ARGB4444:
318 case DRM_FORMAT_XRGB4444:
319 cntl |= CNTL_LCDBPP16_444;
320 if (priv->variant->st_bitmux_control)
321 cntl |= CNTL_ST_1XBPP_444;
326 WARN_ONCE(true, "Unknown FB format 0x%08x\n",
331 /* The PL110 in Integrator/Versatile does the BGR routing externally */
332 if (priv->variant->external_bgr)
335 /* Power sequence: first enable and chill */
336 writel(cntl, priv->regs + priv->ctrl);
339 * We expect this delay to stabilize the contrast
340 * voltage Vee as stipulated by the manual
344 if (priv->variant_display_enable)
345 priv->variant_display_enable(drm, fb->format->format);
349 writel(cntl, priv->regs + priv->ctrl);
351 if (!priv->variant->broken_vblank)
352 drm_crtc_vblank_on(crtc);
355 static void pl111_display_disable(struct drm_simple_display_pipe *pipe)
357 struct drm_crtc *crtc = &pipe->crtc;
358 struct drm_device *drm = crtc->dev;
359 struct pl111_drm_dev_private *priv = drm->dev_private;
362 if (!priv->variant->broken_vblank)
363 drm_crtc_vblank_off(crtc);
366 cntl = readl(priv->regs + priv->ctrl);
367 if (cntl & CNTL_LCDPWR) {
368 cntl &= ~CNTL_LCDPWR;
369 writel(cntl, priv->regs + priv->ctrl);
373 * We expect this delay to stabilize the contrast voltage Vee as
374 * stipulated by the manual
378 if (priv->variant_display_disable)
379 priv->variant_display_disable(drm);
382 writel(0, priv->regs + priv->ctrl);
384 clk_disable_unprepare(priv->clk);
387 static void pl111_display_update(struct drm_simple_display_pipe *pipe,
388 struct drm_plane_state *old_pstate)
390 struct drm_crtc *crtc = &pipe->crtc;
391 struct drm_device *drm = crtc->dev;
392 struct pl111_drm_dev_private *priv = drm->dev_private;
393 struct drm_pending_vblank_event *event = crtc->state->event;
394 struct drm_plane *plane = &pipe->plane;
395 struct drm_plane_state *pstate = plane->state;
396 struct drm_framebuffer *fb = pstate->fb;
399 u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
401 writel(addr, priv->regs + CLCD_UBAS);
405 crtc->state->event = NULL;
407 spin_lock_irq(&crtc->dev->event_lock);
408 if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
409 drm_crtc_arm_vblank_event(crtc, event);
411 drm_crtc_send_vblank_event(crtc, event);
412 spin_unlock_irq(&crtc->dev->event_lock);
416 static int pl111_display_enable_vblank(struct drm_simple_display_pipe *pipe)
418 struct drm_crtc *crtc = &pipe->crtc;
419 struct drm_device *drm = crtc->dev;
420 struct pl111_drm_dev_private *priv = drm->dev_private;
422 writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + priv->ienb);
427 static void pl111_display_disable_vblank(struct drm_simple_display_pipe *pipe)
429 struct drm_crtc *crtc = &pipe->crtc;
430 struct drm_device *drm = crtc->dev;
431 struct pl111_drm_dev_private *priv = drm->dev_private;
433 writel(0, priv->regs + priv->ienb);
436 static struct drm_simple_display_pipe_funcs pl111_display_funcs = {
437 .mode_valid = pl111_mode_valid,
438 .check = pl111_display_check,
439 .enable = pl111_display_enable,
440 .disable = pl111_display_disable,
441 .update = pl111_display_update,
444 static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
445 unsigned long *prate, bool set_parent)
447 int best_div = 1, div;
448 struct clk_hw *parent = clk_hw_get_parent(hw);
449 unsigned long best_prate = 0;
450 unsigned long best_diff = ~0ul;
451 int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
453 for (div = 1; div < max_div; div++) {
454 unsigned long this_prate, div_rate, diff;
457 this_prate = clk_hw_round_rate(parent, rate * div);
460 div_rate = DIV_ROUND_UP_ULL(this_prate, div);
461 diff = abs(rate - div_rate);
463 if (diff < best_diff) {
466 best_prate = this_prate;
474 static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
475 unsigned long *prate)
477 int div = pl111_clk_div_choose_div(hw, rate, prate, true);
479 return DIV_ROUND_UP_ULL(*prate, div);
482 static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
485 struct pl111_drm_dev_private *priv =
486 container_of(hw, struct pl111_drm_dev_private, clk_div);
487 u32 tim2 = readl(priv->regs + CLCD_TIM2);
493 div = tim2 & TIM2_PCD_LO_MASK;
494 div |= (tim2 & TIM2_PCD_HI_MASK) >>
495 (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
498 return DIV_ROUND_UP_ULL(prate, div);
501 static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
504 struct pl111_drm_dev_private *priv =
505 container_of(hw, struct pl111_drm_dev_private, clk_div);
506 int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
509 spin_lock(&priv->tim2_lock);
510 tim2 = readl(priv->regs + CLCD_TIM2);
511 tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
517 tim2 |= div & TIM2_PCD_LO_MASK;
518 tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
521 writel(tim2, priv->regs + CLCD_TIM2);
522 spin_unlock(&priv->tim2_lock);
527 static const struct clk_ops pl111_clk_div_ops = {
528 .recalc_rate = pl111_clk_div_recalc_rate,
529 .round_rate = pl111_clk_div_round_rate,
530 .set_rate = pl111_clk_div_set_rate,
534 pl111_init_clock_divider(struct drm_device *drm)
536 struct pl111_drm_dev_private *priv = drm->dev_private;
537 struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
538 struct clk_hw *div = &priv->clk_div;
539 const char *parent_name;
540 struct clk_init_data init = {
542 .ops = &pl111_clk_div_ops,
543 .parent_names = &parent_name,
545 .flags = CLK_SET_RATE_PARENT,
549 if (IS_ERR(parent)) {
550 dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
551 return PTR_ERR(parent);
554 spin_lock_init(&priv->tim2_lock);
556 /* If the clock divider is broken, use the parent directly */
557 if (priv->variant->broken_clockdivider) {
561 parent_name = __clk_get_name(parent);
564 ret = devm_clk_hw_register(drm->dev, div);
566 priv->clk = div->clk;
570 int pl111_display_init(struct drm_device *drm)
572 struct pl111_drm_dev_private *priv = drm->dev_private;
575 ret = pl111_init_clock_divider(drm);
579 if (!priv->variant->broken_vblank) {
580 pl111_display_funcs.enable_vblank = pl111_display_enable_vblank;
581 pl111_display_funcs.disable_vblank = pl111_display_disable_vblank;
584 ret = drm_simple_display_pipe_init(drm, &priv->pipe,
585 &pl111_display_funcs,
586 priv->variant->formats,
587 priv->variant->nformats,