1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ChromeOS specific ACPI extensions
4  *
5  * Copyright 2022 Google LLC
6  *
7  * This driver attaches to the ChromeOS ACPI device and then exports the
8  * values reported by the ACPI in a sysfs directory. All values are
9  * presented in the string form (numbers as decimal values) and can be
10  * accessed as the contents of the appropriate read only files in the
11  * sysfs directory tree.
12  */
13 #include <linux/acpi.h>
14 #include <linux/platform_device.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/module.h>
18 
19 #define ACPI_ATTR_NAME_LEN 4
20 
21 #define DEV_ATTR(_var, _name)					\
22 	static struct device_attribute dev_attr_##_var =	\
23 		__ATTR(_name, 0444, chromeos_first_level_attr_show, NULL);
24 
25 #define GPIO_ATTR_GROUP(_group, _name, _num)						\
26 	static umode_t attr_is_visible_gpio_##_num(struct kobject *kobj,		\
27 						   struct attribute *attr, int n)	\
28 	{										\
29 		if (_num < chromeos_acpi_gpio_groups)					\
30 			return attr->mode;						\
31 		return 0;								\
32 	}										\
33 	static ssize_t chromeos_attr_show_gpio_##_num(struct device *dev,		\
34 						      struct device_attribute *attr,	\
35 						      char *buf)			\
36 	{										\
37 		char name[ACPI_ATTR_NAME_LEN + 1];					\
38 		int ret, num;								\
39 											\
40 		ret = parse_attr_name(attr->attr.name, name, &num);			\
41 		if (ret)								\
42 			return ret;							\
43 		return chromeos_acpi_evaluate_method(dev, _num, num, name, buf);	\
44 	}										\
45 	static struct device_attribute dev_attr_0_##_group =				\
46 		__ATTR(GPIO.0, 0444, chromeos_attr_show_gpio_##_num, NULL);		\
47 	static struct device_attribute dev_attr_1_##_group =				\
48 		__ATTR(GPIO.1, 0444, chromeos_attr_show_gpio_##_num, NULL);		\
49 	static struct device_attribute dev_attr_2_##_group =				\
50 		__ATTR(GPIO.2, 0444, chromeos_attr_show_gpio_##_num, NULL);		\
51 	static struct device_attribute dev_attr_3_##_group =				\
52 		__ATTR(GPIO.3, 0444, chromeos_attr_show_gpio_##_num, NULL);		\
53 											\
54 	static struct attribute *attrs_##_group[] = {					\
55 		&dev_attr_0_##_group.attr,						\
56 		&dev_attr_1_##_group.attr,						\
57 		&dev_attr_2_##_group.attr,						\
58 		&dev_attr_3_##_group.attr,						\
59 		NULL									\
60 	};										\
61 	static const struct attribute_group attr_group_##_group = {			\
62 		.name = _name,								\
63 		.is_visible = attr_is_visible_gpio_##_num,				\
64 		.attrs = attrs_##_group,						\
65 	};
66 
67 static unsigned int chromeos_acpi_gpio_groups;
68 
69 /* Parse the ACPI package and return the data related to that attribute */
70 static int chromeos_acpi_handle_package(struct device *dev, union acpi_object *obj,
71 					int pkg_num, int sub_pkg_num, char *name, char *buf)
72 {
73 	union acpi_object *element = obj->package.elements;
74 
75 	if (pkg_num >= obj->package.count)
76 		return -EINVAL;
77 	element += pkg_num;
78 
79 	if (element->type == ACPI_TYPE_PACKAGE) {
80 		if (sub_pkg_num >= element->package.count)
81 			return -EINVAL;
82 		/* select sub element inside this package */
83 		element = element->package.elements;
84 		element += sub_pkg_num;
85 	}
86 
87 	switch (element->type) {
88 	case ACPI_TYPE_INTEGER:
89 		return sysfs_emit(buf, "%d\n", (int)element->integer.value);
90 	case ACPI_TYPE_STRING:
91 		return sysfs_emit(buf, "%s\n", element->string.pointer);
92 	case ACPI_TYPE_BUFFER:
93 		return sysfs_emit(buf, "%s\n", element->buffer.pointer);
94 	default:
95 		dev_err(dev, "element type %d not supported\n", element->type);
96 		return -EINVAL;
97 	}
98 }
99 
100 static int chromeos_acpi_evaluate_method(struct device *dev, int pkg_num, int sub_pkg_num,
101 					 char *name, char *buf)
102 {
103 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
104 	acpi_status status;
105 	int ret = -EINVAL;
106 
107 	status = acpi_evaluate_object(ACPI_HANDLE(dev), name, NULL, &output);
108 	if (ACPI_FAILURE(status)) {
109 		dev_err(dev, "failed to retrieve %s. %s\n", name, acpi_format_exception(status));
110 		return ret;
111 	}
112 
113 	if (((union acpi_object *)output.pointer)->type == ACPI_TYPE_PACKAGE)
114 		ret = chromeos_acpi_handle_package(dev, output.pointer, pkg_num, sub_pkg_num,
115 						   name, buf);
116 
117 	kfree(output.pointer);
118 	return ret;
119 }
120 
121 static int parse_attr_name(const char *name, char *attr_name, int *attr_num)
122 {
123 	int ret;
124 
125 	ret = strscpy(attr_name, name, ACPI_ATTR_NAME_LEN + 1);
126 	if (ret == -E2BIG)
127 		return kstrtoint(&name[ACPI_ATTR_NAME_LEN + 1], 0, attr_num);
128 	return 0;
129 }
130 
131 static ssize_t chromeos_first_level_attr_show(struct device *dev, struct device_attribute *attr,
132 					      char *buf)
133 {
134 	char attr_name[ACPI_ATTR_NAME_LEN + 1];
135 	int ret, attr_num = 0;
136 
137 	ret = parse_attr_name(attr->attr.name, attr_name, &attr_num);
138 	if (ret)
139 		return ret;
140 	return chromeos_acpi_evaluate_method(dev, attr_num, 0, attr_name, buf);
141 }
142 
143 static unsigned int get_gpio_pkg_num(struct device *dev)
144 {
145 	struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
146 	union acpi_object *obj;
147 	acpi_status status;
148 	unsigned int count = 0;
149 	char *name = "GPIO";
150 
151 	status = acpi_evaluate_object(ACPI_HANDLE(dev), name, NULL, &output);
152 	if (ACPI_FAILURE(status)) {
153 		dev_err(dev, "failed to retrieve %s. %s\n", name, acpi_format_exception(status));
154 		return count;
155 	}
156 
157 	obj = output.pointer;
158 
159 	if (obj->type == ACPI_TYPE_PACKAGE)
160 		count = obj->package.count;
161 
162 	kfree(output.pointer);
163 	return count;
164 }
165 
166 DEV_ATTR(binf2, BINF.2)
167 DEV_ATTR(binf3, BINF.3)
168 DEV_ATTR(chsw, CHSW)
169 DEV_ATTR(fmap, FMAP)
170 DEV_ATTR(frid, FRID)
171 DEV_ATTR(fwid, FWID)
172 DEV_ATTR(hwid, HWID)
173 DEV_ATTR(meck, MECK)
174 DEV_ATTR(vbnv0, VBNV.0)
175 DEV_ATTR(vbnv1, VBNV.1)
176 DEV_ATTR(vdat, VDAT)
177 
178 static struct attribute *first_level_attrs[] = {
179 	&dev_attr_binf2.attr,
180 	&dev_attr_binf3.attr,
181 	&dev_attr_chsw.attr,
182 	&dev_attr_fmap.attr,
183 	&dev_attr_frid.attr,
184 	&dev_attr_fwid.attr,
185 	&dev_attr_hwid.attr,
186 	&dev_attr_meck.attr,
187 	&dev_attr_vbnv0.attr,
188 	&dev_attr_vbnv1.attr,
189 	&dev_attr_vdat.attr,
190 	NULL
191 };
192 
193 static const struct attribute_group first_level_attr_group = {
194 	.attrs = first_level_attrs,
195 };
196 
197 /*
198  * Every platform can have a different number of GPIO attribute groups.
199  * Define upper limit groups. At run time, the platform decides to show
200  * the present number of groups only, others are hidden.
201  */
202 GPIO_ATTR_GROUP(gpio0, "GPIO.0", 0)
203 GPIO_ATTR_GROUP(gpio1, "GPIO.1", 1)
204 GPIO_ATTR_GROUP(gpio2, "GPIO.2", 2)
205 GPIO_ATTR_GROUP(gpio3, "GPIO.3", 3)
206 GPIO_ATTR_GROUP(gpio4, "GPIO.4", 4)
207 GPIO_ATTR_GROUP(gpio5, "GPIO.5", 5)
208 GPIO_ATTR_GROUP(gpio6, "GPIO.6", 6)
209 GPIO_ATTR_GROUP(gpio7, "GPIO.7", 7)
210 
211 static const struct attribute_group *chromeos_acpi_all_groups[] = {
212 	&first_level_attr_group,
213 	&attr_group_gpio0,
214 	&attr_group_gpio1,
215 	&attr_group_gpio2,
216 	&attr_group_gpio3,
217 	&attr_group_gpio4,
218 	&attr_group_gpio5,
219 	&attr_group_gpio6,
220 	&attr_group_gpio7,
221 	NULL
222 };
223 
224 static int chromeos_acpi_device_probe(struct platform_device *pdev)
225 {
226 	chromeos_acpi_gpio_groups = get_gpio_pkg_num(&pdev->dev);
227 
228 	/*
229 	 * If the platform has more GPIO attribute groups than the number of
230 	 * groups this driver supports, give out a warning message.
231 	 */
232 	if (chromeos_acpi_gpio_groups > ARRAY_SIZE(chromeos_acpi_all_groups) - 2)
233 		dev_warn(&pdev->dev, "Only %zu GPIO attr groups supported by the driver out of total %u.\n",
234 			 ARRAY_SIZE(chromeos_acpi_all_groups) - 2, chromeos_acpi_gpio_groups);
235 	return 0;
236 }
237 
238 /* GGL is valid PNP ID of Google. PNP ID can be used with the ACPI devices. */
239 static const struct acpi_device_id chromeos_device_ids[] = {
240 	{ "GGL0001", 0 },
241 	{}
242 };
243 MODULE_DEVICE_TABLE(acpi, chromeos_device_ids);
244 
245 static struct platform_driver chromeos_acpi_device_driver = {
246 	.probe = chromeos_acpi_device_probe,
247 	.driver = {
248 		.name = KBUILD_MODNAME,
249 		.dev_groups = chromeos_acpi_all_groups,
250 		.acpi_match_table = chromeos_device_ids,
251 	}
252 };
253 module_platform_driver(chromeos_acpi_device_driver);
254 
255 MODULE_AUTHOR("Muhammad Usama Anjum <usama.anjum@collabora.com>");
256 MODULE_LICENSE("GPL");
257 MODULE_DESCRIPTION("ChromeOS specific ACPI extensions");
258