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