xref: /linux/drivers/platform/x86/intel/int3472/common.c (revision 84bbfe6b6435658132df2880258d34babe46d3e0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Author: Dan Scally <djrscally@gmail.com> */
3 
4 #include <linux/acpi.h>
5 #include <linux/slab.h>
6 
7 #include "common.h"
8 
9 union acpi_object *skl_int3472_get_acpi_buffer(struct acpi_device *adev, char *id)
10 {
11 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
12 	acpi_handle handle = adev->handle;
13 	union acpi_object *obj;
14 	acpi_status status;
15 
16 	status = acpi_evaluate_object(handle, id, NULL, &buffer);
17 	if (ACPI_FAILURE(status))
18 		return ERR_PTR(-ENODEV);
19 
20 	obj = buffer.pointer;
21 	if (!obj)
22 		return ERR_PTR(-ENODEV);
23 
24 	if (obj->type != ACPI_TYPE_BUFFER) {
25 		acpi_handle_err(handle, "%s object is not an ACPI buffer\n", id);
26 		kfree(obj);
27 		return ERR_PTR(-EINVAL);
28 	}
29 
30 	return obj;
31 }
32 EXPORT_SYMBOL_GPL(skl_int3472_get_acpi_buffer);
33 
34 int skl_int3472_fill_cldb(struct acpi_device *adev, struct int3472_cldb *cldb)
35 {
36 	union acpi_object *obj;
37 	int ret;
38 
39 	obj = skl_int3472_get_acpi_buffer(adev, "CLDB");
40 	if (IS_ERR(obj))
41 		return PTR_ERR(obj);
42 
43 	if (obj->buffer.length > sizeof(*cldb)) {
44 		acpi_handle_err(adev->handle, "The CLDB buffer is too large\n");
45 		ret = -EINVAL;
46 		goto out_free_obj;
47 	}
48 
49 	memcpy(cldb, obj->buffer.pointer, obj->buffer.length);
50 	ret = 0;
51 
52 out_free_obj:
53 	kfree(obj);
54 	return ret;
55 }
56 EXPORT_SYMBOL_GPL(skl_int3472_fill_cldb);
57 
58 /* sensor_adev_ret may be NULL, name_ret must not be NULL */
59 int skl_int3472_get_sensor_adev_and_name(struct device *dev,
60 					 struct acpi_device **sensor_adev_ret,
61 					 const char **name_ret)
62 {
63 	struct acpi_device *adev = ACPI_COMPANION(dev);
64 	struct acpi_device *sensor;
65 	int ret = 0;
66 
67 	sensor = acpi_dev_get_next_consumer_dev(adev, NULL);
68 	if (!sensor) {
69 		dev_err(dev, "INT3472 seems to have no dependents.\n");
70 		return -ENODEV;
71 	}
72 
73 	*name_ret = devm_kasprintf(dev, GFP_KERNEL, I2C_DEV_NAME_FORMAT,
74 				   acpi_dev_name(sensor));
75 	if (!*name_ret)
76 		ret = -ENOMEM;
77 
78 	if (ret == 0 && sensor_adev_ret)
79 		*sensor_adev_ret = sensor;
80 	else
81 		acpi_dev_put(sensor);
82 
83 	return ret;
84 }
85 EXPORT_SYMBOL_GPL(skl_int3472_get_sensor_adev_and_name);
86 
87 MODULE_DESCRIPTION("Intel SkyLake INT3472 ACPI Device Driver library");
88 MODULE_AUTHOR("Daniel Scally <djrscally@gmail.com>");
89 MODULE_LICENSE("GPL");
90