Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | static 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); |
| 46 | if (key->can_be_disabled) |
| 47 | acpi_dp_add_integer(dsd, "linux,can-disable", |
| 48 | key->can_be_disabled); |
| 49 | if (key->debounce_interval) |
| 50 | acpi_dp_add_integer(dsd, "debounce-interval", |
| 51 | key->debounce_interval); |
| 52 | acpi_dp_add_gpio(dsd, "gpios", parent_path, 0, 0, |
| 53 | config->gpio.polarity); |
| 54 | |
| 55 | return dsd; |
| 56 | } |
| 57 | |
| 58 | static void gpio_keys_fill_ssdt_generator(struct device *dev) |
| 59 | { |
| 60 | struct drivers_generic_gpio_keys_config *config = dev->chip_info; |
| 61 | const char *scope = acpi_device_scope(dev); |
| 62 | const char *path = acpi_device_path(dev); |
| 63 | struct acpi_dp *dsd, *child; |
| 64 | const char *drv_string = config->is_polled ? "gpio-keys-polled" |
| 65 | : "gpio-keys"; |
| 66 | |
| 67 | if (!dev->enabled || !scope || !path || !config->gpio.pin_count) |
| 68 | return; |
| 69 | |
| 70 | /* Device */ |
| 71 | acpigen_write_scope(scope); |
| 72 | acpigen_write_device(acpi_device_name(dev)); |
| 73 | |
| 74 | /* _HID is set to PRP0001 */ |
| 75 | acpigen_write_name_string("_HID", ACPI_DT_NAMESPACE_HID); |
| 76 | |
| 77 | /* Resources - _CRS */ |
| 78 | acpigen_write_name("_CRS"); |
| 79 | acpigen_write_resourcetemplate_header(); |
| 80 | acpi_device_write_gpio(&config->gpio); |
| 81 | acpigen_write_resourcetemplate_footer(); |
| 82 | |
| 83 | /* DSD */ |
| 84 | dsd = acpi_dp_new_table("_DSD"); |
| 85 | acpi_dp_add_string(dsd, "compatible", drv_string); |
| 86 | if (config->is_polled) |
| 87 | acpi_dp_add_integer(dsd, "poll-interval", |
Elyes HAOUAS | 0d8f1da | 2018-05-28 15:48:04 +0200 | [diff] [blame^] | 88 | config->poll_interval); |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 89 | /* Child device defining key */ |
| 90 | child = gpio_keys_add_child_node(config, path); |
| 91 | if (child) |
| 92 | acpi_dp_add_child(dsd, "button-0", child); |
| 93 | acpi_dp_write(dsd); |
| 94 | |
| 95 | acpigen_pop_len(); /* Device */ |
| 96 | acpigen_pop_len(); /* Scope */ |
| 97 | } |
| 98 | |
| 99 | static const char *gpio_keys_acpi_name(const struct device *dev) |
| 100 | { |
| 101 | struct drivers_generic_gpio_keys_config *config = dev->chip_info; |
| 102 | static char name[5]; |
| 103 | |
| 104 | if (config->name) |
| 105 | return config->name; |
| 106 | |
| 107 | snprintf(name, sizeof(name), "K%03.3X", config->gpio.pins[0]); |
| 108 | name[4] = '\0'; |
| 109 | |
| 110 | return name; |
| 111 | } |
| 112 | |
| 113 | static struct device_operations gpio_keys_ops = { |
| 114 | .read_resources = DEVICE_NOOP, |
Elyes HAOUAS | 0d8f1da | 2018-05-28 15:48:04 +0200 | [diff] [blame^] | 115 | .set_resources = DEVICE_NOOP, |
| 116 | .enable_resources = DEVICE_NOOP, |
Furquan Shaikh | 3c8e00e | 2018-01-11 19:28:37 -0800 | [diff] [blame] | 117 | .acpi_name = &gpio_keys_acpi_name, |
| 118 | .acpi_fill_ssdt_generator = &gpio_keys_fill_ssdt_generator, |
| 119 | }; |
| 120 | |
| 121 | static void gpio_keys_enable(struct device *dev) |
| 122 | { |
| 123 | dev->ops = &gpio_keys_ops; |
| 124 | } |
| 125 | |
| 126 | struct chip_operations drivers_generic_gpio_keys_ops = { |
| 127 | CHIP_NAME("GPIO Keys") |
| 128 | .enable_dev = &gpio_keys_enable |
| 129 | }; |