soc/intel/alderlake: Add ADL-P 4+4 with 28W TDP
[coreboot.git] / src / drivers / spi / acpi / acpi.c
blob9b6fd80023f24ef645b87a2321f53b2bb4be4155
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <acpi/acpi_device.h>
4 #include <acpi/acpigen.h>
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>
10 #include <string.h>
11 #include "chip.h"
13 static int spi_acpi_get_bus(const struct device *dev)
15 struct device *spi_dev;
16 struct device_operations *ops;
18 if (!dev->bus || !dev->bus->dev)
19 return -1;
21 spi_dev = dev->bus->dev;
22 ops = spi_dev->ops;
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);
28 return -1;
31 static bool spi_acpi_add_gpios_to_crs(struct drivers_spi_acpi_config *config)
34 * Return false if:
35 * 1. GPIOs are exported via a power resource, or
36 * 2. Both reset and enable GPIOs are not provided.
38 if (config->has_power_resource ||
39 ((config->reset_gpio.pin_count == 0) &&
40 (config->enable_gpio.pin_count == 0)))
41 return false;
43 return true;
46 static int spi_acpi_write_gpio(struct acpi_gpio *gpio, int *curr_index)
48 int ret = -1;
50 if (gpio->pin_count == 0)
51 return ret;
53 acpi_device_write_gpio(gpio);
54 ret = *curr_index;
55 (*curr_index)++;
57 return ret;
60 static void spi_acpi_fill_ssdt_generator(const struct device *dev)
62 struct drivers_spi_acpi_config *config = dev->chip_info;
63 const char *scope = acpi_device_scope(dev);
64 const char *path = acpi_device_path(dev);
65 struct acpi_spi spi = {
66 .device_select = dev->path.spi.cs,
67 .speed = config->speed ? : 1 * MHz,
68 .resource = scope,
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,
75 int curr_index = 0;
76 int irq_gpio_index = -1;
77 int reset_gpio_index = -1;
78 int enable_gpio_index = -1;
80 if (!scope)
81 return;
83 if (spi_acpi_get_bus(dev) == -1) {
84 printk(BIOS_ERR, "%s: ERROR: Cannot get bus for device.\n",
85 dev_path(dev));
86 return;
89 if (!config->hid) {
90 printk(BIOS_ERR, "%s: ERROR: HID required.\n", dev_path(dev));
91 return;
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);
103 acpigen_write_STA(acpi_device_status(dev));
105 /* Resources */
106 acpigen_write_name("_CRS");
107 acpigen_write_resourcetemplate_header();
108 acpi_device_write_spi(&spi);
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);
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);
124 acpigen_write_resourcetemplate_footer();
126 /* Wake capabilities */
127 if (config->wake) {
128 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT);
129 acpigen_write_PRW(config->wake, 3);
132 /* Write device properties if needed */
133 if (config->compat_string || irq_gpio_index >= 0 ||
134 reset_gpio_index >= 0 || enable_gpio_index >= 0) {
135 struct acpi_dp *dsd = acpi_dp_new_table("_DSD");
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,
142 config->irq_gpio.active_low);
143 if (reset_gpio_index >= 0)
144 acpi_dp_add_gpio(dsd, "reset-gpios", path,
145 reset_gpio_index, 0,
146 config->reset_gpio.active_low);
147 if (enable_gpio_index >= 0)
148 acpi_dp_add_gpio(dsd, "enable-gpios", path,
149 enable_gpio_index, 0,
150 config->enable_gpio.active_low);
151 acpi_dp_write(dsd);
154 /* Power Resource */
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
167 acpi_device_add_power_res(&power_res_params);
170 acpigen_pop_len(); /* Device */
171 acpigen_pop_len(); /* Scope */
173 printk(BIOS_INFO, "%s: %s at %s\n", path,
174 config->desc ? : dev->chip_ops->name, dev_path(dev));
177 static const char *spi_acpi_name(const struct device *dev)
179 struct drivers_spi_acpi_config *config = dev->chip_info;
180 static char name[5];
182 if (config->name)
183 return config->name;
185 snprintf(name, sizeof(name), "S%03.3X", spi_acpi_get_bus(dev));
186 name[4] = '\0';
187 return name;
190 static struct device_operations spi_acpi_ops = {
191 .read_resources = noop_read_resources,
192 .set_resources = noop_set_resources,
193 .acpi_name = spi_acpi_name,
194 .acpi_fill_ssdt = spi_acpi_fill_ssdt_generator,
197 static void spi_acpi_enable(struct device *dev)
199 dev->ops = &spi_acpi_ops;
202 struct chip_operations drivers_spi_acpi_ops = {
203 CHIP_NAME("SPI Device")
204 .enable_dev = spi_acpi_enable