1 /* 2 * thermal.h ($Revision: 0 $) 3 * 4 * Copyright (C) 2008 Intel Corp 5 * Copyright (C) 2008 Zhang Rui <[email protected]> 6 * Copyright (C) 2008 Sujith Thomas <[email protected]> 7 * 8 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; version 2 of the License. 12 * 13 * This program is distributed in the hope that it will be useful, but 14 * WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 * General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License along 19 * with this program; if not, write to the Free Software Foundation, Inc., 20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 21 * 22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 23 */ 24 25 #ifndef __THERMAL_H__ 26 #define __THERMAL_H__ 27 28 #include <linux/of.h> 29 #include <linux/idr.h> 30 #include <linux/device.h> 31 #include <linux/workqueue.h> 32 #include <uapi/linux/thermal.h> 33 34 #define THERMAL_TRIPS_NONE -1 35 #define THERMAL_MAX_TRIPS 12 36 37 /* invalid cooling state */ 38 #define THERMAL_CSTATE_INVALID -1UL 39 40 /* No upper/lower limit requirement */ 41 #define THERMAL_NO_LIMIT ((u32)~0) 42 43 /* Default weight of a bound cooling device */ 44 #define THERMAL_WEIGHT_DEFAULT 0 45 46 /* Unit conversion macros */ 47 #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732 >= 0) ? \ 48 ((long)t-2732+5)/10 : ((long)t-2732-5)/10) 49 #define CELSIUS_TO_KELVIN(t) ((t)*10+2732) 50 #define DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, off) (((t) - (off)) * 100) 51 #define DECI_KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, 2732) 52 #define MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, off) (((t) / 100) + (off)) 53 #define MILLICELSIUS_TO_DECI_KELVIN(t) MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, 2732) 54 55 /* Default Thermal Governor */ 56 #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE) 57 #define DEFAULT_THERMAL_GOVERNOR "step_wise" 58 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE) 59 #define DEFAULT_THERMAL_GOVERNOR "fair_share" 60 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE) 61 #define DEFAULT_THERMAL_GOVERNOR "user_space" 62 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR) 63 #define DEFAULT_THERMAL_GOVERNOR "power_allocator" 64 #endif 65 66 struct thermal_zone_device; 67 struct thermal_cooling_device; 68 struct thermal_instance; 69 70 enum thermal_device_mode { 71 THERMAL_DEVICE_DISABLED = 0, 72 THERMAL_DEVICE_ENABLED, 73 }; 74 75 enum thermal_trip_type { 76 THERMAL_TRIP_ACTIVE = 0, 77 THERMAL_TRIP_PASSIVE, 78 THERMAL_TRIP_HOT, 79 THERMAL_TRIP_CRITICAL, 80 }; 81 82 enum thermal_trend { 83 THERMAL_TREND_STABLE, /* temperature is stable */ 84 THERMAL_TREND_RAISING, /* temperature is raising */ 85 THERMAL_TREND_DROPPING, /* temperature is dropping */ 86 THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */ 87 THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */ 88 }; 89 90 struct thermal_zone_device_ops { 91 int (*bind) (struct thermal_zone_device *, 92 struct thermal_cooling_device *); 93 int (*unbind) (struct thermal_zone_device *, 94 struct thermal_cooling_device *); 95 int (*get_temp) (struct thermal_zone_device *, int *); 96 int (*get_mode) (struct thermal_zone_device *, 97 enum thermal_device_mode *); 98 int (*set_mode) (struct thermal_zone_device *, 99 enum thermal_device_mode); 100 int (*get_trip_type) (struct thermal_zone_device *, int, 101 enum thermal_trip_type *); 102 int (*get_trip_temp) (struct thermal_zone_device *, int, int *); 103 int (*set_trip_temp) (struct thermal_zone_device *, int, int); 104 int (*get_trip_hyst) (struct thermal_zone_device *, int, int *); 105 int (*set_trip_hyst) (struct thermal_zone_device *, int, 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 *, int, 109 enum thermal_trend *); 110 int (*notify) (struct thermal_zone_device *, int, 111 enum thermal_trip_type); 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 *, 119 struct thermal_zone_device *, u32 *); 120 int (*state2power)(struct thermal_cooling_device *, 121 struct thermal_zone_device *, unsigned long, u32 *); 122 int (*power2state)(struct thermal_cooling_device *, 123 struct thermal_zone_device *, u32, unsigned long *); 124 }; 125 126 struct thermal_cooling_device { 127 int id; 128 char type[THERMAL_NAME_LENGTH]; 129 struct device device; 130 struct device_node *np; 131 void *devdata; 132 const struct thermal_cooling_device_ops *ops; 133 bool updated; /* true if the cooling device does not need update */ 134 struct mutex lock; /* protect thermal_instances list */ 135 struct list_head thermal_instances; 136 struct list_head node; 137 }; 138 139 struct thermal_attr { 140 struct device_attribute attr; 141 char name[THERMAL_NAME_LENGTH]; 142 }; 143 144 /** 145 * struct thermal_zone_device - structure for a thermal zone 146 * @id: unique id number for each thermal zone 147 * @type: the thermal zone device type 148 * @device: &struct device for this thermal zone 149 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature 150 * @trip_type_attrs: attributes for trip points for sysfs: trip type 151 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis 152 * @devdata: private pointer for device private data 153 * @trips: number of trip points the thermal zone supports 154 * @passive_delay: number of milliseconds to wait between polls when 155 * performing passive cooling. 156 * @polling_delay: number of milliseconds to wait between polls when 157 * checking whether trip points have been crossed (0 for 158 * interrupt driven systems) 159 * @temperature: current temperature. This is only for core code, 160 * drivers should use thermal_zone_get_temp() to get the 161 * current temperature 162 * @last_temperature: previous temperature read 163 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION 164 * @passive: 1 if you've crossed a passive trip point, 0 otherwise. 165 * @forced_passive: If > 0, temperature at which to switch on all ACPI 166 * processor cooling devices. Currently only used by the 167 * step-wise governor. 168 * @ops: operations this &thermal_zone_device supports 169 * @tzp: thermal zone parameters 170 * @governor: pointer to the governor for this thermal zone 171 * @governor_data: private pointer for governor data 172 * @thermal_instances: list of &struct thermal_instance of this thermal zone 173 * @idr: &struct idr to generate unique id for this zone's cooling 174 * devices 175 * @lock: lock to protect thermal_instances list 176 * @node: node in thermal_tz_list (in thermal_core.c) 177 * @poll_queue: delayed work for polling 178 */ 179 struct thermal_zone_device { 180 int id; 181 char type[THERMAL_NAME_LENGTH]; 182 struct device device; 183 struct thermal_attr *trip_temp_attrs; 184 struct thermal_attr *trip_type_attrs; 185 struct thermal_attr *trip_hyst_attrs; 186 void *devdata; 187 int trips; 188 int passive_delay; 189 int polling_delay; 190 int temperature; 191 int last_temperature; 192 int emul_temperature; 193 int passive; 194 unsigned int forced_passive; 195 struct thermal_zone_device_ops *ops; 196 struct thermal_zone_params *tzp; 197 struct thermal_governor *governor; 198 void *governor_data; 199 struct list_head thermal_instances; 200 struct idr idr; 201 struct mutex lock; 202 struct list_head node; 203 struct delayed_work poll_queue; 204 }; 205 206 /** 207 * struct thermal_governor - structure that holds thermal governor information 208 * @name: name of the governor 209 * @bind_to_tz: callback called when binding to a thermal zone. If it 210 * returns 0, the governor is bound to the thermal zone, 211 * otherwise it fails. 212 * @unbind_from_tz: callback called when a governor is unbound from a 213 * thermal zone. 214 * @throttle: callback called for every trip point even if temperature is 215 * below the trip point temperature 216 * @governor_list: node in thermal_governor_list (in thermal_core.c) 217 */ 218 struct thermal_governor { 219 char name[THERMAL_NAME_LENGTH]; 220 int (*bind_to_tz)(struct thermal_zone_device *tz); 221 void (*unbind_from_tz)(struct thermal_zone_device *tz); 222 int (*throttle)(struct thermal_zone_device *tz, int trip); 223 struct list_head governor_list; 224 }; 225 226 /* Structure that holds binding parameters for a zone */ 227 struct thermal_bind_params { 228 struct thermal_cooling_device *cdev; 229 230 /* 231 * This is a measure of 'how effectively these devices can 232 * cool 'this' thermal zone. It shall be determined by 233 * platform characterization. This value is relative to the 234 * rest of the weights so a cooling device whose weight is 235 * double that of another cooling device is twice as 236 * effective. See Documentation/thermal/sysfs-api.txt for more 237 * information. 238 */ 239 int weight; 240 241 /* 242 * This is a bit mask that gives the binding relation between this 243 * thermal zone and cdev, for a particular trip point. 244 * See Documentation/thermal/sysfs-api.txt for more information. 245 */ 246 int trip_mask; 247 248 /* 249 * This is an array of cooling state limits. Must have exactly 250 * 2 * thermal_zone.number_of_trip_points. It is an array consisting 251 * of tuples <lower-state upper-state> of state limits. Each trip 252 * will be associated with one state limit tuple when binding. 253 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS> 254 * on all trips. 255 */ 256 unsigned long *binding_limits; 257 int (*match) (struct thermal_zone_device *tz, 258 struct thermal_cooling_device *cdev); 259 }; 260 261 /* Structure to define Thermal Zone parameters */ 262 struct thermal_zone_params { 263 char governor_name[THERMAL_NAME_LENGTH]; 264 265 /* 266 * a boolean to indicate if the thermal to hwmon sysfs interface 267 * is required. when no_hwmon == false, a hwmon sysfs interface 268 * will be created. when no_hwmon == true, nothing will be done 269 */ 270 bool no_hwmon; 271 272 int num_tbps; /* Number of tbp entries */ 273 struct thermal_bind_params *tbp; 274 275 /* 276 * Sustainable power (heat) that this thermal zone can dissipate in 277 * mW 278 */ 279 u32 sustainable_power; 280 281 /* 282 * Proportional parameter of the PID controller when 283 * overshooting (i.e., when temperature is below the target) 284 */ 285 s32 k_po; 286 287 /* 288 * Proportional parameter of the PID controller when 289 * undershooting 290 */ 291 s32 k_pu; 292 293 /* Integral parameter of the PID controller */ 294 s32 k_i; 295 296 /* Derivative parameter of the PID controller */ 297 s32 k_d; 298 299 /* threshold below which the error is no longer accumulated */ 300 s32 integral_cutoff; 301 302 /* 303 * @slope: slope of a linear temperature adjustment curve. 304 * Used by thermal zone drivers. 305 */ 306 int slope; 307 /* 308 * @offset: offset of a linear temperature adjustment curve. 309 * Used by thermal zone drivers (default 0). 310 */ 311 int offset; 312 }; 313 314 struct thermal_genl_event { 315 u32 orig; 316 enum events event; 317 }; 318 319 /** 320 * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones 321 * 322 * Mandatory: 323 * @get_temp: a pointer to a function that reads the sensor temperature. 324 * 325 * Optional: 326 * @get_trend: a pointer to a function that reads the sensor temperature trend. 327 * @set_emul_temp: a pointer to a function that sets sensor emulated 328 * temperature. 329 */ 330 struct thermal_zone_of_device_ops { 331 int (*get_temp)(void *, int *); 332 int (*get_trend)(void *, long *); 333 int (*set_emul_temp)(void *, int); 334 }; 335 336 /** 337 * struct thermal_trip - representation of a point in temperature domain 338 * @np: pointer to struct device_node that this trip point was created from 339 * @temperature: temperature value in miliCelsius 340 * @hysteresis: relative hysteresis in miliCelsius 341 * @type: trip point type 342 */ 343 344 struct thermal_trip { 345 struct device_node *np; 346 unsigned long int temperature; 347 unsigned long int hysteresis; 348 enum thermal_trip_type type; 349 }; 350 351 /* Function declarations */ 352 #ifdef CONFIG_THERMAL_OF 353 struct thermal_zone_device * 354 thermal_zone_of_sensor_register(struct device *dev, int id, void *data, 355 const struct thermal_zone_of_device_ops *ops); 356 void thermal_zone_of_sensor_unregister(struct device *dev, 357 struct thermal_zone_device *tz); 358 #else 359 static inline struct thermal_zone_device * 360 thermal_zone_of_sensor_register(struct device *dev, int id, void *data, 361 const struct thermal_zone_of_device_ops *ops) 362 { 363 return NULL; 364 } 365 366 static inline 367 void thermal_zone_of_sensor_unregister(struct device *dev, 368 struct thermal_zone_device *tz) 369 { 370 } 371 372 #endif 373 374 #if IS_ENABLED(CONFIG_THERMAL) 375 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev) 376 { 377 return cdev->ops->get_requested_power && cdev->ops->state2power && 378 cdev->ops->power2state; 379 } 380 381 int power_actor_get_max_power(struct thermal_cooling_device *, 382 struct thermal_zone_device *tz, u32 *max_power); 383 int power_actor_set_power(struct thermal_cooling_device *, 384 struct thermal_instance *, u32); 385 struct thermal_zone_device *thermal_zone_device_register(const char *, int, int, 386 void *, struct thermal_zone_device_ops *, 387 struct thermal_zone_params *, int, int); 388 void thermal_zone_device_unregister(struct thermal_zone_device *); 389 390 int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int, 391 struct thermal_cooling_device *, 392 unsigned long, unsigned long, 393 unsigned int); 394 int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int, 395 struct thermal_cooling_device *); 396 void thermal_zone_device_update(struct thermal_zone_device *); 397 398 struct thermal_cooling_device *thermal_cooling_device_register(char *, void *, 399 const struct thermal_cooling_device_ops *); 400 struct thermal_cooling_device * 401 thermal_of_cooling_device_register(struct device_node *np, char *, void *, 402 const struct thermal_cooling_device_ops *); 403 void thermal_cooling_device_unregister(struct thermal_cooling_device *); 404 struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name); 405 int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); 406 407 int get_tz_trend(struct thermal_zone_device *, int); 408 struct thermal_instance *get_thermal_instance(struct thermal_zone_device *, 409 struct thermal_cooling_device *, int); 410 void thermal_cdev_update(struct thermal_cooling_device *); 411 void thermal_notify_framework(struct thermal_zone_device *, int); 412 #else 413 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev) 414 { return false; } 415 static inline int power_actor_get_max_power(struct thermal_cooling_device *cdev, 416 struct thermal_zone_device *tz, u32 *max_power) 417 { return 0; } 418 static inline int power_actor_set_power(struct thermal_cooling_device *cdev, 419 struct thermal_instance *tz, u32 power) 420 { return 0; } 421 static inline struct thermal_zone_device *thermal_zone_device_register( 422 const char *type, int trips, int mask, void *devdata, 423 struct thermal_zone_device_ops *ops, 424 const struct thermal_zone_params *tzp, 425 int passive_delay, int polling_delay) 426 { return ERR_PTR(-ENODEV); } 427 static inline void thermal_zone_device_unregister( 428 struct thermal_zone_device *tz) 429 { } 430 static inline int thermal_zone_bind_cooling_device( 431 struct thermal_zone_device *tz, int trip, 432 struct thermal_cooling_device *cdev, 433 unsigned long upper, unsigned long lower) 434 { return -ENODEV; } 435 static inline int thermal_zone_unbind_cooling_device( 436 struct thermal_zone_device *tz, int trip, 437 struct thermal_cooling_device *cdev) 438 { return -ENODEV; } 439 static inline void thermal_zone_device_update(struct thermal_zone_device *tz) 440 { } 441 static inline struct thermal_cooling_device * 442 thermal_cooling_device_register(char *type, void *devdata, 443 const struct thermal_cooling_device_ops *ops) 444 { return ERR_PTR(-ENODEV); } 445 static inline struct thermal_cooling_device * 446 thermal_of_cooling_device_register(struct device_node *np, 447 char *type, void *devdata, const struct thermal_cooling_device_ops *ops) 448 { return ERR_PTR(-ENODEV); } 449 static inline void thermal_cooling_device_unregister( 450 struct thermal_cooling_device *cdev) 451 { } 452 static inline struct thermal_zone_device *thermal_zone_get_zone_by_name( 453 const char *name) 454 { return ERR_PTR(-ENODEV); } 455 static inline int thermal_zone_get_temp( 456 struct thermal_zone_device *tz, int *temp) 457 { return -ENODEV; } 458 static inline int get_tz_trend(struct thermal_zone_device *tz, int trip) 459 { return -ENODEV; } 460 static inline struct thermal_instance * 461 get_thermal_instance(struct thermal_zone_device *tz, 462 struct thermal_cooling_device *cdev, int trip) 463 { return ERR_PTR(-ENODEV); } 464 static inline void thermal_cdev_update(struct thermal_cooling_device *cdev) 465 { } 466 static inline void thermal_notify_framework(struct thermal_zone_device *tz, 467 int trip) 468 { } 469 #endif /* CONFIG_THERMAL */ 470 471 #if defined(CONFIG_NET) && IS_ENABLED(CONFIG_THERMAL) 472 extern int thermal_generate_netlink_event(struct thermal_zone_device *tz, 473 enum events event); 474 #else 475 static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz, 476 enum events event) 477 { 478 return 0; 479 } 480 #endif 481 482 #endif /* __THERMAL_H__ */ 483