videomodes.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /*
  2. * (C) Copyright 2004
  3. * Pierre Aubert, Staubli Faverges , <p.aubert@staubli.com>
  4. * Copyright 2011 Freescale Semiconductor, Inc.
  5. *
  6. * SPDX-License-Identifier: GPL-2.0+
  7. */
  8. /************************************************************************
  9. Get Parameters for the video mode:
  10. The default video mode can be defined in CONFIG_SYS_DEFAULT_VIDEO_MODE.
  11. If undefined, default video mode is set to 0x301
  12. Parameters can be set via the variable "videomode" in the environment.
  13. 2 diferent ways are possible:
  14. "videomode=301" - 301 is a hexadecimal number describing the VESA
  15. mode. Following modes are implemented:
  16. Colors 640x480 800x600 1024x768 1152x864 1280x1024
  17. --------+---------------------------------------------
  18. 8 bits | 0x301 0x303 0x305 0x161 0x307
  19. 15 bits | 0x310 0x313 0x316 0x162 0x319
  20. 16 bits | 0x311 0x314 0x317 0x163 0x31A
  21. 24 bits | 0x312 0x315 0x318 ? 0x31B
  22. --------+---------------------------------------------
  23. "videomode=bootargs"
  24. - the parameters are parsed from the bootargs.
  25. The format is "NAME:VALUE,NAME:VALUE" etc.
  26. Ex.:
  27. "bootargs=video=ctfb:x:800,y:600,depth:16,pclk:25000"
  28. Parameters not included in the list will be taken from
  29. the default mode, which is one of the following:
  30. mode:0 640x480x24
  31. mode:1 800x600x16
  32. mode:2 1024x768x8
  33. mode:3 960x720x24
  34. mode:4 1152x864x16
  35. mode:5 1280x1024x8
  36. if "mode" is not provided within the parameter list,
  37. mode:0 is assumed.
  38. Following parameters are supported:
  39. x xres = visible resolution horizontal
  40. y yres = visible resolution vertical
  41. pclk pixelclocks in pico sec
  42. le left_marging time from sync to picture in pixelclocks
  43. ri right_marging time from picture to sync in pixelclocks
  44. up upper_margin time from sync to picture
  45. lo lower_margin
  46. hs hsync_len length of horizontal sync
  47. vs vsync_len length of vertical sync
  48. sync see FB_SYNC_*
  49. vmode see FB_VMODE_*
  50. depth Color depth in bits per pixel
  51. All other parameters in the variable bootargs are ignored.
  52. It is also possible to set the parameters direct in the
  53. variable "videomode", or in another variable i.e.
  54. "myvideo" and setting the variable "videomode=myvideo"..
  55. ****************************************************************************/
  56. #include <common.h>
  57. #include <edid.h>
  58. #include <errno.h>
  59. #include <linux/ctype.h>
  60. #include "videomodes.h"
  61. const struct ctfb_vesa_modes vesa_modes[VESA_MODES_COUNT] = {
  62. {0x301, RES_MODE_640x480, 8},
  63. {0x310, RES_MODE_640x480, 15},
  64. {0x311, RES_MODE_640x480, 16},
  65. {0x312, RES_MODE_640x480, 24},
  66. {0x303, RES_MODE_800x600, 8},
  67. {0x313, RES_MODE_800x600, 15},
  68. {0x314, RES_MODE_800x600, 16},
  69. {0x315, RES_MODE_800x600, 24},
  70. {0x305, RES_MODE_1024x768, 8},
  71. {0x316, RES_MODE_1024x768, 15},
  72. {0x317, RES_MODE_1024x768, 16},
  73. {0x318, RES_MODE_1024x768, 24},
  74. {0x161, RES_MODE_1152x864, 8},
  75. {0x162, RES_MODE_1152x864, 15},
  76. {0x163, RES_MODE_1152x864, 16},
  77. {0x307, RES_MODE_1280x1024, 8},
  78. {0x319, RES_MODE_1280x1024, 15},
  79. {0x31A, RES_MODE_1280x1024, 16},
  80. {0x31B, RES_MODE_1280x1024, 24},
  81. };
  82. const struct ctfb_res_modes res_mode_init[RES_MODES_COUNT] = {
  83. /* x y hz pixclk ps/kHz le ri up lo hs vs s vmode */
  84. #ifndef CONFIG_VIDEO_STD_TIMINGS
  85. { 640, 480, 60, 39721, 25180, 40, 24, 32, 11, 96, 2, 0, FB_VMODE_NONINTERLACED},
  86. { 800, 600, 60, 27778, 36000, 64, 24, 22, 1, 72, 2, 0, FB_VMODE_NONINTERLACED},
  87. {1024, 768, 60, 15384, 65000, 168, 8, 29, 3, 144, 4, 0, FB_VMODE_NONINTERLACED},
  88. { 960, 720, 80, 13100, 76335, 160, 40, 32, 8, 80, 4, 0, FB_VMODE_NONINTERLACED},
  89. {1152, 864, 60, 12004, 83300, 200, 64, 32, 16, 80, 4, 0, FB_VMODE_NONINTERLACED},
  90. {1280, 1024, 60, 9090, 110000, 200, 48, 26, 1, 184, 3, 0, FB_VMODE_NONINTERLACED},
  91. #else
  92. { 640, 480, 60, 39683, 25200, 48, 16, 33, 10, 96, 2, 0, FB_VMODE_NONINTERLACED},
  93. { 800, 600, 60, 25000, 40000, 88, 40, 23, 1, 128, 4, FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED},
  94. {1024, 768, 60, 15384, 65000, 160, 24, 29, 3, 136, 6, 0, FB_VMODE_NONINTERLACED},
  95. { 960, 720, 75, 13468, 74250, 176, 72, 27, 1, 112, 2, 0, FB_VMODE_NONINTERLACED},
  96. {1152, 864, 75, 9259, 108000, 256, 64, 32, 1, 128, 3, FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED},
  97. {1280, 1024, 60, 9259, 108000, 248, 48, 38, 1, 112, 3, FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED},
  98. #endif
  99. {1280, 720, 60, 13468, 74250, 220, 110, 20, 5, 40, 5, FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED},
  100. {1360, 768, 60, 11696, 85500, 256, 64, 17, 3, 112, 7, 0, FB_VMODE_NONINTERLACED},
  101. {1920, 1080, 60, 6734, 148500, 148, 88, 36, 4, 44, 5, FB_SYNC_HOR_HIGH_ACT | FB_SYNC_VERT_HIGH_ACT, FB_VMODE_NONINTERLACED},
  102. {1920, 1200, 60, 6494, 154000, 80, 48, 26, 3, 32, 6, FB_SYNC_HOR_HIGH_ACT, FB_VMODE_NONINTERLACED},
  103. };
  104. /************************************************************************
  105. * Get Parameters for the video mode:
  106. */
  107. /*********************************************************************
  108. * returns the length to the next seperator
  109. */
  110. static int
  111. video_get_param_len(const char *start, char sep)
  112. {
  113. int i = 0;
  114. while ((*start != 0) && (*start != sep)) {
  115. start++;
  116. i++;
  117. }
  118. return i;
  119. }
  120. static int
  121. video_search_param (char *start, char *param)
  122. {
  123. int len, totallen, i;
  124. char *p = start;
  125. len = strlen (param);
  126. totallen = len + strlen (start);
  127. for (i = 0; i < totallen; i++) {
  128. if (strncmp (p++, param, len) == 0)
  129. return (i);
  130. }
  131. return -1;
  132. }
  133. /***************************************************************
  134. * Get parameter via the environment as it is done for the
  135. * linux kernel i.e:
  136. * video=ctfb:x:800,xv:1280,y:600,yv:1024,depth:16,mode:0,pclk:25000,
  137. * le:56,ri:48,up:26,lo:5,hs:152,vs:2,sync:0,vmode:0,accel:0
  138. *
  139. * penv is a pointer to the environment, containing the string, or the name of
  140. * another environment variable. It could even be the term "bootargs"
  141. */
  142. #define GET_OPTION(name,var) \
  143. if(strncmp(p,name,strlen(name))==0) { \
  144. val_s=p+strlen(name); \
  145. var=simple_strtoul(val_s, NULL, 10); \
  146. }
  147. int video_get_params (struct ctfb_res_modes *pPar, char *penv)
  148. {
  149. char *p, *s, *val_s;
  150. int i = 0;
  151. int bpp;
  152. int mode;
  153. /* first search for the environment containing the real param string */
  154. s = penv;
  155. if ((p = getenv (s)) != NULL)
  156. s = p;
  157. /*
  158. * in case of the bootargs line, we have to start
  159. * after "video=ctfb:"
  160. */
  161. i = video_search_param (s, "video=ctfb:");
  162. if (i >= 0) {
  163. s += i;
  164. s += strlen ("video=ctfb:");
  165. }
  166. /* search for mode as a default value */
  167. p = s;
  168. mode = 0; /* default */
  169. while ((i = video_get_param_len (p, ',')) != 0) {
  170. GET_OPTION ("mode:", mode)
  171. p += i;
  172. if (*p != 0)
  173. p++; /* skip ',' */
  174. }
  175. if (mode >= RES_MODES_COUNT)
  176. mode = 0;
  177. *pPar = res_mode_init[mode]; /* copy default values */
  178. bpp = 24 - ((mode % 3) * 8);
  179. p = s; /* restart */
  180. while ((i = video_get_param_len (p, ',')) != 0) {
  181. GET_OPTION ("x:", pPar->xres)
  182. GET_OPTION ("y:", pPar->yres)
  183. GET_OPTION ("refresh:", pPar->refresh)
  184. GET_OPTION ("le:", pPar->left_margin)
  185. GET_OPTION ("ri:", pPar->right_margin)
  186. GET_OPTION ("up:", pPar->upper_margin)
  187. GET_OPTION ("lo:", pPar->lower_margin)
  188. GET_OPTION ("hs:", pPar->hsync_len)
  189. GET_OPTION ("vs:", pPar->vsync_len)
  190. GET_OPTION ("sync:", pPar->sync)
  191. GET_OPTION ("vmode:", pPar->vmode)
  192. GET_OPTION ("pclk:", pPar->pixclock)
  193. GET_OPTION ("pclk_khz:", pPar->pixclock_khz)
  194. GET_OPTION ("depth:", bpp)
  195. p += i;
  196. if (*p != 0)
  197. p++; /* skip ',' */
  198. }
  199. return bpp;
  200. }
  201. /*
  202. * Parse the 'video-mode' environment variable
  203. *
  204. * Example: "video-mode=fslfb:1280x1024-32@60,monitor=dvi". See
  205. * doc/README.video for more information on how to set the variable.
  206. *
  207. * @xres: returned value of X-resolution
  208. * @yres: returned value of Y-resolution
  209. * @depth: returned value of color depth
  210. * @freq: returned value of monitor frequency
  211. * @options: pointer to any remaining options, or NULL
  212. *
  213. * Returns 1 if valid values were found, 0 otherwise
  214. */
  215. int video_get_video_mode(unsigned int *xres, unsigned int *yres,
  216. unsigned int *depth, unsigned int *freq, const char **options)
  217. {
  218. char *p = getenv("video-mode");
  219. if (!p)
  220. return 0;
  221. /* Skip over the driver name, which we don't care about. */
  222. p = strchr(p, ':');
  223. if (!p)
  224. return 0;
  225. /* Get the X-resolution*/
  226. while (*p && !isdigit(*p))
  227. p++;
  228. *xres = simple_strtoul(p, &p, 10);
  229. if (!*xres)
  230. return 0;
  231. /* Get the Y-resolution */
  232. while (*p && !isdigit(*p))
  233. p++;
  234. *yres = simple_strtoul(p, &p, 10);
  235. if (!*yres)
  236. return 0;
  237. /* Get the depth */
  238. while (*p && !isdigit(*p))
  239. p++;
  240. *depth = simple_strtoul(p, &p, 10);
  241. if (!*depth)
  242. return 0;
  243. /* Get the frequency */
  244. while (*p && !isdigit(*p))
  245. p++;
  246. *freq = simple_strtoul(p, &p, 10);
  247. if (!*freq)
  248. return 0;
  249. /* Find the extra options, if any */
  250. p = strchr(p, ',');
  251. *options = p ? p + 1 : NULL;
  252. return 1;
  253. }
  254. /*
  255. * Parse the 'video-mode' environment variable using video_get_video_mode()
  256. * and lookup the matching ctfb_res_modes in res_mode_init.
  257. *
  258. * @default_mode: RES_MODE_##x## define for the mode to store in mode_ret
  259. * when 'video-mode' is not set or does not contain a valid mode
  260. * @default_depth: depth to set when 'video-mode' is not set
  261. * @mode_ret: pointer where the mode will be stored
  262. * @depth_ret: pointer where the depth will be stored
  263. * @options: pointer to any remaining options, or NULL
  264. */
  265. void video_get_ctfb_res_modes(int default_mode, unsigned int default_depth,
  266. const struct ctfb_res_modes **mode_ret,
  267. unsigned int *depth_ret,
  268. const char **options)
  269. {
  270. unsigned int i, xres, yres, depth, refresh;
  271. *mode_ret = &res_mode_init[default_mode];
  272. *depth_ret = default_depth;
  273. *options = NULL;
  274. if (!video_get_video_mode(&xres, &yres, &depth, &refresh, options))
  275. return;
  276. for (i = 0; i < RES_MODES_COUNT; i++) {
  277. if (res_mode_init[i].xres == xres &&
  278. res_mode_init[i].yres == yres &&
  279. res_mode_init[i].refresh == refresh) {
  280. *mode_ret = &res_mode_init[i];
  281. *depth_ret = depth;
  282. return;
  283. }
  284. }
  285. printf("video-mode %dx%d-%d@%d not available, falling back to %dx%d-%d@%d\n",
  286. xres, yres, depth, refresh, (*mode_ret)->xres,
  287. (*mode_ret)->yres, *depth_ret, (*mode_ret)->refresh);
  288. }
  289. /*
  290. * Find the named string option within the ',' separated options string, and
  291. * store its value in dest.
  292. *
  293. * @options: ',' separated options string
  294. * @name: name of the option to look for
  295. * @dest: destination buffer to store the value of the option in
  296. * @dest_len: length of dest
  297. * @def: value to store in dest if the option is not present in options
  298. */
  299. void video_get_option_string(const char *options, const char *name,
  300. char *dest, int dest_len, const char *def)
  301. {
  302. const char *p = options;
  303. const int name_len = strlen(name);
  304. int i, len;
  305. while (p && (i = video_get_param_len(p, ',')) != 0) {
  306. if (strncmp(p, name, name_len) == 0 && p[name_len] == '=') {
  307. len = i - (name_len + 1);
  308. if (len >= dest_len)
  309. len = dest_len - 1;
  310. memcpy(dest, &p[name_len + 1], len);
  311. dest[len] = 0;
  312. return;
  313. }
  314. p += i;
  315. if (*p != 0)
  316. p++; /* skip ',' */
  317. }
  318. strcpy(dest, def);
  319. }
  320. /*
  321. * Find the named integer option within the ',' separated options string, and
  322. * return its value.
  323. *
  324. * @options: ',' separated options string
  325. * @name: name of the option to look for
  326. * @def: value to return if the option is not present in options
  327. */
  328. int video_get_option_int(const char *options, const char *name, int def)
  329. {
  330. const char *p = options;
  331. const int name_len = strlen(name);
  332. int i;
  333. while (p && (i = video_get_param_len(p, ',')) != 0) {
  334. if (strncmp(p, name, name_len) == 0 && p[name_len] == '=')
  335. return simple_strtoul(&p[name_len + 1], NULL, 10);
  336. p += i;
  337. if (*p != 0)
  338. p++; /* skip ',' */
  339. }
  340. return def;
  341. }
  342. /**
  343. * Convert an EDID detailed timing to a struct ctfb_res_modes
  344. *
  345. * @param t The EDID detailed timing to be converted
  346. * @param mode Returns the converted timing
  347. *
  348. * @return 0 on success, or a negative errno on error
  349. */
  350. int video_edid_dtd_to_ctfb_res_modes(struct edid_detailed_timing *t,
  351. struct ctfb_res_modes *mode)
  352. {
  353. int margin, h_total, v_total;
  354. /* Check all timings are non 0 */
  355. if (EDID_DETAILED_TIMING_PIXEL_CLOCK(*t) == 0 ||
  356. EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*t) == 0 ||
  357. EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*t) == 0 ||
  358. EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*t) == 0 ||
  359. EDID_DETAILED_TIMING_VERTICAL_BLANKING(*t) == 0 ||
  360. EDID_DETAILED_TIMING_HSYNC_OFFSET(*t) == 0 ||
  361. EDID_DETAILED_TIMING_HSYNC_PULSE_WIDTH(*t) == 0 ||
  362. EDID_DETAILED_TIMING_VSYNC_OFFSET(*t) == 0 ||
  363. EDID_DETAILED_TIMING_VSYNC_PULSE_WIDTH(*t) == 0 ||
  364. /* 3d formats are not supported*/
  365. EDID_DETAILED_TIMING_FLAG_STEREO(*t) != 0)
  366. return -EINVAL;
  367. mode->xres = EDID_DETAILED_TIMING_HORIZONTAL_ACTIVE(*t);
  368. mode->yres = EDID_DETAILED_TIMING_VERTICAL_ACTIVE(*t);
  369. h_total = mode->xres + EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*t);
  370. v_total = mode->yres + EDID_DETAILED_TIMING_VERTICAL_BLANKING(*t);
  371. mode->refresh = EDID_DETAILED_TIMING_PIXEL_CLOCK(*t) /
  372. (h_total * v_total);
  373. mode->pixclock_khz = EDID_DETAILED_TIMING_PIXEL_CLOCK(*t) / 1000;
  374. mode->pixclock = 1000000000L / mode->pixclock_khz;
  375. mode->right_margin = EDID_DETAILED_TIMING_HSYNC_OFFSET(*t);
  376. mode->hsync_len = EDID_DETAILED_TIMING_HSYNC_PULSE_WIDTH(*t);
  377. margin = EDID_DETAILED_TIMING_HORIZONTAL_BLANKING(*t) -
  378. (mode->right_margin + mode->hsync_len);
  379. if (margin <= 0)
  380. return -EINVAL;
  381. mode->left_margin = margin;
  382. mode->lower_margin = EDID_DETAILED_TIMING_VSYNC_OFFSET(*t);
  383. mode->vsync_len = EDID_DETAILED_TIMING_VSYNC_PULSE_WIDTH(*t);
  384. margin = EDID_DETAILED_TIMING_VERTICAL_BLANKING(*t) -
  385. (mode->lower_margin + mode->vsync_len);
  386. if (margin <= 0)
  387. return -EINVAL;
  388. mode->upper_margin = margin;
  389. mode->sync = 0;
  390. if (EDID_DETAILED_TIMING_FLAG_HSYNC_POLARITY(*t))
  391. mode->sync |= FB_SYNC_HOR_HIGH_ACT;
  392. if (EDID_DETAILED_TIMING_FLAG_VSYNC_POLARITY(*t))
  393. mode->sync |= FB_SYNC_VERT_HIGH_ACT;
  394. if (EDID_DETAILED_TIMING_FLAG_INTERLACED(*t))
  395. mode->vmode = FB_VMODE_INTERLACED;
  396. else
  397. mode->vmode = FB_VMODE_NONINTERLACED;
  398. return 0;
  399. }