xref: /aosp_15_r20/external/coreboot/src/drivers/generic/gpio_keys/gpio_keys.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <acpi/acpi_device.h>
4 #include <acpi/acpigen.h>
5 #include <device/device.h>
6 #include <stdio.h>
7 
8 #include "chip.h"
9 
gpio_keys_add_child_node(struct drivers_generic_gpio_keys_config * config,const char * parent_path)10 static struct acpi_dp *gpio_keys_add_child_node(
11 			struct drivers_generic_gpio_keys_config *config,
12 			const char *parent_path)
13 {
14 	struct key_info *key = &config->key;
15 	struct acpi_dp *dsd;
16 
17 	if (!key->dev_name || !key->linux_code)
18 		return NULL;
19 
20 	dsd = acpi_dp_new_table(config->key.dev_name);
21 
22 	acpi_dp_add_integer(dsd, "linux,code", key->linux_code);
23 	if (key->linux_input_type)
24 		acpi_dp_add_integer(dsd, "linux,input-type",
25 				    key->linux_input_type);
26 	if (key->label)
27 		acpi_dp_add_string(dsd, "label", key->label);
28 
29 	if (key->wakeup_route == WAKEUP_ROUTE_SCI)
30 		acpigen_write_PRW(key->wake_gpe, 3);
31 
32 	if (key->wakeup_route != WAKEUP_ROUTE_DISABLED) {
33 		acpi_dp_add_integer(dsd, "wakeup-source", 1);
34 		acpi_dp_add_integer(dsd, "wakeup-event-action",
35 					key->wakeup_event_action);
36 	}
37 
38 	if (key->can_be_disabled)
39 		acpi_dp_add_integer(dsd, "linux,can-disable",
40 				    key->can_be_disabled);
41 	if (key->debounce_interval)
42 		acpi_dp_add_integer(dsd, "debounce-interval",
43 				    key->debounce_interval);
44 	acpi_dp_add_gpio(dsd, "gpios", parent_path, 0, 0,
45 			 config->gpio.active_low);
46 
47 	return dsd;
48 }
49 
gpio_keys_fill_ssdt_generator(const struct device * dev)50 static void gpio_keys_fill_ssdt_generator(const struct device *dev)
51 {
52 	struct drivers_generic_gpio_keys_config *config = dev->chip_info;
53 	const char *scope = acpi_device_scope(dev);
54 	const char *path = acpi_device_path(dev);
55 	struct acpi_dp *dsd, *child;
56 	const char *drv_string = config->is_polled ? "gpio-keys-polled"
57 				: "gpio-keys";
58 
59 	if (!scope || !path || !config->gpio.pin_count)
60 		return;
61 
62 	/* Device */
63 	acpigen_write_scope(scope);
64 	acpigen_write_device(acpi_device_name(dev));
65 
66 	/* _HID is set to PRP0001 */
67 	acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);
68 
69 	/* Resources - _CRS */
70 	acpigen_write_name("_CRS");
71 	acpigen_write_resourcetemplate_header();
72 	acpi_device_write_gpio(&config->gpio);
73 	acpigen_write_resourcetemplate_footer();
74 
75 	/* DSD */
76 	dsd = acpi_dp_new_table("_DSD");
77 	acpi_dp_add_string(dsd, "compatible", drv_string);
78 	if (config->label)
79 		acpi_dp_add_string(dsd, "label", config->label);
80 	if (config->is_polled)
81 		acpi_dp_add_integer(dsd, "poll-interval",
82 					config->poll_interval);
83 	/* Child device defining key */
84 	child = gpio_keys_add_child_node(config, path);
85 	if (child)
86 		acpi_dp_add_child(dsd, "button-0", child);
87 	acpi_dp_write(dsd);
88 
89 	acpigen_write_STA(acpi_device_status(dev));
90 
91 	acpigen_pop_len(); /* Device */
92 	acpigen_pop_len(); /* Scope */
93 }
94 
gpio_keys_acpi_name(const struct device * dev)95 static const char *gpio_keys_acpi_name(const struct device *dev)
96 {
97 	struct drivers_generic_gpio_keys_config *config = dev->chip_info;
98 	static char name[5];
99 
100 	if (config->name)
101 		return config->name;
102 
103 	snprintf(name, sizeof(name), "K%03.3X", config->gpio.pins[0]);
104 	name[4] = '\0';
105 
106 	return name;
107 }
108 
109 static struct device_operations gpio_keys_ops = {
110 	.read_resources		= noop_read_resources,
111 	.set_resources		= noop_set_resources,
112 	.acpi_name		= gpio_keys_acpi_name,
113 	.acpi_fill_ssdt		= gpio_keys_fill_ssdt_generator,
114 };
115 
gpio_keys_enable(struct device * dev)116 static void gpio_keys_enable(struct device *dev)
117 {
118 	dev->ops = &gpio_keys_ops;
119 }
120 
121 struct chip_operations drivers_generic_gpio_keys_ops = {
122 	.name = "GPIO Keys",
123 	.enable_dev = gpio_keys_enable
124 };
125