17e3c0381SLina Iyer /* SPDX-License-Identifier: GPL-2.0 */
271350db4SDurgadoss R /*
371350db4SDurgadoss R  *  thermal_core.h
471350db4SDurgadoss R  *
571350db4SDurgadoss R  *  Copyright (C) 2012  Intel Corp
671350db4SDurgadoss R  *  Author: Durgadoss R <durgadoss.r@intel.com>
771350db4SDurgadoss R  */
871350db4SDurgadoss R 
971350db4SDurgadoss R #ifndef __THERMAL_CORE_H__
1071350db4SDurgadoss R #define __THERMAL_CORE_H__
1171350db4SDurgadoss R 
1271350db4SDurgadoss R #include <linux/device.h>
1371350db4SDurgadoss R #include <linux/thermal.h>
1471350db4SDurgadoss R 
155b8583d3SDaniel Lezcano #include "thermal_netlink.h"
165b8583d3SDaniel Lezcano 
178097db40SDaniel Lezcano /* Default Thermal Governor */
188097db40SDaniel Lezcano #if defined(CONFIG_THERMAL_DEFAULT_GOV_STEP_WISE)
198097db40SDaniel Lezcano #define DEFAULT_THERMAL_GOVERNOR       "step_wise"
208097db40SDaniel Lezcano #elif defined(CONFIG_THERMAL_DEFAULT_GOV_FAIR_SHARE)
218097db40SDaniel Lezcano #define DEFAULT_THERMAL_GOVERNOR       "fair_share"
228097db40SDaniel Lezcano #elif defined(CONFIG_THERMAL_DEFAULT_GOV_USER_SPACE)
238097db40SDaniel Lezcano #define DEFAULT_THERMAL_GOVERNOR       "user_space"
248097db40SDaniel Lezcano #elif defined(CONFIG_THERMAL_DEFAULT_GOV_POWER_ALLOCATOR)
258097db40SDaniel Lezcano #define DEFAULT_THERMAL_GOVERNOR       "power_allocator"
268097db40SDaniel Lezcano #endif
278097db40SDaniel Lezcano 
2871350db4SDurgadoss R /* Initial state of a cooling device during binding */
2971350db4SDurgadoss R #define THERMAL_NO_TARGET -1UL
3071350db4SDurgadoss R 
31980af75eSDaniel Lezcano /* Init section thermal table */
32980af75eSDaniel Lezcano extern struct thermal_governor *__governor_thermal_table[];
33980af75eSDaniel Lezcano extern struct thermal_governor *__governor_thermal_table_end[];
34980af75eSDaniel Lezcano 
35980af75eSDaniel Lezcano #define THERMAL_TABLE_ENTRY(table, name)			\
36980af75eSDaniel Lezcano 	static typeof(name) *__thermal_table_entry_##name	\
3733def849SJoe Perches 	__used __section("__" #table "_thermal_table") = &name
38980af75eSDaniel Lezcano 
39980af75eSDaniel Lezcano #define THERMAL_GOVERNOR_DECLARE(name)	THERMAL_TABLE_ENTRY(governor, name)
40980af75eSDaniel Lezcano 
41980af75eSDaniel Lezcano #define for_each_governor_table(__governor)		\
42980af75eSDaniel Lezcano 	for (__governor = __governor_thermal_table;	\
43980af75eSDaniel Lezcano 	     __governor < __governor_thermal_table_end;	\
44980af75eSDaniel Lezcano 	     __governor++)
45980af75eSDaniel Lezcano 
463d44a509SDaniel Lezcano int for_each_thermal_zone(int (*cb)(struct thermal_zone_device *, void *),
473d44a509SDaniel Lezcano 			  void *);
483d44a509SDaniel Lezcano 
493d44a509SDaniel Lezcano int for_each_thermal_cooling_device(int (*cb)(struct thermal_cooling_device *,
503d44a509SDaniel Lezcano 					      void *), void *);
513d44a509SDaniel Lezcano 
523d44a509SDaniel Lezcano int for_each_thermal_governor(int (*cb)(struct thermal_governor *, void *),
533d44a509SDaniel Lezcano 			      void *thermal_governor);
543d44a509SDaniel Lezcano 
55329b064fSDaniel Lezcano struct thermal_zone_device *thermal_zone_get_by_id(int id);
56329b064fSDaniel Lezcano 
57c68df440SDaniel Lezcano struct thermal_attr {
58c68df440SDaniel Lezcano 	struct device_attribute attr;
59c68df440SDaniel Lezcano 	char name[THERMAL_NAME_LENGTH];
60c68df440SDaniel Lezcano };
61c68df440SDaniel Lezcano 
6233a88af1SDaniel Lezcano static inline bool cdev_is_power_actor(struct thermal_cooling_device *cdev)
6333a88af1SDaniel Lezcano {
6433a88af1SDaniel Lezcano 	return cdev->ops->get_requested_power && cdev->ops->state2power &&
6533a88af1SDaniel Lezcano 		cdev->ops->power2state;
6633a88af1SDaniel Lezcano }
6733a88af1SDaniel Lezcano 
6823ff8529SDaniel Lezcano void thermal_cdev_update(struct thermal_cooling_device *);
69b70dbf40SLukasz Luba void __thermal_cdev_update(struct thermal_cooling_device *cdev);
7023ff8529SDaniel Lezcano 
71f0129c23SDaniel Lezcano int get_tz_trend(struct thermal_zone_device *tz, int trip);
72f0129c23SDaniel Lezcano 
7306f1041fSDaniel Lezcano struct thermal_instance *
7406f1041fSDaniel Lezcano get_thermal_instance(struct thermal_zone_device *tz,
7506f1041fSDaniel Lezcano 		     struct thermal_cooling_device *cdev,
7606f1041fSDaniel Lezcano 		     int trip);
7706f1041fSDaniel Lezcano 
7871350db4SDurgadoss R /*
7971350db4SDurgadoss R  * This structure is used to describe the behavior of
8071350db4SDurgadoss R  * a certain cooling device on a certain trip point
8171350db4SDurgadoss R  * in a certain thermal zone
8271350db4SDurgadoss R  */
8371350db4SDurgadoss R struct thermal_instance {
8471350db4SDurgadoss R 	int id;
8571350db4SDurgadoss R 	char name[THERMAL_NAME_LENGTH];
8671350db4SDurgadoss R 	struct thermal_zone_device *tz;
8771350db4SDurgadoss R 	struct thermal_cooling_device *cdev;
8871350db4SDurgadoss R 	int trip;
89bb431ba2SZhang Rui 	bool initialized;
9071350db4SDurgadoss R 	unsigned long upper;	/* Highest cooling state for this trip point */
9171350db4SDurgadoss R 	unsigned long lower;	/* Lowest cooling state for this trip point */
9271350db4SDurgadoss R 	unsigned long target;	/* expected cooling state */
9371350db4SDurgadoss R 	char attr_name[THERMAL_NAME_LENGTH];
9471350db4SDurgadoss R 	struct device_attribute attr;
95db916513SJavi Merino 	char weight_attr_name[THERMAL_NAME_LENGTH];
96db916513SJavi Merino 	struct device_attribute weight_attr;
9771350db4SDurgadoss R 	struct list_head tz_node; /* node in tz->thermal_instances */
9871350db4SDurgadoss R 	struct list_head cdev_node; /* node in cdev->thermal_instances */
996cd9e9f6SKapileshwar Singh 	unsigned int weight; /* The weight of the cooling device */
10071350db4SDurgadoss R };
10171350db4SDurgadoss R 
102ba78da44SEduardo Valentin #define to_thermal_zone(_dev) \
103ba78da44SEduardo Valentin 	container_of(_dev, struct thermal_zone_device, device)
104ba78da44SEduardo Valentin 
10599ea2effSEduardo Valentin #define to_cooling_device(_dev)	\
10699ea2effSEduardo Valentin 	container_of(_dev, struct thermal_cooling_device, device)
10799ea2effSEduardo Valentin 
10880a26a5cSZhang Rui int thermal_register_governor(struct thermal_governor *);
10980a26a5cSZhang Rui void thermal_unregister_governor(struct thermal_governor *);
1106b885202SEduardo Valentin int thermal_zone_device_set_policy(struct thermal_zone_device *, char *);
11197d2423bSEduardo Valentin int thermal_build_list_of_policies(char *buf);
11205eeee2bSGuenter Roeck void __thermal_zone_device_update(struct thermal_zone_device *tz,
11305eeee2bSGuenter Roeck 				  enum thermal_notify_event event);
11480a26a5cSZhang Rui 
115bceb5646SDaniel Lezcano /* Helpers */
116a930da9bSDaniel Lezcano void __thermal_zone_set_trips(struct thermal_zone_device *tz);
117*7c3d5c20SDaniel Lezcano int __thermal_zone_get_trip(struct thermal_zone_device *tz, int trip_id,
118*7c3d5c20SDaniel Lezcano 			    struct thermal_trip *trip);
119a930da9bSDaniel Lezcano int __thermal_zone_get_temp(struct thermal_zone_device *tz, int *temp);
120bceb5646SDaniel Lezcano 
121a369ee88SEduardo Valentin /* sysfs I/F */
122a369ee88SEduardo Valentin int thermal_zone_create_device_groups(struct thermal_zone_device *, int);
12332fa5ba3SChristophe Jaillet void thermal_zone_destroy_device_groups(struct thermal_zone_device *);
12445cf2ec9SEduardo Valentin void thermal_cooling_device_setup_sysfs(struct thermal_cooling_device *);
1258ea22951SViresh Kumar void thermal_cooling_device_destroy_sysfs(struct thermal_cooling_device *cdev);
12645cf2ec9SEduardo Valentin /* used only at binding time */
12733e678d4SViresh Kumar ssize_t trip_point_show(struct device *, struct device_attribute *, char *);
12833e678d4SViresh Kumar ssize_t weight_show(struct device *, struct device_attribute *, char *);
12933e678d4SViresh Kumar ssize_t weight_store(struct device *, struct device_attribute *, const char *,
13033e678d4SViresh Kumar 		     size_t);
131a369ee88SEduardo Valentin 
1328ea22951SViresh Kumar #ifdef CONFIG_THERMAL_STATISTICS
1338ea22951SViresh Kumar void thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
1348ea22951SViresh Kumar 					 unsigned long new_state);
1358ea22951SViresh Kumar #else
1368ea22951SViresh Kumar static inline void
1378ea22951SViresh Kumar thermal_cooling_device_stats_update(struct thermal_cooling_device *cdev,
1388ea22951SViresh Kumar 				    unsigned long new_state) {}
1398ea22951SViresh Kumar #endif /* CONFIG_THERMAL_STATISTICS */
1408ea22951SViresh Kumar 
1414e5e4705SEduardo Valentin /* device tree support */
1424e5e4705SEduardo Valentin #ifdef CONFIG_THERMAL_OF
14308dab66eSLukasz Majewski int of_thermal_get_ntrips(struct thermal_zone_device *);
144a9bf2cc4SLukasz Majewski bool of_thermal_is_trip_valid(struct thermal_zone_device *, int);
145ebc3193aSGeert Uytterhoeven const struct thermal_trip *
146ce8be778SLukasz Majewski of_thermal_get_trip_points(struct thermal_zone_device *);
1474e5e4705SEduardo Valentin #else
14808dab66eSLukasz Majewski static inline int of_thermal_get_ntrips(struct thermal_zone_device *tz)
14908dab66eSLukasz Majewski {
15008dab66eSLukasz Majewski 	return 0;
15108dab66eSLukasz Majewski }
152a9bf2cc4SLukasz Majewski static inline bool of_thermal_is_trip_valid(struct thermal_zone_device *tz,
153a9bf2cc4SLukasz Majewski 					    int trip)
154a9bf2cc4SLukasz Majewski {
155ce1d9491SJoe Perches 	return false;
156a9bf2cc4SLukasz Majewski }
157ebc3193aSGeert Uytterhoeven static inline const struct thermal_trip *
158ce8be778SLukasz Majewski of_thermal_get_trip_points(struct thermal_zone_device *tz)
159ce8be778SLukasz Majewski {
160ce8be778SLukasz Majewski 	return NULL;
161ce8be778SLukasz Majewski }
1624e5e4705SEduardo Valentin #endif
1634e5e4705SEduardo Valentin 
164514acd00SAndrzej Pietrasiewicz int thermal_zone_device_is_enabled(struct thermal_zone_device *tz);
165514acd00SAndrzej Pietrasiewicz 
16671350db4SDurgadoss R #endif /* __THERMAL_CORE_H__ */
167