2 * rcar_du_lvdsenc.c -- R-Car Display Unit LVDS Encoder
4 * Copyright (C) 2013-2014 Renesas Electronics Corporation
6 * Contact: Laurent Pinchart (laurent.pinchart@ideasonboard.com)
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/clk.h>
15 #include <linux/delay.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
20 #include "rcar_du_drv.h"
21 #include "rcar_du_encoder.h"
22 #include "rcar_du_lvdsenc.h"
23 #include "rcar_lvds_regs.h"
25 struct rcar_du_lvdsenc
{
26 struct rcar_du_device
*dev
;
33 enum rcar_lvds_input input
;
36 static void rcar_lvds_write(struct rcar_du_lvdsenc
*lvds
, u32 reg
, u32 data
)
38 iowrite32(data
, lvds
->mmio
+ reg
);
41 static void rcar_du_lvdsenc_start_gen2(struct rcar_du_lvdsenc
*lvds
,
42 struct rcar_du_crtc
*rcrtc
)
44 const struct drm_display_mode
*mode
= &rcrtc
->crtc
.mode
;
45 unsigned int freq
= mode
->clock
;
49 /* PLL clock configuration */
51 pllcr
= LVDPLLCR_CEEN
| LVDPLLCR_COSEL
| LVDPLLCR_PLLDLYCNT_38M
;
52 else if (freq
< 61000)
53 pllcr
= LVDPLLCR_CEEN
| LVDPLLCR_COSEL
| LVDPLLCR_PLLDLYCNT_60M
;
54 else if (freq
< 121000)
55 pllcr
= LVDPLLCR_CEEN
| LVDPLLCR_COSEL
| LVDPLLCR_PLLDLYCNT_121M
;
57 pllcr
= LVDPLLCR_PLLDLYCNT_150M
;
59 rcar_lvds_write(lvds
, LVDPLLCR
, pllcr
);
61 /* Select the input, hardcode mode 0, enable LVDS operation and turn
64 lvdcr0
= LVDCR0_BEN
| LVDCR0_LVEN
;
65 if (rcrtc
->index
== 2)
66 lvdcr0
|= LVDCR0_DUSEL
;
67 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
69 /* Turn all the channels on. */
70 rcar_lvds_write(lvds
, LVDCR1
,
71 LVDCR1_CHSTBY_GEN2(3) | LVDCR1_CHSTBY_GEN2(2) |
72 LVDCR1_CHSTBY_GEN2(1) | LVDCR1_CHSTBY_GEN2(0) |
75 /* Turn the PLL on, wait for the startup delay, and turn the output
78 lvdcr0
|= LVDCR0_PLLON
;
79 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
81 usleep_range(100, 150);
83 lvdcr0
|= LVDCR0_LVRES
;
84 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
87 static void rcar_du_lvdsenc_start_gen3(struct rcar_du_lvdsenc
*lvds
,
88 struct rcar_du_crtc
*rcrtc
)
90 const struct drm_display_mode
*mode
= &rcrtc
->crtc
.mode
;
91 unsigned int freq
= mode
->clock
;
95 /* PLL clock configuration */
97 pllcr
= LVDPLLCR_PLLDIVCNT_42M
;
98 else if (freq
< 85000)
99 pllcr
= LVDPLLCR_PLLDIVCNT_85M
;
100 else if (freq
< 128000)
101 pllcr
= LVDPLLCR_PLLDIVCNT_128M
;
103 pllcr
= LVDPLLCR_PLLDIVCNT_148M
;
105 rcar_lvds_write(lvds
, LVDPLLCR
, pllcr
);
107 /* Turn the PLL on, set it to LVDS normal mode, wait for the startup
108 * delay and turn the output on.
110 lvdcr0
= LVDCR0_PLLON
;
111 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
113 lvdcr0
|= LVDCR0_PWD
;
114 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
116 usleep_range(100, 150);
118 lvdcr0
|= LVDCR0_LVRES
;
119 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
121 /* Turn all the channels on. */
122 rcar_lvds_write(lvds
, LVDCR1
,
123 LVDCR1_CHSTBY_GEN3(3) | LVDCR1_CHSTBY_GEN3(2) |
124 LVDCR1_CHSTBY_GEN3(1) | LVDCR1_CHSTBY_GEN3(0) |
125 LVDCR1_CLKSTBY_GEN3
);
128 static int rcar_du_lvdsenc_start(struct rcar_du_lvdsenc
*lvds
,
129 struct rcar_du_crtc
*rcrtc
)
137 ret
= clk_prepare_enable(lvds
->clock
);
141 /* Hardcode the channels and control signals routing for now.
148 rcar_lvds_write(lvds
, LVDCTRCR
, LVDCTRCR_CTR3SEL_ZERO
|
149 LVDCTRCR_CTR2SEL_DISP
| LVDCTRCR_CTR1SEL_VSYNC
|
150 LVDCTRCR_CTR0SEL_HSYNC
);
152 if (rcar_du_needs(lvds
->dev
, RCAR_DU_QUIRK_LVDS_LANES
))
153 lvdhcr
= LVDCHCR_CHSEL_CH(0, 0) | LVDCHCR_CHSEL_CH(1, 3)
154 | LVDCHCR_CHSEL_CH(2, 2) | LVDCHCR_CHSEL_CH(3, 1);
156 lvdhcr
= LVDCHCR_CHSEL_CH(0, 0) | LVDCHCR_CHSEL_CH(1, 1)
157 | LVDCHCR_CHSEL_CH(2, 2) | LVDCHCR_CHSEL_CH(3, 3);
159 rcar_lvds_write(lvds
, LVDCHCR
, lvdhcr
);
161 /* Perform generation-specific initialization. */
162 if (lvds
->dev
->info
->gen
< 3)
163 rcar_du_lvdsenc_start_gen2(lvds
, rcrtc
);
165 rcar_du_lvdsenc_start_gen3(lvds
, rcrtc
);
167 lvds
->enabled
= true;
172 static void rcar_du_lvdsenc_stop(struct rcar_du_lvdsenc
*lvds
)
177 rcar_lvds_write(lvds
, LVDCR0
, 0);
178 rcar_lvds_write(lvds
, LVDCR1
, 0);
180 clk_disable_unprepare(lvds
->clock
);
182 lvds
->enabled
= false;
185 int rcar_du_lvdsenc_enable(struct rcar_du_lvdsenc
*lvds
, struct drm_crtc
*crtc
,
189 rcar_du_lvdsenc_stop(lvds
);
192 struct rcar_du_crtc
*rcrtc
= to_rcar_crtc(crtc
);
193 return rcar_du_lvdsenc_start(lvds
, rcrtc
);
198 void rcar_du_lvdsenc_atomic_check(struct rcar_du_lvdsenc
*lvds
,
199 struct drm_display_mode
*mode
)
201 struct rcar_du_device
*rcdu
= lvds
->dev
;
203 /* The internal LVDS encoder has a restricted clock frequency operating
204 * range (30MHz to 150MHz on Gen2, 25.175MHz to 148.5MHz on Gen3). Clamp
205 * the clock accordingly.
207 if (rcdu
->info
->gen
< 3)
208 mode
->clock
= clamp(mode
->clock
, 30000, 150000);
210 mode
->clock
= clamp(mode
->clock
, 25175, 148500);
213 static int rcar_du_lvdsenc_get_resources(struct rcar_du_lvdsenc
*lvds
,
214 struct platform_device
*pdev
)
216 struct resource
*mem
;
219 sprintf(name
, "lvds.%u", lvds
->index
);
221 mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
222 lvds
->mmio
= devm_ioremap_resource(&pdev
->dev
, mem
);
223 if (IS_ERR(lvds
->mmio
))
224 return PTR_ERR(lvds
->mmio
);
226 lvds
->clock
= devm_clk_get(&pdev
->dev
, name
);
227 if (IS_ERR(lvds
->clock
)) {
228 dev_err(&pdev
->dev
, "failed to get clock for %s\n", name
);
229 return PTR_ERR(lvds
->clock
);
235 int rcar_du_lvdsenc_init(struct rcar_du_device
*rcdu
)
237 struct platform_device
*pdev
= to_platform_device(rcdu
->dev
);
238 struct rcar_du_lvdsenc
*lvds
;
242 for (i
= 0; i
< rcdu
->info
->num_lvds
; ++i
) {
243 lvds
= devm_kzalloc(&pdev
->dev
, sizeof(*lvds
), GFP_KERNEL
);
245 dev_err(&pdev
->dev
, "failed to allocate private data\n");
251 lvds
->input
= i
? RCAR_LVDS_INPUT_DU1
: RCAR_LVDS_INPUT_DU0
;
252 lvds
->enabled
= false;
254 ret
= rcar_du_lvdsenc_get_resources(lvds
, pdev
);
258 rcdu
->lvds
[i
] = lvds
;