summaryrefslogtreecommitdiffstats
path: root/drivers/platform/x86/inspur_platform_profile.c
blob: 743705bddda368fd07dd154be170bf0afe44f411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// SPDX-License-Identifier: GPL-2.0
/*
 *  Inspur WMI Platform Profile
 *
 *  Copyright (C) 2018	      Ai Chao <aichao@kylinos.cn>
 */

#include <linux/acpi.h>
#include <linux/device.h>
#include <linux/module.h>
#include <linux/platform_profile.h>
#include <linux/wmi.h>

#define WMI_INSPUR_POWERMODE_BIOS_GUID "596C31E3-332D-43C9-AEE9-585493284F5D"

enum inspur_wmi_method_ids {
	INSPUR_WMI_GET_POWERMODE = 0x02,
	INSPUR_WMI_SET_POWERMODE = 0x03,
};

/*
 * Power Mode:
 *           0x0: Balance Mode
 *           0x1: Performance Mode
 *           0x2: Power Saver Mode
 */
enum inspur_tmp_profile {
	INSPUR_TMP_PROFILE_BALANCE	= 0,
	INSPUR_TMP_PROFILE_PERFORMANCE	= 1,
	INSPUR_TMP_PROFILE_POWERSAVE	= 2,
};

struct inspur_wmi_priv {
	struct wmi_device *wdev;
	struct platform_profile_handler handler;
};

static int inspur_wmi_perform_query(struct wmi_device *wdev,
				    enum inspur_wmi_method_ids query_id,
				    void *buffer, size_t insize,
				    size_t outsize)
{
	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
	struct acpi_buffer input = { insize, buffer};
	union acpi_object *obj;
	acpi_status status;
	int ret = 0;

	status = wmidev_evaluate_method(wdev, 0, query_id, &input, &output);
	if (ACPI_FAILURE(status)) {
		dev_err(&wdev->dev, "EC Powermode control failed: %s\n",
			acpi_format_exception(status));
		return -EIO;
	}

	obj = output.pointer;
	if (!obj)
		return -EINVAL;

	if (obj->type != ACPI_TYPE_BUFFER ||
	    obj->buffer.length != outsize) {
		ret = -EINVAL;
		goto out_free;
	}

	memcpy(buffer, obj->buffer.pointer, obj->buffer.length);

out_free:
	kfree(obj);
	return ret;
}

/*
 * Set Power Mode to EC RAM. If Power Mode value greater than 0x3,
 * return error
 * Method ID: 0x3
 * Arg: 4 Bytes
 * Byte [0]: Power Mode:
 *         0x0: Balance Mode
 *         0x1: Performance Mode
 *         0x2: Power Saver Mode
 * Return Value: 4 Bytes
 * Byte [0]: Return Code
 *         0x0: No Error
 *         0x1: Error
 */
static int inspur_platform_profile_set(struct platform_profile_handler *pprof,
				       enum platform_profile_option profile)
{
	struct inspur_wmi_priv *priv = container_of(pprof, struct inspur_wmi_priv,
						    handler);
	u8 ret_code[4] = {0, 0, 0, 0};
	int ret;

	switch (profile) {
	case PLATFORM_PROFILE_BALANCED:
		ret_code[0] = INSPUR_TMP_PROFILE_BALANCE;
		break;
	case PLATFORM_PROFILE_PERFORMANCE:
		ret_code[0] = INSPUR_TMP_PROFILE_PERFORMANCE;
		break;
	case PLATFORM_PROFILE_LOW_POWER:
		ret_code[0] = INSPUR_TMP_PROFILE_POWERSAVE;
		break;
	default:
		return -EOPNOTSUPP;
	}

	ret = inspur_wmi_perform_query(priv->wdev, INSPUR_WMI_SET_POWERMODE,
				       ret_code, sizeof(ret_code),
				       sizeof(ret_code));

	if (ret < 0)
		return ret;

	if (ret_code[0])
		return -EBADRQC;

	return 0;
}

/*
 * Get Power Mode from EC RAM, If Power Mode value greater than 0x3,
 * return error
 * Method ID: 0x2
 * Return Value: 4 Bytes
 * Byte [0]: Return Code
 *         0x0: No Error
 *         0x1: Error
 * Byte [1]: Power Mode
 *         0x0: Balance Mode
 *         0x1: Performance Mode
 *         0x2: Power Saver Mode
 */
static int inspur_platform_profile_get(struct platform_profile_handler *pprof,
				       enum platform_profile_option *profile)
{
	struct inspur_wmi_priv *priv = container_of(pprof, struct inspur_wmi_priv,
						    handler);
	u8 ret_code[4] = {0, 0, 0, 0};
	int ret;

	ret = inspur_wmi_perform_query(priv->wdev, INSPUR_WMI_GET_POWERMODE,
				       &ret_code, sizeof(ret_code),
				       sizeof(ret_code));
	if (ret < 0)
		return ret;

	if (ret_code[0])
		return -EBADRQC;

	switch (ret_code[1]) {
	case INSPUR_TMP_PROFILE_BALANCE:
		*profile = PLATFORM_PROFILE_BALANCED;
		break;
	case INSPUR_TMP_PROFILE_PERFORMANCE:
		*profile = PLATFORM_PROFILE_PERFORMANCE;
		break;
	case INSPUR_TMP_PROFILE_POWERSAVE:
		*profile = PLATFORM_PROFILE_LOW_POWER;
		break;
	default:
		return -EINVAL;
	}

	return 0;
}

static int inspur_wmi_probe(struct wmi_device *wdev, const void *context)
{
	struct inspur_wmi_priv *priv;

	priv = devm_kzalloc(&wdev->dev, sizeof(*priv), GFP_KERNEL);
	if (!priv)
		return -ENOMEM;

	priv->wdev = wdev;
	dev_set_drvdata(&wdev->dev, priv);

	priv->handler.profile_get = inspur_platform_profile_get;
	priv->handler.profile_set = inspur_platform_profile_set;

	set_bit(PLATFORM_PROFILE_LOW_POWER, priv->handler.choices);
	set_bit(PLATFORM_PROFILE_BALANCED, priv->handler.choices);
	set_bit(PLATFORM_PROFILE_PERFORMANCE, priv->handler.choices);

	return platform_profile_register(&priv->handler);
}

static void inspur_wmi_remove(struct wmi_device *wdev)
{
	platform_profile_remove();
}

static const struct wmi_device_id inspur_wmi_id_table[] = {
	{ .guid_string = WMI_INSPUR_POWERMODE_BIOS_GUID },
	{  }
};

MODULE_DEVICE_TABLE(wmi, inspur_wmi_id_table);

static struct wmi_driver inspur_wmi_driver = {
	.driver = {
		.name = "inspur-wmi-platform-profile",
		.probe_type = PROBE_PREFER_ASYNCHRONOUS,
	},
	.id_table = inspur_wmi_id_table,
	.probe = inspur_wmi_probe,
	.remove = inspur_wmi_remove,
};

module_wmi_driver(inspur_wmi_driver);

MODULE_AUTHOR("Ai Chao <aichao@kylinos.cn>");
MODULE_DESCRIPTION("Platform Profile Support for Inspur");
MODULE_LICENSE("GPL");