| 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 | }; |
| 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 thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id, |
| 207 | struct thermal_trip *trip); |
| 208 | int for_each_thermal_trip(struct thermal_zone_device *tz, |
| 209 | int (*cb)(struct thermal_trip *, void *), |
| 210 | void *data); |
| 211 | int thermal_zone_for_each_trip(struct thermal_zone_device *tz, |
| 212 | int (*cb)(struct thermal_trip *, void *), |
| 213 | void *data); |
| 214 | int thermal_zone_get_num_trips(struct thermal_zone_device *tz); |
| 215 | void thermal_zone_set_trip_temp(struct thermal_zone_device *tz, |
| 216 | struct thermal_trip *trip, int temp); |
| 217 | |
| 218 | int thermal_zone_get_crit_temp(struct thermal_zone_device *tz, int *temp); |
| 219 | |
| 220 | #ifdef CONFIG_THERMAL |
| 221 | struct thermal_zone_device *thermal_zone_device_register_with_trips( |
| 222 | const char *type, |
| 223 | const struct thermal_trip *trips, |
| 224 | int num_trips, void *devdata, |
| 225 | const struct thermal_zone_device_ops *ops, |
| 226 | const struct thermal_zone_params *tzp, |
| 227 | unsigned int passive_delay, |
| 228 | unsigned int polling_delay); |
| 229 | |
| 230 | struct thermal_zone_device *thermal_tripless_zone_device_register( |
| 231 | const char *type, |
| 232 | void *devdata, |
| 233 | const struct thermal_zone_device_ops *ops, |
| 234 | const struct thermal_zone_params *tzp); |
| 235 | |
| 236 | void thermal_zone_device_unregister(struct thermal_zone_device *tz); |
| 237 | |
| 238 | void *thermal_zone_device_priv(struct thermal_zone_device *tzd); |
| 239 | const char *thermal_zone_device_type(struct thermal_zone_device *tzd); |
| 240 | int thermal_zone_device_id(struct thermal_zone_device *tzd); |
| 241 | struct device *thermal_zone_device(struct thermal_zone_device *tzd); |
| 242 | |
| 243 | int thermal_bind_cdev_to_trip(struct thermal_zone_device *tz, |
| 244 | const struct thermal_trip *trip, |
| 245 | struct thermal_cooling_device *cdev, |
| 246 | unsigned long upper, unsigned long lower, |
| 247 | unsigned int weight); |
| 248 | int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int, |
| 249 | struct thermal_cooling_device *, |
| 250 | unsigned long, unsigned long, |
| 251 | unsigned int); |
| 252 | int thermal_unbind_cdev_from_trip(struct thermal_zone_device *tz, |
| 253 | const struct thermal_trip *trip, |
| 254 | struct thermal_cooling_device *cdev); |
| 255 | int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int, |
| 256 | struct thermal_cooling_device *); |
| 257 | void thermal_zone_device_update(struct thermal_zone_device *, |
| 258 | enum thermal_notify_event); |
| 259 | |
| 260 | struct thermal_cooling_device *thermal_cooling_device_register(const char *, |
| 261 | void *, const struct thermal_cooling_device_ops *); |
| 262 | struct thermal_cooling_device * |
| 263 | thermal_of_cooling_device_register(struct device_node *np, const char *, void *, |
| 264 | const struct thermal_cooling_device_ops *); |
| 265 | struct thermal_cooling_device * |
| 266 | devm_thermal_of_cooling_device_register(struct device *dev, |
| 267 | struct device_node *np, |
| 268 | const char *type, void *devdata, |
| 269 | const struct thermal_cooling_device_ops *ops); |
| 270 | void thermal_cooling_device_update(struct thermal_cooling_device *); |
| 271 | void thermal_cooling_device_unregister(struct thermal_cooling_device *); |
| 272 | struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name); |
| 273 | int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); |
| 274 | int thermal_zone_get_slope(struct thermal_zone_device *tz); |
| 275 | int thermal_zone_get_offset(struct thermal_zone_device *tz); |
| 276 | bool thermal_trip_is_bound_to_cdev(struct thermal_zone_device *tz, |
| 277 | const struct thermal_trip *trip, |
| 278 | struct thermal_cooling_device *cdev); |
| 279 | |
| 280 | int thermal_zone_device_enable(struct thermal_zone_device *tz); |
| 281 | int thermal_zone_device_disable(struct thermal_zone_device *tz); |
| 282 | void thermal_zone_device_critical(struct thermal_zone_device *tz); |
| 283 | #else |
| 284 | static inline struct thermal_zone_device *thermal_zone_device_register_with_trips( |
| 285 | const char *type, |
| 286 | const struct thermal_trip *trips, |
| 287 | int num_trips, void *devdata, |
| 288 | const struct thermal_zone_device_ops *ops, |
| 289 | const struct thermal_zone_params *tzp, |
| 290 | int passive_delay, int polling_delay) |
| 291 | { return ERR_PTR(-ENODEV); } |
| 292 | |
| 293 | static inline struct thermal_zone_device *thermal_tripless_zone_device_register( |
| 294 | const char *type, |
| 295 | void *devdata, |
| 296 | struct thermal_zone_device_ops *ops, |
| 297 | const struct thermal_zone_params *tzp) |
| 298 | { return ERR_PTR(-ENODEV); } |
| 299 | |
| 300 | static inline void thermal_zone_device_unregister(struct thermal_zone_device *tz) |
| 301 | { } |
| 302 | |
| 303 | static inline struct thermal_cooling_device * |
| 304 | thermal_cooling_device_register(const char *type, void *devdata, |
| 305 | const struct thermal_cooling_device_ops *ops) |
| 306 | { return ERR_PTR(-ENODEV); } |
| 307 | static inline struct thermal_cooling_device * |
| 308 | thermal_of_cooling_device_register(struct device_node *np, |
| 309 | const char *type, void *devdata, |
| 310 | const struct thermal_cooling_device_ops *ops) |
| 311 | { return ERR_PTR(-ENODEV); } |
| 312 | static inline struct thermal_cooling_device * |
| 313 | devm_thermal_of_cooling_device_register(struct device *dev, |
| 314 | struct device_node *np, |
| 315 | const char *type, void *devdata, |
| 316 | const struct thermal_cooling_device_ops *ops) |
| 317 | { |
| 318 | return ERR_PTR(-ENODEV); |
| 319 | } |
| 320 | static inline void thermal_cooling_device_unregister( |
| 321 | struct thermal_cooling_device *cdev) |
| 322 | { } |
| 323 | static inline struct thermal_zone_device *thermal_zone_get_zone_by_name( |
| 324 | const char *name) |
| 325 | { return ERR_PTR(-ENODEV); } |
| 326 | static inline int thermal_zone_get_temp( |
| 327 | struct thermal_zone_device *tz, int *temp) |
| 328 | { return -ENODEV; } |
| 329 | static inline int thermal_zone_get_slope( |
| 330 | struct thermal_zone_device *tz) |
| 331 | { return -ENODEV; } |
| 332 | static inline int thermal_zone_get_offset( |
| 333 | struct thermal_zone_device *tz) |
| 334 | { return -ENODEV; } |
| 335 | |
| 336 | static inline void *thermal_zone_device_priv(struct thermal_zone_device *tz) |
| 337 | { |
| 338 | return NULL; |
| 339 | } |
| 340 | |
| 341 | static inline const char *thermal_zone_device_type(struct thermal_zone_device *tzd) |
| 342 | { |
| 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | static inline int thermal_zone_device_id(struct thermal_zone_device *tzd) |
| 347 | { |
| 348 | return -ENODEV; |
| 349 | } |
| 350 | |
| 351 | static inline int thermal_zone_device_enable(struct thermal_zone_device *tz) |
| 352 | { return -ENODEV; } |
| 353 | |
| 354 | static inline int thermal_zone_device_disable(struct thermal_zone_device *tz) |
| 355 | { return -ENODEV; } |
| 356 | #endif /* CONFIG_THERMAL */ |
| 357 | |
| 358 | #endif /* __THERMAL_H__ */ |