From patchwork Thu Dec 6 12:43:46 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 153023 Delivered-To: patch@linaro.org Received: by 2002:a2e:299d:0:0:0:0:0 with SMTP id p29-v6csp10479858ljp; Thu, 6 Dec 2018 04:45:01 -0800 (PST) X-Google-Smtp-Source: AFSGD/XV/7t0aK5xUOt3wrfbh+g1U3OicvFxsZtx+zXBmbDfVmdlmovFdbCBApHzfcVT3qqvZ2Ft X-Received: by 2002:a17:902:b903:: with SMTP id bf3mr27523277plb.289.1544100301648; Thu, 06 Dec 2018 04:45:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1544100301; cv=none; d=google.com; s=arc-20160816; b=TDrt2YbP+IgFqGORyWHbAi4clhkcLjq+37cxGWJOKcQkBdQbNWIFkoYM3CHwzwkK0c Jv3ztBgwpaylXAKRIil8CQwrSGHe6GHCyALByX4dyn39GllxcWFOMd38HykRIrWhe2xa y9MJNy6LEb3DHAZMzmWpqELfUaPJFKcx7kKu93kgfb+/pve1KDN8y/EPoieC1nutpTJO GrgDk2CFUiDMKY5elluB+pKWOJfxqLa+VbJkIEKNJstmQtgaUaJXlHfXR4Vdq6La15a+ FoHaC8MlHLMqT0S9HKPIiBQ7TKKd5vdBCrIEcLxQaHO+zKCji2em02dm3N/ZHcNBfLVm u2fA== 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=m0RHJZseajc2Hw4Z+KYKfKkiR8hwIO3G8Y+psnfNew4=; b=ygZPXws2fGQRcry/JQTDDTWCs5eJO5jBBKKOijokjN2o9GGB4eokve43SvYdH0jAMT DkCNgOxKQ0v5hf2CCvdYplyAzdND+G5kPtwC3vEGAdWigqh7hFDheGhdnKR4EbTL12m+ 3vfqt1vLn0PmL68pOqleYivBUaJzGrzAOSXiZAfUATf8JvTgxVn+FJBm5y1f6tQ9hS7X 1B3lI/UzJcoeGubGj4LARizR46b/xyiYAEdC78P9b39A+R5HM8ayHRTlMLlftr6Ynu2u oyQJdttBO5qJRcT1xUmrytrieY2tQ4NF+bOulIJ9ubs7BFWuL6Aa6exryzX9V83PR7xx +c9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=b4Y9SCB7; 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 p64si240753pfg.79.2018.12.06.04.45.01; Thu, 06 Dec 2018 04:45:01 -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=b4Y9SCB7; 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 S1729746AbeLFMo1 (ORCPT + 31 others); Thu, 6 Dec 2018 07:44:27 -0500 Received: from mail-lf1-f67.google.com ([209.85.167.67]:38811 "EHLO mail-lf1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729667AbeLFMoY (ORCPT ); Thu, 6 Dec 2018 07:44:24 -0500 Received: by mail-lf1-f67.google.com with SMTP id p86so246668lfg.5 for ; Thu, 06 Dec 2018 04:44:21 -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=m0RHJZseajc2Hw4Z+KYKfKkiR8hwIO3G8Y+psnfNew4=; b=b4Y9SCB7D7zraqlNG7d7lOK10xEvKIP5VAAtxCJ+nEq3Jvjc1pEw1WosBwP1QCyeko 7brm4VxHcNOiLQbaYmMAvZDdYrwJRpjFj7Z737OxwFwYlJ9ftG/43Vnd6x2epVLVW4X6 4rSnlJTTTWWI3doFqTN+NUj0B6oYk4m9XQ0PU= 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=m0RHJZseajc2Hw4Z+KYKfKkiR8hwIO3G8Y+psnfNew4=; b=n4ycO43DeNYBWW8T95A+6M6JpGH4pCedHBk8xkDX7lY9Q2GmXrwrAWC+949pnc/FgX CcNPNrzxfmufzh9Ql8PZEt21bBYHK3VQyKQbgenlGx3U3voHHYBzKEIxKPMohc/hRsLH +0tuh/Cppj+d6jl7hLjDDw92RCyOVnssA4r5UzZ8HCTFjBUGTdYrtfWiMRQU+jJvF6uc jM0XkptWTX/rFZNVqxhuMh11hblUntOa0S8lcpYe99AAt57v8WAeHjwFZg/TaeZiGezi 93n7TjvJQf3fBTfvJwTSnFNmxGeMXG1+c/dHMdgwv8zMvsJzMppc8tfpRG0LFNtjIhbm lbWg== X-Gm-Message-State: AA+aEWYUl3b9S1sc1RVl+LWE6gOoeSqrmSGKDpbGQ1oE7GZzao466yF6 KAbe9TrfhRy2346PKDX3RILYww== X-Received: by 2002:a19:5059:: with SMTP id z25mr16458447lfj.120.1544100260998; Thu, 06 Dec 2018 04:44:20 -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.19 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 06 Dec 2018 04:44:19 -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 10/15 v4] gpio: Add devm_gpiod_unhinge() Date: Thu, 6 Dec 2018 13:43:46 +0100 Message-Id: <20181206124351.10155-11-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 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 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 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 f9591b5c9748..0acc2cc6e868 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();