2 * devfreq_cooling: Thermal cooling device implementation for devices using
5 * Copyright (C) 2014-2015 ARM Limited
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
12 * kind, whether express or implied; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 * - If OPPs are added or removed after devfreq cooling has
18 * registered, the devfreq cooling won't react to it.
21 #include <linux/devfreq.h>
22 #include <linux/devfreq_cooling.h>
23 #include <linux/export.h>
24 #include <linux/idr.h>
25 #include <linux/slab.h>
26 #include <linux/pm_opp.h>
27 #include <linux/thermal.h>
29 #include <trace/events/thermal.h>
31 static DEFINE_IDA(devfreq_ida);
34 * struct devfreq_cooling_device - Devfreq cooling device
35 * @id: unique integer value corresponding to each
36 * devfreq_cooling_device registered.
37 * @cdev: Pointer to associated thermal cooling device.
38 * @devfreq: Pointer to associated devfreq device.
39 * @cooling_state: Current cooling state.
40 * @power_table: Pointer to table with maximum power draw for each
41 * cooling state. State is the index into the table, and
43 * @freq_table: Pointer to a table with the frequencies sorted in descending
44 * order. You can index the table by cooling device state
45 * @freq_table_size: Size of the @freq_table and @power_table
46 * @power_ops: Pointer to devfreq_cooling_power, used to generate the
49 struct devfreq_cooling_device {
51 struct thermal_cooling_device *cdev;
52 struct devfreq *devfreq;
53 unsigned long cooling_state;
56 size_t freq_table_size;
57 struct devfreq_cooling_power *power_ops;
61 * partition_enable_opps() - disable all opps above a given state
62 * @dfc: Pointer to devfreq we are operating on
63 * @cdev_state: cooling device state we're setting
65 * Go through the OPPs of the device, enabling all OPPs until
66 * @cdev_state and disabling those frequencies above it.
68 static int partition_enable_opps(struct devfreq_cooling_device *dfc,
69 unsigned long cdev_state)
72 struct device *dev = dfc->devfreq->dev.parent;
74 for (i = 0; i < dfc->freq_table_size; i++) {
75 struct dev_pm_opp *opp;
77 unsigned int freq = dfc->freq_table[i];
78 bool want_enable = i >= cdev_state ? true : false;
80 opp = dev_pm_opp_find_freq_exact(dev, freq, !want_enable);
82 if (PTR_ERR(opp) == -ERANGE)
90 ret = dev_pm_opp_enable(dev, freq);
92 ret = dev_pm_opp_disable(dev, freq);
101 static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
102 unsigned long *state)
104 struct devfreq_cooling_device *dfc = cdev->devdata;
106 *state = dfc->freq_table_size - 1;
111 static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
112 unsigned long *state)
114 struct devfreq_cooling_device *dfc = cdev->devdata;
116 *state = dfc->cooling_state;
121 static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
124 struct devfreq_cooling_device *dfc = cdev->devdata;
125 struct devfreq *df = dfc->devfreq;
126 struct device *dev = df->dev.parent;
129 if (state == dfc->cooling_state)
132 dev_dbg(dev, "Setting cooling state %lu\n", state);
134 if (state >= dfc->freq_table_size)
137 ret = partition_enable_opps(dfc, state);
141 dfc->cooling_state = state;
147 * freq_get_state() - get the cooling state corresponding to a frequency
148 * @dfc: Pointer to devfreq cooling device
149 * @freq: frequency in Hz
151 * Return: the cooling state associated with the @freq, or
152 * THERMAL_CSTATE_INVALID if it wasn't found.
155 freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq)
159 for (i = 0; i < dfc->freq_table_size; i++) {
160 if (dfc->freq_table[i] == freq)
164 return THERMAL_CSTATE_INVALID;
168 * get_static_power() - calculate the static power
169 * @dfc: Pointer to devfreq cooling device
170 * @freq: Frequency in Hz
172 * Calculate the static power in milliwatts using the supplied
173 * get_static_power(). The current voltage is calculated using the
174 * OPP library. If no get_static_power() was supplied, assume the
175 * static power is negligible.
178 get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq)
180 struct devfreq *df = dfc->devfreq;
181 struct device *dev = df->dev.parent;
182 unsigned long voltage;
183 struct dev_pm_opp *opp;
185 if (!dfc->power_ops->get_static_power)
188 opp = dev_pm_opp_find_freq_exact(dev, freq, true);
189 if (PTR_ERR(opp) == -ERANGE)
190 opp = dev_pm_opp_find_freq_exact(dev, freq, false);
193 dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
198 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
202 dev_err_ratelimited(dev,
203 "Failed to get voltage for frequency %lu\n",
208 return dfc->power_ops->get_static_power(df, voltage);
212 * get_dynamic_power - calculate the dynamic power
213 * @dfc: Pointer to devfreq cooling device
214 * @freq: Frequency in Hz
215 * @voltage: Voltage in millivolts
217 * Calculate the dynamic power in milliwatts consumed by the device at
218 * frequency @freq and voltage @voltage. If the get_dynamic_power()
219 * was supplied as part of the devfreq_cooling_power struct, then that
220 * function is used. Otherwise, a simple power model (Pdyn = Coeff *
221 * Voltage^2 * Frequency) is used.
224 get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
225 unsigned long voltage)
229 struct devfreq_cooling_power *dfc_power = dfc->power_ops;
231 if (dfc_power->get_dynamic_power)
232 return dfc_power->get_dynamic_power(dfc->devfreq, freq,
235 freq_mhz = freq / 1000000;
236 power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
237 do_div(power, 1000000000);
242 static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
243 struct thermal_zone_device *tz,
246 struct devfreq_cooling_device *dfc = cdev->devdata;
247 struct devfreq *df = dfc->devfreq;
248 struct devfreq_dev_status *status = &df->last_status;
250 unsigned long freq = status->current_frequency;
251 u32 dyn_power, static_power;
253 /* Get dynamic power for state */
254 state = freq_get_state(dfc, freq);
255 if (state == THERMAL_CSTATE_INVALID)
258 dyn_power = dfc->power_table[state];
260 /* Scale dynamic power for utilization */
261 dyn_power = (dyn_power * status->busy_time) / status->total_time;
263 /* Get static power */
264 static_power = get_static_power(dfc, freq);
266 trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
269 *power = dyn_power + static_power;
274 static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
275 struct thermal_zone_device *tz,
279 struct devfreq_cooling_device *dfc = cdev->devdata;
283 if (state >= dfc->freq_table_size)
286 freq = dfc->freq_table[state];
287 static_power = get_static_power(dfc, freq);
289 *power = dfc->power_table[state] + static_power;
293 static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
294 struct thermal_zone_device *tz,
295 u32 power, unsigned long *state)
297 struct devfreq_cooling_device *dfc = cdev->devdata;
298 struct devfreq *df = dfc->devfreq;
299 struct devfreq_dev_status *status = &df->last_status;
300 unsigned long freq = status->current_frequency;
301 unsigned long busy_time;
306 static_power = get_static_power(dfc, freq);
308 dyn_power = power - static_power;
309 dyn_power = dyn_power > 0 ? dyn_power : 0;
311 /* Scale dynamic power for utilization */
312 busy_time = status->busy_time ?: 1;
313 dyn_power = (dyn_power * status->total_time) / busy_time;
316 * Find the first cooling state that is within the power
317 * budget for dynamic power.
319 for (i = 0; i < dfc->freq_table_size - 1; i++)
320 if (dyn_power >= dfc->power_table[i])
324 trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
328 static struct thermal_cooling_device_ops devfreq_cooling_ops = {
329 .get_max_state = devfreq_cooling_get_max_state,
330 .get_cur_state = devfreq_cooling_get_cur_state,
331 .set_cur_state = devfreq_cooling_set_cur_state,
335 * devfreq_cooling_gen_tables() - Generate power and freq tables.
336 * @dfc: Pointer to devfreq cooling device.
338 * Generate power and frequency tables: the power table hold the
339 * device's maximum power usage at each cooling state (OPP). The
340 * static and dynamic power using the appropriate voltage and
341 * frequency for the state, is acquired from the struct
342 * devfreq_cooling_power, and summed to make the maximum power draw.
344 * The frequency table holds the frequencies in descending order.
345 * That way its indexed by cooling device state.
347 * The tables are malloced, and pointers put in dfc. They must be
348 * freed when unregistering the devfreq cooling device.
350 * Return: 0 on success, negative error code on failure.
352 static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
354 struct devfreq *df = dfc->devfreq;
355 struct device *dev = df->dev.parent;
358 u32 *power_table = NULL;
362 num_opps = dev_pm_opp_get_opp_count(dev);
364 if (dfc->power_ops) {
365 power_table = kcalloc(num_opps, sizeof(*power_table),
371 freq_table = kcalloc(num_opps, sizeof(*freq_table),
375 goto free_power_table;
378 for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
379 unsigned long power_dyn, voltage;
380 struct dev_pm_opp *opp;
382 opp = dev_pm_opp_find_freq_floor(dev, &freq);
388 voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
391 if (dfc->power_ops) {
392 power_dyn = get_dynamic_power(dfc, freq, voltage);
394 dev_dbg(dev, "Dynamic power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
395 freq / 1000000, voltage, power_dyn, power_dyn);
397 power_table[i] = power_dyn;
400 freq_table[i] = freq;
404 dfc->power_table = power_table;
406 dfc->freq_table = freq_table;
407 dfc->freq_table_size = num_opps;
420 * of_devfreq_cooling_register_power() - Register devfreq cooling device,
421 * with OF and power information.
422 * @np: Pointer to OF device_node.
423 * @df: Pointer to devfreq device.
424 * @dfc_power: Pointer to devfreq_cooling_power.
426 * Register a devfreq cooling device. The available OPPs must be
427 * registered on the device.
429 * If @dfc_power is provided, the cooling device is registered with the
430 * power extensions. For the power extensions to work correctly,
431 * devfreq should use the simple_ondemand governor, other governors
432 * are not currently supported.
434 struct thermal_cooling_device *
435 of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
436 struct devfreq_cooling_power *dfc_power)
438 struct thermal_cooling_device *cdev;
439 struct devfreq_cooling_device *dfc;
440 char dev_name[THERMAL_NAME_LENGTH];
443 dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
445 return ERR_PTR(-ENOMEM);
450 dfc->power_ops = dfc_power;
452 devfreq_cooling_ops.get_requested_power =
453 devfreq_cooling_get_requested_power;
454 devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
455 devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
458 err = devfreq_cooling_gen_tables(dfc);
462 err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
467 snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
469 cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
470 &devfreq_cooling_ops);
473 dev_err(df->dev.parent,
474 "Failed to register devfreq cooling device (%d)\n",
484 ida_simple_remove(&devfreq_ida, dfc->id);
486 kfree(dfc->power_table);
487 kfree(dfc->freq_table);
493 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
496 * of_devfreq_cooling_register() - Register devfreq cooling device,
497 * with OF information.
498 * @np: Pointer to OF device_node.
499 * @df: Pointer to devfreq device.
501 struct thermal_cooling_device *
502 of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
504 return of_devfreq_cooling_register_power(np, df, NULL);
506 EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
509 * devfreq_cooling_register() - Register devfreq cooling device.
510 * @df: Pointer to devfreq device.
512 struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
514 return of_devfreq_cooling_register(NULL, df);
516 EXPORT_SYMBOL_GPL(devfreq_cooling_register);
519 * devfreq_cooling_unregister() - Unregister devfreq cooling device.
520 * @dfc: Pointer to devfreq cooling device to unregister.
522 void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
524 struct devfreq_cooling_device *dfc;
531 thermal_cooling_device_unregister(dfc->cdev);
532 ida_simple_remove(&devfreq_ida, dfc->id);
533 kfree(dfc->power_table);
534 kfree(dfc->freq_table);
538 EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);