blob: 37b90cabd9b34603d86ff89b418224b51c009dbd [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -08002
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpi_device.h>
4#include <acpi/acpigen.h>
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -08005#include <device/device.h>
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -08006#include <string.h>
7
8#include "chip.h"
9
10static 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);
Furquan Shaikhfa8b75f2020-06-26 01:19:46 -070028
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) {
Karthikeyan Ramasubramaniand5c458f2019-02-06 10:44:15 -070033 acpi_dp_add_integer(dsd, "wakeup-source", 1);
Karthikeyan Ramasubramanianc4427392018-12-26 21:37:24 -070034 acpi_dp_add_integer(dsd, "wakeup-event-action",
35 key->wakeup_event_action);
36 }
37
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080038 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,
Furquan Shaikhd2d5e442020-07-01 01:37:13 -070045 config->gpio.active_low);
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080046
47 return dsd;
48}
49
Furquan Shaikh7536a392020-04-24 21:59:21 -070050static void gpio_keys_fill_ssdt_generator(const struct device *dev)
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080051{
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
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -070059 if (!scope || !path || !config->gpio.pin_count)
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080060 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);
Ben Zhange91422d2021-05-19 15:22:17 -040078 if (config->label)
79 acpi_dp_add_string(dsd, "label", config->label);
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080080 if (config->is_polled)
81 acpi_dp_add_integer(dsd, "poll-interval",
Elyes HAOUAS0d8f1da2018-05-28 15:48:04 +020082 config->poll_interval);
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080083 /* 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
Matt DeVillier2dc68932023-09-13 18:42:12 -050089 acpigen_write_STA(acpi_device_status(dev));
90
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080091 acpigen_pop_len(); /* Device */
92 acpigen_pop_len(); /* Scope */
93}
94
95static 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
109static struct device_operations gpio_keys_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200110 .read_resources = noop_read_resources,
111 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200112 .acpi_name = gpio_keys_acpi_name,
113 .acpi_fill_ssdt = gpio_keys_fill_ssdt_generator,
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -0800114};
115
116static void gpio_keys_enable(struct device *dev)
117{
118 dev->ops = &gpio_keys_ops;
119}
120
121struct chip_operations drivers_generic_gpio_keys_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900122 .name = "GPIO Keys",
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100123 .enable_dev = gpio_keys_enable
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -0800124};