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