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 <[email protected]> 7 * Copyright (C) 2008 Sujith Thomas <[email protected]> 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 32 struct thermal_zone_device; 33 struct thermal_cooling_device; 34 struct thermal_instance; 35 struct thermal_debugfs; 36 struct thermal_attr; 37 38 enum 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 */ 45 enum 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 }; 60 61 /** 62 * struct thermal_trip - representation of a point in temperature domain 63 * @temperature: temperature value in miliCelsius 64 * @hysteresis: relative hysteresis in miliCelsius 65 * @type: trip point type 66 * @priv: pointer to driver data associated with this trip 67 * @flags: flags representing binary properties of the trip 68 */ 69 struct thermal_trip { 70 int temperature; 71 int hysteresis; 72 enum thermal_trip_type type; 73 u8 flags; 74 void *priv; 75 }; 76 77 #define THERMAL_TRIP_FLAG_RW_TEMP BIT(0) 78 #define THERMAL_TRIP_FLAG_RW_HYST BIT(1) 79 80 #define THERMAL_TRIP_FLAG_RW (THERMAL_TRIP_FLAG_RW_TEMP | \ 81 THERMAL_TRIP_FLAG_RW_HYST) 82 83 #define THERMAL_TRIP_PRIV_TO_INT(_val_) (uintptr_t)(_val_) 84 #define THERMAL_INT_TO_TRIP_PRIV(_val_) (void *)(uintptr_t)(_val_) 85 86 struct thermal_zone_device; 87 88 struct thermal_zone_device_ops { 89 int (*bind) (struct thermal_zone_device *, 90 struct thermal_cooling_device *); 91 int (*unbind) (struct thermal_zone_device *, 92 struct thermal_cooling_device *); 93 int (*get_temp) (struct thermal_zone_device *, int *); 94 int (*set_trips) (struct thermal_zone_device *, int, int); 95 int (*change_mode) (struct thermal_zone_device *, 96 enum thermal_device_mode); 97 int (*set_trip_temp) (struct thermal_zone_device *, 98 const struct thermal_trip *, int); 99 int (*get_crit_temp) (struct thermal_zone_device *, int *); 100 int (*set_emul_temp) (struct thermal_zone_device *, int); 101 int (*get_trend) (struct thermal_zone_device *, 102 const struct thermal_trip *, enum thermal_trend *); 103 void (*hot)(struct thermal_zone_device *); 104 void (*critical)(struct thermal_zone_device *); 105 }; 106 107 struct thermal_cooling_device_ops { 108 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *); 109 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *); 110 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long); 111 int (*get_requested_power)(struct thermal_cooling_device *, u32 *); 112 int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *); 113 int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *); 114 }; 115 116 struct thermal_cooling_device { 117 int id; 118 const char *type; 119 unsigned long max_state; 120 struct device device; 121 struct device_node *np; 122 void *devdata; 123 void *stats; 124 const struct thermal_cooling_device_ops *ops; 125 bool updated; /* true if the cooling device does not need update */ 126 struct mutex lock; /* protect thermal_instances list */ 127 struct list_head thermal_instances; 128 struct list_head node; 129 #ifdef CONFIG_THERMAL_DEBUGFS 130 struct thermal_debugfs *debugfs; 131 #endif 132 }; 133 134 /* Structure to define Thermal Zone parameters */ 135 struct thermal_zone_params { 136 const char *governor_name; 137 138 /* 139 * a boolean to indicate if the thermal to hwmon sysfs interface 140 * is required. when no_hwmon == false, a hwmon sysfs interface 141 * will be created. when no_hwmon == true, nothing will be done 142 */ 143 bool no_hwmon; 144 145 /* 146 * Sustainable power (heat) that this thermal zone can dissipate in 147 * mW 148 */ 149 u32 sustainable_power; 150 151 /* 152 * Proportional parameter of the PID controller when 153 * overshooting (i.e., when temperature is below the target) 154 */ 155 s32 k_po; 156 157 /* 158 * Proportional parameter of the PID controller when 159 * undershooting 160 */ 161 s32 k_pu; 162 163 /* Integral parameter of the PID controller */ 164 s32 k_i; 165 166 /* Derivative parameter of the PID controller */ 167 s32 k_d; 168 169 /* threshold below which the error is no longer accumulated */ 170 s32 integral_cutoff; 171 172 /* 173 * @slope: slope of a linear temperature adjustment curve. 174 * Used by thermal zone drivers. 175 */ 176 int slope; 177 /* 178 * @offset: offset of a linear temperature adjustment curve. 179 * Used by thermal zone drivers (default 0). 180 */ 181 int offset; 182 }; 183 184 /* Function declarations */ 185 #ifdef CONFIG_THERMAL_OF 186 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, 187 const struct thermal_zone_device_ops *ops); 188 189 void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz); 190 191 #else 192 193 static inline 194 struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, 195 const struct thermal_zone_device_ops *ops) 196 { 197 return ERR_PTR(-ENOTSUPP); 198 } 199 200 static inline void devm_thermal_of_zone_unregister(struct device *dev, 201 struct thermal_zone_device *tz) 202 { 203 } 204 #endif 205 206 int for_each_thermal_trip(struct thermal_zone_device *tz, 207 int (*cb)(struct thermal_trip *, void *), 208 void *data); 209 int thermal_zone_for_each_trip(struct thermal_zone_device *tz, 210 int (*cb)(struct thermal_trip *, void *), 211 void *data); 212 void thermal_zone_set_trip_temp(struct thermal_zone_device *tz, 213 struct thermal_trip *trip, int temp); 214 215 int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp); 216 217 #ifdef CONFIG_THERMAL 218 struct thermal_zone_device *thermal_zone_device_register_with_trips( 219 const char *type, 220 const struct thermal_trip *trips, 221 int num_trips, void *devdata, 222 const struct thermal_zone_device_ops *ops, 223 const struct thermal_zone_params *tzp, 224 unsigned int passive_delay, 225 unsigned int polling_delay); 226 227 struct thermal_zone_device *thermal_tripless_zone_device_register( 228 const char *type, 229 void *devdata, 230 const struct thermal_zone_device_ops *ops, 231 const struct thermal_zone_params *tzp); 232 233 void thermal_zone_device_unregister(struct thermal_zone_device *tz); 234 235 void *thermal_zone_device_priv(struct thermal_zone_device *tzd); 236 const char *thermal_zone_device_type(struct thermal_zone_device *tzd); 237 int thermal_zone_device_id(struct thermal_zone_device *tzd); 238 struct device *thermal_zone_device(struct thermal_zone_device *tzd); 239 240 int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz, 241 const struct thermal_trip *trip, 242 struct thermal_cooling_device *cdev, 243 unsigned long upper, unsigned long lower, 244 unsigned int weight); 245 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int, 246 struct thermal_cooling_device *, 247 unsigned long, unsigned long, 248 unsigned int); 249 int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz, 250 const struct thermal_trip *trip, 251 struct thermal_cooling_device *cdev); 252 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int, 253 struct thermal_cooling_device *); 254 void thermal_zone_device_update(struct thermal_zone_device *, 255 enum thermal_notify_event); 256 257 struct thermal_cooling_device *thermal_cooling_device_register(const char *, 258 void *, const struct thermal_cooling_device_ops *); 259 struct thermal_cooling_device * 260 thermal_of_cooling_device_register(struct device_node *np, const char *, void *, 261 const struct thermal_cooling_device_ops *); 262 struct thermal_cooling_device * 263 devm_thermal_of_cooling_device_register(struct device *dev, 264 struct device_node *np, 265 const char *type, void *devdata, 266 const struct thermal_cooling_device_ops *ops); 267 void thermal_cooling_device_update(struct thermal_cooling_device *); 268 void thermal_cooling_device_unregister(struct thermal_cooling_device *); 269 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name); 270 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); 271 int thermal_zone_get_slope(struct thermal_zone_device *tz); 272 int thermal_zone_get_offset(struct thermal_zone_device *tz); 273 bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz, 274 const struct thermal_trip *trip, 275 struct thermal_cooling_device *cdev); 276 277 int thermal_zone_device_enable(struct thermal_zone_device *tz); 278 int thermal_zone_device_disable(struct thermal_zone_device *tz); 279 void thermal_zone_device_critical(struct thermal_zone_device *tz); 280 #else 281 static inline struct thermal_zone_device *thermal_zone_device_register_with_trips( 282 const char *type, 283 const struct thermal_trip *trips, 284 int num_trips, void *devdata, 285 const struct thermal_zone_device_ops *ops, 286 const struct thermal_zone_params *tzp, 287 int passive_delay, int polling_delay) 288 { return ERR_PTR(-ENODEV); } 289 290 static inline struct thermal_zone_device *thermal_tripless_zone_device_register( 291 const char *type, 292 void *devdata, 293 struct thermal_zone_device_ops *ops, 294 const struct thermal_zone_params *tzp) 295 { return ERR_PTR(-ENODEV); } 296 297 static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz) 298 { } 299 300 static inline struct thermal_cooling_device * 301 thermal_cooling_device_register(const char *type, void *devdata, 302 const struct thermal_cooling_device_ops *ops) 303 { return ERR_PTR(-ENODEV); } 304 static inline struct thermal_cooling_device * 305 thermal_of_cooling_device_register(struct device_node *np, 306 const char *type, void *devdata, 307 const struct thermal_cooling_device_ops *ops) 308 { return ERR_PTR(-ENODEV); } 309 static inline struct thermal_cooling_device * 310 devm_thermal_of_cooling_device_register(struct device *dev, 311 struct device_node *np, 312 const char *type, void *devdata, 313 const struct thermal_cooling_device_ops *ops) 314 { 315 return ERR_PTR(-ENODEV); 316 } 317 static inline void thermal_cooling_device_unregister( 318 struct thermal_cooling_device *cdev) 319 { } 320 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name( 321 const char *name) 322 { return ERR_PTR(-ENODEV); } 323 static inline int thermal_zone_get_temp( 324 struct thermal_zone_device *tz, int *temp) 325 { return -ENODEV; } 326 static inline int thermal_zone_get_slope( 327 struct thermal_zone_device *tz) 328 { return -ENODEV; } 329 static inline int thermal_zone_get_offset( 330 struct thermal_zone_device *tz) 331 { return -ENODEV; } 332 333 static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz) 334 { 335 return NULL; 336 } 337 338 static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd) 339 { 340 return NULL; 341 } 342 343 static inline int thermal_zone_device_id(struct thermal_zone_device *tzd) 344 { 345 return -ENODEV; 346 } 347 348 static inline int thermal_zone_device_enable(struct thermal_zone_device *tz) 349 { return -ENODEV; } 350 351 static inline int thermal_zone_device_disable(struct thermal_zone_device *tz) 352 { return -ENODEV; } 353 #endif /* CONFIG_THERMAL */ 354 355 #endif /* __THERMAL_H__ */ 356