videomodes.c 15 KB

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