Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * devfreq_cooling: Thermal cooling device implementation for devices using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *                  devfreq
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Copyright (C) 2014-2015 ARM Limited
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * TODO:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *    - If OPPs are added or removed after devfreq cooling has
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *      registered, the devfreq cooling won't react to it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/devfreq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/devfreq_cooling.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/idr.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/pm_opp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/pm_qos.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <trace/events/thermal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) #define HZ_PER_KHZ		1000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) #define SCALE_ERROR_MITIGATION	100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) static DEFINE_IDA(devfreq_ida);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  * struct devfreq_cooling_device - Devfreq cooling device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31)  * @id:		unique integer value corresponding to each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32)  *		devfreq_cooling_device registered.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33)  * @cdev:	Pointer to associated thermal cooling device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * @devfreq:	Pointer to associated devfreq device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * @cooling_state:	Current cooling state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * @power_table:	Pointer to table with maximum power draw for each
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *			cooling state. State is the index into the table, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  *			the power is in mW.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  * @freq_table:	Pointer to a table with the frequencies sorted in descending
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40)  *		order.  You can index the table by cooling device state
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41)  * @freq_table_size:	Size of the @freq_table and @power_table
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42)  * @power_ops:	Pointer to devfreq_cooling_power, used to generate the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43)  *		@power_table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44)  * @res_util:	Resource utilization scaling factor for the power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  *		It is multiplied by 100 to minimize the error. It is used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  *		for estimation of the power budget instead of using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  *		'utilization' (which is	'busy_time / 'total_time').
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  *		The 'res_util' range is from 100 to (power_table[state] * 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  *		for the corresponding 'state'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * @capped_state:	index to cooling state with in dynamic power budget
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * @req_max_freq:	PM QoS request for limiting the maximum frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *			of the devfreq device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) struct devfreq_cooling_device {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	int id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	struct thermal_cooling_device *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct devfreq *devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	unsigned long cooling_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u32 *power_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	u32 *freq_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	size_t freq_table_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	struct devfreq_cooling_power *power_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	u32 res_util;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	int capped_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	struct dev_pm_qos_request req_max_freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) static int devfreq_cooling_get_max_state(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 					 unsigned long *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	struct devfreq_cooling_device *dfc = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	*state = dfc->freq_table_size - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) static int devfreq_cooling_get_cur_state(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 					 unsigned long *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 	struct devfreq_cooling_device *dfc = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 	*state = dfc->cooling_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) static int devfreq_cooling_set_cur_state(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 					 unsigned long state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	struct devfreq_cooling_device *dfc = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	struct devfreq *df = dfc->devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	struct device *dev = df->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	unsigned long freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (state == dfc->cooling_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	dev_dbg(dev, "Setting cooling state %lu\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (state >= dfc->freq_table_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	freq = dfc->freq_table[state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	dev_pm_qos_update_request(&dfc->req_max_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 				  DIV_ROUND_UP(freq, HZ_PER_KHZ));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	dfc->cooling_state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)  * freq_get_state() - get the cooling state corresponding to a frequency
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)  * @dfc:	Pointer to devfreq cooling device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)  * @freq:	frequency in Hz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)  * Return: the cooling state associated with the @freq, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)  * THERMAL_CSTATE_INVALID if it wasn't found.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) freq_get_state(struct devfreq_cooling_device *dfc, unsigned long freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	for (i = 0; i < dfc->freq_table_size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		if (dfc->freq_table[i] == freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 			return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 	return THERMAL_CSTATE_INVALID;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static unsigned long get_voltage(struct devfreq *df, unsigned long freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	struct device *dev = df->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	unsigned long voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	struct dev_pm_opp *opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	opp = dev_pm_opp_find_freq_exact(dev, freq, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 	if (PTR_ERR(opp) == -ERANGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 		opp = dev_pm_opp_find_freq_exact(dev, freq, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 	if (IS_ERR(opp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		dev_err_ratelimited(dev, "Failed to find OPP for frequency %lu: %ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 				    freq, PTR_ERR(opp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	dev_pm_opp_put(opp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (voltage == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		dev_err_ratelimited(dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 				    "Failed to get voltage for frequency %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 				    freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	return voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * get_static_power() - calculate the static power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  * @dfc:	Pointer to devfreq cooling device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * @freq:	Frequency in Hz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * Calculate the static power in milliwatts using the supplied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  * get_static_power().  The current voltage is calculated using the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)  * OPP library.  If no get_static_power() was supplied, assume the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)  * static power is negligible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) get_static_power(struct devfreq_cooling_device *dfc, unsigned long freq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	struct devfreq *df = dfc->devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	unsigned long voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	if (!dfc->power_ops->get_static_power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	voltage = get_voltage(df, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) 	if (voltage == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	return dfc->power_ops->get_static_power(df, voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  * get_dynamic_power - calculate the dynamic power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  * @dfc:	Pointer to devfreq cooling device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  * @freq:	Frequency in Hz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)  * @voltage:	Voltage in millivolts
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)  * Calculate the dynamic power in milliwatts consumed by the device at
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)  * frequency @freq and voltage @voltage.  If the get_dynamic_power()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)  * was supplied as part of the devfreq_cooling_power struct, then that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)  * function is used.  Otherwise, a simple power model (Pdyn = Coeff *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)  * Voltage^2 * Frequency) is used.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) static unsigned long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) get_dynamic_power(struct devfreq_cooling_device *dfc, unsigned long freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		  unsigned long voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	u64 power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	u32 freq_mhz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 	struct devfreq_cooling_power *dfc_power = dfc->power_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	if (dfc_power->get_dynamic_power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 		return dfc_power->get_dynamic_power(dfc->devfreq, freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 						    voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	freq_mhz = freq / 1000000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 	power = (u64)dfc_power->dyn_power_coeff * freq_mhz * voltage * voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 	do_div(power, 1000000000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	return power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static inline unsigned long get_total_power(struct devfreq_cooling_device *dfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 					    unsigned long freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 					    unsigned long voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	return get_static_power(dfc, freq) + get_dynamic_power(dfc, freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 							       voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) static int devfreq_cooling_get_requested_power(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 					       u32 *power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	struct devfreq_cooling_device *dfc = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 	struct devfreq *df = dfc->devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) 	struct devfreq_dev_status *status = &df->last_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 	unsigned long state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 	unsigned long freq = status->current_frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 	unsigned long voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	u32 dyn_power = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 	u32 static_power = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	int res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 	state = freq_get_state(dfc, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (state == THERMAL_CSTATE_INVALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		res = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 	if (dfc->power_ops->get_real_power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		voltage = get_voltage(df, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		if (voltage == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 			res = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 		res = dfc->power_ops->get_real_power(df, power, freq, voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		if (!res) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 			state = dfc->capped_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 			dfc->res_util = dfc->power_table[state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 			dfc->res_util *= SCALE_ERROR_MITIGATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 			if (*power > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 				dfc->res_util /= *power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 			goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		dyn_power = dfc->power_table[state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		/* Scale dynamic power for utilization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 		dyn_power *= status->busy_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 		dyn_power /= status->total_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		/* Get static power */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		static_power = get_static_power(dfc, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 		*power = dyn_power + static_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	trace_thermal_power_devfreq_get_power(cdev, status, freq, dyn_power,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 					      static_power, *power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	/* It is safe to set max in this case */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	dfc->res_util = SCALE_ERROR_MITIGATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) static int devfreq_cooling_state2power(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) 				       unsigned long state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) 				       u32 *power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	struct devfreq_cooling_device *dfc = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 	unsigned long freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 	u32 static_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	if (state >= dfc->freq_table_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 	freq = dfc->freq_table[state];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 	static_power = get_static_power(dfc, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	*power = dfc->power_table[state] + static_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) static int devfreq_cooling_power2state(struct thermal_cooling_device *cdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 				       u32 power, unsigned long *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 	struct devfreq_cooling_device *dfc = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 	struct devfreq *df = dfc->devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	struct devfreq_dev_status *status = &df->last_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	unsigned long freq = status->current_frequency;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 	unsigned long busy_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	s32 dyn_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 	u32 static_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 	s32 est_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (dfc->power_ops->get_real_power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		/* Scale for resource utilization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		est_power = power * dfc->res_util;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 		est_power /= SCALE_ERROR_MITIGATION;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		static_power = get_static_power(dfc, freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 		dyn_power = power - static_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		dyn_power = dyn_power > 0 ? dyn_power : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 		/* Scale dynamic power for utilization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 		busy_time = status->busy_time ?: 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 		est_power = (dyn_power * status->total_time) / busy_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	 * Find the first cooling state that is within the power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 	 * budget for dynamic power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 	for (i = 0; i < dfc->freq_table_size - 1; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 		if (est_power >= dfc->power_table[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) 	*state = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) 	dfc->capped_state = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) 	trace_thermal_power_devfreq_limit(cdev, freq, *state, power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) static struct thermal_cooling_device_ops devfreq_cooling_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) 	.get_max_state = devfreq_cooling_get_max_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) 	.get_cur_state = devfreq_cooling_get_cur_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) 	.set_cur_state = devfreq_cooling_set_cur_state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * devfreq_cooling_gen_tables() - Generate power and freq tables.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  * @dfc: Pointer to devfreq cooling device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  * Generate power and frequency tables: the power table hold the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)  * device's maximum power usage at each cooling state (OPP).  The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)  * static and dynamic power using the appropriate voltage and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)  * frequency for the state, is acquired from the struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364)  * devfreq_cooling_power, and summed to make the maximum power draw.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366)  * The frequency table holds the frequencies in descending order.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)  * That way its indexed by cooling device state.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)  * The tables are malloced, and pointers put in dfc.  They must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)  * freed when unregistering the devfreq cooling device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)  * Return: 0 on success, negative error code on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static int devfreq_cooling_gen_tables(struct devfreq_cooling_device *dfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 	struct devfreq *df = dfc->devfreq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 	struct device *dev = df->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 	int ret, num_opps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 	unsigned long freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 	u32 *power_table = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 	u32 *freq_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 	num_opps = dev_pm_opp_get_opp_count(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 	if (dfc->power_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 		power_table = kcalloc(num_opps, sizeof(*power_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 				      GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		if (!power_table)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 	freq_table = kcalloc(num_opps, sizeof(*freq_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			     GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	if (!freq_table) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 		ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) 		goto free_power_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) 	for (i = 0, freq = ULONG_MAX; i < num_opps; i++, freq--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) 		unsigned long power, voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) 		struct dev_pm_opp *opp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) 		opp = dev_pm_opp_find_freq_floor(dev, &freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) 		if (IS_ERR(opp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) 			ret = PTR_ERR(opp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) 			goto free_tables;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) 		voltage = dev_pm_opp_get_voltage(opp) / 1000; /* mV */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) 		dev_pm_opp_put(opp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) 		if (dfc->power_ops) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) 			if (dfc->power_ops->get_real_power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) 				power = get_total_power(dfc, freq, voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) 				power = get_dynamic_power(dfc, freq, voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) 			dev_dbg(dev, "Power table: %lu MHz @ %lu mV: %lu = %lu mW\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) 				freq / 1000000, voltage, power, power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) 			power_table[i] = power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) 		freq_table[i] = freq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) 	if (dfc->power_ops)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) 		dfc->power_table = power_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) 	dfc->freq_table = freq_table;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) 	dfc->freq_table_size = num_opps;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) free_tables:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) 	kfree(freq_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) free_power_table:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) 	kfree(power_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)  * of_devfreq_cooling_register_power() - Register devfreq cooling device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)  *                                      with OF and power information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447)  * @np:	Pointer to OF device_node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)  * @df:	Pointer to devfreq device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449)  * @dfc_power:	Pointer to devfreq_cooling_power.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451)  * Register a devfreq cooling device.  The available OPPs must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)  * registered on the device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454)  * If @dfc_power is provided, the cooling device is registered with the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455)  * power extensions.  For the power extensions to work correctly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)  * devfreq should use the simple_ondemand governor, other governors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)  * are not currently supported.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) struct thermal_cooling_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) of_devfreq_cooling_register_power(struct device_node *np, struct devfreq *df,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) 				  struct devfreq_cooling_power *dfc_power)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) 	struct thermal_cooling_device *cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) 	struct devfreq_cooling_device *dfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) 	char dev_name[THERMAL_NAME_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) 	dfc = kzalloc(sizeof(*dfc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) 	if (!dfc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) 		return ERR_PTR(-ENOMEM);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) 	dfc->devfreq = df;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) 	if (dfc_power) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) 		dfc->power_ops = dfc_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) 		devfreq_cooling_ops.get_requested_power =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) 			devfreq_cooling_get_requested_power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) 		devfreq_cooling_ops.state2power = devfreq_cooling_state2power;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) 		devfreq_cooling_ops.power2state = devfreq_cooling_power2state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) 	err = devfreq_cooling_gen_tables(dfc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) 		goto free_dfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) 	err = dev_pm_qos_add_request(df->dev.parent, &dfc->req_max_freq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) 				     DEV_PM_QOS_MAX_FREQUENCY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) 				     PM_QOS_MAX_FREQUENCY_DEFAULT_VALUE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) 		goto free_tables;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) 	err = ida_simple_get(&devfreq_ida, 0, 0, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) 		goto remove_qos_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) 	dfc->id = err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) 	snprintf(dev_name, sizeof(dev_name), "thermal-devfreq-%d", dfc->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) 	cdev = thermal_of_cooling_device_register(np, dev_name, dfc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) 						  &devfreq_cooling_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) 	if (IS_ERR(cdev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) 		err = PTR_ERR(cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) 		dev_err(df->dev.parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) 			"Failed to register devfreq cooling device (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) 			err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) 		goto release_ida;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) 	dfc->cdev = cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) 	return cdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) release_ida:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) 	ida_simple_remove(&devfreq_ida, dfc->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) remove_qos_req:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) 	dev_pm_qos_remove_request(&dfc->req_max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) free_tables:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) 	kfree(dfc->power_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) 	kfree(dfc->freq_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) free_dfc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) 	kfree(dfc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) 	return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) EXPORT_SYMBOL_GPL(of_devfreq_cooling_register_power);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)  * of_devfreq_cooling_register() - Register devfreq cooling device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)  *                                with OF information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533)  * @np: Pointer to OF device_node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534)  * @df: Pointer to devfreq device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) struct thermal_cooling_device *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) of_devfreq_cooling_register(struct device_node *np, struct devfreq *df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) 	return of_devfreq_cooling_register_power(np, df, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) EXPORT_SYMBOL_GPL(of_devfreq_cooling_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)  * devfreq_cooling_register() - Register devfreq cooling device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)  * @df: Pointer to devfreq device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) struct thermal_cooling_device *devfreq_cooling_register(struct devfreq *df)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) 	return of_devfreq_cooling_register(NULL, df);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) EXPORT_SYMBOL_GPL(devfreq_cooling_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)  * devfreq_cooling_unregister() - Unregister devfreq cooling device.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)  * @cdev: Pointer to devfreq cooling device to unregister.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) void devfreq_cooling_unregister(struct thermal_cooling_device *cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) 	struct devfreq_cooling_device *dfc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) 	if (!cdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) 	dfc = cdev->devdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) 	thermal_cooling_device_unregister(dfc->cdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) 	ida_simple_remove(&devfreq_ida, dfc->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) 	dev_pm_qos_remove_request(&dfc->req_max_freq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) 	kfree(dfc->power_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) 	kfree(dfc->freq_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) 	kfree(dfc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) EXPORT_SYMBOL_GPL(devfreq_cooling_unregister);