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
36
37
38
39struct reset_control {
40 struct reset_controller_dev *rcdev;
41 struct list_head list;
42 unsigned int id;
43 unsigned int refcnt;
44 bool shared;
45 atomic_t deassert_count;
46 atomic_t triggered_count;
47};
48
49
50
51
52
53
54
55
56
57
58static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
59 const struct of_phandle_args *reset_spec)
60{
61 if (reset_spec->args[0] >= rcdev->nr_resets)
62 return -EINVAL;
63
64 return reset_spec->args[0];
65}
66
67
68
69
70
71int reset_controller_register(struct reset_controller_dev *rcdev)
72{
73 if (!rcdev->of_xlate) {
74 rcdev->of_reset_n_cells = 1;
75 rcdev->of_xlate = of_reset_simple_xlate;
76 }
77
78 INIT_LIST_HEAD(&rcdev->reset_control_head);
79
80 mutex_lock(&reset_list_mutex);
81 list_add(&rcdev->list, &reset_controller_list);
82 mutex_unlock(&reset_list_mutex);
83
84 return 0;
85}
86EXPORT_SYMBOL_GPL(reset_controller_register);
87
88
89
90
91
92void reset_controller_unregister(struct reset_controller_dev *rcdev)
93{
94 mutex_lock(&reset_list_mutex);
95 list_del(&rcdev->list);
96 mutex_unlock(&reset_list_mutex);
97}
98EXPORT_SYMBOL_GPL(reset_controller_unregister);
99
100static void devm_reset_controller_release(struct device *dev, void *res)
101{
102 reset_controller_unregister(*(struct reset_controller_dev **)res);
103}
104
105
106
107
108
109
110
111
112
113
114int devm_reset_controller_register(struct device *dev,
115 struct reset_controller_dev *rcdev)
116{
117 struct reset_controller_dev **rcdevp;
118 int ret;
119
120 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
121 GFP_KERNEL);
122 if (!rcdevp)
123 return -ENOMEM;
124
125 ret = reset_controller_register(rcdev);
126 if (!ret) {
127 *rcdevp = rcdev;
128 devres_add(dev, rcdevp);
129 } else {
130 devres_free(rcdevp);
131 }
132
133 return ret;
134}
135EXPORT_SYMBOL_GPL(devm_reset_controller_register);
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150int reset_control_reset(struct reset_control *rstc)
151{
152 int ret;
153
154 if (!rstc)
155 return 0;
156
157 if (WARN_ON(IS_ERR(rstc)))
158 return -EINVAL;
159
160 if (!rstc->rcdev->ops->reset)
161 return -ENOTSUPP;
162
163 if (rstc->shared) {
164 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
165 return -EINVAL;
166
167 if (atomic_inc_return(&rstc->triggered_count) != 1)
168 return 0;
169 }
170
171 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
172 if (rstc->shared && ret)
173 atomic_dec(&rstc->triggered_count);
174
175 return ret;
176}
177EXPORT_SYMBOL_GPL(reset_control_reset);
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196int reset_control_assert(struct reset_control *rstc)
197{
198 if (!rstc)
199 return 0;
200
201 if (WARN_ON(IS_ERR(rstc)))
202 return -EINVAL;
203
204 if (!rstc->rcdev->ops->assert)
205 return -ENOTSUPP;
206
207 if (rstc->shared) {
208 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
209 return -EINVAL;
210
211 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
212 return -EINVAL;
213
214 if (atomic_dec_return(&rstc->deassert_count) != 0)
215 return 0;
216 }
217
218 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
219}
220EXPORT_SYMBOL_GPL(reset_control_assert);
221
222
223
224
225
226
227
228
229
230
231
232
233
234int reset_control_deassert(struct reset_control *rstc)
235{
236 if (!rstc)
237 return 0;
238
239 if (WARN_ON(IS_ERR(rstc)))
240 return -EINVAL;
241
242 if (!rstc->rcdev->ops->deassert)
243 return -ENOTSUPP;
244
245 if (rstc->shared) {
246 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
247 return -EINVAL;
248
249 if (atomic_inc_return(&rstc->deassert_count) != 1)
250 return 0;
251 }
252
253 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
254}
255EXPORT_SYMBOL_GPL(reset_control_deassert);
256
257
258
259
260
261
262
263int reset_control_status(struct reset_control *rstc)
264{
265 if (!rstc)
266 return 0;
267
268 if (WARN_ON(IS_ERR(rstc)))
269 return -EINVAL;
270
271 if (rstc->rcdev->ops->status)
272 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
273
274 return -ENOTSUPP;
275}
276EXPORT_SYMBOL_GPL(reset_control_status);
277
278static struct reset_control *__reset_control_get_internal(
279 struct reset_controller_dev *rcdev,
280 unsigned int index, bool shared)
281{
282 struct reset_control *rstc;
283
284 lockdep_assert_held(&reset_list_mutex);
285
286 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
287 if (rstc->id == index) {
288 if (WARN_ON(!rstc->shared || !shared))
289 return ERR_PTR(-EBUSY);
290
291 rstc->refcnt++;
292 return rstc;
293 }
294 }
295
296 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
297 if (!rstc)
298 return ERR_PTR(-ENOMEM);
299
300 try_module_get(rcdev->owner);
301
302 rstc->rcdev = rcdev;
303 list_add(&rstc->list, &rcdev->reset_control_head);
304 rstc->id = index;
305 rstc->refcnt = 1;
306 rstc->shared = shared;
307
308 return rstc;
309}
310
311static void __reset_control_put_internal(struct reset_control *rstc)
312{
313 lockdep_assert_held(&reset_list_mutex);
314
315 if (--rstc->refcnt)
316 return;
317
318 module_put(rstc->rcdev->owner);
319
320 list_del(&rstc->list);
321 kfree(rstc);
322}
323
324struct reset_control *__of_reset_control_get(struct device_node *node,
325 const char *id, int index, bool shared,
326 bool optional)
327{
328 struct reset_control *rstc;
329 struct reset_controller_dev *r, *rcdev;
330 struct of_phandle_args args;
331 int rstc_id;
332 int ret;
333
334 if (!node)
335 return ERR_PTR(-EINVAL);
336
337 if (id) {
338 index = of_property_match_string(node,
339 "reset-names", id);
340 if (index == -EILSEQ)
341 return ERR_PTR(index);
342 if (index < 0)
343 return optional ? NULL : ERR_PTR(-ENOENT);
344 }
345
346 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
347 index, &args);
348 if (ret == -EINVAL)
349 return ERR_PTR(ret);
350 if (ret)
351 return optional ? NULL : ERR_PTR(ret);
352
353 mutex_lock(&reset_list_mutex);
354 rcdev = NULL;
355 list_for_each_entry(r, &reset_controller_list, list) {
356 if (args.np == r->of_node) {
357 rcdev = r;
358 break;
359 }
360 }
361 of_node_put(args.np);
362
363 if (!rcdev) {
364 mutex_unlock(&reset_list_mutex);
365 return ERR_PTR(-EPROBE_DEFER);
366 }
367
368 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
369 mutex_unlock(&reset_list_mutex);
370 return ERR_PTR(-EINVAL);
371 }
372
373 rstc_id = rcdev->of_xlate(rcdev, &args);
374 if (rstc_id < 0) {
375 mutex_unlock(&reset_list_mutex);
376 return ERR_PTR(rstc_id);
377 }
378
379
380 rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
381
382 mutex_unlock(&reset_list_mutex);
383
384 return rstc;
385}
386EXPORT_SYMBOL_GPL(__of_reset_control_get);
387
388struct reset_control *__reset_control_get(struct device *dev, const char *id,
389 int index, bool shared, bool optional)
390{
391 if (dev->of_node)
392 return __of_reset_control_get(dev->of_node, id, index, shared,
393 optional);
394
395 return optional ? NULL : ERR_PTR(-EINVAL);
396}
397EXPORT_SYMBOL_GPL(__reset_control_get);
398
399
400
401
402
403
404void reset_control_put(struct reset_control *rstc)
405{
406 if (IS_ERR_OR_NULL(rstc))
407 return;
408
409 mutex_lock(&reset_list_mutex);
410 __reset_control_put_internal(rstc);
411 mutex_unlock(&reset_list_mutex);
412}
413EXPORT_SYMBOL_GPL(reset_control_put);
414
415static void devm_reset_control_release(struct device *dev, void *res)
416{
417 reset_control_put(*(struct reset_control **)res);
418}
419
420struct reset_control *__devm_reset_control_get(struct device *dev,
421 const char *id, int index, bool shared,
422 bool optional)
423{
424 struct reset_control **ptr, *rstc;
425
426 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
427 GFP_KERNEL);
428 if (!ptr)
429 return ERR_PTR(-ENOMEM);
430
431 rstc = __reset_control_get(dev, id, index, shared, optional);
432 if (!IS_ERR(rstc)) {
433 *ptr = rstc;
434 devres_add(dev, ptr);
435 } else {
436 devres_free(ptr);
437 }
438
439 return rstc;
440}
441EXPORT_SYMBOL_GPL(__devm_reset_control_get);
442
443
444
445
446
447
448
449
450
451
452int device_reset(struct device *dev)
453{
454 struct reset_control *rstc;
455 int ret;
456
457 rstc = reset_control_get(dev, NULL);
458 if (IS_ERR(rstc))
459 return PTR_ERR(rstc);
460
461 ret = reset_control_reset(rstc);
462
463 reset_control_put(rstc);
464
465 return ret;
466}
467EXPORT_SYMBOL_GPL(device_reset);
468