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> |
Elyes Haouas | bdd03c2 | 2024-05-27 11:20:07 +0200 | [diff] [blame^] | 6 | #include <stdio.h> |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 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 | |
Matt DeVillier | 2dc6893 | 2023-09-13 18:42:12 -0500 | [diff] [blame] | 90 | acpigen_write_STA(acpi_device_status(dev)); |
| 91 | |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 92 | acpigen_pop_len(); /* Device */ |
| 93 | acpigen_pop_len(); /* Scope */ |
| 94 | } |
| 95 | |
| 96 | static const char *gpio_keys_acpi_name(const struct device *dev) |
| 97 | { |
| 98 | struct drivers_generic_gpio_keys_config *config = dev->chip_info; |
| 99 | static char name[5]; |
| 100 | |
| 101 | if (config->name) |
| 102 | return config->name; |
| 103 | |
| 104 | snprintf(name, sizeof(name), "K%03.3X", config->gpio.pins[0]); |
| 105 | name[4] = '\0'; |
| 106 | |
| 107 | return name; |
| 108 | } |
| 109 | |
| 110 | static struct device_operations gpio_keys_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 111 | .read_resources = noop_read_resources, |
| 112 | .set_resources = noop_set_resources, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 113 | .acpi_name = gpio_keys_acpi_name, |
| 114 | .acpi_fill_ssdt = gpio_keys_fill_ssdt_generator, |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
| 117 | static void gpio_keys_enable(struct device *dev) |
| 118 | { |
| 119 | dev->ops = &gpio_keys_ops; |
| 120 | } |
| 121 | |
| 122 | struct chip_operations drivers_generic_gpio_keys_ops = { |
Nicholas Sudsgaard | bfb11be | 2024-01-30 09:53:46 +0900 | [diff] [blame] | 123 | .name = "GPIO Keys", |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 124 | .enable_dev = gpio_keys_enable |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 125 | }; |