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