blob: 753a555a482aab444e7ac7efa50d5517ced308d1 [file] [log] [blame]
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -08001/*
2 * This file is part of the coreboot project.
3 *
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -08004 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <arch/acpi_device.h>
15#include <arch/acpigen.h>
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080016#include <device/device.h>
17#include <device/path.h>
18#include <string.h>
19
20#include "chip.h"
21
22static struct acpi_dp *gpio_keys_add_child_node(
23 struct drivers_generic_gpio_keys_config *config,
24 const char *parent_path)
25{
26 struct key_info *key = &config->key;
27 struct acpi_dp *dsd;
28
29 if (!key->dev_name || !key->linux_code)
30 return NULL;
31
32 dsd = acpi_dp_new_table(config->key.dev_name);
33
34 acpi_dp_add_integer(dsd, "linux,code", key->linux_code);
35 if (key->linux_input_type)
36 acpi_dp_add_integer(dsd, "linux,input-type",
37 key->linux_input_type);
38 if (key->label)
39 acpi_dp_add_string(dsd, "label", key->label);
Karthikeyan Ramasubramanianc4427392018-12-26 21:37:24 -070040 if (key->wake) {
Karthikeyan Ramasubramaniand5c458f2019-02-06 10:44:15 -070041 acpi_dp_add_integer(dsd, "wakeup-source", 1);
Karthikeyan Ramasubramanianfd1557f2018-12-07 10:57:39 -070042 acpigen_write_PRW(key->wake, 3);
Karthikeyan Ramasubramanianc4427392018-12-26 21:37:24 -070043 acpi_dp_add_integer(dsd, "wakeup-event-action",
44 key->wakeup_event_action);
45 }
46
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080047 if (key->can_be_disabled)
48 acpi_dp_add_integer(dsd, "linux,can-disable",
49 key->can_be_disabled);
50 if (key->debounce_interval)
51 acpi_dp_add_integer(dsd, "debounce-interval",
52 key->debounce_interval);
53 acpi_dp_add_gpio(dsd, "gpios", parent_path, 0, 0,
54 config->gpio.polarity);
55
56 return dsd;
57}
58
59static void gpio_keys_fill_ssdt_generator(struct device *dev)
60{
61 struct drivers_generic_gpio_keys_config *config = dev->chip_info;
62 const char *scope = acpi_device_scope(dev);
63 const char *path = acpi_device_path(dev);
64 struct acpi_dp *dsd, *child;
65 const char *drv_string = config->is_polled ? "gpio-keys-polled"
66 : "gpio-keys";
67
68 if (!dev->enabled || !scope || !path || !config->gpio.pin_count)
69 return;
70
71 /* Device */
72 acpigen_write_scope(scope);
73 acpigen_write_device(acpi_device_name(dev));
74
75 /* _HID is set to PRP0001 */
76 acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);
77
78 /* Resources - _CRS */
79 acpigen_write_name("_CRS");
80 acpigen_write_resourcetemplate_header();
81 acpi_device_write_gpio(&config->gpio);
82 acpigen_write_resourcetemplate_footer();
83
84 /* DSD */
85 dsd = acpi_dp_new_table("_DSD");
86 acpi_dp_add_string(dsd, "compatible", drv_string);
87 if (config->is_polled)
88 acpi_dp_add_integer(dsd, "poll-interval",
Elyes HAOUAS0d8f1da2018-05-28 15:48:04 +020089 config->poll_interval);
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -080090 /* Child device defining key */
91 child = gpio_keys_add_child_node(config, path);
92 if (child)
93 acpi_dp_add_child(dsd, "button-0", child);
94 acpi_dp_write(dsd);
95
96 acpigen_pop_len(); /* Device */
97 acpigen_pop_len(); /* Scope */
98}
99
100static const char *gpio_keys_acpi_name(const struct device *dev)
101{
102 struct drivers_generic_gpio_keys_config *config = dev->chip_info;
103 static char name[5];
104
105 if (config->name)
106 return config->name;
107
108 snprintf(name, sizeof(name), "K%03.3X", config->gpio.pins[0]);
109 name[4] = '\0';
110
111 return name;
112}
113
114static struct device_operations gpio_keys_ops = {
115 .read_resources = DEVICE_NOOP,
Elyes HAOUAS0d8f1da2018-05-28 15:48:04 +0200116 .set_resources = DEVICE_NOOP,
117 .enable_resources = DEVICE_NOOP,
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100118 .acpi_name = gpio_keys_acpi_name,
119 .acpi_fill_ssdt_generator = gpio_keys_fill_ssdt_generator,
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -0800120};
121
122static void gpio_keys_enable(struct device *dev)
123{
124 dev->ops = &gpio_keys_ops;
125}
126
127struct chip_operations drivers_generic_gpio_keys_ops = {
128 CHIP_NAME("GPIO Keys")
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100129 .enable_dev = gpio_keys_enable
Furquan Shaikh3c8e00e2018-01-11 19:28:37 -0800130};