| 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 | |
| 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 | 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 | */ |
| 72 | struct 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 | |
| 89 | struct cooling_spec { |
| 90 | unsigned long upper; /* Highest cooling state */ |
| 91 | unsigned long lower; /* Lowest cooling state */ |
| 92 | unsigned int weight; /* Cooling device weight */ |
| 93 | }; |
| 94 | |
| 95 | struct thermal_zone_device_ops { |
| 96 | bool (*should_bind) (struct thermal_zone_device *, |
| 97 | const struct thermal_trip *, |
| 98 | struct thermal_cooling_device *, |
| 99 | struct cooling_spec *); |
| 100 | int (*get_temp) (struct thermal_zone_device *, int *); |
| 101 | int (*set_trips) (struct thermal_zone_device *, int, int); |
| 102 | int (*change_mode) (struct thermal_zone_device *, |
| 103 | enum thermal_device_mode); |
| 104 | int (*set_trip_temp) (struct thermal_zone_device *, |
| 105 | const struct thermal_trip *, int); |
| 106 | int (*get_crit_temp) (struct thermal_zone_device *, int *); |
| 107 | int (*set_emul_temp) (struct thermal_zone_device *, int); |
| 108 | int (*get_trend) (struct thermal_zone_device *, |
| 109 | const struct thermal_trip *, enum thermal_trend *); |
| 110 | void (*hot)(struct thermal_zone_device *); |
| 111 | void (*critical)(struct thermal_zone_device *); |
| 112 | }; |
| 113 | |
| 114 | struct thermal_cooling_device_ops { |
| 115 | int (*get_max_state) (struct thermal_cooling_device *, unsigned long *); |
| 116 | int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *); |
| 117 | int (*set_cur_state) (struct thermal_cooling_device *, unsigned long); |
| 118 | int (*get_requested_power)(struct thermal_cooling_device *, u32 *); |
| 119 | int (*state2power)(struct thermal_cooling_device *, unsigned long, u32 *); |
| 120 | int (*power2state)(struct thermal_cooling_device *, u32, unsigned long *); |
| 121 | }; |
| 122 | |
| 123 | struct thermal_cooling_device { |
| 124 | int id; |
| 125 | const char *type; |
| 126 | unsigned long max_state; |
| 127 | struct device device; |
| 128 | struct device_node *np; |
| 129 | void *devdata; |
| 130 | void *stats; |
| 131 | const struct thermal_cooling_device_ops *ops; |
| 132 | bool updated; /* true if the cooling device does not need update */ |
| 133 | struct mutex lock; /* protect thermal_instances list */ |
| 134 | struct list_head thermal_instances; |
| 135 | struct list_head node; |
| 136 | #ifdef CONFIG_THERMAL_DEBUGFS |
| 137 | struct thermal_debugfs *debugfs; |
| 138 | #endif |
| 139 | }; |
| 140 | |
| 141 | DEFINE_GUARD(cooling_dev, struct thermal_cooling_device *, mutex_lock(&_T->lock), |
| 142 | mutex_unlock(&_T->lock)) |
| 143 | |
| 144 | /* Structure to define Thermal Zone parameters */ |
| 145 | struct thermal_zone_params { |
| 146 | const char *governor_name; |
| 147 | |
| 148 | /* |
| 149 | * a boolean to indicate if the thermal to hwmon sysfs interface |
| 150 | * is required. when no_hwmon == false, a hwmon sysfs interface |
| 151 | * will be created. when no_hwmon == true, nothing will be done |
| 152 | */ |
| 153 | bool no_hwmon; |
| 154 | |
| 155 | /* |
| 156 | * Sustainable power (heat) that this thermal zone can dissipate in |
| 157 | * mW |
| 158 | */ |
| 159 | u32 sustainable_power; |
| 160 | |
| 161 | /* |
| 162 | * Proportional parameter of the PID controller when |
| 163 | * overshooting (i.e., when temperature is below the target) |
| 164 | */ |
| 165 | s32 k_po; |
| 166 | |
| 167 | /* |
| 168 | * Proportional parameter of the PID controller when |
| 169 | * undershooting |
| 170 | */ |
| 171 | s32 k_pu; |
| 172 | |
| 173 | /* Integral parameter of the PID controller */ |
| 174 | s32 k_i; |
| 175 | |
| 176 | /* Derivative parameter of the PID controller */ |
| 177 | s32 k_d; |
| 178 | |
| 179 | /* threshold below which the error is no longer accumulated */ |
| 180 | s32 integral_cutoff; |
| 181 | |
| 182 | /* |
| 183 | * @slope: slope of a linear temperature adjustment curve. |
| 184 | * Used by thermal zone drivers. |
| 185 | */ |
| 186 | int slope; |
| 187 | /* |
| 188 | * @offset: offset of a linear temperature adjustment curve. |
| 189 | * Used by thermal zone drivers (default 0). |
| 190 | */ |
| 191 | int offset; |
| 192 | }; |
| 193 | |
| 194 | /* Function declarations */ |
| 195 | #ifdef CONFIG_THERMAL_OF |
| 196 | struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, |
| 197 | const struct thermal_zone_device_ops *ops); |
| 198 | |
| 199 | void devm_thermal_of_zone_unregister(struct device *dev, struct thermal_zone_device *tz); |
| 200 | |
| 201 | #else |
| 202 | |
| 203 | static inline |
| 204 | struct thermal_zone_device *devm_thermal_of_zone_register(struct device *dev, int id, void *data, |
| 205 | const struct thermal_zone_device_ops *ops) |
| 206 | { |
| 207 | return ERR_PTR(-ENOTSUPP); |
| 208 | } |
| 209 | |
| 210 | static inline void devm_thermal_of_zone_unregister(struct device *dev, |
| 211 | struct thermal_zone_device *tz) |
| 212 | { |
| 213 | } |
| 214 | #endif |
| 215 | |
| 216 | int for_each_thermal_trip(struct thermal_zone_device *tz, |
| 217 | int (*cb)(struct thermal_trip *, void *), |
| 218 | void *data); |
| 219 | int thermal_zone_for_each_trip(struct thermal_zone_device *tz, |
| 220 | int (*cb)(struct thermal_trip *, void *), |
| 221 | void *data); |
| 222 | void thermal_zone_set_trip_temp(struct thermal_zone_device *tz, |
| 223 | struct thermal_trip *trip, int temp); |
| 224 | |
| 225 | int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp); |
| 226 | |
| 227 | #ifdef CONFIG_THERMAL |
| 228 | struct thermal_zone_device *thermal_zone_device_register_with_trips( |
| 229 | const char *type, |
| 230 | const struct thermal_trip *trips, |
| 231 | int num_trips, void *devdata, |
| 232 | const struct thermal_zone_device_ops *ops, |
| 233 | const struct thermal_zone_params *tzp, |
| 234 | unsigned int passive_delay, |
| 235 | unsigned int polling_delay); |
| 236 | |
| 237 | struct thermal_zone_device *thermal_tripless_zone_device_register( |
| 238 | const char *type, |
| 239 | void *devdata, |
| 240 | const struct thermal_zone_device_ops *ops, |
| 241 | const struct thermal_zone_params *tzp); |
| 242 | |
| 243 | void thermal_zone_device_unregister(struct thermal_zone_device *tz); |
| 244 | |
| 245 | void *thermal_zone_device_priv(struct thermal_zone_device *tzd); |
| 246 | const char *thermal_zone_device_type(struct thermal_zone_device *tzd); |
| 247 | int thermal_zone_device_id(struct thermal_zone_device *tzd); |
| 248 | struct device *thermal_zone_device(struct thermal_zone_device *tzd); |
| 249 | |
| 250 | void thermal_zone_device_update(struct thermal_zone_device *, |
| 251 | enum thermal_notify_event); |
| 252 | |
| 253 | struct thermal_cooling_device *thermal_cooling_device_register(const char *, |
| 254 | void *, const struct thermal_cooling_device_ops *); |
| 255 | struct thermal_cooling_device * |
| 256 | thermal_of_cooling_device_register(struct device_node *np, const char *, void *, |
| 257 | const struct thermal_cooling_device_ops *); |
| 258 | struct thermal_cooling_device * |
| 259 | devm_thermal_of_cooling_device_register(struct device *dev, |
| 260 | struct device_node *np, |
| 261 | const char *type, void *devdata, |
| 262 | const struct thermal_cooling_device_ops *ops); |
| 263 | void thermal_cooling_device_update(struct thermal_cooling_device *); |
| 264 | void thermal_cooling_device_unregister(struct thermal_cooling_device *); |
| 265 | struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name); |
| 266 | int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); |
| 267 | int thermal_zone_get_slope(struct thermal_zone_device *tz); |
| 268 | int thermal_zone_get_offset(struct thermal_zone_device *tz); |
| 269 | bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz, |
| 270 | const struct thermal_trip *trip, |
| 271 | struct thermal_cooling_device *cdev); |
| 272 | |
| 273 | int thermal_zone_device_enable(struct thermal_zone_device *tz); |
| 274 | int thermal_zone_device_disable(struct thermal_zone_device *tz); |
| 275 | void thermal_zone_device_critical(struct thermal_zone_device *tz); |
| 276 | #else |
| 277 | static inline struct thermal_zone_device *thermal_zone_device_register_with_trips( |
| 278 | const char *type, |
| 279 | const struct thermal_trip *trips, |
| 280 | int num_trips, void *devdata, |
| 281 | const struct thermal_zone_device_ops *ops, |
| 282 | const struct thermal_zone_params *tzp, |
| 283 | int passive_delay, int polling_delay) |
| 284 | { return ERR_PTR(-ENODEV); } |
| 285 | |
| 286 | static inline struct thermal_zone_device *thermal_tripless_zone_device_register( |
| 287 | const char *type, |
| 288 | void *devdata, |
| 289 | struct thermal_zone_device_ops *ops, |
| 290 | const struct thermal_zone_params *tzp) |
| 291 | { return ERR_PTR(-ENODEV); } |
| 292 | |
| 293 | static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 294 | { } |
| 295 | |
| 296 | static inline void thermal_zone_device_update(struct thermal_zone_device *tz, |
| 297 | enum thermal_notify_event event) |
| 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__ */ |