mb/amb/birman*/gpio: remove configuration for VDD_MEM_VID[0,1]
[coreboot2.git] / src / drivers / i2c / generic / generic.c
blob260ea83b10c2bf72ece90f70c02d4358f56c1eae
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/i2c_bus.h>
7 #include <device/i2c_simple.h>
8 #include <device/device.h>
9 #include <gpio.h>
10 #include <stdio.h>
12 #include "chip.h"
14 #if CONFIG(HAVE_ACPI_TABLES)
16 static bool i2c_generic_add_gpios_to_crs(struct drivers_i2c_generic_config *cfg)
19 * Return false if:
20 * 1. GPIOs are exported via a power resource, or
21 * 2. Both reset and enable GPIOs are not provided.
23 if (cfg->has_power_resource ||
24 ((cfg->reset_gpio.pin_count == 0) &&
25 (cfg->enable_gpio.pin_count == 0)))
26 return false;
28 return true;
31 void i2c_generic_fill_ssdt(const struct device *dev,
32 void (*callback)(const struct device *dev),
33 struct drivers_i2c_generic_config *config)
35 const char *scope = acpi_device_scope(dev);
36 struct acpi_i2c i2c = {
37 .address = dev->path.i2c.device,
38 .mode_10bit = dev->path.i2c.mode_10bit,
39 .speed = config->speed ? : I2C_SPEED_FAST,
40 .resource = scope,
42 struct acpi_dp *dsd = NULL;
43 int curr_index = 0;
44 int reset_gpio_index = -1, enable_gpio_index = -1, irq_gpio_index = -1;
45 const char *path = acpi_device_path(dev);
47 if (!scope)
48 return;
50 if (!config->hid) {
51 printk(BIOS_ERR, "%s: HID required but not set\n", dev_path(dev));
52 return;
55 if (config->detect) {
56 struct device *const busdev = i2c_busdev(dev);
57 if (!i2c_dev_detect(busdev, dev->path.i2c.device)) {
58 printk(BIOS_SPEW, "%s: %s at %s -- NOT FOUND, skipping\n",
59 path,
60 config->desc ? : dev->chip_ops->name,
61 dev_path(dev));
62 return;
66 /* Device */
67 acpigen_write_scope(scope);
68 acpigen_write_device(acpi_device_name(dev));
69 acpigen_write_name_string("_HID", config->hid);
70 if (config->cid)
71 acpigen_write_name_string("_CID", config->cid);
72 if (config->sub)
73 acpigen_write_name_string("_SUB", config->sub);
74 acpigen_write_name_integer("_UID", config->uid);
75 if (config->desc)
76 acpigen_write_name_string("_DDN", config->desc);
77 acpigen_write_STA(acpi_device_status(dev));
79 /* Resources */
80 acpigen_write_name("_CRS");
81 acpigen_write_resourcetemplate_header();
82 acpi_device_write_i2c(&i2c);
84 /* Use either Interrupt() or GpioInt() */
85 if (config->irq_gpio.pin_count)
86 irq_gpio_index = acpi_device_write_dsd_gpio(&config->irq_gpio,
87 &curr_index);
88 else
89 acpi_device_write_interrupt(&config->irq);
91 if (i2c_generic_add_gpios_to_crs(config) == true) {
92 reset_gpio_index = acpi_device_write_dsd_gpio(&config->reset_gpio,
93 &curr_index);
94 enable_gpio_index = acpi_device_write_dsd_gpio(&config->enable_gpio,
95 &curr_index);
97 acpigen_write_resourcetemplate_footer();
99 /* Wake capabilities */
100 if (config->wake) {
101 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT);
102 acpigen_write_PRW(config->wake, 3);
105 /* DSD */
106 if (config->probed || config->property_count || config->compat_string ||
107 (reset_gpio_index != -1) ||
108 (enable_gpio_index != -1) || (irq_gpio_index != -1)) {
109 dsd = acpi_dp_new_table("_DSD");
110 if (config->compat_string)
111 acpi_dp_add_string(dsd, "compatible",
112 config->compat_string);
113 if (config->probed)
114 acpi_dp_add_integer(dsd, "linux,probed", 1);
115 if (irq_gpio_index != -1)
116 acpi_dp_add_gpio(dsd, "irq-gpios", path,
117 irq_gpio_index, 0,
118 config->irq_gpio.active_low);
119 if (reset_gpio_index != -1)
120 acpi_dp_add_gpio(dsd, "reset-gpios", path,
121 reset_gpio_index, 0,
122 config->reset_gpio.active_low);
123 if (enable_gpio_index != -1)
124 acpi_dp_add_gpio(dsd, "enable-gpios", path,
125 enable_gpio_index, 0,
126 config->enable_gpio.active_low);
127 /* Add generic property list */
128 acpi_dp_add_property_list(dsd, config->property_list,
129 config->property_count);
130 acpi_dp_write(dsd);
133 /* Power Resource */
134 if (config->has_power_resource) {
135 const struct acpi_power_res_params power_res_params = {
136 &config->reset_gpio,
137 config->reset_delay_ms,
138 config->reset_off_delay_ms,
139 &config->enable_gpio,
140 config->enable_delay_ms,
141 config->enable_off_delay_ms,
142 &config->stop_gpio,
143 config->stop_delay_ms,
144 config->stop_off_delay_ms
146 acpi_device_add_power_res(&power_res_params);
149 /* Rotation Matrix */
150 if (config->has_rotation_matrix) {
151 acpigen_write_method("ROTM", 0);
152 acpigen_write_name("RBUF");
153 acpigen_write_package(3);
155 for (int i = 0; i < 3; i++) {
156 char matrix_row[12];
157 snprintf(matrix_row, sizeof(matrix_row), "%d %d %d",
158 config->rotation_matrix[i * 3 + 0],
159 config->rotation_matrix[i * 3 + 1],
160 config->rotation_matrix[i * 3 + 2]);
162 acpigen_write_string(matrix_row);
164 acpigen_pop_len();
165 acpigen_write_return_namestr("RBUF");
167 acpigen_pop_len();
170 /* Chip Direct Mapping */
171 if (config->cdm_index != CDM_NOT_PRESENT) {
172 acpigen_write_method("_CDM", 1);
173 acpigen_write_return_integer(0xabcd00 | config->cdm_index);
174 acpigen_pop_len();
177 /* Callback if any. */
178 if (callback)
179 callback(dev);
181 acpigen_pop_len(); /* Device */
182 acpigen_pop_len(); /* Scope */
184 printk(BIOS_INFO, "%s: %s at %s\n", path,
185 config->desc ? : dev->chip_ops->name, dev_path(dev));
188 static void i2c_generic_fill_ssdt_generator(const struct device *dev)
190 i2c_generic_fill_ssdt(dev, NULL, dev->chip_info);
193 /* Use name specified in config or build one from I2C address */
194 static const char *i2c_generic_acpi_name(const struct device *dev)
196 struct drivers_i2c_generic_config *config = dev->chip_info;
197 static char name[5];
199 if (config->name)
200 return config->name;
202 snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device);
203 name[4] = '\0';
204 return name;
206 #endif
208 static struct device_operations i2c_generic_ops = {
209 .read_resources = noop_read_resources,
210 .set_resources = noop_set_resources,
211 #if CONFIG(HAVE_ACPI_TABLES)
212 .acpi_name = i2c_generic_acpi_name,
213 .acpi_fill_ssdt = i2c_generic_fill_ssdt_generator,
214 #endif
217 static void i2c_generic_enable(struct device *dev)
219 struct drivers_i2c_generic_config *config = dev->chip_info;
221 if (!config)
222 return;
224 /* Check if device is present by reading GPIO */
225 if (config->device_present_gpio) {
226 int present = gpio_get(config->device_present_gpio);
227 present ^= config->device_present_gpio_invert;
229 printk(BIOS_INFO, "%s is %spresent\n",
230 dev->chip_ops->name, present ? "" : "not ");
232 if (!present) {
233 dev->enabled = 0;
234 return;
238 dev->ops = &i2c_generic_ops;
240 /* Name the device as per description provided in devicetree */
241 if (config->desc)
242 dev->name = config->desc;
245 struct chip_operations drivers_i2c_generic_ops = {
246 .name = "I2C Device",
247 .enable_dev = i2c_generic_enable