xref: /linux/drivers/thermal/thermal_core.h (revision e6a901a00822659181c93c86d8bbc2a17779fddc)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  *  thermal_core.h
4  *
5  *  Copyright (C) 2012  Intel Corp
6  *  Author: Durgadoss R <durgadoss.r@intel.com>
7  */
8 
9 #ifndef __THERMAL_CORE_H__
10 #define __THERMAL_CORE_H__
11 
12 #include <linux/device.h>
13 #include <linux/thermal.h>
14 
15 #include "thermal_netlink.h"
16 #include "thermal_debugfs.h"
17 
18 /* Default Thermal Governor */
19 #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
20 #define DEFAULT_THERMAL_GOVERNOR       "step_wise"
21 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
22 #define DEFAULT_THERMAL_GOVERNOR       "fair_share"
23 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
24 #define DEFAULT_THERMAL_GOVERNOR       "user_space"
25 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
26 #define DEFAULT_THERMAL_GOVERNOR       "power_allocator"
27 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_BANG_BANG)
28 #define DEFAULT_THERMAL_GOVERNOR       "bang_bang"
29 #endif
30 
31 /* Initial state of a cooling device during binding */
32 #define THERMAL_NO_TARGET -1UL
33 
34 /* Init section thermal table */
35 extern struct thermal_governor *__governor_thermal_table[];
36 extern struct thermal_governor *__governor_thermal_table_end[];
37 
38 #define THERMAL_TABLE_ENTRY(table, name)			\
39 	static typeof(name) *__thermal_table_entry_##name	\
40 	__used __section("__" #table "_thermal_table") = &name
41 
42 #define THERMAL_GOVERNOR_DECLARE(name)	THERMAL_TABLE_ENTRY(governor, name)
43 
44 #define for_each_governor_table(__governor)		\
45 	for (__governor = __governor_thermal_table;	\
46 	     __governor < __governor_thermal_table_end;	\
47 	     __governor++)
48 
49 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
50 			  void *);
51 
52 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
53 					      void *), void *);
54 
55 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
56 			      void *thermal_governor);
57 
58 struct thermal_zone_device *thermal_zone_get_by_id(int id);
59 
60 struct thermal_attr {
61 	struct device_attribute attr;
62 	char name[THERMAL_NAME_LENGTH];
63 };
64 
65 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
66 {
67 	return cdev->ops->get_requested_power && cdev->ops->state2power &&
68 		cdev->ops->power2state;
69 }
70 
71 void thermal_cdev_update(struct thermal_cooling_device *);
72 void __thermal_cdev_update(struct thermal_cooling_device *cdev);
73 
74 int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip);
75 
76 struct thermal_instance *
77 get_thermal_instance(struct thermal_zone_device *tz,
78 		     struct thermal_cooling_device *cdev,
79 		     int trip);
80 
81 /*
82  * This structure is used to describe the behavior of
83  * a certain cooling device on a certain trip point
84  * in a certain thermal zone
85  */
86 struct thermal_instance {
87 	int id;
88 	char name[THERMAL_NAME_LENGTH];
89 	struct thermal_zone_device *tz;
90 	struct thermal_cooling_device *cdev;
91 	const struct thermal_trip *trip;
92 	bool initialized;
93 	unsigned long upper;	/* Highest cooling state for this trip point */
94 	unsigned long lower;	/* Lowest cooling state for this trip point */
95 	unsigned long target;	/* expected cooling state */
96 	char attr_name[THERMAL_NAME_LENGTH];
97 	struct device_attribute attr;
98 	char weight_attr_name[THERMAL_NAME_LENGTH];
99 	struct device_attribute weight_attr;
100 	struct list_head tz_node; /* node in tz->thermal_instances */
101 	struct list_head cdev_node; /* node in cdev->thermal_instances */
102 	unsigned int weight; /* The weight of the cooling device */
103 	bool upper_no_limit;
104 };
105 
106 #define to_thermal_zone(_dev) \
107 	container_of(_dev, struct thermal_zone_device, device)
108 
109 #define to_cooling_device(_dev)	\
110 	container_of(_dev, struct thermal_cooling_device, device)
111 
112 int thermal_register_governor(struct thermal_governor *);
113 void thermal_unregister_governor(struct thermal_governor *);
114 int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
115 int thermal_build_list_of_policies(char *buf);
116 void __thermal_zone_device_update(struct thermal_zone_device *tz,
117 				  enum thermal_notify_event event);
118 void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz);
119 void thermal_governor_update_tz(struct thermal_zone_device *tz,
120 				enum thermal_notify_event reason);
121 
122 /* Helpers */
123 #define for_each_trip(__tz, __trip)	\
124 	for (__trip = __tz->trips; __trip - __tz->trips < __tz->num_trips; __trip++)
125 
126 void __thermal_zone_set_trips(struct thermal_zone_device *tz);
127 int thermal_zone_trip_id(const struct thermal_zone_device *tz,
128 			 const struct thermal_trip *trip);
129 void thermal_zone_trip_updated(struct thermal_zone_device *tz,
130 			       const struct thermal_trip *trip);
131 int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
132 
133 /* sysfs I/F */
134 int thermal_zone_create_device_groups(struct thermal_zone_device *tz);
135 void thermal_zone_destroy_device_groups(struct thermal_zone_device *);
136 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
137 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);
138 void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev);
139 /* used only at binding time */
140 ssize_t trip_point_show(struct device *, struct device_attribute *, char *);
141 ssize_t weight_show(struct device *, struct device_attribute *, char *);
142 ssize_t weight_store(struct device *, struct device_attribute *, const char *,
143 		     size_t);
144 
145 #ifdef CONFIG_THERMAL_STATISTICS
146 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
147 					 unsigned long new_state);
148 #else
149 static inline void
150 thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
151 				    unsigned long new_state) {}
152 #endif /* CONFIG_THERMAL_STATISTICS */
153 
154 /* device tree support */
155 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz);
156 
157 #endif /* __THERMAL_CORE_H__ */
158