From patchwork Tue May 5 08:43:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Armstrong X-Patchwork-Id: 1283364 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=xOtfbKW0; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49GYCx68F1z9sRf for ; Tue, 5 May 2020 18:43:41 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id E6DC68205B; Tue, 5 May 2020 10:43:30 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b="xOtfbKW0"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 3B07881F6F; Tue, 5 May 2020 10:43:29 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id BA4AF81F6F for ; Tue, 5 May 2020 10:43:26 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=narmstrong@baylibre.com Received: by mail-wr1-x441.google.com with SMTP id g13so1659894wrb.8 for ; Tue, 05 May 2020 01:43:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=w1ainhA5kBRYbUW+5Sf76wIUmxG2kIm+RsEaOCbiRgE=; b=xOtfbKW0WbxaiOjGqN0juNHfEtofs11Is9h2WR73gvL0dPH9vnMPh3eToHu0O/cSw+ 8UcEmTGlSjjlTpbnx9nMaIfkJUWIopL7NmLos8N84AHW/AI/B0M84qDVihqXyMRVY+Pd CiifwpFm/Vwsd1tlgmmNXVekg/Hd5wx+JlZLTONvRxAjYEU2fcuKhdVVau2BUQesG75W IY1tTwrXpk1ENydMzb38GtkdW4VT1Kr3sUOEdwyvVceQIE8QeZQmfo12OGVTGCNZLwFx pzbQxfCeBpZNVrWN2CyhRwxXyS/9Zvrc/RyA73RsARM2a16pMLzSqqI1FWHeO/1wpbf0 01KQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=w1ainhA5kBRYbUW+5Sf76wIUmxG2kIm+RsEaOCbiRgE=; b=I9TrQ/3XO/nqWmoRpxoJ4WwwHAq9M3/RiR7HE5iV1EKQRUdSUwk1tCG3k6Vz6K8W8t 6tVGfmiV95tdnv6mWbKu5cNrJT4+3f2ZcBPwP7sDsjB/GN2qyErC5xDhdOUF56a5VuV5 QRTafAWa0EsfAsBGU5pRefFowfsJ5Je6jUscun5KZKmLFSvhEkDGHH1I0K+fjaZQTn0c iq4Ru9crTS8NcuwLp9F93LjXn2+RIgD0cNTq9v367jG4GUNjq/1Zo5ePlK5MWVw/c772 L1ISzfsFBMBprUBSKWt/N5MwlveY647dQHrucqJ0ORNmQ4HZQWmKIAQ/geaKjdRQgE7u 6SGg== X-Gm-Message-State: AGi0PuaGpGxz50IwuoyD02J3TnkcUEKuK8Ba50FxeepJBe+PbTVW26rZ ns+fFKsyRsbL13lwQaJ2CG8WutdISF4x6Q== X-Google-Smtp-Source: APiQypKFSFmR6ad/HIPH76unxGhkjTYSuzD14LS+KZjTYeZYyS347aMIRK/luUJaTm+BgthhyApuag== X-Received: by 2002:a5d:4d50:: with SMTP id a16mr2635340wru.219.1588668206336; Tue, 05 May 2020 01:43:26 -0700 (PDT) Received: from localhost.localdomain ([2a01:e35:2ec0:82b0:4460:3fd3:382:4a71]) by smtp.gmail.com with ESMTPSA id n2sm1959654wrt.33.2020.05.05.01.43.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2020 01:43:25 -0700 (PDT) From: Neil Armstrong To: sjg@chromium.org Cc: patrick.delaunay@st.com, u-boot@lists.denx.de, Neil Armstrong Subject: [PATCH v2 1/2] gpio: emulate open drain & open source in dm_gpio_set_value() Date: Tue, 5 May 2020 10:43:17 +0200 Message-Id: <20200505084318.15307-2-narmstrong@baylibre.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20200505084318.15307-1-narmstrong@baylibre.com> References: <20200505084318.15307-1-narmstrong@baylibre.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.2 at phobos.denx.de X-Virus-Status: Clean Handle the GPIOD_OPEN_DRAIN & GPIOD_OPEN_SOURCE flags to emulate open drain and open source by setting the GPIO line as input depending on the requested value. The behaviour is taken from the Linux gpiolib. Signed-off-by: Neil Armstrong Reviewed-by: Simon Glass --- drivers/gpio/gpio-uclass.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/drivers/gpio/gpio-uclass.c b/drivers/gpio/gpio-uclass.c index 757ab7106e..d3cea11f76 100644 --- a/drivers/gpio/gpio-uclass.c +++ b/drivers/gpio/gpio-uclass.c @@ -526,6 +526,21 @@ int dm_gpio_set_value(const struct gpio_desc *desc, int value) if (desc->flags & GPIOD_ACTIVE_LOW) value = !value; + + /* + * Emulate open drain by not actively driving the line high or + * Emulate open source by not actively driving the line low + */ + if ((desc->flags & GPIOD_OPEN_DRAIN && value) || + (desc->flags & GPIOD_OPEN_SOURCE && !value)) + return gpio_get_ops(desc->dev)->direction_input(desc->dev, + desc->offset); + else if (desc->flags & GPIOD_OPEN_DRAIN || + desc->flags & GPIOD_OPEN_SOURCE) + return gpio_get_ops(desc->dev)->direction_output(desc->dev, + desc->offset, + value); + gpio_get_ops(desc->dev)->set_value(desc->dev, desc->offset, value); return 0; } From patchwork Tue May 5 08:43:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Neil Armstrong X-Patchwork-Id: 1283365 X-Patchwork-Delegate: trini@ti.com Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=85.214.62.61; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.a=rsa-sha256 header.s=20150623 header.b=Thnva6gZ; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [85.214.62.61]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49GYD66vqQz9sRf for ; Tue, 5 May 2020 18:43:50 +1000 (AEST) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 92D298206A; Tue, 5 May 2020 10:43:33 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=baylibre-com.20150623.gappssmtp.com header.i=@baylibre-com.20150623.gappssmtp.com header.b="Thnva6gZ"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 0F18E8205C; Tue, 5 May 2020 10:43:31 +0200 (CEST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,SPF_HELO_NONE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x42d.google.com (mail-wr1-x42d.google.com [IPv6:2a00:1450:4864:20::42d]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 4A3AE81F67 for ; Tue, 5 May 2020 10:43:28 +0200 (CEST) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=baylibre.com Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=narmstrong@baylibre.com Received: by mail-wr1-x42d.google.com with SMTP id f13so1648495wrm.13 for ; Tue, 05 May 2020 01:43:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=UDEgJlJ/xQB0rGerYrY1j+C5t4XCs4BaoKQt2ogQYas=; b=Thnva6gZThEJYgA/Mi4HRCaqlvykvXk758YfOZhtP2zWMy8rE9ElzDzaPKhrQ65rK1 CPdycdRQ3/g1lsrNgPYOA0F1TNh60iCY1Wp2uhBapCMtNb4wnLV8aP8GpcPAvUH7eFLs 3t2NXxHXXsAvq08cy0aA1/charEFNtyM/5Hp1k1sIJeDUkqeyfUg9QK5/qojBFZclnqA bG2pNNNpsnsMlNstvqsKteC9OfO6aG7+TMRiZ0noUL3lq35VAe2WZnN3ZtpeCcIiCC6s 1SK5LEKxRWJHt9uBJq/WkTe19kYbxrQ/sUdfBT2aDLxFiDFMewmhTy/LqRM7K3rYVFsB MvVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=UDEgJlJ/xQB0rGerYrY1j+C5t4XCs4BaoKQt2ogQYas=; b=fl8CgQ5OLHlfTyo49ss+Y2TiA8OrqEGJmU6scgcoOv1thiKTE8udVkOM623iDMqg42 ZNR4hR4PJZbc9PDEJAjkreJowJXLiFS/QBRBAJd1vRWQkyI9SjmaSJ5jNA+dXEqOSdHA DpGAN6x32/qqC3vrO90TmyY6m0RxPkTpdb2EjQeaE1DMTADX/62fSKuOdJFd9xlP3iXD eOjIDS3XWDPQdTGnTC64g/OMUYTwEQfRXpGkEA0A2Ujp93/Ep4Tk6Tkez2HU4YiNZRwM 74VRoPo1KGMqcv7eDG11zQGsX7ng5jQzpc8MhA2CvMnE2z/gToW/n1M40T1JHN3sxnV9 wI1Q== X-Gm-Message-State: AGi0PuY5w/o30P+e7+Nc9Enittcfmn3GMflUj0c0wIlHuxnRqmXXopxj LZks2fPH9A4v7uGmslj5jdiddg== X-Google-Smtp-Source: APiQypJdtu0EPFyqI21y9ivTUJUpPpkkgr7kj/idfJO845avGcp7AU7YBifYs+SnIyecbBqV5OkQFA== X-Received: by 2002:adf:e812:: with SMTP id o18mr2535588wrm.185.1588668207776; Tue, 05 May 2020 01:43:27 -0700 (PDT) Received: from localhost.localdomain ([2a01:e35:2ec0:82b0:4460:3fd3:382:4a71]) by smtp.gmail.com with ESMTPSA id n2sm1959654wrt.33.2020.05.05.01.43.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 May 2020 01:43:27 -0700 (PDT) From: Neil Armstrong To: sjg@chromium.org Cc: patrick.delaunay@st.com, u-boot@lists.denx.de, Neil Armstrong Subject: [PATCH v2 2/2] test: dm: update test for open-drain/open-source emulation in gpio-uclass Date: Tue, 5 May 2020 10:43:18 +0200 Message-Id: <20200505084318.15307-3-narmstrong@baylibre.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20200505084318.15307-1-narmstrong@baylibre.com> References: <20200505084318.15307-1-narmstrong@baylibre.com> MIME-Version: 1.0 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.30rc1 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.2 at phobos.denx.de X-Virus-Status: Clean Add tests for testing open-drain/open-source emulation in gpio-uclass. It also adds two test3-gpios configured as GPIO_ACTIVE_LOW. Signed-off-by: Neil Armstrong Reviewed-by: Simon Glass --- arch/sandbox/dts/test.dts | 4 +- test/dm/gpio.c | 89 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) diff --git a/arch/sandbox/dts/test.dts b/arch/sandbox/dts/test.dts index 4bccfbe6e1..e9b9404363 100644 --- a/arch/sandbox/dts/test.dts +++ b/arch/sandbox/dts/test.dts @@ -104,7 +104,9 @@ <&gpio_c 2 GPIO_OUT>, <&gpio_c 3 (GPIO_IN|GPIO_PULL_UP)>, <&gpio_c 4 (GPIO_IN|GPIO_PULL_DOWN)>, - <&gpio_c 5 GPIO_IN>; + <&gpio_c 5 GPIO_IN>, + <&gpio_c 6 (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN)>, + <&gpio_c 7 (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE)>; int-value = <1234>; uint-value = <(-1234)>; int64-value = /bits/ 64 <0x1111222233334444>; diff --git a/test/dm/gpio.c b/test/dm/gpio.c index f5c7aaf3bc..7c18e5c411 100644 --- a/test/dm/gpio.c +++ b/test/dm/gpio.c @@ -112,6 +112,95 @@ static int dm_test_gpio(struct unit_test_state *uts) } DM_TEST(dm_test_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); +/* Test that GPIO open-drain/open-source emulation works correctly */ +static int dm_test_gpio_opendrain_opensource(struct unit_test_state *uts) +{ + struct gpio_desc desc_list[8]; + struct udevice *dev, *gpio_c; + char buf[80]; + + ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); + ut_asserteq_str("a-test", dev->name); + + ut_assertok(uclass_get_device(UCLASS_GPIO, 3, &gpio_c)); + ut_asserteq_str("pinmux-gpios", gpio_c->name); + + ut_asserteq(8, gpio_request_list_by_name(dev, "test3-gpios", desc_list, + ARRAY_SIZE(desc_list), 0)) + + ut_asserteq(true, !!device_active(gpio_c)); + ut_asserteq_ptr(gpio_c, desc_list[0].dev); + ut_asserteq_ptr(gpio_c, desc_list[1].dev); + ut_asserteq_ptr(gpio_c, desc_list[2].dev); + ut_asserteq_ptr(gpio_c, desc_list[3].dev); + ut_asserteq_ptr(gpio_c, desc_list[4].dev); + ut_asserteq_ptr(gpio_c, desc_list[5].dev); + ut_asserteq_ptr(gpio_c, desc_list[6].dev); + ut_asserteq_ptr(gpio_c, desc_list[7].dev); + + /* GPIO 0 is (GPIO_OUT|GPIO_OPEN_DRAIN) */ + ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, + sandbox_gpio_get_dir_flags(gpio_c, 0)); + + /* Set it as output high, should become an input */ + ut_assertok(dm_gpio_set_value(&desc_list[0], 1)); + ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf))); + ut_asserteq_str("c0: input: 0 [x] a-test.test3-gpios0", buf); + + /* Set it as output low, should become output low */ + ut_assertok(dm_gpio_set_value(&desc_list[0], 0)); + ut_assertok(gpio_get_status(gpio_c, 0, buf, sizeof(buf))); + ut_asserteq_str("c0: output: 0 [x] a-test.test3-gpios0", buf); + + /* GPIO 1 is (GPIO_OUT|GPIO_OPEN_SOURCE) */ + ut_asserteq(GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, + sandbox_gpio_get_dir_flags(gpio_c, 1)); + + /* Set it as output high, should become output high */ + ut_assertok(dm_gpio_set_value(&desc_list[1], 1)); + ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); + ut_asserteq_str("c1: output: 1 [x] a-test.test3-gpios1", buf); + + /* Set it as output low, should become an input */ + ut_assertok(dm_gpio_set_value(&desc_list[1], 0)); + ut_assertok(gpio_get_status(gpio_c, 1, buf, sizeof(buf))); + ut_asserteq_str("c1: input: 1 [x] a-test.test3-gpios1", buf); + + /* GPIO 6 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_DRAIN) */ + ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_DRAIN, + sandbox_gpio_get_dir_flags(gpio_c, 6)); + + /* Set it as output high, should become output low */ + ut_assertok(dm_gpio_set_value(&desc_list[6], 1)); + ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf))); + ut_asserteq_str("c6: output: 0 [x] a-test.test3-gpios6", buf); + + /* Set it as output low, should become an input */ + ut_assertok(dm_gpio_set_value(&desc_list[6], 0)); + ut_assertok(gpio_get_status(gpio_c, 6, buf, sizeof(buf))); + ut_asserteq_str("c6: input: 0 [x] a-test.test3-gpios6", buf); + + /* GPIO 7 is (GPIO_ACTIVE_LOW|GPIO_OUT|GPIO_OPEN_SOURCE) */ + ut_asserteq(GPIOD_ACTIVE_LOW | GPIOD_IS_OUT | GPIOD_OPEN_SOURCE, + sandbox_gpio_get_dir_flags(gpio_c, 7)); + + /* Set it as output high, should become an input */ + ut_assertok(dm_gpio_set_value(&desc_list[7], 1)); + ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf))); + ut_asserteq_str("c7: input: 0 [x] a-test.test3-gpios7", buf); + + /* Set it as output low, should become output high */ + ut_assertok(dm_gpio_set_value(&desc_list[7], 0)); + ut_assertok(gpio_get_status(gpio_c, 7, buf, sizeof(buf))); + ut_asserteq_str("c7: output: 1 [x] a-test.test3-gpios7", buf); + + ut_assertok(gpio_free_list(dev, desc_list, 8)); + + return 0; +} +DM_TEST(dm_test_gpio_opendrain_opensource, + DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); + /* Test that sandbox anonymous GPIOs work correctly */ static int dm_test_gpio_anon(struct unit_test_state *uts) {