brintos

brintos / linux-shallow public Read only

0
0
Text · 8.9 KiB · 6246c06 Raw
322 lines · c
1// SPDX-License-Identifier: GPL-2.0-only2/*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 Object14 * @obj: acpi_object pointer which has the integer value15 * @out: output pointer will get integer value16 *17 * Function is used to retrieve integer value from acpi object.18 *19 * Return:20 * * 0 on success21 * * -EIO if there is an issue in acpi_object passed.22 */23static 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 mode33 * @context: pointer to driver context structure34 *35 * sar_data is updated based on regulatory value36 * context->reg_value will never exceed MAX_REGULATORY37 */38static 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 information64 * @context: pointer to driver context structure65 * @item : acpi_object pointer66 *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 is69 * skipped and the specific entry in context structure will have the default value70 * 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 success75 * AE_ERROR on error76 */77static 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 = kmalloc_array(data->total_dev_mode,95					       sizeof(struct wwan_device_mode_info), GFP_KERNEL);96	if (!data->device_mode_info)97		return AE_ERROR;98 99	for (itr = 0; itr < data->total_dev_mode; itr++) {100		struct wwan_device_mode_info temp = { 0 };101 102		num = &item->package.elements[itr + 1];103		if (num->type != ACPI_TYPE_PACKAGE || num->package.count < TOTAL_DATA)104			continue;105		if (get_int_value(&num->package.elements[0], &temp.device_mode))106			continue;107		if (get_int_value(&num->package.elements[1], &temp.bandtable_index))108			continue;109		if (get_int_value(&num->package.elements[2], &temp.antennatable_index))110			continue;111		if (get_int_value(&num->package.elements[3], &temp.sartable_index))112			continue;113		data->device_mode_info[itr] = temp;114	}115	return AE_OK;116}117 118/**119 * sar_get_device_mode: Extraction of information from BIOS via DSM calls120 * @device: ACPI device for which to retrieve the data121 *122 * Retrieve the current device mode information from the BIOS.123 *124 * Return:125 * AE_OK on success126 * AE_ERROR on error127 */128static acpi_status sar_get_device_mode(struct platform_device *device)129{130	struct wwan_sar_context *context = dev_get_drvdata(&device->dev);131	acpi_status status = AE_OK;132	union acpi_object *out;133	u32 rev = 0;134 135	out = acpi_evaluate_dsm_typed(context->handle, &context->guid, rev,136				      COMMAND_ID_DEV_MODE, NULL, ACPI_TYPE_INTEGER);137	if (!out) {138		dev_err(&device->dev, "DSM cmd:%d Failed to retrieve value\n", COMMAND_ID_DEV_MODE);139		status = AE_ERROR;140		goto dev_mode_error;141	}142	context->sar_data.device_mode = out->integer.value;143	update_sar_data(context);144	sysfs_notify(&device->dev.kobj, NULL, SYSFS_DATANAME);145 146dev_mode_error:147	ACPI_FREE(out);148	return status;149}150 151static const struct acpi_device_id sar_device_ids[] = {152	{ "INTC1092", 0},153	{}154};155MODULE_DEVICE_TABLE(acpi, sar_device_ids);156 157static ssize_t intc_data_show(struct device *dev, struct device_attribute *attr, char *buf)158{159	struct wwan_sar_context *context = dev_get_drvdata(dev);160 161	return sysfs_emit(buf, "%d %d %d %d\n", context->sar_data.device_mode,162		      context->sar_data.bandtable_index,163		      context->sar_data.antennatable_index,164		      context->sar_data.sartable_index);165}166static DEVICE_ATTR_RO(intc_data);167 168static ssize_t intc_reg_show(struct device *dev, struct device_attribute *attr, char *buf)169{170	struct wwan_sar_context *context = dev_get_drvdata(dev);171 172	return sysfs_emit(buf, "%d\n", context->reg_value);173}174 175static ssize_t intc_reg_store(struct device *dev, struct device_attribute *attr,176			      const char *buf, size_t count)177{178	struct wwan_sar_context *context = dev_get_drvdata(dev);179	unsigned int value;180	int read;181 182	if (!count)183		return -EINVAL;184	read = kstrtouint(buf, 10, &value);185	if (read < 0)186		return read;187	if (value >= MAX_REGULATORY)188		return -EOVERFLOW;189	context->reg_value = value;190	update_sar_data(context);191	sysfs_notify(&dev->kobj, NULL, SYSFS_DATANAME);192	return count;193}194static DEVICE_ATTR_RW(intc_reg);195 196static struct attribute *intcsar_attrs[] = {197	&dev_attr_intc_data.attr,198	&dev_attr_intc_reg.attr,199	NULL200};201 202static struct attribute_group intcsar_group = {203	.attrs = intcsar_attrs,204};205 206static void sar_notify(acpi_handle handle, u32 event, void *data)207{208	struct platform_device *device = data;209 210	if (event == SAR_EVENT) {211		if (sar_get_device_mode(device) != AE_OK)212			dev_err(&device->dev, "sar_get_device_mode error");213	}214}215 216static void sar_get_data(int reg, struct wwan_sar_context *context)217{218	union acpi_object *out, req;219	u32 rev = 0;220 221	req.type = ACPI_TYPE_INTEGER;222	req.integer.value = reg;223	out = acpi_evaluate_dsm_typed(context->handle, &context->guid, rev,224				      COMMAND_ID_CONFIG_TABLE, &req, ACPI_TYPE_PACKAGE);225	if (!out)226		return;227	if (out->package.count >= 3 &&228	    out->package.elements[0].type == ACPI_TYPE_INTEGER &&229	    out->package.elements[1].type == ACPI_TYPE_INTEGER &&230	    out->package.elements[2].type == ACPI_TYPE_PACKAGE &&231	    out->package.elements[2].package.count > 0) {232		context->config_data[reg].version = out->package.elements[0].integer.value;233		context->config_data[reg].total_dev_mode =234			out->package.elements[1].integer.value;235		if (context->config_data[reg].total_dev_mode <= 0 ||236		    context->config_data[reg].total_dev_mode > MAX_DEV_MODES) {237			ACPI_FREE(out);238			return;239		}240		parse_package(context, &out->package.elements[2]);241	}242	ACPI_FREE(out);243}244 245static int sar_probe(struct platform_device *device)246{247	struct wwan_sar_context *context;248	int reg;249	int result;250 251	context = kzalloc(sizeof(*context), GFP_KERNEL);252	if (!context)253		return -ENOMEM;254 255	context->sar_device = device;256	context->handle = ACPI_HANDLE(&device->dev);257	dev_set_drvdata(&device->dev, context);258 259	result = guid_parse(SAR_DSM_UUID, &context->guid);260	if (result) {261		dev_err(&device->dev, "SAR UUID parse error: %d\n", result);262		goto r_free;263	}264 265	for (reg = 0; reg < MAX_REGULATORY; reg++)266		sar_get_data(reg, context);267 268	if (sar_get_device_mode(device) != AE_OK) {269		dev_err(&device->dev, "Failed to get device mode\n");270		result = -EIO;271		goto r_free;272	}273 274	result = sysfs_create_group(&device->dev.kobj, &intcsar_group);275	if (result) {276		dev_err(&device->dev, "sysfs creation failed\n");277		goto r_free;278	}279 280	if (acpi_install_notify_handler(ACPI_HANDLE(&device->dev), ACPI_DEVICE_NOTIFY,281					sar_notify, (void *)device) != AE_OK) {282		dev_err(&device->dev, "Failed acpi_install_notify_handler\n");283		result = -EIO;284		goto r_sys;285	}286	return 0;287 288r_sys:289	sysfs_remove_group(&device->dev.kobj, &intcsar_group);290r_free:291	kfree(context);292	return result;293}294 295static void sar_remove(struct platform_device *device)296{297	struct wwan_sar_context *context = dev_get_drvdata(&device->dev);298	int reg;299 300	acpi_remove_notify_handler(ACPI_HANDLE(&device->dev),301				   ACPI_DEVICE_NOTIFY, sar_notify);302	sysfs_remove_group(&device->dev.kobj, &intcsar_group);303	for (reg = 0; reg < MAX_REGULATORY; reg++)304		kfree(context->config_data[reg].device_mode_info);305 306	kfree(context);307}308 309static struct platform_driver sar_driver = {310	.probe = sar_probe,311	.remove_new = sar_remove,312	.driver = {313		.name = DRVNAME,314		.acpi_match_table = ACPI_PTR(sar_device_ids)315	}316};317module_platform_driver(sar_driver);318 319MODULE_LICENSE("GPL v2");320MODULE_DESCRIPTION("Platform device driver for INTEL MODEM BIOS SAR");321MODULE_AUTHOR("Shravan Sudhakar <s.shravan@intel.com>");322