blob: 9ab929049b9dfc00c5e05a3350218fb853365821 [file] [log] [blame]
Philipp Zabel61fc4132012-11-19 17:23:13 +01001/*
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
21static DEFINE_MUTEX(reset_controller_list_mutex);
22static 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 */
31struct reset_control {
32 struct reset_controller_dev *rcdev;
33 struct device *dev;
34 unsigned int id;
35};
36
37/**
38 * of_reset_simple_xlate - translate reset_spec to the reset line number
39 * @rcdev: a pointer to the reset controller device
40 * @reset_spec: reset line specifier as found in the device tree
41 * @flags: a flags pointer to fill in (optional)
42 *
43 * This simple translation function should be used for reset controllers
44 * with 1:1 mapping, where reset lines can be indexed by number without gaps.
45 */
Rashika Kheria0c5b2b92013-12-19 14:11:10 +053046static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
Philipp Zabel61fc4132012-11-19 17:23:13 +010047 const struct of_phandle_args *reset_spec)
48{
49 if (WARN_ON(reset_spec->args_count != rcdev->of_reset_n_cells))
50 return -EINVAL;
51
52 if (reset_spec->args[0] >= rcdev->nr_resets)
53 return -EINVAL;
54
55 return reset_spec->args[0];
56}
Philipp Zabel61fc4132012-11-19 17:23:13 +010057
58/**
59 * reset_controller_register - register a reset controller device
60 * @rcdev: a pointer to the initialized reset controller device
61 */
62int reset_controller_register(struct reset_controller_dev *rcdev)
63{
64 if (!rcdev->of_xlate) {
65 rcdev->of_reset_n_cells = 1;
66 rcdev->of_xlate = of_reset_simple_xlate;
67 }
68
69 mutex_lock(&reset_controller_list_mutex);
70 list_add(&rcdev->list, &reset_controller_list);
71 mutex_unlock(&reset_controller_list_mutex);
72
73 return 0;
74}
75EXPORT_SYMBOL_GPL(reset_controller_register);
76
77/**
78 * reset_controller_unregister - unregister a reset controller device
79 * @rcdev: a pointer to the reset controller device
80 */
81void reset_controller_unregister(struct reset_controller_dev *rcdev)
82{
83 mutex_lock(&reset_controller_list_mutex);
84 list_del(&rcdev->list);
85 mutex_unlock(&reset_controller_list_mutex);
86}
87EXPORT_SYMBOL_GPL(reset_controller_unregister);
88
89/**
90 * reset_control_reset - reset the controlled device
91 * @rstc: reset controller
92 */
93int reset_control_reset(struct reset_control *rstc)
94{
95 if (rstc->rcdev->ops->reset)
96 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
97
Philipp Zabel39b4da72015-10-29 09:55:00 +010098 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +010099}
100EXPORT_SYMBOL_GPL(reset_control_reset);
101
102/**
103 * reset_control_assert - asserts the reset line
104 * @rstc: reset controller
105 */
106int reset_control_assert(struct reset_control *rstc)
107{
108 if (rstc->rcdev->ops->assert)
109 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
110
Philipp Zabel39b4da72015-10-29 09:55:00 +0100111 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100112}
113EXPORT_SYMBOL_GPL(reset_control_assert);
114
115/**
116 * reset_control_deassert - deasserts the reset line
117 * @rstc: reset controller
118 */
119int reset_control_deassert(struct reset_control *rstc)
120{
121 if (rstc->rcdev->ops->deassert)
122 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
123
Philipp Zabel39b4da72015-10-29 09:55:00 +0100124 return -ENOTSUPP;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100125}
126EXPORT_SYMBOL_GPL(reset_control_deassert);
127
128/**
Dinh Nguyen729de412014-10-10 10:21:14 -0500129 * reset_control_status - returns a negative errno if not supported, a
130 * positive value if the reset line is asserted, or zero if the reset
131 * line is not asserted.
132 * @rstc: reset controller
133 */
134int reset_control_status(struct reset_control *rstc)
135{
136 if (rstc->rcdev->ops->status)
137 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
138
Philipp Zabel39b4da72015-10-29 09:55:00 +0100139 return -ENOTSUPP;
Dinh Nguyen729de412014-10-10 10:21:14 -0500140}
141EXPORT_SYMBOL_GPL(reset_control_status);
142
143/**
Vince Hsuc0a13aa62015-07-13 13:39:39 +0100144 * of_reset_control_get_by_index - Lookup and obtain a reference to a reset
145 * controller by index.
Maxime Ripardfc0a5922013-12-20 22:41:07 +0100146 * @node: device to be reset by the controller
Vince Hsuc0a13aa62015-07-13 13:39:39 +0100147 * @index: index of the reset controller
Philipp Zabel61fc4132012-11-19 17:23:13 +0100148 *
Vince Hsuc0a13aa62015-07-13 13:39:39 +0100149 * This is to be used to perform a list of resets for a device or power domain
150 * in whatever order. Returns a struct reset_control or IS_ERR() condition
151 * containing errno.
Philipp Zabel61fc4132012-11-19 17:23:13 +0100152 */
Vince Hsuc0a13aa62015-07-13 13:39:39 +0100153struct reset_control *of_reset_control_get_by_index(struct device_node *node,
154 int index)
Philipp Zabel61fc4132012-11-19 17:23:13 +0100155{
156 struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER);
157 struct reset_controller_dev *r, *rcdev;
158 struct of_phandle_args args;
Philipp Zabel61fc4132012-11-19 17:23:13 +0100159 int rstc_id;
160 int ret;
161
Maxime Ripardfc0a5922013-12-20 22:41:07 +0100162 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
Philipp Zabel61fc4132012-11-19 17:23:13 +0100163 index, &args);
164 if (ret)
165 return ERR_PTR(ret);
166
167 mutex_lock(&reset_controller_list_mutex);
168 rcdev = NULL;
169 list_for_each_entry(r, &reset_controller_list, list) {
170 if (args.np == r->of_node) {
171 rcdev = r;
172 break;
173 }
174 }
175 of_node_put(args.np);
176
177 if (!rcdev) {
178 mutex_unlock(&reset_controller_list_mutex);
Philipp Zabel3d103022013-07-18 13:55:22 +0200179 return ERR_PTR(-EPROBE_DEFER);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100180 }
181
182 rstc_id = rcdev->of_xlate(rcdev, &args);
183 if (rstc_id < 0) {
184 mutex_unlock(&reset_controller_list_mutex);
185 return ERR_PTR(rstc_id);
186 }
187
188 try_module_get(rcdev->owner);
189 mutex_unlock(&reset_controller_list_mutex);
190
191 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
192 if (!rstc) {
Dan Carpenter6034bb22013-04-03 08:02:53 +0300193 module_put(rcdev->owner);
Philipp Zabel61fc4132012-11-19 17:23:13 +0100194 return ERR_PTR(-ENOMEM);
195 }
196
Philipp Zabel61fc4132012-11-19 17:23:13 +0100197 rstc->rcdev = rcdev;
198 rstc->id = rstc_id;
199
200 return rstc;
201}
Vince Hsuc0a13aa62015-07-13 13:39:39 +0100202EXPORT_SYMBOL_GPL(of_reset_control_get_by_index);
203
204/**
205 * of_reset_control_get - Lookup and obtain a reference to a reset controller.
206 * @node: device to be reset by the controller
207 * @id: reset line name
208 *
209 * Returns a struct reset_control or IS_ERR() condition containing errno.
210 *
211 * Use of id names is optional.
212 */
213struct reset_control *of_reset_control_get(struct device_node *node,
214 const char *id)
215{
216 int index = 0;
217
Alban Bedel3d812162015-09-01 17:28:31 +0200218 if (id) {
Vince Hsuc0a13aa62015-07-13 13:39:39 +0100219 index = of_property_match_string(node,
220 "reset-names", id);
Alban Bedel3d812162015-09-01 17:28:31 +0200221 if (index < 0)
222 return ERR_PTR(-ENOENT);
223 }
Vince Hsuc0a13aa62015-07-13 13:39:39 +0100224 return of_reset_control_get_by_index(node, index);
225}
Maxime Ripardfc0a5922013-12-20 22:41:07 +0100226EXPORT_SYMBOL_GPL(of_reset_control_get);
227
228/**
229 * reset_control_get - Lookup and obtain a reference to a reset controller.
230 * @dev: device to be reset by the controller
231 * @id: reset line name
232 *
233 * Returns a struct reset_control or IS_ERR() condition containing errno.
234 *
235 * Use of id names is optional.
236 */
237struct reset_control *reset_control_get(struct device *dev, const char *id)
238{
239 struct reset_control *rstc;
240
241 if (!dev)
242 return ERR_PTR(-EINVAL);
243
244 rstc = of_reset_control_get(dev->of_node, id);
245 if (!IS_ERR(rstc))
246 rstc->dev = dev;
247
248 return rstc;
249}
Philipp Zabel61fc4132012-11-19 17:23:13 +0100250EXPORT_SYMBOL_GPL(reset_control_get);
251
252/**
253 * reset_control_put - free the reset controller
254 * @rstc: reset controller
255 */
256
257void reset_control_put(struct reset_control *rstc)
258{
259 if (IS_ERR(rstc))
260 return;
261
262 module_put(rstc->rcdev->owner);
263 kfree(rstc);
264}
265EXPORT_SYMBOL_GPL(reset_control_put);
266
267static void devm_reset_control_release(struct device *dev, void *res)
268{
269 reset_control_put(*(struct reset_control **)res);
270}
271
272/**
273 * devm_reset_control_get - resource managed reset_control_get()
274 * @dev: device to be reset by the controller
275 * @id: reset line name
276 *
277 * Managed reset_control_get(). For reset controllers returned from this
278 * function, reset_control_put() is called automatically on driver detach.
279 * See reset_control_get() for more information.
280 */
281struct reset_control *devm_reset_control_get(struct device *dev, const char *id)
282{
283 struct reset_control **ptr, *rstc;
284
285 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
286 GFP_KERNEL);
287 if (!ptr)
288 return ERR_PTR(-ENOMEM);
289
290 rstc = reset_control_get(dev, id);
291 if (!IS_ERR(rstc)) {
292 *ptr = rstc;
293 devres_add(dev, ptr);
294 } else {
295 devres_free(ptr);
296 }
297
298 return rstc;
299}
300EXPORT_SYMBOL_GPL(devm_reset_control_get);
301
Philipp Zabel61fc4132012-11-19 17:23:13 +0100302/**
303 * device_reset - find reset controller associated with the device
304 * and perform reset
305 * @dev: device to be reset by the controller
306 *
307 * Convenience wrapper for reset_control_get() and reset_control_reset().
308 * This is useful for the common case of devices with single, dedicated reset
309 * lines.
310 */
311int device_reset(struct device *dev)
312{
313 struct reset_control *rstc;
314 int ret;
315
316 rstc = reset_control_get(dev, NULL);
317 if (IS_ERR(rstc))
318 return PTR_ERR(rstc);
319
320 ret = reset_control_reset(rstc);
321
322 reset_control_put(rstc);
323
324 return ret;
325}
326EXPORT_SYMBOL_GPL(device_reset);