From patchwork Thu Dec 6 12:43:45 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 153017 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp10479372ljp; Thu, 6 Dec 2018 04:44:25 -0800 (PST) X-Google-Smtp-Source: AFSGD/XOTEsc3qVNKhR4GMygFM0Rc+q9d6c1+i7DJJOU1Yx7JZUxDG3JC6lWZ0EevE28oAAd73sY X-Received: by 2002:a17:902:2dc3:: with SMTP id p61mr27445968plb.166.1544100265372; Thu, 06 Dec 2018 04:44:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544100265; cv=none; d=google.com; s=arc-20160816; b=jQCl/GNnpgLMKOrCnRgqpEIzvBrq+LxKvnuiF07/IFpcP7FEh2L2heFy5vQUlDIUVg BwuEAo+Kn1LnLjf/h0GcHo7ChN1VIc6jJWTB6nK3VbN7f/A9T+G8ZwGKw5nXEgPgUMpX UvaYC1wc5KK84wpLj0cAIzDedu77CVGVPjQ8BOThdW3906RGg+jN63F3pOx79D726OKj UR7VptlKOBLcvNBTYCDSHxHbdeh0KIjALpiAcOu4PRLIXu5dgH9FSulA50Z+uVlydO16 90nmsgAGRRVAWyOZysUZASLxKI8LpaJyT83T3UDRe5XKgAbjlwWF8h7a3VpktnB7zUIY 5PtQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=AhlKqI8Sf7OwwyTXqg/8o97lEVTsK7W63wvXXbyp2DA=; b=qYqVH/3W6zAnZ6aRrZcGo+quXQWBtoqRzr7dHVtMg2o6sE2BpwBdQycAi3SMcnodcF lDxqfdVTx0j7TC/7LREcr1cnLcK/vkcJB23r8WjG61y4cMKE3obg97PsEVwQ62paae3X K14k8ylbaU/phtuEaBPldpV6T6kxf3VwxFjz/X0gHuZP31/ssVM/x5DlImSWvNNvcNQm /u4XLbYGru6iEJ8mRL3+1cPQ8XHurrNec+QHGPfQ6bSYf5AgI7Uq+Mv3JyjajvJVrjU6 CpXVY8tYEnud/d6irRykWffGy7XCz2ETg4TpUV5kPi/v83cS9XJshi86SHAmGDTPeeTv 285w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=NgNgybUV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 h68si208548plb.375.2018.12.06.04.44.24; Thu, 06 Dec 2018 04:44:25 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=NgNgybUV; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-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 S1729729AbeLFMoX (ORCPT + 31 others); Thu, 6 Dec 2018 07:44:23 -0500 Received: from mail-lf1-f68.google.com ([209.85.167.68]:39866 "EHLO mail-lf1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729369AbeLFMoV (ORCPT ); Thu, 6 Dec 2018 07:44:21 -0500 Received: by mail-lf1-f68.google.com with SMTP id n18so239933lfh.6 for ; Thu, 06 Dec 2018 04:44:19 -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:in-reply-to:references :mime-version:content-transfer-encoding; bh=AhlKqI8Sf7OwwyTXqg/8o97lEVTsK7W63wvXXbyp2DA=; b=NgNgybUVYedAJyqssM+aetsaZYtCBJqAHmLXHKsu3elwVSMH2cra2hWqdiJXCErpFG VHMSM+gH3wRZw3Z8T6u4zUWhIdEbC62+nMuhFjLTzG7ulzovGCDz6ofeyDuVmt2dz5yN njJut7HTp1QVbwoRnO5rwv9NTpFteEL7OtwTo= 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=AhlKqI8Sf7OwwyTXqg/8o97lEVTsK7W63wvXXbyp2DA=; b=tA84c2cEVBA1d+g8wCBC/UXwEEXuFZ+xuNqJurLKu/r5jWdughMQ6AQu3EH/N2YTFz NEEuBLKQLoNrN/zvXvVJfXw2oU0YkH3RJtHQT8n/mmwGP4PmbzNMjQX4/mo/axZJJtEZ nHOJrjbLII52v6qt1x8+OvU1/e+k46nHaYXvCV7ldEneBRSPdO6pUNCmkJ8arRGs+GfC /flROkxwXF016NCQyr6KfsEodcmydiXN/QnkVNrS0a0IF6SZt6dhjCeFnzEq38zsSHEI jGhomFhkqFOkDJSgUzLuhnFpqaE8/Acgh6Ta4Pv27TL8/HvVztqeSG/cz+g5d6x+PKyO ZVBA== X-Gm-Message-State: AA+aEWZBuhRq+PTDfrfsb2KZaucd5CuZs/HkVV5Gsa2PHp50BCK3RSvl EgU5lFCK3l7kWra2dfdplr21JQ== X-Received: by 2002:a19:22c2:: with SMTP id i185mr14023531lfi.2.1544100258697; Thu, 06 Dec 2018 04:44:18 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id j25-v6sm44071lji.77.2018.12.06.04.44.17 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 06 Dec 2018 04:44:17 -0800 (PST) From: Linus Walleij To: Liam Girdwood , Mark Brown Cc: linux-kernel@vger.kernel.org, Charles Keepax , Bartosz Golaszewski , Marek Szyprowski , Linus Walleij Subject: [PATCH 09/15 v4] gpio: devres: Handle nonexclusive GPIOs Date: Thu, 6 Dec 2018 13:43:45 +0100 Message-Id: <20181206124351.10155-10-linus.walleij@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181206124351.10155-1-linus.walleij@linaro.org> References: <20181206124351.10155-1-linus.walleij@linaro.org> MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@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 --- ChangeLog v3->v4: - Pass a pointer to a pointer &desc instead of just the descriptor pointer: it is the memory address storing the pointer that hooks into resource management. ChangeLog v1->v3: - New patch. - 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 first thought to apply it to the GPIO tree directly, but since it is not a regression it is better to put it into the regulator tree with the rest of the patches. --- 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 01959369360b..f9591b5c9748 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;