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 /* Default Thermal Governor */
16 #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
17 #define DEFAULT_THERMAL_GOVERNOR       "step_wise"
18 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
19 #define DEFAULT_THERMAL_GOVERNOR       "fair_share"
20 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
21 #define DEFAULT_THERMAL_GOVERNOR       "user_space"
22 #elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
23 #define DEFAULT_THERMAL_GOVERNOR       "power_allocator"
24 #endif
25 
26 /* Initial state of a cooling device during binding */
27 #define THERMAL_NO_TARGET -1UL
28 
29 /* Init section thermal table */
30 extern struct thermal_governor *__governor_thermal_table[];
31 extern struct thermal_governor *__governor_thermal_table_end[];
32 
33 #define THERMAL_TABLE_ENTRY(table, name)			\
34 	static typeof(name) *__thermal_table_entry_##name	\
35 	__used __section(__##table##_thermal_table) = &name
36 
37 #define THERMAL_GOVERNOR_DECLARE(name)	THERMAL_TABLE_ENTRY(governor, name)
38 
39 #define for_each_governor_table(__governor)		\
40 	for (__governor = __governor_thermal_table;	\
41 	     __governor < __governor_thermal_table_end;	\
42 	     __governor++)
43 
44 struct thermal_attr {
45 	struct device_attribute attr;
46 	char name[THERMAL_NAME_LENGTH];
47 };
48 
49 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
50 {
51 	return cdev->ops->get_requested_power && cdev->ops->state2power &&
52 		cdev->ops->power2state;
53 }
54 
55 int power_actor_get_max_power(struct thermal_cooling_device *cdev,
56 			      struct thermal_zone_device *tz, u32 *max_power);
57 int power_actor_get_min_power(struct thermal_cooling_device *cdev,
58 			      struct thermal_zone_device *tz, u32 *min_power);
59 int power_actor_set_power(struct thermal_cooling_device *cdev,
60 			  struct thermal_instance *ti, u32 power);
61 /**
62  * struct thermal_trip - representation of a point in temperature domain
63  * @np: pointer to struct device_node that this trip point was created from
64  * @temperature: temperature value in miliCelsius
65  * @hysteresis: relative hysteresis in miliCelsius
66  * @type: trip point type
67  */
68 struct thermal_trip {
69 	struct device_node *np;
70 	int temperature;
71 	int hysteresis;
72 	enum thermal_trip_type type;
73 };
74 
75 int get_tz_trend(struct thermal_zone_device *tz, int trip);
76 
77 struct thermal_instance *
78 get_thermal_instance(struct thermal_zone_device *tz,
79 		     struct thermal_cooling_device *cdev,
80 		     int trip);
81 
82 /*
83  * This structure is used to describe the behavior of
84  * a certain cooling device on a certain trip point
85  * in a certain thermal zone
86  */
87 struct thermal_instance {
88 	int id;
89 	char name[THERMAL_NAME_LENGTH];
90 	struct thermal_zone_device *tz;
91 	struct thermal_cooling_device *cdev;
92 	int trip;
93 	bool initialized;
94 	unsigned long upper;	/* Highest cooling state for this trip point */
95 	unsigned long lower;	/* Lowest cooling state for this trip point */
96 	unsigned long target;	/* expected cooling state */
97 	char attr_name[THERMAL_NAME_LENGTH];
98 	struct device_attribute attr;
99 	char weight_attr_name[THERMAL_NAME_LENGTH];
100 	struct device_attribute weight_attr;
101 	struct list_head tz_node; /* node in tz->thermal_instances */
102 	struct list_head cdev_node; /* node in cdev->thermal_instances */
103 	unsigned int weight; /* The weight of the cooling device */
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 void thermal_zone_device_rebind_exception(struct thermal_zone_device *,
115 					  const char *, size_t);
116 void thermal_zone_device_unbind_exception(struct thermal_zone_device *,
117 					  const char *, size_t);
118 int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
119 int thermal_build_list_of_policies(char *buf);
120 
121 /* Helpers */
122 void thermal_zone_set_trips(struct thermal_zone_device *tz);
123 
124 /* sysfs I/F */
125 int thermal_zone_create_device_groups(struct thermal_zone_device *, int);
126 void thermal_zone_destroy_device_groups(struct thermal_zone_device *);
127 void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
128 void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);
129 /* used only at binding time */
130 ssize_t trip_point_show(struct device *, struct device_attribute *, char *);
131 ssize_t weight_show(struct device *, struct device_attribute *, char *);
132 ssize_t weight_store(struct device *, struct device_attribute *, const char *,
133 		     size_t);
134 
135 #ifdef CONFIG_THERMAL_STATISTICS
136 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
137 					 unsigned long new_state);
138 #else
139 static inline void
140 thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
141 				    unsigned long new_state) {}
142 #endif /* CONFIG_THERMAL_STATISTICS */
143 
144 /* device tree support */
145 #ifdef CONFIG_THERMAL_OF
146 int of_parse_thermal_zones(void);
147 int of_thermal_get_ntrips(struct thermal_zone_device *);
148 bool of_thermal_is_trip_valid(struct thermal_zone_device *, int);
149 const struct thermal_trip *
150 of_thermal_get_trip_points(struct thermal_zone_device *);
151 #else
152 static inline int of_parse_thermal_zones(void) { return 0; }
153 static inline int of_thermal_get_ntrips(struct thermal_zone_device *tz)
154 {
155 	return 0;
156 }
157 static inline bool of_thermal_is_trip_valid(struct thermal_zone_device *tz,
158 					    int trip)
159 {
160 	return false;
161 }
162 static inline const struct thermal_trip *
163 of_thermal_get_trip_points(struct thermal_zone_device *tz)
164 {
165 	return NULL;
166 }
167 #endif
168 
169 #endif /* __THERMAL_CORE_H__ */
170