hc
2024-09-20 a36159eec6ca17402b0e146b86efaf76568dc353
kernel/drivers/clk/clk.c
....@@ -1,11 +1,7 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * Copyright (C) 2010-2011 Canonical Ltd <jeremy.kerr@canonical.com>
34 * Copyright (C) 2011-2012 Linaro Ltd <mturquette@linaro.org>
4
- * Copyright (c) 2017-2019, The Linux Foundation. All rights reserved.
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License version 2 as
8
- * published by the Free Software Foundation.
95 *
106 * Standard functionality for the common clock API. See Documentation/driver-api/clk.rst
117 */
....@@ -25,10 +21,6 @@
2521 #include <linux/pm_runtime.h>
2622 #include <linux/sched.h>
2723 #include <linux/clkdev.h>
28
-#include <linux/uaccess.h>
29
-#include <linux/of_platform.h>
30
-#include <linux/pm_opp.h>
31
-#include <linux/regulator/consumer.h>
3224
3325 #include "clk.h"
3426
....@@ -51,17 +43,15 @@
5143 NULL,
5244 };
5345
54
-/*
55
- * clk_rate_change_list is used during clk_core_set_rate_nolock() calls to
56
- * handle vdd_class vote tracking. core->rate_change_node is added to
57
- * clk_rate_change_list when core->new_rate requires a different voltage level
58
- * (core->new_vdd_class_vote) than core->vdd_class_vote. Elements are removed
59
- * from the list after unvoting core->vdd_class_vote immediately before
60
- * returning from clk_core_set_rate_nolock().
61
- */
62
-static LIST_HEAD(clk_rate_change_list);
63
-
6446 /*** private data structures ***/
47
+
48
+struct clk_parent_map {
49
+ const struct clk_hw *hw;
50
+ struct clk_core *core;
51
+ const char *fw_name;
52
+ const char *name;
53
+ int index;
54
+};
6555
6656 struct clk_core {
6757 const char *name;
....@@ -69,11 +59,11 @@
6959 struct clk_hw *hw;
7060 struct module *owner;
7161 struct device *dev;
62
+ struct device_node *of_node;
7263 struct clk_core *parent;
73
- const char **parent_names;
74
- struct clk_core **parents;
75
- unsigned int num_parents;
76
- unsigned int new_parent_index;
64
+ struct clk_parent_map *parents;
65
+ u8 num_parents;
66
+ u8 new_parent_index;
7767 unsigned long rate;
7868 unsigned long req_rate;
7969 unsigned long new_rate;
....@@ -87,8 +77,6 @@
8777 unsigned int enable_count;
8878 unsigned int prepare_count;
8979 unsigned int protect_count;
90
- bool need_handoff_enable;
91
- bool need_handoff_prepare;
9280 unsigned long min_rate;
9381 unsigned long max_rate;
9482 unsigned long accuracy;
....@@ -98,17 +86,11 @@
9886 struct hlist_node child_node;
9987 struct hlist_head clks;
10088 unsigned int notifier_count;
101
-#ifdef CONFIG_COMMON_CLK_DEBUGFS
89
+#ifdef CONFIG_DEBUG_FS
10290 struct dentry *dentry;
10391 struct hlist_node debug_node;
10492 #endif
10593 struct kref ref;
106
- struct clk_vdd_class *vdd_class;
107
- int vdd_class_vote;
108
- int new_vdd_class_vote;
109
- struct list_head rate_change_node;
110
- unsigned long *rate_max;
111
- int num_rate_max;
11294 };
11395
11496 #define CREATE_TRACE_POINTS
....@@ -116,6 +98,7 @@
11698
11799 struct clk {
118100 struct clk_core *core;
101
+ struct device *dev;
119102 const char *dev_id;
120103 const char *con_id;
121104 unsigned long min_rate;
....@@ -270,6 +253,17 @@
270253 }
271254 }
272255
256
+ /*
257
+ * This could be called with the enable lock held, or from atomic
258
+ * context. If the parent isn't enabled already, we can't do
259
+ * anything here. We can also assume this clock isn't enabled.
260
+ */
261
+ if ((core->flags & CLK_OPS_PARENT_ENABLE) && core->parent)
262
+ if (!clk_core_is_enabled(core->parent)) {
263
+ ret = false;
264
+ goto done;
265
+ }
266
+
273267 ret = core->ops->is_enabled(core->hw);
274268 done:
275269 if (core->rpm_enabled)
....@@ -353,17 +347,124 @@
353347 return NULL;
354348 }
355349
350
+#ifdef CONFIG_OF
351
+static int of_parse_clkspec(const struct device_node *np, int index,
352
+ const char *name, struct of_phandle_args *out_args);
353
+static struct clk_hw *
354
+of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec);
355
+#else
356
+static inline int of_parse_clkspec(const struct device_node *np, int index,
357
+ const char *name,
358
+ struct of_phandle_args *out_args)
359
+{
360
+ return -ENOENT;
361
+}
362
+static inline struct clk_hw *
363
+of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
364
+{
365
+ return ERR_PTR(-ENOENT);
366
+}
367
+#endif
368
+
369
+/**
370
+ * clk_core_get - Find the clk_core parent of a clk
371
+ * @core: clk to find parent of
372
+ * @p_index: parent index to search for
373
+ *
374
+ * This is the preferred method for clk providers to find the parent of a
375
+ * clk when that parent is external to the clk controller. The parent_names
376
+ * array is indexed and treated as a local name matching a string in the device
377
+ * node's 'clock-names' property or as the 'con_id' matching the device's
378
+ * dev_name() in a clk_lookup. This allows clk providers to use their own
379
+ * namespace instead of looking for a globally unique parent string.
380
+ *
381
+ * For example the following DT snippet would allow a clock registered by the
382
+ * clock-controller@c001 that has a clk_init_data::parent_data array
383
+ * with 'xtal' in the 'name' member to find the clock provided by the
384
+ * clock-controller@f00abcd without needing to get the globally unique name of
385
+ * the xtal clk.
386
+ *
387
+ * parent: clock-controller@f00abcd {
388
+ * reg = <0xf00abcd 0xabcd>;
389
+ * #clock-cells = <0>;
390
+ * };
391
+ *
392
+ * clock-controller@c001 {
393
+ * reg = <0xc001 0xf00d>;
394
+ * clocks = <&parent>;
395
+ * clock-names = "xtal";
396
+ * #clock-cells = <1>;
397
+ * };
398
+ *
399
+ * Returns: -ENOENT when the provider can't be found or the clk doesn't
400
+ * exist in the provider or the name can't be found in the DT node or
401
+ * in a clkdev lookup. NULL when the provider knows about the clk but it
402
+ * isn't provided on this system.
403
+ * A valid clk_core pointer when the clk can be found in the provider.
404
+ */
405
+static struct clk_core *clk_core_get(struct clk_core *core, u8 p_index)
406
+{
407
+ const char *name = core->parents[p_index].fw_name;
408
+ int index = core->parents[p_index].index;
409
+ struct clk_hw *hw = ERR_PTR(-ENOENT);
410
+ struct device *dev = core->dev;
411
+ const char *dev_id = dev ? dev_name(dev) : NULL;
412
+ struct device_node *np = core->of_node;
413
+ struct of_phandle_args clkspec;
414
+
415
+ if (np && (name || index >= 0) &&
416
+ !of_parse_clkspec(np, index, name, &clkspec)) {
417
+ hw = of_clk_get_hw_from_clkspec(&clkspec);
418
+ of_node_put(clkspec.np);
419
+ } else if (name) {
420
+ /*
421
+ * If the DT search above couldn't find the provider fallback to
422
+ * looking up via clkdev based clk_lookups.
423
+ */
424
+ hw = clk_find_hw(dev_id, name);
425
+ }
426
+
427
+ if (IS_ERR(hw))
428
+ return ERR_CAST(hw);
429
+
430
+ return hw->core;
431
+}
432
+
433
+static void clk_core_fill_parent_index(struct clk_core *core, u8 index)
434
+{
435
+ struct clk_parent_map *entry = &core->parents[index];
436
+ struct clk_core *parent = ERR_PTR(-ENOENT);
437
+
438
+ if (entry->hw) {
439
+ parent = entry->hw->core;
440
+ /*
441
+ * We have a direct reference but it isn't registered yet?
442
+ * Orphan it and let clk_reparent() update the orphan status
443
+ * when the parent is registered.
444
+ */
445
+ if (!parent)
446
+ parent = ERR_PTR(-EPROBE_DEFER);
447
+ } else {
448
+ parent = clk_core_get(core, index);
449
+ if (PTR_ERR(parent) == -ENOENT && entry->name)
450
+ parent = clk_core_lookup(entry->name);
451
+ }
452
+
453
+ /* Only cache it if it's not an error */
454
+ if (!IS_ERR(parent))
455
+ entry->core = parent;
456
+}
457
+
356458 static struct clk_core *clk_core_get_parent_by_index(struct clk_core *core,
357459 u8 index)
358460 {
359
- if (!core || index >= core->num_parents)
461
+ if (!core || index >= core->num_parents || !core->parents)
360462 return NULL;
361463
362
- if (!core->parents[index])
363
- core->parents[index] =
364
- clk_core_lookup(core->parent_names[index]);
464
+ if (!core->parents[index].core)
465
+ clk_core_fill_parent_index(core, index);
365466
366
- return core->parents[index];
467
+ return core->parents[index].core;
367468 }
368469
369470 struct clk_hw *
....@@ -384,23 +485,18 @@
384485
385486 static unsigned long clk_core_get_rate_nolock(struct clk_core *core)
386487 {
387
- unsigned long ret;
488
+ if (!core)
489
+ return 0;
388490
389
- if (!core) {
390
- ret = 0;
391
- goto out;
392
- }
491
+ if (!core->num_parents || core->parent)
492
+ return core->rate;
393493
394
- ret = core->rate;
395
-
396
- if (!core->num_parents)
397
- goto out;
398
-
399
- if (!core->parent)
400
- ret = 0;
401
-
402
-out:
403
- return ret;
494
+ /*
495
+ * Clk must have a parent because num_parents > 0 but the parent isn't
496
+ * known yet. Best to return 0 as the rate of this clk until we can
497
+ * properly recalc the rate based on the parent's rate.
498
+ */
499
+ return 0;
404500 }
405501
406502 unsigned long clk_hw_get_rate(const struct clk_hw *hw)
....@@ -409,19 +505,13 @@
409505 }
410506 EXPORT_SYMBOL_GPL(clk_hw_get_rate);
411507
412
-static unsigned long __clk_get_accuracy(struct clk_core *core)
508
+static unsigned long clk_core_get_accuracy_no_lock(struct clk_core *core)
413509 {
414510 if (!core)
415511 return 0;
416512
417513 return core->accuracy;
418514 }
419
-
420
-unsigned long __clk_get_flags(struct clk *clk)
421
-{
422
- return !clk ? 0 : clk->core->flags;
423
-}
424
-EXPORT_SYMBOL_GPL(__clk_get_flags);
425515
426516 unsigned long clk_hw_get_flags(const struct clk_hw *hw)
427517 {
....@@ -439,6 +529,7 @@
439529 {
440530 return clk_core_rate_is_protected(hw->core);
441531 }
532
+EXPORT_SYMBOL_GPL(clk_hw_rate_is_protected);
442533
443534 bool clk_hw_is_enabled(const struct clk_hw *hw)
444535 {
....@@ -541,6 +632,8 @@
541632 {
542633 struct clk *clk_user;
543634
635
+ lockdep_assert_held(&prepare_lock);
636
+
544637 *min_rate = core->min_rate;
545638 *max_rate = core->max_rate;
546639
....@@ -549,6 +642,24 @@
549642
550643 hlist_for_each_entry(clk_user, &core->clks, clks_node)
551644 *max_rate = min(*max_rate, clk_user->max_rate);
645
+}
646
+
647
+static bool clk_core_check_boundaries(struct clk_core *core,
648
+ unsigned long min_rate,
649
+ unsigned long max_rate)
650
+{
651
+ struct clk *user;
652
+
653
+ lockdep_assert_held(&prepare_lock);
654
+
655
+ if (min_rate > core->max_rate || max_rate < core->min_rate)
656
+ return false;
657
+
658
+ hlist_for_each_entry(user, &core->clks, clks_node)
659
+ if (min_rate > user->max_rate || max_rate < user->min_rate)
660
+ return false;
661
+
662
+ return true;
552663 }
553664
554665 void clk_hw_set_rate_range(struct clk_hw *hw, unsigned long min_rate,
....@@ -560,29 +671,15 @@
560671 EXPORT_SYMBOL_GPL(clk_hw_set_rate_range);
561672
562673 /*
563
- * Aggregate the rate of all the enabled child nodes and exclude that
564
- * of the child node for which this request was made.
565
- */
566
-unsigned long clk_aggregate_rate(struct clk_hw *hw,
567
- const struct clk_core *parent)
568
-{
569
- struct clk_core *child;
570
- unsigned long aggre_rate = 0;
571
-
572
- hlist_for_each_entry(child, &parent->children, child_node) {
573
- if (child->enable_count &&
574
- strcmp(child->name, hw->init->name))
575
- aggre_rate = max(child->rate, aggre_rate);
576
- }
577
-
578
- return aggre_rate;
579
-}
580
-EXPORT_SYMBOL_GPL(clk_aggregate_rate);
581
-
582
-/*
674
+ * __clk_mux_determine_rate - clk_ops::determine_rate implementation for a mux type clk
675
+ * @hw: mux type clk to determine rate on
676
+ * @req: rate request, also used to return preferred parent and frequencies
677
+ *
583678 * Helper for finding best parent to provide a given frequency. This can be used
584679 * directly as a determine_rate callback (e.g. for a mux), or from a more
585680 * complex clock that may combine a mux with other operations.
681
+ *
682
+ * Returns: 0 on success, -EERROR value on error
586683 */
587684 int __clk_mux_determine_rate(struct clk_hw *hw,
588685 struct clk_rate_request *req)
....@@ -597,201 +694,6 @@
597694 return clk_mux_determine_rate_flags(hw, req, CLK_MUX_ROUND_CLOSEST);
598695 }
599696 EXPORT_SYMBOL_GPL(__clk_mux_determine_rate_closest);
600
-
601
-/*
602
- * Find the voltage level required for a given clock rate.
603
- */
604
-static int clk_find_vdd_level(struct clk_core *clk, unsigned long rate)
605
-{
606
- int level;
607
-
608
- /*
609
- * For certain PLLs, due to the limitation in the bits allocated for
610
- * programming the fractional divider, the actual rate of the PLL will
611
- * be slightly higher than the requested rate (in the order of several
612
- * Hz). To accommodate this difference, convert the FMAX rate and the
613
- * clock frequency to KHz and use that for deriving the voltage level.
614
- */
615
- for (level = 0; level < clk->num_rate_max; level++)
616
- if (DIV_ROUND_CLOSEST(rate, 1000) <=
617
- DIV_ROUND_CLOSEST(clk->rate_max[level], 1000) &&
618
- clk->rate_max[level] > 0)
619
- break;
620
-
621
- if (level == clk->num_rate_max) {
622
- pr_err("Rate %lu for %s is greater than highest Fmax\n", rate,
623
- clk->name);
624
- return -EINVAL;
625
- }
626
-
627
- return level;
628
-}
629
-
630
-/*
631
- * Update voltage level given the current votes.
632
- */
633
-static int clk_update_vdd(struct clk_vdd_class *vdd_class)
634
-{
635
- int level, rc = 0, i, ignore;
636
- struct regulator **r = vdd_class->regulator;
637
- int *uv = vdd_class->vdd_uv;
638
- int n_reg = vdd_class->num_regulators;
639
- int cur_lvl = vdd_class->cur_level;
640
- int max_lvl = vdd_class->num_levels - 1;
641
- int cur_base = cur_lvl * n_reg;
642
- int new_base;
643
-
644
- /* aggregate votes */
645
- for (level = max_lvl; level > 0; level--)
646
- if (vdd_class->level_votes[level])
647
- break;
648
-
649
- if (level == cur_lvl)
650
- return 0;
651
-
652
- max_lvl = max_lvl * n_reg;
653
- new_base = level * n_reg;
654
-
655
- for (i = 0; i < vdd_class->num_regulators; i++) {
656
- pr_debug("Set Voltage level Min %d, Max %d\n", uv[new_base + i],
657
- uv[max_lvl + i]);
658
- rc = regulator_set_voltage(r[i], uv[new_base + i], INT_MAX);
659
- if (rc)
660
- goto set_voltage_fail;
661
-
662
- if (cur_lvl == 0 || cur_lvl == vdd_class->num_levels)
663
- rc = regulator_enable(r[i]);
664
- else if (level == 0)
665
- rc = regulator_disable(r[i]);
666
- if (rc)
667
- goto enable_disable_fail;
668
- }
669
-
670
- if (vdd_class->set_vdd && !vdd_class->num_regulators)
671
- rc = vdd_class->set_vdd(vdd_class, level);
672
-
673
- if (!rc)
674
- vdd_class->cur_level = level;
675
-
676
- return rc;
677
-
678
-enable_disable_fail:
679
- regulator_set_voltage(r[i], uv[cur_base + i], INT_MAX);
680
-
681
-set_voltage_fail:
682
- for (i--; i >= 0; i--) {
683
- regulator_set_voltage(r[i], uv[cur_base + i], INT_MAX);
684
- if (cur_lvl == 0 || cur_lvl == vdd_class->num_levels)
685
- regulator_disable(r[i]);
686
- else if (level == 0)
687
- ignore = regulator_enable(r[i]);
688
- }
689
-
690
- return rc;
691
-}
692
-
693
-/*
694
- * Vote for a voltage level.
695
- */
696
-int clk_vote_vdd_level(struct clk_vdd_class *vdd_class, int level)
697
-{
698
- int rc = 0;
699
-
700
- if (level >= vdd_class->num_levels)
701
- return -EINVAL;
702
-
703
- mutex_lock(&vdd_class->lock);
704
-
705
- vdd_class->level_votes[level]++;
706
-
707
- rc = clk_update_vdd(vdd_class);
708
- if (rc)
709
- vdd_class->level_votes[level]--;
710
-
711
- mutex_unlock(&vdd_class->lock);
712
-
713
- return rc;
714
-}
715
-EXPORT_SYMBOL_GPL(clk_vote_vdd_level);
716
-
717
-/*
718
- * Remove vote for a voltage level.
719
- */
720
-int clk_unvote_vdd_level(struct clk_vdd_class *vdd_class, int level)
721
-{
722
- int rc = 0;
723
-
724
- if (level >= vdd_class->num_levels)
725
- return -EINVAL;
726
-
727
- mutex_lock(&vdd_class->lock);
728
-
729
- if (WARN(!vdd_class->level_votes[level],
730
- "Reference counts are incorrect for %s level %d\n",
731
- vdd_class->class_name, level)) {
732
- rc = -EINVAL;
733
- goto out;
734
- }
735
-
736
- vdd_class->level_votes[level]--;
737
-
738
- rc = clk_update_vdd(vdd_class);
739
- if (rc)
740
- vdd_class->level_votes[level]++;
741
-
742
-out:
743
- mutex_unlock(&vdd_class->lock);
744
- return rc;
745
-}
746
-EXPORT_SYMBOL_GPL(clk_unvote_vdd_level);
747
-
748
-/*
749
- * Vote for a voltage level corresponding to a clock's rate.
750
- */
751
-int clk_vote_rate_vdd(struct clk_core *core, unsigned long rate)
752
-{
753
- int level;
754
-
755
- if (!core->vdd_class)
756
- return 0;
757
-
758
- level = clk_find_vdd_level(core, rate);
759
- if (level < 0)
760
- return level;
761
-
762
- return clk_vote_vdd_level(core->vdd_class, level);
763
-}
764
-EXPORT_SYMBOL_GPL(clk_vote_rate_vdd);
765
-
766
-/*
767
- * Remove vote for a voltage level corresponding to a clock's rate.
768
- */
769
-void clk_unvote_rate_vdd(struct clk_core *core, unsigned long rate)
770
-{
771
- int level;
772
-
773
- if (!core->vdd_class)
774
- return;
775
-
776
- level = clk_find_vdd_level(core, rate);
777
- if (level < 0)
778
- return;
779
-
780
- clk_unvote_vdd_level(core->vdd_class, level);
781
-}
782
-EXPORT_SYMBOL_GPL(clk_unvote_rate_vdd);
783
-
784
-static bool clk_is_rate_level_valid(struct clk_core *core, unsigned long rate)
785
-{
786
- int level;
787
-
788
- if (!core->vdd_class)
789
- return true;
790
-
791
- level = clk_find_vdd_level(core, rate);
792
-
793
- return level >= 0;
794
-}
795697
796698 /*** clk api ***/
797699
....@@ -901,7 +803,7 @@
901803 * clk_rate_exclusive_get - get exclusivity over the clk rate control
902804 * @clk: the clk over which the exclusity of rate control is requested
903805 *
904
- * clk_rate_exlusive_get() begins a critical section during which a clock
806
+ * clk_rate_exclusive_get() begins a critical section during which a clock
905807 * consumer cannot tolerate any other consumer making any operation on the
906808 * clock which could result in a rate change or rate glitch. Exclusive clocks
907809 * cannot have their rate changed, either directly or indirectly due to changes
....@@ -957,17 +859,9 @@
957859 if (core->ops->unprepare)
958860 core->ops->unprepare(core->hw);
959861
960
- clk_pm_runtime_put(core);
961
-
962862 trace_clk_unprepare_complete(core);
963
-
964
- if (core->vdd_class) {
965
- clk_unvote_vdd_level(core->vdd_class, core->vdd_class_vote);
966
- core->vdd_class_vote = 0;
967
- core->new_vdd_class_vote = 0;
968
- }
969
-
970863 clk_core_unprepare(core->parent);
864
+ clk_pm_runtime_put(core);
971865 }
972866
973867 static void clk_core_unprepare_lock(struct clk_core *core)
....@@ -1017,28 +911,13 @@
1017911
1018912 trace_clk_prepare(core);
1019913
1020
- ret = clk_vote_rate_vdd(core, core->rate);
1021
- if (ret) {
1022
- clk_core_unprepare(core->parent);
1023
- return ret;
1024
- }
1025
- if (core->vdd_class) {
1026
- core->vdd_class_vote
1027
- = clk_find_vdd_level(core, core->rate);
1028
- core->new_vdd_class_vote = core->vdd_class_vote;
1029
- }
1030
-
1031914 if (core->ops->prepare)
1032915 ret = core->ops->prepare(core->hw);
1033916
1034917 trace_clk_prepare_complete(core);
1035918
1036
- if (ret) {
1037
- clk_unvote_rate_vdd(core, core->rate);
1038
- core->vdd_class_vote = 0;
1039
- core->new_vdd_class_vote = 0;
919
+ if (ret)
1040920 goto unprepare;
1041
- }
1042921 }
1043922
1044923 core->prepare_count++;
....@@ -1199,6 +1078,101 @@
11991078 }
12001079
12011080 /**
1081
+ * clk_gate_restore_context - restore context for poweroff
1082
+ * @hw: the clk_hw pointer of clock whose state is to be restored
1083
+ *
1084
+ * The clock gate restore context function enables or disables
1085
+ * the gate clocks based on the enable_count. This is done in cases
1086
+ * where the clock context is lost and based on the enable_count
1087
+ * the clock either needs to be enabled/disabled. This
1088
+ * helps restore the state of gate clocks.
1089
+ */
1090
+void clk_gate_restore_context(struct clk_hw *hw)
1091
+{
1092
+ struct clk_core *core = hw->core;
1093
+
1094
+ if (core->enable_count)
1095
+ core->ops->enable(hw);
1096
+ else
1097
+ core->ops->disable(hw);
1098
+}
1099
+EXPORT_SYMBOL_GPL(clk_gate_restore_context);
1100
+
1101
+static int clk_core_save_context(struct clk_core *core)
1102
+{
1103
+ struct clk_core *child;
1104
+ int ret = 0;
1105
+
1106
+ hlist_for_each_entry(child, &core->children, child_node) {
1107
+ ret = clk_core_save_context(child);
1108
+ if (ret < 0)
1109
+ return ret;
1110
+ }
1111
+
1112
+ if (core->ops && core->ops->save_context)
1113
+ ret = core->ops->save_context(core->hw);
1114
+
1115
+ return ret;
1116
+}
1117
+
1118
+static void clk_core_restore_context(struct clk_core *core)
1119
+{
1120
+ struct clk_core *child;
1121
+
1122
+ if (core->ops && core->ops->restore_context)
1123
+ core->ops->restore_context(core->hw);
1124
+
1125
+ hlist_for_each_entry(child, &core->children, child_node)
1126
+ clk_core_restore_context(child);
1127
+}
1128
+
1129
+/**
1130
+ * clk_save_context - save clock context for poweroff
1131
+ *
1132
+ * Saves the context of the clock register for powerstates in which the
1133
+ * contents of the registers will be lost. Occurs deep within the suspend
1134
+ * code. Returns 0 on success.
1135
+ */
1136
+int clk_save_context(void)
1137
+{
1138
+ struct clk_core *clk;
1139
+ int ret;
1140
+
1141
+ hlist_for_each_entry(clk, &clk_root_list, child_node) {
1142
+ ret = clk_core_save_context(clk);
1143
+ if (ret < 0)
1144
+ return ret;
1145
+ }
1146
+
1147
+ hlist_for_each_entry(clk, &clk_orphan_list, child_node) {
1148
+ ret = clk_core_save_context(clk);
1149
+ if (ret < 0)
1150
+ return ret;
1151
+ }
1152
+
1153
+ return 0;
1154
+}
1155
+EXPORT_SYMBOL_GPL(clk_save_context);
1156
+
1157
+/**
1158
+ * clk_restore_context - restore clock context after poweroff
1159
+ *
1160
+ * Restore the saved clock context upon resume.
1161
+ *
1162
+ */
1163
+void clk_restore_context(void)
1164
+{
1165
+ struct clk_core *core;
1166
+
1167
+ hlist_for_each_entry(core, &clk_root_list, child_node)
1168
+ clk_core_restore_context(core);
1169
+
1170
+ hlist_for_each_entry(core, &clk_orphan_list, child_node)
1171
+ clk_core_restore_context(core);
1172
+}
1173
+EXPORT_SYMBOL_GPL(clk_restore_context);
1174
+
1175
+/**
12021176 * clk_enable - ungate a clock
12031177 * @clk: the clk being ungated
12041178 *
....@@ -1241,7 +1215,7 @@
12411215 clk_core_unprepare_lock(core);
12421216 }
12431217
1244
-static void clk_unprepare_unused_subtree(struct clk_core *core)
1218
+static void __init clk_unprepare_unused_subtree(struct clk_core *core)
12451219 {
12461220 struct clk_core *child;
12471221
....@@ -1253,19 +1227,6 @@
12531227 if (dev_has_sync_state(core->dev) &&
12541228 !(core->flags & CLK_DONT_HOLD_STATE))
12551229 return;
1256
-
1257
- /*
1258
- * setting CLK_ENABLE_HAND_OFF flag triggers this conditional
1259
- *
1260
- * need_handoff_prepare implies this clk was already prepared by
1261
- * __clk_init. now we have a proper user, so unset the flag in our
1262
- * internal bookkeeping. See CLK_ENABLE_HAND_OFF flag in clk-provider.h
1263
- * for details.
1264
- */
1265
- if (core->need_handoff_prepare) {
1266
- core->need_handoff_prepare = false;
1267
- clk_core_unprepare(core);
1268
- }
12691230
12701231 if (core->prepare_count)
12711232 return;
....@@ -1288,7 +1249,7 @@
12881249 clk_pm_runtime_put(core);
12891250 }
12901251
1291
-static void clk_disable_unused_subtree(struct clk_core *core)
1252
+static void __init clk_disable_unused_subtree(struct clk_core *core)
12921253 {
12931254 struct clk_core *child;
12941255 unsigned long flags;
....@@ -1301,21 +1262,6 @@
13011262 if (dev_has_sync_state(core->dev) &&
13021263 !(core->flags & CLK_DONT_HOLD_STATE))
13031264 return;
1304
-
1305
- /*
1306
- * setting CLK_ENABLE_HAND_OFF flag triggers this conditional
1307
- *
1308
- * need_handoff_enable implies this clk was already enabled by
1309
- * __clk_init. now we have a proper user, so unset the flag in our
1310
- * internal bookkeeping. See CLK_ENABLE_HAND_OFF flag in clk-provider.h
1311
- * for details.
1312
- */
1313
- if (core->need_handoff_enable) {
1314
- core->need_handoff_enable = false;
1315
- flags = clk_enable_lock();
1316
- clk_core_disable(core);
1317
- clk_enable_unlock(flags);
1318
- }
13191265
13201266 if (core->flags & CLK_OPS_PARENT_ENABLE)
13211267 clk_core_prepare_enable(core->parent);
....@@ -1353,7 +1299,7 @@
13531299 clk_core_disable_unprepare(core->parent);
13541300 }
13551301
1356
-static bool clk_ignore_unused;
1302
+static bool clk_ignore_unused __initdata;
13571303 static int __init clk_ignore_unused_setup(char *__unused)
13581304 {
13591305 clk_ignore_unused = true;
....@@ -1361,7 +1307,7 @@
13611307 }
13621308 __setup("clk_ignore_unused", clk_ignore_unused_setup);
13631309
1364
-static int clk_disable_unused(void)
1310
+static int __init clk_disable_unused(void)
13651311 {
13661312 struct clk_core *core;
13671313
....@@ -1476,10 +1422,7 @@
14761422
14771423 static bool clk_core_can_round(struct clk_core * const core)
14781424 {
1479
- if (core->ops->determine_rate || core->ops->round_rate)
1480
- return true;
1481
-
1482
- return false;
1425
+ return core->ops->determine_rate || core->ops->round_rate;
14831426 }
14841427
14851428 static int clk_core_round_rate_nolock(struct clk_core *core,
....@@ -1521,6 +1464,21 @@
15211464 }
15221465 EXPORT_SYMBOL_GPL(__clk_determine_rate);
15231466
1467
+/**
1468
+ * clk_hw_round_rate() - round the given rate for a hw clk
1469
+ * @hw: the hw clk for which we are rounding a rate
1470
+ * @rate: the rate which is to be rounded
1471
+ *
1472
+ * Takes in a rate as input and rounds it to a rate that the clk can actually
1473
+ * use.
1474
+ *
1475
+ * Context: prepare_lock must be held.
1476
+ * For clk providers to call from within clk_ops such as .round_rate,
1477
+ * .determine_rate.
1478
+ *
1479
+ * Return: returns rounded rate of hw clk if clk supports round_rate operation
1480
+ * else returns the parent rate.
1481
+ */
15241482 unsigned long clk_hw_round_rate(struct clk_hw *hw, unsigned long rate)
15251483 {
15261484 int ret;
....@@ -1642,18 +1600,12 @@
16421600 __clk_recalc_accuracies(child);
16431601 }
16441602
1645
-static long clk_core_get_accuracy(struct clk_core *core)
1603
+static long clk_core_get_accuracy_recalc(struct clk_core *core)
16461604 {
1647
- unsigned long accuracy;
1648
-
1649
- clk_prepare_lock();
16501605 if (core && (core->flags & CLK_GET_ACCURACY_NOCACHE))
16511606 __clk_recalc_accuracies(core);
16521607
1653
- accuracy = __clk_get_accuracy(core);
1654
- clk_prepare_unlock();
1655
-
1656
- return accuracy;
1608
+ return clk_core_get_accuracy_no_lock(core);
16571609 }
16581610
16591611 /**
....@@ -1667,10 +1619,16 @@
16671619 */
16681620 long clk_get_accuracy(struct clk *clk)
16691621 {
1622
+ long accuracy;
1623
+
16701624 if (!clk)
16711625 return 0;
16721626
1673
- return clk_core_get_accuracy(clk->core);
1627
+ clk_prepare_lock();
1628
+ accuracy = clk_core_get_accuracy_recalc(clk->core);
1629
+ clk_prepare_unlock();
1630
+
1631
+ return accuracy;
16741632 }
16751633 EXPORT_SYMBOL_GPL(clk_get_accuracy);
16761634
....@@ -1724,19 +1682,12 @@
17241682 __clk_recalc_rates(child, msg);
17251683 }
17261684
1727
-static unsigned long clk_core_get_rate(struct clk_core *core)
1685
+static unsigned long clk_core_get_rate_recalc(struct clk_core *core)
17281686 {
1729
- unsigned long rate;
1730
-
1731
- clk_prepare_lock();
1732
-
17331687 if (core && (core->flags & CLK_GET_RATE_NOCACHE))
17341688 __clk_recalc_rates(core, 0);
17351689
1736
- rate = clk_core_get_rate_nolock(core);
1737
- clk_prepare_unlock();
1738
-
1739
- return rate;
1690
+ return clk_core_get_rate_nolock(core);
17401691 }
17411692
17421693 /**
....@@ -1749,10 +1700,16 @@
17491700 */
17501701 unsigned long clk_get_rate(struct clk *clk)
17511702 {
1703
+ unsigned long rate;
1704
+
17521705 if (!clk)
17531706 return 0;
17541707
1755
- return clk_core_get_rate(clk->core);
1708
+ clk_prepare_lock();
1709
+ rate = clk_core_get_rate_recalc(clk->core);
1710
+ clk_prepare_unlock();
1711
+
1712
+ return rate;
17561713 }
17571714 EXPORT_SYMBOL_GPL(clk_get_rate);
17581715
....@@ -1764,12 +1721,58 @@
17641721 if (!parent)
17651722 return -EINVAL;
17661723
1767
- for (i = 0; i < core->num_parents; i++)
1768
- if (clk_core_get_parent_by_index(core, i) == parent)
1724
+ for (i = 0; i < core->num_parents; i++) {
1725
+ /* Found it first try! */
1726
+ if (core->parents[i].core == parent)
17691727 return i;
17701728
1771
- return -EINVAL;
1729
+ /* Something else is here, so keep looking */
1730
+ if (core->parents[i].core)
1731
+ continue;
1732
+
1733
+ /* Maybe core hasn't been cached but the hw is all we know? */
1734
+ if (core->parents[i].hw) {
1735
+ if (core->parents[i].hw == parent->hw)
1736
+ break;
1737
+
1738
+ /* Didn't match, but we're expecting a clk_hw */
1739
+ continue;
1740
+ }
1741
+
1742
+ /* Maybe it hasn't been cached (clk_set_parent() path) */
1743
+ if (parent == clk_core_get(core, i))
1744
+ break;
1745
+
1746
+ /* Fallback to comparing globally unique names */
1747
+ if (core->parents[i].name &&
1748
+ !strcmp(parent->name, core->parents[i].name))
1749
+ break;
1750
+ }
1751
+
1752
+ if (i == core->num_parents)
1753
+ return -EINVAL;
1754
+
1755
+ core->parents[i].core = parent;
1756
+ return i;
17721757 }
1758
+
1759
+/**
1760
+ * clk_hw_get_parent_index - return the index of the parent clock
1761
+ * @hw: clk_hw associated with the clk being consumed
1762
+ *
1763
+ * Fetches and returns the index of parent clock. Returns -EINVAL if the given
1764
+ * clock does not have a current parent.
1765
+ */
1766
+int clk_hw_get_parent_index(struct clk_hw *hw)
1767
+{
1768
+ struct clk_hw *parent = clk_hw_get_parent(hw);
1769
+
1770
+ if (WARN_ON(parent == NULL))
1771
+ return -EINVAL;
1772
+
1773
+ return clk_fetch_parent_index(hw->core, parent->core);
1774
+}
1775
+EXPORT_SYMBOL_GPL(clk_hw_get_parent_index);
17731776
17741777 static void clk_core_hold_state(struct clk_core *core)
17751778 {
....@@ -1979,59 +1982,12 @@
19791982 return ret;
19801983 }
19811984
1982
-/*
1983
- * Vote for the voltage level required for core->new_rate. Keep track of all
1984
- * clocks with a changed voltage level in clk_rate_change_list.
1985
- */
1986
-static int clk_vote_new_rate_vdd(struct clk_core *core)
1987
-{
1988
- int cur_level, next_level;
1989
- int ret;
1990
-
1991
- if (IS_ERR_OR_NULL(core) || !core->vdd_class)
1992
- return 0;
1993
-
1994
- if (!clk_core_is_prepared(core))
1995
- return 0;
1996
-
1997
- cur_level = core->new_vdd_class_vote;
1998
- next_level = clk_find_vdd_level(core, core->new_rate);
1999
- if (cur_level == next_level)
2000
- return 0;
2001
-
2002
- ret = clk_vote_vdd_level(core->vdd_class, next_level);
2003
- if (ret)
2004
- return ret;
2005
-
2006
- core->new_vdd_class_vote = next_level;
2007
-
2008
- if (list_empty(&core->rate_change_node)) {
2009
- list_add(&core->rate_change_node, &clk_rate_change_list);
2010
- } else {
2011
- /*
2012
- * A different new_rate has been determined for a clock that
2013
- * was already encountered in the clock tree traversal so the
2014
- * level that was previously voted for it should be removed.
2015
- */
2016
- ret = clk_unvote_vdd_level(core->vdd_class, cur_level);
2017
- if (ret)
2018
- return ret;
2019
- }
2020
-
2021
- return 0;
2022
-}
2023
-
2024
-static int clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
1985
+static void clk_calc_subtree(struct clk_core *core, unsigned long new_rate,
20251986 struct clk_core *new_parent, u8 p_index)
20261987 {
20271988 struct clk_core *child;
2028
- int ret;
20291989
20301990 core->new_rate = new_rate;
2031
- ret = clk_vote_new_rate_vdd(core);
2032
- if (ret)
2033
- return ret;
2034
-
20351991 core->new_parent = new_parent;
20361992 core->new_parent_index = p_index;
20371993 /* include clk in new parent's PRE_RATE_CHANGE notifications */
....@@ -2041,12 +1997,8 @@
20411997
20421998 hlist_for_each_entry(child, &core->children, child_node) {
20431999 child->new_rate = clk_recalc(child, new_rate);
2044
- ret = clk_calc_subtree(child, child->new_rate, NULL, 0);
2045
- if (ret)
2046
- return ret;
2000
+ clk_calc_subtree(child, child->new_rate, NULL, 0);
20472001 }
2048
-
2049
- return 0;
20502002 }
20512003
20522004 /*
....@@ -2125,23 +2077,12 @@
21252077 }
21262078 }
21272079
2128
- /*
2129
- * Certain PLLs only have 16 bits to program the fractional divider.
2130
- * Hence the programmed rate might be slightly different than the
2131
- * requested one.
2132
- */
21332080 if ((core->flags & CLK_SET_RATE_PARENT) && parent &&
2134
- (DIV_ROUND_CLOSEST(best_parent_rate, 1000) !=
2135
- DIV_ROUND_CLOSEST(parent->rate, 1000)))
2081
+ best_parent_rate != parent->rate)
21362082 top = clk_calc_new_rates(parent, best_parent_rate);
21372083
21382084 out:
2139
- if (!clk_is_rate_level_valid(core, rate))
2140
- return NULL;
2141
-
2142
- ret = clk_calc_subtree(core, new_rate, parent, p_index);
2143
- if (ret)
2144
- return NULL;
2085
+ clk_calc_subtree(core, new_rate, parent, p_index);
21452086
21462087 return top;
21472088 }
....@@ -2166,6 +2107,13 @@
21662107 fail_clk = core;
21672108 }
21682109
2110
+ if (core->ops->pre_rate_change) {
2111
+ ret = core->ops->pre_rate_change(core->hw, core->rate,
2112
+ core->new_rate);
2113
+ if (ret)
2114
+ fail_clk = core;
2115
+ }
2116
+
21692117 hlist_for_each_entry(child, &core->children, child_node) {
21702118 /* Skip children who will be reparented to another clock */
21712119 if (child->new_parent && child->new_parent != core)
....@@ -2185,14 +2133,11 @@
21852133 return fail_clk;
21862134 }
21872135
2188
-static int clk_core_set_rate_nolock(struct clk_core *core,
2189
- unsigned long req_rate);
2190
-
21912136 /*
21922137 * walk down a subtree and set the new rates notifying the rate
21932138 * change on the way
21942139 */
2195
-static int clk_change_rate(struct clk_core *core)
2140
+static void clk_change_rate(struct clk_core *core)
21962141 {
21972142 struct clk_core *child;
21982143 struct hlist_node *tmp;
....@@ -2201,7 +2146,6 @@
22012146 bool skip_set_rate = false;
22022147 struct clk_core *old_parent;
22032148 struct clk_core *parent = NULL;
2204
- int rc = 0;
22052149
22062150 old_rate = core->rate;
22072151
....@@ -2213,9 +2157,8 @@
22132157 best_parent_rate = core->parent->rate;
22142158 }
22152159
2216
- rc = clk_pm_runtime_get(core);
2217
- if (rc)
2218
- return rc;
2160
+ if (clk_pm_runtime_get(core))
2161
+ return;
22192162
22202163 if (core->flags & CLK_SET_RATE_UNGATE) {
22212164 unsigned long flags;
....@@ -2225,8 +2168,6 @@
22252168 clk_core_enable(core);
22262169 clk_enable_unlock(flags);
22272170 }
2228
-
2229
- trace_clk_set_rate(core, core->new_rate);
22302171
22312172 if (core->new_parent && core->new_parent != core->parent) {
22322173 old_parent = __clk_set_parent_before(core, core->new_parent);
....@@ -2248,14 +2189,10 @@
22482189 if (core->flags & CLK_OPS_PARENT_ENABLE)
22492190 clk_core_prepare_enable(parent);
22502191
2251
- if (!skip_set_rate && core->ops->set_rate) {
2252
- rc = core->ops->set_rate(core->hw, core->new_rate,
2253
- best_parent_rate);
2254
- if (rc) {
2255
- trace_clk_set_rate_complete(core, core->new_rate);
2256
- goto err_set_rate;
2257
- }
2258
- }
2192
+ trace_clk_set_rate(core, core->new_rate);
2193
+
2194
+ if (!skip_set_rate && core->ops->set_rate)
2195
+ core->ops->set_rate(core->hw, core->new_rate, best_parent_rate);
22592196
22602197 trace_clk_set_rate_complete(core, core->new_rate);
22612198
....@@ -2279,6 +2216,9 @@
22792216 if (core->flags & CLK_RECALC_NEW_RATES)
22802217 (void)clk_calc_new_rates(core, core->new_rate);
22812218
2219
+ if (core->ops->post_rate_change)
2220
+ core->ops->post_rate_change(core->hw, old_rate, core->rate);
2221
+
22822222 /*
22832223 * Use safe iteration, as change_rate can actually swap parents
22842224 * for certain clock types.
....@@ -2287,24 +2227,14 @@
22872227 /* Skip children who will be reparented to another clock */
22882228 if (child->new_parent && child->new_parent != core)
22892229 continue;
2290
- rc = clk_change_rate(child);
2291
- if (rc)
2292
- goto err_set_rate;
2230
+ clk_change_rate(child);
22932231 }
22942232
22952233 /* handle the new child who might not be in core->children yet */
22962234 if (core->new_child)
2297
- rc = clk_change_rate(core->new_child);
2235
+ clk_change_rate(core->new_child);
22982236
2299
- /* handle a changed clock that needs to readjust its rate */
2300
- if (core->flags & CLK_KEEP_REQ_RATE && core->req_rate
2301
- && core->new_rate != old_rate
2302
- && core->new_rate != core->req_rate)
2303
- clk_core_set_rate_nolock(core, core->req_rate);
2304
-
2305
-err_set_rate:
23062237 clk_pm_runtime_put(core);
2307
- return rc;
23082238 }
23092239
23102240 static unsigned long clk_core_req_round_rate_nolock(struct clk_core *core,
....@@ -2334,85 +2264,12 @@
23342264 return ret ? 0 : req.rate;
23352265 }
23362266
2337
-/*
2338
- * Unvote for the voltage level required for each core->new_vdd_class_vote in
2339
- * clk_rate_change_list. This is used when undoing voltage requests after an
2340
- * error is encountered before any physical rate changing.
2341
- */
2342
-static void clk_unvote_new_rate_vdd(void)
2343
-{
2344
- struct clk_core *core;
2345
-
2346
- list_for_each_entry(core, &clk_rate_change_list, rate_change_node) {
2347
- clk_unvote_vdd_level(core->vdd_class, core->new_vdd_class_vote);
2348
- core->new_vdd_class_vote = core->vdd_class_vote;
2349
- }
2350
-}
2351
-
2352
-/*
2353
- * Unvote for the voltage level required for each core->vdd_class_vote in
2354
- * clk_rate_change_list.
2355
- */
2356
-static int clk_unvote_old_rate_vdd(void)
2357
-{
2358
- struct clk_core *core;
2359
- int ret;
2360
-
2361
- list_for_each_entry(core, &clk_rate_change_list, rate_change_node) {
2362
- ret = clk_unvote_vdd_level(core->vdd_class,
2363
- core->vdd_class_vote);
2364
- if (ret)
2365
- return ret;
2366
- }
2367
-
2368
- return 0;
2369
-}
2370
-
2371
-/*
2372
- * In the case that rate setting fails, apply the max voltage level needed
2373
- * by either the old or new rate for each changed clock.
2374
- */
2375
-static void clk_vote_safe_vdd(void)
2376
-{
2377
- struct clk_core *core;
2378
-
2379
- list_for_each_entry(core, &clk_rate_change_list, rate_change_node) {
2380
- if (core->vdd_class_vote > core->new_vdd_class_vote) {
2381
- clk_vote_vdd_level(core->vdd_class,
2382
- core->vdd_class_vote);
2383
- clk_unvote_vdd_level(core->vdd_class,
2384
- core->new_vdd_class_vote);
2385
- core->new_vdd_class_vote = core->vdd_class_vote;
2386
- }
2387
- }
2388
-}
2389
-
2390
-static void clk_cleanup_vdd_votes(void)
2391
-{
2392
- struct clk_core *core, *temp;
2393
-
2394
- list_for_each_entry_safe(core, temp, &clk_rate_change_list,
2395
- rate_change_node) {
2396
- core->vdd_class_vote = core->new_vdd_class_vote;
2397
- list_del_init(&core->rate_change_node);
2398
- }
2399
-}
2400
-
24012267 static int clk_core_set_rate_nolock(struct clk_core *core,
24022268 unsigned long req_rate)
24032269 {
24042270 struct clk_core *top, *fail_clk;
24052271 unsigned long rate;
24062272 int ret = 0;
2407
- /*
2408
- * The prepare lock ensures mutual exclusion with other tasks.
2409
- * set_rate_nesting_count is a static so that it can be incremented in
2410
- * the case of reentrancy caused by a set_rate() ops callback itself
2411
- * calling clk_set_rate(). That way, the voltage level votes for the
2412
- * old rates are safely removed when the original invocation of this
2413
- * function completes.
2414
- */
2415
- static unsigned int set_rate_nesting_count;
24162273
24172274 if (!core)
24182275 return 0;
....@@ -2427,63 +2284,31 @@
24272284 if (clk_core_rate_is_protected(core))
24282285 return -EBUSY;
24292286
2430
- set_rate_nesting_count++;
2431
-
24322287 /* calculate new rates and get the topmost changed clock */
24332288 top = clk_calc_new_rates(core, req_rate);
2434
- if (!top) {
2435
- ret = -EINVAL;
2436
- goto pre_rate_change_err;
2437
- }
2289
+ if (!top)
2290
+ return -EINVAL;
24382291
24392292 ret = clk_pm_runtime_get(core);
24402293 if (ret)
2441
- goto pre_rate_change_err;
2294
+ return ret;
24422295
24432296 /* notify that we are about to change rates */
24442297 fail_clk = clk_propagate_rate_change(top, PRE_RATE_CHANGE);
24452298 if (fail_clk) {
2446
- pr_debug("%s: failed to set %s clock to run at %lu\n", __func__,
2447
- fail_clk->name, req_rate);
2299
+ pr_debug("%s: failed to set %s rate\n", __func__,
2300
+ fail_clk->name);
24482301 clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
24492302 ret = -EBUSY;
2450
- clk_pm_runtime_put(core);
2451
- goto pre_rate_change_err;
2303
+ goto err;
24522304 }
2453
-
2454
- core->req_rate = req_rate;
24552305
24562306 /* change the rates */
2457
- ret = clk_change_rate(top);
2458
- set_rate_nesting_count--;
2459
- if (ret) {
2460
- pr_err("%s: failed to set %s clock to run at %lu\n", __func__,
2461
- top->name, req_rate);
2462
- clk_propagate_rate_change(top, ABORT_RATE_CHANGE);
2463
- clk_vote_safe_vdd();
2464
- goto post_rate_change_err;
2465
- }
2307
+ clk_change_rate(top);
24662308
2467
-post_rate_change_err:
2468
- /*
2469
- * Only remove vdd_class level votes for old clock rates after all
2470
- * nested clk_set_rate() calls have completed.
2471
- */
2472
- if (set_rate_nesting_count == 0) {
2473
- ret |= clk_unvote_old_rate_vdd();
2474
- clk_cleanup_vdd_votes();
2475
- }
2476
-
2309
+ core->req_rate = req_rate;
2310
+err:
24772311 clk_pm_runtime_put(core);
2478
-
2479
- return ret;
2480
-
2481
-pre_rate_change_err:
2482
- set_rate_nesting_count--;
2483
- if (set_rate_nesting_count == 0) {
2484
- clk_unvote_new_rate_vdd();
2485
- clk_cleanup_vdd_votes();
2486
- }
24872312
24882313 return ret;
24892314 }
....@@ -2534,7 +2359,7 @@
25342359 EXPORT_SYMBOL_GPL(clk_set_rate);
25352360
25362361 /**
2537
- * clk_set_rate_exclusive - specify a new rate get exclusive control
2362
+ * clk_set_rate_exclusive - specify a new rate and get exclusive control
25382363 * @clk: the clk whose rate is being changed
25392364 * @rate: the new rate for clk
25402365 *
....@@ -2542,7 +2367,7 @@
25422367 * within a critical section
25432368 *
25442369 * This can be used initially to ensure that at least 1 consumer is
2545
- * statisfied when several consumers are competing for exclusivity over the
2370
+ * satisfied when several consumers are competing for exclusivity over the
25462371 * same clock provider.
25472372 *
25482373 * The exclusivity is not applied if setting the rate failed.
....@@ -2614,6 +2439,11 @@
26142439 clk->min_rate = min;
26152440 clk->max_rate = max;
26162441
2442
+ if (!clk_core_check_boundaries(clk->core, min, max)) {
2443
+ ret = -EINVAL;
2444
+ goto out;
2445
+ }
2446
+
26172447 rate = clk_core_get_rate_nolock(clk->core);
26182448 if (rate < min || rate > max) {
26192449 /*
....@@ -2642,6 +2472,7 @@
26422472 }
26432473 }
26442474
2475
+out:
26452476 if (clk->exclusive_count)
26462477 clk_core_rate_protect(clk->core);
26472478
....@@ -2744,6 +2575,7 @@
27442575 bool clk_has_parent(struct clk *clk, struct clk *parent)
27452576 {
27462577 struct clk_core *core, *parent_core;
2578
+ int i;
27472579
27482580 /* NULL clocks should be nops, so return success if either is NULL. */
27492581 if (!clk || !parent)
....@@ -2756,8 +2588,11 @@
27562588 if (core->parent == parent_core)
27572589 return true;
27582590
2759
- return match_string(core->parent_names, core->num_parents,
2760
- parent_core->name) >= 0;
2591
+ for (i = 0; i < core->num_parents; i++)
2592
+ if (!strcmp(core->parents[i].name, parent_core->name))
2593
+ return true;
2594
+
2595
+ return false;
27612596 }
27622597 EXPORT_SYMBOL_GPL(clk_has_parent);
27632598
....@@ -2773,10 +2608,10 @@
27732608 if (!core)
27742609 return 0;
27752610
2776
- if (core->parent == parent && !(core->flags & CLK_IS_MEASURE))
2611
+ if (core->parent == parent)
27772612 return 0;
27782613
2779
- /* verify ops for for multi-parent clks */
2614
+ /* verify ops for multi-parent clks */
27802615 if (core->num_parents > 1 && !core->ops->set_parent)
27812616 return -EPERM;
27822617
....@@ -2825,6 +2660,12 @@
28252660
28262661 return ret;
28272662 }
2663
+
2664
+int clk_hw_set_parent(struct clk_hw *hw, struct clk_hw *parent)
2665
+{
2666
+ return clk_core_set_parent_nolock(hw->core, parent->core);
2667
+}
2668
+EXPORT_SYMBOL_GPL(clk_hw_set_parent);
28282669
28292670 /**
28302671 * clk_set_parent - switch the parent of a mux clk
....@@ -2944,12 +2785,14 @@
29442785 {
29452786 int ret;
29462787
2947
- clk_prepare_lock();
2788
+ lockdep_assert_held(&prepare_lock);
2789
+ if (!core->ops->get_phase)
2790
+ return 0;
2791
+
29482792 /* Always try to update cached phase if possible */
2949
- if (core->ops->get_phase)
2950
- core->phase = core->ops->get_phase(core->hw);
2951
- ret = core->phase;
2952
- clk_prepare_unlock();
2793
+ ret = core->ops->get_phase(core->hw);
2794
+ if (ret >= 0)
2795
+ core->phase = ret;
29532796
29542797 return ret;
29552798 }
....@@ -2963,10 +2806,16 @@
29632806 */
29642807 int clk_get_phase(struct clk *clk)
29652808 {
2809
+ int ret;
2810
+
29662811 if (!clk)
29672812 return 0;
29682813
2969
- return clk_core_get_phase(clk->core);
2814
+ clk_prepare_lock();
2815
+ ret = clk_core_get_phase(clk->core);
2816
+ clk_prepare_unlock();
2817
+
2818
+ return ret;
29702819 }
29712820 EXPORT_SYMBOL_GPL(clk_get_phase);
29722821
....@@ -3162,26 +3011,9 @@
31623011 }
31633012 EXPORT_SYMBOL_GPL(clk_is_match);
31643013
3165
-int clk_set_flags(struct clk *clk, unsigned long flags)
3166
-{
3167
- if (!clk)
3168
- return 0;
3169
-
3170
- if (!clk->core->ops->set_flags)
3171
- return -EINVAL;
3172
-
3173
- return clk->core->ops->set_flags(clk->core->hw, flags);
3174
-}
3175
-EXPORT_SYMBOL_GPL(clk_set_flags);
3176
-
3177
-void clk_debug_print_hw(struct clk_core *clk, struct seq_file *f)
3178
-{
3179
-}
3180
-EXPORT_SYMBOL(clk_debug_print_hw);
3181
-
31823014 /*** debugfs support ***/
31833015
3184
-#ifdef CONFIG_COMMON_CLK_DEBUGFS
3016
+#ifdef CONFIG_DEBUG_FS
31853017 #include <linux/debugfs.h>
31863018
31873019 static struct dentry *rootdir;
....@@ -3197,25 +3029,28 @@
31973029 static void clk_summary_show_one(struct seq_file *s, struct clk_core *c,
31983030 int level)
31993031 {
3200
- if (!c)
3201
- return;
3032
+ int phase;
32023033
3203
- seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu %5d %6d\n",
3034
+ seq_printf(s, "%*s%-*s %7d %8d %8d %11lu %10lu ",
32043035 level * 3 + 1, "",
32053036 30 - level * 3, c->name,
32063037 c->enable_count, c->prepare_count, c->protect_count,
3207
- clk_core_get_rate(c), clk_core_get_accuracy(c),
3208
- clk_core_get_phase(c),
3209
- clk_core_get_scaled_duty_cycle(c, 100000));
3038
+ clk_core_get_rate_recalc(c),
3039
+ clk_core_get_accuracy_recalc(c));
3040
+
3041
+ phase = clk_core_get_phase(c);
3042
+ if (phase >= 0)
3043
+ seq_printf(s, "%5d", phase);
3044
+ else
3045
+ seq_puts(s, "-----");
3046
+
3047
+ seq_printf(s, " %6d\n", clk_core_get_scaled_duty_cycle(c, 100000));
32103048 }
32113049
32123050 static void clk_summary_show_subtree(struct seq_file *s, struct clk_core *c,
32133051 int level)
32143052 {
32153053 struct clk_core *child;
3216
-
3217
- if (!c)
3218
- return;
32193054
32203055 clk_summary_show_one(s, c, level);
32213056
....@@ -3246,17 +3081,23 @@
32463081
32473082 static void clk_dump_one(struct seq_file *s, struct clk_core *c, int level)
32483083 {
3249
- if (!c)
3250
- return;
3084
+ int phase;
3085
+ unsigned long min_rate, max_rate;
3086
+
3087
+ clk_core_get_boundaries(c, &min_rate, &max_rate);
32513088
32523089 /* This should be JSON format, i.e. elements separated with a comma */
32533090 seq_printf(s, "\"%s\": { ", c->name);
32543091 seq_printf(s, "\"enable_count\": %d,", c->enable_count);
32553092 seq_printf(s, "\"prepare_count\": %d,", c->prepare_count);
32563093 seq_printf(s, "\"protect_count\": %d,", c->protect_count);
3257
- seq_printf(s, "\"rate\": %lu,", clk_core_get_rate(c));
3258
- seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy(c));
3259
- seq_printf(s, "\"phase\": %d,", clk_core_get_phase(c));
3094
+ seq_printf(s, "\"rate\": %lu,", clk_core_get_rate_recalc(c));
3095
+ seq_printf(s, "\"min_rate\": %lu,", min_rate);
3096
+ seq_printf(s, "\"max_rate\": %lu,", max_rate);
3097
+ seq_printf(s, "\"accuracy\": %lu,", clk_core_get_accuracy_recalc(c));
3098
+ phase = clk_core_get_phase(c);
3099
+ if (phase >= 0)
3100
+ seq_printf(s, "\"phase\": %d,", phase);
32603101 seq_printf(s, "\"duty_cycle\": %u",
32613102 clk_core_get_scaled_duty_cycle(c, 100000));
32623103 }
....@@ -3264,9 +3105,6 @@
32643105 static void clk_dump_subtree(struct seq_file *s, struct clk_core *c, int level)
32653106 {
32663107 struct clk_core *child;
3267
-
3268
- if (!c)
3269
- return;
32703108
32713109 clk_dump_one(s, c, level);
32723110
....@@ -3303,6 +3141,70 @@
33033141 }
33043142 DEFINE_SHOW_ATTRIBUTE(clk_dump);
33053143
3144
+#ifdef CONFIG_ANDROID_BINDER_IPC
3145
+#define CLOCK_ALLOW_WRITE_DEBUGFS
3146
+#else
3147
+#undef CLOCK_ALLOW_WRITE_DEBUGFS
3148
+#endif
3149
+#ifdef CLOCK_ALLOW_WRITE_DEBUGFS
3150
+/*
3151
+ * This can be dangerous, therefore don't provide any real compile time
3152
+ * configuration option for this feature.
3153
+ * People who want to use this will need to modify the source code directly.
3154
+ */
3155
+static int clk_rate_set(void *data, u64 val)
3156
+{
3157
+ struct clk_core *core = data;
3158
+ int ret;
3159
+
3160
+ clk_prepare_lock();
3161
+ ret = clk_core_set_rate_nolock(core, val);
3162
+ clk_prepare_unlock();
3163
+
3164
+ return ret;
3165
+}
3166
+
3167
+#define clk_rate_mode 0644
3168
+
3169
+static int clk_prepare_enable_set(void *data, u64 val)
3170
+{
3171
+ struct clk_core *core = data;
3172
+ int ret = 0;
3173
+
3174
+ if (val)
3175
+ ret = clk_prepare_enable(core->hw->clk);
3176
+ else
3177
+ clk_disable_unprepare(core->hw->clk);
3178
+
3179
+ return ret;
3180
+}
3181
+
3182
+static int clk_prepare_enable_get(void *data, u64 *val)
3183
+{
3184
+ struct clk_core *core = data;
3185
+
3186
+ *val = core->enable_count && core->prepare_count;
3187
+ return 0;
3188
+}
3189
+
3190
+DEFINE_DEBUGFS_ATTRIBUTE(clk_prepare_enable_fops, clk_prepare_enable_get,
3191
+ clk_prepare_enable_set, "%llu\n");
3192
+
3193
+#else
3194
+#define clk_rate_set NULL
3195
+#define clk_rate_mode 0444
3196
+#endif
3197
+
3198
+static int clk_rate_get(void *data, u64 *val)
3199
+{
3200
+ struct clk_core *core = data;
3201
+
3202
+ *val = core->rate;
3203
+ return 0;
3204
+}
3205
+
3206
+DEFINE_DEBUGFS_ATTRIBUTE(clk_rate_fops, clk_rate_get, clk_rate_set, "%llu\n");
3207
+
33063208 static const struct {
33073209 unsigned long flag;
33083210 const char *name;
....@@ -3312,7 +3214,6 @@
33123214 ENTRY(CLK_SET_PARENT_GATE),
33133215 ENTRY(CLK_SET_RATE_PARENT),
33143216 ENTRY(CLK_IGNORE_UNUSED),
3315
- ENTRY(CLK_IS_BASIC),
33163217 ENTRY(CLK_GET_RATE_NOCACHE),
33173218 ENTRY(CLK_SET_RATE_NO_REPARENT),
33183219 ENTRY(CLK_GET_ACCURACY_NOCACHE),
....@@ -3321,8 +3222,6 @@
33213222 ENTRY(CLK_IS_CRITICAL),
33223223 ENTRY(CLK_OPS_PARENT_ENABLE),
33233224 ENTRY(CLK_DUTY_CYCLE_PARENT),
3324
- ENTRY(CLK_DONT_HOLD_STATE),
3325
- ENTRY(CLK_KEEP_REQ_RATE),
33263225 #undef ENTRY
33273226 };
33283227
....@@ -3347,19 +3246,64 @@
33473246 }
33483247 DEFINE_SHOW_ATTRIBUTE(clk_flags);
33493248
3249
+static void possible_parent_show(struct seq_file *s, struct clk_core *core,
3250
+ unsigned int i, char terminator)
3251
+{
3252
+ struct clk_core *parent;
3253
+
3254
+ /*
3255
+ * Go through the following options to fetch a parent's name.
3256
+ *
3257
+ * 1. Fetch the registered parent clock and use its name
3258
+ * 2. Use the global (fallback) name if specified
3259
+ * 3. Use the local fw_name if provided
3260
+ * 4. Fetch parent clock's clock-output-name if DT index was set
3261
+ *
3262
+ * This may still fail in some cases, such as when the parent is
3263
+ * specified directly via a struct clk_hw pointer, but it isn't
3264
+ * registered (yet).
3265
+ */
3266
+ parent = clk_core_get_parent_by_index(core, i);
3267
+ if (parent)
3268
+ seq_puts(s, parent->name);
3269
+ else if (core->parents[i].name)
3270
+ seq_puts(s, core->parents[i].name);
3271
+ else if (core->parents[i].fw_name)
3272
+ seq_printf(s, "<%s>(fw)", core->parents[i].fw_name);
3273
+ else if (core->parents[i].index >= 0)
3274
+ seq_puts(s,
3275
+ of_clk_get_parent_name(core->of_node,
3276
+ core->parents[i].index));
3277
+ else
3278
+ seq_puts(s, "(missing)");
3279
+
3280
+ seq_putc(s, terminator);
3281
+}
3282
+
33503283 static int possible_parents_show(struct seq_file *s, void *data)
33513284 {
33523285 struct clk_core *core = s->private;
33533286 int i;
33543287
33553288 for (i = 0; i < core->num_parents - 1; i++)
3356
- seq_printf(s, "%s ", core->parent_names[i]);
3289
+ possible_parent_show(s, core, i, ' ');
33573290
3358
- seq_printf(s, "%s\n", core->parent_names[i]);
3291
+ possible_parent_show(s, core, i, '\n');
33593292
33603293 return 0;
33613294 }
33623295 DEFINE_SHOW_ATTRIBUTE(possible_parents);
3296
+
3297
+static int current_parent_show(struct seq_file *s, void *data)
3298
+{
3299
+ struct clk_core *core = s->private;
3300
+
3301
+ if (core->parent)
3302
+ seq_printf(s, "%s\n", core->parent->name);
3303
+
3304
+ return 0;
3305
+}
3306
+DEFINE_SHOW_ATTRIBUTE(current_parent);
33633307
33643308 static int clk_duty_cycle_show(struct seq_file *s, void *data)
33653309 {
....@@ -3372,210 +3316,33 @@
33723316 }
33733317 DEFINE_SHOW_ATTRIBUTE(clk_duty_cycle);
33743318
3375
-static int clock_debug_rate_set(void *data, u64 val)
3319
+static int clk_min_rate_show(struct seq_file *s, void *data)
33763320 {
3377
- struct clk_core *core = data;
3378
- int ret;
3321
+ struct clk_core *core = s->private;
3322
+ unsigned long min_rate, max_rate;
33793323
3380
- ret = clk_set_rate(core->hw->clk, val);
3381
- if (ret)
3382
- pr_err("clk_set_rate(%lu) failed (%d)\n",
3383
- (unsigned long)val, ret);
3384
-
3385
- return ret;
3386
-}
3387
-
3388
-static int clock_debug_rate_get(void *data, u64 *val)
3389
-{
3390
- struct clk_core *core = data;
3391
-
3392
- *val = core->hw->core->rate;
3324
+ clk_prepare_lock();
3325
+ clk_core_get_boundaries(core, &min_rate, &max_rate);
3326
+ clk_prepare_unlock();
3327
+ seq_printf(s, "%lu\n", min_rate);
33933328
33943329 return 0;
33953330 }
3331
+DEFINE_SHOW_ATTRIBUTE(clk_min_rate);
33963332
3397
-DEFINE_SIMPLE_ATTRIBUTE(clock_rate_fops, clock_debug_rate_get,
3398
- clock_debug_rate_set, "%llu\n");
3399
-
3400
-static int clock_available_parent_show(struct seq_file *s, void *data)
3333
+static int clk_max_rate_show(struct seq_file *s, void *data)
34013334 {
3402
- struct clk_core *core = (struct clk_core *)s->private;
3403
- int i;
3335
+ struct clk_core *core = s->private;
3336
+ unsigned long min_rate, max_rate;
34043337
3405
- for (i = 0; i < core->num_parents; i++) {
3406
- if (!core->parents[i])
3407
- continue;
3408
- seq_printf(s, "%s ", core->parents[i]->name);
3409
- }
3410
- seq_puts(s, "\n");
3338
+ clk_prepare_lock();
3339
+ clk_core_get_boundaries(core, &min_rate, &max_rate);
3340
+ clk_prepare_unlock();
3341
+ seq_printf(s, "%lu\n", max_rate);
34113342
34123343 return 0;
34133344 }
3414
-
3415
-static int clock_available_parent_open(struct inode *inode, struct file *file)
3416
-{
3417
- return single_open(file, clock_available_parent_show, inode->i_private);
3418
-}
3419
-
3420
-static const struct file_operations clock_available_parent_fops = {
3421
- .open = clock_available_parent_open,
3422
- .read = seq_read,
3423
- .llseek = seq_lseek,
3424
- .release = single_release,
3425
-};
3426
-
3427
-static ssize_t clock_parent_read(struct file *filp, char __user *ubuf,
3428
- size_t cnt, loff_t *ppos)
3429
-{
3430
- char name[256] = {0};
3431
- struct clk_core *core = filp->private_data;
3432
- struct clk_core *p = core->parent;
3433
-
3434
- snprintf(name, sizeof(name), "%s\n", p ? p->name : "None\n");
3435
-
3436
- return simple_read_from_buffer(ubuf, cnt, ppos, name, strlen(name));
3437
-}
3438
-
3439
-static ssize_t clock_parent_write(struct file *filp, const char __user *buf,
3440
- size_t cnt, loff_t *ppos)
3441
-{
3442
- char temp[256] = {0};
3443
- char name[256] = {0};
3444
- struct clk_core *core = filp->private_data;
3445
- unsigned int ret, i;
3446
-
3447
- if (copy_from_user(temp, buf, cnt))
3448
- return -EINVAL;
3449
-
3450
- ret = sscanf(temp, "%s", name);
3451
- if (ret != 1)
3452
- return -EINVAL;
3453
-
3454
- for (i = 0; i < core->num_parents; i++) {
3455
- if (!core->parents[i])
3456
- continue;
3457
- if (!strcmp(core->parents[i]->name, name)) {
3458
- if (core->parents[i] != core->parent)
3459
- clk_core_set_parent_nolock(core,
3460
- core->parents[i]);
3461
- break;
3462
- }
3463
- }
3464
-
3465
- return cnt;
3466
-}
3467
-
3468
-static const struct file_operations clock_parent_fops = {
3469
- .open = simple_open,
3470
- .read = clock_parent_read,
3471
- .write = clock_parent_write,
3472
-};
3473
-
3474
-static int clock_debug_enable_set(void *data, u64 val)
3475
-{
3476
- struct clk_core *core = data;
3477
- int rc = 0;
3478
-
3479
- if (val)
3480
- rc = clk_prepare_enable(core->hw->clk);
3481
- else
3482
- clk_disable_unprepare(core->hw->clk);
3483
-
3484
- return rc;
3485
-}
3486
-
3487
-static int clock_debug_enable_get(void *data, u64 *val)
3488
-{
3489
- struct clk_core *core = data;
3490
- int enabled = 0;
3491
-
3492
- enabled = core->enable_count;
3493
-
3494
- *val = enabled;
3495
-
3496
- return 0;
3497
-}
3498
-
3499
-DEFINE_DEBUGFS_ATTRIBUTE(clock_enable_fops, clock_debug_enable_get,
3500
- clock_debug_enable_set, "%lld\n");
3501
-
3502
-#define clock_debug_output(m, c, fmt, ...) \
3503
-do { \
3504
- if (m) \
3505
- seq_printf(m, fmt, ##__VA_ARGS__); \
3506
- else if (c) \
3507
- pr_cont(fmt, ##__VA_ARGS__); \
3508
- else \
3509
- pr_info(fmt, ##__VA_ARGS__); \
3510
-} while (0)
3511
-
3512
-static int clock_debug_print_clock(struct clk_core *c, struct seq_file *s)
3513
-{
3514
- char *start = "";
3515
- struct clk *clk;
3516
-
3517
- if (!c || !c->prepare_count)
3518
- return 0;
3519
-
3520
- clk = c->hw->clk;
3521
-
3522
- clock_debug_output(s, 0, "\t");
3523
-
3524
- do {
3525
- clock_debug_output(s, 1, "%s%s:%u:%u [%ld]", start,
3526
- clk->core->name,
3527
- clk->core->prepare_count,
3528
- clk->core->enable_count,
3529
- clk->core->rate);
3530
- start = " -> ";
3531
- } while ((clk = clk_get_parent(clk)));
3532
-
3533
- clock_debug_output(s, 1, "\n");
3534
-
3535
- return 1;
3536
-}
3537
-
3538
-/*
3539
- * clock_debug_print_enabled_clocks() - Print names of enabled clocks
3540
- */
3541
-static void clock_debug_print_enabled_clocks(struct seq_file *s)
3542
-{
3543
- struct clk_core *core;
3544
- int cnt = 0;
3545
-
3546
- clock_debug_output(s, 0, "Enabled clocks:\n");
3547
-
3548
- mutex_lock(&clk_debug_lock);
3549
-
3550
- hlist_for_each_entry(core, &clk_debug_list, debug_node)
3551
- cnt += clock_debug_print_clock(core, s);
3552
-
3553
- mutex_unlock(&clk_debug_lock);
3554
-
3555
- if (cnt)
3556
- clock_debug_output(s, 0, "Enabled clock count: %d\n", cnt);
3557
- else
3558
- clock_debug_output(s, 0, "No clocks enabled.\n");
3559
-}
3560
-
3561
-static int enabled_clocks_show(struct seq_file *s, void *unused)
3562
-{
3563
- clock_debug_print_enabled_clocks(s);
3564
-
3565
- return 0;
3566
-}
3567
-
3568
-static int enabled_clocks_open(struct inode *inode, struct file *file)
3569
-{
3570
- return single_open(file, enabled_clocks_show, inode->i_private);
3571
-}
3572
-
3573
-static const struct file_operations clk_enabled_list_fops = {
3574
- .open = enabled_clocks_open,
3575
- .read = seq_read,
3576
- .llseek = seq_lseek,
3577
- .release = seq_release,
3578
-};
3345
+DEFINE_SHOW_ATTRIBUTE(clk_max_rate);
35793346
35803347 static void clk_debug_create_one(struct clk_core *core, struct dentry *pdentry)
35813348 {
....@@ -3587,22 +3354,27 @@
35873354 root = debugfs_create_dir(core->name, pdentry);
35883355 core->dentry = root;
35893356
3590
- debugfs_create_file("clk_rate", 0444, root, core,
3591
- &clock_rate_fops);
3357
+ debugfs_create_file("clk_rate", clk_rate_mode, root, core,
3358
+ &clk_rate_fops);
3359
+ debugfs_create_file("clk_min_rate", 0444, root, core, &clk_min_rate_fops);
3360
+ debugfs_create_file("clk_max_rate", 0444, root, core, &clk_max_rate_fops);
35923361 debugfs_create_ulong("clk_accuracy", 0444, root, &core->accuracy);
35933362 debugfs_create_u32("clk_phase", 0444, root, &core->phase);
35943363 debugfs_create_file("clk_flags", 0444, root, core, &clk_flags_fops);
35953364 debugfs_create_u32("clk_prepare_count", 0444, root, &core->prepare_count);
3596
- debugfs_create_file("clk_enable_count", 0444, root, core,
3597
- &clock_enable_fops);
3365
+ debugfs_create_u32("clk_enable_count", 0444, root, &core->enable_count);
35983366 debugfs_create_u32("clk_protect_count", 0444, root, &core->protect_count);
35993367 debugfs_create_u32("clk_notifier_count", 0444, root, &core->notifier_count);
36003368 debugfs_create_file("clk_duty_cycle", 0444, root, core,
36013369 &clk_duty_cycle_fops);
3602
- debugfs_create_file("clk_available_parent", 0444, root, core,
3603
- &clock_available_parent_fops);
3604
- debugfs_create_file("clk_parent", 0444, root, core,
3605
- &clock_parent_fops);
3370
+#ifdef CLOCK_ALLOW_WRITE_DEBUGFS
3371
+ debugfs_create_file("clk_prepare_enable", 0644, root, core,
3372
+ &clk_prepare_enable_fops);
3373
+#endif
3374
+
3375
+ if (core->num_parents > 0)
3376
+ debugfs_create_file("clk_parent", 0444, root, core,
3377
+ &current_parent_fops);
36063378
36073379 if (core->num_parents > 1)
36083380 debugfs_create_file("clk_possible_parents", 0444, root, core,
....@@ -3659,6 +3431,24 @@
36593431 {
36603432 struct clk_core *core;
36613433
3434
+#ifdef CLOCK_ALLOW_WRITE_DEBUGFS
3435
+ pr_warn("\n");
3436
+ pr_warn("********************************************************************\n");
3437
+ pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3438
+ pr_warn("** **\n");
3439
+ pr_warn("** WRITEABLE clk DebugFS SUPPORT HAS BEEN ENABLED IN THIS KERNEL **\n");
3440
+ pr_warn("** **\n");
3441
+ pr_warn("** This means that this kernel is built to expose clk operations **\n");
3442
+ pr_warn("** such as parent or rate setting, enabling, disabling, etc. **\n");
3443
+ pr_warn("** to userspace, which may compromise security on your system. **\n");
3444
+ pr_warn("** **\n");
3445
+ pr_warn("** If you see this message and you are not debugging the **\n");
3446
+ pr_warn("** kernel, report this immediately to your vendor! **\n");
3447
+ pr_warn("** **\n");
3448
+ pr_warn("** NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE NOTICE **\n");
3449
+ pr_warn("********************************************************************\n");
3450
+#endif
3451
+
36623452 rootdir = debugfs_create_dir("clk", NULL);
36633453
36643454 debugfs_create_file("clk_summary", 0444, rootdir, &all_lists,
....@@ -3669,9 +3459,6 @@
36693459 &clk_summary_fops);
36703460 debugfs_create_file("clk_orphan_dump", 0444, rootdir, &orphan_list,
36713461 &clk_dump_fops);
3672
-
3673
- debugfs_create_file("clk_enabled_list", 0444, rootdir,
3674
- &clk_debug_list, &clk_enabled_list_fops);
36753462
36763463 mutex_lock(&clk_debug_lock);
36773464 hlist_for_each_entry(core, &clk_debug_list, debug_node)
....@@ -3685,14 +3472,52 @@
36853472 late_initcall(clk_debug_init);
36863473 #else
36873474 static inline void clk_debug_register(struct clk_core *core) { }
3688
-static inline void clk_debug_reparent(struct clk_core *core,
3689
- struct clk_core *new_parent)
3690
-{
3691
-}
36923475 static inline void clk_debug_unregister(struct clk_core *core)
36933476 {
36943477 }
36953478 #endif
3479
+
3480
+static void clk_core_reparent_orphans_nolock(void)
3481
+{
3482
+ struct clk_core *orphan;
3483
+ struct hlist_node *tmp2;
3484
+
3485
+ /*
3486
+ * walk the list of orphan clocks and reparent any that newly finds a
3487
+ * parent.
3488
+ */
3489
+ hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3490
+ struct clk_core *parent = __clk_init_parent(orphan);
3491
+
3492
+ /*
3493
+ * We need to use __clk_set_parent_before() and _after() to
3494
+ * to properly migrate any prepare/enable count of the orphan
3495
+ * clock. This is important for CLK_IS_CRITICAL clocks, which
3496
+ * are enabled during init but might not have a parent yet.
3497
+ */
3498
+ if (parent) {
3499
+ /* update the clk tree topology */
3500
+ __clk_set_parent_before(orphan, parent);
3501
+ __clk_set_parent_after(orphan, parent, NULL);
3502
+ __clk_recalc_accuracies(orphan);
3503
+ __clk_recalc_rates(orphan, 0);
3504
+ __clk_core_update_orphan_hold_state(orphan);
3505
+
3506
+ /*
3507
+ * __clk_init_parent() will set the initial req_rate to
3508
+ * 0 if the clock doesn't have clk_ops::recalc_rate and
3509
+ * is an orphan when it's registered.
3510
+ *
3511
+ * 'req_rate' is used by clk_set_rate_range() and
3512
+ * clk_put() to trigger a clk_set_rate() call whenever
3513
+ * the boundaries are modified. Let's make sure
3514
+ * 'req_rate' is set to something non-zero so that
3515
+ * clk_set_rate_range() doesn't drop the frequency.
3516
+ */
3517
+ orphan->req_rate = orphan->rate;
3518
+ }
3519
+ }
3520
+}
36963521
36973522 /**
36983523 * __clk_core_init - initialize the data structures in a struct clk_core
....@@ -3703,15 +3528,23 @@
37033528 */
37043529 static int __clk_core_init(struct clk_core *core)
37053530 {
3706
- int i, ret;
3707
- struct clk_core *orphan;
3708
- struct hlist_node *tmp2;
3531
+ int ret;
3532
+ struct clk_core *parent;
37093533 unsigned long rate;
3534
+ int phase;
37103535
37113536 if (!core)
37123537 return -EINVAL;
37133538
37143539 clk_prepare_lock();
3540
+
3541
+ /*
3542
+ * Set hw->core after grabbing the prepare_lock to synchronize with
3543
+ * callers of clk_core_fill_parent_index() where we treat hw->core
3544
+ * being NULL as the clk not being registered yet. This is crucial so
3545
+ * that clks aren't parented until their parent is fully registered.
3546
+ */
3547
+ core->hw->core = core;
37153548
37163549 ret = clk_pm_runtime_get(core);
37173550 if (ret)
....@@ -3757,13 +3590,27 @@
37573590 goto out;
37583591 }
37593592
3760
- /* throw a WARN if any entries in parent_names are NULL */
3761
- for (i = 0; i < core->num_parents; i++)
3762
- WARN(!core->parent_names[i],
3763
- "%s: invalid NULL in %s's .parent_names\n",
3764
- __func__, core->name);
3593
+ /*
3594
+ * optional platform-specific magic
3595
+ *
3596
+ * The .init callback is not used by any of the basic clock types, but
3597
+ * exists for weird hardware that must perform initialization magic for
3598
+ * CCF to get an accurate view of clock for any other callbacks. It may
3599
+ * also be used needs to perform dynamic allocations. Such allocation
3600
+ * must be freed in the terminate() callback.
3601
+ * This callback shall not be used to initialize the parameters state,
3602
+ * such as rate, parent, etc ...
3603
+ *
3604
+ * If it exist, this callback should called before any other callback of
3605
+ * the clock
3606
+ */
3607
+ if (core->ops->init) {
3608
+ ret = core->ops->init(core->hw);
3609
+ if (ret)
3610
+ goto out;
3611
+ }
37653612
3766
- core->parent = __clk_init_parent(core);
3613
+ parent = core->parent = __clk_init_parent(core);
37673614
37683615 /*
37693616 * Populate core->parent if parent has already been clk_core_init'd. If
....@@ -3775,10 +3622,9 @@
37753622 * clocks and re-parent any that are children of the clock currently
37763623 * being clk_init'd.
37773624 */
3778
- if (core->parent) {
3779
- hlist_add_head(&core->child_node,
3780
- &core->parent->children);
3781
- core->orphan = core->parent->orphan;
3625
+ if (parent) {
3626
+ hlist_add_head(&core->child_node, &parent->children);
3627
+ core->orphan = parent->orphan;
37823628 } else if (!core->num_parents) {
37833629 hlist_add_head(&core->child_node, &clk_root_list);
37843630 core->orphan = false;
....@@ -3786,17 +3632,6 @@
37863632 hlist_add_head(&core->child_node, &clk_orphan_list);
37873633 core->orphan = true;
37883634 }
3789
-
3790
- /*
3791
- * optional platform-specific magic
3792
- *
3793
- * The .init callback is not used by any of the basic clock types, but
3794
- * exists for weird hardware that must perform initialization magic.
3795
- * Please consider other ways of solving initialization problems before
3796
- * using this callback, as its use is discouraged.
3797
- */
3798
- if (core->ops->init)
3799
- core->ops->init(core->hw);
38003635
38013636 /*
38023637 * Set clk's accuracy. The preferred method is to use
....@@ -3807,21 +3642,24 @@
38073642 */
38083643 if (core->ops->recalc_accuracy)
38093644 core->accuracy = core->ops->recalc_accuracy(core->hw,
3810
- __clk_get_accuracy(core->parent));
3811
- else if (core->parent)
3812
- core->accuracy = core->parent->accuracy;
3645
+ clk_core_get_accuracy_no_lock(parent));
3646
+ else if (parent)
3647
+ core->accuracy = parent->accuracy;
38133648 else
38143649 core->accuracy = 0;
38153650
38163651 /*
3817
- * Set clk's phase.
3652
+ * Set clk's phase by clk_core_get_phase() caching the phase.
38183653 * Since a phase is by definition relative to its parent, just
38193654 * query the current clock phase, or just assume it's in phase.
38203655 */
3821
- if (core->ops->get_phase)
3822
- core->phase = core->ops->get_phase(core->hw);
3823
- else
3824
- core->phase = 0;
3656
+ phase = clk_core_get_phase(core);
3657
+ if (phase < 0) {
3658
+ ret = phase;
3659
+ pr_warn("%s: Failed to get phase for clk '%s'\n", __func__,
3660
+ core->name);
3661
+ goto out;
3662
+ }
38253663
38263664 /*
38273665 * Set clk's duty cycle.
....@@ -3836,9 +3674,9 @@
38363674 */
38373675 if (core->ops->recalc_rate)
38383676 rate = core->ops->recalc_rate(core->hw,
3839
- clk_core_get_rate_nolock(core->parent));
3840
- else if (core->parent)
3841
- rate = core->parent->rate;
3677
+ clk_core_get_rate_nolock(parent));
3678
+ else if (parent)
3679
+ rate = parent->rate;
38423680 else
38433681 rate = 0;
38443682 core->rate = core->req_rate = rate;
....@@ -3853,96 +3691,36 @@
38533691 if (core->flags & CLK_IS_CRITICAL) {
38543692 unsigned long flags;
38553693
3856
- clk_core_prepare(core);
3694
+ ret = clk_core_prepare(core);
3695
+ if (ret) {
3696
+ pr_warn("%s: critical clk '%s' failed to prepare\n",
3697
+ __func__, core->name);
3698
+ goto out;
3699
+ }
38573700
38583701 flags = clk_enable_lock();
3859
- clk_core_enable(core);
3702
+ ret = clk_core_enable(core);
38603703 clk_enable_unlock(flags);
3704
+ if (ret) {
3705
+ pr_warn("%s: critical clk '%s' failed to enable\n",
3706
+ __func__, core->name);
3707
+ clk_core_unprepare(core);
3708
+ goto out;
3709
+ }
38613710 }
38623711
38633712 clk_core_hold_state(core);
3713
+ clk_core_reparent_orphans_nolock();
38643714
3865
- /*
3866
- * walk the list of orphan clocks and reparent any that newly finds a
3867
- * parent.
3868
- */
3869
- hlist_for_each_entry_safe(orphan, tmp2, &clk_orphan_list, child_node) {
3870
- struct clk_core *parent = __clk_init_parent(orphan);
3871
-
3872
- /*
3873
- * We need to use __clk_set_parent_before() and _after() to
3874
- * to properly migrate any prepare/enable count of the orphan
3875
- * clock. This is important for CLK_IS_CRITICAL clocks, which
3876
- * are enabled during init but might not have a parent yet.
3877
- */
3878
- if (parent) {
3879
- /* update the clk tree topology */
3880
- __clk_set_parent_before(orphan, parent);
3881
- __clk_set_parent_after(orphan, parent, NULL);
3882
- __clk_recalc_accuracies(orphan);
3883
- __clk_recalc_rates(orphan, 0);
3884
- __clk_core_update_orphan_hold_state(orphan);
3885
- }
3886
- }
3887
-
3888
- /*
3889
- * optional platform-specific magic
3890
- *
3891
- * The .init callback is not used by any of the basic clock types, but
3892
- * exists for weird hardware that must perform initialization magic.
3893
- * Please consider other ways of solving initialization problems before
3894
- * using this callback, as its use is discouraged.
3895
- */
3896
- if (core->ops->init)
3897
- core->ops->init(core->hw);
3898
-
3899
- if (core->flags & CLK_IS_CRITICAL) {
3900
- unsigned long flags;
3901
-
3902
- clk_core_prepare(core);
3903
-
3904
- flags = clk_enable_lock();
3905
- clk_core_enable(core);
3906
- clk_enable_unlock(flags);
3907
- }
3908
-
3909
- /*
3910
- * enable clocks with the CLK_ENABLE_HAND_OFF flag set
3911
- *
3912
- * This flag causes the framework to enable the clock at registration
3913
- * time, which is sometimes necessary for clocks that would cause a
3914
- * system crash when gated (e.g. cpu, memory, etc). The prepare_count
3915
- * is migrated over to the first clk consumer to call clk_prepare().
3916
- * Similarly the clk's enable_count is migrated to the first consumer
3917
- * to call clk_enable().
3918
- */
3919
- if (core->flags & CLK_ENABLE_HAND_OFF) {
3920
- unsigned long flags;
3921
-
3922
- /*
3923
- * Few clocks might have hardware gating which would be
3924
- * required to be ON before prepare/enabling the clocks. So
3925
- * check if the clock has been turned ON earlier and we should
3926
- * prepare/enable those clocks.
3927
- */
3928
- if (clk_core_is_enabled(core)) {
3929
- core->need_handoff_prepare = true;
3930
- core->need_handoff_enable = true;
3931
- ret = clk_core_prepare(core);
3932
- if (ret)
3933
- goto out;
3934
- flags = clk_enable_lock();
3935
- clk_core_enable(core);
3936
- clk_enable_unlock(flags);
3937
- }
3938
- }
39393715
39403716 kref_init(&core->ref);
39413717 out:
39423718 clk_pm_runtime_put(core);
39433719 unlock:
3944
- if (ret)
3720
+ if (ret) {
39453721 hlist_del_init(&core->child_node);
3722
+ core->hw->core = NULL;
3723
+ }
39463724
39473725 clk_prepare_unlock();
39483726
....@@ -3952,8 +3730,38 @@
39523730 return ret;
39533731 }
39543732
3955
-static struct clk *clk_hw_create_clk(struct clk_hw *hw, const char *dev_id,
3956
- const char *con_id)
3733
+/**
3734
+ * clk_core_link_consumer - Add a clk consumer to the list of consumers in a clk_core
3735
+ * @core: clk to add consumer to
3736
+ * @clk: consumer to link to a clk
3737
+ */
3738
+static void clk_core_link_consumer(struct clk_core *core, struct clk *clk)
3739
+{
3740
+ clk_prepare_lock();
3741
+ hlist_add_head(&clk->clks_node, &core->clks);
3742
+ clk_prepare_unlock();
3743
+}
3744
+
3745
+/**
3746
+ * clk_core_unlink_consumer - Remove a clk consumer from the list of consumers in a clk_core
3747
+ * @clk: consumer to unlink
3748
+ */
3749
+static void clk_core_unlink_consumer(struct clk *clk)
3750
+{
3751
+ lockdep_assert_held(&prepare_lock);
3752
+ hlist_del(&clk->clks_node);
3753
+}
3754
+
3755
+/**
3756
+ * alloc_clk - Allocate a clk consumer, but leave it unlinked to the clk_core
3757
+ * @core: clk to allocate a consumer for
3758
+ * @dev_id: string describing device name
3759
+ * @con_id: connection ID string on device
3760
+ *
3761
+ * Returns: clk consumer left unlinked from the consumer list
3762
+ */
3763
+static struct clk *alloc_clk(struct clk_core *core, const char *dev_id,
3764
+ const char *con_id)
39573765 {
39583766 struct clk *clk;
39593767
....@@ -3961,56 +3769,192 @@
39613769 if (!clk)
39623770 return ERR_PTR(-ENOMEM);
39633771
3964
- clk->core = hw->core;
3772
+ clk->core = core;
39653773 clk->dev_id = dev_id;
39663774 clk->con_id = kstrdup_const(con_id, GFP_KERNEL);
39673775 clk->max_rate = ULONG_MAX;
39683776
3969
- clk_prepare_lock();
3970
- hlist_add_head(&clk->clks_node, &hw->core->clks);
3971
- clk_prepare_unlock();
3972
-
39733777 return clk;
39743778 }
39753779
3976
-struct clk *__clk_create_clk(struct clk_hw *hw, const char *dev_id,
3977
- const char *con_id, bool with_orphans)
3780
+/**
3781
+ * free_clk - Free a clk consumer
3782
+ * @clk: clk consumer to free
3783
+ *
3784
+ * Note, this assumes the clk has been unlinked from the clk_core consumer
3785
+ * list.
3786
+ */
3787
+static void free_clk(struct clk *clk)
39783788 {
3979
- /* This is to allow this function to be chained to others */
3980
- if (!hw || IS_ERR(hw))
3981
- return (struct clk *) hw;
3982
-
3983
- if (hw->core->orphan && !with_orphans)
3984
- return ERR_PTR(-EPROBE_DEFER);
3985
-
3986
- return clk_hw_create_clk(hw, dev_id, con_id);
3987
-}
3988
-
3989
-void __clk_free_clk(struct clk *clk)
3990
-{
3991
- clk_prepare_lock();
3992
- hlist_del(&clk->clks_node);
3993
- clk_prepare_unlock();
3994
-
39953789 kfree_const(clk->con_id);
39963790 kfree(clk);
39973791 }
39983792
39993793 /**
4000
- * clk_register - allocate a new clock, register it and return an opaque cookie
4001
- * @dev: device that is registering this clock
4002
- * @hw: link to hardware-specific clock data
3794
+ * clk_hw_create_clk: Allocate and link a clk consumer to a clk_core given
3795
+ * a clk_hw
3796
+ * @dev: clk consumer device
3797
+ * @hw: clk_hw associated with the clk being consumed
3798
+ * @dev_id: string describing device name
3799
+ * @con_id: connection ID string on device
40033800 *
4004
- * clk_register is the primary interface for populating the clock tree with new
4005
- * clock nodes. It returns a pointer to the newly allocated struct clk which
4006
- * cannot be dereferenced by driver code but may be used in conjunction with the
4007
- * rest of the clock API. In the event of an error clk_register will return an
4008
- * error code; drivers must test for an error code after calling clk_register.
3801
+ * This is the main function used to create a clk pointer for use by clk
3802
+ * consumers. It connects a consumer to the clk_core and clk_hw structures
3803
+ * used by the framework and clk provider respectively.
40093804 */
4010
-struct clk *clk_register(struct device *dev, struct clk_hw *hw)
3805
+struct clk *clk_hw_create_clk(struct device *dev, struct clk_hw *hw,
3806
+ const char *dev_id, const char *con_id)
40113807 {
4012
- int i, ret;
3808
+ struct clk *clk;
40133809 struct clk_core *core;
3810
+
3811
+ /* This is to allow this function to be chained to others */
3812
+ if (IS_ERR_OR_NULL(hw))
3813
+ return ERR_CAST(hw);
3814
+
3815
+ core = hw->core;
3816
+ clk = alloc_clk(core, dev_id, con_id);
3817
+ if (IS_ERR(clk))
3818
+ return clk;
3819
+ clk->dev = dev;
3820
+
3821
+ if (!try_module_get(core->owner)) {
3822
+ free_clk(clk);
3823
+ return ERR_PTR(-ENOENT);
3824
+ }
3825
+
3826
+ kref_get(&core->ref);
3827
+ clk_core_link_consumer(core, clk);
3828
+
3829
+ return clk;
3830
+}
3831
+
3832
+/**
3833
+ * clk_hw_get_clk - get clk consumer given an clk_hw
3834
+ * @hw: clk_hw associated with the clk being consumed
3835
+ * @con_id: connection ID string on device
3836
+ *
3837
+ * Returns: new clk consumer
3838
+ * This is the function to be used by providers which need
3839
+ * to get a consumer clk and act on the clock element
3840
+ * Calls to this function must be balanced with calls clk_put()
3841
+ */
3842
+struct clk *clk_hw_get_clk(struct clk_hw *hw, const char *con_id)
3843
+{
3844
+ struct device *dev = hw->core->dev;
3845
+ const char *name = dev ? dev_name(dev) : NULL;
3846
+
3847
+ return clk_hw_create_clk(dev, hw, name, con_id);
3848
+}
3849
+EXPORT_SYMBOL(clk_hw_get_clk);
3850
+
3851
+static int clk_cpy_name(const char **dst_p, const char *src, bool must_exist)
3852
+{
3853
+ const char *dst;
3854
+
3855
+ if (!src) {
3856
+ if (must_exist)
3857
+ return -EINVAL;
3858
+ return 0;
3859
+ }
3860
+
3861
+ *dst_p = dst = kstrdup_const(src, GFP_KERNEL);
3862
+ if (!dst)
3863
+ return -ENOMEM;
3864
+
3865
+ return 0;
3866
+}
3867
+
3868
+static int clk_core_populate_parent_map(struct clk_core *core,
3869
+ const struct clk_init_data *init)
3870
+{
3871
+ u8 num_parents = init->num_parents;
3872
+ const char * const *parent_names = init->parent_names;
3873
+ const struct clk_hw **parent_hws = init->parent_hws;
3874
+ const struct clk_parent_data *parent_data = init->parent_data;
3875
+ int i, ret = 0;
3876
+ struct clk_parent_map *parents, *parent;
3877
+
3878
+ if (!num_parents)
3879
+ return 0;
3880
+
3881
+ /*
3882
+ * Avoid unnecessary string look-ups of clk_core's possible parents by
3883
+ * having a cache of names/clk_hw pointers to clk_core pointers.
3884
+ */
3885
+ parents = kcalloc(num_parents, sizeof(*parents), GFP_KERNEL);
3886
+ core->parents = parents;
3887
+ if (!parents)
3888
+ return -ENOMEM;
3889
+
3890
+ /* Copy everything over because it might be __initdata */
3891
+ for (i = 0, parent = parents; i < num_parents; i++, parent++) {
3892
+ parent->index = -1;
3893
+ if (parent_names) {
3894
+ /* throw a WARN if any entries are NULL */
3895
+ WARN(!parent_names[i],
3896
+ "%s: invalid NULL in %s's .parent_names\n",
3897
+ __func__, core->name);
3898
+ ret = clk_cpy_name(&parent->name, parent_names[i],
3899
+ true);
3900
+ } else if (parent_data) {
3901
+ parent->hw = parent_data[i].hw;
3902
+ parent->index = parent_data[i].index;
3903
+ ret = clk_cpy_name(&parent->fw_name,
3904
+ parent_data[i].fw_name, false);
3905
+ if (!ret)
3906
+ ret = clk_cpy_name(&parent->name,
3907
+ parent_data[i].name,
3908
+ false);
3909
+ } else if (parent_hws) {
3910
+ parent->hw = parent_hws[i];
3911
+ } else {
3912
+ ret = -EINVAL;
3913
+ WARN(1, "Must specify parents if num_parents > 0\n");
3914
+ }
3915
+
3916
+ if (ret) {
3917
+ do {
3918
+ kfree_const(parents[i].name);
3919
+ kfree_const(parents[i].fw_name);
3920
+ } while (--i >= 0);
3921
+ kfree(parents);
3922
+
3923
+ return ret;
3924
+ }
3925
+ }
3926
+
3927
+ return 0;
3928
+}
3929
+
3930
+static void clk_core_free_parent_map(struct clk_core *core)
3931
+{
3932
+ int i = core->num_parents;
3933
+
3934
+ if (!core->num_parents)
3935
+ return;
3936
+
3937
+ while (--i >= 0) {
3938
+ kfree_const(core->parents[i].name);
3939
+ kfree_const(core->parents[i].fw_name);
3940
+ }
3941
+
3942
+ kfree(core->parents);
3943
+}
3944
+
3945
+static struct clk *
3946
+__clk_register(struct device *dev, struct device_node *np, struct clk_hw *hw)
3947
+{
3948
+ int ret;
3949
+ struct clk_core *core;
3950
+ const struct clk_init_data *init = hw->init;
3951
+
3952
+ /*
3953
+ * The init data is not supposed to be used outside of registration path.
3954
+ * Set it to NULL so that provider drivers can't use it either and so that
3955
+ * we catch use of hw->init early on in the core.
3956
+ */
3957
+ hw->init = NULL;
40143958
40153959 core = kzalloc(sizeof(*core), GFP_KERNEL);
40163960 if (!core) {
....@@ -4018,90 +3962,108 @@
40183962 goto fail_out;
40193963 }
40203964
4021
- core->name = kstrdup_const(hw->init->name, GFP_KERNEL);
3965
+ core->name = kstrdup_const(init->name, GFP_KERNEL);
40223966 if (!core->name) {
40233967 ret = -ENOMEM;
40243968 goto fail_name;
40253969 }
40263970
4027
- if (WARN_ON(!hw->init->ops)) {
3971
+ if (WARN_ON(!init->ops)) {
40283972 ret = -EINVAL;
40293973 goto fail_ops;
40303974 }
4031
- core->ops = hw->init->ops;
3975
+ core->ops = init->ops;
40323976
40333977 if (dev && pm_runtime_enabled(dev))
40343978 core->rpm_enabled = true;
40353979 core->dev = dev;
3980
+ core->of_node = np;
40363981 if (dev && dev->driver)
40373982 core->owner = dev->driver->owner;
40383983 core->hw = hw;
4039
- core->flags = hw->init->flags;
4040
- core->num_parents = hw->init->num_parents;
3984
+ core->flags = init->flags;
3985
+ core->num_parents = init->num_parents;
40413986 core->min_rate = 0;
40423987 core->max_rate = ULONG_MAX;
4043
- core->vdd_class = hw->init->vdd_class;
4044
- core->rate_max = hw->init->rate_max;
4045
- core->num_rate_max = hw->init->num_rate_max;
4046
- hw->core = core;
40473988
4048
- /* allocate local copy in case parent_names is __initdata */
4049
- core->parent_names = kcalloc(core->num_parents, sizeof(char *),
4050
- GFP_KERNEL);
4051
-
4052
- if (!core->parent_names) {
4053
- ret = -ENOMEM;
4054
- goto fail_parent_names;
4055
- }
4056
-
4057
-
4058
- /* copy each string name in case parent_names is __initdata */
4059
- for (i = 0; i < core->num_parents; i++) {
4060
- core->parent_names[i] = kstrdup_const(hw->init->parent_names[i],
4061
- GFP_KERNEL);
4062
- if (!core->parent_names[i]) {
4063
- ret = -ENOMEM;
4064
- goto fail_parent_names_copy;
4065
- }
4066
- }
4067
-
4068
- /* avoid unnecessary string look-ups of clk_core's possible parents. */
4069
- core->parents = kcalloc(core->num_parents, sizeof(*core->parents),
4070
- GFP_KERNEL);
4071
- if (!core->parents) {
4072
- ret = -ENOMEM;
3989
+ ret = clk_core_populate_parent_map(core, init);
3990
+ if (ret)
40733991 goto fail_parents;
4074
- };
40753992
40763993 INIT_HLIST_HEAD(&core->clks);
4077
- INIT_LIST_HEAD(&core->rate_change_node);
40783994
4079
- hw->clk = clk_hw_create_clk(hw, NULL, NULL);
3995
+ /*
3996
+ * Don't call clk_hw_create_clk() here because that would pin the
3997
+ * provider module to itself and prevent it from ever being removed.
3998
+ */
3999
+ hw->clk = alloc_clk(core, NULL, NULL);
40804000 if (IS_ERR(hw->clk)) {
40814001 ret = PTR_ERR(hw->clk);
4082
- goto fail_parents;
4002
+ goto fail_create_clk;
40834003 }
4004
+
4005
+ clk_core_link_consumer(core, hw->clk);
40844006
40854007 ret = __clk_core_init(core);
40864008 if (!ret)
40874009 return hw->clk;
40884010
4089
- __clk_free_clk(hw->clk);
4011
+ clk_prepare_lock();
4012
+ clk_core_unlink_consumer(hw->clk);
4013
+ clk_prepare_unlock();
4014
+
4015
+ free_clk(hw->clk);
40904016 hw->clk = NULL;
40914017
4018
+fail_create_clk:
4019
+ clk_core_free_parent_map(core);
40924020 fail_parents:
4093
- kfree(core->parents);
4094
-fail_parent_names_copy:
4095
- while (--i >= 0)
4096
- kfree_const(core->parent_names[i]);
4097
- kfree(core->parent_names);
4098
-fail_parent_names:
40994021 fail_ops:
41004022 kfree_const(core->name);
41014023 fail_name:
41024024 kfree(core);
41034025 fail_out:
41044026 return ERR_PTR(ret);
4027
+}
4028
+
4029
+/**
4030
+ * dev_or_parent_of_node() - Get device node of @dev or @dev's parent
4031
+ * @dev: Device to get device node of
4032
+ *
4033
+ * Return: device node pointer of @dev, or the device node pointer of
4034
+ * @dev->parent if dev doesn't have a device node, or NULL if neither
4035
+ * @dev or @dev->parent have a device node.
4036
+ */
4037
+static struct device_node *dev_or_parent_of_node(struct device *dev)
4038
+{
4039
+ struct device_node *np;
4040
+
4041
+ if (!dev)
4042
+ return NULL;
4043
+
4044
+ np = dev_of_node(dev);
4045
+ if (!np)
4046
+ np = dev_of_node(dev->parent);
4047
+
4048
+ return np;
4049
+}
4050
+
4051
+/**
4052
+ * clk_register - allocate a new clock, register it and return an opaque cookie
4053
+ * @dev: device that is registering this clock
4054
+ * @hw: link to hardware-specific clock data
4055
+ *
4056
+ * clk_register is the *deprecated* interface for populating the clock tree with
4057
+ * new clock nodes. Use clk_hw_register() instead.
4058
+ *
4059
+ * Returns: a pointer to the newly allocated struct clk which
4060
+ * cannot be dereferenced by driver code but may be used in conjunction with the
4061
+ * rest of the clock API. In the event of an error clk_register will return an
4062
+ * error code; drivers must test for an error code after calling clk_register.
4063
+ */
4064
+struct clk *clk_register(struct device *dev, struct clk_hw *hw)
4065
+{
4066
+ return __clk_register(dev, dev_or_parent_of_node(dev), hw);
41054067 }
41064068 EXPORT_SYMBOL_GPL(clk_register);
41074069
....@@ -4117,23 +4079,36 @@
41174079 */
41184080 int clk_hw_register(struct device *dev, struct clk_hw *hw)
41194081 {
4120
- return PTR_ERR_OR_ZERO(clk_register(dev, hw));
4082
+ return PTR_ERR_OR_ZERO(__clk_register(dev, dev_or_parent_of_node(dev),
4083
+ hw));
41214084 }
41224085 EXPORT_SYMBOL_GPL(clk_hw_register);
4086
+
4087
+/*
4088
+ * of_clk_hw_register - register a clk_hw and return an error code
4089
+ * @node: device_node of device that is registering this clock
4090
+ * @hw: link to hardware-specific clock data
4091
+ *
4092
+ * of_clk_hw_register() is the primary interface for populating the clock tree
4093
+ * with new clock nodes when a struct device is not available, but a struct
4094
+ * device_node is. It returns an integer equal to zero indicating success or
4095
+ * less than zero indicating failure. Drivers must test for an error code after
4096
+ * calling of_clk_hw_register().
4097
+ */
4098
+int of_clk_hw_register(struct device_node *node, struct clk_hw *hw)
4099
+{
4100
+ return PTR_ERR_OR_ZERO(__clk_register(NULL, node, hw));
4101
+}
4102
+EXPORT_SYMBOL_GPL(of_clk_hw_register);
41234103
41244104 /* Free memory allocated for a clock. */
41254105 static void __clk_release(struct kref *ref)
41264106 {
41274107 struct clk_core *core = container_of(ref, struct clk_core, ref);
4128
- int i = core->num_parents;
41294108
41304109 lockdep_assert_held(&prepare_lock);
41314110
4132
- kfree(core->parents);
4133
- while (--i >= 0)
4134
- kfree_const(core->parent_names[i]);
4135
-
4136
- kfree(core->parent_names);
4111
+ clk_core_free_parent_map(core);
41374112 kfree_const(core->name);
41384113 kfree(core);
41394114 }
....@@ -4180,8 +4155,8 @@
41804155 struct clk_core *child;
41814156
41824157 for (i = 0; i < root->num_parents; i++)
4183
- if (root->parents[i] == target)
4184
- root->parents[i] = NULL;
4158
+ if (root->parents[i].core == target)
4159
+ root->parents[i].core = NULL;
41854160
41864161 hlist_for_each_entry(child, &root->children, child_node)
41874162 clk_core_evict_parent_cache_subtree(child, target);
....@@ -4208,6 +4183,7 @@
42084183 void clk_unregister(struct clk *clk)
42094184 {
42104185 unsigned long flags;
4186
+ const struct clk_ops *ops;
42114187
42124188 if (!clk || WARN_ON_ONCE(IS_ERR(clk)))
42134189 return;
....@@ -4216,7 +4192,8 @@
42164192
42174193 clk_prepare_lock();
42184194
4219
- if (clk->core->ops == &clk_nodrv_ops) {
4195
+ ops = clk->core->ops;
4196
+ if (ops == &clk_nodrv_ops) {
42204197 pr_err("%s: unregistered clock: %s\n", __func__,
42214198 clk->core->name);
42224199 goto unlock;
....@@ -4228,6 +4205,9 @@
42284205 flags = clk_enable_lock();
42294206 clk->core->ops = &clk_nodrv_ops;
42304207 clk_enable_unlock(flags);
4208
+
4209
+ if (ops->terminate)
4210
+ ops->terminate(clk->core->hw);
42314211
42324212 if (!hlist_empty(&clk->core->children)) {
42334213 struct clk_core *child;
....@@ -4252,6 +4232,7 @@
42524232 __func__, clk->core->name);
42534233
42544234 kref_put(&clk->core->ref, __clk_release);
4235
+ free_clk(clk);
42554236 unlock:
42564237 clk_prepare_unlock();
42574238 }
....@@ -4267,174 +4248,14 @@
42674248 }
42684249 EXPORT_SYMBOL_GPL(clk_hw_unregister);
42694250
4270
-static void devm_clk_release(struct device *dev, void *res)
4251
+static void devm_clk_unregister_cb(struct device *dev, void *res)
42714252 {
42724253 clk_unregister(*(struct clk **)res);
42734254 }
42744255
4275
-static void devm_clk_hw_release(struct device *dev, void *res)
4256
+static void devm_clk_hw_unregister_cb(struct device *dev, void *res)
42764257 {
42774258 clk_hw_unregister(*(struct clk_hw **)res);
4278
-}
4279
-
4280
-#define MAX_LEN_OPP_HANDLE 50
4281
-#define LEN_OPP_HANDLE 16
4282
-
4283
-static int derive_device_list(struct device **device_list,
4284
- struct clk_core *core,
4285
- struct device_node *np,
4286
- char *clk_handle_name, int count)
4287
-{
4288
- int j;
4289
- struct platform_device *pdev;
4290
- struct device_node *dev_node;
4291
-
4292
- for (j = 0; j < count; j++) {
4293
- device_list[j] = NULL;
4294
- dev_node = of_parse_phandle(np, clk_handle_name, j);
4295
- if (!dev_node) {
4296
- pr_err("Unable to get device_node pointer for %s opp-handle (%s)\n",
4297
- core->name, clk_handle_name);
4298
- return -ENODEV;
4299
- }
4300
-
4301
- pdev = of_find_device_by_node(dev_node);
4302
- if (!pdev) {
4303
- pr_err("Unable to find platform_device node for %s opp-handle\n",
4304
- core->name);
4305
- return -ENODEV;
4306
- }
4307
- device_list[j] = &pdev->dev;
4308
- }
4309
- return 0;
4310
-}
4311
-
4312
-static int clk_get_voltage(struct clk_core *core, unsigned long rate, int n)
4313
-{
4314
- struct clk_vdd_class *vdd;
4315
- int level, corner;
4316
-
4317
- /* Use the first regulator in the vdd class for the OPP table. */
4318
- vdd = core->vdd_class;
4319
- if (vdd->num_regulators > 1) {
4320
- corner = vdd->vdd_uv[vdd->num_regulators * n];
4321
- } else {
4322
- level = clk_find_vdd_level(core, rate);
4323
- if (level < 0) {
4324
- pr_err("Could not find vdd level\n");
4325
- return -EINVAL;
4326
- }
4327
- corner = vdd->vdd_uv[level];
4328
- }
4329
-
4330
- if (!corner) {
4331
- pr_err("%s: Unable to find vdd level for rate %lu\n",
4332
- core->name, rate);
4333
- return -EINVAL;
4334
- }
4335
-
4336
- return corner;
4337
-}
4338
-
4339
-static int clk_add_and_print_opp(struct clk_hw *hw,
4340
- struct device **device_list, int count,
4341
- unsigned long rate, int uv, int n)
4342
-{
4343
- struct clk_core *core = hw->core;
4344
- int j, ret = 0;
4345
-
4346
- for (j = 0; j < count; j++) {
4347
- ret = dev_pm_opp_add(device_list[j], rate, uv);
4348
- if (ret) {
4349
- pr_err("%s: couldn't add OPP for %lu - err: %d\n",
4350
- core->name, rate, ret);
4351
- return ret;
4352
- }
4353
-
4354
- if (n == 0 || n == core->num_rate_max - 1 ||
4355
- rate == clk_hw_round_rate(hw, INT_MAX))
4356
- pr_info("%s: set OPP pair(%lu Hz: %u uV) on %s\n",
4357
- core->name, rate, uv,
4358
- dev_name(device_list[j]));
4359
- }
4360
- return ret;
4361
-}
4362
-
4363
-static void clk_populate_clock_opp_table(struct device_node *np,
4364
- struct clk_hw *hw)
4365
-{
4366
- struct device **device_list;
4367
- struct clk_core *core = hw->core;
4368
- char clk_handle_name[MAX_LEN_OPP_HANDLE];
4369
- int n, len, count, uv, ret;
4370
- unsigned long rate = 0, rrate = 0;
4371
-
4372
- if (!core || !core->num_rate_max)
4373
- return;
4374
-
4375
- if (strlen(core->name) + LEN_OPP_HANDLE < MAX_LEN_OPP_HANDLE) {
4376
- ret = snprintf(clk_handle_name, ARRAY_SIZE(clk_handle_name),
4377
- "qcom,%s-opp-handle", core->name);
4378
- if (ret < strlen(core->name) + LEN_OPP_HANDLE) {
4379
- pr_err("%s: Failed to hold clk_handle_name\n",
4380
- core->name);
4381
- return;
4382
- }
4383
- } else {
4384
- pr_err("clk name (%s) too large to fit in clk_handle_name\n",
4385
- core->name);
4386
- return;
4387
- }
4388
-
4389
- if (of_find_property(np, clk_handle_name, &len)) {
4390
- count = len/sizeof(u32);
4391
-
4392
- device_list = kmalloc_array(count, sizeof(struct device *),
4393
- GFP_KERNEL);
4394
- if (!device_list)
4395
- return;
4396
-
4397
- ret = derive_device_list(device_list, core, np,
4398
- clk_handle_name, count);
4399
- if (ret < 0) {
4400
- pr_err("Failed to fill device_list for %s\n",
4401
- clk_handle_name);
4402
- goto err_derive_device_list;
4403
- }
4404
- } else {
4405
- pr_debug("Unable to find %s\n", clk_handle_name);
4406
- return;
4407
- }
4408
-
4409
- for (n = 0; ; n++) {
4410
- rrate = clk_hw_round_rate(hw, rate + 1);
4411
- if (!rrate) {
4412
- pr_err("clk_round_rate failed for %s\n",
4413
- core->name);
4414
- goto err_derive_device_list;
4415
- }
4416
-
4417
- /*
4418
- * If clk_hw_round_rate gives the same value on consecutive
4419
- * iterations, exit the loop since we're at the maximum clock
4420
- * frequency.
4421
- */
4422
- if (rate == rrate)
4423
- break;
4424
- rate = rrate;
4425
-
4426
- uv = clk_get_voltage(core, rate, n);
4427
- if (uv < 0)
4428
- goto err_derive_device_list;
4429
-
4430
- ret = clk_add_and_print_opp(hw, device_list, count,
4431
- rate, uv, n);
4432
- if (ret)
4433
- goto err_derive_device_list;
4434
- }
4435
-
4436
-err_derive_device_list:
4437
- kfree(device_list);
44384259 }
44394260
44404261 /**
....@@ -4442,16 +4263,17 @@
44424263 * @dev: device that is registering this clock
44434264 * @hw: link to hardware-specific clock data
44444265 *
4445
- * Managed clk_register(). Clocks returned from this function are
4446
- * automatically clk_unregister()ed on driver detach. See clk_register() for
4447
- * more information.
4266
+ * Managed clk_register(). This function is *deprecated*, use devm_clk_hw_register() instead.
4267
+ *
4268
+ * Clocks returned from this function are automatically clk_unregister()ed on
4269
+ * driver detach. See clk_register() for more information.
44484270 */
44494271 struct clk *devm_clk_register(struct device *dev, struct clk_hw *hw)
44504272 {
44514273 struct clk *clk;
44524274 struct clk **clkp;
44534275
4454
- clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
4276
+ clkp = devres_alloc(devm_clk_unregister_cb, sizeof(*clkp), GFP_KERNEL);
44554277 if (!clkp)
44564278 return ERR_PTR(-ENOMEM);
44574279
....@@ -4463,7 +4285,6 @@
44634285 devres_free(clkp);
44644286 }
44654287
4466
- clk_populate_clock_opp_table(dev->of_node, hw);
44674288 return clk;
44684289 }
44694290 EXPORT_SYMBOL_GPL(devm_clk_register);
....@@ -4482,7 +4303,7 @@
44824303 struct clk_hw **hwp;
44834304 int ret;
44844305
4485
- hwp = devres_alloc(devm_clk_hw_release, sizeof(*hwp), GFP_KERNEL);
4306
+ hwp = devres_alloc(devm_clk_hw_unregister_cb, sizeof(*hwp), GFP_KERNEL);
44864307 if (!hwp)
44874308 return -ENOMEM;
44884309
....@@ -4494,7 +4315,6 @@
44944315 devres_free(hwp);
44954316 }
44964317
4497
- clk_populate_clock_opp_table(dev->of_node, hw);
44984318 return ret;
44994319 }
45004320 EXPORT_SYMBOL_GPL(devm_clk_hw_register);
....@@ -4518,6 +4338,7 @@
45184338
45194339 /**
45204340 * devm_clk_unregister - resource managed clk_unregister()
4341
+ * @dev: device that is unregistering the clock data
45214342 * @clk: clock to unregister
45224343 *
45234344 * Deallocate a clock allocated with devm_clk_register(). Normally
....@@ -4526,7 +4347,7 @@
45264347 */
45274348 void devm_clk_unregister(struct device *dev, struct clk *clk)
45284349 {
4529
- WARN_ON(devres_release(dev, devm_clk_release, devm_clk_match, clk));
4350
+ WARN_ON(devres_release(dev, devm_clk_unregister_cb, devm_clk_match, clk));
45304351 }
45314352 EXPORT_SYMBOL_GPL(devm_clk_unregister);
45324353
....@@ -4541,28 +4362,58 @@
45414362 */
45424363 void devm_clk_hw_unregister(struct device *dev, struct clk_hw *hw)
45434364 {
4544
- WARN_ON(devres_release(dev, devm_clk_hw_release, devm_clk_hw_match,
4365
+ WARN_ON(devres_release(dev, devm_clk_hw_unregister_cb, devm_clk_hw_match,
45454366 hw));
45464367 }
45474368 EXPORT_SYMBOL_GPL(devm_clk_hw_unregister);
45484369
4370
+static void devm_clk_release(struct device *dev, void *res)
4371
+{
4372
+ clk_put(*(struct clk **)res);
4373
+}
4374
+
4375
+/**
4376
+ * devm_clk_hw_get_clk - resource managed clk_hw_get_clk()
4377
+ * @dev: device that is registering this clock
4378
+ * @hw: clk_hw associated with the clk being consumed
4379
+ * @con_id: connection ID string on device
4380
+ *
4381
+ * Managed clk_hw_get_clk(). Clocks got with this function are
4382
+ * automatically clk_put() on driver detach. See clk_put()
4383
+ * for more information.
4384
+ */
4385
+struct clk *devm_clk_hw_get_clk(struct device *dev, struct clk_hw *hw,
4386
+ const char *con_id)
4387
+{
4388
+ struct clk *clk;
4389
+ struct clk **clkp;
4390
+
4391
+ /* This should not happen because it would mean we have drivers
4392
+ * passing around clk_hw pointers instead of having the caller use
4393
+ * proper clk_get() style APIs
4394
+ */
4395
+ WARN_ON_ONCE(dev != hw->core->dev);
4396
+
4397
+ clkp = devres_alloc(devm_clk_release, sizeof(*clkp), GFP_KERNEL);
4398
+ if (!clkp)
4399
+ return ERR_PTR(-ENOMEM);
4400
+
4401
+ clk = clk_hw_get_clk(hw, con_id);
4402
+ if (!IS_ERR(clk)) {
4403
+ *clkp = clk;
4404
+ devres_add(dev, clkp);
4405
+ } else {
4406
+ devres_free(clkp);
4407
+ }
4408
+
4409
+ return clk;
4410
+}
4411
+EXPORT_SYMBOL_GPL(devm_clk_hw_get_clk);
4412
+
45494413 /*
45504414 * clkdev helpers
45514415 */
4552
-int __clk_get(struct clk *clk)
4553
-{
4554
- struct clk_core *core = !clk ? NULL : clk->core;
45554416
4556
- if (core) {
4557
- if (!try_module_get(core->owner))
4558
- return 0;
4559
-
4560
- kref_get(&core->ref);
4561
- }
4562
- return 1;
4563
-}
4564
-
4565
-/* keep in sync with __clk_free_clk */
45664417 void __clk_put(struct clk *clk)
45674418 {
45684419 struct module *owner;
....@@ -4596,8 +4447,7 @@
45964447
45974448 module_put(owner);
45984449
4599
- kfree_const(clk->con_id);
4600
- kfree(clk);
4450
+ free_clk(clk);
46014451 }
46024452
46034453 /*** clk rate change notifiers ***/
....@@ -4702,13 +4552,58 @@
47024552 }
47034553 EXPORT_SYMBOL_GPL(clk_notifier_unregister);
47044554
4555
+struct clk_notifier_devres {
4556
+ struct clk *clk;
4557
+ struct notifier_block *nb;
4558
+};
4559
+
4560
+static void devm_clk_notifier_release(struct device *dev, void *res)
4561
+{
4562
+ struct clk_notifier_devres *devres = res;
4563
+
4564
+ clk_notifier_unregister(devres->clk, devres->nb);
4565
+}
4566
+
4567
+int devm_clk_notifier_register(struct device *dev, struct clk *clk,
4568
+ struct notifier_block *nb)
4569
+{
4570
+ struct clk_notifier_devres *devres;
4571
+ int ret;
4572
+
4573
+ devres = devres_alloc(devm_clk_notifier_release,
4574
+ sizeof(*devres), GFP_KERNEL);
4575
+
4576
+ if (!devres)
4577
+ return -ENOMEM;
4578
+
4579
+ ret = clk_notifier_register(clk, nb);
4580
+ if (!ret) {
4581
+ devres->clk = clk;
4582
+ devres->nb = nb;
4583
+ } else {
4584
+ devres_free(devres);
4585
+ }
4586
+
4587
+ return ret;
4588
+}
4589
+EXPORT_SYMBOL_GPL(devm_clk_notifier_register);
4590
+
47054591 #ifdef CONFIG_OF
4592
+static void clk_core_reparent_orphans(void)
4593
+{
4594
+ clk_prepare_lock();
4595
+ clk_core_reparent_orphans_nolock();
4596
+ clk_prepare_unlock();
4597
+}
4598
+
47064599 /**
47074600 * struct of_clk_provider - Clock provider registration structure
47084601 * @link: Entry in global list of clock providers
47094602 * @node: Pointer to device tree node of clock provider
47104603 * @get: Get clock callback. Returns NULL or a struct clk for the
47114604 * given clock specifier
4605
+ * @get_hw: Get clk_hw callback. Returns NULL, ERR_PTR or a
4606
+ * struct clk_hw for the given clock specifier
47124607 * @data: context pointer to be passed into @get callback
47134608 */
47144609 struct of_clk_provider {
....@@ -4720,8 +4615,9 @@
47204615 void *data;
47214616 };
47224617
4618
+extern struct of_device_id __clk_of_table;
47234619 static const struct of_device_id __clk_of_table_sentinel
4724
- __used __section(__clk_of_table_end);
4620
+ __used __section("__clk_of_table_end");
47254621
47264622 static LIST_HEAD(of_clk_providers);
47274623 static DEFINE_MUTEX(of_clk_mutex);
....@@ -4773,6 +4669,8 @@
47734669 * @np: Device node pointer associated with clock provider
47744670 * @clk_src_get: callback for decoding clock
47754671 * @data: context pointer for @clk_src_get callback.
4672
+ *
4673
+ * This function is *deprecated*. Use of_clk_add_hw_provider() instead.
47764674 */
47774675 int of_clk_add_provider(struct device_node *np,
47784676 struct clk *(*clk_src_get)(struct of_phandle_args *clkspec,
....@@ -4781,6 +4679,9 @@
47814679 {
47824680 struct of_clk_provider *cp;
47834681 int ret;
4682
+
4683
+ if (!np)
4684
+ return 0;
47844685
47854686 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
47864687 if (!cp)
....@@ -4795,9 +4696,13 @@
47954696 mutex_unlock(&of_clk_mutex);
47964697 pr_debug("Added clock from %pOF\n", np);
47974698
4699
+ clk_core_reparent_orphans();
4700
+
47984701 ret = of_clk_set_defaults(np, true);
47994702 if (ret < 0)
48004703 of_clk_del_provider(np);
4704
+
4705
+ fwnode_dev_initialized(&np->fwnode, true);
48014706
48024707 return ret;
48034708 }
....@@ -4817,6 +4722,9 @@
48174722 struct of_clk_provider *cp;
48184723 int ret;
48194724
4725
+ if (!np)
4726
+ return 0;
4727
+
48204728 cp = kzalloc(sizeof(*cp), GFP_KERNEL);
48214729 if (!cp)
48224730 return -ENOMEM;
....@@ -4829,6 +4737,8 @@
48294737 list_add(&cp->link, &of_clk_providers);
48304738 mutex_unlock(&of_clk_mutex);
48314739 pr_debug("Added clk_hw provider from %pOF\n", np);
4740
+
4741
+ clk_core_reparent_orphans();
48324742
48334743 ret = of_clk_set_defaults(np, true);
48344744 if (ret < 0)
....@@ -4843,6 +4753,39 @@
48434753 of_clk_del_provider(*(struct device_node **)res);
48444754 }
48454755
4756
+/*
4757
+ * We allow a child device to use its parent device as the clock provider node
4758
+ * for cases like MFD sub-devices where the child device driver wants to use
4759
+ * devm_*() APIs but not list the device in DT as a sub-node.
4760
+ */
4761
+static struct device_node *get_clk_provider_node(struct device *dev)
4762
+{
4763
+ struct device_node *np, *parent_np;
4764
+
4765
+ np = dev->of_node;
4766
+ parent_np = dev->parent ? dev->parent->of_node : NULL;
4767
+
4768
+ if (!of_find_property(np, "#clock-cells", NULL))
4769
+ if (of_find_property(parent_np, "#clock-cells", NULL))
4770
+ np = parent_np;
4771
+
4772
+ return np;
4773
+}
4774
+
4775
+/**
4776
+ * devm_of_clk_add_hw_provider() - Managed clk provider node registration
4777
+ * @dev: Device acting as the clock provider (used for DT node and lifetime)
4778
+ * @get: callback for decoding clk_hw
4779
+ * @data: context pointer for @get callback
4780
+ *
4781
+ * Registers clock provider for given device's node. If the device has no DT
4782
+ * node or if the device node lacks of clock provider information (#clock-cells)
4783
+ * then the parent device's node is scanned for this information. If parent node
4784
+ * has the #clock-cells then it is used in registration. Provider is
4785
+ * automatically released at device exit.
4786
+ *
4787
+ * Return: 0 on success or an errno on failure.
4788
+ */
48464789 int devm_of_clk_add_hw_provider(struct device *dev,
48474790 struct clk_hw *(*get)(struct of_phandle_args *clkspec,
48484791 void *data),
....@@ -4856,7 +4799,7 @@
48564799 if (!ptr)
48574800 return -ENOMEM;
48584801
4859
- np = dev->of_node;
4802
+ np = get_clk_provider_node(dev);
48604803 ret = of_clk_add_hw_provider(np, get, data);
48614804 if (!ret) {
48624805 *ptr = np;
....@@ -4877,10 +4820,14 @@
48774820 {
48784821 struct of_clk_provider *cp;
48794822
4823
+ if (!np)
4824
+ return;
4825
+
48804826 mutex_lock(&of_clk_mutex);
48814827 list_for_each_entry(cp, &of_clk_providers, link) {
48824828 if (cp->node == np) {
48834829 list_del(&cp->link);
4830
+ fwnode_dev_initialized(&np->fwnode, false);
48844831 of_node_put(cp->node);
48854832 kfree(cp);
48864833 break;
....@@ -4900,16 +4847,95 @@
49004847 return *np == data;
49014848 }
49024849
4850
+/**
4851
+ * devm_of_clk_del_provider() - Remove clock provider registered using devm
4852
+ * @dev: Device to whose lifetime the clock provider was bound
4853
+ */
49034854 void devm_of_clk_del_provider(struct device *dev)
49044855 {
49054856 int ret;
4857
+ struct device_node *np = get_clk_provider_node(dev);
49064858
49074859 ret = devres_release(dev, devm_of_clk_release_provider,
4908
- devm_clk_provider_match, dev->of_node);
4860
+ devm_clk_provider_match, np);
49094861
49104862 WARN_ON(ret);
49114863 }
49124864 EXPORT_SYMBOL(devm_of_clk_del_provider);
4865
+
4866
+/**
4867
+ * of_parse_clkspec() - Parse a DT clock specifier for a given device node
4868
+ * @np: device node to parse clock specifier from
4869
+ * @index: index of phandle to parse clock out of. If index < 0, @name is used
4870
+ * @name: clock name to find and parse. If name is NULL, the index is used
4871
+ * @out_args: Result of parsing the clock specifier
4872
+ *
4873
+ * Parses a device node's "clocks" and "clock-names" properties to find the
4874
+ * phandle and cells for the index or name that is desired. The resulting clock
4875
+ * specifier is placed into @out_args, or an errno is returned when there's a
4876
+ * parsing error. The @index argument is ignored if @name is non-NULL.
4877
+ *
4878
+ * Example:
4879
+ *
4880
+ * phandle1: clock-controller@1 {
4881
+ * #clock-cells = <2>;
4882
+ * }
4883
+ *
4884
+ * phandle2: clock-controller@2 {
4885
+ * #clock-cells = <1>;
4886
+ * }
4887
+ *
4888
+ * clock-consumer@3 {
4889
+ * clocks = <&phandle1 1 2 &phandle2 3>;
4890
+ * clock-names = "name1", "name2";
4891
+ * }
4892
+ *
4893
+ * To get a device_node for `clock-controller@2' node you may call this
4894
+ * function a few different ways:
4895
+ *
4896
+ * of_parse_clkspec(clock-consumer@3, -1, "name2", &args);
4897
+ * of_parse_clkspec(clock-consumer@3, 1, NULL, &args);
4898
+ * of_parse_clkspec(clock-consumer@3, 1, "name2", &args);
4899
+ *
4900
+ * Return: 0 upon successfully parsing the clock specifier. Otherwise, -ENOENT
4901
+ * if @name is NULL or -EINVAL if @name is non-NULL and it can't be found in
4902
+ * the "clock-names" property of @np.
4903
+ */
4904
+static int of_parse_clkspec(const struct device_node *np, int index,
4905
+ const char *name, struct of_phandle_args *out_args)
4906
+{
4907
+ int ret = -ENOENT;
4908
+
4909
+ /* Walk up the tree of devices looking for a clock property that matches */
4910
+ while (np) {
4911
+ /*
4912
+ * For named clocks, first look up the name in the
4913
+ * "clock-names" property. If it cannot be found, then index
4914
+ * will be an error code and of_parse_phandle_with_args() will
4915
+ * return -EINVAL.
4916
+ */
4917
+ if (name)
4918
+ index = of_property_match_string(np, "clock-names", name);
4919
+ ret = of_parse_phandle_with_args(np, "clocks", "#clock-cells",
4920
+ index, out_args);
4921
+ if (!ret)
4922
+ break;
4923
+ if (name && index >= 0)
4924
+ break;
4925
+
4926
+ /*
4927
+ * No matching clock found on this node. If the parent node
4928
+ * has a "clock-ranges" property, then we can try one of its
4929
+ * clocks.
4930
+ */
4931
+ np = np->parent;
4932
+ if (np && !of_get_property(np, "clock-ranges", NULL))
4933
+ break;
4934
+ index = 0;
4935
+ }
4936
+
4937
+ return ret;
4938
+}
49134939
49144940 static struct clk_hw *
49154941 __of_clk_get_hw_from_provider(struct of_clk_provider *provider,
....@@ -4926,38 +4952,26 @@
49264952 return __clk_get_hw(clk);
49274953 }
49284954
4929
-struct clk *__of_clk_get_from_provider(struct of_phandle_args *clkspec,
4930
- const char *dev_id, const char *con_id,
4931
- bool with_orphans)
4955
+static struct clk_hw *
4956
+of_clk_get_hw_from_clkspec(struct of_phandle_args *clkspec)
49324957 {
49334958 struct of_clk_provider *provider;
4934
- struct clk *clk = ERR_PTR(-EPROBE_DEFER);
4935
- struct clk_hw *hw;
4959
+ struct clk_hw *hw = ERR_PTR(-EPROBE_DEFER);
49364960
49374961 if (!clkspec)
49384962 return ERR_PTR(-EINVAL);
49394963
4940
- /* Check if we have such a provider in our array */
49414964 mutex_lock(&of_clk_mutex);
49424965 list_for_each_entry(provider, &of_clk_providers, link) {
49434966 if (provider->node == clkspec->np) {
49444967 hw = __of_clk_get_hw_from_provider(provider, clkspec);
4945
- clk = __clk_create_clk(hw, dev_id, con_id,
4946
- with_orphans);
4947
- }
4948
-
4949
- if (!IS_ERR(clk)) {
4950
- if (!__clk_get(clk)) {
4951
- __clk_free_clk(clk);
4952
- clk = ERR_PTR(-ENOENT);
4953
- }
4954
-
4955
- break;
4968
+ if (!IS_ERR(hw))
4969
+ break;
49564970 }
49574971 }
49584972 mutex_unlock(&of_clk_mutex);
49594973
4960
- return clk;
4974
+ return hw;
49614975 }
49624976
49634977 /**
....@@ -4970,27 +4984,61 @@
49704984 */
49714985 struct clk *of_clk_get_from_provider(struct of_phandle_args *clkspec)
49724986 {
4973
- return __of_clk_get_from_provider(clkspec, NULL, __func__, false);
4974
-}
4987
+ struct clk_hw *hw = of_clk_get_hw_from_clkspec(clkspec);
49754988
4976
-/**
4977
- * of_clk_get_from_provider_with_orphans() - Lookup clock from a clock provider
4978
- * @clkspec: pointer to a clock specifier data structure
4979
- *
4980
- * This function looks up a struct clk from the registered list of clock
4981
- * providers, an input is a clock specifier data structure as returned
4982
- * from the of_parse_phandle_with_args() function call.
4983
- *
4984
- * The difference to of_clk_get_from_provider() is that this function will
4985
- * also successfully lookup orphan-clocks, as it in some cases may be
4986
- * necessary to access such orphan-clocks as well.
4987
- */
4988
-struct clk *
4989
-of_clk_get_from_provider_with_orphans(struct of_phandle_args *clkspec)
4990
-{
4991
- return __of_clk_get_from_provider(clkspec, NULL, __func__, true);
4989
+ return clk_hw_create_clk(NULL, hw, NULL, __func__);
49924990 }
49934991 EXPORT_SYMBOL_GPL(of_clk_get_from_provider);
4992
+
4993
+struct clk_hw *of_clk_get_hw(struct device_node *np, int index,
4994
+ const char *con_id)
4995
+{
4996
+ int ret;
4997
+ struct clk_hw *hw;
4998
+ struct of_phandle_args clkspec;
4999
+
5000
+ ret = of_parse_clkspec(np, index, con_id, &clkspec);
5001
+ if (ret)
5002
+ return ERR_PTR(ret);
5003
+
5004
+ hw = of_clk_get_hw_from_clkspec(&clkspec);
5005
+ of_node_put(clkspec.np);
5006
+
5007
+ return hw;
5008
+}
5009
+
5010
+static struct clk *__of_clk_get(struct device_node *np,
5011
+ int index, const char *dev_id,
5012
+ const char *con_id)
5013
+{
5014
+ struct clk_hw *hw = of_clk_get_hw(np, index, con_id);
5015
+
5016
+ return clk_hw_create_clk(NULL, hw, dev_id, con_id);
5017
+}
5018
+
5019
+struct clk *of_clk_get(struct device_node *np, int index)
5020
+{
5021
+ return __of_clk_get(np, index, np->full_name, NULL);
5022
+}
5023
+EXPORT_SYMBOL(of_clk_get);
5024
+
5025
+/**
5026
+ * of_clk_get_by_name() - Parse and lookup a clock referenced by a device node
5027
+ * @np: pointer to clock consumer node
5028
+ * @name: name of consumer's clock input, or NULL for the first clock reference
5029
+ *
5030
+ * This function parses the clocks and clock-names properties,
5031
+ * and uses them to look up the struct clk from the registered list of clock
5032
+ * providers.
5033
+ */
5034
+struct clk *of_clk_get_by_name(struct device_node *np, const char *name)
5035
+{
5036
+ if (!np)
5037
+ return ERR_PTR(-ENOENT);
5038
+
5039
+ return __of_clk_get(np, 0, np->full_name, name);
5040
+}
5041
+EXPORT_SYMBOL(of_clk_get_by_name);
49945042
49955043 /**
49965044 * of_clk_get_parent_count() - Count the number of clocks a device node has
....@@ -4998,7 +5046,7 @@
49985046 *
49995047 * Returns: The number of clocks that are possible parents of this node
50005048 */
5001
-unsigned int of_clk_get_parent_count(struct device_node *np)
5049
+unsigned int of_clk_get_parent_count(const struct device_node *np)
50025050 {
50035051 int count;
50045052
....@@ -5010,7 +5058,7 @@
50105058 }
50115059 EXPORT_SYMBOL_GPL(of_clk_get_parent_count);
50125060
5013
-const char *of_clk_get_parent_name(struct device_node *np, int index)
5061
+const char *of_clk_get_parent_name(const struct device_node *np, int index)
50145062 {
50155063 struct of_phandle_args clkspec;
50165064 struct property *prop;
....@@ -5150,8 +5198,8 @@
51505198 *
51515199 * Return: error code or zero on success
51525200 */
5153
-int of_clk_detect_critical(struct device_node *np,
5154
- int index, unsigned long *flags)
5201
+int of_clk_detect_critical(struct device_node *np, int index,
5202
+ unsigned long *flags)
51555203 {
51565204 struct property *prop;
51575205 const __be32 *cur;
....@@ -5296,12 +5344,12 @@
52965344 return cnt;
52975345 }
52985346
5299
-static const struct file_operations clk_rate_fops = {
5300
- .open = clk_rate_open,
5301
- .read = seq_read,
5302
- .llseek = seq_lseek,
5303
- .release = single_release,
5304
- .write = clk_rate_write,
5347
+static const struct proc_ops clk_rate_proc_ops = {
5348
+ .proc_open = clk_rate_open,
5349
+ .proc_read = seq_read,
5350
+ .proc_write = clk_rate_write,
5351
+ .proc_lseek = seq_lseek,
5352
+ .proc_release = single_release,
53055353 };
53065354
53075355 static int clk_enable_show(struct seq_file *s, void *v)
....@@ -5357,12 +5405,12 @@
53575405 return cnt;
53585406 }
53595407
5360
-static const struct file_operations clk_enable_fops = {
5361
- .open = clk_enable_open,
5362
- .read = seq_read,
5363
- .llseek = seq_lseek,
5364
- .release = single_release,
5365
- .write = clk_enable_write,
5408
+static const struct proc_ops clk_enable_proc_ops = {
5409
+ .proc_open = clk_enable_open,
5410
+ .proc_read = seq_read,
5411
+ .proc_write = clk_enable_write,
5412
+ .proc_lseek = seq_lseek,
5413
+ .proc_release = single_release,
53665414 };
53675415
53685416 static int clk_parent_show(struct seq_file *s, void *v)
....@@ -5416,12 +5464,12 @@
54165464 return cnt;
54175465 }
54185466
5419
-static const struct file_operations clk_parent_fops = {
5420
- .open = clk_parent_open,
5421
- .read = seq_read,
5422
- .llseek = seq_lseek,
5423
- .release = single_release,
5424
- .write = clk_parent_write,
5467
+static const struct proc_ops clk_parent_proc_ops = {
5468
+ .proc_open = clk_parent_open,
5469
+ .proc_read = seq_read,
5470
+ .proc_write = clk_parent_write,
5471
+ .proc_lseek = seq_lseek,
5472
+ .proc_release = single_release,
54255473 };
54265474
54275475 static void clk_proc_summary_show_one(struct seq_file *s, struct clk_core *c,
....@@ -5434,7 +5482,8 @@
54345482 level * 3 + 1, "",
54355483 30 - level * 3, c->name,
54365484 c->enable_count, c->prepare_count, c->protect_count,
5437
- clk_core_get_rate(c), clk_core_get_accuracy(c),
5485
+ clk_core_get_rate_recalc(c),
5486
+ clk_core_get_accuracy_recalc(c),
54385487 clk_core_get_phase(c),
54395488 clk_core_get_scaled_duty_cycle(c, 100000));
54405489 }
....@@ -5487,15 +5536,15 @@
54875536 if (!proc_clk_root)
54885537 return -EINVAL;
54895538
5490
- ent = proc_create("rate", 0644, proc_clk_root, &clk_rate_fops);
5539
+ ent = proc_create("rate", 0644, proc_clk_root, &clk_rate_proc_ops);
54915540 if (!ent)
54925541 goto fail;
54935542
5494
- ent = proc_create("enable", 0644, proc_clk_root, &clk_enable_fops);
5543
+ ent = proc_create("enable", 0644, proc_clk_root, &clk_enable_proc_ops);
54955544 if (!ent)
54965545 goto fail;
54975546
5498
- ent = proc_create("parent", 0644, proc_clk_root, &clk_parent_fops);
5547
+ ent = proc_create("parent", 0644, proc_clk_root, &clk_parent_proc_ops);
54995548 if (!ent)
55005549 goto fail;
55015550