thermal.h 17 KB

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