Skip to content

Commit 032838f

Browse files
committed
drm/pl111: Register the clock divider and use it.
This is required for the panel to work on bcm911360, where CLCDCLK is the fixed 200Mhz AXI41 clock. The rate set is still passed up to the CLCDCLK, for platforms that have a settable rate on that one. v2: Set SET_RATE_PARENT (caught by Linus Walleij), depend on COMMON_CLK. v3: Mark the clk_ops static (caught by Stephen). Signed-off-by: Eric Anholt <eric@anholt.net> Link: http://patchwork.freedesktop.org/patch/msgid/20170508193348.30236-1-eric@anholt.net Reviewed-by: Linus Walleij <linus.walleij@linaro.org> Reviewed-by: Stephen Boyd <sboyd@codeaurora.org>
1 parent 2098105 commit 032838f

5 files changed

Lines changed: 163 additions & 24 deletions

File tree

‎drivers/gpu/drm/pl111/Kconfig‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ config DRM_PL111
22
tristate "DRM Support for PL111 CLCD Controller"
33
depends on DRM
44
depends on ARM || ARM64 || COMPILE_TEST
5+
depends on COMMON_CLK
56
select DRM_KMS_HELPER
67
select DRM_KMS_CMA_HELPER
78
select DRM_GEM_CMA_HELPER

‎drivers/gpu/drm/pl111/pl111_display.c‎

