Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * A devfreq driver for NVIDIA Tegra SoCs |
| 3 | * |
| 4 | * Copyright (c) 2014 NVIDIA CORPORATION. All rights reserved. |
| 5 | * Copyright (C) 2014 Google, Inc |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms and conditions of the GNU General Public License, |
| 9 | * version 2, as published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | * more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <linux/clk.h> |
| 22 | #include <linux/cpufreq.h> |
| 23 | #include <linux/devfreq.h> |
| 24 | #include <linux/interrupt.h> |
| 25 | #include <linux/io.h> |
| 26 | #include <linux/module.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 27 | #include <linux/mod_devicetable.h> |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 28 | #include <linux/platform_device.h> |
| 29 | #include <linux/pm_opp.h> |
| 30 | #include <linux/reset.h> |
| 31 | |
| 32 | #include "governor.h" |
| 33 | |
| 34 | #define ACTMON_GLB_STATUS 0x0 |
| 35 | #define ACTMON_GLB_PERIOD_CTRL 0x4 |
| 36 | |
| 37 | #define ACTMON_DEV_CTRL 0x0 |
| 38 | #define ACTMON_DEV_CTRL_K_VAL_SHIFT 10 |
| 39 | #define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18) |
| 40 | #define ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN BIT(20) |
| 41 | #define ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN BIT(21) |
| 42 | #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT 23 |
| 43 | #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT 26 |
| 44 | #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN BIT(29) |
| 45 | #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30) |
| 46 | #define ACTMON_DEV_CTRL_ENB BIT(31) |
| 47 | |
| 48 | #define ACTMON_DEV_UPPER_WMARK 0x4 |
| 49 | #define ACTMON_DEV_LOWER_WMARK 0x8 |
| 50 | #define ACTMON_DEV_INIT_AVG 0xc |
| 51 | #define ACTMON_DEV_AVG_UPPER_WMARK 0x10 |
| 52 | #define ACTMON_DEV_AVG_LOWER_WMARK 0x14 |
| 53 | #define ACTMON_DEV_COUNT_WEIGHT 0x18 |
| 54 | #define ACTMON_DEV_AVG_COUNT 0x20 |
| 55 | #define ACTMON_DEV_INTR_STATUS 0x24 |
| 56 | |
| 57 | #define ACTMON_INTR_STATUS_CLEAR 0xffffffff |
| 58 | |
| 59 | #define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31) |
| 60 | #define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30) |
| 61 | |
| 62 | #define ACTMON_ABOVE_WMARK_WINDOW 1 |
| 63 | #define ACTMON_BELOW_WMARK_WINDOW 3 |
| 64 | #define ACTMON_BOOST_FREQ_STEP 16000 |
| 65 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 66 | /* |
| 67 | * Activity counter is incremented every 256 memory transactions, and each |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 68 | * transaction takes 4 EMC clocks for Tegra124; So the COUNT_WEIGHT is |
| 69 | * 4 * 256 = 1024. |
| 70 | */ |
| 71 | #define ACTMON_COUNT_WEIGHT 0x400 |
| 72 | |
| 73 | /* |
| 74 | * ACTMON_AVERAGE_WINDOW_LOG2: default value for @DEV_CTRL_K_VAL, which |
| 75 | * translates to 2 ^ (K_VAL + 1). ex: 2 ^ (6 + 1) = 128 |
| 76 | */ |
| 77 | #define ACTMON_AVERAGE_WINDOW_LOG2 6 |
| 78 | #define ACTMON_SAMPLING_PERIOD 12 /* ms */ |
| 79 | #define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */ |
| 80 | |
| 81 | #define KHZ 1000 |
| 82 | |
| 83 | /* Assume that the bus is saturated if the utilization is 25% */ |
| 84 | #define BUS_SATURATION_RATIO 25 |
| 85 | |
| 86 | /** |
| 87 | * struct tegra_devfreq_device_config - configuration specific to an ACTMON |
| 88 | * device |
| 89 | * |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 90 | * Coefficients and thresholds are percentages unless otherwise noted |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 91 | */ |
| 92 | struct tegra_devfreq_device_config { |
| 93 | u32 offset; |
| 94 | u32 irq_mask; |
| 95 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 96 | /* Factors applied to boost_freq every consecutive watermark breach */ |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 97 | unsigned int boost_up_coeff; |
| 98 | unsigned int boost_down_coeff; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 99 | |
| 100 | /* Define the watermark bounds when applied to the current avg */ |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 101 | unsigned int boost_up_threshold; |
| 102 | unsigned int boost_down_threshold; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 103 | |
| 104 | /* |
| 105 | * Threshold of activity (cycles) below which the CPU frequency isn't |
| 106 | * to be taken into account. This is to avoid increasing the EMC |
| 107 | * frequency when the CPU is very busy but not accessing the bus often. |
| 108 | */ |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 109 | u32 avg_dependency_threshold; |
| 110 | }; |
| 111 | |
| 112 | enum tegra_actmon_device { |
| 113 | MCALL = 0, |
| 114 | MCCPU, |
| 115 | }; |
| 116 | |
| 117 | static struct tegra_devfreq_device_config actmon_device_configs[] = { |
| 118 | { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 119 | /* MCALL: All memory accesses (including from the CPUs) */ |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 120 | .offset = 0x1c0, |
| 121 | .irq_mask = 1 << 26, |
| 122 | .boost_up_coeff = 200, |
| 123 | .boost_down_coeff = 50, |
| 124 | .boost_up_threshold = 60, |
| 125 | .boost_down_threshold = 40, |
| 126 | }, |
| 127 | { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 128 | /* MCCPU: memory accesses from the CPUs */ |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 129 | .offset = 0x200, |
| 130 | .irq_mask = 1 << 25, |
| 131 | .boost_up_coeff = 800, |
| 132 | .boost_down_coeff = 90, |
| 133 | .boost_up_threshold = 27, |
| 134 | .boost_down_threshold = 10, |
| 135 | .avg_dependency_threshold = 50000, |
| 136 | }, |
| 137 | }; |
| 138 | |
| 139 | /** |
| 140 | * struct tegra_devfreq_device - state specific to an ACTMON device |
| 141 | * |
| 142 | * Frequencies are in kHz. |
| 143 | */ |
| 144 | struct tegra_devfreq_device { |
| 145 | const struct tegra_devfreq_device_config *config; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 146 | void __iomem *regs; |
| 147 | spinlock_t lock; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 148 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 149 | /* Average event count sampled in the last interrupt */ |
| 150 | u32 avg_count; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 151 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 152 | /* |
| 153 | * Extra frequency to increase the target by due to consecutive |
| 154 | * watermark breaches. |
| 155 | */ |
| 156 | unsigned long boost_freq; |
| 157 | |
| 158 | /* Optimal frequency calculated from the stats for this device */ |
| 159 | unsigned long target_freq; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 160 | }; |
| 161 | |
| 162 | struct tegra_devfreq { |
| 163 | struct devfreq *devfreq; |
| 164 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 165 | struct reset_control *reset; |
| 166 | struct clk *clock; |
| 167 | void __iomem *regs; |
| 168 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 169 | struct clk *emc_clock; |
| 170 | unsigned long max_freq; |
| 171 | unsigned long cur_freq; |
| 172 | struct notifier_block rate_change_nb; |
| 173 | |
| 174 | struct tegra_devfreq_device devices[ARRAY_SIZE(actmon_device_configs)]; |
| 175 | }; |
| 176 | |
| 177 | struct tegra_actmon_emc_ratio { |
| 178 | unsigned long cpu_freq; |
| 179 | unsigned long emc_freq; |
| 180 | }; |
| 181 | |
| 182 | static struct tegra_actmon_emc_ratio actmon_emc_ratios[] = { |
| 183 | { 1400000, ULONG_MAX }, |
| 184 | { 1200000, 750000 }, |
| 185 | { 1100000, 600000 }, |
| 186 | { 1000000, 500000 }, |
| 187 | { 800000, 375000 }, |
| 188 | { 500000, 200000 }, |
| 189 | { 250000, 100000 }, |
| 190 | }; |
| 191 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 192 | static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset) |
| 193 | { |
| 194 | return readl(tegra->regs + offset); |
| 195 | } |
| 196 | |
| 197 | static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset) |
| 198 | { |
| 199 | writel(val, tegra->regs + offset); |
| 200 | } |
| 201 | |
| 202 | static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset) |
| 203 | { |
| 204 | return readl(dev->regs + offset); |
| 205 | } |
| 206 | |
| 207 | static void device_writel(struct tegra_devfreq_device *dev, u32 val, |
| 208 | u32 offset) |
| 209 | { |
| 210 | writel(val, dev->regs + offset); |
| 211 | } |
| 212 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 213 | static unsigned long do_percent(unsigned long val, unsigned int pct) |
| 214 | { |
| 215 | return val * pct / 100; |
| 216 | } |
| 217 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 218 | static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra, |
| 219 | struct tegra_devfreq_device *dev) |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 220 | { |
| 221 | u32 avg = dev->avg_count; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 222 | u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ; |
| 223 | u32 band = avg_band_freq * ACTMON_SAMPLING_PERIOD; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 224 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 225 | device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK); |
| 226 | |
| 227 | avg = max(dev->avg_count, band); |
| 228 | device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra, |
| 232 | struct tegra_devfreq_device *dev) |
| 233 | { |
| 234 | u32 val = tegra->cur_freq * ACTMON_SAMPLING_PERIOD; |
| 235 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 236 | device_writel(dev, do_percent(val, dev->config->boost_up_threshold), |
| 237 | ACTMON_DEV_UPPER_WMARK); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 238 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 239 | device_writel(dev, do_percent(val, dev->config->boost_down_threshold), |
| 240 | ACTMON_DEV_LOWER_WMARK); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void actmon_write_barrier(struct tegra_devfreq *tegra) |
| 244 | { |
| 245 | /* ensure the update has reached the ACTMON */ |
| 246 | wmb(); |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 247 | actmon_readl(tegra, ACTMON_GLB_STATUS); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 250 | static void actmon_isr_device(struct tegra_devfreq *tegra, |
| 251 | struct tegra_devfreq_device *dev) |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 252 | { |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 253 | unsigned long flags; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 254 | u32 intr_status, dev_ctrl; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 255 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 256 | spin_lock_irqsave(&dev->lock, flags); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 257 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 258 | dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT); |
| 259 | tegra_devfreq_update_avg_wmark(tegra, dev); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 260 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 261 | intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS); |
| 262 | dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 263 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 264 | if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) { |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 265 | /* |
| 266 | * new_boost = min(old_boost * up_coef + step, max_freq) |
| 267 | */ |
| 268 | dev->boost_freq = do_percent(dev->boost_freq, |
| 269 | dev->config->boost_up_coeff); |
| 270 | dev->boost_freq += ACTMON_BOOST_FREQ_STEP; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 271 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 272 | dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN; |
| 273 | |
| 274 | if (dev->boost_freq >= tegra->max_freq) |
| 275 | dev->boost_freq = tegra->max_freq; |
| 276 | else |
| 277 | dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN; |
| 278 | } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) { |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 279 | /* |
| 280 | * new_boost = old_boost * down_coef |
| 281 | * or 0 if (old_boost * down_coef < step / 2) |
| 282 | */ |
| 283 | dev->boost_freq = do_percent(dev->boost_freq, |
| 284 | dev->config->boost_down_coeff); |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 285 | |
| 286 | dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN; |
| 287 | |
| 288 | if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1)) |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 289 | dev->boost_freq = 0; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 290 | else |
| 291 | dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | if (dev->config->avg_dependency_threshold) { |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 295 | if (dev->avg_count >= dev->config->avg_dependency_threshold) |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 296 | dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 297 | else if (dev->boost_freq == 0) |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 298 | dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 299 | } |
| 300 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 301 | device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL); |
| 302 | |
| 303 | device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 304 | |
| 305 | actmon_write_barrier(tegra); |
| 306 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 307 | spin_unlock_irqrestore(&dev->lock, flags); |
| 308 | } |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 309 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 310 | static irqreturn_t actmon_isr(int irq, void *data) |
| 311 | { |
| 312 | struct tegra_devfreq *tegra = data; |
| 313 | bool handled = false; |
| 314 | unsigned int i; |
| 315 | u32 val; |
| 316 | |
| 317 | val = actmon_readl(tegra, ACTMON_GLB_STATUS); |
| 318 | for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) { |
| 319 | if (val & tegra->devices[i].config->irq_mask) { |
| 320 | actmon_isr_device(tegra, tegra->devices + i); |
| 321 | handled = true; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | return handled ? IRQ_WAKE_THREAD : IRQ_NONE; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra, |
| 329 | unsigned long cpu_freq) |
| 330 | { |
| 331 | unsigned int i; |
| 332 | struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios; |
| 333 | |
| 334 | for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) { |
| 335 | if (cpu_freq >= ratio->cpu_freq) { |
| 336 | if (ratio->emc_freq >= tegra->max_freq) |
| 337 | return tegra->max_freq; |
| 338 | else |
| 339 | return ratio->emc_freq; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | static void actmon_update_target(struct tegra_devfreq *tegra, |
| 347 | struct tegra_devfreq_device *dev) |
| 348 | { |
| 349 | unsigned long cpu_freq = 0; |
| 350 | unsigned long static_cpu_emc_freq = 0; |
| 351 | unsigned int avg_sustain_coef; |
| 352 | unsigned long flags; |
| 353 | |
| 354 | if (dev->config->avg_dependency_threshold) { |
| 355 | cpu_freq = cpufreq_get(0); |
| 356 | static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq); |
| 357 | } |
| 358 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 359 | spin_lock_irqsave(&dev->lock, flags); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 360 | |
| 361 | dev->target_freq = dev->avg_count / ACTMON_SAMPLING_PERIOD; |
| 362 | avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold; |
| 363 | dev->target_freq = do_percent(dev->target_freq, avg_sustain_coef); |
| 364 | dev->target_freq += dev->boost_freq; |
| 365 | |
| 366 | if (dev->avg_count >= dev->config->avg_dependency_threshold) |
| 367 | dev->target_freq = max(dev->target_freq, static_cpu_emc_freq); |
| 368 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 369 | spin_unlock_irqrestore(&dev->lock, flags); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static irqreturn_t actmon_thread_isr(int irq, void *data) |
| 373 | { |
| 374 | struct tegra_devfreq *tegra = data; |
| 375 | |
| 376 | mutex_lock(&tegra->devfreq->lock); |
| 377 | update_devfreq(tegra->devfreq); |
| 378 | mutex_unlock(&tegra->devfreq->lock); |
| 379 | |
| 380 | return IRQ_HANDLED; |
| 381 | } |
| 382 | |
| 383 | static int tegra_actmon_rate_notify_cb(struct notifier_block *nb, |
| 384 | unsigned long action, void *ptr) |
| 385 | { |
| 386 | struct clk_notifier_data *data = ptr; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 387 | struct tegra_devfreq *tegra; |
| 388 | struct tegra_devfreq_device *dev; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 389 | unsigned int i; |
| 390 | unsigned long flags; |
| 391 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 392 | if (action != POST_RATE_CHANGE) |
| 393 | return NOTIFY_OK; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 394 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 395 | tegra = container_of(nb, struct tegra_devfreq, rate_change_nb); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 396 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 397 | tegra->cur_freq = data->new_rate / KHZ; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 398 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 399 | for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) { |
| 400 | dev = &tegra->devices[i]; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 401 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 402 | spin_lock_irqsave(&dev->lock, flags); |
| 403 | tegra_devfreq_update_wmark(tegra, dev); |
| 404 | spin_unlock_irqrestore(&dev->lock, flags); |
| 405 | } |
| 406 | |
| 407 | actmon_write_barrier(tegra); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 408 | |
| 409 | return NOTIFY_OK; |
| 410 | } |
| 411 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 412 | static void tegra_actmon_enable_interrupts(struct tegra_devfreq *tegra) |
| 413 | { |
| 414 | struct tegra_devfreq_device *dev; |
| 415 | u32 val; |
| 416 | unsigned int i; |
| 417 | |
| 418 | for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) { |
| 419 | dev = &tegra->devices[i]; |
| 420 | |
| 421 | val = device_readl(dev, ACTMON_DEV_CTRL); |
| 422 | val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN; |
| 423 | val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN; |
| 424 | val |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN; |
| 425 | val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN; |
| 426 | |
| 427 | device_writel(dev, val, ACTMON_DEV_CTRL); |
| 428 | } |
| 429 | |
| 430 | actmon_write_barrier(tegra); |
| 431 | } |
| 432 | |
| 433 | static void tegra_actmon_disable_interrupts(struct tegra_devfreq *tegra) |
| 434 | { |
| 435 | struct tegra_devfreq_device *dev; |
| 436 | u32 val; |
| 437 | unsigned int i; |
| 438 | |
| 439 | for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) { |
| 440 | dev = &tegra->devices[i]; |
| 441 | |
| 442 | val = device_readl(dev, ACTMON_DEV_CTRL); |
| 443 | val &= ~ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN; |
| 444 | val &= ~ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN; |
| 445 | val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN; |
| 446 | val &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN; |
| 447 | |
| 448 | device_writel(dev, val, ACTMON_DEV_CTRL); |
| 449 | } |
| 450 | |
| 451 | actmon_write_barrier(tegra); |
| 452 | } |
| 453 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 454 | static void tegra_actmon_configure_device(struct tegra_devfreq *tegra, |
| 455 | struct tegra_devfreq_device *dev) |
| 456 | { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 457 | u32 val = 0; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 458 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 459 | dev->target_freq = tegra->cur_freq; |
| 460 | |
| 461 | dev->avg_count = tegra->cur_freq * ACTMON_SAMPLING_PERIOD; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 462 | device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 463 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 464 | tegra_devfreq_update_avg_wmark(tegra, dev); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 465 | tegra_devfreq_update_wmark(tegra, dev); |
| 466 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 467 | device_writel(dev, ACTMON_COUNT_WEIGHT, ACTMON_DEV_COUNT_WEIGHT); |
| 468 | device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 469 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 470 | val |= ACTMON_DEV_CTRL_ENB_PERIODIC; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 471 | val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1) |
| 472 | << ACTMON_DEV_CTRL_K_VAL_SHIFT; |
| 473 | val |= (ACTMON_BELOW_WMARK_WINDOW - 1) |
| 474 | << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT; |
| 475 | val |= (ACTMON_ABOVE_WMARK_WINDOW - 1) |
| 476 | << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 477 | val |= ACTMON_DEV_CTRL_ENB; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 478 | |
| 479 | device_writel(dev, val, ACTMON_DEV_CTRL); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 480 | |
| 481 | actmon_write_barrier(tegra); |
| 482 | } |
| 483 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 484 | static int tegra_devfreq_target(struct device *dev, unsigned long *freq, |
| 485 | u32 flags) |
| 486 | { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 487 | struct tegra_devfreq *tegra = dev_get_drvdata(dev); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 488 | struct dev_pm_opp *opp; |
Dmitry Osipenko | 42b888f | 2019-05-02 02:38:00 +0300 | [diff] [blame] | 489 | unsigned long rate; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 490 | |
Dmitry Osipenko | 42b888f | 2019-05-02 02:38:00 +0300 | [diff] [blame] | 491 | opp = devfreq_recommended_opp(dev, freq, flags); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 492 | if (IS_ERR(opp)) { |
Dmitry Osipenko | 42b888f | 2019-05-02 02:38:00 +0300 | [diff] [blame] | 493 | dev_err(dev, "Failed to find opp for %lu Hz\n", *freq); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 494 | return PTR_ERR(opp); |
| 495 | } |
| 496 | rate = dev_pm_opp_get_freq(opp); |
Viresh Kumar | 8a31d9d9 | 2017-01-23 10:11:47 +0530 | [diff] [blame] | 497 | dev_pm_opp_put(opp); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 498 | |
Tomeu Vizoso | c70eea7 | 2015-03-17 10:36:14 +0100 | [diff] [blame] | 499 | clk_set_min_rate(tegra->emc_clock, rate); |
| 500 | clk_set_rate(tegra->emc_clock, 0); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 501 | |
| 502 | return 0; |
| 503 | } |
| 504 | |
| 505 | static int tegra_devfreq_get_dev_status(struct device *dev, |
| 506 | struct devfreq_dev_status *stat) |
| 507 | { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 508 | struct tegra_devfreq *tegra = dev_get_drvdata(dev); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 509 | struct tegra_devfreq_device *actmon_dev; |
| 510 | |
Dmitry Osipenko | 42b888f | 2019-05-02 02:38:00 +0300 | [diff] [blame] | 511 | stat->current_frequency = tegra->cur_freq * KHZ; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 512 | |
| 513 | /* To be used by the tegra governor */ |
| 514 | stat->private_data = tegra; |
| 515 | |
| 516 | /* The below are to be used by the other governors */ |
| 517 | |
| 518 | actmon_dev = &tegra->devices[MCALL]; |
| 519 | |
| 520 | /* Number of cycles spent on memory access */ |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 521 | stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 522 | |
| 523 | /* The bus can be considered to be saturated way before 100% */ |
| 524 | stat->busy_time *= 100 / BUS_SATURATION_RATIO; |
| 525 | |
| 526 | /* Number of cycles in a sampling period */ |
| 527 | stat->total_time = ACTMON_SAMPLING_PERIOD * tegra->cur_freq; |
| 528 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 529 | stat->busy_time = min(stat->busy_time, stat->total_time); |
| 530 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 534 | static struct devfreq_dev_profile tegra_devfreq_profile = { |
| 535 | .polling_ms = 0, |
| 536 | .target = tegra_devfreq_target, |
| 537 | .get_dev_status = tegra_devfreq_get_dev_status, |
| 538 | }; |
| 539 | |
| 540 | static int tegra_governor_get_target(struct devfreq *devfreq, |
| 541 | unsigned long *freq) |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 542 | { |
MyungJoo Ham | 14de390 | 2015-08-18 13:47:41 +0900 | [diff] [blame] | 543 | struct devfreq_dev_status *stat; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 544 | struct tegra_devfreq *tegra; |
| 545 | struct tegra_devfreq_device *dev; |
| 546 | unsigned long target_freq = 0; |
| 547 | unsigned int i; |
| 548 | int err; |
| 549 | |
MyungJoo Ham | 14de390 | 2015-08-18 13:47:41 +0900 | [diff] [blame] | 550 | err = devfreq_update_stats(devfreq); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 551 | if (err) |
| 552 | return err; |
| 553 | |
MyungJoo Ham | 14de390 | 2015-08-18 13:47:41 +0900 | [diff] [blame] | 554 | stat = &devfreq->last_status; |
| 555 | |
| 556 | tegra = stat->private_data; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 557 | |
| 558 | for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) { |
| 559 | dev = &tegra->devices[i]; |
| 560 | |
| 561 | actmon_update_target(tegra, dev); |
| 562 | |
| 563 | target_freq = max(target_freq, dev->target_freq); |
| 564 | } |
| 565 | |
Dmitry Osipenko | 42b888f | 2019-05-02 02:38:00 +0300 | [diff] [blame] | 566 | *freq = target_freq * KHZ; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 567 | |
| 568 | return 0; |
| 569 | } |
| 570 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 571 | static int tegra_governor_event_handler(struct devfreq *devfreq, |
| 572 | unsigned int event, void *data) |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 573 | { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 574 | struct tegra_devfreq *tegra; |
| 575 | int ret = 0; |
| 576 | |
| 577 | tegra = dev_get_drvdata(devfreq->dev.parent); |
| 578 | |
| 579 | switch (event) { |
| 580 | case DEVFREQ_GOV_START: |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 581 | devfreq_monitor_start(devfreq); |
Tomeu Vizoso | 34ed504 | 2015-03-17 10:36:17 +0100 | [diff] [blame] | 582 | tegra_actmon_enable_interrupts(tegra); |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 583 | break; |
| 584 | |
| 585 | case DEVFREQ_GOV_STOP: |
| 586 | tegra_actmon_disable_interrupts(tegra); |
| 587 | devfreq_monitor_stop(devfreq); |
| 588 | break; |
| 589 | |
| 590 | case DEVFREQ_GOV_SUSPEND: |
| 591 | tegra_actmon_disable_interrupts(tegra); |
| 592 | devfreq_monitor_suspend(devfreq); |
| 593 | break; |
| 594 | |
| 595 | case DEVFREQ_GOV_RESUME: |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 596 | devfreq_monitor_resume(devfreq); |
Tomeu Vizoso | 34ed504 | 2015-03-17 10:36:17 +0100 | [diff] [blame] | 597 | tegra_actmon_enable_interrupts(tegra); |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 598 | break; |
| 599 | } |
| 600 | |
| 601 | return ret; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | static struct devfreq_governor tegra_devfreq_governor = { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 605 | .name = "tegra_actmon", |
| 606 | .get_target_freq = tegra_governor_get_target, |
| 607 | .event_handler = tegra_governor_event_handler, |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 608 | }; |
| 609 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 610 | static int tegra_devfreq_probe(struct platform_device *pdev) |
| 611 | { |
| 612 | struct tegra_devfreq *tegra; |
| 613 | struct tegra_devfreq_device *dev; |
| 614 | struct resource *res; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 615 | unsigned int i; |
Tomeu Vizoso | 5d498b4 | 2015-03-17 10:36:15 +0100 | [diff] [blame] | 616 | unsigned long rate; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 617 | int irq; |
| 618 | int err; |
| 619 | |
| 620 | tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL); |
| 621 | if (!tegra) |
| 622 | return -ENOMEM; |
| 623 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 624 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 625 | |
| 626 | tegra->regs = devm_ioremap_resource(&pdev->dev, res); |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 627 | if (IS_ERR(tegra->regs)) |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 628 | return PTR_ERR(tegra->regs); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 629 | |
| 630 | tegra->reset = devm_reset_control_get(&pdev->dev, "actmon"); |
| 631 | if (IS_ERR(tegra->reset)) { |
| 632 | dev_err(&pdev->dev, "Failed to get reset\n"); |
| 633 | return PTR_ERR(tegra->reset); |
| 634 | } |
| 635 | |
| 636 | tegra->clock = devm_clk_get(&pdev->dev, "actmon"); |
| 637 | if (IS_ERR(tegra->clock)) { |
| 638 | dev_err(&pdev->dev, "Failed to get actmon clock\n"); |
| 639 | return PTR_ERR(tegra->clock); |
| 640 | } |
| 641 | |
| 642 | tegra->emc_clock = devm_clk_get(&pdev->dev, "emc"); |
| 643 | if (IS_ERR(tegra->emc_clock)) { |
| 644 | dev_err(&pdev->dev, "Failed to get emc clock\n"); |
| 645 | return PTR_ERR(tegra->emc_clock); |
| 646 | } |
| 647 | |
Tomeu Vizoso | c70eea7 | 2015-03-17 10:36:14 +0100 | [diff] [blame] | 648 | clk_set_rate(tegra->emc_clock, ULONG_MAX); |
| 649 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 650 | tegra->rate_change_nb.notifier_call = tegra_actmon_rate_notify_cb; |
| 651 | err = clk_notifier_register(tegra->emc_clock, &tegra->rate_change_nb); |
| 652 | if (err) { |
| 653 | dev_err(&pdev->dev, |
| 654 | "Failed to register rate change notifier\n"); |
| 655 | return err; |
| 656 | } |
| 657 | |
| 658 | reset_control_assert(tegra->reset); |
| 659 | |
| 660 | err = clk_prepare_enable(tegra->clock); |
| 661 | if (err) { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 662 | dev_err(&pdev->dev, |
| 663 | "Failed to prepare and enable ACTMON clock\n"); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 664 | return err; |
| 665 | } |
| 666 | |
| 667 | reset_control_deassert(tegra->reset); |
| 668 | |
Tomeu Vizoso | c70eea7 | 2015-03-17 10:36:14 +0100 | [diff] [blame] | 669 | tegra->max_freq = clk_round_rate(tegra->emc_clock, ULONG_MAX) / KHZ; |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 670 | tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ; |
| 671 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 672 | actmon_writel(tegra, ACTMON_SAMPLING_PERIOD - 1, |
| 673 | ACTMON_GLB_PERIOD_CTRL); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 674 | |
| 675 | for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) { |
| 676 | dev = tegra->devices + i; |
| 677 | dev->config = actmon_device_configs + i; |
| 678 | dev->regs = tegra->regs + dev->config->offset; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 679 | spin_lock_init(&dev->lock); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 680 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 681 | tegra_actmon_configure_device(tegra, dev); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 682 | } |
| 683 | |
Tomeu Vizoso | 5d498b4 | 2015-03-17 10:36:15 +0100 | [diff] [blame] | 684 | for (rate = 0; rate <= tegra->max_freq * KHZ; rate++) { |
| 685 | rate = clk_round_rate(tegra->emc_clock, rate); |
| 686 | dev_pm_opp_add(&pdev->dev, rate, 0); |
| 687 | } |
| 688 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 689 | irq = platform_get_irq(pdev, 0); |
Gustavo A. R. Silva | 9e578b3 | 2017-07-03 07:47:38 -0500 | [diff] [blame] | 690 | if (irq < 0) { |
| 691 | dev_err(&pdev->dev, "Failed to get IRQ: %d\n", irq); |
| 692 | return irq; |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 693 | } |
| 694 | |
Tomeu Vizoso | 2da19b1 | 2015-03-17 10:36:16 +0100 | [diff] [blame] | 695 | platform_set_drvdata(pdev, tegra); |
| 696 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 697 | err = devm_request_threaded_irq(&pdev->dev, irq, actmon_isr, |
| 698 | actmon_thread_isr, IRQF_SHARED, |
| 699 | "tegra-devfreq", tegra); |
| 700 | if (err) { |
| 701 | dev_err(&pdev->dev, "Interrupt request failed\n"); |
| 702 | return err; |
| 703 | } |
| 704 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 705 | tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock); |
| 706 | tegra->devfreq = devm_devfreq_add_device(&pdev->dev, |
| 707 | &tegra_devfreq_profile, |
| 708 | "tegra_actmon", |
| 709 | NULL); |
| 710 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 711 | return 0; |
| 712 | } |
| 713 | |
| 714 | static int tegra_devfreq_remove(struct platform_device *pdev) |
| 715 | { |
| 716 | struct tegra_devfreq *tegra = platform_get_drvdata(pdev); |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 717 | int irq = platform_get_irq(pdev, 0); |
| 718 | u32 val; |
| 719 | unsigned int i; |
| 720 | |
| 721 | for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) { |
| 722 | val = device_readl(&tegra->devices[i], ACTMON_DEV_CTRL); |
| 723 | val &= ~ACTMON_DEV_CTRL_ENB; |
| 724 | device_writel(&tegra->devices[i], val, ACTMON_DEV_CTRL); |
| 725 | } |
| 726 | |
| 727 | actmon_write_barrier(tegra); |
| 728 | |
| 729 | devm_free_irq(&pdev->dev, irq, tegra); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 730 | |
| 731 | clk_notifier_unregister(tegra->emc_clock, &tegra->rate_change_nb); |
| 732 | |
| 733 | clk_disable_unprepare(tegra->clock); |
| 734 | |
| 735 | return 0; |
| 736 | } |
| 737 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 738 | static const struct of_device_id tegra_devfreq_of_match[] = { |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 739 | { .compatible = "nvidia,tegra124-actmon" }, |
| 740 | { }, |
| 741 | }; |
| 742 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 743 | MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match); |
| 744 | |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 745 | static struct platform_driver tegra_devfreq_driver = { |
| 746 | .probe = tegra_devfreq_probe, |
| 747 | .remove = tegra_devfreq_remove, |
| 748 | .driver = { |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 749 | .name = "tegra-devfreq", |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 750 | .of_match_table = tegra_devfreq_of_match, |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 751 | }, |
| 752 | }; |
Tomeu Vizoso | 358b615 | 2015-03-30 17:33:23 +0200 | [diff] [blame] | 753 | |
| 754 | static int __init tegra_devfreq_init(void) |
| 755 | { |
| 756 | int ret = 0; |
| 757 | |
| 758 | ret = devfreq_add_governor(&tegra_devfreq_governor); |
| 759 | if (ret) { |
| 760 | pr_err("%s: failed to add governor: %d\n", __func__, ret); |
| 761 | return ret; |
| 762 | } |
| 763 | |
| 764 | ret = platform_driver_register(&tegra_devfreq_driver); |
| 765 | if (ret) |
| 766 | devfreq_remove_governor(&tegra_devfreq_governor); |
| 767 | |
| 768 | return ret; |
| 769 | } |
| 770 | module_init(tegra_devfreq_init) |
| 771 | |
| 772 | static void __exit tegra_devfreq_exit(void) |
| 773 | { |
| 774 | int ret = 0; |
| 775 | |
| 776 | platform_driver_unregister(&tegra_devfreq_driver); |
| 777 | |
| 778 | ret = devfreq_remove_governor(&tegra_devfreq_governor); |
| 779 | if (ret) |
| 780 | pr_err("%s: failed to remove governor: %d\n", __func__, ret); |
| 781 | } |
| 782 | module_exit(tegra_devfreq_exit) |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 783 | |
Tomeu Vizoso | 11573e9 | 2015-03-17 10:36:12 +0100 | [diff] [blame] | 784 | MODULE_LICENSE("GPL v2"); |
Tomeu Vizoso | 6234f38 | 2014-11-24 13:28:17 +0100 | [diff] [blame] | 785 | MODULE_DESCRIPTION("Tegra devfreq driver"); |
| 786 | MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>"); |