GNU Linux-libre 4.9.317-gnu1
[releases.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/atomic.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/of.h>
18 #include <linux/reset.h>
19 #include <linux/reset-controller.h>
20 #include <linux/slab.h>
21
22 static DEFINE_MUTEX(reset_list_mutex);
23 static LIST_HEAD(reset_controller_list);
24
25 /**
26  * struct reset_control - a reset control
27  * @rcdev: a pointer to the reset controller device
28  *         this reset control belongs to
29  * @list: list entry for the rcdev's reset controller list
30  * @id: ID of the reset controller in the reset
31  *      controller device
32  * @refcnt: Number of gets of this reset_control
33  * @shared: Is this a shared (1), or an exclusive (0) reset_control?
34  * @deassert_cnt: Number of times this reset line has been deasserted
35  */
36 struct reset_control {
37         struct reset_controller_dev *rcdev;
38         struct list_head list;
39         unsigned int id;
40         unsigned int refcnt;
41         int shared;
42         atomic_t deassert_count;
43 };
44
45 /**
46  * of_reset_simple_xlate - translate reset_spec to the reset line number
47  * @rcdev: a pointer to the reset controller device
48  * @reset_spec: reset line specifier as found in the device tree
49  * @flags: a flags pointer to fill in (optional)
50  *
51  * This simple translation function should be used for reset controllers
52  * with 1:1 mapping, where reset lines can be indexed by number without gaps.
53  */
54 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
55                           const struct of_phandle_args *reset_spec)
56 {
57         if (reset_spec->args[0] >= rcdev->nr_resets)
58                 return -EINVAL;
59
60         return reset_spec->args[0];
61 }
62
63 /**
64  * reset_controller_register - register a reset controller device
65  * @rcdev: a pointer to the initialized reset controller device
66  */
67 int reset_controller_register(struct reset_controller_dev *rcdev)
68 {
69         if (!rcdev->of_xlate) {
70                 rcdev->of_reset_n_cells = 1;
71                 rcdev->of_xlate = of_reset_simple_xlate;
72         }
73
74         INIT_LIST_HEAD(&rcdev->reset_control_head);
75
76         mutex_lock(&reset_list_mutex);
77         list_add(&rcdev->list, &reset_controller_list);
78         mutex_unlock(&reset_list_mutex);
79
80         return 0;
81 }
82 EXPORT_SYMBOL_GPL(reset_controller_register);
83
84 /**
85  * reset_controller_unregister - unregister a reset controller device
86  * @rcdev: a pointer to the reset controller device
87  */
88 void reset_controller_unregister(struct reset_controller_dev *rcdev)
89 {
90         mutex_lock(&reset_list_mutex);
91         list_del(&rcdev->list);
92         mutex_unlock(&reset_list_mutex);
93 }
94 EXPORT_SYMBOL_GPL(reset_controller_unregister);
95
96 static void devm_reset_controller_release(struct device *dev, void *res)
97 {
98         reset_controller_unregister(*(struct reset_controller_dev **)res);
99 }
100
101 /**
102  * devm_reset_controller_register - resource managed reset_controller_register()
103  * @dev: device that is registering this reset controller
104  * @rcdev: a pointer to the initialized reset controller device
105  *
106  * Managed reset_controller_register(). For reset controllers registered by
107  * this function, reset_controller_unregister() is automatically called on
108  * driver detach. See reset_controller_register() for more information.
109  */
110 int devm_reset_controller_register(struct device *dev,
111                                    struct reset_controller_dev *rcdev)
112 {
113         struct reset_controller_dev **rcdevp;
114         int ret;
115
116         rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
117                               GFP_KERNEL);
118         if (!rcdevp)
119                 return -ENOMEM;
120
121         ret = reset_controller_register(rcdev);
122         if (!ret) {
123                 *rcdevp = rcdev;
124                 devres_add(dev, rcdevp);
125         } else {
126                 devres_free(rcdevp);
127         }
128
129         return ret;
130 }
131 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
132
133 /**
134  * reset_control_reset - reset the controlled device
135  * @rstc: reset controller
136  *
137  * Calling this on a shared reset controller is an error.
138  *
139  * If rstc is NULL it is an optional reset and the function will just
140  * return 0.
141  */
142 int reset_control_reset(struct reset_control *rstc)
143 {
144         if (!rstc)
145                 return 0;
146
147         if (WARN_ON(IS_ERR(rstc)))
148                 return -EINVAL;
149
150         if (rstc->rcdev->ops->reset)
151                 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
152
153         return -ENOTSUPP;
154 }
155 EXPORT_SYMBOL_GPL(reset_control_reset);
156
157 /**
158  * reset_control_assert - asserts the reset line
159  * @rstc: reset controller
160  *
161  * Calling this on an exclusive reset controller guarantees that the reset
162  * will be asserted. When called on a shared reset controller the line may
163  * still be deasserted, as long as other users keep it so.
164  *
165  * For shared reset controls a driver cannot expect the hw's registers and
166  * internal state to be reset, but must be prepared for this to happen.
167  *
168  * If rstc is NULL it is an optional reset and the function will just
169  * return 0.
170  */
171 int reset_control_assert(struct reset_control *rstc)
172 {
173         if (!rstc)
174                 return 0;
175
176         if (WARN_ON(IS_ERR(rstc)))
177                 return -EINVAL;
178
179         if (!rstc->rcdev->ops->assert)
180                 return -ENOTSUPP;
181
182         if (rstc->shared) {
183                 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
184                         return -EINVAL;
185
186                 if (atomic_dec_return(&rstc->deassert_count) != 0)
187                         return 0;
188         }
189
190         return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
191 }
192 EXPORT_SYMBOL_GPL(reset_control_assert);
193
194 /**
195  * reset_control_deassert - deasserts the reset line
196  * @rstc: reset controller
197  *
198  * After calling this function, the reset is guaranteed to be deasserted.
199  *
200  * If rstc is NULL it is an optional reset and the function will just
201  * return 0.
202  */
203 int reset_control_deassert(struct reset_control *rstc)
204 {
205         if (!rstc)
206                 return 0;
207
208         if (WARN_ON(IS_ERR(rstc)))
209                 return -EINVAL;
210
211         if (!rstc->rcdev->ops->deassert)
212                 return -ENOTSUPP;
213
214         if (rstc->shared) {
215                 if (atomic_inc_return(&rstc->deassert_count) != 1)
216                         return 0;
217         }
218
219         return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
220 }
221 EXPORT_SYMBOL_GPL(reset_control_deassert);
222
223 /**
224  * reset_control_status - returns a negative errno if not supported, a
225  * positive value if the reset line is asserted, or zero if the reset
226  * line is not asserted or if the desc is NULL (optional reset).
227  * @rstc: reset controller
228  */
229 int reset_control_status(struct reset_control *rstc)
230 {
231         if (!rstc)
232                 return 0;
233
234         if (WARN_ON(IS_ERR(rstc)))
235                 return -EINVAL;
236
237         if (rstc->rcdev->ops->status)
238                 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
239
240         return -ENOTSUPP;
241 }
242 EXPORT_SYMBOL_GPL(reset_control_status);
243
244 static struct reset_control *__reset_control_get_internal(
245                                 struct reset_controller_dev *rcdev,
246                                 unsigned int index, int shared)
247 {
248         struct reset_control *rstc;
249
250         lockdep_assert_held(&reset_list_mutex);
251
252         list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
253                 if (rstc->id == index) {
254                         if (WARN_ON(!rstc->shared || !shared))
255                                 return ERR_PTR(-EBUSY);
256
257                         rstc->refcnt++;
258                         return rstc;
259                 }
260         }
261
262         rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
263         if (!rstc)
264                 return ERR_PTR(-ENOMEM);
265
266         if (!try_module_get(rcdev->owner)) {
267                 kfree(rstc);
268                 return ERR_PTR(-ENODEV);
269         }
270
271         rstc->rcdev = rcdev;
272         list_add(&rstc->list, &rcdev->reset_control_head);
273         rstc->id = index;
274         rstc->refcnt = 1;
275         rstc->shared = shared;
276
277         return rstc;
278 }
279
280 static void __reset_control_put_internal(struct reset_control *rstc)
281 {
282         lockdep_assert_held(&reset_list_mutex);
283
284         if (--rstc->refcnt)
285                 return;
286
287         module_put(rstc->rcdev->owner);
288
289         list_del(&rstc->list);
290         kfree(rstc);
291 }
292
293 struct reset_control *__of_reset_control_get(struct device_node *node,
294                                      const char *id, int index, bool shared,
295                                      bool optional)
296 {
297         struct reset_control *rstc;
298         struct reset_controller_dev *r, *rcdev;
299         struct of_phandle_args args;
300         int rstc_id;
301         int ret;
302
303         if (!node)
304                 return ERR_PTR(-EINVAL);
305
306         if (id) {
307                 index = of_property_match_string(node,
308                                                  "reset-names", id);
309                 if (index == -EILSEQ)
310                         return ERR_PTR(index);
311                 if (index < 0)
312                         return optional ? NULL : ERR_PTR(-ENOENT);
313         }
314
315         ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
316                                          index, &args);
317         if (ret == -EINVAL)
318                 return ERR_PTR(ret);
319         if (ret)
320                 return optional ? NULL : ERR_PTR(ret);
321
322         mutex_lock(&reset_list_mutex);
323         rcdev = NULL;
324         list_for_each_entry(r, &reset_controller_list, list) {
325                 if (args.np == r->of_node) {
326                         rcdev = r;
327                         break;
328                 }
329         }
330
331         if (!rcdev) {
332                 rstc = ERR_PTR(-EPROBE_DEFER);
333                 goto out;
334         }
335
336         if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
337                 rstc = ERR_PTR(-EINVAL);
338                 goto out;
339         }
340
341         rstc_id = rcdev->of_xlate(rcdev, &args);
342         if (rstc_id < 0) {
343                 rstc = ERR_PTR(rstc_id);
344                 goto out;
345         }
346
347         /* reset_list_mutex also protects the rcdev's reset_control list */
348         rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
349
350 out:
351         mutex_unlock(&reset_list_mutex);
352         of_node_put(args.np);
353
354         return rstc;
355 }
356 EXPORT_SYMBOL_GPL(__of_reset_control_get);
357
358 struct reset_control *__reset_control_get(struct device *dev, const char *id,
359                                           int index, bool shared, bool optional)
360 {
361         if (dev->of_node)
362                 return __of_reset_control_get(dev->of_node, id, index, shared,
363                                               optional);
364
365         return optional ? NULL : ERR_PTR(-EINVAL);
366 }
367 EXPORT_SYMBOL_GPL(__reset_control_get);
368
369 /**
370  * reset_control_put - free the reset controller
371  * @rstc: reset controller
372  */
373
374 void reset_control_put(struct reset_control *rstc)
375 {
376         if (IS_ERR_OR_NULL(rstc))
377                 return;
378
379         mutex_lock(&reset_list_mutex);
380         __reset_control_put_internal(rstc);
381         mutex_unlock(&reset_list_mutex);
382 }
383 EXPORT_SYMBOL_GPL(reset_control_put);
384
385 static void devm_reset_control_release(struct device *dev, void *res)
386 {
387         reset_control_put(*(struct reset_control **)res);
388 }
389
390 struct reset_control *__devm_reset_control_get(struct device *dev,
391                                      const char *id, int index, bool shared,
392                                      bool optional)
393 {
394         struct reset_control **ptr, *rstc;
395
396         ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
397                            GFP_KERNEL);
398         if (!ptr)
399                 return ERR_PTR(-ENOMEM);
400
401         rstc = __reset_control_get(dev, id, index, shared, optional);
402         if (!IS_ERR(rstc)) {
403                 *ptr = rstc;
404                 devres_add(dev, ptr);
405         } else {
406                 devres_free(ptr);
407         }
408
409         return rstc;
410 }
411 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
412
413 /**
414  * device_reset - find reset controller associated with the device
415  *                and perform reset
416  * @dev: device to be reset by the controller
417  * @optional: whether it is optional to reset the device
418  *
419  * Convenience wrapper for __reset_control_get() and reset_control_reset().
420  * This is useful for the common case of devices with single, dedicated reset
421  * lines.
422  */
423 int __device_reset(struct device *dev, bool optional)
424 {
425         struct reset_control *rstc;
426         int ret;
427
428         rstc = __reset_control_get(dev, NULL, 0, 0, optional);
429         if (IS_ERR(rstc))
430                 return PTR_ERR(rstc);
431
432         ret = reset_control_reset(rstc);
433
434         reset_control_put(rstc);
435
436         return ret;
437 }
438 EXPORT_SYMBOL_GPL(__device_reset);