xref: /rk3399_rockchip-uboot/drivers/video/drm/rockchip_lvds.c (revision 3befe43d907ef2b615f68ddeb032ba74e0375df1)
1 /*
2  * (C) Copyright 2008-2017 Fuzhou Rockchip Electronics Co., Ltd
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <config.h>
8 #include <common.h>
9 #include <errno.h>
10 #include <dm/device.h>
11 #include <dm/read.h>
12 #include <dm/ofnode.h>
13 #include <syscon.h>
14 #include <regmap.h>
15 #include <dm/device.h>
16 #include <dm/read.h>
17 #include <linux/media-bus-format.h>
18 
19 #include "rockchip_display.h"
20 #include "rockchip_connector.h"
21 #include "rockchip_phy.h"
22 #include "rockchip_panel.h"
23 
24 #define HIWORD_UPDATE(v, h, l)		(((v) << (l)) | (GENMASK(h, l) << 16))
25 
26 #define PX30_GRF_PD_VO_CON1		0x0438
27 #define PX30_LVDS_SELECT(x)		HIWORD_UPDATE(x, 14, 13)
28 #define PX30_LVDS_MODE_EN(x)		HIWORD_UPDATE(x, 12, 12)
29 #define PX30_LVDS_MSBSEL(x)		HIWORD_UPDATE(x, 11, 11)
30 #define PX30_LVDS_P2S_EN(x)		HIWORD_UPDATE(x, 10, 10)
31 #define PX30_LVDS_VOP_SEL(x)		HIWORD_UPDATE(x,  1,  1)
32 
33 #define RK3126_GRF_LVDS_CON0		0x0150
34 #define RK3126_LVDS_P2S_EN(x)		HIWORD_UPDATE(x,  9,  9)
35 #define RK3126_LVDS_MODE_EN(x)		HIWORD_UPDATE(x,  6,  6)
36 #define RK3126_LVDS_MSBSEL(x)		HIWORD_UPDATE(x,  3,  3)
37 #define RK3126_LVDS_SELECT(x)		HIWORD_UPDATE(x,  2,  1)
38 
39 #define RK3288_GRF_SOC_CON6		0x025c
40 #define RK3288_LVDS_LCDC_SEL(x)		HIWORD_UPDATE(x,  3,  3)
41 #define RK3288_GRF_SOC_CON7		0x0260
42 #define RK3288_LVDS_PWRDWN(x)		HIWORD_UPDATE(x, 15, 15)
43 #define RK3288_LVDS_CON_ENABLE_2(x)	HIWORD_UPDATE(x, 12, 12)
44 #define RK3288_LVDS_CON_ENABLE_1(x)	HIWORD_UPDATE(x, 11, 11)
45 #define RK3288_LVDS_CON_DEN_POL(x)	HIWORD_UPDATE(x, 10, 10)
46 #define RK3288_LVDS_CON_HS_POL(x)	HIWORD_UPDATE(x,  9,  9)
47 #define RK3288_LVDS_CON_CLKINV(x)	HIWORD_UPDATE(x,  8,  8)
48 #define RK3288_LVDS_CON_STARTPHASE(x)	HIWORD_UPDATE(x,  7,  7)
49 #define RK3288_LVDS_CON_TTL_EN(x)	HIWORD_UPDATE(x,  6,  6)
50 #define RK3288_LVDS_CON_STARTSEL(x)	HIWORD_UPDATE(x,  5,  5)
51 #define RK3288_LVDS_CON_CHASEL(x)	HIWORD_UPDATE(x,  4,  4)
52 #define RK3288_LVDS_CON_MSBSEL(x)	HIWORD_UPDATE(x,  3,  3)
53 #define RK3288_LVDS_CON_SELECT(x)	HIWORD_UPDATE(x,  2,  0)
54 
55 #define RK3368_GRF_SOC_CON7		0x041c
56 #define RK3368_LVDS_SELECT(x)		HIWORD_UPDATE(x, 14, 13)
57 #define RK3368_LVDS_MODE_EN(x)		HIWORD_UPDATE(x, 12, 12)
58 #define RK3368_LVDS_MSBSEL(x)		HIWORD_UPDATE(x, 11, 11)
59 #define RK3368_LVDS_P2S_EN(x)		HIWORD_UPDATE(x,  6,  6)
60 
61 enum lvds_format {
62 	LVDS_8BIT_MODE_FORMAT_1,
63 	LVDS_8BIT_MODE_FORMAT_2,
64 	LVDS_8BIT_MODE_FORMAT_3,
65 	LVDS_6BIT_MODE,
66 };
67 
68 struct rockchip_lvds;
69 
70 struct rockchip_lvds_funcs {
71 	void (*enable)(struct rockchip_lvds *lvds, int pipe);
72 	void (*disable)(struct rockchip_lvds *lvds);
73 };
74 
75 struct rockchip_lvds {
76 	struct udevice *dev;
77 	struct regmap *grf;
78 	struct rockchip_phy *phy;
79 	const struct drm_display_mode *mode;
80 	const struct rockchip_lvds_funcs *funcs;
81 	enum lvds_format format;
82 	bool data_swap;
83 	bool dual_channel;
84 };
85 
86 static inline struct rockchip_lvds *state_to_lvds(struct display_state *state)
87 {
88 	struct connector_state *conn_state = &state->conn_state;
89 
90 	return dev_get_priv(conn_state->dev);
91 }
92 
93 static int rockchip_lvds_connector_init(struct display_state *state)
94 {
95 	struct rockchip_lvds *lvds = state_to_lvds(state);
96 	struct connector_state *conn_state = &state->conn_state;
97 	struct rockchip_panel *panel = state_get_panel(state);
98 
99 	lvds->mode = &conn_state->mode;
100 	lvds->phy = conn_state->phy;
101 
102 	switch (panel->bus_format) {
103 	case MEDIA_BUS_FMT_RGB666_1X7X3_SPWG:	/* jeida-18 */
104 		lvds->format = LVDS_6BIT_MODE;
105 		break;
106 	case MEDIA_BUS_FMT_RGB888_1X7X4_JEIDA:	/* jeida-24 */
107 		lvds->format = LVDS_8BIT_MODE_FORMAT_2;
108 		break;
109 	case MEDIA_BUS_FMT_RGB888_1X7X4_SPWG:	/* vesa-24 */
110 	default:
111 		lvds->format = LVDS_8BIT_MODE_FORMAT_1;
112 		break;
113 	}
114 
115 	conn_state->type = DRM_MODE_CONNECTOR_LVDS;
116 	conn_state->output_mode = ROCKCHIP_OUT_MODE_P888;
117 	conn_state->color_space = V4L2_COLORSPACE_DEFAULT;
118 
119 	return 0;
120 }
121 
122 static int rockchip_lvds_connector_enable(struct display_state *state)
123 {
124 	struct rockchip_lvds *lvds = state_to_lvds(state);
125 	struct crtc_state *crtc_state = &state->crtc_state;
126 	int pipe = crtc_state->crtc_id;
127 	int ret;
128 
129 	if (lvds->funcs->enable)
130 		lvds->funcs->enable(lvds, pipe);
131 
132 	ret = rockchip_phy_set_mode(lvds->phy, PHY_MODE_VIDEO_LVDS);
133 	if (ret) {
134 		dev_err(lvds->dev, "failed to set phy mode: %d\n", ret);
135 		return ret;
136 	}
137 
138 	rockchip_phy_power_on(lvds->phy);
139 
140 	return 0;
141 }
142 
143 static int rockchip_lvds_connector_disable(struct display_state *state)
144 {
145 	struct rockchip_lvds *lvds = state_to_lvds(state);
146 
147 	rockchip_phy_power_off(lvds->phy);
148 
149 	if (lvds->funcs->disable)
150 		lvds->funcs->disable(lvds);
151 
152 	return 0;
153 }
154 
155 static const struct rockchip_connector_funcs rockchip_lvds_connector_funcs = {
156 	.init = rockchip_lvds_connector_init,
157 	.enable = rockchip_lvds_connector_enable,
158 	.disable = rockchip_lvds_connector_disable,
159 };
160 
161 static int rockchip_lvds_probe(struct udevice *dev)
162 {
163 	struct rockchip_lvds *lvds = dev_get_priv(dev);
164 	const struct rockchip_connector *connector =
165 		(const struct rockchip_connector *)dev_get_driver_data(dev);
166 
167 	lvds->dev = dev;
168 	lvds->funcs = connector->data;
169 	lvds->grf = syscon_get_regmap(dev_get_parent(dev));
170 	lvds->dual_channel = dev_read_bool(dev, "dual-channel");
171 	lvds->data_swap = dev_read_bool(dev, "rockchip,data-swap");
172 
173 	return 0;
174 }
175 
176 static void px30_lvds_enable(struct rockchip_lvds *lvds, int pipe)
177 {
178 	regmap_write(lvds->grf, PX30_GRF_PD_VO_CON1,
179 		     PX30_LVDS_SELECT(lvds->format) |
180 		     PX30_LVDS_MODE_EN(1) | PX30_LVDS_MSBSEL(1) |
181 		     PX30_LVDS_P2S_EN(1) | PX30_LVDS_VOP_SEL(pipe));
182 }
183 
184 static void px30_lvds_disable(struct rockchip_lvds *lvds)
185 {
186 	regmap_write(lvds->grf, PX30_GRF_PD_VO_CON1,
187 		     PX30_LVDS_MODE_EN(0) | PX30_LVDS_P2S_EN(0));
188 }
189 
190 static const struct rockchip_lvds_funcs px30_lvds_funcs = {
191 	.enable = px30_lvds_enable,
192 	.disable = px30_lvds_disable,
193 };
194 
195 static const struct rockchip_connector px30_lvds_driver_data = {
196 	 .funcs = &rockchip_lvds_connector_funcs,
197 	 .data = &px30_lvds_funcs,
198 };
199 
200 static void rk3126_lvds_enable(struct rockchip_lvds *lvds, int pipe)
201 {
202 	regmap_write(lvds->grf, RK3126_GRF_LVDS_CON0,
203 		     RK3126_LVDS_P2S_EN(1) | RK3126_LVDS_MODE_EN(1) |
204 		     RK3126_LVDS_MSBSEL(1) | RK3126_LVDS_SELECT(lvds->format));
205 }
206 
207 static void rk3126_lvds_disable(struct rockchip_lvds *lvds)
208 {
209 	regmap_write(lvds->grf, RK3126_GRF_LVDS_CON0,
210 		     RK3126_LVDS_P2S_EN(0) | RK3126_LVDS_MODE_EN(0));
211 }
212 
213 static const struct rockchip_lvds_funcs rk3126_lvds_funcs = {
214 	.enable = rk3126_lvds_enable,
215 	.disable = rk3126_lvds_disable,
216 };
217 
218 static const struct rockchip_connector rk3126_lvds_driver_data = {
219 	 .funcs = &rockchip_lvds_connector_funcs,
220 	 .data = &rk3126_lvds_funcs,
221 };
222 
223 static void rk3288_lvds_enable(struct rockchip_lvds *lvds, int pipe)
224 {
225 	const struct drm_display_mode *mode = lvds->mode;
226 	u32 val;
227 
228 	regmap_write(lvds->grf, RK3288_GRF_SOC_CON6,
229 		     RK3288_LVDS_LCDC_SEL(pipe));
230 
231 	val = RK3288_LVDS_PWRDWN(0) | RK3288_LVDS_CON_CLKINV(0) |
232 	      RK3288_LVDS_CON_CHASEL(lvds->dual_channel) |
233 	      RK3288_LVDS_CON_SELECT(lvds->format);
234 
235 	if (lvds->dual_channel) {
236 		u32 h_bp = mode->htotal - mode->hsync_start;
237 
238 		val |= RK3288_LVDS_CON_ENABLE_2(1) |
239 		       RK3288_LVDS_CON_ENABLE_1(1) |
240 		       RK3288_LVDS_CON_STARTSEL(lvds->data_swap);
241 
242 		if (h_bp % 2)
243 			val |= RK3288_LVDS_CON_STARTPHASE(1);
244 		else
245 			val |= RK3288_LVDS_CON_STARTPHASE(0);
246 	} else {
247 		val |= RK3288_LVDS_CON_ENABLE_2(0) |
248 		       RK3288_LVDS_CON_ENABLE_1(1);
249 	}
250 
251 	regmap_write(lvds->grf, RK3288_GRF_SOC_CON7, val);
252 
253 	rockchip_phy_set_bus_width(lvds->phy, lvds->dual_channel ? 2 : 1);
254 }
255 
256 static void rk3288_lvds_disable(struct rockchip_lvds *lvds)
257 {
258 	regmap_write(lvds->grf, RK3288_GRF_SOC_CON7, RK3288_LVDS_PWRDWN(1));
259 }
260 
261 static const struct rockchip_lvds_funcs rk3288_lvds_funcs = {
262 	.enable = rk3288_lvds_enable,
263 	.disable = rk3288_lvds_disable,
264 };
265 
266 static const struct rockchip_connector rk3288_lvds_driver_data = {
267 	 .funcs = &rockchip_lvds_connector_funcs,
268 	 .data = &rk3288_lvds_funcs,
269 };
270 
271 static void rk3368_lvds_enable(struct rockchip_lvds *lvds, int pipe)
272 {
273 	regmap_write(lvds->grf, RK3368_GRF_SOC_CON7,
274 		     RK3368_LVDS_SELECT(lvds->format) |
275 		     RK3368_LVDS_MODE_EN(1) | RK3368_LVDS_MSBSEL(1) |
276 		     RK3368_LVDS_P2S_EN(1));
277 }
278 
279 static void rk3368_lvds_disable(struct rockchip_lvds *lvds)
280 {
281 	regmap_write(lvds->grf, RK3368_GRF_SOC_CON7,
282 		     RK3368_LVDS_MODE_EN(0) | RK3368_LVDS_P2S_EN(0));
283 }
284 
285 static const struct rockchip_lvds_funcs rk3368_lvds_funcs = {
286 	.enable = rk3368_lvds_enable,
287 	.disable = rk3368_lvds_disable,
288 };
289 
290 static const struct rockchip_connector rk3368_lvds_driver_data = {
291 	 .funcs = &rockchip_lvds_connector_funcs,
292 	 .data = &rk3368_lvds_funcs,
293 };
294 
295 static const struct udevice_id rockchip_lvds_ids[] = {
296 	{
297 		.compatible = "rockchip,px30-lvds",
298 		.data = (ulong)&px30_lvds_driver_data,
299 	},
300 	{
301 		.compatible = "rockchip,rk3126-lvds",
302 		.data = (ulong)&rk3126_lvds_driver_data,
303 	},
304 	{
305 		.compatible = "rockchip,rk3288-lvds",
306 		.data = (ulong)&rk3288_lvds_driver_data,
307 	},
308 	{
309 		.compatible = "rockchip,rk3368-lvds",
310 		.data = (ulong)&rk3368_lvds_driver_data,
311 	},
312 	{}
313 };
314 
315 U_BOOT_DRIVER(rockchip_lvds) = {
316 	.name = "rockchip_lvds",
317 	.id = UCLASS_DISPLAY,
318 	.of_match = rockchip_lvds_ids,
319 	.probe = rockchip_lvds_probe,
320 	.priv_auto_alloc_size = sizeof(struct rockchip_lvds),
321 };
322