aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/platform/x86/intel/int1092/intel_sar.c
blob: e03943e6380a6dfec2438c379d27674f0d69293b (plain) (blame)
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// SPDX-License-Identifier: GPL-2.0-only
/*
 * Copyright (c) 2021, Intel Corporation.
 */

#include <linux/acpi.h>
#include <linux/kobject.h>
#include <linux/platform_device.h>
#include <linux/sysfs.h>
#include "intel_sar.h"

/**
 * get_int_value: Retrieve integer values from ACPI Object
 * @obj: acpi_object pointer which has the integer value
 * @out: output pointer will get integer value
 *
 * Function is used to retrieve integer value from acpi object.
 *
 * Return:
 * * 0 on success
 * * -EIO if there is an issue in acpi_object passed.
 */
static int get_int_value(union acpi_object *obj, int *out)
{
	if (!obj || obj->type != ACPI_TYPE_INTEGER)
		return -EIO;
	*out = (int)obj->integer.value;
	return 0;
}

/**
 * update_sar_data: sar data is updated based on regulatory mode
 * @context: pointer to driver context structure
 *
 * sar_data is updated based on regulatory value
 * context->reg_value will never exceed MAX_REGULATORY
 */
static void update_sar_data(struct wwan_sar_context *context)
{
	struct wwan_device_mode_configuration *config =
		&context->config_data[context->reg_value];

	if (config->device_mode_info &&
	    context->sar_data.device_mode < config->total_dev_mode) {
		int itr = 0;

		for (itr = 0; itr < config->total_dev_mode; itr++) {
			if (context->sar_data.device_mode ==
				config->device_mode_info[itr].device_mode) {
				struct wwan_device_mode_info *dev_mode =
				&config->device_mode_info[itr];

				context->sar_data.antennatable_index = dev_mode->antennatable_index;
				context->sar_data.bandtable_index = dev_mode->bandtable_index;
				context->sar_data.sartable_index = dev_mode->sartable_index;
				break;
			}
		}
	}
}

/**
 * parse_package: parse acpi package for retrieving SAR information
 * @context: pointer to driver context structure
 * @item : acpi_object pointer
 *
 * Given acpi_object is iterated to retrieve information for each device mode.
 * If a given package corresponding to a specific device mode is faulty, it is
 * skipped and the specific entry in context structure will have the default value
 * of zero. Decoding of subsequent device modes is realized by having "continue"
 * statements in the for loop on encountering error in parsing given device mode.
 *
 * Return:
 * AE_OK if success
 * AE_ERROR on error
 */
static acpi_status parse_package(struct wwan_sar_context *context, union acpi_object *item)
{
	struct wwan_device_mode_configuration *data;
	int value, itr, reg;
	union acpi_object *num;

	num = &item->package.elements[0];
	if (get_int_value(num, &value) || value < 0 || value >= MAX_REGULATORY)
		return AE_ERROR;

	reg = value;

	data = &context->config_data[reg];
	if (data->total_dev_mode > MAX_DEV_MODES ||	data->total_dev_mode == 0 ||
	    item->package.count <= data->total_dev_mode)
		return AE_ERROR;

	data->device_mode_info = kmalloc_array(data->total_dev_mode,
					       sizeof(struct wwan_device_mode_info), GFP_KERNEL);
	if (!data->device_mode_info)
		return AE_ERROR;

	for (itr = 0; itr < data->total_dev_mode; itr++) {
		struct wwan_device_mode_info temp = { 0 };

		num = &item->package.elements[itr + 1];
		if (num->type != ACPI_TYPE_PACKAGE || num->package.count < TOTAL_DATA)
			continue;
		if (get_int_value(&num->package.elements[0], &temp.device_mode))
			continue;
		if (get_int_value(&num->package.elements[1], &temp.bandtable_index))
			continue;
		if (get_int_value(&num->package.elements[2], &temp.antennatable_index))
			continue;
		if (get_int_value(&num->package.elements[3], &temp.sartable_index))
			continue;
		data->device_mode_info[itr] = temp;
	}
	return AE_OK;
}

/**
 * sar_get_device_mode: Extraction of information from BIOS via DSM calls
 * @device: ACPI device for which to retrieve the data
 *
 * Retrieve the current device mode information from the BIOS.
 *
 * Return:
 * AE_OK on success
 * AE_ERROR on error
 */
static acpi_status sar_get_device_mode(struct platform_device *device)
{
	struct wwan_sar_context *context = dev_get_drvdata(&device->dev);
	acpi_status status = AE_OK;
	union acpi_object *out;
	u32 rev = 0;
	int value;

	out = acpi_evaluate_dsm(context->handle, &context->guid, rev,
				COMMAND_ID_DEV_MODE, NULL);
	if (get_int_value(out, &value)) {
		dev_err(&device->dev, "DSM cmd:%d Failed to retrieve value\n", COMMAND_ID_DEV_MODE);
		status = AE_ERROR;
		goto dev_mode_error;
	}
	context->sar_data.device_mode = value;
	update_sar_data(context);
	sysfs_notify(&device->dev.kobj, NULL, SYSFS_DATANAME);

dev_mode_error:
	ACPI_FREE(out);
	return status;
}

static const struct acpi_device_id sar_device_ids[] = {
	{ "INTC1092", 0},
	{}
};
MODULE_DEVICE_TABLE(acpi, sar_device_ids);

