xref: /linux/drivers/platform/x86/x86-android-tablets/core.c (revision 6d47b4f08436cb682fb2644e6265a3897fd42a77)
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/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  */
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 
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 *
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 
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 *
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 
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 
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 -ENOMEM;
269 
270 	return 0;
271 }
272 
273 static __init struct device *
274 get_serdev_controller_by_pci_parent(const struct x86_serdev_info *info)
275 {
276 	struct pci_dev *pdev;
277 
278 	pdev = pci_get_domain_bus_and_slot(0, 0, info->ctrl.pci.devfn);
279 	if (!pdev)
280 		return ERR_PTR(-EPROBE_DEFER);
281 
282 	/* This puts our reference on pdev and returns a ref on the ctrl */
283 	return get_serdev_controller_from_parent(&pdev->dev, 0, info->ctrl_devname);
284 }
285 
286 static __init int x86_instantiate_serdev(const struct x86_dev_info *dev_info, int idx)
287 {
288 	const struct x86_serdev_info *info = &dev_info->serdev_info[idx];
289 	struct acpi_device *serdev_adev;
290 	struct serdev_device *serdev;
291 	struct device *ctrl_dev;
292 	int ret = -ENODEV;
293 
294 	if (dev_info->use_pci)
295 		ctrl_dev = get_serdev_controller_by_pci_parent(info);
296 	else
297 		ctrl_dev = get_serdev_controller(info->ctrl.acpi.hid, info->ctrl.acpi.uid,
298 						 0, info->ctrl_devname);
299 	if (IS_ERR(ctrl_dev))
300 		return PTR_ERR(ctrl_dev);
301 
302 	serdev_adev = acpi_dev_get_first_match_dev(info->serdev_hid, NULL, -1);
303 	if (!serdev_adev) {
304 		pr_err("error could not get %s serdev adev\n", info->serdev_hid);
305 		goto put_ctrl_dev;
306 	}
307 
308 	serdev = serdev_device_alloc(to_serdev_controller(ctrl_dev));
309 	if (!serdev) {
310 		ret = -ENOMEM;
311 		goto put_serdev_adev;
312 	}
313 
314 	ACPI_COMPANION_SET(&serdev->dev, serdev_adev);
315 	acpi_device_set_enumerated(serdev_adev);
316 
317 	ret = serdev_device_add(serdev);
318 	if (ret) {
319 		dev_err(&serdev->dev, "error %d adding serdev\n", ret);
320 		serdev_device_put(serdev);
321 		goto put_serdev_adev;
322 	}
323 
324 	serdevs[idx] = serdev;
325 
326 put_serdev_adev:
327 	acpi_dev_put(serdev_adev);
328 put_ctrl_dev:
329 	put_device(ctrl_dev);
330 	return ret;
331 }
332 
333 static void x86_android_tablet_remove(struct platform_device *pdev)
334 {
335 	int i;
336 
337 	for (i = serdev_count - 1; i >= 0; i--) {
338 		if (serdevs[i])
339 			serdev_device_remove(serdevs[i]);
340 	}
341 
342 	kfree(serdevs);
343 
344 	for (i = pdev_count - 1; i >= 0; i--)
345 		platform_device_unregister(pdevs[i]);
346 
347 	kfree(pdevs);
348 	kfree(buttons);
349 
350 	for (i = spi_dev_count - 1; i >= 0; i--)
351 		spi_unregister_device(spi_devs[i]);
352 
353 	kfree(spi_devs);
354 
355 	for (i = i2c_client_count - 1; i >= 0; i--)
356 		i2c_unregister_device(i2c_clients[i]);
357 
358 	kfree(i2c_clients);
359 
360 	if (exit_handler)
361 		exit_handler();
362 
363 	for (i = 0; gpiod_lookup_tables && gpiod_lookup_tables[i]; i++)
364 		gpiod_remove_lookup_table(gpiod_lookup_tables[i]);
365 
366 	software_node_unregister(bat_swnode);
367 }
368 
369 static __init int x86_android_tablet_probe(struct platform_device *pdev)
370 {
371 	const struct x86_dev_info *dev_info;
372 	const struct dmi_system_id *id;
373 	int i, ret = 0;
374 
375 	id = dmi_first_match(x86_android_tablet_ids);
376 	if (!id)
377 		return -ENODEV;
378 
379 	dev_info = id->driver_data;
380 	/* Allow x86_android_tablet_device use before probe() exits */
381 	x86_android_tablet_device = pdev;
382 
383 	/*
384 	 * Since this runs from module_init() it cannot use -EPROBE_DEFER,
385 	 * instead pre-load any modules which are listed as requirements.
386 	 */
387 	for (i = 0; dev_info->modules && dev_info->modules[i]; i++)
388 		request_module(dev_info->modules[i]);
389 
390 	bat_swnode = dev_info->bat_swnode;
391 	if (bat_swnode) {
392 		ret = software_node_register(bat_swnode);
393 		if (ret)
394 			return ret;
395 	}
396 
397 	gpiod_lookup_tables = dev_info->gpiod_lookup_tables;
398 	for (i = 0; gpiod_lookup_tables && gpiod_lookup_tables[i]; i++)
399 		gpiod_add_lookup_table(gpiod_lookup_tables[i]);
400 
401 	if (dev_info->init) {
402 		ret = dev_info->init(&pdev->dev);
403 		if (ret < 0) {
404 			x86_android_tablet_remove(pdev);
405 			return ret;
406 		}
407 		exit_handler = dev_info->exit;
408 	}
409 
410 	i2c_clients = kcalloc(dev_info->i2c_client_count, sizeof(*i2c_clients), GFP_KERNEL);
411 	if (!i2c_clients) {
412 		x86_android_tablet_remove(pdev);
413 		return -ENOMEM;
414 	}
415 
416 	i2c_client_count = dev_info->i2c_client_count;
417 	for (i = 0; i < i2c_client_count; i++) {
418 		ret = x86_instantiate_i2c_client(dev_info, i);
419 		if (ret < 0) {
420 			x86_android_tablet_remove(pdev);
421 			return ret;
422 		}
423 	}
424 
425 	spi_devs = kcalloc(dev_info->spi_dev_count, sizeof(*spi_devs), GFP_KERNEL);
426 	if (!spi_devs) {
427 		x86_android_tablet_remove(pdev);
428 		return -ENOMEM;
429 	}
430 
431 	spi_dev_count = dev_info->spi_dev_count;
432 	for (i = 0; i < spi_dev_count; i++) {
433 		ret = x86_instantiate_spi_dev(dev_info, i);
434 		if (ret < 0) {
435 			x86_android_tablet_remove(pdev);
436 			return ret;
437 		}
438 	}
439 
440 	/* + 1 to make space for the (optional) gpio_keys_button platform device */
441 	pdevs = kcalloc(dev_info->pdev_count + 1, sizeof(*pdevs), GFP_KERNEL);
442 	if (!pdevs) {
443 		x86_android_tablet_remove(pdev);
444 		return -ENOMEM;
445 	}
446 
447 	pdev_count = dev_info->pdev_count;
448 	for (i = 0; i < pdev_count; i++) {
449 		pdevs[i] = platform_device_register_full(&dev_info->pdev_info[i]);
450 		if (IS_ERR(pdevs[i])) {
451 			ret = PTR_ERR(pdevs[i]);
452 			x86_android_tablet_remove(pdev);
453 			return ret;
454 		}
455 	}
456 
457 	serdevs = kcalloc(dev_info->serdev_count, sizeof(*serdevs), GFP_KERNEL);
458 	if (!serdevs) {
459 		x86_android_tablet_remove(pdev);
460 		return -ENOMEM;
461 	}
462 
463 	serdev_count = dev_info->serdev_count;
464 	for (i = 0; i < serdev_count; i++) {
465 		ret = x86_instantiate_serdev(dev_info, i);
466 		if (ret < 0) {
467 			x86_android_tablet_remove(pdev);
468 			return ret;
469 		}
470 	}
471 
472 	if (dev_info->gpio_button_count) {
473 		struct gpio_keys_platform_data pdata = { };
474 		struct gpio_desc *gpiod;
475 
476 		buttons = kcalloc(dev_info->gpio_button_count, sizeof(*buttons), GFP_KERNEL);
477 		if (!buttons) {
478 			x86_android_tablet_remove(pdev);
479 			return -ENOMEM;
480 		}
481 
482 		for (i = 0; i < dev_info->gpio_button_count; i++) {
483 			ret = x86_android_tablet_get_gpiod(dev_info->gpio_button[i].chip,
484 							   dev_info->gpio_button[i].pin,
485 							   dev_info->gpio_button[i].button.desc,
486 							   false, GPIOD_IN, &gpiod);
487 			if (ret < 0) {
488 				x86_android_tablet_remove(pdev);
489 				return ret;
490 			}
491 
492 			buttons[i] = dev_info->gpio_button[i].button;
493 			buttons[i].gpio = desc_to_gpio(gpiod);
494 			/* Release GPIO descriptor so that gpio-keys can request it */
495 			devm_gpiod_put(&x86_android_tablet_device->dev, gpiod);
496 		}
497 
498 		pdata.buttons = buttons;
499 		pdata.nbuttons = dev_info->gpio_button_count;
500 
501 		pdevs[pdev_count] = platform_device_register_data(&pdev->dev, "gpio-keys",
502 								  PLATFORM_DEVID_AUTO,
503 								  &pdata, sizeof(pdata));
504 		if (IS_ERR(pdevs[pdev_count])) {
505 			ret = PTR_ERR(pdevs[pdev_count]);
506 			x86_android_tablet_remove(pdev);
507 			return ret;
508 		}
509 		pdev_count++;
510 	}
511 
512 	return 0;
513 }
514 
515 static struct platform_driver x86_android_tablet_driver = {
516 	.driver = {
517 		.name = KBUILD_MODNAME,
518 	},
519 	.remove = x86_android_tablet_remove,
520 };
521 
522 static int __init x86_android_tablet_init(void)
523 {
524 	x86_android_tablet_device = platform_create_bundle(&x86_android_tablet_driver,
525 						   x86_android_tablet_probe,
526 						   NULL, 0, NULL, 0);
527 
528 	return PTR_ERR_OR_ZERO(x86_android_tablet_device);
529 }
530 module_init(x86_android_tablet_init);
531 
532 static void __exit x86_android_tablet_exit(void)
533 {
534 	platform_device_unregister(x86_android_tablet_device);
535 	platform_driver_unregister(&x86_android_tablet_driver);
536 }
537 module_exit(x86_android_tablet_exit);
538 
539 MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
540 MODULE_DESCRIPTION("X86 Android tablets DSDT fixups driver");
541 MODULE_LICENSE("GPL");
542