162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci   drm_edid_load.c: use a built-in EDID data set or load it via the firmware
462306a36Sopenharmony_ci		    interface
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci   Copyright (C) 2012 Carsten Emde <C.Emde@osadl.org>
762306a36Sopenharmony_ci
862306a36Sopenharmony_ci*/
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/firmware.h>
1162306a36Sopenharmony_ci#include <linux/module.h>
1262306a36Sopenharmony_ci#include <linux/platform_device.h>
1362306a36Sopenharmony_ci
1462306a36Sopenharmony_ci#include <drm/drm_connector.h>
1562306a36Sopenharmony_ci#include <drm/drm_drv.h>
1662306a36Sopenharmony_ci#include <drm/drm_edid.h>
1762306a36Sopenharmony_ci#include <drm/drm_print.h>
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include "drm_crtc_internal.h"
2062306a36Sopenharmony_ci
2162306a36Sopenharmony_cistatic char edid_firmware[PATH_MAX];
2262306a36Sopenharmony_cimodule_param_string(edid_firmware, edid_firmware, sizeof(edid_firmware), 0644);
2362306a36Sopenharmony_ciMODULE_PARM_DESC(edid_firmware, "Do not probe monitor, use specified EDID blob "
2462306a36Sopenharmony_ci	"from built-in data or /lib/firmware instead. ");
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/* Use only for backward compatibility with drm_kms_helper.edid_firmware */
2762306a36Sopenharmony_ciint __drm_set_edid_firmware_path(const char *path)
2862306a36Sopenharmony_ci{
2962306a36Sopenharmony_ci	scnprintf(edid_firmware, sizeof(edid_firmware), "%s", path);
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci	return 0;
3262306a36Sopenharmony_ci}
3362306a36Sopenharmony_ciEXPORT_SYMBOL(__drm_set_edid_firmware_path);
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci/* Use only for backward compatibility with drm_kms_helper.edid_firmware */
3662306a36Sopenharmony_ciint __drm_get_edid_firmware_path(char *buf, size_t bufsize)
3762306a36Sopenharmony_ci{
3862306a36Sopenharmony_ci	return scnprintf(buf, bufsize, "%s", edid_firmware);
3962306a36Sopenharmony_ci}
4062306a36Sopenharmony_ciEXPORT_SYMBOL(__drm_get_edid_firmware_path);
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#define GENERIC_EDIDS 6
4362306a36Sopenharmony_cistatic const char * const generic_edid_name[GENERIC_EDIDS] = {
4462306a36Sopenharmony_ci	"edid/800x600.bin",
4562306a36Sopenharmony_ci	"edid/1024x768.bin",
4662306a36Sopenharmony_ci	"edid/1280x1024.bin",
4762306a36Sopenharmony_ci	"edid/1600x1200.bin",
4862306a36Sopenharmony_ci	"edid/1680x1050.bin",
4962306a36Sopenharmony_ci	"edid/1920x1080.bin",
5062306a36Sopenharmony_ci};
5162306a36Sopenharmony_ci
5262306a36Sopenharmony_cistatic const u8 generic_edid[GENERIC_EDIDS][128] = {
5362306a36Sopenharmony_ci	{
5462306a36Sopenharmony_ci	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
5562306a36Sopenharmony_ci	0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5662306a36Sopenharmony_ci	0x05, 0x16, 0x01, 0x03, 0x6d, 0x1b, 0x14, 0x78,
5762306a36Sopenharmony_ci	0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
5862306a36Sopenharmony_ci	0x20, 0x50, 0x54, 0x01, 0x00, 0x00, 0x45, 0x40,
5962306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
6062306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xa0, 0x0f,
6162306a36Sopenharmony_ci	0x20, 0x00, 0x31, 0x58, 0x1c, 0x20, 0x28, 0x80,
6262306a36Sopenharmony_ci	0x14, 0x00, 0x15, 0xd0, 0x10, 0x00, 0x00, 0x1e,
6362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
6462306a36Sopenharmony_ci	0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
6562306a36Sopenharmony_ci	0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
6662306a36Sopenharmony_ci	0x3d, 0x24, 0x26, 0x05, 0x00, 0x0a, 0x20, 0x20,
6762306a36Sopenharmony_ci	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
6862306a36Sopenharmony_ci	0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x53,
6962306a36Sopenharmony_ci	0x56, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0xc2,
7062306a36Sopenharmony_ci	},
7162306a36Sopenharmony_ci	{
7262306a36Sopenharmony_ci	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
7362306a36Sopenharmony_ci	0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
7462306a36Sopenharmony_ci	0x05, 0x16, 0x01, 0x03, 0x6d, 0x23, 0x1a, 0x78,
7562306a36Sopenharmony_ci	0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
7662306a36Sopenharmony_ci	0x20, 0x50, 0x54, 0x00, 0x08, 0x00, 0x61, 0x40,
7762306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
7862306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x64, 0x19,
7962306a36Sopenharmony_ci	0x00, 0x40, 0x41, 0x00, 0x26, 0x30, 0x08, 0x90,
8062306a36Sopenharmony_ci	0x36, 0x00, 0x63, 0x0a, 0x11, 0x00, 0x00, 0x18,
8162306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
8262306a36Sopenharmony_ci	0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
8362306a36Sopenharmony_ci	0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
8462306a36Sopenharmony_ci	0x3d, 0x2f, 0x31, 0x07, 0x00, 0x0a, 0x20, 0x20,
8562306a36Sopenharmony_ci	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
8662306a36Sopenharmony_ci	0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x58,
8762306a36Sopenharmony_ci	0x47, 0x41, 0x0a, 0x20, 0x20, 0x20, 0x00, 0x55,
8862306a36Sopenharmony_ci	},
8962306a36Sopenharmony_ci	{
9062306a36Sopenharmony_ci	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
9162306a36Sopenharmony_ci	0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
9262306a36Sopenharmony_ci	0x05, 0x16, 0x01, 0x03, 0x6d, 0x2c, 0x23, 0x78,
9362306a36Sopenharmony_ci	0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
9462306a36Sopenharmony_ci	0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0x81, 0x80,
9562306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
9662306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x30, 0x2a,
9762306a36Sopenharmony_ci	0x00, 0x98, 0x51, 0x00, 0x2a, 0x40, 0x30, 0x70,
9862306a36Sopenharmony_ci	0x13, 0x00, 0xbc, 0x63, 0x11, 0x00, 0x00, 0x1e,
9962306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
10062306a36Sopenharmony_ci	0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
10162306a36Sopenharmony_ci	0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
10262306a36Sopenharmony_ci	0x3d, 0x3e, 0x40, 0x0b, 0x00, 0x0a, 0x20, 0x20,
10362306a36Sopenharmony_ci	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
10462306a36Sopenharmony_ci	0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x53,
10562306a36Sopenharmony_ci	0x58, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0xa0,
10662306a36Sopenharmony_ci	},
10762306a36Sopenharmony_ci	{
10862306a36Sopenharmony_ci	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
10962306a36Sopenharmony_ci	0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
11062306a36Sopenharmony_ci	0x05, 0x16, 0x01, 0x03, 0x6d, 0x37, 0x29, 0x78,
11162306a36Sopenharmony_ci	0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
11262306a36Sopenharmony_ci	0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xa9, 0x40,
11362306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
11462306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x48, 0x3f,
11562306a36Sopenharmony_ci	0x40, 0x30, 0x62, 0xb0, 0x32, 0x40, 0x40, 0xc0,
11662306a36Sopenharmony_ci	0x13, 0x00, 0x2b, 0xa0, 0x21, 0x00, 0x00, 0x1e,
11762306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
11862306a36Sopenharmony_ci	0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
11962306a36Sopenharmony_ci	0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
12062306a36Sopenharmony_ci	0x3d, 0x4a, 0x4c, 0x11, 0x00, 0x0a, 0x20, 0x20,
12162306a36Sopenharmony_ci	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
12262306a36Sopenharmony_ci	0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x55,
12362306a36Sopenharmony_ci	0x58, 0x47, 0x41, 0x0a, 0x20, 0x20, 0x00, 0x9d,
12462306a36Sopenharmony_ci	},
12562306a36Sopenharmony_ci	{
12662306a36Sopenharmony_ci	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
12762306a36Sopenharmony_ci	0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
12862306a36Sopenharmony_ci	0x05, 0x16, 0x01, 0x03, 0x6d, 0x2b, 0x1b, 0x78,
12962306a36Sopenharmony_ci	0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
13062306a36Sopenharmony_ci	0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xb3, 0x00,
13162306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
13262306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x21, 0x39,
13362306a36Sopenharmony_ci	0x90, 0x30, 0x62, 0x1a, 0x27, 0x40, 0x68, 0xb0,
13462306a36Sopenharmony_ci	0x36, 0x00, 0xb5, 0x11, 0x11, 0x00, 0x00, 0x1e,
13562306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
13662306a36Sopenharmony_ci	0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
13762306a36Sopenharmony_ci	0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
13862306a36Sopenharmony_ci	0x3d, 0x40, 0x42, 0x0f, 0x00, 0x0a, 0x20, 0x20,
13962306a36Sopenharmony_ci	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
14062306a36Sopenharmony_ci	0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x57,
14162306a36Sopenharmony_ci	0x53, 0x58, 0x47, 0x41, 0x0a, 0x20, 0x00, 0x26,
14262306a36Sopenharmony_ci	},
14362306a36Sopenharmony_ci	{
14462306a36Sopenharmony_ci	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
14562306a36Sopenharmony_ci	0x31, 0xd8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
14662306a36Sopenharmony_ci	0x05, 0x16, 0x01, 0x03, 0x6d, 0x32, 0x1c, 0x78,
14762306a36Sopenharmony_ci	0xea, 0x5e, 0xc0, 0xa4, 0x59, 0x4a, 0x98, 0x25,
14862306a36Sopenharmony_ci	0x20, 0x50, 0x54, 0x00, 0x00, 0x00, 0xd1, 0xc0,
14962306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
15062306a36Sopenharmony_ci	0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x3a,
15162306a36Sopenharmony_ci	0x80, 0x18, 0x71, 0x38, 0x2d, 0x40, 0x58, 0x2c,
15262306a36Sopenharmony_ci	0x45, 0x00, 0xf4, 0x19, 0x11, 0x00, 0x00, 0x1e,
15362306a36Sopenharmony_ci	0x00, 0x00, 0x00, 0xff, 0x00, 0x4c, 0x69, 0x6e,
15462306a36Sopenharmony_ci	0x75, 0x78, 0x20, 0x23, 0x30, 0x0a, 0x20, 0x20,
15562306a36Sopenharmony_ci	0x20, 0x20, 0x00, 0x00, 0x00, 0xfd, 0x00, 0x3b,
15662306a36Sopenharmony_ci	0x3d, 0x42, 0x44, 0x0f, 0x00, 0x0a, 0x20, 0x20,
15762306a36Sopenharmony_ci	0x20, 0x20, 0x20, 0x20, 0x00, 0x00, 0x00, 0xfc,
15862306a36Sopenharmony_ci	0x00, 0x4c, 0x69, 0x6e, 0x75, 0x78, 0x20, 0x46,
15962306a36Sopenharmony_ci	0x48, 0x44, 0x0a, 0x20, 0x20, 0x20, 0x00, 0x05,
16062306a36Sopenharmony_ci	},
16162306a36Sopenharmony_ci};
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cistatic const struct drm_edid *edid_load(struct drm_connector *connector, const char *name)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	const struct firmware *fw = NULL;
16662306a36Sopenharmony_ci	const u8 *fwdata;
16762306a36Sopenharmony_ci	const struct drm_edid *drm_edid;
16862306a36Sopenharmony_ci	int fwsize, builtin;
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci	builtin = match_string(generic_edid_name, GENERIC_EDIDS, name);
17162306a36Sopenharmony_ci	if (builtin >= 0) {
17262306a36Sopenharmony_ci		fwdata = generic_edid[builtin];
17362306a36Sopenharmony_ci		fwsize = sizeof(generic_edid[builtin]);
17462306a36Sopenharmony_ci	} else {
17562306a36Sopenharmony_ci		int err;
17662306a36Sopenharmony_ci
17762306a36Sopenharmony_ci		err = request_firmware(&fw, name, connector->dev->dev);
17862306a36Sopenharmony_ci		if (err) {
17962306a36Sopenharmony_ci			drm_err(connector->dev,
18062306a36Sopenharmony_ci				"[CONNECTOR:%d:%s] Requesting EDID firmware \"%s\" failed (err=%d)\n",
18162306a36Sopenharmony_ci				connector->base.id, connector->name,
18262306a36Sopenharmony_ci				name, err);
18362306a36Sopenharmony_ci			return ERR_PTR(err);
18462306a36Sopenharmony_ci		}
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci		fwdata = fw->data;
18762306a36Sopenharmony_ci		fwsize = fw->size;
18862306a36Sopenharmony_ci	}
18962306a36Sopenharmony_ci
19062306a36Sopenharmony_ci	drm_dbg_kms(connector->dev, "[CONNECTOR:%d:%s] Loaded %s firmware EDID \"%s\"\n",
19162306a36Sopenharmony_ci		    connector->base.id, connector->name,
19262306a36Sopenharmony_ci		    builtin >= 0 ? "built-in" : "external", name);
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	drm_edid = drm_edid_alloc(fwdata, fwsize);
19562306a36Sopenharmony_ci	if (!drm_edid_valid(drm_edid)) {
19662306a36Sopenharmony_ci		drm_err(connector->dev, "Invalid firmware EDID \"%s\"\n", name);
19762306a36Sopenharmony_ci		drm_edid_free(drm_edid);
19862306a36Sopenharmony_ci		drm_edid = ERR_PTR(-EINVAL);
19962306a36Sopenharmony_ci	}
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci	release_firmware(fw);
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	return drm_edid;
20462306a36Sopenharmony_ci}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ciconst struct drm_edid *drm_edid_load_firmware(struct drm_connector *connector)
20762306a36Sopenharmony_ci{
20862306a36Sopenharmony_ci	char *edidname, *last, *colon, *fwstr, *edidstr, *fallback = NULL;
20962306a36Sopenharmony_ci	const struct drm_edid *drm_edid;
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_ci	if (edid_firmware[0] == '\0')
21262306a36Sopenharmony_ci		return ERR_PTR(-ENOENT);
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci	/*
21562306a36Sopenharmony_ci	 * If there are multiple edid files specified and separated
21662306a36Sopenharmony_ci	 * by commas, search through the list looking for one that
21762306a36Sopenharmony_ci	 * matches the connector.
21862306a36Sopenharmony_ci	 *
21962306a36Sopenharmony_ci	 * If there's one or more that doesn't specify a connector, keep
22062306a36Sopenharmony_ci	 * the last one found one as a fallback.
22162306a36Sopenharmony_ci	 */
22262306a36Sopenharmony_ci	fwstr = kstrdup(edid_firmware, GFP_KERNEL);
22362306a36Sopenharmony_ci	if (!fwstr)
22462306a36Sopenharmony_ci		return ERR_PTR(-ENOMEM);
22562306a36Sopenharmony_ci	edidstr = fwstr;
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	while ((edidname = strsep(&edidstr, ","))) {
22862306a36Sopenharmony_ci		colon = strchr(edidname, ':');
22962306a36Sopenharmony_ci		if (colon != NULL) {
23062306a36Sopenharmony_ci			if (strncmp(connector->name, edidname, colon - edidname))
23162306a36Sopenharmony_ci				continue;
23262306a36Sopenharmony_ci			edidname = colon + 1;
23362306a36Sopenharmony_ci			break;
23462306a36Sopenharmony_ci		}
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci		if (*edidname != '\0') /* corner case: multiple ',' */
23762306a36Sopenharmony_ci			fallback = edidname;
23862306a36Sopenharmony_ci	}
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci	if (!edidname) {
24162306a36Sopenharmony_ci		if (!fallback) {
24262306a36Sopenharmony_ci			kfree(fwstr);
24362306a36Sopenharmony_ci			return ERR_PTR(-ENOENT);
24462306a36Sopenharmony_ci		}
24562306a36Sopenharmony_ci		edidname = fallback;
24662306a36Sopenharmony_ci	}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_ci	last = edidname + strlen(edidname) - 1;
24962306a36Sopenharmony_ci	if (*last == '\n')
25062306a36Sopenharmony_ci		*last = '\0';
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ci	drm_edid = edid_load(connector, edidname);
25362306a36Sopenharmony_ci
25462306a36Sopenharmony_ci	kfree(fwstr);
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci	return drm_edid;
25762306a36Sopenharmony_ci}
258