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