From patchwork Wed Dec 5 12:47:17 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 152901 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp9256317ljp; Wed, 5 Dec 2018 04:48:24 -0800 (PST) X-Google-Smtp-Source: AFSGD/WpvTse4YSeCRSUBoCyBRUtCvJadSxzAgJMOHEBmS7ancpOXVU0ln3DhbQ33cI2ldb0Qitg X-Received: by 2002:a62:18ce:: with SMTP id 197mr25206926pfy.88.1544014104212; Wed, 05 Dec 2018 04:48:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544014104; cv=none; d=google.com; s=arc-20160816; b=vMoSVjZpiPQbf34+/2l6AFS2GRS9zJNXcCy49FF0jcmOzv0T91b4BUwETIXWYoaoGq jGt2i1USne8nT8ce/fklOy47mBRt2pbzTTN3nlhQYuipBPS+PyaXzEmpM19taLv6glBV yXvB+BJG/OZ/XyJQCeJNWX53b2l2B0DGNS1qtyA9EIlLvY2fudG4ndmWYbEY7ylK4oaz DxBpt0/GGk6v0S8X+QqNESpn5CvQlJfyPhuvY17iWDUQR3PVitANO3LUOvGAi1Fy3Gw8 5x8qQk0/etMBLImMb5ZkO3DNf19m1Uc4O0VKCi2YAsMGa+0KmZ3dpJvupW7F9o3hx0Sk WemQ== 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=2AByHMOP3S01NinyElG6HmeuOLN4IWUXl2HPqQubasw=; b=XEqa4hyYiUVz9WaT/M4vHx1hZlUUUpg6I7IlKDTPPdsHZR8XKY6yd6qASjWA4d0+Gj hNCrYz8EqJbFhpilVhRhCVLTIoCREcVXq06HE37AjzrFyWJsiAJhQIai5PSji/O8Uh9X F6B/diazYMGelLLVU2WqD9h0TKY4zzn5jDZg297/wW82Goq9VE9M1Pcv0P7d+RDrkr4N bwkYq8vyuq95Qn0sUGhvU26OCGUx+Dldf7XNqJAuK3sdp8NomrFDL2vj7rnCEJ/c6Ght EUl+mP38bOHXccSozfh57iOnvdm5urt1vDq6bvo9ZKtguuVFiIK17gFEXFDxTBwTlaDL PoIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=KHwdU6US; 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 w12si18042427pgs.182.2018.12.05.04.48.23; Wed, 05 Dec 2018 04:48:24 -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=KHwdU6US; 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 S1728029AbeLEMsW (ORCPT + 31 others); Wed, 5 Dec 2018 07:48:22 -0500 Received: from mail-lj1-f196.google.com ([209.85.208.196]:41427 "EHLO mail-lj1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727700AbeLEMrz (ORCPT ); Wed, 5 Dec 2018 07:47:55 -0500 Received: by mail-lj1-f196.google.com with SMTP id k15-v6so733589ljc.8 for ; Wed, 05 Dec 2018 04:47:53 -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=2AByHMOP3S01NinyElG6HmeuOLN4IWUXl2HPqQubasw=; b=KHwdU6USscCF/4a1LQxFqOmSbcfva5RfFEiVtKCJ57nfdTXetVjAPKMnfaTs2mLp1e HeC+6+QT6Q0OZcCRSyZ+yFTmMx7A7FHwFNOC2k2ymgTM+U47gMDDj/4C2Pv6iUufa3m8 lGbwGevATPK87shLl0vP1HQt1mAVxJsTTKByI= 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=2AByHMOP3S01NinyElG6HmeuOLN4IWUXl2HPqQubasw=; b=P+CI0rjPGmNOxTeVfnyot4utmpyrISxL8mft7+8nG+CrjEmlUMQ/URLs3pSQp8ACzJ ccYU2N43VZKzK0/DuB8tVFnNCXIfI9P2H1i293e8X02X2fJSVhC3bZoZ1ShWke5Gnkm4 8Hh/DGjRV1cIxALdseTPgJ38BCDyD4Z9sLlBcaSuPkZNoWftxbQtF/Alyf2zJQDo0lIV tNpf/TNYRAQu55iwLa4aHmfGFXkg9kGSpL60xIF39twAtuuvhbQN9PqmjvSsfPGD0jM6 Wq7AfKUeXl1ARFnJK7c/rL/zOQf16utWo+L+OkdgcuCPdCkIvi02N6ZX/V7GVnntFEJ5 hpTA== X-Gm-Message-State: AA+aEWY/DMrudoD/2aqXIlFDym6WMXMWas5IhUymLtr7yvQTWUoiBOZs ywIRc8aauJge868AU0QPMeFghQ== X-Received: by 2002:a2e:990e:: with SMTP id v14-v6mr17428594lji.60.1544014072481; Wed, 05 Dec 2018 04:47:52 -0800 (PST) Received: from genomnajs.ideon.se ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id e19-v6sm3797125ljf.67.2018.12.05.04.47.51 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 05 Dec 2018 04:47:51 -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 11/15 v3] gpio: Add devm_gpiod_unhinge() Date: Wed, 5 Dec 2018 13:47:17 +0100 Message-Id: <20181205124721.26624-12-linus.walleij@linaro.org> X-Mailer: git-send-email 2.19.2 In-Reply-To: <20181205124721.26624-1-linus.walleij@linaro.org> References: <20181205124721.26624-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 This adds a function named devm_gpiod_unhinge() that removes the resource management from a GPIO descriptor. I am not sure if this is the best anglosaxon name for the function, no other managed resources have an equivalent currently, but I chose "unhinge" as the closest intuitive thing I could imagine that fits Rusty Russell's API design criterions "the obvious use is the correct one" and "the name tells you how to use it". The idea came out of a remark from Mark Brown that it should be possible to handle over management of a resource from devres to the regulator core, and indeed we can do that. Cc: Bartosz Golaszewski Signed-off-by: Linus Walleij --- ChangeLog v2->v3: - Make sure to bail out of gpiod_unhinge() if the descritor passed in is NULL or an error pointer. - Accept of devres_destroy() returns -ENOENT: if we have nonexclusive GPIOs, we may attempt to remove resource management from the same descriptor multiple times. ChangeLog v1->v2: - New patch to be able to hand over GPIO descriptors to the regulator core. - Mark: feel free to apply this to the regulator tree with the regulator tree with the rest. --- Documentation/driver-model/devres.txt | 1 + drivers/gpio/gpiolib-devres.c | 30 +++++++++++++++++++++++++++ include/linux/gpio/consumer.h | 10 +++++++++ 3 files changed, 41 insertions(+) -- 2.19.2 diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index 43681ca0837f..fc4cc24dfb97 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt @@ -254,6 +254,7 @@ GPIO devm_gpiod_get_index_optional() devm_gpiod_get_optional() devm_gpiod_put() + devm_gpiod_unhinge() devm_gpiochip_add_data() devm_gpiochip_remove() devm_gpio_request() diff --git a/drivers/gpio/gpiolib-devres.c b/drivers/gpio/gpiolib-devres.c index a57e968025a8..e9a17ec4de02 100644 --- a/drivers/gpio/gpiolib-devres.c +++ b/drivers/gpio/gpiolib-devres.c @@ -346,6 +346,36 @@ void devm_gpiod_put(struct device *dev, struct gpio_desc *desc) } EXPORT_SYMBOL(devm_gpiod_put); +/** + * devm_gpiod_unhinge - Remove resource management from a gpio descriptor + * @dev: GPIO consumer + * @desc: GPIO descriptor to remove resource management from + * + * Remove resource management from a GPIO descriptor. This is needed when + * you want to hand over lifecycle management of a descriptor to another + * mechanism. + */ + +void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc) +{ + int ret; + + if (IS_ERR_OR_NULL(desc)) + return; + ret = devres_destroy(dev, devm_gpiod_release, + devm_gpiod_match, desc); + /* + * If the GPIO descriptor is requested as nonexclusive, we + * may call this function several times on the same descriptor + * so it is OK if devres_destroy() returns -ENOENT. + */ + if (ret == -ENOENT) + return; + /* Anything else we should warn about */ + WARN_ON(ret); +} +EXPORT_SYMBOL(devm_gpiod_unhinge); + /** * devm_gpiod_put_array - Resource-managed gpiod_put_array() * @dev: GPIO consumer diff --git a/include/linux/gpio/consumer.h b/include/linux/gpio/consumer.h index 348885f2f3d3..8aebcf822082 100644 --- a/include/linux/gpio/consumer.h +++ b/include/linux/gpio/consumer.h @@ -104,6 +104,7 @@ struct gpio_descs *__must_check devm_gpiod_get_array_optional(struct device *dev, const char *con_id, enum gpiod_flags flags); void devm_gpiod_put(struct device *dev, struct gpio_desc *desc); +void devm_gpiod_unhinge(struct device *dev, struct gpio_desc *desc); void devm_gpiod_put_array(struct device *dev, struct gpio_descs *descs); int gpiod_get_direction(struct gpio_desc *desc); @@ -249,6 +250,15 @@ static inline void gpiod_put(struct gpio_desc *desc) WARN_ON(1); } +static inline void devm_gpiod_unhinge(struct device *dev, + struct gpio_desc *desc) +{ + might_sleep(); + + /* GPIO can never have been requested */ + WARN_ON(1); +} + static inline void gpiod_put_array(struct gpio_descs *descs) { might_sleep();