2 * rcar_du_lvdsenc.c -- R-Car Display Unit LVDS Encoder
4 * Copyright (C) 2013 Renesas 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 int rcar_du_lvdsenc_start(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
;
51 if (lvds
->dpms
== DRM_MODE_DPMS_ON
)
54 ret
= clk_prepare_enable(lvds
->clock
);
58 /* PLL clock configuration */
60 pllcr
= LVDPLLCR_CEEN
| LVDPLLCR_COSEL
| LVDPLLCR_PLLDLYCNT_38M
;
61 else if (freq
<= 60000)
62 pllcr
= LVDPLLCR_CEEN
| LVDPLLCR_COSEL
| LVDPLLCR_PLLDLYCNT_60M
;
63 else if (freq
<= 121000)
64 pllcr
= LVDPLLCR_CEEN
| LVDPLLCR_COSEL
| LVDPLLCR_PLLDLYCNT_121M
;
66 pllcr
= LVDPLLCR_PLLDLYCNT_150M
;
68 rcar_lvds_write(lvds
, LVDPLLCR
, pllcr
);
70 /* Hardcode the channels and control signals routing for now.
77 rcar_lvds_write(lvds
, LVDCTRCR
, LVDCTRCR_CTR3SEL_ZERO
|
78 LVDCTRCR_CTR2SEL_DISP
| LVDCTRCR_CTR1SEL_VSYNC
|
79 LVDCTRCR_CTR0SEL_HSYNC
);
81 if (rcar_du_needs(lvds
->dev
, RCAR_DU_QUIRK_LVDS_LANES
))
82 lvdhcr
= LVDCHCR_CHSEL_CH(0, 0) | LVDCHCR_CHSEL_CH(1, 3)
83 | LVDCHCR_CHSEL_CH(2, 2) | LVDCHCR_CHSEL_CH(3, 1);
85 lvdhcr
= LVDCHCR_CHSEL_CH(0, 0) | LVDCHCR_CHSEL_CH(1, 1)
86 | LVDCHCR_CHSEL_CH(2, 2) | LVDCHCR_CHSEL_CH(3, 3);
88 rcar_lvds_write(lvds
, LVDCHCR
, lvdhcr
);
90 /* Select the input, hardcode mode 0, enable LVDS operation and turn
93 lvdcr0
= LVDCR0_BEN
| LVDCR0_LVEN
;
94 if (rcrtc
->index
== 2)
95 lvdcr0
|= LVDCR0_DUSEL
;
96 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
98 /* Turn all the channels on. */
99 rcar_lvds_write(lvds
, LVDCR1
, LVDCR1_CHSTBY(3) | LVDCR1_CHSTBY(2) |
100 LVDCR1_CHSTBY(1) | LVDCR1_CHSTBY(0) | LVDCR1_CLKSTBY
);
102 /* Turn the PLL on, wait for the startup delay, and turn the output
105 lvdcr0
|= LVDCR0_PLLEN
;
106 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
108 usleep_range(100, 150);
110 lvdcr0
|= LVDCR0_LVRES
;
111 rcar_lvds_write(lvds
, LVDCR0
, lvdcr0
);
113 lvds
->dpms
= DRM_MODE_DPMS_ON
;
117 static void rcar_du_lvdsenc_stop(struct rcar_du_lvdsenc
*lvds
)
119 if (lvds
->dpms
== DRM_MODE_DPMS_OFF
)
122 rcar_lvds_write(lvds
, LVDCR0
, 0);
123 rcar_lvds_write(lvds
, LVDCR1
, 0);
125 clk_disable_unprepare(lvds
->clock
);
127 lvds
->dpms
= DRM_MODE_DPMS_OFF
;
130 int rcar_du_lvdsenc_dpms(struct rcar_du_lvdsenc
*lvds
,
131 struct drm_crtc
*crtc
, int mode
)
133 if (mode
== DRM_MODE_DPMS_OFF
) {
134 rcar_du_lvdsenc_stop(lvds
);
137 struct rcar_du_crtc
*rcrtc
= to_rcar_crtc(crtc
);
138 return rcar_du_lvdsenc_start(lvds
, rcrtc
);
143 static int rcar_du_lvdsenc_get_resources(struct rcar_du_lvdsenc
*lvds
,
144 struct platform_device
*pdev
)
146 struct resource
*mem
;
149 sprintf(name
, "lvds.%u", lvds
->index
);
151 mem
= platform_get_resource_byname(pdev
, IORESOURCE_MEM
, name
);
152 lvds
->mmio
= devm_ioremap_resource(&pdev
->dev
, mem
);
153 if (IS_ERR(lvds
->mmio
))
154 return PTR_ERR(lvds
->mmio
);
156 lvds
->clock
= devm_clk_get(&pdev
->dev
, name
);
157 if (IS_ERR(lvds
->clock
)) {
158 dev_err(&pdev
->dev
, "failed to get clock for %s\n", name
);
159 return PTR_ERR(lvds
->clock
);
165 int rcar_du_lvdsenc_init(struct rcar_du_device
*rcdu
)
167 struct platform_device
*pdev
= to_platform_device(rcdu
->dev
);
168 struct rcar_du_lvdsenc
*lvds
;
172 for (i
= 0; i
< rcdu
->info
->num_lvds
; ++i
) {
173 lvds
= devm_kzalloc(&pdev
->dev
, sizeof(*lvds
), GFP_KERNEL
);
175 dev_err(&pdev
->dev
, "failed to allocate private data\n");
181 lvds
->input
= i
? RCAR_LVDS_INPUT_DU1
: RCAR_LVDS_INPUT_DU0
;
182 lvds
->dpms
= DRM_MODE_DPMS_OFF
;
184 ret
= rcar_du_lvdsenc_get_resources(lvds
, pdev
);
188 rcdu
->lvds
[i
] = lvds
;