| 1 |
|
| 2 |
|
| 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> |
| 11 |
|
| 12 |
#include "chip.h" |
| 13 |
|
| 14 |
#if CONFIG(HAVE_ACPI_TABLES) |
| 15 |
|
| 16 |
static bool i2c_generic_add_gpios_to_crs(struct drivers_i2c_generic_config *cfg) |
| 17 |
{ |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
if (cfg->has_power_resource || |
| 24 |
((cfg->reset_gpio.pin_count == 0) && |
| 25 |
(cfg->enable_gpio.pin_count == 0))) |
| 26 |
return false; |
| 27 |
|
| 28 |
return true; |
| 29 |
} |
| 30 |
|
| 31 |
void i2c_generic_fill_ssdt(const struct device *dev, |
| 32 |
void (*callback)(const struct device *dev), |
| 33 |
struct drivers_i2c_generic_config *config) |
| 34 |
{ |
| 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, |
| 41 |
}; |
| 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); |
| 46 |
|
| 47 |
if (!scope) |
| 48 |
return; |
| 49 |
|
| 50 |
if (!config->hid) { |
| 51 |
printk(BIOS_ERR, "%s: HID required but not set\n", dev_path(dev)); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 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; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 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)); |
| 78 |
|
| 79 |
|
| 80 |
acpigen_write_name("_CRS"); |
| 81 |
acpigen_write_resourcetemplate_header(); |
| 82 |
acpi_device_write_i2c(&i2c); |
| 83 |
|
| 84 |
|
| 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); |
| 90 |
|
| 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); |
| 96 |
} |
| 97 |
acpigen_write_resourcetemplate_footer(); |
| 98 |
|
| 99 |
|
| 100 |
if (config->wake) { |
| 101 |
acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT); |
| 102 |
acpigen_write_PRW(config->wake, 3); |
| 103 |
} |
| 104 |
|
| 105 |
|
| 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 |
|
| 128 |
acpi_dp_add_property_list(dsd, config->property_list, |
| 129 |
config->property_count); |
| 130 |
acpi_dp_write(dsd); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 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, |
| 145 |
config->use_gpio_for_status |
| 146 |
}; |
| 147 |
acpi_device_add_power_res(&power_res_params); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
if (config->has_rotation_matrix) { |
| 152 |
acpigen_write_method("ROTM", 0); |
| 153 |
acpigen_write_name("RBUF"); |
| 154 |
acpigen_write_package(3); |
| 155 |
|
| 156 |
for (int i = 0; i < 3; i++) { |
| 157 |
char matrix_row[12]; |
| 158 |
snprintf(matrix_row, sizeof(matrix_row), "%d %d %d", |
| 159 |
config->rotation_matrix[i * 3 + 0], |
| 160 |
config->rotation_matrix[i * 3 + 1], |
| 161 |
config->rotation_matrix[i * 3 + 2]); |
| 162 |
|
| 163 |
acpigen_write_string(matrix_row); |
| 164 |
} |
| 165 |
acpigen_pop_len(); |
| 166 |
acpigen_write_return_namestr("RBUF"); |
| 167 |
|
| 168 |
acpigen_pop_len(); |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
if (config->cdm_index != CDM_NOT_PRESENT) { |
| 173 |
acpigen_write_method("_CDM", 1); |
| 174 |
acpigen_write_return_integer(0xabcd00 | config->cdm_index); |
| 175 |
acpigen_pop_len(); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
if (callback) |
| 180 |
callback(dev); |
| 181 |
|
| 182 |
acpigen_pop_len(); |
| 183 |
acpigen_pop_len(); |
| 184 |
|
| 185 |
printk(BIOS_INFO, "%s: %s at %s\n", path, |
| 186 |
config->desc ? : dev->chip_ops->name, dev_path(dev)); |
| 187 |
} |
| 188 |
|
| 189 |
static void i2c_generic_fill_ssdt_generator(const struct device *dev) |
| 190 |
{ |
| 191 |
i2c_generic_fill_ssdt(dev, NULL, dev->chip_info); |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
static const char *i2c_generic_acpi_name(const struct device *dev) |
| 196 |
{ |
| 197 |
struct drivers_i2c_generic_config *config = dev->chip_info; |
| 198 |
static char name[5]; |
| 199 |
|
| 200 |
if (config->name) |
| 201 |
return config->name; |
| 202 |
|
| 203 |
snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device); |
| 204 |
name[4] = '\0'; |
| 205 |
return name; |
| 206 |
} |
| 207 |
#endif |
| 208 |
|
| 209 |
static struct device_operations i2c_generic_ops = { |
| 210 |
.read_resources = noop_read_resources, |
| 211 |
.set_resources = noop_set_resources, |
| 212 |
#if CONFIG(HAVE_ACPI_TABLES) |
| 213 |
.acpi_name = i2c_generic_acpi_name, |
| 214 |
.acpi_fill_ssdt = i2c_generic_fill_ssdt_generator, |
| 215 |
#endif |
| 216 |
}; |
| 217 |
|
| 218 |
static void i2c_generic_enable(struct device *dev) |
| 219 |
{ |
| 220 |
struct drivers_i2c_generic_config *config = dev->chip_info; |
| 221 |
|
| 222 |
if (!config) |
| 223 |
return; |
| 224 |
|
| 225 |
|
| 226 |
if (config->device_present_gpio) { |
| 227 |
int present = gpio_get(config->device_present_gpio); |
| 228 |
present ^= config->device_present_gpio_invert; |
| 229 |
|
| 230 |
printk(BIOS_INFO, "%s is %spresent\n", |
| 231 |
dev->chip_ops->name, present ? "" : "not "); |
| 232 |
|
| 233 |
if (!present) { |
| 234 |
dev->enabled = 0; |
| 235 |
return; |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
dev->ops = &i2c_generic_ops; |
| 240 |
|
| 241 |
|
| 242 |
if (config->desc) |
| 243 |
dev->name = config->desc; |
| 244 |
} |
| 245 |
|
| 246 |
struct chip_operations drivers_i2c_generic_ops = { |
| 247 |
.name = "I2C Device", |
| 248 |
.enable_dev = i2c_generic_enable |
| 249 |
}; |