]> nv-tegra.nvidia Code Review - linux-2.6.git/blob - arch/arm/mach-tegra/tegra11_dvfs.c
ARM: tegra11: dvfs: Updated dvfs tables of c-bus modules
[linux-2.6.git] / arch / arm / mach-tegra / tegra11_dvfs.c
1 /*
2  * arch/arm/mach-tegra/tegra11_dvfs.c
3  *
4  * Copyright (C) 2012 NVIDIA Corporation.
5  *
6  * This software is licensed under the terms of the GNU General Public
7  * License version 2, as published by the Free Software Foundation, and
8  * may be copied, distributed, and modified under those terms.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  */
16
17 #include <linux/kernel.h>
18 #include <linux/init.h>
19 #include <linux/string.h>
20 #include <linux/module.h>
21 #include <linux/clk.h>
22 #include <linux/kobject.h>
23 #include <linux/err.h>
24
25 #include "clock.h"
26 #include "dvfs.h"
27 #include "fuse.h"
28 #include "board.h"
29 #include "tegra_cl_dvfs.h"
30
31 static bool tegra_dvfs_cpu_disabled;
32 static bool tegra_dvfs_core_disabled;
33
34 #define KHZ 1000
35 #define MHZ 1000000
36
37 /* FIXME: need tegra11 step */
38 #define VDD_SAFE_STEP                   100
39
40 static struct dvfs_rail tegra11_dvfs_rail_vdd_cpu = {
41         .reg_id = "vdd_cpu",
42         .max_millivolts = 1400,
43         .min_millivolts = 800,
44         .step = VDD_SAFE_STEP,
45         .jmp_to_zero = true,
46 };
47
48 static struct dvfs_rail tegra11_dvfs_rail_vdd_core = {
49         .reg_id = "vdd_core",
50         .max_millivolts = 1400,
51         .min_millivolts = 800,
52         .step = VDD_SAFE_STEP,
53 };
54
55 static struct dvfs_rail *tegra11_dvfs_rails[] = {
56         &tegra11_dvfs_rail_vdd_cpu,
57         &tegra11_dvfs_rail_vdd_core,
58 };
59
60 /* default cvb alignment on Tegra11 - 10mV */
61 int __attribute__((weak)) tegra_get_cvb_alignment_uV(void)
62 {
63         return 10000;
64 }
65
66 /* CPU DVFS tables */
67 /* FIXME: real data */
68 static struct cpu_cvb_dvfs cpu_cvb_dvfs_table[] = {
69         {
70                 .speedo_id = 0,
71                 .max_mv = 1250,
72                 .min_dfll_mv = 1000,
73                 .freqs_mult = MHZ,
74                 .speedo_scale = 100,
75                 .voltage_scale = 100,
76                 .cvb_table = {
77                         /*f    dfll: c0,     c1,   c2  pll:  c0,   c1,    c2 */
78                         { 306, { 107330,  -1569,   0}, {  90000,    0,    0} },
79                         { 408, { 111250,  -1666,   0}, {  90000,    0,    0} },
80                         { 510, { 110000,  -1460,   0}, {  94000,    0,    0} },
81                         { 612, { 117290,  -1745,   0}, {  94000,    0,    0} },
82                         { 714, { 122700,  -1910,   0}, {  99000,    0,    0} },
83                         { 816, { 125620,  -1945,   0}, {  99000,    0,    0} },
84                         { 918, { 130560,  -2076,   0}, { 103000,    0,    0} },
85                         {1020, { 137280,  -2303,   0}, { 103000,    0,    0} },
86                         {1122, { 146440,  -2660,   0}, { 109000,    0,    0} },
87                         {1224, { 152190,  -2825,   0}, { 109000,    0,    0} },
88                         {1326, { 157520,  -2953,   0}, { 112000,    0,    0} },
89                         {1428, { 166100,  -3261,   0}, { 140000,    0,    0} },
90                         {1530, { 176410,  -3647,   0}, { 140000,    0,    0} },
91                         {1632, { 189620,  -4186,   0}, { 140000,    0,    0} },
92                         {1734, { 203190,  -4725,   0}, { 140000,    0,    0} },
93                         {1836, { 222670,  -5573,   0}, { 140000,    0,    0} },
94                         {1938, { 256210,  -7165,   0}, { 140000,    0,    0} },
95                         {2040, { 250050,  -6544,   0}, { 140000,    0,    0} },
96                         {   0, {      0,      0,   0}, {      0,    0,    0} },
97                 },
98         }
99 };
100
101 static int cpu_millivolts[MAX_DVFS_FREQS];
102 static int cpu_dfll_millivolts[MAX_DVFS_FREQS];
103
104 static struct dvfs cpu_dvfs = {
105         .clk_name       = "cpu_g",
106         .process_id     = -1,
107         .millivolts     = cpu_millivolts,
108         .dfll_millivolts = cpu_dfll_millivolts,
109         .auto_dvfs      = true,
110         .dvfs_rail      = &tegra11_dvfs_rail_vdd_cpu,
111         .dfll_data      = {
112                 .tune0          = 0x00b0019d,
113                 .tune1          = 0x0000001f,
114                 .droop_rate_min = 1000000,
115         },
116 };
117
118 /* Core DVFS tables */
119 /* FIXME: real data */
120 static const int core_millivolts[MAX_DVFS_FREQS] = {
121         1000, 1050, 1100, 1120};
122
123 #define CORE_DVFS(_clk_name, _speedo_id, _auto, _mult, _freqs...)       \
124         {                                                       \
125                 .clk_name       = _clk_name,                    \
126                 .speedo_id      = _speedo_id,                   \
127                 .process_id     = -1,                           \
128                 .freqs          = {_freqs},                     \
129                 .freqs_mult     = _mult,                        \
130                 .millivolts     = core_millivolts,              \
131                 .auto_dvfs      = _auto,                        \
132                 .dvfs_rail      = &tegra11_dvfs_rail_vdd_core,  \
133         }
134
135 static struct dvfs core_dvfs_table[] = {
136         /* Core voltages (mV):                  1000,  1050,     1100,    1120, */
137         /* Clock limits for internal blocks, PLLs */
138 #ifndef CONFIG_TEGRA_SIMULATION_PLATFORM
139         CORE_DVFS("cpu_lp", -1, 1, KHZ,       288000, 372000,  468000,  468000),
140         CORE_DVFS("emc",   -1, 1, KHZ,        384000, 528000,  800000,  800000),
141         CORE_DVFS("sbus",  -1, 1, KHZ,        136000, 204000,  204000,  204000),
142
143         CORE_DVFS("vi",    -1, 1, KHZ,        144000, 192000,  240000,  240000),
144
145         CORE_DVFS("2d",    -1, 1, KHZ,        276000, 372000,  456000,  480000),
146         CORE_DVFS("3d",    -1, 1, KHZ,        276000, 372000,  456000,  480000),
147         CORE_DVFS("epp",   -1, 1, KHZ,        276000, 372000,  456000,  480000),
148
149         CORE_DVFS("msenc", -1, 1, KHZ,        228000, 288000,  360000,  384000),
150         CORE_DVFS("se",    -1, 1, KHZ,        228000, 288000,  360000,  384000),
151         CORE_DVFS("tsec",  -1, 1, KHZ,        228000, 288000,  360000,  384000),
152         CORE_DVFS("vde",   -1, 1, KHZ,        228000, 288000,  360000,  384000),
153
154         CORE_DVFS("host1x", -1, 1, KHZ,       136000, 163200,  204000,  204000),
155
156 #ifdef CONFIG_TEGRA_DUAL_CBUS
157         CORE_DVFS("c2bus", -1, 1, KHZ,        276000, 372000,  456000,  480000),
158         CORE_DVFS("c3bus", -1, 1, KHZ,        228000, 288000,  360000,  384000),
159 #else
160         CORE_DVFS("cbus",  -1, 1, KHZ,        228000, 288000,  360000,  384000),
161 #endif
162
163         CORE_DVFS("pll_m", -1, 1, KHZ,        660000, 792000,  936000,  936000),
164         CORE_DVFS("pll_c", -1, 1, KHZ,        660000, 792000,  936000,  936000),
165         CORE_DVFS("pll_c2", -1, 1, KHZ,       660000, 792000,  936000,  936000),
166         CORE_DVFS("pll_c3", -1, 1, KHZ,       660000, 792000,  936000,  936000),
167         CORE_DVFS("pll_d_out0", -1, 1, KHZ,   660000, 792000,  936000,  936000),
168         CORE_DVFS("pll_d2_out0", -1, 1, KHZ,  660000, 792000,  936000,  936000),
169         CORE_DVFS("pll_re_out", -1, 1, KHZ,   660000, 792000,  936000,  936000),
170
171         /* Core voltages (mV):                 1000,   1050,    1100,    1120, */
172         /* Clock limits for I/O peripherals */
173         CORE_DVFS("i2c1", -1, 1, KHZ,         81600,  102000,  136000,  136000),
174         CORE_DVFS("i2c2", -1, 1, KHZ,         81600,  102000,  136000,  136000),
175         CORE_DVFS("i2c3", -1, 1, KHZ,         81600,  102000,  136000,  136000),
176         CORE_DVFS("i2c4", -1, 1, KHZ,         81600,  102000,  136000,  136000),
177
178         CORE_DVFS("sbc1", -1, 1, KHZ,         48000,   48000,   48000,   48000),
179         CORE_DVFS("sbc2", -1, 1, KHZ,         48000,   48000,   48000,   48000),
180         CORE_DVFS("sbc3", -1, 1, KHZ,         48000,   48000,   48000,   48000),
181         CORE_DVFS("sbc4", -1, 1, KHZ,         48000,   48000,   48000,   48000),
182         CORE_DVFS("sbc5", -1, 1, KHZ,         48000,   48000,   48000,   48000),
183         CORE_DVFS("sbc6", -1, 1, KHZ,         48000,   48000,   48000,   48000),
184
185         /* FIXME: re-enable after UART hs driver is updated */
186 #if 0
187         CORE_DVFS("uarta", -1, 1, KHZ,        102000, 102000,  102000,  102000),
188         CORE_DVFS("uartb", -1, 1, KHZ,        102000, 102000,  102000,  102000),
189         CORE_DVFS("uartc", -1, 1, KHZ,        102000, 102000,  102000,  102000),
190         CORE_DVFS("uartd", -1, 1, KHZ,        102000, 102000,  102000,  102000),
191         CORE_DVFS("uarte", -1, 1, KHZ,        102000, 102000,  102000,  102000),
192 #endif
193         CORE_DVFS("sdmmc1", -1, 1, KHZ,       163200, 163200,  163200,  163200),
194         CORE_DVFS("sdmmc2", -1, 1, KHZ,       163200, 163200,  163200,  163200),
195         CORE_DVFS("sdmmc3", -1, 1, KHZ,       163200, 163200,  163200,  163200),
196         CORE_DVFS("sdmmc4", -1, 1, KHZ,       163200, 163200,  163200,  163200),
197
198         CORE_DVFS("pwm",  -1, 1, KHZ,          48000,  48000,   48000,   48000),
199
200         CORE_DVFS("csi",  -1, 1, KHZ,         102000, 102000,  102000,  102000),
201         CORE_DVFS("dsia", -1, 1, KHZ,         125000, 125000,  500000,  500000),
202         CORE_DVFS("dsib", -1, 1, KHZ,         125000, 125000,  500000,  500000),
203         CORE_DVFS("dsialp", -1, 1, KHZ,       102000, 102000,  156000,  156000),
204         CORE_DVFS("dsiblp", -1, 1, KHZ,       102000, 102000,  156000,  156000),
205         CORE_DVFS("hdmi", -1, 1, KHZ,         148500, 198000,  198000,  198000),
206
207         /*
208          * The clock rate for the display controllers that determines the
209          * necessary core voltage depends on a divider that is internal
210          * to the display block.  Disable auto-dvfs on the display clocks,
211          * and let the display driver call tegra_dvfs_set_rate manually
212          */
213         CORE_DVFS("disp1", -1, 0, KHZ,         144000, 192000,  240000,  240000),
214         CORE_DVFS("disp2", -1, 0, KHZ,         144000, 192000,  240000,  240000),
215
216         CORE_DVFS("xusb_falcon_src", -1, 1, KHZ, 204000, 336000,  336000,  336000),
217         CORE_DVFS("xusb_host_src",   -1, 1, KHZ,  58300, 112000,  112000,  112000),
218         CORE_DVFS("xusb_dev_src",    -1, 1, KHZ,  58300, 112000,  112000,  112000),
219         CORE_DVFS("xusb_ss_src",     -1, 1, KHZ,  60000, 120000,  120000,  120000),
220         CORE_DVFS("xusb_fs_src",     -1, 1, KHZ,  48000,  48000,   48000,   48000),
221         CORE_DVFS("xusb_hs_src",     -1, 1, KHZ,  60000,  60000,   60000,   60000),
222 #endif
223 };
224
225 int tegra_dvfs_disable_core_set(const char *arg, const struct kernel_param *kp)
226 {
227         int ret;
228
229         ret = param_set_bool(arg, kp);
230         if (ret)
231                 return ret;
232
233         if (tegra_dvfs_core_disabled)
234                 tegra_dvfs_rail_disable(&tegra11_dvfs_rail_vdd_core);
235         else
236                 tegra_dvfs_rail_enable(&tegra11_dvfs_rail_vdd_core);
237
238         return 0;
239 }
240
241 int tegra_dvfs_disable_cpu_set(const char *arg, const struct kernel_param *kp)
242 {
243         int ret;
244
245         ret = param_set_bool(arg, kp);
246         if (ret)
247                 return ret;
248
249         if (tegra_dvfs_cpu_disabled)
250                 tegra_dvfs_rail_disable(&tegra11_dvfs_rail_vdd_cpu);
251         else
252                 tegra_dvfs_rail_enable(&tegra11_dvfs_rail_vdd_cpu);
253
254         return 0;
255 }
256
257 int tegra_dvfs_disable_get(char *buffer, const struct kernel_param *kp)
258 {
259         return param_get_bool(buffer, kp);
260 }
261
262 static struct kernel_param_ops tegra_dvfs_disable_core_ops = {
263         .set = tegra_dvfs_disable_core_set,
264         .get = tegra_dvfs_disable_get,
265 };
266
267 static struct kernel_param_ops tegra_dvfs_disable_cpu_ops = {
268         .set = tegra_dvfs_disable_cpu_set,
269         .get = tegra_dvfs_disable_get,
270 };
271
272 module_param_cb(disable_core, &tegra_dvfs_disable_core_ops,
273         &tegra_dvfs_core_disabled, 0644);
274 module_param_cb(disable_cpu, &tegra_dvfs_disable_cpu_ops,
275         &tegra_dvfs_cpu_disabled, 0644);
276
277
278 static bool __init can_update_max_rate(struct clk *c, struct dvfs *d)
279 {
280         /* Don't update manual dvfs clocks */
281         if (!d->auto_dvfs)
282                 return false;
283
284         /*
285          * Don't update EMC shared bus, since EMC dvfs is board dependent: max
286          * rate and EMC scaling frequencies are determined by tegra BCT (flashed
287          * together with the image) and board specific EMC DFS table; we will
288          * check the scaling ladder against nominal core voltage when the table
289          * is loaded (and if on particular board the table is not loaded, EMC
290          * scaling is disabled).
291          */
292         if (c->ops->shared_bus_update && (c->flags & PERIPH_EMC_ENB))
293                 return false;
294
295         /*
296          * Don't update shared cbus, and don't propagate common cbus dvfs
297          * limit down to shared users, but set maximum rate for each user
298          * equal to the respective client limit.
299          */
300         if (c->ops->shared_bus_update && (c->flags & PERIPH_ON_CBUS)) {
301                 struct clk *user;
302                 unsigned long rate;
303
304                 list_for_each_entry(
305                         user, &c->shared_bus_list, u.shared_bus_user.node) {
306                         if (user->u.shared_bus_user.client) {
307                                 rate = user->u.shared_bus_user.client->max_rate;
308                                 user->max_rate = rate;
309                                 user->u.shared_bus_user.rate = rate;
310                         }
311                 }
312                 return false;
313         }
314
315         /* Other, than EMC and cbus, auto-dvfs clocks can be updated */
316         return true;
317 }
318
319 static void __init init_dvfs_one(struct dvfs *d, int max_freq_index)
320 {
321         int ret;
322         struct clk *c = tegra_get_clock_by_name(d->clk_name);
323
324         if (!c) {
325                 pr_debug("tegra11_dvfs: no clock found for %s\n",
326                         d->clk_name);
327                 return;
328         }
329
330         /* Update max rate for auto-dvfs clocks, with shared bus exceptions */
331         if (can_update_max_rate(c, d)) {
332                 BUG_ON(!d->freqs[max_freq_index]);
333                 tegra_init_max_rate(
334                         c, d->freqs[max_freq_index] * d->freqs_mult);
335         }
336         d->max_millivolts = d->dvfs_rail->nominal_millivolts;
337
338         ret = tegra_enable_dvfs_on_clk(c, d);
339         if (ret)
340                 pr_err("tegra11_dvfs: failed to enable dvfs on %s\n", c->name);
341 }
342
343 static bool __init match_dvfs_one(struct dvfs *d, int speedo_id, int process_id)
344 {
345         if ((d->process_id != -1 && d->process_id != process_id) ||
346                 (d->speedo_id != -1 && d->speedo_id != speedo_id)) {
347                 pr_debug("tegra11_dvfs: rejected %s speedo %d,"
348                         " process %d\n", d->clk_name, d->speedo_id,
349                         d->process_id);
350                 return false;
351         }
352         return true;
353 }
354
355
356 /* cvb_mv = ((c2 * speedo / s_scale + c1) * speedo / s_scale + c0) / v_scale */
357 static inline int get_cvb_voltage(int speedo, int s_scale,
358                                   struct cpu_cvb_dvfs_parameters *cvb)
359 {
360         /* apply only speedo scale: output mv = cvb_mv * v_scale */
361         int mv;
362         mv = DIV_ROUND_CLOSEST(cvb->c2 * speedo, s_scale);
363         mv = DIV_ROUND_CLOSEST((mv + cvb->c1) * speedo, s_scale) + cvb->c0;
364         return mv;
365 }
366
367 static inline int round_cvb_voltage(int mv, int v_scale)
368 {
369         /* combined: apply voltage scale and round to cvb alignment step */
370         int cvb_align_step_uv = tegra_get_cvb_alignment_uV();
371
372         return DIV_ROUND_UP(mv * 1000, v_scale * cvb_align_step_uv) *
373                 cvb_align_step_uv / 1000;
374 }
375
376 static int __init set_cpu_dvfs_data(int speedo_id, struct dvfs *cpu_dvfs,
377                                     int *max_freq_index)
378 {
379         int i, j, mv, dfll_mv;
380         unsigned long fmax_at_vmin = 0;
381         unsigned long fmax_pll_mode = 0;
382         unsigned long fmin_use_dfll = 0;
383         struct cpu_cvb_dvfs *d = NULL;
384         struct cpu_cvb_dvfs_table *table = NULL;
385         int speedo = tegra_cpu_speedo_value();
386
387         /* Find matching cvb dvfs entry */
388         for (i = 0; i < ARRAY_SIZE(cpu_cvb_dvfs_table); i++) {
389                 d = &cpu_cvb_dvfs_table[i];
390                 if (speedo_id == d->speedo_id)
391                         break;
392         }
393
394         if (!d) {
395                 pr_err("tegra11_dvfs: no cpu dvfs table for speedo_id %d\n",
396                        speedo_id);
397                 return -ENOENT;
398         }
399         BUG_ON(d->min_dfll_mv < tegra11_dvfs_rail_vdd_cpu.min_millivolts);
400
401         /*
402          * Use CVB table to fill in CPU dvfs frequencies and voltages. Each
403          * CVB entry specifies CPU frequency and CVB coefficients to calculate
404          * the respective voltage when either DFLL or PLL is used as CPU clock
405          * source.
406          *
407          * Minimum voltage limit is applied only to DFLL source. For PLL source
408          * voltage can go as low as table specifies. Maximum voltage limit is
409          * applied to both sources, but differently: directly clip voltage for
410          * DFLL, and limit maximum frequency for PLL.
411          */
412         for (i = 0, j = 0; i < MAX_DVFS_FREQS; i++) {
413                 table = &d->cvb_table[i];
414                 if (!table->freq)
415                         break;
416
417                 dfll_mv = get_cvb_voltage(
418                         speedo, d->speedo_scale, &table->cvb_dfll_param);
419                 dfll_mv = round_cvb_voltage(dfll_mv, d->voltage_scale);
420
421                 mv = get_cvb_voltage(
422                         speedo, d->speedo_scale, &table->cvb_pll_param);
423                 mv = round_cvb_voltage(mv, d->voltage_scale);
424
425                 /* Check maximum frequency at minimum voltage for dfll source */
426                 dfll_mv = max(dfll_mv, d->min_dfll_mv);
427                 if (dfll_mv > d->min_dfll_mv) {
428                         if (!j)
429                                 break;  /* 1st entry already above Vmin */
430                         if (!fmax_at_vmin)
431                                 fmax_at_vmin = cpu_dvfs->freqs[j - 1];
432                 }
433
434                 /* Clip maximum frequency at maximum voltage for pll source */
435                 if (mv > d->max_mv) {
436                         if (!j)
437                                 break;  /* 1st entry already above Vmax */
438                         if (!fmax_pll_mode)
439                                 fmax_pll_mode = cpu_dvfs->freqs[j - 1];
440                 }
441
442                 /* Minimum rate with pll source voltage above dfll Vmin */
443                 if ((mv >= d->min_dfll_mv) && (!fmin_use_dfll))
444                         fmin_use_dfll = table->freq;
445
446                 /* fill in dvfs tables */
447                 cpu_dvfs->freqs[j] = table->freq;
448                 cpu_dfll_millivolts[j] = min(dfll_mv, d->max_mv);
449                 cpu_millivolts[j] = mv;
450                 j++;
451
452                 /*
453                  * "Round-up" frequency list cut-off (keep first entry that
454                  *  exceeds max voltage - the voltage limit will be enforced
455                  *  anyway, so when requested this frequency dfll will settle
456                  *  at whatever high frequency it can on the particular chip)
457                  */
458                 if (dfll_mv > d->max_mv)
459                         break;
460         }
461         /* Table must not be empty and must have and at least one entry below,
462            and one entry above Vmin */
463         if (!i || !j || !fmax_at_vmin) {
464                 pr_err("tegra11_dvfs: invalid cpu dvfs table for speedo_id %d\n",
465                        speedo_id);
466                 return -ENOENT;
467         }
468
469         /* Must have crossover between dfll and pll operating ranges */
470         if (!fmin_use_dfll || (fmin_use_dfll > fmax_at_vmin)) {
471                 pr_err("tegra11_dvfs: no crossover of dfll and pll voltages\n");
472                 return -EINVAL;
473         }
474
475         /* dvfs tables are successfully populated - fill in the rest */
476         cpu_dvfs->speedo_id = speedo_id;
477         cpu_dvfs->freqs_mult = d->freqs_mult;
478         cpu_dvfs->dvfs_rail->nominal_millivolts = min(d->max_mv,
479                 max(cpu_millivolts[j - 1], cpu_dfll_millivolts[j - 1]));
480         *max_freq_index = j - 1;
481
482         cpu_dvfs->dfll_data.max_rate_boost = fmax_pll_mode ?
483                 (cpu_dvfs->freqs[j - 1] - fmax_pll_mode) * d->freqs_mult : 0;
484         cpu_dvfs->dfll_data.out_rate_min = fmax_at_vmin * d->freqs_mult;
485         cpu_dvfs->dfll_data.use_dfll_rate_min = fmin_use_dfll * d->freqs_mult;
486         cpu_dvfs->dfll_data.min_millivolts = d->min_dfll_mv;
487         return 0;
488 }
489
490 static int __init get_core_nominal_mv_index(int speedo_id)
491 {
492         int i;
493         int mv = tegra_core_speedo_mv();
494         int core_edp_limit = get_core_edp();
495
496         /*
497          * Start with nominal level for the chips with this speedo_id. Then,
498          * make sure core nominal voltage is below edp limit for the board
499          * (if edp limit is set).
500          */
501         if (core_edp_limit)
502                 mv = min(mv, core_edp_limit);
503
504         /* Round nominal level down to the nearest core scaling step */
505         for (i = 0; i < MAX_DVFS_FREQS; i++) {
506                 if ((core_millivolts[i] == 0) || (mv < core_millivolts[i]))
507                         break;
508         }
509
510         if (i == 0) {
511                 pr_err("tegra11_dvfs: unable to adjust core dvfs table to"
512                        " nominal voltage %d\n", mv);
513                 return -ENOSYS;
514         }
515         return i - 1;
516 }
517
518 int tegra_cpu_dvfs_alter(int edp_thermal_index, const cpumask_t *cpus,
519                          bool before_clk_update, int cpu_event)
520 {
521         /* empty definition for tegra11 */
522         return 0;
523 }
524
525 void __init tegra11x_init_dvfs(void)
526 {
527         int cpu_speedo_id = tegra_cpu_speedo_id();
528         int soc_speedo_id = tegra_soc_speedo_id();
529         int core_process_id = tegra_core_process_id();
530
531         int i;
532         int core_nominal_mv_index;
533         int cpu_max_freq_index;
534
535 #ifndef CONFIG_TEGRA_CORE_DVFS
536         tegra_dvfs_core_disabled = true;
537 #endif
538 #ifndef CONFIG_TEGRA_CPU_DVFS
539         tegra_dvfs_cpu_disabled = true;
540 #endif
541         /* Setup rail bins */
542         tegra11_dvfs_rail_vdd_cpu.stats.bin_uV = tegra_get_cvb_alignment_uV();
543         tegra11_dvfs_rail_vdd_core.stats.bin_uV = tegra_get_cvb_alignment_uV();
544
545         /*
546          * Find nominal voltages for core (1st) and cpu rails before rail
547          * init. Nominal voltage index in core scaling ladder can also be
548          * used to determine max dvfs frequencies for all core clocks. In
549          * case of error disable core scaling and set index to 0, so that
550          * core clocks would not exceed rates allowed at minimum voltage.
551          */
552         core_nominal_mv_index = get_core_nominal_mv_index(soc_speedo_id);
553         if (core_nominal_mv_index < 0) {
554                 tegra11_dvfs_rail_vdd_core.disabled = true;
555                 tegra_dvfs_core_disabled = true;
556                 core_nominal_mv_index = 0;
557         }
558         tegra11_dvfs_rail_vdd_core.nominal_millivolts =
559                 core_millivolts[core_nominal_mv_index];
560
561         /*
562          * Setup cpu dvfs and dfll tables from cvb data, determine nominal
563          * voltage for cpu rail, and cpu maximum frequency. Note that entire
564          * frequency range is guaranteed only when dfll is used as cpu clock
565          * source. Reaching maximum frequency with pll as cpu clock source
566          * may not be possible within nominal voltage range (dvfs mechanism
567          * would automatically fail frequency request in this case, so that
568          * voltage limit is not violated). Error when cpu dvfs table can not
569          * be constructed must never happen.
570          */
571         if (set_cpu_dvfs_data(cpu_speedo_id, &cpu_dvfs, &cpu_max_freq_index))
572                 BUG();
573
574         /* Init rail structures and dependencies */
575         tegra_dvfs_init_rails(tegra11_dvfs_rails,
576                 ARRAY_SIZE(tegra11_dvfs_rails));
577
578         /* Search core dvfs table for speedo/process matching entries and
579            initialize dvfs-ed clocks */
580         for (i = 0; i <  ARRAY_SIZE(core_dvfs_table); i++) {
581                 struct dvfs *d = &core_dvfs_table[i];
582                 if (!match_dvfs_one(d, soc_speedo_id, core_process_id))
583                         continue;
584                 init_dvfs_one(d, core_nominal_mv_index);
585         }
586
587         /* Initialize matching cpu dvfs entry already found when nominal
588            voltage was determined */
589         init_dvfs_one(&cpu_dvfs, cpu_max_freq_index);
590
591         /* Finally disable dvfs on rails if necessary */
592         if (tegra_dvfs_core_disabled)
593                 tegra_dvfs_rail_disable(&tegra11_dvfs_rail_vdd_core);
594         if (tegra_dvfs_cpu_disabled)
595                 tegra_dvfs_rail_disable(&tegra11_dvfs_rail_vdd_cpu);
596
597         pr_info("tegra dvfs: VDD_CPU nominal %dmV, scaling %s\n",
598                 tegra11_dvfs_rail_vdd_cpu.nominal_millivolts,
599                 tegra_dvfs_cpu_disabled ? "disabled" : "enabled");
600         pr_info("tegra dvfs: VDD_CORE nominal %dmV, scaling %s\n",
601                 tegra11_dvfs_rail_vdd_core.nominal_millivolts,
602                 tegra_dvfs_core_disabled ? "disabled" : "enabled");
603 }
604
605 int tegra_dvfs_rail_disable_prepare(struct dvfs_rail *rail)
606 {
607         return 0;
608 }
609
610 int tegra_dvfs_rail_post_enable(struct dvfs_rail *rail)
611 {
612         return 0;
613 }
614
615 /*
616  * sysfs and dvfs interfaces to cap tegra core domains frequencies
617  */
618 static DEFINE_MUTEX(core_cap_lock);
619
620 struct core_cap {
621         int refcnt;
622         int level;
623 };
624 static struct core_cap core_buses_cap;
625 static struct core_cap kdvfs_core_cap;
626 static struct core_cap user_core_cap;
627
628 static struct kobject *cap_kobj;
629
630 /* Arranged in order required for enabling/lowering the cap */
631 static struct {
632         const char *cap_name;
633         struct clk *cap_clk;
634         unsigned long freqs[MAX_DVFS_FREQS];
635 } core_cap_table[] = {
636 #ifdef CONFIG_TEGRA_DUAL_CBUS
637         { .cap_name = "cap.c2bus" },
638         { .cap_name = "cap.c3bus" },
639 #else
640         { .cap_name = "cap.cbus" },
641 #endif
642         { .cap_name = "cap.sclk" },
643         { .cap_name = "cap.emc" },
644 };
645
646
647 static void core_cap_level_set(int level)
648 {
649         int i, j;
650
651         for (j = 0; j < ARRAY_SIZE(core_millivolts); j++) {
652                 int v = core_millivolts[j];
653                 if ((v == 0) || (level < v))
654                         break;
655         }
656         j = (j == 0) ? 0 : j - 1;
657         level = core_millivolts[j];
658
659         if (level < core_buses_cap.level) {
660                 for (i = 0; i < ARRAY_SIZE(core_cap_table); i++)
661                         if (core_cap_table[i].cap_clk)
662                                 clk_set_rate(core_cap_table[i].cap_clk,
663                                              core_cap_table[i].freqs[j]);
664         } else if (level > core_buses_cap.level) {
665                 for (i = ARRAY_SIZE(core_cap_table) - 1; i >= 0; i--)
666                         if (core_cap_table[i].cap_clk)
667                                 clk_set_rate(core_cap_table[i].cap_clk,
668                                              core_cap_table[i].freqs[j]);
669         }
670         core_buses_cap.level = level;
671 }
672
673 static void core_cap_update(void)
674 {
675         int new_level = tegra11_dvfs_rail_vdd_core.max_millivolts;
676
677         if (kdvfs_core_cap.refcnt)
678                 new_level = min(new_level, kdvfs_core_cap.level);
679         if (user_core_cap.refcnt)
680                 new_level = min(new_level, user_core_cap.level);
681
682         if (core_buses_cap.level != new_level)
683                 core_cap_level_set(new_level);
684 }
685
686 static void core_cap_enable(bool enable)
687 {
688         if (enable)
689                 core_buses_cap.refcnt++;
690         else if (core_buses_cap.refcnt)
691                 core_buses_cap.refcnt--;
692
693         core_cap_update();
694 }
695
696 static ssize_t
697 core_cap_state_show(struct kobject *kobj, struct kobj_attribute *attr,
698                     char *buf)
699 {
700         return sprintf(buf, "%d (%d)\n", core_buses_cap.refcnt ? 1 : 0,
701                         user_core_cap.refcnt ? 1 : 0);
702 }
703 static ssize_t
704 core_cap_state_store(struct kobject *kobj, struct kobj_attribute *attr,
705                      const char *buf, size_t count)
706 {
707         int state;
708
709         if (sscanf(buf, "%d", &state) != 1)
710                 return -1;
711
712         mutex_lock(&core_cap_lock);
713
714         if (state) {
715                 user_core_cap.refcnt++;
716                 if (user_core_cap.refcnt == 1)
717                         core_cap_enable(true);
718         } else if (user_core_cap.refcnt) {
719                 user_core_cap.refcnt--;
720                 if (user_core_cap.refcnt == 0)
721                         core_cap_enable(false);
722         }
723
724         mutex_unlock(&core_cap_lock);
725         return count;
726 }
727
728 static ssize_t
729 core_cap_level_show(struct kobject *kobj, struct kobj_attribute *attr,
730                     char *buf)
731 {
732         return sprintf(buf, "%d (%d)\n", core_buses_cap.level,
733                         user_core_cap.level);
734 }
735 static ssize_t
736 core_cap_level_store(struct kobject *kobj, struct kobj_attribute *attr,
737                      const char *buf, size_t count)
738 {
739         int level;
740
741         if (sscanf(buf, "%d", &level) != 1)
742                 return -1;
743
744         mutex_lock(&core_cap_lock);
745         user_core_cap.level = level;
746         core_cap_update();
747         mutex_unlock(&core_cap_lock);
748         return count;
749 }
750
751 static struct kobj_attribute cap_state_attribute =
752         __ATTR(core_cap_state, 0644, core_cap_state_show, core_cap_state_store);
753 static struct kobj_attribute cap_level_attribute =
754         __ATTR(core_cap_level, 0644, core_cap_level_show, core_cap_level_store);
755
756 const struct attribute *cap_attributes[] = {
757         &cap_state_attribute.attr,
758         &cap_level_attribute.attr,
759         NULL,
760 };
761
762 void tegra_dvfs_core_cap_enable(bool enable)
763 {
764         mutex_lock(&core_cap_lock);
765
766         if (enable) {
767                 kdvfs_core_cap.refcnt++;
768                 if (kdvfs_core_cap.refcnt == 1)
769                         core_cap_enable(true);
770         } else if (kdvfs_core_cap.refcnt) {
771                 kdvfs_core_cap.refcnt--;
772                 if (kdvfs_core_cap.refcnt == 0)
773                         core_cap_enable(false);
774         }
775         mutex_unlock(&core_cap_lock);
776 }
777
778 void tegra_dvfs_core_cap_level_set(int level)
779 {
780         mutex_lock(&core_cap_lock);
781         kdvfs_core_cap.level = level;
782         core_cap_update();
783         mutex_unlock(&core_cap_lock);
784 }
785
786 static int __init init_core_cap_one(struct clk *c, unsigned long *freqs)
787 {
788         int i, v, next_v = 0;
789         unsigned long rate, next_rate = 0;
790
791         for (i = 0; i < ARRAY_SIZE(core_millivolts); i++) {
792                 v = core_millivolts[i];
793                 if (v == 0)
794                         break;
795
796                 for (;;) {
797                         rate = next_rate;
798                         next_rate = clk_round_rate(c->parent, rate + 1000);
799                         if (IS_ERR_VALUE(next_rate)) {
800                                 pr_debug("tegra11_dvfs: failed to round %s rate %lu\n",
801                                          c->name, rate);
802                                 return -EINVAL;
803                         }
804                         if (rate == next_rate)
805                                 break;
806
807                         next_v = tegra_dvfs_predict_millivolts(
808                                 c->parent, next_rate);
809                         if (IS_ERR_VALUE(next_v)) {
810                                 pr_debug("tegra11_dvfs: failed to predict %s mV for rate %lu\n",
811                                          c->name, next_rate);
812                                 return -EINVAL;
813                         }
814                         if (next_v > v)
815                                 break;
816                 }
817
818                 if (rate == 0) {
819                         rate = next_rate;
820                         pr_warn("tegra11_dvfs: minimum %s rate %lu requires %d mV\n",
821                                 c->name, rate, next_v);
822                 }
823                 freqs[i] = rate;
824                 next_rate = rate;
825         }
826         return 0;
827 }
828
829 static int __init tegra_dvfs_init_core_cap(void)
830 {
831         int i;
832         struct clk *c = NULL;
833
834         core_buses_cap.level = kdvfs_core_cap.level = user_core_cap.level =
835                 tegra11_dvfs_rail_vdd_core.max_millivolts;
836
837         for (i = 0; i < ARRAY_SIZE(core_cap_table); i++) {
838                 c = tegra_get_clock_by_name(core_cap_table[i].cap_name);
839                 if (!c || !c->parent ||
840                     init_core_cap_one(c, core_cap_table[i].freqs)) {
841                         pr_err("tegra11_dvfs: failed to initialize %s table\n",
842                                core_cap_table[i].cap_name);
843                         continue;
844                 }
845                 core_cap_table[i].cap_clk = c;
846         }
847
848         cap_kobj = kobject_create_and_add("tegra_cap", kernel_kobj);
849         if (!cap_kobj) {
850                 pr_err("tegra11_dvfs: failed to create sysfs cap object\n");
851                 return 0;
852         }
853
854         if (sysfs_create_files(cap_kobj, cap_attributes)) {
855                 pr_err("tegra11_dvfs: failed to create sysfs cap interface\n");
856                 return 0;
857         }
858         pr_info("tegra dvfs: tegra sysfs cap interface is initialized\n");
859
860         return 0;
861 }
862 late_initcall(tegra_dvfs_init_core_cap);