1b78b4982SMaximilian Luz // SPDX-License-Identifier: GPL-2.0+
2b78b4982SMaximilian Luz /*
3b78b4982SMaximilian Luz  * Surface Platform Profile / Performance Mode driver for Surface System
4b78b4982SMaximilian Luz  * Aggregator Module (thermal subsystem).
5b78b4982SMaximilian Luz  *
6221756e6SMaximilian Luz  * Copyright (C) 2021-2022 Maximilian Luz <luzmaximilian@gmail.com>
7b78b4982SMaximilian Luz  */
8b78b4982SMaximilian Luz 
9b78b4982SMaximilian Luz #include <asm/unaligned.h>
10b78b4982SMaximilian Luz #include <linux/kernel.h>
11b78b4982SMaximilian Luz #include <linux/module.h>
12b78b4982SMaximilian Luz #include <linux/platform_profile.h>
13b78b4982SMaximilian Luz #include <linux/types.h>
14b78b4982SMaximilian Luz 
15b78b4982SMaximilian Luz #include <linux/surface_aggregator/device.h>
16b78b4982SMaximilian Luz 
17b78b4982SMaximilian Luz enum ssam_tmp_profile {
18b78b4982SMaximilian Luz 	SSAM_TMP_PROFILE_NORMAL             = 1,
19b78b4982SMaximilian Luz 	SSAM_TMP_PROFILE_BATTERY_SAVER      = 2,
20b78b4982SMaximilian Luz 	SSAM_TMP_PROFILE_BETTER_PERFORMANCE = 3,
21b78b4982SMaximilian Luz 	SSAM_TMP_PROFILE_BEST_PERFORMANCE   = 4,
22b78b4982SMaximilian Luz };
23b78b4982SMaximilian Luz 
24b78b4982SMaximilian Luz struct ssam_tmp_profile_info {
25b78b4982SMaximilian Luz 	__le32 profile;
26b78b4982SMaximilian Luz 	__le16 unknown1;
27b78b4982SMaximilian Luz 	__le16 unknown2;
28b78b4982SMaximilian Luz } __packed;
29b78b4982SMaximilian Luz 
30b78b4982SMaximilian Luz struct ssam_tmp_profile_device {
31b78b4982SMaximilian Luz 	struct ssam_device *sdev;
32b78b4982SMaximilian Luz 	struct platform_profile_handler handler;
33b78b4982SMaximilian Luz };
34b78b4982SMaximilian Luz 
3503ee3183SMaximilian Luz SSAM_DEFINE_SYNC_REQUEST_CL_R(__ssam_tmp_profile_get, struct ssam_tmp_profile_info, {
36b78b4982SMaximilian Luz 	.target_category = SSAM_SSH_TC_TMP,
37b78b4982SMaximilian Luz 	.command_id      = 0x02,
38b78b4982SMaximilian Luz });
39b78b4982SMaximilian Luz 
4003ee3183SMaximilian Luz SSAM_DEFINE_SYNC_REQUEST_CL_W(__ssam_tmp_profile_set, __le32, {
41b78b4982SMaximilian Luz 	.target_category = SSAM_SSH_TC_TMP,
42b78b4982SMaximilian Luz 	.command_id      = 0x03,
43b78b4982SMaximilian Luz });
44b78b4982SMaximilian Luz 
ssam_tmp_profile_get(struct ssam_device * sdev,enum ssam_tmp_profile * p)45b78b4982SMaximilian Luz static int ssam_tmp_profile_get(struct ssam_device *sdev, enum ssam_tmp_profile *p)
46b78b4982SMaximilian Luz {
47b78b4982SMaximilian Luz 	struct ssam_tmp_profile_info info;
48b78b4982SMaximilian Luz 	int status;
49b78b4982SMaximilian Luz 
50b78b4982SMaximilian Luz 	status = ssam_retry(__ssam_tmp_profile_get, sdev, &info);
51b78b4982SMaximilian Luz 	if (status < 0)
52b78b4982SMaximilian Luz 		return status;
53b78b4982SMaximilian Luz 
54b78b4982SMaximilian Luz 	*p = le32_to_cpu(info.profile);
55b78b4982SMaximilian Luz 	return 0;
56b78b4982SMaximilian Luz }
57b78b4982SMaximilian Luz 
ssam_tmp_profile_set(struct ssam_device * sdev,enum ssam_tmp_profile p)58b78b4982SMaximilian Luz static int ssam_tmp_profile_set(struct ssam_device *sdev, enum ssam_tmp_profile p)
59b78b4982SMaximilian Luz {
60b78b4982SMaximilian Luz 	__le32 profile_le = cpu_to_le32(p);
61b78b4982SMaximilian Luz 
62b78b4982SMaximilian Luz 	return ssam_retry(__ssam_tmp_profile_set, sdev, &profile_le);
63b78b4982SMaximilian Luz }
64b78b4982SMaximilian Luz 
convert_ssam_to_profile(struct ssam_device * sdev,enum ssam_tmp_profile p)65b78b4982SMaximilian Luz static int convert_ssam_to_profile(struct ssam_device *sdev, enum ssam_tmp_profile p)
66b78b4982SMaximilian Luz {
67b78b4982SMaximilian Luz 	switch (p) {
68b78b4982SMaximilian Luz 	case SSAM_TMP_PROFILE_NORMAL:
69b78b4982SMaximilian Luz 		return PLATFORM_PROFILE_BALANCED;
70b78b4982SMaximilian Luz 
71b78b4982SMaximilian Luz 	case SSAM_TMP_PROFILE_BATTERY_SAVER:
72b78b4982SMaximilian Luz 		return PLATFORM_PROFILE_LOW_POWER;
73b78b4982SMaximilian Luz 
74b78b4982SMaximilian Luz 	case SSAM_TMP_PROFILE_BETTER_PERFORMANCE:
75b78b4982SMaximilian Luz 		return PLATFORM_PROFILE_BALANCED_PERFORMANCE;
76b78b4982SMaximilian Luz 
77b78b4982SMaximilian Luz 	case SSAM_TMP_PROFILE_BEST_PERFORMANCE:
78b78b4982SMaximilian Luz 		return PLATFORM_PROFILE_PERFORMANCE;
79b78b4982SMaximilian Luz 
80b78b4982SMaximilian Luz 	default:
81b78b4982SMaximilian Luz 		dev_err(&sdev->dev, "invalid performance profile: %d", p);
82b78b4982SMaximilian Luz 		return -EINVAL;
83b78b4982SMaximilian Luz 	}
84b78b4982SMaximilian Luz }
85b78b4982SMaximilian Luz 
convert_profile_to_ssam(struct ssam_device * sdev,enum platform_profile_option p)86b78b4982SMaximilian Luz static int convert_profile_to_ssam(struct ssam_device *sdev, enum platform_profile_option p)
87b78b4982SMaximilian Luz {
88b78b4982SMaximilian Luz 	switch (p) {
89b78b4982SMaximilian Luz 	case PLATFORM_PROFILE_LOW_POWER:
90b78b4982SMaximilian Luz 		return SSAM_TMP_PROFILE_BATTERY_SAVER;
91b78b4982SMaximilian Luz 
92b78b4982SMaximilian Luz 	case PLATFORM_PROFILE_BALANCED:
93b78b4982SMaximilian Luz 		return SSAM_TMP_PROFILE_NORMAL;
94b78b4982SMaximilian Luz 
95b78b4982SMaximilian Luz 	case PLATFORM_PROFILE_BALANCED_PERFORMANCE:
96b78b4982SMaximilian Luz 		return SSAM_TMP_PROFILE_BETTER_PERFORMANCE;
97b78b4982SMaximilian Luz 
98b78b4982SMaximilian Luz 	case PLATFORM_PROFILE_PERFORMANCE:
99b78b4982SMaximilian Luz 		return SSAM_TMP_PROFILE_BEST_PERFORMANCE;
100b78b4982SMaximilian Luz 
101b78b4982SMaximilian Luz 	default:
102b78b4982SMaximilian Luz 		/* This should have already been caught by platform_profile_store(). */
103b78b4982SMaximilian Luz 		WARN(true, "unsupported platform profile");
104b78b4982SMaximilian Luz 		return -EOPNOTSUPP;
105b78b4982SMaximilian Luz 	}
106b78b4982SMaximilian Luz }
107b78b4982SMaximilian Luz 
ssam_platform_profile_get(struct platform_profile_handler * pprof,enum platform_profile_option * profile)108b78b4982SMaximilian Luz static int ssam_platform_profile_get(struct platform_profile_handler *pprof,
109b78b4982SMaximilian Luz 				     enum platform_profile_option *profile)
110b78b4982SMaximilian Luz {
111b78b4982SMaximilian Luz 	struct ssam_tmp_profile_device *tpd;
112b78b4982SMaximilian Luz 	enum ssam_tmp_profile tp;
113b78b4982SMaximilian Luz 	int status;
114b78b4982SMaximilian Luz 
115b78b4982SMaximilian Luz 	tpd = container_of(pprof, struct ssam_tmp_profile_device, handler);
116b78b4982SMaximilian Luz 
117b78b4982SMaximilian Luz 	status = ssam_tmp_profile_get(tpd->sdev, &tp);
118b78b4982SMaximilian Luz 	if (status)
119b78b4982SMaximilian Luz 		return status;
120b78b4982SMaximilian Luz 
121b78b4982SMaximilian Luz 	status = convert_ssam_to_profile(tpd->sdev, tp);
122b78b4982SMaximilian Luz 	if (status < 0)
123b78b4982SMaximilian Luz 		return status;
124b78b4982SMaximilian Luz 
125b78b4982SMaximilian Luz 	*profile = status;
126b78b4982SMaximilian Luz 	return 0;
127b78b4982SMaximilian Luz }
128b78b4982SMaximilian Luz 
ssam_platform_profile_set(struct platform_profile_handler * pprof,enum platform_profile_option profile)129b78b4982SMaximilian Luz static int ssam_platform_profile_set(struct platform_profile_handler *pprof,
130b78b4982SMaximilian Luz 				     enum platform_profile_option profile)
131b78b4982SMaximilian Luz {
132b78b4982SMaximilian Luz 	struct ssam_tmp_profile_device *tpd;
133b78b4982SMaximilian Luz 	int tp;
134b78b4982SMaximilian Luz 
135b78b4982SMaximilian Luz 	tpd = container_of(pprof, struct ssam_tmp_profile_device, handler);
136b78b4982SMaximilian Luz 
137b78b4982SMaximilian Luz 	tp = convert_profile_to_ssam(tpd->sdev, profile);
138b78b4982SMaximilian Luz 	if (tp < 0)
139b78b4982SMaximilian Luz 		return tp;
140b78b4982SMaximilian Luz 
141b78b4982SMaximilian Luz 	return ssam_tmp_profile_set(tpd->sdev, tp);
142b78b4982SMaximilian Luz }
143b78b4982SMaximilian Luz 
surface_platform_profile_probe(struct ssam_device * sdev)144b78b4982SMaximilian Luz static int surface_platform_profile_probe(struct ssam_device *sdev)
145b78b4982SMaximilian Luz {
146b78b4982SMaximilian Luz 	struct ssam_tmp_profile_device *tpd;
147b78b4982SMaximilian Luz 
148b78b4982SMaximilian Luz 	tpd = devm_kzalloc(&sdev->dev, sizeof(*tpd), GFP_KERNEL);
149b78b4982SMaximilian Luz 	if (!tpd)
150b78b4982SMaximilian Luz 		return -ENOMEM;
151b78b4982SMaximilian Luz 
152b78b4982SMaximilian Luz 	tpd->sdev = sdev;
153b78b4982SMaximilian Luz 
154b78b4982SMaximilian Luz 	tpd->handler.profile_get = ssam_platform_profile_get;
155b78b4982SMaximilian Luz 	tpd->handler.profile_set = ssam_platform_profile_set;
156b78b4982SMaximilian Luz 
157b78b4982SMaximilian Luz 	set_bit(PLATFORM_PROFILE_LOW_POWER, tpd->handler.choices);
158b78b4982SMaximilian Luz 	set_bit(PLATFORM_PROFILE_BALANCED, tpd->handler.choices);
159b78b4982SMaximilian Luz 	set_bit(PLATFORM_PROFILE_BALANCED_PERFORMANCE, tpd->handler.choices);
160b78b4982SMaximilian Luz 	set_bit(PLATFORM_PROFILE_PERFORMANCE, tpd->handler.choices);
161b78b4982SMaximilian Luz 
162*fe0e04cfSArmin Wolf 	return platform_profile_register(&tpd->handler);
163b78b4982SMaximilian Luz }
164b78b4982SMaximilian Luz 
surface_platform_profile_remove(struct ssam_device * sdev)165b78b4982SMaximilian Luz static void surface_platform_profile_remove(struct ssam_device *sdev)
166b78b4982SMaximilian Luz {
167b78b4982SMaximilian Luz 	platform_profile_remove();
168b78b4982SMaximilian Luz }
169b78b4982SMaximilian Luz 
170b78b4982SMaximilian Luz static const struct ssam_device_id ssam_platform_profile_match[] = {
17178abf1b5SMaximilian Luz 	{ SSAM_SDEV(TMP, SAM, 0x00, 0x01) },
172b78b4982SMaximilian Luz 	{ },
173b78b4982SMaximilian Luz };
174b78b4982SMaximilian Luz MODULE_DEVICE_TABLE(ssam, ssam_platform_profile_match);
175b78b4982SMaximilian Luz 
176b78b4982SMaximilian Luz static struct ssam_device_driver surface_platform_profile = {
177b78b4982SMaximilian Luz 	.probe = surface_platform_profile_probe,
178b78b4982SMaximilian Luz 	.remove = surface_platform_profile_remove,
179b78b4982SMaximilian Luz 	.match_table = ssam_platform_profile_match,
180b78b4982SMaximilian Luz 	.driver = {
181b78b4982SMaximilian Luz 		.name = "surface_platform_profile",
182b78b4982SMaximilian Luz 		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
183b78b4982SMaximilian Luz 	},
184b78b4982SMaximilian Luz };
185b78b4982SMaximilian Luz module_ssam_device_driver(surface_platform_profile);
186b78b4982SMaximilian Luz 
187b78b4982SMaximilian Luz MODULE_AUTHOR("Maximilian Luz <luzmaximilian@gmail.com>");
188b78b4982SMaximilian Luz MODULE_DESCRIPTION("Platform Profile Support for Surface System Aggregator Module");
189b78b4982SMaximilian Luz MODULE_LICENSE("GPL");
190