display.c 12 KB

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