thermal.h 17 KB

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