1 // SPDX-License-Identifier: GPL-2.0-or-later
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
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13 #include <linux/acpi.h>
14 #include <linux/dmi.h>
15 #include <linux/gpio/consumer.h>
16 #include <linux/gpio/machine.h>
17 #include <linux/irq.h>
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/serdev.h>
21 #include <linux/string.h>
23 #include "x86-android-tablets.h"
24 #include "../serdev_helpers.h"
26 static struct platform_device *x86_android_tablet_device;
29 * This helper allows getting a gpio_desc *before* the actual device consuming
30 * the GPIO has been instantiated. This function _must_ only be used to handle
31 * this special case such as e.g. :
33 * 1. Getting an IRQ from a GPIO for i2c_board_info.irq which is passed to
34 * i2c_client_new() to instantiate i2c_client-s; or
35 * 2. Calling desc_to_gpio() to get an old style GPIO number for gpio_keys
36 * platform_data which still uses old style GPIO numbers.
38 * Since the consuming device has not been instatiated yet a dynamic lookup
39 * is generated using the special x86_android_tablet dev for dev_id.
41 * For normal GPIO lookups a standard static gpiod_lookup_table _must_ be used.
43 int x86_android_tablet_get_gpiod(const char *chip, int pin, const char *con_id,
44 bool active_low, enum gpiod_flags dflags,
45 struct gpio_desc **desc)
47 struct gpiod_lookup_table *lookup;
48 struct gpio_desc *gpiod;
50 lookup = kzalloc(struct_size(lookup, table, 2), GFP_KERNEL);
54 lookup->dev_id = KBUILD_MODNAME;
56 GPIO_LOOKUP(chip, pin, con_id, active_low ? GPIO_ACTIVE_LOW : GPIO_ACTIVE_HIGH);
58 gpiod_add_lookup_table(lookup);
59 gpiod = devm_gpiod_get(&x86_android_tablet_device->dev, con_id, dflags);
60 gpiod_remove_lookup_table(lookup);
64 pr_err("error %ld getting GPIO %s %d\n", PTR_ERR(gpiod), chip, pin);
65 return PTR_ERR(gpiod);
74 int x86_acpi_irq_helper_get(const struct x86_acpi_irq_data *data)
76 struct irq_fwspec fwspec = { };
77 struct irq_domain *domain;
78 struct acpi_device *adev;
79 struct gpio_desc *gpiod;
80 unsigned int irq_type;
86 case X86_ACPI_IRQ_TYPE_APIC:
88 * The DSDT may already reference the GSI in a device skipped by
89 * acpi_quirk_skip_i2c_client_enumeration(). Unregister the GSI
90 * to avoid EBUSY errors in this case.
92 acpi_unregister_gsi(data->index);
93 irq = acpi_register_gsi(NULL, data->index, data->trigger, data->polarity);
95 pr_err("error %d getting APIC IRQ %d\n", irq, data->index);
98 case X86_ACPI_IRQ_TYPE_GPIOINT:
99 /* Like acpi_dev_gpio_irq_get(), but without parsing ACPI resources */
100 ret = x86_android_tablet_get_gpiod(data->chip, data->index, data->con_id,
101 false, GPIOD_ASIS, &gpiod);
105 irq = gpiod_to_irq(gpiod);
107 pr_err("error %d getting IRQ %s %d\n", irq, data->chip, data->index);
111 irq_type = acpi_dev_get_irq_type(data->trigger, data->polarity);
112 if (irq_type != IRQ_TYPE_NONE && irq_type != irq_get_trigger_type(irq))
113 irq_set_irq_type(irq, irq_type);
116 devm_gpiod_put(&x86_android_tablet_device->dev, gpiod);
119 case X86_ACPI_IRQ_TYPE_PMIC:
120 status = acpi_get_handle(NULL, data->chip, &handle);
121 if (ACPI_FAILURE(status)) {
122 pr_err("error could not get %s handle\n", data->chip);
126 adev = acpi_fetch_acpi_dev(handle);
128 pr_err("error could not get %s adev\n", data->chip);
132 fwspec.fwnode = acpi_fwnode_handle(adev);
133 domain = irq_find_matching_fwspec(&fwspec, data->domain);
135 pr_err("error could not find IRQ domain for %s\n", data->chip);
139 return irq_create_mapping(domain, data->index);
145 static int i2c_client_count;
146 static int spi_dev_count;
147 static int pdev_count;
148 static int serdev_count;
149 static struct i2c_client **i2c_clients;
150 static struct spi_device **spi_devs;
151 static struct platform_device **pdevs;
152 static struct serdev_device **serdevs;
153 static struct gpio_keys_button *buttons;
154 static struct gpiod_lookup_table * const *gpiod_lookup_tables;
155 static const struct software_node *bat_swnode;
156 static void (*exit_handler)(void);
158 static __init int x86_instantiate_i2c_client(const struct x86_dev_info *dev_info,
161 const struct x86_i2c_client_info *client_info = &dev_info->i2c_client_info[idx];
162 struct i2c_board_info board_info = client_info->board_info;
163 struct i2c_adapter *adap;
167 board_info.irq = x86_acpi_irq_helper_get(&client_info->irq_data);
168 if (board_info.irq < 0)
169 return board_info.irq;
171 status = acpi_get_handle(NULL, client_info->adapter_path, &handle);
172 if (ACPI_FAILURE(status)) {
173 pr_err("Error could not get %s handle\n", client_info->adapter_path);
177 adap = i2c_acpi_find_adapter_by_handle(handle);
179 pr_err("error could not get %s adapter\n", client_info->adapter_path);
183 i2c_clients[idx] = i2c_new_client_device(adap, &board_info);
184 put_device(&adap->dev);
185 if (IS_ERR(i2c_clients[idx]))
186 return dev_err_probe(&adap->dev, PTR_ERR(i2c_clients[idx]),
187 "creating I2C-client %d\n", idx);
192 static __init int x86_instantiate_spi_dev(const struct x86_dev_info *dev_info, int idx)
194 const struct x86_spi_dev_info *spi_dev_info = &dev_info->spi_dev_info[idx];
195 struct spi_board_info board_info = spi_dev_info->board_info;
196 struct spi_controller *controller;
197 struct acpi_device *adev;
201 board_info.irq = x86_acpi_irq_helper_get(&spi_dev_info->irq_data);
202 if (board_info.irq < 0)
203 return board_info.irq;
205 status = acpi_get_handle(NULL, spi_dev_info->ctrl_path, &handle);
206 if (ACPI_FAILURE(status)) {
207 pr_err("Error could not get %s handle\n", spi_dev_info->ctrl_path);
211 adev = acpi_fetch_acpi_dev(handle);
213 pr_err("Error could not get adev for %s\n", spi_dev_info->ctrl_path);
217 controller = acpi_spi_find_controller_by_adev(adev);
219 pr_err("Error could not get SPI controller for %s\n", spi_dev_info->ctrl_path);
223 spi_devs[idx] = spi_new_device(controller, &board_info);
224 put_device(&controller->dev);
226 return dev_err_probe(&controller->dev, -ENOMEM,
227 "creating SPI-device %d\n", idx);
232 static __init int x86_instantiate_serdev(const struct x86_serdev_info *info, int idx)
234 struct acpi_device *serdev_adev;
235 struct serdev_device *serdev;
236 struct device *ctrl_dev;
239 ctrl_dev = get_serdev_controller(info->ctrl_hid, info->ctrl_uid, 0,
241 if (IS_ERR(ctrl_dev))
242 return PTR_ERR(ctrl_dev);
244 serdev_adev = acpi_dev_get_first_match_dev(info->serdev_hid, NULL, -1);
246 pr_err("error could not get %s serdev adev\n", info->serdev_hid);
250 serdev = serdev_device_alloc(to_serdev_controller(ctrl_dev));
253 goto put_serdev_adev;
256 ACPI_COMPANION_SET(&serdev->dev, serdev_adev);
257 acpi_device_set_enumerated(serdev_adev);
259 ret = serdev_device_add(serdev);
261 dev_err(&serdev->dev, "error %d adding serdev\n", ret);
262 serdev_device_put(serdev);
263 goto put_serdev_adev;
266 serdevs[idx] = serdev;
269 acpi_dev_put(serdev_adev);
271 put_device(ctrl_dev);
275 static void x86_android_tablet_remove(struct platform_device *pdev)
279 for (i = serdev_count - 1; i >= 0; i--) {
281 serdev_device_remove(serdevs[i]);
286 for (i = pdev_count - 1; i >= 0; i--)
287 platform_device_unregister(pdevs[i]);
292 for (i = spi_dev_count - 1; i >= 0; i--)
293 spi_unregister_device(spi_devs[i]);
297 for (i = i2c_client_count - 1; i >= 0; i--)
298 i2c_unregister_device(i2c_clients[i]);
305 for (i = 0; gpiod_lookup_tables && gpiod_lookup_tables[i]; i++)
306 gpiod_remove_lookup_table(gpiod_lookup_tables[i]);
308 software_node_unregister(bat_swnode);
311 static __init int x86_android_tablet_probe(struct platform_device *pdev)
313 const struct x86_dev_info *dev_info;
314 const struct dmi_system_id *id;
317 id = dmi_first_match(x86_android_tablet_ids);
321 dev_info = id->driver_data;
322 /* Allow x86_android_tablet_device use before probe() exits */
323 x86_android_tablet_device = pdev;
326 * Since this runs from module_init() it cannot use -EPROBE_DEFER,
327 * instead pre-load any modules which are listed as requirements.
329 for (i = 0; dev_info->modules && dev_info->modules[i]; i++)
330 request_module(dev_info->modules[i]);
332 bat_swnode = dev_info->bat_swnode;
334 ret = software_node_register(bat_swnode);
339 gpiod_lookup_tables = dev_info->gpiod_lookup_tables;
340 for (i = 0; gpiod_lookup_tables && gpiod_lookup_tables[i]; i++)
341 gpiod_add_lookup_table(gpiod_lookup_tables[i]);
343 if (dev_info->init) {
344 ret = dev_info->init(&pdev->dev);
346 x86_android_tablet_remove(pdev);
349 exit_handler = dev_info->exit;
352 i2c_clients = kcalloc(dev_info->i2c_client_count, sizeof(*i2c_clients), GFP_KERNEL);
354 x86_android_tablet_remove(pdev);
358 i2c_client_count = dev_info->i2c_client_count;
359 for (i = 0; i < i2c_client_count; i++) {
360 ret = x86_instantiate_i2c_client(dev_info, i);
362 x86_android_tablet_remove(pdev);
367 spi_devs = kcalloc(dev_info->spi_dev_count, sizeof(*spi_devs), GFP_KERNEL);
369 x86_android_tablet_remove(pdev);
373 spi_dev_count = dev_info->spi_dev_count;
374 for (i = 0; i < spi_dev_count; i++) {
375 ret = x86_instantiate_spi_dev(dev_info, i);
377 x86_android_tablet_remove(pdev);
382 /* + 1 to make space for (optional) gpio_keys_button pdev */
383 pdevs = kcalloc(dev_info->pdev_count + 1, sizeof(*pdevs), GFP_KERNEL);
385 x86_android_tablet_remove(pdev);
389 pdev_count = dev_info->pdev_count;
390 for (i = 0; i < pdev_count; i++) {
391 pdevs[i] = platform_device_register_full(&dev_info->pdev_info[i]);
392 if (IS_ERR(pdevs[i])) {
393 x86_android_tablet_remove(pdev);
394 return PTR_ERR(pdevs[i]);
398 serdevs = kcalloc(dev_info->serdev_count, sizeof(*serdevs), GFP_KERNEL);
400 x86_android_tablet_remove(pdev);
404 serdev_count = dev_info->serdev_count;
405 for (i = 0; i < serdev_count; i++) {
406 ret = x86_instantiate_serdev(&dev_info->serdev_info[i], i);
408 x86_android_tablet_remove(pdev);
413 if (dev_info->gpio_button_count) {
414 struct gpio_keys_platform_data pdata = { };
415 struct gpio_desc *gpiod;
417 buttons = kcalloc(dev_info->gpio_button_count, sizeof(*buttons), GFP_KERNEL);
419 x86_android_tablet_remove(pdev);
423 for (i = 0; i < dev_info->gpio_button_count; i++) {
424 ret = x86_android_tablet_get_gpiod(dev_info->gpio_button[i].chip,
425 dev_info->gpio_button[i].pin,
426 dev_info->gpio_button[i].button.desc,
427 false, GPIOD_IN, &gpiod);
429 x86_android_tablet_remove(pdev);
433 buttons[i] = dev_info->gpio_button[i].button;
434 buttons[i].gpio = desc_to_gpio(gpiod);
435 /* Release gpiod so that gpio-keys can request it */
436 devm_gpiod_put(&x86_android_tablet_device->dev, gpiod);
439 pdata.buttons = buttons;
440 pdata.nbuttons = dev_info->gpio_button_count;
442 pdevs[pdev_count] = platform_device_register_data(&pdev->dev, "gpio-keys",
444 &pdata, sizeof(pdata));
445 if (IS_ERR(pdevs[pdev_count])) {
446 x86_android_tablet_remove(pdev);
447 return PTR_ERR(pdevs[pdev_count]);
455 static struct platform_driver x86_android_tablet_driver = {
457 .name = KBUILD_MODNAME,
459 .remove_new = x86_android_tablet_remove,
462 static int __init x86_android_tablet_init(void)
464 x86_android_tablet_device = platform_create_bundle(&x86_android_tablet_driver,
465 x86_android_tablet_probe,
468 return PTR_ERR_OR_ZERO(x86_android_tablet_device);
470 module_init(x86_android_tablet_init);
472 static void __exit x86_android_tablet_exit(void)
474 platform_device_unregister(x86_android_tablet_device);
475 platform_driver_unregister(&x86_android_tablet_driver);
477 module_exit(x86_android_tablet_exit);
480 MODULE_DESCRIPTION("X86 Android tablets DSDT fixups driver");
481 MODULE_LICENSE("GPL");