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/kref.h>
17#include <linux/module.h>
18#include <linux/of.h>
19#include <linux/reset.h>
20#include <linux/reset-controller.h>
21#include <linux/slab.h>
22
23static DEFINE_MUTEX(reset_list_mutex);
24static LIST_HEAD(reset_controller_list);
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40struct reset_control {
41 struct reset_controller_dev *rcdev;
42 struct list_head list;
43 unsigned int id;
44 struct kref refcnt;
45 bool shared;
46 bool array;
47 atomic_t deassert_count;
48 atomic_t triggered_count;
49};
50
51
52
53
54
55
56
57struct reset_control_array {
58 struct reset_control base;
59 unsigned int num_rstcs;
60 struct reset_control *rstc[];
61};
62
63
64
65
66
67
68
69
70
71
72static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
73 const struct of_phandle_args *reset_spec)
74{
75 if (reset_spec->args[0] >= rcdev->nr_resets)
76 return -EINVAL;
77
78 return reset_spec->args[0];
79}
80
81
82
83
84
85int reset_controller_register(struct reset_controller_dev *rcdev)
86{
87 if (!rcdev->of_xlate) {
88 rcdev->of_reset_n_cells = 1;
89 rcdev->of_xlate = of_reset_simple_xlate;
90 }
91
92 INIT_LIST_HEAD(&rcdev->reset_control_head);
93
94 mutex_lock(&reset_list_mutex);
95 list_add(&rcdev->list, &reset_controller_list);
96 mutex_unlock(&reset_list_mutex);
97
98 return 0;
99}
100EXPORT_SYMBOL_GPL(reset_controller_register);
101
102
103
104
105
106void reset_controller_unregister(struct reset_controller_dev *rcdev)
107{
108 mutex_lock(&reset_list_mutex);
109 list_del(&rcdev->list);
110 mutex_unlock(&reset_list_mutex);
111}
112EXPORT_SYMBOL_GPL(reset_controller_unregister);
113
114static void devm_reset_controller_release(struct device *dev, void *res)
115{
116 reset_controller_unregister(*(struct reset_controller_dev **)res);
117}
118
119
120
121
122
123
124
125
126
127
128int devm_reset_controller_register(struct device *dev,
129 struct reset_controller_dev *rcdev)
130{
131 struct reset_controller_dev **rcdevp;
132 int ret;
133
134 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
135 GFP_KERNEL);
136 if (!rcdevp)
137 return -ENOMEM;
138
139 ret = reset_controller_register(rcdev);
140 if (!ret) {
141 *rcdevp = rcdev;
142 devres_add(dev, rcdevp);
143 } else {
144 devres_free(rcdevp);
145 }
146
147 return ret;
148}
149EXPORT_SYMBOL_GPL(devm_reset_controller_register);
150
151static inline struct reset_control_array *
152rstc_to_array(struct reset_control *rstc) {
153 return container_of(rstc, struct reset_control_array, base);
154}
155
156static int reset_control_array_reset(struct reset_control_array *resets)
157{
158 int ret, i;
159
160 for (i = 0; i < resets->num_rstcs; i++) {
161 ret = reset_control_reset(resets->rstc[i]);
162 if (ret)
163 return ret;
164 }
165
166 return 0;
167}
168
169static int reset_control_array_assert(struct reset_control_array *resets)
170{
171 int ret, i;
172
173 for (i = 0; i < resets->num_rstcs; i++) {
174 ret = reset_control_assert(resets->rstc[i]);
175 if (ret)
176 goto err;
177 }
178
179 return 0;
180
181err:
182 while (i--)
183 reset_control_deassert(resets->rstc[i]);
184 return ret;
185}
186
187static int reset_control_array_deassert(struct reset_control_array *resets)
188{
189 int ret, i;
190
191 for (i = 0; i < resets->num_rstcs; i++) {
192 ret = reset_control_deassert(resets->rstc[i]);
193 if (ret)
194 goto err;
195 }
196
197 return 0;
198
199err:
200 while (i--)
201 reset_control_assert(resets->rstc[i]);
202 return ret;
203}
204
205static inline bool reset_control_is_array(struct reset_control *rstc)
206{
207 return rstc->array;
208}
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223int reset_control_reset(struct reset_control *rstc)
224{
225 int ret;
226
227 if (!rstc)
228 return 0;
229
230 if (WARN_ON(IS_ERR(rstc)))
231 return -EINVAL;
232
233 if (reset_control_is_array(rstc))
234 return reset_control_array_reset(rstc_to_array(rstc));
235
236 if (!rstc->rcdev->ops->reset)
237 return -ENOTSUPP;
238
239 if (rstc->shared) {
240 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
241 return -EINVAL;
242
243 if (atomic_inc_return(&rstc->triggered_count) != 1)
244 return 0;
245 }
246
247 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
248 if (rstc->shared && ret)
249 atomic_dec(&rstc->triggered_count);
250
251 return ret;
252}
253EXPORT_SYMBOL_GPL(reset_control_reset);
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272int reset_control_assert(struct reset_control *rstc)
273{
274 if (!rstc)
275 return 0;
276
277 if (WARN_ON(IS_ERR(rstc)))
278 return -EINVAL;
279
280 if (reset_control_is_array(rstc))
281 return reset_control_array_assert(rstc_to_array(rstc));
282
283 if (rstc->shared) {
284 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
285 return -EINVAL;
286
287 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
288 return -EINVAL;
289
290 if (atomic_dec_return(&rstc->deassert_count) != 0)
291 return 0;
292
293
294
295
296
297 if (!rstc->rcdev->ops->assert)
298 return 0;
299 } else {
300
301
302
303
304
305 if (!rstc->rcdev->ops->assert)
306 return -ENOTSUPP;
307 }
308
309 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
310}
311EXPORT_SYMBOL_GPL(reset_control_assert);
312
313
314
315
316
317
318
319
320
321
322
323
324
325int reset_control_deassert(struct reset_control *rstc)
326{
327 if (!rstc)
328 return 0;
329
330 if (WARN_ON(IS_ERR(rstc)))
331 return -EINVAL;
332
333 if (reset_control_is_array(rstc))
334 return reset_control_array_deassert(rstc_to_array(rstc));
335
336 if (rstc->shared) {
337 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
338 return -EINVAL;
339
340 if (atomic_inc_return(&rstc->deassert_count) != 1)
341 return 0;
342 }
343
344
345
346
347
348
349
350
351 if (!rstc->rcdev->ops->deassert)
352 return 0;
353
354 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
355}
356EXPORT_SYMBOL_GPL(reset_control_deassert);
357
358
359
360
361
362
363
364int reset_control_status(struct reset_control *rstc)
365{
366 if (!rstc)
367 return 0;
368
369 if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
370 return -EINVAL;
371
372 if (rstc->rcdev->ops->status)
373 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
374
375 return -ENOTSUPP;
376}
377EXPORT_SYMBOL_GPL(reset_control_status);
378
379static struct reset_control *__reset_control_get_internal(
380 struct reset_controller_dev *rcdev,
381 unsigned int index, bool shared)
382{
383 struct reset_control *rstc;
384
385 lockdep_assert_held(&reset_list_mutex);
386
387 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
388 if (rstc->id == index) {
389 if (WARN_ON(!rstc->shared || !shared))
390 return ERR_PTR(-EBUSY);
391
392 kref_get(&rstc->refcnt);
393 return rstc;
394 }
395 }
396
397 rstc = kzalloc(sizeof(*rstc), GFP_KERNEL);
398 if (!rstc)
399 return ERR_PTR(-ENOMEM);
400
401 try_module_get(rcdev->owner);
402
403 rstc->rcdev = rcdev;
404 list_add(&rstc->list, &rcdev->reset_control_head);
405 rstc->id = index;
406 kref_init(&rstc->refcnt);
407 rstc->shared = shared;
408
409 return rstc;
410}
411
412static void __reset_control_release(struct kref *kref)
413{
414 struct reset_control *rstc = container_of(kref, struct reset_control,
415 refcnt);
416
417 lockdep_assert_held(&reset_list_mutex);
418
419 module_put(rstc->rcdev->owner);
420
421 list_del(&rstc->list);
422 kfree(rstc);
423}
424
425static void __reset_control_put_internal(struct reset_control *rstc)
426{
427 lockdep_assert_held(&reset_list_mutex);
428
429 kref_put(&rstc->refcnt, __reset_control_release);
430}
431
432struct reset_control *__of_reset_control_get(struct device_node *node,
433 const char *id, int index, bool shared,
434 bool optional)
435{
436 struct reset_control *rstc;
437 struct reset_controller_dev *r, *rcdev;
438 struct of_phandle_args args;
439 int rstc_id;
440 int ret;
441
442 if (!node)
443 return ERR_PTR(-EINVAL);
444
445 if (id) {
446 index = of_property_match_string(node,
447 "reset-names", id);
448 if (index == -EILSEQ)
449 return ERR_PTR(index);
450 if (index < 0)
451 return optional ? NULL : ERR_PTR(-ENOENT);
452 }
453
454 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
455 index, &args);
456 if (ret == -EINVAL)
457 return ERR_PTR(ret);
458 if (ret)
459 return optional ? NULL : ERR_PTR(ret);
460
461 mutex_lock(&reset_list_mutex);
462 rcdev = NULL;
463 list_for_each_entry(r, &reset_controller_list, list) {
464 if (args.np == r->of_node) {
465 rcdev = r;
466 break;
467 }
468 }
469 of_node_put(args.np);
470
471 if (!rcdev) {
472 mutex_unlock(&reset_list_mutex);
473 return ERR_PTR(-EPROBE_DEFER);
474 }
475
476 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
477 mutex_unlock(&reset_list_mutex);
478 return ERR_PTR(-EINVAL);
479 }
480
481 rstc_id = rcdev->of_xlate(rcdev, &args);
482 if (rstc_id < 0) {
483 mutex_unlock(&reset_list_mutex);
484 return ERR_PTR(rstc_id);
485 }
486
487
488 rstc = __reset_control_get_internal(rcdev, rstc_id, shared);
489
490 mutex_unlock(&reset_list_mutex);
491
492 return rstc;
493}
494EXPORT_SYMBOL_GPL(__of_reset_control_get);
495
496struct reset_control *__reset_control_get(struct device *dev, const char *id,
497 int index, bool shared, bool optional)
498{
499 if (dev->of_node)
500 return __of_reset_control_get(dev->of_node, id, index, shared,
501 optional);
502
503 return optional ? NULL : ERR_PTR(-EINVAL);
504}
505EXPORT_SYMBOL_GPL(__reset_control_get);
506
507static void reset_control_array_put(struct reset_control_array *resets)
508{
509 int i;
510
511 mutex_lock(&reset_list_mutex);
512 for (i = 0; i < resets->num_rstcs; i++)
513 __reset_control_put_internal(resets->rstc[i]);
514 mutex_unlock(&reset_list_mutex);
515}
516
517
518
519
520
521void reset_control_put(struct reset_control *rstc)
522{
523 if (IS_ERR_OR_NULL(rstc))
524 return;
525
526 if (reset_control_is_array(rstc)) {
527 reset_control_array_put(rstc_to_array(rstc));
528 return;
529 }
530
531 mutex_lock(&reset_list_mutex);
532 __reset_control_put_internal(rstc);
533 mutex_unlock(&reset_list_mutex);
534}
535EXPORT_SYMBOL_GPL(reset_control_put);
536
537static void devm_reset_control_release(struct device *dev, void *res)
538{
539 reset_control_put(*(struct reset_control **)res);
540}
541
542struct reset_control *__devm_reset_control_get(struct device *dev,
543 const char *id, int index, bool shared,
544 bool optional)
545{
546 struct reset_control **ptr, *rstc;
547
548 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
549 GFP_KERNEL);
550 if (!ptr)
551 return ERR_PTR(-ENOMEM);
552
553 rstc = __reset_control_get(dev, id, index, shared, optional);
554 if (!IS_ERR(rstc)) {
555 *ptr = rstc;
556 devres_add(dev, ptr);
557 } else {
558 devres_free(ptr);
559 }
560
561 return rstc;
562}
563EXPORT_SYMBOL_GPL(__devm_reset_control_get);
564
565
566
567
568
569
570
571
572
573
574int device_reset(struct device *dev)
575{
576 struct reset_control *rstc;
577 int ret;
578
579 rstc = reset_control_get(dev, NULL);
580 if (IS_ERR(rstc))
581 return PTR_ERR(rstc);
582
583 ret = reset_control_reset(rstc);
584
585 reset_control_put(rstc);
586
587 return ret;
588}
589EXPORT_SYMBOL_GPL(device_reset);
590
591
592
593
594
595
596
597
598
599
600
601
602static int of_reset_control_get_count(struct device_node *node)
603{
604 int count;
605
606 if (!node)
607 return -EINVAL;
608
609 count = of_count_phandle_with_args(node, "resets", "#reset-cells");
610 if (count == 0)
611 count = -ENOENT;
612
613 return count;
614}
615
616
617
618
619
620
621
622
623
624
625
626
627struct reset_control *
628of_reset_control_array_get(struct device_node *np, bool shared, bool optional)
629{
630 struct reset_control_array *resets;
631 struct reset_control *rstc;
632 int num, i;
633
634 num = of_reset_control_get_count(np);
635 if (num < 0)
636 return optional ? NULL : ERR_PTR(num);
637
638 resets = kzalloc(sizeof(*resets) + sizeof(resets->rstc[0]) * num,
639 GFP_KERNEL);
640 if (!resets)
641 return ERR_PTR(-ENOMEM);
642
643 for (i = 0; i < num; i++) {
644 rstc = __of_reset_control_get(np, NULL, i, shared, optional);
645 if (IS_ERR(rstc))
646 goto err_rst;
647 resets->rstc[i] = rstc;
648 }
649 resets->num_rstcs = num;
650 resets->base.array = true;
651
652 return &resets->base;
653
654err_rst:
655 mutex_lock(&reset_list_mutex);
656 while (--i >= 0)
657 __reset_control_put_internal(resets->rstc[i]);
658 mutex_unlock(&reset_list_mutex);
659
660 kfree(resets);
661
662 return rstc;
663}
664EXPORT_SYMBOL_GPL(of_reset_control_array_get);
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680struct reset_control *
681devm_reset_control_array_get(struct device *dev, bool shared, bool optional)
682{
683 struct reset_control **devres;
684 struct reset_control *rstc;
685
686 devres = devres_alloc(devm_reset_control_release, sizeof(*devres),
687 GFP_KERNEL);
688 if (!devres)
689 return ERR_PTR(-ENOMEM);
690
691 rstc = of_reset_control_array_get(dev->of_node, shared, optional);
692 if (IS_ERR(rstc)) {
693 devres_free(devres);
694 return rstc;
695 }
696
697 *devres = rstc;
698 devres_add(dev, devres);
699
700 return rstc;
701}
702EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
703