blob: 4310cb342a9fb1f58ac362168df270c6e33fc8d3 [file] [log] [blame]
Lukasz Luba0de967f2020-07-30 17:51:17 +01001// SPDX-License-Identifier: GPL-2.0
Ørjan Eidea76caf52015-09-10 18:09:30 +01002/*
3 * devfreq_cooling: Thermal cooling device implementation for devices using
4 * devfreq
5 *
6 * Copyright (C) 2014-2015 ARM Limited
7 *
Ørjan Eidea76caf52015-09-10 18:09:30 +01008 * TODO:
9 * - If OPPs are added or removed after devfreq cooling has
10 * registered, the devfreq cooling won't react to it.
11 */
12
13#include <linux/devfreq.h>
14#include <linux/devfreq_cooling.h>
Lukasz Luba84e0d872020-12-10 14:30:12 +000015#include <linux/energy_model.h>
Ørjan Eidea76caf52015-09-10 18:09:30 +010016#include <linux/export.h>
17#include <linux/slab.h>
18#include <linux/pm_opp.h>
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +000019#include <linux/pm_qos.h>
Ørjan Eidea76caf52015-09-10 18:09:30 +010020#include <linux/thermal.h>
Daniel Lezcano73b718c2021-09-07 19:57:51 -070021#include <linux/units.h>
Ørjan Eidea76caf52015-09-10 18:09:30 +010022
Javi Merino9876b1a2015-09-10 18:09:31 +010023#include <trace/events/thermal.h>
24
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +000025#define SCALE_ERROR_MITIGATION 100
Lukasz Luba2be83da2017-05-04 12:34:32 +010026
Ørjan Eidea76caf52015-09-10 18:09:30 +010027/**
28 * struct devfreq_cooling_device - Devfreq cooling device
Ørjan Eidea76caf52015-09-10 18:09:30 +010029 * devfreq_cooling_device registered.
30 * @cdev: Pointer to associated thermal cooling device.
31 * @devfreq: Pointer to associated devfreq device.
32 * @cooling_state: Current cooling state.
Ørjan Eidea76caf52015-09-10 18:09:30 +010033 * @freq_table: Pointer to a table with the frequencies sorted in descending
34 * order. You can index the table by cooling device state
Lukasz Luba615510f2020-12-10 14:30:13 +000035 * @max_state: It is the last index, that is, one less than the number of the
36 * OPPs
37 * @power_ops: Pointer to devfreq_cooling_power, a more precised model.
Lukasz Luba2be83da2017-05-04 12:34:32 +010038 * @res_util: Resource utilization scaling factor for the power.
39 * It is multiplied by 100 to minimize the error. It is used
40 * for estimation of the power budget instead of using
Lukasz Luba615510f2020-12-10 14:30:13 +000041 * 'utilization' (which is 'busy_time' / 'total_time').
42 * The 'res_util' range is from 100 to power * 100 for the
43 * corresponding 'state'.
Amit Kucheria1b5cb952019-11-20 21:15:13 +053044 * @capped_state: index to cooling state with in dynamic power budget
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +000045 * @req_max_freq: PM QoS request for limiting the maximum frequency
46 * of the devfreq device.
Lukasz Luba44011172020-12-15 15:42:21 +000047 * @em_pd: Energy Model for the associated Devfreq device
Ørjan Eidea76caf52015-09-10 18:09:30 +010048 */
49struct devfreq_cooling_device {
Ørjan Eidea76caf52015-09-10 18:09:30 +010050 struct thermal_cooling_device *cdev;
51 struct devfreq *devfreq;
52 unsigned long cooling_state;
Ørjan Eidea76caf52015-09-10 18:09:30 +010053 u32 *freq_table;
Lukasz Luba615510f2020-12-10 14:30:13 +000054 size_t max_state;
Ørjan Eidea76caf52015-09-10 18:09:30 +010055 struct devfreq_cooling_power *power_ops;
Lukasz Luba2be83da2017-05-04 12:34:32 +010056 u32 res_util;
57 int capped_state;
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +000058 struct dev_pm_qos_request req_max_freq;
Lukasz Luba44011172020-12-15 15:42:21 +000059 struct em_perf_domain *em_pd;
Ørjan Eidea76caf52015-09-10 18:09:30 +010060};
61
Ørjan Eidea76caf52015-09-10 18:09:30 +010062static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
63 unsigned long *state)
64{
65 struct devfreq_cooling_device *dfc = cdev->devdata;
66
Lukasz Luba615510f2020-12-10 14:30:13 +000067 *state = dfc->max_state;
Ørjan Eidea76caf52015-09-10 18:09:30 +010068
69 return 0;
70}
71
72static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
73 unsigned long *state)
74{
75 struct devfreq_cooling_device *dfc = cdev->devdata;
76
77 *state = dfc->cooling_state;
78
79 return 0;
80}
81
82static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
83 unsigned long state)
84{
85 struct devfreq_cooling_device *dfc = cdev->devdata;
86 struct devfreq *df = dfc->devfreq;
87 struct device *dev = df->dev.parent;
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +000088 unsigned long freq;
Lukasz Luba615510f2020-12-10 14:30:13 +000089 int perf_idx;
Ørjan Eidea76caf52015-09-10 18:09:30 +010090
91 if (state == dfc->cooling_state)
92 return 0;
93
94 dev_dbg(dev, "Setting cooling state %lu\n", state);
95
Lukasz Luba615510f2020-12-10 14:30:13 +000096 if (state > dfc->max_state)
Ørjan Eidea76caf52015-09-10 18:09:30 +010097 return -EINVAL;
98
Lukasz Luba44011172020-12-15 15:42:21 +000099 if (dfc->em_pd) {
Lukasz Luba615510f2020-12-10 14:30:13 +0000100 perf_idx = dfc->max_state - state;
Lukasz Luba44011172020-12-15 15:42:21 +0000101 freq = dfc->em_pd->table[perf_idx].frequency * 1000;
Lukasz Luba615510f2020-12-10 14:30:13 +0000102 } else {
103 freq = dfc->freq_table[state];
104 }
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +0000105
106 dev_pm_qos_update_request(&dfc->req_max_freq,
107 DIV_ROUND_UP(freq, HZ_PER_KHZ));
Ørjan Eidea76caf52015-09-10 18:09:30 +0100108
109 dfc->cooling_state = state;
110
111 return 0;
112}
113
114/**
Lukasz Luba615510f2020-12-10 14:30:13 +0000115 * get_perf_idx() - get the performance index corresponding to a frequency
116 * @em_pd: Pointer to device's Energy Model
117 * @freq: frequency in kHz
Ørjan Eidea76caf52015-09-10 18:09:30 +0100118 *
Lukasz Luba615510f2020-12-10 14:30:13 +0000119 * Return: the performance index associated with the @freq, or
120 * -EINVAL if it wasn't found.
Ørjan Eidea76caf52015-09-10 18:09:30 +0100121 */
Lukasz Luba615510f2020-12-10 14:30:13 +0000122static int get_perf_idx(struct em_perf_domain *em_pd, unsigned long freq)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100123{
124 int i;
125
Lukasz Luba615510f2020-12-10 14:30:13 +0000126 for (i = 0; i < em_pd->nr_perf_states; i++) {
127 if (em_pd->table[i].frequency == freq)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100128 return i;
129 }
130
Lukasz Luba615510f2020-12-10 14:30:13 +0000131 return -EINVAL;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100132}
133
Lukasz Lubae34cab42017-05-04 12:34:31 +0100134static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100135{
Ørjan Eidea76caf52015-09-10 18:09:30 +0100136 struct device *dev = df->dev.parent;
137 unsigned long voltage;
138 struct dev_pm_opp *opp;
139
Ørjan Eidea76caf52015-09-10 18:09:30 +0100140 opp = dev_pm_opp_find_freq_exact(dev, freq, true);
Viresh Kumara4e49c92017-02-07 09:40:01 +0530141 if (PTR_ERR(opp) == -ERANGE)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100142 opp = dev_pm_opp_find_freq_exact(dev, freq, false);
143
Viresh Kumarafd1f4e2017-02-07 09:40:03 +0530144 if (IS_ERR(opp)) {
145 dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
146 freq, PTR_ERR(opp));
147 return 0;
148 }
149
Ørjan Eidea76caf52015-09-10 18:09:30 +0100150 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530151 dev_pm_opp_put(opp);
Ørjan Eidea76caf52015-09-10 18:09:30 +0100152
153 if (voltage == 0) {
Viresh Kumar8327b832017-02-07 09:40:02 +0530154 dev_err_ratelimited(dev,
Viresh Kumarafd1f4e2017-02-07 09:40:03 +0530155 "Failed to get voltage for frequency %lu\n",
156 freq);
Ørjan Eidea76caf52015-09-10 18:09:30 +0100157 }
158
Lukasz Lubae34cab42017-05-04 12:34:31 +0100159 return voltage;
160}
161
Lukasz Luba229794e2020-12-10 14:30:11 +0000162static void _normalize_load(struct devfreq_dev_status *status)
163{
164 if (status->total_time > 0xfffff) {
165 status->total_time >>= 10;
166 status->busy_time >>= 10;
167 }
168
169 status->busy_time <<= 10;
170 status->busy_time /= status->total_time ? : 1;
171
172 status->busy_time = status->busy_time ? : 1;
173 status->total_time = 1024;
174}
Lukasz Luba2be83da2017-05-04 12:34:32 +0100175
Ørjan Eidea76caf52015-09-10 18:09:30 +0100176static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
Ørjan Eidea76caf52015-09-10 18:09:30 +0100177 u32 *power)
178{
179 struct devfreq_cooling_device *dfc = cdev->devdata;
180 struct devfreq *df = dfc->devfreq;
Lukasz Luba229794e2020-12-10 14:30:11 +0000181 struct devfreq_dev_status status;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100182 unsigned long state;
Lukasz Luba229794e2020-12-10 14:30:11 +0000183 unsigned long freq;
Lukasz Luba2be83da2017-05-04 12:34:32 +0100184 unsigned long voltage;
Lukasz Luba615510f2020-12-10 14:30:13 +0000185 int res, perf_idx;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100186
Lukasz Luba229794e2020-12-10 14:30:11 +0000187 mutex_lock(&df->lock);
188 status = df->last_status;
189 mutex_unlock(&df->lock);
190
191 freq = status.current_frequency;
192
Lukasz Luba615510f2020-12-10 14:30:13 +0000193 if (dfc->power_ops && dfc->power_ops->get_real_power) {
Lukasz Luba2be83da2017-05-04 12:34:32 +0100194 voltage = get_voltage(df, freq);
195 if (voltage == 0) {
196 res = -EINVAL;
197 goto fail;
198 }
Ørjan Eidea76caf52015-09-10 18:09:30 +0100199
Lukasz Luba2be83da2017-05-04 12:34:32 +0100200 res = dfc->power_ops->get_real_power(df, power, freq, voltage);
201 if (!res) {
202 state = dfc->capped_state;
Lukasz Luba44011172020-12-15 15:42:21 +0000203 dfc->res_util = dfc->em_pd->table[state].power;
Lukasz Luba2be83da2017-05-04 12:34:32 +0100204 dfc->res_util *= SCALE_ERROR_MITIGATION;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100205
Lukasz Luba2be83da2017-05-04 12:34:32 +0100206 if (*power > 1)
207 dfc->res_util /= *power;
208 } else {
209 goto fail;
210 }
211 } else {
Lukasz Luba615510f2020-12-10 14:30:13 +0000212 /* Energy Model frequencies are in kHz */
Lukasz Luba44011172020-12-15 15:42:21 +0000213 perf_idx = get_perf_idx(dfc->em_pd, freq / 1000);
Lukasz Luba615510f2020-12-10 14:30:13 +0000214 if (perf_idx < 0) {
215 res = -EAGAIN;
216 goto fail;
217 }
Lukasz Luba2be83da2017-05-04 12:34:32 +0100218
Lukasz Luba229794e2020-12-10 14:30:11 +0000219 _normalize_load(&status);
220
Lukasz Luba615510f2020-12-10 14:30:13 +0000221 /* Scale power for utilization */
Lukasz Luba44011172020-12-15 15:42:21 +0000222 *power = dfc->em_pd->table[perf_idx].power;
Lukasz Luba615510f2020-12-10 14:30:13 +0000223 *power *= status.busy_time;
224 *power >>= 10;
Lukasz Luba2be83da2017-05-04 12:34:32 +0100225 }
Ørjan Eidea76caf52015-09-10 18:09:30 +0100226
Lukasz Luba229794e2020-12-10 14:30:11 +0000227 trace_thermal_power_devfreq_get_power(cdev, &status, freq, *power);
Javi Merino9876b1a2015-09-10 18:09:31 +0100228
Ørjan Eidea76caf52015-09-10 18:09:30 +0100229 return 0;
Lukasz Luba2be83da2017-05-04 12:34:32 +0100230fail:
231 /* It is safe to set max in this case */
232 dfc->res_util = SCALE_ERROR_MITIGATION;
233 return res;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100234}
235
236static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
Lukasz Luba615510f2020-12-10 14:30:13 +0000237 unsigned long state, u32 *power)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100238{
239 struct devfreq_cooling_device *dfc = cdev->devdata;
Lukasz Luba615510f2020-12-10 14:30:13 +0000240 int perf_idx;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100241
Lukasz Luba615510f2020-12-10 14:30:13 +0000242 if (state > dfc->max_state)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100243 return -EINVAL;
244
Lukasz Luba615510f2020-12-10 14:30:13 +0000245 perf_idx = dfc->max_state - state;
Lukasz Luba44011172020-12-15 15:42:21 +0000246 *power = dfc->em_pd->table[perf_idx].power;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100247
Ørjan Eidea76caf52015-09-10 18:09:30 +0100248 return 0;
249}
250
251static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
Ørjan Eidea76caf52015-09-10 18:09:30 +0100252 u32 power, unsigned long *state)
253{
254 struct devfreq_cooling_device *dfc = cdev->devdata;
255 struct devfreq *df = dfc->devfreq;
Lukasz Luba229794e2020-12-10 14:30:11 +0000256 struct devfreq_dev_status status;
257 unsigned long freq;
Lukasz Luba2be83da2017-05-04 12:34:32 +0100258 s32 est_power;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100259 int i;
260
Lukasz Luba229794e2020-12-10 14:30:11 +0000261 mutex_lock(&df->lock);
262 status = df->last_status;
263 mutex_unlock(&df->lock);
264
265 freq = status.current_frequency;
266
Lukasz Luba615510f2020-12-10 14:30:13 +0000267 if (dfc->power_ops && dfc->power_ops->get_real_power) {
Lukasz Luba2be83da2017-05-04 12:34:32 +0100268 /* Scale for resource utilization */
269 est_power = power * dfc->res_util;
270 est_power /= SCALE_ERROR_MITIGATION;
271 } else {
Lukasz Luba2be83da2017-05-04 12:34:32 +0100272 /* Scale dynamic power for utilization */
Lukasz Luba229794e2020-12-10 14:30:11 +0000273 _normalize_load(&status);
Lukasz Luba615510f2020-12-10 14:30:13 +0000274 est_power = power << 10;
275 est_power /= status.busy_time;
Lukasz Luba2be83da2017-05-04 12:34:32 +0100276 }
Ørjan Eidea76caf52015-09-10 18:09:30 +0100277
278 /*
279 * Find the first cooling state that is within the power
Lukasz Luba615510f2020-12-10 14:30:13 +0000280 * budget. The EM power table is sorted ascending.
Ørjan Eidea76caf52015-09-10 18:09:30 +0100281 */
Lukasz Luba615510f2020-12-10 14:30:13 +0000282 for (i = dfc->max_state; i > 0; i--)
Lukasz Luba44011172020-12-15 15:42:21 +0000283 if (est_power >= dfc->em_pd->table[i].power)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100284 break;
285
Lukasz Luba615510f2020-12-10 14:30:13 +0000286 *state = dfc->max_state - i;
287 dfc->capped_state = *state;
288
Javi Merino9876b1a2015-09-10 18:09:31 +0100289 trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
Ørjan Eidea76caf52015-09-10 18:09:30 +0100290 return 0;
291}
292
293static struct thermal_cooling_device_ops devfreq_cooling_ops = {
294 .get_max_state = devfreq_cooling_get_max_state,
295 .get_cur_state = devfreq_cooling_get_cur_state,
296 .set_cur_state = devfreq_cooling_set_cur_state,
297};
298
299/**
Lukasz Luba615510f2020-12-10 14:30:13 +0000300 * devfreq_cooling_gen_tables() - Generate frequency table.
301 * @dfc: Pointer to devfreq cooling device.
302 * @num_opps: Number of OPPs
Ørjan Eidea76caf52015-09-10 18:09:30 +0100303 *
Lukasz Luba615510f2020-12-10 14:30:13 +0000304 * Generate frequency table which holds the frequencies in descending
305 * order. That way its indexed by cooling device state. This is for
306 * compatibility with drivers which do not register Energy Model.
Ørjan Eidea76caf52015-09-10 18:09:30 +0100307 *
308 * Return: 0 on success, negative error code on failure.
309 */
Lukasz Luba615510f2020-12-10 14:30:13 +0000310static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc,
311 int num_opps)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100312{
313 struct devfreq *df = dfc->devfreq;
314 struct device *dev = df->dev.parent;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100315 unsigned long freq;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100316 int i;
317
Lukasz Luba615510f2020-12-10 14:30:13 +0000318 dfc->freq_table = kcalloc(num_opps, sizeof(*dfc->freq_table),
Ørjan Eidea76caf52015-09-10 18:09:30 +0100319 GFP_KERNEL);
Lukasz Luba615510f2020-12-10 14:30:13 +0000320 if (!dfc->freq_table)
321 return -ENOMEM;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100322
323 for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
Ørjan Eidea76caf52015-09-10 18:09:30 +0100324 struct dev_pm_opp *opp;
325
Ørjan Eidea76caf52015-09-10 18:09:30 +0100326 opp = dev_pm_opp_find_freq_floor(dev, &freq);
327 if (IS_ERR(opp)) {
Lukasz Luba615510f2020-12-10 14:30:13 +0000328 kfree(dfc->freq_table);
329 return PTR_ERR(opp);
Ørjan Eidea76caf52015-09-10 18:09:30 +0100330 }
331
Viresh Kumar8a31d9d92017-01-23 10:11:47 +0530332 dev_pm_opp_put(opp);
Lukasz Luba615510f2020-12-10 14:30:13 +0000333 dfc->freq_table[i] = freq;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100334 }
335
Ørjan Eidea76caf52015-09-10 18:09:30 +0100336 return 0;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100337}
338
339/**
340 * of_devfreq_cooling_register_power() - Register devfreq cooling device,
341 * with OF and power information.
342 * @np: Pointer to OF device_node.
343 * @df: Pointer to devfreq device.
344 * @dfc_power: Pointer to devfreq_cooling_power.
345 *
346 * Register a devfreq cooling device. The available OPPs must be
347 * registered on the device.
348 *
349 * If @dfc_power is provided, the cooling device is registered with the
350 * power extensions. For the power extensions to work correctly,
351 * devfreq should use the simple_ondemand governor, other governors
352 * are not currently supported.
353 */
Javi Merino3c99c2c2015-11-02 19:03:03 +0000354struct thermal_cooling_device *
Ørjan Eidea76caf52015-09-10 18:09:30 +0100355of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
356 struct devfreq_cooling_power *dfc_power)
357{
358 struct thermal_cooling_device *cdev;
Lukasz Luba615510f2020-12-10 14:30:13 +0000359 struct device *dev = df->dev.parent;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100360 struct devfreq_cooling_device *dfc;
Daniel Lezcanof8d354e2021-03-14 12:13:31 +0100361 char *name;
Lukasz Luba615510f2020-12-10 14:30:13 +0000362 int err, num_opps;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100363
364 dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
365 if (!dfc)
366 return ERR_PTR(-ENOMEM);
367
368 dfc->devfreq = df;
369
Lukasz Luba44011172020-12-15 15:42:21 +0000370 dfc->em_pd = em_pd_get(dev);
371 if (dfc->em_pd) {
Ørjan Eidea76caf52015-09-10 18:09:30 +0100372 devfreq_cooling_ops.get_requested_power =
373 devfreq_cooling_get_requested_power;
374 devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
375 devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
Lukasz Luba615510f2020-12-10 14:30:13 +0000376
377 dfc->power_ops = dfc_power;
378
Lukasz Luba44011172020-12-15 15:42:21 +0000379 num_opps = em_pd_nr_perf_states(dfc->em_pd);
Lukasz Luba615510f2020-12-10 14:30:13 +0000380 } else {
381 /* Backward compatibility for drivers which do not use IPA */
382 dev_dbg(dev, "missing EM for cooling device\n");
383
384 num_opps = dev_pm_opp_get_opp_count(dev);
385
386 err = devfreq_cooling_gen_tables(dfc, num_opps);
387 if (err)
388 goto free_dfc;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100389 }
390
Lukasz Luba615510f2020-12-10 14:30:13 +0000391 if (num_opps <= 0) {
392 err = -EINVAL;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100393 goto free_dfc;
Lukasz Luba615510f2020-12-10 14:30:13 +0000394 }
Ørjan Eidea76caf52015-09-10 18:09:30 +0100395
Lukasz Luba615510f2020-12-10 14:30:13 +0000396 /* max_state is an index, not a counter */
397 dfc->max_state = num_opps - 1;
398
399 err = dev_pm_qos_add_request(dev, &dfc->req_max_freq,
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +0000400 DEV_PM_QOS_MAX_FREQUENCY,
401 PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
Matthew Wilcox2f96c032016-12-21 09:47:06 -0800402 if (err < 0)
Lukasz Luba615510f2020-12-10 14:30:13 +0000403 goto free_table;
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +0000404
Daniel Lezcano9aa80ab2021-03-19 21:24:23 +0100405 err = -ENOMEM;
Daniel Lezcanof8d354e2021-03-14 12:13:31 +0100406 name = kasprintf(GFP_KERNEL, "devfreq-%s", dev_name(dev));
407 if (!name)
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +0000408 goto remove_qos_req;
Lukasz Luba615510f2020-12-10 14:30:13 +0000409
Daniel Lezcanof8d354e2021-03-14 12:13:31 +0100410 cdev = thermal_of_cooling_device_register(np, name, dfc,
Ørjan Eidea76caf52015-09-10 18:09:30 +0100411 &devfreq_cooling_ops);
Daniel Lezcanof8d354e2021-03-14 12:13:31 +0100412 kfree(name);
413
Ørjan Eidea76caf52015-09-10 18:09:30 +0100414 if (IS_ERR(cdev)) {
415 err = PTR_ERR(cdev);
Lukasz Luba615510f2020-12-10 14:30:13 +0000416 dev_err(dev,
Ørjan Eidea76caf52015-09-10 18:09:30 +0100417 "Failed to register devfreq cooling device (%d)\n",
418 err);
Daniel Lezcanof8d354e2021-03-14 12:13:31 +0100419 goto remove_qos_req;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100420 }
421
422 dfc->cdev = cdev;
423
Javi Merino3c99c2c2015-11-02 19:03:03 +0000424 return cdev;
Ørjan Eidea76caf52015-09-10 18:09:30 +0100425
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +0000426remove_qos_req:
427 dev_pm_qos_remove_request(&dfc->req_max_freq);
Lukasz Luba615510f2020-12-10 14:30:13 +0000428free_table:
Ørjan Eidea76caf52015-09-10 18:09:30 +0100429 kfree(dfc->freq_table);
430free_dfc:
431 kfree(dfc);
432
433 return ERR_PTR(err);
434}
435EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
436
437/**
438 * of_devfreq_cooling_register() - Register devfreq cooling device,
439 * with OF information.
440 * @np: Pointer to OF device_node.
441 * @df: Pointer to devfreq device.
442 */
Javi Merino3c99c2c2015-11-02 19:03:03 +0000443struct thermal_cooling_device *
Ørjan Eidea76caf52015-09-10 18:09:30 +0100444of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
445{
446 return of_devfreq_cooling_register_power(np, df, NULL);
447}
448EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
449
450/**
451 * devfreq_cooling_register() - Register devfreq cooling device.
452 * @df: Pointer to devfreq device.
453 */
Javi Merino3c99c2c2015-11-02 19:03:03 +0000454struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100455{
456 return of_devfreq_cooling_register(NULL, df);
457}
458EXPORT_SYMBOL_GPL(devfreq_cooling_register);
459
460/**
Yang Li8b2ea892021-06-09 15:22:30 +0800461 * devfreq_cooling_em_register() - Register devfreq cooling device with
Lukasz Luba84e0d872020-12-10 14:30:12 +0000462 * power information and automatically register Energy Model (EM)
463 * @df: Pointer to devfreq device.
464 * @dfc_power: Pointer to devfreq_cooling_power.
465 *
466 * Register a devfreq cooling device and automatically register EM. The
467 * available OPPs must be registered for the device.
468 *
469 * If @dfc_power is provided, the cooling device is registered with the
470 * power extensions. It is using the simple Energy Model which requires
471 * "dynamic-power-coefficient" a devicetree property. To not break drivers
472 * which miss that DT property, the function won't bail out when the EM
473 * registration failed. The cooling device will be registered if everything
474 * else is OK.
475 */
476struct thermal_cooling_device *
477devfreq_cooling_em_register(struct devfreq *df,
478 struct devfreq_cooling_power *dfc_power)
479{
480 struct thermal_cooling_device *cdev;
481 struct device *dev;
482 int ret;
483
484 if (IS_ERR_OR_NULL(df))
485 return ERR_PTR(-EINVAL);
486
487 dev = df->dev.parent;
488
489 ret = dev_pm_opp_of_register_em(dev, NULL);
490 if (ret)
491 dev_dbg(dev, "Unable to register EM for devfreq cooling device (%d)\n",
492 ret);
493
494 cdev = of_devfreq_cooling_register_power(dev->of_node, df, dfc_power);
495
496 if (IS_ERR_OR_NULL(cdev))
497 em_dev_unregister_perf_domain(dev);
498
499 return cdev;
500}
501EXPORT_SYMBOL_GPL(devfreq_cooling_em_register);
502
503/**
Ørjan Eidea76caf52015-09-10 18:09:30 +0100504 * devfreq_cooling_unregister() - Unregister devfreq cooling device.
Amit Kucheria1b5cb952019-11-20 21:15:13 +0530505 * @cdev: Pointer to devfreq cooling device to unregister.
Lukasz Luba84e0d872020-12-10 14:30:12 +0000506 *
507 * Unregisters devfreq cooling device and related Energy Model if it was
508 * present.
Ørjan Eidea76caf52015-09-10 18:09:30 +0100509 */
Javi Merino3c99c2c2015-11-02 19:03:03 +0000510void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
Ørjan Eidea76caf52015-09-10 18:09:30 +0100511{
Javi Merino3c99c2c2015-11-02 19:03:03 +0000512 struct devfreq_cooling_device *dfc;
Lukasz Luba84e0d872020-12-10 14:30:12 +0000513 struct device *dev;
Javi Merino3c99c2c2015-11-02 19:03:03 +0000514
Lukasz Luba84e0d872020-12-10 14:30:12 +0000515 if (IS_ERR_OR_NULL(cdev))
Ørjan Eidea76caf52015-09-10 18:09:30 +0100516 return;
517
Javi Merino3c99c2c2015-11-02 19:03:03 +0000518 dfc = cdev->devdata;
Lukasz Luba84e0d872020-12-10 14:30:12 +0000519 dev = dfc->devfreq->dev.parent;
Javi Merino3c99c2c2015-11-02 19:03:03 +0000520
Ørjan Eidea76caf52015-09-10 18:09:30 +0100521 thermal_cooling_device_unregister(dfc->cdev);
Matthias Kaehlcke04fa9c82020-03-18 11:45:46 +0000522 dev_pm_qos_remove_request(&dfc->req_max_freq);
Lukasz Luba84e0d872020-12-10 14:30:12 +0000523
524 em_dev_unregister_perf_domain(dev);
525
Ørjan Eidea76caf52015-09-10 18:09:30 +0100526 kfree(dfc->freq_table);
Ørjan Eidea76caf52015-09-10 18:09:30 +0100527 kfree(dfc);
528}
529EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);