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