xref: /linux/drivers/platform/x86/intel/int1092/intel_sar.c (revision 5b05bb3f6c5716fab6911e12d60dd1f43ad9806a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2021, Intel Corporation.
4  */
5 
6 #include <linux/acpi.h>
7 #include <linux/kobject.h>
8 #include <linux/platform_device.h>
9 #include <linux/sysfs.h>
10 #include "intel_sar.h"
11 
12 /**
13  * get_int_value: Retrieve integer values from ACPI Object
14  * @obj: acpi_object pointer which has the integer value
15  * @out: output pointer will get integer value
16  *
17  * Function is used to retrieve integer value from acpi object.
18  *
19  * Return:
20  * * 0 on success
21  * * -EIO if there is an issue in acpi_object passed.
22  */
get_int_value(union acpi_object * obj,int * out)23 static int get_int_value(union acpi_object *obj, int *out)
24 {
25 	if (!obj || obj->type != ACPI_TYPE_INTEGER)
26 		return -EIO;
27 	*out = (int)obj->integer.value;
28 	return 0;
29 }
30 
31 /**
32  * update_sar_data: sar data is updated based on regulatory mode
33  * @context: pointer to driver context structure
34  *
35  * sar_data is updated based on regulatory value
36  * context->reg_value will never exceed MAX_REGULATORY
37  */
update_sar_data(struct wwan_sar_context * context)38 static void update_sar_data(struct wwan_sar_context *context)
39 {
40 	struct wwan_device_mode_configuration *config =
41 		&context->config_data[context->reg_value];
42 
43 	if (config->device_mode_info &&
44 	    context->sar_data.device_mode < config->total_dev_mode) {
45 		int itr = 0;
46 
47 		for (itr = 0; itr < config->total_dev_mode; itr++) {
48 			if (context->sar_data.device_mode ==
49 				config->device_mode_info[itr].device_mode) {
50 				struct wwan_device_mode_info *dev_mode =
51 				&config->device_mode_info[itr];
52 
53 				context->sar_data.antennatable_index = dev_mode->antennatable_index;
54 				context->sar_data.bandtable_index = dev_mode->bandtable_index;
55 				context->sar_data.sartable_index = dev_mode->sartable_index;
56 				break;
57 			}
58 		}
59 	}
60 }
61 
62 /**
63  * parse_package: parse acpi package for retrieving SAR information
64  * @context: pointer to driver context structure
65  * @item : acpi_object pointer
66  *
67  * Given acpi_object is iterated to retrieve information for each device mode.
68  * If a given package corresponding to a specific device mode is faulty, it is
69  * skipped and the specific entry in context structure will have the default value
70  * of zero. Decoding of subsequent device modes is realized by having "continue"
71  * statements in the for loop on encountering error in parsing given device mode.
72  *
73  * Return:
74  * AE_OK if success
75  * AE_ERROR on error
76  */
parse_package(struct wwan_sar_context * context,union acpi_object * item)77 static acpi_status parse_package(struct wwan_sar_context *context, union acpi_object *item)
78 {
79 	struct wwan_device_mode_configuration *data;
80 	int value, itr, reg;
81 	union acpi_object *num;
82 
83 	num = &item->package.elements[0];
84 	if (get_int_value(num, &value) || value < 0 || value >= MAX_REGULATORY)
85 		return AE_ERROR;
86 
87 	reg = value;
88 
89 	data = &context->config_data[reg];
90 	if (data->total_dev_mode > MAX_DEV_MODES ||	data->total_dev_mode == 0 ||
91 	    item->package.count <= data->total_dev_mode)
92 		return AE_ERROR;
93 
94 	data->device_mode_info = devm_kcalloc(&context->sar_device->dev,
95 					      data->total_dev_mode,
96 					      sizeof(*data->device_mode_info),
97 					      GFP_KERNEL);
98 	if (!data->device_mode_info)
99 		return AE_ERROR;
100 
101 	for (itr = 0; itr < data->total_dev_mode; itr++) {
102 		struct wwan_device_mode_info temp = { 0 };
103 
104 		num = &item->package.elements[itr + 1];
105 		if (num->type != ACPI_TYPE_PACKAGE || num->package.count < TOTAL_DATA)
106 			continue;
107 		if (get_int_value(&num->package.elements[0], &temp.device_mode))
108 			continue;
109 		if (get_int_value(&num->package.elements[1], &temp.bandtable_index))
110 			continue;
111 		if (get_int_value(&num->package.elements[2], &temp.antennatable_index))
112 			continue;
113 		if (get_int_value(&num->package.elements[3], &temp.sartable_index))
114 			continue;
115 		data->device_mode_info[itr] = temp;
116 	}
117 	return AE_OK;
118 }
119 
120 /**
121  * sar_get_device_mode: Extraction of information from BIOS via DSM calls
122  * @device: ACPI device for which to retrieve the data
123  *
124  * Retrieve the current device mode information from the BIOS.
125  *
126  * Return:
127  * AE_OK on success
128  * AE_ERROR on error
129  */
sar_get_device_mode(struct platform_device * device)130 static acpi_status sar_get_device_mode(struct platform_device *device)
131 {
132 	struct wwan_sar_context *context = dev_get_drvdata(&device->dev);
133 	acpi_status status = AE_OK;
134 	union acpi_object *out;
135 	u32 rev = 0;
136 
137 	out = acpi_evaluate_dsm_typed(context->handle, &context->guid, rev,
138 				      COMMAND_ID_DEV_MODE, NULL, ACPI_TYPE_INTEGER);
139 	if (!out) {
140 		dev_err(&device->dev, "DSM cmd:%d Failed to retrieve value\n", COMMAND_ID_DEV_MODE);
141 		status = AE_ERROR;
142 		goto dev_mode_error;
143 	}
144 	context->sar_data.device_mode = out->integer.value;
145 	update_sar_data(context);
146 	sysfs_notify(&device->dev.kobj, NULL, SYSFS_DATANAME);
147 
148 dev_mode_error:
149 	ACPI_FREE(out);
150 	return status;
151 }
152 
153 static const struct acpi_device_id sar_device_ids[] = {
154 	{ "INTC1092", 0},
155 	{}
156 };
157 MODULE_DEVICE_TABLE(acpi, sar_device_ids);
158 
intc_data_show(struct device * dev,struct device_attribute * attr,char * buf)159 static ssize_t intc_data_show(struct device *dev, struct device_attribute *attr, char *buf)
160 {
161 	struct wwan_sar_context *context = dev_get_drvdata(dev);
162 
163 	return sysfs_emit(buf, "%d %d %d %d\n", context->sar_data.device_mode,
164 		      context->sar_data.bandtable_index,
165 		      context->sar_data.antennatable_index,
166 		      context->sar_data.sartable_index);
167 }
168 static DEVICE_ATTR_RO(intc_data);
169 
intc_reg_show(struct device * dev,struct device_attribute * attr,char * buf)170 static ssize_t intc_reg_show(struct device *dev, struct device_attribute *attr, char *buf)
171 {
172 	struct wwan_sar_context *context = dev_get_drvdata(dev);
173 
174 	return sysfs_emit(buf, "%d\n", context->reg_value);
175 }
176 
intc_reg_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)177 static ssize_t intc_reg_store(struct device *dev, struct device_attribute *attr,
178 			      const char *buf, size_t count)
179 {
180 	struct wwan_sar_context *context = dev_get_drvdata(dev);
181 	unsigned int value;
182 	int read;
183 
184 	if (!count)
185 		return -EINVAL;
186 	read = kstrtouint(buf, 10, &value);
187 	if (read < 0)
188 		return read;
189 	if (value >= MAX_REGULATORY)
190 		return -EOVERFLOW;
191 	context->reg_value = value;
192 	update_sar_data(context);
193 	sysfs_notify(&dev->kobj, NULL, SYSFS_DATANAME);
194 	return count;
195 }
196 static DEVICE_ATTR_RW(intc_reg);
197 
198 static struct attribute *intcsar_attrs[] = {
199 	&dev_attr_intc_data.attr,
200 	&dev_attr_intc_reg.attr,
201 	NULL
202 };
203 
204 static struct attribute_group intcsar_group = {
205 	.attrs = intcsar_attrs,
206 };
207 
sar_notify(acpi_handle handle,u32 event,void * data)208 static void sar_notify(acpi_handle handle, u32 event, void *data)
209 {
210 	struct platform_device *device = data;
211 
212 	if (event == SAR_EVENT) {
213 		if (sar_get_device_mode(device) != AE_OK)
214 			dev_err(&device->dev, "sar_get_device_mode error");
215 	}
216 }
217 
sar_get_data(int reg,struct wwan_sar_context * context)218 static void sar_get_data(int reg, struct wwan_sar_context *context)
219 {
220 	union acpi_object *out, req;
221 	u32 rev = 0;
222 
223 	req.type = ACPI_TYPE_INTEGER;
224 	req.integer.value = reg;
225 	out = acpi_evaluate_dsm_typed(context->handle, &context->guid, rev,
226 				      COMMAND_ID_CONFIG_TABLE, &req, ACPI_TYPE_PACKAGE);
227 	if (!out)
228 		return;
229 	if (out->package.count >= 3 &&
230 	    out->package.elements[0].type == ACPI_TYPE_INTEGER &&
231 	    out->package.elements[1].type == ACPI_TYPE_INTEGER &&
232 	    out->package.elements[2].type == ACPI_TYPE_PACKAGE &&
233 	    out->package.elements[2].package.count > 0) {
234 		context->config_data[reg].version = out->package.elements[0].integer.value;
235 		context->config_data[reg].total_dev_mode =
236 			out->package.elements[1].integer.value;
237 		if (context->config_data[reg].total_dev_mode <= 0 ||
238 		    context->config_data[reg].total_dev_mode > MAX_DEV_MODES) {
239 			ACPI_FREE(out);
240 			return;
241 		}
242 		parse_package(context, &out->package.elements[2]);
243 	}
244 	ACPI_FREE(out);
245 }
246 
sar_probe(struct platform_device * device)247 static int sar_probe(struct platform_device *device)
248 {
249 	struct wwan_sar_context *context;
250 	acpi_handle handle;
251 	int reg;
252 	int result;
253 
254 	handle = ACPI_HANDLE(&device->dev);
255 	if (!handle)
256 		return -ENODEV;
257 
258 	context = devm_kzalloc(&device->dev, sizeof(*context), GFP_KERNEL);
259 	if (!context)
260 		return -ENOMEM;
261 
262 	context->sar_device = device;
263 	context->handle = handle;
264 	dev_set_drvdata(&device->dev, context);
265 
266 	result = guid_parse(SAR_DSM_UUID, &context->guid);
267 	if (result) {
268 		dev_err(&device->dev, "SAR UUID parse error: %d\n", result);
269 		return result;
270 	}
271 
272 	for (reg = 0; reg < MAX_REGULATORY; reg++)
273 		sar_get_data(reg, context);
274 
275 	if (sar_get_device_mode(device) != AE_OK) {
276 		dev_err(&device->dev, "Failed to get device mode\n");
277 		return -EIO;
278 	}
279 
280 	result = sysfs_create_group(&device->dev.kobj, &intcsar_group);
281 	if (result) {
282 		dev_err(&device->dev, "sysfs creation failed\n");
283 		return result;
284 	}
285 
286 	if (acpi_install_notify_handler(ACPI_HANDLE(&device->dev), ACPI_DEVICE_NOTIFY,
287 					sar_notify, (void *)device) != AE_OK) {
288 		dev_err(&device->dev, "Failed acpi_install_notify_handler\n");
289 		sysfs_remove_group(&device->dev.kobj, &intcsar_group);
290 		return -EIO;
291 	}
292 	return 0;
293 }
294 
sar_remove(struct platform_device * device)295 static void sar_remove(struct platform_device *device)
296 {
297 	acpi_remove_notify_handler(ACPI_HANDLE(&device->dev),
298 				   ACPI_DEVICE_NOTIFY, sar_notify);
299 	sysfs_remove_group(&device->dev.kobj, &intcsar_group);
300 }
301 
302 static struct platform_driver sar_driver = {
303 	.probe = sar_probe,
304 	.remove = sar_remove,
305 	.driver = {
306 		.name = DRVNAME,
307 		.acpi_match_table = ACPI_PTR(sar_device_ids)
308 	}
309 };
310 module_platform_driver(sar_driver);
311 
312 MODULE_LICENSE("GPL v2");
313 MODULE_DESCRIPTION("Platform device driver for INTEL MODEM BIOS SAR");
314 MODULE_AUTHOR("Shravan Sudhakar <s.shravan@intel.com>");
315