summaryrefslogtreecommitdiff
path: root/src/drivers/generic/gpio_keys/gpio_keys.c
blob: 28e5be571978e86caaf99f9d94377ddd34962887 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* SPDX-License-Identifier: GPL-2.0-only */

#include <acpi/acpi_device.h>
#include <acpi/acpigen.h>
#include <device/device.h>
#include <device/path.h>
#include <string.h>

#include "chip.h"

static struct acpi_dp *gpio_keys_add_child_node(
			struct drivers_generic_gpio_keys_config *config,
			const char *parent_path)
{
	struct key_info *key = &config->key;
	struct acpi_dp *dsd;

	if (!key->dev_name || !key->linux_code)
		return NULL;

	dsd = acpi_dp_new_table(config->key.dev_name);

	acpi_dp_add_integer(dsd, "linux,code", key->linux_code);
	if (key->linux_input_type)
		acpi_dp_add_integer(dsd, "linux,input-type",
				    key->linux_input_type);
	if (key->label)
		acpi_dp_add_string(dsd, "label", key->label);

	if (key->wakeup_route == WAKEUP_ROUTE_SCI)
		acpigen_write_PRW(key->wake_gpe, 3);

	if (key->wakeup_route != WAKEUP_ROUTE_DISABLED) {
		acpi_dp_add_integer(dsd, "wakeup-source", 1);
		acpi_dp_add_integer(dsd, "wakeup-event-action",
					key->wakeup_event_action);
	}

	if (key->can_be_disabled)
		acpi_dp_add_integer(dsd, "linux,can-disable",
				    key->can_be_disabled);
	if (key->debounce_interval)
		acpi_dp_add_integer(dsd, "debounce-interval",
				    key->debounce_interval);
	acpi_dp_add_gpio(dsd, "gpios", parent_path, 0, 0,
			 config->gpio.active_low);

	return dsd;
}

static void gpio_keys_fill_ssdt_generator(const struct device *dev)
{
	struct drivers_generic_gpio_keys_config *config = dev->chip_info;
	const char *scope = acpi_device_scope(dev);
	const char *path = acpi_device_path(dev);
	struct acpi_dp *dsd, *child;
	const char *drv_string = config->is_polled ? "gpio-keys-polled"
				: "gpio-keys";

	if (!scope || !path || !config->gpio.pin_count)
		return;

	/* Device */
	acpigen_write_scope(scope);
	acpigen_write_device(acpi_device_name(dev));

	/* _HID is set to PRP0001 */
	acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID);

	/* Resources - _CRS */
	acpigen_write_name("_CRS");
	acpigen_write_resourcetemplate_header();
	acpi_device_write_gpio(&config->gpio);
	acpigen_write_resourcetemplate_footer();

	/* DSD */
	dsd = acpi_dp_new_table("_DSD");
	acpi_dp_add_string(dsd, "compatible", drv_string);
	if (config->label)
		acpi_dp_add_string(dsd, "label", config->label);
	if (config->is_polled)
		acpi_dp_add_integer(dsd, "poll-interval",
					config->poll_interval);
	/* Child device defining key */
	child = gpio_keys_add_child_node(config, path);
	if (child)
		acpi_dp_add_child(dsd, "button-0", child);
	acpi_dp_write(dsd);

	acpigen_write_STA(acpi_device_status(dev));

	acpigen_pop_len(); /* Device */
	acpigen_pop_len(); /* Scope */
}

static const char *gpio_keys_acpi_name(const struct device *dev)
{
	struct drivers_generic_gpio_keys_config *config = dev->chip_info;
	static char name[5];

	if (config->name)
		return config->name;

	snprintf(name, sizeof(name), "K%03.3X", config->gpio.pins[0]);
	name[4] = '\0';

	return name;
}

static struct device_operations gpio_keys_ops = {
	.read_resources		= noop_read_resources,
	.set_resources		= noop_set_resources,
	.acpi_name		= gpio_keys_acpi_name,
	.acpi_fill_ssdt		= gpio_keys_fill_ssdt_generator,
};

static void gpio_keys_enable(struct device *dev)
{
	dev->ops = &gpio_keys_ops;
}

struct chip_operations drivers_generic_gpio_keys_ops = {
	.name = "GPIO Keys",
	.enable_dev = gpio_keys_enable
};