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/amba/clcd-regs.h>
13 #include <linux/clk.h>
14 #include <linux/version.h>
15 #include <linux/dma-buf.h>
16 #include <linux/of_graph.h>
19 #include <drm/drm_gem_cma_helper.h>
20 #include <drm/drm_gem_framebuffer_helper.h>
21 #include <drm/drm_fb_cma_helper.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_crtc *crtc,
50 const struct drm_display_mode *mode)
52 struct drm_device *drm = 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;
130 u32 ppl, hsw, hfp, hbp;
131 u32 lpp, vsw, vfp, vbp;
135 ret = clk_set_rate(priv->clk, mode->clock * 1000);
138 "Failed to set pixel clock rate to %d: %d\n",
139 mode->clock * 1000, ret);
142 clk_prepare_enable(priv->clk);
144 ppl = (mode->hdisplay / 16) - 1;
145 hsw = mode->hsync_end - mode->hsync_start - 1;
146 hfp = mode->hsync_start - mode->hdisplay - 1;
147 hbp = mode->htotal - mode->hsync_end - 1;
149 lpp = mode->vdisplay - 1;
150 vsw = mode->vsync_end - mode->vsync_start - 1;
151 vfp = mode->vsync_start - mode->vdisplay;
152 vbp = mode->vtotal - mode->vsync_end;
154 cpl = mode->hdisplay - 1;
160 priv->regs + CLCD_TIM0);
165 priv->regs + CLCD_TIM1);
167 spin_lock(&priv->tim2_lock);
169 tim2 = readl(priv->regs + CLCD_TIM2);
170 tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
172 if (priv->variant->broken_clockdivider)
175 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
178 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
182 if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
185 if (connector->display_info.bus_flags &
186 DRM_BUS_FLAG_PIXDATA_DRIVE_NEGEDGE)
191 const struct drm_bridge_timings *btimings = bridge->timings;
194 * Here is when things get really fun. Sometimes the bridge
195 * timings are such that the signal out from PL11x is not
196 * stable before the receiving bridge (such as a dumb VGA DAC
197 * or similar) samples it. If that happens, we compensate by
198 * the only method we have: output the data on the opposite
199 * edge of the clock so it is for sure stable when it gets
202 * The PL111 manual does not contain proper timining diagrams
203 * or data for these details, but we know from experiments
204 * that the setup time is more than 3000 picoseconds (3 ns).
205 * If we have a bridge that requires the signal to be stable
206 * earlier than 3000 ps before the clock pulse, we have to
207 * output the data on the opposite edge to avoid flicker.
209 if (btimings && btimings->setup_time_ps >= 3000)
214 writel(tim2, priv->regs + CLCD_TIM2);
215 spin_unlock(&priv->tim2_lock);
217 writel(0, priv->regs + CLCD_TIM3);
219 /* Hard-code TFT panel */
220 cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDVCOMP(1);
221 /* On the ST Micro variant, assume all 24 bits are connected */
222 if (priv->variant->st_bitmux_control)
223 cntl |= CNTL_ST_CDWID_24;
226 * Note that the the ARM hardware's format reader takes 'r' from
227 * the low bit, while DRM formats list channels from high bit
228 * to low bit as you read left to right. The ST Micro version of
229 * the PL110 (LCDC) however uses the standard DRM format.
231 switch (fb->format->format) {
232 case DRM_FORMAT_BGR888:
233 /* Only supported on the ST Micro variant */
234 if (priv->variant->st_bitmux_control)
235 cntl |= CNTL_ST_LCDBPP24_PACKED | CNTL_BGR;
237 case DRM_FORMAT_RGB888:
238 /* Only supported on the ST Micro variant */
239 if (priv->variant->st_bitmux_control)
240 cntl |= CNTL_ST_LCDBPP24_PACKED;
242 case DRM_FORMAT_ABGR8888:
243 case DRM_FORMAT_XBGR8888:
244 if (priv->variant->st_bitmux_control)
245 cntl |= CNTL_LCDBPP24 | CNTL_BGR;
247 cntl |= CNTL_LCDBPP24;
249 case DRM_FORMAT_ARGB8888:
250 case DRM_FORMAT_XRGB8888:
251 if (priv->variant->st_bitmux_control)
252 cntl |= CNTL_LCDBPP24;
254 cntl |= CNTL_LCDBPP24 | CNTL_BGR;
256 case DRM_FORMAT_BGR565:
257 if (priv->variant->is_pl110)
258 cntl |= CNTL_LCDBPP16;
259 else if (priv->variant->st_bitmux_control)
260 cntl |= CNTL_LCDBPP16 | CNTL_ST_1XBPP_565 | CNTL_BGR;
262 cntl |= CNTL_LCDBPP16_565;
264 case DRM_FORMAT_RGB565:
265 if (priv->variant->is_pl110)
266 cntl |= CNTL_LCDBPP16 | CNTL_BGR;
267 else if (priv->variant->st_bitmux_control)
268 cntl |= CNTL_LCDBPP16 | CNTL_ST_1XBPP_565;
270 cntl |= CNTL_LCDBPP16_565 | CNTL_BGR;
272 case DRM_FORMAT_ABGR1555:
273 case DRM_FORMAT_XBGR1555:
274 cntl |= CNTL_LCDBPP16;
275 if (priv->variant->st_bitmux_control)
276 cntl |= CNTL_ST_1XBPP_5551 | CNTL_BGR;
278 case DRM_FORMAT_ARGB1555:
279 case DRM_FORMAT_XRGB1555:
280 cntl |= CNTL_LCDBPP16;
281 if (priv->variant->st_bitmux_control)
282 cntl |= CNTL_ST_1XBPP_5551;
286 case DRM_FORMAT_ABGR4444:
287 case DRM_FORMAT_XBGR4444:
288 cntl |= CNTL_LCDBPP16_444;
289 if (priv->variant->st_bitmux_control)
290 cntl |= CNTL_ST_1XBPP_444 | CNTL_BGR;
292 case DRM_FORMAT_ARGB4444:
293 case DRM_FORMAT_XRGB4444:
294 cntl |= CNTL_LCDBPP16_444;
295 if (priv->variant->st_bitmux_control)
296 cntl |= CNTL_ST_1XBPP_444;
301 WARN_ONCE(true, "Unknown FB format 0x%08x\n",
306 /* The PL110 in Integrator/Versatile does the BGR routing externally */
307 if (priv->variant->external_bgr)
310 /* Power sequence: first enable and chill */
311 writel(cntl, priv->regs + priv->ctrl);
314 * We expect this delay to stabilize the contrast
315 * voltage Vee as stipulated by the manual
319 if (priv->variant_display_enable)
320 priv->variant_display_enable(drm, fb->format->format);
324 writel(cntl, priv->regs + priv->ctrl);
326 if (!priv->variant->broken_vblank)
327 drm_crtc_vblank_on(crtc);
330 void pl111_display_disable(struct drm_simple_display_pipe *pipe)
332 struct drm_crtc *crtc = &pipe->crtc;
333 struct drm_device *drm = crtc->dev;
334 struct pl111_drm_dev_private *priv = drm->dev_private;
337 if (!priv->variant->broken_vblank)
338 drm_crtc_vblank_off(crtc);
341 cntl = readl(priv->regs + priv->ctrl);
342 if (cntl & CNTL_LCDPWR) {
343 cntl &= ~CNTL_LCDPWR;
344 writel(cntl, priv->regs + priv->ctrl);
348 * We expect this delay to stabilize the contrast voltage Vee as
349 * stipulated by the manual
353 if (priv->variant_display_disable)
354 priv->variant_display_disable(drm);
357 writel(0, priv->regs + priv->ctrl);
359 clk_disable_unprepare(priv->clk);
362 static void pl111_display_update(struct drm_simple_display_pipe *pipe,
363 struct drm_plane_state *old_pstate)
365 struct drm_crtc *crtc = &pipe->crtc;
366 struct drm_device *drm = crtc->dev;
367 struct pl111_drm_dev_private *priv = drm->dev_private;
368 struct drm_pending_vblank_event *event = crtc->state->event;
369 struct drm_plane *plane = &pipe->plane;
370 struct drm_plane_state *pstate = plane->state;
371 struct drm_framebuffer *fb = pstate->fb;
374 u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
376 writel(addr, priv->regs + CLCD_UBAS);
380 crtc->state->event = NULL;
382 spin_lock_irq(&crtc->dev->event_lock);
383 if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
384 drm_crtc_arm_vblank_event(crtc, event);
386 drm_crtc_send_vblank_event(crtc, event);
387 spin_unlock_irq(&crtc->dev->event_lock);
391 static int pl111_display_enable_vblank(struct drm_simple_display_pipe *pipe)
393 struct drm_crtc *crtc = &pipe->crtc;
394 struct drm_device *drm = crtc->dev;
395 struct pl111_drm_dev_private *priv = drm->dev_private;
397 writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + priv->ienb);
402 static void pl111_display_disable_vblank(struct drm_simple_display_pipe *pipe)
404 struct drm_crtc *crtc = &pipe->crtc;
405 struct drm_device *drm = crtc->dev;
406 struct pl111_drm_dev_private *priv = drm->dev_private;
408 writel(0, priv->regs + priv->ienb);
411 static struct drm_simple_display_pipe_funcs pl111_display_funcs = {
412 .mode_valid = pl111_mode_valid,
413 .check = pl111_display_check,
414 .enable = pl111_display_enable,
415 .disable = pl111_display_disable,
416 .update = pl111_display_update,
417 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
420 static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
421 unsigned long *prate, bool set_parent)
423 int best_div = 1, div;
424 struct clk_hw *parent = clk_hw_get_parent(hw);
425 unsigned long best_prate = 0;
426 unsigned long best_diff = ~0ul;
427 int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
429 for (div = 1; div < max_div; div++) {
430 unsigned long this_prate, div_rate, diff;
433 this_prate = clk_hw_round_rate(parent, rate * div);
436 div_rate = DIV_ROUND_UP_ULL(this_prate, div);
437 diff = abs(rate - div_rate);
439 if (diff < best_diff) {
442 best_prate = this_prate;
450 static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
451 unsigned long *prate)
453 int div = pl111_clk_div_choose_div(hw, rate, prate, true);
455 return DIV_ROUND_UP_ULL(*prate, div);
458 static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
461 struct pl111_drm_dev_private *priv =
462 container_of(hw, struct pl111_drm_dev_private, clk_div);
463 u32 tim2 = readl(priv->regs + CLCD_TIM2);
469 div = tim2 & TIM2_PCD_LO_MASK;
470 div |= (tim2 & TIM2_PCD_HI_MASK) >>
471 (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
474 return DIV_ROUND_UP_ULL(prate, div);
477 static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
480 struct pl111_drm_dev_private *priv =
481 container_of(hw, struct pl111_drm_dev_private, clk_div);
482 int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
485 spin_lock(&priv->tim2_lock);
486 tim2 = readl(priv->regs + CLCD_TIM2);
487 tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
493 tim2 |= div & TIM2_PCD_LO_MASK;
494 tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
497 writel(tim2, priv->regs + CLCD_TIM2);
498 spin_unlock(&priv->tim2_lock);
503 static const struct clk_ops pl111_clk_div_ops = {
504 .recalc_rate = pl111_clk_div_recalc_rate,
505 .round_rate = pl111_clk_div_round_rate,
506 .set_rate = pl111_clk_div_set_rate,
510 pl111_init_clock_divider(struct drm_device *drm)
512 struct pl111_drm_dev_private *priv = drm->dev_private;
513 struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
514 struct clk_hw *div = &priv->clk_div;
515 const char *parent_name;
516 struct clk_init_data init = {
518 .ops = &pl111_clk_div_ops,
519 .parent_names = &parent_name,
521 .flags = CLK_SET_RATE_PARENT,
525 if (IS_ERR(parent)) {
526 dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
527 return PTR_ERR(parent);
530 spin_lock_init(&priv->tim2_lock);
532 /* If the clock divider is broken, use the parent directly */
533 if (priv->variant->broken_clockdivider) {
537 parent_name = __clk_get_name(parent);
540 ret = devm_clk_hw_register(drm->dev, div);
542 priv->clk = div->clk;
546 int pl111_display_init(struct drm_device *drm)
548 struct pl111_drm_dev_private *priv = drm->dev_private;
549 struct device *dev = drm->dev;
550 struct device_node *endpoint;
554 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
558 if (of_property_read_u32_array(endpoint,
559 "arm,pl11x,tft-r0g0b0-pads",
561 ARRAY_SIZE(tft_r0b0g0)) != 0) {
562 dev_err(dev, "arm,pl11x,tft-r0g0b0-pads should be 3 ints\n");
563 of_node_put(endpoint);
566 of_node_put(endpoint);
568 ret = pl111_init_clock_divider(drm);
572 if (!priv->variant->broken_vblank) {
573 pl111_display_funcs.enable_vblank = pl111_display_enable_vblank;
574 pl111_display_funcs.disable_vblank = pl111_display_disable_vblank;
577 ret = drm_simple_display_pipe_init(drm, &priv->pipe,
578 &pl111_display_funcs,
579 priv->variant->formats,
580 priv->variant->nformats,