1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Reset Controller framework
4 *
5 * Copyright 2013 Philipp Zabel, Pengutronix
6 */
7
8 #include <linux/acpi.h>
9 #include <linux/atomic.h>
10 #include <linux/auxiliary_bus.h>
11 #include <linux/cleanup.h>
12 #include <linux/device.h>
13 #include <linux/err.h>
14 #include <linux/export.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/gpio/machine.h>
17 #include <linux/gpio/property.h>
18 #include <linux/idr.h>
19 #include <linux/kernel.h>
20 #include <linux/kref.h>
21 #include <linux/module.h>
22 #include <linux/of.h>
23 #include <linux/reset.h>
24 #include <linux/reset-controller.h>
25 #include <linux/slab.h>
26
27 static DEFINE_MUTEX(reset_list_mutex);
28 static LIST_HEAD(reset_controller_list);
29
30 /* Protects reset_gpio_lookup_list */
31 static DEFINE_MUTEX(reset_gpio_lookup_mutex);
32 static LIST_HEAD(reset_gpio_lookup_list);
33 static DEFINE_IDA(reset_gpio_ida);
34
35 /**
36 * struct reset_control - a reset control
37 * @rcdev: a pointer to the reset controller device
38 * this reset control belongs to
39 * @list: list entry for the rcdev's reset controller list
40 * @id: ID of the reset controller in the reset
41 * controller device
42 * @refcnt: Number of gets of this reset_control
43 * @acquired: Only one reset_control may be acquired for a given rcdev and id.
44 * @shared: Is this a shared (1), or an exclusive (0) reset_control?
45 * @array: Is this an array of reset controls (1)?
46 * @deassert_count: Number of times this reset line has been deasserted
47 * @triggered_count: Number of times this reset line has been reset. Currently
48 * only used for shared resets, which means that the value
49 * will be either 0 or 1.
50 */
51 struct reset_control {
52 struct reset_controller_dev *rcdev;
53 struct list_head list;
54 unsigned int id;
55 struct kref refcnt;
56 bool acquired;
57 bool shared;
58 bool array;
59 atomic_t deassert_count;
60 atomic_t triggered_count;
61 };
62
63 /**
64 * struct reset_control_array - an array of reset controls
65 * @base: reset control for compatibility with reset control API functions
66 * @num_rstcs: number of reset controls
67 * @rstc: array of reset controls
68 */
69 struct reset_control_array {
70 struct reset_control base;
71 unsigned int num_rstcs;
72 struct reset_control *rstc[] __counted_by(num_rstcs);
73 };
74
75 /**
76 * struct reset_gpio_lookup - lookup key for ad-hoc created reset-gpio devices
77 * @of_args: phandle to the reset controller with all the args like GPIO number
78 * @swnode: Software node containing the reference to the GPIO provider
79 * @list: list entry for the reset_gpio_lookup_list
80 */
81 struct reset_gpio_lookup {
82 struct of_phandle_args of_args;
83 struct fwnode_handle *swnode;
84 struct list_head list;
85 };
86
rcdev_name(struct reset_controller_dev * rcdev)87 static const char *rcdev_name(struct reset_controller_dev *rcdev)
88 {
89 if (rcdev->dev)
90 return dev_name(rcdev->dev);
91
92 if (rcdev->of_node)
93 return rcdev->of_node->full_name;
94
95 if (rcdev->of_args)
96 return rcdev->of_args->np->full_name;
97
98 return NULL;
99 }
100
101 /**
102 * of_reset_simple_xlate - translate reset_spec to the reset line number
103 * @rcdev: a pointer to the reset controller device
104 * @reset_spec: reset line specifier as found in the device tree
105 *
106 * This static translation function is used by default if of_xlate in
107 * :c:type:`reset_controller_dev` is not set. It is useful for all reset
108 * controllers with 1:1 mapping, where reset lines can be indexed by number
109 * without gaps.
110 */
of_reset_simple_xlate(struct reset_controller_dev * rcdev,const struct of_phandle_args * reset_spec)111 static int of_reset_simple_xlate(struct reset_controller_dev *rcdev,
112 const struct of_phandle_args *reset_spec)
113 {
114 if (reset_spec->args[0] >= rcdev->nr_resets)
115 return -EINVAL;
116
117 return reset_spec->args[0];
118 }
119
120 /**
121 * reset_controller_register - register a reset controller device
122 * @rcdev: a pointer to the initialized reset controller device
123 */
reset_controller_register(struct reset_controller_dev * rcdev)124 int reset_controller_register(struct reset_controller_dev *rcdev)
125 {
126 if (rcdev->of_node && rcdev->of_args)
127 return -EINVAL;
128
129 if (!rcdev->of_xlate) {
130 rcdev->of_reset_n_cells = 1;
131 rcdev->of_xlate = of_reset_simple_xlate;
132 }
133
134 INIT_LIST_HEAD(&rcdev->reset_control_head);
135
136 mutex_lock(&reset_list_mutex);
137 list_add(&rcdev->list, &reset_controller_list);
138 mutex_unlock(&reset_list_mutex);
139
140 return 0;
141 }
142 EXPORT_SYMBOL_GPL(reset_controller_register);
143
144 /**
145 * reset_controller_unregister - unregister a reset controller device
146 * @rcdev: a pointer to the reset controller device
147 */
reset_controller_unregister(struct reset_controller_dev * rcdev)148 void reset_controller_unregister(struct reset_controller_dev *rcdev)
149 {
150 mutex_lock(&reset_list_mutex);
151 list_del(&rcdev->list);
152 mutex_unlock(&reset_list_mutex);
153 }
154 EXPORT_SYMBOL_GPL(reset_controller_unregister);
155
devm_reset_controller_release(struct device * dev,void * res)156 static void devm_reset_controller_release(struct device *dev, void *res)
157 {
158 reset_controller_unregister(*(struct reset_controller_dev **)res);
159 }
160
161 /**
162 * devm_reset_controller_register - resource managed reset_controller_register()
163 * @dev: device that is registering this reset controller
164 * @rcdev: a pointer to the initialized reset controller device
165 *
166 * Managed reset_controller_register(). For reset controllers registered by
167 * this function, reset_controller_unregister() is automatically called on
168 * driver detach. See reset_controller_register() for more information.
169 */
devm_reset_controller_register(struct device * dev,struct reset_controller_dev * rcdev)170 int devm_reset_controller_register(struct device *dev,
171 struct reset_controller_dev *rcdev)
172 {
173 struct reset_controller_dev **rcdevp;
174 int ret;
175
176 rcdevp = devres_alloc(devm_reset_controller_release, sizeof(*rcdevp),
177 GFP_KERNEL);
178 if (!rcdevp)
179 return -ENOMEM;
180
181 ret = reset_controller_register(rcdev);
182 if (ret) {
183 devres_free(rcdevp);
184 return ret;
185 }
186
187 *rcdevp = rcdev;
188 devres_add(dev, rcdevp);
189
190 return ret;
191 }
192 EXPORT_SYMBOL_GPL(devm_reset_controller_register);
193
194 static inline struct reset_control_array *
rstc_to_array(struct reset_control * rstc)195 rstc_to_array(struct reset_control *rstc) {
196 return container_of(rstc, struct reset_control_array, base);
197 }
198
reset_control_array_reset(struct reset_control_array * resets)199 static int reset_control_array_reset(struct reset_control_array *resets)
200 {
201 int ret, i;
202
203 for (i = 0; i < resets->num_rstcs; i++) {
204 ret = reset_control_reset(resets->rstc[i]);
205 if (ret)
206 return ret;
207 }
208
209 return 0;
210 }
211
reset_control_array_rearm(struct reset_control_array * resets)212 static int reset_control_array_rearm(struct reset_control_array *resets)
213 {
214 struct reset_control *rstc;
215 int i;
216
217 for (i = 0; i < resets->num_rstcs; i++) {
218 rstc = resets->rstc[i];
219
220 if (!rstc)
221 continue;
222
223 if (WARN_ON(IS_ERR(rstc)))
224 return -EINVAL;
225
226 if (rstc->shared) {
227 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
228 return -EINVAL;
229 } else {
230 if (!rstc->acquired)
231 return -EPERM;
232 }
233 }
234
235 for (i = 0; i < resets->num_rstcs; i++) {
236 rstc = resets->rstc[i];
237
238 if (rstc && rstc->shared)
239 WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0);
240 }
241
242 return 0;
243 }
244
reset_control_array_assert(struct reset_control_array * resets)245 static int reset_control_array_assert(struct reset_control_array *resets)
246 {
247 int ret, i;
248
249 for (i = 0; i < resets->num_rstcs; i++) {
250 ret = reset_control_assert(resets->rstc[i]);
251 if (ret)
252 goto err;
253 }
254
255 return 0;
256
257 err:
258 while (i--)
259 reset_control_deassert(resets->rstc[i]);
260 return ret;
261 }
262
reset_control_array_deassert(struct reset_control_array * resets)263 static int reset_control_array_deassert(struct reset_control_array *resets)
264 {
265 int ret, i;
266
267 for (i = 0; i < resets->num_rstcs; i++) {
268 ret = reset_control_deassert(resets->rstc[i]);
269 if (ret)
270 goto err;
271 }
272
273 return 0;
274
275 err:
276 while (i--)
277 reset_control_assert(resets->rstc[i]);
278 return ret;
279 }
280
reset_control_array_acquire(struct reset_control_array * resets)281 static int reset_control_array_acquire(struct reset_control_array *resets)
282 {
283 unsigned int i;
284 int err;
285
286 for (i = 0; i < resets->num_rstcs; i++) {
287 err = reset_control_acquire(resets->rstc[i]);
288 if (err < 0)
289 goto release;
290 }
291
292 return 0;
293
294 release:
295 while (i--)
296 reset_control_release(resets->rstc[i]);
297
298 return err;
299 }
300
reset_control_array_release(struct reset_control_array * resets)301 static void reset_control_array_release(struct reset_control_array *resets)
302 {
303 unsigned int i;
304
305 for (i = 0; i < resets->num_rstcs; i++)
306 reset_control_release(resets->rstc[i]);
307 }
308
reset_control_is_array(struct reset_control * rstc)309 static inline bool reset_control_is_array(struct reset_control *rstc)
310 {
311 return rstc->array;
312 }
313
314 /**
315 * reset_control_reset - reset the controlled device
316 * @rstc: reset controller
317 *
318 * On a shared reset line the actual reset pulse is only triggered once for the
319 * lifetime of the reset_control instance: for all but the first caller this is
320 * a no-op.
321 * Consumers must not use reset_control_(de)assert on shared reset lines when
322 * reset_control_reset has been used.
323 *
324 * If rstc is NULL it is an optional reset and the function will just
325 * return 0.
326 */
reset_control_reset(struct reset_control * rstc)327 int reset_control_reset(struct reset_control *rstc)
328 {
329 int ret;
330
331 if (!rstc)
332 return 0;
333
334 if (WARN_ON(IS_ERR(rstc)))
335 return -EINVAL;
336
337 if (reset_control_is_array(rstc))
338 return reset_control_array_reset(rstc_to_array(rstc));
339
340 if (!rstc->rcdev->ops->reset)
341 return -ENOTSUPP;
342
343 if (rstc->shared) {
344 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
345 return -EINVAL;
346
347 if (atomic_inc_return(&rstc->triggered_count) != 1)
348 return 0;
349 } else {
350 if (!rstc->acquired)
351 return -EPERM;
352 }
353
354 ret = rstc->rcdev->ops->reset(rstc->rcdev, rstc->id);
355 if (rstc->shared && ret)
356 atomic_dec(&rstc->triggered_count);
357
358 return ret;
359 }
360 EXPORT_SYMBOL_GPL(reset_control_reset);
361
362 /**
363 * reset_control_bulk_reset - reset the controlled devices in order
364 * @num_rstcs: number of entries in rstcs array
365 * @rstcs: array of struct reset_control_bulk_data with reset controls set
366 *
367 * Issue a reset on all provided reset controls, in order.
368 *
369 * See also: reset_control_reset()
370 */
reset_control_bulk_reset(int num_rstcs,struct reset_control_bulk_data * rstcs)371 int reset_control_bulk_reset(int num_rstcs,
372 struct reset_control_bulk_data *rstcs)
373 {
374 int ret, i;
375
376 for (i = 0; i < num_rstcs; i++) {
377 ret = reset_control_reset(rstcs[i].rstc);
378 if (ret)
379 return ret;
380 }
381
382 return 0;
383 }
384 EXPORT_SYMBOL_GPL(reset_control_bulk_reset);
385
386 /**
387 * reset_control_rearm - allow shared reset line to be re-triggered"
388 * @rstc: reset controller
389 *
390 * On a shared reset line the actual reset pulse is only triggered once for the
391 * lifetime of the reset_control instance, except if this call is used.
392 *
393 * Calls to this function must be balanced with calls to reset_control_reset,
394 * a warning is thrown in case triggered_count ever dips below 0.
395 *
396 * Consumers must not use reset_control_(de)assert on shared reset lines when
397 * reset_control_reset or reset_control_rearm have been used.
398 *
399 * If rstc is NULL the function will just return 0.
400 */
reset_control_rearm(struct reset_control * rstc)401 int reset_control_rearm(struct reset_control *rstc)
402 {
403 if (!rstc)
404 return 0;
405
406 if (WARN_ON(IS_ERR(rstc)))
407 return -EINVAL;
408
409 if (reset_control_is_array(rstc))
410 return reset_control_array_rearm(rstc_to_array(rstc));
411
412 if (rstc->shared) {
413 if (WARN_ON(atomic_read(&rstc->deassert_count) != 0))
414 return -EINVAL;
415
416 WARN_ON(atomic_dec_return(&rstc->triggered_count) < 0);
417 } else {
418 if (!rstc->acquired)
419 return -EPERM;
420 }
421
422 return 0;
423 }
424 EXPORT_SYMBOL_GPL(reset_control_rearm);
425
426 /**
427 * reset_control_assert - asserts the reset line
428 * @rstc: reset controller
429 *
430 * Calling this on an exclusive reset controller guarantees that the reset
431 * will be asserted. When called on a shared reset controller the line may
432 * still be deasserted, as long as other users keep it so.
433 *
434 * For shared reset controls a driver cannot expect the hw's registers and
435 * internal state to be reset, but must be prepared for this to happen.
436 * Consumers must not use reset_control_reset on shared reset lines when
437 * reset_control_(de)assert has been used.
438 *
439 * If rstc is NULL it is an optional reset and the function will just
440 * return 0.
441 */
reset_control_assert(struct reset_control * rstc)442 int reset_control_assert(struct reset_control *rstc)
443 {
444 if (!rstc)
445 return 0;
446
447 if (WARN_ON(IS_ERR(rstc)))
448 return -EINVAL;
449
450 if (reset_control_is_array(rstc))
451 return reset_control_array_assert(rstc_to_array(rstc));
452
453 if (rstc->shared) {
454 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
455 return -EINVAL;
456
457 if (WARN_ON(atomic_read(&rstc->deassert_count) == 0))
458 return -EINVAL;
459
460 if (atomic_dec_return(&rstc->deassert_count) != 0)
461 return 0;
462
463 /*
464 * Shared reset controls allow the reset line to be in any state
465 * after this call, so doing nothing is a valid option.
466 */
467 if (!rstc->rcdev->ops->assert)
468 return 0;
469 } else {
470 /*
471 * If the reset controller does not implement .assert(), there
472 * is no way to guarantee that the reset line is asserted after
473 * this call.
474 */
475 if (!rstc->rcdev->ops->assert)
476 return -ENOTSUPP;
477
478 if (!rstc->acquired) {
479 WARN(1, "reset %s (ID: %u) is not acquired\n",
480 rcdev_name(rstc->rcdev), rstc->id);
481 return -EPERM;
482 }
483 }
484
485 return rstc->rcdev->ops->assert(rstc->rcdev, rstc->id);
486 }
487 EXPORT_SYMBOL_GPL(reset_control_assert);
488
489 /**
490 * reset_control_bulk_assert - asserts the reset lines in order
491 * @num_rstcs: number of entries in rstcs array
492 * @rstcs: array of struct reset_control_bulk_data with reset controls set
493 *
494 * Assert the reset lines for all provided reset controls, in order.
495 * If an assertion fails, already asserted resets are deasserted again.
496 *
497 * See also: reset_control_assert()
498 */
reset_control_bulk_assert(int num_rstcs,struct reset_control_bulk_data * rstcs)499 int reset_control_bulk_assert(int num_rstcs,
500 struct reset_control_bulk_data *rstcs)
501 {
502 int ret, i;
503
504 for (i = 0; i < num_rstcs; i++) {
505 ret = reset_control_assert(rstcs[i].rstc);
506 if (ret)
507 goto err;
508 }
509
510 return 0;
511
512 err:
513 while (i--)
514 reset_control_deassert(rstcs[i].rstc);
515 return ret;
516 }
517 EXPORT_SYMBOL_GPL(reset_control_bulk_assert);
518
519 /**
520 * reset_control_deassert - deasserts the reset line
521 * @rstc: reset controller
522 *
523 * After calling this function, the reset is guaranteed to be deasserted.
524 * Consumers must not use reset_control_reset on shared reset lines when
525 * reset_control_(de)assert has been used.
526 *
527 * If rstc is NULL it is an optional reset and the function will just
528 * return 0.
529 */
reset_control_deassert(struct reset_control * rstc)530 int reset_control_deassert(struct reset_control *rstc)
531 {
532 if (!rstc)
533 return 0;
534
535 if (WARN_ON(IS_ERR(rstc)))
536 return -EINVAL;
537
538 if (reset_control_is_array(rstc))
539 return reset_control_array_deassert(rstc_to_array(rstc));
540
541 if (rstc->shared) {
542 if (WARN_ON(atomic_read(&rstc->triggered_count) != 0))
543 return -EINVAL;
544
545 if (atomic_inc_return(&rstc->deassert_count) != 1)
546 return 0;
547 } else {
548 if (!rstc->acquired) {
549 WARN(1, "reset %s (ID: %u) is not acquired\n",
550 rcdev_name(rstc->rcdev), rstc->id);
551 return -EPERM;
552 }
553 }
554
555 /*
556 * If the reset controller does not implement .deassert(), we assume
557 * that it handles self-deasserting reset lines via .reset(). In that
558 * case, the reset lines are deasserted by default. If that is not the
559 * case, the reset controller driver should implement .deassert() and
560 * return -ENOTSUPP.
561 */
562 if (!rstc->rcdev->ops->deassert)
563 return 0;
564
565 return rstc->rcdev->ops->deassert(rstc->rcdev, rstc->id);
566 }
567 EXPORT_SYMBOL_GPL(reset_control_deassert);
568
569 /**
570 * reset_control_bulk_deassert - deasserts the reset lines in reverse order
571 * @num_rstcs: number of entries in rstcs array
572 * @rstcs: array of struct reset_control_bulk_data with reset controls set
573 *
574 * Deassert the reset lines for all provided reset controls, in reverse order.
575 * If a deassertion fails, already deasserted resets are asserted again.
576 *
577 * See also: reset_control_deassert()
578 */
reset_control_bulk_deassert(int num_rstcs,struct reset_control_bulk_data * rstcs)579 int reset_control_bulk_deassert(int num_rstcs,
580 struct reset_control_bulk_data *rstcs)
581 {
582 int ret, i;
583
584 for (i = num_rstcs - 1; i >= 0; i--) {
585 ret = reset_control_deassert(rstcs[i].rstc);
586 if (ret)
587 goto err;
588 }
589
590 return 0;
591
592 err:
593 while (i < num_rstcs)
594 reset_control_assert(rstcs[i++].rstc);
595 return ret;
596 }
597 EXPORT_SYMBOL_GPL(reset_control_bulk_deassert);
598
599 /**
600 * reset_control_status - returns a negative errno if not supported, a
601 * positive value if the reset line is asserted, or zero if the reset
602 * line is not asserted or if the desc is NULL (optional reset).
603 * @rstc: reset controller
604 */
reset_control_status(struct reset_control * rstc)605 int reset_control_status(struct reset_control *rstc)
606 {
607 if (!rstc)
608 return 0;
609
610 if (WARN_ON(IS_ERR(rstc)) || reset_control_is_array(rstc))
611 return -EINVAL;
612
613 if (rstc->rcdev->ops->status)
614 return rstc->rcdev->ops->status(rstc->rcdev, rstc->id);
615
616 return -ENOTSUPP;
617 }
618 EXPORT_SYMBOL_GPL(reset_control_status);
619
620 /**
621 * reset_control_acquire() - acquires a reset control for exclusive use
622 * @rstc: reset control
623 *
624 * This is used to explicitly acquire a reset control for exclusive use. Note
625 * that exclusive resets are requested as acquired by default. In order for a
626 * second consumer to be able to control the reset, the first consumer has to
627 * release it first. Typically the easiest way to achieve this is to call the
628 * reset_control_get_exclusive_released() to obtain an instance of the reset
629 * control. Such reset controls are not acquired by default.
630 *
631 * Consumers implementing shared access to an exclusive reset need to follow
632 * a specific protocol in order to work together. Before consumers can change
633 * a reset they must acquire exclusive access using reset_control_acquire().
634 * After they are done operating the reset, they must release exclusive access
635 * with a call to reset_control_release(). Consumers are not granted exclusive
636 * access to the reset as long as another consumer hasn't released a reset.
637 *
638 * See also: reset_control_release()
639 */
reset_control_acquire(struct reset_control * rstc)640 int reset_control_acquire(struct reset_control *rstc)
641 {
642 struct reset_control *rc;
643
644 if (!rstc)
645 return 0;
646
647 if (WARN_ON(IS_ERR(rstc)))
648 return -EINVAL;
649
650 if (reset_control_is_array(rstc))
651 return reset_control_array_acquire(rstc_to_array(rstc));
652
653 mutex_lock(&reset_list_mutex);
654
655 if (rstc->acquired) {
656 mutex_unlock(&reset_list_mutex);
657 return 0;
658 }
659
660 list_for_each_entry(rc, &rstc->rcdev->reset_control_head, list) {
661 if (rstc != rc && rstc->id == rc->id) {
662 if (rc->acquired) {
663 mutex_unlock(&reset_list_mutex);
664 return -EBUSY;
665 }
666 }
667 }
668
669 rstc->acquired = true;
670
671 mutex_unlock(&reset_list_mutex);
672 return 0;
673 }
674 EXPORT_SYMBOL_GPL(reset_control_acquire);
675
676 /**
677 * reset_control_bulk_acquire - acquires reset controls for exclusive use
678 * @num_rstcs: number of entries in rstcs array
679 * @rstcs: array of struct reset_control_bulk_data with reset controls set
680 *
681 * This is used to explicitly acquire reset controls requested with
682 * reset_control_bulk_get_exclusive_release() for temporary exclusive use.
683 *
684 * See also: reset_control_acquire(), reset_control_bulk_release()
685 */
reset_control_bulk_acquire(int num_rstcs,struct reset_control_bulk_data * rstcs)686 int reset_control_bulk_acquire(int num_rstcs,
687 struct reset_control_bulk_data *rstcs)
688 {
689 int ret, i;
690
691 for (i = 0; i < num_rstcs; i++) {
692 ret = reset_control_acquire(rstcs[i].rstc);
693 if (ret)
694 goto err;
695 }
696
697 return 0;
698
699 err:
700 while (i--)
701 reset_control_release(rstcs[i].rstc);
702 return ret;
703 }
704 EXPORT_SYMBOL_GPL(reset_control_bulk_acquire);
705
706 /**
707 * reset_control_release() - releases exclusive access to a reset control
708 * @rstc: reset control
709 *
710 * Releases exclusive access right to a reset control previously obtained by a
711 * call to reset_control_acquire(). Until a consumer calls this function, no
712 * other consumers will be granted exclusive access.
713 *
714 * See also: reset_control_acquire()
715 */
reset_control_release(struct reset_control * rstc)716 void reset_control_release(struct reset_control *rstc)
717 {
718 if (!rstc || WARN_ON(IS_ERR(rstc)))
719 return;
720
721 if (reset_control_is_array(rstc))
722 reset_control_array_release(rstc_to_array(rstc));
723 else
724 rstc->acquired = false;
725 }
726 EXPORT_SYMBOL_GPL(reset_control_release);
727
728 /**
729 * reset_control_bulk_release() - releases exclusive access to reset controls
730 * @num_rstcs: number of entries in rstcs array
731 * @rstcs: array of struct reset_control_bulk_data with reset controls set
732 *
733 * Releases exclusive access right to reset controls previously obtained by a
734 * call to reset_control_bulk_acquire().
735 *
736 * See also: reset_control_release(), reset_control_bulk_acquire()
737 */
reset_control_bulk_release(int num_rstcs,struct reset_control_bulk_data * rstcs)738 void reset_control_bulk_release(int num_rstcs,
739 struct reset_control_bulk_data *rstcs)
740 {
741 int i;
742
743 for (i = 0; i < num_rstcs; i++)
744 reset_control_release(rstcs[i].rstc);
745 }
746 EXPORT_SYMBOL_GPL(reset_control_bulk_release);
747
748 static struct reset_control *
__reset_control_get_internal(struct reset_controller_dev * rcdev,unsigned int index,enum reset_control_flags flags)749 __reset_control_get_internal(struct reset_controller_dev *rcdev,
750 unsigned int index, enum reset_control_flags flags)
751 {
752 bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED;
753 bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED;
754 struct reset_control *rstc;
755
756 lockdep_assert_held(&reset_list_mutex);
757
758 /* Expect callers to filter out OPTIONAL and DEASSERTED bits */
759 if (WARN_ON(flags & ~(RESET_CONTROL_FLAGS_BIT_SHARED |
760 RESET_CONTROL_FLAGS_BIT_ACQUIRED)))
761 return ERR_PTR(-EINVAL);
762
763 list_for_each_entry(rstc, &rcdev->reset_control_head, list) {
764 if (rstc->id == index) {
765 /*
766 * Allow creating a secondary exclusive reset_control
767 * that is initially not acquired for an already
768 * controlled reset line.
769 */
770 if (!rstc->shared && !shared && !acquired)
771 break;
772
773 if (WARN_ON(!rstc->shared || !shared))
774 return ERR_PTR(-EBUSY);
775
776 kref_get(&rstc->refcnt);
777 return rstc;
778 }
779 }
780
781 rstc = kzalloc_obj(*rstc);
782 if (!rstc)
783 return ERR_PTR(-ENOMEM);
784
785 if (!try_module_get(rcdev->owner)) {
786 kfree(rstc);
787 return ERR_PTR(-ENODEV);
788 }
789
790 rstc->rcdev = rcdev;
791 list_add(&rstc->list, &rcdev->reset_control_head);
792 rstc->id = index;
793 kref_init(&rstc->refcnt);
794 rstc->acquired = acquired;
795 rstc->shared = shared;
796 get_device(rcdev->dev);
797
798 return rstc;
799 }
800
__reset_control_release(struct kref * kref)801 static void __reset_control_release(struct kref *kref)
802 {
803 struct reset_control *rstc = container_of(kref, struct reset_control,
804 refcnt);
805
806 lockdep_assert_held(&reset_list_mutex);
807
808 module_put(rstc->rcdev->owner);
809
810 list_del(&rstc->list);
811 put_device(rstc->rcdev->dev);
812 kfree(rstc);
813 }
814
__reset_control_put_internal(struct reset_control * rstc)815 static void __reset_control_put_internal(struct reset_control *rstc)
816 {
817 lockdep_assert_held(&reset_list_mutex);
818
819 if (IS_ERR_OR_NULL(rstc))
820 return;
821
822 kref_put(&rstc->refcnt, __reset_control_release);
823 }
824
reset_gpio_aux_device_release(struct device * dev)825 static void reset_gpio_aux_device_release(struct device *dev)
826 {
827 struct auxiliary_device *adev = to_auxiliary_dev(dev);
828
829 kfree(adev);
830 }
831
reset_add_gpio_aux_device(struct device * parent,struct fwnode_handle * swnode,int id,void * pdata)832 static int reset_add_gpio_aux_device(struct device *parent,
833 struct fwnode_handle *swnode,
834 int id, void *pdata)
835 {
836 struct auxiliary_device *adev;
837 int ret;
838
839 adev = kzalloc_obj(*adev);
840 if (!adev)
841 return -ENOMEM;
842
843 adev->id = id;
844 adev->name = "gpio";
845 adev->dev.parent = parent;
846 adev->dev.platform_data = pdata;
847 adev->dev.release = reset_gpio_aux_device_release;
848 device_set_node(&adev->dev, swnode);
849
850 ret = auxiliary_device_init(adev);
851 if (ret) {
852 kfree(adev);
853 return ret;
854 }
855
856 ret = __auxiliary_device_add(adev, "reset");
857 if (ret) {
858 auxiliary_device_uninit(adev);
859 kfree(adev);
860 return ret;
861 }
862
863 return ret;
864 }
865
866 /*
867 * @args: phandle to the GPIO provider with all the args like GPIO number
868 */
__reset_add_reset_gpio_device(const struct of_phandle_args * args)869 static int __reset_add_reset_gpio_device(const struct of_phandle_args *args)
870 {
871 struct property_entry properties[3] = { };
872 unsigned int offset, of_flags, lflags;
873 struct reset_gpio_lookup *rgpio_dev;
874 struct device *parent;
875 int id, ret, prop = 0;
876
877 /*
878 * Currently only #gpio-cells=2 is supported with the meaning of:
879 * args[0]: GPIO number
880 * args[1]: GPIO flags
881 * TODO: Handle other cases.
882 */
883 if (args->args_count != 2)
884 return -ENOENT;
885
886 /*
887 * Registering reset-gpio device might cause immediate
888 * bind, resulting in its probe() registering new reset controller thus
889 * taking reset_list_mutex lock via reset_controller_register().
890 */
891 lockdep_assert_not_held(&reset_list_mutex);
892
893 offset = args->args[0];
894 of_flags = args->args[1];
895
896 /*
897 * Later we map GPIO flags between OF and Linux, however not all
898 * constants from include/dt-bindings/gpio/gpio.h and
899 * include/linux/gpio/machine.h match each other.
900 *
901 * FIXME: Find a better way of translating OF flags to GPIO lookup
902 * flags.
903 */
904 if (of_flags > GPIO_ACTIVE_LOW) {
905 pr_err("reset-gpio code does not support GPIO flags %u for GPIO %u\n",
906 of_flags, offset);
907 return -EINVAL;
908 }
909
910 struct gpio_device *gdev __free(gpio_device_put) =
911 gpio_device_find_by_fwnode(of_fwnode_handle(args->np));
912 if (!gdev)
913 return -EPROBE_DEFER;
914
915 guard(mutex)(&reset_gpio_lookup_mutex);
916
917 list_for_each_entry(rgpio_dev, &reset_gpio_lookup_list, list) {
918 if (args->np == rgpio_dev->of_args.np) {
919 if (of_phandle_args_equal(args, &rgpio_dev->of_args))
920 return 0; /* Already on the list, done */
921 }
922 }
923
924 lflags = GPIO_PERSISTENT | (of_flags & GPIO_ACTIVE_LOW);
925 parent = gpio_device_to_device(gdev);
926 properties[prop++] = PROPERTY_ENTRY_STRING("compatible", "reset-gpio");
927 properties[prop++] = PROPERTY_ENTRY_GPIO("reset-gpios", parent->fwnode, offset, lflags);
928
929 id = ida_alloc(&reset_gpio_ida, GFP_KERNEL);
930 if (id < 0)
931 return id;
932
933 /* Not freed on success, because it is persisent subsystem data. */
934 rgpio_dev = kzalloc_obj(*rgpio_dev);
935 if (!rgpio_dev) {
936 ret = -ENOMEM;
937 goto err_ida_free;
938 }
939
940 rgpio_dev->of_args = *args;
941 /*
942 * We keep the device_node reference, but of_args.np is put at the end
943 * of __of_reset_control_get(), so get it one more time.
944 * Hold reference as long as rgpio_dev memory is valid.
945 */
946 of_node_get(rgpio_dev->of_args.np);
947
948 rgpio_dev->swnode = fwnode_create_software_node(properties, NULL);
949 if (IS_ERR(rgpio_dev->swnode)) {
950 ret = PTR_ERR(rgpio_dev->swnode);
951 goto err_put_of_node;
952 }
953
954 ret = reset_add_gpio_aux_device(parent, rgpio_dev->swnode, id,
955 &rgpio_dev->of_args);
956 if (ret)
957 goto err_del_swnode;
958
959 list_add(&rgpio_dev->list, &reset_gpio_lookup_list);
960
961 return 0;
962
963 err_del_swnode:
964 fwnode_remove_software_node(rgpio_dev->swnode);
965 err_put_of_node:
966 of_node_put(rgpio_dev->of_args.np);
967 kfree(rgpio_dev);
968 err_ida_free:
969 ida_free(&reset_gpio_ida, id);
970
971 return ret;
972 }
973
__reset_find_rcdev(const struct of_phandle_args * args,bool gpio_fallback)974 static struct reset_controller_dev *__reset_find_rcdev(const struct of_phandle_args *args,
975 bool gpio_fallback)
976 {
977 struct reset_controller_dev *rcdev;
978
979 lockdep_assert_held(&reset_list_mutex);
980
981 list_for_each_entry(rcdev, &reset_controller_list, list) {
982 if (gpio_fallback) {
983 if (rcdev->of_args && of_phandle_args_equal(args,
984 rcdev->of_args))
985 return rcdev;
986 } else {
987 if (args->np == rcdev->of_node)
988 return rcdev;
989 }
990 }
991
992 return NULL;
993 }
994
995 struct reset_control *
__of_reset_control_get(struct device_node * node,const char * id,int index,enum reset_control_flags flags)996 __of_reset_control_get(struct device_node *node, const char *id, int index,
997 enum reset_control_flags flags)
998 {
999 bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
1000 bool gpio_fallback = false;
1001 struct reset_control *rstc;
1002 struct reset_controller_dev *rcdev;
1003 struct of_phandle_args args;
1004 int rstc_id;
1005 int ret;
1006
1007 if (!node)
1008 return ERR_PTR(-EINVAL);
1009
1010 if (id) {
1011 index = of_property_match_string(node,
1012 "reset-names", id);
1013 if (index == -EILSEQ)
1014 return ERR_PTR(index);
1015 if (index < 0)
1016 return optional ? NULL : ERR_PTR(-ENOENT);
1017 }
1018
1019 ret = of_parse_phandle_with_args(node, "resets", "#reset-cells",
1020 index, &args);
1021 if (ret == -EINVAL)
1022 return ERR_PTR(ret);
1023 if (ret) {
1024 if (!IS_ENABLED(CONFIG_RESET_GPIO))
1025 return optional ? NULL : ERR_PTR(ret);
1026
1027 /*
1028 * There can be only one reset-gpio for regular devices, so
1029 * don't bother with the "reset-gpios" phandle index.
1030 */
1031 ret = of_parse_phandle_with_args(node, "reset-gpios", "#gpio-cells",
1032 0, &args);
1033 if (ret)
1034 return optional ? NULL : ERR_PTR(ret);
1035
1036 gpio_fallback = true;
1037
1038 ret = __reset_add_reset_gpio_device(&args);
1039 if (ret) {
1040 rstc = ERR_PTR(ret);
1041 goto out_put;
1042 }
1043 }
1044
1045 mutex_lock(&reset_list_mutex);
1046 rcdev = __reset_find_rcdev(&args, gpio_fallback);
1047 if (!rcdev) {
1048 rstc = ERR_PTR(-EPROBE_DEFER);
1049 goto out_unlock;
1050 }
1051
1052 if (WARN_ON(args.args_count != rcdev->of_reset_n_cells)) {
1053 rstc = ERR_PTR(-EINVAL);
1054 goto out_unlock;
1055 }
1056
1057 rstc_id = rcdev->of_xlate(rcdev, &args);
1058 if (rstc_id < 0) {
1059 rstc = ERR_PTR(rstc_id);
1060 goto out_unlock;
1061 }
1062
1063 flags &= ~RESET_CONTROL_FLAGS_BIT_OPTIONAL;
1064
1065 /* reset_list_mutex also protects the rcdev's reset_control list */
1066 rstc = __reset_control_get_internal(rcdev, rstc_id, flags);
1067
1068 out_unlock:
1069 mutex_unlock(&reset_list_mutex);
1070 out_put:
1071 of_node_put(args.np);
1072
1073 return rstc;
1074 }
1075 EXPORT_SYMBOL_GPL(__of_reset_control_get);
1076
__reset_control_get(struct device * dev,const char * id,int index,enum reset_control_flags flags)1077 struct reset_control *__reset_control_get(struct device *dev, const char *id,
1078 int index, enum reset_control_flags flags)
1079 {
1080 bool shared = flags & RESET_CONTROL_FLAGS_BIT_SHARED;
1081 bool acquired = flags & RESET_CONTROL_FLAGS_BIT_ACQUIRED;
1082 bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
1083
1084 if (WARN_ON(shared && acquired))
1085 return ERR_PTR(-EINVAL);
1086
1087 if (dev->of_node)
1088 return __of_reset_control_get(dev->of_node, id, index, flags);
1089
1090 return optional ? NULL : ERR_PTR(-ENOENT);
1091 }
1092 EXPORT_SYMBOL_GPL(__reset_control_get);
1093
__reset_control_bulk_get(struct device * dev,int num_rstcs,struct reset_control_bulk_data * rstcs,enum reset_control_flags flags)1094 int __reset_control_bulk_get(struct device *dev, int num_rstcs,
1095 struct reset_control_bulk_data *rstcs,
1096 enum reset_control_flags flags)
1097 {
1098 int ret, i;
1099
1100 for (i = 0; i < num_rstcs; i++) {
1101 rstcs[i].rstc = __reset_control_get(dev, rstcs[i].id, 0, flags);
1102 if (IS_ERR(rstcs[i].rstc)) {
1103 ret = PTR_ERR(rstcs[i].rstc);
1104 goto err;
1105 }
1106 }
1107
1108 return 0;
1109
1110 err:
1111 mutex_lock(&reset_list_mutex);
1112 while (i--)
1113 __reset_control_put_internal(rstcs[i].rstc);
1114 mutex_unlock(&reset_list_mutex);
1115 return ret;
1116 }
1117 EXPORT_SYMBOL_GPL(__reset_control_bulk_get);
1118
reset_control_array_put(struct reset_control_array * resets)1119 static void reset_control_array_put(struct reset_control_array *resets)
1120 {
1121 int i;
1122
1123 mutex_lock(&reset_list_mutex);
1124 for (i = 0; i < resets->num_rstcs; i++)
1125 __reset_control_put_internal(resets->rstc[i]);
1126 mutex_unlock(&reset_list_mutex);
1127 kfree(resets);
1128 }
1129
1130 /**
1131 * reset_control_put - free the reset controller
1132 * @rstc: reset controller
1133 */
reset_control_put(struct reset_control * rstc)1134 void reset_control_put(struct reset_control *rstc)
1135 {
1136 if (IS_ERR_OR_NULL(rstc))
1137 return;
1138
1139 if (reset_control_is_array(rstc)) {
1140 reset_control_array_put(rstc_to_array(rstc));
1141 return;
1142 }
1143
1144 mutex_lock(&reset_list_mutex);
1145 __reset_control_put_internal(rstc);
1146 mutex_unlock(&reset_list_mutex);
1147 }
1148 EXPORT_SYMBOL_GPL(reset_control_put);
1149
1150 /**
1151 * reset_control_bulk_put - free the reset controllers
1152 * @num_rstcs: number of entries in rstcs array
1153 * @rstcs: array of struct reset_control_bulk_data with reset controls set
1154 */
reset_control_bulk_put(int num_rstcs,struct reset_control_bulk_data * rstcs)1155 void reset_control_bulk_put(int num_rstcs, struct reset_control_bulk_data *rstcs)
1156 {
1157 mutex_lock(&reset_list_mutex);
1158 while (num_rstcs--)
1159 __reset_control_put_internal(rstcs[num_rstcs].rstc);
1160 mutex_unlock(&reset_list_mutex);
1161 }
1162 EXPORT_SYMBOL_GPL(reset_control_bulk_put);
1163
devm_reset_control_release(struct device * dev,void * res)1164 static void devm_reset_control_release(struct device *dev, void *res)
1165 {
1166 reset_control_put(*(struct reset_control **)res);
1167 }
1168
devm_reset_control_release_deasserted(struct device * dev,void * res)1169 static void devm_reset_control_release_deasserted(struct device *dev, void *res)
1170 {
1171 struct reset_control *rstc = *(struct reset_control **)res;
1172
1173 reset_control_assert(rstc);
1174 reset_control_put(rstc);
1175 }
1176
1177 struct reset_control *
__devm_reset_control_get(struct device * dev,const char * id,int index,enum reset_control_flags flags)1178 __devm_reset_control_get(struct device *dev, const char *id, int index,
1179 enum reset_control_flags flags)
1180 {
1181 struct reset_control **ptr, *rstc;
1182 bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED;
1183
1184 ptr = devres_alloc(deasserted ? devm_reset_control_release_deasserted :
1185 devm_reset_control_release, sizeof(*ptr),
1186 GFP_KERNEL);
1187 if (!ptr)
1188 return ERR_PTR(-ENOMEM);
1189
1190 flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED;
1191
1192 rstc = __reset_control_get(dev, id, index, flags);
1193 if (IS_ERR_OR_NULL(rstc)) {
1194 devres_free(ptr);
1195 return rstc;
1196 }
1197
1198 if (deasserted) {
1199 int ret;
1200
1201 ret = reset_control_deassert(rstc);
1202 if (ret) {
1203 reset_control_put(rstc);
1204 devres_free(ptr);
1205 return ERR_PTR(ret);
1206 }
1207 }
1208
1209 *ptr = rstc;
1210 devres_add(dev, ptr);
1211
1212 return rstc;
1213 }
1214 EXPORT_SYMBOL_GPL(__devm_reset_control_get);
1215
1216 struct reset_control_bulk_devres {
1217 int num_rstcs;
1218 struct reset_control_bulk_data *rstcs;
1219 };
1220
devm_reset_control_bulk_release(struct device * dev,void * res)1221 static void devm_reset_control_bulk_release(struct device *dev, void *res)
1222 {
1223 struct reset_control_bulk_devres *devres = res;
1224
1225 reset_control_bulk_put(devres->num_rstcs, devres->rstcs);
1226 }
1227
devm_reset_control_bulk_release_deasserted(struct device * dev,void * res)1228 static void devm_reset_control_bulk_release_deasserted(struct device *dev, void *res)
1229 {
1230 struct reset_control_bulk_devres *devres = res;
1231
1232 reset_control_bulk_assert(devres->num_rstcs, devres->rstcs);
1233 reset_control_bulk_put(devres->num_rstcs, devres->rstcs);
1234 }
1235
__devm_reset_control_bulk_get(struct device * dev,int num_rstcs,struct reset_control_bulk_data * rstcs,enum reset_control_flags flags)1236 int __devm_reset_control_bulk_get(struct device *dev, int num_rstcs,
1237 struct reset_control_bulk_data *rstcs,
1238 enum reset_control_flags flags)
1239 {
1240 struct reset_control_bulk_devres *ptr;
1241 bool deasserted = flags & RESET_CONTROL_FLAGS_BIT_DEASSERTED;
1242 int ret;
1243
1244 ptr = devres_alloc(deasserted ? devm_reset_control_bulk_release_deasserted :
1245 devm_reset_control_bulk_release, sizeof(*ptr),
1246 GFP_KERNEL);
1247 if (!ptr)
1248 return -ENOMEM;
1249
1250 flags &= ~RESET_CONTROL_FLAGS_BIT_DEASSERTED;
1251
1252 ret = __reset_control_bulk_get(dev, num_rstcs, rstcs, flags);
1253 if (ret < 0) {
1254 devres_free(ptr);
1255 return ret;
1256 }
1257
1258 if (deasserted) {
1259 ret = reset_control_bulk_deassert(num_rstcs, rstcs);
1260 if (ret) {
1261 reset_control_bulk_put(num_rstcs, rstcs);
1262 devres_free(ptr);
1263 return ret;
1264 }
1265 }
1266
1267 ptr->num_rstcs = num_rstcs;
1268 ptr->rstcs = rstcs;
1269 devres_add(dev, ptr);
1270
1271 return 0;
1272 }
1273 EXPORT_SYMBOL_GPL(__devm_reset_control_bulk_get);
1274
1275 /**
1276 * __device_reset - find reset controller associated with the device
1277 * and perform reset
1278 * @dev: device to be reset by the controller
1279 * @optional: whether it is optional to reset the device
1280 *
1281 * Convenience wrapper for __reset_control_get() and reset_control_reset().
1282 * This is useful for the common case of devices with single, dedicated reset
1283 * lines. _RST firmware method will be called for devices with ACPI.
1284 */
__device_reset(struct device * dev,bool optional)1285 int __device_reset(struct device *dev, bool optional)
1286 {
1287 enum reset_control_flags flags;
1288 struct reset_control *rstc;
1289 int ret;
1290
1291 #ifdef CONFIG_ACPI
1292 acpi_handle handle = ACPI_HANDLE(dev);
1293
1294 if (handle) {
1295 if (!acpi_has_method(handle, "_RST"))
1296 return optional ? 0 : -ENOENT;
1297 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_RST", NULL,
1298 NULL)))
1299 return -EIO;
1300 }
1301 #endif
1302
1303 flags = optional ? RESET_CONTROL_OPTIONAL_EXCLUSIVE : RESET_CONTROL_EXCLUSIVE;
1304 rstc = __reset_control_get(dev, NULL, 0, flags);
1305 if (IS_ERR(rstc))
1306 return PTR_ERR(rstc);
1307
1308 ret = reset_control_reset(rstc);
1309
1310 reset_control_put(rstc);
1311
1312 return ret;
1313 }
1314 EXPORT_SYMBOL_GPL(__device_reset);
1315
1316 /*
1317 * APIs to manage an array of reset controls.
1318 */
1319
1320 /**
1321 * of_reset_control_get_count - Count number of resets available with a device
1322 *
1323 * @node: device node that contains 'resets'.
1324 *
1325 * Returns positive reset count on success, or error number on failure and
1326 * on count being zero.
1327 */
of_reset_control_get_count(struct device_node * node)1328 static int of_reset_control_get_count(struct device_node *node)
1329 {
1330 int count;
1331
1332 if (!node)
1333 return -EINVAL;
1334
1335 count = of_count_phandle_with_args(node, "resets", "#reset-cells");
1336 if (count == 0)
1337 count = -ENOENT;
1338
1339 return count;
1340 }
1341
1342 /**
1343 * of_reset_control_array_get - Get a list of reset controls using
1344 * device node.
1345 *
1346 * @np: device node for the device that requests the reset controls array
1347 * @flags: whether reset controls are shared, optional, acquired
1348 *
1349 * Returns pointer to allocated reset_control on success or error on failure
1350 */
1351 struct reset_control *
of_reset_control_array_get(struct device_node * np,enum reset_control_flags flags)1352 of_reset_control_array_get(struct device_node *np, enum reset_control_flags flags)
1353 {
1354 bool optional = flags & RESET_CONTROL_FLAGS_BIT_OPTIONAL;
1355 struct reset_control_array *resets;
1356 struct reset_control *rstc;
1357 int num, i;
1358
1359 num = of_reset_control_get_count(np);
1360 if (num < 0)
1361 return optional ? NULL : ERR_PTR(num);
1362
1363 resets = kzalloc_flex(*resets, rstc, num);
1364 if (!resets)
1365 return ERR_PTR(-ENOMEM);
1366 resets->num_rstcs = num;
1367
1368 for (i = 0; i < num; i++) {
1369 rstc = __of_reset_control_get(np, NULL, i, flags);
1370 if (IS_ERR(rstc))
1371 goto err_rst;
1372 resets->rstc[i] = rstc;
1373 }
1374 resets->base.array = true;
1375
1376 return &resets->base;
1377
1378 err_rst:
1379 mutex_lock(&reset_list_mutex);
1380 while (--i >= 0)
1381 __reset_control_put_internal(resets->rstc[i]);
1382 mutex_unlock(&reset_list_mutex);
1383
1384 kfree(resets);
1385
1386 return rstc;
1387 }
1388 EXPORT_SYMBOL_GPL(of_reset_control_array_get);
1389
1390 /**
1391 * devm_reset_control_array_get - Resource managed reset control array get
1392 *
1393 * @dev: device that requests the list of reset controls
1394 * @flags: whether reset controls are shared, optional, acquired
1395 *
1396 * The reset control array APIs are intended for a list of resets
1397 * that just have to be asserted or deasserted, without any
1398 * requirements on the order.
1399 *
1400 * Returns pointer to allocated reset_control on success or error on failure
1401 */
1402 struct reset_control *
devm_reset_control_array_get(struct device * dev,enum reset_control_flags flags)1403 devm_reset_control_array_get(struct device *dev, enum reset_control_flags flags)
1404 {
1405 struct reset_control **ptr, *rstc;
1406
1407 ptr = devres_alloc(devm_reset_control_release, sizeof(*ptr),
1408 GFP_KERNEL);
1409 if (!ptr)
1410 return ERR_PTR(-ENOMEM);
1411
1412 rstc = of_reset_control_array_get(dev->of_node, flags);
1413 if (IS_ERR_OR_NULL(rstc)) {
1414 devres_free(ptr);
1415 return rstc;
1416 }
1417
1418 *ptr = rstc;
1419 devres_add(dev, ptr);
1420
1421 return rstc;
1422 }
1423 EXPORT_SYMBOL_GPL(devm_reset_control_array_get);
1424
1425 /**
1426 * reset_control_get_count - Count number of resets available with a device
1427 *
1428 * @dev: device for which to return the number of resets
1429 *
1430 * Returns positive reset count on success, or error number on failure and
1431 * on count being zero.
1432 */
reset_control_get_count(struct device * dev)1433 int reset_control_get_count(struct device *dev)
1434 {
1435 if (dev->of_node)
1436 return of_reset_control_get_count(dev->of_node);
1437
1438 return -ENOENT;
1439 }
1440 EXPORT_SYMBOL_GPL(reset_control_get_count);
1441