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