thermal.h 16 KB

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