static ssize_t intc_data_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct wwan_sar_context *context = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d %d %d %d\n", context->sar_data.device_mode,
		      context->sar_data.bandtable_index,
		      context->sar_data.antennatable_index,
		      context->sar_data.sartable_index);
}
static DEVICE_ATTR_RO(intc_data);

static ssize_t intc_reg_show(struct device *dev, struct device_attribute *attr, char *buf)
{
	struct wwan_sar_context *context = dev_get_drvdata(dev);

	return sysfs_emit(buf, "%d\n", context->reg_value);
}

static ssize_t intc_reg_store(struct device *dev, struct device_attribute *attr,
			      const char *buf, size_t count)
{
	struct wwan_sar_context *context = dev_get_drvdata(dev);
	unsigned int value;
	int read;

	if (!count)
		return -EINVAL;
	read = kstrtouint(buf, 10, &value);
	if (read < 0)
		return read;
	if (value >= MAX_REGULATORY)
		return -EOVERFLOW;
	context->reg_value = value;
	update_sar_data(context);
	sysfs_notify(&dev->kobj, NULL, SYSFS_DATANAME);
	return count;
}
static DEVICE_ATTR_RW(intc_reg);

static struct attribute *intcsar_attrs[] = {
	&dev_attr_intc_data.attr,
	&dev_attr_intc_reg.attr,
	NULL
};

static struct attribute_group intcsar_group = {
	.attrs = intcsar_attrs,
};

static void sar_notify(acpi_handle handle, u32 event, void *data)
{
	struct platform_device *device = data;

	if (event == SAR_EVENT) {
		if (sar_get_device_mode(device) != AE_OK)
			dev_err(&device->dev, "sar_get_device_mode error");
	}
}

static void sar_get_data(int reg, struct wwan_sar_context *context)
{
	union acpi_object *out, req;
	u32 rev = 0;

	req.type = ACPI_TYPE_INTEGER;
	req.integer.value = reg;
	out = acpi_evaluate_dsm(context->handle, &context->guid, rev,
				COMMAND_ID_CONFIG_TABLE, &req);
	if (!out)
		return;
	if (out->type == ACPI_TYPE_PACKAGE && out->package.count >= 3 &&
	    out->package.elements[0].type == ACPI_TYPE_INTEGER &&
	    out->package.elements[1].type == ACPI_TYPE_INTEGER &&
	    out->package.elements[2].type == ACPI_TYPE_PACKAGE &&
	    out->package.elements[2].package.count > 0) {
		context->config_data[reg].version = out->package.elements[0].integer.value;
		context->config_data[reg].total_dev_mode =
			out->package.elements[1].integer.value;
		if (context->config_data[reg].total_dev_mode <= 0 ||
		    context->config_data[reg].total_dev_mode > MAX_DEV_MODES) {
			ACPI_FREE(out);
			return;
		}
		parse_package(context, &out->package.elements[2]);
	}
	ACPI_FREE(out);
}

static int sar_probe(struct platform_device *device)
{
	struct wwan_sar_context *context;
	int reg;
	int result;

	context = kzalloc(sizeof(*context), GFP_KERNEL);
	if (!context)
		return -ENOMEM;

	context->sar_device = device;
	context->handle = ACPI_HANDLE(&device->dev);
	dev_set_drvdata(&device->dev, context);

	result = guid_parse(SAR_DSM_UUID, &context->guid);
	if (result) {
		dev_err(&device->dev, "SAR UUID parse error: %d\n", result);
		goto r_free;
	}

	for (reg = 0; reg < MAX_REGULATORY; reg++)
		sar_get_data(reg, context);

	if (sar_get_device_mode(device) != AE_OK) {
		dev_err(&device->dev, "Failed to get device mode\n");
		result = -EIO;
		goto r_free;
	}

	result = sysfs_create_group(&device->dev.kobj, &intcsar_group);
	if (result) {
		dev_err(&device->dev, "sysfs creation failed\n");
		goto r_free;
	}

	if (acpi_install_notify_handler(ACPI_HANDLE(&device->dev), ACPI_DEVICE_NOTIFY,
					sar_notify, (void *)device) != AE_OK) {
		dev_err(&device->dev, "Failed acpi_install_notify_handler\n");
		result = -EIO;
		goto r_sys;
	}
	return 0;

r_sys:
	sysfs_remove_group(&device->dev.kobj, &intcsar_group);
r_free:
	kfree(context);
	return result;
}

static int sar_remove(struct platform_device *device)
{
	struct wwan_sar_context *context = dev_get_drvdata(&device->dev);
	int reg;

	acpi_remove_notify_handler(ACPI_HANDLE(&device->dev),
				   ACPI_DEVICE_NOTIFY, sar_notify);
	sysfs_remove_group(&device->dev.kobj, &intcsar_group);
	for (reg = 0; reg < MAX_REGULATORY; reg++)
		kfree(context->config_data[reg].device_mode_info);

	kfree(context);
	return 0;
}

static struct platform_driver sar_driver = {
	.probe = sar_probe,
	.remove = sar_remove,
	.driver = {
		.name = DRVNAME,
		.acpi_match_table = ACPI_PTR(sar_device_ids)
	}
};
module_platform_driver(sar_driver);

MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("Platform device driver for INTEL MODEM BIOS SAR");
MODULE_AUTHOR("Shravan Sudhakar <s.shravan@intel.com>");