Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 1 | /* |
Igor Grinberg | 811acf9 | 2013-04-22 01:06:53 +0000 | [diff] [blame] | 2 | * (C) Copyright 2012 - 2013 CompuLab, Ltd. <www.compulab.co.il> |
Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 3 | * |
| 4 | * Authors: Nikita Kiryanov <nikita@compulab.co.il> |
| 5 | * |
Nikita Kiryanov | 4fc4afa | 2013-01-30 21:39:59 +0000 | [diff] [blame] | 6 | * Parsing code based on linux/drivers/video/pxafb.c |
| 7 | * |
Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 8 | * See file CREDITS for list of people who contributed to this |
| 9 | * project. |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU General Public License as |
| 13 | * published by the Free Software Foundation; either version 2 of |
| 14 | * the License, or (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program; if not, write to the Free Software |
| 23 | * Foundation, Inc. |
| 24 | */ |
| 25 | #include <common.h> |
| 26 | #include <asm/gpio.h> |
| 27 | #include <asm/io.h> |
| 28 | #include <stdio_dev.h> |
| 29 | #include <asm/arch/dss.h> |
| 30 | #include <lcd.h> |
| 31 | #include <asm/arch-omap3/dss.h> |
| 32 | |
| 33 | DECLARE_GLOBAL_DATA_PTR; |
| 34 | |
| 35 | enum display_type { |
| 36 | NONE, |
| 37 | DVI, |
Nikita Kiryanov | 4fc4afa | 2013-01-30 21:39:59 +0000 | [diff] [blame] | 38 | DVI_CUSTOM, |
Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define CMAP_ADDR 0x80100000 |
| 42 | |
| 43 | /* |
| 44 | * The frame buffer is allocated before we have the chance to parse user input. |
| 45 | * To make sure enough memory is allocated for all resolutions, we define |
| 46 | * vl_{col | row} to the maximal resolution supported by OMAP3. |
| 47 | */ |
| 48 | vidinfo_t panel_info = { |
| 49 | .vl_col = 1400, |
| 50 | .vl_row = 1050, |
| 51 | .vl_bpix = LCD_BPP, |
| 52 | .cmap = (ushort *)CMAP_ADDR, |
| 53 | }; |
| 54 | |
| 55 | static struct panel_config panel_cfg; |
| 56 | static enum display_type lcd_def; |
| 57 | |
| 58 | /* |
| 59 | * A note on DVI presets; |
| 60 | * U-Boot can convert 8 bit BMP data to 16 bit BMP data, and OMAP DSS can |
| 61 | * convert 16 bit data into 24 bit data. Thus, GFXFORMAT_RGB16 allows us to |
| 62 | * support two BMP types with one setting. |
| 63 | */ |
| 64 | static const struct panel_config preset_dvi_640X480 = { |
| 65 | .lcd_size = PANEL_LCD_SIZE(640, 480), |
| 66 | .timing_h = DSS_HBP(48) | DSS_HFP(16) | DSS_HSW(96), |
| 67 | .timing_v = DSS_VBP(33) | DSS_VFP(10) | DSS_VSW(2), |
| 68 | .divisor = 12 | (1 << 16), |
| 69 | .data_lines = LCD_INTERFACE_24_BIT, |
| 70 | .panel_type = ACTIVE_DISPLAY, |
| 71 | .load_mode = 2, |
| 72 | .gfx_format = GFXFORMAT_RGB16, |
| 73 | }; |
| 74 | |
| 75 | static const struct panel_config preset_dvi_800X600 = { |
| 76 | .lcd_size = PANEL_LCD_SIZE(800, 600), |
| 77 | .timing_h = DSS_HBP(88) | DSS_HFP(40) | DSS_HSW(128), |
| 78 | .timing_v = DSS_VBP(23) | DSS_VFP(1) | DSS_VSW(4), |
| 79 | .divisor = 8 | (1 << 16), |
| 80 | .data_lines = LCD_INTERFACE_24_BIT, |
| 81 | .panel_type = ACTIVE_DISPLAY, |
| 82 | .load_mode = 2, |
| 83 | .gfx_format = GFXFORMAT_RGB16, |
| 84 | }; |
| 85 | |
| 86 | static const struct panel_config preset_dvi_1024X768 = { |
| 87 | .lcd_size = PANEL_LCD_SIZE(1024, 768), |
| 88 | .timing_h = DSS_HBP(160) | DSS_HFP(24) | DSS_HSW(136), |
| 89 | .timing_v = DSS_VBP(29) | DSS_VFP(3) | DSS_VSW(6), |
| 90 | .divisor = 5 | (1 << 16), |
| 91 | .data_lines = LCD_INTERFACE_24_BIT, |
| 92 | .panel_type = ACTIVE_DISPLAY, |
| 93 | .load_mode = 2, |
| 94 | .gfx_format = GFXFORMAT_RGB16, |
| 95 | }; |
| 96 | |
| 97 | static const struct panel_config preset_dvi_1152X864 = { |
| 98 | .lcd_size = PANEL_LCD_SIZE(1152, 864), |
| 99 | .timing_h = DSS_HBP(256) | DSS_HFP(64) | DSS_HSW(128), |
| 100 | .timing_v = DSS_VBP(32) | DSS_VFP(1) | DSS_VSW(3), |
| 101 | .divisor = 3 | (1 << 16), |
| 102 | .data_lines = LCD_INTERFACE_24_BIT, |
| 103 | .panel_type = ACTIVE_DISPLAY, |
| 104 | .load_mode = 2, |
| 105 | .gfx_format = GFXFORMAT_RGB16, |
| 106 | }; |
| 107 | |
| 108 | static const struct panel_config preset_dvi_1280X960 = { |
| 109 | .lcd_size = PANEL_LCD_SIZE(1280, 960), |
| 110 | .timing_h = DSS_HBP(312) | DSS_HFP(96) | DSS_HSW(112), |
| 111 | .timing_v = DSS_VBP(36) | DSS_VFP(1) | DSS_VSW(3), |
| 112 | .divisor = 3 | (1 << 16), |
| 113 | .data_lines = LCD_INTERFACE_24_BIT, |
| 114 | .panel_type = ACTIVE_DISPLAY, |
| 115 | .load_mode = 2, |
| 116 | .gfx_format = GFXFORMAT_RGB16, |
| 117 | }; |
| 118 | |
| 119 | static const struct panel_config preset_dvi_1280X1024 = { |
| 120 | .lcd_size = PANEL_LCD_SIZE(1280, 1024), |
| 121 | .timing_h = DSS_HBP(248) | DSS_HFP(48) | DSS_HSW(112), |
| 122 | .timing_v = DSS_VBP(38) | DSS_VFP(1) | DSS_VSW(3), |
| 123 | .divisor = 3 | (1 << 16), |
| 124 | .data_lines = LCD_INTERFACE_24_BIT, |
| 125 | .panel_type = ACTIVE_DISPLAY, |
| 126 | .load_mode = 2, |
| 127 | .gfx_format = GFXFORMAT_RGB16, |
| 128 | }; |
| 129 | |
| 130 | /* |
| 131 | * set_resolution_params() |
| 132 | * |
| 133 | * Due to usage of multiple display related APIs resolution data is located in |
| 134 | * more than one place. This function updates them all. |
| 135 | */ |
| 136 | static void set_resolution_params(int x, int y) |
| 137 | { |
| 138 | panel_cfg.lcd_size = PANEL_LCD_SIZE(x, y); |
| 139 | panel_info.vl_col = x; |
| 140 | panel_info.vl_row = y; |
| 141 | lcd_line_length = (panel_info.vl_col * NBITS(panel_info.vl_bpix)) / 8; |
| 142 | } |
| 143 | |
| 144 | static void set_preset(const struct panel_config preset, int x_res, int y_res) |
| 145 | { |
| 146 | panel_cfg = preset; |
| 147 | set_resolution_params(x_res, y_res); |
| 148 | } |
| 149 | |
| 150 | static enum display_type set_dvi_preset(const struct panel_config preset, |
| 151 | int x_res, int y_res) |
| 152 | { |
| 153 | set_preset(preset, x_res, y_res); |
| 154 | return DVI; |
| 155 | } |
| 156 | |
| 157 | /* |
Nikita Kiryanov | 4fc4afa | 2013-01-30 21:39:59 +0000 | [diff] [blame] | 158 | * parse_mode() - parse the mode parameter of custom lcd settings |
| 159 | * |
| 160 | * @mode: <res_x>x<res_y> |
| 161 | * |
| 162 | * Returns -1 on error, 0 on success. |
| 163 | */ |
| 164 | static int parse_mode(const char *mode) |
| 165 | { |
| 166 | unsigned int modelen = strlen(mode); |
| 167 | int res_specified = 0; |
| 168 | unsigned int xres = 0, yres = 0; |
| 169 | int yres_specified = 0; |
| 170 | int i; |
| 171 | |
| 172 | for (i = modelen - 1; i >= 0; i--) { |
| 173 | switch (mode[i]) { |
| 174 | case 'x': |
| 175 | if (!yres_specified) { |
| 176 | yres = simple_strtoul(&mode[i + 1], NULL, 0); |
| 177 | yres_specified = 1; |
| 178 | } else { |
| 179 | goto done_parsing; |
| 180 | } |
| 181 | |
| 182 | break; |
| 183 | case '0' ... '9': |
| 184 | break; |
| 185 | default: |
| 186 | goto done_parsing; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | if (i < 0 && yres_specified) { |
| 191 | xres = simple_strtoul(mode, NULL, 0); |
| 192 | res_specified = 1; |
| 193 | } |
| 194 | |
| 195 | done_parsing: |
| 196 | if (res_specified) { |
| 197 | set_resolution_params(xres, yres); |
| 198 | } else { |
| 199 | printf("LCD: invalid mode: %s\n", mode); |
| 200 | return -1; |
| 201 | } |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | #define PIXEL_CLK_NUMERATOR (26 * 432 / 39) |
| 207 | /* |
| 208 | * parse_pixclock() - Parse the pixclock parameter of custom lcd settings |
| 209 | * |
| 210 | * @pixclock: the desired pixel clock |
| 211 | * |
| 212 | * Returns -1 on error, 0 on success. |
| 213 | * |
| 214 | * Handling the pixel_clock: |
| 215 | * |
| 216 | * Pixel clock is defined in the OMAP35x TRM as follows: |
| 217 | * pixel_clock = |
| 218 | * (SYS_CLK * 2 * PRCM.CM_CLKSEL2_PLL[18:8]) / |
| 219 | * (DSS.DISPC_DIVISOR[23:16] * DSS.DISPC_DIVISOR[6:0] * |
| 220 | * PRCM.CM_CLKSEL_DSS[4:0] * (PRCM.CM_CLKSEL2_PLL[6:0] + 1)) |
| 221 | * |
| 222 | * In practice, this means that in order to set the |
| 223 | * divisor for the desired pixel clock one needs to |
| 224 | * solve the following equation: |
| 225 | * |
| 226 | * 26 * 432 / (39 * <pixel_clock>) = DSS.DISPC_DIVISOR[6:0] |
| 227 | * |
| 228 | * NOTE: the explicit equation above is reduced. Do not |
| 229 | * try to infer anything from these numbers. |
| 230 | */ |
| 231 | static int parse_pixclock(char *pixclock) |
| 232 | { |
| 233 | int divisor, pixclock_val; |
| 234 | char *pixclk_start = pixclock; |
| 235 | |
| 236 | pixclock_val = simple_strtoul(pixclock, &pixclock, 10); |
| 237 | divisor = DIV_ROUND_UP(PIXEL_CLK_NUMERATOR, pixclock_val); |
| 238 | /* 0 and 1 are illegal values for PCD */ |
| 239 | if (divisor <= 1) |
| 240 | divisor = 2; |
| 241 | |
| 242 | panel_cfg.divisor = divisor | (1 << 16); |
| 243 | if (pixclock[0] != '\0') { |
| 244 | printf("LCD: invalid value for pixclock:%s\n", pixclk_start); |
| 245 | return -1; |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* |
| 252 | * parse_setting() - parse a single setting of custom lcd parameters |
| 253 | * |
| 254 | * @setting: The custom lcd setting <name>:<value> |
| 255 | * |
| 256 | * Returns -1 on failure, 0 on success. |
| 257 | */ |
| 258 | static int parse_setting(char *setting) |
| 259 | { |
| 260 | int num_val; |
| 261 | char *setting_start = setting; |
| 262 | |
| 263 | if (!strncmp(setting, "mode:", 5)) { |
| 264 | return parse_mode(setting + 5); |
| 265 | } else if (!strncmp(setting, "pixclock:", 9)) { |
| 266 | return parse_pixclock(setting + 9); |
| 267 | } else if (!strncmp(setting, "left:", 5)) { |
| 268 | num_val = simple_strtoul(setting + 5, &setting, 0); |
| 269 | panel_cfg.timing_h |= DSS_HBP(num_val); |
| 270 | } else if (!strncmp(setting, "right:", 6)) { |
| 271 | num_val = simple_strtoul(setting + 6, &setting, 0); |
| 272 | panel_cfg.timing_h |= DSS_HFP(num_val); |
| 273 | } else if (!strncmp(setting, "upper:", 6)) { |
| 274 | num_val = simple_strtoul(setting + 6, &setting, 0); |
| 275 | panel_cfg.timing_v |= DSS_VBP(num_val); |
| 276 | } else if (!strncmp(setting, "lower:", 6)) { |
| 277 | num_val = simple_strtoul(setting + 6, &setting, 0); |
| 278 | panel_cfg.timing_v |= DSS_VFP(num_val); |
| 279 | } else if (!strncmp(setting, "hsynclen:", 9)) { |
| 280 | num_val = simple_strtoul(setting + 9, &setting, 0); |
| 281 | panel_cfg.timing_h |= DSS_HSW(num_val); |
| 282 | } else if (!strncmp(setting, "vsynclen:", 9)) { |
| 283 | num_val = simple_strtoul(setting + 9, &setting, 0); |
| 284 | panel_cfg.timing_v |= DSS_VSW(num_val); |
| 285 | } else if (!strncmp(setting, "hsync:", 6)) { |
| 286 | if (simple_strtoul(setting + 6, &setting, 0) == 0) |
| 287 | panel_cfg.pol_freq |= DSS_IHS; |
| 288 | else |
| 289 | panel_cfg.pol_freq &= ~DSS_IHS; |
| 290 | } else if (!strncmp(setting, "vsync:", 6)) { |
| 291 | if (simple_strtoul(setting + 6, &setting, 0) == 0) |
| 292 | panel_cfg.pol_freq |= DSS_IVS; |
| 293 | else |
| 294 | panel_cfg.pol_freq &= ~DSS_IVS; |
| 295 | } else if (!strncmp(setting, "outputen:", 9)) { |
| 296 | if (simple_strtoul(setting + 9, &setting, 0) == 0) |
| 297 | panel_cfg.pol_freq |= DSS_IEO; |
| 298 | else |
| 299 | panel_cfg.pol_freq &= ~DSS_IEO; |
| 300 | } else if (!strncmp(setting, "pixclockpol:", 12)) { |
| 301 | if (simple_strtoul(setting + 12, &setting, 0) == 0) |
| 302 | panel_cfg.pol_freq |= DSS_IPC; |
| 303 | else |
| 304 | panel_cfg.pol_freq &= ~DSS_IPC; |
| 305 | } else if (!strncmp(setting, "active", 6)) { |
| 306 | panel_cfg.panel_type = ACTIVE_DISPLAY; |
| 307 | return 0; /* Avoid sanity check below */ |
| 308 | } else if (!strncmp(setting, "passive", 7)) { |
| 309 | panel_cfg.panel_type = PASSIVE_DISPLAY; |
| 310 | return 0; /* Avoid sanity check below */ |
| 311 | } else if (!strncmp(setting, "display:", 8)) { |
| 312 | if (!strncmp(setting + 8, "dvi", 3)) { |
| 313 | lcd_def = DVI_CUSTOM; |
| 314 | return 0; /* Avoid sanity check below */ |
| 315 | } |
| 316 | } else { |
| 317 | printf("LCD: unknown option %s\n", setting_start); |
| 318 | return -1; |
| 319 | } |
| 320 | |
| 321 | if (setting[0] != '\0') { |
| 322 | printf("LCD: invalid value for %s\n", setting_start); |
| 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | return 0; |
| 327 | } |
| 328 | |
| 329 | /* |
| 330 | * env_parse_customlcd() - parse custom lcd params from an environment variable. |
| 331 | * |
| 332 | * @custom_lcd_params: The environment variable containing the lcd params. |
| 333 | * |
| 334 | * Returns -1 on failure, 0 on success. |
| 335 | */ |
| 336 | static int parse_customlcd(char *custom_lcd_params) |
| 337 | { |
| 338 | char params_cpy[160]; |
| 339 | char *setting; |
| 340 | |
| 341 | strncpy(params_cpy, custom_lcd_params, 160); |
| 342 | setting = strtok(params_cpy, ","); |
| 343 | while (setting) { |
| 344 | if (parse_setting(setting) < 0) |
| 345 | return -1; |
| 346 | |
| 347 | setting = strtok(NULL, ","); |
| 348 | } |
| 349 | |
| 350 | /* Currently we don't support changing this via custom lcd params */ |
| 351 | panel_cfg.data_lines = LCD_INTERFACE_24_BIT; |
| 352 | panel_cfg.gfx_format = GFXFORMAT_RGB16; /* See dvi predefines note */ |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | /* |
Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 358 | * env_parse_displaytype() - parse display type. |
| 359 | * |
| 360 | * Parses the environment variable "displaytype", which contains the |
| 361 | * name of the display type or preset, in which case it applies its |
| 362 | * configurations. |
| 363 | * |
| 364 | * Returns the type of display that was specified. |
| 365 | */ |
| 366 | static enum display_type env_parse_displaytype(char *displaytype) |
| 367 | { |
| 368 | if (!strncmp(displaytype, "dvi640x480", 10)) |
| 369 | return set_dvi_preset(preset_dvi_640X480, 640, 480); |
| 370 | else if (!strncmp(displaytype, "dvi800x600", 10)) |
| 371 | return set_dvi_preset(preset_dvi_800X600, 800, 600); |
| 372 | else if (!strncmp(displaytype, "dvi1024x768", 11)) |
| 373 | return set_dvi_preset(preset_dvi_1024X768, 1024, 768); |
| 374 | else if (!strncmp(displaytype, "dvi1152x864", 11)) |
| 375 | return set_dvi_preset(preset_dvi_1152X864, 1152, 864); |
| 376 | else if (!strncmp(displaytype, "dvi1280x960", 11)) |
| 377 | return set_dvi_preset(preset_dvi_1280X960, 1280, 960); |
| 378 | else if (!strncmp(displaytype, "dvi1280x1024", 12)) |
| 379 | return set_dvi_preset(preset_dvi_1280X1024, 1280, 1024); |
| 380 | |
| 381 | return NONE; |
| 382 | } |
| 383 | |
Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 384 | void lcd_ctrl_init(void *lcdbase) |
| 385 | { |
| 386 | struct prcm *prcm = (struct prcm *)PRCM_BASE; |
Nikita Kiryanov | 4fc4afa | 2013-01-30 21:39:59 +0000 | [diff] [blame] | 387 | char *custom_lcd; |
Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 388 | char *displaytype = getenv("displaytype"); |
| 389 | |
| 390 | if (displaytype == NULL) |
| 391 | return; |
| 392 | |
| 393 | lcd_def = env_parse_displaytype(displaytype); |
Nikita Kiryanov | 4fc4afa | 2013-01-30 21:39:59 +0000 | [diff] [blame] | 394 | /* If we did not recognize the preset, check if it's an env variable */ |
| 395 | if (lcd_def == NONE) { |
| 396 | custom_lcd = getenv(displaytype); |
| 397 | if (custom_lcd == NULL || parse_customlcd(custom_lcd) < 0) |
| 398 | return; |
| 399 | } |
Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 400 | |
| 401 | panel_cfg.frame_buffer = lcdbase; |
| 402 | omap3_dss_panel_config(&panel_cfg); |
| 403 | /* |
| 404 | * Pixel clock is defined with many divisions and only few |
| 405 | * multiplications of the system clock. Since DSS FCLK divisor is set |
| 406 | * to 16 by default, we need to set it to a smaller value, like 3 |
| 407 | * (chosen via trial and error). |
| 408 | */ |
| 409 | clrsetbits_le32(&prcm->clksel_dss, 0xF, 3); |
| 410 | } |
| 411 | |
| 412 | void lcd_enable(void) |
| 413 | { |
Nikita Kiryanov | 4fc4afa | 2013-01-30 21:39:59 +0000 | [diff] [blame] | 414 | if (lcd_def == DVI || lcd_def == DVI_CUSTOM) { |
Nikita Kiryanov | 7878ca5 | 2013-01-30 21:39:58 +0000 | [diff] [blame] | 415 | gpio_direction_output(54, 0); /* Turn on DVI */ |
| 416 | omap3_dss_enable(); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | void lcd_setcolreg(ushort regno, ushort red, ushort green, ushort blue) {} |