205b6d700769298c55b1b701b73902c51fd918ad
[librecmc/librecmc.git] /
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}
5
6 Add device managed APIs devm_pinctrl_register() and
7 devm_pinctrl_unregister() for the APIs pinctrl_register()
8 and pinctrl_unregister().
9
10 This helps in reducing code in error path and sometimes
11 removal of .remove callback for driver unbind.
12
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>
17 ---
18  drivers/pinctrl/core.c          | 63 +++++++++++++++++++++++++++++++++++++++++
19  include/linux/pinctrl/pinctrl.h |  6 ++++
20  2 files changed, 69 insertions(+)
21
22 --- a/drivers/pinctrl/core.c
23 +++ b/drivers/pinctrl/core.c
24 @@ -1861,6 +1861,69 @@ void pinctrl_unregister(struct pinctrl_d
25  }
26  EXPORT_SYMBOL_GPL(pinctrl_unregister);
27  
28 +static void devm_pinctrl_dev_release(struct device *dev, void *res)
29 +{
30 +       struct pinctrl_dev *pctldev = *(struct pinctrl_dev **)res;
31 +
32 +       pinctrl_unregister(pctldev);
33 +}
34 +
35 +static int devm_pinctrl_dev_match(struct device *dev, void *res, void *data)
36 +{
37 +       struct pctldev **r = res;
38 +
39 +       if (WARN_ON(!r || !*r))
40 +               return 0;
41 +
42 +       return *r == data;
43 +}
44 +
45 +/**
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
50 + *
51 + * Returns an error pointer if pincontrol register failed. Otherwise
52 + * it returns valid pinctrl handle.
53 + *
54 + * The pinctrl device will be automatically released when the device is unbound.
55 + */
56 +struct pinctrl_dev *devm_pinctrl_register(struct device *dev,
57 +                                         struct pinctrl_desc *pctldesc,
58 +                                         void *driver_data)
59 +{
60 +       struct pinctrl_dev **ptr, *pctldev;
61 +
62 +       ptr = devres_alloc(devm_pinctrl_dev_release, sizeof(*ptr), GFP_KERNEL);
63 +       if (!ptr)
64 +               return ERR_PTR(-ENOMEM);
65 +
66 +       pctldev = pinctrl_register(pctldesc, dev, driver_data);
67 +       if (IS_ERR(pctldev)) {
68 +               devres_free(ptr);
69 +               return pctldev;
70 +       }
71 +
72 +       *ptr = pctldev;
73 +       devres_add(dev, ptr);
74 +
75 +       return pctldev;
76 +}
77 +EXPORT_SYMBOL_GPL(devm_pinctrl_register);
78 +
79 +/**
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.
83 + */
84 +void devm_pinctrl_unregister(struct device *dev, struct pinctrl_dev *pctldev)
85 +{
86 +       WARN_ON(devres_release(dev, devm_pinctrl_dev_release,
87 +                              devm_pinctrl_dev_match, pctldev));
88 +}
89 +EXPORT_SYMBOL_GPL(devm_pinctrl_unregister);
90 +
91  static int __init pinctrl_init(void)
92  {
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);
105 +
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);