Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -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 | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 5 | #include <console/console.h> |
| 6 | #include <device/device.h> |
| 7 | #include <device/path.h> |
| 8 | #include <device/spi.h> |
| 9 | #include <spi-generic.h> |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 10 | #include <string.h> |
| 11 | #include "chip.h" |
| 12 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 13 | static int spi_acpi_get_bus(const struct device *dev) |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 14 | { |
| 15 | struct device *spi_dev; |
| 16 | struct device_operations *ops; |
| 17 | |
| 18 | if (!dev->bus || !dev->bus->dev) |
| 19 | return -1; |
| 20 | |
| 21 | spi_dev = dev->bus->dev; |
| 22 | ops = spi_dev->ops; |
| 23 | |
| 24 | if (ops && ops->ops_spi_bus && |
| 25 | ops->ops_spi_bus->dev_to_bus) |
| 26 | return ops->ops_spi_bus->dev_to_bus(spi_dev); |
| 27 | |
| 28 | return -1; |
| 29 | } |
| 30 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 31 | static bool spi_acpi_add_gpios_to_crs(struct drivers_spi_acpi_config *config) |
| 32 | { |
| 33 | /* |
| 34 | * Return false if: |
| 35 | * 1. Request to explicitly disable export of GPIOs in CRS, or |
| 36 | * 2. Both reset and enable GPIOs are not provided. |
| 37 | */ |
| 38 | if (config->disable_gpio_export_in_crs || |
| 39 | ((config->reset_gpio.pin_count == 0) && |
| 40 | (config->enable_gpio.pin_count == 0))) |
| 41 | return false; |
| 42 | |
| 43 | return true; |
| 44 | } |
| 45 | |
| 46 | static int spi_acpi_write_gpio(struct acpi_gpio *gpio, int *curr_index) |
| 47 | { |
| 48 | int ret = -1; |
| 49 | |
| 50 | if (gpio->pin_count == 0) |
| 51 | return ret; |
| 52 | |
| 53 | acpi_device_write_gpio(gpio); |
| 54 | ret = *curr_index; |
| 55 | (*curr_index)++; |
| 56 | |
| 57 | return ret; |
| 58 | } |
| 59 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 60 | static void spi_acpi_fill_ssdt_generator(const struct device *dev) |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 61 | { |
| 62 | struct drivers_spi_acpi_config *config = dev->chip_info; |
| 63 | const char *scope = acpi_device_scope(dev); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 64 | const char *path = acpi_device_path(dev); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 65 | struct acpi_spi spi = { |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 66 | .device_select = dev->path.spi.cs, |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 67 | .speed = config->speed ? : 1 * MHz, |
| 68 | .resource = scope, |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 69 | .device_select_polarity = SPI_POLARITY_LOW, |
| 70 | .wire_mode = SPI_4_WIRE_MODE, |
| 71 | .data_bit_length = 8, |
| 72 | .clock_phase = SPI_CLOCK_PHASE_FIRST, |
| 73 | .clock_polarity = SPI_POLARITY_LOW, |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 74 | }; |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 75 | int curr_index = 0; |
| 76 | int irq_gpio_index = -1; |
| 77 | int reset_gpio_index = -1; |
| 78 | int enable_gpio_index = -1; |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 79 | |
Karthikeyan Ramasubramanian | d1c0f95 | 2020-11-02 16:26:52 -0700 | [diff] [blame] | 80 | if (!scope) |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 81 | return; |
| 82 | |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 83 | if (spi_acpi_get_bus(dev) == -1) { |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 84 | printk(BIOS_ERR, "%s: ERROR: Cannot get bus for device.\n", |
Furquan Shaikh | 5bda642 | 2017-04-07 07:13:24 -0700 | [diff] [blame] | 85 | dev_path(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (!config->hid) { |
| 90 | printk(BIOS_ERR, "%s: ERROR: HID required.\n", dev_path(dev)); |
| 91 | return; |
| 92 | } |
| 93 | |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 94 | /* Device */ |
| 95 | acpigen_write_scope(scope); |
| 96 | acpigen_write_device(acpi_device_name(dev)); |
| 97 | acpigen_write_name_string("_HID", config->hid); |
| 98 | if (config->cid) |
| 99 | acpigen_write_name_string("_CID", config->cid); |
| 100 | acpigen_write_name_integer("_UID", config->uid); |
| 101 | if (config->desc) |
| 102 | acpigen_write_name_string("_DDN", config->desc); |
Hung-Te Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame] | 103 | acpigen_write_STA(acpi_device_status(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 104 | |
| 105 | /* Resources */ |
| 106 | acpigen_write_name("_CRS"); |
| 107 | acpigen_write_resourcetemplate_header(); |
| 108 | acpi_device_write_spi(&spi); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 109 | |
| 110 | /* Use either Interrupt() or GpioInt() */ |
| 111 | if (config->irq_gpio.pin_count) |
| 112 | irq_gpio_index = spi_acpi_write_gpio(&config->irq_gpio, |
| 113 | &curr_index); |
| 114 | else |
| 115 | acpi_device_write_interrupt(&config->irq); |
| 116 | |
| 117 | /* Add enable/reset GPIOs if needed */ |
| 118 | if (spi_acpi_add_gpios_to_crs(config)) { |
| 119 | reset_gpio_index = spi_acpi_write_gpio(&config->reset_gpio, |
| 120 | &curr_index); |
| 121 | enable_gpio_index = spi_acpi_write_gpio(&config->enable_gpio, |
| 122 | &curr_index); |
| 123 | } |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 124 | acpigen_write_resourcetemplate_footer(); |
| 125 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 126 | /* Wake capabilities */ |
| 127 | if (config->wake) { |
| 128 | acpigen_write_name_integer("_S0W", 4); |
| 129 | acpigen_write_PRW(config->wake, 3); |
| 130 | }; |
| 131 | |
| 132 | /* Write device properties if needed */ |
| 133 | if (config->compat_string || irq_gpio_index >= 0 || |
| 134 | reset_gpio_index >= 0 || enable_gpio_index >= 0) { |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 135 | struct acpi_dp *dsd = acpi_dp_new_table("_DSD"); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 136 | if (config->compat_string) |
| 137 | acpi_dp_add_string(dsd, "compatible", |
| 138 | config->compat_string); |
| 139 | if (irq_gpio_index >= 0) |
| 140 | acpi_dp_add_gpio(dsd, "irq-gpios", path, |
| 141 | irq_gpio_index, 0, |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 142 | config->irq_gpio.active_low); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 143 | if (reset_gpio_index >= 0) |
| 144 | acpi_dp_add_gpio(dsd, "reset-gpios", path, |
| 145 | reset_gpio_index, 0, |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 146 | config->reset_gpio.active_low); |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 147 | if (enable_gpio_index >= 0) |
| 148 | acpi_dp_add_gpio(dsd, "enable-gpios", path, |
| 149 | enable_gpio_index, 0, |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 150 | config->enable_gpio.active_low); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 151 | acpi_dp_write(dsd); |
| 152 | } |
| 153 | |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 154 | /* Power Resource */ |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 155 | if (config->has_power_resource) { |
| 156 | const struct acpi_power_res_params power_res_params = { |
| 157 | &config->reset_gpio, |
| 158 | config->reset_delay_ms, |
| 159 | config->reset_off_delay_ms, |
| 160 | &config->enable_gpio, |
| 161 | config->enable_delay_ms, |
| 162 | config->enable_off_delay_ms, |
| 163 | &config->stop_gpio, |
| 164 | config->stop_delay_ms, |
| 165 | config->stop_off_delay_ms |
| 166 | }; |
| 167 | acpi_device_add_power_res(&power_res_params); |
| 168 | } |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 169 | |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 170 | acpigen_pop_len(); /* Device */ |
| 171 | acpigen_pop_len(); /* Scope */ |
Duncan Laurie | c9db384 | 2017-02-17 17:14:35 -0800 | [diff] [blame] | 172 | |
| 173 | printk(BIOS_INFO, "%s: %s at %s\n", path, |
| 174 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 177 | static const char *spi_acpi_name(const struct device *dev) |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 178 | { |
| 179 | struct drivers_spi_acpi_config *config = dev->chip_info; |
| 180 | static char name[5]; |
| 181 | |
| 182 | if (config->name) |
| 183 | return config->name; |
| 184 | |
| 185 | snprintf(name, sizeof(name), "S%03.3X", spi_acpi_get_bus(dev)); |
| 186 | name[4] = '\0'; |
| 187 | return name; |
| 188 | } |
| 189 | |
| 190 | static struct device_operations spi_acpi_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 191 | .read_resources = noop_read_resources, |
| 192 | .set_resources = noop_set_resources, |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 193 | .acpi_name = spi_acpi_name, |
| 194 | .acpi_fill_ssdt = spi_acpi_fill_ssdt_generator, |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | static void spi_acpi_enable(struct device *dev) |
| 198 | { |
| 199 | dev->ops = &spi_acpi_ops; |
| 200 | } |
| 201 | |
| 202 | struct chip_operations drivers_spi_acpi_ops = { |
| 203 | CHIP_NAME("SPI Device") |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 204 | .enable_dev = spi_acpi_enable |
Furquan Shaikh | 20a91c9 | 2017-02-11 11:16:18 -0800 | [diff] [blame] | 205 | }; |