Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 2 | |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 3 | #include <acpi/acpi_device.h> |
| 4 | #include <acpi/acpigen.h> |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 5 | #include <device/device.h> |
| 6 | #include <device/path.h> |
| 7 | #include <string.h> |
| 8 | |
| 9 | #include "chip.h" |
| 10 | |
| 11 | static 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 Shaikh | fa8b75f | 2020-06-26 01:19:46 -0700 | [diff] [blame] | 29 | |
| 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 Ramasubramanian | d5c458f | 2019-02-06 10:44:15 -0700 | [diff] [blame] | 34 | acpi_dp_add_integer(dsd, "wakeup-source", 1); |
Karthikeyan Ramasubramanian | c442739 | 2018-12-26 21:37:24 -0700 | [diff] [blame] | 35 | acpi_dp_add_integer(dsd, "wakeup-event-action", |
| 36 | key->wakeup_event_action); |
| 37 | } |
| 38 | |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 39 | 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 Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 46 | config->gpio.active_low); |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 47 | |
| 48 | return dsd; |
| 49 | } |
| 50 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 51 | static void gpio_keys_fill_ssdt_generator(const struct device *dev) |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 52 | { |
| 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 Ramasubramanian | d1c0f95 | 2020-11-02 16:26:52 -0700 | [diff] [blame] | 60 | if (!scope || !path || !config->gpio.pin_count) |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 61 | 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 Zhang | e91422d | 2021-05-19 15:22:17 -0400 | [diff] [blame] | 79 | if (config->label) |
| 80 | acpi_dp_add_string(dsd, "label", config->label); |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 81 | if (config->is_polled) |
| 82 | acpi_dp_add_integer(dsd, "poll-interval", |
Elyes HAOUAS | 0d8f1da | 2018-05-28 15:48:04 +0200 | [diff] [blame] | 83 | config->poll_interval); |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 84 | /* 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 | |
| 90 | acpigen_pop_len(); /* Device */ |
| 91 | acpigen_pop_len(); /* Scope */ |
| 92 | } |
| 93 | |
| 94 | static const char *gpio_keys_acpi_name(const struct device *dev) |
| 95 | { |
| 96 | struct drivers_generic_gpio_keys_config *config = dev->chip_info; |
| 97 | static char name[5]; |
| 98 | |
| 99 | if (config->name) |
| 100 | return config->name; |
| 101 | |
| 102 | snprintf(name, sizeof(name), "K%03.3X", config->gpio.pins[0]); |
| 103 | name[4] = '\0'; |
| 104 | |
| 105 | return name; |
| 106 | } |
| 107 | |
| 108 | static struct device_operations gpio_keys_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 109 | .read_resources = noop_read_resources, |
| 110 | .set_resources = noop_set_resources, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 111 | .acpi_name = gpio_keys_acpi_name, |
| 112 | .acpi_fill_ssdt = gpio_keys_fill_ssdt_generator, |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | static void gpio_keys_enable(struct device *dev) |
| 116 | { |
| 117 | dev->ops = &gpio_keys_ops; |
| 118 | } |
| 119 | |
| 120 | struct chip_operations drivers_generic_gpio_keys_ops = { |
| 121 | CHIP_NAME("GPIO Keys") |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 122 | .enable_dev = gpio_keys_enable |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 123 | }; |