sparc: Convert naked unsigned uses to unsigned int
[linux-2.6-block.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         unsigned int id;
34 };
35
36 /**
37  * of_reset_simple_xlate - translate reset_spec to the reset line number
38  * @rcdev: a pointer to the reset controller device
39  * @reset_spec: reset line specifier as found in the device tree
40  * @flags: a flags pointer to fill in (optional)
41  *
42  * This simple translation function should be used for reset controllers
43  * with 1:1 mapping, where reset lines can be indexed by number without gaps.
44  */
45 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
46                           const struct of_phandle_args *reset_spec)
47 {
48         if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
49                 return -EINVAL;
50
51         if (reset_spec->args[0] >= rcdev->nr_resets)
52                 return -EINVAL;
53
54         return reset_spec->args[0];
55 }
56
57 /**
58  * reset_controller_register - register a reset controller device
59  * @rcdev: a pointer to the initialized reset controller device
60  */
61 int reset_controller_register(struct reset_controller_dev *rcdev)
62 {
63         if (!rcdev->of_xlate) {
64                 rcdev->of_reset_n_cells = 1;
65                 rcdev->of_xlate = of_reset_simple_xlate;
66         }
67
68         mutex_lock(&reset_controller_list_mutex);
69         list_add(&rcdev->list, &reset_controller_list);
70         mutex_unlock(&reset_controller_list_mutex);
71
72         return 0;
73 }
74 EXPORT_SYMBOL_GPL(reset_controller_register);
75
76 /**
77  * reset_controller_unregister - unregister a reset controller device
78  * @rcdev: a pointer to the reset controller device
79  */
80 void reset_controller_unregister(struct reset_controller_dev *rcdev)
81 {
82         mutex_lock(&reset_controller_list_mutex);
83         list_del(&rcdev->list);
84         mutex_unlock(&reset_controller_list_mutex);
85 }
86 EXPORT_SYMBOL_GPL(reset_controller_unregister);
87
88 /**
89  * reset_control_reset - reset the controlled device
90  * @rstc: reset controller
91  */
92 int reset_control_reset(struct reset_control *rstc)
93 {
94         if (rstc->rcdev->ops->reset)
95                 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
96
97         return -ENOTSUPP;
98 }
99 EXPORT_SYMBOL_GPL(reset_control_reset);
100
101 /**
102  * reset_control_assert - asserts the reset line
103  * @rstc: reset controller
104  */
105 int reset_control_assert(struct reset_control *rstc)
106 {
107         if (rstc->rcdev->ops->assert)
108                 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
109
110         return -ENOTSUPP;
111 }
112 EXPORT_SYMBOL_GPL(reset_control_assert);
113
114 /**
115  * reset_control_deassert - deasserts the reset line
116  * @rstc: reset controller
117  */
118 int reset_control_deassert(struct reset_control *rstc)
119 {
120         if (rstc->rcdev->ops->deassert)
121                 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
122
123         return -ENOTSUPP;
124 }
125 EXPORT_SYMBOL_GPL(reset_control_deassert);
126
127 /**
128  * reset_control_status - returns a negative errno if not supported, a
129  * positive value if the reset line is asserted, or zero if the reset
130  * line is not asserted.
131  * @rstc: reset controller
132  */
133 int reset_control_status(struct reset_control *rstc)
134 {
135         if (rstc->rcdev->ops->status)
136                 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
137
138         return -ENOTSUPP;
139 }
140 EXPORT_SYMBOL_GPL(reset_control_status);
141
142 /**
143  * of_reset_control_get_by_index - Lookup and obtain a reference to a reset
144  * controller by index.
145  * @node: device to be reset by the controller
146  * @index: index of the reset controller
147  *
148  * This is to be used to perform a list of resets for a device or power domain
149  * in whatever order. Returns a struct reset_control or IS_ERR() condition
150  * containing errno.
151  */
152 struct reset_control *of_reset_control_get_by_index(struct device_node *node,
153                                            int index)
154 {
155         struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
156         struct reset_controller_dev *r, *rcdev;
157         struct of_phandle_args args;
158         int rstc_id;
159         int ret;
160
161         ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
162                                          index, &args);
163         if (ret)
164                 return ERR_PTR(ret);
165
166         mutex_lock(&reset_controller_list_mutex);
167         rcdev = NULL;
168         list_for_each_entry(r, &reset_controller_list, list) {
169                 if (args.np == r->of_node) {
170                         rcdev = r;
171                         break;
172                 }
173         }
174         of_node_put(args.np);
175
176         if (!rcdev) {
177                 mutex_unlock(&reset_controller_list_mutex);
178                 return ERR_PTR(-EPROBE_DEFER);
179         }
180
181         rstc_id = rcdev->of_xlate(rcdev, &args);
182         if (rstc_id < 0) {
183                 mutex_unlock(&reset_controller_list_mutex);
184                 return ERR_PTR(rstc_id);
185         }
186
187         try_module_get(rcdev->owner);
188         mutex_unlock(&reset_controller_list_mutex);
189
190         rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
191         if (!rstc) {
192                 module_put(rcdev->owner);
193                 return ERR_PTR(-ENOMEM);
194         }
195
196         rstc->rcdev = rcdev;
197         rstc->id = rstc_id;
198
199         return rstc;
200 }
201 EXPORT_SYMBOL_GPL(of_reset_control_get_by_index);
202
203 /**
204  * of_reset_control_get - Lookup and obtain a reference to a reset controller.
205  * @node: device to be reset by the controller
206  * @id: reset line name
207  *
208  * Returns a struct reset_control or IS_ERR() condition containing errno.
209  *
210  * Use of id names is optional.
211  */
212 struct reset_control *of_reset_control_get(struct device_node *node,
213                                            const char *id)
214 {
215         int index = 0;
216
217         if (id) {
218                 index = of_property_match_string(node,
219                                                  "reset-names", id);
220                 if (index < 0)
221                         return ERR_PTR(-ENOENT);
222         }
223         return of_reset_control_get_by_index(node, index);
224 }
225 EXPORT_SYMBOL_GPL(of_reset_control_get);
226
227 /**
228  * reset_control_get - Lookup and obtain a reference to a reset controller.
229  * @dev: device to be reset by the controller
230  * @id: reset line name
231  *
232  * Returns a struct reset_control or IS_ERR() condition containing errno.
233  *
234  * Use of id names is optional.
235  */
236 struct reset_control *reset_control_get(struct device *dev, const char *id)
237 {
238         if (!dev)
239                 return ERR_PTR(-EINVAL);
240
241         return of_reset_control_get(dev->of_node, id);
242 }
243 EXPORT_SYMBOL_GPL(reset_control_get);
244
245 /**
246  * reset_control_put - free the reset controller
247  * @rstc: reset controller
248  */
249
250 void reset_control_put(struct reset_control *rstc)
251 {
252         if (IS_ERR(rstc))
253                 return;
254
255         module_put(rstc->rcdev->owner);
256         kfree(rstc);
257 }
258 EXPORT_SYMBOL_GPL(reset_control_put);
259
260 static void devm_reset_control_release(struct device *dev, void *res)
261 {
262         reset_control_put(*(struct reset_control **)res);
263 }
264
265 /**
266  * devm_reset_control_get - resource managed reset_control_get()
267  * @dev: device to be reset by the controller
268  * @id: reset line name
269  *
270  * Managed reset_control_get(). For reset controllers returned from this
271  * function, reset_control_put() is called automatically on driver detach.
272  * See reset_control_get() for more information.
273  */
274 struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
275 {
276         struct reset_control **ptr, *rstc;
277
278         ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
279                            GFP_KERNEL);
280         if (!ptr)
281                 return ERR_PTR(-ENOMEM);
282
283         rstc = reset_control_get(dev, id);
284         if (!IS_ERR(rstc)) {
285                 *ptr = rstc;
286                 devres_add(dev, ptr);
287         } else {
288                 devres_free(ptr);
289         }
290
291         return rstc;
292 }
293 EXPORT_SYMBOL_GPL(devm_reset_control_get);
294
295 /**
296  * device_reset - find reset controller associated with the device
297  *                and perform reset
298  * @dev: device to be reset by the controller
299  *
300  * Convenience wrapper for reset_control_get() and reset_control_reset().
301  * This is useful for the common case of devices with single, dedicated reset
302  * lines.
303  */
304 int device_reset(struct device *dev)
305 {
306         struct reset_control *rstc;
307         int ret;
308
309         rstc = reset_control_get(dev, NULL);
310         if (IS_ERR(rstc))
311                 return PTR_ERR(rstc);
312
313         ret = reset_control_reset(rstc);
314
315         reset_control_put(rstc);
316
317         return ret;
318 }
319 EXPORT_SYMBOL_GPL(device_reset);