xref: /dflybsd-src/sys/dev/drm/drm_fb_helper.c (revision af4b81b9fa0b40a0b482311a55a909d593a54aee)
15718399fSFrançois Tigeot /*
25718399fSFrançois Tigeot  * Copyright (c) 2006-2009 Red Hat Inc.
35718399fSFrançois Tigeot  * Copyright (c) 2006-2008 Intel Corporation
45718399fSFrançois Tigeot  * Copyright (c) 2007 Dave Airlie <airlied@linux.ie>
55718399fSFrançois Tigeot  *
65718399fSFrançois Tigeot  * DRM framebuffer helper functions
75718399fSFrançois Tigeot  *
85718399fSFrançois Tigeot  * Permission to use, copy, modify, distribute, and sell this software and its
95718399fSFrançois Tigeot  * documentation for any purpose is hereby granted without fee, provided that
105718399fSFrançois Tigeot  * the above copyright notice appear in all copies and that both that copyright
115718399fSFrançois Tigeot  * notice and this permission notice appear in supporting documentation, and
125718399fSFrançois Tigeot  * that the name of the copyright holders not be used in advertising or
135718399fSFrançois Tigeot  * publicity pertaining to distribution of the software without specific,
145718399fSFrançois Tigeot  * written prior permission.  The copyright holders make no representations
155718399fSFrançois Tigeot  * about the suitability of this software for any purpose.  It is provided "as
165718399fSFrançois Tigeot  * is" without express or implied warranty.
175718399fSFrançois Tigeot  *
185718399fSFrançois Tigeot  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
195718399fSFrançois Tigeot  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
205718399fSFrançois Tigeot  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
215718399fSFrançois Tigeot  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
225718399fSFrançois Tigeot  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
235718399fSFrançois Tigeot  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
245718399fSFrançois Tigeot  * OF THIS SOFTWARE.
255718399fSFrançois Tigeot  *
265718399fSFrançois Tigeot  * Authors:
275718399fSFrançois Tigeot  *      Dave Airlie <airlied@linux.ie>
285718399fSFrançois Tigeot  *      Jesse Barnes <jesse.barnes@intel.com>
295718399fSFrançois Tigeot  *
305718399fSFrançois Tigeot  * $FreeBSD: src/sys/dev/drm2/drm_fb_helper.c,v 1.1 2012/05/22 11:07:44 kib Exp $
315718399fSFrançois Tigeot  */
325718399fSFrançois Tigeot 
3318e26a6dSFrançois Tigeot #include <drm/drmP.h>
3418e26a6dSFrançois Tigeot #include <drm/drm_crtc.h>
3518e26a6dSFrançois Tigeot #include <drm/drm_fb_helper.h>
3618e26a6dSFrançois Tigeot #include <drm/drm_crtc_helper.h>
375718399fSFrançois Tigeot 
3828828b89SFrançois Tigeot static LINUX_LIST_HEAD(kernel_fb_helper_list);
395718399fSFrançois Tigeot 
405718399fSFrançois Tigeot /* simple single crtc case helper function */
415718399fSFrançois Tigeot int drm_fb_helper_single_add_all_connectors(struct drm_fb_helper *fb_helper)
425718399fSFrançois Tigeot {
435718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
445718399fSFrançois Tigeot 	struct drm_connector *connector;
455718399fSFrançois Tigeot 
465718399fSFrançois Tigeot 	list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
475718399fSFrançois Tigeot 		struct drm_fb_helper_connector *fb_helper_connector;
485718399fSFrançois Tigeot 
495718399fSFrançois Tigeot 		fb_helper_connector = kmalloc(
505718399fSFrançois Tigeot 		    sizeof(struct drm_fb_helper_connector), DRM_MEM_KMS,
515718399fSFrançois Tigeot 		    M_WAITOK | M_ZERO);
525718399fSFrançois Tigeot 
535718399fSFrançois Tigeot 		fb_helper_connector->connector = connector;
545718399fSFrançois Tigeot 		fb_helper->connector_info[fb_helper->connector_count++] = fb_helper_connector;
555718399fSFrançois Tigeot 	}
565718399fSFrançois Tigeot 	return 0;
575718399fSFrançois Tigeot }
585718399fSFrançois Tigeot 
595718399fSFrançois Tigeot const char *fb_mode_option;
605718399fSFrançois Tigeot 
615718399fSFrançois Tigeot /**
625718399fSFrançois Tigeot  * drm_fb_helper_connector_parse_command_line - parse command line for connector
635718399fSFrançois Tigeot  * @connector - connector to parse line for
645718399fSFrançois Tigeot  * @mode_option - per connector mode option
655718399fSFrançois Tigeot  *
665718399fSFrançois Tigeot  * This parses the connector specific then generic command lines for
675718399fSFrançois Tigeot  * modes and options to configure the connector.
685718399fSFrançois Tigeot  *
695718399fSFrançois Tigeot  * This uses the same parameters as the fb modedb.c, except for extra
705718399fSFrançois Tigeot  *	<xres>x<yres>[M][R][-<bpp>][@<refresh>][i][m][eDd]
715718399fSFrançois Tigeot  *
725718399fSFrançois Tigeot  * enable/enable Digital/disable bit at the end
735718399fSFrançois Tigeot  */
745718399fSFrançois Tigeot static bool drm_fb_helper_connector_parse_command_line(struct drm_fb_helper_connector *fb_helper_conn,
755718399fSFrançois Tigeot 						       const char *mode_option)
765718399fSFrançois Tigeot {
775718399fSFrançois Tigeot 	const char *name;
785718399fSFrançois Tigeot 	unsigned int namelen;
795718399fSFrançois Tigeot 	int res_specified = 0, bpp_specified = 0, refresh_specified = 0;
805718399fSFrançois Tigeot 	unsigned int xres = 0, yres = 0, bpp = 32, refresh = 0;
815718399fSFrançois Tigeot 	int yres_specified = 0, cvt = 0, rb = 0, interlace = 0, margins = 0;
825718399fSFrançois Tigeot 	int i;
835718399fSFrançois Tigeot 	enum drm_connector_force force = DRM_FORCE_UNSPECIFIED;
845718399fSFrançois Tigeot 	struct drm_fb_helper_cmdline_mode *cmdline_mode;
855718399fSFrançois Tigeot 	struct drm_connector *connector;
865718399fSFrançois Tigeot 
875718399fSFrançois Tigeot 	if (!fb_helper_conn)
885718399fSFrançois Tigeot 		return false;
895718399fSFrançois Tigeot 	connector = fb_helper_conn->connector;
905718399fSFrançois Tigeot 
915718399fSFrançois Tigeot 	cmdline_mode = &fb_helper_conn->cmdline_mode;
925718399fSFrançois Tigeot 	if (!mode_option)
935718399fSFrançois Tigeot 		mode_option = fb_mode_option;
945718399fSFrançois Tigeot 
955718399fSFrançois Tigeot 	if (!mode_option) {
965718399fSFrançois Tigeot 		cmdline_mode->specified = false;
975718399fSFrançois Tigeot 		return false;
985718399fSFrançois Tigeot 	}
995718399fSFrançois Tigeot 
1005718399fSFrançois Tigeot 	name = mode_option;
1015718399fSFrançois Tigeot 	namelen = strlen(name);
1025718399fSFrançois Tigeot 	for (i = namelen-1; i >= 0; i--) {
1035718399fSFrançois Tigeot 		switch (name[i]) {
1045718399fSFrançois Tigeot 		case '@':
1055718399fSFrançois Tigeot 			namelen = i;
1065718399fSFrançois Tigeot 			if (!refresh_specified && !bpp_specified &&
1075718399fSFrançois Tigeot 			    !yres_specified) {
1085718399fSFrançois Tigeot 				refresh = strtol(&name[i+1], NULL, 10);
1095718399fSFrançois Tigeot 				refresh_specified = 1;
1105718399fSFrançois Tigeot 				if (cvt || rb)
1115718399fSFrançois Tigeot 					cvt = 0;
1125718399fSFrançois Tigeot 			} else
1135718399fSFrançois Tigeot 				goto done;
1145718399fSFrançois Tigeot 			break;
1155718399fSFrançois Tigeot 		case '-':
1165718399fSFrançois Tigeot 			namelen = i;
1175718399fSFrançois Tigeot 			if (!bpp_specified && !yres_specified) {
1185718399fSFrançois Tigeot 				bpp = strtol(&name[i+1], NULL, 10);
1195718399fSFrançois Tigeot 				bpp_specified = 1;
1205718399fSFrançois Tigeot 				if (cvt || rb)
1215718399fSFrançois Tigeot 					cvt = 0;
1225718399fSFrançois Tigeot 			} else
1235718399fSFrançois Tigeot 				goto done;
1245718399fSFrançois Tigeot 			break;
1255718399fSFrançois Tigeot 		case 'x':
1265718399fSFrançois Tigeot 			if (!yres_specified) {
1275718399fSFrançois Tigeot 				yres = strtol(&name[i+1], NULL, 10);
1285718399fSFrançois Tigeot 				yres_specified = 1;
1295718399fSFrançois Tigeot 			} else
1305718399fSFrançois Tigeot 				goto done;
1315718399fSFrançois Tigeot 		case '0' ... '9':
1325718399fSFrançois Tigeot 			break;
1335718399fSFrançois Tigeot 		case 'M':
1345718399fSFrançois Tigeot 			if (!yres_specified)
1355718399fSFrançois Tigeot 				cvt = 1;
1365718399fSFrançois Tigeot 			break;
1375718399fSFrançois Tigeot 		case 'R':
1385718399fSFrançois Tigeot 			if (cvt)
1395718399fSFrançois Tigeot 				rb = 1;
1405718399fSFrançois Tigeot 			break;
1415718399fSFrançois Tigeot 		case 'm':
1425718399fSFrançois Tigeot 			if (!cvt)
1435718399fSFrançois Tigeot 				margins = 1;
1445718399fSFrançois Tigeot 			break;
1455718399fSFrançois Tigeot 		case 'i':
1465718399fSFrançois Tigeot 			if (!cvt)
1475718399fSFrançois Tigeot 				interlace = 1;
1485718399fSFrançois Tigeot 			break;
1495718399fSFrançois Tigeot 		case 'e':
1505718399fSFrançois Tigeot 			force = DRM_FORCE_ON;
1515718399fSFrançois Tigeot 			break;
1525718399fSFrançois Tigeot 		case 'D':
1535718399fSFrançois Tigeot 			if ((connector->connector_type != DRM_MODE_CONNECTOR_DVII) &&
1545718399fSFrançois Tigeot 			    (connector->connector_type != DRM_MODE_CONNECTOR_HDMIB))
1555718399fSFrançois Tigeot 				force = DRM_FORCE_ON;
1565718399fSFrançois Tigeot 			else
1575718399fSFrançois Tigeot 				force = DRM_FORCE_ON_DIGITAL;
1585718399fSFrançois Tigeot 			break;
1595718399fSFrançois Tigeot 		case 'd':
1605718399fSFrançois Tigeot 			force = DRM_FORCE_OFF;
1615718399fSFrançois Tigeot 			break;
1625718399fSFrançois Tigeot 		default:
1635718399fSFrançois Tigeot 			goto done;
1645718399fSFrançois Tigeot 		}
1655718399fSFrançois Tigeot 	}
1665718399fSFrançois Tigeot 	if (i < 0 && yres_specified) {
1675718399fSFrançois Tigeot 		xres = strtol(name, NULL, 10);
1685718399fSFrançois Tigeot 		res_specified = 1;
1695718399fSFrançois Tigeot 	}
1705718399fSFrançois Tigeot done:
1715718399fSFrançois Tigeot 
1725718399fSFrançois Tigeot 	DRM_DEBUG_KMS("cmdline mode for connector %s %dx%d@%dHz%s%s%s\n",
1735718399fSFrançois Tigeot 		drm_get_connector_name(connector), xres, yres,
1745718399fSFrançois Tigeot 		(refresh) ? refresh : 60, (rb) ? " reduced blanking" :
1755718399fSFrançois Tigeot 		"", (margins) ? " with margins" : "", (interlace) ?
1765718399fSFrançois Tigeot 		" interlaced" : "");
1775718399fSFrançois Tigeot 
1785718399fSFrançois Tigeot 	if (force) {
1795718399fSFrançois Tigeot 		const char *s;
1805718399fSFrançois Tigeot 		switch (force) {
1815718399fSFrançois Tigeot 		case DRM_FORCE_OFF: s = "OFF"; break;
1825718399fSFrançois Tigeot 		case DRM_FORCE_ON_DIGITAL: s = "ON - dig"; break;
1835718399fSFrançois Tigeot 		default:
1845718399fSFrançois Tigeot 		case DRM_FORCE_ON: s = "ON"; break;
1855718399fSFrançois Tigeot 		}
1865718399fSFrançois Tigeot 
1875718399fSFrançois Tigeot 		DRM_INFO("forcing %s connector %s\n",
1885718399fSFrançois Tigeot 			 drm_get_connector_name(connector), s);
1895718399fSFrançois Tigeot 		connector->force = force;
1905718399fSFrançois Tigeot 	}
1915718399fSFrançois Tigeot 
1925718399fSFrançois Tigeot 	if (res_specified) {
1935718399fSFrançois Tigeot 		cmdline_mode->specified = true;
1945718399fSFrançois Tigeot 		cmdline_mode->xres = xres;
1955718399fSFrançois Tigeot 		cmdline_mode->yres = yres;
1965718399fSFrançois Tigeot 	}
1975718399fSFrançois Tigeot 
1985718399fSFrançois Tigeot 	if (refresh_specified) {
1995718399fSFrançois Tigeot 		cmdline_mode->refresh_specified = true;
2005718399fSFrançois Tigeot 		cmdline_mode->refresh = refresh;
2015718399fSFrançois Tigeot 	}
2025718399fSFrançois Tigeot 
2035718399fSFrançois Tigeot 	if (bpp_specified) {
2045718399fSFrançois Tigeot 		cmdline_mode->bpp_specified = true;
2055718399fSFrançois Tigeot 		cmdline_mode->bpp = bpp;
2065718399fSFrançois Tigeot 	}
2075718399fSFrançois Tigeot 	cmdline_mode->rb = rb ? true : false;
2085718399fSFrançois Tigeot 	cmdline_mode->cvt = cvt  ? true : false;
2095718399fSFrançois Tigeot 	cmdline_mode->interlace = interlace ? true : false;
2105718399fSFrançois Tigeot 
2115718399fSFrançois Tigeot 	return true;
2125718399fSFrançois Tigeot }
2135718399fSFrançois Tigeot 
2145718399fSFrançois Tigeot static int
2155718399fSFrançois Tigeot fb_get_options(const char *connector_name, char **option)
2165718399fSFrançois Tigeot {
2175718399fSFrançois Tigeot 
2185718399fSFrançois Tigeot 	return (1);
2195718399fSFrançois Tigeot }
2205718399fSFrançois Tigeot 
2215718399fSFrançois Tigeot static int drm_fb_helper_parse_command_line(struct drm_fb_helper *fb_helper)
2225718399fSFrançois Tigeot {
2235718399fSFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_conn;
2245718399fSFrançois Tigeot 	int i;
2255718399fSFrançois Tigeot 
2265718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
2275718399fSFrançois Tigeot 		char *option = NULL;
2285718399fSFrançois Tigeot 
2295718399fSFrançois Tigeot 		fb_helper_conn = fb_helper->connector_info[i];
2305718399fSFrançois Tigeot 
2315718399fSFrançois Tigeot 		/* do something on return - turn off connector maybe */
2325718399fSFrançois Tigeot 		if (fb_get_options(drm_get_connector_name(fb_helper_conn->connector), &option))
2335718399fSFrançois Tigeot 			continue;
2345718399fSFrançois Tigeot 
2355718399fSFrançois Tigeot 		drm_fb_helper_connector_parse_command_line(fb_helper_conn, option);
2365718399fSFrançois Tigeot 	}
2375718399fSFrançois Tigeot 	return 0;
2385718399fSFrançois Tigeot }
2395718399fSFrançois Tigeot 
2405718399fSFrançois Tigeot #if 0
2415718399fSFrançois Tigeot static void drm_fb_helper_save_lut_atomic(struct drm_crtc *crtc, struct drm_fb_helper *helper)
2425718399fSFrançois Tigeot {
2435718399fSFrançois Tigeot 	uint16_t *r_base, *g_base, *b_base;
2445718399fSFrançois Tigeot 	int i;
2455718399fSFrançois Tigeot 
2465718399fSFrançois Tigeot 	r_base = crtc->gamma_store;
2475718399fSFrançois Tigeot 	g_base = r_base + crtc->gamma_size;
2485718399fSFrançois Tigeot 	b_base = g_base + crtc->gamma_size;
2495718399fSFrançois Tigeot 
2505718399fSFrançois Tigeot 	for (i = 0; i < crtc->gamma_size; i++)
2515718399fSFrançois Tigeot 		helper->funcs->gamma_get(crtc, &r_base[i], &g_base[i], &b_base[i], i);
2525718399fSFrançois Tigeot }
2535718399fSFrançois Tigeot 
2545718399fSFrançois Tigeot static void drm_fb_helper_restore_lut_atomic(struct drm_crtc *crtc)
2555718399fSFrançois Tigeot {
2565718399fSFrançois Tigeot 	uint16_t *r_base, *g_base, *b_base;
2575718399fSFrançois Tigeot 
2585718399fSFrançois Tigeot 	r_base = crtc->gamma_store;
2595718399fSFrançois Tigeot 	g_base = r_base + crtc->gamma_size;
2605718399fSFrançois Tigeot 	b_base = g_base + crtc->gamma_size;
2615718399fSFrançois Tigeot 
2625718399fSFrançois Tigeot 	crtc->funcs->gamma_set(crtc, r_base, g_base, b_base, 0, crtc->gamma_size);
2635718399fSFrançois Tigeot }
2645718399fSFrançois Tigeot #endif
2655718399fSFrançois Tigeot 
2665718399fSFrançois Tigeot #if 0
2675718399fSFrançois Tigeot int drm_fb_helper_debug_enter(struct fb_info *info)
2685718399fSFrançois Tigeot {
2695718399fSFrançois Tigeot 	struct drm_fb_helper *helper = info->par;
2705718399fSFrançois Tigeot 	struct drm_crtc_helper_funcs *funcs;
2715718399fSFrançois Tigeot 	int i;
2725718399fSFrançois Tigeot 
2735718399fSFrançois Tigeot 	if (list_empty(&kernel_fb_helper_list))
2745718399fSFrançois Tigeot 		return false;
2755718399fSFrançois Tigeot 
2765718399fSFrançois Tigeot 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
2775718399fSFrançois Tigeot 		for (i = 0; i < helper->crtc_count; i++) {
2785718399fSFrançois Tigeot 			struct drm_mode_set *mode_set =
2795718399fSFrançois Tigeot 				&helper->crtc_info[i].mode_set;
2805718399fSFrançois Tigeot 
2815718399fSFrançois Tigeot 			if (!mode_set->crtc->enabled)
2825718399fSFrançois Tigeot 				continue;
2835718399fSFrançois Tigeot 
2845718399fSFrançois Tigeot 			funcs =	mode_set->crtc->helper_private;
2855718399fSFrançois Tigeot 			drm_fb_helper_save_lut_atomic(mode_set->crtc, helper);
2865718399fSFrançois Tigeot 			funcs->mode_set_base_atomic(mode_set->crtc,
2875718399fSFrançois Tigeot 						    mode_set->fb,
2885718399fSFrançois Tigeot 						    mode_set->x,
2895718399fSFrançois Tigeot 						    mode_set->y,
2905718399fSFrançois Tigeot 						    ENTER_ATOMIC_MODE_SET);
2915718399fSFrançois Tigeot 		}
2925718399fSFrançois Tigeot 	}
2935718399fSFrançois Tigeot 
2945718399fSFrançois Tigeot 	return 0;
2955718399fSFrançois Tigeot }
2965718399fSFrançois Tigeot #endif
2975718399fSFrançois Tigeot 
2985718399fSFrançois Tigeot #if 0
2995718399fSFrançois Tigeot /* Find the real fb for a given fb helper CRTC */
3005718399fSFrançois Tigeot static struct drm_framebuffer *drm_mode_config_fb(struct drm_crtc *crtc)
3015718399fSFrançois Tigeot {
3025718399fSFrançois Tigeot 	struct drm_device *dev = crtc->dev;
3035718399fSFrançois Tigeot 	struct drm_crtc *c;
3045718399fSFrançois Tigeot 
3055718399fSFrançois Tigeot 	list_for_each_entry(c, &dev->mode_config.crtc_list, head) {
3065718399fSFrançois Tigeot 		if (crtc->base.id == c->base.id)
3075718399fSFrançois Tigeot 			return c->fb;
3085718399fSFrançois Tigeot 	}
3095718399fSFrançois Tigeot 
3105718399fSFrançois Tigeot 	return NULL;
3115718399fSFrançois Tigeot }
3125718399fSFrançois Tigeot #endif
3135718399fSFrançois Tigeot 
3145718399fSFrançois Tigeot #if 0
3155718399fSFrançois Tigeot int drm_fb_helper_debug_leave(struct fb_info *info)
3165718399fSFrançois Tigeot {
3175718399fSFrançois Tigeot 	struct drm_fb_helper *helper = info->par;
3185718399fSFrançois Tigeot 	struct drm_crtc *crtc;
3195718399fSFrançois Tigeot 	struct drm_crtc_helper_funcs *funcs;
3205718399fSFrançois Tigeot 	struct drm_framebuffer *fb;
3215718399fSFrançois Tigeot 	int i;
3225718399fSFrançois Tigeot 
3235718399fSFrançois Tigeot 	for (i = 0; i < helper->crtc_count; i++) {
3245718399fSFrançois Tigeot 		struct drm_mode_set *mode_set = &helper->crtc_info[i].mode_set;
3255718399fSFrançois Tigeot 		crtc = mode_set->crtc;
3265718399fSFrançois Tigeot 		funcs = crtc->helper_private;
3275718399fSFrançois Tigeot 		fb = drm_mode_config_fb(crtc);
3285718399fSFrançois Tigeot 
3295718399fSFrançois Tigeot 		if (!crtc->enabled)
3305718399fSFrançois Tigeot 			continue;
3315718399fSFrançois Tigeot 
3325718399fSFrançois Tigeot 		if (!fb) {
3335718399fSFrançois Tigeot 			DRM_ERROR("no fb to restore??\n");
3345718399fSFrançois Tigeot 			continue;
3355718399fSFrançois Tigeot 		}
3365718399fSFrançois Tigeot 
3375718399fSFrançois Tigeot 		drm_fb_helper_restore_lut_atomic(mode_set->crtc);
3385718399fSFrançois Tigeot 		funcs->mode_set_base_atomic(mode_set->crtc, fb, crtc->x,
3395718399fSFrançois Tigeot 					    crtc->y, LEAVE_ATOMIC_MODE_SET);
3405718399fSFrançois Tigeot 	}
3415718399fSFrançois Tigeot 
3425718399fSFrançois Tigeot 	return 0;
3435718399fSFrançois Tigeot }
3445718399fSFrançois Tigeot #endif
3455718399fSFrançois Tigeot 
3465718399fSFrançois Tigeot bool drm_fb_helper_restore_fbdev_mode(struct drm_fb_helper *fb_helper)
3475718399fSFrançois Tigeot {
3485718399fSFrançois Tigeot 	bool error = false;
3495718399fSFrançois Tigeot 	int i, ret;
3505718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
3515718399fSFrançois Tigeot 		struct drm_mode_set *mode_set = &fb_helper->crtc_info[i].mode_set;
3525718399fSFrançois Tigeot 		ret = drm_crtc_helper_set_config(mode_set);
3535718399fSFrançois Tigeot 		if (ret)
3545718399fSFrançois Tigeot 			error = true;
3555718399fSFrançois Tigeot 	}
3565718399fSFrançois Tigeot 	return error;
3575718399fSFrançois Tigeot }
3585718399fSFrançois Tigeot 
3595718399fSFrançois Tigeot #if 0
3605718399fSFrançois Tigeot bool drm_fb_helper_force_kernel_mode(void)
3615718399fSFrançois Tigeot {
3625718399fSFrançois Tigeot 	bool ret, error = false;
3635718399fSFrançois Tigeot 	struct drm_fb_helper *helper;
3645718399fSFrançois Tigeot 
3655718399fSFrançois Tigeot 	if (list_empty(&kernel_fb_helper_list))
3665718399fSFrançois Tigeot 		return false;
3675718399fSFrançois Tigeot 
3685718399fSFrançois Tigeot 	list_for_each_entry(helper, &kernel_fb_helper_list, kernel_fb_list) {
3695718399fSFrançois Tigeot 		if (helper->dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3705718399fSFrançois Tigeot 			continue;
3715718399fSFrançois Tigeot 
3725718399fSFrançois Tigeot 		ret = drm_fb_helper_restore_fbdev_mode(helper);
3735718399fSFrançois Tigeot 		if (ret)
3745718399fSFrançois Tigeot 			error = true;
3755718399fSFrançois Tigeot 	}
3765718399fSFrançois Tigeot 	return error;
3775718399fSFrançois Tigeot }
3785718399fSFrançois Tigeot #endif
3795718399fSFrançois Tigeot 
3805718399fSFrançois Tigeot #if 0
3815718399fSFrançois Tigeot int drm_fb_helper_panic(struct notifier_block *n, unsigned long ununsed,
3825718399fSFrançois Tigeot 			void *panic_str)
3835718399fSFrançois Tigeot {
3845718399fSFrançois Tigeot 	printf("panic occurred, switching back to text console\n");
3855718399fSFrançois Tigeot 	return drm_fb_helper_force_kernel_mode();
3865718399fSFrançois Tigeot 	return 0;
3875718399fSFrançois Tigeot }
3885718399fSFrançois Tigeot 
3895718399fSFrançois Tigeot static struct notifier_block paniced = {
3905718399fSFrançois Tigeot 	.notifier_call = drm_fb_helper_panic,
3915718399fSFrançois Tigeot };
3925718399fSFrançois Tigeot 
3935718399fSFrançois Tigeot /**
3945718399fSFrançois Tigeot  * drm_fb_helper_restore - restore the framebuffer console (kernel) config
3955718399fSFrançois Tigeot  *
3965718399fSFrançois Tigeot  * Restore's the kernel's fbcon mode, used for lastclose & panic paths.
3975718399fSFrançois Tigeot  */
3985718399fSFrançois Tigeot void drm_fb_helper_restore(void)
3995718399fSFrançois Tigeot {
4005718399fSFrançois Tigeot 	bool ret;
4015718399fSFrançois Tigeot 	ret = drm_fb_helper_force_kernel_mode();
4025718399fSFrançois Tigeot 	if (ret == true)
4035718399fSFrançois Tigeot 		DRM_ERROR("Failed to restore crtc configuration\n");
4045718399fSFrançois Tigeot }
4055718399fSFrançois Tigeot 
4065718399fSFrançois Tigeot #ifdef CONFIG_MAGIC_SYSRQ
4075718399fSFrançois Tigeot static void drm_fb_helper_restore_work_fn(struct work_struct *ignored)
4085718399fSFrançois Tigeot {
4095718399fSFrançois Tigeot 	drm_fb_helper_restore();
4105718399fSFrançois Tigeot }
4115718399fSFrançois Tigeot static DECLARE_WORK(drm_fb_helper_restore_work, drm_fb_helper_restore_work_fn);
4125718399fSFrançois Tigeot 
4135718399fSFrançois Tigeot static void drm_fb_helper_sysrq(int dummy1)
4145718399fSFrançois Tigeot {
4155718399fSFrançois Tigeot 	schedule_work(&drm_fb_helper_restore_work);
4165718399fSFrançois Tigeot }
4175718399fSFrançois Tigeot 
4185718399fSFrançois Tigeot static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = {
4195718399fSFrançois Tigeot 	.handler = drm_fb_helper_sysrq,
4205718399fSFrançois Tigeot 	.help_msg = "force-fb(V)",
4215718399fSFrançois Tigeot 	.action_msg = "Restore framebuffer console",
4225718399fSFrançois Tigeot };
4235718399fSFrançois Tigeot #else
4245718399fSFrançois Tigeot static struct sysrq_key_op sysrq_drm_fb_helper_restore_op = { };
4255718399fSFrançois Tigeot #endif
4265718399fSFrançois Tigeot #endif
4275718399fSFrançois Tigeot 
4285718399fSFrançois Tigeot #if 0
4295718399fSFrançois Tigeot static void drm_fb_helper_on(struct fb_info *info)
4305718399fSFrançois Tigeot {
4315718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
4325718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
4335718399fSFrançois Tigeot 	struct drm_crtc *crtc;
4345718399fSFrançois Tigeot 	struct drm_crtc_helper_funcs *crtc_funcs;
4355718399fSFrançois Tigeot 	struct drm_connector *connector;
4365718399fSFrançois Tigeot 	struct drm_encoder *encoder;
4375718399fSFrançois Tigeot 	int i, j;
4385718399fSFrançois Tigeot 
4395718399fSFrançois Tigeot 	/*
4405718399fSFrançois Tigeot 	 * For each CRTC in this fb, turn the crtc on then,
4415718399fSFrançois Tigeot 	 * find all associated encoders and turn them on.
4425718399fSFrançois Tigeot 	 */
4435718399fSFrançois Tigeot 	sx_xlock(&dev->mode_config.mutex);
4445718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
4455718399fSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
4465718399fSFrançois Tigeot 		crtc_funcs = crtc->helper_private;
4475718399fSFrançois Tigeot 
4485718399fSFrançois Tigeot 		if (!crtc->enabled)
4495718399fSFrançois Tigeot 			continue;
4505718399fSFrançois Tigeot 
4515718399fSFrançois Tigeot 		crtc_funcs->dpms(crtc, DRM_MODE_DPMS_ON);
4525718399fSFrançois Tigeot 
4535718399fSFrançois Tigeot 		/* Walk the connectors & encoders on this fb turning them on */
4545718399fSFrançois Tigeot 		for (j = 0; j < fb_helper->connector_count; j++) {
4555718399fSFrançois Tigeot 			connector = fb_helper->connector_info[j]->connector;
4565718399fSFrançois Tigeot 			connector->dpms = DRM_MODE_DPMS_ON;
4575718399fSFrançois Tigeot 			drm_connector_property_set_value(connector,
4585718399fSFrançois Tigeot 							 dev->mode_config.dpms_property,
4595718399fSFrançois Tigeot 							 DRM_MODE_DPMS_ON);
4605718399fSFrançois Tigeot 		}
4615718399fSFrançois Tigeot 		/* Found a CRTC on this fb, now find encoders */
4625718399fSFrançois Tigeot 		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
4635718399fSFrançois Tigeot 			if (encoder->crtc == crtc) {
4645718399fSFrançois Tigeot 				struct drm_encoder_helper_funcs *encoder_funcs;
4655718399fSFrançois Tigeot 
4665718399fSFrançois Tigeot 				encoder_funcs = encoder->helper_private;
4675718399fSFrançois Tigeot 				encoder_funcs->dpms(encoder, DRM_MODE_DPMS_ON);
4685718399fSFrançois Tigeot 			}
4695718399fSFrançois Tigeot 		}
4705718399fSFrançois Tigeot 	}
4715718399fSFrançois Tigeot 	sx_xunlock(&dev->mode_config.mutex);
4725718399fSFrançois Tigeot }
4735718399fSFrançois Tigeot #endif
4745718399fSFrançois Tigeot 
4755718399fSFrançois Tigeot #if 0
4765718399fSFrançois Tigeot static void drm_fb_helper_off(struct fb_info *info, int dpms_mode)
4775718399fSFrançois Tigeot {
4785718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
4795718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
4805718399fSFrançois Tigeot 	struct drm_crtc *crtc;
4815718399fSFrançois Tigeot 	struct drm_crtc_helper_funcs *crtc_funcs;
4825718399fSFrançois Tigeot 	struct drm_connector *connector;
4835718399fSFrançois Tigeot 	struct drm_encoder *encoder;
4845718399fSFrançois Tigeot 	int i, j;
4855718399fSFrançois Tigeot 
4865718399fSFrançois Tigeot 	/*
4875718399fSFrançois Tigeot 	 * For each CRTC in this fb, find all associated encoders
4885718399fSFrançois Tigeot 	 * and turn them off, then turn off the CRTC.
4895718399fSFrançois Tigeot 	 */
4905718399fSFrançois Tigeot 	sx_xlock(&dev->mode_config.mutex);
4915718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
4925718399fSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
4935718399fSFrançois Tigeot 		crtc_funcs = crtc->helper_private;
4945718399fSFrançois Tigeot 
4955718399fSFrançois Tigeot 		if (!crtc->enabled)
4965718399fSFrançois Tigeot 			continue;
4975718399fSFrançois Tigeot 
4985718399fSFrançois Tigeot 		/* Walk the connectors on this fb and mark them off */
4995718399fSFrançois Tigeot 		for (j = 0; j < fb_helper->connector_count; j++) {
5005718399fSFrançois Tigeot 			connector = fb_helper->connector_info[j]->connector;
5015718399fSFrançois Tigeot 			connector->dpms = dpms_mode;
5025718399fSFrançois Tigeot 			drm_connector_property_set_value(connector,
5035718399fSFrançois Tigeot 							 dev->mode_config.dpms_property,
5045718399fSFrançois Tigeot 							 dpms_mode);
5055718399fSFrançois Tigeot 		}
5065718399fSFrançois Tigeot 		/* Found a CRTC on this fb, now find encoders */
5075718399fSFrançois Tigeot 		list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
5085718399fSFrançois Tigeot 			if (encoder->crtc == crtc) {
5095718399fSFrançois Tigeot 				struct drm_encoder_helper_funcs *encoder_funcs;
5105718399fSFrançois Tigeot 
5115718399fSFrançois Tigeot 				encoder_funcs = encoder->helper_private;
5125718399fSFrançois Tigeot 				encoder_funcs->dpms(encoder, dpms_mode);
5135718399fSFrançois Tigeot 			}
5145718399fSFrançois Tigeot 		}
5155718399fSFrançois Tigeot 		crtc_funcs->dpms(crtc, DRM_MODE_DPMS_OFF);
5165718399fSFrançois Tigeot 	}
5175718399fSFrançois Tigeot 	sx_xunlock(&dev->mode_config.mutex);
5185718399fSFrançois Tigeot }
5195718399fSFrançois Tigeot #endif
5205718399fSFrançois Tigeot 
5215718399fSFrançois Tigeot #if 0
5225718399fSFrançois Tigeot int drm_fb_helper_blank(int blank, struct fb_info *info)
5235718399fSFrançois Tigeot {
5245718399fSFrançois Tigeot 	switch (blank) {
5255718399fSFrançois Tigeot 	/* Display: On; HSync: On, VSync: On */
5265718399fSFrançois Tigeot 	case FB_BLANK_UNBLANK:
5275718399fSFrançois Tigeot 		drm_fb_helper_on(info);
5285718399fSFrançois Tigeot 		break;
5295718399fSFrançois Tigeot 	/* Display: Off; HSync: On, VSync: On */
5305718399fSFrançois Tigeot 	case FB_BLANK_NORMAL:
5315718399fSFrançois Tigeot 		drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
5325718399fSFrançois Tigeot 		break;
5335718399fSFrançois Tigeot 	/* Display: Off; HSync: Off, VSync: On */
5345718399fSFrançois Tigeot 	case FB_BLANK_HSYNC_SUSPEND:
5355718399fSFrançois Tigeot 		drm_fb_helper_off(info, DRM_MODE_DPMS_STANDBY);
5365718399fSFrançois Tigeot 		break;
5375718399fSFrançois Tigeot 	/* Display: Off; HSync: On, VSync: Off */
5385718399fSFrançois Tigeot 	case FB_BLANK_VSYNC_SUSPEND:
5395718399fSFrançois Tigeot 		drm_fb_helper_off(info, DRM_MODE_DPMS_SUSPEND);
5405718399fSFrançois Tigeot 		break;
5415718399fSFrançois Tigeot 	/* Display: Off; HSync: Off, VSync: Off */
5425718399fSFrançois Tigeot 	case FB_BLANK_POWERDOWN:
5435718399fSFrançois Tigeot 		drm_fb_helper_off(info, DRM_MODE_DPMS_OFF);
5445718399fSFrançois Tigeot 		break;
5455718399fSFrançois Tigeot 	}
5465718399fSFrançois Tigeot 	return 0;
5475718399fSFrançois Tigeot }
5485718399fSFrançois Tigeot #endif
5495718399fSFrançois Tigeot 
5505718399fSFrançois Tigeot static void drm_fb_helper_crtc_free(struct drm_fb_helper *helper)
5515718399fSFrançois Tigeot {
5525718399fSFrançois Tigeot 	int i;
5535718399fSFrançois Tigeot 
5545718399fSFrançois Tigeot 	for (i = 0; i < helper->connector_count; i++)
5555718399fSFrançois Tigeot 		drm_free(helper->connector_info[i], DRM_MEM_KMS);
5565718399fSFrançois Tigeot 	drm_free(helper->connector_info, DRM_MEM_KMS);
5576f486c69SFrançois Tigeot 	for (i = 0; i < helper->crtc_count; i++) {
5585718399fSFrançois Tigeot 		drm_free(helper->crtc_info[i].mode_set.connectors, DRM_MEM_KMS);
5596f486c69SFrançois Tigeot 		if (helper->crtc_info[i].mode_set.mode)
5606f486c69SFrançois Tigeot 			drm_mode_destroy(helper->dev, helper->crtc_info[i].mode_set.mode);
5616f486c69SFrançois Tigeot 	}
5625718399fSFrançois Tigeot 	drm_free(helper->crtc_info, DRM_MEM_KMS);
5635718399fSFrançois Tigeot }
5645718399fSFrançois Tigeot 
5655718399fSFrançois Tigeot int drm_fb_helper_init(struct drm_device *dev,
5665718399fSFrançois Tigeot 		       struct drm_fb_helper *fb_helper,
5675718399fSFrançois Tigeot 		       int crtc_count, int max_conn_count)
5685718399fSFrançois Tigeot {
5695718399fSFrançois Tigeot 	struct drm_crtc *crtc;
5705718399fSFrançois Tigeot 	int i;
5715718399fSFrançois Tigeot 
5725718399fSFrançois Tigeot 	fb_helper->dev = dev;
5735718399fSFrançois Tigeot 
5745718399fSFrançois Tigeot 	INIT_LIST_HEAD(&fb_helper->kernel_fb_list);
5755718399fSFrançois Tigeot 
5765718399fSFrançois Tigeot 	fb_helper->crtc_info = kmalloc(crtc_count *
5775718399fSFrançois Tigeot 	    sizeof(struct drm_fb_helper_crtc), DRM_MEM_KMS, M_WAITOK | M_ZERO);
5785718399fSFrançois Tigeot 
5795718399fSFrançois Tigeot 	fb_helper->crtc_count = crtc_count;
5805718399fSFrançois Tigeot 	fb_helper->connector_info = kmalloc(dev->mode_config.num_connector *
5815718399fSFrançois Tigeot 	    sizeof(struct drm_fb_helper_connector *), DRM_MEM_KMS,
5825718399fSFrançois Tigeot 	    M_WAITOK | M_ZERO);
5835718399fSFrançois Tigeot 	fb_helper->connector_count = 0;
5845718399fSFrançois Tigeot 
5855718399fSFrançois Tigeot 	for (i = 0; i < crtc_count; i++) {
5865718399fSFrançois Tigeot 		fb_helper->crtc_info[i].mode_set.connectors =
5875718399fSFrançois Tigeot 			kmalloc(max_conn_count * sizeof(struct drm_connector *),
5885718399fSFrançois Tigeot 			    DRM_MEM_KMS, M_WAITOK | M_ZERO);
5895718399fSFrançois Tigeot 
5905718399fSFrançois Tigeot 		fb_helper->crtc_info[i].mode_set.num_connectors = 0;
5915718399fSFrançois Tigeot 	}
5925718399fSFrançois Tigeot 
5935718399fSFrançois Tigeot 	i = 0;
5945718399fSFrançois Tigeot 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
5955718399fSFrançois Tigeot 		fb_helper->crtc_info[i].crtc_id = crtc->base.id;
5965718399fSFrançois Tigeot 		fb_helper->crtc_info[i].mode_set.crtc = crtc;
5975718399fSFrançois Tigeot 		i++;
5985718399fSFrançois Tigeot 	}
5995718399fSFrançois Tigeot 	fb_helper->conn_limit = max_conn_count;
6005718399fSFrançois Tigeot 	return 0;
6015718399fSFrançois Tigeot }
6025718399fSFrançois Tigeot 
6035718399fSFrançois Tigeot void drm_fb_helper_fini(struct drm_fb_helper *fb_helper)
6045718399fSFrançois Tigeot {
6055718399fSFrançois Tigeot 	if (!list_empty(&fb_helper->kernel_fb_list)) {
6065718399fSFrançois Tigeot 		list_del(&fb_helper->kernel_fb_list);
6075718399fSFrançois Tigeot 		if (list_empty(&kernel_fb_helper_list)) {
6085718399fSFrançois Tigeot #if 0
6095718399fSFrançois Tigeot 			printk(KERN_INFO "drm: unregistered panic notifier\n");
6105718399fSFrançois Tigeot 			atomic_notifier_chain_unregister(&panic_notifier_list,
6115718399fSFrançois Tigeot 							 &paniced);
6125718399fSFrançois Tigeot 			unregister_sysrq_key('v', &sysrq_drm_fb_helper_restore_op);
6135718399fSFrançois Tigeot #endif
6145718399fSFrançois Tigeot 		}
6155718399fSFrançois Tigeot 	}
6165718399fSFrançois Tigeot 
6175718399fSFrançois Tigeot 	drm_fb_helper_crtc_free(fb_helper);
6185718399fSFrançois Tigeot 
6195718399fSFrançois Tigeot }
6205718399fSFrançois Tigeot 
6215718399fSFrançois Tigeot #if 0
6225718399fSFrançois Tigeot static int setcolreg(struct drm_crtc *crtc, u16 red, u16 green,
6235718399fSFrançois Tigeot 		     u16 blue, u16 regno, struct fb_info *info)
6245718399fSFrançois Tigeot {
6255718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
6265718399fSFrançois Tigeot 	struct drm_framebuffer *fb = fb_helper->fb;
6275718399fSFrançois Tigeot 	int pindex;
6285718399fSFrançois Tigeot 
6295718399fSFrançois Tigeot 	if (info->fix.visual == FB_VISUAL_trueCOLOR) {
6305718399fSFrançois Tigeot 		u32 *palette;
6315718399fSFrançois Tigeot 		u32 value;
6325718399fSFrançois Tigeot 		/* place color in psuedopalette */
6335718399fSFrançois Tigeot 		if (regno > 16)
6345718399fSFrançois Tigeot 			return -EINVAL;
6355718399fSFrançois Tigeot 		palette = (u32 *)info->pseudo_palette;
6365718399fSFrançois Tigeot 		red >>= (16 - info->var.red.length);
6375718399fSFrançois Tigeot 		green >>= (16 - info->var.green.length);
6385718399fSFrançois Tigeot 		blue >>= (16 - info->var.blue.length);
6395718399fSFrançois Tigeot 		value = (red << info->var.red.offset) |
6405718399fSFrançois Tigeot 			(green << info->var.green.offset) |
6415718399fSFrançois Tigeot 			(blue << info->var.blue.offset);
6425718399fSFrançois Tigeot 		if (info->var.transp.length > 0) {
6435718399fSFrançois Tigeot 			u32 mask = (1 << info->var.transp.length) - 1;
6445718399fSFrançois Tigeot 			mask <<= info->var.transp.offset;
6455718399fSFrançois Tigeot 			value |= mask;
6465718399fSFrançois Tigeot 		}
6475718399fSFrançois Tigeot 		palette[regno] = value;
6485718399fSFrançois Tigeot 		return 0;
6495718399fSFrançois Tigeot 	}
6505718399fSFrançois Tigeot 
6515718399fSFrançois Tigeot 	pindex = regno;
6525718399fSFrançois Tigeot 
6535718399fSFrançois Tigeot 	if (fb->bits_per_pixel == 16) {
6545718399fSFrançois Tigeot 		pindex = regno << 3;
6555718399fSFrançois Tigeot 
6565718399fSFrançois Tigeot 		if (fb->depth == 16 && regno > 63)
6575718399fSFrançois Tigeot 			return -EINVAL;
6585718399fSFrançois Tigeot 		if (fb->depth == 15 && regno > 31)
6595718399fSFrançois Tigeot 			return -EINVAL;
6605718399fSFrançois Tigeot 
6615718399fSFrançois Tigeot 		if (fb->depth == 16) {
6625718399fSFrançois Tigeot 			u16 r, g, b;
6635718399fSFrançois Tigeot 			int i;
6645718399fSFrançois Tigeot 			if (regno < 32) {
6655718399fSFrançois Tigeot 				for (i = 0; i < 8; i++)
6665718399fSFrançois Tigeot 					fb_helper->funcs->gamma_set(crtc, red,
6675718399fSFrançois Tigeot 						green, blue, pindex + i);
6685718399fSFrançois Tigeot 			}
6695718399fSFrançois Tigeot 
6705718399fSFrançois Tigeot 			fb_helper->funcs->gamma_get(crtc, &r,
6715718399fSFrançois Tigeot 						    &g, &b,
6725718399fSFrançois Tigeot 						    pindex >> 1);
6735718399fSFrançois Tigeot 
6745718399fSFrançois Tigeot 			for (i = 0; i < 4; i++)
6755718399fSFrançois Tigeot 				fb_helper->funcs->gamma_set(crtc, r,
6765718399fSFrançois Tigeot 							    green, b,
6775718399fSFrançois Tigeot 							    (pindex >> 1) + i);
6785718399fSFrançois Tigeot 		}
6795718399fSFrançois Tigeot 	}
6805718399fSFrançois Tigeot 
6815718399fSFrançois Tigeot 	if (fb->depth != 16)
6825718399fSFrançois Tigeot 		fb_helper->funcs->gamma_set(crtc, red, green, blue, pindex);
6835718399fSFrançois Tigeot 	return 0;
6845718399fSFrançois Tigeot }
6855718399fSFrançois Tigeot #endif
6865718399fSFrançois Tigeot 
6875718399fSFrançois Tigeot #if 0
6885718399fSFrançois Tigeot int drm_fb_helper_setcmap(struct fb_cmap *cmap, struct fb_info *info)
6895718399fSFrançois Tigeot {
6905718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
6915718399fSFrançois Tigeot 	struct drm_crtc_helper_funcs *crtc_funcs;
6925718399fSFrançois Tigeot 	u16 *red, *green, *blue, *transp;
6935718399fSFrançois Tigeot 	struct drm_crtc *crtc;
6945718399fSFrançois Tigeot 	int i, j, rc = 0;
6955718399fSFrançois Tigeot 	int start;
6965718399fSFrançois Tigeot 
6975718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
6985718399fSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
6995718399fSFrançois Tigeot 		crtc_funcs = crtc->helper_private;
7005718399fSFrançois Tigeot 
7015718399fSFrançois Tigeot 		red = cmap->red;
7025718399fSFrançois Tigeot 		green = cmap->green;
7035718399fSFrançois Tigeot 		blue = cmap->blue;
7045718399fSFrançois Tigeot 		transp = cmap->transp;
7055718399fSFrançois Tigeot 		start = cmap->start;
7065718399fSFrançois Tigeot 
7075718399fSFrançois Tigeot 		for (j = 0; j < cmap->len; j++) {
7085718399fSFrançois Tigeot 			u16 hred, hgreen, hblue, htransp = 0xffff;
7095718399fSFrançois Tigeot 
7105718399fSFrançois Tigeot 			hred = *red++;
7115718399fSFrançois Tigeot 			hgreen = *green++;
7125718399fSFrançois Tigeot 			hblue = *blue++;
7135718399fSFrançois Tigeot 
7145718399fSFrançois Tigeot 			if (transp)
7155718399fSFrançois Tigeot 				htransp = *transp++;
7165718399fSFrançois Tigeot 
7175718399fSFrançois Tigeot 			rc = setcolreg(crtc, hred, hgreen, hblue, start++, info);
7185718399fSFrançois Tigeot 			if (rc)
7195718399fSFrançois Tigeot 				return rc;
7205718399fSFrançois Tigeot 		}
7215718399fSFrançois Tigeot 		crtc_funcs->load_lut(crtc);
7225718399fSFrançois Tigeot 	}
7235718399fSFrançois Tigeot 	return rc;
7245718399fSFrançois Tigeot }
7255718399fSFrançois Tigeot #endif
7265718399fSFrançois Tigeot 
7275718399fSFrançois Tigeot #if 0
7285718399fSFrançois Tigeot int drm_fb_helper_check_var(struct fb_var_screeninfo *var,
7295718399fSFrançois Tigeot 			    struct fb_info *info)
7305718399fSFrançois Tigeot {
7315718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
7325718399fSFrançois Tigeot 	struct drm_framebuffer *fb = fb_helper->fb;
7335718399fSFrançois Tigeot 	int depth;
7345718399fSFrançois Tigeot 
7355718399fSFrançois Tigeot 	if (var->pixclock != 0 || in_dbg_master())
7365718399fSFrançois Tigeot 		return -EINVAL;
7375718399fSFrançois Tigeot 
7385718399fSFrançois Tigeot 	/* Need to resize the fb object !!! */
7395718399fSFrançois Tigeot 	if (var->bits_per_pixel > fb->bits_per_pixel ||
7405718399fSFrançois Tigeot 	    var->xres > fb->width || var->yres > fb->height ||
7415718399fSFrançois Tigeot 	    var->xres_virtual > fb->width || var->yres_virtual > fb->height) {
7425718399fSFrançois Tigeot 		DRM_DEBUG("fb userspace requested width/height/bpp is greater than current fb "
7435718399fSFrançois Tigeot 			  "request %dx%d-%d (virtual %dx%d) > %dx%d-%d\n",
7445718399fSFrançois Tigeot 			  var->xres, var->yres, var->bits_per_pixel,
7455718399fSFrançois Tigeot 			  var->xres_virtual, var->yres_virtual,
7465718399fSFrançois Tigeot 			  fb->width, fb->height, fb->bits_per_pixel);
7475718399fSFrançois Tigeot 		return -EINVAL;
7485718399fSFrançois Tigeot 	}
7495718399fSFrançois Tigeot 
7505718399fSFrançois Tigeot 	switch (var->bits_per_pixel) {
7515718399fSFrançois Tigeot 	case 16:
7525718399fSFrançois Tigeot 		depth = (var->green.length == 6) ? 16 : 15;
7535718399fSFrançois Tigeot 		break;
7545718399fSFrançois Tigeot 	case 32:
7555718399fSFrançois Tigeot 		depth = (var->transp.length > 0) ? 32 : 24;
7565718399fSFrançois Tigeot 		break;
7575718399fSFrançois Tigeot 	default:
7585718399fSFrançois Tigeot 		depth = var->bits_per_pixel;
7595718399fSFrançois Tigeot 		break;
7605718399fSFrançois Tigeot 	}
7615718399fSFrançois Tigeot 
7625718399fSFrançois Tigeot 	switch (depth) {
7635718399fSFrançois Tigeot 	case 8:
7645718399fSFrançois Tigeot 		var->red.offset = 0;
7655718399fSFrançois Tigeot 		var->green.offset = 0;
7665718399fSFrançois Tigeot 		var->blue.offset = 0;
7675718399fSFrançois Tigeot 		var->red.length = 8;
7685718399fSFrançois Tigeot 		var->green.length = 8;
7695718399fSFrançois Tigeot 		var->blue.length = 8;
7705718399fSFrançois Tigeot 		var->transp.length = 0;
7715718399fSFrançois Tigeot 		var->transp.offset = 0;
7725718399fSFrançois Tigeot 		break;
7735718399fSFrançois Tigeot 	case 15:
7745718399fSFrançois Tigeot 		var->red.offset = 10;
7755718399fSFrançois Tigeot 		var->green.offset = 5;
7765718399fSFrançois Tigeot 		var->blue.offset = 0;
7775718399fSFrançois Tigeot 		var->red.length = 5;
7785718399fSFrançois Tigeot 		var->green.length = 5;
7795718399fSFrançois Tigeot 		var->blue.length = 5;
7805718399fSFrançois Tigeot 		var->transp.length = 1;
7815718399fSFrançois Tigeot 		var->transp.offset = 15;
7825718399fSFrançois Tigeot 		break;
7835718399fSFrançois Tigeot 	case 16:
7845718399fSFrançois Tigeot 		var->red.offset = 11;
7855718399fSFrançois Tigeot 		var->green.offset = 5;
7865718399fSFrançois Tigeot 		var->blue.offset = 0;
7875718399fSFrançois Tigeot 		var->red.length = 5;
7885718399fSFrançois Tigeot 		var->green.length = 6;
7895718399fSFrançois Tigeot 		var->blue.length = 5;
7905718399fSFrançois Tigeot 		var->transp.length = 0;
7915718399fSFrançois Tigeot 		var->transp.offset = 0;
7925718399fSFrançois Tigeot 		break;
7935718399fSFrançois Tigeot 	case 24:
7945718399fSFrançois Tigeot 		var->red.offset = 16;
7955718399fSFrançois Tigeot 		var->green.offset = 8;
7965718399fSFrançois Tigeot 		var->blue.offset = 0;
7975718399fSFrançois Tigeot 		var->red.length = 8;
7985718399fSFrançois Tigeot 		var->green.length = 8;
7995718399fSFrançois Tigeot 		var->blue.length = 8;
8005718399fSFrançois Tigeot 		var->transp.length = 0;
8015718399fSFrançois Tigeot 		var->transp.offset = 0;
8025718399fSFrançois Tigeot 		break;
8035718399fSFrançois Tigeot 	case 32:
8045718399fSFrançois Tigeot 		var->red.offset = 16;
8055718399fSFrançois Tigeot 		var->green.offset = 8;
8065718399fSFrançois Tigeot 		var->blue.offset = 0;
8075718399fSFrançois Tigeot 		var->red.length = 8;
8085718399fSFrançois Tigeot 		var->green.length = 8;
8095718399fSFrançois Tigeot 		var->blue.length = 8;
8105718399fSFrançois Tigeot 		var->transp.length = 8;
8115718399fSFrançois Tigeot 		var->transp.offset = 24;
8125718399fSFrançois Tigeot 		break;
8135718399fSFrançois Tigeot 	default:
8145718399fSFrançois Tigeot 		return -EINVAL;
8155718399fSFrançois Tigeot 	}
8165718399fSFrançois Tigeot 	return 0;
8175718399fSFrançois Tigeot }
8185718399fSFrançois Tigeot #endif
8195718399fSFrançois Tigeot 
8205718399fSFrançois Tigeot #if 0
8215718399fSFrançois Tigeot /* this will let fbcon do the mode init */
8225718399fSFrançois Tigeot int drm_fb_helper_set_par(struct fb_info *info)
8235718399fSFrançois Tigeot {
8245718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
8255718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
8265718399fSFrançois Tigeot 	struct fb_var_screeninfo *var = &info->var;
8275718399fSFrançois Tigeot 	struct drm_crtc *crtc;
8285718399fSFrançois Tigeot 	int ret;
8295718399fSFrançois Tigeot 	int i;
8305718399fSFrançois Tigeot 
8315718399fSFrançois Tigeot 	if (var->pixclock != 0) {
8325718399fSFrançois Tigeot 		DRM_ERROR("PIXEL CLOCK SET\n");
8335718399fSFrançois Tigeot 		return -EINVAL;
8345718399fSFrançois Tigeot 	}
8355718399fSFrançois Tigeot 
8365718399fSFrançois Tigeot 	mutex_lock(&dev->mode_config.mutex);
8375718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
8385718399fSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
8395718399fSFrançois Tigeot 		ret = crtc->funcs->set_config(&fb_helper->crtc_info[i].mode_set);
8405718399fSFrançois Tigeot 		if (ret) {
8415718399fSFrançois Tigeot 			mutex_unlock(&dev->mode_config.mutex);
8425718399fSFrançois Tigeot 			return ret;
8435718399fSFrançois Tigeot 		}
8445718399fSFrançois Tigeot 	}
8455718399fSFrançois Tigeot 	mutex_unlock(&dev->mode_config.mutex);
8465718399fSFrançois Tigeot 
8475718399fSFrançois Tigeot 	if (fb_helper->delayed_hotplug) {
8485718399fSFrançois Tigeot 		fb_helper->delayed_hotplug = false;
8495718399fSFrançois Tigeot 		drm_fb_helper_hotplug_event(fb_helper);
8505718399fSFrançois Tigeot 	}
8515718399fSFrançois Tigeot 	return 0;
8525718399fSFrançois Tigeot }
8535718399fSFrançois Tigeot #endif
8545718399fSFrançois Tigeot 
8555718399fSFrançois Tigeot #if 0
8565718399fSFrançois Tigeot int drm_fb_helper_pan_display(struct fb_var_screeninfo *var,
8575718399fSFrançois Tigeot 			      struct fb_info *info)
8585718399fSFrançois Tigeot {
8595718399fSFrançois Tigeot 	struct drm_fb_helper *fb_helper = info->par;
8605718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
8615718399fSFrançois Tigeot 	struct drm_mode_set *modeset;
8625718399fSFrançois Tigeot 	struct drm_crtc *crtc;
8635718399fSFrançois Tigeot 	int ret = 0;
8645718399fSFrançois Tigeot 	int i;
8655718399fSFrançois Tigeot 
8665718399fSFrançois Tigeot 	mutex_lock(&dev->mode_config.mutex);
8675718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
8685718399fSFrançois Tigeot 		crtc = fb_helper->crtc_info[i].mode_set.crtc;
8695718399fSFrançois Tigeot 
8705718399fSFrançois Tigeot 		modeset = &fb_helper->crtc_info[i].mode_set;
8715718399fSFrançois Tigeot 
8725718399fSFrançois Tigeot 		modeset->x = var->xoffset;
8735718399fSFrançois Tigeot 		modeset->y = var->yoffset;
8745718399fSFrançois Tigeot 
8755718399fSFrançois Tigeot 		if (modeset->num_connectors) {
8765718399fSFrançois Tigeot 			ret = crtc->funcs->set_config(modeset);
8775718399fSFrançois Tigeot 			if (!ret) {
8785718399fSFrançois Tigeot 				info->var.xoffset = var->xoffset;
8795718399fSFrançois Tigeot 				info->var.yoffset = var->yoffset;
8805718399fSFrançois Tigeot 			}
8815718399fSFrançois Tigeot 		}
8825718399fSFrançois Tigeot 	}
8835718399fSFrançois Tigeot 	mutex_unlock(&dev->mode_config.mutex);
8845718399fSFrançois Tigeot 	return ret;
8855718399fSFrançois Tigeot }
8865718399fSFrançois Tigeot #endif
8875718399fSFrançois Tigeot 
8885718399fSFrançois Tigeot int drm_fb_helper_single_fb_probe(struct drm_fb_helper *fb_helper,
8895718399fSFrançois Tigeot 				  int preferred_bpp)
8905718399fSFrançois Tigeot {
8915718399fSFrançois Tigeot 	int new_fb = 0;
8925718399fSFrançois Tigeot 	int crtc_count = 0;
8935718399fSFrançois Tigeot 	int i;
8945718399fSFrançois Tigeot #if 0
8955718399fSFrançois Tigeot 	struct fb_info *info;
8965718399fSFrançois Tigeot #endif
8975718399fSFrançois Tigeot 	struct drm_fb_helper_surface_size sizes;
8985718399fSFrançois Tigeot 	int gamma_size = 0;
8995718399fSFrançois Tigeot 
9005718399fSFrançois Tigeot 	memset(&sizes, 0, sizeof(struct drm_fb_helper_surface_size));
9015718399fSFrançois Tigeot 	sizes.surface_depth = 24;
9025718399fSFrançois Tigeot 	sizes.surface_bpp = 32;
9035718399fSFrançois Tigeot 	sizes.fb_width = (unsigned)-1;
9045718399fSFrançois Tigeot 	sizes.fb_height = (unsigned)-1;
9055718399fSFrançois Tigeot 
9065718399fSFrançois Tigeot 	/* if driver picks 8 or 16 by default use that
9075718399fSFrançois Tigeot 	   for both depth/bpp */
9085718399fSFrançois Tigeot 	if (preferred_bpp != sizes.surface_bpp) {
9095718399fSFrançois Tigeot 		sizes.surface_depth = sizes.surface_bpp = preferred_bpp;
9105718399fSFrançois Tigeot 	}
9115718399fSFrançois Tigeot 	/* first up get a count of crtcs now in use and new min/maxes width/heights */
9125718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
9135718399fSFrançois Tigeot 		struct drm_fb_helper_connector *fb_helper_conn = fb_helper->connector_info[i];
9145718399fSFrançois Tigeot 		struct drm_fb_helper_cmdline_mode *cmdline_mode;
9155718399fSFrançois Tigeot 
9165718399fSFrançois Tigeot 		cmdline_mode = &fb_helper_conn->cmdline_mode;
9175718399fSFrançois Tigeot 
9185718399fSFrançois Tigeot 		if (cmdline_mode->bpp_specified) {
9195718399fSFrançois Tigeot 			switch (cmdline_mode->bpp) {
9205718399fSFrançois Tigeot 			case 8:
9215718399fSFrançois Tigeot 				sizes.surface_depth = sizes.surface_bpp = 8;
9225718399fSFrançois Tigeot 				break;
9235718399fSFrançois Tigeot 			case 15:
9245718399fSFrançois Tigeot 				sizes.surface_depth = 15;
9255718399fSFrançois Tigeot 				sizes.surface_bpp = 16;
9265718399fSFrançois Tigeot 				break;
9275718399fSFrançois Tigeot 			case 16:
9285718399fSFrançois Tigeot 				sizes.surface_depth = sizes.surface_bpp = 16;
9295718399fSFrançois Tigeot 				break;
9305718399fSFrançois Tigeot 			case 24:
9315718399fSFrançois Tigeot 				sizes.surface_depth = sizes.surface_bpp = 24;
9325718399fSFrançois Tigeot 				break;
9335718399fSFrançois Tigeot 			case 32:
9345718399fSFrançois Tigeot 				sizes.surface_depth = 24;
9355718399fSFrançois Tigeot 				sizes.surface_bpp = 32;
9365718399fSFrançois Tigeot 				break;
9375718399fSFrançois Tigeot 			}
9385718399fSFrançois Tigeot 			break;
9395718399fSFrançois Tigeot 		}
9405718399fSFrançois Tigeot 	}
9415718399fSFrançois Tigeot 
9425718399fSFrançois Tigeot 	crtc_count = 0;
9435718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
9445718399fSFrançois Tigeot 		struct drm_display_mode *desired_mode;
9455718399fSFrançois Tigeot 		desired_mode = fb_helper->crtc_info[i].desired_mode;
9465718399fSFrançois Tigeot 
9475718399fSFrançois Tigeot 		if (desired_mode) {
9485718399fSFrançois Tigeot 			if (gamma_size == 0)
9495718399fSFrançois Tigeot 				gamma_size = fb_helper->crtc_info[i].mode_set.crtc->gamma_size;
9505718399fSFrançois Tigeot 			if (desired_mode->hdisplay < sizes.fb_width)
9515718399fSFrançois Tigeot 				sizes.fb_width = desired_mode->hdisplay;
9525718399fSFrançois Tigeot 			if (desired_mode->vdisplay < sizes.fb_height)
9535718399fSFrançois Tigeot 				sizes.fb_height = desired_mode->vdisplay;
9545718399fSFrançois Tigeot 			if (desired_mode->hdisplay > sizes.surface_width)
9555718399fSFrançois Tigeot 				sizes.surface_width = desired_mode->hdisplay;
9565718399fSFrançois Tigeot 			if (desired_mode->vdisplay > sizes.surface_height)
9575718399fSFrançois Tigeot 				sizes.surface_height = desired_mode->vdisplay;
9585718399fSFrançois Tigeot 			crtc_count++;
9595718399fSFrançois Tigeot 		}
9605718399fSFrançois Tigeot 	}
9615718399fSFrançois Tigeot 
9625718399fSFrançois Tigeot 	if (crtc_count == 0 || sizes.fb_width == -1 || sizes.fb_height == -1) {
9635718399fSFrançois Tigeot 		/* hmm everyone went away - assume VGA cable just fell out
9645718399fSFrançois Tigeot 		   and will come back later. */
9655718399fSFrançois Tigeot 		DRM_INFO("Cannot find any crtc or sizes - going 1024x768\n");
9665718399fSFrançois Tigeot 		sizes.fb_width = sizes.surface_width = 1024;
9675718399fSFrançois Tigeot 		sizes.fb_height = sizes.surface_height = 768;
9685718399fSFrançois Tigeot 	}
9695718399fSFrançois Tigeot 
9705718399fSFrançois Tigeot 	/* push down into drivers */
9715718399fSFrançois Tigeot 	new_fb = (*fb_helper->funcs->fb_probe)(fb_helper, &sizes);
9725718399fSFrançois Tigeot 	if (new_fb < 0)
9735718399fSFrançois Tigeot 		return new_fb;
9745718399fSFrançois Tigeot 
9755718399fSFrançois Tigeot #if 0
9765718399fSFrançois Tigeot 	info = fb_helper->fbdev;
9775718399fSFrançois Tigeot #endif
9785718399fSFrançois Tigeot 
9795718399fSFrançois Tigeot 	/* set the fb pointer */
9805718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
9815718399fSFrançois Tigeot 		fb_helper->crtc_info[i].mode_set.fb = fb_helper->fb;
9825718399fSFrançois Tigeot 	}
9835718399fSFrançois Tigeot 
9845718399fSFrançois Tigeot #if 0
9855718399fSFrançois Tigeot 	if (new_fb) {
9865718399fSFrançois Tigeot 		info->var.pixclock = 0;
9875718399fSFrançois Tigeot 		if (register_framebuffer(info) < 0) {
9885718399fSFrançois Tigeot 			return -EINVAL;
9895718399fSFrançois Tigeot 		}
9905718399fSFrançois Tigeot 
9915718399fSFrançois Tigeot 		printf("fb%d: %s frame buffer device\n", info->node,
9925718399fSFrançois Tigeot 		       info->fix.id);
9935718399fSFrançois Tigeot 
9945718399fSFrançois Tigeot 	} else {
9955718399fSFrançois Tigeot 		drm_fb_helper_set_par(info);
9965718399fSFrançois Tigeot 	}
9975718399fSFrançois Tigeot 
9985718399fSFrançois Tigeot 	/* Switch back to kernel console on panic */
9995718399fSFrançois Tigeot 	/* multi card linked list maybe */
10005718399fSFrançois Tigeot 	if (list_empty(&kernel_fb_helper_list)) {
10015718399fSFrançois Tigeot 		printf("drm: registered panic notifier\n");
10025718399fSFrançois Tigeot 		atomic_notifier_chain_register(&panic_notifier_list,
10035718399fSFrançois Tigeot 					       &paniced);
10045718399fSFrançois Tigeot 	}
10055718399fSFrançois Tigeot 	if (new_fb)
10065718399fSFrançois Tigeot 		list_add(&fb_helper->kernel_fb_list, &kernel_fb_helper_list);
10075718399fSFrançois Tigeot #endif
10085718399fSFrançois Tigeot 
10095718399fSFrançois Tigeot 	return 0;
10105718399fSFrançois Tigeot }
10115718399fSFrançois Tigeot 
10125718399fSFrançois Tigeot #if 0
10135718399fSFrançois Tigeot void drm_fb_helper_fill_fix(struct fb_info *info, uint32_t pitch,
10145718399fSFrançois Tigeot 			    uint32_t depth)
10155718399fSFrançois Tigeot {
10165718399fSFrançois Tigeot 	info->fix.type = FB_TYPE_PACKED_PIXELS;
10175718399fSFrançois Tigeot 	info->fix.visual = depth == 8 ? FB_VISUAL_PSEUDOCOLOR :
10185718399fSFrançois Tigeot 		FB_VISUAL_trueCOLOR;
10195718399fSFrançois Tigeot 	info->fix.mmio_start = 0;
10205718399fSFrançois Tigeot 	info->fix.mmio_len = 0;
10215718399fSFrançois Tigeot 	info->fix.type_aux = 0;
10225718399fSFrançois Tigeot 	info->fix.xpanstep = 1; /* doing it in hw */
10235718399fSFrançois Tigeot 	info->fix.ypanstep = 1; /* doing it in hw */
10245718399fSFrançois Tigeot 	info->fix.ywrapstep = 0;
10255718399fSFrançois Tigeot 	info->fix.accel = FB_ACCEL_NONE;
10265718399fSFrançois Tigeot 	info->fix.type_aux = 0;
10275718399fSFrançois Tigeot 
10285718399fSFrançois Tigeot 	info->fix.line_length = pitch;
10295718399fSFrançois Tigeot 	return;
10305718399fSFrançois Tigeot }
10315718399fSFrançois Tigeot 
10325718399fSFrançois Tigeot void drm_fb_helper_fill_var(struct fb_info *info, struct drm_fb_helper *fb_helper,
10335718399fSFrançois Tigeot 			    uint32_t fb_width, uint32_t fb_height)
10345718399fSFrançois Tigeot {
10355718399fSFrançois Tigeot 	struct drm_framebuffer *fb = fb_helper->fb;
10365718399fSFrançois Tigeot 	info->pseudo_palette = fb_helper->pseudo_palette;
10375718399fSFrançois Tigeot 	info->var.xres_virtual = fb->width;
10385718399fSFrançois Tigeot 	info->var.yres_virtual = fb->height;
10395718399fSFrançois Tigeot 	info->var.bits_per_pixel = fb->bits_per_pixel;
10405718399fSFrançois Tigeot 	info->var.accel_flags = FB_ACCELF_TEXT;
10415718399fSFrançois Tigeot 	info->var.xoffset = 0;
10425718399fSFrançois Tigeot 	info->var.yoffset = 0;
10435718399fSFrançois Tigeot 	info->var.activate = FB_ACTIVATE_NOW;
10445718399fSFrançois Tigeot 	info->var.height = -1;
10455718399fSFrançois Tigeot 	info->var.width = -1;
10465718399fSFrançois Tigeot 
10475718399fSFrançois Tigeot 	switch (fb->depth) {
10485718399fSFrançois Tigeot 	case 8:
10495718399fSFrançois Tigeot 		info->var.red.offset = 0;
10505718399fSFrançois Tigeot 		info->var.green.offset = 0;
10515718399fSFrançois Tigeot 		info->var.blue.offset = 0;
10525718399fSFrançois Tigeot 		info->var.red.length = 8; /* 8bit DAC */
10535718399fSFrançois Tigeot 		info->var.green.length = 8;
10545718399fSFrançois Tigeot 		info->var.blue.length = 8;
10555718399fSFrançois Tigeot 		info->var.transp.offset = 0;
10565718399fSFrançois Tigeot 		info->var.transp.length = 0;
10575718399fSFrançois Tigeot 		break;
10585718399fSFrançois Tigeot 	case 15:
10595718399fSFrançois Tigeot 		info->var.red.offset = 10;
10605718399fSFrançois Tigeot 		info->var.green.offset = 5;
10615718399fSFrançois Tigeot 		info->var.blue.offset = 0;
10625718399fSFrançois Tigeot 		info->var.red.length = 5;
10635718399fSFrançois Tigeot 		info->var.green.length = 5;
10645718399fSFrançois Tigeot 		info->var.blue.length = 5;
10655718399fSFrançois Tigeot 		info->var.transp.offset = 15;
10665718399fSFrançois Tigeot 		info->var.transp.length = 1;
10675718399fSFrançois Tigeot 		break;
10685718399fSFrançois Tigeot 	case 16:
10695718399fSFrançois Tigeot 		info->var.red.offset = 11;
10705718399fSFrançois Tigeot 		info->var.green.offset = 5;
10715718399fSFrançois Tigeot 		info->var.blue.offset = 0;
10725718399fSFrançois Tigeot 		info->var.red.length = 5;
10735718399fSFrançois Tigeot 		info->var.green.length = 6;
10745718399fSFrançois Tigeot 		info->var.blue.length = 5;
10755718399fSFrançois Tigeot 		info->var.transp.offset = 0;
10765718399fSFrançois Tigeot 		break;
10775718399fSFrançois Tigeot 	case 24:
10785718399fSFrançois Tigeot 		info->var.red.offset = 16;
10795718399fSFrançois Tigeot 		info->var.green.offset = 8;
10805718399fSFrançois Tigeot 		info->var.blue.offset = 0;
10815718399fSFrançois Tigeot 		info->var.red.length = 8;
10825718399fSFrançois Tigeot 		info->var.green.length = 8;
10835718399fSFrançois Tigeot 		info->var.blue.length = 8;
10845718399fSFrançois Tigeot 		info->var.transp.offset = 0;
10855718399fSFrançois Tigeot 		info->var.transp.length = 0;
10865718399fSFrançois Tigeot 		break;
10875718399fSFrançois Tigeot 	case 32:
10885718399fSFrançois Tigeot 		info->var.red.offset = 16;
10895718399fSFrançois Tigeot 		info->var.green.offset = 8;
10905718399fSFrançois Tigeot 		info->var.blue.offset = 0;
10915718399fSFrançois Tigeot 		info->var.red.length = 8;
10925718399fSFrançois Tigeot 		info->var.green.length = 8;
10935718399fSFrançois Tigeot 		info->var.blue.length = 8;
10945718399fSFrançois Tigeot 		info->var.transp.offset = 24;
10955718399fSFrançois Tigeot 		info->var.transp.length = 8;
10965718399fSFrançois Tigeot 		break;
10975718399fSFrançois Tigeot 	default:
10985718399fSFrançois Tigeot 		break;
10995718399fSFrançois Tigeot 	}
11005718399fSFrançois Tigeot 
11015718399fSFrançois Tigeot 	info->var.xres = fb_width;
11025718399fSFrançois Tigeot 	info->var.yres = fb_height;
11035718399fSFrançois Tigeot }
11045718399fSFrançois Tigeot #endif
11055718399fSFrançois Tigeot 
11065718399fSFrançois Tigeot static int drm_fb_helper_probe_connector_modes(struct drm_fb_helper *fb_helper,
11075718399fSFrançois Tigeot 					       uint32_t maxX,
11085718399fSFrançois Tigeot 					       uint32_t maxY)
11095718399fSFrançois Tigeot {
11105718399fSFrançois Tigeot 	struct drm_connector *connector;
11115718399fSFrançois Tigeot 	int count = 0;
11125718399fSFrançois Tigeot 	int i;
11135718399fSFrançois Tigeot 
11145718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
11155718399fSFrançois Tigeot 		connector = fb_helper->connector_info[i]->connector;
11165718399fSFrançois Tigeot 		count += connector->funcs->fill_modes(connector, maxX, maxY);
11175718399fSFrançois Tigeot 	}
11185718399fSFrançois Tigeot 
11195718399fSFrançois Tigeot 	return count;
11205718399fSFrançois Tigeot }
11215718399fSFrançois Tigeot 
11225718399fSFrançois Tigeot static struct drm_display_mode *drm_has_preferred_mode(struct drm_fb_helper_connector *fb_connector, int width, int height)
11235718399fSFrançois Tigeot {
11245718399fSFrançois Tigeot 	struct drm_display_mode *mode;
11255718399fSFrançois Tigeot 
11265718399fSFrançois Tigeot 	list_for_each_entry(mode, &fb_connector->connector->modes, head) {
11275718399fSFrançois Tigeot 		if (drm_mode_width(mode) > width ||
11285718399fSFrançois Tigeot 		    drm_mode_height(mode) > height)
11295718399fSFrançois Tigeot 			continue;
11305718399fSFrançois Tigeot 		if (mode->type & DRM_MODE_TYPE_PREFERRED)
11315718399fSFrançois Tigeot 			return mode;
11325718399fSFrançois Tigeot 	}
11335718399fSFrançois Tigeot 	return NULL;
11345718399fSFrançois Tigeot }
11355718399fSFrançois Tigeot 
11365718399fSFrançois Tigeot static bool drm_has_cmdline_mode(struct drm_fb_helper_connector *fb_connector)
11375718399fSFrançois Tigeot {
11385718399fSFrançois Tigeot 	struct drm_fb_helper_cmdline_mode *cmdline_mode;
11395718399fSFrançois Tigeot 	cmdline_mode = &fb_connector->cmdline_mode;
11405718399fSFrançois Tigeot 	return cmdline_mode->specified;
11415718399fSFrançois Tigeot }
11425718399fSFrançois Tigeot 
11435718399fSFrançois Tigeot static struct drm_display_mode *drm_pick_cmdline_mode(struct drm_fb_helper_connector *fb_helper_conn,
11445718399fSFrançois Tigeot 						      int width, int height)
11455718399fSFrançois Tigeot {
11465718399fSFrançois Tigeot 	struct drm_cmdline_mode *cmdline_mode;
11475718399fSFrançois Tigeot 	struct drm_display_mode *mode = NULL;
11485718399fSFrançois Tigeot 
11495718399fSFrançois Tigeot 	cmdline_mode = &fb_helper_conn->cmdline_mode1;
11505718399fSFrançois Tigeot 	if (cmdline_mode->specified == false &&
11515718399fSFrançois Tigeot 	    !drm_fetch_cmdline_mode_from_kenv(fb_helper_conn->connector,
11525718399fSFrançois Tigeot 	    cmdline_mode))
11535718399fSFrançois Tigeot 			return (NULL);
11545718399fSFrançois Tigeot 
11555718399fSFrançois Tigeot 	/* attempt to find a matching mode in the list of modes
11565718399fSFrançois Tigeot 	 *  we have gotten so far, if not add a CVT mode that conforms
11575718399fSFrançois Tigeot 	 */
11585718399fSFrançois Tigeot 	if (cmdline_mode->rb || cmdline_mode->margins)
11595718399fSFrançois Tigeot 		goto create_mode;
11605718399fSFrançois Tigeot 
11615718399fSFrançois Tigeot 	list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
11625718399fSFrançois Tigeot 		/* check width/height */
11635718399fSFrançois Tigeot 		if (mode->hdisplay != cmdline_mode->xres ||
11645718399fSFrançois Tigeot 		    mode->vdisplay != cmdline_mode->yres)
11655718399fSFrançois Tigeot 			continue;
11665718399fSFrançois Tigeot 
11675718399fSFrançois Tigeot 		if (cmdline_mode->refresh_specified) {
11685718399fSFrançois Tigeot 			if (mode->vrefresh != cmdline_mode->refresh)
11695718399fSFrançois Tigeot 				continue;
11705718399fSFrançois Tigeot 		}
11715718399fSFrançois Tigeot 
11725718399fSFrançois Tigeot 		if (cmdline_mode->interlace) {
11735718399fSFrançois Tigeot 			if (!(mode->flags & DRM_MODE_FLAG_INTERLACE))
11745718399fSFrançois Tigeot 				continue;
11755718399fSFrançois Tigeot 		}
11765718399fSFrançois Tigeot 		return mode;
11775718399fSFrançois Tigeot 	}
11785718399fSFrançois Tigeot 
11795718399fSFrançois Tigeot create_mode:
11805718399fSFrançois Tigeot 	if (cmdline_mode->cvt)
11815718399fSFrançois Tigeot 		mode = drm_cvt_mode(fb_helper_conn->connector->dev,
11825718399fSFrançois Tigeot 				    cmdline_mode->xres, cmdline_mode->yres,
11835718399fSFrançois Tigeot 				    cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
11845718399fSFrançois Tigeot 				    cmdline_mode->rb, cmdline_mode->interlace,
11855718399fSFrançois Tigeot 				    cmdline_mode->margins);
11865718399fSFrançois Tigeot 	else
11875718399fSFrançois Tigeot 		mode = drm_gtf_mode(fb_helper_conn->connector->dev,
11885718399fSFrançois Tigeot 				    cmdline_mode->xres, cmdline_mode->yres,
11895718399fSFrançois Tigeot 				    cmdline_mode->refresh_specified ? cmdline_mode->refresh : 60,
11905718399fSFrançois Tigeot 				    cmdline_mode->interlace,
11915718399fSFrançois Tigeot 				    cmdline_mode->margins);
11925718399fSFrançois Tigeot 	drm_mode_set_crtcinfo(mode, CRTC_INTERLACE_HALVE_V);
11935718399fSFrançois Tigeot 	list_add(&mode->head, &fb_helper_conn->connector->modes);
11945718399fSFrançois Tigeot 	return mode;
11955718399fSFrançois Tigeot }
11965718399fSFrançois Tigeot 
11975718399fSFrançois Tigeot static bool drm_connector_enabled(struct drm_connector *connector, bool strict)
11985718399fSFrançois Tigeot {
11995718399fSFrançois Tigeot 	bool enable;
12005718399fSFrançois Tigeot 
12015718399fSFrançois Tigeot 	if (strict) {
12025718399fSFrançois Tigeot 		enable = connector->status == connector_status_connected;
12035718399fSFrançois Tigeot 	} else {
12045718399fSFrançois Tigeot 		enable = connector->status != connector_status_disconnected;
12055718399fSFrançois Tigeot 	}
12065718399fSFrançois Tigeot 	return enable;
12075718399fSFrançois Tigeot }
12085718399fSFrançois Tigeot 
12095718399fSFrançois Tigeot static void drm_enable_connectors(struct drm_fb_helper *fb_helper,
12105718399fSFrançois Tigeot 				  bool *enabled)
12115718399fSFrançois Tigeot {
12125718399fSFrançois Tigeot 	bool any_enabled = false;
12135718399fSFrançois Tigeot 	struct drm_connector *connector;
12145718399fSFrançois Tigeot 	int i = 0;
12155718399fSFrançois Tigeot 
12165718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
12175718399fSFrançois Tigeot 		connector = fb_helper->connector_info[i]->connector;
12185718399fSFrançois Tigeot 		enabled[i] = drm_connector_enabled(connector, true);
12195718399fSFrançois Tigeot 		DRM_DEBUG_KMS("connector %d enabled? %s\n", connector->base.id,
12205718399fSFrançois Tigeot 			  enabled[i] ? "yes" : "no");
12215718399fSFrançois Tigeot 		any_enabled |= enabled[i];
12225718399fSFrançois Tigeot 	}
12235718399fSFrançois Tigeot 
12245718399fSFrançois Tigeot 	if (any_enabled)
12255718399fSFrançois Tigeot 		return;
12265718399fSFrançois Tigeot 
12275718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
12285718399fSFrançois Tigeot 		connector = fb_helper->connector_info[i]->connector;
12295718399fSFrançois Tigeot 		enabled[i] = drm_connector_enabled(connector, false);
12305718399fSFrançois Tigeot 	}
12315718399fSFrançois Tigeot }
12325718399fSFrançois Tigeot 
12335718399fSFrançois Tigeot static bool drm_target_cloned(struct drm_fb_helper *fb_helper,
12345718399fSFrançois Tigeot 			      struct drm_display_mode **modes,
12355718399fSFrançois Tigeot 			      bool *enabled, int width, int height)
12365718399fSFrançois Tigeot {
12375718399fSFrançois Tigeot 	int count, i, j;
12385718399fSFrançois Tigeot 	bool can_clone = false;
12395718399fSFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_conn;
12405718399fSFrançois Tigeot 	struct drm_display_mode *dmt_mode, *mode;
12415718399fSFrançois Tigeot 
12425718399fSFrançois Tigeot 	/* only contemplate cloning in the single crtc case */
12435718399fSFrançois Tigeot 	if (fb_helper->crtc_count > 1)
12445718399fSFrançois Tigeot 		return false;
12455718399fSFrançois Tigeot 
12465718399fSFrançois Tigeot 	count = 0;
12475718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
12485718399fSFrançois Tigeot 		if (enabled[i])
12495718399fSFrançois Tigeot 			count++;
12505718399fSFrançois Tigeot 	}
12515718399fSFrançois Tigeot 
12525718399fSFrançois Tigeot 	/* only contemplate cloning if more than one connector is enabled */
12535718399fSFrançois Tigeot 	if (count <= 1)
12545718399fSFrançois Tigeot 		return false;
12555718399fSFrançois Tigeot 
12565718399fSFrançois Tigeot 	/* check the command line or if nothing common pick 1024x768 */
12575718399fSFrançois Tigeot 	can_clone = true;
12585718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
12595718399fSFrançois Tigeot 		if (!enabled[i])
12605718399fSFrançois Tigeot 			continue;
12615718399fSFrançois Tigeot 		fb_helper_conn = fb_helper->connector_info[i];
12625718399fSFrançois Tigeot 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
12635718399fSFrançois Tigeot 		if (!modes[i]) {
12645718399fSFrançois Tigeot 			can_clone = false;
12655718399fSFrançois Tigeot 			break;
12665718399fSFrançois Tigeot 		}
12675718399fSFrançois Tigeot 		for (j = 0; j < i; j++) {
12685718399fSFrançois Tigeot 			if (!enabled[j])
12695718399fSFrançois Tigeot 				continue;
12705718399fSFrançois Tigeot 			if (!drm_mode_equal(modes[j], modes[i]))
12715718399fSFrançois Tigeot 				can_clone = false;
12725718399fSFrançois Tigeot 		}
12735718399fSFrançois Tigeot 	}
12745718399fSFrançois Tigeot 
12755718399fSFrançois Tigeot 	if (can_clone) {
12765718399fSFrançois Tigeot 		DRM_DEBUG_KMS("can clone using command line\n");
12775718399fSFrançois Tigeot 		return true;
12785718399fSFrançois Tigeot 	}
12795718399fSFrançois Tigeot 
12805718399fSFrançois Tigeot 	/* try and find a 1024x768 mode on each connector */
12815718399fSFrançois Tigeot 	can_clone = true;
12825718399fSFrançois Tigeot 	dmt_mode = drm_mode_find_dmt(fb_helper->dev, 1024, 768, 60);
12835718399fSFrançois Tigeot 
12845718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
12855718399fSFrançois Tigeot 
12865718399fSFrançois Tigeot 		if (!enabled[i])
12875718399fSFrançois Tigeot 			continue;
12885718399fSFrançois Tigeot 
12895718399fSFrançois Tigeot 		fb_helper_conn = fb_helper->connector_info[i];
12905718399fSFrançois Tigeot 		list_for_each_entry(mode, &fb_helper_conn->connector->modes, head) {
12915718399fSFrançois Tigeot 			if (drm_mode_equal(mode, dmt_mode))
12925718399fSFrançois Tigeot 				modes[i] = mode;
12935718399fSFrançois Tigeot 		}
12945718399fSFrançois Tigeot 		if (!modes[i])
12955718399fSFrançois Tigeot 			can_clone = false;
12965718399fSFrançois Tigeot 	}
12975718399fSFrançois Tigeot 
12985718399fSFrançois Tigeot 	if (can_clone) {
12995718399fSFrançois Tigeot 		DRM_DEBUG_KMS("can clone using 1024x768\n");
13005718399fSFrançois Tigeot 		return true;
13015718399fSFrançois Tigeot 	}
13025718399fSFrançois Tigeot 	DRM_INFO("kms: can't enable cloning when we probably wanted to.\n");
13035718399fSFrançois Tigeot 	return false;
13045718399fSFrançois Tigeot }
13055718399fSFrançois Tigeot 
13065718399fSFrançois Tigeot static bool drm_target_preferred(struct drm_fb_helper *fb_helper,
13075718399fSFrançois Tigeot 				 struct drm_display_mode **modes,
13085718399fSFrançois Tigeot 				 bool *enabled, int width, int height)
13095718399fSFrançois Tigeot {
13105718399fSFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_conn;
13115718399fSFrançois Tigeot 	int i;
13125718399fSFrançois Tigeot 
13135718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
13145718399fSFrançois Tigeot 		fb_helper_conn = fb_helper->connector_info[i];
13155718399fSFrançois Tigeot 
13165718399fSFrançois Tigeot 		if (enabled[i] == false)
13175718399fSFrançois Tigeot 			continue;
13185718399fSFrançois Tigeot 
13195718399fSFrançois Tigeot 		DRM_DEBUG_KMS("looking for cmdline mode on connector %d\n",
13205718399fSFrançois Tigeot 			      fb_helper_conn->connector->base.id);
13215718399fSFrançois Tigeot 
13225718399fSFrançois Tigeot 		/* got for command line mode first */
13235718399fSFrançois Tigeot 		modes[i] = drm_pick_cmdline_mode(fb_helper_conn, width, height);
13245718399fSFrançois Tigeot 		if (!modes[i]) {
13255718399fSFrançois Tigeot 			DRM_DEBUG_KMS("looking for preferred mode on connector %d\n",
13265718399fSFrançois Tigeot 				      fb_helper_conn->connector->base.id);
13275718399fSFrançois Tigeot 			modes[i] = drm_has_preferred_mode(fb_helper_conn, width, height);
13285718399fSFrançois Tigeot 		}
13295718399fSFrançois Tigeot 		/* No preferred modes, pick one off the list */
13305718399fSFrançois Tigeot 		if (!modes[i] && !list_empty(&fb_helper_conn->connector->modes)) {
13315718399fSFrançois Tigeot 			list_for_each_entry(modes[i], &fb_helper_conn->connector->modes, head)
13325718399fSFrançois Tigeot 				break;
13335718399fSFrançois Tigeot 		}
13345718399fSFrançois Tigeot 		DRM_DEBUG_KMS("found mode %s\n", modes[i] ? modes[i]->name :
13355718399fSFrançois Tigeot 			  "none");
13365718399fSFrançois Tigeot 	}
13375718399fSFrançois Tigeot 	return true;
13385718399fSFrançois Tigeot }
13395718399fSFrançois Tigeot 
13405718399fSFrançois Tigeot static int drm_pick_crtcs(struct drm_fb_helper *fb_helper,
13415718399fSFrançois Tigeot 			  struct drm_fb_helper_crtc **best_crtcs,
13425718399fSFrançois Tigeot 			  struct drm_display_mode **modes,
13435718399fSFrançois Tigeot 			  int n, int width, int height)
13445718399fSFrançois Tigeot {
13455718399fSFrançois Tigeot 	int c, o;
13465718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
13475718399fSFrançois Tigeot 	struct drm_connector *connector;
13485718399fSFrançois Tigeot 	struct drm_connector_helper_funcs *connector_funcs;
13495718399fSFrançois Tigeot 	struct drm_encoder *encoder;
13505718399fSFrançois Tigeot 	struct drm_fb_helper_crtc *best_crtc;
13515718399fSFrançois Tigeot 	int my_score, best_score, score;
13525718399fSFrançois Tigeot 	struct drm_fb_helper_crtc **crtcs, *crtc;
13535718399fSFrançois Tigeot 	struct drm_fb_helper_connector *fb_helper_conn;
13545718399fSFrançois Tigeot 
13555718399fSFrançois Tigeot 	if (n == fb_helper->connector_count)
13565718399fSFrançois Tigeot 		return 0;
13575718399fSFrançois Tigeot 
13585718399fSFrançois Tigeot 	fb_helper_conn = fb_helper->connector_info[n];
13595718399fSFrançois Tigeot 	connector = fb_helper_conn->connector;
13605718399fSFrançois Tigeot 
13615718399fSFrançois Tigeot 	best_crtcs[n] = NULL;
13625718399fSFrançois Tigeot 	best_crtc = NULL;
13635718399fSFrançois Tigeot 	best_score = drm_pick_crtcs(fb_helper, best_crtcs, modes, n+1, width, height);
13645718399fSFrançois Tigeot 	if (modes[n] == NULL)
13655718399fSFrançois Tigeot 		return best_score;
13665718399fSFrançois Tigeot 
13675718399fSFrançois Tigeot 	crtcs = kmalloc(dev->mode_config.num_connector *
13685718399fSFrançois Tigeot 	    sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
13695718399fSFrançois Tigeot 	    M_WAITOK | M_ZERO);
13705718399fSFrançois Tigeot 
13715718399fSFrançois Tigeot 	my_score = 1;
13725718399fSFrançois Tigeot 	if (connector->status == connector_status_connected)
13735718399fSFrançois Tigeot 		my_score++;
13745718399fSFrançois Tigeot 	if (drm_has_cmdline_mode(fb_helper_conn))
13755718399fSFrançois Tigeot 		my_score++;
13765718399fSFrançois Tigeot 	if (drm_has_preferred_mode(fb_helper_conn, width, height))
13775718399fSFrançois Tigeot 		my_score++;
13785718399fSFrançois Tigeot 
13795718399fSFrançois Tigeot 	connector_funcs = connector->helper_private;
13805718399fSFrançois Tigeot 	encoder = connector_funcs->best_encoder(connector);
13815718399fSFrançois Tigeot 	if (!encoder)
13825718399fSFrançois Tigeot 		goto out;
13835718399fSFrançois Tigeot 
13845718399fSFrançois Tigeot 	/* select a crtc for this connector and then attempt to configure
13855718399fSFrançois Tigeot 	   remaining connectors */
13865718399fSFrançois Tigeot 	for (c = 0; c < fb_helper->crtc_count; c++) {
13875718399fSFrançois Tigeot 		crtc = &fb_helper->crtc_info[c];
13885718399fSFrançois Tigeot 
13895718399fSFrançois Tigeot 		if ((encoder->possible_crtcs & (1 << c)) == 0) {
13905718399fSFrançois Tigeot 			continue;
13915718399fSFrançois Tigeot 		}
13925718399fSFrançois Tigeot 
13935718399fSFrançois Tigeot 		for (o = 0; o < n; o++)
13945718399fSFrançois Tigeot 			if (best_crtcs[o] == crtc)
13955718399fSFrançois Tigeot 				break;
13965718399fSFrançois Tigeot 
13975718399fSFrançois Tigeot 		if (o < n) {
13985718399fSFrançois Tigeot 			/* ignore cloning unless only a single crtc */
13995718399fSFrançois Tigeot 			if (fb_helper->crtc_count > 1)
14005718399fSFrançois Tigeot 				continue;
14015718399fSFrançois Tigeot 
14025718399fSFrançois Tigeot 			if (!drm_mode_equal(modes[o], modes[n]))
14035718399fSFrançois Tigeot 				continue;
14045718399fSFrançois Tigeot 		}
14055718399fSFrançois Tigeot 
14065718399fSFrançois Tigeot 		crtcs[n] = crtc;
14075718399fSFrançois Tigeot 		memcpy(crtcs, best_crtcs, n * sizeof(struct drm_fb_helper_crtc *));
14085718399fSFrançois Tigeot 		score = my_score + drm_pick_crtcs(fb_helper, crtcs, modes, n + 1,
14095718399fSFrançois Tigeot 						  width, height);
14105718399fSFrançois Tigeot 		if (score > best_score) {
14115718399fSFrançois Tigeot 			best_crtc = crtc;
14125718399fSFrançois Tigeot 			best_score = score;
14135718399fSFrançois Tigeot 			memcpy(best_crtcs, crtcs,
14145718399fSFrançois Tigeot 			       dev->mode_config.num_connector *
14155718399fSFrançois Tigeot 			       sizeof(struct drm_fb_helper_crtc *));
14165718399fSFrançois Tigeot 		}
14175718399fSFrançois Tigeot 	}
14185718399fSFrançois Tigeot out:
14195718399fSFrançois Tigeot 	drm_free(crtcs, DRM_MEM_KMS);
14205718399fSFrançois Tigeot 	return best_score;
14215718399fSFrançois Tigeot }
14225718399fSFrançois Tigeot 
14235718399fSFrançois Tigeot static void drm_setup_crtcs(struct drm_fb_helper *fb_helper)
14245718399fSFrançois Tigeot {
14255718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
14265718399fSFrançois Tigeot 	struct drm_fb_helper_crtc **crtcs;
14275718399fSFrançois Tigeot 	struct drm_display_mode **modes;
14285718399fSFrançois Tigeot 	struct drm_encoder *encoder;
14295718399fSFrançois Tigeot 	struct drm_mode_set *modeset;
14305718399fSFrançois Tigeot 	bool *enabled;
14315718399fSFrançois Tigeot 	int width, height;
14325718399fSFrançois Tigeot 	int i, ret;
14335718399fSFrançois Tigeot 
14345718399fSFrançois Tigeot 	DRM_DEBUG_KMS("\n");
14355718399fSFrançois Tigeot 
14365718399fSFrançois Tigeot 	width = dev->mode_config.max_width;
14375718399fSFrançois Tigeot 	height = dev->mode_config.max_height;
14385718399fSFrançois Tigeot 
14395718399fSFrançois Tigeot 	/* clean out all the encoder/crtc combos */
14405718399fSFrançois Tigeot 	list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
14415718399fSFrançois Tigeot 		encoder->crtc = NULL;
14425718399fSFrançois Tigeot 	}
14435718399fSFrançois Tigeot 
14445718399fSFrançois Tigeot 	crtcs = kmalloc(dev->mode_config.num_connector *
14455718399fSFrançois Tigeot 	    sizeof(struct drm_fb_helper_crtc *), DRM_MEM_KMS,
14465718399fSFrançois Tigeot 	    M_WAITOK | M_ZERO);
14475718399fSFrançois Tigeot 	modes = kmalloc(dev->mode_config.num_connector *
14485718399fSFrançois Tigeot 	    sizeof(struct drm_display_mode *), DRM_MEM_KMS,
14495718399fSFrançois Tigeot 	    M_WAITOK | M_ZERO);
14505718399fSFrançois Tigeot 	enabled = kmalloc(dev->mode_config.num_connector *
14515718399fSFrançois Tigeot 	    sizeof(bool), DRM_MEM_KMS, M_WAITOK | M_ZERO);
14525718399fSFrançois Tigeot 
14535718399fSFrançois Tigeot 	drm_enable_connectors(fb_helper, enabled);
14545718399fSFrançois Tigeot 
14555718399fSFrançois Tigeot 	ret = drm_target_cloned(fb_helper, modes, enabled, width, height);
14565718399fSFrançois Tigeot 	if (!ret) {
14575718399fSFrançois Tigeot 		ret = drm_target_preferred(fb_helper, modes, enabled, width, height);
14585718399fSFrançois Tigeot 		if (!ret)
14595718399fSFrançois Tigeot 			DRM_ERROR("Unable to find initial modes\n");
14605718399fSFrançois Tigeot 	}
14615718399fSFrançois Tigeot 
14625718399fSFrançois Tigeot 	DRM_DEBUG_KMS("picking CRTCs for %dx%d config\n", width, height);
14635718399fSFrançois Tigeot 
14645718399fSFrançois Tigeot 	drm_pick_crtcs(fb_helper, crtcs, modes, 0, width, height);
14655718399fSFrançois Tigeot 
14665718399fSFrançois Tigeot 	/* need to set the modesets up here for use later */
14675718399fSFrançois Tigeot 	/* fill out the connector<->crtc mappings into the modesets */
14685718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->crtc_count; i++) {
14695718399fSFrançois Tigeot 		modeset = &fb_helper->crtc_info[i].mode_set;
14705718399fSFrançois Tigeot 		modeset->num_connectors = 0;
14715718399fSFrançois Tigeot 	}
14725718399fSFrançois Tigeot 
14735718399fSFrançois Tigeot 	for (i = 0; i < fb_helper->connector_count; i++) {
14745718399fSFrançois Tigeot 		struct drm_display_mode *mode = modes[i];
14755718399fSFrançois Tigeot 		struct drm_fb_helper_crtc *fb_crtc = crtcs[i];
14765718399fSFrançois Tigeot 		modeset = &fb_crtc->mode_set;
14775718399fSFrançois Tigeot 
14785718399fSFrançois Tigeot 		if (mode && fb_crtc) {
14795718399fSFrançois Tigeot 			DRM_DEBUG_KMS("desired mode %s set on crtc %d\n",
14805718399fSFrançois Tigeot 				      mode->name, fb_crtc->mode_set.crtc->base.id);
14815718399fSFrançois Tigeot 			fb_crtc->desired_mode = mode;
14825718399fSFrançois Tigeot 			if (modeset->mode)
14835718399fSFrançois Tigeot 				drm_mode_destroy(dev, modeset->mode);
14845718399fSFrançois Tigeot 			modeset->mode = drm_mode_duplicate(dev,
14855718399fSFrançois Tigeot 							   fb_crtc->desired_mode);
14865718399fSFrançois Tigeot 			modeset->connectors[modeset->num_connectors++] = fb_helper->connector_info[i]->connector;
14875718399fSFrançois Tigeot 		}
14885718399fSFrançois Tigeot 	}
14895718399fSFrançois Tigeot 
14905718399fSFrançois Tigeot 	drm_free(crtcs, DRM_MEM_KMS);
14915718399fSFrançois Tigeot 	drm_free(modes, DRM_MEM_KMS);
14925718399fSFrançois Tigeot 	drm_free(enabled, DRM_MEM_KMS);
14935718399fSFrançois Tigeot }
14945718399fSFrançois Tigeot 
14955718399fSFrançois Tigeot /**
14965718399fSFrançois Tigeot  * drm_helper_initial_config - setup a sane initial connector configuration
14975718399fSFrançois Tigeot  * @dev: DRM device
14985718399fSFrançois Tigeot  *
14995718399fSFrançois Tigeot  * LOCKING:
15005718399fSFrançois Tigeot  * Called at init time, must take mode config lock.
15015718399fSFrançois Tigeot  *
15025718399fSFrançois Tigeot  * Scan the CRTCs and connectors and try to put together an initial setup.
15035718399fSFrançois Tigeot  * At the moment, this is a cloned configuration across all heads with
15045718399fSFrançois Tigeot  * a new framebuffer object as the backing store.
15055718399fSFrançois Tigeot  *
15065718399fSFrançois Tigeot  * RETURNS:
15075718399fSFrançois Tigeot  * Zero if everything went ok, nonzero otherwise.
15085718399fSFrançois Tigeot  */
15095718399fSFrançois Tigeot bool drm_fb_helper_initial_config(struct drm_fb_helper *fb_helper, int bpp_sel)
15105718399fSFrançois Tigeot {
15115718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
15125718399fSFrançois Tigeot 	int count = 0;
15135718399fSFrançois Tigeot 
15145718399fSFrançois Tigeot 	/* disable all the possible outputs/crtcs before entering KMS mode */
15155718399fSFrançois Tigeot 	drm_helper_disable_unused_functions(fb_helper->dev);
15165718399fSFrançois Tigeot 
15175718399fSFrançois Tigeot 	drm_fb_helper_parse_command_line(fb_helper);
15185718399fSFrançois Tigeot 
15195718399fSFrançois Tigeot 	count = drm_fb_helper_probe_connector_modes(fb_helper,
15205718399fSFrançois Tigeot 						    dev->mode_config.max_width,
15215718399fSFrançois Tigeot 						    dev->mode_config.max_height);
15225718399fSFrançois Tigeot 	/*
15235718399fSFrançois Tigeot 	 * we shouldn't end up with no modes here.
15245718399fSFrançois Tigeot 	 */
15255718399fSFrançois Tigeot 	if (count == 0) {
15265718399fSFrançois Tigeot 		kprintf("No connectors reported connected with modes\n");
15275718399fSFrançois Tigeot 	}
15285718399fSFrançois Tigeot 	drm_setup_crtcs(fb_helper);
15295718399fSFrançois Tigeot 
15305718399fSFrançois Tigeot 	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
15315718399fSFrançois Tigeot }
15325718399fSFrançois Tigeot 
15335718399fSFrançois Tigeot int drm_fb_helper_hotplug_event(struct drm_fb_helper *fb_helper)
15345718399fSFrançois Tigeot {
15355718399fSFrançois Tigeot 	struct drm_device *dev = fb_helper->dev;
15365718399fSFrançois Tigeot 	int count = 0;
15375718399fSFrançois Tigeot 	u32 max_width, max_height, bpp_sel;
15385718399fSFrançois Tigeot 	bool bound = false, crtcs_bound = false;
15395718399fSFrançois Tigeot 	struct drm_crtc *crtc;
15405718399fSFrançois Tigeot 
15415718399fSFrançois Tigeot 	if (!fb_helper->fb)
15425718399fSFrançois Tigeot 		return 0;
15435718399fSFrançois Tigeot 
1544*af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_EXCLUSIVE);
15455718399fSFrançois Tigeot 	list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
15465718399fSFrançois Tigeot 		if (crtc->fb)
15475718399fSFrançois Tigeot 			crtcs_bound = true;
15485718399fSFrançois Tigeot 		if (crtc->fb == fb_helper->fb)
15495718399fSFrançois Tigeot 			bound = true;
15505718399fSFrançois Tigeot 	}
15515718399fSFrançois Tigeot 
15525718399fSFrançois Tigeot 	if (!bound && crtcs_bound) {
15535718399fSFrançois Tigeot 		fb_helper->delayed_hotplug = true;
1554*af4b81b9SFrançois Tigeot 		lockmgr(&dev->mode_config.mutex, LK_RELEASE);
15555718399fSFrançois Tigeot 		return 0;
15565718399fSFrançois Tigeot 	}
15575718399fSFrançois Tigeot 	DRM_DEBUG_KMS("\n");
15585718399fSFrançois Tigeot 
15595718399fSFrançois Tigeot 	max_width = fb_helper->fb->width;
15605718399fSFrançois Tigeot 	max_height = fb_helper->fb->height;
15615718399fSFrançois Tigeot 	bpp_sel = fb_helper->fb->bits_per_pixel;
15625718399fSFrançois Tigeot 
15635718399fSFrançois Tigeot 	count = drm_fb_helper_probe_connector_modes(fb_helper, max_width,
15645718399fSFrançois Tigeot 						    max_height);
15655718399fSFrançois Tigeot 	drm_setup_crtcs(fb_helper);
1566*af4b81b9SFrançois Tigeot 	lockmgr(&dev->mode_config.mutex, LK_RELEASE);
15675718399fSFrançois Tigeot 
15685718399fSFrançois Tigeot 	return drm_fb_helper_single_fb_probe(fb_helper, bpp_sel);
15695718399fSFrançois Tigeot }
15705718399fSFrançois Tigeot 
1571