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, | ||
