videomode_lookup.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /****************************************************************************
  2. * video/videomode/videomode_lookup.c
  3. *
  4. * Copyright (C) 2019 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Derives from logic in FreeBSD which has an equivalent 3-clause BSD
  8. * license:
  9. *
  10. * Copyright (c) 2006 Itronix Inc. All rights reserved.
  11. * Written by Garrett D'Amore for Itronix Inc.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions
  15. * are met:
  16. *
  17. * 1. Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * 3. The name of Itronix Inc. may not be used to endorse
  23. * or promote products derived from this software without specific
  24. * prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND ANY EXPRESS
  27. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
  30. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  32. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  34. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  35. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  36. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <sys/types.h>
  44. #include <string.h>
  45. #include <debug.h>
  46. #include <nuttx/video/videomode.h>
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /* These macros help the mode lines below fit on one line. */
  51. #define HP VID_PHSYNC
  52. #define HN VID_NHSYNC
  53. #define VP VID_PVSYNC
  54. #define VN VID_NVSYNC
  55. #define I VID_INTERLACE
  56. #define DS VID_DBLSCAN
  57. #define M(nm,hr,vr,clk,hs,he,ht,vs,ve,vt,f) \
  58. { \
  59. clk, hr, hs, he, ht, vr, vs, ve, vt, 0, f, nm \
  60. }
  61. /****************************************************************************
  62. * Private Data
  63. ****************************************************************************/
  64. static const struct videomode_s g_videomodes[] =
  65. {
  66. M("640x350x85",
  67. 640, 350, 31500, 672, 736, 832, 382, 385, 445, HP | VN),
  68. M("640x400x85",
  69. 640, 400, 31500, 672, 736, 832, 401, 404, 445, HN | VP),
  70. M("720x400x70",
  71. 720, 400, 28320, 738, 846, 900, 412, 414, 449, HN | VP),
  72. M("720x400x85",
  73. 720, 400, 35500, 756, 828, 936, 401, 404, 446, HN | VP),
  74. M("720x400x87",
  75. 720, 400, 35500, 738, 846, 900, 421, 423, 449, HN | VN),
  76. M("640x480x60",
  77. 640, 480, 25175, 656, 752, 800, 490, 492, 525, HN | VN),
  78. M("640x480x72",
  79. 640, 480, 31500, 664, 704, 832, 489, 492, 520, HN | VN),
  80. M("640x480x75",
  81. 640, 480, 31500, 656, 720, 840, 481, 484, 500, HN | VN),
  82. M("640x480x85",
  83. 640, 480, 36000, 696, 752, 832, 481, 484, 509, HN | VN),
  84. M("800x600x56",
  85. 800, 600, 36000, 824, 896, 1024, 601, 603, 625, HP | VP),
  86. M("800x600x60",
  87. 800, 600, 40000, 840, 968, 1056, 601, 605, 628, HP | VP),
  88. M("800x600x72",
  89. 800, 600, 50000, 856, 976, 1040, 637, 643, 666, HP | VP),
  90. M("800x600x75",
  91. 800, 600, 49500, 816, 896, 1056, 601, 604, 625, HP | VP),
  92. M("800x600x85",
  93. 800, 600, 56250, 832, 896, 1048, 601, 604, 631, HP | VP),
  94. M("1024x768x87i",
  95. 1024, 768, 44900, 1032, 1208, 1264, 768, 776, 817, HP | VP | I),
  96. M("1024x768x60",
  97. 1024, 768, 65000, 1048, 1184, 1344, 771, 777, 806, HN | VN),
  98. M("1024x768x70",
  99. 1024, 768, 75000, 1048, 1184, 1328, 771, 777, 806, HN | VN),
  100. M("1024x768x75",
  101. 1024, 768, 78750, 1040, 1136, 1312, 769, 772, 800, HP | VP),
  102. M("1024x768x85",
  103. 1024, 768, 94500, 1072, 1168, 1376, 769, 772, 808, HP | VP),
  104. M("1024x768x89",
  105. 1024, 768, 100000, 1108, 1280, 1408, 768, 780, 796, HP | VP),
  106. M("1152x864x75",
  107. 1152, 864, 108000, 1216, 1344, 1600, 865, 868, 900, HP | VP),
  108. M("1280x768x75",
  109. 1280, 768, 105640, 1312, 1712, 1744, 782, 792, 807, HN | VP),
  110. M("1280x960x60",
  111. 1280, 960, 108000, 1376, 1488, 1800, 961, 964, 1000, HP | VP),
  112. M("1280x960x85",
  113. 1280, 960, 148500, 1344, 1504, 1728, 961, 964, 1011, HP | VP),
  114. M("1280x1024x60",
  115. 1280, 1024, 108000, 1328, 1440, 1688, 1025, 1028, 1066, HP | VP),
  116. M("1280x1024x70",
  117. 1280, 1024, 126000, 1328, 1440, 1688, 1025, 1028, 1066, HP | VP),
  118. M("1280x1024x75",
  119. 1280, 1024, 135000, 1296, 1440, 1688, 1025, 1028, 1066, HP | VP),
  120. M("1280x1024x85",
  121. 1280, 1024, 157500, 1344, 1504, 1728, 1025, 1028, 1072, HP | VP),
  122. M("1600x1200x60",
  123. 1600, 1200, 162000, 1664, 1856, 2160, 1201, 1204, 1250, HP | VP),
  124. M("1600x1200x65",
  125. 1600, 1200, 175500, 1664, 1856, 2160, 1201, 1204, 1250, HP | VP),
  126. M("1600x1200x70",
  127. 1600, 1200, 189000, 1664, 1856, 2160, 1201, 1204, 1250, HP | VP),
  128. M("1600x1200x75",
  129. 1600, 1200, 202500, 1664, 1856, 2160, 1201, 1204, 1250, HP | VP),
  130. M("1600x1200x85",
  131. 1600, 1200, 229500, 1664, 1856, 2160, 1201, 1204, 1250, HP | VP),
  132. M("1680x1050x60",
  133. 1680, 1050, 147140, 1784, 1968, 2256, 1051, 1054, 1087, HP | VP),
  134. M("1792x1344x60",
  135. 1792, 1344, 204800, 1920, 2120, 2448, 1345, 1348, 1394, HN | VP),
  136. M("1792x1344x75",
  137. 1792, 1344, 261000, 1888, 2104, 2456, 1345, 1348, 1417, HN | VP),
  138. M("1856x1392x60",
  139. 1856, 1392, 218300, 1952, 2176, 2528, 1393, 1396, 1439, HN | VP),
  140. M("1856x1392x75",
  141. 1856, 1392, 288000, 1984, 2208, 2560, 1393, 1396, 1500, HN | VP),
  142. M("1920x1440x60",
  143. 1920, 1440, 234000, 2048, 2256, 2600, 1441, 1444, 1500, HN | VP),
  144. M("1920x1440x75",
  145. 1920, 1440, 297000, 2064, 2288, 2640, 1441, 1444, 1500, HN | VP),
  146. M("832x624x74",
  147. 832, 624, 57284, 864, 928, 1152, 625, 628, 667, HN | VN),
  148. M("1152x768x54",
  149. 1152, 768, 64995, 1178, 1314, 1472, 771, 777, 806, HP | VP),
  150. M("1400x1050x60",
  151. 1400, 1050, 122000, 1488, 1640, 1880, 1052, 1064, 1082, HP | VP),
  152. M("1400x1050x74",
  153. 1400, 1050, 155800, 1464, 1784, 1912, 1052, 1064, 1090, HP | VP),
  154. M("1152x900x66",
  155. 1152, 900, 94500, 1192, 1320, 1528, 902, 906, 937, HN | VN),
  156. M("1152x900x76",
  157. 1152, 900, 105560, 1168, 1280, 1472, 902, 906, 943, HN | VN),
  158. /* Derived Double Scan Modes */
  159. M("320x175x85",
  160. 320, 175, 15750, 336, 368, 416, 191, 192, 222, HP | VN | DS),
  161. M("320x200x85",
  162. 320, 200, 15750, 336, 368, 416, 200, 202, 222, HN | VP | DS),
  163. M("360x200x70",
  164. 360, 200, 14160, 369, 423, 450, 206, 207, 224, HN | VP | DS),
  165. M("360x200x85",
  166. 360, 200, 17750, 378, 414, 468, 200, 202, 223, HN | VP | DS),
  167. M("360x200x87",
  168. 360, 200, 17750, 369, 423, 450, 210, 211, 224, HN | VN | DS),
  169. M("320x240x60",
  170. 320, 240, 12587, 328, 376, 400, 245, 246, 262, HN | VN | DS),
  171. M("320x240x72",
  172. 320, 240, 15750, 332, 352, 416, 244, 246, 260, HN | VN | DS),
  173. M("320x240x75",
  174. 320, 240, 15750, 328, 360, 420, 240, 242, 250, HN | VN | DS),
  175. M("320x240x85",
  176. 320, 240, 18000, 348, 376, 416, 240, 242, 254, HN | VN | DS),
  177. M("400x300x56",
  178. 400, 300, 18000, 412, 448, 512, 300, 301, 312, HP | VP | DS),
  179. M("400x300x60",
  180. 400, 300, 20000, 420, 484, 528, 300, 302, 314, HP | VP | DS),
  181. M("400x300x72",
  182. 400, 300, 25000, 428, 488, 520, 318, 321, 333, HP | VP | DS),
  183. M("400x300x75",
  184. 400, 300, 24750, 408, 448, 528, 300, 302, 312, HP | VP | DS),
  185. M("400x300x85",
  186. 400, 300, 28125, 416, 448, 524, 300, 302, 315, HP | VP | DS),
  187. M("512x384x87i",
  188. 512, 384, 22450, 516, 604, 632, 384, 388, 408, HP | VP | DS | I),
  189. M("512x384x60",
  190. 512, 384, 32500, 524, 592, 672, 385, 388, 403, HN | VN | DS),
  191. M("512x384x70",
  192. 512, 384, 37500, 524, 592, 664, 385, 388, 403, HN | VN | DS),
  193. M("512x384x75",
  194. 512, 384, 39375, 520, 568, 656, 384, 386, 400, HP | VP | DS),
  195. M("512x384x85",
  196. 512, 384, 47250, 536, 584, 688, 384, 386, 404, HP | VP | DS),
  197. M("512x384x89",
  198. 512, 384, 50000, 554, 640, 704, 384, 390, 398, HP | VP | DS),
  199. M("576x432x75",
  200. 576, 432, 54000, 608, 672, 800, 432, 434, 450, HP | VP | DS),
  201. M("640x384x75",
  202. 640, 384, 52820, 656, 856, 872, 391, 396, 403, HN | VP | DS),
  203. M("640x480x60",
  204. 640, 480, 54000, 688, 744, 900, 480, 482, 500, HP | VP | DS),
  205. M("640x480x85",
  206. 640, 480, 74250, 672, 752, 864, 480, 482, 505, HP | VP | DS),
  207. M("640x512x60",
  208. 640, 512, 54000, 664, 720, 844, 512, 514, 533, HP | VP | DS),
  209. M("640x512x70",
  210. 640, 512, 63000, 664, 720, 844, 512, 514, 533, HP | VP | DS),
  211. M("640x512x75",
  212. 640, 512, 67500, 648, 720, 844, 512, 514, 533, HP | VP | DS),
  213. M("640x512x85",
  214. 640, 512, 78750, 672, 752, 864, 512, 514, 536, HP | VP | DS),
  215. M("800x600x60",
  216. 800, 600, 81000, 832, 928, 1080, 600, 602, 625, HP | VP | DS),
  217. M("800x600x65",
  218. 800, 600, 87750, 832, 928, 1080, 600, 602, 625, HP | VP | DS),
  219. M("800x600x70",
  220. 800, 600, 94500, 832, 928, 1080, 600, 602, 625, HP | VP | DS),
  221. M("800x600x75",
  222. 800, 600, 101250, 832, 928, 1080, 600, 602, 625, HP | VP | DS),
  223. M("800x600x85",
  224. 800, 600, 114750, 832, 928, 1080, 600, 602, 625, HP | VP | DS),
  225. M("840x525x60",
  226. 840, 525, 73570, 892, 984, 1128, 525, 527, 543, HP | VP | DS),
  227. M("896x672x60",
  228. 896, 672, 102400, 960, 1060, 1224, 672, 674, 697, HN | VP | DS),
  229. M("896x672x75",
  230. 896, 672, 130500, 944, 1052, 1228, 672, 674, 708, HN | VP | DS),
  231. M("928x696x60",
  232. 928, 696, 109150, 976, 1088, 1264, 696, 698, 719, HN | VP | DS),
  233. M("928x696x75",
  234. 928, 696, 144000, 992, 1104, 1280, 696, 698, 750, HN | VP | DS),
  235. M("960x720x60",
  236. 960, 720, 117000, 1024, 1128, 1300, 720, 722, 750, HN | VP | DS),
  237. M("960x720x75",
  238. 960, 720, 148500, 1032, 1144, 1320, 720, 722, 750, HN | VP | DS),
  239. M("416x312x74",
  240. 416, 312, 28642, 432, 464, 576, 312, 314, 333, HN | VN | DS),
  241. M("576x384x54",
  242. 576, 384, 32497, 589, 657, 736, 385, 388, 403, HP | VP | DS),
  243. M("700x525x60",
  244. 700, 525, 61000, 744, 820, 940, 526, 532, 541, HP | VP | DS),
  245. M("700x525x74",
  246. 700, 525, 77900, 732, 892, 956, 526, 532, 545, HP | VP | DS),
  247. M("576x450x66",
  248. 576, 450, 47250, 596, 660, 764, 451, 453, 468, HN | VN | DS),
  249. M("576x450x76",
  250. 576, 450, 52780, 584, 640, 736, 451, 453, 471, HN | VN | DS),
  251. };
  252. static const int g_nvideomodes = 46;
  253. /****************************************************************************
  254. * Public Functions
  255. ****************************************************************************/
  256. /****************************************************************************
  257. * Name: videomode_lookup_by_name
  258. *
  259. * Description:
  260. * Find the video mode in a look-up table by the name assigned to the
  261. * video mode.
  262. *
  263. ****************************************************************************/
  264. FAR const struct videomode_s *videomode_lookup_by_name(FAR const char *name)
  265. {
  266. int i;
  267. for (i = 0; i < g_nvideomodes; i++)
  268. {
  269. if (strcmp(name, g_videomodes[i].name) == 0)
  270. {
  271. return &g_videomodes[i];
  272. }
  273. }
  274. return NULL;
  275. }
  276. /****************************************************************************
  277. * Name: videomode_lookup_by_dotclock
  278. *
  279. * Description:
  280. * Find the video mode in a look-up table with the matching width and
  281. * height and the closest dot clock that does not exceed the requested
  282. * dot clock.
  283. *
  284. ****************************************************************************/
  285. #if 0 /* Not used */
  286. FAR const struct videomode_s *
  287. videomode_lookup_by_dotclock(uint16_t width, uint16_t height,
  288. uint32_t dotclock)
  289. {
  290. FAR const struct videomode_s *curr;
  291. FAR const struct videomode_s *best = NULL;
  292. int i;
  293. lcdinfo("Looking for %u x %u at up to %lu kHz\n",
  294. width, height, (unsigned long)dotclock);
  295. for (i = 0; i < g_nvideomodes; i++)
  296. {
  297. curr = &g_videomodes[i];
  298. if (curr->hdisplay == width && curr->vdisplay == height &&
  299. curr->dotclock <= dotclock)
  300. {
  301. if (best != NULL)
  302. {
  303. if (curr->dotclock > best->dotclock)
  304. {
  305. best = curr;
  306. }
  307. }
  308. }
  309. else
  310. {
  311. best = curr;
  312. }
  313. }
  314. if (best != NULL)
  315. {
  316. lcdinfo("Found %s\n", best->name);
  317. }
  318. return best;
  319. }
  320. #endif
  321. /****************************************************************************
  322. * Name: videomode_lookup_by_refresh
  323. *
  324. * Description:
  325. * Find the video mode in a look-up table with the matching width and
  326. * height and the closest refresh rate that does not exceed the requested
  327. * rate.
  328. *
  329. ****************************************************************************/
  330. #if 0 /* Not used */
  331. FAR const struct videomode_s *
  332. videomode_lookup_by_refresh(uint16_t width, uint16_t height,
  333. uint16_t refresh)
  334. {
  335. FAR const struct videomode_s *curr;
  336. FAR const struct videomode_s *best = NULL;
  337. uint32_t mref;
  338. int closest = 1000;
  339. int diff;
  340. int i;
  341. lcdinfo("Looking for %u x %u at up to %u Hz\n",
  342. width, height, refresh);
  343. for (i = 0; i < g_nvideomodes; i++)
  344. {
  345. curr = &g_videomodes[i];
  346. if (curr->hdisplay == width && curr->vdisplay == height)
  347. {
  348. mref = curr->dotclock * 1000 / (curr->htotal * curr->vtotal);
  349. diff = mref - (uint32_t)refresh;
  350. if (diff < 0)
  351. {
  352. diff = -diff;
  353. }
  354. lcdinfo("%s in %lu Hz, diff %d\n",
  355. curr->name, (unsigned long)mref, diff);
  356. if (best != NULL)
  357. {
  358. if (diff < closest)
  359. {
  360. best = curr;
  361. closest = diff;
  362. }
  363. }
  364. else
  365. {
  366. best = curr;
  367. closest = diff;
  368. }
  369. }
  370. }
  371. if (best != NULL)
  372. {
  373. lcdinfo("Found %s %lu\n",
  374. best->name, (unsigned long)best->dotclock);
  375. }
  376. return best;
  377. }
  378. #endif