Simon Glass | 3ef2a72 | 2015-04-14 21:03:42 -0600 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2014 Google Inc. |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | * |
| 6 | * Extracted from Chromium coreboot commit 3f59b13d |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
| 11 | #include <edid.h> |
| 12 | #include <errno.h> |
| 13 | #include <displayport.h> |
| 14 | #include <edid.h> |
| 15 | #include <fdtdec.h> |
| 16 | #include <lcd.h> |
| 17 | #include <asm/gpio.h> |
| 18 | #include <asm/io.h> |
| 19 | #include <asm/arch/clock.h> |
| 20 | #include <asm/arch/pwm.h> |
| 21 | #include <asm/arch-tegra/dc.h> |
| 22 | #include "displayport.h" |
| 23 | |
| 24 | DECLARE_GLOBAL_DATA_PTR; |
| 25 | |
| 26 | /* return in 1000ths of a Hertz */ |
| 27 | static int tegra_dc_calc_refresh(const struct display_timing *timing) |
| 28 | { |
| 29 | int h_total, v_total, refresh; |
| 30 | int pclk = timing->pixelclock.typ; |
| 31 | |
| 32 | h_total = timing->hactive.typ + timing->hfront_porch.typ + |
| 33 | timing->hback_porch.typ + timing->hsync_len.typ; |
| 34 | v_total = timing->vactive.typ + timing->vfront_porch.typ + |
| 35 | timing->vback_porch.typ + timing->vsync_len.typ; |
| 36 | if (!pclk || !h_total || !v_total) |
| 37 | return 0; |
| 38 | refresh = pclk / h_total; |
| 39 | refresh *= 1000; |
| 40 | refresh /= v_total; |
| 41 | |
| 42 | return refresh; |
| 43 | } |
| 44 | |
| 45 | static void print_mode(const struct display_timing *timing) |
| 46 | { |
| 47 | int refresh = tegra_dc_calc_refresh(timing); |
| 48 | |
| 49 | debug("MODE:%dx%d@%d.%03uHz pclk=%d\n", |
| 50 | timing->hactive.typ, timing->vactive.typ, refresh / 1000, |
| 51 | refresh % 1000, timing->pixelclock.typ); |
| 52 | } |
| 53 | |
| 54 | static int update_display_mode(struct dc_ctlr *disp_ctrl, |
| 55 | const struct display_timing *timing, |
| 56 | int href_to_sync, int vref_to_sync) |
| 57 | { |
| 58 | print_mode(timing); |
| 59 | |
| 60 | writel(0x1, &disp_ctrl->disp.disp_timing_opt); |
| 61 | |
| 62 | writel(vref_to_sync << 16 | href_to_sync, |
| 63 | &disp_ctrl->disp.ref_to_sync); |
| 64 | |
| 65 | writel(timing->vsync_len.typ << 16 | timing->hsync_len.typ, |
| 66 | &disp_ctrl->disp.sync_width); |
| 67 | |
| 68 | writel(((timing->vback_porch.typ - vref_to_sync) << 16) | |
| 69 | timing->hback_porch.typ, &disp_ctrl->disp.back_porch); |
| 70 | |
| 71 | writel(((timing->vfront_porch.typ + vref_to_sync) << 16) | |
| 72 | timing->hfront_porch.typ, &disp_ctrl->disp.front_porch); |
| 73 | |
| 74 | writel(timing->hactive.typ | (timing->vactive.typ << 16), |
| 75 | &disp_ctrl->disp.disp_active); |
| 76 | |
| 77 | /** |
| 78 | * We want to use PLLD_out0, which is PLLD / 2: |
| 79 | * PixelClock = (PLLD / 2) / ShiftClockDiv / PixelClockDiv. |
| 80 | * |
| 81 | * Currently most panels work inside clock range 50MHz~100MHz, and PLLD |
| 82 | * has some requirements to have VCO in range 500MHz~1000MHz (see |
| 83 | * clock.c for more detail). To simplify calculation, we set |
| 84 | * PixelClockDiv to 1 and ShiftClockDiv to 1. In future these values |
| 85 | * may be calculated by clock_display, to allow wider frequency range. |
| 86 | * |
| 87 | * Note ShiftClockDiv is a 7.1 format value. |
| 88 | */ |
| 89 | const u32 shift_clock_div = 1; |
| 90 | writel((PIXEL_CLK_DIVIDER_PCD1 << PIXEL_CLK_DIVIDER_SHIFT) | |
| 91 | ((shift_clock_div - 1) * 2) << SHIFT_CLK_DIVIDER_SHIFT, |
| 92 | &disp_ctrl->disp.disp_clk_ctrl); |
| 93 | debug("%s: PixelClock=%u, ShiftClockDiv=%u\n", __func__, |
| 94 | timing->pixelclock.typ, shift_clock_div); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int tegra_depth_for_bpp(int bpp) |
| 99 | { |
| 100 | switch (bpp) { |
| 101 | case 32: |
| 102 | return COLOR_DEPTH_R8G8B8A8; |
| 103 | case 16: |
| 104 | return COLOR_DEPTH_B5G6R5; |
| 105 | default: |
| 106 | debug("Unsupported LCD bit depth"); |
| 107 | return -1; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static int update_window(struct dc_ctlr *disp_ctrl, |
| 112 | u32 frame_buffer, int fb_bits_per_pixel, |
| 113 | const struct display_timing *timing) |
| 114 | { |
| 115 | const u32 colour_white = 0xffffff; |
| 116 | int colour_depth; |
| 117 | u32 val; |
| 118 | |
| 119 | writel(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header); |
| 120 | |
| 121 | writel(((timing->vactive.typ << 16) | timing->hactive.typ), |
| 122 | &disp_ctrl->win.size); |
| 123 | writel(((timing->vactive.typ << 16) | |
| 124 | (timing->hactive.typ * fb_bits_per_pixel / 8)), |
| 125 | &disp_ctrl->win.prescaled_size); |
| 126 | writel(((timing->hactive.typ * fb_bits_per_pixel / 8 + 31) / |
| 127 | 32 * 32), &disp_ctrl->win.line_stride); |
| 128 | |
| 129 | colour_depth = tegra_depth_for_bpp(fb_bits_per_pixel); |
| 130 | if (colour_depth == -1) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | writel(colour_depth, &disp_ctrl->win.color_depth); |
| 134 | |
| 135 | writel(frame_buffer, &disp_ctrl->winbuf.start_addr); |
| 136 | writel(0x1000 << V_DDA_INC_SHIFT | 0x1000 << H_DDA_INC_SHIFT, |
| 137 | &disp_ctrl->win.dda_increment); |
| 138 | |
| 139 | writel(colour_white, &disp_ctrl->disp.blend_background_color); |
| 140 | writel(CTRL_MODE_C_DISPLAY << CTRL_MODE_SHIFT, |
| 141 | &disp_ctrl->cmd.disp_cmd); |
| 142 | |
| 143 | writel(WRITE_MUX_ACTIVE, &disp_ctrl->cmd.state_access); |
| 144 | |
| 145 | val = GENERAL_ACT_REQ | WIN_A_ACT_REQ; |
| 146 | val |= GENERAL_UPDATE | WIN_A_UPDATE; |
| 147 | writel(val, &disp_ctrl->cmd.state_ctrl); |
| 148 | |
| 149 | /* Enable win_a */ |
| 150 | val = readl(&disp_ctrl->win.win_opt); |
| 151 | writel(val | WIN_ENABLE, &disp_ctrl->win.win_opt); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int tegra_dc_init(struct dc_ctlr *disp_ctrl) |
| 157 | { |
| 158 | /* do not accept interrupts during initialization */ |
| 159 | writel(0x00000000, &disp_ctrl->cmd.int_mask); |
| 160 | writel(WRITE_MUX_ASSEMBLY | READ_MUX_ASSEMBLY, |
| 161 | &disp_ctrl->cmd.state_access); |
| 162 | writel(WINDOW_A_SELECT, &disp_ctrl->cmd.disp_win_header); |
| 163 | writel(0x00000000, &disp_ctrl->win.win_opt); |
| 164 | writel(0x00000000, &disp_ctrl->win.byte_swap); |
| 165 | writel(0x00000000, &disp_ctrl->win.buffer_ctrl); |
| 166 | |
| 167 | writel(0x00000000, &disp_ctrl->win.pos); |
| 168 | writel(0x00000000, &disp_ctrl->win.h_initial_dda); |
| 169 | writel(0x00000000, &disp_ctrl->win.v_initial_dda); |
| 170 | writel(0x00000000, &disp_ctrl->win.dda_increment); |
| 171 | writel(0x00000000, &disp_ctrl->win.dv_ctrl); |
| 172 | |
| 173 | writel(0x01000000, &disp_ctrl->win.blend_layer_ctrl); |
| 174 | writel(0x00000000, &disp_ctrl->win.blend_match_select); |
| 175 | writel(0x00000000, &disp_ctrl->win.blend_nomatch_select); |
| 176 | writel(0x00000000, &disp_ctrl->win.blend_alpha_1bit); |
| 177 | |
| 178 | writel(0x00000000, &disp_ctrl->winbuf.start_addr_hi); |
| 179 | writel(0x00000000, &disp_ctrl->winbuf.addr_h_offset); |
| 180 | writel(0x00000000, &disp_ctrl->winbuf.addr_v_offset); |
| 181 | |
| 182 | writel(0x00000000, &disp_ctrl->com.crc_checksum); |
| 183 | writel(0x00000000, &disp_ctrl->com.pin_output_enb[0]); |
| 184 | writel(0x00000000, &disp_ctrl->com.pin_output_enb[1]); |
| 185 | writel(0x00000000, &disp_ctrl->com.pin_output_enb[2]); |
| 186 | writel(0x00000000, &disp_ctrl->com.pin_output_enb[3]); |
| 187 | writel(0x00000000, &disp_ctrl->disp.disp_signal_opt0); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static void dump_config(int panel_bpp, struct display_timing *timing) |
| 193 | { |
| 194 | printf("timing->hactive.typ = %d\n", timing->hactive.typ); |
| 195 | printf("timing->vactive.typ = %d\n", timing->vactive.typ); |
| 196 | printf("timing->pixelclock.typ = %d\n", timing->pixelclock.typ); |
| 197 | |
| 198 | printf("timing->hfront_porch.typ = %d\n", timing->hfront_porch.typ); |
| 199 | printf("timing->hsync_len.typ = %d\n", timing->hsync_len.typ); |
| 200 | printf("timing->hback_porch.typ = %d\n", timing->hback_porch.typ); |
| 201 | |
| 202 | printf("timing->vfront_porch.typ %d\n", timing->vfront_porch.typ); |
| 203 | printf("timing->vsync_len.typ = %d\n", timing->vsync_len.typ); |
| 204 | printf("timing->vback_porch.typ = %d\n", timing->vback_porch.typ); |
| 205 | |
| 206 | printf("panel_bits_per_pixel = %d\n", panel_bpp); |
| 207 | } |
| 208 | |
| 209 | static int display_update_config_from_edid(struct udevice *dp_dev, |
| 210 | int *panel_bppp, |
| 211 | struct display_timing *timing) |
| 212 | { |
| 213 | u8 buf[EDID_SIZE]; |
| 214 | int bpc, ret; |
| 215 | |
| 216 | ret = display_port_read_edid(dp_dev, buf, sizeof(buf)); |
| 217 | if (ret < 0) |
| 218 | return ret; |
| 219 | ret = edid_get_timing(buf, ret, timing, &bpc); |
| 220 | if (ret) |
| 221 | return ret; |
| 222 | |
| 223 | /* Use this information if valid */ |
| 224 | if (bpc != -1) |
| 225 | *panel_bppp = bpc * 3; |
| 226 | |
| 227 | return 0; |
| 228 | } |
| 229 | |
| 230 | /* Somewhat torturous method */ |
| 231 | static int get_backlight_info(const void *blob, struct gpio_desc *vdd, |
| 232 | struct gpio_desc *enable, int *pwmp) |
| 233 | { |
| 234 | int sor, panel, backlight, power; |
| 235 | const u32 *prop; |
| 236 | int len; |
| 237 | int ret; |
| 238 | |
| 239 | *pwmp = 0; |
| 240 | sor = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA124_SOR); |
| 241 | if (sor < 0) |
| 242 | return -ENOENT; |
| 243 | panel = fdtdec_lookup_phandle(blob, sor, "nvidia,panel"); |
| 244 | if (panel < 0) |
| 245 | return -ENOENT; |
| 246 | backlight = fdtdec_lookup_phandle(blob, panel, "backlight"); |
| 247 | if (backlight < 0) |
| 248 | return -ENOENT; |
| 249 | ret = gpio_request_by_name_nodev(blob, backlight, "enable-gpios", 0, |
| 250 | enable, GPIOD_IS_OUT); |
| 251 | if (ret) |
| 252 | return ret; |
| 253 | prop = fdt_getprop(blob, backlight, "pwms", &len); |
| 254 | if (!prop || len != 3 * sizeof(u32)) |
| 255 | return -EINVAL; |
| 256 | *pwmp = fdt32_to_cpu(prop[1]); |
| 257 | |
| 258 | power = fdtdec_lookup_phandle(blob, backlight, "power-supply"); |
| 259 | if (power < 0) |
| 260 | return -ENOENT; |
| 261 | ret = gpio_request_by_name_nodev(blob, power, "gpio", 0, vdd, |
| 262 | GPIOD_IS_OUT); |
| 263 | if (ret) |
| 264 | goto err; |
| 265 | |
| 266 | return 0; |
| 267 | |
| 268 | err: |
| 269 | dm_gpio_free(NULL, enable); |
| 270 | return ret; |
| 271 | } |
| 272 | |
| 273 | int display_init(void *lcdbase, int fb_bits_per_pixel, |
| 274 | struct display_timing *timing) |
| 275 | { |
| 276 | struct dc_ctlr *dc_ctlr; |
| 277 | const void *blob = gd->fdt_blob; |
| 278 | struct udevice *dp_dev; |
| 279 | const int href_to_sync = 1, vref_to_sync = 1; |
| 280 | int panel_bpp = 18; /* default 18 bits per pixel */ |
| 281 | u32 plld_rate; |
| 282 | struct gpio_desc vdd_gpio, enable_gpio; |
| 283 | int pwm; |
| 284 | int node; |
| 285 | int ret; |
| 286 | |
| 287 | ret = uclass_get_device(UCLASS_DISPLAY_PORT, 0, &dp_dev); |
| 288 | if (ret) |
| 289 | return ret; |
| 290 | |
| 291 | node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA124_DC); |
| 292 | if (node < 0) |
| 293 | return -ENOENT; |
| 294 | dc_ctlr = (struct dc_ctlr *)fdtdec_get_addr(blob, node, "reg"); |
| 295 | if (fdtdec_decode_display_timing(blob, node, 0, timing)) |
| 296 | return -EINVAL; |
| 297 | |
| 298 | ret = display_update_config_from_edid(dp_dev, &panel_bpp, timing); |
| 299 | if (ret) { |
| 300 | debug("%s: Failed to decode EDID, using defaults\n", __func__); |
| 301 | dump_config(panel_bpp, timing); |
| 302 | } |
| 303 | |
| 304 | if (!get_backlight_info(blob, &vdd_gpio, &enable_gpio, &pwm)) { |
| 305 | dm_gpio_set_value(&vdd_gpio, 1); |
| 306 | debug("%s: backlight vdd setting gpio %08x to %d\n", |
| 307 | __func__, gpio_get_number(&vdd_gpio), 1); |
| 308 | } |
| 309 | |
| 310 | /* |
| 311 | * The plld is programmed with the assumption of the SHIFT_CLK_DIVIDER |
| 312 | * and PIXEL_CLK_DIVIDER are zero (divide by 1). See the |
| 313 | * update_display_mode() for detail. |
| 314 | */ |
| 315 | plld_rate = clock_set_display_rate(timing->pixelclock.typ * 2); |
| 316 | if (plld_rate == 0) { |
| 317 | printf("dc: clock init failed\n"); |
| 318 | return -EIO; |
| 319 | } else if (plld_rate != timing->pixelclock.typ * 2) { |
| 320 | debug("dc: plld rounded to %u\n", plld_rate); |
| 321 | timing->pixelclock.typ = plld_rate / 2; |
| 322 | } |
| 323 | |
| 324 | /* Init dc */ |
| 325 | ret = tegra_dc_init(dc_ctlr); |
| 326 | if (ret) { |
| 327 | debug("dc: init failed\n"); |
| 328 | return ret; |
| 329 | } |
| 330 | |
| 331 | /* Configure dc mode */ |
| 332 | ret = update_display_mode(dc_ctlr, timing, href_to_sync, vref_to_sync); |
| 333 | if (ret) { |
| 334 | debug("dc: failed to configure display mode\n"); |
| 335 | return ret; |
| 336 | } |
| 337 | |
| 338 | /* Enable dp */ |
| 339 | ret = display_port_enable(dp_dev, panel_bpp, timing); |
| 340 | if (ret) |
| 341 | return ret; |
| 342 | |
| 343 | ret = update_window(dc_ctlr, (ulong)lcdbase, fb_bits_per_pixel, timing); |
| 344 | if (ret) |
| 345 | return ret; |
| 346 | |
| 347 | /* Set up Tegra PWM to drive the panel backlight */ |
| 348 | pwm_enable(pwm, 0, 220, 0x2e); |
| 349 | udelay(10 * 1000); |
| 350 | |
| 351 | if (dm_gpio_is_valid(&enable_gpio)) { |
| 352 | dm_gpio_set_value(&enable_gpio, 1); |
| 353 | debug("%s: backlight enable setting gpio %08x to %d\n", |
| 354 | __func__, gpio_get_number(&enable_gpio), 1); |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |