1 From 80e0f8d94d3090f0f7bf3faf3e6180e920ee0d22 Mon Sep 17 00:00:00 2001
2 From: Laxman Dewangan <ldewangan@nvidia.com>
3 Date: Wed, 24 Feb 2016 14:12:59 +0530
4 Subject: [PATCH] pinctrl: Add devm_ apis for pinctrl_{register, unregister}
6 Add device managed APIs devm_pinctrl_register() and
7 devm_pinctrl_unregister() for the APIs pinctrl_register()
8 and pinctrl_unregister().
10 This helps in reducing code in error path and sometimes
11 removal of .remove callback for driver unbind.
13 Signed-off-by: Laxman Dewangan <ldewangan@nvidia.com>
14 Reviewed-by: Philipp Zabel <p.zabel@pengutronix.de>
15 Acked-by: Bjorn Andersson <bjorn.andersson@linaro.org>
16 Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
18 drivers/pinctrl/core.c | 63 +++++++++++++++++++++++++++++++++++++++++
19 include/linux/pinctrl/pinctrl.h | 6 ++++
20 2 files changed, 69 insertions(+)
22 --- a/drivers/pinctrl/core.c
23 +++ b/drivers/pinctrl/core.c
24 @@ -1861,6 +1861,69 @@ void pinctrl_unregister(struct pinctrl_d
26 EXPORT_SYMBOL_GPL(pinctrl_unregister);
28 +static void devm_pinctrl_dev_release(struct device *dev, void *res)
30 + struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
32 + pinctrl_unregister(pctldev);
35 +static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
37 + struct pctldev **r = res;
39 + if (WARN_ON(!r || !*r))
46 + * devm_pinctrl_register() - Resource managed version of pinctrl_register().
47 + * @dev: parent device for this pin controller
48 + * @pctldesc: descriptor for this pin controller
49 + * @driver_data: private pin controller data for this pin controller
51 + * Returns an error pointer if pincontrol register failed. Otherwise
52 + * it returns valid pinctrl handle.
54 + * The pinctrl device will be automatically released when the device is unbound.
56 +struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
57 + struct pinctrl_desc *pctldesc,
60 + struct pinctrl_dev **ptr, *pctldev;
62 + ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
64 + return ERR_PTR(-ENOMEM);
66 + pctldev = pinctrl_register(pctldesc, dev, driver_data);
67 + if (IS_ERR(pctldev)) {
73 + devres_add(dev, ptr);
77 +EXPORT_SYMBOL_GPL(devm_pinctrl_register);
80 + * devm_pinctrl_unregister() - Resource managed version of pinctrl_unregister().
81 + * @dev: device for which which resource was allocated
82 + * @pctldev: the pinctrl device to unregister.
84 +void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
86 + WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
87 + devm_pinctrl_dev_match, pctldev));
89 +EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
91 static int __init pinctrl_init(void)
93 pr_info("initialized pinctrl subsystem\n");
94 --- a/include/linux/pinctrl/pinctrl.h
95 +++ b/include/linux/pinctrl/pinctrl.h
96 @@ -144,6 +144,12 @@ struct pinctrl_desc {
97 extern struct pinctrl_dev *pinctrl_register(struct pinctrl_desc *pctldesc,
98 struct device *dev, void *driver_data);
99 extern void pinctrl_unregister(struct pinctrl_dev *pctldev);
100 +extern struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
101 + struct pinctrl_desc *pctldesc,
102 + void *driver_data);
103 +extern void devm_pinctrl_unregister(struct device *dev,
104 + struct pinctrl_dev *pctldev);
106 extern bool pin_is_valid(struct pinctrl_dev *pctldev, int pin);
107 extern void pinctrl_add_gpio_range(struct pinctrl_dev *pctldev,
108 struct pinctrl_gpio_range *range);