]>
Commit | Line | Data |
---|---|---|
785b93ef DA |
1 | /* |
2 | * Copyright (c) 2006-2009 Red Hat Inc. | |
3 | * Copyright (c) 2006-2008 Intel Corporation | |
4 | * Copyright (c) 2007 Dave Airlie <airlied@linux.ie> | |
5 | * | |
6 | * DRM framebuffer helper functions | |
7 | * | |
8 | * Permission to use, copy, modify, distribute, and sell this software and its | |
9 | * documentation for any purpose is hereby granted without fee, provided that | |
10 | * the above copyright notice appear in all copies and that both that copyright | |
11 | * notice and this permission notice appear in supporting documentation, and | |
12 | * that the name of the copyright holders not be used in advertising or | |
13 | * publicity pertaining to distribution of the software without specific, | |
14 | * written prior permission. The copyright holders make no representations | |
15 | * about the suitability of this software for any purpose. It is provided "as | |
16 | * is" without express or implied warranty. | |
17 | * | |
18 | * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
19 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
20 | * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
21 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
22 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
23 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE | |
24 | * OF THIS SOFTWARE. | |
25 | * | |
26 | * Authors: | |
27 | * Dave Airlie <airlied@linux.ie> | |
28 | * Jesse Barnes <jesse.barnes@intel.com> | |
29 | */ | |
3b40a443 | 30 | #include <linux/kernel.h> |
785b93ef DA |
31 | #include <linux/sysrq.h> |
32 | #include <linux/fb.h> | |
33 | #include "drmP.h" | |
34 | #include "drm_crtc.h" | |
35 | #include "drm_fb_helper.h" | |
36 | #include "drm_crtc_helper.h" | |
37 | ||
6fcefd56 DA |
38 | MODULE_AUTHOR("David Airlie, Jesse Barnes"); |
39 | MODULE_DESCRIPTION("DRM KMS helper"); | |
40 | MODULE_LICENSE("GPL and additional rights"); | |
41 | ||
785b93ef DA |
42 | static LIST_HEAD(kernel_fb_helper_list); |
43 | ||
0b4c0f3f DA |
44 | /* simple single crtc case helper function */ |
45 | int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper) | |
d50ba256 | 46 | { |
0b4c0f3f DA |
47 | struct drm_device *dev = fb_helper->dev; |
48 | struct drm_connector *connector; | |
49 | int i; | |
50 | ||
51 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
52 | struct drm_fb_helper_connector *fb_helper_connector; | |
53 | ||
54 | fb_helper_connector = kzalloc(sizeof(struct drm_fb_helper_connector), GFP_KERNEL); | |
55 | if (!fb_helper_connector) | |
56 | goto fail; | |
d50ba256 | 57 | |
0b4c0f3f DA |
58 | fb_helper_connector->connector = connector; |
59 | fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector; | |
60 | } | |
d50ba256 | 61 | return 0; |
0b4c0f3f DA |
62 | fail: |
63 | for (i = 0; i < fb_helper->connector_count; i++) { | |
64 | kfree(fb_helper->connector_info[i]); | |
65 | fb_helper->connector_info[i] = NULL; | |
66 | } | |
67 | fb_helper->connector_count = 0; | |
68 | return -ENOMEM; | |
d50ba256 | 69 | } |
0b4c0f3f | 70 | EXPORT_SYMBOL(drm_fb_helper_single_add_all_connectors); |
d50ba256 | 71 | |
d50ba256 DA |
72 | /** |
73 | * drm_fb_helper_connector_parse_command_line - parse command line for connector | |
74 | * @connector - connector to parse line for | |
75 | * @mode_option - per connector mode option | |
76 | * | |
77 | * This parses the connector specific then generic command lines for | |
78 | * modes and options to configure the connector. | |
79 | * | |
80 | * This uses the same parameters as the fb modedb.c, except for extra | |
81 | * <xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd] | |
82 | * | |
83 | * enable/enable Digital/disable bit at the end | |
84 | */ | |
0b4c0f3f | 85 | static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn, |
d50ba256 DA |
86 | const char *mode_option) |
87 | { | |
88 | const char *name; | |
89 | unsigned int namelen; | |
90 | int res_specified = 0, bpp_specified = 0, refresh_specified = 0; | |
91 | unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0; | |
92 | int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0; | |
93 | int i; | |
94 | enum drm_connector_force force = DRM_FORCE_UNSPECIFIED; | |
8ef8678c | 95 | struct drm_fb_helper_cmdline_mode *cmdline_mode; |
0b4c0f3f | 96 | struct drm_connector *connector = fb_helper_conn->connector; |
d50ba256 | 97 | |
0b4c0f3f | 98 | if (!fb_helper_conn) |
8ef8678c DA |
99 | return false; |
100 | ||
0b4c0f3f | 101 | cmdline_mode = &fb_helper_conn->cmdline_mode; |
d50ba256 DA |
102 | if (!mode_option) |
103 | mode_option = fb_mode_option; | |
104 | ||
105 | if (!mode_option) { | |
106 | cmdline_mode->specified = false; | |
107 | return false; | |
108 | } | |
109 | ||
110 | name = mode_option; | |
111 | namelen = strlen(name); | |
112 | for (i = namelen-1; i >= 0; i--) { | |
113 | switch (name[i]) { | |
114 | case '@': | |
115 | namelen = i; | |
116 | if (!refresh_specified && !bpp_specified && | |
117 | !yres_specified) { | |
3b40a443 | 118 | refresh = simple_strtol(&name[i+1], NULL, 10); |
d50ba256 DA |
119 | refresh_specified = 1; |
120 | if (cvt || rb) | |
121 | cvt = 0; | |
122 | } else | |
123 | goto done; | |
124 | break; | |
125 | case '-': | |
126 | namelen = i; | |
127 | if (!bpp_specified && !yres_specified) { | |
3b40a443 | 128 | bpp = simple_strtol(&name[i+1], NULL, 10); |
d50ba256 DA |
129 | bpp_specified = 1; |
130 | if (cvt || rb) | |
131 | cvt = 0; | |
132 | } else | |
133 | goto done; | |
134 | break; | |
135 | case 'x': | |
136 | if (!yres_specified) { | |
3b40a443 | 137 | yres = simple_strtol(&name[i+1], NULL, 10); |
d50ba256 DA |
138 | yres_specified = 1; |
139 | } else | |
140 | goto done; | |
141 | case '0' ... '9': | |
142 | break; | |
143 | case 'M': | |
144 | if (!yres_specified) | |
145 | cvt = 1; | |
146 | break; | |
147 | case 'R': | |
148 | if (!cvt) | |
149 | rb = 1; | |
150 | break; | |
151 | case 'm': | |
152 | if (!cvt) | |
153 | margins = 1; | |
154 | break; | |
155 | case 'i': | |
156 | if (!cvt) | |
157 | interlace = 1; | |
158 | break; | |
159 | case 'e': | |
160 | force = DRM_FORCE_ON; | |
161 | break; | |
162 | case 'D': | |
e89a8c90 | 163 | if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) && |
d50ba256 DA |
164 | (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB)) |
165 | force = DRM_FORCE_ON; | |
166 | else | |
167 | force = DRM_FORCE_ON_DIGITAL; | |
168 | break; | |
169 | case 'd': | |
170 | force = DRM_FORCE_OFF; | |
171 | break; | |
172 | default: | |
173 | goto done; | |
174 | } | |
175 | } | |
176 | if (i < 0 && yres_specified) { | |
3b40a443 | 177 | xres = simple_strtol(name, NULL, 10); |
d50ba256 DA |
178 | res_specified = 1; |
179 | } | |
180 | done: | |
181 | ||
182 | DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n", | |
183 | drm_get_connector_name(connector), xres, yres, | |
184 | (refresh) ? refresh : 60, (rb) ? " reduced blanking" : | |
185 | "", (margins) ? " with margins" : "", (interlace) ? | |
186 | " interlaced" : ""); | |
187 | ||
188 | if (force) { | |
189 | const char *s; | |
190 | switch (force) { | |
191 | case DRM_FORCE_OFF: s = "OFF"; break; | |
192 | case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break; | |
193 | default: | |
194 | case DRM_FORCE_ON: s = "ON"; break; | |
195 | } | |
196 | ||
197 | DRM_INFO("forcing %s connector %s\n", | |
198 | drm_get_connector_name(connector), s); | |
199 | connector->force = force; | |
200 | } | |
201 | ||
202 | if (res_specified) { | |
203 | cmdline_mode->specified = true; | |
204 | cmdline_mode->xres = xres; | |
205 | cmdline_mode->yres = yres; | |
206 | } | |
207 | ||
208 | if (refresh_specified) { | |
209 | cmdline_mode->refresh_specified = true; | |
210 | cmdline_mode->refresh = refresh; | |
211 | } | |
212 | ||
213 | if (bpp_specified) { | |
214 | cmdline_mode->bpp_specified = true; | |
215 | cmdline_mode->bpp = bpp; | |
216 | } | |
217 | cmdline_mode->rb = rb ? true : false; | |
218 | cmdline_mode->cvt = cvt ? true : false; | |
219 | cmdline_mode->interlace = interlace ? true : false; | |
220 | ||
221 | return true; | |
222 | } | |
223 | ||
0b4c0f3f | 224 | static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper) |
d50ba256 | 225 | { |
0b4c0f3f DA |
226 | struct drm_fb_helper_connector *fb_helper_conn; |
227 | int i; | |
d50ba256 | 228 | |
0b4c0f3f | 229 | for (i = 0; i < fb_helper->connector_count; i++) { |
d50ba256 DA |
230 | char *option = NULL; |
231 | ||
0b4c0f3f DA |
232 | fb_helper_conn = fb_helper->connector_info[i]; |
233 | ||
d50ba256 | 234 | /* do something on return - turn off connector maybe */ |
0b4c0f3f | 235 | if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option)) |
d50ba256 DA |
236 | continue; |
237 | ||
0b4c0f3f | 238 | drm_fb_helper_connector_parse_command_line(fb_helper_conn, option); |
d50ba256 DA |
239 | } |
240 | return 0; | |
241 | } | |
242 | ||
785b93ef DA |
243 | bool drm_fb_helper_force_kernel_mode(void) |
244 | { | |
245 | int i = 0; | |
246 | bool ret, error = false; | |
247 | struct drm_fb_helper *helper; | |
248 | ||
249 | if (list_empty(&kernel_fb_helper_list)) | |
250 | return false; | |
251 | ||
252 | list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) { | |
253 | for (i = 0; i < helper->crtc_count; i++) { | |
254 | struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set; | |
255 | ret = drm_crtc_helper_set_config(mode_set); | |
256 | if (ret) | |
257 | error = true; | |
258 | } | |
259 | } | |
260 | return error; | |
261 | } | |
262 | ||
263 | int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed, | |
264 | void *panic_str) | |
265 | { | |
266 | DRM_ERROR("panic occurred, switching back to text console\n"); | |
267 | return drm_fb_helper_force_kernel_mode(); | |
268 | return 0; | |
269 | } | |
270 | EXPORT_SYMBOL(drm_fb_helper_panic); | |
271 | ||
272 | static struct notifier_block paniced = { | |
273 | .notifier_call = drm_fb_helper_panic, | |
274 | }; | |
275 | ||
276 | /** | |
277 | * drm_fb_helper_restore - restore the framebuffer console (kernel) config | |
278 | * | |
279 | * Restore's the kernel's fbcon mode, used for lastclose & panic paths. | |
280 | */ | |
281 | void drm_fb_helper_restore(void) | |
282 | { | |
283 | bool ret; | |
284 | ret = drm_fb_helper_force_kernel_mode(); | |
285 | if (ret == true) | |
286 | DRM_ERROR("Failed to restore crtc configuration\n"); | |
287 | } | |
288 | EXPORT_SYMBOL(drm_fb_helper_restore); | |
289 | ||
bea1d35b | 290 | #ifdef CONFIG_MAGIC_SYSRQ |
785b93ef DA |
291 | static void drm_fb_helper_restore_work_fn(struct work_struct *ignored) |
292 | { | |
293 | drm_fb_helper_restore(); | |
294 | } | |
295 | static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn); | |
296 | ||
297 | static void drm_fb_helper_sysrq(int dummy1, struct tty_struct *dummy3) | |
298 | { | |
299 | schedule_work(&drm_fb_helper_restore_work); | |
300 | } | |
301 | ||
302 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { | |
303 | .handler = drm_fb_helper_sysrq, | |
304 | .help_msg = "force-fb(V)", | |
305 | .action_msg = "Restore framebuffer console", | |
306 | }; | |
b8c40d62 RD |
307 | #else |
308 | static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { }; | |
bea1d35b | 309 | #endif |
785b93ef DA |
310 | |
311 | static void drm_fb_helper_on(struct fb_info *info) | |
312 | { | |
313 | struct drm_fb_helper *fb_helper = info->par; | |
314 | struct drm_device *dev = fb_helper->dev; | |
315 | struct drm_crtc *crtc; | |
8be48d92 | 316 | struct drm_crtc_helper_funcs *crtc_funcs; |
785b93ef DA |
317 | struct drm_encoder *encoder; |
318 | int i; | |
319 | ||
320 | /* | |
321 | * For each CRTC in this fb, turn the crtc on then, | |
322 | * find all associated encoders and turn them on. | |
323 | */ | |
8be48d92 | 324 | mutex_lock(&dev->mode_config.mutex); |
e87b2c42 | 325 | for (i = 0; i < fb_helper->crtc_count; i++) { |
8be48d92 DA |
326 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
327 | crtc_funcs = crtc->helper_private; | |
785b93ef | 328 | |
8be48d92 DA |
329 | if (!crtc->enabled) |
330 | continue; | |
331 | ||
332 | crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON); | |
785b93ef | 333 | |
785b93ef | 334 | |
8be48d92 DA |
335 | /* Found a CRTC on this fb, now find encoders */ |
336 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
337 | if (encoder->crtc == crtc) { | |
338 | struct drm_encoder_helper_funcs *encoder_funcs; | |
785b93ef | 339 | |
8be48d92 DA |
340 | encoder_funcs = encoder->helper_private; |
341 | encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON); | |
785b93ef DA |
342 | } |
343 | } | |
344 | } | |
8be48d92 | 345 | mutex_unlock(&dev->mode_config.mutex); |
785b93ef DA |
346 | } |
347 | ||
348 | static void drm_fb_helper_off(struct fb_info *info, int dpms_mode) | |
349 | { | |
350 | struct drm_fb_helper *fb_helper = info->par; | |
351 | struct drm_device *dev = fb_helper->dev; | |
352 | struct drm_crtc *crtc; | |
8be48d92 | 353 | struct drm_crtc_helper_funcs *crtc_funcs; |
785b93ef DA |
354 | struct drm_encoder *encoder; |
355 | int i; | |
356 | ||
357 | /* | |
358 | * For each CRTC in this fb, find all associated encoders | |
359 | * and turn them off, then turn off the CRTC. | |
360 | */ | |
8be48d92 | 361 | mutex_lock(&dev->mode_config.mutex); |
e87b2c42 | 362 | for (i = 0; i < fb_helper->crtc_count; i++) { |
8be48d92 DA |
363 | crtc = fb_helper->crtc_info[i].mode_set.crtc; |
364 | crtc_funcs = crtc->helper_private; | |
e87b2c42 | 365 | |
8be48d92 DA |
366 | if (!crtc->enabled) |
367 | continue; | |
e87b2c42 | 368 | |
8be48d92 DA |
369 | /* Found a CRTC on this fb, now find encoders */ |
370 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
371 | if (encoder->crtc == crtc) { | |
372 | struct drm_encoder_helper_funcs *encoder_funcs; | |
e87b2c42 | 373 | |
8be48d92 DA |
374 | encoder_funcs = encoder->helper_private; |
375 | encoder_funcs->dpms(encoder, dpms_mode); | |
e87b2c42 | 376 | } |
785b93ef | 377 | } |
8be48d92 | 378 | crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF); |
785b93ef | 379 | } |
8be48d92 | 380 | mutex_unlock(&dev->mode_config.mutex); |
785b93ef DA |
381 | } |
382 | ||
383 | int drm_fb_helper_blank(int blank, struct fb_info *info) | |
384 | { | |
385 | switch (blank) { | |
731b5a15 | 386 | /* Display: On; HSync: On, VSync: On */ |
785b93ef DA |
387 | case FB_BLANK_UNBLANK: |
388 | drm_fb_helper_on(info); | |
389 | break; | |
731b5a15 | 390 | /* Display: Off; HSync: On, VSync: On */ |
785b93ef | 391 | case FB_BLANK_NORMAL: |
5fd4df4d | 392 | drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY); |
785b93ef | 393 | break; |
731b5a15 | 394 | /* Display: Off; HSync: Off, VSync: On */ |
785b93ef DA |
395 | case FB_BLANK_HSYNC_SUSPEND: |
396 | drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY); | |
397 | break; | |
731b5a15 | 398 | /* Display: Off; HSync: On, VSync: Off */ |
785b93ef DA |
399 | case FB_BLANK_VSYNC_SUSPEND: |
400 | drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND); | |
401 | break; | |
731b5a15 | 402 | /* Display: Off; HSync: Off, VSync: Off */ |
785b93ef DA |
403 | case FB_BLANK_POWERDOWN: |
404 | drm_fb_helper_off(info, DRM_MODE_DPMS_OFF); | |
405 | break; | |
406 | } | |
407 | return 0; | |
408 | } | |
409 | EXPORT_SYMBOL(drm_fb_helper_blank); | |
410 | ||
411 | static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper) | |
412 | { | |
413 | int i; | |
414 | ||
0b4c0f3f DA |
415 | for (i = 0; i < helper->connector_count; i++) |
416 | kfree(helper->connector_info[i]); | |
417 | kfree(helper->connector_info); | |
785b93ef DA |
418 | for (i = 0; i < helper->crtc_count; i++) |
419 | kfree(helper->crtc_info[i].mode_set.connectors); | |
420 | kfree(helper->crtc_info); | |
421 | } | |
422 | ||
8be48d92 DA |
423 | int drm_fb_helper_init_crtc_count(struct drm_device *dev, |
424 | struct drm_fb_helper *helper, | |
425 | int crtc_count, int max_conn_count) | |
785b93ef | 426 | { |
785b93ef DA |
427 | struct drm_crtc *crtc; |
428 | int ret = 0; | |
429 | int i; | |
430 | ||
8be48d92 DA |
431 | INIT_LIST_HEAD(&helper->kernel_fb_list); |
432 | helper->dev = dev; | |
785b93ef DA |
433 | helper->crtc_info = kcalloc(crtc_count, sizeof(struct drm_fb_helper_crtc), GFP_KERNEL); |
434 | if (!helper->crtc_info) | |
435 | return -ENOMEM; | |
785b93ef DA |
436 | helper->crtc_count = crtc_count; |
437 | ||
0b4c0f3f DA |
438 | helper->connector_info = kcalloc(dev->mode_config.num_connector, sizeof(struct drm_fb_helper_connector *), GFP_KERNEL); |
439 | if (!helper->connector_info) { | |
440 | kfree(helper->crtc_info); | |
441 | return -ENOMEM; | |
442 | } | |
443 | helper->connector_count = 0; | |
444 | ||
785b93ef DA |
445 | for (i = 0; i < crtc_count; i++) { |
446 | helper->crtc_info[i].mode_set.connectors = | |
447 | kcalloc(max_conn_count, | |
448 | sizeof(struct drm_connector *), | |
449 | GFP_KERNEL); | |
450 | ||
451 | if (!helper->crtc_info[i].mode_set.connectors) { | |
452 | ret = -ENOMEM; | |
453 | goto out_free; | |
454 | } | |
455 | helper->crtc_info[i].mode_set.num_connectors = 0; | |
456 | } | |
457 | ||
458 | i = 0; | |
459 | list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { | |
460 | helper->crtc_info[i].crtc_id = crtc->base.id; | |
461 | helper->crtc_info[i].mode_set.crtc = crtc; | |
462 | i++; | |
463 | } | |
464 | helper->conn_limit = max_conn_count; | |
465 | return 0; | |
466 | out_free: | |
467 | drm_fb_helper_crtc_free(helper); | |
468 | return -ENOMEM; | |
469 | } | |
470 | EXPORT_SYMBOL(drm_fb_helper_init_crtc_count); | |
471 | ||
c850cb78 | 472 | static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green, |
b8c00ac5 DA |
473 | u16 blue, u16 regno, struct fb_info *info) |
474 | { | |
475 | struct drm_fb_helper *fb_helper = info->par; | |
476 | struct drm_framebuffer *fb = fb_helper->fb; | |
477 | int pindex; | |
478 | ||
c850cb78 DA |
479 | if (info->fix.visual == FB_VISUAL_TRUECOLOR) { |
480 | u32 *palette; | |
481 | u32 value; | |
482 | /* place color in psuedopalette */ | |
483 | if (regno > 16) | |
484 | return -EINVAL; | |
485 | palette = (u32 *)info->pseudo_palette; | |
486 | red >>= (16 - info->var.red.length); | |
487 | green >>= (16 - info->var.green.length); | |
488 | blue >>= (16 - info->var.blue.length); | |
489 | value = (red << info->var.red.offset) | | |
490 | (green << info->var.green.offset) | | |
491 | (blue << info->var.blue.offset); | |
492 | palette[regno] = value; | |
493 | return 0; | |
494 | } | |
495 | ||
b8c00ac5 DA |
496 | pindex = regno; |
497 | ||
498 | if (fb->bits_per_pixel == 16) { | |
499 | pindex = regno << 3; | |
500 | ||
501 | if (fb->depth == 16 && regno > 63) | |
c850cb78 | 502 | return -EINVAL; |
b8c00ac5 | 503 | if (fb->depth == 15 && regno > 31) |
c850cb78 | 504 | return -EINVAL; |
b8c00ac5 DA |
505 | |
506 | if (fb->depth == 16) { | |
507 | u16 r, g, b; | |
508 | int i; | |
509 | if (regno < 32) { | |
510 | for (i = 0; i < 8; i++) | |
511 | fb_helper->funcs->gamma_set(crtc, red, | |
512 | green, blue, pindex + i); | |
513 | } | |
514 | ||
515 | fb_helper->funcs->gamma_get(crtc, &r, | |
516 | &g, &b, | |
517 | pindex >> 1); | |
518 | ||
519 | for (i = 0; i < 4; i++) | |
520 | fb_helper->funcs->gamma_set(crtc, r, | |
521 | green, b, | |
522 | (pindex >> 1) + i); | |
523 | } | |
524 | } | |
525 | ||
526 | if (fb->depth != 16) | |
527 | fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex); | |
c850cb78 | 528 | return 0; |
b8c00ac5 DA |
529 | } |
530 | ||
068143d3 DA |
531 | int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info) |
532 | { | |
533 | struct drm_fb_helper *fb_helper = info->par; | |
8be48d92 | 534 | struct drm_crtc_helper_funcs *crtc_funcs; |
068143d3 DA |
535 | u16 *red, *green, *blue, *transp; |
536 | struct drm_crtc *crtc; | |
537 | int i, rc = 0; | |
538 | int start; | |
539 | ||
8be48d92 DA |
540 | for (i = 0; i < fb_helper->crtc_count; i++) { |
541 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
542 | crtc_funcs = crtc->helper_private; | |
068143d3 DA |
543 | |
544 | red = cmap->red; | |
545 | green = cmap->green; | |
546 | blue = cmap->blue; | |
547 | transp = cmap->transp; | |
548 | start = cmap->start; | |
549 | ||
550 | for (i = 0; i < cmap->len; i++) { | |
551 | u16 hred, hgreen, hblue, htransp = 0xffff; | |
552 | ||
553 | hred = *red++; | |
554 | hgreen = *green++; | |
555 | hblue = *blue++; | |
556 | ||
557 | if (transp) | |
558 | htransp = *transp++; | |
559 | ||
c850cb78 DA |
560 | rc = setcolreg(crtc, hred, hgreen, hblue, start++, info); |
561 | if (rc) | |
562 | return rc; | |
068143d3 DA |
563 | } |
564 | crtc_funcs->load_lut(crtc); | |
565 | } | |
566 | return rc; | |
567 | } | |
568 | EXPORT_SYMBOL(drm_fb_helper_setcmap); | |
569 | ||
785b93ef DA |
570 | int drm_fb_helper_setcolreg(unsigned regno, |
571 | unsigned red, | |
572 | unsigned green, | |
573 | unsigned blue, | |
574 | unsigned transp, | |
575 | struct fb_info *info) | |
576 | { | |
577 | struct drm_fb_helper *fb_helper = info->par; | |
785b93ef | 578 | struct drm_crtc *crtc; |
8be48d92 | 579 | struct drm_crtc_helper_funcs *crtc_funcs; |
785b93ef | 580 | int i; |
c850cb78 | 581 | int ret; |
785b93ef | 582 | |
b8c00ac5 DA |
583 | if (regno > 255) |
584 | return 1; | |
785b93ef | 585 | |
8be48d92 DA |
586 | for (i = 0; i < fb_helper->crtc_count; i++) { |
587 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
588 | crtc_funcs = crtc->helper_private; | |
785b93ef | 589 | |
c850cb78 DA |
590 | ret = setcolreg(crtc, red, green, blue, regno, info); |
591 | if (ret) | |
592 | return ret; | |
785b93ef | 593 | |
b8c00ac5 | 594 | crtc_funcs->load_lut(crtc); |
785b93ef DA |
595 | } |
596 | return 0; | |
597 | } | |
598 | EXPORT_SYMBOL(drm_fb_helper_setcolreg); | |
599 | ||
600 | int drm_fb_helper_check_var(struct fb_var_screeninfo *var, | |
601 | struct fb_info *info) | |
602 | { | |
603 | struct drm_fb_helper *fb_helper = info->par; | |
604 | struct drm_framebuffer *fb = fb_helper->fb; | |
605 | int depth; | |
606 | ||
5349ef31 | 607 | if (var->pixclock != 0) |
785b93ef DA |
608 | return -EINVAL; |
609 | ||
610 | /* Need to resize the fb object !!! */ | |
509c7d83 DA |
611 | if (var->bits_per_pixel > fb->bits_per_pixel || var->xres > fb->width || var->yres > fb->height) { |
612 | DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb " | |
613 | "object %dx%d-%d > %dx%d-%d\n", var->xres, var->yres, var->bits_per_pixel, | |
614 | fb->width, fb->height, fb->bits_per_pixel); | |
785b93ef DA |
615 | return -EINVAL; |
616 | } | |
617 | ||
618 | switch (var->bits_per_pixel) { | |
619 | case 16: | |
620 | depth = (var->green.length == 6) ? 16 : 15; | |
621 | break; | |
622 | case 32: | |
623 | depth = (var->transp.length > 0) ? 32 : 24; | |
624 | break; | |
625 | default: | |
626 | depth = var->bits_per_pixel; | |
627 | break; | |
628 | } | |
629 | ||
630 | switch (depth) { | |
631 | case 8: | |
632 | var->red.offset = 0; | |
633 | var->green.offset = 0; | |
634 | var->blue.offset = 0; | |
635 | var->red.length = 8; | |
636 | var->green.length = 8; | |
637 | var->blue.length = 8; | |
638 | var->transp.length = 0; | |
639 | var->transp.offset = 0; | |
640 | break; | |
641 | case 15: | |
642 | var->red.offset = 10; | |
643 | var->green.offset = 5; | |
644 | var->blue.offset = 0; | |
645 | var->red.length = 5; | |
646 | var->green.length = 5; | |
647 | var->blue.length = 5; | |
648 | var->transp.length = 1; | |
649 | var->transp.offset = 15; | |
650 | break; | |
651 | case 16: | |
652 | var->red.offset = 11; | |
653 | var->green.offset = 5; | |
654 | var->blue.offset = 0; | |
655 | var->red.length = 5; | |
656 | var->green.length = 6; | |
657 | var->blue.length = 5; | |
658 | var->transp.length = 0; | |
659 | var->transp.offset = 0; | |
660 | break; | |
661 | case 24: | |
662 | var->red.offset = 16; | |
663 | var->green.offset = 8; | |
664 | var->blue.offset = 0; | |
665 | var->red.length = 8; | |
666 | var->green.length = 8; | |
667 | var->blue.length = 8; | |
668 | var->transp.length = 0; | |
669 | var->transp.offset = 0; | |
670 | break; | |
671 | case 32: | |
672 | var->red.offset = 16; | |
673 | var->green.offset = 8; | |
674 | var->blue.offset = 0; | |
675 | var->red.length = 8; | |
676 | var->green.length = 8; | |
677 | var->blue.length = 8; | |
678 | var->transp.length = 8; | |
679 | var->transp.offset = 24; | |
680 | break; | |
681 | default: | |
682 | return -EINVAL; | |
683 | } | |
684 | return 0; | |
685 | } | |
686 | EXPORT_SYMBOL(drm_fb_helper_check_var); | |
687 | ||
688 | /* this will let fbcon do the mode init */ | |
689 | int drm_fb_helper_set_par(struct fb_info *info) | |
690 | { | |
691 | struct drm_fb_helper *fb_helper = info->par; | |
692 | struct drm_device *dev = fb_helper->dev; | |
693 | struct fb_var_screeninfo *var = &info->var; | |
694 | struct drm_crtc *crtc; | |
695 | int ret; | |
696 | int i; | |
697 | ||
5349ef31 | 698 | if (var->pixclock != 0) { |
172e91f5 | 699 | DRM_ERROR("PIXEL CLOCK SET\n"); |
785b93ef DA |
700 | return -EINVAL; |
701 | } | |
702 | ||
8be48d92 DA |
703 | mutex_lock(&dev->mode_config.mutex); |
704 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
705 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
0b4c0f3f DA |
706 | ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set); |
707 | if (ret) { | |
708 | mutex_unlock(&dev->mode_config.mutex); | |
709 | return ret; | |
785b93ef DA |
710 | } |
711 | } | |
8be48d92 | 712 | mutex_unlock(&dev->mode_config.mutex); |
785b93ef DA |
713 | return 0; |
714 | } | |
715 | EXPORT_SYMBOL(drm_fb_helper_set_par); | |
716 | ||
717 | int drm_fb_helper_pan_display(struct fb_var_screeninfo *var, | |
718 | struct fb_info *info) | |
719 | { | |
720 | struct drm_fb_helper *fb_helper = info->par; | |
721 | struct drm_device *dev = fb_helper->dev; | |
722 | struct drm_mode_set *modeset; | |
723 | struct drm_crtc *crtc; | |
724 | int ret = 0; | |
725 | int i; | |
726 | ||
8be48d92 DA |
727 | mutex_lock(&dev->mode_config.mutex); |
728 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
729 | crtc = fb_helper->crtc_info[i].mode_set.crtc; | |
785b93ef DA |
730 | |
731 | modeset = &fb_helper->crtc_info[i].mode_set; | |
732 | ||
733 | modeset->x = var->xoffset; | |
734 | modeset->y = var->yoffset; | |
735 | ||
736 | if (modeset->num_connectors) { | |
785b93ef | 737 | ret = crtc->funcs->set_config(modeset); |
785b93ef DA |
738 | if (!ret) { |
739 | info->var.xoffset = var->xoffset; | |
740 | info->var.yoffset = var->yoffset; | |
741 | } | |
742 | } | |
743 | } | |
8be48d92 | 744 | mutex_unlock(&dev->mode_config.mutex); |
785b93ef DA |
745 | return ret; |
746 | } | |
747 | EXPORT_SYMBOL(drm_fb_helper_pan_display); | |
748 | ||
8be48d92 DA |
749 | int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper, |
750 | int preferred_bpp) | |
785b93ef | 751 | { |
785b93ef DA |
752 | int new_fb = 0; |
753 | int crtc_count = 0; | |
8be48d92 | 754 | int ret, i; |
785b93ef | 755 | struct fb_info *info; |
38651674 | 756 | struct drm_fb_helper_surface_size sizes; |
8be48d92 | 757 | int gamma_size = 0; |
38651674 DA |
758 | |
759 | memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size)); | |
760 | sizes.surface_depth = 24; | |
761 | sizes.surface_bpp = 32; | |
762 | sizes.fb_width = (unsigned)-1; | |
763 | sizes.fb_height = (unsigned)-1; | |
785b93ef | 764 | |
b8c00ac5 DA |
765 | /* if driver picks 8 or 16 by default use that |
766 | for both depth/bpp */ | |
38651674 DA |
767 | if (preferred_bpp != sizes.surface_bpp) { |
768 | sizes.surface_depth = sizes.surface_bpp = preferred_bpp; | |
b8c00ac5 | 769 | } |
785b93ef | 770 | /* first up get a count of crtcs now in use and new min/maxes width/heights */ |
0b4c0f3f DA |
771 | for (i = 0; i < fb_helper->connector_count; i++) { |
772 | struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i]; | |
8ef8678c DA |
773 | struct drm_fb_helper_cmdline_mode *cmdline_mode; |
774 | ||
0b4c0f3f | 775 | cmdline_mode = &fb_helper_conn->cmdline_mode; |
d50ba256 DA |
776 | |
777 | if (cmdline_mode->bpp_specified) { | |
778 | switch (cmdline_mode->bpp) { | |
779 | case 8: | |
38651674 | 780 | sizes.surface_depth = sizes.surface_bpp = 8; |
d50ba256 DA |
781 | break; |
782 | case 15: | |
38651674 DA |
783 | sizes.surface_depth = 15; |
784 | sizes.surface_bpp = 16; | |
d50ba256 DA |
785 | break; |
786 | case 16: | |
38651674 | 787 | sizes.surface_depth = sizes.surface_bpp = 16; |
d50ba256 DA |
788 | break; |
789 | case 24: | |
38651674 | 790 | sizes.surface_depth = sizes.surface_bpp = 24; |
d50ba256 DA |
791 | break; |
792 | case 32: | |
38651674 DA |
793 | sizes.surface_depth = 24; |
794 | sizes.surface_bpp = 32; | |
d50ba256 DA |
795 | break; |
796 | } | |
797 | break; | |
798 | } | |
799 | } | |
800 | ||
8be48d92 DA |
801 | crtc_count = 0; |
802 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
803 | struct drm_display_mode *desired_mode; | |
804 | desired_mode = fb_helper->crtc_info[i].desired_mode; | |
805 | ||
806 | if (desired_mode) { | |
807 | if (gamma_size == 0) | |
808 | gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size; | |
809 | if (desired_mode->hdisplay < sizes.fb_width) | |
810 | sizes.fb_width = desired_mode->hdisplay; | |
811 | if (desired_mode->vdisplay < sizes.fb_height) | |
812 | sizes.fb_height = desired_mode->vdisplay; | |
813 | if (desired_mode->hdisplay > sizes.surface_width) | |
814 | sizes.surface_width = desired_mode->hdisplay; | |
815 | if (desired_mode->vdisplay > sizes.surface_height) | |
816 | sizes.surface_height = desired_mode->vdisplay; | |
785b93ef DA |
817 | crtc_count++; |
818 | } | |
819 | } | |
820 | ||
38651674 | 821 | if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) { |
785b93ef DA |
822 | /* hmm everyone went away - assume VGA cable just fell out |
823 | and will come back later. */ | |
19b4b445 DA |
824 | DRM_ERROR("Cannot find any crtc or sizes - going 1024x768\n"); |
825 | sizes.fb_width = sizes.surface_width = 1024; | |
826 | sizes.fb_height = sizes.surface_height = 768; | |
785b93ef DA |
827 | } |
828 | ||
38651674 | 829 | /* push down into drivers */ |
8be48d92 | 830 | new_fb = (*fb_helper->fb_probe)(fb_helper, &sizes); |
38651674 DA |
831 | if (new_fb < 0) |
832 | return new_fb; | |
785b93ef | 833 | |
38651674 | 834 | info = fb_helper->fbdev; |
785b93ef | 835 | |
8be48d92 DA |
836 | /* set the fb pointer */ |
837 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
838 | fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb; | |
785b93ef | 839 | } |
785b93ef DA |
840 | |
841 | if (new_fb) { | |
5349ef31 | 842 | info->var.pixclock = 0; |
8be48d92 | 843 | ret = fb_alloc_cmap(&info->cmap, gamma_size, 0); |
3bea21b6 CL |
844 | if (ret) |
845 | return ret; | |
846 | if (register_framebuffer(info) < 0) { | |
847 | fb_dealloc_cmap(&info->cmap); | |
785b93ef | 848 | return -EINVAL; |
3bea21b6 | 849 | } |
38651674 DA |
850 | |
851 | printk(KERN_INFO "fb%d: %s frame buffer device\n", info->node, | |
852 | info->fix.id); | |
853 | ||
785b93ef DA |
854 | } else { |
855 | drm_fb_helper_set_par(info); | |
856 | } | |
785b93ef DA |
857 | |
858 | /* Switch back to kernel console on panic */ | |
859 | /* multi card linked list maybe */ | |
860 | if (list_empty(&kernel_fb_helper_list)) { | |
861 | printk(KERN_INFO "registered panic notifier\n"); | |
862 | atomic_notifier_chain_register(&panic_notifier_list, | |
863 | &paniced); | |
864 | register_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); | |
865 | } | |
38651674 DA |
866 | if (new_fb) |
867 | list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list); | |
868 | ||
785b93ef DA |
869 | return 0; |
870 | } | |
871 | EXPORT_SYMBOL(drm_fb_helper_single_fb_probe); | |
872 | ||
873 | void drm_fb_helper_free(struct drm_fb_helper *helper) | |
874 | { | |
8be48d92 DA |
875 | if (!list_empty(&helper->kernel_fb_list)) { |
876 | list_del(&helper->kernel_fb_list); | |
877 | if (list_empty(&kernel_fb_helper_list)) { | |
878 | printk(KERN_INFO "unregistered panic notifier\n"); | |
879 | atomic_notifier_chain_unregister(&panic_notifier_list, | |
880 | &paniced); | |
881 | unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op); | |
882 | } | |
785b93ef DA |
883 | } |
884 | drm_fb_helper_crtc_free(helper); | |
8be48d92 DA |
885 | if (helper->fbdev->cmap.len) |
886 | fb_dealloc_cmap(&helper->fbdev->cmap); | |
785b93ef DA |
887 | } |
888 | EXPORT_SYMBOL(drm_fb_helper_free); | |
889 | ||
068143d3 DA |
890 | void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch, |
891 | uint32_t depth) | |
785b93ef DA |
892 | { |
893 | info->fix.type = FB_TYPE_PACKED_PIXELS; | |
068143d3 | 894 | info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR : |
c850cb78 | 895 | FB_VISUAL_TRUECOLOR; |
785b93ef DA |
896 | info->fix.type_aux = 0; |
897 | info->fix.xpanstep = 1; /* doing it in hw */ | |
898 | info->fix.ypanstep = 1; /* doing it in hw */ | |
899 | info->fix.ywrapstep = 0; | |
3420e742 | 900 | info->fix.accel = FB_ACCEL_NONE; |
785b93ef DA |
901 | info->fix.type_aux = 0; |
902 | ||
903 | info->fix.line_length = pitch; | |
904 | return; | |
905 | } | |
906 | EXPORT_SYMBOL(drm_fb_helper_fill_fix); | |
907 | ||
38651674 | 908 | void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper, |
785b93ef DA |
909 | uint32_t fb_width, uint32_t fb_height) |
910 | { | |
38651674 DA |
911 | struct drm_framebuffer *fb = fb_helper->fb; |
912 | info->pseudo_palette = fb_helper->pseudo_palette; | |
785b93ef DA |
913 | info->var.xres_virtual = fb->width; |
914 | info->var.yres_virtual = fb->height; | |
915 | info->var.bits_per_pixel = fb->bits_per_pixel; | |
916 | info->var.xoffset = 0; | |
917 | info->var.yoffset = 0; | |
918 | info->var.activate = FB_ACTIVATE_NOW; | |
919 | info->var.height = -1; | |
920 | info->var.width = -1; | |
921 | ||
922 | switch (fb->depth) { | |
923 | case 8: | |
924 | info->var.red.offset = 0; | |
925 | info->var.green.offset = 0; | |
926 | info->var.blue.offset = 0; | |
927 | info->var.red.length = 8; /* 8bit DAC */ | |
928 | info->var.green.length = 8; | |
929 | info->var.blue.length = 8; | |
930 | info->var.transp.offset = 0; | |
931 | info->var.transp.length = 0; | |
932 | break; | |
933 | case 15: | |
934 | info->var.red.offset = 10; | |
935 | info->var.green.offset = 5; | |
936 | info->var.blue.offset = 0; | |
937 | info->var.red.length = 5; | |
938 | info->var.green.length = 5; | |
939 | info->var.blue.length = 5; | |
940 | info->var.transp.offset = 15; | |
941 | info->var.transp.length = 1; | |
942 | break; | |
943 | case 16: | |
944 | info->var.red.offset = 11; | |
945 | info->var.green.offset = 5; | |
946 | info->var.blue.offset = 0; | |
947 | info->var.red.length = 5; | |
948 | info->var.green.length = 6; | |
949 | info->var.blue.length = 5; | |
950 | info->var.transp.offset = 0; | |
951 | break; | |
952 | case 24: | |
953 | info->var.red.offset = 16; | |
954 | info->var.green.offset = 8; | |
955 | info->var.blue.offset = 0; | |
956 | info->var.red.length = 8; | |
957 | info->var.green.length = 8; | |
958 | info->var.blue.length = 8; | |
959 | info->var.transp.offset = 0; | |
960 | info->var.transp.length = 0; | |
961 | break; | |
962 | case 32: | |
963 | info->var.red.offset = 16; | |
964 | info->var.green.offset = 8; | |
965 | info->var.blue.offset = 0; | |
966 | info->var.red.length = 8; | |
967 | info->var.green.length = 8; | |
968 | info->var.blue.length = 8; | |
969 | info->var.transp.offset = 24; | |
970 | info->var.transp.length = 8; | |
971 | break; | |
972 | default: | |
973 | break; | |
974 | } | |
975 | ||
976 | info->var.xres = fb_width; | |
977 | info->var.yres = fb_height; | |
978 | } | |
979 | EXPORT_SYMBOL(drm_fb_helper_fill_var); | |
38651674 | 980 | |
0b4c0f3f DA |
981 | static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper, |
982 | uint32_t maxX, | |
983 | uint32_t maxY) | |
38651674 DA |
984 | { |
985 | struct drm_connector *connector; | |
986 | int count = 0; | |
0b4c0f3f | 987 | int i; |
38651674 | 988 | |
0b4c0f3f DA |
989 | for (i = 0; i < fb_helper->connector_count; i++) { |
990 | connector = fb_helper->connector_info[i]->connector; | |
38651674 DA |
991 | count += connector->funcs->fill_modes(connector, maxX, maxY); |
992 | } | |
993 | ||
994 | return count; | |
995 | } | |
996 | ||
0b4c0f3f | 997 | static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height) |
38651674 DA |
998 | { |
999 | struct drm_display_mode *mode; | |
1000 | ||
0b4c0f3f | 1001 | list_for_each_entry(mode, &fb_connector->connector->modes, head) { |
38651674 DA |
1002 | if (drm_mode_width(mode) > width || |
1003 | drm_mode_height(mode) > height) | |
1004 | continue; | |
1005 | if (mode->type & DRM_MODE_TYPE_PREFERRED) | |
1006 | return mode; | |
1007 | } | |
1008 | return NULL; | |
1009 | } | |
1010 | ||
0b4c0f3f | 1011 | static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector) |
38651674 | 1012 | { |
38651674 | 1013 | struct drm_fb_helper_cmdline_mode *cmdline_mode; |
0b4c0f3f | 1014 | cmdline_mode = &fb_connector->cmdline_mode; |
38651674 DA |
1015 | return cmdline_mode->specified; |
1016 | } | |
1017 | ||
0b4c0f3f DA |
1018 | static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn, |
1019 | int width, int height) | |
38651674 | 1020 | { |
38651674 DA |
1021 | struct drm_fb_helper_cmdline_mode *cmdline_mode; |
1022 | struct drm_display_mode *mode = NULL; | |
1023 | ||
0b4c0f3f | 1024 | cmdline_mode = &fb_helper_conn->cmdline_mode; |
38651674 DA |
1025 | if (cmdline_mode->specified == false) |
1026 | return mode; | |
1027 | ||
1028 | /* attempt to find a matching mode in the list of modes | |
1029 | * we have gotten so far, if not add a CVT mode that conforms | |
1030 | */ | |
1031 | if (cmdline_mode->rb || cmdline_mode->margins) | |
1032 | goto create_mode; | |
1033 | ||
0b4c0f3f | 1034 | list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) { |
38651674 DA |
1035 | /* check width/height */ |
1036 | if (mode->hdisplay != cmdline_mode->xres || | |
1037 | mode->vdisplay != cmdline_mode->yres) | |
1038 | continue; | |
1039 | ||
1040 | if (cmdline_mode->refresh_specified) { | |
1041 | if (mode->vrefresh != cmdline_mode->refresh) | |
1042 | continue; | |
1043 | } | |
1044 | ||
1045 | if (cmdline_mode->interlace) { | |
1046 | if (!(mode->flags & DRM_MODE_FLAG_INTERLACE)) | |
1047 | continue; | |
1048 | } | |
1049 | return mode; | |
1050 | } | |
1051 | ||
1052 | create_mode: | |
0b4c0f3f | 1053 | mode = drm_cvt_mode(fb_helper_conn->connector->dev, cmdline_mode->xres, |
38651674 DA |
1054 | cmdline_mode->yres, |
1055 | cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60, | |
1056 | cmdline_mode->rb, cmdline_mode->interlace, | |
1057 | cmdline_mode->margins); | |
1058 | drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V); | |
0b4c0f3f | 1059 | list_add(&mode->head, &fb_helper_conn->connector->modes); |
38651674 DA |
1060 | return mode; |
1061 | } | |
1062 | ||
1063 | static bool drm_connector_enabled(struct drm_connector *connector, bool strict) | |
1064 | { | |
1065 | bool enable; | |
1066 | ||
1067 | if (strict) { | |
1068 | enable = connector->status == connector_status_connected; | |
1069 | } else { | |
1070 | enable = connector->status != connector_status_disconnected; | |
1071 | } | |
1072 | return enable; | |
1073 | } | |
1074 | ||
0b4c0f3f DA |
1075 | static void drm_enable_connectors(struct drm_fb_helper *fb_helper, |
1076 | bool *enabled) | |
38651674 DA |
1077 | { |
1078 | bool any_enabled = false; | |
1079 | struct drm_connector *connector; | |
1080 | int i = 0; | |
1081 | ||
0b4c0f3f DA |
1082 | for (i = 0; i < fb_helper->connector_count; i++) { |
1083 | connector = fb_helper->connector_info[i]->connector; | |
38651674 DA |
1084 | enabled[i] = drm_connector_enabled(connector, true); |
1085 | DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id, | |
1086 | enabled[i] ? "yes" : "no"); | |
1087 | any_enabled |= enabled[i]; | |
38651674 DA |
1088 | } |
1089 | ||
1090 | if (any_enabled) | |
1091 | return; | |
1092 | ||
0b4c0f3f DA |
1093 | for (i = 0; i < fb_helper->connector_count; i++) { |
1094 | connector = fb_helper->connector_info[i]->connector; | |
38651674 | 1095 | enabled[i] = drm_connector_enabled(connector, false); |
38651674 DA |
1096 | } |
1097 | } | |
1098 | ||
0b4c0f3f | 1099 | static bool drm_target_preferred(struct drm_fb_helper *fb_helper, |
38651674 DA |
1100 | struct drm_display_mode **modes, |
1101 | bool *enabled, int width, int height) | |
1102 | { | |
0b4c0f3f DA |
1103 | struct drm_fb_helper_connector *fb_helper_conn; |
1104 | int i; | |
38651674 | 1105 | |
0b4c0f3f DA |
1106 | for (i = 0; i < fb_helper->connector_count; i++) { |
1107 | fb_helper_conn = fb_helper->connector_info[i]; | |
38651674 | 1108 | |
0b4c0f3f | 1109 | if (enabled[i] == false) |
38651674 | 1110 | continue; |
38651674 DA |
1111 | |
1112 | DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n", | |
0b4c0f3f | 1113 | fb_helper_conn->connector->base.id); |
38651674 DA |
1114 | |
1115 | /* got for command line mode first */ | |
0b4c0f3f | 1116 | modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height); |
38651674 DA |
1117 | if (!modes[i]) { |
1118 | DRM_DEBUG_KMS("looking for preferred mode on connector %d\n", | |
0b4c0f3f DA |
1119 | fb_helper_conn->connector->base.id); |
1120 | modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height); | |
38651674 DA |
1121 | } |
1122 | /* No preferred modes, pick one off the list */ | |
0b4c0f3f DA |
1123 | if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) { |
1124 | list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head) | |
38651674 DA |
1125 | break; |
1126 | } | |
1127 | DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name : | |
1128 | "none"); | |
38651674 DA |
1129 | } |
1130 | return true; | |
1131 | } | |
1132 | ||
8be48d92 DA |
1133 | static int drm_pick_crtcs(struct drm_fb_helper *fb_helper, |
1134 | struct drm_fb_helper_crtc **best_crtcs, | |
38651674 DA |
1135 | struct drm_display_mode **modes, |
1136 | int n, int width, int height) | |
1137 | { | |
1138 | int c, o; | |
8be48d92 | 1139 | struct drm_device *dev = fb_helper->dev; |
38651674 DA |
1140 | struct drm_connector *connector; |
1141 | struct drm_connector_helper_funcs *connector_funcs; | |
1142 | struct drm_encoder *encoder; | |
8be48d92 | 1143 | struct drm_fb_helper_crtc *best_crtc; |
38651674 | 1144 | int my_score, best_score, score; |
8be48d92 | 1145 | struct drm_fb_helper_crtc **crtcs, *crtc; |
0b4c0f3f | 1146 | struct drm_fb_helper_connector *fb_helper_conn; |
38651674 | 1147 | |
0b4c0f3f | 1148 | if (n == fb_helper->connector_count) |
38651674 | 1149 | return 0; |
0b4c0f3f DA |
1150 | |
1151 | fb_helper_conn = fb_helper->connector_info[n]; | |
1152 | connector = fb_helper_conn->connector; | |
38651674 DA |
1153 | |
1154 | best_crtcs[n] = NULL; | |
1155 | best_crtc = NULL; | |
8be48d92 | 1156 | best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height); |
38651674 DA |
1157 | if (modes[n] == NULL) |
1158 | return best_score; | |
1159 | ||
8be48d92 DA |
1160 | crtcs = kzalloc(dev->mode_config.num_connector * |
1161 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); | |
38651674 DA |
1162 | if (!crtcs) |
1163 | return best_score; | |
1164 | ||
1165 | my_score = 1; | |
1166 | if (connector->status == connector_status_connected) | |
1167 | my_score++; | |
0b4c0f3f | 1168 | if (drm_has_cmdline_mode(fb_helper_conn)) |
38651674 | 1169 | my_score++; |
0b4c0f3f | 1170 | if (drm_has_preferred_mode(fb_helper_conn, width, height)) |
38651674 DA |
1171 | my_score++; |
1172 | ||
1173 | connector_funcs = connector->helper_private; | |
1174 | encoder = connector_funcs->best_encoder(connector); | |
1175 | if (!encoder) | |
1176 | goto out; | |
1177 | ||
38651674 DA |
1178 | /* select a crtc for this connector and then attempt to configure |
1179 | remaining connectors */ | |
8be48d92 DA |
1180 | for (c = 0; c < fb_helper->crtc_count; c++) { |
1181 | crtc = &fb_helper->crtc_info[c]; | |
38651674 DA |
1182 | |
1183 | if ((encoder->possible_crtcs & (1 << c)) == 0) { | |
38651674 DA |
1184 | continue; |
1185 | } | |
1186 | ||
1187 | for (o = 0; o < n; o++) | |
1188 | if (best_crtcs[o] == crtc) | |
1189 | break; | |
1190 | ||
1191 | if (o < n) { | |
1192 | /* ignore cloning for now */ | |
38651674 DA |
1193 | continue; |
1194 | } | |
1195 | ||
1196 | crtcs[n] = crtc; | |
8be48d92 DA |
1197 | memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *)); |
1198 | score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1, | |
38651674 DA |
1199 | width, height); |
1200 | if (score > best_score) { | |
1201 | best_crtc = crtc; | |
1202 | best_score = score; | |
1203 | memcpy(best_crtcs, crtcs, | |
1204 | dev->mode_config.num_connector * | |
8be48d92 | 1205 | sizeof(struct drm_fb_helper_crtc *)); |
38651674 | 1206 | } |
38651674 DA |
1207 | } |
1208 | out: | |
1209 | kfree(crtcs); | |
1210 | return best_score; | |
1211 | } | |
1212 | ||
8be48d92 | 1213 | static void drm_setup_crtcs(struct drm_fb_helper *fb_helper) |
38651674 | 1214 | { |
8be48d92 DA |
1215 | struct drm_device *dev = fb_helper->dev; |
1216 | struct drm_fb_helper_crtc **crtcs; | |
38651674 DA |
1217 | struct drm_display_mode **modes; |
1218 | struct drm_encoder *encoder; | |
8be48d92 | 1219 | struct drm_mode_set *modeset; |
38651674 DA |
1220 | bool *enabled; |
1221 | int width, height; | |
1222 | int i, ret; | |
1223 | ||
1224 | DRM_DEBUG_KMS("\n"); | |
1225 | ||
1226 | width = dev->mode_config.max_width; | |
1227 | height = dev->mode_config.max_height; | |
1228 | ||
1229 | /* clean out all the encoder/crtc combos */ | |
1230 | list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) { | |
1231 | encoder->crtc = NULL; | |
1232 | } | |
1233 | ||
1234 | crtcs = kcalloc(dev->mode_config.num_connector, | |
8be48d92 | 1235 | sizeof(struct drm_fb_helper_crtc *), GFP_KERNEL); |
38651674 DA |
1236 | modes = kcalloc(dev->mode_config.num_connector, |
1237 | sizeof(struct drm_display_mode *), GFP_KERNEL); | |
1238 | enabled = kcalloc(dev->mode_config.num_connector, | |
1239 | sizeof(bool), GFP_KERNEL); | |
1240 | ||
0b4c0f3f | 1241 | drm_enable_connectors(fb_helper, enabled); |
38651674 | 1242 | |
0b4c0f3f | 1243 | ret = drm_target_preferred(fb_helper, modes, enabled, width, height); |
38651674 DA |
1244 | if (!ret) |
1245 | DRM_ERROR("Unable to find initial modes\n"); | |
1246 | ||
1247 | DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height); | |
1248 | ||
8be48d92 DA |
1249 | drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height); |
1250 | ||
1251 | /* need to set the modesets up here for use later */ | |
1252 | /* fill out the connector<->crtc mappings into the modesets */ | |
1253 | for (i = 0; i < fb_helper->crtc_count; i++) { | |
1254 | modeset = &fb_helper->crtc_info[i].mode_set; | |
1255 | modeset->num_connectors = 0; | |
1256 | } | |
38651674 | 1257 | |
0b4c0f3f | 1258 | for (i = 0; i < fb_helper->connector_count; i++) { |
38651674 | 1259 | struct drm_display_mode *mode = modes[i]; |
8be48d92 DA |
1260 | struct drm_fb_helper_crtc *fb_crtc = crtcs[i]; |
1261 | modeset = &fb_crtc->mode_set; | |
38651674 | 1262 | |
8be48d92 | 1263 | if (mode && fb_crtc) { |
38651674 | 1264 | DRM_DEBUG_KMS("desired mode %s set on crtc %d\n", |
8be48d92 DA |
1265 | mode->name, fb_crtc->mode_set.crtc->base.id); |
1266 | fb_crtc->desired_mode = mode; | |
1267 | if (modeset->mode) | |
1268 | drm_mode_destroy(dev, modeset->mode); | |
1269 | modeset->mode = drm_mode_duplicate(dev, | |
1270 | fb_crtc->desired_mode); | |
0b4c0f3f | 1271 | modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector; |
38651674 | 1272 | } |
38651674 DA |
1273 | } |
1274 | ||
1275 | kfree(crtcs); | |
1276 | kfree(modes); | |
1277 | kfree(enabled); | |
1278 | } | |
1279 | ||
1280 | /** | |
1281 | * drm_helper_initial_config - setup a sane initial connector configuration | |
1282 | * @dev: DRM device | |
1283 | * | |
1284 | * LOCKING: | |
1285 | * Called at init time, must take mode config lock. | |
1286 | * | |
1287 | * Scan the CRTCs and connectors and try to put together an initial setup. | |
1288 | * At the moment, this is a cloned configuration across all heads with | |
1289 | * a new framebuffer object as the backing store. | |
1290 | * | |
1291 | * RETURNS: | |
1292 | * Zero if everything went ok, nonzero otherwise. | |
1293 | */ | |
8be48d92 | 1294 | bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper) |
38651674 | 1295 | { |
8be48d92 | 1296 | struct drm_device *dev = fb_helper->dev; |
38651674 DA |
1297 | int count = 0; |
1298 | ||
1299 | /* disable all the possible outputs/crtcs before entering KMS mode */ | |
8be48d92 | 1300 | drm_helper_disable_unused_functions(fb_helper->dev); |
38651674 | 1301 | |
0b4c0f3f | 1302 | drm_fb_helper_parse_command_line(fb_helper); |
38651674 | 1303 | |
0b4c0f3f DA |
1304 | count = drm_fb_helper_probe_connector_modes(fb_helper, |
1305 | dev->mode_config.max_width, | |
1306 | dev->mode_config.max_height); | |
38651674 DA |
1307 | |
1308 | /* | |
1309 | * we shouldn't end up with no modes here. | |
1310 | */ | |
5c4426a7 DA |
1311 | if (count == 0) { |
1312 | if (fb_helper->poll_enabled) { | |
1313 | delayed_slow_work_enqueue(&fb_helper->output_poll_slow_work, | |
1314 | 5*HZ); | |
1315 | printk(KERN_INFO "No connectors reported connected with modes - started polling\n"); | |
1316 | } else | |
1317 | printk(KERN_INFO "No connectors reported connected with modes\n"); | |
1318 | } | |
8be48d92 | 1319 | drm_setup_crtcs(fb_helper); |
38651674 DA |
1320 | |
1321 | return 0; | |
1322 | } | |
8be48d92 | 1323 | EXPORT_SYMBOL(drm_fb_helper_initial_config); |
38651674 | 1324 | |
8be48d92 | 1325 | bool drm_helper_fb_hotplug_event(struct drm_fb_helper *fb_helper, |
5c4426a7 | 1326 | u32 max_width, u32 max_height, bool polled) |
38651674 | 1327 | { |
5c4426a7 DA |
1328 | int count = 0; |
1329 | int ret; | |
38651674 DA |
1330 | DRM_DEBUG_KMS("\n"); |
1331 | ||
5c4426a7 | 1332 | count = drm_fb_helper_probe_connector_modes(fb_helper, max_width, |
0b4c0f3f | 1333 | max_height); |
5c4426a7 DA |
1334 | if (fb_helper->poll_enabled && !polled) { |
1335 | if (count) { | |
1336 | delayed_slow_work_cancel(&fb_helper->output_poll_slow_work); | |
1337 | } else { | |
1338 | ret = delayed_slow_work_enqueue(&fb_helper->output_poll_slow_work, 5*HZ); | |
1339 | } | |
1340 | } | |
8be48d92 | 1341 | drm_setup_crtcs(fb_helper); |
38651674 DA |
1342 | |
1343 | return true; | |
1344 | } | |
1345 | EXPORT_SYMBOL(drm_helper_fb_hotplug_event); | |
5c4426a7 DA |
1346 | |
1347 | static void output_poll_execute(struct slow_work *work) | |
1348 | { | |
1349 | struct delayed_slow_work *delayed_work = container_of(work, struct delayed_slow_work, work); | |
1350 | struct drm_fb_helper *fb_helper = container_of(delayed_work, struct drm_fb_helper, output_poll_slow_work); | |
1351 | struct drm_device *dev = fb_helper->dev; | |
1352 | struct drm_connector *connector; | |
1353 | enum drm_connector_status old_status, status; | |
1354 | bool repoll = true, changed = false; | |
1355 | int ret; | |
1356 | ||
1357 | list_for_each_entry(connector, &dev->mode_config.connector_list, head) { | |
1358 | old_status = connector->status; | |
1359 | status = connector->funcs->detect(connector); | |
1360 | if (old_status != status) { | |
1361 | changed = true; | |
1362 | /* something changed */ | |
1363 | } | |
1364 | if (status == connector_status_connected) { | |
1365 | DRM_DEBUG("%s is connected - stop polling\n", drm_get_connector_name(connector)); | |
1366 | repoll = false; | |
1367 | } | |
1368 | } | |
1369 | ||
1370 | if (repoll) { | |
1371 | ret = delayed_slow_work_enqueue(delayed_work, 5*HZ); | |
1372 | if (ret) | |
1373 | DRM_ERROR("delayed enqueue failed %d\n", ret); | |
1374 | } | |
1375 | ||
1376 | if (changed) { | |
1377 | if (fb_helper->fb_poll_changed) | |
1378 | fb_helper->fb_poll_changed(fb_helper); | |
1379 | } | |
1380 | } | |
1381 | ||
1382 | struct slow_work_ops output_poll_ops = { | |
1383 | .execute = output_poll_execute, | |
1384 | }; | |
1385 | ||
1386 | void drm_fb_helper_poll_init(struct drm_fb_helper *fb_helper) | |
1387 | { | |
1388 | int ret; | |
1389 | ||
1390 | ret = slow_work_register_user(THIS_MODULE); | |
1391 | ||
1392 | delayed_slow_work_init(&fb_helper->output_poll_slow_work, &output_poll_ops); | |
1393 | fb_helper->poll_enabled = true; | |
1394 | } | |
1395 | EXPORT_SYMBOL(drm_fb_helper_poll_init); | |
1396 | ||
1397 | void drm_fb_helper_poll_fini(struct drm_fb_helper *fb_helper) | |
1398 | { | |
1399 | delayed_slow_work_cancel(&fb_helper->output_poll_slow_work); | |
1400 | slow_work_unregister_user(THIS_MODULE); | |
1401 | } | |
1402 | EXPORT_SYMBOL(drm_fb_helper_poll_fini); |