blob: cc4b5b70015037969ef42cdcb2699573f5a6c015 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glasse161ccf2012-10-17 13:24:51 +00002/*
3 * Copyright (c) 2011 The Chromium OS Authors.
Simon Glasse161ccf2012-10-17 13:24:51 +00004 */
Simon Glassb1c50fb2016-01-30 16:37:57 -07005
Svyatoslav Ryhel7673aba2023-03-27 11:11:46 +03006#include <backlight.h>
Simon Glasse865ef32016-01-30 16:37:56 -07007#include <dm.h>
Simon Glasse161ccf2012-10-17 13:24:51 +00008#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass44fe9e42016-05-08 16:55:20 -060010#include <panel.h>
Simon Glass655306c2020-05-10 11:39:58 -060011#include <part.h>
Simon Glassd8af3c92016-01-30 16:38:01 -070012#include <pwm.h>
Simon Glasse865ef32016-01-30 16:37:56 -070013#include <video.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glasse161ccf2012-10-17 13:24:51 +000016#include <asm/system.h>
17#include <asm/gpio.h>
Simon Glassd8fc3c52016-01-30 16:37:53 -070018#include <asm/io.h>
Simon Glasse161ccf2012-10-17 13:24:51 +000019
20#include <asm/arch/clock.h>
21#include <asm/arch/funcmux.h>
22#include <asm/arch/pinmux.h>
Svyatoslav Ryhel1b0789b2024-01-23 19:16:22 +020023#include <asm/arch/powergate.h>
Simon Glasse161ccf2012-10-17 13:24:51 +000024#include <asm/arch/pwm.h>
Svyatoslav Ryhel75fec412024-01-23 19:16:18 +020025
26#include "tegra-dc.h"
Simon Glasse161ccf2012-10-17 13:24:51 +000027
28DECLARE_GLOBAL_DATA_PTR;
29
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +020030/* Holder of Tegra per-SOC DC differences */
31struct tegra_dc_soc_info {
32 bool has_timer;
33 bool has_rgb;
Svyatoslav Ryhel1b0789b2024-01-23 19:16:22 +020034 bool has_pgate;
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +020035};
36
Simon Glass923128f2016-01-30 16:37:55 -070037/* Information about the display controller */
38struct tegra_lcd_priv {
Simon Glass923128f2016-01-30 16:37:55 -070039 int width; /* width in pixels */
40 int height; /* height in pixels */
Simon Glass44fe9e42016-05-08 16:55:20 -060041 enum video_log2_bpp log2_bpp; /* colour depth */
42 struct display_timing timing;
43 struct udevice *panel;
Svyatoslav Ryhel9716fe52023-03-27 11:11:44 +030044 struct dc_ctlr *dc; /* Display controller regmap */
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +020045 const struct tegra_dc_soc_info *soc;
Simon Glass923128f2016-01-30 16:37:55 -070046 fdt_addr_t frame_buffer; /* Address of frame buffer */
47 unsigned pixel_clock; /* Pixel clock in Hz */
Svyatoslav Ryhelc1f260a2023-03-27 11:11:42 +030048 int dc_clk[2]; /* Contains clk and its parent */
Svyatoslav Ryhel4f5b79b2023-03-27 11:11:45 +030049 bool rotation; /* 180 degree panel turn */
Svyatoslav Ryhelbae46f32024-01-23 19:16:19 +020050 bool pipe; /* DC controller: 0 for A, 1 for B */
Simon Glass923128f2016-01-30 16:37:55 -070051};
52
Simon Glasse161ccf2012-10-17 13:24:51 +000053enum {
54 /* Maximum LCD size we support */
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +020055 LCD_MAX_WIDTH = 2560,
56 LCD_MAX_HEIGHT = 1600,
Simon Glasse865ef32016-01-30 16:37:56 -070057 LCD_MAX_LOG2_BPP = VIDEO_BPP16,
Simon Glasse161ccf2012-10-17 13:24:51 +000058};
59
Svyatoslav Ryhel4f5b79b2023-03-27 11:11:45 +030060static void update_window(struct tegra_lcd_priv *priv,
61 struct disp_ctl_win *win)
Simon Glassd8fc3c52016-01-30 16:37:53 -070062{
Svyatoslav Ryhel4f5b79b2023-03-27 11:11:45 +030063 struct dc_ctlr *dc = priv->dc;
Simon Glassd8fc3c52016-01-30 16:37:53 -070064 unsigned h_dda, v_dda;
65 unsigned long val;
66
67 val = readl(&dc->cmd.disp_win_header);
68 val |= WINDOW_A_SELECT;
69 writel(val, &dc->cmd.disp_win_header);
70
71 writel(win->fmt, &dc->win.color_depth);
72
73 clrsetbits_le32(&dc->win.byte_swap, BYTE_SWAP_MASK,
74 BYTE_SWAP_NOSWAP << BYTE_SWAP_SHIFT);
75
76 val = win->out_x << H_POSITION_SHIFT;
77 val |= win->out_y << V_POSITION_SHIFT;
78 writel(val, &dc->win.pos);
79
80 val = win->out_w << H_SIZE_SHIFT;
81 val |= win->out_h << V_SIZE_SHIFT;
82 writel(val, &dc->win.size);
83
84 val = (win->w * win->bpp / 8) << H_PRESCALED_SIZE_SHIFT;
85 val |= win->h << V_PRESCALED_SIZE_SHIFT;
86 writel(val, &dc->win.prescaled_size);
87
88 writel(0, &dc->win.h_initial_dda);
89 writel(0, &dc->win.v_initial_dda);
90
91 h_dda = (win->w * 0x1000) / max(win->out_w - 1, 1U);
92 v_dda = (win->h * 0x1000) / max(win->out_h - 1, 1U);
93
94 val = h_dda << H_DDA_INC_SHIFT;
95 val |= v_dda << V_DDA_INC_SHIFT;
96 writel(val, &dc->win.dda_increment);
97
98 writel(win->stride, &dc->win.line_stride);
99 writel(0, &dc->win.buf_stride);
100
101 val = WIN_ENABLE;
102 if (win->bpp < 24)
103 val |= COLOR_EXPAND;
Svyatoslav Ryhel4f5b79b2023-03-27 11:11:45 +0300104
105 if (priv->rotation)
106 val |= H_DIRECTION | V_DIRECTION;
107
Simon Glassd8fc3c52016-01-30 16:37:53 -0700108 writel(val, &dc->win.win_opt);
109
110 writel((unsigned long)win->phys_addr, &dc->winbuf.start_addr);
111 writel(win->x, &dc->winbuf.addr_h_offset);
112 writel(win->y, &dc->winbuf.addr_v_offset);
113
114 writel(0xff00, &dc->win.blend_nokey);
115 writel(0xff00, &dc->win.blend_1win);
116
117 val = GENERAL_ACT_REQ | WIN_A_ACT_REQ;
118 val |= GENERAL_UPDATE | WIN_A_UPDATE;
119 writel(val, &dc->cmd.state_ctrl);
120}
121
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200122static int update_display_mode(struct tegra_lcd_priv *priv)
Simon Glassd8fc3c52016-01-30 16:37:53 -0700123{
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200124 struct dc_disp_reg *disp = &priv->dc->disp;
Simon Glass44fe9e42016-05-08 16:55:20 -0600125 struct display_timing *dt = &priv->timing;
Simon Glassd8fc3c52016-01-30 16:37:53 -0700126 unsigned long val;
127 unsigned long rate;
128 unsigned long div;
129
130 writel(0x0, &disp->disp_timing_opt);
Simon Glassd8fc3c52016-01-30 16:37:53 -0700131
Simon Glass44fe9e42016-05-08 16:55:20 -0600132 writel(1 | 1 << 16, &disp->ref_to_sync);
133 writel(dt->hsync_len.typ | dt->vsync_len.typ << 16, &disp->sync_width);
134 writel(dt->hback_porch.typ | dt->vback_porch.typ << 16,
135 &disp->back_porch);
136 writel((dt->hfront_porch.typ - 1) | (dt->vfront_porch.typ - 1) << 16,
137 &disp->front_porch);
138 writel(dt->hactive.typ | (dt->vactive.typ << 16), &disp->disp_active);
Simon Glassd8fc3c52016-01-30 16:37:53 -0700139
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200140 if (priv->soc->has_rgb) {
141 val = DE_SELECT_ACTIVE << DE_SELECT_SHIFT;
142 val |= DE_CONTROL_NORMAL << DE_CONTROL_SHIFT;
143 writel(val, &disp->data_enable_opt);
Simon Glassd8fc3c52016-01-30 16:37:53 -0700144
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200145 val = DATA_FORMAT_DF1P1C << DATA_FORMAT_SHIFT;
146 val |= DATA_ALIGNMENT_MSB << DATA_ALIGNMENT_SHIFT;
147 val |= DATA_ORDER_RED_BLUE << DATA_ORDER_SHIFT;
148 writel(val, &disp->disp_interface_ctrl);
149 }
Simon Glassd8fc3c52016-01-30 16:37:53 -0700150
151 /*
152 * The pixel clock divider is in 7.1 format (where the bottom bit
153 * represents 0.5). Here we calculate the divider needed to get from
154 * the display clock (typically 600MHz) to the pixel clock. We round
155 * up or down as requried.
156 */
Svyatoslav Ryhelc1f260a2023-03-27 11:11:42 +0300157 rate = clock_get_periph_rate(priv->dc_clk[0], priv->dc_clk[1]);
Simon Glasse865ef32016-01-30 16:37:56 -0700158 div = ((rate * 2 + priv->pixel_clock / 2) / priv->pixel_clock) - 2;
Simon Glassd8fc3c52016-01-30 16:37:53 -0700159 debug("Display clock %lu, divider %lu\n", rate, div);
160
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200161 if (priv->soc->has_rgb)
162 writel(0x00010001, &disp->shift_clk_opt);
Simon Glassd8fc3c52016-01-30 16:37:53 -0700163
164 val = PIXEL_CLK_DIVIDER_PCD1 << PIXEL_CLK_DIVIDER_SHIFT;
165 val |= div << SHIFT_CLK_DIVIDER_SHIFT;
166 writel(val, &disp->disp_clk_ctrl);
167
168 return 0;
169}
170
171/* Start up the display and turn on power to PWMs */
172static void basic_init(struct dc_cmd_reg *cmd)
173{
174 u32 val;
175
176 writel(0x00000100, &cmd->gen_incr_syncpt_ctrl);
177 writel(0x0000011a, &cmd->cont_syncpt_vsync);
178 writel(0x00000000, &cmd->int_type);
179 writel(0x00000000, &cmd->int_polarity);
180 writel(0x00000000, &cmd->int_mask);
181 writel(0x00000000, &cmd->int_enb);
182
183 val = PW0_ENABLE | PW1_ENABLE | PW2_ENABLE;
184 val |= PW3_ENABLE | PW4_ENABLE | PM0_ENABLE;
185 val |= PM1_ENABLE;
186 writel(val, &cmd->disp_pow_ctrl);
187
188 val = readl(&cmd->disp_cmd);
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200189 val &= ~CTRL_MODE_MASK;
Simon Glassd8fc3c52016-01-30 16:37:53 -0700190 val |= CTRL_MODE_C_DISPLAY << CTRL_MODE_SHIFT;
191 writel(val, &cmd->disp_cmd);
192}
193
194static void basic_init_timer(struct dc_disp_reg *disp)
195{
196 writel(0x00000020, &disp->mem_high_pri);
197 writel(0x00000001, &disp->mem_high_pri_timer);
198}
199
200static const u32 rgb_enb_tab[PIN_REG_COUNT] = {
201 0x00000000,
202 0x00000000,
203 0x00000000,
204 0x00000000,
205};
206
207static const u32 rgb_polarity_tab[PIN_REG_COUNT] = {
208 0x00000000,
209 0x01000000,
210 0x00000000,
211 0x00000000,
212};
213
214static const u32 rgb_data_tab[PIN_REG_COUNT] = {
215 0x00000000,
216 0x00000000,
217 0x00000000,
218 0x00000000,
219};
220
221static const u32 rgb_sel_tab[PIN_OUTPUT_SEL_COUNT] = {
222 0x00000000,
223 0x00000000,
224 0x00000000,
225 0x00000000,
226 0x00210222,
227 0x00002200,
228 0x00020000,
229};
230
231static void rgb_enable(struct dc_com_reg *com)
232{
233 int i;
234
235 for (i = 0; i < PIN_REG_COUNT; i++) {
236 writel(rgb_enb_tab[i], &com->pin_output_enb[i]);
237 writel(rgb_polarity_tab[i], &com->pin_output_polarity[i]);
238 writel(rgb_data_tab[i], &com->pin_output_data[i]);
239 }
240
241 for (i = 0; i < PIN_OUTPUT_SEL_COUNT; i++)
242 writel(rgb_sel_tab[i], &com->pin_output_sel[i]);
243}
244
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200245static int setup_window(struct tegra_lcd_priv *priv,
246 struct disp_ctl_win *win)
Simon Glassd8fc3c52016-01-30 16:37:53 -0700247{
Svyatoslav Ryhel4f5b79b2023-03-27 11:11:45 +0300248 if (priv->rotation) {
Svyatoslav Ryhel597eecb2024-01-23 19:16:17 +0200249 win->x = priv->width * 2 - 1;
250 win->y = priv->height - 1;
Svyatoslav Ryhel4f5b79b2023-03-27 11:11:45 +0300251 } else {
252 win->x = 0;
253 win->y = 0;
254 }
255
Simon Glasse865ef32016-01-30 16:37:56 -0700256 win->w = priv->width;
257 win->h = priv->height;
Simon Glassd8fc3c52016-01-30 16:37:53 -0700258 win->out_x = 0;
259 win->out_y = 0;
Simon Glasse865ef32016-01-30 16:37:56 -0700260 win->out_w = priv->width;
261 win->out_h = priv->height;
262 win->phys_addr = priv->frame_buffer;
263 win->stride = priv->width * (1 << priv->log2_bpp) / 8;
264 debug("%s: depth = %d\n", __func__, priv->log2_bpp);
265 switch (priv->log2_bpp) {
Simon Glass44fe9e42016-05-08 16:55:20 -0600266 case VIDEO_BPP32:
Simon Glassd8fc3c52016-01-30 16:37:53 -0700267 win->fmt = COLOR_DEPTH_R8G8B8A8;
268 win->bpp = 32;
269 break;
Simon Glass44fe9e42016-05-08 16:55:20 -0600270 case VIDEO_BPP16:
Simon Glassd8fc3c52016-01-30 16:37:53 -0700271 win->fmt = COLOR_DEPTH_B5G6R5;
272 win->bpp = 16;
273 break;
274
275 default:
276 debug("Unsupported LCD bit depth");
277 return -1;
278 }
279
280 return 0;
281}
282
Simon Glassd8fc3c52016-01-30 16:37:53 -0700283/**
Simon Glassd8fc3c52016-01-30 16:37:53 -0700284 * Register a new display based on device tree configuration.
285 *
Robert P. J. Day8d56db92016-07-15 13:44:45 -0400286 * The frame buffer can be positioned by U-Boot or overridden by the fdt.
Simon Glassd8fc3c52016-01-30 16:37:53 -0700287 * You should pass in the U-Boot address here, and check the contents of
Simon Glass923128f2016-01-30 16:37:55 -0700288 * struct tegra_lcd_priv to see what was actually chosen.
Simon Glassd8fc3c52016-01-30 16:37:53 -0700289 *
Simon Glasse865ef32016-01-30 16:37:56 -0700290 * @param priv Driver's private data
Simon Glassd8fc3c52016-01-30 16:37:53 -0700291 * @param default_lcd_base Default address of LCD frame buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100292 * Return: 0 if ok, -1 on error (unsupported bits per pixel)
Simon Glassd8fc3c52016-01-30 16:37:53 -0700293 */
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200294static int tegra_display_probe(struct tegra_lcd_priv *priv,
Simon Glasse865ef32016-01-30 16:37:56 -0700295 void *default_lcd_base)
Simon Glassd8fc3c52016-01-30 16:37:53 -0700296{
297 struct disp_ctl_win window;
Svyatoslav Ryhelc1f260a2023-03-27 11:11:42 +0300298 unsigned long rate = clock_get_rate(priv->dc_clk[1]);
Simon Glassd8fc3c52016-01-30 16:37:53 -0700299
Simon Glasse865ef32016-01-30 16:37:56 -0700300 priv->frame_buffer = (u32)default_lcd_base;
Simon Glassd8fc3c52016-01-30 16:37:53 -0700301
Simon Glassd8fc3c52016-01-30 16:37:53 -0700302 /*
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200303 * We halve the rate if DISP1 parent is PLLD, since actual parent
Svyatoslav Ryhelc1f260a2023-03-27 11:11:42 +0300304 * is plld_out0 which is PLLD divided by 2.
Simon Glassd8fc3c52016-01-30 16:37:53 -0700305 */
Svyatoslav Ryhelc1f260a2023-03-27 11:11:42 +0300306 if (priv->dc_clk[1] == CLOCK_ID_DISPLAY)
307 rate /= 2;
308
Svyatoslav Ryhele38ac622024-01-23 19:16:20 +0200309#ifndef CONFIG_TEGRA20
310 /* PLLD2 obeys same rules as PLLD but it is present only on T30+ */
311 if (priv->dc_clk[1] == CLOCK_ID_DISPLAY2)
312 rate /= 2;
313#endif
314
Svyatoslav Ryhelc1f260a2023-03-27 11:11:42 +0300315 /*
316 * HOST1X is init by default at 150MHz with PLLC as parent
317 */
318 clock_start_periph_pll(PERIPH_ID_HOST1X, CLOCK_ID_CGENERAL,
319 150 * 1000000);
320 clock_start_periph_pll(priv->dc_clk[0], priv->dc_clk[1],
321 rate);
322
Svyatoslav Ryhel9716fe52023-03-27 11:11:44 +0300323 basic_init(&priv->dc->cmd);
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200324
325 if (priv->soc->has_timer)
326 basic_init_timer(&priv->dc->disp);
327
328 if (priv->soc->has_rgb)
329 rgb_enable(&priv->dc->com);
Simon Glassd8fc3c52016-01-30 16:37:53 -0700330
Simon Glasse865ef32016-01-30 16:37:56 -0700331 if (priv->pixel_clock)
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200332 update_display_mode(priv);
Simon Glassd8fc3c52016-01-30 16:37:53 -0700333
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200334 if (setup_window(priv, &window))
Simon Glassd8fc3c52016-01-30 16:37:53 -0700335 return -1;
336
Svyatoslav Ryhel4f5b79b2023-03-27 11:11:45 +0300337 update_window(priv, &window);
Simon Glassd8fc3c52016-01-30 16:37:53 -0700338
339 return 0;
340}
341
Simon Glasse865ef32016-01-30 16:37:56 -0700342static int tegra_lcd_probe(struct udevice *dev)
Simon Glasse161ccf2012-10-17 13:24:51 +0000343{
Simon Glassb75b15b2020-12-03 16:55:23 -0700344 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Simon Glasse865ef32016-01-30 16:37:56 -0700345 struct video_priv *uc_priv = dev_get_uclass_priv(dev);
346 struct tegra_lcd_priv *priv = dev_get_priv(dev);
Simon Glass44fe9e42016-05-08 16:55:20 -0600347 int ret;
Simon Glasse865ef32016-01-30 16:37:56 -0700348
Simon Glasse865ef32016-01-30 16:37:56 -0700349 /* Initialize the Tegra display controller */
Marcel Ziswilercad56712023-03-27 11:11:40 +0300350#ifdef CONFIG_TEGRA20
Simon Glass44fe9e42016-05-08 16:55:20 -0600351 funcmux_select(PERIPH_ID_DISP1, FUNCMUX_DEFAULT);
Marcel Ziswilercad56712023-03-27 11:11:40 +0300352#endif
353
Svyatoslav Ryhel1b0789b2024-01-23 19:16:22 +0200354 if (priv->soc->has_pgate) {
355 uint powergate;
356
357 if (priv->pipe)
358 powergate = TEGRA_POWERGATE_DISB;
359 else
360 powergate = TEGRA_POWERGATE_DIS;
361
362 ret = tegra_powergate_power_off(powergate);
363 if (ret < 0) {
364 log_err("failed to power off DISP gate: %d", ret);
365 return ret;
366 }
367
368 ret = tegra_powergate_sequence_power_up(powergate,
369 priv->dc_clk[0]);
370 if (ret < 0) {
371 log_err("failed to power up DISP gate: %d", ret);
372 return ret;
373 }
374 }
375
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200376 if (tegra_display_probe(priv, (void *)plat->base)) {
377 debug("%s: Failed to probe display driver\n", __func__);
Simon Glasse865ef32016-01-30 16:37:56 -0700378 return -1;
Simon Glasse161ccf2012-10-17 13:24:51 +0000379 }
Simon Glasse865ef32016-01-30 16:37:56 -0700380
Marcel Ziswilercad56712023-03-27 11:11:40 +0300381#ifdef CONFIG_TEGRA20
Simon Glass44fe9e42016-05-08 16:55:20 -0600382 pinmux_set_func(PMUX_PINGRP_GPU, PMUX_FUNC_PWM);
383 pinmux_tristate_disable(PMUX_PINGRP_GPU);
Marcel Ziswilercad56712023-03-27 11:11:40 +0300384#endif
Simon Glass44fe9e42016-05-08 16:55:20 -0600385
386 ret = panel_enable_backlight(priv->panel);
387 if (ret) {
388 debug("%s: Cannot enable backlight, ret=%d\n", __func__, ret);
389 return ret;
390 }
Simon Glasse865ef32016-01-30 16:37:56 -0700391
Svyatoslav Ryhel7673aba2023-03-27 11:11:46 +0300392 ret = panel_set_backlight(priv->panel, BACKLIGHT_DEFAULT);
393 if (ret) {
394 debug("%s: Cannot set backlight to default, ret=%d\n", __func__, ret);
395 return ret;
396 }
397
Simon Glassbbdae4b2016-05-08 16:55:21 -0600398 mmu_set_region_dcache_behaviour(priv->frame_buffer, plat->size,
399 DCACHE_WRITETHROUGH);
Simon Glasse865ef32016-01-30 16:37:56 -0700400
401 /* Enable flushing after LCD writes if requested */
Simon Glassbbdae4b2016-05-08 16:55:21 -0600402 video_set_flush_dcache(dev, true);
Simon Glasse865ef32016-01-30 16:37:56 -0700403
404 uc_priv->xsize = priv->width;
405 uc_priv->ysize = priv->height;
406 uc_priv->bpix = priv->log2_bpp;
407 debug("LCD frame buffer at %pa, size %x\n", &priv->frame_buffer,
408 plat->size);
409
410 return 0;
411}
412
Simon Glassaad29ae2020-12-03 16:55:21 -0700413static int tegra_lcd_of_to_plat(struct udevice *dev)
Simon Glass60740e72016-01-30 16:37:59 -0700414{
415 struct tegra_lcd_priv *priv = dev_get_priv(dev);
416 const void *blob = gd->fdt_blob;
Simon Glass44fe9e42016-05-08 16:55:20 -0600417 struct display_timing *timing;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700418 int node = dev_of_offset(dev);
Simon Glass60740e72016-01-30 16:37:59 -0700419 int panel_node;
420 int rgb;
Simon Glassd8af3c92016-01-30 16:38:01 -0700421 int ret;
Simon Glass60740e72016-01-30 16:37:59 -0700422
Svyatoslav Ryhel9716fe52023-03-27 11:11:44 +0300423 priv->dc = (struct dc_ctlr *)dev_read_addr_ptr(dev);
424 if (!priv->dc) {
Simon Glass60740e72016-01-30 16:37:59 -0700425 debug("%s: No display controller address\n", __func__);
426 return -EINVAL;
427 }
428
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200429 priv->soc = (struct tegra_dc_soc_info *)dev_get_driver_data(dev);
430
Svyatoslav Ryhelc1f260a2023-03-27 11:11:42 +0300431 ret = clock_decode_pair(dev, priv->dc_clk);
432 if (ret < 0) {
433 debug("%s: Cannot decode clocks for '%s' (ret = %d)\n",
434 __func__, dev->name, ret);
435 return -EINVAL;
436 }
437
Svyatoslav Ryhel4f5b79b2023-03-27 11:11:45 +0300438 priv->rotation = dev_read_bool(dev, "nvidia,180-rotation");
439
Svyatoslav Ryhelbae46f32024-01-23 19:16:19 +0200440 if (!strcmp(dev->name, TEGRA_DC_B))
441 priv->pipe = 1;
442
Simon Glass60740e72016-01-30 16:37:59 -0700443 rgb = fdt_subnode_offset(blob, node, "rgb");
Simon Glass44fe9e42016-05-08 16:55:20 -0600444 if (rgb < 0) {
445 debug("%s: Cannot find rgb subnode for '%s' (ret=%d)\n",
446 __func__, dev->name, rgb);
Simon Glass60740e72016-01-30 16:37:59 -0700447 return -EINVAL;
448 }
449
Simon Glass44fe9e42016-05-08 16:55:20 -0600450 /*
451 * Sadly the panel phandle is in an rgb subnode so we cannot use
452 * uclass_get_device_by_phandle().
453 */
454 panel_node = fdtdec_lookup_phandle(blob, rgb, "nvidia,panel");
455 if (panel_node < 0) {
456 debug("%s: Cannot find panel information\n", __func__);
Simon Glass60740e72016-01-30 16:37:59 -0700457 return -EINVAL;
458 }
Svyatoslav Ryheld8806292023-03-27 11:11:43 +0300459
Simon Glass44fe9e42016-05-08 16:55:20 -0600460 ret = uclass_get_device_by_of_offset(UCLASS_PANEL, panel_node,
461 &priv->panel);
Simon Glassd8af3c92016-01-30 16:38:01 -0700462 if (ret) {
Simon Glass44fe9e42016-05-08 16:55:20 -0600463 debug("%s: Cannot find panel for '%s' (ret=%d)\n", __func__,
464 dev->name, ret);
465 return ret;
Simon Glassd8af3c92016-01-30 16:38:01 -0700466 }
Simon Glass60740e72016-01-30 16:37:59 -0700467
Svyatoslav Ryhelbae46f32024-01-23 19:16:19 +0200468 /* Fill the platform data for internal devices */
Svyatoslav Ryhel0c8aa5e2023-03-27 11:11:47 +0300469 if (!strcmp(priv->panel->name, TEGRA_DSI_A) ||
470 !strcmp(priv->panel->name, TEGRA_DSI_B)) {
471 struct tegra_dc_plat *dc_plat = dev_get_plat(priv->panel);
472
473 dc_plat->dev = dev;
474 dc_plat->dc = priv->dc;
Svyatoslav Ryhelbae46f32024-01-23 19:16:19 +0200475 dc_plat->pipe = priv->pipe;
Svyatoslav Ryhel0c8aa5e2023-03-27 11:11:47 +0300476 }
477
Svyatoslav Ryheld8806292023-03-27 11:11:43 +0300478 ret = panel_get_display_timing(priv->panel, &priv->timing);
479 if (ret) {
480 ret = fdtdec_decode_display_timing(blob, rgb, 0, &priv->timing);
481 if (ret) {
482 debug("%s: Cannot read display timing for '%s' (ret=%d)\n",
483 __func__, dev->name, ret);
484 return -EINVAL;
485 }
486 }
487
488 timing = &priv->timing;
489 priv->width = timing->hactive.typ;
490 priv->height = timing->vactive.typ;
491 priv->pixel_clock = timing->pixelclock.typ;
492 priv->log2_bpp = VIDEO_BPP16;
493
Simon Glass60740e72016-01-30 16:37:59 -0700494 return 0;
495}
496
Simon Glasse865ef32016-01-30 16:37:56 -0700497static int tegra_lcd_bind(struct udevice *dev)
498{
Simon Glassb75b15b2020-12-03 16:55:23 -0700499 struct video_uc_plat *plat = dev_get_uclass_plat(dev);
Stephen Warren225da8b2016-04-19 16:19:30 -0600500 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700501 int node = dev_of_offset(dev);
Stephen Warren225da8b2016-04-19 16:19:30 -0600502 int rgb;
503
504 rgb = fdt_subnode_offset(blob, node, "rgb");
505 if ((rgb < 0) || !fdtdec_get_is_enabled(blob, rgb))
506 return -ENODEV;
Simon Glasse865ef32016-01-30 16:37:56 -0700507
508 plat->size = LCD_MAX_WIDTH * LCD_MAX_HEIGHT *
509 (1 << LCD_MAX_LOG2_BPP) / 8;
510
511 return 0;
Simon Glasse161ccf2012-10-17 13:24:51 +0000512}
Simon Glasse865ef32016-01-30 16:37:56 -0700513
514static const struct video_ops tegra_lcd_ops = {
515};
516
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200517static const struct tegra_dc_soc_info tegra20_dc_soc_info = {
518 .has_timer = true,
519 .has_rgb = true,
Svyatoslav Ryhel1b0789b2024-01-23 19:16:22 +0200520 .has_pgate = false,
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200521};
522
523static const struct tegra_dc_soc_info tegra30_dc_soc_info = {
524 .has_timer = false,
525 .has_rgb = true,
Svyatoslav Ryhel1b0789b2024-01-23 19:16:22 +0200526 .has_pgate = false,
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200527};
528
529static const struct tegra_dc_soc_info tegra114_dc_soc_info = {
530 .has_timer = false,
531 .has_rgb = false,
Svyatoslav Ryhel1b0789b2024-01-23 19:16:22 +0200532 .has_pgate = true,
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200533};
534
Simon Glasse865ef32016-01-30 16:37:56 -0700535static const struct udevice_id tegra_lcd_ids[] = {
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200536 {
537 .compatible = "nvidia,tegra20-dc",
538 .data = (ulong)&tegra20_dc_soc_info
539 }, {
540 .compatible = "nvidia,tegra30-dc",
541 .data = (ulong)&tegra30_dc_soc_info
542 }, {
543 .compatible = "nvidia,tegra114-dc",
544 .data = (ulong)&tegra114_dc_soc_info
545 }, {
546 /* sentinel */
547 }
Simon Glasse865ef32016-01-30 16:37:56 -0700548};
549
550U_BOOT_DRIVER(tegra_lcd) = {
Svyatoslav Ryhel9d53a7b2024-01-23 19:16:16 +0200551 .name = "tegra_lcd",
552 .id = UCLASS_VIDEO,
553 .of_match = tegra_lcd_ids,
554 .ops = &tegra_lcd_ops,
555 .bind = tegra_lcd_bind,
556 .probe = tegra_lcd_probe,
Simon Glassaad29ae2020-12-03 16:55:21 -0700557 .of_to_plat = tegra_lcd_of_to_plat,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700558 .priv_auto = sizeof(struct tegra_lcd_priv),
Simon Glasse865ef32016-01-30 16:37:56 -0700559};