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