Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame^] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | /* This file is part of the coreboot project. */ |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 3 | |
| 4 | #include <arch/acpi_device.h> |
| 5 | #include <arch/acpigen.h> |
| 6 | #include <console/console.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 7 | #include <device/i2c_simple.h> |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 8 | #include <device/device.h> |
| 9 | #include <device/path.h> |
| 10 | #include <gpio.h> |
| 11 | #include <stdint.h> |
| 12 | #include <string.h> |
| 13 | #include "chip.h" |
| 14 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 15 | #if CONFIG(HAVE_ACPI_TABLES) |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 16 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 17 | static bool i2c_generic_add_gpios_to_crs(struct drivers_i2c_generic_config *cfg) |
| 18 | { |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame] | 19 | /* |
| 20 | * Return false if: |
| 21 | * 1. Request to explicitly disable export of GPIOs in CRS, or |
| 22 | * 2. Both reset and enable GPIOs are not provided. |
| 23 | */ |
| 24 | if (cfg->disable_gpio_export_in_crs || |
| 25 | ((cfg->reset_gpio.pin_count == 0) && |
| 26 | (cfg->enable_gpio.pin_count == 0))) |
| 27 | return false; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 28 | |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame] | 29 | return true; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static int i2c_generic_write_gpio(struct acpi_gpio *gpio, int *curr_index) |
| 33 | { |
| 34 | int ret = -1; |
| 35 | |
| 36 | if (gpio->pin_count == 0) |
| 37 | return ret; |
| 38 | |
| 39 | acpi_device_write_gpio(gpio); |
| 40 | ret = *curr_index; |
| 41 | (*curr_index)++; |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 46 | void i2c_generic_fill_ssdt(struct device *dev, |
Furquan Shaikh | fdc1b2e | 2016-12-13 21:50:32 -0800 | [diff] [blame] | 47 | void (*callback)(struct device *dev), |
| 48 | struct drivers_i2c_generic_config *config) |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 49 | { |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 50 | const char *scope = acpi_device_scope(dev); |
| 51 | struct acpi_i2c i2c = { |
| 52 | .address = dev->path.i2c.device, |
| 53 | .mode_10bit = dev->path.i2c.mode_10bit, |
Duncan Laurie | 14b2a4d | 2016-06-08 13:49:40 -0700 | [diff] [blame] | 54 | .speed = config->speed ? : I2C_SPEED_FAST, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 55 | .resource = scope, |
| 56 | }; |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 57 | struct acpi_dp *dsd = NULL; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 58 | int curr_index = 0; |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 59 | int reset_gpio_index = -1, enable_gpio_index = -1, irq_gpio_index = -1; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 60 | const char *path = acpi_device_path(dev); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 61 | |
| 62 | if (!dev->enabled || !scope) |
| 63 | return; |
| 64 | |
| 65 | if (!config->hid) { |
| 66 | printk(BIOS_ERR, "%s: ERROR: HID required\n", dev_path(dev)); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | /* Device */ |
| 71 | acpigen_write_scope(scope); |
| 72 | acpigen_write_device(acpi_device_name(dev)); |
| 73 | acpigen_write_name_string("_HID", config->hid); |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 74 | if (config->cid) |
| 75 | acpigen_write_name_string("_CID", config->cid); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 76 | acpigen_write_name_integer("_UID", config->uid); |
| 77 | acpigen_write_name_string("_DDN", config->desc); |
Hung-Te Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame] | 78 | acpigen_write_STA(acpi_device_status(dev)); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 79 | |
| 80 | /* Resources */ |
| 81 | acpigen_write_name("_CRS"); |
| 82 | acpigen_write_resourcetemplate_header(); |
| 83 | acpi_device_write_i2c(&i2c); |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 84 | |
| 85 | /* Use either Interrupt() or GpioInt() */ |
| 86 | if (config->irq_gpio.pin_count) |
| 87 | irq_gpio_index = i2c_generic_write_gpio(&config->irq_gpio, |
| 88 | &curr_index); |
| 89 | else |
| 90 | acpi_device_write_interrupt(&config->irq); |
| 91 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 92 | if (i2c_generic_add_gpios_to_crs(config) == true) { |
| 93 | reset_gpio_index = i2c_generic_write_gpio(&config->reset_gpio, |
| 94 | &curr_index); |
| 95 | enable_gpio_index = i2c_generic_write_gpio(&config->enable_gpio, |
| 96 | &curr_index); |
| 97 | } |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 98 | acpigen_write_resourcetemplate_footer(); |
| 99 | |
| 100 | /* Wake capabilities */ |
| 101 | if (config->wake) { |
| 102 | acpigen_write_name_integer("_S0W", 4); |
| 103 | acpigen_write_PRW(config->wake, 3); |
| 104 | } |
| 105 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 106 | /* DSD */ |
Furquan Shaikh | 8ce36ac | 2019-08-09 07:59:06 -0700 | [diff] [blame] | 107 | if (config->probed || config->property_count || config->compat_string || |
Duncan Laurie | 1533a3c | 2017-08-29 08:28:58 -0700 | [diff] [blame] | 108 | (reset_gpio_index != -1) || |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 109 | (enable_gpio_index != -1) || (irq_gpio_index != -1)) { |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 110 | dsd = acpi_dp_new_table("_DSD"); |
Furquan Shaikh | 8ce36ac | 2019-08-09 07:59:06 -0700 | [diff] [blame] | 111 | if (config->compat_string) |
| 112 | acpi_dp_add_string(dsd, "compatible", |
| 113 | config->compat_string); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 114 | if (config->probed) |
| 115 | acpi_dp_add_integer(dsd, "linux,probed", 1); |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 116 | if (irq_gpio_index != -1) |
| 117 | acpi_dp_add_gpio(dsd, "irq-gpios", path, |
| 118 | irq_gpio_index, 0, |
| 119 | config->irq_gpio.polarity == |
| 120 | ACPI_GPIO_ACTIVE_LOW); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 121 | if (reset_gpio_index != -1) |
| 122 | acpi_dp_add_gpio(dsd, "reset-gpios", path, |
| 123 | reset_gpio_index, 0, |
| 124 | config->reset_gpio.polarity); |
| 125 | if (enable_gpio_index != -1) |
| 126 | acpi_dp_add_gpio(dsd, "enable-gpios", path, |
| 127 | enable_gpio_index, 0, |
| 128 | config->enable_gpio.polarity); |
Duncan Laurie | 1533a3c | 2017-08-29 08:28:58 -0700 | [diff] [blame] | 129 | /* Add generic property list */ |
| 130 | acpi_dp_add_property_list(dsd, config->property_list, |
| 131 | config->property_count); |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 132 | acpi_dp_write(dsd); |
| 133 | } |
| 134 | |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 135 | /* Power Resource */ |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 136 | if (config->has_power_resource) { |
| 137 | const struct acpi_power_res_params power_res_params = { |
| 138 | &config->reset_gpio, |
| 139 | config->reset_delay_ms, |
| 140 | config->reset_off_delay_ms, |
| 141 | &config->enable_gpio, |
| 142 | config->enable_delay_ms, |
| 143 | config->enable_off_delay_ms, |
| 144 | &config->stop_gpio, |
| 145 | config->stop_delay_ms, |
| 146 | config->stop_off_delay_ms |
| 147 | }; |
| 148 | acpi_device_add_power_res(&power_res_params); |
| 149 | } |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 150 | |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 151 | /* Callback if any. */ |
| 152 | if (callback) |
| 153 | callback(dev); |
| 154 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 155 | acpigen_pop_len(); /* Device */ |
| 156 | acpigen_pop_len(); /* Scope */ |
| 157 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 158 | printk(BIOS_INFO, "%s: %s at %s\n", path, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 159 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
| 160 | } |
| 161 | |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 162 | static void i2c_generic_fill_ssdt_generator(struct device *dev) |
| 163 | { |
Furquan Shaikh | fdc1b2e | 2016-12-13 21:50:32 -0800 | [diff] [blame] | 164 | i2c_generic_fill_ssdt(dev, NULL, dev->chip_info); |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 167 | /* Use name specified in config or build one from I2C address */ |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 168 | static const char *i2c_generic_acpi_name(const struct device *dev) |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 169 | { |
| 170 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 171 | static char name[5]; |
| 172 | |
| 173 | if (config->name) |
Furquan Shaikh | 3f4e627 | 2016-10-18 15:11:32 -0700 | [diff] [blame] | 174 | return config->name; |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 175 | |
| 176 | snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device); |
| 177 | name[4] = '\0'; |
| 178 | return name; |
| 179 | } |
| 180 | #endif |
| 181 | |
| 182 | static struct device_operations i2c_generic_ops = { |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 183 | .read_resources = DEVICE_NOOP, |
| 184 | .set_resources = DEVICE_NOOP, |
| 185 | .enable_resources = DEVICE_NOOP, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 186 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 187 | .acpi_name = i2c_generic_acpi_name, |
| 188 | .acpi_fill_ssdt = i2c_generic_fill_ssdt_generator, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 189 | #endif |
| 190 | }; |
| 191 | |
| 192 | static void i2c_generic_enable(struct device *dev) |
| 193 | { |
| 194 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 195 | |
Furquan Shaikh | 24681f1 | 2018-06-10 13:33:44 -0700 | [diff] [blame] | 196 | if (!config) |
| 197 | return; |
| 198 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 199 | /* Check if device is present by reading GPIO */ |
| 200 | if (config->device_present_gpio) { |
| 201 | int present = gpio_get(config->device_present_gpio); |
| 202 | present ^= config->device_present_gpio_invert; |
| 203 | |
| 204 | printk(BIOS_INFO, "%s is %spresent\n", |
| 205 | dev->chip_ops->name, present ? "" : "not "); |
| 206 | |
| 207 | if (!present) { |
| 208 | dev->enabled = 0; |
| 209 | return; |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | dev->ops = &i2c_generic_ops; |
Naresh G Solanki | 69e9e71 | 2018-06-04 17:45:19 +0530 | [diff] [blame] | 214 | |
| 215 | /* Name the device as per description provided in devicetree */ |
Furquan Shaikh | 24681f1 | 2018-06-10 13:33:44 -0700 | [diff] [blame] | 216 | if (config->desc) |
Naresh G Solanki | 69e9e71 | 2018-06-04 17:45:19 +0530 | [diff] [blame] | 217 | dev->name = config->desc; |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | struct chip_operations drivers_i2c_generic_ops = { |
| 221 | CHIP_NAME("I2C Device") |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 222 | .enable_dev = i2c_generic_enable |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 223 | }; |