1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2021, Intel Corporation.
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"
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
17 * Function is used to retrieve integer value from acpi object.
21 * * -EIO if there is an issue in acpi_object passed.
23 static int get_int_value(union acpi_object *obj, int *out)
25 if (!obj || obj->type != ACPI_TYPE_INTEGER)
27 *out = (int)obj->integer.value;
32 * update_sar_data: sar data is updated based on regulatory mode
33 * @context: pointer to driver context structure
35 * sar_data is updated based on regulatory value
36 * context->reg_value will never exceed MAX_REGULATORY
38 static void update_sar_data(struct wwan_sar_context *context)
40 struct wwan_device_mode_configuration *config =
41 &context->config_data[context->reg_value];
43 if (config->device_mode_info &&
44 context->sar_data.device_mode < config->total_dev_mode) {
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];
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;
63 * parse_package: parse acpi package for retrieving SAR information
64 * @context: pointer to driver context structure
65 * @item : acpi_object pointer
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.
77 static acpi_status parse_package(struct wwan_sar_context *context, union acpi_object *item)
79 struct wwan_device_mode_configuration *data;
81 union acpi_object *num;
83 num = &item->package.elements[0];
84 if (get_int_value(num, &value) || value < 0 || value >= MAX_REGULATORY)
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)
94 data->device_mode_info = kmalloc_array(data->total_dev_mode,
95 sizeof(struct wwan_device_mode_info), GFP_KERNEL);
96 if (!data->device_mode_info)
99 for (itr = 0; itr < data->total_dev_mode; itr++) {
100 struct wwan_device_mode_info temp = { 0 };
102 num = &item->package.elements[itr + 1];
103 if (num->type != ACPI_TYPE_PACKAGE || num->package.count < TOTAL_DATA)
105 if (get_int_value(&num->package.elements[0], &temp.device_mode))
107 if (get_int_value(&num->package.elements[1], &temp.bandtable_index))
109 if (get_int_value(&num->package.elements[2], &temp.antennatable_index))
111 if (get_int_value(&num->package.elements[3], &temp.sartable_index))
113 data->device_mode_info[itr] = temp;
119 * sar_get_device_mode: Extraction of information from BIOS via DSM calls
120 * @device: ACPI device for which to retrieve the data
122 * Retrieve the current device mode information from the BIOS.
128 static acpi_status sar_get_device_mode(struct platform_device *device)
130 struct wwan_sar_context *context = dev_get_drvdata(&device->dev);
131 acpi_status status = AE_OK;
132 union acpi_object *out;
136 out = acpi_evaluate_dsm(context->handle, &context->guid, rev,
137 COMMAND_ID_DEV_MODE, NULL);
138 if (get_int_value(out, &value)) {
139 dev_err(&device->dev, "DSM cmd:%d Failed to retrieve value\n", COMMAND_ID_DEV_MODE);
143 context->sar_data.device_mode = value;
144 update_sar_data(context);
145 sysfs_notify(&device->dev.kobj, NULL, SYSFS_DATANAME);
152 static const struct acpi_device_id sar_device_ids[] = {
156 MODULE_DEVICE_TABLE(acpi, sar_device_ids);
158 static ssize_t intc_data_show(struct device *dev, struct device_attribute *attr, char *buf)
160 struct wwan_sar_context *context = dev_get_drvdata(dev);
162 return sysfs_emit(buf, "%d %d %d %d\n", context->sar_data.device_mode,
163 context->sar_data.bandtable_index,
164 context->sar_data.antennatable_index,
165 context->sar_data.sartable_index);
167 static DEVICE_ATTR_RO(intc_data);
169 static ssize_t intc_reg_show(struct device *dev, struct device_attribute *attr, char *buf)
171 struct wwan_sar_context *context = dev_get_drvdata(dev);
173 return sysfs_emit(buf, "%d\n", context->reg_value);
176 static ssize_t intc_reg_store(struct device *dev, struct device_attribute *attr,
177 const char *buf, size_t count)
179 struct wwan_sar_context *context = dev_get_drvdata(dev);
185 read = kstrtouint(buf, 10, &value);
188 if (value >= MAX_REGULATORY)
190 context->reg_value = value;
191 update_sar_data(context);
192 sysfs_notify(&dev->kobj, NULL, SYSFS_DATANAME);
195 static DEVICE_ATTR_RW(intc_reg);
197 static struct attribute *intcsar_attrs[] = {
198 &dev_attr_intc_data.attr,
199 &dev_attr_intc_reg.attr,
203 static struct attribute_group intcsar_group = {
204 .attrs = intcsar_attrs,
207 static void sar_notify(acpi_handle handle, u32 event, void *data)
209 struct platform_device *device = data;
211 if (event == SAR_EVENT) {
212 if (sar_get_device_mode(device) != AE_OK)
213 dev_err(&device->dev, "sar_get_device_mode error");
217 static void sar_get_data(int reg, struct wwan_sar_context *context)
219 union acpi_object *out, req;
222 req.type = ACPI_TYPE_INTEGER;
223 req.integer.value = reg;
224 out = acpi_evaluate_dsm(context->handle, &context->guid, rev,
225 COMMAND_ID_CONFIG_TABLE, &req);
228 if (out->type == ACPI_TYPE_PACKAGE && out->package.count >= 3 &&
229 out->package.elements[0].type == ACPI_TYPE_INTEGER &&
230 out->package.elements[1].type == ACPI_TYPE_INTEGER &&
231 out->package.elements[2].type == ACPI_TYPE_PACKAGE &&
232 out->package.elements[2].package.count > 0) {
233 context->config_data[reg].version = out->package.elements[0].integer.value;
234 context->config_data[reg].total_dev_mode =
235 out->package.elements[1].integer.value;
236 if (context->config_data[reg].total_dev_mode <= 0 ||
237 context->config_data[reg].total_dev_mode > MAX_DEV_MODES) {
241 parse_package(context, &out->package.elements[2]);
246 static int sar_probe(struct platform_device *device)
248 struct wwan_sar_context *context;
252 context = kzalloc(sizeof(*context), GFP_KERNEL);
256 context->sar_device = device;
257 context->handle = ACPI_HANDLE(&device->dev);
258 dev_set_drvdata(&device->dev, context);
260 result = guid_parse(SAR_DSM_UUID, &context->guid);
262 dev_err(&device->dev, "SAR UUID parse error: %d\n", result);
266 for (reg = 0; reg < MAX_REGULATORY; reg++)
267 sar_get_data(reg, context);
269 if (sar_get_device_mode(device) != AE_OK) {
270 dev_err(&device->dev, "Failed to get device mode\n");
275 result = sysfs_create_group(&device->dev.kobj, &intcsar_group);
277 dev_err(&device->dev, "sysfs creation failed\n");
281 if (acpi_install_notify_handler(ACPI_HANDLE(&device->dev), ACPI_DEVICE_NOTIFY,
282 sar_notify, (void *)device) != AE_OK) {
283 dev_err(&device->dev, "Failed acpi_install_notify_handler\n");
290 sysfs_remove_group(&device->dev.kobj, &intcsar_group);
296 static int sar_remove(struct platform_device *device)
298 struct wwan_sar_context *context = dev_get_drvdata(&device->dev);
301 acpi_remove_notify_handler(ACPI_HANDLE(&device->dev),
302 ACPI_DEVICE_NOTIFY, sar_notify);
303 sysfs_remove_group(&device->dev.kobj, &intcsar_group);
304 for (reg = 0; reg < MAX_REGULATORY; reg++)
305 kfree(context->config_data[reg].device_mode_info);
311 static struct platform_driver sar_driver = {
313 .remove = sar_remove,
316 .acpi_match_table = ACPI_PTR(sar_device_ids)
319 module_platform_driver(sar_driver);
321 MODULE_LICENSE("GPL v2");
322 MODULE_DESCRIPTION("Platform device driver for INTEL MODEM BIOS SAR");