ramips: refresh patches
[oweals/openwrt.git] / target / linux / ipq806x / patches-4.19 / 0055-cpufreq-dt-Add-L2-frequency-scaling-support.patch
1 --- a/drivers/cpufreq/cpufreq-dt.c
2 +++ b/drivers/cpufreq/cpufreq-dt.c
3 @@ -48,16 +48,71 @@ static int set_target(struct cpufreq_pol
4  {
5         struct private_data *priv = policy->driver_data;
6         unsigned long freq = policy->freq_table[index].frequency;
7 +       struct clk *l2_clk = policy->l2_clk;
8 +       struct regulator *l2_regulator = policy->l2_regulator;
9 +       unsigned long l2_freq, target_l2_freq;
10 +       unsigned long l2_vol, target_l2_vol;
11 +       unsigned long target_freq;
12         int ret;
13  
14         mutex_lock(&priv->lock);
15         ret = dev_pm_opp_set_rate(priv->cpu_dev, freq * 1000);
16  
17         if (!ret) {
18 +               if (policy->l2_rate_set) {
19 +                       static unsigned long krait_l2[CONFIG_NR_CPUS] = { };
20 +                       int cpu, l2_index, tol = 0;
21 +
22 +                       target_freq = freq * 1000;
23 +
24 +                       krait_l2[policy->cpu] = target_freq;
25 +                       for_each_present_cpu(cpu)
26 +                               target_freq = max(target_freq, krait_l2[cpu]);
27 +
28 +                       for (l2_index = 2; l2_index >= 0; l2_index--)
29 +                               if (target_freq >= policy->l2_cpufreq[l2_index])
30 +                                       break;
31 +
32 +                       l2_freq = clk_get_rate(l2_clk);
33 +                       target_l2_freq = policy->l2_rate[l2_index];
34 +
35 +                       if (l2_freq != target_l2_freq) {
36 +
37 +                               /*
38 +                                * Set to idle bin if switching from normal to high bin 
39 +                                * or vice versa
40 +                                */
41 +                               if ( (l2_index == 2 && l2_freq == policy->l2_rate[1]) ||
42 +                                        (l2_index == 1 && l2_freq == policy->l2_rate[2]) ) {
43 +                                       ret = clk_set_rate(l2_clk, policy->l2_rate[0]);
44 +                                       if (ret)
45 +                                               goto exit;
46 +                               }
47 +                               /* scale l2 with the core */
48 +                               ret = clk_set_rate(l2_clk, target_l2_freq);
49 +                               if (ret)
50 +                                       goto exit;
51 +
52 +                               if (policy->l2_volt_set) {
53 +
54 +                                       l2_vol = regulator_get_voltage(l2_regulator);
55 +                                       target_l2_vol = policy->l2_volt[l2_index];
56 +
57 +                                       if (l2_vol != target_l2_vol) {
58 +                                               tol = target_l2_vol * policy->l2_volt_tol / 100;
59 +                                               ret = regulator_set_voltage_tol(l2_regulator,target_l2_vol,tol);
60 +                                               if (ret)
61 +                                                       goto exit;
62 +                                       }
63 +                               }
64 +                       }
65 +               }
66                 priv->opp_freq = freq * 1000;
67                 arch_set_freq_scale(policy->related_cpus, freq,
68                                     policy->cpuinfo.max_freq);
69         }
70 +
71 +exit:
72         mutex_unlock(&priv->lock);
73  
74         return ret;
75 @@ -200,6 +255,9 @@ static int cpufreq_init(struct cpufreq_p
76         bool fallback = false;
77         const char *name;
78         int ret;
79 +       struct device_node *np, *l2_np;
80 +       struct clk *l2_clk = NULL;
81 +       struct regulator *l2_regulator = NULL;
82  
83         cpu_dev = get_cpu_device(policy->cpu);
84         if (!cpu_dev) {
85 @@ -307,6 +365,57 @@ static int cpufreq_init(struct cpufreq_p
86  
87         policy->suspend_freq = dev_pm_opp_get_suspend_opp_freq(cpu_dev) / 1000;
88  
89 +       policy->l2_rate_set = false;
90 +       policy->l2_volt_set = false;
91 +
92 +       l2_clk = clk_get(cpu_dev, "l2");
93 +       if (!IS_ERR(l2_clk))
94 +               policy->l2_clk = l2_clk;
95 +
96 +       l2_np = of_find_node_by_name(NULL, "qcom,l2");
97 +       if (l2_np) {
98 +               struct device_node *vdd;
99 +               np = of_node_get(priv->cpu_dev->of_node);
100 +
101 +               if (np)
102 +                       of_property_read_u32(np, "voltage-tolerance", &policy->l2_volt_tol);
103 +
104 +               of_property_read_u32_array(l2_np, "qcom,l2-rates", policy->l2_rate, 3);
105 +               if (policy->l2_rate[0] && policy->l2_rate[1] && policy->l2_rate[2]) {
106 +                       policy->l2_rate_set = true;
107 +                       of_property_read_u32_array(l2_np, "qcom,l2-cpufreq", policy->l2_cpufreq, 3);
108 +                       of_property_read_u32_array(l2_np, "qcom,l2-volt", policy->l2_volt, 3);
109 +               } else
110 +                       pr_warn("L2: failed to parse L2 rates\n");
111 +
112 +               if (!policy->l2_cpufreq[0] && !policy->l2_cpufreq[1] && 
113 +                       !policy->l2_cpufreq[2] && policy->l2_rate_set) {
114 +                       int i;
115 +
116 +                       pr_warn("L2: failed to parse target cpu freq, using defaults\n");
117 +                       for (i = 0; i < 3; i++)
118 +                               policy->l2_cpufreq[i] = policy->l2_rate[i];
119 +               }
120 +
121 +               if (policy->l2_volt[0] && policy->l2_volt[1] && policy->l2_volt[2] &&
122 +                       policy->l2_volt_tol && policy->l2_rate_set) {
123 +                       vdd = of_parse_phandle(l2_np, "qcom,l2-supply", 0);
124 +
125 +                       if (vdd) {
126 +                               l2_regulator = devm_regulator_get(cpu_dev, vdd->name);
127 +                               if (!IS_ERR(l2_regulator)) {
128 +                                       policy->l2_regulator = l2_regulator;
129 +                                       policy->l2_volt_set = true;
130 +                               } else {
131 +                                       pr_warn("failed to get l2 supply\n");
132 +                                       l2_regulator = NULL;
133 +                               }
134 +
135 +                               of_node_put(vdd);
136 +                       }
137 +               }
138 +       }
139 +
140         /* Support turbo/boost mode */
141         if (policy_has_boost_freq(policy)) {
142                 /* This gets disabled by core on driver unregister */
143 --- a/include/linux/cpufreq.h
144 +++ b/include/linux/cpufreq.h
145 @@ -72,7 +72,15 @@ struct cpufreq_policy {
146                                                 should set cpufreq */
147         unsigned int            cpu;    /* cpu managing this policy, must be online */
148  
149 -       struct clk              *clk;
150 +       struct clk                      *clk;
151 +       struct clk                      *l2_clk; /* L2 clock */
152 +       struct regulator        *l2_regulator; /* L2 supply */
153 +       unsigned int            l2_rate[3]; /* L2 bus clock rate */
154 +       bool                            l2_rate_set;
155 +       unsigned int            l2_cpufreq[3]; /* L2 target CPU frequency */
156 +       unsigned int            l2_volt[3]; /* L2 voltage array */
157 +       bool                            l2_volt_set;
158 +       unsigned int            l2_volt_tol; /* L2 voltage tolerance */
159         struct cpufreq_cpuinfo  cpuinfo;/* see above */
160  
161         unsigned int            min;    /* in kHz */