Commit | Line | Data |
---|---|---|
61fc4131 PZ |
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/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 | ||
21 | static DEFINE_MUTEX(reset_controller_list_mutex); | |
22 | static 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 | */ | |
31 | struct 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 | */ | |
0c5b2b91 | 46 | static int of_reset_simple_xlate(struct reset_controller_dev *rcdev, |
61fc4131 PZ |
47 | 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 | } | |
61fc4131 PZ |
57 | |
58 | /** | |
59 | * reset_controller_register - register a reset controller device | |
60 | * @rcdev: a pointer to the initialized reset controller device | |
61 | */ | |
62 | int 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 | } | |
75 | EXPORT_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 | */ | |
81 | void 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 | } | |
87 | EXPORT_SYMBOL_GPL(reset_controller_unregister); | |
88 | ||
89 | /** | |
90 | * reset_control_reset - reset the controlled device | |
91 | * @rstc: reset controller | |
92 | */ | |
93 | int 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 | ||
39b4da71 | 98 | return -ENOTSUPP; |
61fc4131 PZ |
99 | } |
100 | EXPORT_SYMBOL_GPL(reset_control_reset); | |
101 | ||
102 | /** | |
103 | * reset_control_assert - asserts the reset line | |
104 | * @rstc: reset controller | |
105 | */ | |
106 | int 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 | ||
39b4da71 | 111 | return -ENOTSUPP; |
61fc4131 PZ |
112 | } |
113 | EXPORT_SYMBOL_GPL(reset_control_assert); | |
114 | ||
115 | /** | |
116 | * reset_control_deassert - deasserts the reset line | |
117 | * @rstc: reset controller | |
118 | */ | |
119 | int 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 | ||
39b4da71 | 124 | return -ENOTSUPP; |
61fc4131 PZ |
125 | } |
126 | EXPORT_SYMBOL_GPL(reset_control_deassert); | |
127 | ||
729de41b DN |
128 | /** |
129 | * 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 | */ | |
134 | int 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 | ||
39b4da71 | 139 | return -ENOTSUPP; |
729de41b DN |
140 | } |
141 | EXPORT_SYMBOL_GPL(reset_control_status); | |
142 | ||
61fc4131 | 143 | /** |
c0a13aa6 VH |
144 | * of_reset_control_get_by_index - Lookup and obtain a reference to a reset |
145 | * controller by index. | |
fc0a5921 | 146 | * @node: device to be reset by the controller |
c0a13aa6 | 147 | * @index: index of the reset controller |
61fc4131 | 148 | * |
c0a13aa6 VH |
149 | * 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. | |
61fc4131 | 152 | */ |
c0a13aa6 VH |
153 | struct reset_control *of_reset_control_get_by_index(struct device_node *node, |
154 | int index) | |
61fc4131 PZ |
155 | { |
156 | struct reset_control *rstc = ERR_PTR(-EPROBE_DEFER); | |
157 | struct reset_controller_dev *r, *rcdev; | |
158 | struct of_phandle_args args; | |
61fc4131 PZ |
159 | int rstc_id; |
160 | int ret; | |
161 | ||
fc0a5921 | 162 | ret = of_parse_phandle_with_args(node, "resets", "#reset-cells", |
61fc4131 PZ |
163 | 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); | |
3d103020 | 179 | return ERR_PTR(-EPROBE_DEFER); |
61fc4131 PZ |
180 | } |
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) { | |
6034bb22 | 193 | module_put(rcdev->owner); |
61fc4131 PZ |
194 | return ERR_PTR(-ENOMEM); |
195 | } | |
196 | ||
61fc4131 PZ |
197 | rstc->rcdev = rcdev; |
198 | rstc->id = rstc_id; | |
199 | ||
200 | return rstc; | |
201 | } | |
c0a13aa6 VH |
202 | EXPORT_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 | */ | |
213 | struct reset_control *of_reset_control_get(struct device_node *node, | |
214 | const char *id) | |
215 | { | |
216 | int index = 0; | |
217 | ||
3d81216f | 218 | if (id) { |
c0a13aa6 VH |
219 | index = of_property_match_string(node, |
220 | "reset-names", id); | |
3d81216f AB |
221 | if (index < 0) |
222 | return ERR_PTR(-ENOENT); | |
223 | } | |
c0a13aa6 VH |
224 | return of_reset_control_get_by_index(node, index); |
225 | } | |
fc0a5921 MR |
226 | EXPORT_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 | */ | |
237 | struct 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 | } | |
61fc4131 PZ |
250 | EXPORT_SYMBOL_GPL(reset_control_get); |
251 | ||
252 | /** | |
253 | * reset_control_put - free the reset controller | |
254 | * @rstc: reset controller | |
255 | */ | |
256 | ||
257 | void 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 | } | |
265 | EXPORT_SYMBOL_GPL(reset_control_put); | |
266 | ||
267 | static 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 | */ | |
281 | struct 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 | } | |
300 | EXPORT_SYMBOL_GPL(devm_reset_control_get); | |
301 | ||
61fc4131 PZ |
302 | /** |
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 | */ | |
311 | int 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 | } | |
326 | EXPORT_SYMBOL_GPL(device_reset); |