Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 2 | |
Furquan Shaikh | 76cedd2 | 2020-05-02 10:24:23 -0700 | [diff] [blame] | 3 | #include <acpi/acpi_device.h> |
| 4 | #include <acpi/acpi_pld.h> |
| 5 | #include <acpi/acpigen.h> |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 6 | #include <console/console.h> |
| 7 | #include <device/device.h> |
| 8 | #include <device/path.h> |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 9 | #include "chip.h" |
| 10 | |
Nick Vaccaro | 0a495eb | 2018-11-12 19:43:35 -0800 | [diff] [blame] | 11 | static bool usb_acpi_add_gpios_to_crs(struct drivers_usb_acpi_config *cfg) |
| 12 | { |
Ricardo Ribalda | df79757 | 2020-10-05 12:44:18 +0000 | [diff] [blame] | 13 | if (cfg->privacy_gpio.pin_count) |
| 14 | return true; |
Nick Vaccaro | 0a495eb | 2018-11-12 19:43:35 -0800 | [diff] [blame] | 15 | |
Ricardo Ribalda | df79757 | 2020-10-05 12:44:18 +0000 | [diff] [blame] | 16 | if (cfg->reset_gpio.pin_count && !cfg->has_power_resource) |
| 17 | return true; |
| 18 | |
| 19 | return false; |
| 20 | } |
| 21 | |
| 22 | static int usb_acpi_write_gpio(struct acpi_gpio *gpio, int *curr_index) |
| 23 | { |
| 24 | int ret = -1; |
| 25 | |
| 26 | if (gpio->pin_count == 0) |
| 27 | return ret; |
| 28 | |
| 29 | acpi_device_write_gpio(gpio); |
| 30 | ret = *curr_index; |
| 31 | (*curr_index)++; |
| 32 | |
| 33 | return ret; |
Nick Vaccaro | 0a495eb | 2018-11-12 19:43:35 -0800 | [diff] [blame] | 34 | } |
| 35 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 36 | static void usb_acpi_fill_ssdt_generator(const struct device *dev) |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 37 | { |
| 38 | struct drivers_usb_acpi_config *config = dev->chip_info; |
| 39 | const char *path = acpi_device_path(dev); |
Maulik V Vaghela | 8e885a5 | 2021-07-02 12:09:42 +0530 | [diff] [blame] | 40 | struct acpi_pld pld; |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 41 | |
Karthikeyan Ramasubramanian | d1c0f95 | 2020-11-02 16:26:52 -0700 | [diff] [blame] | 42 | if (!path || !config) |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 43 | return; |
| 44 | |
| 45 | /* Don't generate output for hubs, only ports */ |
| 46 | if (config->type == UPC_TYPE_HUB) |
| 47 | return; |
| 48 | |
| 49 | acpigen_write_scope(path); |
| 50 | if (config->desc) |
| 51 | acpigen_write_name_string("_DDN", config->desc); |
| 52 | acpigen_write_upc(config->type); |
| 53 | |
Maulik V Vaghela | 8e885a5 | 2021-07-02 12:09:42 +0530 | [diff] [blame] | 54 | if (usb_acpi_get_pld(dev, &pld)) |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 55 | acpigen_write_pld(&pld); |
Maulik V Vaghela | 8e885a5 | 2021-07-02 12:09:42 +0530 | [diff] [blame] | 56 | else |
| 57 | printk(BIOS_ERR, "Error retrieving PLD for %s\n", path); |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 58 | |
Nick Vaccaro | 0a495eb | 2018-11-12 19:43:35 -0800 | [diff] [blame] | 59 | /* Resources */ |
| 60 | if (usb_acpi_add_gpios_to_crs(config) == true) { |
| 61 | struct acpi_dp *dsd; |
Ricardo Ribalda | df79757 | 2020-10-05 12:44:18 +0000 | [diff] [blame] | 62 | int idx = 0; |
| 63 | int reset_gpio_index = -1; |
| 64 | int privacy_gpio_index; |
Nick Vaccaro | 0a495eb | 2018-11-12 19:43:35 -0800 | [diff] [blame] | 65 | |
| 66 | acpigen_write_name("_CRS"); |
| 67 | acpigen_write_resourcetemplate_header(); |
Ricardo Ribalda | df79757 | 2020-10-05 12:44:18 +0000 | [diff] [blame] | 68 | if (!config->has_power_resource) { |
| 69 | reset_gpio_index = usb_acpi_write_gpio( |
| 70 | &config->reset_gpio, &idx); |
| 71 | } |
| 72 | privacy_gpio_index = usb_acpi_write_gpio(&config->privacy_gpio, |
| 73 | &idx); |
Nick Vaccaro | 0a495eb | 2018-11-12 19:43:35 -0800 | [diff] [blame] | 74 | acpigen_write_resourcetemplate_footer(); |
| 75 | |
| 76 | dsd = acpi_dp_new_table("_DSD"); |
Ricardo Ribalda | df79757 | 2020-10-05 12:44:18 +0000 | [diff] [blame] | 77 | if (reset_gpio_index >= 0) |
| 78 | acpi_dp_add_gpio(dsd, "reset-gpio", path, |
| 79 | reset_gpio_index, 0, |
| 80 | config->reset_gpio.active_low); |
| 81 | if (privacy_gpio_index >= 0) |
| 82 | acpi_dp_add_gpio(dsd, "privacy-gpio", path, |
| 83 | privacy_gpio_index, 0, |
| 84 | config->privacy_gpio.active_low); |
Nick Vaccaro | 0a495eb | 2018-11-12 19:43:35 -0800 | [diff] [blame] | 85 | acpi_dp_write(dsd); |
| 86 | } |
| 87 | |
Karthikeyan Ramasubramanian | 5eac877 | 2020-10-23 15:15:01 -0600 | [diff] [blame] | 88 | if (config->has_power_resource) { |
| 89 | const struct acpi_power_res_params power_res_params = { |
| 90 | &config->reset_gpio, |
| 91 | config->reset_delay_ms, |
| 92 | config->reset_off_delay_ms, |
| 93 | &config->enable_gpio, |
| 94 | config->enable_delay_ms, |
| 95 | config->enable_off_delay_ms, |
| 96 | NULL, |
| 97 | 0, |
| 98 | 0 |
| 99 | }; |
| 100 | acpi_device_add_power_res(&power_res_params); |
| 101 | } |
| 102 | |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 103 | acpigen_pop_len(); |
| 104 | |
| 105 | printk(BIOS_INFO, "%s: %s at %s\n", path, |
| 106 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
| 107 | } |
| 108 | |
| 109 | static struct device_operations usb_acpi_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 110 | .read_resources = noop_read_resources, |
| 111 | .set_resources = noop_set_resources, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 112 | .scan_bus = scan_static_bus, |
| 113 | .acpi_fill_ssdt = usb_acpi_fill_ssdt_generator, |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | static void usb_acpi_enable(struct device *dev) |
| 117 | { |
| 118 | dev->ops = &usb_acpi_ops; |
| 119 | } |
| 120 | |
| 121 | struct chip_operations drivers_usb_acpi_ops = { |
| 122 | CHIP_NAME("USB ACPI Device") |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 123 | .enable_dev = usb_acpi_enable |
Duncan Laurie | 4721f43 | 2018-05-07 15:24:10 -0700 | [diff] [blame] | 124 | }; |
Maulik V Vaghela | 8e885a5 | 2021-07-02 12:09:42 +0530 | [diff] [blame] | 125 | |
| 126 | bool usb_acpi_get_pld(const struct device *usb_device, struct acpi_pld *pld) |
| 127 | { |
| 128 | struct drivers_usb_acpi_config *config = usb_device->chip_info; |
| 129 | |
| 130 | if (!usb_device || !usb_device->chip_info || |
| 131 | usb_device->chip_ops != &drivers_usb_acpi_ops) |
| 132 | return false; |
| 133 | |
| 134 | if (config->use_custom_pld) |
Angel Pons | afab34c | 2021-07-13 19:03:29 +0200 | [diff] [blame^] | 135 | *pld = config->custom_pld; |
Maulik V Vaghela | 8e885a5 | 2021-07-02 12:09:42 +0530 | [diff] [blame] | 136 | else |
| 137 | acpi_pld_fill_usb(pld, config->type, &config->group); |
| 138 | |
| 139 | return true; |
| 140 | } |