1
2
3
4
5
6
7
8
9
10
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>
17#include <linux/of.h>
18#include <linux/reset.h>
19#include <linux/reset-controller.h>
20#include <linux/slab.h>
21
22static DEFINE_MUTEX(reset_list_mutex);
23static LIST_HEAD(reset_controller_list);
24
25
26
27
28
29
30
31
32
33
34
35
36struct reset_control {
37 struct reset_controller_dev *rcdev;
38 struct list_head list;
39 unsigned int id;
40 unsigned int refcnt;
41 int shared;
42 atomic_t deassert_count;
43};
44
45
46
47
48
49
50
51
52
53
54static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
55 const struct of_phandle_args *reset_spec)
56{
57 if (reset_spec->args[0] >= rcdev->nr_resets)
58 return -EINVAL;
59
60 return reset_spec->args[0];
61}
62
63
64
65
66
67int reset_controller_register(struct reset_controller_dev *rcdev)
68{
69 if (!rcdev->of_xlate) {
70 rcdev->of_reset_n_cells = 1;
71 rcdev->of_xlate = of_reset_simple_xlate;
72 }
73
74 INIT_LIST_HEAD(&rcdev->reset_control_head);
75
76 mutex_lock(&reset_list_mutex);
77 list_add(&rcdev->list, &reset_controller_list);
78 mutex_unlock(&reset_list_mutex);
79
80 return 0;
81}
82EXPORT_SYMBOL_GPL(reset_controller_register);
83
84
85
86
87
88void reset_controller_unregister(struct reset_controller_dev *rcdev)
89{
90 mutex_lock(&reset_list_mutex);
91 list_del(&rcdev->list);
92 mutex_unlock(&reset_list_mutex);
93}
94EXPORT_SYMBOL_GPL(reset_controller_unregister);
95
96static void devm_reset_controller_release(struct device *dev, void *res)
97{
98 reset_controller_unregister(*(struct reset_controller_dev **)res);
99}
100
101
102
103
104
105
106
107
108
109
110int devm_reset_controller_register(struct device *dev,
111 struct reset_controller_dev *rcdev)
112{
113 struct reset_controller_dev **rcdevp;
114 int ret;
115
116 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
117 GFP_KERNEL);
118 if (!rcdevp)
119 return -ENOMEM;
120
121 ret = reset_controller_register(rcdev);
122 if (!ret) {
123 *rcdevp = rcdev;
124 devres_add(dev, rcdevp);
125 } else {
126 devres_free(rcdevp);
127 }
128
129 return ret;
130}
131EXPORT_SYMBOL_GPL(devm_reset_controller_register);
132
133
134
135
136
137
138
139int reset_control_reset(struct reset_control *rstc)
140{
141 if (WARN_ON(rstc->shared))
142 return -EINVAL;
143
144 if (rstc->rcdev->ops->reset)
145 return rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
146
147 return -ENOTSUPP;
148}
149EXPORT_SYMBOL_GPL(reset_control_reset);
150
151
152
153
154
155
156
157
158
159
160
161
162int reset_control_assert(struct reset_control *rstc)
163{
164 if (!rstc->rcdev->ops->assert)
165 return -ENOTSUPP;
166
167 if (rstc->shared) {
168 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
169 return -EINVAL;
170
171 if (atomic_dec_return(&rstc->deassert_count) != 0)
172 return 0;
173 }
174
175 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
176}
177EXPORT_SYMBOL_GPL(reset_control_assert);
178
179
180
181
182
183
184
185int reset_control_deassert(struct reset_control *rstc)
186{
187 if (!rstc->rcdev->ops->deassert)
188 return -ENOTSUPP;
189
190 if (rstc->shared) {
191 if (atomic_inc_return(&rstc->deassert_count) != 1)
192 return 0;
193 }
194
195 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
196}
197EXPORT_SYMBOL_GPL(reset_control_deassert);
198
199
200
201
202
203
204
205int reset_control_status(struct reset_control *rstc)
206{
207 if (rstc->rcdev->ops->status)
208 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
209
210 return -ENOTSUPP;
211}
212EXPORT_SYMBOL_GPL(reset_control_status);
213
214static struct reset_control *__reset_control_get(
215 struct reset_controller_dev *rcdev,
216 unsigned int index, int shared)
217{
218 struct reset_control *rstc;
219
220 lockdep_assert_held(&reset_list_mutex);
221
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);
226
227 rstc->refcnt++;
228 return rstc;
229 }
230 }
231
232 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
233 if (!rstc)
234 return ERR_PTR(-ENOMEM);
235
236 try_module_get(rcdev->owner);
237
238 rstc->rcdev = rcdev;
239 list_add(&rstc->list, &rcdev->reset_control_head);
240 rstc->id = index;
241 rstc->refcnt = 1;
242 rstc->shared = shared;
243
244 return rstc;
245}
246
247static void __reset_control_put(struct reset_control *rstc)
248{
249 lockdep_assert_held(&reset_list_mutex);
250
251 if (--rstc->refcnt)
252 return;
253
254 module_put(rstc->rcdev->owner);
255
256 list_del(&rstc->list);
257 kfree(rstc);
258}
259
260struct reset_control *__of_reset_control_get(struct device_node *node,
261 const char *id, int index, int shared)
262{
263 struct reset_control *rstc;
264 struct reset_controller_dev *r, *rcdev;
265 struct of_phandle_args args;
266 int rstc_id;
267 int ret;
268
269 if (!node)
270 return ERR_PTR(-EINVAL);
271
272 if (id) {
273 index = of_property_match_string(node,
274 "reset-names", id);
275 if (index < 0)
276 return ERR_PTR(-ENOENT);
277 }
278
279 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
280 index, &args);
281 if (ret)
282 return ERR_PTR(ret);
283
284 mutex_lock(&reset_list_mutex);
285 rcdev = NULL;
286 list_for_each_entry(r, &reset_controller_list, list) {
287 if (args.np == r->of_node) {
288 rcdev = r;
289 break;
290 }
291 }
292 of_node_put(args.np);
293
294 if (!rcdev) {
295 mutex_unlock(&reset_list_mutex);
296 return ERR_PTR(-EPROBE_DEFER);
297 }
298
299 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
300 mutex_unlock(&reset_list_mutex);
301 return ERR_PTR(-EINVAL);
302 }
303
304 rstc_id = rcdev->of_xlate(rcdev, &args);
305 if (rstc_id < 0) {
306 mutex_unlock(&reset_list_mutex);
307 return ERR_PTR(rstc_id);
308 }
309
310
311 rstc = __reset_control_get(rcdev, rstc_id, shared);
312
313 mutex_unlock(&reset_list_mutex);
314
315 return rstc;
316}
317EXPORT_SYMBOL_GPL(__of_reset_control_get);
318
319
320
321
322
323
324void reset_control_put(struct reset_control *rstc)
325{
326 if (IS_ERR(rstc))
327 return;
328
329 mutex_lock(&reset_list_mutex);
330 __reset_control_put(rstc);
331 mutex_unlock(&reset_list_mutex);
332}
333EXPORT_SYMBOL_GPL(reset_control_put);
334
335static void devm_reset_control_release(struct device *dev, void *res)
336{
337 reset_control_put(*(struct reset_control **)res);
338}
339
340struct reset_control *__devm_reset_control_get(struct device *dev,
341 const char *id, int index, int shared)
342{
343 struct reset_control **ptr, *rstc;
344
345 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
346 GFP_KERNEL);
347 if (!ptr)
348 return ERR_PTR(-ENOMEM);
349
350 rstc = __of_reset_control_get(dev ? dev->of_node : NULL,
351 id, index, shared);
352 if (!IS_ERR(rstc)) {
353 *ptr = rstc;
354 devres_add(dev, ptr);
355 } else {
356 devres_free(ptr);
357 }
358
359 return rstc;
360}
361EXPORT_SYMBOL_GPL(__devm_reset_control_get);
362
363
364
365
366
367
368
369
370
371
372int device_reset(struct device *dev)
373{
374 struct reset_control *rstc;
375 int ret;
376
377 rstc = reset_control_get(dev, NULL);
378 if (IS_ERR(rstc))
379 return PTR_ERR(rstc);
380
381 ret = reset_control_reset(rstc);
382
383 reset_control_put(rstc);
384
385 return ret;
386}
387EXPORT_SYMBOL_GPL(device_reset);
388