xref: /rk3399_rockchip-uboot/drivers/video/tegra.c (revision 46d1d5dd43e4d7c78f1fd6a5ee958d7b8a8626eb)
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * See file CREDITS for list of people who contributed to this
4  * project.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
19  * MA 02111-1307 USA
20  */
21 
22 #include <common.h>
23 #include <fdtdec.h>
24 #include <lcd.h>
25 
26 #include <asm/system.h>
27 #include <asm/gpio.h>
28 
29 #include <asm/arch/clock.h>
30 #include <asm/arch/funcmux.h>
31 #include <asm/arch/pinmux.h>
32 #include <asm/arch/pwm.h>
33 #include <asm/arch/display.h>
34 #include <asm/arch-tegra/timer.h>
35 
36 DECLARE_GLOBAL_DATA_PTR;
37 
38 /* These are the stages we go throuh in enabling the LCD */
39 enum stage_t {
40 	STAGE_START,
41 	STAGE_PANEL_VDD,
42 	STAGE_LVDS,
43 	STAGE_BACKLIGHT_VDD,
44 	STAGE_PWM,
45 	STAGE_BACKLIGHT_EN,
46 	STAGE_DONE,
47 };
48 
49 static enum stage_t stage;	/* Current stage we are at */
50 static unsigned long timer_next; /* Time we can move onto next stage */
51 
52 /* Our LCD config, set up in handle_stage() */
53 static struct fdt_panel_config config;
54 struct fdt_disp_config *disp_config;	/* Display controller config */
55 
56 enum {
57 	/* Maximum LCD size we support */
58 	LCD_MAX_WIDTH		= 1366,
59 	LCD_MAX_HEIGHT		= 768,
60 	LCD_MAX_LOG2_BPP	= 4,		/* 2^4 = 16 bpp */
61 };
62 
63 int lcd_line_length;
64 
65 void *lcd_base;			/* Start of framebuffer memory	*/
66 void *lcd_console_address;	/* Start of console buffer	*/
67 
68 short console_col;
69 short console_row;
70 
71 vidinfo_t panel_info = {
72 	/* Insert a value here so that we don't end up in the BSS */
73 	.vl_col = -1,
74 };
75 
76 char lcd_cursor_enabled;
77 
78 ushort lcd_cursor_width;
79 ushort lcd_cursor_height;
80 
81 #ifndef CONFIG_OF_CONTROL
82 #error "You must enable CONFIG_OF_CONTROL to get Tegra LCD support"
83 #endif
84 
85 void lcd_cursor_size(ushort width, ushort height)
86 {
87 	lcd_cursor_width = width;
88 	lcd_cursor_height = height;
89 }
90 
91 void lcd_toggle_cursor(void)
92 {
93 	ushort x, y;
94 	uchar *dest;
95 	ushort row;
96 
97 	x = console_col * lcd_cursor_width;
98 	y = console_row * lcd_cursor_height;
99 	dest = (uchar *)(lcd_base + y * lcd_line_length + x * (1 << LCD_BPP) /
100 			8);
101 
102 	for (row = 0; row < lcd_cursor_height; ++row, dest += lcd_line_length) {
103 		ushort *d = (ushort *)dest;
104 		ushort color;
105 		int i;
106 
107 		for (i = 0; i < lcd_cursor_width; ++i) {
108 			color = *d;
109 			color ^= lcd_getfgcolor();
110 			*d = color;
111 			++d;
112 		}
113 	}
114 }
115 
116 void lcd_cursor_on(void)
117 {
118 	lcd_cursor_enabled = 1;
119 	lcd_toggle_cursor();
120 }
121 void lcd_cursor_off(void)
122 {
123 	lcd_cursor_enabled = 0;
124 	lcd_toggle_cursor();
125 }
126 
127 char lcd_is_cursor_enabled(void)
128 {
129 	return lcd_cursor_enabled;
130 }
131 
132 static void update_panel_size(struct fdt_disp_config *config)
133 {
134 	panel_info.vl_col = config->width;
135 	panel_info.vl_row = config->height;
136 	panel_info.vl_bpix = config->log2_bpp;
137 }
138 
139 /*
140  *  Main init function called by lcd driver.
141  *  Inits and then prints test pattern if required.
142  */
143 
144 void lcd_ctrl_init(void *lcdbase)
145 {
146 	int type = DCACHE_OFF;
147 	int size;
148 
149 	assert(disp_config);
150 
151 	lcd_base = (void *)disp_config->frame_buffer;
152 
153 	/* Make sure that we can acommodate the selected LCD */
154 	assert(disp_config->width <= LCD_MAX_WIDTH);
155 	assert(disp_config->height <= LCD_MAX_HEIGHT);
156 	assert(disp_config->log2_bpp <= LCD_MAX_LOG2_BPP);
157 	if (disp_config->width <= LCD_MAX_WIDTH
158 			&& disp_config->height <= LCD_MAX_HEIGHT
159 			&& disp_config->log2_bpp <= LCD_MAX_LOG2_BPP)
160 		update_panel_size(disp_config);
161 	size = lcd_get_size(&lcd_line_length);
162 
163 	/* Set up the LCD caching as requested */
164 	if (config.cache_type & FDT_LCD_CACHE_WRITE_THROUGH)
165 		type = DCACHE_WRITETHROUGH;
166 	else if (config.cache_type & FDT_LCD_CACHE_WRITE_BACK)
167 		type = DCACHE_WRITEBACK;
168 	mmu_set_region_dcache_behaviour(disp_config->frame_buffer, size, type);
169 
170 	/* Enable flushing after LCD writes if requested */
171 	lcd_set_flush_dcache(config.cache_type & FDT_LCD_CACHE_FLUSH);
172 
173 	debug("LCD frame buffer at %p\n", lcd_base);
174 }
175 
176 ulong calc_fbsize(void)
177 {
178 	return (panel_info.vl_col * panel_info.vl_row *
179 		NBITS(panel_info.vl_bpix)) / 8;
180 }
181 
182 void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue)
183 {
184 }
185 
186 void tegra_lcd_early_init(const void *blob)
187 {
188 	/*
189 	 * Go with the maximum size for now. We will fix this up after
190 	 * relocation. These values are only used for memory alocation.
191 	 */
192 	panel_info.vl_col = LCD_MAX_WIDTH;
193 	panel_info.vl_row = LCD_MAX_HEIGHT;
194 	panel_info.vl_bpix = LCD_MAX_LOG2_BPP;
195 }
196 
197 /**
198  * Decode the panel information from the fdt.
199  *
200  * @param blob		fdt blob
201  * @param config	structure to store fdt config into
202  * @return 0 if ok, -ve on error
203  */
204 static int fdt_decode_lcd(const void *blob, struct fdt_panel_config *config)
205 {
206 	int display_node;
207 
208 	disp_config = tegra_display_get_config();
209 	if (!disp_config) {
210 		debug("%s: Display controller is not configured\n", __func__);
211 		return -1;
212 	}
213 	display_node = disp_config->panel_node;
214 	if (display_node < 0) {
215 		debug("%s: No panel configuration available\n", __func__);
216 		return -1;
217 	}
218 
219 	config->pwm_channel = pwm_request(blob, display_node, "nvidia,pwm");
220 	if (config->pwm_channel < 0) {
221 		debug("%s: Unable to request PWM channel\n", __func__);
222 		return -1;
223 	}
224 
225 	config->cache_type = fdtdec_get_int(blob, display_node,
226 					    "nvidia,cache-type",
227 					    FDT_LCD_CACHE_WRITE_BACK_FLUSH);
228 
229 	/* These GPIOs are all optional */
230 	fdtdec_decode_gpio(blob, display_node, "nvidia,backlight-enable-gpios",
231 			    &config->backlight_en);
232 	fdtdec_decode_gpio(blob, display_node, "nvidia,lvds-shutdown-gpios",
233 			   &config->lvds_shutdown);
234 	fdtdec_decode_gpio(blob, display_node, "nvidia,backlight-vdd-gpios",
235 			   &config->backlight_vdd);
236 	fdtdec_decode_gpio(blob, display_node, "nvidia,panel-vdd-gpios",
237 			   &config->panel_vdd);
238 
239 	return fdtdec_get_int_array(blob, display_node, "nvidia,panel-timings",
240 			config->panel_timings, FDT_LCD_TIMINGS);
241 }
242 
243 /**
244  * Handle the next stage of device init
245  */
246 static int handle_stage(const void *blob)
247 {
248 	debug("%s: stage %d\n", __func__, stage);
249 
250 	/* do the things for this stage */
251 	switch (stage) {
252 	case STAGE_START:
253 		/* Initialize the Tegra display controller */
254 		if (tegra_display_probe(gd->fdt_blob, (void *)gd->fb_base)) {
255 			printf("%s: Failed to probe display driver\n",
256 			__func__);
257 			return -1;
258 		}
259 
260 		/* get panel details */
261 		if (fdt_decode_lcd(blob, &config)) {
262 			printf("No valid LCD information in device tree\n");
263 			return -1;
264 		}
265 
266 		/*
267 		 * It is possible that the FDT has requested that the LCD be
268 		 * disabled. We currently don't support this. It would require
269 		 * changes to U-Boot LCD subsystem to have LCD support
270 		 * compiled in but not used. An easier option might be to
271 		 * still have a frame buffer, but leave the backlight off and
272 		 * remove all mention of lcd in the stdout environment
273 		 * variable.
274 		 */
275 
276 		funcmux_select(PERIPH_ID_DISP1, FUNCMUX_DEFAULT);
277 
278 		fdtdec_setup_gpio(&config.panel_vdd);
279 		fdtdec_setup_gpio(&config.lvds_shutdown);
280 		fdtdec_setup_gpio(&config.backlight_vdd);
281 		fdtdec_setup_gpio(&config.backlight_en);
282 
283 		/*
284 		 * TODO: If fdt includes output flag we can omit this code
285 		 * since fdtdec_setup_gpio will do it for us.
286 		 */
287 		if (fdt_gpio_isvalid(&config.panel_vdd))
288 			gpio_direction_output(config.panel_vdd.gpio, 0);
289 		if (fdt_gpio_isvalid(&config.lvds_shutdown))
290 			gpio_direction_output(config.lvds_shutdown.gpio, 0);
291 		if (fdt_gpio_isvalid(&config.backlight_vdd))
292 			gpio_direction_output(config.backlight_vdd.gpio, 0);
293 		if (fdt_gpio_isvalid(&config.backlight_en))
294 			gpio_direction_output(config.backlight_en.gpio, 0);
295 		break;
296 	case STAGE_PANEL_VDD:
297 		if (fdt_gpio_isvalid(&config.panel_vdd))
298 			gpio_direction_output(config.panel_vdd.gpio, 1);
299 		break;
300 	case STAGE_LVDS:
301 		if (fdt_gpio_isvalid(&config.lvds_shutdown))
302 			gpio_set_value(config.lvds_shutdown.gpio, 1);
303 		break;
304 	case STAGE_BACKLIGHT_VDD:
305 		if (fdt_gpio_isvalid(&config.backlight_vdd))
306 			gpio_set_value(config.backlight_vdd.gpio, 1);
307 		break;
308 	case STAGE_PWM:
309 		/* Enable PWM at 15/16 high, 32768 Hz with divider 1 */
310 		pinmux_set_func(PINGRP_GPU, PMUX_FUNC_PWM);
311 		pinmux_tristate_disable(PINGRP_GPU);
312 
313 		pwm_enable(config.pwm_channel, 32768, 0xdf, 1);
314 		break;
315 	case STAGE_BACKLIGHT_EN:
316 		if (fdt_gpio_isvalid(&config.backlight_en))
317 			gpio_set_value(config.backlight_en.gpio, 1);
318 		break;
319 	case STAGE_DONE:
320 		break;
321 	}
322 
323 	/* set up timer for next stage */
324 	timer_next = timer_get_us();
325 	if (stage < FDT_LCD_TIMINGS)
326 		timer_next += config.panel_timings[stage] * 1000;
327 
328 	/* move to next stage */
329 	stage++;
330 	return 0;
331 }
332 
333 int tegra_lcd_check_next_stage(const void *blob, int wait)
334 {
335 	if (stage == STAGE_DONE)
336 		return 0;
337 
338 	do {
339 		/* wait if we need to */
340 		debug("%s: stage %d\n", __func__, stage);
341 		if (stage != STAGE_START) {
342 			int delay = timer_next - timer_get_us();
343 
344 			if (delay > 0) {
345 				if (wait)
346 					udelay(delay);
347 				else
348 					return 0;
349 			}
350 		}
351 
352 		if (handle_stage(blob))
353 			return -1;
354 	} while (wait && stage != STAGE_DONE);
355 	if (stage == STAGE_DONE)
356 		debug("%s: LCD init complete\n", __func__);
357 
358 	return 0;
359 }
360 
361 void lcd_enable(void)
362 {
363 	/*
364 	 * Backlight and power init will be done separately in
365 	 * tegra_lcd_check_next_stage(), which should be called in
366 	 * board_late_init().
367 	 *
368 	 * U-Boot code supports only colour depth, selected at compile time.
369 	 * The device tree setting should match this. Otherwise the display
370 	 * will not look right, and U-Boot may crash.
371 	 */
372 	if (disp_config->log2_bpp != LCD_BPP) {
373 		printf("%s: Error: LCD depth configured in FDT (%d = %dbpp)"
374 			" must match setting of LCD_BPP (%d)\n", __func__,
375 		       disp_config->log2_bpp, disp_config->bpp, LCD_BPP);
376 	}
377 }
378