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