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 <[email protected]>
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/acpi.h>
14 #include <linux/device.h>
15 #include <linux/dmi.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/gpio/machine.h>
18 #include <linux/irq.h>
19 #include <linux/module.h>
20 #include <linux/pci.h>
21 #include <linux/platform_device.h>
22 #include <linux/serdev.h>
23 #include <linux/string.h>
24 
25 #include "x86-android-tablets.h"
26 #include "../serdev_helpers.h"
27 
28 static struct platform_device *x86_android_tablet_device;
29 
30 /*
31  * This helper allows getting a GPIO descriptor *before* the actual device
32  * consuming it has been instantiated. This function MUST only be used to
33  * handle this special case such as, e.g.:
34  *
35  * 1. Getting an IRQ from a GPIO for i2c_board_info.irq which is passed to
36  * i2c_client_new() to instantiate i2c_client-s; or
37  * 2. Calling desc_to_gpio() to get an old style GPIO number for gpio-keys
38  * platform_data which still uses old style GPIO numbers.
39  *
40  * Since the consuming device has not been instantiated yet a dynamic lookup
41  * is generated using the special x86_android_tablet device for dev_id.
42  *
43  * For normal GPIO lookups a standard static struct gpiod_lookup_table MUST be used.
44  */
x86_android_tablet_get_gpiod(const char * chip,int pin,const char * con_id,bool active_low,enum gpiod_flags dflags,struct gpio_desc ** desc)45 int x86_android_tablet_get_gpiod(const char *chip, int pin, const char *con_id,
46 				 bool active_low, enum gpiod_flags dflags,
47 				 struct gpio_desc **desc)
48 {
49 	struct gpiod_lookup_table *lookup;
50 	struct gpio_desc *gpiod;
51 
52 	lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
53 	if (!lookup)
54 		return -ENOMEM;
55 
56 	lookup->dev_id = KBUILD_MODNAME;
57 	lookup->table[0] =
58 		GPIO_LOOKUP(chip, pin, con_id, active_low ? GPIO_ACTIVE_LOW : GPIO_ACTIVE_HIGH);
59 
60 	gpiod_add_lookup_table(lookup);
61 	gpiod = devm_gpiod_get(&x86_android_tablet_device->dev, con_id, dflags);
62 	gpiod_remove_lookup_table(lookup);
63 	kfree(lookup);
64 
65 	if (IS_ERR(gpiod)) {
66 		pr_err("error %ld getting GPIO %s %d\n", PTR_ERR(gpiod), chip, pin);
67 		return PTR_ERR(gpiod);
68 	}
69 
70 	if (desc)
71 		*desc = gpiod;
72 
73 	return 0;
74 }
75 
x86_acpi_irq_helper_get(const struct x86_acpi_irq_data * data)76 int x86_acpi_irq_helper_get(const struct x86_acpi_irq_data *data)
77 {
78 	struct irq_fwspec fwspec = { };
79 	struct irq_domain *domain;
80 	struct acpi_device *adev;
81 	struct gpio_desc *gpiod;
82 	unsigned int irq_type;
83 	acpi_handle handle;
84 	acpi_status status;
85 	int irq, ret;
86 
87 	switch (data->type) {
88 	case X86_ACPI_IRQ_TYPE_APIC:
89 		/*
90 		 * The DSDT may already reference the GSI in a device skipped by
91 		 * acpi_quirk_skip_i2c_client_enumeration(). Unregister the GSI
92 		 * to avoid -EBUSY errors in this case.
93 		 */
94 		acpi_unregister_gsi(data->index);
95 		irq = acpi_register_gsi(NULL, data->index, data->trigger, data->polarity);
96 		if (irq < 0)
97 			pr_err("error %d getting APIC IRQ %d\n", irq, data->index);
98 
99 		return irq;
100 	case X86_ACPI_IRQ_TYPE_GPIOINT:
101 		/* Like acpi_dev_gpio_irq_get(), but without parsing ACPI resources */
102 		ret = x86_android_tablet_get_gpiod(data->chip, data->index, data->con_id,
103 						   false, GPIOD_ASIS, &gpiod);
104 		if (ret)
105 			return ret;
106 
107 		irq = gpiod_to_irq(gpiod);
108 		if (irq < 0) {
109 			pr_err("error %d getting IRQ %s %d\n", irq, data->chip, data->index);
110 			return irq;
111 		}
112 
113 		irq_type = acpi_dev_get_irq_type(data->trigger, data->polarity);
114 		if (irq_type != IRQ_TYPE_NONE && irq_type != irq_get_trigger_type(irq))
115 			irq_set_irq_type(irq, irq_type);
116 
117 		if (data->free_gpio)
118 			devm_gpiod_put(&x86_android_tablet_device->dev, gpiod);
119 
120 		return irq;
121 	case X86_ACPI_IRQ_TYPE_PMIC:
122 		status = acpi_get_handle(NULL, data->chip, &handle);
123 		if (ACPI_FAILURE(status)) {
124 			pr_err("error could not get %s handle\n", data->chip);
125 			return -ENODEV;
126 		}
127 
128 		adev = acpi_fetch_acpi_dev(handle);
129 		if (!adev) {
130 			pr_err("error could not get %s adev\n", data->chip);
131 			return -ENODEV;
132 		}
133 
134 		fwspec.fwnode = acpi_fwnode_handle(adev);
135 		domain = irq_find_matching_fwspec(&fwspec, data->domain);
136 		if (!domain) {
137 			pr_err("error could not find IRQ domain for %s\n", data->chip);
138 			return -ENODEV;
139 		}
140 
141 		return irq_create_mapping(domain, data->index);
142 	default:
143 		return 0;
144 	}
145 }
146 
147 static int i2c_client_count;
148 static int spi_dev_count;
149 static int pdev_count;
150 static int serdev_count;
151 static struct i2c_client **i2c_clients;
152 static struct spi_device **spi_devs;
153 static struct platform_device **pdevs;
154 static struct serdev_device **serdevs;
155 static struct gpio_keys_button *buttons;
156 static struct gpiod_lookup_table * const *gpiod_lookup_tables;
157 static const struct software_node *bat_swnode;
158 static void (*exit_handler)(void);
159 
160 static __init struct i2c_adapter *
get_i2c_adap_by_handle(const struct x86_i2c_client_info * client_info)161 get_i2c_adap_by_handle(const struct x86_i2c_client_info *client_info)
162 {
163 	acpi_handle handle;
164 	acpi_status status;
165 
166 	status = acpi_get_handle(NULL, client_info->adapter_path, &handle);
167 	if (ACPI_FAILURE(status)) {
168 		pr_err("Error could not get %s handle\n", client_info->adapter_path);
169 		return NULL;
170 	}
171 
172 	return i2c_acpi_find_adapter_by_handle(handle);
173 }
174 
match_parent(struct device * dev,const void * data)175 static __init int match_parent(struct device *dev, const void *data)
176 {
177 	return dev->parent == data;
178 }
179 
180 static __init struct i2c_adapter *
get_i2c_adap_by_pci_parent(const struct x86_i2c_client_info * client_info)181 get_i2c_adap_by_pci_parent(const struct x86_i2c_client_info *client_info)
182 {
183 	struct i2c_adapter *adap = NULL;
184 	struct device *pdev, *adap_dev;
185 
186 	pdev = bus_find_device_by_name(&pci_bus_type, NULL, client_info->adapter_path);
187 	if (!pdev) {
188 		pr_err("Error could not find %s PCI device\n", client_info->adapter_path);
189 		return NULL;
190 	}
191 
192 	adap_dev = bus_find_device(&i2c_bus_type, NULL, pdev, match_parent);
193 	if (adap_dev) {
194 		adap = i2c_verify_adapter(adap_dev);
195 		if (!adap)
196 			put_device(adap_dev);
197 	}
198 
199 	put_device(pdev);
200 
201 	return adap;
202 }
203 
x86_instantiate_i2c_client(const struct x86_dev_info * dev_info,int idx)204 static __init int x86_instantiate_i2c_client(const struct x86_dev_info *dev_info,
205 					     int idx)
206 {
207 	const struct x86_i2c_client_info *client_info = &dev_info->i2c_client_info[idx];
208 	struct i2c_board_info board_info = client_info->board_info;
209 	struct i2c_adapter *adap;
210 
211 	board_info.irq = x86_acpi_irq_helper_get(&client_info->irq_data);
212 	if (board_info.irq < 0)
213 		return board_info.irq;
214 
215 	if (dev_info->use_pci)
216 		adap = get_i2c_adap_by_pci_parent(client_info);
217 	else
218 		adap = get_i2c_adap_by_handle(client_info);
219 
220 	if (!adap) {
221 		pr_err("error could not get %s adapter\n", client_info->adapter_path);
222 		return -ENODEV;
223 	}
224 
225 	i2c_clients[idx] = i2c_new_client_device(adap, &board_info);
226 	put_device(&adap->dev);
227 	if (IS_ERR(i2c_clients[idx]))
228 		return dev_err_probe(&adap->dev, PTR_ERR(i2c_clients[idx]),
229 				      "creating I2C-client %d\n", idx);
230 
231 	return 0;
232 }
233 
x86_instantiate_spi_dev(const struct x86_dev_info * dev_info,int idx)234 static __init int x86_instantiate_spi_dev(const struct x86_dev_info *dev_info, int idx)
235 {
236 	const struct x86_spi_dev_info *spi_dev_info = &dev_info->spi_dev_info[idx];
237 	struct spi_board_info board_info = spi_dev_info->board_info;
238 	struct spi_controller *controller;
239 	struct acpi_device *adev;
240 	acpi_handle handle;
241 	acpi_status status;
242 
243 	board_info.irq = x86_acpi_irq_helper_get(&spi_dev_info->irq_data);
244 	if (board_info.irq < 0)
245 		return board_info.irq;
246 
247 	status = acpi_get_handle(NULL, spi_dev_info->ctrl_path, &handle);
248 	if (ACPI_FAILURE(status)) {
249 		pr_err("Error could not get %s handle\n", spi_dev_info->ctrl_path);
250 		return -ENODEV;
251 	}
252 
253 	adev = acpi_fetch_acpi_dev(handle);
254 	if (!adev) {
255 		pr_err("Error could not get adev for %s\n", spi_dev_info->ctrl_path);
256 		return -ENODEV;
257 	}
258 
259 	controller = acpi_spi_find_controller_by_adev(adev);
260 	if (!controller) {
261 		pr_err("Error could not get SPI controller for %s\n", spi_dev_info->ctrl_path);
262 		return -ENODEV;
263 	}
264 
265 	spi_devs[idx] = spi_new_device(controller, &board_info);
266 	put_device(&controller->dev);
267 	if (!spi_devs[idx])
268 		return dev_err_probe(&controller->dev, -ENOMEM,
269 				     "creating SPI-device %d\n", idx);
270 
271 	return 0;
272 }
273 
274 static __init struct device *
get_serdev_controller_by_pci_parent(const struct x86_serdev_info * info)275 get_serdev_controller_by_pci_parent(const struct x86_serdev_info *info)
276 {
277 	struct pci_dev *pdev;
278 
279 	pdev = pci_get_domain_bus_and_slot(0, 0, info->ctrl.pci.devfn);
280 	if (!pdev)
281 		return ERR_PTR(-EPROBE_DEFER);
282 
283 	/* This puts our reference on pdev and returns a ref on the ctrl */
284 	return get_serdev_controller_from_parent(&pdev->dev, 0, info->ctrl_devname);
285 }
286 
x86_instantiate_serdev(const struct x86_dev_info * dev_info,int idx)287 static __init int x86_instantiate_serdev(const struct x86_dev_info *dev_info, int idx)
288 {
289 	const struct x86_serdev_info *info = &dev_info->serdev_info[idx];
290 	struct acpi_device *serdev_adev;
291 	struct serdev_device *serdev;
292 	struct device *ctrl_dev;
293 	int ret = -ENODEV;
294 
295 	if (dev_info->use_pci)
296 		ctrl_dev = get_serdev_controller_by_pci_parent(info);
297 	else
298 		ctrl_dev = get_serdev_controller(info->ctrl.acpi.hid, info->ctrl.acpi.uid,
299 						 0, info->ctrl_devname);
300 	if (IS_ERR(ctrl_dev))
301 		return PTR_ERR(ctrl_dev);
302 
303 	serdev_adev = acpi_dev_get_first_match_dev(info->serdev_hid, NULL, -1);
304 	if (!serdev_adev) {
305 		pr_err("error could not get %s serdev adev\n", info->serdev_hid);
306 		goto put_ctrl_dev;
307 	}
308 
309 	serdev = serdev_device_alloc(to_serdev_controller(ctrl_dev));
310 	if (!serdev) {
311 		ret = -ENOMEM;
312 		goto put_serdev_adev;
313 	}
314 
315 	ACPI_COMPANION_SET(&serdev->dev, serdev_adev);
316 	acpi_device_set_enumerated(serdev_adev);
317 
318 	ret = serdev_device_add(serdev);
319 	if (ret) {
320 		dev_err(&serdev->dev, "error %d adding serdev\n", ret);
321 		serdev_device_put(serdev);
322 		goto put_serdev_adev;
323 	}
324 
325 	serdevs[idx] = serdev;
326 
327 put_serdev_adev:
328 	acpi_dev_put(serdev_adev);
329 put_ctrl_dev:
330 	put_device(ctrl_dev);
331 	return ret;
332 }
333 
x86_android_tablet_remove(struct platform_device * pdev)334 static void x86_android_tablet_remove(struct platform_device *pdev)
335 {
336 	int i;
337 
338 	for (i = serdev_count - 1; i >= 0; i--) {
339 		if (serdevs[i])
340 			serdev_device_remove(serdevs[i]);
341 	}
342 
343 	kfree(serdevs);
344 
345 	for (i = pdev_count - 1; i >= 0; i--)
346 		platform_device_unregister(pdevs[i]);
347 
348 	kfree(pdevs);
349 	kfree(buttons);
350 
351 	for (i = spi_dev_count - 1; i >= 0; i--)
352 		spi_unregister_device(spi_devs[i]);
353 
354 	kfree(spi_devs);
355 
356 	for (i = i2c_client_count - 1; i >= 0; i--)
357 		i2c_unregister_device(i2c_clients[i]);
358 
359 	kfree(i2c_clients);
360 
361 	if (exit_handler)
362 		exit_handler();
363 
364 	for (i = 0; gpiod_lookup_tables && gpiod_lookup_tables[i]; i++)
365 		gpiod_remove_lookup_table(gpiod_lookup_tables[i]);
366 
367 	software_node_unregister(bat_swnode);
368 }
369 
x86_android_tablet_probe(struct platform_device * pdev)370 static __init int x86_android_tablet_probe(struct platform_device *pdev)
371 {
372 	const struct x86_dev_info *dev_info;
373 	const struct dmi_system_id *id;
374 	int i, ret = 0;
375 
376 	id = dmi_first_match(x86_android_tablet_ids);
377 	if (!id)
378 		return -ENODEV;
379 
380 	dev_info = id->driver_data;
381 	/* Allow x86_android_tablet_device use before probe() exits */
382 	x86_android_tablet_device = pdev;
383 
384 	/*
385 	 * Since this runs from module_init() it cannot use -EPROBE_DEFER,
386 	 * instead pre-load any modules which are listed as requirements.
387 	 */
388 	for (i = 0; dev_info->modules && dev_info->modules[i]; i++)
389 		request_module(dev_info->modules[i]);
390 
391 	bat_swnode = dev_info->bat_swnode;
392 	if (bat_swnode) {
393 		ret = software_node_register(bat_swnode);
394 		if (ret)
395 			return ret;
396 	}
397 
398 	gpiod_lookup_tables = dev_info->gpiod_lookup_tables;
399 	for (i = 0; gpiod_lookup_tables && gpiod_lookup_tables[i]; i++)
400 		gpiod_add_lookup_table(gpiod_lookup_tables[i]);
401 
402 	if (dev_info->init) {
403 		ret = dev_info->init(&pdev->dev);
404 		if (ret < 0) {
405 			x86_android_tablet_remove(pdev);
406 			return ret;
407 		}
408 		exit_handler = dev_info->exit;
409 	}
410 
411 	i2c_clients = kcalloc(dev_info->i2c_client_count, sizeof(*i2c_clients), GFP_KERNEL);
412 	if (!i2c_clients) {
413 		x86_android_tablet_remove(pdev);
414 		return -ENOMEM;
415 	}
416 
417 	i2c_client_count = dev_info->i2c_client_count;
418 	for (i = 0; i < i2c_client_count; i++) {
419 		ret = x86_instantiate_i2c_client(dev_info, i);
420 		if (ret < 0) {
421 			x86_android_tablet_remove(pdev);
422 			return ret;
423 		}
424 	}
425 
426 	spi_devs = kcalloc(dev_info->spi_dev_count, sizeof(*spi_devs), GFP_KERNEL);
427 	if (!spi_devs) {
428 		x86_android_tablet_remove(pdev);
429 		return -ENOMEM;
430 	}
431 
432 	spi_dev_count = dev_info->spi_dev_count;
433 	for (i = 0; i < spi_dev_count; i++) {
434 		ret = x86_instantiate_spi_dev(dev_info, i);
435 		if (ret < 0) {
436 			x86_android_tablet_remove(pdev);
437 			return ret;
438 		}
439 	}
440 
441 	/* + 1 to make space for the (optional) gpio_keys_button platform device */
442 	pdevs = kcalloc(dev_info->pdev_count + 1, sizeof(*pdevs), GFP_KERNEL);
443 	if (!pdevs) {
444 		x86_android_tablet_remove(pdev);
445 		return -ENOMEM;
446 	}
447 
448 	pdev_count = dev_info->pdev_count;
449 	for (i = 0; i < pdev_count; i++) {
450 		pdevs[i] = platform_device_register_full(&dev_info->pdev_info[i]);
451 		if (IS_ERR(pdevs[i])) {
452 			ret = PTR_ERR(pdevs[i]);
453 			x86_android_tablet_remove(pdev);
454 			return ret;
455 		}
456 	}
457 
458 	serdevs = kcalloc(dev_info->serdev_count, sizeof(*serdevs), GFP_KERNEL);
459 	if (!serdevs) {
460 		x86_android_tablet_remove(pdev);
461 		return -ENOMEM;
462 	}
463 
464 	serdev_count = dev_info->serdev_count;
465 	for (i = 0; i < serdev_count; i++) {
466 		ret = x86_instantiate_serdev(dev_info, i);
467 		if (ret < 0) {
468 			x86_android_tablet_remove(pdev);
469 			return ret;
470 		}
471 	}
472 
473 	if (dev_info->gpio_button_count) {
474 		struct gpio_keys_platform_data pdata = { };
475 		struct gpio_desc *gpiod;
476 
477 		buttons = kcalloc(dev_info->gpio_button_count, sizeof(*buttons), GFP_KERNEL);
478 		if (!buttons) {
479 			x86_android_tablet_remove(pdev);
480 			return -ENOMEM;
481 		}
482 
483 		for (i = 0; i < dev_info->gpio_button_count; i++) {
484 			ret = x86_android_tablet_get_gpiod(dev_info->gpio_button[i].chip,
485 							   dev_info->gpio_button[i].pin,
486 							   dev_info->gpio_button[i].button.desc,
487 							   false, GPIOD_IN, &gpiod);
488 			if (ret < 0) {
489 				x86_android_tablet_remove(pdev);
490 				return ret;
491 			}
492 
493 			buttons[i] = dev_info->gpio_button[i].button;
494 			buttons[i].gpio = desc_to_gpio(gpiod);
495 			/* Release GPIO descriptor so that gpio-keys can request it */
496 			devm_gpiod_put(&x86_android_tablet_device->dev, gpiod);
497 		}
498 
499 		pdata.buttons = buttons;
500 		pdata.nbuttons = dev_info->gpio_button_count;
501 
502 		pdevs[pdev_count] = platform_device_register_data(&pdev->dev, "gpio-keys",
503 								  PLATFORM_DEVID_AUTO,
504 								  &pdata, sizeof(pdata));
505 		if (IS_ERR(pdevs[pdev_count])) {
506 			ret = PTR_ERR(pdevs[pdev_count]);
507 			x86_android_tablet_remove(pdev);
508 			return ret;
509 		}
510 		pdev_count++;
511 	}
512 
513 	return 0;
514 }
515 
516 static struct platform_driver x86_android_tablet_driver = {
517 	.driver = {
518 		.name = KBUILD_MODNAME,
519 	},
520 	.remove = x86_android_tablet_remove,
521 };
522 
x86_android_tablet_init(void)523 static int __init x86_android_tablet_init(void)
524 {
525 	x86_android_tablet_device = platform_create_bundle(&x86_android_tablet_driver,
526 						   x86_android_tablet_probe,
527 						   NULL, 0, NULL, 0);
528 
529 	return PTR_ERR_OR_ZERO(x86_android_tablet_device);
530 }
531 module_init(x86_android_tablet_init);
532 
x86_android_tablet_exit(void)533 static void __exit x86_android_tablet_exit(void)
534 {
535 	platform_device_unregister(x86_android_tablet_device);
536 	platform_driver_unregister(&x86_android_tablet_driver);
537 }
538 module_exit(x86_android_tablet_exit);
539 
540 MODULE_AUTHOR("Hans de Goede <[email protected]>");
541 MODULE_DESCRIPTION("X86 Android tablets DSDT fixups driver");
542 MODULE_LICENSE("GPL");
543