[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <CAMknhBG2ySYNEvghnO63wE=x6W8jk0-T2oWZKeXApM_d_eeoCw@mail.gmail.com>
Date: Tue, 30 Apr 2024 11:19:01 -0500
From: David Lechner <dlechner@...libre.com>
To: Liam Girdwood <lgirdwood@...il.com>, Mark Brown <broonie@...nel.org>,
Jean Delvare <jdelvare@...e.com>, Guenter Roeck <linux@...ck-us.net>,
Jonathan Cameron <jic23@...nel.org>, Dmitry Torokhov <dmitry.torokhov@...il.com>
Cc: Jonathan Corbet <corbet@....net>, Support Opensource <support.opensource@...semi.com>,
Cosmin Tanislav <cosmin.tanislav@...log.com>, Lars-Peter Clausen <lars@...afoo.de>,
Michael Hennerich <Michael.Hennerich@...log.com>, Antoniu Miclaus <antoniu.miclaus@...log.com>,
Greg Kroah-Hartman <gregkh@...uxfoundation.org>, linux-doc@...r.kernel.org,
linux-kernel@...r.kernel.org, linux-hwmon@...r.kernel.org,
linux-iio@...r.kernel.org, linux-staging@...ts.linux.dev,
linux-input@...r.kernel.org
Subject: Re: [PATCH v2 1/7] regulator: devres: add API for reference voltage supplies
On Mon, Apr 29, 2024 at 6:40 PM David Lechner <dlechner@...libre.com> wrote:
>
> A common use case for regulators is to supply a reference voltage to an
> analog input or output device. This adds a new devres API to get,
> enable, and get the voltage in a single call. This allows eliminating
> boilerplate code in drivers that use reference supplies in this way.
>
> Signed-off-by: David Lechner <dlechner@...libre.com>
> ---
>
> v2 changes:
> * removed dev_err_probe() on error return
> * dropped optional version of the function since there is no more
> difference after dev_err_probe() is removed
> * renamed function to devm_regulator_get_enable_read_voltage()
> * added unwinding on error paths
> ---
> Documentation/driver-api/driver-model/devres.rst | 1 +
> drivers/regulator/devres.c | 59 ++++++++++++++++++++++++
> include/linux/regulator/consumer.h | 7 +++
> 3 files changed, 67 insertions(+)
>
> diff --git a/Documentation/driver-api/driver-model/devres.rst b/Documentation/driver-api/driver-model/devres.rst
> index 7be8b8dd5f00..18caebad7376 100644
> --- a/Documentation/driver-api/driver-model/devres.rst
> +++ b/Documentation/driver-api/driver-model/devres.rst
> @@ -433,6 +433,7 @@ REGULATOR
> devm_regulator_bulk_put()
> devm_regulator_get()
> devm_regulator_get_enable()
> + devm_regulator_get_enable_read_voltage()
> devm_regulator_get_enable_optional()
> devm_regulator_get_exclusive()
> devm_regulator_get_optional()
> diff --git a/drivers/regulator/devres.c b/drivers/regulator/devres.c
> index 90bb0d178885..4f290b9b559b 100644
> --- a/drivers/regulator/devres.c
> +++ b/drivers/regulator/devres.c
> @@ -145,6 +145,65 @@ struct regulator *devm_regulator_get_optional(struct device *dev,
> }
> EXPORT_SYMBOL_GPL(devm_regulator_get_optional);
>
> +/**
> + * devm_regulator_get_enable_read_voltage - Resource managed regulator get and
> + * enable that returns the voltage
> + * @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(), regulator_enable(), and
> + * regulator_get_voltage() for more information.
> + *
> + * This is a convenience function for supplies that provide a reference voltage
> + * where the consumer driver just needs to know the voltage and keep the
> + * regulator enabled.
> + *
> + * In cases where the supply is not strictly required, callers can check for
> + * -ENODEV error and handle it accordingly.
> + *
> + * Returns: voltage in microvolts on success, or an error code on failure.
> + */
> +int devm_regulator_get_enable_read_voltage(struct device *dev, const char *id)
> +{
> + struct regulator *r;
> + int ret;
> +
> + /*
> + * Since we need a real voltage, we use devm_regulator_get_optional()
> + * rather than getting a dummy regulator with devm_regulator_get() and
> + * then letting regulator_get_voltage() fail with -EINVAL. This way, the
> + * caller can handle the -ENODEV error code if needed instead of the
> + * ambiguous -EINVAL.
> + */
> + r = devm_regulator_get_optional(dev, id);
> + if (IS_ERR(r))
> + return PTR_ERR(r);
> +
> + ret = regulator_enable(r);
> + if (ret)
> + goto err_regulator_put;
> +
> + ret = devm_add_action_or_reset(dev, regulator_action_disable, r);
> + if (ret)
> + goto err_regulator_put;
> +
> + ret = regulator_get_voltage(r);
> + if (ret < 0)
> + goto err_release_action;
> +
> + return 0;
Oof. Apparently I didn't test this as well as I should have after
adding the unwinding. This obviously should return ret, not 0.
I did more extensive testing today, including faking error returns to
test unwinding to make sure I didn't miss anything else.
> +
> +err_release_action:
> + devm_release_action(dev, regulator_action_disable, r);
> +err_regulator_put:
> + devm_regulator_put(r);
> +
> + return ret;
> +}
> +EXPORT_SYMBOL_GPL(devm_regulator_get_enable_read_voltage);
> +
Powered by blists - more mailing lists