]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/platform/tegra/tegra21_dvfs.c
tegra21: dvfs: Integrate CPU DVFS table p4v57
[sojka/nv-tegra/linux-3.10.git] / drivers / platform / tegra / tegra21_dvfs.c
1 /*
2  * drivers/platform/tegra/tegra21_dvfs.c
3  *
4  * Copyright (c) 2012-2015 NVIDIA CORPORATION. All rights reserved.
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 #include <linux/tegra-fuse.h>
25 #include <linux/pm_qos.h>
26 #include <linux/of_address.h>
27
28 #include <linux/platform/tegra/clock.h>
29 #include <linux/platform/tegra/dvfs.h>
30 #include <linux/platform/tegra/tegra_cl_dvfs.h>
31 #include <linux/platform/tegra/cpu-tegra.h>
32 #include "board.h"
33 #include "tegra_core_sysfs_limits.h"
34
35 static bool tegra_dvfs_cpu_disabled;
36 static bool tegra_dvfs_core_disabled;
37 static bool tegra_dvfs_gpu_disabled;
38
39 #define KHZ 1000
40 #define MHZ 1000000
41
42 /* FIXME: need tegra21 step */
43 #define VDD_SAFE_STEP                   100
44
45 static int vdd_core_vmin_trips_table[MAX_THERMAL_LIMITS];
46 static int vdd_core_therm_floors_table[MAX_THERMAL_LIMITS];
47 static struct tegra_cooling_device core_vmin_cdev = {
48         .compatible = "nvidia,tegra210-rail-vmin-cdev",
49 };
50 static int vdd_core_vmax_trips_table[MAX_THERMAL_LIMITS];
51 static int vdd_core_therm_caps_table[MAX_THERMAL_LIMITS];
52 static struct tegra_cooling_device core_vmax_cdev = {
53         .compatible = "nvidia,tegra210-rail-vmax-cdev",
54 };
55
56 static int vdd_cpu_vmin_trips_table[MAX_THERMAL_LIMITS];
57 static int vdd_cpu_therm_floors_table[MAX_THERMAL_LIMITS];
58 static struct tegra_cooling_device cpu_vmin_cdev = {
59         .compatible = "nvidia,tegra210-rail-vmin-cdev",
60 };
61 static int vdd_cpu_vmax_trips_table[MAX_THERMAL_LIMITS];
62 static int vdd_cpu_therm_caps_table[MAX_THERMAL_LIMITS];
63 #ifndef CONFIG_TEGRA_CPU_VOLT_CAP
64 static struct tegra_cooling_device cpu_vmax_cdev = {
65         .compatible = "nvidia,tegra210-rail-vmax-cdev",
66 };
67 #endif
68
69 static struct clk *vgpu_cap_clk;
70 static unsigned long gpu_cap_rates[MAX_THERMAL_LIMITS];
71 static int vdd_gpu_vmax_trips_table[MAX_THERMAL_LIMITS];
72 static int vdd_gpu_therm_caps_table[MAX_THERMAL_LIMITS];
73 static struct tegra_cooling_device gpu_vmax_cdev = {
74         .compatible = "nvidia,tegra210-rail-vmax-cdev",
75 };
76 static struct tegra_cooling_device gpu_vts_cdev = {
77         .compatible = "nvidia,tegra210-rail-scaling-cdev",
78 };
79
80 static struct dvfs_rail tegra21_dvfs_rail_vdd_cpu = {
81         .reg_id = "vdd_cpu",
82         .max_millivolts = 1300,
83         .step = VDD_SAFE_STEP,
84         .step_up = 1300,
85         .jmp_to_zero = true,
86         .vmin_cdev = &cpu_vmin_cdev,
87 #ifndef CONFIG_TEGRA_CPU_VOLT_CAP
88         .vmax_cdev = &cpu_vmax_cdev,
89 #endif
90         .alignment = {
91                 .step_uv = 6250, /* 6.25mV */
92         },
93         .stats = {
94                 .bin_uV = 6250, /* 6.25mV */
95         },
96         .version = "p4v57_p4Av05",
97 };
98
99 static struct dvfs_rail tegra21_dvfs_rail_vdd_core = {
100         .reg_id = "vdd_core",
101         .max_millivolts = 1300,
102         .step = VDD_SAFE_STEP,
103         .step_up = 1300,
104         .vmin_cdev = &core_vmin_cdev,
105         .vmax_cdev = &core_vmax_cdev,
106         .alignment = {
107                 .step_uv = 12500, /* 12.5mV */
108         },
109         .version = "p4v50_p4Av05",
110 };
111
112 static struct dvfs_rail tegra21_dvfs_rail_vdd_gpu = {
113         .reg_id = "vdd_gpu",
114         .max_millivolts = 1300,
115         .step = VDD_SAFE_STEP,
116         .step_up = 1300,
117         .in_band_pm = true,
118         .vts_cdev = &gpu_vts_cdev,
119         .vmax_cdev = &gpu_vmax_cdev,
120         .alignment = {
121                 .step_uv = 6250, /* 6.25mV */
122         },
123         .stats = {
124                 .bin_uV = 6250, /* 6.25mV */
125         },
126         .version = "p4v50_p4Av05",
127 };
128
129 static struct dvfs_rail *tegra21_dvfs_rails[] = {
130         &tegra21_dvfs_rail_vdd_cpu,
131         &tegra21_dvfs_rail_vdd_core,
132         &tegra21_dvfs_rail_vdd_gpu,
133 };
134
135 /* FIXME: Remove after bringup */
136 #define BRINGUP_CVB_V_MARGIN    25
137 #define BRINGUP_CVB_V_MARGIN_EX 5
138 #define CPU_BOOST_THRESHOLD     2014500
139
140 /* CPU DVFS tables */
141 #define CPU_CVB_TABLE           \
142         .freqs_mult = KHZ,      \
143         .speedo_scale = 100,    \
144         .voltage_scale = 1000,  \
145         .cvb_table = {          \
146                 /* f      dfll:    c0,       c1,       c2    pll:    c0,       c1,       c2 */   \
147                 {  204000, {  1007452,   -23865,      370 }, {        0,        0,        0 } }, \
148                 {  306000, {  1052709,   -24875,      370 }, {        0,        0,        0 } }, \
149                 {  408000, {  1099069,   -25895,      370 }, {        0,        0,        0 } }, \
150                 {  510000, {  1146534,   -26905,      370 }, {        0,        0,        0 } }, \
151                 {  612000, {  1195102,   -27915,      370 }, {        0,        0,        0 } }, \
152                 {  714000, {  1244773,   -28925,      370 }, {        0,        0,        0 } }, \
153                 {  816000, {  1295549,   -29935,      370 }, {        0,        0,        0 } }, \
154                 {  918000, {  1347428,   -30955,      370 }, {        0,        0,        0 } }, \
155                 { 1020000, {  1400411,   -31965,      370 }, { -2875621,   358099,    -8585 } }, \
156                 { 1122000, {  1454497,   -32975,      370 }, {   -52225,   104159,    -2816 } }, \
157                 { 1224000, {  1509687,   -33985,      370 }, {  1076868,     8356,     -727 } }, \
158                 { 1326000, {  1565981,   -35005,      370 }, {  2208191,   -84659,     1240 } }, \
159                 { 1428000, {  1623379,   -36015,      370 }, {  2519460,  -105063,     1611 } }, \
160                 { 1530000, {  1681880,   -37025,      370 }, {  2639809,  -108729,     1626 } }, \
161                 { 1632000, {  1741485,   -38035,      370 }, {  2889664,  -122173,     1834 } }, \
162                 { 1734000, {  1802194,   -39055,      370 }, {  3386160,  -154021,     2393 } }, \
163                 { 1836000, {  1864006,   -40065,      370 }, {  5100873,  -279186,     4747 } }, \
164                 { 1912500, {  1910780,   -40815,      370 }, {  5100873,  -279186,     4747 } }, \
165                 { 2014500, {  1227000,        0,        0 }, {  5100873,  -279186,     4747 } }, \
166                 { 2218500, {  1227000,        0,        0 }, {  5100873,  -279186,     4747 } }, \
167                 { 0,       { }, { }, }, \
168         }
169
170 #define CPU_CVB_TABLE_XA        \
171         .freqs_mult = KHZ,      \
172         .speedo_scale = 100,    \
173         .voltage_scale = 1000,  \
174         .cvb_table = {          \
175                 /* f      dfll:    c0,       c1,       c2    pll:    c0,       c1,       c2 */   \
176                 {  204000, {  1352603,   -57545,     1150 }, {        0,        0,        0 } }, \
177                 {  306000, {  1412572,   -59595,     1150 }, {        0,        0,        0 } }, \
178                 {  408000, {  1474199,   -61635,     1150 }, {        0,        0,        0 } }, \
179                 {  510000, {  1537484,   -63675,     1150 }, {        0,        0,        0 } }, \
180                 {  612000, {  1602427,   -65715,     1150 }, {        0,        0,        0 } }, \
181                 {  714000, {  1669028,   -67755,     1150 }, {        0,        0,        0 } }, \
182                 {  816000, {  1737288,   -69795,     1150 }, {        0,        0,        0 } }, \
183                 {  918000, {  1807205,   -71845,     1150 }, {        0,        0,        0 } }, \
184                 { 1020000, {  1878780,   -73885,     1150 }, { -2875621,   358099,    -8585 } }, \
185                 { 1122000, {  1952013,   -75925,     1150 }, {   -52225,   104159,    -2816 } }, \
186                 { 1224000, {  2026905,   -77965,     1150 }, {  1076868,     8356,     -727 } }, \
187                 { 1326000, {  2103454,   -80005,     1150 }, {  2208191,   -84659,     1240 } }, \
188                 { 1428000, {  2181661,   -82055,     1150 }, {  2519460,  -105063,     1611 } }, \
189                 { 1530000, {  2261527,   -84095,     1150 }, {  2639809,  -108729,     1626 } }, \
190                 { 1606500, {  2320112,   -85615,     1150 }, {  2889664,  -122173,     1834 } }, \
191                 { 1632000, {  2343050,   -86135,     1150 }, {  3386160,  -154021,     2393 } }, \
192                 { 0,       { }, { }, }, \
193         }
194
195 #define CPU_CVB_TABLE_EUCM1     \
196         .freqs_mult = KHZ,      \
197         .speedo_scale = 100,    \
198         .voltage_scale = 1000,  \
199         .cvb_table = {          \
200                 /* f      dfll:    c0,       c1,       c2    pll:    c0,       c1,       c2 */   \
201                 {  204000, {   734429,        0,        0 }, {        0,        0,        0 } }, \
202                 {  306000, {   768191,        0,        0 }, {        0,        0,        0 } }, \
203                 {  408000, {   801953,        0,        0 }, {        0,        0,        0 } }, \
204                 {  510000, {   835715,        0,        0 }, {        0,        0,        0 } }, \
205                 {  612000, {   869477,        0,        0 }, {        0,        0,        0 } }, \
206                 {  714000, {   903239,        0,        0 }, {        0,        0,        0 } }, \
207                 {  816000, {   937001,        0,        0 }, {        0,        0,        0 } }, \
208                 {  918000, {   970763,        0,        0 }, {        0,        0,        0 } }, \
209                 { 1020000, {  1004525,        0,        0 }, { -2875621,   358099,    -8585 } }, \
210                 { 1122000, {  1038287,        0,        0 }, {   -52225,   104159,    -2816 } }, \
211                 { 1224000, {  1072049,        0,        0 }, {  1076868,     8356,     -727 } }, \
212                 { 1326000, {  1105811,        0,        0 }, {  2208191,   -84659,     1240 } }, \
213                 { 1428000, {  1130000,        0,        0 }, {  2519460,  -105063,     1611 } }, \
214                 { 1555500, {  1130000,        0,        0 }, {  2639809,  -108729,     1626 } }, \
215                 { 1632000, {  1170000,        0,        0 }, {  2889664,  -122173,     1834 } }, \
216                 { 1734000, {  1227500,        0,        0 }, {  3386160,  -154021,     2393 } }, \
217                 { 0,       { }, { }, }, \
218         }
219
220 #define CPU_CVB_TABLE_EUCM2     \
221         .freqs_mult = KHZ,      \
222         .speedo_scale = 100,    \
223         .voltage_scale = 1000,  \
224         .cvb_table = {          \
225                 /* f      dfll:    c0,       c1,       c2    pll:    c0,       c1,       c2 */   \
226                 {  204000, {  742283 ,        0,        0 }, {        0,        0,        0 } }, \
227                 {  306000, {  776249 ,        0,        0 }, {        0,        0,        0 } }, \
228                 {  408000, {  810215 ,        0,        0 }, {        0,        0,        0 } }, \
229                 {  510000, {  844181 ,        0,        0 }, {        0,        0,        0 } }, \
230                 {  612000, {  878147 ,        0,        0 }, {        0,        0,        0 } }, \
231                 {  714000, {  912113 ,        0,        0 }, {        0,        0,        0 } }, \
232                 {  816000, {  946079 ,        0,        0 }, {        0,        0,        0 } }, \
233                 {  918000, {  980045 ,        0,        0 }, {        0,        0,        0 } }, \
234                 { 1020000, {  1014011,        0,        0 }, { -2875621,   358099,    -8585 } }, \
235                 { 1122000, {  1047977,        0,        0 }, {   -52225,   104159,    -2816 } }, \
236                 { 1224000, {  1081943,        0,        0 }, {  1076868,     8356,     -727 } }, \
237                 { 1326000, {  1090000,        0,        0 }, {  2208191,   -84659,     1240 } }, \
238                 { 1479000, {  1090000,        0,        0 }, {  2519460,  -105063,     1611 } }, \
239                 { 1555500, {  1162000,        0,        0 }, {  2639809,  -108729,     1626 } }, \
240                 { 1683000, {  1195000,        0,        0 }, {  2889664,  -122173,     1834 } }, \
241                 { 0,       { }, { }, }, \
242         }
243
244 static struct cpu_cvb_dvfs cpu_cvb_dvfs_table[] = {
245         {
246                 .speedo_id = 8,
247                 .process_id = 0,
248                 .dfll_tune_data  = {
249                         .tune0          = 0xFFEAD0FF,
250                         .tune1          = 0x020091D9,
251                         .droop_rate_min = 1000000,
252                         .min_millivolts = 900,
253                 },
254                 .pll_tune_data = {
255                         .min_millivolts = 950,
256                 },
257                 .max_mv = 1195,
258                 .max_freq = 1683000,
259                 CPU_CVB_TABLE_EUCM2,
260         },
261         {
262                 .speedo_id = 8,
263                 .process_id = 1,
264                 .dfll_tune_data  = {
265                         .tune0          = 0xFFEAD0FF,
266                         .tune1          = 0x025501D0,
267                         .droop_rate_min = 1000000,
268                         .min_millivolts = 900,
269                 },
270                 .pll_tune_data = {
271                         .min_millivolts = 950,
272                 },
273                 .max_mv = 1195,
274                 .max_freq = 1683000,
275                 CPU_CVB_TABLE_EUCM2,
276         },
277
278         {
279                 .speedo_id = 7,
280                 .process_id = 0,
281                 .dfll_tune_data  = {
282                         .tune0          = 0xFFEAD0FF,
283                         .tune0_high_mv  = 0xFFEAD0FF,
284                         .tune_high_min_millivolts = 864,
285                         .tune1          = 0x020091D9,
286                         .droop_rate_min = 1000000,
287                         .min_millivolts = 841,
288                 },
289                 .pll_tune_data = {
290                         .min_millivolts = 950,
291                 },
292                 .max_mv = 1227,
293                 .max_freq = 1734000,
294                 CPU_CVB_TABLE_EUCM1,
295         },
296         {
297                 .speedo_id = 7,
298                 .process_id = 1,
299                 .dfll_tune_data  = {
300                         .tune0          = 0xFFEAD0FF,
301                         .tune0_high_mv  = 0xFFEAD0FF,
302                         .tune_high_min_millivolts = 864,
303                         .tune1          = 0x025501D0,
304                         .droop_rate_min = 1000000,
305                         .min_millivolts = 841,
306                 },
307                 .pll_tune_data = {
308                         .min_millivolts = 950,
309                 },
310                 .max_mv = 1227,
311                 .max_freq = 1734000,
312                 CPU_CVB_TABLE_EUCM1,
313         },
314
315         {
316                 .speedo_id = 6,
317                 .process_id = 0,
318                 .dfll_tune_data  = {
319                         .tune0          = 0xFFEAD0FF,
320                         .tune1          = 0x020091D9,
321                         .droop_rate_min = 1000000,
322                         .min_millivolts = 870,
323                 },
324                 .pll_tune_data = {
325                         .min_millivolts = 950,
326                 },
327                 .max_mv = 1150,
328                 .max_freq = 2014500,
329                 CPU_CVB_TABLE,
330         },
331         {
332                 .speedo_id = 6,
333                 .process_id = 1,
334                 .dfll_tune_data  = {
335                         .tune0          = 0xFFEAD0FF,
336                         .tune1          = 0x025501D0,
337                         .droop_rate_min = 1000000,
338                         .min_millivolts = 870,
339                 },
340                 .pll_tune_data = {
341                         .min_millivolts = 950,
342                 },
343                 .max_mv = 1150,
344                 .max_freq = 2014500,
345                 CPU_CVB_TABLE,
346         },
347
348         {
349                 .speedo_id = 5,
350                 .process_id = 0,
351                 .dfll_tune_data  = {
352                         .tune0          = 0xFFEAD0FF,
353                         .tune0_high_mv  = 0xFFEAD0FF,
354                         .tune_high_min_millivolts = 864,
355                         .tune1          = 0x020091D9,
356                         .droop_rate_min = 1000000,
357                         .min_millivolts = 818,
358                 },
359                 .pll_tune_data = {
360                         .min_millivolts = 950,
361                 },
362                 .max_mv = 1227,
363                 .max_freq = 1912500,
364                 CPU_CVB_TABLE,
365         },
366         {
367                 .speedo_id = 5,
368                 .process_id = 1,
369                 .dfll_tune_data  = {
370                         .tune0          = 0xFFEAD0FF,
371                         .tune0_high_mv  = 0xFFEAD0FF,
372                         .tune_high_min_millivolts = 864,
373                         .tune1          = 0x025501D0,
374                         .droop_rate_min = 1000000,
375                         .min_millivolts = 818,
376                 },
377                 .pll_tune_data = {
378                         .min_millivolts = 950,
379                 },
380                 .max_mv = 1227,
381                 .max_freq = 1912500,
382                 CPU_CVB_TABLE,
383         },
384
385         {
386                 .speedo_id = 4,
387                 .process_id = 0,
388                 .dfll_tune_data  = {
389                         .tune0          = 0x0000C0FF,
390                         .tune1          = 0x012261D9,
391                         .droop_rate_min = 1000000,
392                         .min_millivolts = 918,
393                 },
394                 .pll_tune_data = {
395                         .min_millivolts = 950,
396                 },
397                 .max_mv = 1113,
398                 .max_freq = 1632000,
399                 CPU_CVB_TABLE_XA,
400         },
401         {
402                 .speedo_id = 4,
403                 .process_id = 1,
404                 .dfll_tune_data  = {
405                         .tune0          = 0x0000C0FF,
406                         .tune1          = 0x0166B1D0,
407                         .droop_rate_min = 1000000,
408                         .min_millivolts = 918,
409                 },
410                 .pll_tune_data = {
411                         .min_millivolts = 950,
412                 },
413                 .max_mv = 1113,
414                 .max_freq = 1632000,
415                 CPU_CVB_TABLE_XA,
416         },
417
418         {
419                 .speedo_id = 3,
420                 .process_id = 0,
421                 .dfll_tune_data  = {
422                         .tune0          = 0xFFEAD0FF,
423                         .tune0_high_mv  = 0xFFEAD0FF,
424                         .tune_high_min_millivolts = 864,
425                         .tune1          = 0x020091D9,
426                         .droop_rate_min = 1000000,
427                         .min_millivolts = 822,
428                 },
429                 .pll_tune_data = {
430                         .min_millivolts = 950,
431                 },
432                 .max_mv = 1227,
433                 .max_freq = 1912500,
434                 CPU_CVB_TABLE,
435         },
436         {
437                 .speedo_id = 3,
438                 .process_id = 1,
439                 .dfll_tune_data  = {
440                         .tune0          = 0xFFEAD0FF,
441                         .tune0_high_mv  = 0xFFEAD0FF,
442                         .tune_high_min_millivolts = 864,
443                         .tune1          = 0x025501D0,
444                         .droop_rate_min = 1000000,
445                         .min_millivolts = 822,
446                 },
447                 .pll_tune_data = {
448                         .min_millivolts = 950,
449                 },
450                 .max_mv = 1227,
451                 .max_freq = 1912500,
452                 CPU_CVB_TABLE,
453         },
454
455         {
456                 .speedo_id = 2,
457                 .process_id = 0,
458                 .dfll_tune_data  = {
459                         .tune0          = 0xFFEAD0FF,
460                         .tune1          = 0x020091D9,
461                         .droop_rate_min = 1000000,
462                         .min_millivolts = 870,
463                 },
464                 .pll_tune_data = {
465                         .min_millivolts = 950,
466                 },
467                 .max_mv = 1227,
468                 .max_freq = 2218500,
469                 CPU_CVB_TABLE,
470         },
471         {
472                 .speedo_id = 2,
473                 .process_id = 1,
474                 .dfll_tune_data  = {
475                         .tune0          = 0xFFEAD0FF,
476                         .tune1          = 0x025501D0,
477                         .droop_rate_min = 1000000,
478                         .min_millivolts = 870,
479                 },
480                 .pll_tune_data = {
481                         .min_millivolts = 950,
482                 },
483                 .max_mv = 1227,
484                 .max_freq = 2218500,
485                 CPU_CVB_TABLE,
486         },
487
488         {
489                 .speedo_id = 1,
490                 .process_id = 0,
491                 .dfll_tune_data  = {
492                         .tune0          = 0xFFEAD0FF,
493                         .tune0_high_mv  = 0xFFEAD0FF,
494                         .tune_high_min_millivolts = 864,
495                         .tune1          = 0x020091D9,
496                         .droop_rate_min = 1000000,
497                         .min_millivolts = 837,
498                 },
499                 .pll_tune_data = {
500                         .min_millivolts = 950,
501                 },
502                 .max_mv = 1227,
503                 .max_freq = 1912500,
504                 CPU_CVB_TABLE,
505         },
506         {
507                 .speedo_id = 1,
508                 .process_id = 1,
509                 .dfll_tune_data  = {
510                         .tune0          = 0xFFEAD0FF,
511                         .tune0_high_mv  = 0xFFEAD0FF,
512                         .tune_high_min_millivolts = 864,
513                         .tune1          = 0x025501D0,
514                         .droop_rate_min = 1000000,
515                         .min_millivolts = 837,
516                 },
517                 .pll_tune_data = {
518                         .min_millivolts = 950,
519                 },
520                 .max_mv = 1227,
521                 .max_freq = 1912500,
522                 CPU_CVB_TABLE,
523         },
524
525         {
526                 .speedo_id = 0,
527                 .process_id = 0,
528                 .dfll_tune_data  = {
529                         .tune0          = 0xFFEAD0FF,
530                         .tune0_high_mv  = 0xFFEAD0FF,
531                         .tune_high_min_millivolts = 864,
532                         .tune1          = 0x020091D9,
533                         .droop_rate_min = 1000000,
534                         .min_millivolts = 850,
535                 },
536                 .pll_tune_data = {
537                         .min_millivolts = 950,
538                 },
539                 .max_mv = 1170,
540                 .max_freq = 1912500,
541                 CPU_CVB_TABLE,
542         },
543         {
544                 .speedo_id = 0,
545                 .process_id = 1,
546                 .dfll_tune_data  = {
547                         .tune0          = 0xFFEAD0FF,
548                         .tune0_high_mv  = 0xFFEAD0FF,
549                         .tune_high_min_millivolts = 864,
550                         .tune1          = 0x025501D0,
551                         .droop_rate_min = 1000000,
552                         .min_millivolts = 850,
553                 },
554                 .pll_tune_data = {
555                         .min_millivolts = 950,
556                 },
557                 .max_mv = 1170,
558                 .max_freq = 1912500,
559                 CPU_CVB_TABLE,
560         },
561 };
562
563 static int cpu_millivolts[MAX_DVFS_FREQS];
564 static int cpu_dfll_millivolts[MAX_DVFS_FREQS];
565
566 static struct dvfs cpu_dvfs = {
567         .clk_name       = "cpu_g",
568         .millivolts     = cpu_millivolts,
569         .dfll_millivolts = cpu_dfll_millivolts,
570         .auto_dvfs      = true,
571         .dvfs_rail      = &tegra21_dvfs_rail_vdd_cpu,
572 };
573
574 /* CPU LP DVFS tables */
575 #define CPU_LP_CVB_TABLE        \
576         .freqs_mult = KHZ,      \
577         .speedo_scale = 100,    \
578         .voltage_scale = 1000,  \
579         .cvb_table = {          \
580                 /* f       dfll pll:    c0,       c1,       c2 */    \
581                 {  204000, { }, {  1112126,   -14875,     -200 }, }, \
582                 {  307200, { }, {  1155822,   -15435,     -200 }, }, \
583                 {  403200, { }, {  1202425,   -16005,     -200 }, }, \
584                 {  518400, { }, {  1251935,   -16565,     -200 }, }, \
585                 {  614400, { }, {  1304351,   -17125,     -200 }, }, \
586                 {  710400, { }, {  1359673,   -17685,     -200 }, }, \
587                 {  825600, { }, {  1417902,   -18245,     -200 }, }, \
588                 {  921600, { }, {  1479037,   -18815,     -200 }, }, \
589                 { 1036800, { }, {  1543079,   -19375,     -200 }, }, \
590                 { 1132800, { }, {  1610027,   -19935,     -200 }, }, \
591                 { 1228800, { }, {  1675881,   -20495,     -200 }, }, \
592                 { 0,       { }, { }, }, \
593         }
594
595 #define CPU_LP_CVB_TABLE_XA     \
596         .freqs_mult = KHZ,      \
597         .speedo_scale = 100,    \
598         .voltage_scale = 1000,  \
599         .cvb_table = {          \
600                 /* f       dfll pll:    c0,       c1,       c2 */    \
601                 {  204000, { }, {  1163829,   -15535,     -200 }, }, \
602                 {  307200, { }, {  1210942,   -16095,     -200 }, }, \
603                 {  403200, { }, {  1260961,   -16665,     -200 }, }, \
604                 {  518400, { }, {  1313886,   -17225,     -200 }, }, \
605                 {  614400, { }, {  1369717,   -17785,     -200 }, }, \
606                 {  710400, { }, {  1428454,   -18345,     -200 }, }, \
607                 {  825600, { }, {  1490098,   -18905,     -200 }, }, \
608                 {  921600, { }, {  1548647,   -19475,     -200 }, }, \
609                 {  940800, { }, {  1556912,   -19605,     -200 }, }, \
610                 { 0,       { }, { }, }, \
611         }
612
613 static struct cpu_cvb_dvfs cpu_lp_cvb_dvfs_table[] = {
614         {
615                 .speedo_id = 5,
616                 .process_id = -1,
617                 .pll_tune_data = {
618                         .min_millivolts = 818,
619                 },
620                 .max_mv = 1227,
621                 .max_freq = 1132800,
622                 CPU_LP_CVB_TABLE,
623         },
624         {
625                 .speedo_id = 4,
626                 .process_id = -1,
627                 .pll_tune_data = {
628                         .min_millivolts = 918,
629                 },
630                 .max_mv = 1113,
631                 .max_freq =  940800,
632                 CPU_LP_CVB_TABLE_XA,
633         },
634         {
635                 .speedo_id = 3,
636                 .process_id = -1,
637                 .pll_tune_data = {
638                         .min_millivolts = 822,
639                 },
640                 .max_mv = 1227,
641                 .max_freq = 1132800,
642                 CPU_LP_CVB_TABLE,
643         },
644         {
645                 .speedo_id = 1,
646                 .process_id = -1,
647                 .pll_tune_data = {
648                         .min_millivolts = 837,
649                 },
650                 .max_mv = 1227,
651                 .max_freq = 1132800,
652                 CPU_LP_CVB_TABLE,
653         },
654         {
655                 .speedo_id = -1,
656                 .process_id = -1,
657                 .pll_tune_data = {
658                         .min_millivolts = 850,
659                 },
660                 .max_mv = 1170,
661                 .max_freq = 1132800,
662                 CPU_LP_CVB_TABLE,
663         },
664 };
665
666 static int cpu_lp_millivolts[MAX_DVFS_FREQS];
667
668 static struct dvfs cpu_lp_dvfs = {
669         .clk_name       = "cpu_lp",
670         .millivolts     = cpu_lp_millivolts,
671         .auto_dvfs      = true,
672         .dvfs_rail      = &tegra21_dvfs_rail_vdd_cpu,
673 };
674
675 /* GPU DVFS tables */
676 #define NA_FREQ_CVB_TABLE       \
677         .freqs_mult = KHZ,      \
678         .speedo_scale = 100,    \
679         .thermal_scale = 10,    \
680         .voltage_scale = 1000,  \
681         .cvb_table = {          \
682                 /* f       dfll pll:    c0,       c1,       c2,       c3,       c4,       c5 */    \
683                 {   76800, { }, {   814294,     8144,     -940,      808,   -21583,      226 }, }, \
684                 {  153600, { }, {   856185,     8144,     -940,      808,   -21583,      226 }, }, \
685                 {  230400, { }, {   898077,     8144,     -940,      808,   -21583,      226 }, }, \
686                 {  307200, { }, {   939968,     8144,     -940,      808,   -21583,      226 }, }, \
687                 {  384000, { }, {   981860,     8144,     -940,      808,   -21583,      226 }, }, \
688                 {  460800, { }, {  1023751,     8144,     -940,      808,   -21583,      226 }, }, \
689                 {  537600, { }, {  1065642,     8144,     -940,      808,   -21583,      226 }, }, \
690                 {  614400, { }, {  1107534,     8144,     -940,      808,   -21583,      226 }, }, \
691                 {  691200, { }, {  1149425,     8144,     -940,      808,   -21583,      226 }, }, \
692                 {  768000, { }, {  1191317,     8144,     -940,      808,   -21583,      226 }, }, \
693                 {  844800, { }, {  1233208,     8144,     -940,      808,   -21583,      226 }, }, \
694                 {  921600, { }, {  1275100,     8144,     -940,      808,   -21583,      226 }, }, \
695                 {  998400, { }, {  1316991,     8144,     -940,      808,   -21583,      226 }, }, \
696                 { 0,       { }, { }, }, \
697         }
698
699 #define NA_FREQ_CVB_TABLE_XA    \
700         .freqs_mult = KHZ,      \
701         .speedo_scale = 100,    \
702         .thermal_scale = 10,    \
703         .voltage_scale = 1000,  \
704         .cvb_table = {          \
705                 /* f       dfll pll:    c0,       c1,       c2,       c3,       c4,       c5 */    \
706                 {   76800, { }, {  1133092,   -20365,     -177,      306,   -13906,      428 }, }, \
707                 {  153600, { }, {  1174337,   -20365,     -177,      306,   -13906,      428 }, }, \
708                 {  230400, { }, {  1215582,   -20365,     -177,      306,   -13906,      428 }, }, \
709                 {  345600, { }, {  1277341,   -20365,     -177,      306,   -13906,      428 }, }, \
710                 {  384000, { }, {  1298071,   -20365,     -177,      306,   -13906,      428 }, }, \
711                 {  460800, { }, {  1339315,   -20365,     -177,      306,   -13906,      428 }, }, \
712                 {  537600, { }, {  1380560,   -20365,     -177,      306,   -13906,      428 }, }, \
713                 {  614400, { }, {  1421804,   -20365,     -177,      306,   -13906,      428 }, }, \
714                 {  691200, { }, {  1463049,   -20365,     -177,      306,   -13906,      428 }, }, \
715                 {  768000, { }, {  1504293,   -20365,     -177,      306,   -13906,      428 }, }, \
716                 {  806400, { }, {  1519915,   -20365,     -177,      306,   -13906,      428 }, }, \
717                 { 0,       { }, { }, }, \
718         }
719
720 #define FIXED_FREQ_CVB_TABLE    \
721         .freqs_mult = KHZ,      \
722         .speedo_scale = 100,    \
723         .thermal_scale = 10,    \
724         .voltage_scale = 1000,  \
725         .cvb_table = {          \
726                 /* f       dfll pll:    c0,       c1,       c2 */    \
727                 {   76800, { }, {  1786666,   -85625,     1632 }, }, \
728                 {  153600, { }, {  1846729,   -87525,     1632 }, }, \
729                 {  230400, { }, {  1910480,   -89425,     1632 }, }, \
730                 {  307200, { }, {  1977920,   -91325,     1632 }, }, \
731                 {  384000, { }, {  2049049,   -93215,     1632 }, }, \
732                 {  460800, { }, {  2122872,   -95095,     1632 }, }, \
733                 {  537600, { }, {  2201331,   -96985,     1632 }, }, \
734                 {  614400, { }, {  2283479,   -98885,     1632 }, }, \
735                 {  691200, { }, {  2369315,  -100785,     1632 }, }, \
736                 {  768000, { }, {  2458841,  -102685,     1632 }, }, \
737                 {  844800, { }, {  2550821,  -104555,     1632 }, }, \
738                 {  921600, { }, {  2647676,  -106455,     1632 }, }, \
739                 { 0,       { }, { }, }, \
740         }
741
742 static struct gpu_cvb_dvfs gpu_cvb_dvfs_table[] = {
743         {
744                 .speedo_id = 4,
745                 .process_id = -1,
746                 .pll_tune_data = {
747                         .min_millivolts = 918,
748                 },
749                 .max_mv = 1113,
750                 .max_freq = 806400,
751 #ifdef CONFIG_TEGRA_USE_NA_GPCPLL
752                 NA_FREQ_CVB_TABLE_XA,
753 #else
754                 FIXED_FREQ_CVB_TABLE,
755 #endif
756         },
757
758         {
759                 .speedo_id = 3,
760                 .process_id = -1,
761                 .pll_tune_data = {
762                         .min_millivolts = 810,
763                 },
764                 .max_mv = 1150,
765                 .max_freq = 921600,
766 #ifdef CONFIG_TEGRA_USE_NA_GPCPLL
767                 NA_FREQ_CVB_TABLE,
768 #else
769                 FIXED_FREQ_CVB_TABLE,
770 #endif
771         },
772
773         {
774                 .speedo_id = 2,
775                 .process_id = -1,
776                 .pll_tune_data = {
777                         .min_millivolts = 818,
778                 },
779                 .max_mv = 1150,
780                 .max_freq = 998400,
781 #ifdef CONFIG_TEGRA_USE_NA_GPCPLL
782                 NA_FREQ_CVB_TABLE,
783 #else
784                 FIXED_FREQ_CVB_TABLE,
785 #endif
786         },
787
788         {
789                 .speedo_id = 1,
790                 .process_id = -1,
791                 .pll_tune_data = {
792                         .min_millivolts = 840,
793                 },
794                 .max_mv = 1150,
795                 .max_freq = 998400,
796 #ifdef CONFIG_TEGRA_USE_NA_GPCPLL
797                 NA_FREQ_CVB_TABLE,
798 #else
799                 FIXED_FREQ_CVB_TABLE,
800 #endif
801         },
802
803         {
804                 .speedo_id = 0,
805                 .process_id = -1,
806                 .pll_tune_data = {
807                         .min_millivolts = 950,
808                 },
809 #ifdef CONFIG_TEGRA_GPU_DVFS
810                 .max_mv = 1150,
811 #else
812                 .max_mv = 1000,
813 #endif
814                 .max_freq = 921600,
815                 FIXED_FREQ_CVB_TABLE,
816         },
817 };
818
819 static int gpu_vmin[MAX_THERMAL_RANGES];
820 static int gpu_peak_millivolts[MAX_DVFS_FREQS];
821 static int gpu_millivolts[MAX_THERMAL_RANGES][MAX_DVFS_FREQS];
822 static struct dvfs gpu_dvfs = {
823         .clk_name       = "gbus",
824         .auto_dvfs      = true,
825         .dvfs_rail      = &tegra21_dvfs_rail_vdd_gpu,
826 };
827
828 /* Core DVFS tables */
829 static const int core_millivolts[MAX_DVFS_FREQS] = {
830         800, 825, 850, 875, 900, 925, 950, 975, 1000, 1025, 1050, 1062, 1075, 1100, 1125 };
831
832 #define CORE_DVFS(_clk_name, _speedo_id, _process_id, _auto, _mult, _freqs...) \
833         {                                                       \
834                 .clk_name       = _clk_name,                    \
835                 .speedo_id      = _speedo_id,                   \
836                 .process_id     = _process_id,                  \
837                 .freqs          = {_freqs},                     \
838                 .freqs_mult     = _mult,                        \
839                 .millivolts     = core_millivolts,              \
840                 .auto_dvfs      = _auto,                        \
841                 .dvfs_rail      = &tegra21_dvfs_rail_vdd_core,  \
842         }
843
844 #define OVRRD_DVFS(_clk_name, _speedo_id, _process_id, _auto, _mult, _freqs...) \
845         {                                                       \
846                 .clk_name       = _clk_name,                    \
847                 .speedo_id      = _speedo_id,                   \
848                 .process_id     = _process_id,                  \
849                 .freqs          = {_freqs},                     \
850                 .freqs_mult     = _mult,                        \
851                 .millivolts     = core_millivolts,              \
852                 .auto_dvfs      = _auto,                        \
853                 .can_override   = true,                         \
854                 .dvfs_rail      = &tegra21_dvfs_rail_vdd_core,  \
855         }
856
857 /*
858  * Exceptions in the below table that disable framework auto-dvfs:
859  *
860  * sbus: Tegra21 platform code calls tegra_dvfs_set_rate manually;
861  * it is necessary for special handling of system clock skipper enabled on T210
862  *
863  * display: display driver calls tegra_dvfs_set_rate manually;
864  * it is necessary because display clock divider is internal to the display
865  * block, and controlled by driver directly.
866  *
867  * uart: has internal clock divider controlled by driver directly; respectively
868  * defined as manual DVFS clock (however, UART is voltage independent, so no DVFS
869  * is enabled on UART clocks, and driver does not call tegra_dvfs_set_rate).
870  *
871  * i2c5: power-i2c transport for PMIC voltage control; voltage independent -
872  * possible deadlock, otherwise.
873  *
874  * pwm: transport for OVR voltage control; voltage independent - possible
875  * deadlock, otherwise.
876  */
877 static struct dvfs core_dvfs_table[] = {
878 /* ID 1 Tables */
879         /* Core voltages(mV):                               800,     825,     850,     875,     900,     925,     950,     975,    1000,    1025,    1050,    1062,    1075,    1100,    1125 */
880         CORE_DVFS("emc",                1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000),
881
882         CORE_DVFS("vic03",              1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  601600,  601600),
883         CORE_DVFS("nvjpg",              1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  601600,  601600),
884         CORE_DVFS("se",                 1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  601600,  601600),
885         CORE_DVFS("tsecb",              1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  601600,  601600),
886         CORE_DVFS("c2bus",              1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  601600,  601600),
887
888         CORE_DVFS("msenc",              1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  652800,  652800),
889         CORE_DVFS("nvdec",              1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  652800,  652800),
890         CORE_DVFS("c3bus",              1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  652800,  652800),
891
892         CORE_DVFS("vi",                 1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  716800,  716800),
893         CORE_DVFS("isp",                1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  716800,  716800),
894         CORE_DVFS("cbus",               1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  716800,  716800),
895
896         CORE_DVFS("adsp_bus",           1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  806400,  806400),
897         CORE_DVFS("ape",                1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  550400,  550400),
898
899         CORE_DVFS("sbus",               1, -1, 0, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  408000,  408000),
900         CORE_DVFS("host1x",             1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  408000,  408000),
901         CORE_DVFS("tsec",               1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  408000,  408000),
902         CORE_DVFS("mselect",            1, -1, 1, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  408000,  408000),
903
904         CORE_DVFS("disp1",              1, -1, 0, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  614400,  614400),
905         CORE_DVFS("disp2",              1, -1, 0, KHZ,        1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,       1,  614400,  614400),
906
907 /* ID 0 Tables */
908         /* Core voltages(mV):                               800,     825,     850,     875,     900,     925,     950,     975,    1000,    1025,    1050,    1062,    1075,    1100,    1125 */
909         CORE_DVFS("emc",                0, 0, 1, KHZ,         1,       1,       1,       1,       1,       1,       1,       1, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000),
910
911         CORE_DVFS("vic03",              0, 0, 1, KHZ,    140800,  140800,  268800,  332800,  371200,  409600,  435200,  473600,  499200,  537600,  563200,  563200,  588800,  601600,  627200),
912         CORE_DVFS("nvjpg",              0, 0, 1, KHZ,    140800,  140800,  268800,  332800,  371200,  409600,  435200,  473600,  499200,  537600,  563200,  563200,  588800,  601600,  627200),
913         CORE_DVFS("se",                 0, 0, 1, KHZ,    140800,  140800,  268800,  332800,  371200,  409600,  435200,  473600,  499200,  537600,  563200,  563200,  588800,  601600,  627200),
914         CORE_DVFS("tsecb",              0, 0, 1, KHZ,    140800,  140800,  268800,  332800,  371200,  409600,  435200,  473600,  499200,  537600,  563200,  563200,  588800,  601600,  627200),
915         CORE_DVFS("c2bus",              0, 0, 1, KHZ,    140800,  140800,  268800,  332800,  371200,  409600,  435200,  473600,  499200,  537600,  563200,  563200,  588800,  601600,  627200),
916
917         CORE_DVFS("msenc",              0, 0, 1, KHZ,    192000,  192000,  345600,  396800,  435200,  473600,  512000,  563200,  601600,  627200,  652800,  652800,  678400,  691200,  716800),
918         CORE_DVFS("nvdec",              0, 0, 1, KHZ,    192000,  192000,  345600,  396800,  435200,  473600,  512000,  563200,  601600,  627200,  652800,  652800,  678400,  691200,  716800),
919         CORE_DVFS("c3bus",              0, 0, 1, KHZ,    192000,  192000,  345600,  396800,  435200,  473600,  512000,  563200,  601600,  627200,  652800,  652800,  678400,  691200,  716800),
920
921         CORE_DVFS("vi",                 0, 0, 1, KHZ,    217600,  217600,  307200,  307200,  371200,  435200,  499200,  550400,  614400,  678400,  742400,  742400,  793600,  793600,  793600),
922         CORE_DVFS("isp",                0, 0, 1, KHZ,    217600,  217600,  307200,  307200,  371200,  435200,  499200,  550400,  614400,  678400,  742400,  742400,  793600,  793600,  793600),
923         CORE_DVFS("cbus",               0, 0, 1, KHZ,    217600,  217600,  307200,  307200,  371200,  435200,  499200,  550400,  614400,  678400,  742400,  742400,  793600,  793600,  793600),
924
925         CORE_DVFS("adsp_bus",           0, 0, 1, KHZ,    153600,  153600,  332800,  371200,  422400,  486400,  563200,  614400,  691200,  742400,  780800,  780800,  819200,  844800,  844800),
926         CORE_DVFS("ape",                0, 0, 1, KHZ,    140800,  140800,  230400,  268800,  307200,  345600,  384000,  448000,  486400,  499200,  499200,  499200,  499200,  499200,  499200),
927
928         CORE_DVFS("sbus",               0, 0, 0, KHZ,    115200,  115200,  179200,  217600,  243200,  268800,  294400,  320000,  345600,  358400,  371200,  371200,  384000,  408000,  408000),
929         CORE_DVFS("host1x",             0, 0, 1, KHZ,     81600,   81600,  140800,  153600,  166400,  192000,  230400,  281600,  320000,  345600,  371200,  371200,  384000,  408000,  408000),
930         CORE_DVFS("tsec",               0, 0, 1, KHZ,    217600,  217600,  384000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000),
931         CORE_DVFS("mselect",            0, 0, 1, KHZ,    204000,  204000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000),
932
933         CORE_DVFS("disp1",              0, 0, 0, KHZ,    153600,  153600,  332800,  371200,  409600,  422400,  460800,  499200,  537600,  576000,  601600,  601600,  640000,  665600,  665600),
934         CORE_DVFS("disp2",              0, 0, 0, KHZ,    153600,  153600,  332800,  371200,  409600,  422400,  460800,  499200,  537600,  576000,  601600,  601600,  640000,  665600,  665600),
935
936         /* Core voltages(mV):                               800,     825,     850,     875,     900,     925,     950,     975,    1000,    1025,    1050,    1062,    1075,    1100,    1125 */
937         CORE_DVFS("emc",                0, 1, 1, KHZ,         1,       1,       1,       1,       1,       1,       1,       1, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000),
938
939         CORE_DVFS("vic03",              0, 1, 1, KHZ,    192000,  192000,  307200,  345600,  409600,  486400,  524800,  550400,  576000,  588800,  614400,  614400,  627200,  627200,  627200),
940         CORE_DVFS("nvjpg",              0, 1, 1, KHZ,    192000,  192000,  307200,  345600,  409600,  486400,  524800,  550400,  576000,  588800,  614400,  614400,  627200,  627200,  627200),
941         CORE_DVFS("se",                 0, 1, 1, KHZ,    192000,  192000,  307200,  345600,  409600,  486400,  524800,  550400,  576000,  588800,  614400,  614400,  627200,  627200,  627200),
942         CORE_DVFS("tsecb",              0, 1, 1, KHZ,    192000,  192000,  307200,  345600,  409600,  486400,  524800,  550400,  576000,  588800,  614400,  614400,  627200,  627200,  627200),
943         CORE_DVFS("c2bus",              0, 1, 1, KHZ,    192000,  192000,  307200,  345600,  409600,  486400,  524800,  550400,  576000,  588800,  614400,  614400,  627200,  627200,  627200),
944
945         CORE_DVFS("msenc",              0, 1, 1, KHZ,    268800,  268800,  384000,  448000,  486400,  550400,  576000,  614400,  652800,  678400,  691200,  691200,  716800,  716800,  716800),
946         CORE_DVFS("nvdec",              0, 1, 1, KHZ,    268800,  268800,  384000,  448000,  486400,  550400,  576000,  614400,  652800,  678400,  691200,  691200,  716800,  716800,  716800),
947         CORE_DVFS("c3bus",              0, 1, 1, KHZ,    268800,  268800,  384000,  448000,  486400,  550400,  576000,  614400,  652800,  678400,  691200,  691200,  716800,  716800,  716800),
948
949         CORE_DVFS("vi",                 0, 1, 1, KHZ,    268800,  268800,  473600,  473600,  576000,  588800,  678400,  691200,  691200,  691200,  793600,  793600,  793600,  793600,  793600),
950         CORE_DVFS("isp",                0, 1, 1, KHZ,    268800,  268800,  473600,  473600,  576000,  588800,  678400,  691200,  691200,  691200,  793600,  793600,  793600,  793600,  793600),
951         CORE_DVFS("cbus",               0, 1, 1, KHZ,    268800,  268800,  473600,  473600,  576000,  588800,  678400,  691200,  691200,  691200,  793600,  793600,  793600,  793600,  793600),
952
953         CORE_DVFS("adsp_bus",           0, 1, 1, KHZ,    230400,  230400,  422400,  460800,  524800,  601600,  652800,  704000,  755200,  819200,  844800,  844800,  844800,  844800,  844800),
954         CORE_DVFS("ape",                0, 1, 1, KHZ,    179200,  179200,  307200,  345600,  371200,  409600,  422400,  460800,  499200,  499200,  499200,  499200,  499200,  499200,  499200),
955
956         CORE_DVFS("sbus",               0, 1, 0, KHZ,    140800,  140800,  230400,  256000,  281600,  307200,  332800,  358400,  371200,  408000,  408000,  408000,  408000,  408000,  408000),
957         CORE_DVFS("host1x",             0, 1, 1, KHZ,     81600,   81600,  153600,  179200,  192000,  217600,  345600,  358400,  384000,  408000,  408000,  408000,  408000,  408000,  408000),
958         CORE_DVFS("tsec",               0, 1, 1, KHZ,    268800,  268800,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000),
959         CORE_DVFS("mselect",            0, 1, 1, KHZ,    204000,  204000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000),
960
961         CORE_DVFS("disp1",              0, 1, 0, KHZ,    243200,  243200,  422400,  460800,  499200,  524800,  563200,  576000,  601600,  640000,  665600,  665600,  665600,  665600,  665600),
962         CORE_DVFS("disp2",              0, 1, 0, KHZ,    243200,  243200,  422400,  460800,  499200,  524800,  563200,  576000,  601600,  640000,  665600,  665600,  665600,  665600,  665600),
963
964         /* Core voltages(mV):                               800,     825,     850,     875,     900,     925,     950,     975,    1000,    1025,    1050,    1062,    1075,    1100,    1125 */
965         CORE_DVFS("emc",                0, 2, 1, KHZ,         1,       1,       1,       1,       1,       1,       1,       1, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000, 1200000),
966
967         CORE_DVFS("vic03",              0, 2, 1, KHZ,    230400,  230400,  371200,  448000,  499200,  563200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200),
968         CORE_DVFS("nvjpg",              0, 2, 1, KHZ,    230400,  230400,  371200,  448000,  499200,  563200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200),
969         CORE_DVFS("se",                 0, 2, 1, KHZ,    230400,  230400,  371200,  448000,  499200,  563200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200),
970         CORE_DVFS("tsecb",              0, 2, 1, KHZ,    230400,  230400,  371200,  448000,  499200,  563200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200),
971         CORE_DVFS("c2bus",              0, 2, 1, KHZ,    230400,  230400,  371200,  448000,  499200,  563200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200,  627200),
972
973         CORE_DVFS("msenc",              0, 2, 1, KHZ,    307200,  307200,  486400,  563200,  614400,  640000,  652800,  678400,  716800,  716800,  716800,  716800,  716800,  716800,  716800),
974         CORE_DVFS("nvdec",              0, 2, 1, KHZ,    307200,  307200,  486400,  563200,  614400,  640000,  652800,  678400,  716800,  716800,  716800,  716800,  716800,  716800,  716800),
975         CORE_DVFS("c3bus",              0, 2, 1, KHZ,    307200,  307200,  486400,  563200,  614400,  640000,  652800,  678400,  716800,  716800,  716800,  716800,  716800,  716800,  716800),
976
977         CORE_DVFS("vi",                 0, 2, 1, KHZ,    384000,  384000,  588800,  678400,  691200,  691200,  768000,  793600,  793600,  793600,  793600,  793600,  793600,  793600,  793600),
978         CORE_DVFS("isp",                0, 2, 1, KHZ,    384000,  384000,  588800,  678400,  691200,  691200,  768000,  793600,  793600,  793600,  793600,  793600,  793600,  793600,  793600),
979         CORE_DVFS("cbus",               0, 2, 1, KHZ,    384000,  384000,  588800,  678400,  691200,  691200,  768000,  793600,  793600,  793600,  793600,  793600,  793600,  793600,  793600),
980
981         CORE_DVFS("adsp_bus",           0, 2, 1, KHZ,    281600,  281600,  499200,  576000,  652800,  691200,  755200,  793600,  844800,  844800,  844800,  844800,  844800,  844800,  844800),
982         CORE_DVFS("ape",                0, 2, 1, KHZ,    230400,  230400,  358400,  396800,  422400,  486400,  499200,  499200,  499200,  499200,  499200,  499200,  499200,  499200,  499200),
983
984         CORE_DVFS("sbus",               0, 2, 0, KHZ,    204800,  204800,  307200,  332800,  371200,  384000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000),
985         CORE_DVFS("host1x",             0, 2, 1, KHZ,    128000,  128000,  217600,  345600,  384000,  384000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000),
986         CORE_DVFS("tsec",               0, 2, 1, KHZ,    345600,  345600,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000),
987         CORE_DVFS("mselect",            0, 2, 1, KHZ,    204000,  204000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000,  408000),
988
989         CORE_DVFS("disp1",              0, 2, 0, KHZ,    345600,  345600,  486400,  524800,  563200,  576000,  627200,  640000,  665600,  665600,  665600,  665600,  665600,  665600,  665600),
990         CORE_DVFS("disp2",              0, 2, 0, KHZ,    345600,  345600,  486400,  524800,  563200,  576000,  627200,  640000,  665600,  665600,  665600,  665600,  665600,  665600,  665600),
991
992 /* Common Tables */
993         /* Core voltages(mV):                               800,     825,     850,     875,     900,     925,     950,     975,    1000,    1025,    1050,    1062,    1075,    1100,    1125 */
994         CORE_DVFS("pll_a",              -1, -1, 1, KHZ, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000),
995         CORE_DVFS("pll_c",              -1, -1, 1, KHZ, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000),
996         CORE_DVFS("pll_c2",             -1, -1, 1, KHZ, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000),
997         CORE_DVFS("pll_c3",             -1, -1, 1, KHZ, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000),
998         CORE_DVFS("pll_c4_out0",        -1, -1, 1, KHZ, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000),
999         CORE_DVFS("pll_d_out0",         -1, -1, 1, KHZ, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000),
1000         CORE_DVFS("pll_d2",             -1, -1, 1, KHZ, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000),
1001         CORE_DVFS("pll_dp",             -1, -1, 1, KHZ, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000, 1130000),
1002
1003         /* Core voltages(mV):                               800,     825,     850,     875,     900,     925,     950,     975,    1000,    1025,    1050,    1062,    1075,    1100,    1125 */
1004         CORE_DVFS("csi",                -1, -1, 1, KHZ,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000),
1005         CORE_DVFS("cilab",              -1, -1, 1, KHZ,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000),
1006         CORE_DVFS("cilcd",              -1, -1, 1, KHZ,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000),
1007         CORE_DVFS("cile",               -1, -1, 1, KHZ,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000),
1008
1009         CORE_DVFS("dsia",               -1, -1, 1, KHZ,  500000,  500000,  500000,  500000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000),
1010         CORE_DVFS("dsib",               -1, -1, 1, KHZ,  500000,  500000,  500000,  500000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000,  750000),
1011
1012         CORE_DVFS("dsialp",             -1, -1, 1, KHZ,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000),
1013         CORE_DVFS("dsiblp",             -1, -1, 1, KHZ,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000),
1014
1015         CORE_DVFS("sor0",               -1, -1, 1, KHZ,  162000,  162000,  270000,  270000,  270000,  540000,  540000,  540000,  540000,  540000,  540000,  540000,  540000,  540000,  540000),
1016         CORE_DVFS("sor1",               -1, -1, 1, KHZ,  148500,  148500,  297000,  297000,  297000,  297000,  297000,  594000,  594000,  594000,  594000,  594000,  594000,  594000,  594000),
1017
1018         CORE_DVFS("i2s0",               -1, -1, 1, KHZ,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576),
1019         CORE_DVFS("i2s1",               -1, -1, 1, KHZ,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576),
1020         CORE_DVFS("i2s2",               -1, -1, 1, KHZ,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576),
1021         CORE_DVFS("i2s3",               -1, -1, 1, KHZ,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576),
1022         CORE_DVFS("i2s4",               -1, -1, 1, KHZ,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576,   24576),
1023
1024         CORE_DVFS("d_audio",            -1, -1, 1, KHZ,   49152,   49152,   98304,   98304,   98304,   98304,   98304,   98304,   98304,   98304,   98304,   98304,   98304,   98304,   98304),
1025         CORE_DVFS("spdif_out",          -1, -1, 1, KHZ,   24576,   24576,   24576,   24576,   24576,   24576,   24728,   24728,   24728,   24728,   24728,   24728,   24728,   24728,   24728),
1026         CORE_DVFS("dmic1",              -1, -1, 1, KHZ,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190),
1027         CORE_DVFS("dmic2",              -1, -1, 1, KHZ,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190),
1028         CORE_DVFS("dmic3",              -1, -1, 1, KHZ,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190,   12190),
1029
1030         CORE_DVFS("hda",                -1, -1, 1, KHZ,   51000,   51000,   51000,   51000,   51000,   51000,   51000,   51000,   51000,   51000,   51000,   51000,   51000,   51000,   51000),
1031         CORE_DVFS("hda2codec_2x",       -1, -1, 1, KHZ,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000),
1032
1033         CORE_DVFS("sdmmc2",             -1, -1, 1, KHZ,       1,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1034         CORE_DVFS("sdmmc4",             -1, -1, 1, KHZ,       1,  200000,  266000,  266000,  266000,  266000,  266000,  266000,  266000,  266000,  266000,  266000,  266000,  266000,  266000),
1035         CORE_DVFS("sdmmc2_ddr",         -1, -1, 1, KHZ,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000),
1036         CORE_DVFS("sdmmc4_ddr",         -1, -1, 1, KHZ,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000),
1037
1038         CORE_DVFS("sdmmc1",             -1, -1, 1, KHZ,  136000,  136000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1039         CORE_DVFS("sdmmc3",             -1, -1, 1, KHZ,  136000,  136000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1040
1041         CORE_DVFS("sdmmc1_ddr",         -1, -1, 1, KHZ,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000),
1042         CORE_DVFS("sdmmc3_ddr",         -1, -1, 1, KHZ,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000,   96000),
1043
1044         CORE_DVFS("xusb_falcon_src",    -1, -1, 1, KHZ,       1,       1,  336000,  336000,  336000,  336000,  336000,  336000,  336000,  336000,  336000,  336000,  336000,  336000,  336000),
1045         CORE_DVFS("xusb_host_src",      -1, -1, 1, KHZ,       1,       1,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000),
1046         CORE_DVFS("xusb_dev_src",       -1, -1, 1, KHZ,       1,       1,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000,  112000),
1047         CORE_DVFS("xusb_ssp_src",       -1, -1, 1, KHZ,       1,       1,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000),
1048         CORE_DVFS("xusb_fs_src",        -1, -1, 1, KHZ,       1,       1,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000),
1049         CORE_DVFS("xusb_hs_src",        -1, -1, 1, KHZ,       1,       1,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000,  120000),
1050         CORE_DVFS("usbd",               -1, -1, 1, KHZ,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000),
1051         CORE_DVFS("usb2",               -1, -1, 1, KHZ,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000),
1052         CORE_DVFS("usb3",               -1, -1, 1, KHZ,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000,  480000),
1053
1054         CORE_DVFS("sata",               -1, -1, 1, KHZ,       1,       1,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000,  102000),
1055         CORE_DVFS("sata_oob",           -1, -1, 1, KHZ,       1,       1,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1056         CORE_DVFS("pciex",              -1, -1, 1, KHZ,       1,       1,  500000,  500000,  500000,  500000,  500000,  500000,  500000,  500000,  500000,  500000,  500000,  500000,  500000),
1057
1058         CORE_DVFS("i2c1",               -1, -1, 1, KHZ,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000),
1059         CORE_DVFS("i2c2",               -1, -1, 1, KHZ,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000),
1060         CORE_DVFS("i2c3",               -1, -1, 1, KHZ,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000),
1061         CORE_DVFS("i2c4",               -1, -1, 1, KHZ,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000),
1062         CORE_DVFS("i2c5",               -1, -1, 1, KHZ,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000),
1063         CORE_DVFS("i2c6",               -1, -1, 1, KHZ,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000),
1064         CORE_DVFS("vii2c",              -1, -1, 1, KHZ,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000),
1065
1066         CORE_DVFS("pwm",                -1, -1, 1, KHZ,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000,   48000),
1067
1068         CORE_DVFS("uarta",              -1, -1, 0, KHZ,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1069         CORE_DVFS("uartb",              -1, -1, 0, KHZ,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1070         CORE_DVFS("uartc",              -1, -1, 0, KHZ,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1071         CORE_DVFS("uartd",              -1, -1, 0, KHZ,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1072         CORE_DVFS("uartape",            -1, -1, 0, KHZ,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000,  204000),
1073
1074         CORE_DVFS("soc_therm",          -1, -1, 1, KHZ,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000,  136000),
1075         CORE_DVFS("tsensor",            -1, -1, 1, KHZ,   19200,   19200,   19200,   19200,   19200,   19200,   19200,   19200,   19200,   19200,   19200,   19200,   19200,   19200,   19200),
1076 };
1077
1078 static struct dvfs sor1_dp_dvfs_table[] = {
1079         CORE_DVFS("sor1",               -1, -1, 1, KHZ,  162000,  162000,  270000,  270000,  270000,  540000,  540000,  540000,  540000,  540000,  540000,  540000,  540000,  540000,  540000),
1080 };
1081
1082 static struct dvfs spi_dvfs_table[] = {
1083         CORE_DVFS("sbc1",               -1, -1, 1, KHZ,   12000,   35000,   50000,   50000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000),
1084         CORE_DVFS("sbc2",               -1, -1, 1, KHZ,   12000,   35000,   50000,   50000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000),
1085         CORE_DVFS("sbc3",               -1, -1, 1, KHZ,   12000,   35000,   50000,   50000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000),
1086         CORE_DVFS("sbc4",               -1, -1, 1, KHZ,   12000,   35000,   50000,   50000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000,   65000),
1087
1088 };
1089
1090 static struct dvfs spi_slave_dvfs_table[] = {
1091         CORE_DVFS("sbc1",               -1, -1, 1, KHZ,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000),
1092         CORE_DVFS("sbc2",               -1, -1, 1, KHZ,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000),
1093         CORE_DVFS("sbc3",               -1, -1, 1, KHZ,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000),
1094         CORE_DVFS("sbc4",               -1, -1, 1, KHZ,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000,   45000),
1095
1096 };
1097
1098 static struct dvfs qspi_sdr_dvfs_table[] = {
1099         CORE_DVFS("qspi",               -1, -1, 1, KHZ,   81600,   81600,  116600,  116600,  116600,  116600,  116600,  116600,  116600,  116600,  116600,  116600,  116600,  116600,  116600),
1100 };
1101
1102 static struct dvfs qspi_ddr_dvfs_table[] = {
1103         CORE_DVFS("qspi",               -1, -1, 1, KHZ,   81600,   81600,   81600,   81600,   81600,   81600,   81600,   81600,   81600,   81600,   81600,   81600,   81600,   81600,   81600),
1104 };
1105
1106 static int tegra_dvfs_disable_core_set(const char *arg,
1107         const struct kernel_param *kp)
1108 {
1109         int ret;
1110
1111         ret = param_set_bool(arg, kp);
1112         if (ret)
1113                 return ret;
1114
1115         if (tegra_dvfs_core_disabled)
1116                 tegra_dvfs_rail_disable(&tegra21_dvfs_rail_vdd_core);
1117         else
1118                 tegra_dvfs_rail_enable(&tegra21_dvfs_rail_vdd_core);
1119
1120         return 0;
1121 }
1122
1123 static int tegra_dvfs_disable_cpu_set(const char *arg,
1124         const struct kernel_param *kp)
1125 {
1126         int ret;
1127
1128         ret = param_set_bool(arg, kp);
1129         if (ret)
1130                 return ret;
1131
1132         if (tegra_dvfs_cpu_disabled)
1133                 tegra_dvfs_rail_disable(&tegra21_dvfs_rail_vdd_cpu);
1134         else
1135                 tegra_dvfs_rail_enable(&tegra21_dvfs_rail_vdd_cpu);
1136
1137         return 0;
1138 }
1139
1140 static int tegra_dvfs_disable_gpu_set(const char *arg,
1141         const struct kernel_param *kp)
1142 {
1143         int ret;
1144
1145         ret = param_set_bool(arg, kp);
1146         if (ret)
1147                 return ret;
1148
1149         if (tegra_dvfs_gpu_disabled)
1150                 tegra_dvfs_rail_disable(&tegra21_dvfs_rail_vdd_gpu);
1151         else
1152                 tegra_dvfs_rail_enable(&tegra21_dvfs_rail_vdd_gpu);
1153
1154         return 0;
1155 }
1156
1157 static int tegra_dvfs_disable_get(char *buffer, const struct kernel_param *kp)
1158 {
1159         return param_get_bool(buffer, kp);
1160 }
1161
1162 static struct kernel_param_ops tegra_dvfs_disable_core_ops = {
1163         .set = tegra_dvfs_disable_core_set,
1164         .get = tegra_dvfs_disable_get,
1165 };
1166
1167 static struct kernel_param_ops tegra_dvfs_disable_cpu_ops = {
1168         .set = tegra_dvfs_disable_cpu_set,
1169         .get = tegra_dvfs_disable_get,
1170 };
1171
1172 static struct kernel_param_ops tegra_dvfs_disable_gpu_ops = {
1173         .set = tegra_dvfs_disable_gpu_set,
1174         .get = tegra_dvfs_disable_get,
1175 };
1176
1177 module_param_cb(disable_core, &tegra_dvfs_disable_core_ops,
1178         &tegra_dvfs_core_disabled, 0644);
1179 module_param_cb(disable_cpu, &tegra_dvfs_disable_cpu_ops,
1180         &tegra_dvfs_cpu_disabled, 0644);
1181 module_param_cb(disable_gpu, &tegra_dvfs_disable_gpu_ops,
1182         &tegra_dvfs_gpu_disabled, 0644);
1183
1184 static bool __init match_dvfs_one(const char *name,
1185         int dvfs_speedo_id, int dvfs_process_id,
1186         int speedo_id, int process_id)
1187 {
1188         if ((dvfs_process_id != -1 && dvfs_process_id != process_id) ||
1189                 (dvfs_speedo_id != -1 && dvfs_speedo_id != speedo_id)) {
1190                 pr_debug("tegra21_dvfs: rejected %s speedo %d, process %d\n",
1191                          name, dvfs_speedo_id, dvfs_process_id);
1192                 return false;
1193         }
1194         return true;
1195 }
1196
1197 /* cvb_mv = ((c2 * speedo / s_scale + c1) * speedo / s_scale + c0) / v_scale */
1198 static inline int get_cvb_voltage(int speedo, int s_scale,
1199                                   struct cvb_dvfs_parameters *cvb)
1200 {
1201         /* apply only speedo scale: output mv = cvb_mv * v_scale */
1202         int mv;
1203         mv = DIV_ROUND_CLOSEST(cvb->c2 * speedo, s_scale);
1204         mv = DIV_ROUND_CLOSEST((mv + cvb->c1) * speedo, s_scale) + cvb->c0;
1205         return mv;
1206 }
1207
1208 /* cvb_t_mv =
1209    ((c3 * speedo / s_scale + c4 + c5 * T / t_scale) * T / t_scale) / v_scale */
1210 static inline int get_cvb_t_voltage(int speedo, int s_scale, int t, int t_scale,
1211                                     struct cvb_dvfs_parameters *cvb)
1212 {
1213         /* apply speedo & temperature scales: output mv = cvb_t_mv * v_scale */
1214         int mv;
1215         mv = DIV_ROUND_CLOSEST(cvb->c3 * speedo, s_scale) + cvb->c4 +
1216                 DIV_ROUND_CLOSEST(cvb->c5 * t, t_scale);
1217         mv = DIV_ROUND_CLOSEST(mv * t, t_scale);
1218         return mv;
1219 }
1220
1221 static int round_cvb_voltage(int mv, int v_scale, struct rail_alignment *align)
1222 {
1223         /* combined: apply voltage scale and round to cvb alignment step */
1224         int uv;
1225         int step = (align->step_uv ? : 1000) * v_scale;
1226         int offset = align->offset_uv * v_scale;
1227
1228         uv = max(mv * 1000, offset) - offset;
1229         uv = DIV_ROUND_UP(uv, step) * align->step_uv + align->offset_uv;
1230         return uv / 1000;
1231 }
1232
1233 static int round_voltage(int mv, struct rail_alignment *align, bool up)
1234 {
1235         if (align->step_uv) {
1236                 int uv = max(mv * 1000, align->offset_uv) - align->offset_uv;
1237                 uv = (uv + (up ? align->step_uv - 1 : 0)) / align->step_uv;
1238                 return (uv * align->step_uv + align->offset_uv) / 1000;
1239         }
1240         return mv;
1241 }
1242
1243 /* Setup CPU clusters tables */
1244
1245 /*
1246  * Setup fast CPU DVFS tables in PLL and DFLL modes from CVB data, determine
1247  * nominal voltage for CPU rail, and CPU maximum frequency. Note that entire
1248  * frequency range is guaranteed only when DFLL is used as CPU clock source.
1249  * Reaching maximum frequency on PLL may not be possible within nominal voltage
1250  * range (DVFS core would fail frequency request in this case, so that voltage
1251  * limit is not violated). Error when CPU DVFS table can not be constructed must
1252  * never happen.
1253  */
1254 static int __init set_cpu_dvfs_data(unsigned long max_freq,
1255         struct cpu_cvb_dvfs *d, struct dvfs *cpu_dvfs, int *max_freq_index)
1256 {
1257         int j, mv, min_mv, dfll_mv, min_dfll_mv;
1258         unsigned long fmax_at_vmin = 0;
1259         unsigned long fmax_pll_mode = 0;
1260         unsigned long fmin_use_dfll = 0;
1261         int speedo = tegra_cpu_speedo_value();
1262
1263         struct cvb_dvfs_table *table = NULL;
1264         struct dvfs_rail *rail = &tegra21_dvfs_rail_vdd_cpu;
1265         struct rail_alignment *align = &rail->alignment;
1266
1267         min_dfll_mv = d->dfll_tune_data.min_millivolts;
1268         if (min_dfll_mv < rail->min_millivolts) {
1269                 pr_debug("tegra21_dvfs: dfll min %dmV below rail min %dmV\n",
1270                      min_dfll_mv, rail->min_millivolts);
1271                 min_dfll_mv = rail->min_millivolts;
1272         }
1273         min_dfll_mv =  round_voltage(min_dfll_mv, align, true);
1274
1275         min_mv = d->pll_tune_data.min_millivolts;
1276         if (min_mv < rail->min_millivolts) {
1277                 pr_debug("tegra21_dvfs: pll min %dmV below rail min %dmV\n",
1278                      min_mv, rail->min_millivolts);
1279                 min_mv = rail->min_millivolts;
1280         }
1281         min_mv =  round_voltage(min_mv, align, true);
1282
1283         d->max_mv = round_voltage(d->max_mv, align, false);
1284         BUG_ON(d->max_mv > rail->max_millivolts);
1285
1286         /*
1287          * Use CVB table to fill in CPU dvfs frequencies and voltages. Each
1288          * CVB entry specifies CPU frequency and CVB coefficients to calculate
1289          * the respective voltage when either DFLL or PLL is used as CPU clock
1290          * source.
1291          *
1292          * Different minimum voltage limits are applied to DFLL and PLL sources.
1293          * Same maximum voltage limit is used for both sources, but differently:
1294          * directly limit voltage for DFLL, and limit maximum frequency for PLL.
1295          */
1296         for (j = 0; j < MAX_DVFS_FREQS; j++) {
1297                 table = &d->cvb_table[j];
1298                 if (!table->freq || (table->freq > max_freq))
1299                         break;
1300
1301                 dfll_mv = get_cvb_voltage(
1302                         speedo, d->speedo_scale, &table->cvb_dfll_param);
1303                 dfll_mv = round_cvb_voltage(dfll_mv, d->voltage_scale, align);
1304                 dfll_mv = max(dfll_mv, min_dfll_mv);
1305
1306                 mv = get_cvb_voltage(
1307                         speedo, d->speedo_scale, &table->cvb_pll_param);
1308 #ifdef BRINGUP_CVB_V_MARGIN
1309                 mv = mv * (100 + BRINGUP_CVB_V_MARGIN +
1310                            BRINGUP_CVB_V_MARGIN_EX) / 100;
1311 #endif
1312                 mv = round_cvb_voltage(mv, d->voltage_scale, align);
1313                 mv = max(mv, min_mv);
1314
1315                 /*
1316                  * Check maximum frequency at minimum voltage for dfll source;
1317                  * round down unless all table entries are above Vmin, then use
1318                  * the 1st entry as is.
1319                  */
1320                 if (dfll_mv > min_dfll_mv) {
1321                         if (!j)
1322                                 fmax_at_vmin = table->freq;
1323                         if (!fmax_at_vmin)
1324                                 fmax_at_vmin = cpu_dvfs->freqs[j - 1];
1325                 }
1326
1327                 /* Clip maximum frequency at maximum voltage for pll source */
1328                 if (mv > d->max_mv) {
1329                         if (!j)
1330                                 break;  /* 1st entry already above Vmax */
1331                         if (!fmax_pll_mode)
1332                                 fmax_pll_mode = cpu_dvfs->freqs[j - 1];
1333                 }
1334
1335                 /* Minimum rate with pll source voltage above dfll Vmin */
1336                 if ((mv >= min_dfll_mv) && (!fmin_use_dfll))
1337                         fmin_use_dfll = table->freq;
1338
1339                 /* fill in dvfs tables */
1340                 cpu_dvfs->freqs[j] = table->freq;
1341                 cpu_dfll_millivolts[j] = min(dfll_mv, d->max_mv);
1342                 cpu_millivolts[j] = mv;
1343         }
1344
1345         /* Table must not be empty, must have at least one entry above Vmin */
1346         if (!j || !fmax_at_vmin) {
1347                 pr_err("tegra21_dvfs: invalid cpu dvfs table\n");
1348                 return -ENOENT;
1349         }
1350
1351         /* In the dfll operating range dfll voltage at any rate should be
1352            better (below) than pll voltage */
1353         if (!fmin_use_dfll || (fmin_use_dfll > fmax_at_vmin)) {
1354                 WARN(1, "tegra21_dvfs: pll voltage is below dfll in the dfll"
1355                         " operating range\n");
1356                 fmin_use_dfll = fmax_at_vmin;
1357         }
1358
1359         /* dvfs tables are successfully populated - fill in the rest */
1360         cpu_dvfs->speedo_id = d->speedo_id;
1361         cpu_dvfs->boost_table = max_freq > CPU_BOOST_THRESHOLD;
1362         cpu_dvfs->process_id = d->process_id;
1363         cpu_dvfs->freqs_mult = d->freqs_mult;
1364         cpu_dvfs->dvfs_rail->nominal_millivolts = min(d->max_mv,
1365                 max(cpu_millivolts[j - 1], cpu_dfll_millivolts[j - 1]));
1366         *max_freq_index = j - 1;
1367
1368         cpu_dvfs->dfll_data = d->dfll_tune_data;
1369         cpu_dvfs->dfll_data.max_rate_boost = fmax_pll_mode ?
1370                 (cpu_dvfs->freqs[j - 1] - fmax_pll_mode) * d->freqs_mult : 0;
1371         cpu_dvfs->dfll_data.out_rate_min = fmax_at_vmin * d->freqs_mult;
1372         cpu_dvfs->dfll_data.use_dfll_rate_min = fmin_use_dfll * d->freqs_mult;
1373         cpu_dvfs->dfll_data.min_millivolts = min_dfll_mv;
1374
1375         if (tegra_dfll_boot_req_khz()) {
1376                 /* If boot on DFLL, rail is already under DFLL control */
1377                 cpu_dvfs->dfll_data.dfll_boot_khz = tegra_dfll_boot_req_khz();
1378                 rail->dfll_mode = true;
1379         }
1380
1381         return 0;
1382 }
1383
1384 static void __init init_cpu_dvfs_table(int *cpu_max_freq_index)
1385 {
1386         int i, ret;
1387         int cpu_speedo_id = tegra_cpu_speedo_id();
1388         int cpu_process_id = tegra_cpu_process_id();
1389
1390         for (ret = 0, i = 0; i <  ARRAY_SIZE(cpu_cvb_dvfs_table); i++) {
1391                 struct cpu_cvb_dvfs *d = &cpu_cvb_dvfs_table[i];
1392                 unsigned long max_freq = d->max_freq;
1393                 if (match_dvfs_one("cpu cvb", d->speedo_id, d->process_id,
1394                                    cpu_speedo_id, cpu_process_id)) {
1395                         ret = set_cpu_dvfs_data(max_freq,
1396                                 d, &cpu_dvfs, cpu_max_freq_index);
1397                         break;
1398                 }
1399         }
1400         BUG_ON((i == ARRAY_SIZE(cpu_cvb_dvfs_table)) || ret);
1401 }
1402
1403 /*
1404  * Setup slow CPU (a.k.a LP CPU) DVFS table from CVB data. Only PLL is used as
1405  * a clock source for slow CPU. Its maximum frequency must be reached within
1406  * nominal voltage -- CVB frequency list is cut off at rate that exceeds either
1407  * sku-based maximum limit or requires voltage above nominal. Error when DVFS
1408  * table can not be constructed must never happen.
1409  *
1410  * Final CPU rail nominal voltage is set as maximum of fast and slow CPUs
1411  * nominal voltages.
1412  */
1413 static int __init set_cpu_lp_dvfs_data(unsigned long max_freq,
1414         struct cpu_cvb_dvfs *d, struct dvfs *cpu_lp_dvfs, int *max_freq_index)
1415 {
1416         int j, mv, min_mv;
1417         int speedo = tegra_cpu_speedo_value(); /* FIXME cpu_lp_speedo */
1418
1419         struct cvb_dvfs_table *table = NULL;
1420         struct dvfs_rail *rail = &tegra21_dvfs_rail_vdd_cpu;
1421         struct rail_alignment *align = &rail->alignment;
1422
1423         min_mv = d->pll_tune_data.min_millivolts;
1424         if (min_mv < rail->min_millivolts) {
1425                 pr_debug("tegra21_dvfs: scpu min %dmV below rail min %dmV\n",
1426                      min_mv, rail->min_millivolts);
1427                 min_mv = rail->min_millivolts;
1428         }
1429         min_mv =  round_voltage(min_mv, align, true);
1430
1431         d->max_mv = round_voltage(d->max_mv, align, false);
1432         BUG_ON(d->max_mv > rail->max_millivolts);
1433         rail->nominal_millivolts = max(rail->nominal_millivolts, d->max_mv);
1434
1435         /*
1436          * Use CVB table to fill in CPU dvfs frequencies and voltages. Each
1437          * CVB entry specifies CPU frequency and CVB coefficients to calculate
1438          * the respective voltage. Only PLL is used as CPU LP clock source, and
1439          * the respective minimum limit is applied to each table entry. Table
1440          * construction is aborted if calculated voltage is above maximum limit.
1441          */
1442         for (j = 0; j < MAX_DVFS_FREQS; j++) {
1443                 table = &d->cvb_table[j];
1444                 if (!table->freq || (table->freq > max_freq))
1445                         break;
1446
1447                 mv = get_cvb_voltage(
1448                         speedo, d->speedo_scale, &table->cvb_pll_param);
1449                 mv = round_cvb_voltage(mv, d->voltage_scale, align);
1450                 mv = max(mv, min_mv);
1451
1452                 if (mv > d->max_mv) {
1453                         pr_warn("tegra21_dvfs: %dmV for %s rate %lu above limit %dmV\n",
1454                              mv, cpu_lp_dvfs->clk_name, table->freq, d->max_mv);
1455                         break;
1456                 }
1457
1458                 /* fill in dvfs tables */
1459                 cpu_lp_dvfs->freqs[j] = table->freq;
1460                 cpu_lp_millivolts[j] = mv;
1461         }
1462
1463         /* Table must not be empty */
1464         if (!j) {
1465                 pr_err("tegra21_dvfs: invalid cpu lp dvfs table\n");
1466                 return -ENOENT;
1467         }
1468
1469         /* dvfs tables are successfully populated - fill in the rest */
1470         cpu_lp_dvfs->speedo_id = d->speedo_id;
1471         cpu_lp_dvfs->process_id = d->process_id;
1472         cpu_lp_dvfs->freqs_mult = d->freqs_mult;
1473         *max_freq_index = j - 1;
1474
1475         return 0;
1476 }
1477
1478 static void __init init_cpu_lp_dvfs_table(int *cpu_lp_max_freq_index)
1479 {
1480         int i, ret;
1481         int cpu_lp_speedo_id = tegra_cpu_speedo_id(); /* FIXME cpu_lp_ */
1482         int cpu_lp_process_id = tegra_cpu_process_id(); /* FIXME cpu_lp_ */
1483
1484         for (ret = 0, i = 0; i <  ARRAY_SIZE(cpu_lp_cvb_dvfs_table); i++) {
1485                 struct cpu_cvb_dvfs *d = &cpu_lp_cvb_dvfs_table[i];
1486                 unsigned long max_freq = d->max_freq;
1487                 if (match_dvfs_one("cpu lp cvb", d->speedo_id, d->process_id,
1488                                    cpu_lp_speedo_id, cpu_lp_process_id)) {
1489                         ret = set_cpu_lp_dvfs_data(max_freq,
1490                                 d, &cpu_lp_dvfs, cpu_lp_max_freq_index);
1491                         break;
1492                 }
1493         }
1494         BUG_ON((i == ARRAY_SIZE(cpu_lp_cvb_dvfs_table)) || ret);
1495 }
1496
1497 /*
1498  * Common for both CPU clusters: initialize thermal profiles, and register
1499  * Vmax cooling device.
1500  */
1501 static int __init init_cpu_rail_thermal_profile(struct dvfs *cpu_dvfs)
1502 {
1503         struct dvfs_rail *rail = &tegra21_dvfs_rail_vdd_cpu;
1504
1505         /*
1506          * Failure to get/configure trips may not be fatal for boot - let it
1507          * boot, even with partial configuration with appropriate WARNING, and
1508          * invalidate cdev. It must not happen with production DT, of course.
1509          */
1510         if (rail->vmin_cdev) {
1511                 if (tegra_dvfs_rail_of_init_vmin_thermal_profile(
1512                         vdd_cpu_vmin_trips_table, vdd_cpu_therm_floors_table,
1513                         rail, &cpu_dvfs->dfll_data))
1514                         rail->vmin_cdev = NULL;
1515         }
1516
1517         if (rail->vmax_cdev) {
1518                 if (tegra_dvfs_rail_of_init_vmax_thermal_profile(
1519                         vdd_cpu_vmax_trips_table, vdd_cpu_therm_caps_table,
1520                         rail, &cpu_dvfs->dfll_data))
1521                         rail->vmax_cdev = NULL;
1522         }
1523
1524         return 0;
1525 }
1526
1527 /*
1528  * CPU Vmax cooling device registration for pll mode:
1529  * - Use CPU capping method provided by CPUFREQ platform driver
1530  * - Skip registration if most aggressive cap is at/above maximum voltage
1531  */
1532 static int __init tegra21_dvfs_register_cpu_vmax_cdev(void)
1533 {
1534         struct dvfs_rail *rail;
1535
1536         rail = &tegra21_dvfs_rail_vdd_cpu;
1537         rail->apply_vmax_cap = tegra_cpu_volt_cap_apply;
1538         if (rail->vmax_cdev) {
1539                 int i = rail->vmax_cdev->trip_temperatures_num;
1540                 if (i && rail->therm_mv_caps[i-1] < rail->nominal_millivolts)
1541                         tegra_dvfs_rail_register_vmax_cdev(rail);
1542         }
1543         return 0;
1544 }
1545 late_initcall(tegra21_dvfs_register_cpu_vmax_cdev);
1546
1547
1548 /* Setup GPU tables */
1549
1550 /*
1551  * Find maximum GPU frequency that can be reached at minimum voltage across all
1552  * temperature ranges.
1553  */
1554 static unsigned long __init find_gpu_fmax_at_vmin(
1555         struct dvfs *gpu_dvfs, int thermal_ranges, int freqs_num)
1556 {
1557         int i, j;
1558         unsigned long fmax = ULONG_MAX;
1559
1560         /*
1561          * For voltage scaling row in each temperature range, as well as peak
1562          * voltage row find maximum frequency at lowest voltage, and return
1563          * minimax. On Tegra21 all GPU DVFS thermal dependencies are integrated
1564          * into thermal DVFS table (i.e., there is no separate thermal floors
1565          * applied in the rail level). Hence, returned frequency specifies max
1566          * frequency safe at minimum voltage across all temperature ranges.
1567          */
1568         for (j = 0; j < thermal_ranges; j++) {
1569                 for (i = 1; i < freqs_num; i++) {
1570                         if (gpu_millivolts[j][i] > gpu_millivolts[j][0])
1571                                 break;
1572                 }
1573                 fmax = min(fmax, gpu_dvfs->freqs[i - 1]);
1574         }
1575
1576         for (i = 1; i < freqs_num; i++) {
1577                 if (gpu_peak_millivolts[i] > gpu_peak_millivolts[0])
1578                         break;
1579         }
1580         fmax = min(fmax, gpu_dvfs->freqs[i - 1]);
1581
1582         return fmax;
1583 }
1584
1585 /*
1586  * Determine minimum voltage safe at maximum frequency across all temperature
1587  * ranges.
1588  */
1589 static int __init find_gpu_vmin_at_fmax(
1590         struct dvfs *gpu_dvfs, int thermal_ranges, int freqs_num)
1591 {
1592         int j, vmin;
1593
1594         /*
1595          * For voltage scaling row in each temperature range find minimum
1596          * voltage at maximum frequency and return max Vmin across ranges.
1597          */
1598         for (vmin = 0, j = 0; j < thermal_ranges; j++)
1599                 vmin = max(vmin, gpu_millivolts[j][freqs_num-1]);
1600
1601         return vmin;
1602 }
1603
1604 /*
1605  * Init thermal scaling trips, find number of thermal ranges; note that the 1st
1606  * trip-point is used for voltage calculations within the lowest range, but
1607  * should not be actually set. Hence, at least 2 scaling trip-points must be
1608  * specified in DT; number of scaling ranges = number of trips in DT; number
1609  * of scaling trips bound to scaling cdev is number of trips in DT minus one.
1610  *
1611  * Failure to get/configure trips may not be fatal for boot - let it try,
1612  * anyway, with appropriate WARNING. It must not happen with production DT, of
1613  * course.
1614  */
1615 static int __init init_gpu_rail_thermal_scaling(struct dvfs_rail *rail,
1616                                                 struct gpu_cvb_dvfs *d)
1617 {
1618         int thermal_ranges = 1; /* No thermal depndencies */
1619
1620         if (!rail->vts_cdev)
1621                 return 1;
1622
1623         thermal_ranges = of_tegra_dvfs_rail_get_cdev_trips(
1624                 rail->vts_cdev, d->vts_trips_table, d->therm_floors_table,
1625                 &rail->alignment, true);
1626
1627         if (thermal_ranges < 0) {
1628                 WARN(1, "tegra21_dvfs: %s: failed to get trips from DT\n",
1629                      rail->reg_id);
1630                 return 1;
1631         }
1632
1633         if (thermal_ranges < 2) {
1634                 WARN(1, "tegra21_dvfs: %s: only %d trip (must be at least 2)\n",
1635                      rail->reg_id, thermal_ranges);
1636                 return 1;
1637         }
1638
1639         rail->vts_cdev->trip_temperatures_num = thermal_ranges - 1;
1640         rail->vts_cdev->trip_temperatures = d->vts_trips_table;
1641         return thermal_ranges;
1642 }
1643
1644 /*
1645  * Initialize thermal capping trips and rates: for each cap point (Tk, Vk) find
1646  * min{ maxF(V <= Vk, j), j >= j0 }, where j0 is index for minimum scaling
1647  * trip-point above Tk with margin: j0 = min{ j, Tj >= Tk - margin }.
1648  */
1649 #define CAP_TRIP_ON_SCALING_MARGIN      5
1650 static void __init init_gpu_cap_rates(struct dvfs *gpu_dvfs,
1651         struct dvfs_rail *rail, int thermal_ranges, int freqs_num)
1652 {
1653         int i, j, k;
1654
1655         for (k = 0; k < rail->vmax_cdev->trip_temperatures_num; k++) {
1656                 int cap_tempr = vdd_gpu_vmax_trips_table[k];
1657                 int cap_level = vdd_gpu_therm_caps_table[k];
1658                 unsigned long cap_freq = clk_get_max_rate(vgpu_cap_clk);
1659
1660                 for (j = 0; j < thermal_ranges; j++) {
1661                         if ((j < thermal_ranges - 1) && /* vts trips=ranges-1 */
1662                             (rail->vts_cdev->trip_temperatures[j] +
1663                             CAP_TRIP_ON_SCALING_MARGIN < cap_tempr))
1664                                 continue;
1665
1666                         for (i = 1; i < freqs_num; i++) {
1667                                 if (gpu_millivolts[j][i] > cap_level)
1668                                         break;
1669                         }
1670                         cap_freq = min(cap_freq, gpu_dvfs->freqs[i - 1]);
1671                 }
1672                 gpu_cap_rates[k] = cap_freq * gpu_dvfs->freqs_mult;
1673         }
1674 }
1675
1676 static int __init init_gpu_rail_thermal_caps(struct dvfs *gpu_dvfs,
1677         struct dvfs_rail *rail, int thermal_ranges, int freqs_num)
1678 {
1679         const char *cap_clk_name = "cap.vgpu.gbus";
1680
1681         if (!rail->vmax_cdev)
1682                 return 0;
1683
1684         vgpu_cap_clk = tegra_get_clock_by_name(cap_clk_name);
1685         if (!vgpu_cap_clk) {
1686                 WARN(1, "tegra21_dvfs: %s: failed to get cap clock %s\n",
1687                      rail->reg_id, cap_clk_name);
1688                 goto err_out;
1689         }
1690
1691         if (tegra_dvfs_rail_of_init_vmax_thermal_profile(
1692                 vdd_gpu_vmax_trips_table, vdd_gpu_therm_caps_table, rail, NULL))
1693                 goto err_out;
1694
1695         if (rail->vts_cdev)
1696                 init_gpu_cap_rates(gpu_dvfs, rail, thermal_ranges, freqs_num);
1697         return 0;
1698
1699 err_out:
1700         rail->vmax_cdev = NULL;
1701         return -ENODEV;
1702 }
1703
1704 /*
1705  * Setup gpu dvfs tables from cvb data, determine nominal voltage for gpu rail,
1706  * and gpu maximum frequency. Error when gpu dvfs table can not be constructed
1707  * must never happen.
1708  */
1709 static int __init set_gpu_dvfs_data(unsigned long max_freq,
1710         struct gpu_cvb_dvfs *d, struct dvfs *gpu_dvfs, int *max_freq_index)
1711 {
1712         int i, j, thermal_ranges, mv, min_mv;
1713         struct cvb_dvfs_table *table = NULL;
1714         int speedo = tegra_gpu_speedo_value();
1715         struct dvfs_rail *rail = &tegra21_dvfs_rail_vdd_gpu;
1716         struct rail_alignment *align = &rail->alignment;
1717
1718         d->max_mv = round_voltage(d->max_mv, align, false);
1719         min_mv = d->pll_tune_data.min_millivolts;
1720         if (min_mv < rail->min_millivolts) {
1721                 pr_debug("tegra21_dvfs: gpu min %dmV below rail min %dmV\n",
1722                          min_mv, rail->min_millivolts);
1723                 min_mv = rail->min_millivolts;
1724         }
1725
1726         /*
1727          * Get scaling thermal ranges; 1 range implies no thermal dependency.
1728          * Invalidate scaling cooling device in the latter case.
1729          */
1730         thermal_ranges = init_gpu_rail_thermal_scaling(rail, d);
1731         if (thermal_ranges == 1)
1732                 rail->vts_cdev = NULL;
1733
1734         /*
1735          * Apply fixed thermal floor for each temperature range
1736          */
1737         for (j = 0; j < thermal_ranges; j++) {
1738                 mv = max(min_mv, d->therm_floors_table[j]);
1739                 gpu_vmin[j] = round_voltage(mv, align, true);
1740         }
1741
1742         /*
1743          * Use CVB table to fill in gpu dvfs frequencies and voltages. Each
1744          * CVB entry specifies gpu frequency and CVB coefficients to calculate
1745          * the respective voltage.
1746          */
1747         for (i = 0; i < MAX_DVFS_FREQS; i++) {
1748                 table = &d->cvb_table[i];
1749                 if (!table->freq || (table->freq > max_freq))
1750                         break;
1751
1752                 mv = get_cvb_voltage(
1753                         speedo, d->speedo_scale, &table->cvb_pll_param);
1754                 for (j = 0; j < thermal_ranges; j++) {
1755                         int mvj = mv;
1756                         int t = thermal_ranges == 1 ? 0 :
1757                                 rail->vts_cdev->trip_temperatures[j];
1758
1759                         /* get thermal offset for this trip-point */
1760                         mvj += get_cvb_t_voltage(speedo, d->speedo_scale,
1761                                 t, d->thermal_scale, &table->cvb_pll_param);
1762                         mvj = round_cvb_voltage(mvj, d->voltage_scale, align);
1763
1764                         /* clip to minimum, abort if above maximum */
1765                         mvj = max(mvj, gpu_vmin[j]);
1766                         if (mvj > d->max_mv)
1767                                 break;
1768
1769                         /*
1770                          * Update voltage for adjacent ranges bounded by this
1771                          * trip-point (cvb & dvfs are transpose matrices, and
1772                          * cvb freq row index is column index for dvfs matrix)
1773                          */
1774                         gpu_millivolts[j][i] = mvj;
1775                         if (j && (gpu_millivolts[j-1][i] < mvj))
1776                                 gpu_millivolts[j-1][i] = mvj;
1777                 }
1778                 /* Make sure all voltages for this frequency are below max */
1779                 if (j < thermal_ranges)
1780                         break;
1781
1782                 /* fill in gpu dvfs tables */
1783                 gpu_dvfs->freqs[i] = table->freq;
1784         }
1785
1786         /*
1787          * Table must not be empty, must have at least one entry in range, and
1788          * must specify monotonically increasing voltage on frequency dependency
1789          * in each temperature range.
1790          */
1791         if (!i || tegra_dvfs_init_thermal_dvfs_voltages(&gpu_millivolts[0][0],
1792                 gpu_peak_millivolts, i, thermal_ranges, gpu_dvfs)) {
1793                 pr_err("tegra21_dvfs: invalid gpu dvfs table\n");
1794                 return -ENOENT;
1795         }
1796
1797         /* Shift out the 1st trip-point */
1798         for (j = 1; j < thermal_ranges; j++)
1799                 rail->vts_cdev->trip_temperatures[j - 1] =
1800                 rail->vts_cdev->trip_temperatures[j];
1801
1802         /* dvfs tables are successfully populated - fill in the gpu dvfs */
1803         gpu_dvfs->speedo_id = d->speedo_id;
1804         gpu_dvfs->process_id = d->process_id;
1805         gpu_dvfs->freqs_mult = d->freqs_mult;
1806
1807         *max_freq_index = i - 1;
1808
1809         gpu_dvfs->dvfs_rail->nominal_millivolts = min(d->max_mv,
1810                 find_gpu_vmin_at_fmax(gpu_dvfs, thermal_ranges, i));
1811
1812         gpu_dvfs->fmax_at_vmin_safe_t = d->freqs_mult *
1813                 find_gpu_fmax_at_vmin(gpu_dvfs, thermal_ranges, i);
1814
1815         /* Initialize thermal capping */
1816         init_gpu_rail_thermal_caps(gpu_dvfs, rail, thermal_ranges, i);
1817
1818 #ifdef CONFIG_TEGRA_USE_NA_GPCPLL
1819         /*
1820          * Set NA DVFS flag, if GPCPLL NA mode is enabled. This is necessary to
1821          * make sure that GPCPLL configuration is updated by tegra core DVFS
1822          * when thermal DVFS cooling device state is changed. Since tegra core
1823          * DVFS does not support NA operations for Vmin cooling device, GPU Vmin
1824          * thermal floors have been integrated with thermal DVFS, and no Vmin
1825          * cooling device is installed.
1826          */
1827         if (tegra_fuse_can_use_na_gpcpll())
1828                 gpu_dvfs->na_dvfs = 1;
1829 #endif
1830         return 0;
1831 }
1832
1833 static void __init init_gpu_dvfs_table(int *gpu_max_freq_index)
1834 {
1835         int i, ret;
1836         int gpu_speedo_id = tegra_gpu_speedo_id();
1837         int gpu_process_id = tegra_gpu_process_id();
1838
1839         for (ret = 0, i = 0; i < ARRAY_SIZE(gpu_cvb_dvfs_table); i++) {
1840                 struct gpu_cvb_dvfs *d = &gpu_cvb_dvfs_table[i];
1841                 unsigned long max_freq = d->max_freq;
1842                 if (match_dvfs_one("gpu cvb", d->speedo_id, d->process_id,
1843                                    gpu_speedo_id, gpu_process_id)) {
1844                         ret = set_gpu_dvfs_data(max_freq,
1845                                 d, &gpu_dvfs, gpu_max_freq_index);
1846                         break;
1847                 }
1848         }
1849         BUG_ON((i == ARRAY_SIZE(gpu_cvb_dvfs_table)) || ret);
1850 }
1851
1852 /*
1853  * GPU Vmax cooling device registration:
1854  * - Use Tegra21 GPU capping method that applies pre-populated cap rates
1855  *   adjusted for each voltage cap trip-point (in case when GPU thermal
1856  *   scaling initialization failed, fall back on using WC rate limit across all
1857  *   thermal ranges).
1858  * - Skip registration if most aggressive cap is at/above maximum voltage
1859  */
1860 static int tegra21_gpu_volt_cap_apply(int *cap_idx, int new_idx, int level)
1861 {
1862         int ret = -EINVAL;
1863         unsigned long flags;
1864         unsigned long cap_rate;
1865
1866         if (!cap_idx)
1867                 return 0;
1868
1869         clk_lock_save(vgpu_cap_clk, &flags);
1870         *cap_idx = new_idx;
1871
1872         if (level) {
1873                 if (gpu_dvfs.dvfs_rail->vts_cdev && gpu_dvfs.therm_dvfs)
1874                         cap_rate = gpu_cap_rates[new_idx - 1];
1875                 else
1876                         cap_rate = tegra_dvfs_predict_hz_at_mv_max_tfloor(
1877                                 clk_get_parent(vgpu_cap_clk), level);
1878         } else {
1879                 cap_rate = clk_get_max_rate(vgpu_cap_clk);
1880         }
1881
1882         if (!IS_ERR_VALUE(cap_rate))
1883                 ret = clk_set_rate_locked(vgpu_cap_clk, cap_rate);
1884         else
1885                 pr_err("tegra21_dvfs: Failed to find GPU cap rate for %dmV\n",
1886                         level);
1887
1888         clk_unlock_restore(vgpu_cap_clk, &flags);
1889         return ret;
1890 }
1891
1892 static int __init tegra21_dvfs_register_gpu_vmax_cdev(void)
1893 {
1894         struct dvfs_rail *rail;
1895
1896         rail = &tegra21_dvfs_rail_vdd_gpu;
1897         rail->apply_vmax_cap = tegra21_gpu_volt_cap_apply;
1898         if (rail->vmax_cdev) {
1899                 int i = rail->vmax_cdev->trip_temperatures_num;
1900                 if (i && rail->therm_mv_caps[i-1] < rail->nominal_millivolts)
1901                         tegra_dvfs_rail_register_vmax_cdev(rail);
1902         }
1903         return 0;
1904 }
1905 late_initcall(tegra21_dvfs_register_gpu_vmax_cdev);
1906
1907 /*
1908  * SPI DVFS tables are different in master and in slave mode. Use master tables
1909  * by default. Check if slave mode is specified for enabled SPI devices in DT,
1910  * and overwrite master table for the respective SPI controller.
1911  */
1912
1913 static __initdata struct {
1914         u64 address;
1915         struct dvfs *d;
1916 } spi_map[] = {
1917         { 0x7000d400, &spi_dvfs_table[0] },
1918         { 0x7000d600, &spi_dvfs_table[1] },
1919         { 0x7000d800, &spi_dvfs_table[2] },
1920         { 0x7000da00, &spi_dvfs_table[3] },
1921 };
1922
1923 static int __init of_update_spi_slave_dvfs(struct device_node *dn)
1924 {
1925         int i;
1926         u64 addr = 0;
1927         const __be32 *reg;
1928
1929         if (!of_device_is_available(dn))
1930                 return 0;
1931
1932         reg = of_get_property(dn, "reg", NULL);
1933         if (reg && of_can_translate_address(dn))
1934                 addr = of_translate_address(dn, reg);
1935
1936         for (i = 0; i < ARRAY_SIZE(spi_map); i++) {
1937                 if (spi_map[i].address == addr) {
1938                         spi_map[i].d = &spi_slave_dvfs_table[i];
1939                         break;
1940                 }
1941         }
1942         return 0;
1943 }
1944
1945 static __initdata struct of_device_id tegra21_dvfs_spi_slave_of_match[] = {
1946         { .compatible = "nvidia,tegra210-spi-slave",
1947           .data = of_update_spi_slave_dvfs, },
1948         { },
1949 };
1950
1951 static void __init init_spi_dvfs(int soc_speedo_id, int core_process_id,
1952                                  int core_nominal_mv_index)
1953 {
1954         int i;
1955
1956         of_tegra_dvfs_init(tegra21_dvfs_spi_slave_of_match);
1957
1958         for (i = 0; i <  ARRAY_SIZE(spi_map); i++) {
1959                 struct dvfs *d = spi_map[i].d;
1960                 if (!match_dvfs_one(d->clk_name, d->speedo_id,
1961                         d->process_id, soc_speedo_id, core_process_id))
1962                         continue;
1963                 tegra_init_dvfs_one(d, core_nominal_mv_index);
1964         }
1965 }
1966
1967 /*
1968  * QSPI DVFS tables are different in SDR and DDR modes. Use SDR tables by
1969  * default. Check if DDR mode is specified for enabled QSPI device in DT,
1970  * and overwrite DVFS table, respectively.
1971  */
1972
1973 static __initdata struct dvfs *qspi_dvfs = &qspi_sdr_dvfs_table[0];
1974
1975 static int __init of_update_qspi_dvfs(struct device_node *dn)
1976 {
1977         if (of_device_is_available(dn)) {
1978                 if (of_get_property(dn, "nvidia,x4-is-ddr", NULL))
1979                         qspi_dvfs = &qspi_ddr_dvfs_table[0];
1980         }
1981         return 0;
1982 }
1983
1984 static __initdata struct of_device_id tegra21_dvfs_qspi_of_match[] = {
1985         { .compatible = "nvidia,tegra210-qspi", .data = of_update_qspi_dvfs, },
1986         { },
1987 };
1988
1989 static void __init init_qspi_dvfs(int soc_speedo_id, int core_process_id,
1990                                   int core_nominal_mv_index)
1991 {
1992         int i;
1993         struct device_node *dn;
1994
1995         of_tegra_dvfs_init(tegra21_dvfs_qspi_of_match);
1996
1997         if (match_dvfs_one(qspi_dvfs->clk_name, qspi_dvfs->speedo_id,
1998                 qspi_dvfs->process_id, soc_speedo_id, core_process_id))
1999                 tegra_init_dvfs_one(qspi_dvfs, core_nominal_mv_index);
2000 }
2001
2002 /*
2003  * SOR1 in HDMI and DP modes has different DVFS tables. The HDMI table is
2004  * specified as primary, and DP mode table is installed as alternative one.
2005  */
2006 static void __init init_sor1_dp_dvfs(int soc_speedo_id, int core_process_id)
2007 {
2008         int i;
2009         struct dvfs *alt_d = &sor1_dp_dvfs_table[0];
2010         struct dvfs_rail *rail = &tegra21_dvfs_rail_vdd_core;
2011         struct clk *c = tegra_get_clock_by_name(alt_d->clk_name);
2012
2013         if (!c || !c->dvfs) {
2014                 if (rail->min_millivolts != rail->nominal_millivolts)
2015                         pr_err("tegra21_dvfs: invalid clock %s for alt dvfs\n",
2016                                 alt_d->clk_name);
2017                 return;
2018         }
2019
2020         if (match_dvfs_one(alt_d->clk_name, alt_d->speedo_id, alt_d->process_id,
2021                            soc_speedo_id, core_process_id)) {
2022                 for (i = 0; i < c->dvfs->num_freqs; i++)
2023                         alt_d->freqs[i] *= alt_d->freqs_mult;
2024                 tegra_dvfs_alt_freqs_install_always(c->dvfs, alt_d->freqs);
2025         }
2026 }
2027
2028 /*
2029  * Clip sku-based core nominal voltage to core DVFS voltage ladder
2030  */
2031 static int __init get_core_nominal_mv_index(int speedo_id)
2032 {
2033         int i;
2034         int mv = tegra_core_speedo_mv();
2035         int core_edp_voltage = get_core_edp();
2036
2037         /*
2038          * Start with nominal level for the chips with this speedo_id. Then,
2039          * make sure core nominal voltage is below edp limit for the board
2040          * (if edp limit is set).
2041          */
2042         if (!core_edp_voltage)
2043                 core_edp_voltage = 1125;        /* default 1.125V EDP limit */
2044
2045         mv = min(mv, core_edp_voltage);
2046 #ifndef CONFIG_TEGRA_CORE_DVFS
2047         mv = min(mv, 1000);             /* Vmax if scaling is disabled */
2048 #endif
2049         /* Round nominal level down to the nearest core scaling step */
2050         for (i = 0; i < MAX_DVFS_FREQS; i++) {
2051                 if ((core_millivolts[i] == 0) || (mv < core_millivolts[i]))
2052                         break;
2053         }
2054
2055         if (i == 0) {
2056                 pr_err("tegra21_dvfs: unable to adjust core dvfs table to"
2057                        " nominal voltage %d\n", mv);
2058                 return -ENOSYS;
2059         }
2060         return i - 1;
2061 }
2062
2063 /*
2064  * Clip sku-based core minimum voltage to core DVFS voltage ladder
2065  */
2066 static int __init get_core_minimum_mv_index(void)
2067 {
2068         int i;
2069         int mv = tegra_core_speedo_min_mv();
2070
2071         /*
2072          * Start with minimum level for the chip sku/speedo. Then, make sure it
2073          * is above initial rail minimum, and finally round up to DVFS voltages.
2074          */
2075         mv = max(mv, tegra21_dvfs_rail_vdd_core.min_millivolts);
2076         for (i = 0; i < MAX_DVFS_FREQS - 1; i++) {
2077                 if ((core_millivolts[i+1] == 0) || (mv <= core_millivolts[i]))
2078                         break;
2079         }
2080         return i;
2081 }
2082
2083 static int __init init_core_rail_thermal_profile(void)
2084 {
2085         struct dvfs_rail *rail = &tegra21_dvfs_rail_vdd_core;
2086
2087         /*
2088          * Failure to get/configure trips may not be fatal for boot - let it
2089          * boot, even with partial configuration with appropriate WARNING, and
2090          * invalidate cdev. It must not happen with production DT, of course.
2091          */
2092         if (rail->vmin_cdev) {
2093                 if (tegra_dvfs_rail_of_init_vmin_thermal_profile(
2094                         vdd_core_vmin_trips_table, vdd_core_therm_floors_table,
2095                         rail, NULL))
2096                         rail->vmin_cdev = NULL;
2097         }
2098
2099         if (rail->vmax_cdev) {
2100                 if (tegra_dvfs_rail_of_init_vmax_thermal_profile(
2101                         vdd_core_vmax_trips_table, vdd_core_therm_caps_table,
2102                         rail, NULL))
2103                         rail->vmax_cdev = NULL;
2104         }
2105
2106         return 0;
2107 }
2108
2109 static int __init of_rails_init(struct device_node *dn)
2110 {
2111         int i;
2112
2113         if (!of_device_is_available(dn))
2114                 return 0;
2115
2116         for (i = 0; i < ARRAY_SIZE(tegra21_dvfs_rails); i++) {
2117                 struct dvfs_rail *rail = tegra21_dvfs_rails[i];
2118                 if (!of_tegra_dvfs_rail_node_parse(dn, rail)) {
2119                         rail->stats.bin_uV = rail->alignment.step_uv;
2120                         return 0;
2121                 }
2122         }
2123         return -ENOENT;
2124 }
2125
2126 static __initdata struct of_device_id tegra21_dvfs_rail_of_match[] = {
2127         { .compatible = "nvidia,tegra210-dvfs-rail", .data = of_rails_init, },
2128         { },
2129 };
2130
2131 void __init tegra21x_init_dvfs(void)
2132 {
2133         int soc_speedo_id = tegra_soc_speedo_id();
2134         int core_process_id = tegra_core_process_id();
2135
2136         int i, ret;
2137         int core_nominal_mv_index;
2138         int gpu_max_freq_index = 0;
2139         int cpu_max_freq_index = 0;
2140         int cpu_lp_max_freq_index = 0;
2141
2142 #ifndef CONFIG_TEGRA_CORE_DVFS
2143         tegra_dvfs_core_disabled = true;
2144 #endif
2145 #ifndef CONFIG_TEGRA_CPU_DVFS
2146         tegra_dvfs_cpu_disabled = true;
2147 #endif
2148 #ifndef CONFIG_TEGRA_GPU_DVFS
2149         tegra_dvfs_gpu_disabled = true;
2150 #endif
2151
2152         of_tegra_dvfs_init(tegra21_dvfs_rail_of_match);
2153
2154         /*
2155          * Find nominal and minimum voltages for core rail before rail
2156          * init. Nominal voltage index in core scaling ladder can also be
2157          * used to determine max dvfs frequencies for all core clocks. In
2158          * case of error disable core scaling and set index to 0, so that
2159          * core clocks would not exceed rates allowed at minimum voltage.
2160          */
2161         core_nominal_mv_index = get_core_nominal_mv_index(soc_speedo_id);
2162         if (core_nominal_mv_index < 0) {
2163                 tegra21_dvfs_rail_vdd_core.disabled = true;
2164                 tegra_dvfs_core_disabled = true;
2165                 core_nominal_mv_index = 0;
2166         }
2167         tegra21_dvfs_rail_vdd_core.nominal_millivolts =
2168                 core_millivolts[core_nominal_mv_index];
2169
2170         i = get_core_minimum_mv_index();
2171         BUG_ON(i > core_nominal_mv_index);
2172         tegra21_dvfs_rail_vdd_core.min_millivolts = core_millivolts[i];
2173
2174         /*
2175          * Construct fast and slow CPU DVFS tables from CVB data; find maximum
2176          * frequency, minimum  and nominal voltage for each CPU cluster, and
2177          * combined rail limits (fast CPU should be initialized 1st).
2178          */
2179         init_cpu_dvfs_table(&cpu_max_freq_index);
2180         init_cpu_lp_dvfs_table(&cpu_lp_max_freq_index);
2181
2182         /* Init cpu thermal profile */
2183         init_cpu_rail_thermal_profile(&cpu_dvfs);
2184
2185         /*
2186          * Construct GPU DVFS table from CVB data; find GPU maximum frequency,
2187          * and nominal voltage.
2188          */
2189         init_gpu_dvfs_table(&gpu_max_freq_index);
2190
2191         /* Init core thermal profile */
2192         init_core_rail_thermal_profile();
2193
2194         /* Init rail structures and dependencies */
2195         tegra_dvfs_init_rails(tegra21_dvfs_rails,
2196                 ARRAY_SIZE(tegra21_dvfs_rails));
2197
2198         /* Search core dvfs table for speedo/process matching entries and
2199            initialize dvfs-ed clocks */
2200         if (!tegra_platform_is_linsim()) {
2201                 for (i = 0; i <  ARRAY_SIZE(core_dvfs_table); i++) {
2202                         struct dvfs *d = &core_dvfs_table[i];
2203                         if (!match_dvfs_one(d->clk_name, d->speedo_id,
2204                                 d->process_id, soc_speedo_id, core_process_id))
2205                                 continue;
2206                         tegra_init_dvfs_one(d, core_nominal_mv_index);
2207                 }
2208                 init_spi_dvfs(soc_speedo_id, core_process_id,
2209                               core_nominal_mv_index);
2210                 init_qspi_dvfs(soc_speedo_id, core_process_id,
2211                                core_nominal_mv_index);
2212                 init_sor1_dp_dvfs(soc_speedo_id, core_process_id);
2213         }
2214
2215         /* Initialize matching gpu dvfs entry already found when nominal
2216            voltage was determined */
2217         tegra_init_dvfs_one(&gpu_dvfs, gpu_max_freq_index);
2218
2219         /* Initialize matching cpu dvfs entry already found when nominal
2220            voltage was determined */
2221         tegra_init_dvfs_one(&cpu_dvfs, cpu_max_freq_index);
2222         tegra_init_dvfs_one(&cpu_lp_dvfs, cpu_lp_max_freq_index);
2223
2224         /* Finally disable dvfs on rails if necessary */
2225         if (tegra_dvfs_core_disabled)
2226                 tegra_dvfs_rail_disable(&tegra21_dvfs_rail_vdd_core);
2227         if (tegra_dvfs_cpu_disabled)
2228                 tegra_dvfs_rail_disable(&tegra21_dvfs_rail_vdd_cpu);
2229         if (tegra_dvfs_gpu_disabled)
2230                 tegra_dvfs_rail_disable(&tegra21_dvfs_rail_vdd_gpu);
2231
2232         for (i = 0; i < ARRAY_SIZE(tegra21_dvfs_rails); i++) {
2233                 struct dvfs_rail *rail = tegra21_dvfs_rails[i];
2234                 pr_info("tegra dvfs: %s: nominal %dmV, offset %duV, step %duV, scaling %s\n",
2235                         rail->reg_id, rail->nominal_millivolts,
2236                         rail->alignment.offset_uv, rail->alignment.step_uv,
2237                         rail->disabled ? "disabled" : "enabled");
2238         }
2239 }
2240
2241 int tegra_dvfs_rail_disable_prepare(struct dvfs_rail *rail)
2242 {
2243         return 0;
2244 }
2245
2246 int tegra_dvfs_rail_post_enable(struct dvfs_rail *rail)
2247 {
2248         return 0;
2249 }
2250
2251 #ifdef CONFIG_TEGRA_CORE_VOLT_CAP
2252 /* Core voltage and bus cap object and tables */
2253 static struct kobject *cap_kobj;
2254 static struct kobject *gpu_kobj;
2255
2256 static struct core_dvfs_cap_table tegra21_core_cap_table[] = {
2257         { .cap_name = "cap.vcore.c2bus" },
2258         { .cap_name = "cap.vcore.c3bus" },
2259         { .cap_name = "cap.vcore.cbus" },
2260         { .cap_name = "cap.vcore.sclk" },
2261         { .cap_name = "cap.vcore.emc" },
2262         { .cap_name = "cap.vcore.host1x" },
2263         { .cap_name = "cap.vcore.mselect" },
2264         { .cap_name = "cap.vcore.ape" },
2265         { .cap_name = "cap.vcore.abus" },
2266 };
2267
2268 static struct core_bus_limit_table tegra21_gpu_cap_syfs = {
2269         .limit_clk_name = "cap.profile.gbus",
2270         .refcnt_attr = {.attr = {.name = "gpu_cap_state", .mode = 0644} },
2271         .level_attr  = {.attr = {.name = "gpu_cap_rate", .mode = 0644} },
2272         .pm_qos_class = PM_QOS_GPU_FREQ_MAX,
2273 };
2274
2275 static struct core_bus_limit_table tegra21_gpu_floor_sysfs = {
2276         .limit_clk_name = "floor.profile.gbus",
2277         .refcnt_attr = {.attr = {.name = "gpu_floor_state", .mode = 0644} },
2278         .level_attr  = {.attr = {.name = "gpu_floor_rate", .mode = 0644} },
2279         .pm_qos_class = PM_QOS_GPU_FREQ_MIN,
2280 };
2281
2282 static struct core_bus_rates_table tegra21_gpu_rates_sysfs = {
2283         .bus_clk_name = "gbus",
2284         .rate_attr = {.attr = {.name = "gpu_rate", .mode = 0444} },
2285         .available_rates_attr = {
2286                 .attr = {.name = "gpu_available_rates", .mode = 0444} },
2287         .time_at_user_rate_attr = {
2288                 .attr = {.name = "gpu_time_at_user_rate", .mode = 0444} },
2289 };
2290
2291 /*
2292  * Core Vmax cooling device registration:
2293  * - Use VDD_CORE capping method provided by DVFS
2294  * - Skip registration if most aggressive cap is at/above maximum voltage
2295  */
2296 static void __init tegra21_dvfs_register_core_vmax_cdev(void)
2297 {
2298         struct dvfs_rail *rail;
2299
2300         rail = &tegra21_dvfs_rail_vdd_core;
2301         rail->apply_vmax_cap = tegra_dvfs_therm_vmax_core_cap_apply;
2302         if (rail->vmax_cdev) {
2303                 int i = rail->vmax_cdev->trip_temperatures_num;
2304                 if (i && rail->therm_mv_caps[i-1] < rail->nominal_millivolts)
2305                         tegra_dvfs_rail_register_vmax_cdev(rail);
2306         }
2307 }
2308
2309 /*
2310  * Initialize core capping interfaces. It can happen only after DVFS is ready.
2311  * Therefore this late initcall must be invoked after clock late initcall where
2312  * DVFS is initialized -- assured by the order in Make file. In addition core
2313  * Vmax cooling device operation depends on core cap interface. Hence, register
2314  * core Vmax cooling device here as well.
2315  */
2316 static int __init tegra21_dvfs_init_core_cap(void)
2317 {
2318         int ret = 0;
2319
2320         if (tegra_platform_is_qt())
2321                 return 0;
2322
2323         /* Init core voltage cap interface */
2324         cap_kobj = kobject_create_and_add("tegra_cap", kernel_kobj);
2325         if (!cap_kobj) {
2326                 pr_err("tegra21_dvfs: failed to create sysfs cap object\n");
2327                 return 0;
2328         }
2329
2330         ret = tegra_init_core_cap(tegra21_core_cap_table,
2331                         ARRAY_SIZE(tegra21_core_cap_table),
2332                         core_millivolts, ARRAY_SIZE(core_millivolts), cap_kobj);
2333         if (ret) {
2334                 pr_err("tegra21_dvfs: failed to init core cap interface (%d)\n",
2335                        ret);
2336                 kobject_del(cap_kobj);
2337                 return 0;
2338         }
2339         tegra_core_cap_debug_init();
2340         pr_info("tegra dvfs: tegra sysfs cap interface is initialized\n");
2341
2342         /* Register core Vmax cooling device */
2343         tegra21_dvfs_register_core_vmax_cdev();
2344
2345         /* Init core shared buses rate limit interfaces */
2346         gpu_kobj = kobject_create_and_add("tegra_gpu", kernel_kobj);
2347         if (!gpu_kobj) {
2348                 pr_err("tegra21_dvfs: failed to create sysfs gpu object\n");
2349                 return 0;
2350         }
2351
2352         ret = tegra_init_shared_bus_cap(&tegra21_gpu_cap_syfs,
2353                                         1, gpu_kobj);
2354         if (ret) {
2355                 pr_err("tegra21_dvfs: failed to init gpu cap interface (%d)\n",
2356                        ret);
2357                 kobject_del(gpu_kobj);
2358                 return 0;
2359         }
2360
2361         ret = tegra_init_shared_bus_floor(&tegra21_gpu_floor_sysfs,
2362                                           1, gpu_kobj);
2363         if (ret) {
2364                 pr_err("tegra21_dvfs: failed to init gpu floor interface (%d)\n",
2365                        ret);
2366                 kobject_del(gpu_kobj);
2367                 return 0;
2368         }
2369
2370         /* Init core shared buses rate inforamtion interfaces */
2371         ret = tegra_init_sysfs_shared_bus_rate(&tegra21_gpu_rates_sysfs,
2372                                                1, gpu_kobj);
2373         if (ret) {
2374                 pr_err("tegra21_dvfs: failed to init gpu rates interface (%d)\n",
2375                        ret);
2376                 kobject_del(gpu_kobj);
2377                 return 0;
2378         }
2379
2380         pr_info("tegra dvfs: tegra sysfs gpu interface is initialized\n");
2381
2382         return 0;
2383 }
2384 late_initcall(tegra21_dvfs_init_core_cap);
2385 #endif