1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * DMI based code to deal with broken DSDTs on X86 tablets which ship with
4  * Android as (part of) the factory image. The factory kernels shipped on these
5  * devices typically have a bunch of things hardcoded, rather than specified
6  * in their DSDT.
7  *
8  * Copyright (C) 2021-2023 Hans de Goede <hdegoede@redhat.com>
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/acpi.h>
14 #include <linux/dmi.h>
15 #include <linux/gpio/driver.h>
16 #include <linux/gpio/machine.h>
17 #include <linux/irq.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/serdev.h>
21 #include <linux/string.h>
22 
23 #include "x86-android-tablets.h"
24 /* For gpiochip_get_desc() which is EXPORT_SYMBOL_GPL() */
25 #include "../../../gpio/gpiolib.h"
26 #include "../../../gpio/gpiolib-acpi.h"
27 
gpiochip_find_match_label(struct gpio_chip * gc,void * data)28 static int gpiochip_find_match_label(struct gpio_chip *gc, void *data)
29 {
30 	return gc->label && !strcmp(gc->label, data);
31 }
32 
x86_android_tablet_get_gpiod(const char * label,int pin,struct gpio_desc ** desc)33 int x86_android_tablet_get_gpiod(const char *label, int pin, struct gpio_desc **desc)
34 {
35 	struct gpio_desc *gpiod;
36 	struct gpio_chip *chip;
37 
38 	chip = gpiochip_find((void *)label, gpiochip_find_match_label);
39 	if (!chip) {
40 		pr_err("error cannot find GPIO chip %s\n", label);
41 		return -ENODEV;
42 	}
43 
44 	gpiod = gpiochip_get_desc(chip, pin);
45 	if (IS_ERR(gpiod)) {
46 		pr_err("error %ld getting GPIO %s %d\n", PTR_ERR(gpiod), label, pin);
47 		return PTR_ERR(gpiod);
48 	}
49 
50 	*desc = gpiod;
51 	return 0;
52 }
53 
x86_acpi_irq_helper_get(const struct x86_acpi_irq_data * data)54 int x86_acpi_irq_helper_get(const struct x86_acpi_irq_data *data)
55 {
56 	struct irq_fwspec fwspec = { };
57 	struct irq_domain *domain;
58 	struct acpi_device *adev;
59 	struct gpio_desc *gpiod;
60 	unsigned int irq_type;
61 	acpi_handle handle;
62 	acpi_status status;
63 	int irq, ret;
64 
65 	switch (data->type) {
66 	case X86_ACPI_IRQ_TYPE_APIC:
67 		/*
68 		 * The DSDT may already reference the GSI in a device skipped by
69 		 * acpi_quirk_skip_i2c_client_enumeration(). Unregister the GSI
70 		 * to avoid EBUSY errors in this case.
71 		 */
72 		acpi_unregister_gsi(data->index);
73 		irq = acpi_register_gsi(NULL, data->index, data->trigger, data->polarity);
74 		if (irq < 0)
75 			pr_err("error %d getting APIC IRQ %d\n", irq, data->index);
76 
77 		return irq;
78 	case X86_ACPI_IRQ_TYPE_GPIOINT:
79 		/* Like acpi_dev_gpio_irq_get(), but without parsing ACPI resources */
80 		ret = x86_android_tablet_get_gpiod(data->chip, data->index, &gpiod);
81 		if (ret)
82 			return ret;
83 
84 		irq = gpiod_to_irq(gpiod);
85 		if (irq < 0) {
86 			pr_err("error %d getting IRQ %s %d\n", irq, data->chip, data->index);
87 			return irq;
88 		}
89 
90 		irq_type = acpi_dev_get_irq_type(data->trigger, data->polarity);
91 		if (irq_type != IRQ_TYPE_NONE && irq_type != irq_get_trigger_type(irq))
92 			irq_set_irq_type(irq, irq_type);
93 
94 		return irq;
95 	case X86_ACPI_IRQ_TYPE_PMIC:
96 		status = acpi_get_handle(NULL, data->chip, &handle);
97 		if (ACPI_FAILURE(status)) {
98 			pr_err("error could not get %s handle\n", data->chip);
99 			return -ENODEV;
100 		}
101 
102 		adev = acpi_fetch_acpi_dev(handle);
103 		if (!adev) {
104 			pr_err("error could not get %s adev\n", data->chip);
105 			return -ENODEV;
106 		}
107 
108 		fwspec.fwnode = acpi_fwnode_handle(adev);
109 		domain = irq_find_matching_fwspec(&fwspec, data->domain);
110 		if (!domain) {
111 			pr_err("error could not find IRQ domain for %s\n", data->chip);
112 			return -ENODEV;
113 		}
114 
115 		return irq_create_mapping(domain, data->index);
116 	default:
117 		return 0;
118 	}
119 }
120 
121 static int i2c_client_count;
122 static int pdev_count;
123 static int serdev_count;
124 static struct i2c_client **i2c_clients;
125 static struct platform_device **pdevs;
126 static struct serdev_device **serdevs;
127 static struct gpio_keys_button *buttons;
128 static struct gpiod_lookup_table * const *gpiod_lookup_tables;
129 static const struct software_node *bat_swnode;
130 static void (*exit_handler)(void);
131 
x86_instantiate_i2c_client(const struct x86_dev_info * dev_info,int idx)132 static __init int x86_instantiate_i2c_client(const struct x86_dev_info *dev_info,
133 					     int idx)
134 {
135 	const struct x86_i2c_client_info *client_info = &dev_info->i2c_client_info[idx];
136 	struct i2c_board_info board_info = client_info->board_info;
137 	struct i2c_adapter *adap;
138 	acpi_handle handle;
139 	acpi_status status;
140 
141 	board_info.irq = x86_acpi_irq_helper_get(&client_info->irq_data);
142 	if (board_info.irq < 0)
143 		return board_info.irq;
144 
145 	status = acpi_get_handle(NULL, client_info->adapter_path, &handle);
146 	if (ACPI_FAILURE(status)) {
147 		pr_err("Error could not get %s handle\n", client_info->adapter_path);
148 		return -ENODEV;
149 	}
150 
151 	adap = i2c_acpi_find_adapter_by_handle(handle);
152 	if (!adap) {
153 		pr_err("error could not get %s adapter\n", client_info->adapter_path);
154 		return -ENODEV;
155 	}
156 
157 	i2c_clients[idx] = i2c_new_client_device(adap, &board_info);
158 	put_device(&adap->dev);
159 	if (IS_ERR(i2c_clients[idx]))
160 		return dev_err_probe(&adap->dev, PTR_ERR(i2c_clients[idx]),
161 				      "creating I2C-client %d\n", idx);
162 
163 	return 0;
164 }
165 
x86_instantiate_serdev(const struct x86_serdev_info * info,int idx)166 static __init int x86_instantiate_serdev(const struct x86_serdev_info *info, int idx)
167 {
168 	struct acpi_device *ctrl_adev, *serdev_adev;
169 	struct serdev_device *serdev;
170 	struct device *ctrl_dev;
171 	int ret = -ENODEV;
172 
173 	ctrl_adev = acpi_dev_get_first_match_dev(info->ctrl_hid, info->ctrl_uid, -1);
174 	if (!ctrl_adev) {
175 		pr_err("error could not get %s/%s ctrl adev\n",
176 		       info->ctrl_hid, info->ctrl_uid);
177 		return -ENODEV;
178 	}
179 
180 	serdev_adev = acpi_dev_get_first_match_dev(info->serdev_hid, NULL, -1);
181 	if (!serdev_adev) {
182 		pr_err("error could not get %s serdev adev\n", info->serdev_hid);
183 		goto put_ctrl_adev;
184 	}
185 
186 	/* get_first_physical_node() returns a weak ref, no need to put() it */
187 	ctrl_dev = acpi_get_first_physical_node(ctrl_adev);
188 	if (!ctrl_dev)	{
189 		pr_err("error could not get %s/%s ctrl physical dev\n",
190 		       info->ctrl_hid, info->ctrl_uid);
191 		goto put_serdev_adev;
192 	}
193 
194 	/* ctrl_dev now points to the controller's parent, get the controller */
195 	ctrl_dev = device_find_child_by_name(ctrl_dev, info->ctrl_devname);
196 	if (!ctrl_dev) {
197 		pr_err("error could not get %s/%s %s ctrl dev\n",
198 		       info->ctrl_hid, info->ctrl_uid, info->ctrl_devname);
199 		goto put_serdev_adev;
200 	}
201 
202 	serdev = serdev_device_alloc(to_serdev_controller(ctrl_dev));
203 	if (!serdev) {
204 		ret = -ENOMEM;
205 		goto put_serdev_adev;
206 	}
207 
208 	ACPI_COMPANION_SET(&serdev->dev, serdev_adev);
209 	acpi_device_set_enumerated(serdev_adev);
210 
211 	ret = serdev_device_add(serdev);
212 	if (ret) {
213 		dev_err(&serdev->dev, "error %d adding serdev\n", ret);
214 		serdev_device_put(serdev);
215 		goto put_serdev_adev;
216 	}
217 
218 	serdevs[idx] = serdev;
219 
220 put_serdev_adev:
221 	acpi_dev_put(serdev_adev);
222 put_ctrl_adev:
223 	acpi_dev_put(ctrl_adev);
224 	return ret;
225 }
226 
x86_android_tablet_cleanup(void)227 static void x86_android_tablet_cleanup(void)
228 {
229 	int i;
230 
231 	for (i = 0; i < serdev_count; i++) {
232 		if (serdevs[i])
233 			serdev_device_remove(serdevs[i]);
234 	}
235 
236 	kfree(serdevs);
237 
238 	for (i = 0; i < pdev_count; i++)
239 		platform_device_unregister(pdevs[i]);
240 
241 	kfree(pdevs);
242 	kfree(buttons);
243 
244 	for (i = 0; i < i2c_client_count; i++)
245 		i2c_unregister_device(i2c_clients[i]);
246 
247 	kfree(i2c_clients);
248 
249 	if (exit_handler)
250 		exit_handler();
251 
252 	for (i = 0; gpiod_lookup_tables && gpiod_lookup_tables[i]; i++)
253 		gpiod_remove_lookup_table(gpiod_lookup_tables[i]);
254 
255 	software_node_unregister(bat_swnode);
256 }
257 
x86_android_tablet_init(void)258 static __init int x86_android_tablet_init(void)
259 {
260 	const struct x86_dev_info *dev_info;
261 	const struct dmi_system_id *id;
262 	struct gpio_chip *chip;
263 	int i, ret = 0;
264 
265 	id = dmi_first_match(x86_android_tablet_ids);
266 	if (!id)
267 		return -ENODEV;
268 
269 	dev_info = id->driver_data;
270 
271 	/*
272 	 * The broken DSDTs on these devices often also include broken
273 	 * _AEI (ACPI Event Interrupt) handlers, disable these.
274 	 */
275 	if (dev_info->invalid_aei_gpiochip) {
276 		chip = gpiochip_find(dev_info->invalid_aei_gpiochip,
277 				     gpiochip_find_match_label);
278 		if (!chip) {
279 			pr_err("error cannot find GPIO chip %s\n", dev_info->invalid_aei_gpiochip);
280 			return -ENODEV;
281 		}
282 		acpi_gpiochip_free_interrupts(chip);
283 	}
284 
285 	/*
286 	 * Since this runs from module_init() it cannot use -EPROBE_DEFER,
287 	 * instead pre-load any modules which are listed as requirements.
288 	 */
289 	for (i = 0; dev_info->modules && dev_info->modules[i]; i++)
290 		request_module(dev_info->modules[i]);
291 
292 	bat_swnode = dev_info->bat_swnode;
293 	if (bat_swnode) {
294 		ret = software_node_register(bat_swnode);
295 		if (ret)
296 			return ret;
297 	}
298 
299 	gpiod_lookup_tables = dev_info->gpiod_lookup_tables;
300 	for (i = 0; gpiod_lookup_tables && gpiod_lookup_tables[i]; i++)
301 		gpiod_add_lookup_table(gpiod_lookup_tables[i]);
302 
303 	if (dev_info->init) {
304 		ret = dev_info->init();
305 		if (ret < 0) {
306 			x86_android_tablet_cleanup();
307 			return ret;
308 		}
309 		exit_handler = dev_info->exit;
310 	}
311 
312 	i2c_clients = kcalloc(dev_info->i2c_client_count, sizeof(*i2c_clients), GFP_KERNEL);
313 	if (!i2c_clients) {
314 		x86_android_tablet_cleanup();
315 		return -ENOMEM;
316 	}
317 
318 	i2c_client_count = dev_info->i2c_client_count;
319 	for (i = 0; i < i2c_client_count; i++) {
320 		ret = x86_instantiate_i2c_client(dev_info, i);
321 		if (ret < 0) {
322 			x86_android_tablet_cleanup();
323 			return ret;
324 		}
325 	}
326 
327 	/* + 1 to make space for (optional) gpio_keys_button pdev */
328 	pdevs = kcalloc(dev_info->pdev_count + 1, sizeof(*pdevs), GFP_KERNEL);
329 	if (!pdevs) {
330 		x86_android_tablet_cleanup();
331 		return -ENOMEM;
332 	}
333 
334 	pdev_count = dev_info->pdev_count;
335 	for (i = 0; i < pdev_count; i++) {
336 		pdevs[i] = platform_device_register_full(&dev_info->pdev_info[i]);
337 		if (IS_ERR(pdevs[i])) {
338 			x86_android_tablet_cleanup();
339 			return PTR_ERR(pdevs[i]);
340 		}
341 	}
342 
343 	serdevs = kcalloc(dev_info->serdev_count, sizeof(*serdevs), GFP_KERNEL);
344 	if (!serdevs) {
345 		x86_android_tablet_cleanup();
346 		return -ENOMEM;
347 	}
348 
349 	serdev_count = dev_info->serdev_count;
350 	for (i = 0; i < serdev_count; i++) {
351 		ret = x86_instantiate_serdev(&dev_info->serdev_info[i], i);
352 		if (ret < 0) {
353 			x86_android_tablet_cleanup();
354 			return ret;
355 		}
356 	}
357 
358 	if (dev_info->gpio_button_count) {
359 		struct gpio_keys_platform_data pdata = { };
360 		struct gpio_desc *gpiod;
361 
362 		buttons = kcalloc(dev_info->gpio_button_count, sizeof(*buttons), GFP_KERNEL);
363 		if (!buttons) {
364 			x86_android_tablet_cleanup();
365 			return -ENOMEM;
366 		}
367 
368 		for (i = 0; i < dev_info->gpio_button_count; i++) {
369 			ret = x86_android_tablet_get_gpiod(dev_info->gpio_button[i].chip,
370 							   dev_info->gpio_button[i].pin, &gpiod);
371 			if (ret < 0) {
372 				x86_android_tablet_cleanup();
373 				return ret;
374 			}
375 
376 			buttons[i] = dev_info->gpio_button[i].button;
377 			buttons[i].gpio = desc_to_gpio(gpiod);
378 		}
379 
380 		pdata.buttons = buttons;
381 		pdata.nbuttons = dev_info->gpio_button_count;
382 
383 		pdevs[pdev_count] = platform_device_register_data(NULL, "gpio-keys",
384 								  PLATFORM_DEVID_AUTO,
385 								  &pdata, sizeof(pdata));
386 		if (IS_ERR(pdevs[pdev_count])) {
387 			x86_android_tablet_cleanup();
388 			return PTR_ERR(pdevs[pdev_count]);
389 		}
390 		pdev_count++;
391 	}
392 
393 	return 0;
394 }
395 
396 module_init(x86_android_tablet_init);
397 module_exit(x86_android_tablet_cleanup);
398 
399 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
400 MODULE_DESCRIPTION("X86 Android tablets DSDT fixups driver");
401 MODULE_LICENSE("GPL");
402