1/* 2 * thermal.h ($Revision: 0 $) 3 * 4 * Copyright (C) 2008 Intel Corp 5 * Copyright (C) 2008 Zhang Rui <rui.zhang@intel.com> 6 * Copyright (C) 2008 Sujith Thomas <sujith.thomas@intel.com> 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/* use value, which < 0K, to indicate an invalid/uninitialized temperature */ 44#define THERMAL_TEMP_INVALID -274000 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#endif 63 64struct thermal_zone_device; 65struct thermal_cooling_device; 66 67enum thermal_device_mode { 68 THERMAL_DEVICE_DISABLED = 0, 69 THERMAL_DEVICE_ENABLED, 70}; 71 72enum thermal_trip_type { 73 THERMAL_TRIP_ACTIVE = 0, 74 THERMAL_TRIP_PASSIVE, 75 THERMAL_TRIP_HOT, 76 THERMAL_TRIP_CRITICAL, 77}; 78 79enum thermal_trend { 80 THERMAL_TREND_STABLE, /* temperature is stable */ 81 THERMAL_TREND_RAISING, /* temperature is raising */ 82 THERMAL_TREND_DROPPING, /* temperature is dropping */ 83 THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */ 84 THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */ 85}; 86 87struct thermal_zone_device_ops { 88 int (*bind) (struct thermal_zone_device *, 89 struct thermal_cooling_device *); 90 int (*unbind) (struct thermal_zone_device *, 91 struct thermal_cooling_device *); 92 int (*get_temp) (struct thermal_zone_device *, unsigned long *); 93 int (*get_mode) (struct thermal_zone_device *, 94 enum thermal_device_mode *); 95 int (*set_mode) (struct thermal_zone_device *, 96 enum thermal_device_mode); 97 int (*get_trip_type) (struct thermal_zone_device *, int, 98 enum thermal_trip_type *); 99 int (*get_trip_temp) (struct thermal_zone_device *, int, 100 unsigned long *); 101 int (*set_trip_temp) (struct thermal_zone_device *, int, 102 unsigned long); 103 int (*get_trip_hyst) (struct thermal_zone_device *, int, 104 unsigned long *); 105 int (*set_trip_hyst) (struct thermal_zone_device *, int, 106 unsigned long); 107 int (*get_crit_temp) (struct thermal_zone_device *, unsigned long *); 108 int (*set_emul_temp) (struct thermal_zone_device *, unsigned long); 109 int (*get_trend) (struct thermal_zone_device *, int, 110 enum thermal_trend *); 111 int (*notify) (struct thermal_zone_device *, int, 112 enum thermal_trip_type); 113}; 114 115struct thermal_cooling_device_ops { 116 int (*get_max_state) (struct thermal_cooling_device *, unsigned long *); 117 int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *); 118 int (*set_cur_state) (struct thermal_cooling_device *, unsigned long); 119}; 120 121struct thermal_cooling_device { 122 int id; 123 char type[THERMAL_NAME_LENGTH]; 124 struct device device; 125 struct device_node *np; 126 void *devdata; 127 const struct thermal_cooling_device_ops *ops; 128 bool updated; /* true if the cooling device does not need update */ 129 struct mutex lock; /* protect thermal_instances list */ 130 struct list_head thermal_instances; 131 struct list_head node; 132}; 133 134struct thermal_attr { 135 struct device_attribute attr; 136 char name[THERMAL_NAME_LENGTH]; 137}; 138 139/** 140 * struct thermal_zone_device - structure for a thermal zone 141 * @id: unique id number for each thermal zone 142 * @type: the thermal zone device type 143 * @device: &struct device for this thermal zone 144 * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature 145 * @trip_type_attrs: attributes for trip points for sysfs: trip type 146 * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis 147 * @devdata: private pointer for device private data 148 * @trips: number of trip points the thermal zone supports 149 * @trips_disabled; bitmap for disabled trips 150 * @passive_delay: number of milliseconds to wait between polls when 151 * performing passive cooling. Currenty only used by the 152 * step-wise governor 153 * @polling_delay: number of milliseconds to wait between polls when 154 * checking whether trip points have been crossed (0 for 155 * interrupt driven systems) 156 * @temperature: current temperature. This is only for core code, 157 * drivers should use thermal_zone_get_temp() to get the 158 * current temperature 159 * @last_temperature: previous temperature read 160 * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION 161 * @passive: 1 if you've crossed a passive trip point, 0 otherwise. 162 * Currenty only used by the step-wise governor. 163 * @forced_passive: If > 0, temperature at which to switch on all ACPI 164 * processor cooling devices. Currently only used by the 165 * step-wise governor. 166 * @need_update: if equals 1, thermal_zone_device_update needs to be invoked. 167 * @ops: operations this &thermal_zone_device supports 168 * @tzp: thermal zone parameters 169 * @governor: pointer to the governor for this thermal zone 170 * @thermal_instances: list of &struct thermal_instance of this thermal zone 171 * @idr: &struct idr to generate unique id for this zone's cooling 172 * devices 173 * @lock: lock to protect thermal_instances list 174 * @node: node in thermal_tz_list (in thermal_core.c) 175 * @poll_queue: delayed work for polling 176 */ 177struct thermal_zone_device { 178 int id; 179 char type[THERMAL_NAME_LENGTH]; 180 struct device device; 181 struct thermal_attr *trip_temp_attrs; 182 struct thermal_attr *trip_type_attrs; 183 struct thermal_attr *trip_hyst_attrs; 184 void *devdata; 185 int trips; 186 unsigned long trips_disabled; /* bitmap for disabled trips */ 187 int passive_delay; 188 int polling_delay; 189 int temperature; 190 int last_temperature; 191 int emul_temperature; 192 int passive; 193 unsigned int forced_passive; 194 atomic_t need_update; 195 struct thermal_zone_device_ops *ops; 196 const struct thermal_zone_params *tzp; 197 struct thermal_governor *governor; 198 struct list_head thermal_instances; 199 struct idr idr; 200 struct mutex lock; 201 struct list_head node; 202 struct delayed_work poll_queue; 203}; 204 205/** 206 * struct thermal_governor - structure that holds thermal governor information 207 * @name: name of the governor 208 * @throttle: callback called for every trip point even if temperature is 209 * below the trip point temperature 210 * @governor_list: node in thermal_governor_list (in thermal_core.c) 211 */ 212struct thermal_governor { 213 char name[THERMAL_NAME_LENGTH]; 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. The shall be determined by platform 225 * characterization. This is on a 'percentage' scale. 226 * See Documentation/thermal/sysfs-api.txt for more information. 227 */ 228 int weight; 229 230 /* 231 * This is a bit mask that gives the binding relation between this 232 * thermal zone and cdev, for a particular trip point. 233 * See Documentation/thermal/sysfs-api.txt for more information. 234 */ 235 int trip_mask; 236 237 /* 238 * This is an array of cooling state limits. Must have exactly 239 * 2 * thermal_zone.number_of_trip_points. It is an array consisting 240 * of tuples <lower-state upper-state> of state limits. Each trip 241 * will be associated with one state limit tuple when binding. 242 * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS> 243 * on all trips. 244 */ 245 unsigned long *binding_limits; 246 int (*match) (struct thermal_zone_device *tz, 247 struct thermal_cooling_device *cdev); 248}; 249 250/* Structure to define Thermal Zone parameters */ 251struct thermal_zone_params { 252 char governor_name[THERMAL_NAME_LENGTH]; 253 254 /* 255 * a boolean to indicate if the thermal to hwmon sysfs interface 256 * is required. when no_hwmon == false, a hwmon sysfs interface 257 * will be created. when no_hwmon == true, nothing will be done 258 */ 259 bool no_hwmon; 260 261 int num_tbps; /* Number of tbp entries */ 262 struct thermal_bind_params *tbp; 263}; 264 265struct thermal_genl_event { 266 u32 orig; 267 enum events event; 268}; 269 270/** 271 * struct thermal_zone_of_device_ops - scallbacks for handling DT based zones 272 * 273 * Mandatory: 274 * @get_temp: a pointer to a function that reads the sensor temperature. 275 * 276 * Optional: 277 * @get_trend: a pointer to a function that reads the sensor temperature trend. 278 * @set_emul_temp: a pointer to a function that sets sensor emulated 279 * temperature. 280 */ 281struct thermal_zone_of_device_ops { 282 int (*get_temp)(void *, long *); 283 int (*get_trend)(void *, long *); 284 int (*set_emul_temp)(void *, unsigned long); 285}; 286 287/** 288 * struct thermal_trip - representation of a point in temperature domain 289 * @np: pointer to struct device_node that this trip point was created from 290 * @temperature: temperature value in miliCelsius 291 * @hysteresis: relative hysteresis in miliCelsius 292 * @type: trip point type 293 */ 294 295struct thermal_trip { 296 struct device_node *np; 297 unsigned long int temperature; 298 unsigned long int hysteresis; 299 enum thermal_trip_type type; 300}; 301 302/* Function declarations */ 303#ifdef CONFIG_THERMAL_OF 304struct thermal_zone_device * 305thermal_zone_of_sensor_register(struct device *dev, int id, void *data, 306 const struct thermal_zone_of_device_ops *ops); 307void thermal_zone_of_sensor_unregister(struct device *dev, 308 struct thermal_zone_device *tz); 309#else 310static inline struct thermal_zone_device * 311thermal_zone_of_sensor_register(struct device *dev, int id, void *data, 312 const struct thermal_zone_of_device_ops *ops) 313{ 314 return NULL; 315} 316 317static inline 318void thermal_zone_of_sensor_unregister(struct device *dev, 319 struct thermal_zone_device *tz) 320{ 321} 322 323#endif 324 325#if IS_ENABLED(CONFIG_THERMAL) 326struct thermal_zone_device *thermal_zone_device_register(const char *, int, int, 327 void *, struct thermal_zone_device_ops *, 328 const struct thermal_zone_params *, int, int); 329void thermal_zone_device_unregister(struct thermal_zone_device *); 330 331int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int, 332 struct thermal_cooling_device *, 333 unsigned long, unsigned long); 334int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int, 335 struct thermal_cooling_device *); 336void thermal_zone_device_update(struct thermal_zone_device *); 337 338struct thermal_cooling_device *thermal_cooling_device_register(char *, void *, 339 const struct thermal_cooling_device_ops *); 340struct thermal_cooling_device * 341thermal_of_cooling_device_register(struct device_node *np, char *, void *, 342 const struct thermal_cooling_device_ops *); 343void thermal_cooling_device_unregister(struct thermal_cooling_device *); 344struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name); 345int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp); 346 347int get_tz_trend(struct thermal_zone_device *, int); 348struct thermal_instance *get_thermal_instance(struct thermal_zone_device *, 349 struct thermal_cooling_device *, int); 350void thermal_cdev_update(struct thermal_cooling_device *); 351void thermal_notify_framework(struct thermal_zone_device *, int); 352#else 353static inline struct thermal_zone_device *thermal_zone_device_register( 354 const char *type, int trips, int mask, void *devdata, 355 struct thermal_zone_device_ops *ops, 356 const struct thermal_zone_params *tzp, 357 int passive_delay, int polling_delay) 358{ return ERR_PTR(-ENODEV); } 359static inline void thermal_zone_device_unregister( 360 struct thermal_zone_device *tz) 361{ } 362static inline int thermal_zone_bind_cooling_device( 363 struct thermal_zone_device *tz, int trip, 364 struct thermal_cooling_device *cdev, 365 unsigned long upper, unsigned long lower) 366{ return -ENODEV; } 367static inline int thermal_zone_unbind_cooling_device( 368 struct thermal_zone_device *tz, int trip, 369 struct thermal_cooling_device *cdev) 370{ return -ENODEV; } 371static inline void thermal_zone_device_update(struct thermal_zone_device *tz) 372{ } 373static inline struct thermal_cooling_device * 374thermal_cooling_device_register(char *type, void *devdata, 375 const struct thermal_cooling_device_ops *ops) 376{ return ERR_PTR(-ENODEV); } 377static inline struct thermal_cooling_device * 378thermal_of_cooling_device_register(struct device_node *np, 379 char *type, void *devdata, const struct thermal_cooling_device_ops *ops) 380{ return ERR_PTR(-ENODEV); } 381static inline void thermal_cooling_device_unregister( 382 struct thermal_cooling_device *cdev) 383{ } 384static inline struct thermal_zone_device *thermal_zone_get_zone_by_name( 385 const char *name) 386{ return ERR_PTR(-ENODEV); } 387static inline int thermal_zone_get_temp( 388 struct thermal_zone_device *tz, unsigned long *temp) 389{ return -ENODEV; } 390static inline int get_tz_trend(struct thermal_zone_device *tz, int trip) 391{ return -ENODEV; } 392static inline struct thermal_instance * 393get_thermal_instance(struct thermal_zone_device *tz, 394 struct thermal_cooling_device *cdev, int trip) 395{ return ERR_PTR(-ENODEV); } 396static inline void thermal_cdev_update(struct thermal_cooling_device *cdev) 397{ } 398static inline void thermal_notify_framework(struct thermal_zone_device *tz, 399 int trip) 400{ } 401#endif /* CONFIG_THERMAL */ 402 403#if defined(CONFIG_NET) && IS_ENABLED(CONFIG_THERMAL) 404extern int thermal_generate_netlink_event(struct thermal_zone_device *tz, 405 enum events event); 406#else 407static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz, 408 enum events event) 409{ 410 return 0; 411} 412#endif 413 414#endif /* __THERMAL_H__ */ 415