From patchwork Tue Dec 4 12:43:03 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 152803 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp8022829ljp; Tue, 4 Dec 2018 04:43:17 -0800 (PST) X-Google-Smtp-Source: AFSGD/WOVPhCqg4668aqxqwLXaL7S+yFLfOiLGYicura6iNjXHxtca4XfR2x4xGXtHdlrTJYjS84 X-Received: by 2002:a62:3888:: with SMTP id f130mr19447205pfa.132.1543927397158; Tue, 04 Dec 2018 04:43:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543927397; cv=none; d=google.com; s=arc-20160816; b=B8j4W9Uush8MoAdFeiQrSu+bfaM6eHS/P6HSjXvCHl+qkb/IQ71pyat/xmgA4EJFT4 yHM7HQlMDe8DV+guVR9PVaWOzh2gBHMBNM11Cz2KOaMix5Bqs+eqPVm3lBRNdVJmVSE8 0hBBnCVI/KSC9mA4KTVx2l2O4ZXfbeIfLEC/pHd10U3m2yQfbTOvtSF9NXXUIpv1SyDI rq7eAFmL/UdDHMkScF89ei+lYIz0ocL/b0TSw8MYR1n+ofimOtSeMFMxuBv765roHGzs 50vD5S1oXTqW7qDF9V4JuMkM6HBumGrExu96kLDQbHmZbKWU4rjmywYbx1se4WO6F+7t Xs+Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=8oQsJI6HVILzABwUd5fVqqmFemrU4B6LB4duQ4c5s/Q=; b=GDNLM8/fcoEBgexfrCqAx5sVy29SbwIf6rVJJmMoqXhkpdzmzZngq3luDXJ2ww7qLn ObTVUEfXEP0vKQ+ApZQKb13vfmPX3y2YrqGnw+YTRIvC+pe9f9hJJM7iCsWcSSW6WeTs hbH6g7KwUtVZHh28I/JaWbUq4rfXECpTIREG92L0gDD5W2Sg/+zZE29qF4md95POy3aB PIfgORjHNHJTDlXU+XppqmZwLjbKJkVwr6pD2tKvLOcerlPbU8xf4EqvVFVgBC/M/zFu nouaCxhy1UHbFLe4L4hgVTzWcvu19ZS16olcm4E7uiP6uM0331gNcNR2BIa2IbX/K4ql oRwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g8H7z0f8; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q73si17892092pfi.205.2018.12.04.04.43.16; Tue, 04 Dec 2018 04:43:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=g8H7z0f8; spf=pass (google.com: best guess record for domain of linux-gpio-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-gpio-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726152AbeLDMnQ (ORCPT + 5 others); Tue, 4 Dec 2018 07:43:16 -0500 Received: from mail-lj1-f194.google.com ([209.85.208.194]:39915 "EHLO mail-lj1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725767AbeLDMnQ (ORCPT ); Tue, 4 Dec 2018 07:43:16 -0500 Received: by mail-lj1-f194.google.com with SMTP id t9-v6so14744756ljh.6 for ; Tue, 04 Dec 2018 04:43:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=8oQsJI6HVILzABwUd5fVqqmFemrU4B6LB4duQ4c5s/Q=; b=g8H7z0f8Lbn2I8nsy/jR1uuK7OzePbBiD/NJVUBQv7WSxjoSh4IFn+ICWj5uEOq0KB DKK55II8VPDnT1Jiz8j7ntq9bFsPeNu0ms6bOn2Oo8Yxc8m7AS7zLt0A7VltmFmVQW17 rNEo7S8W9gluZFyXvIpx8BZyyN2r3bBZCC5dc= 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:mime-version :content-transfer-encoding; bh=8oQsJI6HVILzABwUd5fVqqmFemrU4B6LB4duQ4c5s/Q=; b=FWStZoHVreC0NQApfIU6lVMNaJmlhUQmDX6Q5OXP8O3vDaq9eoCgGykWdrqUPgg5p0 jU+8jK5kmYVAkU1Z7AJhqgUAeeiTut4HFoUz2X82VKYda30gb+UhjCQgQl1pQyL7VqWX 4tKEIAeU72H7M2akLthXJHG19UCjhzgALSQoKvpZO+wS2s1tli+9TsGpaiOYgWO+tB3v PzvosJoFnPGLyVn2fOnaRktSeIyQWfEsxN939SG+0IHTFm3yMTUHPDMSHmKsVXjK/TkB Bvg4s5aEM92UNeTrxlOFdA27CL+2yhAcSWUgT5DrXOLP4cqRekaMp+XU2T7k2une6K/s 3kzg== X-Gm-Message-State: AA+aEWbaPcNjSaWZVr8B3R3dex8vkuSui4xedwdjtmAKNIXgPPgYxrRJ mJibcbt/kxIPsg085JVOZ9lJkJ4U2I7iwA== X-Received: by 2002:a2e:5b1d:: with SMTP id p29-v6mr12621686ljb.176.1543927391848; Tue, 04 Dec 2018 04:43:11 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id f8sm3194154lfe.72.2018.12.04.04.43.10 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Tue, 04 Dec 2018 04:43:10 -0800 (PST) From: Linus Walleij To: linux-gpio@vger.kernel.org Cc: Linus Walleij , Mark Brown , Bartosz Golaszewski , Marek Szyprowski Subject: [PATCH] gpio: devres: Handle nonexclusive GPIOs Date: Tue, 4 Dec 2018 13:43:03 +0100 Message-Id: <20181204124303.8233-1-linus.walleij@linaro.org> X-Mailer: git-send-email 2.19.2 MIME-Version: 1.0 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org When we get a nonexeclusive GPIO descriptor using managed resources, we should only add it to the list of managed resources once: on the first user. Augment the devm_gpiod_get_index() and devm_gpiod_get_from_of_node() calls to account for this by checking if the descriptor is already resource managed before we proceed to allocate a new resource management struct. Cc: Mark Brown Cc: Bartosz Golaszewski Cc: Marek Szyprowski Fixes: b0ce7b29bfcd ("regulator/gpio: Allow nonexclusive GPIO access") Reported-by: Marek Szyprowski Signed-off-by: Linus Walleij --- This fix is in response to an issue Marek saw in the fixups for resource-managed GPIO descriptors used with ena_gpiod in the regulator subsystem. I will merge this as a fix so the other fixes can assume it is in place once I have a confirmation is solves the problem. --- drivers/gpio/gpiolib-devres.c | 50 ++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 12 deletions(-) -- 2.19.2 diff --git a/drivers/gpio/gpiolib-devres.c b/drivers/gpio/gpiolib-devres.c index e35751bf0ea8..304ad54dabb7 100644 --- a/drivers/gpio/gpiolib-devres.c +++ b/drivers/gpio/gpiolib-devres.c @@ -98,15 +98,28 @@ struct gpio_desc *__must_check devm_gpiod_get_index(struct device *dev, struct gpio_desc **dr; struct gpio_desc *desc; + desc = gpiod_get_index(dev, con_id, idx, flags); + if (IS_ERR(desc)) + return desc; + + /* + * For non-exclusive GPIO descriptors, check if this descriptor is + * already under resource management by this device. + */ + if (flags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { + struct devres *dres; + + dres = devres_find(dev, devm_gpiod_release, + devm_gpiod_match, desc); + if (dres) + return desc; + } + dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), GFP_KERNEL); - if (!dr) + if (!dr) { + gpiod_put(desc); return ERR_PTR(-ENOMEM); - - desc = gpiod_get_index(dev, con_id, idx, flags); - if (IS_ERR(desc)) { - devres_free(dr); - return desc; } *dr = desc; @@ -140,15 +153,28 @@ struct gpio_desc *devm_gpiod_get_from_of_node(struct device *dev, struct gpio_desc **dr; struct gpio_desc *desc; + desc = gpiod_get_from_of_node(node, propname, index, dflags, label); + if (IS_ERR(desc)) + return desc; + + /* + * For non-exclusive GPIO descriptors, check if this descriptor is + * already under resource management by this device. + */ + if (dflags & GPIOD_FLAGS_BIT_NONEXCLUSIVE) { + struct devres *dres; + + dres = devres_find(dev, devm_gpiod_release, + devm_gpiod_match, desc); + if (dres) + return desc; + } + dr = devres_alloc(devm_gpiod_release, sizeof(struct gpio_desc *), GFP_KERNEL); - if (!dr) + if (!dr) { + gpiod_put(desc); return ERR_PTR(-ENOMEM); - - desc = gpiod_get_from_of_node(node, propname, index, dflags, label); - if (IS_ERR(desc)) { - devres_free(dr); - return desc; } *dr = desc;