Lines changed: 147 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ static void pl111_display_enable(struct drm_simple_display_pipe *pipe,
108108
u32 cntl;
109109
u32 ppl, hsw, hfp, hbp;
110110
u32 lpp, vsw, vfp, vbp;
111-
u32 cpl;
111+
u32 cpl, tim2;
112112
int ret;
113113

114114
ret = clk_set_rate(priv->clk, mode->clock * 1000);
@@ -142,20 +142,28 @@ static void pl111_display_enable(struct drm_simple_display_pipe *pipe,
142142
(vfp << 16) |
143143
(vbp << 24),
144144
priv->regs + CLCD_TIM1);
145-
/* XXX: We currently always use CLCDCLK with no divisor. We
146-
* could probably reduce power consumption by using HCLK
147-
* (apb_pclk) with a divisor when it gets us near our target
148-
* pixel clock.
149-
*/
150-
writel(((mode->flags & DRM_MODE_FLAG_NHSYNC) ? TIM2_IHS : 0) |
151-
((mode->flags & DRM_MODE_FLAG_NVSYNC) ? TIM2_IVS : 0) |
152-
((connector->display_info.bus_flags &
153-
DRM_BUS_FLAG_DE_LOW) ? TIM2_IOE : 0) |
154-
((connector->display_info.bus_flags &
155-
DRM_BUS_FLAG_PIXDATA_NEGEDGE) ? TIM2_IPC : 0) |
156-
TIM2_BCD |
157-
(cpl << 16),
158-
priv->regs + CLCD_TIM2);
145+
146+
spin_lock(&priv->tim2_lock);
147+
148+
tim2 = readl(priv->regs + CLCD_TIM2);
149+
tim2 &= (TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
150+
151+
if (mode->flags & DRM_MODE_FLAG_NHSYNC)
152+
tim2 |= TIM2_IHS;
153+
154+
if (mode->flags & DRM_MODE_FLAG_NVSYNC)
155+
tim2 |= TIM2_IVS;
156+
157+
if (connector->display_info.bus_flags & DRM_BUS_FLAG_DE_LOW)
158+
tim2 |= TIM2_IOE;
159+
160+
if (connector->display_info.bus_flags & DRM_BUS_FLAG_PIXDATA_NEGEDGE)
161+
tim2 |= TIM2_IPC;
162+
163+
tim2 |= cpl << 16;
164+
writel(tim2, priv->regs + CLCD_TIM2);
165+
spin_unlock(&priv->tim2_lock);
166+
159167
writel(0, priv->regs + CLCD_TIM3);
160168

161169
drm_panel_prepare(priv->connector.panel);
@@ -288,6 +296,126 @@ const struct drm_simple_display_pipe_funcs pl111_display_funcs = {
288296
.prepare_fb = pl111_display_prepare_fb,
289297
};
290298

299+
static int pl111_clk_div_choose_div(struct clk_hw *hw, unsigned long rate,
300+
unsigned long *prate, bool set_parent)
301+
{
302+
int best_div = 1, div;
303+
struct clk_hw *parent = clk_hw_get_parent(hw);
304+
unsigned long best_prate = 0;
305+
unsigned long best_diff = ~0ul;
306+
int max_div = (1 << (TIM2_PCD_LO_BITS + TIM2_PCD_HI_BITS)) - 1;
307+
308+
for (div = 1; div < max_div; div++) {
309+
unsigned long this_prate, div_rate, diff;
310+
311+
if (set_parent)
312+
this_prate = clk_hw_round_rate(parent, rate * div);
313+
else
314+
this_prate = *prate;
315+
div_rate = DIV_ROUND_UP_ULL(this_prate, div);
316+
diff = abs(rate - div_rate);
317+
318+
if (diff < best_diff) {
319+
best_div = div;
320+
best_diff = diff;
321+
best_prate = this_prate;
322+
}
323+
}
324+
325+
*prate = best_prate;
326+
return best_div;
327+
}
328+
329+
static long pl111_clk_div_round_rate(struct clk_hw *hw, unsigned long rate,
330+
unsigned long *prate)
331+
{
332+
int div = pl111_clk_div_choose_div(hw, rate, prate, true);
333+
334+
return DIV_ROUND_UP_ULL(*prate, div);
335+
}
336+
337+
static unsigned long pl111_clk_div_recalc_rate(struct clk_hw *hw,
338+
unsigned long prate)
339+
{
340+
struct pl111_drm_dev_private *priv =
341+
container_of(hw, struct pl111_drm_dev_private, clk_div);
342+
u32 tim2 = readl(priv->regs + CLCD_TIM2);
343+
int div;
344+
345+
if (tim2 & TIM2_BCD)
346+
return prate;
347+
348+
div = tim2 & TIM2_PCD_LO_MASK;
349+
div |= (tim2 & TIM2_PCD_HI_MASK) >>
350+
(TIM2_PCD_HI_SHIFT - TIM2_PCD_LO_BITS);
351+
div += 2;
352+
353+
return DIV_ROUND_UP_ULL(prate, div);
354+
}
355+
356+
static int pl111_clk_div_set_rate(struct clk_hw *hw, unsigned long rate,
357+
unsigned long prate)
358+
{
359+
struct pl111_drm_dev_private *priv =
360+
container_of(hw, struct pl111_drm_dev_private, clk_div);
361+
int div = pl111_clk_div_choose_div(hw, rate, &prate, false);
362+
u32 tim2;
363+
364+
spin_lock(&priv->tim2_lock);
365+
tim2 = readl(priv->regs + CLCD_TIM2);
366+
tim2 &= ~(TIM2_BCD | TIM2_PCD_LO_MASK | TIM2_PCD_HI_MASK);
367+
368+
if (div == 1) {
369+
tim2 |= TIM2_BCD;
370+
} else {
371+
div -= 2;
372+
tim2 |= div & TIM2_PCD_LO_MASK;
373+
tim2 |= (div >> TIM2_PCD_LO_BITS) << TIM2_PCD_HI_SHIFT;
374+
}
375+
376+
writel(tim2, priv->regs + CLCD_TIM2);
377+
spin_unlock(&priv->tim2_lock);
378+
379+
return 0;
380+
}
381+
382+
static const struct clk_ops pl111_clk_div_ops = {
383+
.recalc_rate = pl111_clk_div_recalc_rate,
384+
.round_rate = pl111_clk_div_round_rate,
385+
.set_rate = pl111_clk_div_set_rate,
386+
};
387+
388+
static int
389+
pl111_init_clock_divider(struct drm_device *drm)
390+
{
391+
struct pl111_drm_dev_private *priv = drm->dev_private;
392+
struct clk *parent = devm_clk_get(drm->dev, "clcdclk");
393+
struct clk_hw *div = &priv->clk_div;
394+
const char *parent_name;
395+
struct clk_init_data init = {
396+
.name = "pl111_div",
397+
.ops = &pl111_clk_div_ops,
398+
.parent_names = &parent_name,
399+
.num_parents = 1,
400+
.flags = CLK_SET_RATE_PARENT,
401+
};
402+
int ret;
403+
404+
if (IS_ERR(parent)) {
405+
dev_err(drm->dev, "CLCD: unable to get clcdclk.\n");
406+
return PTR_ERR(parent);
407+
}
408+
parent_name = __clk_get_name(parent);
409+
410+
spin_lock_init(&priv->tim2_lock);
411+
div->init = &init;
412+
413+
ret = devm_clk_hw_register(drm->dev, div);
414+
415+
priv->clk = div->clk;
416+
return ret;
417+
}
418+
291419
int pl111_display_init(struct drm_device *drm)
292420
{
293421
struct pl111_drm_dev_private *priv = drm->dev_private;
@@ -333,6 +461,10 @@ int pl111_display_init(struct drm_device *drm)
333461
return -EINVAL;
334462
}
335463

464+
ret = pl111_init_clock_divider(drm);
465+
if (ret)
466+
return ret;
467+
336468
ret = drm_simple_display_pipe_init(drm, &priv->pipe,
337469
&pl111_display_funcs,
338470
formats, ARRAY_SIZE(formats),

‎drivers/gpu/drm/pl111/pl111_drm.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <drm/drm_gem.h>
2323
#include <drm/drm_simple_kms_helper.h>
24+
#include <linux/clk-provider.h>
2425

2526
#define CLCD_IRQ_NEXTBASE_UPDATE BIT(2)
2627

@@ -37,7 +38,14 @@ struct pl111_drm_dev_private {
3738
struct drm_fbdev_cma *fbdev;
3839

3940
void *regs;
41+
/* The pixel clock (a reference to our clock divider off of CLCDCLK). */
4042
struct clk *clk;
43+
/* pl111's internal clock divider. */
44+
struct clk_hw clk_div;
45+
/* Lock to sync access to CLCD_TIM2 between the common clock
46+
* subsystem and pl111_display_enable().
47+
*/
48+
spinlock_t tim2_lock;
4149
};
4250

4351
#define to_pl111_connector(x) \

‎drivers/gpu/drm/pl111/pl111_drv.c‎

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@
5050
* - Read back hardware state at boot to skip reprogramming the
5151
* hardware when doing a no-op modeset.
5252
*
53-
* - Use the internal clock divisor to reduce power consumption by
54-
* using HCLK (apb_pclk) when appropriate.
53+
* - Use the CLKSEL bit to support switching between the two external
54+
* clock parents.
5555
*/
5656

5757
#include <linux/amba/bus.h>
@@ -195,13 +195,6 @@ static int pl111_amba_probe(struct amba_device *amba_dev,
195195
priv->drm = drm;
196196
drm->dev_private = priv;
197197

198-
priv->clk = devm_clk_get(dev, "clcdclk");
199-
if (IS_ERR(priv->clk)) {
200-
dev_err(dev, "CLCD: unable to get clk.\n");
201-
ret = PTR_ERR(priv->clk);
202-
goto dev_unref;
203-
}
204-
205198
priv->regs = devm_ioremap_resource(dev, &amba_dev->res);
206199
if (!priv->regs) {
207200
dev_err(dev, "%s failed mmio\n", __func__);

‎include/linux/amba/clcd-regs.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,17 @@
3939
#define CLCD_PALL 0x00000200
4040
#define CLCD_PALETTE 0x00000200
4141

42+
#define TIM2_PCD_LO_MASK GENMASK(4, 0)
43+
#define TIM2_PCD_LO_BITS 5
4244
#define TIM2_CLKSEL (1 << 5)
4345
#define TIM2_IVS (1 << 11)
4446
#define TIM2_IHS (1 << 12)
4547
#define TIM2_IPC (1 << 13)
4648
#define TIM2_IOE (1 << 14)
4749
#define TIM2_BCD (1 << 26)
50+
#define TIM2_PCD_HI_MASK GENMASK(31, 27)
51+
#define TIM2_PCD_HI_BITS 5
52+
#define TIM2_PCD_HI_SHIFT 27
4853

4954
#define CNTL_LCDEN (1 << 0)
5055
#define CNTL_LCDBPP1 (0 << 1)

0 commit comments

Comments
 (0)