From patchwork Mon Nov 7 14:16:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Maxime Ripard X-Patchwork-Id: 13034454 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CD8C4C43217 for ; Mon, 7 Nov 2022 14:18:34 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 2E2BE10E349; Mon, 7 Nov 2022 14:17:59 +0000 (UTC) Received: from wnew2-smtp.messagingengine.com (wnew2-smtp.messagingengine.com [64.147.123.27]) by gabe.freedesktop.org (Postfix) with ESMTPS id A699010E33C; Mon, 7 Nov 2022 14:17:38 +0000 (UTC) Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailnew.west.internal (Postfix) with ESMTP id E9C6B2B05E60; Mon, 7 Nov 2022 09:17:35 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Mon, 07 Nov 2022 09:17:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cerno.tech; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1667830655; x= 1667837855; bh=P1KWsASnYvTi2I1+CBGAVsqir1G/tMqeUHWEZ+PSqHs=; b=h S2xCfU/fygdElNNHroTorMWajd0Le0Umyz3+94zhUQuipmxHUsQt3zShZ2hXLbaQ pcISmCuzlWNNvtrPGlVJUPa6ePs5sZjq2sPOoaaAJdSjysYoeA8exVMidX5pVSEZ GD/42ev4Qj2UYYq2dzZdrK5eb74+CpT5hmqNZEB194aZexeJ8+9DeV3kI03E/xx9 E9oXx1i/e0GjQrhjxu5ZtakNAMvqoGFzwG5QxRZaiSAjwfhmtD1dzgzNO7EY9kFU 2IL1lGYrZuQnv+wi/XRJA8itjku5m0etEGWpG4W78WJeLSIMOfGzzQNCcrJjhhGm hXK1Qz3fy4ITROH9Mbh3Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1667830655; x= 1667837855; bh=P1KWsASnYvTi2I1+CBGAVsqir1G/tMqeUHWEZ+PSqHs=; b=p VkqTLgAIlTvk8WPNyUDZRKe/9M1XgCxsboBvelMsktSDWRe5u2r33eP7Nyc2EJNW slmPpPS1BjP4ecXDTpoqV3NPf97LoB+05Rg7QHjvXYsXTkGM8FTipJPAgKFtz8UL GTCl6IUWyWGQGyqNjadFgGGLCiE8HSr73rdmAEKXmsaoHAl8jGAa8Dsqhw0e6vx6 gLukdyr1jB9M6yH1jmIJLsRV5gojDpkEH4VL/nCrUUzeKKHLaoVGaHOrdgjm01wI gDgQ6ghsvCj2+5I254snhcwQWOREtK1Rc7aUPmiIotg9eDgxZC1k1V/NO0bpB1U2 msDpqMAykaEQUCSUuDcsQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvgedrvdekgdeiudcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefhfffuggfgtgfkfhgjvfevofesthekredtredtjeenucfhrhhomhepofgrgihi mhgvucftihhprghrugcuoehmrgigihhmvgestggvrhhnohdrthgvtghhqeenucggtffrrg htthgvrhhnpedvgfevjefhtdetveevhfelieeuueetgfdvgeevkeegudejffegfffgtedt leelhfenucevlhhushhtvghrufhiiigvpedunecurfgrrhgrmhepmhgrihhlfhhrohhmpe hmrgigihhmvgestggvrhhnohdrthgvtghh X-ME-Proxy: Feedback-ID: i8771445c:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 7 Nov 2022 09:17:34 -0500 (EST) From: Maxime Ripard Date: Mon, 07 Nov 2022 15:16:38 +0100 Subject: [PATCH v7 12/23] drm/connector: Add a function to lookup a TV mode by its name MIME-Version: 1.0 Message-Id: <20220728-rpi-analog-tv-properties-v7-12-7072a478c6b3@cerno.tech> References: <20220728-rpi-analog-tv-properties-v7-0-7072a478c6b3@cerno.tech> In-Reply-To: <20220728-rpi-analog-tv-properties-v7-0-7072a478c6b3@cerno.tech> To: Jani Nikula , Joonas Lahtinen , Tvrtko Ursulin , Ben Skeggs , Rodrigo Vivi , Maxime Ripard , Samuel Holland , Jernej Skrabec , Maarten Lankhorst , Emma Anholt , Karol Herbst , Daniel Vetter , Chen-Yu Tsai , Lyude Paul , Thomas Zimmermann , David Airlie X-Mailer: b4 0.11.0-dev-99e3a X-Developer-Signature: v=1; a=openpgp-sha256; l=5877; i=maxime@cerno.tech; h=from:subject:message-id; bh=OibD9L+0N3v79XwK5wYQ+LNcQ4mtLEndDfkBHXvN8N8=; b=owGbwMvMwCX2+D1vfrpE4FHG02pJDMmZwg6iPVHPz4rP7rxQt/eu7/OI3zfyjuSUmVRfeHGFUTuJ cfPcjlIWBjEuBlkxRZYYYfMlcadmve5k45sHM4eVCWQIAxenAEzkQSQjw0dR/Y4VfBMFFNMmiv26du LppCyrnfq/JDe8bw9jbZ7wLpaRYUeJEu//X7OW5kn4pZy9mVLq5bdIJ6H40YKdBwr1nQ/GsQEA X-Developer-Key: i=maxime@cerno.tech; a=openpgp; fpr=BE5675C37E818C8B5764241C254BCFC56BF6CE8D X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: =?unknown-8bit?q?Dom_Cobley_=3Cdom=40raspberrypi=2Ecom=3E=2C_Dave_Steven?= =?unknown-8bit?q?son_=3Cdave=2Estevenson=40raspberrypi=2Ecom=3E=2C_nouveau?= =?unknown-8bit?q?=40lists=2Efreedesktop=2Eorg=2C_intel-gfx=40lists=2Efreede?= =?unknown-8bit?q?sktop=2Eorg=2C_linux-kernel=40vger=2Ekernel=2Eorg=2C_dri-d?= =?unknown-8bit?q?evel=40lists=2Efreedesktop=2Eorg=2C_Phil_Elwell_=3Cphil=40?= =?unknown-8bit?q?raspberrypi=2Ecom=3E=2C_Hans_de_Goede_=3Chdegoede=40redhat?= =?unknown-8bit?q?=2Ecom=3E=2C_Noralf_Tr=C3=B8nnes_=3Cnoralf=40tronnes=2Eorg?= =?unknown-8bit?q?=3E=2C_Geert_Uytterhoeven_=3Cgeert=40linux-m68k=2Eorg=3E?= =?unknown-8bit?q?=2C_Maxime_Ripard_=3Cmaxime=40cerno=2Etech=3E=2C_Mateusz_K?= =?unknown-8bit?q?wiatkowski_=3Ckfyatek+publicgit=40gmail=2Ecom=3E=2C_linux-?= =?unknown-8bit?q?sunxi=40lists=2Elinux=2Edev=2C_linux-arm-kernel=40lists=2E?= =?unknown-8bit?q?infradead=2Eorg?= Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" As part of the command line parsing rework coming in the next patches, we'll need to lookup drm_connector_tv_mode values by their name, already defined in drm_tv_mode_enum_list. In order to avoid any code duplication, let's do a function that will perform a lookup of a TV mode name and return its value. Reviewed-by: Noralf Trønnes Signed-off-by: Maxime Ripard --- Changes in v7: - Add kunit tests --- drivers/gpu/drm/drm_connector.c | 24 ++++++++ drivers/gpu/drm/tests/Makefile | 1 + drivers/gpu/drm/tests/drm_connector_test.c | 90 ++++++++++++++++++++++++++++++ include/drm/drm_connector.h | 2 + 4 files changed, 117 insertions(+) diff --git a/drivers/gpu/drm/drm_connector.c b/drivers/gpu/drm/drm_connector.c index f9a6cb792bda..9aec878c8509 100644 --- a/drivers/gpu/drm/drm_connector.c +++ b/drivers/gpu/drm/drm_connector.c @@ -995,6 +995,30 @@ static const struct drm_prop_enum_list drm_tv_mode_enum_list[] = { }; DRM_ENUM_NAME_FN(drm_get_tv_mode_name, drm_tv_mode_enum_list) +/** + * drm_get_tv_mode_from_name - Translates a TV mode name into its enum value + * @name: TV Mode name we want to convert + * @len: Length of @name + * + * Translates @name into an enum drm_connector_tv_mode. + * + * Returns: the enum value on success, a negative errno otherwise. + */ +int drm_get_tv_mode_from_name(const char *name, size_t len) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(drm_tv_mode_enum_list); i++) { + const struct drm_prop_enum_list *item = &drm_tv_mode_enum_list[i]; + + if (strlen(item->name) == len && !strncmp(item->name, name, len)) + return item->type; + } + + return -EINVAL; +} +EXPORT_SYMBOL(drm_get_tv_mode_from_name); + static const struct drm_prop_enum_list drm_tv_select_enum_list[] = { { DRM_MODE_SUBCONNECTOR_Automatic, "Automatic" }, /* DVI-I and TV-out */ { DRM_MODE_SUBCONNECTOR_Composite, "Composite" }, /* TV-out */ diff --git a/drivers/gpu/drm/tests/Makefile b/drivers/gpu/drm/tests/Makefile index b22ac96fdd65..c7903c112c65 100644 --- a/drivers/gpu/drm/tests/Makefile +++ b/drivers/gpu/drm/tests/Makefile @@ -3,6 +3,7 @@ obj-$(CONFIG_DRM_KUNIT_TEST) += \ drm_buddy_test.o \ drm_cmdline_parser_test.o \ + drm_connector_test.o \ drm_damage_helper_test.o \ drm_dp_mst_helper_test.o \ drm_format_helper_test.o \ diff --git a/drivers/gpu/drm/tests/drm_connector_test.c b/drivers/gpu/drm/tests/drm_connector_test.c new file mode 100644 index 000000000000..f2272b9df211 --- /dev/null +++ b/drivers/gpu/drm/tests/drm_connector_test.c @@ -0,0 +1,90 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Kunit test for drm_modes functions + */ + +#include + +#include + +struct drm_get_tv_mode_from_name_test { + const char *name; + enum drm_connector_tv_mode expected_mode; +}; + +#define TV_MODE_NAME(_name, _mode) \ + { \ + .name = _name, \ + .expected_mode = _mode, \ + } + +static void drm_test_get_tv_mode_from_name_valid(struct kunit *test) +{ + const struct drm_get_tv_mode_from_name_test *params = test->param_value; + + KUNIT_EXPECT_EQ(test, + drm_get_tv_mode_from_name(params->name, strlen(params->name)), + params->expected_mode); +} + +static const +struct drm_get_tv_mode_from_name_test drm_get_tv_mode_from_name_valid_tests[] = { + TV_MODE_NAME("NTSC", DRM_MODE_TV_MODE_NTSC), + TV_MODE_NAME("NTSC-443", DRM_MODE_TV_MODE_NTSC_443), + TV_MODE_NAME("NTSC-J", DRM_MODE_TV_MODE_NTSC_J), + TV_MODE_NAME("PAL", DRM_MODE_TV_MODE_PAL), + TV_MODE_NAME("PAL-M", DRM_MODE_TV_MODE_PAL_M), + TV_MODE_NAME("PAL-N", DRM_MODE_TV_MODE_PAL_N), + TV_MODE_NAME("SECAM", DRM_MODE_TV_MODE_SECAM), +}; + +static void +drm_get_tv_mode_from_name_valid_desc(const struct drm_get_tv_mode_from_name_test *t, + char *desc) +{ + sprintf(desc, "%s", t->name); +} +KUNIT_ARRAY_PARAM(drm_get_tv_mode_from_name_valid, + drm_get_tv_mode_from_name_valid_tests, + drm_get_tv_mode_from_name_valid_desc); + +static void drm_test_get_tv_mode_from_name_invalid(struct kunit *test) +{ + const char *name = *(const char **)test->param_value; + + KUNIT_EXPECT_LT(test, + drm_get_tv_mode_from_name(name, strlen(name)), + 0); +} + +static const +char *drm_get_tv_mode_from_name_invalid_tests[] = { + /* Truncated */ + "NTS", +}; + +static void +drm_get_tv_mode_from_name_invalid_desc(const char **name, char *desc) +{ + sprintf(desc, "%s", *name); +} +KUNIT_ARRAY_PARAM(drm_get_tv_mode_from_name_invalid, + drm_get_tv_mode_from_name_invalid_tests, + drm_get_tv_mode_from_name_invalid_desc); + +static struct kunit_case drm_get_tv_mode_from_name_tests[] = { + KUNIT_CASE_PARAM(drm_test_get_tv_mode_from_name_valid, + drm_get_tv_mode_from_name_valid_gen_params), + KUNIT_CASE_PARAM(drm_test_get_tv_mode_from_name_invalid, + drm_get_tv_mode_from_name_invalid_gen_params), + { } +}; + +static struct kunit_suite drm_get_tv_mode_from_name_test_suite = { + .name = "drm_get_tv_mode_from_name", + .test_cases = drm_get_tv_mode_from_name_tests, +}; + +kunit_test_suites( + &drm_get_tv_mode_from_name_test_suite +); diff --git a/include/drm/drm_connector.h b/include/drm/drm_connector.h index 4927dcb2573f..c856f4871697 100644 --- a/include/drm/drm_connector.h +++ b/include/drm/drm_connector.h @@ -1872,6 +1872,8 @@ const char *drm_get_dp_subconnector_name(int val); const char *drm_get_content_protection_name(int val); const char *drm_get_hdcp_content_type_name(int val); +int drm_get_tv_mode_from_name(const char *name, size_t len); + int drm_mode_create_dvi_i_properties(struct drm_device *dev); void drm_connector_attach_dp_subconnector_property(struct drm_connector *connector);