Merge tag 'imx-soc-3.18' of git://git.kernel.org/pub/scm/linux/kernel/git/shawnguo...
[cascardo/linux.git] / drivers / reset / core.c
1 /*
2  * Reset Controller framework
3  *
4  * Copyright 2013 Philipp Zabel, Pengutronix
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  */
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of.h>
17 #include <linux/reset.h>
18 #include <linux/reset-controller.h>
19 #include <linux/slab.h>
20
21 static DEFINE_MUTEX(reset_controller_list_mutex);
22 static LIST_HEAD(reset_controller_list);
23
24 /**
25  * struct reset_control - a reset control
26  * @rcdev: a pointer to the reset controller device
27  *         this reset control belongs to
28  * @id: ID of the reset controller in the reset
29  *      controller device
30  */
31 struct reset_control {
32         struct reset_controller_dev *rcdev;
33         struct device *dev;
34         unsigned int id;
35 };
36
37 /**
38  * of_reset_simple_xlate - translate reset_spec to the reset line number
39  * @rcdev: a pointer to the reset controller device
40  * @reset_spec: reset line specifier as found in the device tree
41  * @flags: a flags pointer to fill in (optional)
42  *
43  * This simple translation function should be used for reset controllers
44  * with 1:1 mapping, where reset lines can be indexed by number without gaps.
45  */
46 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
47                           const struct of_phandle_args *reset_spec)
48 {
49         if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
50                 return -EINVAL;
51
52         if (reset_spec->args[0] >= rcdev->nr_resets)
53                 return -EINVAL;
54
55         return reset_spec->args[0];
56 }
57
58 /**
59  * reset_controller_register - register a reset controller device
60  * @rcdev: a pointer to the initialized reset controller device
61  */
62 int reset_controller_register(struct reset_controller_dev *rcdev)
63 {
64         if (!rcdev->of_xlate) {
65                 rcdev->of_reset_n_cells = 1;
66                 rcdev->of_xlate = of_reset_simple_xlate;
67         }
68
69         mutex_lock(&reset_controller_list_mutex);
70         list_add(&rcdev->list, &reset_controller_list);
71         mutex_unlock(&reset_controller_list_mutex);
72
73         return 0;
74 }
75 EXPORT_SYMBOL_GPL(reset_controller_register);
76
77 /**
78  * reset_controller_unregister - unregister a reset controller device
79  * @rcdev: a pointer to the reset controller device
80  */
81 void reset_controller_unregister(struct reset_controller_dev *rcdev)
82 {
83         mutex_lock(&reset_controller_list_mutex);
84         list_del(&rcdev->list);
85         mutex_unlock(&reset_controller_list_mutex);
86 }
87 EXPORT_SYMBOL_GPL(reset_controller_unregister);
88
89 /**
90  * reset_control_reset - reset the controlled device
91  * @rstc: reset controller
92  */
93 int reset_control_reset(struct reset_control *rstc)
94 {
95         if (rstc->rcdev->ops->reset)
96                 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
97
98         return -ENOSYS;
99 }
100 EXPORT_SYMBOL_GPL(reset_control_reset);
101
102 /**
103  * reset_control_assert - asserts the reset line
104  * @rstc: reset controller
105  */
106 int reset_control_assert(struct reset_control *rstc)
107 {
108         if (rstc->rcdev->ops->assert)
109                 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
110
111         return -ENOSYS;
112 }
113 EXPORT_SYMBOL_GPL(reset_control_assert);
114
115 /**
116  * reset_control_deassert - deasserts the reset line
117  * @rstc: reset controller
118  */
119 int reset_control_deassert(struct reset_control *rstc)
120 {
121         if (rstc->rcdev->ops->deassert)
122                 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
123
124         return -ENOSYS;
125 }
126 EXPORT_SYMBOL_GPL(reset_control_deassert);
127
128 /**
129  * of_reset_control_get - Lookup and obtain a reference to a reset controller.
130  * @node: device to be reset by the controller
131  * @id: reset line name
132  *
133  * Returns a struct reset_control or IS_ERR() condition containing errno.
134  *
135  * Use of id names is optional.
136  */
137 struct reset_control *of_reset_control_get(struct device_node *node,
138                                            const char *id)
139 {
140         struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
141         struct reset_controller_dev *r, *rcdev;
142         struct of_phandle_args args;
143         int index = 0;
144         int rstc_id;
145         int ret;
146
147         if (id)
148                 index = of_property_match_string(node,
149                                                  "reset-names", id);
150         ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
151                                          index, &args);
152         if (ret)
153                 return ERR_PTR(ret);
154
155         mutex_lock(&reset_controller_list_mutex);
156         rcdev = NULL;
157         list_for_each_entry(r, &reset_controller_list, list) {
158                 if (args.np == r->of_node) {
159                         rcdev = r;
160                         break;
161                 }
162         }
163         of_node_put(args.np);
164
165         if (!rcdev) {
166                 mutex_unlock(&reset_controller_list_mutex);
167                 return ERR_PTR(-EPROBE_DEFER);
168         }
169
170         rstc_id = rcdev->of_xlate(rcdev, &args);
171         if (rstc_id < 0) {
172                 mutex_unlock(&reset_controller_list_mutex);
173                 return ERR_PTR(rstc_id);
174         }
175
176         try_module_get(rcdev->owner);
177         mutex_unlock(&reset_controller_list_mutex);
178
179         rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
180         if (!rstc) {
181                 module_put(rcdev->owner);
182                 return ERR_PTR(-ENOMEM);
183         }
184
185         rstc->rcdev = rcdev;
186         rstc->id = rstc_id;
187
188         return rstc;
189 }
190 EXPORT_SYMBOL_GPL(of_reset_control_get);
191
192 /**
193  * reset_control_get - Lookup and obtain a reference to a reset controller.
194  * @dev: device to be reset by the controller
195  * @id: reset line name
196  *
197  * Returns a struct reset_control or IS_ERR() condition containing errno.
198  *
199  * Use of id names is optional.
200  */
201 struct reset_control *reset_control_get(struct device *dev, const char *id)
202 {
203         struct reset_control *rstc;
204
205         if (!dev)
206                 return ERR_PTR(-EINVAL);
207
208         rstc = of_reset_control_get(dev->of_node, id);
209         if (!IS_ERR(rstc))
210                 rstc->dev = dev;
211
212         return rstc;
213 }
214 EXPORT_SYMBOL_GPL(reset_control_get);
215
216 /**
217  * reset_control_put - free the reset controller
218  * @rstc: reset controller
219  */
220
221 void reset_control_put(struct reset_control *rstc)
222 {
223         if (IS_ERR(rstc))
224                 return;
225
226         module_put(rstc->rcdev->owner);
227         kfree(rstc);
228 }
229 EXPORT_SYMBOL_GPL(reset_control_put);
230
231 static void devm_reset_control_release(struct device *dev, void *res)
232 {
233         reset_control_put(*(struct reset_control **)res);
234 }
235
236 /**
237  * devm_reset_control_get - resource managed reset_control_get()
238  * @dev: device to be reset by the controller
239  * @id: reset line name
240  *
241  * Managed reset_control_get(). For reset controllers returned from this
242  * function, reset_control_put() is called automatically on driver detach.
243  * See reset_control_get() for more information.
244  */
245 struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
246 {
247         struct reset_control **ptr, *rstc;
248
249         ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
250                            GFP_KERNEL);
251         if (!ptr)
252                 return ERR_PTR(-ENOMEM);
253
254         rstc = reset_control_get(dev, id);
255         if (!IS_ERR(rstc)) {
256                 *ptr = rstc;
257                 devres_add(dev, ptr);
258         } else {
259                 devres_free(ptr);
260         }
261
262         return rstc;
263 }
264 EXPORT_SYMBOL_GPL(devm_reset_control_get);
265
266 /**
267  * device_reset - find reset controller associated with the device
268  *                and perform reset
269  * @dev: device to be reset by the controller
270  *
271  * Convenience wrapper for reset_control_get() and reset_control_reset().
272  * This is useful for the common case of devices with single, dedicated reset
273  * lines.
274  */
275 int device_reset(struct device *dev)
276 {
277         struct reset_control *rstc;
278         int ret;
279
280         rstc = reset_control_get(dev, NULL);
281         if (IS_ERR(rstc))
282                 return PTR_ERR(rstc);
283
284         ret = reset_control_reset(rstc);
285
286         reset_control_put(rstc);
287
288         return ret;
289 }
290 EXPORT_SYMBOL_GPL(device_reset);