at v6.13 12 kB view raw
1/* SPDX-License-Identifier: GPL-2.0 */ 2/* 3 * thermal.h ($Revision: 0 $) 4 * 5 * Copyright (C) 2008 Intel Corp 6 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> 7 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> 8 */ 9 10#ifndef __THERMAL_H__ 11#define __THERMAL_H__ 12 13#include <linux/of.h> 14#include <linux/idr.h> 15#include <linux/device.h> 16#include <linux/sysfs.h> 17#include <linux/workqueue.h> 18#include <uapi/linux/thermal.h> 19 20/* invalid cooling state */ 21#define THERMAL_CSTATE_INVALID -1UL 22 23/* No upper/lower limit requirement */ 24#define THERMAL_NO_LIMIT ((u32)~0) 25 26/* Default weight of a bound cooling device */ 27#define THERMAL_WEIGHT_DEFAULT 0 28 29/* use value, which < 0K, to indicate an invalid/uninitialized temperature */ 30#define THERMAL_TEMP_INVALID -274000 31 32struct thermal_zone_device; 33struct thermal_cooling_device; 34struct thermal_instance; 35struct thermal_debugfs; 36struct thermal_attr; 37 38enum thermal_trend { 39 THERMAL_TREND_STABLE, /* temperature is stable */ 40 THERMAL_TREND_RAISING, /* temperature is raising */ 41 THERMAL_TREND_DROPPING, /* temperature is dropping */ 42}; 43 44/* Thermal notification reason */ 45enum thermal_notify_event { 46 THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */ 47 THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */ 48 THERMAL_TRIP_VIOLATED, /* TRIP Point violation */ 49 THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */ 50 THERMAL_DEVICE_DOWN, /* Thermal device is down */ 51 THERMAL_DEVICE_UP, /* Thermal device is up after a down event */ 52 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */ 53 THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */ 54 THERMAL_EVENT_KEEP_ALIVE, /* Request for user space handler to respond */ 55 THERMAL_TZ_BIND_CDEV, /* Cooling dev is bind to the thermal zone */ 56 THERMAL_TZ_UNBIND_CDEV, /* Cooling dev is unbind from the thermal zone */ 57 THERMAL_INSTANCE_WEIGHT_CHANGED, /* Thermal instance weight changed */ 58 THERMAL_TZ_RESUME, /* Thermal zone is resuming after system sleep */ 59 THERMAL_TZ_ADD_THRESHOLD, /* Threshold added */ 60 THERMAL_TZ_DEL_THRESHOLD, /* Threshold deleted */ 61 THERMAL_TZ_FLUSH_THRESHOLDS, /* All thresholds deleted */ 62}; 63 64/** 65 * struct thermal_trip - representation of a point in temperature domain 66 * @temperature: temperature value in miliCelsius 67 * @hysteresis: relative hysteresis in miliCelsius 68 * @type: trip point type 69 * @priv: pointer to driver data associated with this trip 70 * @flags: flags representing binary properties of the trip 71 */ 72struct thermal_trip { 73 int temperature; 74 int hysteresis; 75 enum thermal_trip_type type; 76 u8 flags; 77 void *priv; 78}; 79 80#define THERMAL_TRIP_FLAG_RW_TEMP BIT(0) 81#define THERMAL_TRIP_FLAG_RW_HYST BIT(1) 82 83#define THERMAL_TRIP_FLAG_RW (THERMAL_TRIP_FLAG_RW_TEMP | \ 84 THERMAL_TRIP_FLAG_RW_HYST) 85 86#define THERMAL_TRIP_PRIV_TO_INT(_val_) (uintptr_t)(_val_) 87#define THERMAL_INT_TO_TRIP_PRIV(_val_) (void *)(uintptr_t)(_val_) 88 89struct thermal_zone_device; 90 91struct cooling_spec { 92 unsigned long upper; /* Highest cooling state */ 93 unsigned long lower; /* Lowest cooling state */ 94 unsigned int weight; /* Cooling device weight */ 95}; 96 97struct thermal_zone_device_ops { 98 bool (*should_bind) (struct thermal_zone_device *, 99 const struct thermal_trip *, 100 struct thermal_cooling_device *, 101 struct cooling_spec *); 102 int (*get_temp) (struct thermal_zone_device *, int *); 103 int (*set_trips) (struct thermal_zone_device *, int, int); 104 int (*change_mode) (struct thermal_zone_device *, 105 enum thermal_device_mode); 106 int (*set_trip_temp) (struct thermal_zone_device *, 107 const struct thermal_trip *, int); 108 int (*get_crit_temp) (struct thermal_zone_device *, int *); 109 int (*set_emul_temp) (struct thermal_zone_device *, int); 110 int (*get_trend) (struct thermal_zone_device *, 111 const struct thermal_trip *, enum thermal_trend *); 112 void (*hot)(struct thermal_zone_device *); 113 void (*critical)(struct thermal_zone_device *); 114}; 115 116struct thermal_cooling_device_ops { 117 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *); 118 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *); 119 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long); 120 int (*get_requested_power)(struct thermal_cooling_device *, u32 *); 121 int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *); 122 int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *); 123}; 124 125struct thermal_cooling_device { 126 int id; 127 const char *type; 128 unsigned long max_state; 129 struct device device; 130 struct device_node *np; 131 void *devdata; 132 void *stats; 133 const struct thermal_cooling_device_ops *ops; 134 bool updated; /* true if the cooling device does not need update */ 135 struct mutex lock; /* protect thermal_instances list */ 136 struct list_head thermal_instances; 137 struct list_head node; 138#ifdef CONFIG_THERMAL_DEBUGFS 139 struct thermal_debugfs *debugfs; 140#endif 141}; 142 143DEFINE_GUARD(cooling_dev, struct thermal_cooling_device *, mutex_lock(&_T->lock), 144 mutex_unlock(&_T->lock)) 145 146/* Structure to define Thermal Zone parameters */ 147struct thermal_zone_params { 148 const char *governor_name; 149 150 /* 151 * a boolean to indicate if the thermal to hwmon sysfs interface 152 * is required. when no_hwmon == false, a hwmon sysfs interface 153 * will be created. when no_hwmon == true, nothing will be done 154 */ 155 bool no_hwmon; 156 157 /* 158 * Sustainable power (heat) that this thermal zone can dissipate in 159 * mW 160 */ 161 u32 sustainable_power; 162 163 /* 164 * Proportional parameter of the PID controller when 165 * overshooting (i.e., when temperature is below the target) 166 */ 167 s32 k_po; 168 169 /* 170 * Proportional parameter of the PID controller when 171 * undershooting 172 */ 173 s32 k_pu; 174 175 /* Integral parameter of the PID controller */ 176 s32 k_i; 177 178 /* Derivative parameter of the PID controller */ 179 s32 k_d; 180 181 /* threshold below which the error is no longer accumulated */ 182 s32 integral_cutoff; 183 184 /* 185 * @slope: slope of a linear temperature adjustment curve. 186 * Used by thermal zone drivers. 187 */ 188 int slope; 189 /* 190 * @offset: offset of a linear temperature adjustment curve. 191 * Used by thermal zone drivers (default 0). 192 */ 193 int offset; 194}; 195 196/* Function declarations */ 197#ifdef CONFIG_THERMAL_OF 198struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, 199 const struct thermal_zone_device_ops *ops); 200 201void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz); 202 203#else 204 205static inline 206struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, 207 const struct thermal_zone_device_ops *ops) 208{ 209 return ERR_PTR(-ENOTSUPP); 210} 211 212static inline void devm_thermal_of_zone_unregister(struct device *dev, 213 struct thermal_zone_device *tz) 214{ 215} 216#endif 217 218int for_each_thermal_trip(struct thermal_zone_device *tz, 219 int (*cb)(struct thermal_trip *, void *), 220 void *data); 221int thermal_zone_for_each_trip(struct thermal_zone_device *tz, 222 int (*cb)(struct thermal_trip *, void *), 223 void *data); 224void thermal_zone_set_trip_temp(struct thermal_zone_device *tz, 225 struct thermal_trip *trip, int temp); 226 227int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp); 228 229#ifdef CONFIG_THERMAL 230struct thermal_zone_device *thermal_zone_device_register_with_trips( 231 const char *type, 232 const struct thermal_trip *trips, 233 int num_trips, void *devdata, 234 const struct thermal_zone_device_ops *ops, 235 const struct thermal_zone_params *tzp, 236 unsigned int passive_delay, 237 unsigned int polling_delay); 238 239struct thermal_zone_device *thermal_tripless_zone_device_register( 240 const char *type, 241 void *devdata, 242 const struct thermal_zone_device_ops *ops, 243 const struct thermal_zone_params *tzp); 244 245void thermal_zone_device_unregister(struct thermal_zone_device *tz); 246 247void *thermal_zone_device_priv(struct thermal_zone_device *tzd); 248const char *thermal_zone_device_type(struct thermal_zone_device *tzd); 249int thermal_zone_device_id(struct thermal_zone_device *tzd); 250struct device *thermal_zone_device(struct thermal_zone_device *tzd); 251 252void thermal_zone_device_update(struct thermal_zone_device *, 253 enum thermal_notify_event); 254 255struct thermal_cooling_device *thermal_cooling_device_register(const char *, 256 void *, const struct thermal_cooling_device_ops *); 257struct thermal_cooling_device * 258thermal_of_cooling_device_register(struct device_node *np, const char *, void *, 259 const struct thermal_cooling_device_ops *); 260struct thermal_cooling_device * 261devm_thermal_of_cooling_device_register(struct device *dev, 262 struct device_node *np, 263 const char *type, void *devdata, 264 const struct thermal_cooling_device_ops *ops); 265void thermal_cooling_device_update(struct thermal_cooling_device *); 266void thermal_cooling_device_unregister(struct thermal_cooling_device *); 267struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name); 268int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); 269int thermal_zone_get_slope(struct thermal_zone_device *tz); 270int thermal_zone_get_offset(struct thermal_zone_device *tz); 271bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz, 272 const struct thermal_trip *trip, 273 struct thermal_cooling_device *cdev); 274 275int thermal_zone_device_enable(struct thermal_zone_device *tz); 276int thermal_zone_device_disable(struct thermal_zone_device *tz); 277void thermal_zone_device_critical(struct thermal_zone_device *tz); 278#else 279static inline struct thermal_zone_device *thermal_zone_device_register_with_trips( 280 const char *type, 281 const struct thermal_trip *trips, 282 int num_trips, void *devdata, 283 const struct thermal_zone_device_ops *ops, 284 const struct thermal_zone_params *tzp, 285 int passive_delay, int polling_delay) 286{ return ERR_PTR(-ENODEV); } 287 288static inline struct thermal_zone_device *thermal_tripless_zone_device_register( 289 const char *type, 290 void *devdata, 291 struct thermal_zone_device_ops *ops, 292 const struct thermal_zone_params *tzp) 293{ return ERR_PTR(-ENODEV); } 294 295static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz) 296{ } 297 298static inline struct thermal_cooling_device * 299thermal_cooling_device_register(const char *type, void *devdata, 300 const struct thermal_cooling_device_ops *ops) 301{ return ERR_PTR(-ENODEV); } 302static inline struct thermal_cooling_device * 303thermal_of_cooling_device_register(struct device_node *np, 304 const char *type, void *devdata, 305 const struct thermal_cooling_device_ops *ops) 306{ return ERR_PTR(-ENODEV); } 307static inline struct thermal_cooling_device * 308devm_thermal_of_cooling_device_register(struct device *dev, 309 struct device_node *np, 310 const char *type, void *devdata, 311 const struct thermal_cooling_device_ops *ops) 312{ 313 return ERR_PTR(-ENODEV); 314} 315static inline void thermal_cooling_device_unregister( 316 struct thermal_cooling_device *cdev) 317{ } 318static inline struct thermal_zone_device *thermal_zone_get_zone_by_name( 319 const char *name) 320{ return ERR_PTR(-ENODEV); } 321static inline int thermal_zone_get_temp( 322 struct thermal_zone_device *tz, int *temp) 323{ return -ENODEV; } 324static inline int thermal_zone_get_slope( 325 struct thermal_zone_device *tz) 326{ return -ENODEV; } 327static inline int thermal_zone_get_offset( 328 struct thermal_zone_device *tz) 329{ return -ENODEV; } 330 331static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz) 332{ 333 return NULL; 334} 335 336static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd) 337{ 338 return NULL; 339} 340 341static inline int thermal_zone_device_id(struct thermal_zone_device *tzd) 342{ 343 return -ENODEV; 344} 345 346static inline int thermal_zone_device_enable(struct thermal_zone_device *tz) 347{ return -ENODEV; } 348 349static inline int thermal_zone_device_disable(struct thermal_zone_device *tz) 350{ return -ENODEV; } 351#endif /* CONFIG_THERMAL */ 352 353#endif /* __THERMAL_H__ */