xref: /openbmc/u-boot/drivers/video/videomodes.c (revision 9b914727)
1 /*
2  * (C) Copyright 2004
3  * Pierre Aubert, Staubli Faverges , <p.aubert@staubli.com>
4  * Copyright 2011 Freescale Semiconductor, Inc.
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24 
25 /************************************************************************
26   Get Parameters for the video mode:
27   The default video mode can be defined in CONFIG_SYS_DEFAULT_VIDEO_MODE.
28   If undefined, default video mode is set to 0x301
29   Parameters can be set via the variable "videomode" in the environment.
30   2 diferent ways are possible:
31   "videomode=301"   - 301 is a hexadecimal number describing the VESA
32 		      mode. Following modes are implemented:
33 
34 		      Colors	640x480 800x600 1024x768 1152x864 1280x1024
35 		     --------+---------------------------------------------
36 		      8 bits |	0x301	0x303	 0x305	  0x161	    0x307
37 		     15 bits |	0x310	0x313	 0x316	  0x162	    0x319
38 		     16 bits |	0x311	0x314	 0x317	  0x163	    0x31A
39 		     24 bits |	0x312	0x315	 0x318	    ?	    0x31B
40 		     --------+---------------------------------------------
41   "videomode=bootargs"
42 		   - the parameters are parsed from the bootargs.
43 		      The format is "NAME:VALUE,NAME:VALUE" etc.
44 		      Ex.:
45 		      "bootargs=video=ctfb:x:800,y:600,depth:16,pclk:25000"
46 		      Parameters not included in the list will be taken from
47 		      the default mode, which is one of the following:
48 		      mode:0  640x480x24
49 		      mode:1  800x600x16
50 		      mode:2  1024x768x8
51 		      mode:3  960x720x24
52 		      mode:4  1152x864x16
53 		      mode:5  1280x1024x8
54 
55 		      if "mode" is not provided within the parameter list,
56 		      mode:0 is assumed.
57 		      Following parameters are supported:
58 		      x	      xres = visible resolution horizontal
59 		      y	      yres = visible resolution vertical
60 		      pclk    pixelclocks in pico sec
61 		      le      left_marging time from sync to picture in pixelclocks
62 		      ri      right_marging time from picture to sync in pixelclocks
63 		      up      upper_margin time from sync to picture
64 		      lo      lower_margin
65 		      hs      hsync_len length of horizontal sync
66 		      vs      vsync_len length of vertical sync
67 		      sync    see FB_SYNC_*
68 		      vmode   see FB_VMODE_*
69 		      depth   Color depth in bits per pixel
70 		      All other parameters in the variable bootargs are ignored.
71 		      It is also possible to set the parameters direct in the
72 		      variable "videomode", or in another variable i.e.
73 		      "myvideo" and setting the variable "videomode=myvideo"..
74 ****************************************************************************/
75 
76 #include <common.h>
77 #include <linux/ctype.h>
78 
79 #include "videomodes.h"
80 
81 const struct ctfb_vesa_modes vesa_modes[VESA_MODES_COUNT] = {
82 	{0x301, RES_MODE_640x480, 8},
83 	{0x310, RES_MODE_640x480, 15},
84 	{0x311, RES_MODE_640x480, 16},
85 	{0x312, RES_MODE_640x480, 24},
86 	{0x303, RES_MODE_800x600, 8},
87 	{0x313, RES_MODE_800x600, 15},
88 	{0x314, RES_MODE_800x600, 16},
89 	{0x315, RES_MODE_800x600, 24},
90 	{0x305, RES_MODE_1024x768, 8},
91 	{0x316, RES_MODE_1024x768, 15},
92 	{0x317, RES_MODE_1024x768, 16},
93 	{0x318, RES_MODE_1024x768, 24},
94 	{0x161, RES_MODE_1152x864, 8},
95 	{0x162, RES_MODE_1152x864, 15},
96 	{0x163, RES_MODE_1152x864, 16},
97 	{0x307, RES_MODE_1280x1024, 8},
98 	{0x319, RES_MODE_1280x1024, 15},
99 	{0x31A, RES_MODE_1280x1024, 16},
100 	{0x31B, RES_MODE_1280x1024, 24},
101 };
102 const struct ctfb_res_modes res_mode_init[RES_MODES_COUNT] = {
103 	/* x	 y pixclk   le	ri  up	lo   hs vs  s  vmode */
104 	{640, 480, 39721, 40, 24, 32, 11, 96, 2, 0, FB_VMODE_NONINTERLACED},
105 	{800, 600, 27778, 64, 24, 22, 1, 72, 2, 0, FB_VMODE_NONINTERLACED},
106 	{1024, 768, 15384, 168, 8, 29, 3, 144, 4, 0, FB_VMODE_NONINTERLACED},
107 	{960, 720, 13100, 160, 40, 32, 8, 80, 4, 0, FB_VMODE_NONINTERLACED},
108 	{1152, 864, 12004, 200, 64, 32, 16, 80, 4, 0, FB_VMODE_NONINTERLACED},
109 	{1280, 1024, 9090, 200, 48, 26, 1, 184, 3, 0, FB_VMODE_NONINTERLACED},
110 };
111 
112 /************************************************************************
113  * Get Parameters for the video mode:
114  */
115 /*********************************************************************
116  * returns the length to the next seperator
117  */
118 static int
119 video_get_param_len (char *start, char sep)
120 {
121 	int i = 0;
122 	while ((*start != 0) && (*start != sep)) {
123 		start++;
124 		i++;
125 	}
126 	return i;
127 }
128 
129 static int
130 video_search_param (char *start, char *param)
131 {
132 	int len, totallen, i;
133 	char *p = start;
134 	len = strlen (param);
135 	totallen = len + strlen (start);
136 	for (i = 0; i < totallen; i++) {
137 		if (strncmp (p++, param, len) == 0)
138 			return (i);
139 	}
140 	return -1;
141 }
142 
143 /***************************************************************
144  * Get parameter via the environment as it is done for the
145  * linux kernel i.e:
146  * video=ctfb:x:800,xv:1280,y:600,yv:1024,depth:16,mode:0,pclk:25000,
147  *	 le:56,ri:48,up:26,lo:5,hs:152,vs:2,sync:0,vmode:0,accel:0
148  *
149  * penv is a pointer to the environment, containing the string, or the name of
150  * another environment variable. It could even be the term "bootargs"
151  */
152 
153 #define GET_OPTION(name,var)				\
154 	if(strncmp(p,name,strlen(name))==0) {		\
155 		val_s=p+strlen(name);			\
156 		var=simple_strtoul(val_s, NULL, 10);	\
157 	}
158 
159 int video_get_params (struct ctfb_res_modes *pPar, char *penv)
160 {
161 	char *p, *s, *val_s;
162 	int i = 0;
163 	int bpp;
164 	int mode;
165 
166 	/* first search for the environment containing the real param string */
167 	s = penv;
168 
169 	if ((p = getenv (s)) != NULL)
170 		s = p;
171 
172 	/*
173 	 * in case of the bootargs line, we have to start
174 	 * after "video=ctfb:"
175 	 */
176 	i = video_search_param (s, "video=ctfb:");
177 	if (i >= 0) {
178 		s += i;
179 		s += strlen ("video=ctfb:");
180 	}
181 	/* search for mode as a default value */
182 	p = s;
183 	mode = 0;		/* default */
184 
185 	while ((i = video_get_param_len (p, ',')) != 0) {
186 		GET_OPTION ("mode:", mode)
187 			p += i;
188 		if (*p != 0)
189 			p++;	/* skip ',' */
190 	}
191 
192 	if (mode >= RES_MODES_COUNT)
193 		mode = 0;
194 
195 	*pPar = res_mode_init[mode];	/* copy default values */
196 	bpp = 24 - ((mode % 3) * 8);
197 	p = s;			/* restart */
198 
199 	while ((i = video_get_param_len (p, ',')) != 0) {
200 		GET_OPTION ("x:", pPar->xres)
201 			GET_OPTION ("y:", pPar->yres)
202 			GET_OPTION ("le:", pPar->left_margin)
203 			GET_OPTION ("ri:", pPar->right_margin)
204 			GET_OPTION ("up:", pPar->upper_margin)
205 			GET_OPTION ("lo:", pPar->lower_margin)
206 			GET_OPTION ("hs:", pPar->hsync_len)
207 			GET_OPTION ("vs:", pPar->vsync_len)
208 			GET_OPTION ("sync:", pPar->sync)
209 			GET_OPTION ("vmode:", pPar->vmode)
210 			GET_OPTION ("pclk:", pPar->pixclock)
211 			GET_OPTION ("depth:", bpp)
212 			p += i;
213 		if (*p != 0)
214 			p++;	/* skip ',' */
215 	}
216 	return bpp;
217 }
218 
219 /*
220  * Parse the 'video-mode' environment variable
221  *
222  * Example: "video-mode=fslfb:1280x1024-32@60,monitor=dvi".  See
223  * doc/README.video for more information on how to set the variable.
224  *
225  * @xres: returned value of X-resolution
226  * @yres: returned value of Y-resolution
227  * @depth: returned value of color depth
228  * @freq: returned value of monitor frequency
229  * @options: pointer to any remaining options, or NULL
230  *
231  * Returns 1 if valid values were found, 0 otherwise
232  */
233 int video_get_video_mode(unsigned int *xres, unsigned int *yres,
234 	unsigned int *depth, unsigned int *freq, const char **options)
235 {
236 	char *p = getenv("video-mode");
237 	if (!p)
238 		return 0;
239 
240 	/* Skip over the driver name, which we don't care about. */
241 	p = strchr(p, ':');
242 	if (!p)
243 		return 0;
244 
245 	/* Get the X-resolution*/
246 	while (*p && !isdigit(*p))
247 		p++;
248 	*xres = simple_strtoul(p, &p, 10);
249 	if (!*xres)
250 		return 0;
251 
252 	/* Get the Y-resolution */
253 	while (*p && !isdigit(*p))
254 		p++;
255 	*yres = simple_strtoul(p, &p, 10);
256 	if (!*yres)
257 		return 0;
258 
259 	/* Get the depth */
260 	while (*p && !isdigit(*p))
261 		p++;
262 	*depth = simple_strtoul(p, &p, 10);
263 	if (!*depth)
264 		return 0;
265 
266 	/* Get the frequency */
267 	while (*p && !isdigit(*p))
268 		p++;
269 	*freq = simple_strtoul(p, &p, 10);
270 	if (!*freq)
271 		return 0;
272 
273 	/* Find the extra options, if any */
274 	p = strchr(p, ',');
275 	*options = p ? p + 1 : NULL;
276 
277 	return 1;
278 }
279