2 * (C) COPYRIGHT 2012-2013 ARM Limited. All rights reserved.
4 * Parts of this file were based on sources as follows:
6 * Copyright (c) 2006-2008 Intel Corporation
8 * Copyright (C) 2011 Texas Instruments
10 * This program is free software and is provided to you under the terms of the
11 * GNU General Public License version 2 as published by the Free Software
12 * Foundation, and any use by you of this program is subject to the terms of
17 #include <linux/amba/clcd-regs.h>
18 #include <linux/clk.h>
19 #include <linux/version.h>
20 #include <linux/dma-buf.h>
21 #include <linux/of_graph.h>
24 #include <drm/drm_gem_cma_helper.h>
25 #include <drm/drm_gem_framebuffer_helper.h>
26 #include <drm/drm_fb_cma_helper.h>
28 #include "pl111_drm.h"
30 irqreturn_t pl111_irq(int irq, void *data)
32 struct pl111_drm_dev_private *priv = data;
34 irqreturn_t status = IRQ_NONE;
36 irq_stat = readl(priv->regs + CLCD_PL111_MIS);
41 if (irq_stat & CLCD_IRQ_NEXTBASE_UPDATE) {
42 drm_crtc_handle_vblank(&priv->pipe.crtc);
47 /* Clear the interrupt once done */
48 writel(irq_stat, priv->regs + CLCD_PL111_ICR);
53 static enum drm_mode_status
54 pl111_mode_valid(struct drm_crtc *crtc,
55 const struct drm_display_mode *mode)
57 struct drm_device *drm = crtc->dev;
58 struct pl111_drm_dev_private *priv = drm->dev_private;
59 u32 cpp = priv->variant->fb_bpp / 8;
63 * We use the pixelclock to also account for interlaced modes, the
64 * resulting bandwidth is in bytes per second.
66 bw = mode->clock * 1000; /* In Hz */
67 bw = bw * mode->hdisplay * mode->vdisplay * cpp;
68 bw = div_u64(bw, mode->htotal * mode->vtotal);
71 * If no bandwidth constraints, anything goes, else
72 * check if we are too fast.
74 if (priv->memory_bw && (bw > priv->memory_bw)) {
75 DRM_DEBUG_KMS("%d x %d @ %d Hz, %d cpp, bw %llu too fast\n",
76 mode->hdisplay, mode->vdisplay,
77 mode->clock * 1000, cpp, bw);
81 DRM_DEBUG_KMS("%d x %d @ %d Hz, %d cpp, bw %llu bytes/s OK\n",
82 mode->hdisplay, mode->vdisplay,
83 mode->clock * 1000, cpp, bw);
88 static int pl111_display_check(struct drm_simple_display_pipe *pipe,
89 struct drm_plane_state *pstate,
90 struct drm_crtc_state *cstate)
92 const struct drm_display_mode *mode = &cstate->mode;
93 struct drm_framebuffer *old_fb = pipe->plane.state->fb;
94 struct drm_framebuffer *fb = pstate->fb;
96 if (mode->hdisplay % 16)
100 u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
102 /* FB base address must be dword aligned. */
106 /* There's no pitch register -- the mode's hdisplay
109 if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0])
112 /* We can't change the FB format in a flicker-free
113 * manner (and only update it during CRTC enable).
115 if (old_fb && old_fb->format != fb->format)
116 cstate->mode_changed = true;
122 static void pl111_display_enable(struct drm_simple_display_pipe *pipe,
123 struct drm_crtc_state *cstate,
124 struct drm_plane_state *plane_state)
126 struct drm_crtc *crtc = &pipe->crtc;
127 struct drm_plane *plane = &pipe->plane;
128 struct drm_device *drm = crtc->dev;
129 struct pl111_drm_dev_private *priv = drm->dev_private;
130 const struct drm_display_mode *mode = &cstate->mode;
131 struct drm_framebuffer *fb = plane->state->fb;
132 struct drm_connector *connector = priv->connector;
133 struct drm_bridge *bridge = priv->bridge;
135 u32 ppl, hsw, hfp, hbp;
136 u32 lpp, vsw, vfp, vbp;
140 ret = clk_set_rate(priv->clk, mode->clock * 1000);
143 "Failed to set pixel clock rate to %d: %d\n",
144 mode->clock * 1000, ret);
147 clk_prepare_enable(priv->clk);
149 ppl = (mode->hdisplay / 16) - 1;
150 hsw = mode->hsync_end - mode->hsync_start - 1;
151 hfp = mode->hsync_start - mode->hdisplay - 1;
152 hbp = mode->htotal - mode->hsync_end - 1;
154 lpp = mode->vdisplay - 1;
155 vsw = mode->vsync_end - mode->vsync_start - 1;
156 vfp = mode->vsync_start - mode->vdisplay;
157 vbp = mode->vtotal - mode->vsync_end;
159 cpl = mode->hdisplay - 1;
165 priv->regs + CLCD_TIM0);
170 priv->regs + CLCD_TIM1);
172 spin_lock(&priv->tim2_lock);
174 tim2 = readl(priv->regs + CLCD_TIM2);
175 tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
177 if (priv->variant->broken_clockdivider)
180 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
183 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
187 if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
190 if (connector->display_info.bus_flags &
191 DRM_BUS_FLAG_PIXDATA_NEGEDGE)
196 const struct drm_bridge_timings *btimings = bridge->timings;
199 * Here is when things get really fun. Sometimes the bridge
200 * timings are such that the signal out from PL11x is not
201 * stable before the receiving bridge (such as a dumb VGA DAC
202 * or similar) samples it. If that happens, we compensate by
203 * the only method we have: output the data on the opposite
204 * edge of the clock so it is for sure stable when it gets
207 * The PL111 manual does not contain proper timining diagrams
208 * or data for these details, but we know from experiments
209 * that the setup time is more than 3000 picoseconds (3 ns).
210 * If we have a bridge that requires the signal to be stable
211 * earlier than 3000 ps before the clock pulse, we have to
212 * output the data on the opposite edge to avoid flicker.
214 if (btimings && btimings->setup_time_ps >= 3000)
219 writel(tim2, priv->regs + CLCD_TIM2);
220 spin_unlock(&priv->tim2_lock);
222 writel(0, priv->regs + CLCD_TIM3);
224 /* Hard-code TFT panel */
225 cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDVCOMP(1);
227 /* Note that the the hardware's format reader takes 'r' from
228 * the low bit, while DRM formats list channels from high bit
229 * to low bit as you read left to right.
231 switch (fb->format->format) {
232 case DRM_FORMAT_ABGR8888:
233 case DRM_FORMAT_XBGR8888:
234 cntl |= CNTL_LCDBPP24;
236 case DRM_FORMAT_ARGB8888:
237 case DRM_FORMAT_XRGB8888:
238 cntl |= CNTL_LCDBPP24 | CNTL_BGR;
240 case DRM_FORMAT_BGR565:
241 if (priv->variant->is_pl110)
242 cntl |= CNTL_LCDBPP16;
244 cntl |= CNTL_LCDBPP16_565;
246 case DRM_FORMAT_RGB565:
247 if (priv->variant->is_pl110)
248 cntl |= CNTL_LCDBPP16;
250 cntl |= CNTL_LCDBPP16_565;
253 case DRM_FORMAT_ABGR1555:
254 case DRM_FORMAT_XBGR1555:
255 cntl |= CNTL_LCDBPP16;
257 case DRM_FORMAT_ARGB1555:
258 case DRM_FORMAT_XRGB1555:
259 cntl |= CNTL_LCDBPP16 | CNTL_BGR;
261 case DRM_FORMAT_ABGR4444:
262 case DRM_FORMAT_XBGR4444:
263 cntl |= CNTL_LCDBPP16_444;
265 case DRM_FORMAT_ARGB4444:
266 case DRM_FORMAT_XRGB4444:
267 cntl |= CNTL_LCDBPP16_444 | CNTL_BGR;
270 WARN_ONCE(true, "Unknown FB format 0x%08x\n",
275 /* The PL110 in Integrator/Versatile does the BGR routing externally */
276 if (priv->variant->external_bgr)
279 /* Power sequence: first enable and chill */
280 writel(cntl, priv->regs + priv->ctrl);
283 * We expect this delay to stabilize the contrast
284 * voltage Vee as stipulated by the manual
288 if (priv->variant_display_enable)
289 priv->variant_display_enable(drm, fb->format->format);
293 writel(cntl, priv->regs + priv->ctrl);
295 if (!priv->variant->broken_vblank)
296 drm_crtc_vblank_on(crtc);
299 void pl111_display_disable(struct drm_simple_display_pipe *pipe)
301 struct drm_crtc *crtc = &pipe->crtc;
302 struct drm_device *drm = crtc->dev;
303 struct pl111_drm_dev_private *priv = drm->dev_private;
306 if (!priv->variant->broken_vblank)
307 drm_crtc_vblank_off(crtc);
310 cntl = readl(priv->regs + priv->ctrl);
311 if (cntl & CNTL_LCDPWR) {
312 cntl &= ~CNTL_LCDPWR;
313 writel(cntl, priv->regs + priv->ctrl);
317 * We expect this delay to stabilize the contrast voltage Vee as
318 * stipulated by the manual
322 if (priv->variant_display_disable)
323 priv->variant_display_disable(drm);
326 writel(0, priv->regs + priv->ctrl);
328 clk_disable_unprepare(priv->clk);
331 static void pl111_display_update(struct drm_simple_display_pipe *pipe,
332 struct drm_plane_state *old_pstate)
334 struct drm_crtc *crtc = &pipe->crtc;
335 struct drm_device *drm = crtc->dev;
336 struct pl111_drm_dev_private *priv = drm->dev_private;
337 struct drm_pending_vblank_event *event = crtc->state->event;
338 struct drm_plane *plane = &pipe->plane;
339 struct drm_plane_state *pstate = plane->state;
340 struct drm_framebuffer *fb = pstate->fb;
343 u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
345 writel(addr, priv->regs + CLCD_UBAS);
349 crtc->state->event = NULL;
351 spin_lock_irq(&crtc->dev->event_lock);
352 if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
353 drm_crtc_arm_vblank_event(crtc, event);
355 drm_crtc_send_vblank_event(crtc, event);
356 spin_unlock_irq(&crtc->dev->event_lock);
360 static int pl111_display_enable_vblank(struct drm_simple_display_pipe *pipe)
362 struct drm_crtc *crtc = &pipe->crtc;
363 struct drm_device *drm = crtc->dev;
364 struct pl111_drm_dev_private *priv = drm->dev_private;
366 writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + priv->ienb);
371 static void pl111_display_disable_vblank(struct drm_simple_display_pipe *pipe)
373 struct drm_crtc *crtc = &pipe->crtc;
374 struct drm_device *drm = crtc->dev;
375 struct pl111_drm_dev_private *priv = drm->dev_private;
377 writel(0, priv->regs + priv->ienb);
380 static struct drm_simple_display_pipe_funcs pl111_display_funcs = {
381 .mode_valid = pl111_mode_valid,
382 .check = pl111_display_check,
383 .enable = pl111_display_enable,
384 .disable = pl111_display_disable,
385 .update = pl111_display_update,
386 .prepare_fb = drm_gem_fb_simple_display_pipe_prepare_fb,
389 static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
390 unsigned long *prate, bool set_parent)
392 int best_div = 1, div;
393 struct clk_hw *parent = clk_hw_get_parent(hw);
394 unsigned long best_prate = 0;
395 unsigned long best_diff = ~0ul;
396 int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
398 for (div = 1; div < max_div; div++) {
399 unsigned long this_prate, div_rate, diff;
402 this_prate = clk_hw_round_rate(parent, rate * div);
405 div_rate = DIV_ROUND_UP_ULL(this_prate, div);
406 diff = abs(rate - div_rate);
408 if (diff < best_diff) {
411 best_prate = this_prate;
419 static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
420 unsigned long *prate)
422 int div = pl111_clk_div_choose_div(hw, rate, prate, true);
424 return DIV_ROUND_UP_ULL(*prate, div);
427 static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
430 struct pl111_drm_dev_private *priv =
431 container_of(hw, struct pl111_drm_dev_private, clk_div);
432 u32 tim2 = readl(priv->regs + CLCD_TIM2);
438 div = tim2 & TIM2_PCD_LO_MASK;
439 div |= (tim2 & TIM2_PCD_HI_MASK) >>
440 (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
443 return DIV_ROUND_UP_ULL(prate, div);
446 static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
449 struct pl111_drm_dev_private *priv =
450 container_of(hw, struct pl111_drm_dev_private, clk_div);
451 int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
454 spin_lock(&priv->tim2_lock);
455 tim2 = readl(priv->regs + CLCD_TIM2);
456 tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
462 tim2 |= div & TIM2_PCD_LO_MASK;
463 tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
466 writel(tim2, priv->regs + CLCD_TIM2);
467 spin_unlock(&priv->tim2_lock);
472 static const struct clk_ops pl111_clk_div_ops = {
473 .recalc_rate = pl111_clk_div_recalc_rate,
474 .round_rate = pl111_clk_div_round_rate,
475 .set_rate = pl111_clk_div_set_rate,
479 pl111_init_clock_divider(struct drm_device *drm)
481 struct pl111_drm_dev_private *priv = drm->dev_private;
482 struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
483 struct clk_hw *div = &priv->clk_div;
484 const char *parent_name;
485 struct clk_init_data init = {
487 .ops = &pl111_clk_div_ops,
488 .parent_names = &parent_name,
490 .flags = CLK_SET_RATE_PARENT,
494 if (IS_ERR(parent)) {
495 dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
496 return PTR_ERR(parent);
498 /* If the clock divider is broken, use the parent directly */
499 if (priv->variant->broken_clockdivider) {
503 parent_name = __clk_get_name(parent);
505 spin_lock_init(&priv->tim2_lock);
508 ret = devm_clk_hw_register(drm->dev, div);
510 priv->clk = div->clk;
514 int pl111_display_init(struct drm_device *drm)
516 struct pl111_drm_dev_private *priv = drm->dev_private;
517 struct device *dev = drm->dev;
518 struct device_node *endpoint;
522 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
526 if (of_property_read_u32_array(endpoint,
527 "arm,pl11x,tft-r0g0b0-pads",
529 ARRAY_SIZE(tft_r0b0g0)) != 0) {
530 dev_err(dev, "arm,pl11x,tft-r0g0b0-pads should be 3 ints\n");
531 of_node_put(endpoint);
534 of_node_put(endpoint);
536 ret = pl111_init_clock_divider(drm);
540 if (!priv->variant->broken_vblank) {
541 pl111_display_funcs.enable_vblank = pl111_display_enable_vblank;
542 pl111_display_funcs.disable_vblank = pl111_display_disable_vblank;
545 ret = drm_simple_display_pipe_init(drm, &priv->pipe,
546 &pl111_display_funcs,
547 priv->variant->formats,
548 priv->variant->nformats,