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