thermal.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #define THERMAL_TRIPS_NONE -1
  31. #define THERMAL_MAX_TRIPS 12
  32. #define THERMAL_NAME_LENGTH 20
  33. /* invalid cooling state */
  34. #define THERMAL_CSTATE_INVALID -1UL
  35. /* No upper/lower limit requirement */
  36. #define THERMAL_NO_LIMIT THERMAL_CSTATE_INVALID
  37. /* Unit conversion macros */
  38. #define KELVIN_TO_CELSIUS(t) (long)(((long)t-2732 >= 0) ? \
  39. ((long)t-2732+5)/10 : ((long)t-2732-5)/10)
  40. #define CELSIUS_TO_KELVIN(t) ((t)*10+2732)
  41. #define DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, off) (((t) - (off)) * 100)
  42. #define DECI_KELVIN_TO_MILLICELSIUS(t) DECI_KELVIN_TO_MILLICELSIUS_WITH_OFFSET(t, 2732)
  43. #define MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, off) (((t) / 100) + (off))
  44. #define MILLICELSIUS_TO_DECI_KELVIN(t) MILLICELSIUS_TO_DECI_KELVIN_WITH_OFFSET(t, 2732)
  45. /* Adding event notification support elements */
  46. #define THERMAL_GENL_FAMILY_NAME "thermal_event"
  47. #define THERMAL_GENL_VERSION 0x01
  48. #define THERMAL_GENL_MCAST_GROUP_NAME "thermal_mc_grp"
  49. /* Default Thermal Governor */
  50. #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
  51. #define DEFAULT_THERMAL_GOVERNOR "step_wise"
  52. #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
  53. #define DEFAULT_THERMAL_GOVERNOR "fair_share"
  54. #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
  55. #define DEFAULT_THERMAL_GOVERNOR "user_space"
  56. #elif defined(CONFIG_THERMAL_DEFAULT_GOV_BACKWARD_COMPATIBLE)
  57. #define DEFAULT_THERMAL_GOVERNOR "backward_compatible"
  58. #endif
  59. struct thermal_zone_device;
  60. struct thermal_cooling_device;
  61. enum thermal_device_mode {
  62. THERMAL_DEVICE_DISABLED = 0,
  63. THERMAL_DEVICE_ENABLED,
  64. };
  65. enum thermal_trip_type {
  66. THERMAL_TRIP_ACTIVE = 0,
  67. THERMAL_TRIP_PASSIVE,
  68. THERMAL_TRIP_HOT,
  69. THERMAL_TRIP_CRITICAL,
  70. };
  71. enum thermal_trend {
  72. THERMAL_TREND_STABLE, /* temperature is stable */
  73. THERMAL_TREND_RAISING, /* temperature is raising */
  74. THERMAL_TREND_DROPPING, /* temperature is dropping */
  75. THERMAL_TREND_RAISE_FULL, /* apply highest cooling action */
  76. THERMAL_TREND_DROP_FULL, /* apply lowest cooling action */
  77. };
  78. /* Events supported by Thermal Netlink */
  79. enum events {
  80. THERMAL_AUX0,
  81. THERMAL_AUX1,
  82. THERMAL_CRITICAL,
  83. THERMAL_DEV_FAULT,
  84. };
  85. /* attributes of thermal_genl_family */
  86. enum {
  87. THERMAL_GENL_ATTR_UNSPEC,
  88. THERMAL_GENL_ATTR_EVENT,
  89. __THERMAL_GENL_ATTR_MAX,
  90. };
  91. #define THERMAL_GENL_ATTR_MAX (__THERMAL_GENL_ATTR_MAX - 1)
  92. /* commands supported by the thermal_genl_family */
  93. enum {
  94. THERMAL_GENL_CMD_UNSPEC,
  95. THERMAL_GENL_CMD_EVENT,
  96. __THERMAL_GENL_CMD_MAX,
  97. };
  98. #define THERMAL_GENL_CMD_MAX (__THERMAL_GENL_CMD_MAX - 1)
  99. struct thermal_zone_device_ops {
  100. int (*bind) (struct thermal_zone_device *,
  101. struct thermal_cooling_device *);
  102. int (*unbind) (struct thermal_zone_device *,
  103. struct thermal_cooling_device *);
  104. int (*get_temp) (struct thermal_zone_device *, unsigned long *);
  105. int (*get_mode) (struct thermal_zone_device *,
  106. enum thermal_device_mode *);
  107. int (*set_mode) (struct thermal_zone_device *,
  108. enum thermal_device_mode);
  109. int (*get_trip_type) (struct thermal_zone_device *, int,
  110. enum thermal_trip_type *);
  111. int (*get_trip_temp) (struct thermal_zone_device *, int,
  112. unsigned long *);
  113. int (*set_trip_temp) (struct thermal_zone_device *, int,
  114. unsigned long);
  115. int (*get_trip_hyst) (struct thermal_zone_device *, int,
  116. unsigned long *);
  117. int (*set_trip_hyst) (struct thermal_zone_device *, int,
  118. unsigned long);
  119. int (*get_crit_temp) (struct thermal_zone_device *, unsigned long *);
  120. int (*set_emul_temp) (struct thermal_zone_device *, unsigned long);
  121. int (*get_trend) (struct thermal_zone_device *, int,
  122. enum thermal_trend *);
  123. int (*notify) (struct thermal_zone_device *, int,
  124. enum thermal_trip_type);
  125. };
  126. struct thermal_cooling_device_ops {
  127. int (*get_max_state) (struct thermal_cooling_device *, unsigned long *);
  128. int (*get_cur_state) (struct thermal_cooling_device *, unsigned long *);
  129. int (*set_cur_state) (struct thermal_cooling_device *, unsigned long);
  130. };
  131. struct thermal_cooling_device {
  132. int id;
  133. char type[THERMAL_NAME_LENGTH];
  134. struct device device;
  135. struct device_node *np;
  136. void *devdata;
  137. const struct thermal_cooling_device_ops *ops;
  138. bool updated; /* true if the cooling device does not need update */
  139. struct mutex lock; /* protect thermal_instances list */
  140. struct list_head thermal_instances;
  141. struct list_head node;
  142. };
  143. struct thermal_attr {
  144. struct device_attribute attr;
  145. char name[THERMAL_NAME_LENGTH];
  146. };
  147. /**
  148. * struct thermal_zone_device - structure for a thermal zone
  149. * @id: unique id number for each thermal zone
  150. * @type: the thermal zone device type
  151. * @device: &struct device for this thermal zone
  152. * @trip_temp_attrs: attributes for trip points for sysfs: trip temperature
  153. * @trip_type_attrs: attributes for trip points for sysfs: trip type
  154. * @trip_hyst_attrs: attributes for trip points for sysfs: trip hysteresis
  155. * @devdata: private pointer for device private data
  156. * @trips: number of trip points the thermal zone supports
  157. * @passive_delay: number of milliseconds to wait between polls when
  158. * performing passive cooling. Currenty only used by the
  159. * step-wise governor
  160. * @polling_delay: number of milliseconds to wait between polls when
  161. * checking whether trip points have been crossed (0 for
  162. * interrupt driven systems)
  163. * @temperature: current temperature. This is only for core code,
  164. * drivers should use thermal_zone_get_temp() to get the
  165. * current temperature
  166. * @last_temperature: previous temperature read
  167. * @emul_temperature: emulated temperature when using CONFIG_THERMAL_EMULATION
  168. * @passive: 1 if you've crossed a passive trip point, 0 otherwise.
  169. * Currenty only used by the step-wise governor.
  170. * @forced_passive: If > 0, temperature at which to switch on all ACPI
  171. * processor cooling devices. Currently only used by the
  172. * step-wise governor.
  173. * @ops: operations this &thermal_zone_device supports
  174. * @tzp: thermal zone parameters
  175. * @governor: pointer to the governor for this thermal zone
  176. * @thermal_instances: list of &struct thermal_instance of this thermal zone
  177. * @idr: &struct idr to generate unique id for this zone's cooling
  178. * devices
  179. * @lock: lock to protect thermal_instances list
  180. * @node: node in thermal_tz_list (in thermal_core.c)
  181. * @poll_queue: delayed work for polling
  182. */
  183. struct thermal_zone_device {
  184. int id;
  185. char type[THERMAL_NAME_LENGTH];
  186. struct device device;
  187. struct thermal_attr *trip_temp_attrs;
  188. struct thermal_attr *trip_type_attrs;
  189. struct thermal_attr *trip_hyst_attrs;
  190. void *devdata;
  191. int trips;
  192. int passive_delay;
  193. int polling_delay;
  194. int temperature;
  195. int last_temperature;
  196. int emul_temperature;
  197. int passive;
  198. unsigned int forced_passive;
  199. struct thermal_zone_device_ops *ops;
  200. const struct thermal_zone_params *tzp;
  201. struct thermal_governor *governor;
  202. struct list_head thermal_instances;
  203. struct idr idr;
  204. struct mutex lock;
  205. struct list_head node;
  206. struct delayed_work poll_queue;
  207. };
  208. /**
  209. * struct thermal_governor - structure that holds thermal governor information
  210. * @name: name of the governor
  211. * @throttle: callback called for every trip point even if temperature is
  212. * below the trip point temperature
  213. * @governor_list: node in thermal_governor_list (in thermal_core.c)
  214. */
  215. struct thermal_governor {
  216. char name[THERMAL_NAME_LENGTH];
  217. int (*throttle)(struct thermal_zone_device *tz, int trip);
  218. struct list_head governor_list;
  219. };
  220. /* Structure that holds binding parameters for a zone */
  221. struct thermal_bind_params {
  222. struct thermal_cooling_device *cdev;
  223. /*
  224. * This is a measure of 'how effectively these devices can
  225. * cool 'this' thermal zone. The shall be determined by platform
  226. * characterization. This is on a 'percentage' scale.
  227. * See Documentation/thermal/sysfs-api.txt for more information.
  228. */
  229. int weight;
  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. * This is an array of cooling state limits. Must have exactly
  238. * 2 * thermal_zone.number_of_trip_points. It is an array consisting
  239. * of tuples <lower-state upper-state> of state limits. Each trip
  240. * will be associated with one state limit tuple when binding.
  241. * A NULL pointer means <THERMAL_NO_LIMITS THERMAL_NO_LIMITS>
  242. * on all trips.
  243. */
  244. unsigned long *binding_limits;
  245. int (*match) (struct thermal_zone_device *tz,
  246. struct thermal_cooling_device *cdev);
  247. };
  248. /* Structure to define Thermal Zone parameters */
  249. struct thermal_zone_params {
  250. char governor_name[THERMAL_NAME_LENGTH];
  251. /*
  252. * a boolean to indicate if the thermal to hwmon sysfs interface
  253. * is required. when no_hwmon == false, a hwmon sysfs interface
  254. * will be created. when no_hwmon == true, nothing will be done
  255. */
  256. bool no_hwmon;
  257. int num_tbps; /* Number of tbp entries */
  258. struct thermal_bind_params *tbp;
  259. };
  260. struct thermal_genl_event {
  261. u32 orig;
  262. enum events event;
  263. };
  264. /* Function declarations */
  265. #ifdef CONFIG_THERMAL_OF
  266. struct thermal_zone_device *
  267. thermal_zone_of_sensor_register(struct device *dev, int id,
  268. void *data, int (*get_temp)(void *, long *),
  269. int (*get_trend)(void *, long *));
  270. void thermal_zone_of_sensor_unregister(struct device *dev,
  271. struct thermal_zone_device *tz);
  272. #else
  273. static inline struct thermal_zone_device *
  274. thermal_zone_of_sensor_register(struct device *dev, int id,
  275. void *data, int (*get_temp)(void *, long *),
  276. int (*get_trend)(void *, long *))
  277. {
  278. return NULL;
  279. }
  280. static inline
  281. void thermal_zone_of_sensor_unregister(struct device *dev,
  282. struct thermal_zone_device *tz)
  283. {
  284. }
  285. #endif
  286. struct thermal_zone_device *thermal_zone_device_register(const char *, int, int,
  287. void *, struct thermal_zone_device_ops *,
  288. const struct thermal_zone_params *, int, int);
  289. void thermal_zone_device_unregister(struct thermal_zone_device *);
  290. int thermal_zone_bind_cooling_device(struct thermal_zone_device *, int,
  291. struct thermal_cooling_device *,
  292. unsigned long, unsigned long);
  293. int thermal_zone_unbind_cooling_device(struct thermal_zone_device *, int,
  294. struct thermal_cooling_device *);
  295. void thermal_zone_device_update(struct thermal_zone_device *);
  296. struct thermal_cooling_device *thermal_cooling_device_register(char *, void *,
  297. const struct thermal_cooling_device_ops *);
  298. struct thermal_cooling_device *
  299. thermal_of_cooling_device_register(struct device_node *np, char *, void *,
  300. const struct thermal_cooling_device_ops *);
  301. void thermal_cooling_device_unregister(struct thermal_cooling_device *);
  302. struct thermal_zone_device *thermal_zone_get_zone_by_name(const char *name);
  303. int thermal_zone_get_temp(struct thermal_zone_device *tz, unsigned long *temp);
  304. int get_tz_trend(struct thermal_zone_device *, int);
  305. struct thermal_instance *get_thermal_instance(struct thermal_zone_device *,
  306. struct thermal_cooling_device *, int);
  307. void thermal_cdev_update(struct thermal_cooling_device *);
  308. void thermal_notify_framework(struct thermal_zone_device *, int);
  309. #ifdef CONFIG_NET
  310. extern int thermal_generate_netlink_event(struct thermal_zone_device *tz,
  311. enum events event);
  312. #else
  313. static inline int thermal_generate_netlink_event(struct thermal_zone_device *tz,
  314. enum events event)
  315. {
  316. return 0;
  317. }
  318. #endif
  319. #endif /* __THERMAL_H__ */