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/module.h>
18 #include <linux/reset.h>
19 #include <linux/reset-controller.h>
20 #include <linux/slab.h>
22 static DEFINE_MUTEX(reset_list_mutex
);
23 static LIST_HEAD(reset_controller_list
);
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
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
36 struct reset_control
{
37 struct reset_controller_dev
*rcdev
;
38 struct list_head list
;
42 atomic_t deassert_count
;
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)
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.
54 static int of_reset_simple_xlate(struct reset_controller_dev
*rcdev
,
55 const struct of_phandle_args
*reset_spec
)
57 if (reset_spec
->args
[0] >= rcdev
->nr_resets
)
60 return reset_spec
->args
[0];
64 * reset_controller_register - register a reset controller device
65 * @rcdev: a pointer to the initialized reset controller device
67 int reset_controller_register(struct reset_controller_dev
*rcdev
)
69 if (!rcdev
->of_xlate
) {
70 rcdev
->of_reset_n_cells
= 1;
71 rcdev
->of_xlate
= of_reset_simple_xlate
;
74 INIT_LIST_HEAD(&rcdev
->reset_control_head
);
76 mutex_lock(&reset_list_mutex
);
77 list_add(&rcdev
->list
, &reset_controller_list
);
78 mutex_unlock(&reset_list_mutex
);
82 EXPORT_SYMBOL_GPL(reset_controller_register
);
85 * reset_controller_unregister - unregister a reset controller device
86 * @rcdev: a pointer to the reset controller device
88 void reset_controller_unregister(struct reset_controller_dev
*rcdev
)
90 mutex_lock(&reset_list_mutex
);
91 list_del(&rcdev
->list
);
92 mutex_unlock(&reset_list_mutex
);
94 EXPORT_SYMBOL_GPL(reset_controller_unregister
);
96 static void devm_reset_controller_release(struct device
*dev
, void *res
)
98 reset_controller_unregister(*(struct reset_controller_dev
**)res
);
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
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.
110 int devm_reset_controller_register(struct device
*dev
,
111 struct reset_controller_dev
*rcdev
)
113 struct reset_controller_dev
**rcdevp
;
116 rcdevp
= devres_alloc(devm_reset_controller_release
, sizeof(*rcdevp
),
121 ret
= reset_controller_register(rcdev
);
124 devres_add(dev
, rcdevp
);
131 EXPORT_SYMBOL_GPL(devm_reset_controller_register
);
134 * reset_control_reset - reset the controlled device
135 * @rstc: reset controller
137 * Calling this on a shared reset controller is an error.
139 int reset_control_reset(struct reset_control
*rstc
)
141 if (WARN_ON(rstc
->shared
))
144 if (rstc
->rcdev
->ops
->reset
)
145 return rstc
->rcdev
->ops
->reset(rstc
->rcdev
, rstc
->id
);
149 EXPORT_SYMBOL_GPL(reset_control_reset
);
152 * reset_control_assert - asserts the reset line
153 * @rstc: reset controller
155 * Calling this on an exclusive reset controller guarantees that the reset
156 * will be asserted. When called on a shared reset controller the line may
157 * still be deasserted, as long as other users keep it so.
159 * For shared reset controls a driver cannot expect the hw's registers and
160 * internal state to be reset, but must be prepared for this to happen.
162 int reset_control_assert(struct reset_control
*rstc
)
164 if (!rstc
->rcdev
->ops
->assert)
168 if (WARN_ON(atomic_read(&rstc
->deassert_count
) == 0))
171 if (atomic_dec_return(&rstc
->deassert_count
) != 0)
175 return rstc
->rcdev
->ops
->assert(rstc
->rcdev
, rstc
->id
);
177 EXPORT_SYMBOL_GPL(reset_control_assert
);
180 * reset_control_deassert - deasserts the reset line
181 * @rstc: reset controller
183 * After calling this function, the reset is guaranteed to be deasserted.
185 int reset_control_deassert(struct reset_control
*rstc
)
187 if (!rstc
->rcdev
->ops
->deassert
)
191 if (atomic_inc_return(&rstc
->deassert_count
) != 1)
195 return rstc
->rcdev
->ops
->deassert(rstc
->rcdev
, rstc
->id
);
197 EXPORT_SYMBOL_GPL(reset_control_deassert
);
200 * reset_control_status - returns a negative errno if not supported, a
201 * positive value if the reset line is asserted, or zero if the reset
202 * line is not asserted.
203 * @rstc: reset controller
205 int reset_control_status(struct reset_control
*rstc
)
207 if (rstc
->rcdev
->ops
->status
)
208 return rstc
->rcdev
->ops
->status(rstc
->rcdev
, rstc
->id
);
212 EXPORT_SYMBOL_GPL(reset_control_status
);
214 static struct reset_control
*__reset_control_get(
215 struct reset_controller_dev
*rcdev
,
216 unsigned int index
, int shared
)
218 struct reset_control
*rstc
;
220 lockdep_assert_held(&reset_list_mutex
);
222 list_for_each_entry(rstc
, &rcdev
->reset_control_head
, list
) {
223 if (rstc
->id
== index
) {
224 if (WARN_ON(!rstc
->shared
|| !shared
))
225 return ERR_PTR(-EBUSY
);
232 rstc
= kzalloc(sizeof(*rstc
), GFP_KERNEL
);
234 return ERR_PTR(-ENOMEM
);
236 try_module_get(rcdev
->owner
);
239 list_add(&rstc
->list
, &rcdev
->reset_control_head
);
242 rstc
->shared
= shared
;
247 static void __reset_control_put(struct reset_control
*rstc
)
249 lockdep_assert_held(&reset_list_mutex
);
254 module_put(rstc
->rcdev
->owner
);
256 list_del(&rstc
->list
);
260 struct reset_control
*__of_reset_control_get(struct device_node
*node
,
261 const char *id
, int index
, int shared
)
263 struct reset_control
*rstc
;
264 struct reset_controller_dev
*r
, *rcdev
;
265 struct of_phandle_args args
;
270 return ERR_PTR(-EINVAL
);
273 index
= of_property_match_string(node
,
276 return ERR_PTR(-ENOENT
);
279 ret
= of_parse_phandle_with_args(node
, "resets", "#reset-cells",
284 mutex_lock(&reset_list_mutex
);
286 list_for_each_entry(r
, &reset_controller_list
, list
) {
287 if (args
.np
== r
->of_node
) {
292 of_node_put(args
.np
);
295 mutex_unlock(&reset_list_mutex
);
296 return ERR_PTR(-EPROBE_DEFER
);
299 if (WARN_ON(args
.args_count
!= rcdev
->of_reset_n_cells
)) {
300 mutex_unlock(&reset_list_mutex
);
301 return ERR_PTR(-EINVAL
);
304 rstc_id
= rcdev
->of_xlate(rcdev
, &args
);
306 mutex_unlock(&reset_list_mutex
);
307 return ERR_PTR(rstc_id
);
310 /* reset_list_mutex also protects the rcdev's reset_control list */
311 rstc
= __reset_control_get(rcdev
, rstc_id
, shared
);
313 mutex_unlock(&reset_list_mutex
);
317 EXPORT_SYMBOL_GPL(__of_reset_control_get
);
320 * reset_control_put - free the reset controller
321 * @rstc: reset controller
324 void reset_control_put(struct reset_control
*rstc
)
329 mutex_lock(&reset_list_mutex
);
330 __reset_control_put(rstc
);
331 mutex_unlock(&reset_list_mutex
);
333 EXPORT_SYMBOL_GPL(reset_control_put
);
335 static void devm_reset_control_release(struct device
*dev
, void *res
)
337 reset_control_put(*(struct reset_control
**)res
);
340 struct reset_control
*__devm_reset_control_get(struct device
*dev
,
341 const char *id
, int index
, int shared
)
343 struct reset_control
**ptr
, *rstc
;
345 ptr
= devres_alloc(devm_reset_control_release
, sizeof(*ptr
),
348 return ERR_PTR(-ENOMEM
);
350 rstc
= __of_reset_control_get(dev
? dev
->of_node
: NULL
,
354 devres_add(dev
, ptr
);
361 EXPORT_SYMBOL_GPL(__devm_reset_control_get
);
364 * device_reset - find reset controller associated with the device
366 * @dev: device to be reset by the controller
368 * Convenience wrapper for reset_control_get() and reset_control_reset().
369 * This is useful for the common case of devices with single, dedicated reset
372 int device_reset(struct device
*dev
)
374 struct reset_control
*rstc
;
377 rstc
= reset_control_get(dev
, NULL
);
379 return PTR_ERR(rstc
);
381 ret
= reset_control_reset(rstc
);
383 reset_control_put(rstc
);
387 EXPORT_SYMBOL_GPL(device_reset
);