1 // SPDX-License-Identifier: GPL-2.0-only
3 * A devfreq driver for NVIDIA Tegra SoCs
5 * Copyright (c) 2014 NVIDIA CORPORATION. All rights reserved.
6 * Copyright (C) 2014 Google, Inc
10 #include <linux/cpufreq.h>
11 #include <linux/devfreq.h>
12 #include <linux/interrupt.h>
14 #include <linux/irq.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_opp.h>
19 #include <linux/reset.h>
20 #include <linux/workqueue.h>
24 #define ACTMON_GLB_STATUS 0x0
25 #define ACTMON_GLB_PERIOD_CTRL 0x4
27 #define ACTMON_DEV_CTRL 0x0
28 #define ACTMON_DEV_CTRL_K_VAL_SHIFT 10
29 #define ACTMON_DEV_CTRL_ENB_PERIODIC BIT(18)
30 #define ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN BIT(20)
31 #define ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN BIT(21)
32 #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT 23
33 #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT 26
34 #define ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN BIT(29)
35 #define ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN BIT(30)
36 #define ACTMON_DEV_CTRL_ENB BIT(31)
38 #define ACTMON_DEV_CTRL_STOP 0x00000000
40 #define ACTMON_DEV_UPPER_WMARK 0x4
41 #define ACTMON_DEV_LOWER_WMARK 0x8
42 #define ACTMON_DEV_INIT_AVG 0xc
43 #define ACTMON_DEV_AVG_UPPER_WMARK 0x10
44 #define ACTMON_DEV_AVG_LOWER_WMARK 0x14
45 #define ACTMON_DEV_COUNT_WEIGHT 0x18
46 #define ACTMON_DEV_AVG_COUNT 0x20
47 #define ACTMON_DEV_INTR_STATUS 0x24
49 #define ACTMON_INTR_STATUS_CLEAR 0xffffffff
51 #define ACTMON_DEV_INTR_CONSECUTIVE_UPPER BIT(31)
52 #define ACTMON_DEV_INTR_CONSECUTIVE_LOWER BIT(30)
54 #define ACTMON_ABOVE_WMARK_WINDOW 1
55 #define ACTMON_BELOW_WMARK_WINDOW 3
56 #define ACTMON_BOOST_FREQ_STEP 16000
59 * Activity counter is incremented every 256 memory transactions, and each
60 * transaction takes 4 EMC clocks for Tegra124; So the COUNT_WEIGHT is
63 #define ACTMON_COUNT_WEIGHT 0x400
66 * ACTMON_AVERAGE_WINDOW_LOG2: default value for @DEV_CTRL_K_VAL, which
67 * translates to 2 ^ (K_VAL + 1). ex: 2 ^ (6 + 1) = 128
69 #define ACTMON_AVERAGE_WINDOW_LOG2 6
70 #define ACTMON_SAMPLING_PERIOD 12 /* ms */
71 #define ACTMON_DEFAULT_AVG_BAND 6 /* 1/10 of % */
75 #define KHZ_MAX (ULONG_MAX / KHZ)
77 /* Assume that the bus is saturated if the utilization is 25% */
78 #define BUS_SATURATION_RATIO 25
81 * struct tegra_devfreq_device_config - configuration specific to an ACTMON
84 * Coefficients and thresholds are percentages unless otherwise noted
86 struct tegra_devfreq_device_config {
90 /* Factors applied to boost_freq every consecutive watermark breach */
91 unsigned int boost_up_coeff;
92 unsigned int boost_down_coeff;
94 /* Define the watermark bounds when applied to the current avg */
95 unsigned int boost_up_threshold;
96 unsigned int boost_down_threshold;
99 * Threshold of activity (cycles) below which the CPU frequency isn't
100 * to be taken into account. This is to avoid increasing the EMC
101 * frequency when the CPU is very busy but not accessing the bus often.
103 u32 avg_dependency_threshold;
106 enum tegra_actmon_device {
111 static const struct tegra_devfreq_device_config actmon_device_configs[] = {
113 /* MCALL: All memory accesses (including from the CPUs) */
116 .boost_up_coeff = 200,
117 .boost_down_coeff = 50,
118 .boost_up_threshold = 60,
119 .boost_down_threshold = 40,
122 /* MCCPU: memory accesses from the CPUs */
125 .boost_up_coeff = 800,
126 .boost_down_coeff = 90,
127 .boost_up_threshold = 27,
128 .boost_down_threshold = 10,
129 .avg_dependency_threshold = 50000,
134 * struct tegra_devfreq_device - state specific to an ACTMON device
136 * Frequencies are in kHz.
138 struct tegra_devfreq_device {
139 const struct tegra_devfreq_device_config *config;
142 /* Average event count sampled in the last interrupt */
146 * Extra frequency to increase the target by due to consecutive
147 * watermark breaches.
149 unsigned long boost_freq;
151 /* Optimal frequency calculated from the stats for this device */
152 unsigned long target_freq;
155 struct tegra_devfreq {
156 struct devfreq *devfreq;
158 struct reset_control *reset;
162 struct clk *emc_clock;
163 unsigned long max_freq;
164 unsigned long cur_freq;
165 struct notifier_block clk_rate_change_nb;
167 struct delayed_work cpufreq_update_work;
168 struct notifier_block cpu_rate_change_nb;
170 struct tegra_devfreq_device devices[ARRAY_SIZE(actmon_device_configs)];
175 struct tegra_actmon_emc_ratio {
176 unsigned long cpu_freq;
177 unsigned long emc_freq;
180 static const struct tegra_actmon_emc_ratio actmon_emc_ratios[] = {
181 { 1400000, KHZ_MAX },
190 static u32 actmon_readl(struct tegra_devfreq *tegra, u32 offset)
192 return readl_relaxed(tegra->regs + offset);
195 static void actmon_writel(struct tegra_devfreq *tegra, u32 val, u32 offset)
197 writel_relaxed(val, tegra->regs + offset);
200 static u32 device_readl(struct tegra_devfreq_device *dev, u32 offset)
202 return readl_relaxed(dev->regs + offset);
205 static void device_writel(struct tegra_devfreq_device *dev, u32 val,
208 writel_relaxed(val, dev->regs + offset);
211 static unsigned long do_percent(unsigned long val, unsigned int pct)
213 return val * pct / 100;
216 static void tegra_devfreq_update_avg_wmark(struct tegra_devfreq *tegra,
217 struct tegra_devfreq_device *dev)
219 u32 avg = dev->avg_count;
220 u32 avg_band_freq = tegra->max_freq * ACTMON_DEFAULT_AVG_BAND / KHZ;
221 u32 band = avg_band_freq * ACTMON_SAMPLING_PERIOD;
223 device_writel(dev, avg + band, ACTMON_DEV_AVG_UPPER_WMARK);
225 avg = max(dev->avg_count, band);
226 device_writel(dev, avg - band, ACTMON_DEV_AVG_LOWER_WMARK);
229 static void tegra_devfreq_update_wmark(struct tegra_devfreq *tegra,
230 struct tegra_devfreq_device *dev)
232 u32 val = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
234 device_writel(dev, do_percent(val, dev->config->boost_up_threshold),
235 ACTMON_DEV_UPPER_WMARK);
237 device_writel(dev, do_percent(val, dev->config->boost_down_threshold),
238 ACTMON_DEV_LOWER_WMARK);
241 static void actmon_isr_device(struct tegra_devfreq *tegra,
242 struct tegra_devfreq_device *dev)
244 u32 intr_status, dev_ctrl;
246 dev->avg_count = device_readl(dev, ACTMON_DEV_AVG_COUNT);
247 tegra_devfreq_update_avg_wmark(tegra, dev);
249 intr_status = device_readl(dev, ACTMON_DEV_INTR_STATUS);
250 dev_ctrl = device_readl(dev, ACTMON_DEV_CTRL);
252 if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_UPPER) {
254 * new_boost = min(old_boost * up_coef + step, max_freq)
256 dev->boost_freq = do_percent(dev->boost_freq,
257 dev->config->boost_up_coeff);
258 dev->boost_freq += ACTMON_BOOST_FREQ_STEP;
260 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
262 if (dev->boost_freq >= tegra->max_freq) {
263 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
264 dev->boost_freq = tegra->max_freq;
266 } else if (intr_status & ACTMON_DEV_INTR_CONSECUTIVE_LOWER) {
268 * new_boost = old_boost * down_coef
269 * or 0 if (old_boost * down_coef < step / 2)
271 dev->boost_freq = do_percent(dev->boost_freq,
272 dev->config->boost_down_coeff);
274 dev_ctrl |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
276 if (dev->boost_freq < (ACTMON_BOOST_FREQ_STEP >> 1)) {
277 dev_ctrl &= ~ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_EN;
282 device_writel(dev, dev_ctrl, ACTMON_DEV_CTRL);
284 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
287 static unsigned long actmon_cpu_to_emc_rate(struct tegra_devfreq *tegra,
288 unsigned long cpu_freq)
291 const struct tegra_actmon_emc_ratio *ratio = actmon_emc_ratios;
293 for (i = 0; i < ARRAY_SIZE(actmon_emc_ratios); i++, ratio++) {
294 if (cpu_freq >= ratio->cpu_freq) {
295 if (ratio->emc_freq >= tegra->max_freq)
296 return tegra->max_freq;
298 return ratio->emc_freq;
305 static unsigned long actmon_device_target_freq(struct tegra_devfreq *tegra,
306 struct tegra_devfreq_device *dev)
308 unsigned int avg_sustain_coef;
309 unsigned long target_freq;
311 target_freq = dev->avg_count / ACTMON_SAMPLING_PERIOD;
312 avg_sustain_coef = 100 * 100 / dev->config->boost_up_threshold;
313 target_freq = do_percent(target_freq, avg_sustain_coef);
314 target_freq += dev->boost_freq;
319 static void actmon_update_target(struct tegra_devfreq *tegra,
320 struct tegra_devfreq_device *dev)
322 unsigned long cpu_freq = 0;
323 unsigned long static_cpu_emc_freq = 0;
325 if (dev->config->avg_dependency_threshold) {
326 cpu_freq = cpufreq_quick_get(0);
327 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
330 dev->target_freq = actmon_device_target_freq(tegra, dev);
332 if (dev->avg_count >= dev->config->avg_dependency_threshold)
333 dev->target_freq = max(dev->target_freq, static_cpu_emc_freq);
336 static irqreturn_t actmon_thread_isr(int irq, void *data)
338 struct tegra_devfreq *tegra = data;
339 bool handled = false;
343 mutex_lock(&tegra->devfreq->lock);
345 val = actmon_readl(tegra, ACTMON_GLB_STATUS);
346 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
347 if (val & tegra->devices[i].config->irq_mask) {
348 actmon_isr_device(tegra, tegra->devices + i);
354 update_devfreq(tegra->devfreq);
356 mutex_unlock(&tegra->devfreq->lock);
358 return handled ? IRQ_HANDLED : IRQ_NONE;
361 static int tegra_actmon_clk_notify_cb(struct notifier_block *nb,
362 unsigned long action, void *ptr)
364 struct clk_notifier_data *data = ptr;
365 struct tegra_devfreq *tegra;
366 struct tegra_devfreq_device *dev;
369 if (action != POST_RATE_CHANGE)
372 tegra = container_of(nb, struct tegra_devfreq, clk_rate_change_nb);
374 tegra->cur_freq = data->new_rate / KHZ;
376 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
377 dev = &tegra->devices[i];
379 tegra_devfreq_update_wmark(tegra, dev);
385 static void tegra_actmon_delayed_update(struct work_struct *work)
387 struct tegra_devfreq *tegra = container_of(work, struct tegra_devfreq,
388 cpufreq_update_work.work);
390 mutex_lock(&tegra->devfreq->lock);
391 update_devfreq(tegra->devfreq);
392 mutex_unlock(&tegra->devfreq->lock);
396 tegra_actmon_cpufreq_contribution(struct tegra_devfreq *tegra,
397 unsigned int cpu_freq)
399 unsigned long static_cpu_emc_freq, dev_freq;
401 /* check whether CPU's freq is taken into account at all */
402 if (tegra->devices[MCCPU].avg_count <
403 tegra->devices[MCCPU].config->avg_dependency_threshold)
406 static_cpu_emc_freq = actmon_cpu_to_emc_rate(tegra, cpu_freq);
407 dev_freq = actmon_device_target_freq(tegra, &tegra->devices[MCCPU]);
409 if (dev_freq >= static_cpu_emc_freq)
412 return static_cpu_emc_freq;
415 static int tegra_actmon_cpu_notify_cb(struct notifier_block *nb,
416 unsigned long action, void *ptr)
418 struct cpufreq_freqs *freqs = ptr;
419 struct tegra_devfreq *tegra;
420 unsigned long old, new, delay;
422 if (action != CPUFREQ_POSTCHANGE)
425 tegra = container_of(nb, struct tegra_devfreq, cpu_rate_change_nb);
428 * Quickly check whether CPU frequency should be taken into account
429 * at all, without blocking CPUFreq's core.
431 if (mutex_trylock(&tegra->devfreq->lock)) {
432 old = tegra_actmon_cpufreq_contribution(tegra, freqs->old);
433 new = tegra_actmon_cpufreq_contribution(tegra, freqs->new);
434 mutex_unlock(&tegra->devfreq->lock);
437 * If CPU's frequency shouldn't be taken into account at
438 * the moment, then there is no need to update the devfreq's
439 * state because ISR will re-check CPU's frequency on the
447 * CPUFreq driver should support CPUFREQ_ASYNC_NOTIFICATION in order
448 * to allow asynchronous notifications. This means we can't block
449 * here for too long, otherwise CPUFreq's core will complain with a
452 delay = msecs_to_jiffies(ACTMON_SAMPLING_PERIOD);
453 schedule_delayed_work(&tegra->cpufreq_update_work, delay);
458 static void tegra_actmon_configure_device(struct tegra_devfreq *tegra,
459 struct tegra_devfreq_device *dev)
463 /* reset boosting on governor's restart */
466 dev->target_freq = tegra->cur_freq;
468 dev->avg_count = tegra->cur_freq * ACTMON_SAMPLING_PERIOD;
469 device_writel(dev, dev->avg_count, ACTMON_DEV_INIT_AVG);
471 tegra_devfreq_update_avg_wmark(tegra, dev);
472 tegra_devfreq_update_wmark(tegra, dev);
474 device_writel(dev, ACTMON_COUNT_WEIGHT, ACTMON_DEV_COUNT_WEIGHT);
475 device_writel(dev, ACTMON_INTR_STATUS_CLEAR, ACTMON_DEV_INTR_STATUS);
477 val |= ACTMON_DEV_CTRL_ENB_PERIODIC;
478 val |= (ACTMON_AVERAGE_WINDOW_LOG2 - 1)
479 << ACTMON_DEV_CTRL_K_VAL_SHIFT;
480 val |= (ACTMON_BELOW_WMARK_WINDOW - 1)
481 << ACTMON_DEV_CTRL_CONSECUTIVE_BELOW_WMARK_NUM_SHIFT;
482 val |= (ACTMON_ABOVE_WMARK_WINDOW - 1)
483 << ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_NUM_SHIFT;
484 val |= ACTMON_DEV_CTRL_AVG_ABOVE_WMARK_EN;
485 val |= ACTMON_DEV_CTRL_AVG_BELOW_WMARK_EN;
486 val |= ACTMON_DEV_CTRL_CONSECUTIVE_ABOVE_WMARK_EN;
487 val |= ACTMON_DEV_CTRL_ENB;
489 device_writel(dev, val, ACTMON_DEV_CTRL);
492 static void tegra_actmon_stop_devices(struct tegra_devfreq *tegra)
494 struct tegra_devfreq_device *dev = tegra->devices;
497 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++, dev++) {
498 device_writel(dev, ACTMON_DEV_CTRL_STOP, ACTMON_DEV_CTRL);
499 device_writel(dev, ACTMON_INTR_STATUS_CLEAR,
500 ACTMON_DEV_INTR_STATUS);
504 static int tegra_actmon_start(struct tegra_devfreq *tegra)
509 actmon_writel(tegra, ACTMON_SAMPLING_PERIOD - 1,
510 ACTMON_GLB_PERIOD_CTRL);
513 * CLK notifications are needed in order to reconfigure the upper
514 * consecutive watermark in accordance to the actual clock rate
515 * to avoid unnecessary upper interrupts.
517 err = clk_notifier_register(tegra->emc_clock,
518 &tegra->clk_rate_change_nb);
520 dev_err(tegra->devfreq->dev.parent,
521 "Failed to register rate change notifier\n");
525 tegra->cur_freq = clk_get_rate(tegra->emc_clock) / KHZ;
527 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++)
528 tegra_actmon_configure_device(tegra, &tegra->devices[i]);
531 * We are estimating CPU's memory bandwidth requirement based on
532 * amount of memory accesses and system's load, judging by CPU's
533 * frequency. We also don't want to receive events about CPU's
534 * frequency transaction when governor is stopped, hence notifier
535 * is registered dynamically.
537 err = cpufreq_register_notifier(&tegra->cpu_rate_change_nb,
538 CPUFREQ_TRANSITION_NOTIFIER);
540 dev_err(tegra->devfreq->dev.parent,
541 "Failed to register rate change notifier: %d\n", err);
545 enable_irq(tegra->irq);
550 tegra_actmon_stop_devices(tegra);
552 clk_notifier_unregister(tegra->emc_clock, &tegra->clk_rate_change_nb);
557 static void tegra_actmon_stop(struct tegra_devfreq *tegra)
559 disable_irq(tegra->irq);
561 cpufreq_unregister_notifier(&tegra->cpu_rate_change_nb,
562 CPUFREQ_TRANSITION_NOTIFIER);
564 cancel_delayed_work_sync(&tegra->cpufreq_update_work);
566 tegra_actmon_stop_devices(tegra);
568 clk_notifier_unregister(tegra->emc_clock, &tegra->clk_rate_change_nb);
571 static int tegra_devfreq_target(struct device *dev, unsigned long *freq,
574 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
575 struct devfreq *devfreq = tegra->devfreq;
576 struct dev_pm_opp *opp;
580 opp = devfreq_recommended_opp(dev, freq, flags);
582 dev_err(dev, "Failed to find opp for %lu Hz\n", *freq);
585 rate = dev_pm_opp_get_freq(opp);
588 err = clk_set_min_rate(tegra->emc_clock, rate * KHZ);
592 err = clk_set_rate(tegra->emc_clock, 0);
594 goto restore_min_rate;
599 clk_set_min_rate(tegra->emc_clock, devfreq->previous_freq);
604 static int tegra_devfreq_get_dev_status(struct device *dev,
605 struct devfreq_dev_status *stat)
607 struct tegra_devfreq *tegra = dev_get_drvdata(dev);
608 struct tegra_devfreq_device *actmon_dev;
609 unsigned long cur_freq;
611 cur_freq = READ_ONCE(tegra->cur_freq);
613 /* To be used by the tegra governor */
614 stat->private_data = tegra;
616 /* The below are to be used by the other governors */
617 stat->current_frequency = cur_freq;
619 actmon_dev = &tegra->devices[MCALL];
621 /* Number of cycles spent on memory access */
622 stat->busy_time = device_readl(actmon_dev, ACTMON_DEV_AVG_COUNT);
624 /* The bus can be considered to be saturated way before 100% */
625 stat->busy_time *= 100 / BUS_SATURATION_RATIO;
627 /* Number of cycles in a sampling period */
628 stat->total_time = ACTMON_SAMPLING_PERIOD * cur_freq;
630 stat->busy_time = min(stat->busy_time, stat->total_time);
635 static struct devfreq_dev_profile tegra_devfreq_profile = {
637 .target = tegra_devfreq_target,
638 .get_dev_status = tegra_devfreq_get_dev_status,
641 static int tegra_governor_get_target(struct devfreq *devfreq,
644 struct devfreq_dev_status *stat;
645 struct tegra_devfreq *tegra;
646 struct tegra_devfreq_device *dev;
647 unsigned long target_freq = 0;
651 err = devfreq_update_stats(devfreq);
655 stat = &devfreq->last_status;
657 tegra = stat->private_data;
659 for (i = 0; i < ARRAY_SIZE(tegra->devices); i++) {
660 dev = &tegra->devices[i];
662 actmon_update_target(tegra, dev);
664 target_freq = max(target_freq, dev->target_freq);
672 static int tegra_governor_event_handler(struct devfreq *devfreq,
673 unsigned int event, void *data)
675 struct tegra_devfreq *tegra = dev_get_drvdata(devfreq->dev.parent);
679 * Couple devfreq-device with the governor early because it is
680 * needed at the moment of governor's start (used by ISR).
682 tegra->devfreq = devfreq;
685 case DEVFREQ_GOV_START:
686 devfreq_monitor_start(devfreq);
687 ret = tegra_actmon_start(tegra);
690 case DEVFREQ_GOV_STOP:
691 tegra_actmon_stop(tegra);
692 devfreq_monitor_stop(devfreq);
695 case DEVFREQ_GOV_SUSPEND:
696 tegra_actmon_stop(tegra);
697 devfreq_monitor_suspend(devfreq);
700 case DEVFREQ_GOV_RESUME:
701 devfreq_monitor_resume(devfreq);
702 ret = tegra_actmon_start(tegra);
709 static struct devfreq_governor tegra_devfreq_governor = {
710 .name = "tegra_actmon",
711 .get_target_freq = tegra_governor_get_target,
712 .event_handler = tegra_governor_event_handler,
716 static int tegra_devfreq_probe(struct platform_device *pdev)
718 struct tegra_devfreq_device *dev;
719 struct tegra_devfreq *tegra;
720 struct devfreq *devfreq;
725 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
729 tegra->regs = devm_platform_ioremap_resource(pdev, 0);
730 if (IS_ERR(tegra->regs))
731 return PTR_ERR(tegra->regs);
733 tegra->reset = devm_reset_control_get(&pdev->dev, "actmon");
734 if (IS_ERR(tegra->reset)) {
735 dev_err(&pdev->dev, "Failed to get reset\n");
736 return PTR_ERR(tegra->reset);
739 tegra->clock = devm_clk_get(&pdev->dev, "actmon");
740 if (IS_ERR(tegra->clock)) {
741 dev_err(&pdev->dev, "Failed to get actmon clock\n");
742 return PTR_ERR(tegra->clock);
745 tegra->emc_clock = devm_clk_get(&pdev->dev, "emc");
746 if (IS_ERR(tegra->emc_clock)) {
747 dev_err(&pdev->dev, "Failed to get emc clock\n");
748 return PTR_ERR(tegra->emc_clock);
751 err = platform_get_irq(pdev, 0);
753 dev_err(&pdev->dev, "Failed to get IRQ: %d\n", err);
758 irq_set_status_flags(tegra->irq, IRQ_NOAUTOEN);
760 err = devm_request_threaded_irq(&pdev->dev, tegra->irq, NULL,
761 actmon_thread_isr, IRQF_ONESHOT,
762 "tegra-devfreq", tegra);
764 dev_err(&pdev->dev, "Interrupt request failed: %d\n", err);
768 reset_control_assert(tegra->reset);
770 err = clk_prepare_enable(tegra->clock);
773 "Failed to prepare and enable ACTMON clock\n");
777 reset_control_deassert(tegra->reset);
779 rate = clk_round_rate(tegra->emc_clock, ULONG_MAX);
781 dev_err(&pdev->dev, "Failed to round clock rate: %ld\n", rate);
785 tegra->max_freq = rate / KHZ;
787 for (i = 0; i < ARRAY_SIZE(actmon_device_configs); i++) {
788 dev = tegra->devices + i;
789 dev->config = actmon_device_configs + i;
790 dev->regs = tegra->regs + dev->config->offset;
793 for (rate = 0; rate <= tegra->max_freq * KHZ; rate++) {
794 rate = clk_round_rate(tegra->emc_clock, rate);
798 "Failed to round clock rate: %ld\n", rate);
803 err = dev_pm_opp_add(&pdev->dev, rate / KHZ, 0);
805 dev_err(&pdev->dev, "Failed to add OPP: %d\n", err);
810 platform_set_drvdata(pdev, tegra);
812 tegra->clk_rate_change_nb.notifier_call = tegra_actmon_clk_notify_cb;
813 tegra->cpu_rate_change_nb.notifier_call = tegra_actmon_cpu_notify_cb;
815 INIT_DELAYED_WORK(&tegra->cpufreq_update_work,
816 tegra_actmon_delayed_update);
818 err = devfreq_add_governor(&tegra_devfreq_governor);
820 dev_err(&pdev->dev, "Failed to add governor: %d\n", err);
824 tegra_devfreq_profile.initial_freq = clk_get_rate(tegra->emc_clock);
825 tegra_devfreq_profile.initial_freq /= KHZ;
827 devfreq = devfreq_add_device(&pdev->dev, &tegra_devfreq_profile,
828 "tegra_actmon", NULL);
829 if (IS_ERR(devfreq)) {
830 err = PTR_ERR(devfreq);
831 goto remove_governor;
837 devfreq_remove_governor(&tegra_devfreq_governor);
840 dev_pm_opp_remove_all_dynamic(&pdev->dev);
842 reset_control_reset(tegra->reset);
843 clk_disable_unprepare(tegra->clock);
848 static int tegra_devfreq_remove(struct platform_device *pdev)
850 struct tegra_devfreq *tegra = platform_get_drvdata(pdev);
852 devfreq_remove_device(tegra->devfreq);
853 devfreq_remove_governor(&tegra_devfreq_governor);
855 dev_pm_opp_remove_all_dynamic(&pdev->dev);
857 reset_control_reset(tegra->reset);
858 clk_disable_unprepare(tegra->clock);
863 static const struct of_device_id tegra_devfreq_of_match[] = {
864 { .compatible = "nvidia,tegra30-actmon" },
865 { .compatible = "nvidia,tegra124-actmon" },
869 MODULE_DEVICE_TABLE(of, tegra_devfreq_of_match);
871 static struct platform_driver tegra_devfreq_driver = {
872 .probe = tegra_devfreq_probe,
873 .remove = tegra_devfreq_remove,
875 .name = "tegra-devfreq",
876 .of_match_table = tegra_devfreq_of_match,
879 module_platform_driver(tegra_devfreq_driver);
881 MODULE_LICENSE("GPL v2");
882 MODULE_DESCRIPTION("Tegra devfreq driver");
883 MODULE_AUTHOR("Tomeu Vizoso <tomeu.vizoso@collabora.com>");