Angel Pons | 8a3453f | 2020-04-02 23:48:19 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [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> |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 5 | #include <console/console.h> |
Matt DeVillier | c636142 | 2022-03-28 22:35:42 -0500 | [diff] [blame] | 6 | #include <device/i2c_bus.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> |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 9 | #include <gpio.h> |
Elyes Haouas | bdd03c2 | 2024-05-27 11:20:07 +0200 | [diff] [blame] | 10 | #include <stdio.h> |
Elyes Haouas | 08375b5 | 2024-05-27 11:36:52 +0200 | [diff] [blame^] | 11 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 12 | #include "chip.h" |
| 13 | |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 14 | #if CONFIG(HAVE_ACPI_TABLES) |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 15 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 16 | static bool i2c_generic_add_gpios_to_crs(struct drivers_i2c_generic_config *cfg) |
| 17 | { |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame] | 18 | /* |
| 19 | * Return false if: |
Matt DeVillier | 4902e9b | 2023-01-11 17:41:37 -0600 | [diff] [blame] | 20 | * 1. GPIOs are exported via a power resource, or |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame] | 21 | * 2. Both reset and enable GPIOs are not provided. |
| 22 | */ |
Matt DeVillier | 4902e9b | 2023-01-11 17:41:37 -0600 | [diff] [blame] | 23 | if (cfg->has_power_resource || |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame] | 24 | ((cfg->reset_gpio.pin_count == 0) && |
| 25 | (cfg->enable_gpio.pin_count == 0))) |
| 26 | return false; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 27 | |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame] | 28 | return true; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 29 | } |
| 30 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 31 | void i2c_generic_fill_ssdt(const struct device *dev, |
Furquan Shaikh | 8220c4b | 2020-04-24 21:44:27 -0700 | [diff] [blame] | 32 | void (*callback)(const struct device *dev), |
Furquan Shaikh | fdc1b2e | 2016-12-13 21:50:32 -0800 | [diff] [blame] | 33 | struct drivers_i2c_generic_config *config) |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 34 | { |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 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, |
Duncan Laurie | 14b2a4d | 2016-06-08 13:49:40 -0700 | [diff] [blame] | 39 | .speed = config->speed ? : I2C_SPEED_FAST, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 40 | .resource = scope, |
| 41 | }; |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 42 | struct acpi_dp *dsd = NULL; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 43 | int curr_index = 0; |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 44 | 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] | 45 | const char *path = acpi_device_path(dev); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 46 | |
Karthikeyan Ramasubramanian | d1c0f95 | 2020-11-02 16:26:52 -0700 | [diff] [blame] | 47 | if (!scope) |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 48 | return; |
| 49 | |
| 50 | if (!config->hid) { |
Matt DeVillier | 2494e93 | 2022-10-31 14:59:31 -0500 | [diff] [blame] | 51 | printk(BIOS_ERR, "%s: HID required but not set\n", dev_path(dev)); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 52 | return; |
| 53 | } |
| 54 | |
Matt DeVillier | c636142 | 2022-03-28 22:35:42 -0500 | [diff] [blame] | 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 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 66 | /* Device */ |
| 67 | acpigen_write_scope(scope); |
| 68 | acpigen_write_device(acpi_device_name(dev)); |
| 69 | acpigen_write_name_string("_HID", config->hid); |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 70 | if (config->cid) |
| 71 | acpigen_write_name_string("_CID", config->cid); |
Matt DeVillier | 2ca55f2 | 2023-01-18 18:14:09 -0600 | [diff] [blame] | 72 | if (config->sub) |
| 73 | acpigen_write_name_string("_SUB", config->sub); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 74 | acpigen_write_name_integer("_UID", config->uid); |
Matt DeVillier | 86dce8f | 2020-11-19 18:29:44 -0600 | [diff] [blame] | 75 | if (config->desc) |
| 76 | acpigen_write_name_string("_DDN", config->desc); |
Hung-Te Lin | b4be50c | 2018-09-10 10:55:49 +0800 | [diff] [blame] | 77 | acpigen_write_STA(acpi_device_status(dev)); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 78 | |
| 79 | /* Resources */ |
| 80 | acpigen_write_name("_CRS"); |
| 81 | acpigen_write_resourcetemplate_header(); |
| 82 | acpi_device_write_i2c(&i2c); |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 83 | |
| 84 | /* Use either Interrupt() or GpioInt() */ |
| 85 | if (config->irq_gpio.pin_count) |
Jianeng Ceng | 01344bc | 2024-04-09 21:20:20 +0800 | [diff] [blame] | 86 | irq_gpio_index = acpi_device_write_dsd_gpio(&config->irq_gpio, |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 87 | &curr_index); |
| 88 | else |
| 89 | acpi_device_write_interrupt(&config->irq); |
| 90 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 91 | if (i2c_generic_add_gpios_to_crs(config) == true) { |
Jianeng Ceng | 01344bc | 2024-04-09 21:20:20 +0800 | [diff] [blame] | 92 | reset_gpio_index = acpi_device_write_dsd_gpio(&config->reset_gpio, |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 93 | &curr_index); |
Jianeng Ceng | 01344bc | 2024-04-09 21:20:20 +0800 | [diff] [blame] | 94 | enable_gpio_index = acpi_device_write_dsd_gpio(&config->enable_gpio, |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 95 | &curr_index); |
| 96 | } |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 97 | acpigen_write_resourcetemplate_footer(); |
| 98 | |
| 99 | /* Wake capabilities */ |
| 100 | if (config->wake) { |
Tony Huang | 1282b00 | 2021-05-03 09:35:56 +0800 | [diff] [blame] | 101 | acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 102 | acpigen_write_PRW(config->wake, 3); |
| 103 | } |
| 104 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 105 | /* DSD */ |
Furquan Shaikh | 8ce36ac | 2019-08-09 07:59:06 -0700 | [diff] [blame] | 106 | if (config->probed || config->property_count || config->compat_string || |
Duncan Laurie | 1533a3c | 2017-08-29 08:28:58 -0700 | [diff] [blame] | 107 | (reset_gpio_index != -1) || |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 108 | (enable_gpio_index != -1) || (irq_gpio_index != -1)) { |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 109 | dsd = acpi_dp_new_table("_DSD"); |
Furquan Shaikh | 8ce36ac | 2019-08-09 07:59:06 -0700 | [diff] [blame] | 110 | if (config->compat_string) |
| 111 | acpi_dp_add_string(dsd, "compatible", |
| 112 | config->compat_string); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 113 | if (config->probed) |
| 114 | acpi_dp_add_integer(dsd, "linux,probed", 1); |
Duncan Laurie | b94e935 | 2017-03-15 11:25:31 -0700 | [diff] [blame] | 115 | if (irq_gpio_index != -1) |
| 116 | acpi_dp_add_gpio(dsd, "irq-gpios", path, |
| 117 | irq_gpio_index, 0, |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 118 | config->irq_gpio.active_low); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 119 | if (reset_gpio_index != -1) |
| 120 | acpi_dp_add_gpio(dsd, "reset-gpios", path, |
| 121 | reset_gpio_index, 0, |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 122 | config->reset_gpio.active_low); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 123 | if (enable_gpio_index != -1) |
| 124 | acpi_dp_add_gpio(dsd, "enable-gpios", path, |
| 125 | enable_gpio_index, 0, |
Furquan Shaikh | d2d5e44 | 2020-07-01 01:37:13 -0700 | [diff] [blame] | 126 | config->enable_gpio.active_low); |
Duncan Laurie | 1533a3c | 2017-08-29 08:28:58 -0700 | [diff] [blame] | 127 | /* Add generic property list */ |
| 128 | acpi_dp_add_property_list(dsd, config->property_list, |
| 129 | config->property_count); |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 130 | acpi_dp_write(dsd); |
| 131 | } |
| 132 | |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 133 | /* Power Resource */ |
Shelley Chen | a060339 | 2018-04-26 13:52:30 -0700 | [diff] [blame] | 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 | }; |
| 146 | acpi_device_add_power_res(&power_res_params); |
| 147 | } |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 148 | |
Sean Rhodes | bba6a21 | 2024-01-23 15:10:59 +0000 | [diff] [blame] | 149 | /* Rotation Matrix */ |
| 150 | if (config->has_rotation_matrix) { |
| 151 | acpigen_write_method("ROTM", 0); |
| 152 | acpigen_write_package(3); |
| 153 | |
| 154 | for (int i = 0; i < 3; i++) { |
| 155 | char matrix_row[12]; |
| 156 | snprintf(matrix_row, sizeof(matrix_row), "%d %d %d", |
| 157 | config->rotation_matrix[i * 3 + 0], |
| 158 | config->rotation_matrix[i * 3 + 1], |
| 159 | config->rotation_matrix[i * 3 + 2]); |
| 160 | |
| 161 | acpigen_write_string(matrix_row); |
| 162 | } |
| 163 | |
| 164 | acpigen_pop_len(); |
| 165 | acpigen_pop_len(); |
| 166 | } |
| 167 | |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 168 | /* Callback if any. */ |
| 169 | if (callback) |
| 170 | callback(dev); |
| 171 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 172 | acpigen_pop_len(); /* Device */ |
| 173 | acpigen_pop_len(); /* Scope */ |
| 174 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 175 | printk(BIOS_INFO, "%s: %s at %s\n", path, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 176 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
| 177 | } |
| 178 | |
Furquan Shaikh | 7536a39 | 2020-04-24 21:59:21 -0700 | [diff] [blame] | 179 | static void i2c_generic_fill_ssdt_generator(const struct device *dev) |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 180 | { |
Furquan Shaikh | fdc1b2e | 2016-12-13 21:50:32 -0800 | [diff] [blame] | 181 | i2c_generic_fill_ssdt(dev, NULL, dev->chip_info); |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 184 | /* Use name specified in config or build one from I2C address */ |
Aaron Durbin | aa090cb | 2017-09-13 16:01:52 -0600 | [diff] [blame] | 185 | static const char *i2c_generic_acpi_name(const struct device *dev) |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 186 | { |
| 187 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 188 | static char name[5]; |
| 189 | |
| 190 | if (config->name) |
Furquan Shaikh | 3f4e627 | 2016-10-18 15:11:32 -0700 | [diff] [blame] | 191 | return config->name; |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 192 | |
| 193 | snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device); |
| 194 | name[4] = '\0'; |
| 195 | return name; |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | static struct device_operations i2c_generic_ops = { |
Nico Huber | 2f8ba69 | 2020-04-05 14:05:24 +0200 | [diff] [blame] | 200 | .read_resources = noop_read_resources, |
| 201 | .set_resources = noop_set_resources, |
Julius Werner | cd49cce | 2019-03-05 16:53:33 -0800 | [diff] [blame] | 202 | #if CONFIG(HAVE_ACPI_TABLES) |
Nico Huber | 68680dd | 2020-03-31 17:34:52 +0200 | [diff] [blame] | 203 | .acpi_name = i2c_generic_acpi_name, |
| 204 | .acpi_fill_ssdt = i2c_generic_fill_ssdt_generator, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 205 | #endif |
| 206 | }; |
| 207 | |
| 208 | static void i2c_generic_enable(struct device *dev) |
| 209 | { |
| 210 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 211 | |
Furquan Shaikh | 24681f1 | 2018-06-10 13:33:44 -0700 | [diff] [blame] | 212 | if (!config) |
| 213 | return; |
| 214 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 215 | /* Check if device is present by reading GPIO */ |
| 216 | if (config->device_present_gpio) { |
| 217 | int present = gpio_get(config->device_present_gpio); |
| 218 | present ^= config->device_present_gpio_invert; |
| 219 | |
| 220 | printk(BIOS_INFO, "%s is %spresent\n", |
| 221 | dev->chip_ops->name, present ? "" : "not "); |
| 222 | |
| 223 | if (!present) { |
| 224 | dev->enabled = 0; |
| 225 | return; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | dev->ops = &i2c_generic_ops; |
Naresh G Solanki | 69e9e71 | 2018-06-04 17:45:19 +0530 | [diff] [blame] | 230 | |
| 231 | /* Name the device as per description provided in devicetree */ |
Furquan Shaikh | 24681f1 | 2018-06-10 13:33:44 -0700 | [diff] [blame] | 232 | if (config->desc) |
Naresh G Solanki | 69e9e71 | 2018-06-04 17:45:19 +0530 | [diff] [blame] | 233 | dev->name = config->desc; |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | struct chip_operations drivers_i2c_generic_ops = { |
Nicholas Sudsgaard | bfb11be | 2024-01-30 09:53:46 +0900 | [diff] [blame] | 237 | .name = "I2C Device", |
Elyes HAOUAS | 2aa3b16 | 2018-11-27 17:02:10 +0100 | [diff] [blame] | 238 | .enable_dev = i2c_generic_enable |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 239 | }; |