blob: 1017f7a69fc2dff1161cfa38c21d4abf30314878 [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
2/* This file is part of the coreboot project. */
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -08003
4#include <arch/acpi_device.h>
5#include <arch/acpigen.h>
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -08006#include <device/device.h>
7#include <device/path.h>
8#include <string.h>
9
10#include "chip.h"
11
12static struct acpi_dp *gpio_keys_add_child_node(
13 struct drivers_generic_gpio_keys_config *config,
14 const char *parent_path)
15{
16 struct key_info *key = &config->key;
17 struct acpi_dp *dsd;
18
19 if (!key->dev_name || !key->linux_code)
20 return NULL;
21
22 dsd = acpi_dp_new_table(config->key.dev_name);
23
24 acpi_dp_add_integer(dsd, "linux,code", key->linux_code);
25 if (key->linux_input_type)
26 acpi_dp_add_integer(dsd, "linux,input-type",
27 key->linux_input_type);
28 if (key->label)
29 acpi_dp_add_string(dsd, "label", key->label);
Karthikeyan Ramasubramanianc4427392018-12-26 21:37:24 -070030 if (key->wake) {
Karthikeyan Ramasubramaniand5c458f2019-02-06 10:44:15 -070031 acpi_dp_add_integer(dsd, "wakeup-source", 1);
Karthikeyan Ramasubramanianfd1557f2018-12-07 10:57:39 -070032 acpigen_write_PRW(key->wake, 3);
Karthikeyan Ramasubramanianc4427392018-12-26 21:37:24 -070033 acpi_dp_add_integer(dsd, "wakeup-event-action",
34 key->wakeup_event_action);
35 }
36
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080037 if (key->can_be_disabled)
38 acpi_dp_add_integer(dsd, "linux,can-disable",
39 key->can_be_disabled);
40 if (key->debounce_interval)
41 acpi_dp_add_integer(dsd, "debounce-interval",
42 key->debounce_interval);
43 acpi_dp_add_gpio(dsd, "gpios", parent_path, 0, 0,
44 config->gpio.polarity);
45
46 return dsd;
47}
48
Furquan Shaikh7536a392020-04-24 21:59:21 -070049static void gpio_keys_fill_ssdt_generator(const struct device *dev)
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080050{
51 struct drivers_generic_gpio_keys_config *config = dev->chip_info;
52 const char *scope = acpi_device_scope(dev);
53 const char *path = acpi_device_path(dev);
54 struct acpi_dp *dsd, *child;
55 const char *drv_string = config->is_polled ? "gpio-keys-polled"
56 : "gpio-keys";
57
58 if (!dev->enabled || !scope || !path || !config->gpio.pin_count)
59 return;
60
61 /* Device */
62 acpigen_write_scope(scope);
63 acpigen_write_device(acpi_device_name(dev));
64
65 /* _HID is set to PRP0001 */
66 acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);
67
68 /* Resources - _CRS */
69 acpigen_write_name("_CRS");
70 acpigen_write_resourcetemplate_header();
71 acpi_device_write_gpio(&config->gpio);
72 acpigen_write_resourcetemplate_footer();
73
74 /* DSD */
75 dsd = acpi_dp_new_table("_DSD");
76 acpi_dp_add_string(dsd, "compatible", drv_string);
77 if (config->is_polled)
78 acpi_dp_add_integer(dsd, "poll-interval",
Elyes HAOUAS0d8f1da2018-05-28 15:48:04 +020079 config->poll_interval);
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080080 /* Child device defining key */
81 child = gpio_keys_add_child_node(config, path);
82 if (child)
83 acpi_dp_add_child(dsd, "button-0", child);
84 acpi_dp_write(dsd);
85
86 acpigen_pop_len(); /* Device */
87 acpigen_pop_len(); /* Scope */
88}
89
90static const char *gpio_keys_acpi_name(const struct device *dev)
91{
92 struct drivers_generic_gpio_keys_config *config = dev->chip_info;
93 static char name[5];
94
95 if (config->name)
96 return config->name;
97
98 snprintf(name, sizeof(name), "K%03.3X", config->gpio.pins[0]);
99 name[4] = '\0';
100
101 return name;
102}
103
104static struct device_operations gpio_keys_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200105 .read_resources = noop_read_resources,
106 .set_resources = noop_set_resources,
Nico Huber68680dd2020-03-31 17:34:52 +0200107 .acpi_name = gpio_keys_acpi_name,
108 .acpi_fill_ssdt = gpio_keys_fill_ssdt_generator,
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -0800109};
110
111static void gpio_keys_enable(struct device *dev)
112{
113 dev->ops = &gpio_keys_ops;
114}
115
116struct chip_operations drivers_generic_gpio_keys_ops = {
117 CHIP_NAME("GPIO Keys")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100118 .enable_dev = gpio_keys_enable
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -0800119};