2 * Reset Controller framework
4 * Copyright 2013 Philipp Zabel, Pengutronix
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.
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/kref.h>
17 #include <linux/module.h>
19 #include <linux/reset.h>
20 #include <linux/reset-controller.h>
21 #include <linux/slab.h>
23 static DEFINE_MUTEX(reset_list_mutex);
24 static LIST_HEAD(reset_controller_list);
27 * struct reset_control - a reset control
28 * @rcdev: a pointer to the reset controller device
29 * this reset control belongs to
30 * @list: list entry for the rcdev's reset controller list
31 * @id: ID of the reset controller in the reset
33 * @refcnt: Number of gets of this reset_control
34 * @shared: Is this a shared (1), or an exclusive (0) reset_control?
35 * @deassert_cnt: Number of times this reset line has been deasserted
36 * @triggered_count: Number of times this reset line has been reset. Currently
37 * only used for shared resets, which means that the value
38 * will be either 0 or 1.
40 struct reset_control {
41 struct reset_controller_dev *rcdev;
42 struct list_head list;
46 atomic_t deassert_count;
47 atomic_t triggered_count;
51 * of_reset_simple_xlate - translate reset_spec to the reset line number
52 * @rcdev: a pointer to the reset controller device
53 * @reset_spec: reset line specifier as found in the device tree
54 * @flags: a flags pointer to fill in (optional)
56 * This simple translation function should be used for reset controllers
57 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
59 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
60 const struct of_phandle_args *reset_spec)
62 if (reset_spec->args[0] >= rcdev->nr_resets)
65 return reset_spec->args[0];
69 * reset_controller_register - register a reset controller device
70 * @rcdev: a pointer to the initialized reset controller device
72 int reset_controller_register(struct reset_controller_dev *rcdev)
74 if (!rcdev->of_xlate) {
75 rcdev->of_reset_n_cells = 1;
76 rcdev->of_xlate = of_reset_simple_xlate;
79 INIT_LIST_HEAD(&rcdev->reset_control_head);
81 mutex_lock(&reset_list_mutex);
82 list_add(&rcdev->list, &reset_controller_list);
83 mutex_unlock(&reset_list_mutex);
87 EXPORT_SYMBOL_GPL(reset_controller_register);
90 * reset_controller_unregister - unregister a reset controller device
91 * @rcdev: a pointer to the reset controller device
93 void reset_controller_unregister(struct reset_controller_dev *rcdev)
95 mutex_lock(&reset_list_mutex);
96 list_del(&rcdev->list);
97 mutex_unlock(&reset_list_mutex);
99 EXPORT_SYMBOL_GPL(reset_controller_unregister);
101 static void devm_reset_controller_release(struct device *dev, void *res)
103 reset_controller_unregister(*(struct reset_controller_dev **)res);
107 * devm_reset_controller_register - resource managed reset_controller_register()
108 * @dev: device that is registering this reset controller
109 * @rcdev: a pointer to the initialized reset controller device
111 * Managed reset_controller_register(). For reset controllers registered by
112 * this function, reset_controller_unregister() is automatically called on
113 * driver detach. See reset_controller_register() for more information.
115 int devm_reset_controller_register(struct device *dev,
116 struct reset_controller_dev *rcdev)
118 struct reset_controller_dev **rcdevp;
121 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
126 ret = reset_controller_register(rcdev);
129 devres_add(dev, rcdevp);
136 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
139 * reset_control_reset - reset the controlled device
140 * @rstc: reset controller
142 * On a shared reset line the actual reset pulse is only triggered once for the
143 * lifetime of the reset_control instance: for all but the first caller this is
145 * Consumers must not use reset_control_(de)assert on shared reset lines when
146 * reset_control_reset has been used.
148 * If rstc is NULL it is an optional reset and the function will just
151 int reset_control_reset(struct reset_control *rstc)
158 if (WARN_ON(IS_ERR(rstc)))
161 if (!rstc->rcdev->ops->reset)
165 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
168 if (atomic_inc_return(&rstc->triggered_count) != 1)
172 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
173 if (rstc->shared && ret)
174 atomic_dec(&rstc->triggered_count);
178 EXPORT_SYMBOL_GPL(reset_control_reset);
181 * reset_control_assert - asserts the reset line
182 * @rstc: reset controller
184 * Calling this on an exclusive reset controller guarantees that the reset
185 * will be asserted. When called on a shared reset controller the line may
186 * still be deasserted, as long as other users keep it so.
188 * For shared reset controls a driver cannot expect the hw's registers and
189 * internal state to be reset, but must be prepared for this to happen.
190 * Consumers must not use reset_control_reset on shared reset lines when
191 * reset_control_(de)assert has been used.
194 * If rstc is NULL it is an optional reset and the function will just
197 int reset_control_assert(struct reset_control *rstc)
202 if (WARN_ON(IS_ERR(rstc)))
205 if (!rstc->rcdev->ops->assert)
209 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
212 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
215 if (atomic_dec_return(&rstc->deassert_count) != 0)
219 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
221 EXPORT_SYMBOL_GPL(reset_control_assert);
224 * reset_control_deassert - deasserts the reset line
225 * @rstc: reset controller
227 * After calling this function, the reset is guaranteed to be deasserted.
228 * Consumers must not use reset_control_reset on shared reset lines when
229 * reset_control_(de)assert has been used.
232 * If rstc is NULL it is an optional reset and the function will just
235 int reset_control_deassert(struct reset_control *rstc)
240 if (WARN_ON(IS_ERR(rstc)))
243 if (!rstc->rcdev->ops->deassert)
247 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
250 if (atomic_inc_return(&rstc->deassert_count) != 1)
254 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
256 EXPORT_SYMBOL_GPL(reset_control_deassert);
259 * reset_control_status - returns a negative errno if not supported, a
260 * positive value if the reset line is asserted, or zero if the reset
261 * line is not asserted or if the desc is NULL (optional reset).
262 * @rstc: reset controller
264 int reset_control_status(struct reset_control *rstc)
269 if (WARN_ON(IS_ERR(rstc)))
272 if (rstc->rcdev->ops->status)
273 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
277 EXPORT_SYMBOL_GPL(reset_control_status);
279 static struct reset_control *__reset_control_get_internal(
280 struct reset_controller_dev *rcdev,
281 unsigned int index, bool shared)
283 struct reset_control *rstc;
285 lockdep_assert_held(&reset_list_mutex);
287 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
288 if (rstc->id == index) {
289 if (WARN_ON(!rstc->shared || !shared))
290 return ERR_PTR(-EBUSY);
292 kref_get(&rstc->refcnt);
297 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
299 return ERR_PTR(-ENOMEM);
301 try_module_get(rcdev->owner);
304 list_add(&rstc->list, &rcdev->reset_control_head);
306 kref_init(&rstc->refcnt);
307 rstc->shared = shared;
312 static void __reset_control_release(struct kref *kref)
314 struct reset_control *rstc = container_of(kref, struct reset_control,
317 lockdep_assert_held(&reset_list_mutex);
319 module_put(rstc->rcdev->owner);
321 list_del(&rstc->list);
325 static void __reset_control_put_internal(struct reset_control *rstc)
327 lockdep_assert_held(&reset_list_mutex);
329 kref_put(&rstc->refcnt, __reset_control_release);
332 struct reset_control *__of_reset_control_get(struct device_node *node,
333 const char *id, int index, bool shared,
336 struct reset_control *rstc;
337 struct reset_controller_dev *r, *rcdev;
338 struct of_phandle_args args;
343 return ERR_PTR(-EINVAL);
346 index = of_property_match_string(node,
348 if (index == -EILSEQ)
349 return ERR_PTR(index);
351 return optional ? NULL : ERR_PTR(-ENOENT);
354 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
359 return optional ? NULL : ERR_PTR(ret);
361 mutex_lock(&reset_list_mutex);
363 list_for_each_entry(r, &reset_controller_list, list) {
364 if (args.np == r->of_node) {
369 of_node_put(args.np);
372 mutex_unlock(&reset_list_mutex);
373 return ERR_PTR(-EPROBE_DEFER);
376 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
377 mutex_unlock(&reset_list_mutex);
378 return ERR_PTR(-EINVAL);
381 rstc_id = rcdev->of_xlate(rcdev, &args);
383 mutex_unlock(&reset_list_mutex);
384 return ERR_PTR(rstc_id);
387 /* reset_list_mutex also protects the rcdev's reset_control list */
388 rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
390 mutex_unlock(&reset_list_mutex);
394 EXPORT_SYMBOL_GPL(__of_reset_control_get);
396 struct reset_control *__reset_control_get(struct device *dev, const char *id,
397 int index, bool shared, bool optional)
400 return __of_reset_control_get(dev->of_node, id, index, shared,
403 return optional ? NULL : ERR_PTR(-EINVAL);
405 EXPORT_SYMBOL_GPL(__reset_control_get);
408 * reset_control_put - free the reset controller
409 * @rstc: reset controller
411 void reset_control_put(struct reset_control *rstc)
413 if (IS_ERR_OR_NULL(rstc))
416 mutex_lock(&reset_list_mutex);
417 __reset_control_put_internal(rstc);
418 mutex_unlock(&reset_list_mutex);
420 EXPORT_SYMBOL_GPL(reset_control_put);
422 static void devm_reset_control_release(struct device *dev, void *res)
424 reset_control_put(*(struct reset_control **)res);
427 struct reset_control *__devm_reset_control_get(struct device *dev,
428 const char *id, int index, bool shared,
431 struct reset_control **ptr, *rstc;
433 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
436 return ERR_PTR(-ENOMEM);
438 rstc = __reset_control_get(dev, id, index, shared, optional);
441 devres_add(dev, ptr);
448 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
451 * device_reset - find reset controller associated with the device
453 * @dev: device to be reset by the controller
455 * Convenience wrapper for reset_control_get() and reset_control_reset().
456 * This is useful for the common case of devices with single, dedicated reset
459 int device_reset(struct device *dev)
461 struct reset_control *rstc;
464 rstc = reset_control_get(dev, NULL);
466 return PTR_ERR(rstc);
468 ret = reset_control_reset(rstc);
470 reset_control_put(rstc);
474 EXPORT_SYMBOL_GPL(device_reset);