1 /* SPDX-License-Identifier: GPL-2.0 */
5 * Copyright (C) 2012 Intel Corp
6 * Author: Durgadoss R <durgadoss.r@intel.com>
9 #ifndef __THERMAL_CORE_H__
10 #define __THERMAL_CORE_H__
12 #include <linux/device.h>
13 #include <linux/thermal.h>
15 #include "thermal_netlink.h"
16 #include "thermal_debugfs.h"
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"
31 /* Initial state of a cooling device during binding */
32 #define THERMAL_NO_TARGET -1UL
34 /* Init section thermal table */
35 extern struct thermal_governor *__governor_thermal_table[];
36 extern struct thermal_governor *__governor_thermal_table_end[];
38 #define THERMAL_TABLE_ENTRY(table, name) \
39 static typeof(name) *__thermal_table_entry_##name \
40 __used __section("__" #table "_thermal_table") = &name
42 #define THERMAL_GOVERNOR_DECLARE(name) THERMAL_TABLE_ENTRY(governor, name)
44 #define for_each_governor_table(__governor) \
45 for (__governor = __governor_thermal_table; \
46 __governor < __governor_thermal_table_end; \
49 int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
52 int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
55 int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
56 void *thermal_governor);
58 struct thermal_zone_device *thermal_zone_get_by_id(int id);
61 struct device_attribute attr;
62 char name[THERMAL_NAME_LENGTH];
65 static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
67 return cdev->ops->get_requested_power && cdev->ops->state2power &&
68 cdev->ops->power2state;
71 void thermal_cdev_update(struct thermal_cooling_device *);
72 void __thermal_cdev_update(struct thermal_cooling_device *cdev);
74 int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip);
76 struct thermal_instance *
77 get_thermal_instance(struct thermal_zone_device *tz,
78 struct thermal_cooling_device *cdev,
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
86 struct thermal_instance {
88 char name[THERMAL_NAME_LENGTH];
89 struct thermal_zone_device *tz;
90 struct thermal_cooling_device *cdev;
91 const struct thermal_trip *trip;
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 */
106 #define to_thermal_zone(_dev) \
107 container_of(_dev, struct thermal_zone_device, device)
109 #define to_cooling_device(_dev) \
110 container_of(_dev, struct thermal_cooling_device, device)
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);
123 #define for_each_trip(__tz, __trip) \
124 for (__trip = __tz->trips; __trip - __tz->trips < __tz->num_trips; __trip++)
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);
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 *,
145 #ifdef CONFIG_THERMAL_STATISTICS
146 void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
147 unsigned long new_state);
150 thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
151 unsigned long new_state) {}
152 #endif /* CONFIG_THERMAL_STATISTICS */
154 /* device tree support */
155 int thermal_zone_device_is_enabled(struct thermal_zone_device *tz);
157 #endif /* __THERMAL_CORE_H__ */