Loading...
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#include "thermal_debugfs.h"
17
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"
29#endif
30
31/* Initial state of a cooling device during binding */
32#define THERMAL_NO_TARGET -1UL
33
34/* Init section thermal table */
35extern struct thermal_governor *__governor_thermal_table[];
36extern struct thermal_governor *__governor_thermal_table_end[];
37
38#define THERMAL_TABLE_ENTRY(table, name) \
39 static typeof(name) *__thermal_table_entry_##name \
40 __used __section("__" #table "_thermal_table") = &name
41
42#define THERMAL_GOVERNOR_DECLARE(name) THERMAL_TABLE_ENTRY(governor, name)
43
44#define for_each_governor_table(__governor) \
45 for (__governor = __governor_thermal_table; \
46 __governor < __governor_thermal_table_end; \
47 __governor++)
48
49int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
50 void *);
51
52int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
53 void *), void *);
54
55int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
56 void *thermal_governor);
57
58struct thermal_zone_device *thermal_zone_get_by_id(int id);
59
60struct thermal_attr {
61 struct device_attribute attr;
62 char name[THERMAL_NAME_LENGTH];
63};
64
65static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
66{
67 return cdev->ops->get_requested_power && cdev->ops->state2power &&
68 cdev->ops->power2state;
69}
70
71void thermal_cdev_update(struct thermal_cooling_device *);
72void __thermal_cdev_update(struct thermal_cooling_device *cdev);
73
74int get_tz_trend(struct thermal_zone_device *tz, const struct thermal_trip *trip);
75
76struct thermal_instance *
77get_thermal_instance(struct thermal_zone_device *tz,
78 struct thermal_cooling_device *cdev,
79 int trip);
80
81/*
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
85 */
86struct thermal_instance {
87 int id;
88 char name[THERMAL_NAME_LENGTH];
89 struct thermal_zone_device *tz;
90 struct thermal_cooling_device *cdev;
91 const struct thermal_trip *trip;
92 bool initialized;
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 */
103 bool upper_no_limit;
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
112int thermal_register_governor(struct thermal_governor *);
113void thermal_unregister_governor(struct thermal_governor *);
114int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
115int thermal_build_list_of_policies(char *buf);
116void __thermal_zone_device_update(struct thermal_zone_device *tz,
117 enum thermal_notify_event event);
118void thermal_zone_device_critical_reboot(struct thermal_zone_device *tz);
119void thermal_governor_update_tz(struct thermal_zone_device *tz,
120 enum thermal_notify_event reason);
121
122/* Helpers */
123#define for_each_trip(__tz, __trip) \
124 for (__trip = __tz->trips; __trip - __tz->trips < __tz->num_trips; __trip++)
125
126void __thermal_zone_set_trips(struct thermal_zone_device *tz);
127int thermal_zone_trip_id(const struct thermal_zone_device *tz,
128 const struct thermal_trip *trip);
129void thermal_zone_trip_updated(struct thermal_zone_device *tz,
130 const struct thermal_trip *trip);
131int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
132
133/* sysfs I/F */
134int thermal_zone_create_device_groups(struct thermal_zone_device *, int);
135void thermal_zone_destroy_device_groups(struct thermal_zone_device *);
136void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
137void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);
138void thermal_cooling_device_stats_reinit(struct thermal_cooling_device *cdev);
139/* used only at binding time */
140ssize_t trip_point_show(struct device *, struct device_attribute *, char *);
141ssize_t weight_show(struct device *, struct device_attribute *, char *);
142ssize_t weight_store(struct device *, struct device_attribute *, const char *,
143 size_t);
144
145#ifdef CONFIG_THERMAL_STATISTICS
146void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
147 unsigned long new_state);
148#else
149static inline void
150thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
151 unsigned long new_state) {}
152#endif /* CONFIG_THERMAL_STATISTICS */
153
154/* device tree support */
155int thermal_zone_device_is_enabled(struct thermal_zone_device *tz);
156
157#endif /* __THERMAL_CORE_H__ */
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 */
32extern struct thermal_governor *__governor_thermal_table[];
33extern 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
46int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
47 void *);
48
49int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
50 void *), void *);
51
52int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
53 void *thermal_governor);
54
55struct thermal_zone_device *thermal_zone_get_by_id(int id);
56
57struct thermal_attr {
58 struct device_attribute attr;
59 char name[THERMAL_NAME_LENGTH];
60};
61
62static 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
68void thermal_cdev_update(struct thermal_cooling_device *);
69void __thermal_cdev_update(struct thermal_cooling_device *cdev);
70
71int get_tz_trend(struct thermal_zone_device *tz, int trip);
72
73struct thermal_instance *
74get_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 */
83struct 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
108int thermal_register_governor(struct thermal_governor *);
109void thermal_unregister_governor(struct thermal_governor *);
110int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
111int thermal_build_list_of_policies(char *buf);
112void __thermal_zone_device_update(struct thermal_zone_device *tz,
113 enum thermal_notify_event event);
114
115/* Helpers */
116void __thermal_zone_set_trips(struct thermal_zone_device *tz);
117int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
118
119/* sysfs I/F */
120int thermal_zone_create_device_groups(struct thermal_zone_device *, int);
121void thermal_zone_destroy_device_groups(struct thermal_zone_device *);
122void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
123void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);
124/* used only at binding time */
125ssize_t trip_point_show(struct device *, struct device_attribute *, char *);
126ssize_t weight_show(struct device *, struct device_attribute *, char *);
127ssize_t weight_store(struct device *, struct device_attribute *, const char *,
128 size_t);
129
130#ifdef CONFIG_THERMAL_STATISTICS
131void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
132 unsigned long new_state);
133#else
134static inline void
135thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
136 unsigned long new_state) {}
137#endif /* CONFIG_THERMAL_STATISTICS */
138
139/* device tree support */
140#ifdef CONFIG_THERMAL_OF
141int of_thermal_get_ntrips(struct thermal_zone_device *);
142bool of_thermal_is_trip_valid(struct thermal_zone_device *, int);
143const struct thermal_trip *
144of_thermal_get_trip_points(struct thermal_zone_device *);
145#else
146static inline int of_thermal_get_ntrips(struct thermal_zone_device *tz)
147{
148 return 0;
149}
150static inline bool of_thermal_is_trip_valid(struct thermal_zone_device *tz,
151 int trip)
152{
153 return false;
154}
155static inline const struct thermal_trip *
156of_thermal_get_trip_points(struct thermal_zone_device *tz)
157{
158 return NULL;
159}
160#endif
161
162int thermal_zone_device_is_enabled(struct thermal_zone_device *tz);
163
164#endif /* __THERMAL_CORE_H__ */