projects/linux·blob master · refs

1140 lines · 27.3 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * Copyright (C) 2024 Linaro Ltd.
4 */
5
6#include <linux/bug.h>
7#include <linux/cleanup.h>
8#include <linux/debugfs.h>
9#include <linux/device.h>
10#include <linux/err.h>
11#include <linux/export.h>
12#include <linux/idr.h>
13#include <linux/kernel.h>
14#include <linux/kref.h>
15#include <linux/list.h>
16#include <linux/lockdep.h>
17#include <linux/module.h>
18#include <linux/mutex.h>
19#include <linux/property.h>
20#include <linux/pwrseq/consumer.h>
21#include <linux/pwrseq/provider.h>
22#include <linux/radix-tree.h>
23#include <linux/rwsem.h>
24#include <linux/slab.h>
25
26/*
27 * Power-sequencing framework for linux.
28 *
29 * This subsystem allows power sequence providers to register a set of targets
30 * that consumers may request and power-up/down.
31 *
32 * Glossary:
33 *
34 * Unit - a unit is a discreet chunk of a power sequence. For instance one unit
35 * may enable a set of regulators, another may enable a specific GPIO. Units
36 * can define dependencies in the form of other units that must be enabled
37 * before it itself can be.
38 *
39 * Target - a target is a set of units (composed of the "final" unit and its
40 * dependencies) that a consumer selects by its name when requesting a handle
41 * to the power sequencer. Via the dependency system, multiple targets may
42 * share the same parts of a power sequence but ignore parts that are
43 * irrelevant.
44 *
45 * Descriptor - a handle passed by the pwrseq core to every consumer that
46 * serves as the entry point to the provider layer. It ensures coherence
47 * between different users and keeps reference counting consistent.
48 *
49 * Each provider must define a .match() callback whose role is to determine
50 * whether a potential consumer is in fact associated with this sequencer.
51 * This allows creating abstraction layers on top of regular device-tree
52 * resources like regulators, clocks and other nodes connected to the consumer
53 * via phandle.
54 */
55
56static DEFINE_IDA(pwrseq_ida);
57
58/*
59 * Protects the device list on the pwrseq bus from concurrent modifications
60 * but allows simultaneous read-only access.
61 */
62static DECLARE_RWSEM(pwrseq_sem);
63
64/**
65 * struct pwrseq_unit - Private power-sequence unit data.
66 * @ref: Reference count for this object. When it goes to 0, the object is
67 * destroyed.
68 * @name: Name of this target.
69 * @list: Link to siblings on the list of all units of a single sequencer.
70 * @deps: List of units on which this unit depends.
71 * @enable: Callback running the part of the power-on sequence provided by
72 * this unit.
73 * @disable: Callback running the part of the power-off sequence provided
74 * by this unit.
75 * @enable_count: Current number of users that enabled this unit. May be the
76 * consumer of the power sequencer or other units that depend
77 * on this one.
78 */
79struct pwrseq_unit {
80 struct kref ref;
81 const char *name;
82 struct list_head list;
83 struct list_head deps;
84 pwrseq_power_state_func enable;
85 pwrseq_power_state_func disable;
86 unsigned int enable_count;
87};
88
89static struct pwrseq_unit *pwrseq_unit_new(const struct pwrseq_unit_data *data)
90{
91 struct pwrseq_unit *unit;
92
93 unit = kzalloc_obj(*unit);
94 if (!unit)
95 return NULL;
96
97 unit->name = kstrdup_const(data->name, GFP_KERNEL);
98 if (!unit->name) {
99 kfree(unit);
100 return NULL;
101 }
102
103 kref_init(&unit->ref);
104 INIT_LIST_HEAD(&unit->list);
105 INIT_LIST_HEAD(&unit->deps);
106 unit->enable = data->enable;
107 unit->disable = data->disable;
108
109 return unit;
110}
111
112static struct pwrseq_unit *pwrseq_unit_get(struct pwrseq_unit *unit)
113{
114 kref_get(&unit->ref);
115
116 return unit;
117}
118
119static void pwrseq_unit_release(struct kref *ref);
120
121static void pwrseq_unit_put(struct pwrseq_unit *unit)
122{
123 kref_put(&unit->ref, pwrseq_unit_release);
124}
125
126/**
127 * struct pwrseq_unit_dep - Wrapper around a reference to the unit structure
128 * allowing to keep it on multiple dependency lists
129 * in different units.
130 * @list: Siblings on the list.
131 * @unit: Address of the referenced unit.
132 */
133struct pwrseq_unit_dep {
134 struct list_head list;
135 struct pwrseq_unit *unit;
136};
137
138static struct pwrseq_unit_dep *pwrseq_unit_dep_new(struct pwrseq_unit *unit)
139{
140 struct pwrseq_unit_dep *dep;
141
142 dep = kzalloc_obj(*dep);
143 if (!dep)
144 return NULL;
145
146 dep->unit = unit;
147
148 return dep;
149}
150
151static void pwrseq_unit_dep_free(struct pwrseq_unit_dep *ref)
152{
153 pwrseq_unit_put(ref->unit);
154 kfree(ref);
155}
156
157static void pwrseq_unit_free_deps(struct list_head *list)
158{
159 struct pwrseq_unit_dep *dep, *next;
160
161 list_for_each_entry_safe(dep, next, list, list) {
162 list_del(&dep->list);
163 pwrseq_unit_dep_free(dep);
164 }
165}
166
167static void pwrseq_unit_release(struct kref *ref)
168{
169 struct pwrseq_unit *unit = container_of(ref, struct pwrseq_unit, ref);
170
171 pwrseq_unit_free_deps(&unit->deps);
172 list_del(&unit->list);
173 kfree_const(unit->name);
174 kfree(unit);
175}
176
177/**
178 * struct pwrseq_target - Private power-sequence target data.
179 * @list: Siblings on the list of all targets exposed by a power sequencer.
180 * @name: Name of the target.
181 * @unit: Final unit for this target.
182 * @post_enable: Callback run after the target unit has been enabled, *after*
183 * the state lock has been released. It's useful for implementing
184 * boot-up delays without blocking other users from powering up
185 * using the same power sequencer.
186 */
187struct pwrseq_target {
188 struct list_head list;
189 const char *name;
190 struct pwrseq_unit *unit;
191 pwrseq_power_state_func post_enable;
192};
193
194static struct pwrseq_target *
195pwrseq_target_new(const struct pwrseq_target_data *data)
196{
197 struct pwrseq_target *target;
198
199 target = kzalloc_obj(*target);
200 if (!target)
201 return NULL;
202
203 target->name = kstrdup_const(data->name, GFP_KERNEL);
204 if (!target->name) {
205 kfree(target);
206 return NULL;
207 }
208
209 target->post_enable = data->post_enable;
210
211 return target;
212}
213
214static void pwrseq_target_free(struct pwrseq_target *target)
215{
216 if (!IS_ERR_OR_NULL(target->unit))
217 pwrseq_unit_put(target->unit);
218 kfree_const(target->name);
219 kfree(target);
220}
221
222/**
223 * struct pwrseq_device - Private power sequencing data.
224 * @dev: Device struct associated with this sequencer.
225 * @id: Device ID.
226 * @owner: Prevents removal of active power sequencing providers.
227 * @rw_lock: Protects the device from being unregistered while in use.
228 * @state_lock: Prevents multiple users running the power sequence at the same
229 * time.
230 * @match: Power sequencer matching callback.
231 * @targets: List of targets exposed by this sequencer.
232 * @units: List of all units supported by this sequencer.
233 */
234struct pwrseq_device {
235 struct device dev;
236 int id;
237 struct module *owner;
238 struct rw_semaphore rw_lock;
239 struct mutex state_lock;
240 pwrseq_match_func match;
241 struct list_head targets;
242 struct list_head units;
243};
244
245static struct pwrseq_device *to_pwrseq_device(struct device *dev)
246{
247 return container_of(dev, struct pwrseq_device, dev);
248}
249
250static struct pwrseq_device *pwrseq_device_get(struct pwrseq_device *pwrseq)
251{
252 get_device(&pwrseq->dev);
253
254 return pwrseq;
255}
256
257static void pwrseq_device_put(struct pwrseq_device *pwrseq)
258{
259 put_device(&pwrseq->dev);
260}
261
262/**
263 * struct pwrseq_desc - Wraps access to the pwrseq_device and ensures that one
264 * user cannot break the reference counting for others.
265 * @pwrseq: Reference to the power sequencing device.
266 * @target: Reference to the target this descriptor allows to control.
267 * @powered_on: Power state set by the holder of the descriptor (not necessarily
268 * corresponding to the actual power state of the device).
269 */
270struct pwrseq_desc {
271 struct pwrseq_device *pwrseq;
272 struct pwrseq_target *target;
273 bool powered_on;
274};
275
276static const struct bus_type pwrseq_bus = {
277 .name = "pwrseq",
278};
279
280static void pwrseq_release(struct device *dev)
281{
282 struct pwrseq_device *pwrseq = to_pwrseq_device(dev);
283 struct pwrseq_target *target, *pos;
284
285 list_for_each_entry_safe(target, pos, &pwrseq->targets, list) {
286 list_del(&target->list);
287 pwrseq_target_free(target);
288 }
289
290 mutex_destroy(&pwrseq->state_lock);
291 ida_free(&pwrseq_ida, pwrseq->id);
292 kfree(pwrseq);
293}
294
295static const struct device_type pwrseq_device_type = {
296 .name = "power_sequencer",
297 .release = pwrseq_release,
298};
299
300static int pwrseq_check_unit_deps(const struct pwrseq_unit_data *data,
301 struct radix_tree_root *visited_units)
302{
303 const struct pwrseq_unit_data *tmp, **cur;
304 int ret;
305
306 ret = radix_tree_insert(visited_units, (unsigned long)data,
307 (void *)data);
308 if (ret)
309 return ret;
310
311 for (cur = data->deps; cur && *cur; cur++) {
312 tmp = radix_tree_lookup(visited_units, (unsigned long)*cur);
313 if (tmp) {
314 WARN(1, "Circular dependency in power sequencing flow detected!\n");
315 return -EINVAL;
316 }
317
318 ret = pwrseq_check_unit_deps(*cur, visited_units);
319 if (ret)
320 return ret;
321 }
322
323 return 0;
324}
325
326static int pwrseq_check_target_deps(const struct pwrseq_target_data *data)
327{
328 struct radix_tree_root visited_units;
329 struct radix_tree_iter iter;
330 void __rcu **slot;
331 int ret;
332
333 if (!data->unit)
334 return -EINVAL;
335
336 INIT_RADIX_TREE(&visited_units, GFP_KERNEL);
337 ret = pwrseq_check_unit_deps(data->unit, &visited_units);
338 radix_tree_for_each_slot(slot, &visited_units, &iter, 0)
339 radix_tree_delete(&visited_units, iter.index);
340
341 return ret;
342}
343
344static int pwrseq_unit_setup_deps(const struct pwrseq_unit_data **data,
345 struct list_head *dep_list,
346 struct list_head *unit_list,
347 struct radix_tree_root *processed_units);
348
349static struct pwrseq_unit *
350pwrseq_unit_setup(const struct pwrseq_unit_data *data,
351 struct list_head *unit_list,
352 struct radix_tree_root *processed_units)
353{
354 struct pwrseq_unit *unit;
355 int ret;
356
357 unit = radix_tree_lookup(processed_units, (unsigned long)data);
358 if (unit)
359 return pwrseq_unit_get(unit);
360
361 unit = pwrseq_unit_new(data);
362 if (!unit)
363 return ERR_PTR(-ENOMEM);
364
365 if (data->deps) {
366 ret = pwrseq_unit_setup_deps(data->deps, &unit->deps,
367 unit_list, processed_units);
368 if (ret) {
369 pwrseq_unit_put(unit);
370 return ERR_PTR(ret);
371 }
372 }
373
374 ret = radix_tree_insert(processed_units, (unsigned long)data, unit);
375 if (ret) {
376 pwrseq_unit_put(unit);
377 return ERR_PTR(ret);
378 }
379
380 list_add_tail(&unit->list, unit_list);
381
382 return unit;
383}
384
385static int pwrseq_unit_setup_deps(const struct pwrseq_unit_data **data,
386 struct list_head *dep_list,
387 struct list_head *unit_list,
388 struct radix_tree_root *processed_units)
389{
390 const struct pwrseq_unit_data *pos;
391 struct pwrseq_unit_dep *dep;
392 struct pwrseq_unit *unit;
393 int i;
394
395 for (i = 0; data[i]; i++) {
396 pos = data[i];
397
398 unit = pwrseq_unit_setup(pos, unit_list, processed_units);
399 if (IS_ERR(unit))
400 return PTR_ERR(unit);
401
402 dep = pwrseq_unit_dep_new(unit);
403 if (!dep) {
404 pwrseq_unit_put(unit);
405 return -ENOMEM;
406 }
407
408 list_add_tail(&dep->list, dep_list);
409 }
410
411 return 0;
412}
413
414static int pwrseq_do_setup_targets(const struct pwrseq_target_data **data,
415 struct pwrseq_device *pwrseq,
416 struct radix_tree_root *processed_units)
417{
418 const struct pwrseq_target_data *pos;
419 struct pwrseq_target *target;
420 int ret, i;
421
422 for (i = 0; data[i]; i++) {
423 pos = data[i];
424
425 ret = pwrseq_check_target_deps(pos);
426 if (ret)
427 return ret;
428
429 target = pwrseq_target_new(pos);
430 if (!target)
431 return -ENOMEM;
432
433 target->unit = pwrseq_unit_setup(pos->unit, &pwrseq->units,
434 processed_units);
435 if (IS_ERR(target->unit)) {
436 ret = PTR_ERR(target->unit);
437 pwrseq_target_free(target);
438 return ret;
439 }
440
441 list_add_tail(&target->list, &pwrseq->targets);
442 }
443
444 return 0;
445}
446
447static int pwrseq_setup_targets(const struct pwrseq_target_data **targets,
448 struct pwrseq_device *pwrseq)
449{
450 struct radix_tree_root processed_units;
451 struct radix_tree_iter iter;
452 void __rcu **slot;
453 int ret;
454
455 INIT_RADIX_TREE(&processed_units, GFP_KERNEL);
456 ret = pwrseq_do_setup_targets(targets, pwrseq, &processed_units);
457 radix_tree_for_each_slot(slot, &processed_units, &iter, 0)
458 radix_tree_delete(&processed_units, iter.index);
459
460 return ret;
461}
462
463/**
464 * pwrseq_device_register() - Register a new power sequencer.
465 * @config: Configuration of the new power sequencing device.
466 *
467 * The config structure is only used during the call and can be freed after
468 * the function returns. The config structure *must* have the parent device
469 * as well as the match() callback and at least one target set.
470 *
471 * Returns:
472 * Returns the address of the new pwrseq device or ERR_PTR() on failure.
473 */
474struct pwrseq_device *
475pwrseq_device_register(const struct pwrseq_config *config)
476{
477 struct pwrseq_device *pwrseq;
478 int ret, id;
479
480 if (!config->parent || !config->match || !config->targets ||
481 !config->targets[0])
482 return ERR_PTR(-EINVAL);
483
484 pwrseq = kzalloc_obj(*pwrseq);
485 if (!pwrseq)
486 return ERR_PTR(-ENOMEM);
487
488 pwrseq->dev.type = &pwrseq_device_type;
489 pwrseq->dev.bus = &pwrseq_bus;
490 pwrseq->dev.parent = config->parent;
491 device_set_node(&pwrseq->dev, dev_fwnode(config->parent));
492 dev_set_drvdata(&pwrseq->dev, config->drvdata);
493
494 id = ida_alloc(&pwrseq_ida, GFP_KERNEL);
495 if (id < 0) {
496 kfree(pwrseq);
497 return ERR_PTR(id);
498 }
499
500 pwrseq->id = id;
501
502 /*
503 * From this point onwards the device's release() callback is
504 * responsible for freeing resources.
505 */
506 device_initialize(&pwrseq->dev);
507
508 pwrseq->owner = config->owner ?: THIS_MODULE;
509 pwrseq->match = config->match;
510
511 init_rwsem(&pwrseq->rw_lock);
512 mutex_init(&pwrseq->state_lock);
513 INIT_LIST_HEAD(&pwrseq->targets);
514 INIT_LIST_HEAD(&pwrseq->units);
515
516 ret = dev_set_name(&pwrseq->dev, "pwrseq.%d", pwrseq->id);
517 if (ret)
518 goto err_put_pwrseq;
519
520 ret = pwrseq_setup_targets(config->targets, pwrseq);
521 if (ret)
522 goto err_put_pwrseq;
523
524 scoped_guard(rwsem_write, &pwrseq_sem) {
525 ret = device_add(&pwrseq->dev);
526 if (ret)
527 goto err_put_pwrseq;
528 }
529
530 return pwrseq;
531
532err_put_pwrseq:
533 pwrseq_device_put(pwrseq);
534 return ERR_PTR(ret);
535}
536EXPORT_SYMBOL_GPL(pwrseq_device_register);
537
538/**
539 * pwrseq_device_unregister() - Unregister the power sequencer.
540 * @pwrseq: Power sequencer to unregister.
541 */
542void pwrseq_device_unregister(struct pwrseq_device *pwrseq)
543{
544 struct device *dev = &pwrseq->dev;
545 struct pwrseq_target *target;
546
547 scoped_guard(rwsem_write, &pwrseq_sem) {
548 guard(rwsem_write)(&pwrseq->rw_lock);
549
550 /*
551 * Holding rw_lock for write excludes all power on/off callers
552 * (they hold it for read), so it's safe to read enable_count
553 * here without taking the state_lock.
554 */
555 list_for_each_entry(target, &pwrseq->targets, list)
556 WARN(target->unit->enable_count,
557 "REMOVING POWER SEQUENCER WITH ACTIVE USERS\n");
558
559 device_del(dev);
560 }
561
562 pwrseq_device_put(pwrseq);
563}
564EXPORT_SYMBOL_GPL(pwrseq_device_unregister);
565
566static void devm_pwrseq_device_unregister(void *data)
567{
568 struct pwrseq_device *pwrseq = data;
569
570 pwrseq_device_unregister(pwrseq);
571}
572
573/**
574 * devm_pwrseq_device_register() - Managed variant of pwrseq_device_register().
575 * @dev: Managing device.
576 * @config: Configuration of the new power sequencing device.
577 *
578 * Returns:
579 * Returns the address of the new pwrseq device or ERR_PTR() on failure.
580 */
581struct pwrseq_device *
582devm_pwrseq_device_register(struct device *dev,
583 const struct pwrseq_config *config)
584{
585 struct pwrseq_device *pwrseq;
586 int ret;
587
588 pwrseq = pwrseq_device_register(config);
589 if (IS_ERR(pwrseq))
590 return pwrseq;
591
592 ret = devm_add_action_or_reset(dev, devm_pwrseq_device_unregister,
593 pwrseq);
594 if (ret)
595 return ERR_PTR(ret);
596
597 return pwrseq;
598}
599EXPORT_SYMBOL_GPL(devm_pwrseq_device_register);
600
601/**
602 * pwrseq_device_get_drvdata() - Get the driver private data associated with
603 * this sequencer.
604 * @pwrseq: Power sequencer object.
605 *
606 * Returns:
607 * Address of the private driver data.
608 */
609void *pwrseq_device_get_drvdata(struct pwrseq_device *pwrseq)
610{
611 return dev_get_drvdata(&pwrseq->dev);
612}
613EXPORT_SYMBOL_GPL(pwrseq_device_get_drvdata);
614
615struct pwrseq_match_data {
616 struct pwrseq_desc *desc;
617 struct device *dev;
618 const char *target;
619};
620
621static int pwrseq_match_device(struct device *pwrseq_dev, void *data)
622{
623 struct pwrseq_device *pwrseq = to_pwrseq_device(pwrseq_dev);
624 struct pwrseq_match_data *match_data = data;
625 struct pwrseq_target *target;
626 int ret;
627
628 lockdep_assert_held_read(&pwrseq_sem);
629
630 guard(rwsem_read)(&pwrseq->rw_lock);
631 if (!device_is_registered(&pwrseq->dev))
632 return 0;
633
634 ret = pwrseq->match(pwrseq, match_data->dev);
635 if (ret == PWRSEQ_NO_MATCH || ret < 0)
636 return ret;
637
638 /* We got the matching device, let's find the right target. */
639 list_for_each_entry(target, &pwrseq->targets, list) {
640 if (strcmp(target->name, match_data->target))
641 continue;
642
643 match_data->desc->target = target;
644 }
645
646 /*
647 * This device does not have this target. No point in deferring as it
648 * will not get a new target dynamically later.
649 */
650 if (!match_data->desc->target)
651 return -ENOENT;
652
653 if (!try_module_get(pwrseq->owner))
654 return -EPROBE_DEFER;
655
656 match_data->desc->pwrseq = pwrseq_device_get(pwrseq);
657
658 return PWRSEQ_MATCH_OK;
659}
660
661/**
662 * pwrseq_get() - Get the power sequencer associated with this device.
663 * @dev: Device for which to get the sequencer.
664 * @target: Name of the target exposed by the sequencer this device wants to
665 * reach.
666 *
667 * Returns:
668 * New power sequencer descriptor for use by the consumer driver or ERR_PTR()
669 * on failure.
670 */
671struct pwrseq_desc *pwrseq_get(struct device *dev, const char *target)
672{
673 struct pwrseq_match_data match_data;
674 int ret;
675
676 struct pwrseq_desc *desc __free(kfree) = kzalloc_obj(*desc);
677 if (!desc)
678 return ERR_PTR(-ENOMEM);
679
680 match_data.desc = desc;
681 match_data.dev = dev;
682 match_data.target = target;
683
684 guard(rwsem_read)(&pwrseq_sem);
685
686 ret = bus_for_each_dev(&pwrseq_bus, NULL, &match_data,
687 pwrseq_match_device);
688 if (ret < 0)
689 return ERR_PTR(ret);
690 if (ret == PWRSEQ_NO_MATCH)
691 /* No device matched. */
692 return ERR_PTR(-EPROBE_DEFER);
693
694 return_ptr(desc);
695}
696EXPORT_SYMBOL_GPL(pwrseq_get);
697
698/**
699 * pwrseq_put() - Release the power sequencer descriptor.
700 * @desc: Descriptor to release.
701 */
702void pwrseq_put(struct pwrseq_desc *desc)
703{
704 struct pwrseq_device *pwrseq;
705
706 if (!desc)
707 return;
708
709 pwrseq = desc->pwrseq;
710
711 if (desc->powered_on)
712 pwrseq_disable(desc);
713
714 kfree(desc);
715 module_put(pwrseq->owner);
716 pwrseq_device_put(pwrseq);
717}
718EXPORT_SYMBOL_GPL(pwrseq_put);
719
720static void devm_pwrseq_put(void *data)
721{
722 struct pwrseq_desc *desc = data;
723
724 pwrseq_put(desc);
725}
726
727/**
728 * devm_pwrseq_get() - Managed variant of pwrseq_get().
729 * @dev: Device for which to get the sequencer and which also manages its
730 * lifetime.
731 * @target: Name of the target exposed by the sequencer this device wants to
732 * reach.
733 *
734 * Returns:
735 * New power sequencer descriptor for use by the consumer driver or ERR_PTR()
736 * on failure.
737 */
738struct pwrseq_desc *devm_pwrseq_get(struct device *dev, const char *target)
739{
740 struct pwrseq_desc *desc;
741 int ret;
742
743 desc = pwrseq_get(dev, target);
744 if (IS_ERR(desc))
745 return desc;
746
747 ret = devm_add_action_or_reset(dev, devm_pwrseq_put, desc);
748 if (ret)
749 return ERR_PTR(ret);
750
751 return desc;
752}
753EXPORT_SYMBOL_GPL(devm_pwrseq_get);
754
755static int pwrseq_unit_enable(struct pwrseq_device *pwrseq,
756 struct pwrseq_unit *target);
757static int pwrseq_unit_disable(struct pwrseq_device *pwrseq,
758 struct pwrseq_unit *target);
759
760static int pwrseq_unit_enable_deps(struct pwrseq_device *pwrseq,
761 struct list_head *list)
762{
763 struct pwrseq_unit_dep *pos;
764 int ret = 0;
765
766 list_for_each_entry(pos, list, list) {
767 ret = pwrseq_unit_enable(pwrseq, pos->unit);
768 if (ret) {
769 list_for_each_entry_continue_reverse(pos, list, list)
770 pwrseq_unit_disable(pwrseq, pos->unit);
771 break;
772 }
773 }
774
775 return ret;
776}
777
778static int pwrseq_unit_disable_deps(struct pwrseq_device *pwrseq,
779 struct list_head *list)
780{
781 struct pwrseq_unit_dep *pos;
782 int ret = 0;
783
784 list_for_each_entry_reverse(pos, list, list) {
785 ret = pwrseq_unit_disable(pwrseq, pos->unit);
786 if (ret) {
787 list_for_each_entry_continue(pos, list, list)
788 pwrseq_unit_enable(pwrseq, pos->unit);
789 break;
790 }
791 }
792
793 return ret;
794}
795
796static int pwrseq_unit_enable(struct pwrseq_device *pwrseq,
797 struct pwrseq_unit *unit)
798{
799 int ret;
800
801 lockdep_assert_held_read(&pwrseq->rw_lock);
802 lockdep_assert_held(&pwrseq->state_lock);
803
804 if (unit->enable_count != 0) {
805 unit->enable_count++;
806 return 0;
807 }
808
809 ret = pwrseq_unit_enable_deps(pwrseq, &unit->deps);
810 if (ret) {
811 dev_err(&pwrseq->dev,
812 "Failed to enable dependencies before power-on for target '%s': %d\n",
813 unit->name, ret);
814 return ret;
815 }
816
817 if (unit->enable) {
818 ret = unit->enable(pwrseq);
819 if (ret) {
820 dev_err(&pwrseq->dev,
821 "Failed to enable target '%s': %d\n",
822 unit->name, ret);
823 pwrseq_unit_disable_deps(pwrseq, &unit->deps);
824 return ret;
825 }
826 }
827
828 unit->enable_count++;
829
830 return 0;
831}
832
833static int pwrseq_unit_disable(struct pwrseq_device *pwrseq,
834 struct pwrseq_unit *unit)
835{
836 int ret;
837
838 lockdep_assert_held_read(&pwrseq->rw_lock);
839 lockdep_assert_held(&pwrseq->state_lock);
840
841 if (unit->enable_count == 0) {
842 WARN(1, "Unmatched power-off for target '%s'\n",
843 unit->name);
844 return -EBUSY;
845 }
846
847 if (unit->enable_count != 1) {
848 unit->enable_count--;
849 return 0;
850 }
851
852 if (unit->disable) {
853 ret = unit->disable(pwrseq);
854 if (ret) {
855 dev_err(&pwrseq->dev,
856 "Failed to disable target '%s': %d\n",
857 unit->name, ret);
858 return ret;
859 }
860 }
861
862 ret = pwrseq_unit_disable_deps(pwrseq, &unit->deps);
863 if (ret) {
864 dev_err(&pwrseq->dev,
865 "Failed to disable dependencies after power-off for target '%s': %d\n",
866 unit->name, ret);
867 if (unit->enable)
868 unit->enable(pwrseq);
869 return ret;
870 }
871
872 unit->enable_count--;
873
874 return 0;
875}
876
877/**
878 * pwrseq_enable() - Issue a power-on request on behalf of the consumer
879 * device.
880 * @desc: Descriptor referencing the power sequencer.
881 *
882 * This function tells the power sequencer that the consumer wants to be
883 * powered-up. The sequencer may already have powered-up the device in which
884 * case the function returns 0. If the power-up sequence is already in
885 * progress, the function will block until it's done and return 0. If this is
886 * the first request, the device will be powered up.
887 *
888 * Returns:
889 * 0 on success, negative error number on failure.
890 */
891int pwrseq_enable(struct pwrseq_desc *desc)
892{
893 struct pwrseq_device *pwrseq;
894 struct pwrseq_target *target;
895 struct pwrseq_unit *unit;
896 int ret;
897
898 might_sleep();
899
900 if (!desc || desc->powered_on)
901 return 0;
902
903 pwrseq = desc->pwrseq;
904 target = desc->target;
905 unit = target->unit;
906
907 guard(rwsem_read)(&pwrseq->rw_lock);
908 if (!device_is_registered(&pwrseq->dev))
909 return -ENODEV;
910
911 scoped_guard(mutex, &pwrseq->state_lock) {
912 ret = pwrseq_unit_enable(pwrseq, unit);
913 if (!ret)
914 desc->powered_on = true;
915 }
916 if (ret)
917 return ret;
918
919 if (target->post_enable) {
920 ret = target->post_enable(pwrseq);
921 if (ret) {
922 scoped_guard(mutex, &pwrseq->state_lock) {
923 pwrseq_unit_disable(pwrseq, unit);
924 desc->powered_on = false;
925 }
926 }
927 }
928
929 return ret;
930}
931EXPORT_SYMBOL_GPL(pwrseq_enable);
932
933/**
934 * pwrseq_disable() - Issue a power-off request on behalf of the consumer
935 * device.
936 * @desc: Descriptor referencing the power sequencer.
937 *
938 * This undoes the effects of pwrseq_enable(). It issues a power-off request
939 * on behalf of the consumer and when the last remaining user does so, the
940 * power-down sequence will be started. If one is in progress, the function
941 * will block until it's complete and then return.
942 *
943 * Returns:
944 * 0 on success, negative error number on failure.
945 */
946int pwrseq_disable(struct pwrseq_desc *desc)
947{
948 struct pwrseq_device *pwrseq;
949 struct pwrseq_unit *unit;
950 int ret;
951
952 might_sleep();
953
954 if (!desc || !desc->powered_on)
955 return 0;
956
957 pwrseq = desc->pwrseq;
958 unit = desc->target->unit;
959
960 guard(rwsem_read)(&pwrseq->rw_lock);
961 if (!device_is_registered(&pwrseq->dev))
962 return -ENODEV;
963
964 guard(mutex)(&pwrseq->state_lock);
965
966 ret = pwrseq_unit_disable(pwrseq, unit);
967 if (!ret)
968 desc->powered_on = false;
969
970 return ret;
971}
972EXPORT_SYMBOL_GPL(pwrseq_disable);
973
974/**
975 * pwrseq_to_device() - Get the pwrseq device pointer from a descriptor.
976 * @desc: Descriptor referencing the power sequencer.
977 *
978 * Return the 'dev' pointer of the power sequencer device associated with @desc.
979 * Consumer drivers can use this to query the pwrseq provider's device tree
980 * node, for example to check for the existence of specific properties.
981 *
982 * Since pwrseq_get() already takes a reference to the pwrseq device, this
983 * function does not take an additional reference.
984 *
985 * Returns:
986 * Pointer to the pwrseq struct device, or NULL if @desc is NULL.
987 */
988struct device *pwrseq_to_device(struct pwrseq_desc *desc)
989{
990 if (!desc)
991 return NULL;
992
993 return &desc->pwrseq->dev;
994}
995EXPORT_SYMBOL_GPL(pwrseq_to_device);
996
997#if IS_ENABLED(CONFIG_DEBUG_FS)
998
999struct pwrseq_debugfs_count_ctx {
1000 struct device *dev;
1001 loff_t index;
1002};
1003
1004static int pwrseq_debugfs_seq_count(struct device *dev, void *data)
1005{
1006 struct pwrseq_debugfs_count_ctx *ctx = data;
1007
1008 ctx->dev = dev;
1009
1010 return ctx->index-- ? 0 : 1;
1011}
1012
1013static void *pwrseq_debugfs_seq_start(struct seq_file *seq, loff_t *pos)
1014{
1015 struct pwrseq_debugfs_count_ctx ctx;
1016
1017 ctx.dev = NULL;
1018 ctx.index = *pos;
1019
1020 /*
1021 * Hold the lock for the entire printout to prevent device removal.
1022 * Reference counts are managed by start()/next()/stop() as required
1023 * by the seq_file contract.
1024 */
1025 down_read(&pwrseq_sem);
1026
1027 bus_for_each_dev(&pwrseq_bus, NULL, &ctx, pwrseq_debugfs_seq_count);
1028 if (!ctx.index)
1029 return NULL;
1030
1031 return get_device(ctx.dev);
1032}
1033
1034static void *pwrseq_debugfs_seq_next(struct seq_file *seq, void *data,
1035 loff_t *pos)
1036{
1037 struct device *curr = data;
1038
1039 ++*pos;
1040
1041 struct device *next = bus_find_next_device(&pwrseq_bus, curr);
1042
1043 put_device(curr);
1044 return next;
1045}
1046
1047static void pwrseq_debugfs_seq_show_target(struct seq_file *seq,
1048 struct pwrseq_target *target)
1049{
1050 seq_printf(seq, " target: [%s] (target unit: [%s])\n",
1051 target->name, target->unit->name);
1052}
1053
1054static void pwrseq_debugfs_seq_show_unit(struct seq_file *seq,
1055 struct pwrseq_unit *unit)
1056{
1057 struct pwrseq_unit_dep *ref;
1058
1059 seq_printf(seq, " unit: [%s] - enable count: %u\n",
1060 unit->name, unit->enable_count);
1061
1062 if (list_empty(&unit->deps))
1063 return;
1064
1065 seq_puts(seq, " dependencies:\n");
1066 list_for_each_entry(ref, &unit->deps, list)
1067 seq_printf(seq, " [%s]\n", ref->unit->name);
1068}
1069
1070static int pwrseq_debugfs_seq_show(struct seq_file *seq, void *data)
1071{
1072 struct device *dev = data;
1073 struct pwrseq_device *pwrseq = to_pwrseq_device(dev);
1074 struct pwrseq_target *target;
1075 struct pwrseq_unit *unit;
1076
1077 seq_printf(seq, "%s (%s):\n", dev_name(dev), dev_name(dev->parent));
1078
1079 seq_puts(seq, " targets:\n");
1080 list_for_each_entry(target, &pwrseq->targets, list)
1081 pwrseq_debugfs_seq_show_target(seq, target);
1082
1083 seq_puts(seq, " units:\n");
1084 list_for_each_entry(unit, &pwrseq->units, list)
1085 pwrseq_debugfs_seq_show_unit(seq, unit);
1086
1087 return 0;
1088}
1089
1090static void pwrseq_debugfs_seq_stop(struct seq_file *seq, void *data)
1091{
1092 if (data)
1093 put_device(data);
1094 up_read(&pwrseq_sem);
1095}
1096
1097static const struct seq_operations pwrseq_debugfs_sops = {
1098 .start = pwrseq_debugfs_seq_start,
1099 .next = pwrseq_debugfs_seq_next,
1100 .show = pwrseq_debugfs_seq_show,
1101 .stop = pwrseq_debugfs_seq_stop,
1102};
1103DEFINE_SEQ_ATTRIBUTE(pwrseq_debugfs);
1104
1105static struct dentry *pwrseq_debugfs_dentry;
1106
1107#endif /* CONFIG_DEBUG_FS */
1108
1109static int __init pwrseq_init(void)
1110{
1111 int ret;
1112
1113 ret = bus_register(&pwrseq_bus);
1114 if (ret) {
1115 pr_err("Failed to register the power sequencer bus\n");
1116 return ret;
1117 }
1118
1119#if IS_ENABLED(CONFIG_DEBUG_FS)
1120 pwrseq_debugfs_dentry = debugfs_create_file("pwrseq", 0444, NULL, NULL,
1121 &pwrseq_debugfs_fops);
1122#endif /* CONFIG_DEBUG_FS */
1123
1124 return 0;
1125}
1126subsys_initcall(pwrseq_init);
1127
1128static void __exit pwrseq_exit(void)
1129{
1130#if IS_ENABLED(CONFIG_DEBUG_FS)
1131 debugfs_remove_recursive(pwrseq_debugfs_dentry);
1132#endif /* CONFIG_DEBUG_FS */
1133
1134 bus_unregister(&pwrseq_bus);
1135}
1136module_exit(pwrseq_exit);
1137
1138MODULE_AUTHOR("Bartosz Golaszewski <[email protected]>");
1139MODULE_DESCRIPTION("Power Sequencing subsystem core");
1140MODULE_LICENSE("GPL");