Extend the use of managed resource functions in regulator by replacing
all calls to unmanaged resource allocation functions in
gpio_regulator_probe() with their managed equivalent.
Add a new function, devm_kmemdup(), to drivers/base/devres.c to support this.
Rob Jones (3):
base: Add block copy func. for managed devices
gpio: allow gpio array requests for managed devices
regulator: use managed resources for gpio_regulator_probe().
drivers/base/devres.c | 25 +++++++++++++
drivers/gpio/devres.c | 63 ++++++++++++++++++++++++++++++++
drivers/regulator/gpio-regulator.c | 70 ++++++++++++------------------------
include/linux/device.h | 2 ++
include/linux/gpio.h | 4 +++
5 files changed, 117 insertions(+), 47 deletions(-)
--
1.7.10.4
Incorporates requested changes.
Note that this patch series no longer includes a change to devm_kstrdup()
as this was not essential to the primary aim of the series. This will be
submitted later once devm_kmemdup() has been accepted.
Add function devm_kmemdup() which is exactly analogous to the
non-managed device function kmemdup().
Reviewed-by: Ian Molton <[email protected]>
Signed-off-by: Rob Jones <[email protected]>
---
drivers/base/devres.c | 25 +++++++++++++++++++++++++
include/linux/device.h | 2 ++
2 files changed, 27 insertions(+)
diff --git a/drivers/base/devres.c b/drivers/base/devres.c
index db4e264..77a6ce7 100644
--- a/drivers/base/devres.c
+++ b/drivers/base/devres.c
@@ -817,6 +817,31 @@ char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp)
EXPORT_SYMBOL_GPL(devm_kstrdup);
/**
+ * devm_kmemdup - Allocate resource managed space and
+ * copy existing data into that.
+ * @dev: Device to allocate memory for
+ * @s: The memory block to duplicate
+ * @gfp: The GFP mask used in the devm_kmalloc() call when
+ * allocating memory
+ * RETURNS:
+ * Pointer to allocated string on success, NULL on failure.
+ */
+void *devm_kmemdup(struct device *dev, const void *s, size_t size, gfp_t gfp)
+{
+ void *buf;
+
+ if (!s || (size == 0))
+ return NULL;
+
+ buf = devm_kmalloc(dev, size, gfp);
+ if (buf)
+ memcpy(buf, s, size);
+
+ return buf;
+}
+EXPORT_SYMBOL_GPL(devm_kmemdup);
+
+/**
* devm_kfree - Resource-managed kfree
* @dev: Device this memory belongs to
* @p: Memory to free
diff --git a/include/linux/device.h b/include/linux/device.h
index d1d1c05..7ace116 100644
--- a/include/linux/device.h
+++ b/include/linux/device.h
@@ -623,6 +623,8 @@ static inline void *devm_kcalloc(struct device *dev,
}
extern void devm_kfree(struct device *dev, void *p);
extern char *devm_kstrdup(struct device *dev, const char *s, gfp_t gfp);
+extern void *devm_kmemdup(struct device *dev,
+ const void *s, size_t size, gfp_t gfp);
void __iomem *devm_ioremap_resource(struct device *dev, struct resource *res);
void __iomem *devm_request_and_ioremap(struct device *dev,
--
1.7.10.4
Use managed resource functions for all resource allocations in
gpio_regulator_probe().
Remove gpio_regulator_remove() as it is now redundant.
Reviewed-by: Ian Molton <[email protected]>
Signed-off-by: Rob Jones <[email protected]>
---
drivers/regulator/gpio-regulator.c | 70 ++++++++++++------------------------
1 file changed, 23 insertions(+), 47 deletions(-)
diff --git a/drivers/regulator/gpio-regulator.c b/drivers/regulator/gpio-regulator.c
index 989b23b..6edde12 100644
--- a/drivers/regulator/gpio-regulator.c
+++ b/drivers/regulator/gpio-regulator.c
@@ -254,30 +254,31 @@ static int gpio_regulator_probe(struct platform_device *pdev)
if (drvdata == NULL)
return -ENOMEM;
- drvdata->desc.name = kstrdup(config->supply_name, GFP_KERNEL);
+ drvdata->desc.name = devm_kstrdup(&pdev->dev,
+ config->supply_name,
+ GFP_KERNEL);
if (drvdata->desc.name == NULL) {
dev_err(&pdev->dev, "Failed to allocate supply name\n");
- ret = -ENOMEM;
- goto err;
+ return -ENOMEM;
}
- drvdata->gpios = kmemdup(config->gpios,
- config->nr_gpios * sizeof(struct gpio),
- GFP_KERNEL);
+ drvdata->gpios = devm_kmemdup(&pdev->dev,
+ config->gpios,
+ config->nr_gpios * sizeof(struct gpio),
+ GFP_KERNEL);
if (drvdata->gpios == NULL) {
dev_err(&pdev->dev, "Failed to allocate gpio data\n");
- ret = -ENOMEM;
- goto err_name;
+ return -ENOMEM;
}
- drvdata->states = kmemdup(config->states,
- config->nr_states *
+ drvdata->states = devm_kmemdup(&pdev->dev,
+ config->states,
+ config->nr_states *
sizeof(struct gpio_regulator_state),
- GFP_KERNEL);
+ GFP_KERNEL);
if (drvdata->states == NULL) {
dev_err(&pdev->dev, "Failed to allocate state data\n");
- ret = -ENOMEM;
- goto err_memgpio;
+ return -ENOMEM;
}
drvdata->nr_states = config->nr_states;
@@ -297,16 +298,17 @@ static int gpio_regulator_probe(struct platform_device *pdev)
break;
default:
dev_err(&pdev->dev, "No regulator type set\n");
- ret = -EINVAL;
- goto err_memgpio;
+ return -EINVAL;
}
drvdata->nr_gpios = config->nr_gpios;
- ret = gpio_request_array(drvdata->gpios, drvdata->nr_gpios);
+ ret = devm_gpio_request_array(&pdev->dev,
+ drvdata->gpios,
+ drvdata->nr_gpios);
if (ret) {
dev_err(&pdev->dev,
"Could not obtain regulator setting GPIOs: %d\n", ret);
- goto err_memstate;
+ return ret;
}
/* build initial state from gpio init data. */
@@ -337,43 +339,18 @@ static int gpio_regulator_probe(struct platform_device *pdev)
cfg.ena_gpio_flags |= GPIOF_OUT_INIT_HIGH;
}
- drvdata->dev = regulator_register(&drvdata->desc, &cfg);
+ drvdata->dev = devm_regulator_register(&pdev->dev,
+ &drvdata->desc,
+ &cfg);
if (IS_ERR(drvdata->dev)) {
ret = PTR_ERR(drvdata->dev);
dev_err(&pdev->dev, "Failed to register regulator: %d\n", ret);
- goto err_stategpio;
+ return ret;
}
platform_set_drvdata(pdev, drvdata);
return 0;
-
-err_stategpio:
- gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
-err_memstate:
- kfree(drvdata->states);
-err_memgpio:
- kfree(drvdata->gpios);
-err_name:
- kfree(drvdata->desc.name);
-err:
- return ret;
-}
-
-static int gpio_regulator_remove(struct platform_device *pdev)
-{
- struct gpio_regulator_data *drvdata = platform_get_drvdata(pdev);
-
- regulator_unregister(drvdata->dev);
-
- gpio_free_array(drvdata->gpios, drvdata->nr_gpios);
-
- kfree(drvdata->states);
- kfree(drvdata->gpios);
-
- kfree(drvdata->desc.name);
-
- return 0;
}
#if defined(CONFIG_OF)
@@ -385,7 +362,6 @@ static const struct of_device_id regulator_gpio_of_match[] = {
static struct platform_driver gpio_regulator_driver = {
.probe = gpio_regulator_probe,
- .remove = gpio_regulator_remove,
.driver = {
.name = "gpio-regulator",
.owner = THIS_MODULE,
--
1.7.10.4
Add functions devm_gpio_request_array() and devm_gpio_free_array()
which are exactly analogous to the equivalent non-managed device
functions gpio_request_array() and gpio_free_array(), which can be
found in the module gpiolib.c.
Reviewed-by: Ian Molton <[email protected]>
Signed-off-by: Rob Jones <[email protected]>
---
drivers/gpio/devres.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++
include/linux/gpio.h | 4 ++++
2 files changed, 67 insertions(+)
diff --git a/drivers/gpio/devres.c b/drivers/gpio/devres.c
index 307464f..b97c34b 100644
--- a/drivers/gpio/devres.c
+++ b/drivers/gpio/devres.c
@@ -186,6 +186,69 @@ int devm_gpio_request_one(struct device *dev, unsigned gpio,
EXPORT_SYMBOL(devm_gpio_request_one);
/**
+ * devm_gpio_request_array - request multiple GPIOs in a single call
+ * for a managed device
+ * @dev: device requesting the gpio
+ * @array: array of the 'struct gpio'
+ * @num: how many GPIOs in the array
+ *
+ * Except for the extra @dev argument, this function takes the
+ * same arguments and performs the same function as
+ * gpio_request(). GPIOs requested with this function will be
+ * automatically freed on driver detach.
+ *
+ * Note that if any gpio request returns an error, any gpios previously
+ * obtained are immediately freed and the error code is returned,
+ * meaning that the whole call failed and no gpios are obtained, no
+ * indication is provided as to which particular request failed.
+ *
+ * If GPIOs allocated with this function need to be freed separately,
+ * devm_gpio_free_array() or devm_gpio_free() must be used.
+ */
+int devm_gpio_request_array(struct device *dev,
+ const struct gpio *array,
+ size_t num)
+{
+ int i, err = 0;
+
+ for (i = 0; i < num; i++, array++) {
+ err = devm_gpio_request_one(dev,
+ array->gpio,
+ array->flags,
+ array->label);
+ if (err) {
+ while (i--)
+ devm_gpio_free(dev, (--array)->gpio);
+ break;
+ }
+ }
+
+ return err;
+}
+EXPORT_SYMBOL(devm_gpio_request_array);
+
+/**
+ * devm_gpio_free_array - release multiple GPIOs in a single call
+ * for a managed device
+ * @dev: device requesting the gpio
+ * @array: array of the 'struct gpio'
+ * @num: how many GPIOs in the array
+ *
+ * Except for the extra @dev argument, this function takes the
+ * same arguments and performs the same function as gpio_free_array().
+ * This function instead of gpio_free_array() should be used to
+ * manually free GPIOs allocated with devm_gpio_request().
+ */
+void devm_gpio_free_array(struct device *dev,
+ const struct gpio *array,
+ size_t num)
+{
+ while (num--)
+ devm_gpio_free(dev, (array++)->gpio);
+}
+EXPORT_SYMBOL(devm_gpio_free_array);
+
+/**
* devm_gpio_free - free a GPIO
* @dev: device to free GPIO for
* @gpio: GPIO to free
diff --git a/include/linux/gpio.h b/include/linux/gpio.h
index 85aa5d0..12d5648 100644
--- a/include/linux/gpio.h
+++ b/include/linux/gpio.h
@@ -84,6 +84,10 @@ struct device;
int devm_gpio_request(struct device *dev, unsigned gpio, const char *label);
int devm_gpio_request_one(struct device *dev, unsigned gpio,
unsigned long flags, const char *label);
+int devm_gpio_request_array(struct device *dev, const struct gpio *array,
+ size_t num);
+void devm_gpio_free_array(struct device *dev, const struct gpio *array,
+ size_t num);
void devm_gpio_free(struct device *dev, unsigned int gpio);
#else /* ! CONFIG_GPIOLIB */
--
1.7.10.4
On Wed, Jul 2, 2014 at 12:05 PM, Rob Jones <[email protected]> wrote:
> Add functions devm_gpio_request_array() and devm_gpio_free_array()
> which are exactly analogous to the equivalent non-managed device
> functions gpio_request_array() and gpio_free_array(), which can be
> found in the module gpiolib.c.
>
> Reviewed-by: Ian Molton <[email protected]>
> Signed-off-by: Rob Jones <[email protected]>
OK I guess this needs to be merged through the regulator tree
so here is:
Acked-by: Linus Walleij <[email protected]>
Yours,
Linus Walleij