1 // SPDX-License-Identifier: GPL-2.0
5 * Based on panfrost_devfreq.c:
6 * Copyright 2019 Collabora ltd.
9 #include <linux/devfreq.h>
10 #include <linux/devfreq_cooling.h>
11 #include <linux/device.h>
12 #include <linux/platform_device.h>
13 #include <linux/pm_opp.h>
14 #include <linux/property.h>
16 #include "lima_device.h"
17 #include "lima_devfreq.h"
19 static void lima_devfreq_update_utilization(struct lima_devfreq *devfreq)
24 last = devfreq->time_last_update;
26 if (devfreq->busy_count > 0)
27 devfreq->busy_time += ktime_sub(now, last);
29 devfreq->idle_time += ktime_sub(now, last);
31 devfreq->time_last_update = now;
34 static int lima_devfreq_target(struct device *dev, unsigned long *freq,
37 struct dev_pm_opp *opp;
39 opp = devfreq_recommended_opp(dev, freq, flags);
44 return dev_pm_opp_set_rate(dev, *freq);
47 static void lima_devfreq_reset(struct lima_devfreq *devfreq)
49 devfreq->busy_time = 0;
50 devfreq->idle_time = 0;
51 devfreq->time_last_update = ktime_get();
54 static int lima_devfreq_get_dev_status(struct device *dev,
55 struct devfreq_dev_status *status)
57 struct lima_device *ldev = dev_get_drvdata(dev);
58 struct lima_devfreq *devfreq = &ldev->devfreq;
59 unsigned long irqflags;
61 status->current_frequency = clk_get_rate(ldev->clk_gpu);
63 spin_lock_irqsave(&devfreq->lock, irqflags);
65 lima_devfreq_update_utilization(devfreq);
67 status->total_time = ktime_to_ns(ktime_add(devfreq->busy_time,
69 status->busy_time = ktime_to_ns(devfreq->busy_time);
71 lima_devfreq_reset(devfreq);
73 spin_unlock_irqrestore(&devfreq->lock, irqflags);
75 dev_dbg(ldev->dev, "busy %lu total %lu %lu %% freq %lu MHz\n",
76 status->busy_time, status->total_time,
77 status->busy_time / (status->total_time / 100),
78 status->current_frequency / 1000 / 1000);
83 static struct devfreq_dev_profile lima_devfreq_profile = {
84 .polling_ms = 50, /* ~3 frames */
85 .target = lima_devfreq_target,
86 .get_dev_status = lima_devfreq_get_dev_status,
89 void lima_devfreq_fini(struct lima_device *ldev)
91 struct lima_devfreq *devfreq = &ldev->devfreq;
93 if (devfreq->cooling) {
94 devfreq_cooling_unregister(devfreq->cooling);
95 devfreq->cooling = NULL;
98 if (devfreq->devfreq) {
99 devm_devfreq_remove_device(ldev->dev, devfreq->devfreq);
100 devfreq->devfreq = NULL;
104 int lima_devfreq_init(struct lima_device *ldev)
106 struct thermal_cooling_device *cooling;
107 struct device *dev = ldev->dev;
108 struct devfreq *devfreq;
109 struct lima_devfreq *ldevfreq = &ldev->devfreq;
110 struct dev_pm_opp *opp;
111 unsigned long cur_freq;
114 if (!device_property_present(dev, "operating-points-v2"))
115 /* Optional, continue without devfreq */
118 spin_lock_init(&ldevfreq->lock);
120 ret = devm_pm_opp_set_clkname(dev, "core");
124 ret = devm_pm_opp_set_regulators(dev, (const char *[]){ "mali" }, 1);
126 /* Continue if the optional regulator is missing */
131 ret = devm_pm_opp_of_add_table(dev);
135 lima_devfreq_reset(ldevfreq);
137 cur_freq = clk_get_rate(ldev->clk_gpu);
139 opp = devfreq_recommended_opp(dev, &cur_freq, 0);
143 lima_devfreq_profile.initial_freq = cur_freq;
146 devfreq = devm_devfreq_add_device(dev, &lima_devfreq_profile,
147 DEVFREQ_GOV_SIMPLE_ONDEMAND, NULL);
148 if (IS_ERR(devfreq)) {
149 dev_err(dev, "Couldn't initialize GPU devfreq\n");
150 return PTR_ERR(devfreq);
153 ldevfreq->devfreq = devfreq;
155 cooling = of_devfreq_cooling_register(dev->of_node, devfreq);
157 dev_info(dev, "Failed to register cooling device\n");
159 ldevfreq->cooling = cooling;
164 void lima_devfreq_record_busy(struct lima_devfreq *devfreq)
166 unsigned long irqflags;
168 if (!devfreq->devfreq)
171 spin_lock_irqsave(&devfreq->lock, irqflags);
173 lima_devfreq_update_utilization(devfreq);
175 devfreq->busy_count++;
177 spin_unlock_irqrestore(&devfreq->lock, irqflags);
180 void lima_devfreq_record_idle(struct lima_devfreq *devfreq)
182 unsigned long irqflags;
184 if (!devfreq->devfreq)
187 spin_lock_irqsave(&devfreq->lock, irqflags);
189 lima_devfreq_update_utilization(devfreq);
191 WARN_ON(--devfreq->busy_count < 0);
193 spin_unlock_irqrestore(&devfreq->lock, irqflags);
196 int lima_devfreq_resume(struct lima_devfreq *devfreq)
198 unsigned long irqflags;
200 if (!devfreq->devfreq)
203 spin_lock_irqsave(&devfreq->lock, irqflags);
205 lima_devfreq_reset(devfreq);
207 spin_unlock_irqrestore(&devfreq->lock, irqflags);
209 return devfreq_resume_device(devfreq->devfreq);
212 int lima_devfreq_suspend(struct lima_devfreq *devfreq)
214 if (!devfreq->devfreq)
217 return devfreq_suspend_device(devfreq->devfreq);