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/device.h>
12 #include <linux/err.h>
13 #include <linux/export.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/reset.h>
18 #include <linux/reset-controller.h>
19 #include <linux/slab.h>
21 static DEFINE_MUTEX(reset_controller_list_mutex
);
22 static LIST_HEAD(reset_controller_list
);
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
31 struct reset_control
{
32 struct reset_controller_dev
*rcdev
;
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)
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.
45 static int of_reset_simple_xlate(struct reset_controller_dev
*rcdev
,
46 const struct of_phandle_args
*reset_spec
)
48 if (reset_spec
->args
[0] >= rcdev
->nr_resets
)
51 return reset_spec
->args
[0];
55 * reset_controller_register - register a reset controller device
56 * @rcdev: a pointer to the initialized reset controller device
58 int reset_controller_register(struct reset_controller_dev
*rcdev
)
60 if (!rcdev
->of_xlate
) {
61 rcdev
->of_reset_n_cells
= 1;
62 rcdev
->of_xlate
= of_reset_simple_xlate
;
65 mutex_lock(&reset_controller_list_mutex
);
66 list_add(&rcdev
->list
, &reset_controller_list
);
67 mutex_unlock(&reset_controller_list_mutex
);
71 EXPORT_SYMBOL_GPL(reset_controller_register
);
74 * reset_controller_unregister - unregister a reset controller device
75 * @rcdev: a pointer to the reset controller device
77 void reset_controller_unregister(struct reset_controller_dev
*rcdev
)
79 mutex_lock(&reset_controller_list_mutex
);
80 list_del(&rcdev
->list
);
81 mutex_unlock(&reset_controller_list_mutex
);
83 EXPORT_SYMBOL_GPL(reset_controller_unregister
);
86 * reset_control_reset - reset the controlled device
87 * @rstc: reset controller
89 int reset_control_reset(struct reset_control
*rstc
)
91 if (rstc
->rcdev
->ops
->reset
)
92 return rstc
->rcdev
->ops
->reset(rstc
->rcdev
, rstc
->id
);
96 EXPORT_SYMBOL_GPL(reset_control_reset
);
99 * reset_control_assert - asserts the reset line
100 * @rstc: reset controller
102 int reset_control_assert(struct reset_control
*rstc
)
104 if (rstc
->rcdev
->ops
->assert)
105 return rstc
->rcdev
->ops
->assert(rstc
->rcdev
, rstc
->id
);
109 EXPORT_SYMBOL_GPL(reset_control_assert
);
112 * reset_control_deassert - deasserts the reset line
113 * @rstc: reset controller
115 int reset_control_deassert(struct reset_control
*rstc
)
117 if (rstc
->rcdev
->ops
->deassert
)
118 return rstc
->rcdev
->ops
->deassert(rstc
->rcdev
, rstc
->id
);
122 EXPORT_SYMBOL_GPL(reset_control_deassert
);
125 * reset_control_status - returns a negative errno if not supported, a
126 * positive value if the reset line is asserted, or zero if the reset
127 * line is not asserted.
128 * @rstc: reset controller
130 int reset_control_status(struct reset_control
*rstc
)
132 if (rstc
->rcdev
->ops
->status
)
133 return rstc
->rcdev
->ops
->status(rstc
->rcdev
, rstc
->id
);
137 EXPORT_SYMBOL_GPL(reset_control_status
);
140 * of_reset_control_get_by_index - Lookup and obtain a reference to a reset
141 * controller by index.
142 * @node: device to be reset by the controller
143 * @index: index of the reset controller
145 * This is to be used to perform a list of resets for a device or power domain
146 * in whatever order. Returns a struct reset_control or IS_ERR() condition
149 struct reset_control
*of_reset_control_get_by_index(struct device_node
*node
,
152 struct reset_control
*rstc
;
153 struct reset_controller_dev
*r
, *rcdev
;
154 struct of_phandle_args args
;
158 ret
= of_parse_phandle_with_args(node
, "resets", "#reset-cells",
163 mutex_lock(&reset_controller_list_mutex
);
165 list_for_each_entry(r
, &reset_controller_list
, list
) {
166 if (args
.np
== r
->of_node
) {
171 of_node_put(args
.np
);
174 mutex_unlock(&reset_controller_list_mutex
);
175 return ERR_PTR(-EPROBE_DEFER
);
178 if (WARN_ON(args
.args_count
!= rcdev
->of_reset_n_cells
)) {
179 mutex_unlock(&reset_controller_list_mutex
);
180 return ERR_PTR(-EINVAL
);
183 rstc_id
= rcdev
->of_xlate(rcdev
, &args
);
185 mutex_unlock(&reset_controller_list_mutex
);
186 return ERR_PTR(rstc_id
);
189 try_module_get(rcdev
->owner
);
190 mutex_unlock(&reset_controller_list_mutex
);
192 rstc
= kzalloc(sizeof(*rstc
), GFP_KERNEL
);
194 module_put(rcdev
->owner
);
195 return ERR_PTR(-ENOMEM
);
203 EXPORT_SYMBOL_GPL(of_reset_control_get_by_index
);
206 * of_reset_control_get - Lookup and obtain a reference to a reset controller.
207 * @node: device to be reset by the controller
208 * @id: reset line name
210 * Returns a struct reset_control or IS_ERR() condition containing errno.
212 * Use of id names is optional.
214 struct reset_control
*of_reset_control_get(struct device_node
*node
,
220 index
= of_property_match_string(node
,
223 return ERR_PTR(-ENOENT
);
225 return of_reset_control_get_by_index(node
, index
);
227 EXPORT_SYMBOL_GPL(of_reset_control_get
);
230 * reset_control_get - Lookup and obtain a reference to a reset controller.
231 * @dev: device to be reset by the controller
232 * @id: reset line name
234 * Returns a struct reset_control or IS_ERR() condition containing errno.
236 * Use of id names is optional.
238 struct reset_control
*reset_control_get(struct device
*dev
, const char *id
)
241 return ERR_PTR(-EINVAL
);
243 return of_reset_control_get(dev
->of_node
, id
);
245 EXPORT_SYMBOL_GPL(reset_control_get
);
248 * reset_control_put - free the reset controller
249 * @rstc: reset controller
252 void reset_control_put(struct reset_control
*rstc
)
257 module_put(rstc
->rcdev
->owner
);
260 EXPORT_SYMBOL_GPL(reset_control_put
);
262 static void devm_reset_control_release(struct device
*dev
, void *res
)
264 reset_control_put(*(struct reset_control
**)res
);
268 * devm_reset_control_get - resource managed reset_control_get()
269 * @dev: device to be reset by the controller
270 * @id: reset line name
272 * Managed reset_control_get(). For reset controllers returned from this
273 * function, reset_control_put() is called automatically on driver detach.
274 * See reset_control_get() for more information.
276 struct reset_control
*devm_reset_control_get(struct device
*dev
, const char *id
)
278 struct reset_control
**ptr
, *rstc
;
280 ptr
= devres_alloc(devm_reset_control_release
, sizeof(*ptr
),
283 return ERR_PTR(-ENOMEM
);
285 rstc
= reset_control_get(dev
, id
);
288 devres_add(dev
, ptr
);
295 EXPORT_SYMBOL_GPL(devm_reset_control_get
);
298 * device_reset - find reset controller associated with the device
300 * @dev: device to be reset by the controller
302 * Convenience wrapper for reset_control_get() and reset_control_reset().
303 * This is useful for the common case of devices with single, dedicated reset
306 int device_reset(struct device
*dev
)
308 struct reset_control
*rstc
;
311 rstc
= reset_control_get(dev
, NULL
);
313 return PTR_ERR(rstc
);
315 ret
= reset_control_reset(rstc
);
317 reset_control_put(rstc
);
321 EXPORT_SYMBOL_GPL(device_reset
);