From patchwork Wed Sep 21 11:42:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matti Vaittinen X-Patchwork-Id: 12983623 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 53A40C6FA8E for ; Wed, 21 Sep 2022 11:42:43 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229785AbiIULmm (ORCPT ); Wed, 21 Sep 2022 07:42:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53078 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229437AbiIULml (ORCPT ); Wed, 21 Sep 2022 07:42:41 -0400 Received: from mail-lj1-x232.google.com (mail-lj1-x232.google.com [IPv6:2a00:1450:4864:20::232]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D44CB8E98B; Wed, 21 Sep 2022 04:42:39 -0700 (PDT) Received: by mail-lj1-x232.google.com with SMTP id b6so6616654ljr.10; Wed, 21 Sep 2022 04:42:39 -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=C/v5U/CH8UoPmKrRTrmN93JfcftB0DZK+X0SeEjsOJU=; b=FjDC6VC8Vfr90AsitmdUCUriNQqGtcpcGGsZoZGswt0qn0vL0jsTE8qbiZ6+AAZfzt Ig0fb3QcylFmXP4i5zhQrzwQmldDocnymgCwpMbSMLRZd0rNUBpOjDya4ihrYS6AHhIl f5o2YJ6lvnGFDDPDVLZq93/Fpo4boQ0TgSafx+4p+4LFpz136bTixndvOx1CdA3HLYt8 DYoYmWuu+yHa2Ok38oFwEA+LWKWJfbv2sJuLudWqK6EyezFjMcu2NlR3fBUW578wIoB/ Cb6vNYC8otbyKT3aduEYSFx2xVqXqBY0n0oupDNriteXdVSM8CDXFJR+p8FdqVyYXois 2GQQ== 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=C/v5U/CH8UoPmKrRTrmN93JfcftB0DZK+X0SeEjsOJU=; b=rNuN9cGYH9U7k8K3cSTmVq1huUcJi+kSrvkuMCtAphgWbHZGumj3TPuD7nYbeljfQx mzn87vCDNP97m9Lj9+qT1sHrJUfOVVkGlWSWZuykrYN2KTe9CYuktlaRNFTQeKf3lc0b peq++7nn9Myydhjg+yKPzhcq0P7WW4TqWvrsLJXnVkbZKm5srSHjSAMZ1+PxCbiS7ah/ 00rCkjg6g/xtmv95stoprvzD5hhvGEF2Gci+UK9KOeStAqUvWzWaKACLyJhFDIrrIjHJ 4zp2mj9zIY93kP3I7uwCb2l8XELAMRZWnIKEmjx6W8fosXK9qKBrmQ1o5E4/gV+HryoX b9uw== X-Gm-Message-State: ACrzQf3H/d2eXaKjfVDNBWUHJOH7Pc40VyyT+4PE23B9T4iCxAJXqTXR 9qMmlLERXc383T8ZA3TTTnI= X-Google-Smtp-Source: AMsMyM4duOwh8VtqRs8Ei1L3d8dgW9TrrSDI7hgpG5kVPEO9AfhhZs6bmPrRry9pFQMa4jai5zxneg== X-Received: by 2002:a2e:924e:0:b0:26c:5dbc:2d80 with SMTP id v14-20020a2e924e000000b0026c5dbc2d80mr2498293ljg.290.1663760558152; Wed, 21 Sep 2022 04:42:38 -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 q14-20020ac2528e000000b0049480c69d98sm386790lfm.239.2022.09.21.04.42.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 21 Sep 2022 04:42:37 -0700 (PDT) Date: Wed, 21 Sep 2022 14:42:29 +0300 From: Matti Vaittinen To: Matti Vaittinen , Matti Vaittinen Cc: Jonathan Cameron , Lars-Peter Clausen , Rob Herring , Krzysztof Kozlowski , Matti Vaittinen , Andy Shevchenko , Nikita Yushchenko , Cosmin Tanislav , Jagath Jog J , linux-iio@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 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: linux-iio@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) --- Please note - this patch is already in Mark's tree. It's here just to allow compilation of the series on top of v6.0-rc4 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) {