Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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