at v5.8 16 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#define THERMAL_TRIPS_NONE -1 21#define THERMAL_MAX_TRIPS 12 22 23/* invalid cooling state */ 24#define THERMAL_CSTATE_INVALID -1UL 25 26/* No upper/lower limit requirement */ 27#define THERMAL_NO_LIMIT ((u32)~0) 28 29/* Default weight of a bound cooling device */ 30#define THERMAL_WEIGHT_DEFAULT 0 31 32/* use value, which < 0K, to indicate an invalid/uninitialized temperature */ 33#define THERMAL_TEMP_INVALID -274000 34 35struct thermal_zone_device; 36struct thermal_cooling_device; 37struct thermal_instance; 38struct thermal_attr; 39 40enum thermal_device_mode { 41 THERMAL_DEVICE_DISABLED = 0, 42 THERMAL_DEVICE_ENABLED, 43}; 44 45enum thermal_trip_type { 46 THERMAL_TRIP_ACTIVE = 0, 47 THERMAL_TRIP_PASSIVE, 48 THERMAL_TRIP_HOT, 49 THERMAL_TRIP_CRITICAL, 50}; 51 52enum thermal_trend { 53 THERMAL_TREND_STABLE, /* temperature is stable */ 54 THERMAL_TREND_RAISING, /* temperature is raising */ 55 THERMAL_TREND_DROPPING, /* temperature is dropping */ 56 THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */ 57 THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */ 58}; 59 60/* Thermal notification reason */ 61enum thermal_notify_event { 62 THERMAL_EVENT_UNSPECIFIED, /* Unspecified event */ 63 THERMAL_EVENT_TEMP_SAMPLE, /* New Temperature sample */ 64 THERMAL_TRIP_VIOLATED, /* TRIP Point violation */ 65 THERMAL_TRIP_CHANGED, /* TRIP Point temperature changed */ 66 THERMAL_DEVICE_DOWN, /* Thermal device is down */ 67 THERMAL_DEVICE_UP, /* Thermal device is up after a down event */ 68 THERMAL_DEVICE_POWER_CAPABILITY_CHANGED, /* power capability changed */ 69 THERMAL_TABLE_CHANGED, /* Thermal table(s) changed */ 70}; 71 72struct thermal_zone_device_ops { 73 int (*bind) (struct thermal_zone_device *, 74 struct thermal_cooling_device *); 75 int (*unbind) (struct thermal_zone_device *, 76 struct thermal_cooling_device *); 77 int (*get_temp) (struct thermal_zone_device *, int *); 78 int (*set_trips) (struct thermal_zone_device *, int, int); 79 int (*get_mode) (struct thermal_zone_device *, 80 enum thermal_device_mode *); 81 int (*set_mode) (struct thermal_zone_device *, 82 enum thermal_device_mode); 83 int (*get_trip_type) (struct thermal_zone_device *, int, 84 enum thermal_trip_type *); 85 int (*get_trip_temp) (struct thermal_zone_device *, int, int *); 86 int (*set_trip_temp) (struct thermal_zone_device *, int, int); 87 int (*get_trip_hyst) (struct thermal_zone_device *, int, int *); 88 int (*set_trip_hyst) (struct thermal_zone_device *, int, int); 89 int (*get_crit_temp) (struct thermal_zone_device *, int *); 90 int (*set_emul_temp) (struct thermal_zone_device *, int); 91 int (*get_trend) (struct thermal_zone_device *, int, 92 enum thermal_trend *); 93 int (*notify) (struct thermal_zone_device *, int, 94 enum thermal_trip_type); 95}; 96 97struct thermal_cooling_device_ops { 98 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *); 99 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *); 100 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long); 101 int (*get_requested_power)(struct thermal_cooling_device *, 102 struct thermal_zone_device *, u32 *); 103 int (*state2power)(struct thermal_cooling_device *, 104 struct thermal_zone_device *, unsigned long, u32 *); 105 int (*power2state)(struct thermal_cooling_device *, 106 struct thermal_zone_device *, u32, unsigned long *); 107}; 108 109struct thermal_cooling_device { 110 int id; 111 char type[THERMAL_NAME_LENGTH]; 112 struct device device; 113 struct device_node *np; 114 void *devdata; 115 void *stats; 116 const struct thermal_cooling_device_ops *ops; 117 bool updated; /* true if the cooling device does not need update */ 118 struct mutex lock; /* protect thermal_instances list */ 119 struct list_head thermal_instances; 120 struct list_head node; 121}; 122 123/** 124 * struct thermal_zone_device - structure for a thermal zone 125 * @id: unique id number for each thermal zone 126 * @type: the thermal zone device type 127 * @device: &struct device for this thermal zone 128 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature 129 * @trip_type_attrs: attributes for trip points for sysfs: trip type 130 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis 131 * @devdata: private pointer for device private data 132 * @trips: number of trip points the thermal zone supports 133 * @trips_disabled; bitmap for disabled trips 134 * @passive_delay: number of milliseconds to wait between polls when 135 * performing passive cooling. 136 * @polling_delay: number of milliseconds to wait between polls when 137 * checking whether trip points have been crossed (0 for 138 * interrupt driven systems) 139 * @temperature: current temperature. This is only for core code, 140 * drivers should use thermal_zone_get_temp() to get the 141 * current temperature 142 * @last_temperature: previous temperature read 143 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION 144 * @passive: 1 if you've crossed a passive trip point, 0 otherwise. 145 * @prev_low_trip: the low current temperature if you've crossed a passive 146 trip point. 147 * @prev_high_trip: the above current temperature if you've crossed a 148 passive trip point. 149 * @forced_passive: If > 0, temperature at which to switch on all ACPI 150 * processor cooling devices. Currently only used by the 151 * step-wise governor. 152 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked. 153 * @ops: operations this &thermal_zone_device supports 154 * @tzp: thermal zone parameters 155 * @governor: pointer to the governor for this thermal zone 156 * @governor_data: private pointer for governor data 157 * @thermal_instances: list of &struct thermal_instance of this thermal zone 158 * @ida: &struct ida to generate unique id for this zone's cooling 159 * devices 160 * @lock: lock to protect thermal_instances list 161 * @node: node in thermal_tz_list (in thermal_core.c) 162 * @poll_queue: delayed work for polling 163 * @notify_event: Last notification event 164 */ 165struct thermal_zone_device { 166 int id; 167 char type[THERMAL_NAME_LENGTH]; 168 struct device device; 169 struct attribute_group trips_attribute_group; 170 struct thermal_attr *trip_temp_attrs; 171 struct thermal_attr *trip_type_attrs; 172 struct thermal_attr *trip_hyst_attrs; 173 void *devdata; 174 int trips; 175 unsigned long trips_disabled; /* bitmap for disabled trips */ 176 int passive_delay; 177 int polling_delay; 178 int temperature; 179 int last_temperature; 180 int emul_temperature; 181 int passive; 182 int prev_low_trip; 183 int prev_high_trip; 184 unsigned int forced_passive; 185 atomic_t need_update; 186 struct thermal_zone_device_ops *ops; 187 struct thermal_zone_params *tzp; 188 struct thermal_governor *governor; 189 void *governor_data; 190 struct list_head thermal_instances; 191 struct ida ida; 192 struct mutex lock; 193 struct list_head node; 194 struct delayed_work poll_queue; 195 enum thermal_notify_event notify_event; 196}; 197 198/** 199 * struct thermal_governor - structure that holds thermal governor information 200 * @name: name of the governor 201 * @bind_to_tz: callback called when binding to a thermal zone. If it 202 * returns 0, the governor is bound to the thermal zone, 203 * otherwise it fails. 204 * @unbind_from_tz: callback called when a governor is unbound from a 205 * thermal zone. 206 * @throttle: callback called for every trip point even if temperature is 207 * below the trip point temperature 208 * @governor_list: node in thermal_governor_list (in thermal_core.c) 209 */ 210struct thermal_governor { 211 char name[THERMAL_NAME_LENGTH]; 212 int (*bind_to_tz)(struct thermal_zone_device *tz); 213 void (*unbind_from_tz)(struct thermal_zone_device *tz); 214 int (*throttle)(struct thermal_zone_device *tz, int trip); 215 struct list_head governor_list; 216}; 217 218/* Structure that holds binding parameters for a zone */ 219struct thermal_bind_params { 220 struct thermal_cooling_device *cdev; 221 222 /* 223 * This is a measure of 'how effectively these devices can 224 * cool 'this' thermal zone. It shall be determined by 225 * platform characterization. This value is relative to the 226 * rest of the weights so a cooling device whose weight is 227 * double that of another cooling device is twice as 228 * effective. See Documentation/driver-api/thermal/sysfs-api.rst for more 229 * information. 230 */ 231 int weight; 232 233 /* 234 * This is a bit mask that gives the binding relation between this 235 * thermal zone and cdev, for a particular trip point. 236 * See Documentation/driver-api/thermal/sysfs-api.rst for more information. 237 */ 238 int trip_mask; 239 240 /* 241 * This is an array of cooling state limits. Must have exactly 242 * 2 * thermal_zone.number_of_trip_points. It is an array consisting 243 * of tuples <lower-state upper-state> of state limits. Each trip 244 * will be associated with one state limit tuple when binding. 245 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS> 246 * on all trips. 247 */ 248 unsigned long *binding_limits; 249 int (*match) (struct thermal_zone_device *tz, 250 struct thermal_cooling_device *cdev); 251}; 252 253/* Structure to define Thermal Zone parameters */ 254struct thermal_zone_params { 255 char governor_name[THERMAL_NAME_LENGTH]; 256 257 /* 258 * a boolean to indicate if the thermal to hwmon sysfs interface 259 * is required. when no_hwmon == false, a hwmon sysfs interface 260 * will be created. when no_hwmon == true, nothing will be done 261 */ 262 bool no_hwmon; 263 264 int num_tbps; /* Number of tbp entries */ 265 struct thermal_bind_params *tbp; 266 267 /* 268 * Sustainable power (heat) that this thermal zone can dissipate in 269 * mW 270 */ 271 u32 sustainable_power; 272 273 /* 274 * Proportional parameter of the PID controller when 275 * overshooting (i.e., when temperature is below the target) 276 */ 277 s32 k_po; 278 279 /* 280 * Proportional parameter of the PID controller when 281 * undershooting 282 */ 283 s32 k_pu; 284 285 /* Integral parameter of the PID controller */ 286 s32 k_i; 287 288 /* Derivative parameter of the PID controller */ 289 s32 k_d; 290 291 /* threshold below which the error is no longer accumulated */ 292 s32 integral_cutoff; 293 294 /* 295 * @slope: slope of a linear temperature adjustment curve. 296 * Used by thermal zone drivers. 297 */ 298 int slope; 299 /* 300 * @offset: offset of a linear temperature adjustment curve. 301 * Used by thermal zone drivers (default 0). 302 */ 303 int offset; 304}; 305 306struct thermal_genl_event { 307 u32 orig; 308 enum events event; 309}; 310 311/** 312 * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones 313 * 314 * Mandatory: 315 * @get_temp: a pointer to a function that reads the sensor temperature. 316 * 317 * Optional: 318 * @get_trend: a pointer to a function that reads the sensor temperature trend. 319 * @set_trips: a pointer to a function that sets a temperature window. When 320 * this window is left the driver must inform the thermal core via 321 * thermal_zone_device_update. 322 * @set_emul_temp: a pointer to a function that sets sensor emulated 323 * temperature. 324 * @set_trip_temp: a pointer to a function that sets the trip temperature on 325 * hardware. 326 */ 327struct thermal_zone_of_device_ops { 328 int (*get_temp)(void *, int *); 329 int (*get_trend)(void *, int, enum thermal_trend *); 330 int (*set_trips)(void *, int, int); 331 int (*set_emul_temp)(void *, int); 332 int (*set_trip_temp)(void *, int, int); 333}; 334 335/* Function declarations */ 336#ifdef CONFIG_THERMAL_OF 337int thermal_zone_of_get_sensor_id(struct device_node *tz_np, 338 struct device_node *sensor_np, 339 u32 *id); 340struct thermal_zone_device * 341thermal_zone_of_sensor_register(struct device *dev, int id, void *data, 342 const struct thermal_zone_of_device_ops *ops); 343void thermal_zone_of_sensor_unregister(struct device *dev, 344 struct thermal_zone_device *tz); 345struct thermal_zone_device *devm_thermal_zone_of_sensor_register( 346 struct device *dev, int id, void *data, 347 const struct thermal_zone_of_device_ops *ops); 348void devm_thermal_zone_of_sensor_unregister(struct device *dev, 349 struct thermal_zone_device *tz); 350#else 351 352static inline int thermal_zone_of_get_sensor_id(struct device_node *tz_np, 353 struct device_node *sensor_np, 354 u32 *id) 355{ 356 return -ENOENT; 357} 358static inline struct thermal_zone_device * 359thermal_zone_of_sensor_register(struct device *dev, int id, void *data, 360 const struct thermal_zone_of_device_ops *ops) 361{ 362 return ERR_PTR(-ENODEV); 363} 364 365static inline 366void thermal_zone_of_sensor_unregister(struct device *dev, 367 struct thermal_zone_device *tz) 368{ 369} 370 371static inline struct thermal_zone_device *devm_thermal_zone_of_sensor_register( 372 struct device *dev, int id, void *data, 373 const struct thermal_zone_of_device_ops *ops) 374{ 375 return ERR_PTR(-ENODEV); 376} 377 378static inline 379void devm_thermal_zone_of_sensor_unregister(struct device *dev, 380 struct thermal_zone_device *tz) 381{ 382} 383 384#endif 385 386#ifdef CONFIG_THERMAL 387struct thermal_zone_device *thermal_zone_device_register(const char *, int, int, 388 void *, struct thermal_zone_device_ops *, 389 struct thermal_zone_params *, int, int); 390void thermal_zone_device_unregister(struct thermal_zone_device *); 391 392int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int, 393 struct thermal_cooling_device *, 394 unsigned long, unsigned long, 395 unsigned int); 396int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int, 397 struct thermal_cooling_device *); 398void thermal_zone_device_update(struct thermal_zone_device *, 399 enum thermal_notify_event); 400 401struct thermal_cooling_device *thermal_cooling_device_register(const char *, 402 void *, const struct thermal_cooling_device_ops *); 403struct thermal_cooling_device * 404thermal_of_cooling_device_register(struct device_node *np, const char *, void *, 405 const struct thermal_cooling_device_ops *); 406struct thermal_cooling_device * 407devm_thermal_of_cooling_device_register(struct device *dev, 408 struct device_node *np, 409 char *type, void *devdata, 410 const struct thermal_cooling_device_ops *ops); 411void thermal_cooling_device_unregister(struct thermal_cooling_device *); 412struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name); 413int thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp); 414int thermal_zone_get_slope(struct thermal_zone_device *tz); 415int thermal_zone_get_offset(struct thermal_zone_device *tz); 416 417void thermal_cdev_update(struct thermal_cooling_device *); 418void thermal_notify_framework(struct thermal_zone_device *, int); 419#else 420static inline struct thermal_zone_device *thermal_zone_device_register( 421 const char *type, int trips, int mask, void *devdata, 422 struct thermal_zone_device_ops *ops, 423 struct thermal_zone_params *tzp, 424 int passive_delay, int polling_delay) 425{ return ERR_PTR(-ENODEV); } 426static inline void thermal_zone_device_unregister( 427 struct thermal_zone_device *tz) 428{ } 429static inline struct thermal_cooling_device * 430thermal_cooling_device_register(char *type, void *devdata, 431 const struct thermal_cooling_device_ops *ops) 432{ return ERR_PTR(-ENODEV); } 433static inline struct thermal_cooling_device * 434thermal_of_cooling_device_register(struct device_node *np, 435 char *type, void *devdata, const struct thermal_cooling_device_ops *ops) 436{ return ERR_PTR(-ENODEV); } 437static inline struct thermal_cooling_device * 438devm_thermal_of_cooling_device_register(struct device *dev, 439 struct device_node *np, 440 char *type, void *devdata, 441 const struct thermal_cooling_device_ops *ops) 442{ 443 return ERR_PTR(-ENODEV); 444} 445static inline void thermal_cooling_device_unregister( 446 struct thermal_cooling_device *cdev) 447{ } 448static inline struct thermal_zone_device *thermal_zone_get_zone_by_name( 449 const char *name) 450{ return ERR_PTR(-ENODEV); } 451static inline int thermal_zone_get_temp( 452 struct thermal_zone_device *tz, int *temp) 453{ return -ENODEV; } 454static inline int thermal_zone_get_slope( 455 struct thermal_zone_device *tz) 456{ return -ENODEV; } 457static inline int thermal_zone_get_offset( 458 struct thermal_zone_device *tz) 459{ return -ENODEV; } 460 461static inline void thermal_cdev_update(struct thermal_cooling_device *cdev) 462{ } 463static inline void thermal_notify_framework(struct thermal_zone_device *tz, 464 int trip) 465{ } 466#endif /* CONFIG_THERMAL */ 467 468#endif /* __THERMAL_H__ */