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
7 * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
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_panel.h>
25 #include <drm/drm_gem_cma_helper.h>
26 #include <drm/drm_gem_framebuffer_helper.h>
27 #include <drm/drm_fb_cma_helper.h>
29 #include "pl111_drm.h"
31 irqreturn_t pl111_irq(int irq, void *data)
33 struct pl111_drm_dev_private *priv = data;
35 irqreturn_t status = IRQ_NONE;
37 irq_stat = readl(priv->regs + CLCD_PL111_MIS);
42 if (irq_stat & CLCD_IRQ_NEXTBASE_UPDATE) {
43 drm_crtc_handle_vblank(&priv->pipe.crtc);
48 /* Clear the interrupt once done */
49 writel(irq_stat, priv->regs + CLCD_PL111_ICR);
54 static int pl111_display_check(struct drm_simple_display_pipe *pipe,
55 struct drm_plane_state *pstate,
56 struct drm_crtc_state *cstate)
58 const struct drm_display_mode *mode = &cstate->mode;
59 struct drm_framebuffer *old_fb = pipe->plane.state->fb;
60 struct drm_framebuffer *fb = pstate->fb;
62 if (mode->hdisplay % 16)
66 u32 offset = drm_fb_cma_get_gem_addr(fb, pstate, 0);
68 /* FB base address must be dword aligned. */
72 /* There's no pitch register -- the mode's hdisplay
75 if (fb->pitches[0] != mode->hdisplay * fb->format->cpp[0])
78 /* We can't change the FB format in a flicker-free
79 * manner (and only update it during CRTC enable).
81 if (old_fb && old_fb->format != fb->format)
82 cstate->mode_changed = true;
88 static void pl111_display_enable(struct drm_simple_display_pipe *pipe,
89 struct drm_crtc_state *cstate)
91 struct drm_crtc *crtc = &pipe->crtc;
92 struct drm_plane *plane = &pipe->plane;
93 struct drm_device *drm = crtc->dev;
94 struct pl111_drm_dev_private *priv = drm->dev_private;
95 const struct drm_display_mode *mode = &cstate->mode;
96 struct drm_framebuffer *fb = plane->state->fb;
97 struct drm_connector *connector = &priv->connector.connector;
99 u32 ppl, hsw, hfp, hbp;
100 u32 lpp, vsw, vfp, vbp;
104 ret = clk_set_rate(priv->clk, mode->clock * 1000);
107 "Failed to set pixel clock rate to %d: %d\n",
108 mode->clock * 1000, ret);
111 clk_prepare_enable(priv->clk);
113 ppl = (mode->hdisplay / 16) - 1;
114 hsw = mode->hsync_end - mode->hsync_start - 1;
115 hfp = mode->hsync_start - mode->hdisplay - 1;
116 hbp = mode->htotal - mode->hsync_end - 1;
118 lpp = mode->vdisplay - 1;
119 vsw = mode->vsync_end - mode->vsync_start - 1;
120 vfp = mode->vsync_start - mode->vdisplay;
121 vbp = mode->vtotal - mode->vsync_end;
123 cpl = mode->hdisplay - 1;
129 priv->regs + CLCD_TIM0);
134 priv->regs + CLCD_TIM1);
136 spin_lock(&priv->tim2_lock);
138 tim2 = readl(priv->regs + CLCD_TIM2);
139 tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
141 if (mode->flags & DRM_MODE_FLAG_NHSYNC)
144 if (mode->flags & DRM_MODE_FLAG_NVSYNC)
147 if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
150 if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
154 writel(tim2, priv->regs + CLCD_TIM2);
155 spin_unlock(&priv->tim2_lock);
157 writel(0, priv->regs + CLCD_TIM3);
159 drm_panel_prepare(priv->connector.panel);
161 /* Enable and Power Up */
162 cntl = CNTL_LCDEN | CNTL_LCDTFT | CNTL_LCDPWR | CNTL_LCDVCOMP(1);
164 /* Note that the the hardware's format reader takes 'r' from
165 * the low bit, while DRM formats list channels from high bit
166 * to low bit as you read left to right.
168 switch (fb->format->format) {
169 case DRM_FORMAT_ABGR8888:
170 case DRM_FORMAT_XBGR8888:
171 cntl |= CNTL_LCDBPP24;
173 case DRM_FORMAT_ARGB8888:
174 case DRM_FORMAT_XRGB8888:
175 cntl |= CNTL_LCDBPP24 | CNTL_BGR;
177 case DRM_FORMAT_BGR565:
178 cntl |= CNTL_LCDBPP16_565;
180 case DRM_FORMAT_RGB565:
181 cntl |= CNTL_LCDBPP16_565 | CNTL_BGR;
183 case DRM_FORMAT_ABGR1555:
184 case DRM_FORMAT_XBGR1555:
185 cntl |= CNTL_LCDBPP16;
187 case DRM_FORMAT_ARGB1555:
188 case DRM_FORMAT_XRGB1555:
189 cntl |= CNTL_LCDBPP16 | CNTL_BGR;
191 case DRM_FORMAT_ABGR4444:
192 case DRM_FORMAT_XBGR4444:
193 cntl |= CNTL_LCDBPP16_444;
195 case DRM_FORMAT_ARGB4444:
196 case DRM_FORMAT_XRGB4444:
197 cntl |= CNTL_LCDBPP16_444 | CNTL_BGR;
200 WARN_ONCE(true, "Unknown FB format 0x%08x\n",
205 writel(cntl, priv->regs + CLCD_PL111_CNTL);
207 drm_panel_enable(priv->connector.panel);
209 drm_crtc_vblank_on(crtc);
212 void pl111_display_disable(struct drm_simple_display_pipe *pipe)
214 struct drm_crtc *crtc = &pipe->crtc;
215 struct drm_device *drm = crtc->dev;
216 struct pl111_drm_dev_private *priv = drm->dev_private;
218 drm_crtc_vblank_off(crtc);
220 drm_panel_disable(priv->connector.panel);
222 /* Disable and Power Down */
223 writel(0, priv->regs + CLCD_PL111_CNTL);
225 drm_panel_unprepare(priv->connector.panel);
227 clk_disable_unprepare(priv->clk);
230 static void pl111_display_update(struct drm_simple_display_pipe *pipe,
231 struct drm_plane_state *old_pstate)
233 struct drm_crtc *crtc = &pipe->crtc;
234 struct drm_device *drm = crtc->dev;
235 struct pl111_drm_dev_private *priv = drm->dev_private;
236 struct drm_pending_vblank_event *event = crtc->state->event;
237 struct drm_plane *plane = &pipe->plane;
238 struct drm_plane_state *pstate = plane->state;
239 struct drm_framebuffer *fb = pstate->fb;
242 u32 addr = drm_fb_cma_get_gem_addr(fb, pstate, 0);
244 writel(addr, priv->regs + CLCD_UBAS);
248 crtc->state->event = NULL;
250 spin_lock_irq(&crtc->dev->event_lock);
251 if (crtc->state->active && drm_crtc_vblank_get(crtc) == 0)
252 drm_crtc_arm_vblank_event(crtc, event);
254 drm_crtc_send_vblank_event(crtc, event);
255 spin_unlock_irq(&crtc->dev->event_lock);
259 int pl111_enable_vblank(struct drm_device *drm, unsigned int crtc)
261 struct pl111_drm_dev_private *priv = drm->dev_private;
263 writel(CLCD_IRQ_NEXTBASE_UPDATE, priv->regs + CLCD_PL111_IENB);
268 void pl111_disable_vblank(struct drm_device *drm, unsigned int crtc)
270 struct pl111_drm_dev_private *priv = drm->dev_private;
272 writel(0, priv->regs + CLCD_PL111_IENB);
275 static int pl111_display_prepare_fb(struct drm_simple_display_pipe *pipe,
276 struct drm_plane_state *plane_state)
278 return drm_gem_fb_prepare_fb(&pipe->plane, plane_state);
281 static const struct drm_simple_display_pipe_funcs pl111_display_funcs = {
282 .check = pl111_display_check,
283 .enable = pl111_display_enable,
284 .disable = pl111_display_disable,
285 .update = pl111_display_update,
286 .prepare_fb = pl111_display_prepare_fb,
289 static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
290 unsigned long *prate, bool set_parent)
292 int best_div = 1, div;
293 struct clk_hw *parent = clk_hw_get_parent(hw);
294 unsigned long best_prate = 0;
295 unsigned long best_diff = ~0ul;
296 int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
298 for (div = 1; div < max_div; div++) {
299 unsigned long this_prate, div_rate, diff;
302 this_prate = clk_hw_round_rate(parent, rate * div);
305 div_rate = DIV_ROUND_UP_ULL(this_prate, div);
306 diff = abs(rate - div_rate);
308 if (diff < best_diff) {
311 best_prate = this_prate;
319 static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
320 unsigned long *prate)
322 int div = pl111_clk_div_choose_div(hw, rate, prate, true);
324 return DIV_ROUND_UP_ULL(*prate, div);
327 static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
330 struct pl111_drm_dev_private *priv =
331 container_of(hw, struct pl111_drm_dev_private, clk_div);
332 u32 tim2 = readl(priv->regs + CLCD_TIM2);
338 div = tim2 & TIM2_PCD_LO_MASK;
339 div |= (tim2 & TIM2_PCD_HI_MASK) >>
340 (TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
343 return DIV_ROUND_UP_ULL(prate, div);
346 static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
349 struct pl111_drm_dev_private *priv =
350 container_of(hw, struct pl111_drm_dev_private, clk_div);
351 int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
354 spin_lock(&priv->tim2_lock);
355 tim2 = readl(priv->regs + CLCD_TIM2);
356 tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
362 tim2 |= div & TIM2_PCD_LO_MASK;
363 tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
366 writel(tim2, priv->regs + CLCD_TIM2);
367 spin_unlock(&priv->tim2_lock);
372 static const struct clk_ops pl111_clk_div_ops = {
373 .recalc_rate = pl111_clk_div_recalc_rate,
374 .round_rate = pl111_clk_div_round_rate,
375 .set_rate = pl111_clk_div_set_rate,
379 pl111_init_clock_divider(struct drm_device *drm)
381 struct pl111_drm_dev_private *priv = drm->dev_private;
382 struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
383 struct clk_hw *div = &priv->clk_div;
384 const char *parent_name;
385 struct clk_init_data init = {
387 .ops = &pl111_clk_div_ops,
388 .parent_names = &parent_name,
390 .flags = CLK_SET_RATE_PARENT,
394 if (IS_ERR(parent)) {
395 dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
396 return PTR_ERR(parent);
398 parent_name = __clk_get_name(parent);
400 spin_lock_init(&priv->tim2_lock);
403 ret = devm_clk_hw_register(drm->dev, div);
405 priv->clk = div->clk;
409 int pl111_display_init(struct drm_device *drm)
411 struct pl111_drm_dev_private *priv = drm->dev_private;
412 struct device *dev = drm->dev;
413 struct device_node *endpoint;
416 static const u32 formats[] = {
433 endpoint = of_graph_get_next_endpoint(dev->of_node, NULL);
437 if (of_property_read_u32_array(endpoint,
438 "arm,pl11x,tft-r0g0b0-pads",
440 ARRAY_SIZE(tft_r0b0g0)) != 0) {
441 dev_err(dev, "arm,pl11x,tft-r0g0b0-pads should be 3 ints\n");
442 of_node_put(endpoint);
445 of_node_put(endpoint);
447 if (tft_r0b0g0[0] != 0 ||
448 tft_r0b0g0[1] != 8 ||
449 tft_r0b0g0[2] != 16) {
450 dev_err(dev, "arm,pl11x,tft-r0g0b0-pads != [0,8,16] not yet supported\n");
454 ret = pl111_init_clock_divider(drm);
458 ret = drm_simple_display_pipe_init(drm, &priv->pipe,
459 &pl111_display_funcs,
460 formats, ARRAY_SIZE(formats),
461 NULL, &priv->connector.connector);