diff options
author | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
---|---|---|
committer | Jonathan Herman <hermanjl@cs.unc.edu> | 2013-01-22 10:38:37 -0500 |
commit | fcc9d2e5a6c89d22b8b773a64fb4ad21ac318446 (patch) | |
tree | a57612d1888735a2ec7972891b68c1ac5ec8faea /arch/arm/mach-tegra/dvfs.c | |
parent | 8dea78da5cee153b8af9c07a2745f6c55057fe12 (diff) |
Diffstat (limited to 'arch/arm/mach-tegra/dvfs.c')
-rw-r--r-- | arch/arm/mach-tegra/dvfs.c | 846 |
1 files changed, 846 insertions, 0 deletions
diff --git a/arch/arm/mach-tegra/dvfs.c b/arch/arm/mach-tegra/dvfs.c new file mode 100644 index 00000000000..8723e6fa60d --- /dev/null +++ b/arch/arm/mach-tegra/dvfs.c | |||
@@ -0,0 +1,846 @@ | |||
1 | /* | ||
2 | * | ||
3 | * Copyright (C) 2010 Google, Inc. | ||
4 | * | ||
5 | * Author: | ||
6 | * Colin Cross <ccross@google.com> | ||
7 | * | ||
8 | * Copyright (C) 2010-2011 NVIDIA Corporation. | ||
9 | * | ||
10 | * This software is licensed under the terms of the GNU General Public | ||
11 | * License version 2, as published by the Free Software Foundation, and | ||
12 | * may be copied, distributed, and modified under those terms. | ||
13 | * | ||
14 | * This program is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | */ | ||
20 | |||
21 | #include <linux/kernel.h> | ||
22 | #include <linux/clk.h> | ||
23 | #include <linux/clkdev.h> | ||
24 | #include <linux/debugfs.h> | ||
25 | #include <linux/init.h> | ||
26 | #include <linux/list.h> | ||
27 | #include <linux/list_sort.h> | ||
28 | #include <linux/module.h> | ||
29 | #include <linux/regulator/consumer.h> | ||
30 | #include <linux/seq_file.h> | ||
31 | #include <linux/slab.h> | ||
32 | #include <linux/suspend.h> | ||
33 | #include <linux/delay.h> | ||
34 | #include <linux/reboot.h> | ||
35 | |||
36 | #include <mach/clk.h> | ||
37 | |||
38 | #include "board.h" | ||
39 | #include "clock.h" | ||
40 | #include "dvfs.h" | ||
41 | |||
42 | #define DVFS_RAIL_STATS_BIN 25 | ||
43 | #define DVFS_RAIL_STATS_SCALE 2 | ||
44 | #define DVFS_RAIL_STATS_RANGE ((DVFS_RAIL_STATS_TOP_BIN - 1) * \ | ||
45 | DVFS_RAIL_STATS_BIN / DVFS_RAIL_STATS_SCALE) | ||
46 | |||
47 | static LIST_HEAD(dvfs_rail_list); | ||
48 | static DEFINE_MUTEX(dvfs_lock); | ||
49 | static DEFINE_MUTEX(rail_disable_lock); | ||
50 | |||
51 | static int dvfs_rail_update(struct dvfs_rail *rail); | ||
52 | |||
53 | void tegra_dvfs_add_relationships(struct dvfs_relationship *rels, int n) | ||
54 | { | ||
55 | int i; | ||
56 | struct dvfs_relationship *rel; | ||
57 | |||
58 | mutex_lock(&dvfs_lock); | ||
59 | |||
60 | for (i = 0; i < n; i++) { | ||
61 | rel = &rels[i]; | ||
62 | list_add_tail(&rel->from_node, &rel->to->relationships_from); | ||
63 | list_add_tail(&rel->to_node, &rel->from->relationships_to); | ||
64 | } | ||
65 | |||
66 | mutex_unlock(&dvfs_lock); | ||
67 | } | ||
68 | |||
69 | int tegra_dvfs_init_rails(struct dvfs_rail *rails[], int n) | ||
70 | { | ||
71 | int i; | ||
72 | |||
73 | mutex_lock(&dvfs_lock); | ||
74 | |||
75 | for (i = 0; i < n; i++) { | ||
76 | INIT_LIST_HEAD(&rails[i]->dvfs); | ||
77 | INIT_LIST_HEAD(&rails[i]->relationships_from); | ||
78 | INIT_LIST_HEAD(&rails[i]->relationships_to); | ||
79 | rails[i]->millivolts = rails[i]->nominal_millivolts; | ||
80 | rails[i]->new_millivolts = rails[i]->nominal_millivolts; | ||
81 | if (!rails[i]->step) | ||
82 | rails[i]->step = rails[i]->max_millivolts; | ||
83 | |||
84 | list_add_tail(&rails[i]->node, &dvfs_rail_list); | ||
85 | } | ||
86 | |||
87 | mutex_unlock(&dvfs_lock); | ||
88 | |||
89 | return 0; | ||
90 | }; | ||
91 | |||
92 | static int dvfs_solve_relationship(struct dvfs_relationship *rel) | ||
93 | { | ||
94 | return rel->solve(rel->from, rel->to); | ||
95 | } | ||
96 | |||
97 | /* rail statistic - called during rail init, or under dfs_lock, or with | ||
98 | CPU0 only on-line, and interrupts disabled */ | ||
99 | static void dvfs_rail_stats_init(struct dvfs_rail *rail, int millivolts) | ||
100 | { | ||
101 | rail->stats.last_update = ktime_get(); | ||
102 | if (millivolts >= rail->min_millivolts) { | ||
103 | int i = 1 + (2 * (millivolts - rail->min_millivolts) * | ||
104 | DVFS_RAIL_STATS_SCALE + DVFS_RAIL_STATS_BIN) / | ||
105 | (2 * DVFS_RAIL_STATS_BIN); | ||
106 | rail->stats.last_index = min(i, DVFS_RAIL_STATS_TOP_BIN); | ||
107 | } | ||
108 | |||
109 | if (rail->max_millivolts > | ||
110 | rail->min_millivolts + DVFS_RAIL_STATS_RANGE) | ||
111 | pr_warn("tegra_dvfs: %s: stats above %d mV will be squashed\n", | ||
112 | rail->reg_id, | ||
113 | rail->min_millivolts + DVFS_RAIL_STATS_RANGE); | ||
114 | } | ||
115 | |||
116 | static void dvfs_rail_stats_update( | ||
117 | struct dvfs_rail *rail, int millivolts, ktime_t now) | ||
118 | { | ||
119 | rail->stats.time_at_mv[rail->stats.last_index] = ktime_add( | ||
120 | rail->stats.time_at_mv[rail->stats.last_index], ktime_sub( | ||
121 | now, rail->stats.last_update)); | ||
122 | rail->stats.last_update = now; | ||
123 | |||
124 | if (rail->stats.off) | ||
125 | return; | ||
126 | |||
127 | if (millivolts >= rail->min_millivolts) { | ||
128 | int i = 1 + (2 * (millivolts - rail->min_millivolts) * | ||
129 | DVFS_RAIL_STATS_SCALE + DVFS_RAIL_STATS_BIN) / | ||
130 | (2 * DVFS_RAIL_STATS_BIN); | ||
131 | rail->stats.last_index = min(i, DVFS_RAIL_STATS_TOP_BIN); | ||
132 | } else if (millivolts == 0) | ||
133 | rail->stats.last_index = 0; | ||
134 | } | ||
135 | |||
136 | static void dvfs_rail_stats_pause(struct dvfs_rail *rail, | ||
137 | ktime_t delta, bool on) | ||
138 | { | ||
139 | int i = on ? rail->stats.last_index : 0; | ||
140 | rail->stats.time_at_mv[i] = ktime_add(rail->stats.time_at_mv[i], delta); | ||
141 | } | ||
142 | |||
143 | void tegra_dvfs_rail_off(struct dvfs_rail *rail, ktime_t now) | ||
144 | { | ||
145 | if (rail) { | ||
146 | dvfs_rail_stats_update(rail, 0, now); | ||
147 | rail->stats.off = true; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | void tegra_dvfs_rail_on(struct dvfs_rail *rail, ktime_t now) | ||
152 | { | ||
153 | if (rail) { | ||
154 | rail->stats.off = false; | ||
155 | dvfs_rail_stats_update(rail, rail->millivolts, now); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | void tegra_dvfs_rail_pause(struct dvfs_rail *rail, ktime_t delta, bool on) | ||
160 | { | ||
161 | if (rail) | ||
162 | dvfs_rail_stats_pause(rail, delta, on); | ||
163 | } | ||
164 | |||
165 | /* Sets the voltage on a dvfs rail to a specific value, and updates any | ||
166 | * rails that depend on this rail. */ | ||
167 | static int dvfs_rail_set_voltage(struct dvfs_rail *rail, int millivolts) | ||
168 | { | ||
169 | int ret = 0; | ||
170 | struct dvfs_relationship *rel; | ||
171 | int step = (millivolts > rail->millivolts) ? rail->step : -rail->step; | ||
172 | int i; | ||
173 | int steps; | ||
174 | bool jmp_to_zero; | ||
175 | |||
176 | if (!rail->reg) { | ||
177 | if (millivolts == rail->millivolts) | ||
178 | return 0; | ||
179 | else | ||
180 | return -EINVAL; | ||
181 | } | ||
182 | |||
183 | if (rail->disabled) | ||
184 | return 0; | ||
185 | |||
186 | rail->resolving_to = true; | ||
187 | jmp_to_zero = rail->jmp_to_zero && | ||
188 | ((millivolts == 0) || (rail->millivolts == 0)); | ||
189 | steps = jmp_to_zero ? 1 : | ||
190 | DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step); | ||
191 | |||
192 | for (i = 0; i < steps; i++) { | ||
193 | if (!jmp_to_zero && | ||
194 | (abs(millivolts - rail->millivolts) > rail->step)) | ||
195 | rail->new_millivolts = rail->millivolts + step; | ||
196 | else | ||
197 | rail->new_millivolts = millivolts; | ||
198 | |||
199 | /* Before changing the voltage, tell each rail that depends | ||
200 | * on this rail that the voltage will change. | ||
201 | * This rail will be the "from" rail in the relationship, | ||
202 | * the rail that depends on this rail will be the "to" rail. | ||
203 | * from->millivolts will be the old voltage | ||
204 | * from->new_millivolts will be the new voltage */ | ||
205 | list_for_each_entry(rel, &rail->relationships_to, to_node) { | ||
206 | ret = dvfs_rail_update(rel->to); | ||
207 | if (ret) | ||
208 | goto out; | ||
209 | } | ||
210 | |||
211 | if (!rail->disabled) { | ||
212 | rail->updating = true; | ||
213 | ret = regulator_set_voltage(rail->reg, | ||
214 | rail->new_millivolts * 1000, | ||
215 | rail->max_millivolts * 1000); | ||
216 | rail->updating = false; | ||
217 | } | ||
218 | if (ret) { | ||
219 | pr_err("Failed to set dvfs regulator %s\n", rail->reg_id); | ||
220 | goto out; | ||
221 | } | ||
222 | |||
223 | rail->millivolts = rail->new_millivolts; | ||
224 | dvfs_rail_stats_update(rail, rail->millivolts, ktime_get()); | ||
225 | |||
226 | /* After changing the voltage, tell each rail that depends | ||
227 | * on this rail that the voltage has changed. | ||
228 | * from->millivolts and from->new_millivolts will be the | ||
229 | * new voltage */ | ||
230 | list_for_each_entry(rel, &rail->relationships_to, to_node) { | ||
231 | ret = dvfs_rail_update(rel->to); | ||
232 | if (ret) | ||
233 | goto out; | ||
234 | } | ||
235 | } | ||
236 | |||
237 | if (unlikely(rail->millivolts != millivolts)) { | ||
238 | pr_err("%s: rail didn't reach target %d in %d steps (%d)\n", | ||
239 | __func__, millivolts, steps, rail->millivolts); | ||
240 | ret = -EINVAL; | ||
241 | } | ||
242 | |||
243 | out: | ||
244 | rail->resolving_to = false; | ||
245 | return ret; | ||
246 | } | ||
247 | |||
248 | /* Determine the minimum valid voltage for a rail, taking into account | ||
249 | * the dvfs clocks and any rails that this rail depends on. Calls | ||
250 | * dvfs_rail_set_voltage with the new voltage, which will call | ||
251 | * dvfs_rail_update on any rails that depend on this rail. */ | ||
252 | static int dvfs_rail_update(struct dvfs_rail *rail) | ||
253 | { | ||
254 | int millivolts = 0; | ||
255 | struct dvfs *d; | ||
256 | struct dvfs_relationship *rel; | ||
257 | int ret = 0; | ||
258 | int steps; | ||
259 | |||
260 | /* if dvfs is suspended, return and handle it during resume */ | ||
261 | if (rail->suspended) | ||
262 | return 0; | ||
263 | |||
264 | /* if regulators are not connected yet, return and handle it later */ | ||
265 | if (!rail->reg) | ||
266 | return 0; | ||
267 | |||
268 | /* if rail update is entered while resolving circular dependencies, | ||
269 | abort recursion */ | ||
270 | if (rail->resolving_to) | ||
271 | return 0; | ||
272 | |||
273 | /* Find the maximum voltage requested by any clock */ | ||
274 | list_for_each_entry(d, &rail->dvfs, reg_node) | ||
275 | millivolts = max(d->cur_millivolts, millivolts); | ||
276 | |||
277 | /* retry update if limited by from-relationship to account for | ||
278 | circular dependencies */ | ||
279 | steps = DIV_ROUND_UP(abs(millivolts - rail->millivolts), rail->step); | ||
280 | for (; steps >= 0; steps--) { | ||
281 | rail->new_millivolts = millivolts; | ||
282 | |||
283 | /* Check any rails that this rail depends on */ | ||
284 | list_for_each_entry(rel, &rail->relationships_from, from_node) | ||
285 | rail->new_millivolts = dvfs_solve_relationship(rel); | ||
286 | |||
287 | if (rail->new_millivolts == rail->millivolts) | ||
288 | break; | ||
289 | |||
290 | ret = dvfs_rail_set_voltage(rail, rail->new_millivolts); | ||
291 | } | ||
292 | |||
293 | return ret; | ||
294 | } | ||
295 | |||
296 | static int dvfs_rail_connect_to_regulator(struct dvfs_rail *rail) | ||
297 | { | ||
298 | struct regulator *reg; | ||
299 | int v; | ||
300 | |||
301 | if (!rail->reg) { | ||
302 | reg = regulator_get(NULL, rail->reg_id); | ||
303 | if (IS_ERR(reg)) { | ||
304 | pr_err("tegra_dvfs: failed to connect %s rail\n", | ||
305 | rail->reg_id); | ||
306 | return -EINVAL; | ||
307 | } | ||
308 | rail->reg = reg; | ||
309 | } | ||
310 | |||
311 | v = regulator_get_voltage(rail->reg); | ||
312 | if (v < 0) { | ||
313 | pr_err("tegra_dvfs: failed initial get %s voltage\n", | ||
314 | rail->reg_id); | ||
315 | return v; | ||
316 | } | ||
317 | rail->millivolts = v / 1000; | ||
318 | rail->new_millivolts = rail->millivolts; | ||
319 | dvfs_rail_stats_init(rail, rail->millivolts); | ||
320 | return 0; | ||
321 | } | ||
322 | |||
323 | static inline unsigned long *dvfs_get_freqs(struct dvfs *d) | ||
324 | { | ||
325 | return (d->alt_freqs_state == ALT_FREQS_ENABLED) ? | ||
326 | &d->alt_freqs[0] : &d->freqs[0]; | ||
327 | } | ||
328 | |||
329 | static int | ||
330 | __tegra_dvfs_set_rate(struct dvfs *d, unsigned long rate) | ||
331 | { | ||
332 | int i = 0; | ||
333 | int ret; | ||
334 | unsigned long *freqs = dvfs_get_freqs(d); | ||
335 | |||
336 | if (freqs == NULL || d->millivolts == NULL) | ||
337 | return -ENODEV; | ||
338 | |||
339 | if (rate > freqs[d->num_freqs - 1]) { | ||
340 | pr_warn("tegra_dvfs: rate %lu too high for dvfs on %s\n", rate, | ||
341 | d->clk_name); | ||
342 | return -EINVAL; | ||
343 | } | ||
344 | |||
345 | if (rate == 0) { | ||
346 | d->cur_millivolts = 0; | ||
347 | } else { | ||
348 | while (i < d->num_freqs && rate > freqs[i]) | ||
349 | i++; | ||
350 | |||
351 | if ((d->max_millivolts) && | ||
352 | (d->millivolts[i] > d->max_millivolts)) { | ||
353 | pr_warn("tegra_dvfs: voltage %d too high for dvfs on" | ||
354 | " %s\n", d->millivolts[i], d->clk_name); | ||
355 | return -EINVAL; | ||
356 | } | ||
357 | d->cur_millivolts = d->millivolts[i]; | ||
358 | } | ||
359 | |||
360 | d->cur_rate = rate; | ||
361 | |||
362 | ret = dvfs_rail_update(d->dvfs_rail); | ||
363 | if (ret) | ||
364 | pr_err("Failed to set regulator %s for clock %s to %d mV\n", | ||
365 | d->dvfs_rail->reg_id, d->clk_name, d->cur_millivolts); | ||
366 | |||
367 | return ret; | ||
368 | } | ||
369 | |||
370 | static inline int dvfs_alt_freqs_set(struct dvfs *d, bool enable) | ||
371 | { | ||
372 | if (d->alt_freqs_state == ALT_FREQS_NOT_SUPPORTED) | ||
373 | return -ENOSYS; | ||
374 | |||
375 | d->alt_freqs_state = enable ? ALT_FREQS_ENABLED : ALT_FREQS_DISABLED; | ||
376 | return 0; | ||
377 | } | ||
378 | |||
379 | int tegra_dvfs_alt_freqs_set(struct dvfs *d, bool enable) | ||
380 | { | ||
381 | int ret; | ||
382 | enum dvfs_alt_freqs old_state; | ||
383 | |||
384 | mutex_lock(&dvfs_lock); | ||
385 | |||
386 | old_state = d->alt_freqs_state; | ||
387 | ret = dvfs_alt_freqs_set(d, enable); | ||
388 | if (!ret && (old_state != d->alt_freqs_state)) | ||
389 | ret = __tegra_dvfs_set_rate(d, d->cur_rate); | ||
390 | |||
391 | mutex_unlock(&dvfs_lock); | ||
392 | return ret; | ||
393 | } | ||
394 | |||
395 | int tegra_dvfs_predict_millivolts(struct clk *c, unsigned long rate) | ||
396 | { | ||
397 | int i; | ||
398 | |||
399 | if (!rate || !c->dvfs) | ||
400 | return 0; | ||
401 | |||
402 | if (!c->dvfs->millivolts) | ||
403 | return -ENODEV; | ||
404 | |||
405 | /* | ||
406 | * Predicted voltage can not be used across the switch to alternative | ||
407 | * frequency limits. For now, just fail the call for clock that has | ||
408 | * alternative limits initialized. | ||
409 | */ | ||
410 | if (c->dvfs->alt_freqs_state != ALT_FREQS_NOT_SUPPORTED) | ||
411 | return -ENOSYS; | ||
412 | |||
413 | for (i = 0; i < c->dvfs->num_freqs; i++) { | ||
414 | if (rate <= c->dvfs->freqs[i]) | ||
415 | break; | ||
416 | } | ||
417 | |||
418 | if (i == c->dvfs->num_freqs) | ||
419 | return -EINVAL; | ||
420 | |||
421 | return c->dvfs->millivolts[i]; | ||
422 | } | ||
423 | |||
424 | int tegra_dvfs_set_rate(struct clk *c, unsigned long rate) | ||
425 | { | ||
426 | int ret; | ||
427 | |||
428 | if (!c->dvfs) | ||
429 | return -EINVAL; | ||
430 | |||
431 | mutex_lock(&dvfs_lock); | ||
432 | ret = __tegra_dvfs_set_rate(c->dvfs, rate); | ||
433 | mutex_unlock(&dvfs_lock); | ||
434 | |||
435 | return ret; | ||
436 | } | ||
437 | EXPORT_SYMBOL(tegra_dvfs_set_rate); | ||
438 | |||
439 | /* May only be called during clock init, does not take any locks on clock c. */ | ||
440 | int __init tegra_enable_dvfs_on_clk(struct clk *c, struct dvfs *d) | ||
441 | { | ||
442 | int i; | ||
443 | |||
444 | if (c->dvfs) { | ||
445 | pr_err("Error when enabling dvfs on %s for clock %s:\n", | ||
446 | d->dvfs_rail->reg_id, c->name); | ||
447 | pr_err("DVFS already enabled for %s\n", | ||
448 | c->dvfs->dvfs_rail->reg_id); | ||
449 | return -EINVAL; | ||
450 | } | ||
451 | |||
452 | for (i = 0; i < MAX_DVFS_FREQS; i++) { | ||
453 | if (d->millivolts[i] == 0) | ||
454 | break; | ||
455 | |||
456 | d->freqs[i] *= d->freqs_mult; | ||
457 | |||
458 | /* If final frequencies are 0, pad with previous frequency */ | ||
459 | if (d->freqs[i] == 0 && i > 1) | ||
460 | d->freqs[i] = d->freqs[i - 1]; | ||
461 | } | ||
462 | d->num_freqs = i; | ||
463 | |||
464 | if (d->auto_dvfs) { | ||
465 | c->auto_dvfs = true; | ||
466 | clk_set_cansleep(c); | ||
467 | } | ||
468 | |||
469 | c->dvfs = d; | ||
470 | |||
471 | mutex_lock(&dvfs_lock); | ||
472 | list_add_tail(&d->reg_node, &d->dvfs_rail->dvfs); | ||
473 | mutex_unlock(&dvfs_lock); | ||
474 | |||
475 | return 0; | ||
476 | } | ||
477 | |||
478 | static bool tegra_dvfs_all_rails_suspended(void) | ||
479 | { | ||
480 | struct dvfs_rail *rail; | ||
481 | bool all_suspended = true; | ||
482 | |||
483 | list_for_each_entry(rail, &dvfs_rail_list, node) | ||
484 | if (!rail->suspended && !rail->disabled) | ||
485 | all_suspended = false; | ||
486 | |||
487 | return all_suspended; | ||
488 | } | ||
489 | |||
490 | static bool tegra_dvfs_from_rails_suspended_or_solved(struct dvfs_rail *to) | ||
491 | { | ||
492 | struct dvfs_relationship *rel; | ||
493 | bool all_suspended = true; | ||
494 | |||
495 | list_for_each_entry(rel, &to->relationships_from, from_node) | ||
496 | if (!rel->from->suspended && !rel->from->disabled && | ||
497 | !rel->solved_at_nominal) | ||
498 | all_suspended = false; | ||
499 | |||
500 | return all_suspended; | ||
501 | } | ||
502 | |||
503 | static int tegra_dvfs_suspend_one(void) | ||
504 | { | ||
505 | struct dvfs_rail *rail; | ||
506 | int ret; | ||
507 | |||
508 | list_for_each_entry(rail, &dvfs_rail_list, node) { | ||
509 | if (!rail->suspended && !rail->disabled && | ||
510 | tegra_dvfs_from_rails_suspended_or_solved(rail)) { | ||
511 | ret = dvfs_rail_set_voltage(rail, | ||
512 | rail->nominal_millivolts); | ||
513 | if (ret) | ||
514 | return ret; | ||
515 | rail->suspended = true; | ||
516 | return 0; | ||
517 | } | ||
518 | } | ||
519 | |||
520 | return -EINVAL; | ||
521 | } | ||
522 | |||
523 | static void tegra_dvfs_resume(void) | ||
524 | { | ||
525 | struct dvfs_rail *rail; | ||
526 | |||
527 | mutex_lock(&dvfs_lock); | ||
528 | |||
529 | list_for_each_entry(rail, &dvfs_rail_list, node) | ||
530 | rail->suspended = false; | ||
531 | |||
532 | list_for_each_entry(rail, &dvfs_rail_list, node) | ||
533 | dvfs_rail_update(rail); | ||
534 | |||
535 | mutex_unlock(&dvfs_lock); | ||
536 | } | ||
537 | |||
538 | static int tegra_dvfs_suspend(void) | ||
539 | { | ||
540 | int ret = 0; | ||
541 | |||
542 | mutex_lock(&dvfs_lock); | ||
543 | |||
544 | while (!tegra_dvfs_all_rails_suspended()) { | ||
545 | ret = tegra_dvfs_suspend_one(); | ||
546 | if (ret) | ||
547 | break; | ||
548 | } | ||
549 | |||
550 | mutex_unlock(&dvfs_lock); | ||
551 | |||
552 | if (ret) | ||
553 | tegra_dvfs_resume(); | ||
554 | |||
555 | return ret; | ||
556 | } | ||
557 | |||
558 | static int tegra_dvfs_pm_notify(struct notifier_block *nb, | ||
559 | unsigned long event, void *data) | ||
560 | { | ||
561 | switch (event) { | ||
562 | case PM_SUSPEND_PREPARE: | ||
563 | if (tegra_dvfs_suspend()) | ||
564 | return NOTIFY_STOP; | ||
565 | break; | ||
566 | case PM_POST_SUSPEND: | ||
567 | tegra_dvfs_resume(); | ||
568 | break; | ||
569 | } | ||
570 | |||
571 | return NOTIFY_OK; | ||
572 | }; | ||
573 | |||
574 | static struct notifier_block tegra_dvfs_nb = { | ||
575 | .notifier_call = tegra_dvfs_pm_notify, | ||
576 | }; | ||
577 | |||
578 | static int tegra_dvfs_reboot_notify(struct notifier_block *nb, | ||
579 | unsigned long event, void *data) | ||
580 | { | ||
581 | switch (event) { | ||
582 | case SYS_RESTART: | ||
583 | case SYS_HALT: | ||
584 | case SYS_POWER_OFF: | ||
585 | tegra_dvfs_suspend(); | ||
586 | return NOTIFY_OK; | ||
587 | } | ||
588 | return NOTIFY_DONE; | ||
589 | } | ||
590 | |||
591 | static struct notifier_block tegra_dvfs_reboot_nb = { | ||
592 | .notifier_call = tegra_dvfs_reboot_notify, | ||
593 | }; | ||
594 | |||
595 | /* must be called with dvfs lock held */ | ||
596 | static void __tegra_dvfs_rail_disable(struct dvfs_rail *rail) | ||
597 | { | ||
598 | int ret; | ||
599 | |||
600 | ret = dvfs_rail_set_voltage(rail, rail->nominal_millivolts); | ||
601 | if (ret) | ||
602 | pr_info("dvfs: failed to set regulator %s to disable " | ||
603 | "voltage %d\n", rail->reg_id, | ||
604 | rail->nominal_millivolts); | ||
605 | rail->disabled = true; | ||
606 | } | ||
607 | |||
608 | /* must be called with dvfs lock held */ | ||
609 | static void __tegra_dvfs_rail_enable(struct dvfs_rail *rail) | ||
610 | { | ||
611 | rail->disabled = false; | ||
612 | dvfs_rail_update(rail); | ||
613 | } | ||
614 | |||
615 | void tegra_dvfs_rail_enable(struct dvfs_rail *rail) | ||
616 | { | ||
617 | mutex_lock(&rail_disable_lock); | ||
618 | |||
619 | if (rail->disabled) { | ||
620 | mutex_lock(&dvfs_lock); | ||
621 | __tegra_dvfs_rail_enable(rail); | ||
622 | mutex_unlock(&dvfs_lock); | ||
623 | |||
624 | tegra_dvfs_rail_post_enable(rail); | ||
625 | } | ||
626 | mutex_unlock(&rail_disable_lock); | ||
627 | |||
628 | } | ||
629 | |||
630 | void tegra_dvfs_rail_disable(struct dvfs_rail *rail) | ||
631 | { | ||
632 | mutex_lock(&rail_disable_lock); | ||
633 | if (rail->disabled) | ||
634 | goto out; | ||
635 | |||
636 | /* rail disable will set it to nominal voltage underneath clock | ||
637 | framework - need to re-configure clock rates that are not safe | ||
638 | at nominal (yes, unsafe at nominal is ugly, but possible). Rate | ||
639 | change must be done outside of dvfs lock. */ | ||
640 | if (tegra_dvfs_rail_disable_prepare(rail)) { | ||
641 | pr_info("dvfs: failed to prepare regulator %s to disable\n", | ||
642 | rail->reg_id); | ||
643 | goto out; | ||
644 | } | ||
645 | |||
646 | mutex_lock(&dvfs_lock); | ||
647 | __tegra_dvfs_rail_disable(rail); | ||
648 | mutex_unlock(&dvfs_lock); | ||
649 | out: | ||
650 | mutex_unlock(&rail_disable_lock); | ||
651 | } | ||
652 | |||
653 | int tegra_dvfs_rail_disable_by_name(const char *reg_id) | ||
654 | { | ||
655 | struct dvfs_rail *rail = tegra_dvfs_get_rail_by_name(reg_id); | ||
656 | if (!rail) | ||
657 | return -EINVAL; | ||
658 | |||
659 | tegra_dvfs_rail_disable(rail); | ||
660 | return 0; | ||
661 | } | ||
662 | |||
663 | struct dvfs_rail *tegra_dvfs_get_rail_by_name(const char *reg_id) | ||
664 | { | ||
665 | struct dvfs_rail *rail; | ||
666 | |||
667 | mutex_lock(&dvfs_lock); | ||
668 | list_for_each_entry(rail, &dvfs_rail_list, node) { | ||
669 | if (!strcmp(reg_id, rail->reg_id)) { | ||
670 | mutex_unlock(&dvfs_lock); | ||
671 | return rail; | ||
672 | } | ||
673 | } | ||
674 | mutex_unlock(&dvfs_lock); | ||
675 | return NULL; | ||
676 | } | ||
677 | |||
678 | bool tegra_dvfs_rail_updating(struct clk *clk) | ||
679 | { | ||
680 | return (!clk ? false : | ||
681 | (!clk->dvfs ? false : | ||
682 | (!clk->dvfs->dvfs_rail ? false : | ||
683 | (clk->dvfs->dvfs_rail->updating)))); | ||
684 | } | ||
685 | |||
686 | /* | ||
687 | * Iterate through all the dvfs regulators, finding the regulator exported | ||
688 | * by the regulator api for each one. Must be called in late init, after | ||
689 | * all the regulator api's regulators are initialized. | ||
690 | */ | ||
691 | int __init tegra_dvfs_late_init(void) | ||
692 | { | ||
693 | bool connected = true; | ||
694 | struct dvfs_rail *rail; | ||
695 | |||
696 | mutex_lock(&dvfs_lock); | ||
697 | |||
698 | list_for_each_entry(rail, &dvfs_rail_list, node) | ||
699 | if (dvfs_rail_connect_to_regulator(rail)) | ||
700 | connected = false; | ||
701 | |||
702 | list_for_each_entry(rail, &dvfs_rail_list, node) | ||
703 | if (connected) | ||
704 | dvfs_rail_update(rail); | ||
705 | else | ||
706 | __tegra_dvfs_rail_disable(rail); | ||
707 | |||
708 | mutex_unlock(&dvfs_lock); | ||
709 | |||
710 | register_pm_notifier(&tegra_dvfs_nb); | ||
711 | register_reboot_notifier(&tegra_dvfs_reboot_nb); | ||
712 | |||
713 | return 0; | ||
714 | } | ||
715 | late_initcall(tegra_dvfs_late_init); | ||
716 | |||
717 | #ifdef CONFIG_DEBUG_FS | ||
718 | static int dvfs_tree_sort_cmp(void *p, struct list_head *a, struct list_head *b) | ||
719 | { | ||
720 | struct dvfs *da = list_entry(a, struct dvfs, reg_node); | ||
721 | struct dvfs *db = list_entry(b, struct dvfs, reg_node); | ||
722 | int ret; | ||
723 | |||
724 | ret = strcmp(da->dvfs_rail->reg_id, db->dvfs_rail->reg_id); | ||
725 | if (ret != 0) | ||
726 | return ret; | ||
727 | |||
728 | if (da->cur_millivolts < db->cur_millivolts) | ||
729 | return 1; | ||
730 | if (da->cur_millivolts > db->cur_millivolts) | ||
731 | return -1; | ||
732 | |||
733 | return strcmp(da->clk_name, db->clk_name); | ||
734 | } | ||
735 | |||
736 | static int dvfs_tree_show(struct seq_file *s, void *data) | ||
737 | { | ||
738 | struct dvfs *d; | ||
739 | struct dvfs_rail *rail; | ||
740 | struct dvfs_relationship *rel; | ||
741 | |||
742 | seq_printf(s, " clock rate mV\n"); | ||
743 | seq_printf(s, "--------------------------------\n"); | ||
744 | |||
745 | mutex_lock(&dvfs_lock); | ||
746 | |||
747 | list_for_each_entry(rail, &dvfs_rail_list, node) { | ||
748 | seq_printf(s, "%s %d mV%s:\n", rail->reg_id, | ||
749 | rail->millivolts, rail->disabled ? " disabled" : ""); | ||
750 | list_for_each_entry(rel, &rail->relationships_from, from_node) { | ||
751 | seq_printf(s, " %-10s %-7d mV %-4d mV\n", | ||
752 | rel->from->reg_id, | ||
753 | rel->from->millivolts, | ||
754 | dvfs_solve_relationship(rel)); | ||
755 | } | ||
756 | |||
757 | list_sort(NULL, &rail->dvfs, dvfs_tree_sort_cmp); | ||
758 | |||
759 | list_for_each_entry(d, &rail->dvfs, reg_node) { | ||
760 | seq_printf(s, " %-10s %-10lu %-4d mV\n", d->clk_name, | ||
761 | d->cur_rate, d->cur_millivolts); | ||
762 | } | ||
763 | } | ||
764 | |||
765 | mutex_unlock(&dvfs_lock); | ||
766 | |||
767 | return 0; | ||
768 | } | ||
769 | |||
770 | static int dvfs_tree_open(struct inode *inode, struct file *file) | ||
771 | { | ||
772 | return single_open(file, dvfs_tree_show, inode->i_private); | ||
773 | } | ||
774 | |||
775 | static const struct file_operations dvfs_tree_fops = { | ||
776 | .open = dvfs_tree_open, | ||
777 | .read = seq_read, | ||
778 | .llseek = seq_lseek, | ||
779 | .release = single_release, | ||
780 | }; | ||
781 | |||
782 | static int rail_stats_show(struct seq_file *s, void *data) | ||
783 | { | ||
784 | int i; | ||
785 | struct dvfs_rail *rail; | ||
786 | |||
787 | seq_printf(s, "%-12s %-10s (bin: %d.%dmV)\n", "millivolts", "time", | ||
788 | DVFS_RAIL_STATS_BIN / DVFS_RAIL_STATS_SCALE, | ||
789 | ((DVFS_RAIL_STATS_BIN * 100) / DVFS_RAIL_STATS_SCALE) % 100); | ||
790 | |||
791 | mutex_lock(&dvfs_lock); | ||
792 | |||
793 | list_for_each_entry(rail, &dvfs_rail_list, node) { | ||
794 | seq_printf(s, "%s\n", rail->reg_id); | ||
795 | dvfs_rail_stats_update(rail, -1, ktime_get()); | ||
796 | |||
797 | seq_printf(s, "%-12d %-10llu\n", 0, | ||
798 | cputime64_to_clock_t(msecs_to_jiffies( | ||
799 | ktime_to_ms(rail->stats.time_at_mv[0])))); | ||
800 | |||
801 | for (i = 1; i <= DVFS_RAIL_STATS_TOP_BIN; i++) { | ||
802 | ktime_t ktime_zero = ktime_set(0, 0); | ||
803 | if (ktime_equal(rail->stats.time_at_mv[i], ktime_zero)) | ||
804 | continue; | ||
805 | seq_printf(s, "%-12d %-10llu\n", | ||
806 | rail->min_millivolts + (i - 1) * | ||
807 | DVFS_RAIL_STATS_BIN / DVFS_RAIL_STATS_SCALE, | ||
808 | cputime64_to_clock_t(msecs_to_jiffies( | ||
809 | ktime_to_ms(rail->stats.time_at_mv[i]))) | ||
810 | ); | ||
811 | } | ||
812 | } | ||
813 | mutex_unlock(&dvfs_lock); | ||
814 | return 0; | ||
815 | } | ||
816 | |||
817 | static int rail_stats_open(struct inode *inode, struct file *file) | ||
818 | { | ||
819 | return single_open(file, rail_stats_show, inode->i_private); | ||
820 | } | ||
821 | |||
822 | static const struct file_operations rail_stats_fops = { | ||
823 | .open = rail_stats_open, | ||
824 | .read = seq_read, | ||
825 | .llseek = seq_lseek, | ||
826 | .release = single_release, | ||
827 | }; | ||
828 | |||
829 | int __init dvfs_debugfs_init(struct dentry *clk_debugfs_root) | ||
830 | { | ||
831 | struct dentry *d; | ||
832 | |||
833 | d = debugfs_create_file("dvfs", S_IRUGO, clk_debugfs_root, NULL, | ||
834 | &dvfs_tree_fops); | ||
835 | if (!d) | ||
836 | return -ENOMEM; | ||
837 | |||
838 | d = debugfs_create_file("rails", S_IRUGO, clk_debugfs_root, NULL, | ||
839 | &rail_stats_fops); | ||
840 | if (!d) | ||
841 | return -ENOMEM; | ||
842 | |||
843 | return 0; | ||
844 | } | ||
845 | |||
846 | #endif | ||