From patchwork Thu Oct 6 14:36:52 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 613606 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8F6ADC4332F for ; Thu, 6 Oct 2022 14:37:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231128AbiJFOhE (ORCPT ); Thu, 6 Oct 2022 10:37:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229637AbiJFOhD (ORCPT ); Thu, 6 Oct 2022 10:37:03 -0400 Received: from mail-lf1-x12d.google.com (mail-lf1-x12d.google.com [IPv6:2a00:1450:4864:20::12d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4D9328321A; Thu, 6 Oct 2022 07:37:02 -0700 (PDT) Received: by mail-lf1-x12d.google.com with SMTP id a29so3039135lfo.1; Thu, 06 Oct 2022 07:37:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date; bh=00zNPjSV0NBnL/fqot4cVHiAuUteLJu0Hqble2AqPlI=; b=jLkxnaE6g2QlVfw6r4SfnRsGhqANVLQCmbqVqAPddbzc2Hlt6vumlPhAwOUTFRAYs9 d/KNkdWYMbp3SCC545Q7PKeHBlOva9VHeiqBqhKaj/jcCSRnMXB3mZDDWHY2wnuwF2VV 9fcWy6y2YU08w3rVhwz3xeO9T8CGSToNhEIQalwMjNy64BrAb6mDOcXgAWK/HIV8On1T eK3st/VoChGvUE3qlTL5tfrT5ePveGYZ8AEjiaZFkYfdLnG5WGBDJxxTIx6FBNMK4268 6pfVhNEZJG1tc/8l2dl0M/F+l3JjUiJZUEzyRuITdriiM21mX+mQ+Y3qYMxM/DYwBWAo BBQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date; bh=00zNPjSV0NBnL/fqot4cVHiAuUteLJu0Hqble2AqPlI=; b=Wrwoum/GQhH1AVMDPB2FOYgOuLSSSqOlQh8nhxmLZRVwovPae5WUBu9LsDpe3Uk0LW LO6TGj3RxZIeOCdy60S6sFSmsOVctgimkTiaiTwqiowb03ixENb3BbU+FsdHE8qtjc8Y hbyxL5zKjJrG3itnjoGzC2Ccq5dNOOgg7erLrgUkAoNQy01VSRexXQlW3XZP1Jqla5qg +oOWvxeXl2i6ciYStqZfGycdQqkWcosFww6YunlEdQG64csuEif8moikdP/eaYTQ1OOZ i2A80HF+Dw1MKQvq79RyYSfpewRBw6fY/OiGH1oQLNjlyBkEjFPldLjYmAA4mHqNkuMI /Dsw== X-Gm-Message-State: ACrzQf2BONKnkwxMqEv5jWQdEe4JIdnT7zvKo+LALd/9tYsrtY68xi4V p/zU07lo4cibD1a99Ic8M0c= X-Google-Smtp-Source: AMsMyM4ar/eKhA4YXpbfMC3LzHoOECJ4YMFrhXTEqAwQpgNZ3C3rSJftHJk85dr/oOKd4qPibTs4Zg== X-Received: by 2002:a19:9202:0:b0:49d:7310:742f with SMTP id u2-20020a199202000000b0049d7310742fmr115161lfd.312.1665067020638; Thu, 06 Oct 2022 07:37:00 -0700 (PDT) Received: from dc75zzyyyyyyyyyyyyyyt-3.rev.dnainternet.fi (dc75zzyyyyyyyyyyyyyyt-3.rev.dnainternet.fi. [2001:14ba:16f3:4a00::1]) by smtp.gmail.com with ESMTPSA id h4-20020a05651c124400b0026be1de1500sm1925140ljh.79.2022.10.06.07.36.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Oct 2022 07:36:59 -0700 (PDT) Date: Thu, 6 Oct 2022 17:36:52 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Matti Vaittinen , Andy Shevchenko , Jagath Jog J , Nikita Yushchenko , Cosmin Tanislav , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v2 1/5] regulator: Add devm helpers for get and enable Message-ID: References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: devicetree@vger.kernel.org A few regulator consumer drivers seem to be just getting a regulator, enabling it and registering a devm-action to disable the regulator at the driver detach and then forget about it. We can simplify this a bit by adding a devm-helper for this pattern. Add devm_regulator_get_enable() and devm_regulator_get_enable_optional() Signed-off-by: Matti Vaittinen (cherry picked from commit b6058e052b842a19c8bb639798d8692cd0e7589f) --- Already in Mark's regulator tree. Not to be merged. Included just for the sake of the completeness. Will be dropped when series is rebased on top of the 6.1-rc1 --- drivers/regulator/devres.c | 59 ++++++++++++++++++++++++++++++ include/linux/regulator/consumer.h | 13 +++++++ 2 files changed, 72 insertions(+) diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c index 32823a87fd40..3cb3eb49ad8a 100644 --- a/drivers/regulator/devres.c +++ b/drivers/regulator/devres.c @@ -70,6 +70,65 @@ struct regulator *devm_regulator_get_exclusive(struct device *dev, } EXPORT_SYMBOL_GPL(devm_regulator_get_exclusive); +static void regulator_action_disable(void *d) +{ + struct regulator *r = (struct regulator *)d; + + regulator_disable(r); +} + +static int _devm_regulator_get_enable(struct device *dev, const char *id, + int get_type) +{ + struct regulator *r; + int ret; + + r = _devm_regulator_get(dev, id, get_type); + if (IS_ERR(r)) + return PTR_ERR(r); + + ret = regulator_enable(r); + if (!ret) + ret = devm_add_action_or_reset(dev, ®ulator_action_disable, r); + + if (ret) + devm_regulator_put(r); + + return ret; +} + +/** + * devm_regulator_get_enable_optional - Resource managed regulator get and enable + * @dev: device to supply + * @id: supply name or regulator ID. + * + * Get and enable regulator for duration of the device life-time. + * regulator_disable() and regulator_put() are automatically called on driver + * detach. See regulator_get_optional() and regulator_enable() for more + * information. + */ +int devm_regulator_get_enable_optional(struct device *dev, const char *id) +{ + return _devm_regulator_get_enable(dev, id, OPTIONAL_GET); +} +EXPORT_SYMBOL_GPL(devm_regulator_get_enable_optional); + +/** + * devm_regulator_get_enable - Resource managed regulator get and enable + * @dev: device to supply + * @id: supply name or regulator ID. + * + * Get and enable regulator for duration of the device life-time. + * regulator_disable() and regulator_put() are automatically called on driver + * detach. See regulator_get() and regulator_enable() for more + * information. + */ +int devm_regulator_get_enable(struct device *dev, const char *id) +{ + return _devm_regulator_get_enable(dev, id, NORMAL_GET); +} +EXPORT_SYMBOL_GPL(devm_regulator_get_enable); + /** * devm_regulator_get_optional - Resource managed regulator_get_optional() * @dev: device to supply diff --git a/include/linux/regulator/consumer.h b/include/linux/regulator/consumer.h index bc6cda706d1f..8e6cf65851b0 100644 --- a/include/linux/regulator/consumer.h +++ b/include/linux/regulator/consumer.h @@ -207,6 +207,8 @@ struct regulator *__must_check regulator_get_optional(struct device *dev, const char *id); struct regulator *__must_check devm_regulator_get_optional(struct device *dev, const char *id); +int devm_regulator_get_enable(struct device *dev, const char *id); +int devm_regulator_get_enable_optional(struct device *dev, const char *id); void regulator_put(struct regulator *regulator); void devm_regulator_put(struct regulator *regulator); @@ -354,6 +356,17 @@ devm_regulator_get_exclusive(struct device *dev, const char *id) return ERR_PTR(-ENODEV); } +static inline int devm_regulator_get_enable(struct device *dev, const char *id) +{ + return -ENODEV; +} + +static inline int devm_regulator_get_enable_optional(struct device *dev, + const char *id) +{ + return -ENODEV; +} + static inline struct regulator *__must_check regulator_get_optional(struct device *dev, const char *id) {