blob: ae9edfdae630677956661694d2f92b9f093b2f44 [file] [log] [blame]
Simon Glass0be8f202012-10-17 13:24:51 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors.
Wolfgang Denk1a459662013-07-08 09:37:19 +02003 * SPDX-License-Identifier: GPL-2.0+
Simon Glass0be8f202012-10-17 13:24:51 +00004 */
Simon Glassf20b2c02016-01-30 16:37:57 -07005
Simon Glass0be8f202012-10-17 13:24:51 +00006#include <common.h>
Simon Glass9e6866d2016-01-30 16:37:56 -07007#include <dm.h>
Simon Glass0be8f202012-10-17 13:24:51 +00008#include <fdtdec.h>
Simon Glass9e6866d2016-01-30 16:37:56 -07009#include <video.h>
Simon Glass0be8f202012-10-17 13:24:51 +000010#include <asm/system.h>
11#include <asm/gpio.h>
Simon Glass71cafc32016-01-30 16:37:53 -070012#include <asm/io.h>
Simon Glass0be8f202012-10-17 13:24:51 +000013
14#include <asm/arch/clock.h>
15#include <asm/arch/funcmux.h>
16#include <asm/arch/pinmux.h>
17#include <asm/arch/pwm.h>
18#include <asm/arch/display.h>
19#include <asm/arch-tegra/timer.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23/* These are the stages we go throuh in enabling the LCD */
24enum stage_t {
25 STAGE_START,
26 STAGE_PANEL_VDD,
27 STAGE_LVDS,
28 STAGE_BACKLIGHT_VDD,
29 STAGE_PWM,
30 STAGE_BACKLIGHT_EN,
31 STAGE_DONE,
32};
33
Simon Glassbfda0372016-01-30 16:37:58 -070034#define FDT_LCD_TIMINGS 4
35
36enum {
37 FDT_LCD_TIMING_REF_TO_SYNC,
38 FDT_LCD_TIMING_SYNC_WIDTH,
39 FDT_LCD_TIMING_BACK_PORCH,
40 FDT_LCD_TIMING_FRONT_PORCH,
41
42 FDT_LCD_TIMING_COUNT,
43};
44
45enum lcd_cache_t {
46 FDT_LCD_CACHE_OFF = 0,
47 FDT_LCD_CACHE_WRITE_THROUGH = 1 << 0,
48 FDT_LCD_CACHE_WRITE_BACK = 1 << 1,
49 FDT_LCD_CACHE_FLUSH = 1 << 2,
50 FDT_LCD_CACHE_WRITE_BACK_FLUSH = FDT_LCD_CACHE_WRITE_BACK |
51 FDT_LCD_CACHE_FLUSH,
52};
53
Simon Glassce0c4742016-01-30 16:37:55 -070054/* Information about the display controller */
55struct tegra_lcd_priv {
Simon Glassf20b2c02016-01-30 16:37:57 -070056 enum stage_t stage; /* Current stage we are at */
57 unsigned long timer_next; /* Time we can move onto next stage */
Simon Glassce0c4742016-01-30 16:37:55 -070058 int width; /* width in pixels */
59 int height; /* height in pixels */
60 int bpp; /* number of bits per pixel */
61
62 /*
63 * log2 of number of bpp, in general, unless it bpp is 24 in which
64 * case this field holds 24 also! This is a U-Boot thing.
65 */
66 int log2_bpp;
67 struct disp_ctlr *disp; /* Display controller to use */
68 fdt_addr_t frame_buffer; /* Address of frame buffer */
69 unsigned pixel_clock; /* Pixel clock in Hz */
70 uint horiz_timing[FDT_LCD_TIMING_COUNT]; /* Horizontal timing */
71 uint vert_timing[FDT_LCD_TIMING_COUNT]; /* Vertical timing */
72 int panel_node; /* node offset of panel information */
73 int pwm_channel; /* PWM channel to use for backlight */
74 enum lcd_cache_t cache_type;
75
76 struct gpio_desc backlight_en; /* GPIO for backlight enable */
77 struct gpio_desc lvds_shutdown; /* GPIO for lvds shutdown */
78 struct gpio_desc backlight_vdd; /* GPIO for backlight vdd */
79 struct gpio_desc panel_vdd; /* GPIO for panel vdd */
80 /*
81 * Panel required timings
82 * Timing 1: delay between panel_vdd-rise and data-rise
83 * Timing 2: delay between data-rise and backlight_vdd-rise
84 * Timing 3: delay between backlight_vdd and pwm-rise
85 * Timing 4: delay between pwm-rise and backlight_en-rise
86 */
87 uint panel_timings[FDT_LCD_TIMINGS];
88};
89
Simon Glass0be8f202012-10-17 13:24:51 +000090enum {
91 /* Maximum LCD size we support */
92 LCD_MAX_WIDTH = 1366,
93 LCD_MAX_HEIGHT = 768,
Simon Glass9e6866d2016-01-30 16:37:56 -070094 LCD_MAX_LOG2_BPP = VIDEO_BPP16,
Simon Glass0be8f202012-10-17 13:24:51 +000095};
96
Simon Glass71cafc32016-01-30 16:37:53 -070097static void update_window(struct dc_ctlr *dc, struct disp_ctl_win *win)
98{
99 unsigned h_dda, v_dda;
100 unsigned long val;
101
102 val = readl(&dc->cmd.disp_win_header);
103 val |= WINDOW_A_SELECT;
104 writel(val, &dc->cmd.disp_win_header);
105
106 writel(win->fmt, &dc->win.color_depth);
107
108 clrsetbits_le32(&dc->win.byte_swap, BYTE_SWAP_MASK,
109 BYTE_SWAP_NOSWAP << BYTE_SWAP_SHIFT);
110
111 val = win->out_x << H_POSITION_SHIFT;
112 val |= win->out_y << V_POSITION_SHIFT;
113 writel(val, &dc->win.pos);
114
115 val = win->out_w << H_SIZE_SHIFT;
116 val |= win->out_h << V_SIZE_SHIFT;
117 writel(val, &dc->win.size);
118
119 val = (win->w * win->bpp / 8) << H_PRESCALED_SIZE_SHIFT;
120 val |= win->h << V_PRESCALED_SIZE_SHIFT;
121 writel(val, &dc->win.prescaled_size);
122
123 writel(0, &dc->win.h_initial_dda);
124 writel(0, &dc->win.v_initial_dda);
125
126 h_dda = (win->w * 0x1000) / max(win->out_w - 1, 1U);
127 v_dda = (win->h * 0x1000) / max(win->out_h - 1, 1U);
128
129 val = h_dda << H_DDA_INC_SHIFT;
130 val |= v_dda << V_DDA_INC_SHIFT;
131 writel(val, &dc->win.dda_increment);
132
133 writel(win->stride, &dc->win.line_stride);
134 writel(0, &dc->win.buf_stride);
135
136 val = WIN_ENABLE;
137 if (win->bpp < 24)
138 val |= COLOR_EXPAND;
139 writel(val, &dc->win.win_opt);
140
141 writel((unsigned long)win->phys_addr, &dc->winbuf.start_addr);
142 writel(win->x, &dc->winbuf.addr_h_offset);
143 writel(win->y, &dc->winbuf.addr_v_offset);
144
145 writel(0xff00, &dc->win.blend_nokey);
146 writel(0xff00, &dc->win.blend_1win);
147
148 val = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
149 val |= GENERAL_UPDATE | WIN_A_UPDATE;
150 writel(val, &dc->cmd.state_ctrl);
151}
152
Simon Glass9e6866d2016-01-30 16:37:56 -0700153static void write_pair(struct tegra_lcd_priv *priv, int item, u32 *reg)
Simon Glass71cafc32016-01-30 16:37:53 -0700154{
Simon Glass9e6866d2016-01-30 16:37:56 -0700155 writel(priv->horiz_timing[item] |
156 (priv->vert_timing[item] << 16), reg);
Simon Glass71cafc32016-01-30 16:37:53 -0700157}
158
159static int update_display_mode(struct dc_disp_reg *disp,
Simon Glass9e6866d2016-01-30 16:37:56 -0700160 struct tegra_lcd_priv *priv)
Simon Glass71cafc32016-01-30 16:37:53 -0700161{
162 unsigned long val;
163 unsigned long rate;
164 unsigned long div;
165
166 writel(0x0, &disp->disp_timing_opt);
Simon Glass9e6866d2016-01-30 16:37:56 -0700167 write_pair(priv, FDT_LCD_TIMING_REF_TO_SYNC, &disp->ref_to_sync);
168 write_pair(priv, FDT_LCD_TIMING_SYNC_WIDTH, &disp->sync_width);
169 write_pair(priv, FDT_LCD_TIMING_BACK_PORCH, &disp->back_porch);
170 write_pair(priv, FDT_LCD_TIMING_FRONT_PORCH, &disp->front_porch);
Simon Glass71cafc32016-01-30 16:37:53 -0700171
Simon Glass9e6866d2016-01-30 16:37:56 -0700172 writel(priv->width | (priv->height << 16), &disp->disp_active);
Simon Glass71cafc32016-01-30 16:37:53 -0700173
174 val = DE_SELECT_ACTIVE << DE_SELECT_SHIFT;
175 val |= DE_CONTROL_NORMAL << DE_CONTROL_SHIFT;
176 writel(val, &disp->data_enable_opt);
177
178 val = DATA_FORMAT_DF1P1C << DATA_FORMAT_SHIFT;
179 val |= DATA_ALIGNMENT_MSB << DATA_ALIGNMENT_SHIFT;
180 val |= DATA_ORDER_RED_BLUE << DATA_ORDER_SHIFT;
181 writel(val, &disp->disp_interface_ctrl);
182
183 /*
184 * The pixel clock divider is in 7.1 format (where the bottom bit
185 * represents 0.5). Here we calculate the divider needed to get from
186 * the display clock (typically 600MHz) to the pixel clock. We round
187 * up or down as requried.
188 */
189 rate = clock_get_periph_rate(PERIPH_ID_DISP1, CLOCK_ID_CGENERAL);
Simon Glass9e6866d2016-01-30 16:37:56 -0700190 div = ((rate * 2 + priv->pixel_clock / 2) / priv->pixel_clock) - 2;
Simon Glass71cafc32016-01-30 16:37:53 -0700191 debug("Display clock %lu, divider %lu\n", rate, div);
192
193 writel(0x00010001, &disp->shift_clk_opt);
194
195 val = PIXEL_CLK_DIVIDER_PCD1 << PIXEL_CLK_DIVIDER_SHIFT;
196 val |= div << SHIFT_CLK_DIVIDER_SHIFT;
197 writel(val, &disp->disp_clk_ctrl);
198
199 return 0;
200}
201
202/* Start up the display and turn on power to PWMs */
203static void basic_init(struct dc_cmd_reg *cmd)
204{
205 u32 val;
206
207 writel(0x00000100, &cmd->gen_incr_syncpt_ctrl);
208 writel(0x0000011a, &cmd->cont_syncpt_vsync);
209 writel(0x00000000, &cmd->int_type);
210 writel(0x00000000, &cmd->int_polarity);
211 writel(0x00000000, &cmd->int_mask);
212 writel(0x00000000, &cmd->int_enb);
213
214 val = PW0_ENABLE | PW1_ENABLE | PW2_ENABLE;
215 val |= PW3_ENABLE | PW4_ENABLE | PM0_ENABLE;
216 val |= PM1_ENABLE;
217 writel(val, &cmd->disp_pow_ctrl);
218
219 val = readl(&cmd->disp_cmd);
220 val |= CTRL_MODE_C_DISPLAY << CTRL_MODE_SHIFT;
221 writel(val, &cmd->disp_cmd);
222}
223
224static void basic_init_timer(struct dc_disp_reg *disp)
225{
226 writel(0x00000020, &disp->mem_high_pri);
227 writel(0x00000001, &disp->mem_high_pri_timer);
228}
229
230static const u32 rgb_enb_tab[PIN_REG_COUNT] = {
231 0x00000000,
232 0x00000000,
233 0x00000000,
234 0x00000000,
235};
236
237static const u32 rgb_polarity_tab[PIN_REG_COUNT] = {
238 0x00000000,
239 0x01000000,
240 0x00000000,
241 0x00000000,
242};
243
244static const u32 rgb_data_tab[PIN_REG_COUNT] = {
245 0x00000000,
246 0x00000000,
247 0x00000000,
248 0x00000000,
249};
250
251static const u32 rgb_sel_tab[PIN_OUTPUT_SEL_COUNT] = {
252 0x00000000,
253 0x00000000,
254 0x00000000,
255 0x00000000,
256 0x00210222,
257 0x00002200,
258 0x00020000,
259};
260
261static void rgb_enable(struct dc_com_reg *com)
262{
263 int i;
264
265 for (i = 0; i < PIN_REG_COUNT; i++) {
266 writel(rgb_enb_tab[i], &com->pin_output_enb[i]);
267 writel(rgb_polarity_tab[i], &com->pin_output_polarity[i]);
268 writel(rgb_data_tab[i], &com->pin_output_data[i]);
269 }
270
271 for (i = 0; i < PIN_OUTPUT_SEL_COUNT; i++)
272 writel(rgb_sel_tab[i], &com->pin_output_sel[i]);
273}
274
275static int setup_window(struct disp_ctl_win *win,
Simon Glass9e6866d2016-01-30 16:37:56 -0700276 struct tegra_lcd_priv *priv)
Simon Glass71cafc32016-01-30 16:37:53 -0700277{
278 win->x = 0;
279 win->y = 0;
Simon Glass9e6866d2016-01-30 16:37:56 -0700280 win->w = priv->width;
281 win->h = priv->height;
Simon Glass71cafc32016-01-30 16:37:53 -0700282 win->out_x = 0;
283 win->out_y = 0;
Simon Glass9e6866d2016-01-30 16:37:56 -0700284 win->out_w = priv->width;
285 win->out_h = priv->height;
286 win->phys_addr = priv->frame_buffer;
287 win->stride = priv->width * (1 << priv->log2_bpp) / 8;
288 debug("%s: depth = %d\n", __func__, priv->log2_bpp);
289 switch (priv->log2_bpp) {
Simon Glass71cafc32016-01-30 16:37:53 -0700290 case 5:
291 case 24:
292 win->fmt = COLOR_DEPTH_R8G8B8A8;
293 win->bpp = 32;
294 break;
295 case 4:
296 win->fmt = COLOR_DEPTH_B5G6R5;
297 win->bpp = 16;
298 break;
299
300 default:
301 debug("Unsupported LCD bit depth");
302 return -1;
303 }
304
305 return 0;
306}
307
Simon Glass71cafc32016-01-30 16:37:53 -0700308static void debug_timing(const char *name, unsigned int timing[])
309{
310#ifdef DEBUG
311 int i;
312
313 debug("%s timing: ", name);
314 for (i = 0; i < FDT_LCD_TIMING_COUNT; i++)
315 debug("%d ", timing[i]);
316 debug("\n");
317#endif
318}
319
320/**
321 * Decode panel information from the fdt, according to a standard binding
322 *
323 * @param blob fdt blob
324 * @param node offset of fdt node to read from
Simon Glass9e6866d2016-01-30 16:37:56 -0700325 * @param priv structure to store fdt config into
Simon Glass71cafc32016-01-30 16:37:53 -0700326 * @return 0 if ok, -ve on error
327 */
328static int tegra_decode_panel(const void *blob, int node,
Simon Glass9e6866d2016-01-30 16:37:56 -0700329 struct tegra_lcd_priv *priv)
Simon Glass71cafc32016-01-30 16:37:53 -0700330{
331 int front, back, ref;
332
Simon Glass9e6866d2016-01-30 16:37:56 -0700333 priv->width = fdtdec_get_int(blob, node, "xres", -1);
334 priv->height = fdtdec_get_int(blob, node, "yres", -1);
335 priv->pixel_clock = fdtdec_get_int(blob, node, "clock", 0);
336 if (!priv->pixel_clock || priv->width == -1 || priv->height == -1) {
Simon Glass71cafc32016-01-30 16:37:53 -0700337 debug("%s: Pixel parameters missing\n", __func__);
338 return -FDT_ERR_NOTFOUND;
339 }
340
341 back = fdtdec_get_int(blob, node, "left-margin", -1);
342 front = fdtdec_get_int(blob, node, "right-margin", -1);
343 ref = fdtdec_get_int(blob, node, "hsync-len", -1);
344 if ((back | front | ref) == -1) {
345 debug("%s: Horizontal parameters missing\n", __func__);
346 return -FDT_ERR_NOTFOUND;
347 }
348
349 /* Use a ref-to-sync of 1 always, and take this from the front porch */
Simon Glass9e6866d2016-01-30 16:37:56 -0700350 priv->horiz_timing[FDT_LCD_TIMING_REF_TO_SYNC] = 1;
351 priv->horiz_timing[FDT_LCD_TIMING_SYNC_WIDTH] = ref;
352 priv->horiz_timing[FDT_LCD_TIMING_BACK_PORCH] = back;
353 priv->horiz_timing[FDT_LCD_TIMING_FRONT_PORCH] = front -
354 priv->horiz_timing[FDT_LCD_TIMING_REF_TO_SYNC];
355 debug_timing("horiz", priv->horiz_timing);
Simon Glass71cafc32016-01-30 16:37:53 -0700356
357 back = fdtdec_get_int(blob, node, "upper-margin", -1);
358 front = fdtdec_get_int(blob, node, "lower-margin", -1);
359 ref = fdtdec_get_int(blob, node, "vsync-len", -1);
360 if ((back | front | ref) == -1) {
361 debug("%s: Vertical parameters missing\n", __func__);
362 return -FDT_ERR_NOTFOUND;
363 }
364
Simon Glass9e6866d2016-01-30 16:37:56 -0700365 priv->vert_timing[FDT_LCD_TIMING_REF_TO_SYNC] = 1;
366 priv->vert_timing[FDT_LCD_TIMING_SYNC_WIDTH] = ref;
367 priv->vert_timing[FDT_LCD_TIMING_BACK_PORCH] = back;
368 priv->vert_timing[FDT_LCD_TIMING_FRONT_PORCH] = front -
369 priv->vert_timing[FDT_LCD_TIMING_REF_TO_SYNC];
370 debug_timing("vert", priv->vert_timing);
Simon Glass71cafc32016-01-30 16:37:53 -0700371
372 return 0;
373}
374
375/**
376 * Decode the display controller information from the fdt.
377 *
378 * @param blob fdt blob
Simon Glass9e6866d2016-01-30 16:37:56 -0700379 * @param priv structure to store fdt priv into
Simon Glass71cafc32016-01-30 16:37:53 -0700380 * @return 0 if ok, -ve on error
381 */
Simon Glass9e6866d2016-01-30 16:37:56 -0700382static int tegra_display_decode_config(const void *blob, int node,
383 struct tegra_lcd_priv *priv)
Simon Glass71cafc32016-01-30 16:37:53 -0700384{
Simon Glass9e6866d2016-01-30 16:37:56 -0700385 int rgb;
Simon Glass71cafc32016-01-30 16:37:53 -0700386 int bpp, bit;
387
Simon Glass9e6866d2016-01-30 16:37:56 -0700388 priv->disp = (struct disp_ctlr *)fdtdec_get_addr(blob, node, "reg");
389 if (!priv->disp) {
Simon Glass71cafc32016-01-30 16:37:53 -0700390 debug("%s: No display controller address\n", __func__);
391 return -1;
392 }
393
394 rgb = fdt_subnode_offset(blob, node, "rgb");
395
Simon Glass9e6866d2016-01-30 16:37:56 -0700396 priv->panel_node = fdtdec_lookup_phandle(blob, rgb, "nvidia,panel");
397 if (priv->panel_node < 0) {
Simon Glass71cafc32016-01-30 16:37:53 -0700398 debug("%s: Cannot find panel information\n", __func__);
399 return -1;
400 }
401
Simon Glass9e6866d2016-01-30 16:37:56 -0700402 if (tegra_decode_panel(blob, priv->panel_node, priv)) {
Simon Glass71cafc32016-01-30 16:37:53 -0700403 debug("%s: Failed to decode panel information\n", __func__);
404 return -1;
405 }
406
Simon Glass9e6866d2016-01-30 16:37:56 -0700407 bpp = fdtdec_get_int(blob, priv->panel_node, "nvidia,bits-per-pixel",
Simon Glass71cafc32016-01-30 16:37:53 -0700408 -1);
409 bit = ffs(bpp) - 1;
410 if (bpp == (1 << bit))
Simon Glass9e6866d2016-01-30 16:37:56 -0700411 priv->log2_bpp = bit;
Simon Glass71cafc32016-01-30 16:37:53 -0700412 else
Simon Glass9e6866d2016-01-30 16:37:56 -0700413 priv->log2_bpp = bpp;
Simon Glass71cafc32016-01-30 16:37:53 -0700414 if (bpp == -1) {
415 debug("%s: Pixel bpp parameters missing\n", __func__);
416 return -FDT_ERR_NOTFOUND;
417 }
Simon Glass9e6866d2016-01-30 16:37:56 -0700418 priv->bpp = bpp;
Simon Glass71cafc32016-01-30 16:37:53 -0700419
420 return 0;
421}
422
423/**
424 * Register a new display based on device tree configuration.
425 *
426 * The frame buffer can be positioned by U-Boot or overriden by the fdt.
427 * You should pass in the U-Boot address here, and check the contents of
Simon Glassce0c4742016-01-30 16:37:55 -0700428 * struct tegra_lcd_priv to see what was actually chosen.
Simon Glass71cafc32016-01-30 16:37:53 -0700429 *
430 * @param blob Device tree blob
Simon Glass9e6866d2016-01-30 16:37:56 -0700431 * @param priv Driver's private data
Simon Glass71cafc32016-01-30 16:37:53 -0700432 * @param default_lcd_base Default address of LCD frame buffer
433 * @return 0 if ok, -1 on error (unsupported bits per pixel)
434 */
Simon Glass9e6866d2016-01-30 16:37:56 -0700435static int tegra_display_probe(const void *blob, struct tegra_lcd_priv *priv,
436 void *default_lcd_base)
Simon Glass71cafc32016-01-30 16:37:53 -0700437{
438 struct disp_ctl_win window;
439 struct dc_ctlr *dc;
440
Simon Glass9e6866d2016-01-30 16:37:56 -0700441 priv->frame_buffer = (u32)default_lcd_base;
Simon Glass71cafc32016-01-30 16:37:53 -0700442
Simon Glass9e6866d2016-01-30 16:37:56 -0700443 dc = (struct dc_ctlr *)priv->disp;
Simon Glass71cafc32016-01-30 16:37:53 -0700444
445 /*
446 * A header file for clock constants was NAKed upstream.
447 * TODO: Put this into the FDT and fdt_lcd struct when we have clock
448 * support there
449 */
450 clock_start_periph_pll(PERIPH_ID_HOST1X, CLOCK_ID_PERIPH,
451 144 * 1000000);
452 clock_start_periph_pll(PERIPH_ID_DISP1, CLOCK_ID_CGENERAL,
453 600 * 1000000);
454 basic_init(&dc->cmd);
455 basic_init_timer(&dc->disp);
456 rgb_enable(&dc->com);
457
Simon Glass9e6866d2016-01-30 16:37:56 -0700458 if (priv->pixel_clock)
459 update_display_mode(&dc->disp, priv);
Simon Glass71cafc32016-01-30 16:37:53 -0700460
Simon Glass9e6866d2016-01-30 16:37:56 -0700461 if (setup_window(&window, priv))
Simon Glass71cafc32016-01-30 16:37:53 -0700462 return -1;
463
464 update_window(dc, &window);
465
466 return 0;
467}
468
Simon Glass0be8f202012-10-17 13:24:51 +0000469/**
470 * Decode the panel information from the fdt.
471 *
472 * @param blob fdt blob
Simon Glass9e6866d2016-01-30 16:37:56 -0700473 * @param priv structure to store fdt config into
Simon Glass0be8f202012-10-17 13:24:51 +0000474 * @return 0 if ok, -ve on error
475 */
Simon Glass9e6866d2016-01-30 16:37:56 -0700476static int fdt_decode_lcd(const void *blob, struct tegra_lcd_priv *priv)
Simon Glass0be8f202012-10-17 13:24:51 +0000477{
478 int display_node;
479
Simon Glass9e6866d2016-01-30 16:37:56 -0700480 display_node = priv->panel_node;
Simon Glass0be8f202012-10-17 13:24:51 +0000481 if (display_node < 0) {
482 debug("%s: No panel configuration available\n", __func__);
483 return -1;
484 }
485
Simon Glass9e6866d2016-01-30 16:37:56 -0700486 priv->pwm_channel = pwm_request(blob, display_node, "nvidia,pwm");
487 if (priv->pwm_channel < 0) {
Simon Glass0be8f202012-10-17 13:24:51 +0000488 debug("%s: Unable to request PWM channel\n", __func__);
489 return -1;
490 }
491
Simon Glass9e6866d2016-01-30 16:37:56 -0700492 priv->cache_type = fdtdec_get_int(blob, display_node,
Simon Glass0be8f202012-10-17 13:24:51 +0000493 "nvidia,cache-type",
494 FDT_LCD_CACHE_WRITE_BACK_FLUSH);
495
496 /* These GPIOs are all optional */
Simon Glass04072cb2015-01-05 20:05:35 -0700497 gpio_request_by_name_nodev(blob, display_node,
498 "nvidia,backlight-enable-gpios", 0,
Simon Glass9e6866d2016-01-30 16:37:56 -0700499 &priv->backlight_en, GPIOD_IS_OUT);
Simon Glass04072cb2015-01-05 20:05:35 -0700500 gpio_request_by_name_nodev(blob, display_node,
501 "nvidia,lvds-shutdown-gpios", 0,
Simon Glass9e6866d2016-01-30 16:37:56 -0700502 &priv->lvds_shutdown, GPIOD_IS_OUT);
Simon Glass04072cb2015-01-05 20:05:35 -0700503 gpio_request_by_name_nodev(blob, display_node,
504 "nvidia,backlight-vdd-gpios", 0,
Simon Glass9e6866d2016-01-30 16:37:56 -0700505 &priv->backlight_vdd, GPIOD_IS_OUT);
Simon Glass04072cb2015-01-05 20:05:35 -0700506 gpio_request_by_name_nodev(blob, display_node,
507 "nvidia,panel-vdd-gpios", 0,
Simon Glass9e6866d2016-01-30 16:37:56 -0700508 &priv->panel_vdd, GPIOD_IS_OUT);
Simon Glass0be8f202012-10-17 13:24:51 +0000509
510 return fdtdec_get_int_array(blob, display_node, "nvidia,panel-timings",
Simon Glass9e6866d2016-01-30 16:37:56 -0700511 priv->panel_timings, FDT_LCD_TIMINGS);
Simon Glass0be8f202012-10-17 13:24:51 +0000512}
513
514/**
515 * Handle the next stage of device init
516 */
Simon Glass9e6866d2016-01-30 16:37:56 -0700517static int handle_stage(const void *blob, struct tegra_lcd_priv *priv)
Simon Glass0be8f202012-10-17 13:24:51 +0000518{
Simon Glassf20b2c02016-01-30 16:37:57 -0700519 debug("%s: stage %d\n", __func__, priv->stage);
Simon Glass0be8f202012-10-17 13:24:51 +0000520
521 /* do the things for this stage */
Simon Glassf20b2c02016-01-30 16:37:57 -0700522 switch (priv->stage) {
Simon Glass0be8f202012-10-17 13:24:51 +0000523 case STAGE_START:
Simon Glass0be8f202012-10-17 13:24:51 +0000524 /*
525 * It is possible that the FDT has requested that the LCD be
526 * disabled. We currently don't support this. It would require
527 * changes to U-Boot LCD subsystem to have LCD support
528 * compiled in but not used. An easier option might be to
529 * still have a frame buffer, but leave the backlight off and
530 * remove all mention of lcd in the stdout environment
531 * variable.
532 */
533
534 funcmux_select(PERIPH_ID_DISP1, FUNCMUX_DEFAULT);
Simon Glass0be8f202012-10-17 13:24:51 +0000535 break;
536 case STAGE_PANEL_VDD:
Simon Glass9e6866d2016-01-30 16:37:56 -0700537 if (dm_gpio_is_valid(&priv->panel_vdd))
538 dm_gpio_set_value(&priv->panel_vdd, 1);
Simon Glass0be8f202012-10-17 13:24:51 +0000539 break;
540 case STAGE_LVDS:
Simon Glass9e6866d2016-01-30 16:37:56 -0700541 if (dm_gpio_is_valid(&priv->lvds_shutdown))
542 dm_gpio_set_value(&priv->lvds_shutdown, 1);
Simon Glass0be8f202012-10-17 13:24:51 +0000543 break;
544 case STAGE_BACKLIGHT_VDD:
Simon Glass9e6866d2016-01-30 16:37:56 -0700545 if (dm_gpio_is_valid(&priv->backlight_vdd))
546 dm_gpio_set_value(&priv->backlight_vdd, 1);
Simon Glass0be8f202012-10-17 13:24:51 +0000547 break;
548 case STAGE_PWM:
549 /* Enable PWM at 15/16 high, 32768 Hz with divider 1 */
Stephen Warren70ad3752014-03-21 12:28:58 -0600550 pinmux_set_func(PMUX_PINGRP_GPU, PMUX_FUNC_PWM);
551 pinmux_tristate_disable(PMUX_PINGRP_GPU);
Simon Glass0be8f202012-10-17 13:24:51 +0000552
Simon Glass9e6866d2016-01-30 16:37:56 -0700553 pwm_enable(priv->pwm_channel, 32768, 0xdf, 1);
Simon Glass0be8f202012-10-17 13:24:51 +0000554 break;
555 case STAGE_BACKLIGHT_EN:
Simon Glass9e6866d2016-01-30 16:37:56 -0700556 if (dm_gpio_is_valid(&priv->backlight_en))
557 dm_gpio_set_value(&priv->backlight_en, 1);
Simon Glass0be8f202012-10-17 13:24:51 +0000558 break;
559 case STAGE_DONE:
560 break;
561 }
562
563 /* set up timer for next stage */
Simon Glassf20b2c02016-01-30 16:37:57 -0700564 priv->timer_next = timer_get_us();
565 if (priv->stage < FDT_LCD_TIMINGS)
566 priv->timer_next += priv->panel_timings[priv->stage] * 1000;
Simon Glass0be8f202012-10-17 13:24:51 +0000567
568 /* move to next stage */
Simon Glassf20b2c02016-01-30 16:37:57 -0700569 priv->stage++;
Simon Glass0be8f202012-10-17 13:24:51 +0000570 return 0;
571}
572
Simon Glass9e6866d2016-01-30 16:37:56 -0700573/**
574 * Perform the next stage of the LCD init if it is time to do so.
575 *
576 * LCD init can be time-consuming because of the number of delays we need
577 * while waiting for the backlight power supply, etc. This function can
578 * be called at various times during U-Boot operation to advance the
579 * initialization of the LCD to the next stage if sufficient time has
580 * passed since the last stage. It keeps track of what stage it is up to
581 * and the time that it is permitted to move to the next stage.
582 *
583 * The final call should have wait=1 to complete the init.
584 *
585 * @param blob fdt blob containing LCD information
586 * @param wait 1 to wait until all init is complete, and then return
587 * 0 to return immediately, potentially doing nothing if it is
588 * not yet time for the next init.
589 */
590static int tegra_lcd_check_next_stage(const void *blob,
591 struct tegra_lcd_priv *priv, int wait)
Simon Glass0be8f202012-10-17 13:24:51 +0000592{
Simon Glassf20b2c02016-01-30 16:37:57 -0700593 if (priv->stage == STAGE_DONE)
Simon Glass0be8f202012-10-17 13:24:51 +0000594 return 0;
595
596 do {
597 /* wait if we need to */
Simon Glassf20b2c02016-01-30 16:37:57 -0700598 debug("%s: stage %d\n", __func__, priv->stage);
599 if (priv->stage != STAGE_START) {
600 int delay = priv->timer_next - timer_get_us();
Simon Glass0be8f202012-10-17 13:24:51 +0000601
602 if (delay > 0) {
603 if (wait)
604 udelay(delay);
605 else
606 return 0;
607 }
608 }
609
Simon Glass9e6866d2016-01-30 16:37:56 -0700610 if (handle_stage(blob, priv))
Simon Glass0be8f202012-10-17 13:24:51 +0000611 return -1;
Simon Glassf20b2c02016-01-30 16:37:57 -0700612 } while (wait && priv->stage != STAGE_DONE);
613 if (priv->stage == STAGE_DONE)
Simon Glass0be8f202012-10-17 13:24:51 +0000614 debug("%s: LCD init complete\n", __func__);
615
616 return 0;
617}
618
Simon Glass9e6866d2016-01-30 16:37:56 -0700619static int tegra_lcd_probe(struct udevice *dev)
Simon Glass0be8f202012-10-17 13:24:51 +0000620{
Simon Glass9e6866d2016-01-30 16:37:56 -0700621 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
622 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
623 struct tegra_lcd_priv *priv = dev_get_priv(dev);
624 const void *blob = gd->fdt_blob;
625 int type = DCACHE_OFF;
626
627 if (tegra_display_decode_config(blob, dev->of_offset, priv))
628 return -1;
629
630 /* get panel details */
631 if (fdt_decode_lcd(blob, priv)) {
632 printf("No valid LCD information in device tree\n");
633 return -1;
Simon Glass0be8f202012-10-17 13:24:51 +0000634 }
Simon Glass9e6866d2016-01-30 16:37:56 -0700635
636 /* Initialize the Tegra display controller */
637 if (tegra_display_probe(blob, priv, (void *)plat->base)) {
638 printf("%s: Failed to probe display driver\n", __func__);
639 return -1;
640 }
641
642 tegra_lcd_check_next_stage(blob, priv, 1);
643
644 /* Set up the LCD caching as requested */
645 if (priv->cache_type & FDT_LCD_CACHE_WRITE_THROUGH)
646 type = DCACHE_WRITETHROUGH;
647 else if (priv->cache_type & FDT_LCD_CACHE_WRITE_BACK)
648 type = DCACHE_WRITEBACK;
649 mmu_set_region_dcache_behaviour(priv->frame_buffer, plat->size, type);
650
651 /* Enable flushing after LCD writes if requested */
652 video_set_flush_dcache(dev, priv->cache_type & FDT_LCD_CACHE_FLUSH);
653
654 uc_priv->xsize = priv->width;
655 uc_priv->ysize = priv->height;
656 uc_priv->bpix = priv->log2_bpp;
657 debug("LCD frame buffer at %pa, size %x\n", &priv->frame_buffer,
658 plat->size);
659
660 return 0;
Simon Glass0be8f202012-10-17 13:24:51 +0000661}
Simon Glass9e6866d2016-01-30 16:37:56 -0700662
663static int tegra_lcd_bind(struct udevice *dev)
664{
665 struct video_uc_platdata *plat = dev_get_uclass_platdata(dev);
666
667 plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
668 (1 << LCD_MAX_LOG2_BPP) / 8;
669
670 return 0;
671}
672
673static const struct video_ops tegra_lcd_ops = {
674};
675
676static const struct udevice_id tegra_lcd_ids[] = {
677 { .compatible = "nvidia,tegra20-dc" },
678 { }
679};
680
681U_BOOT_DRIVER(tegra_lcd) = {
682 .name = "tegra_lcd",
683 .id = UCLASS_VIDEO,
684 .of_match = tegra_lcd_ids,
685 .ops = &tegra_lcd_ops,
686 .bind = tegra_lcd_bind,
687 .probe = tegra_lcd_probe,
688 .priv_auto_alloc_size = sizeof(struct tegra_lcd_priv),
689};