blob: 28e5be571978e86caaf99f9d94377ddd34962887 [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>
6#include <device/path.h>
7#include <string.h>
8
9#include "chip.h"
10
11static struct acpi_dp *gpio_keys_add_child_node(
12 struct drivers_generic_gpio_keys_config *config,
13 const char *parent_path)
14{
15 struct key_info *key = &config->key;
16 struct acpi_dp *dsd;
17
18 if (!key->dev_name || !key->linux_code)
19 return NULL;
20
21 dsd = acpi_dp_new_table(config->key.dev_name);
22
23 acpi_dp_add_integer(dsd, "linux,code", key->linux_code);
24 if (key->linux_input_type)
25 acpi_dp_add_integer(dsd, "linux,input-type",
26 key->linux_input_type);
27 if (key->label)
28 acpi_dp_add_string(dsd, "label", key->label);
Furquan Shaikhfa8b75f2020-06-26 01:19:46 -070029
30 if (key->wakeup_route == WAKEUP_ROUTE_SCI)
31 acpigen_write_PRW(key->wake_gpe, 3);
32
33 if (key->wakeup_route != WAKEUP_ROUTE_DISABLED) {
Karthikeyan Ramasubramaniand5c458f2019-02-06 10:44:15 -070034 acpi_dp_add_integer(dsd, "wakeup-source", 1);
Karthikeyan Ramasubramanianc4427392018-12-26 21:37:24 -070035 acpi_dp_add_integer(dsd, "wakeup-event-action",
36 key->wakeup_event_action);
37 }
38
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080039 if (key->can_be_disabled)
40 acpi_dp_add_integer(dsd, "linux,can-disable",
41 key->can_be_disabled);
42 if (key->debounce_interval)
43 acpi_dp_add_integer(dsd, "debounce-interval",
44 key->debounce_interval);
45 acpi_dp_add_gpio(dsd, "gpios", parent_path, 0, 0,
Furquan Shaikhd2d5e442020-07-01 01:37:13 -070046 config->gpio.active_low);
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080047
48 return dsd;
49}
50
Furquan Shaikh7536a392020-04-24 21:59:21 -070051static void gpio_keys_fill_ssdt_generator(const struct device *dev)
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080052{
53 struct drivers_generic_gpio_keys_config *config = dev->chip_info;
54 const char *scope = acpi_device_scope(dev);
55 const char *path = acpi_device_path(dev);
56 struct acpi_dp *dsd, *child;
57 const char *drv_string = config->is_polled ? "gpio-keys-polled"
58 : "gpio-keys";
59
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -070060 if (!scope || !path || !config->gpio.pin_count)
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080061 return;
62
63 /* Device */
64 acpigen_write_scope(scope);
65 acpigen_write_device(acpi_device_name(dev));
66
67 /* _HID is set to PRP0001 */
68 acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);
69
70 /* Resources - _CRS */
71 acpigen_write_name("_CRS");
72 acpigen_write_resourcetemplate_header();
73 acpi_device_write_gpio(&config->gpio);
74 acpigen_write_resourcetemplate_footer();
75
76 /* DSD */
77 dsd = acpi_dp_new_table("_DSD");
78 acpi_dp_add_string(dsd, "compatible", drv_string);
Ben Zhange91422d2021-05-19 15:22:17 -040079 if (config->label)
80 acpi_dp_add_string(dsd, "label", config->label);
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080081 if (config->is_polled)
82 acpi_dp_add_integer(dsd, "poll-interval",
Elyes HAOUAS0d8f1da2018-05-28 15:48:04 +020083 config->poll_interval);
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080084 /* Child device defining key */
85 child = gpio_keys_add_child_node(config, path);
86 if (child)
87 acpi_dp_add_child(dsd, "button-0", child);
88 acpi_dp_write(dsd);
89
Matt DeVillier2dc68932023-09-13 18:42:12 -050090 acpigen_write_STA(acpi_device_status(dev));
91
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080092 acpigen_pop_len(); /* Device */
93 acpigen_pop_len(); /* Scope */
94}
95
96static const char *gpio_keys_acpi_name(const struct device *dev)
97{
98 struct drivers_generic_gpio_keys_config *config = dev->chip_info;
99 static char name[5];
100
101 if (config->name)
102 return config->name;
103
104 snprintf(name, sizeof(name), "K%03.3X", config->gpio.pins[0]);
105 name[4] = '\0';
106
107 return name;
108}
109
110static struct device_operations gpio_keys_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200111 .read_resources = noop_read_resources,
112 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200113 .acpi_name = gpio_keys_acpi_name,
114 .acpi_fill_ssdt = gpio_keys_fill_ssdt_generator,
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -0800115};
116
117static void gpio_keys_enable(struct device *dev)
118{
119 dev->ops = &gpio_keys_ops;
120}
121
122struct chip_operations drivers_generic_gpio_keys_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900123 .name = "GPIO Keys",
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100124 .enable_dev = gpio_keys_enable
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -0800125};