Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright 2016 Google Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <arch/acpi_device.h> |
| 17 | #include <arch/acpigen.h> |
| 18 | #include <console/console.h> |
| 19 | #include <device/i2c.h> |
| 20 | #include <device/device.h> |
| 21 | #include <device/path.h> |
| 22 | #include <gpio.h> |
| 23 | #include <stdint.h> |
| 24 | #include <string.h> |
| 25 | #include "chip.h" |
| 26 | |
| 27 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 28 | |
| 29 | static void i2c_generic_add_power_res(struct drivers_i2c_generic_config *config) |
| 30 | { |
| 31 | const char *power_res_dev_states[] = { "_PR0", "_PR3" }; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 32 | unsigned reset_gpio = config->reset_gpio.pins[0]; |
| 33 | unsigned enable_gpio = config->enable_gpio.pins[0]; |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 34 | |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame^] | 35 | if (!config->has_power_resource) |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 36 | return; |
| 37 | |
| 38 | if (!reset_gpio && !enable_gpio) |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 39 | return; |
| 40 | |
| 41 | /* PowerResource (PRIC, 0, 0) */ |
| 42 | acpigen_write_power_res("PRIC", 0, 0, power_res_dev_states, |
| 43 | ARRAY_SIZE(power_res_dev_states)); |
| 44 | |
| 45 | /* Method (_STA, 0, NotSerialized) { Return (0x1) } */ |
| 46 | acpigen_write_STA(0x1); |
| 47 | |
| 48 | /* Method (_ON, 0, Serialized) */ |
| 49 | acpigen_write_method_serialized("_ON", 0); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 50 | if (reset_gpio) |
| 51 | acpigen_soc_set_tx_gpio(reset_gpio); |
| 52 | if (enable_gpio) { |
| 53 | acpigen_soc_set_tx_gpio(enable_gpio); |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 54 | acpigen_write_sleep(config->enable_delay_ms); |
| 55 | } |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 56 | if (reset_gpio) { |
| 57 | acpigen_soc_clear_tx_gpio(reset_gpio); |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 58 | acpigen_write_sleep(config->reset_delay_ms); |
| 59 | } |
| 60 | acpigen_pop_len(); /* _ON method */ |
| 61 | |
| 62 | /* Method (_OFF, 0, Serialized) */ |
| 63 | acpigen_write_method_serialized("_OFF", 0); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 64 | if (reset_gpio) |
| 65 | acpigen_soc_set_tx_gpio(reset_gpio); |
| 66 | if (enable_gpio) |
| 67 | acpigen_soc_clear_tx_gpio(enable_gpio); |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 68 | acpigen_pop_len(); /* _OFF method */ |
| 69 | |
| 70 | acpigen_pop_len(); /* PowerResource PRIC */ |
| 71 | } |
| 72 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 73 | static bool i2c_generic_add_gpios_to_crs(struct drivers_i2c_generic_config *cfg) |
| 74 | { |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame^] | 75 | /* |
| 76 | * Return false if: |
| 77 | * 1. Request to explicitly disable export of GPIOs in CRS, or |
| 78 | * 2. Both reset and enable GPIOs are not provided. |
| 79 | */ |
| 80 | if (cfg->disable_gpio_export_in_crs || |
| 81 | ((cfg->reset_gpio.pin_count == 0) && |
| 82 | (cfg->enable_gpio.pin_count == 0))) |
| 83 | return false; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 84 | |
Furquan Shaikh | 71d830f | 2017-01-25 17:53:01 -0800 | [diff] [blame^] | 85 | return true; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | static int i2c_generic_write_gpio(struct acpi_gpio *gpio, int *curr_index) |
| 89 | { |
| 90 | int ret = -1; |
| 91 | |
| 92 | if (gpio->pin_count == 0) |
| 93 | return ret; |
| 94 | |
| 95 | acpi_device_write_gpio(gpio); |
| 96 | ret = *curr_index; |
| 97 | (*curr_index)++; |
| 98 | |
| 99 | return ret; |
| 100 | } |
| 101 | |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 102 | void i2c_generic_fill_ssdt(struct device *dev, |
Furquan Shaikh | fdc1b2e | 2016-12-13 21:50:32 -0800 | [diff] [blame] | 103 | void (*callback)(struct device *dev), |
| 104 | struct drivers_i2c_generic_config *config) |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 105 | { |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 106 | const char *scope = acpi_device_scope(dev); |
| 107 | struct acpi_i2c i2c = { |
| 108 | .address = dev->path.i2c.device, |
| 109 | .mode_10bit = dev->path.i2c.mode_10bit, |
Duncan Laurie | 14b2a4d | 2016-06-08 13:49:40 -0700 | [diff] [blame] | 110 | .speed = config->speed ? : I2C_SPEED_FAST, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 111 | .resource = scope, |
| 112 | }; |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 113 | struct acpi_dp *dsd = NULL; |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 114 | int curr_index = 0; |
| 115 | int reset_gpio_index = -1, enable_gpio_index = -1; |
| 116 | const char *path = acpi_device_path(dev); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 117 | |
| 118 | if (!dev->enabled || !scope) |
| 119 | return; |
| 120 | |
| 121 | if (!config->hid) { |
| 122 | printk(BIOS_ERR, "%s: ERROR: HID required\n", dev_path(dev)); |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | /* Device */ |
| 127 | acpigen_write_scope(scope); |
| 128 | acpigen_write_device(acpi_device_name(dev)); |
| 129 | acpigen_write_name_string("_HID", config->hid); |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 130 | if (config->cid) |
| 131 | acpigen_write_name_string("_CID", config->cid); |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 132 | acpigen_write_name_integer("_UID", config->uid); |
| 133 | acpigen_write_name_string("_DDN", config->desc); |
| 134 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 135 | |
| 136 | /* Resources */ |
| 137 | acpigen_write_name("_CRS"); |
| 138 | acpigen_write_resourcetemplate_header(); |
| 139 | acpi_device_write_i2c(&i2c); |
| 140 | acpi_device_write_interrupt(&config->irq); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 141 | if (i2c_generic_add_gpios_to_crs(config) == true) { |
| 142 | reset_gpio_index = i2c_generic_write_gpio(&config->reset_gpio, |
| 143 | &curr_index); |
| 144 | enable_gpio_index = i2c_generic_write_gpio(&config->enable_gpio, |
| 145 | &curr_index); |
| 146 | } |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 147 | acpigen_write_resourcetemplate_footer(); |
| 148 | |
| 149 | /* Wake capabilities */ |
| 150 | if (config->wake) { |
| 151 | acpigen_write_name_integer("_S0W", 4); |
| 152 | acpigen_write_PRW(config->wake, 3); |
| 153 | } |
| 154 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 155 | /* DSD */ |
| 156 | if (config->probed || (reset_gpio_index != -1) || |
| 157 | (enable_gpio_index != -1)) { |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 158 | dsd = acpi_dp_new_table("_DSD"); |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 159 | if (config->probed) |
| 160 | acpi_dp_add_integer(dsd, "linux,probed", 1); |
| 161 | if (reset_gpio_index != -1) |
| 162 | acpi_dp_add_gpio(dsd, "reset-gpios", path, |
| 163 | reset_gpio_index, 0, |
| 164 | config->reset_gpio.polarity); |
| 165 | if (enable_gpio_index != -1) |
| 166 | acpi_dp_add_gpio(dsd, "enable-gpios", path, |
| 167 | enable_gpio_index, 0, |
| 168 | config->enable_gpio.polarity); |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 169 | acpi_dp_write(dsd); |
| 170 | } |
| 171 | |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame] | 172 | /* Power Resource */ |
| 173 | i2c_generic_add_power_res(config); |
| 174 | |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 175 | /* Callback if any. */ |
| 176 | if (callback) |
| 177 | callback(dev); |
| 178 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 179 | acpigen_pop_len(); /* Device */ |
| 180 | acpigen_pop_len(); /* Scope */ |
| 181 | |
Furquan Shaikh | 98915bb | 2016-12-12 09:23:01 -0800 | [diff] [blame] | 182 | printk(BIOS_INFO, "%s: %s at %s\n", path, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 183 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
| 184 | } |
| 185 | |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 186 | static void i2c_generic_fill_ssdt_generator(struct device *dev) |
| 187 | { |
Furquan Shaikh | fdc1b2e | 2016-12-13 21:50:32 -0800 | [diff] [blame] | 188 | i2c_generic_fill_ssdt(dev, NULL, dev->chip_info); |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 191 | /* Use name specified in config or build one from I2C address */ |
| 192 | static const char *i2c_generic_acpi_name(struct device *dev) |
| 193 | { |
| 194 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 195 | static char name[5]; |
| 196 | |
| 197 | if (config->name) |
Furquan Shaikh | 3f4e627 | 2016-10-18 15:11:32 -0700 | [diff] [blame] | 198 | return config->name; |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 199 | |
| 200 | snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device); |
| 201 | name[4] = '\0'; |
| 202 | return name; |
| 203 | } |
| 204 | #endif |
| 205 | |
| 206 | static struct device_operations i2c_generic_ops = { |
| 207 | .read_resources = DEVICE_NOOP, |
| 208 | .set_resources = DEVICE_NOOP, |
| 209 | .enable_resources = DEVICE_NOOP, |
| 210 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) |
| 211 | .acpi_name = &i2c_generic_acpi_name, |
Furquan Shaikh | 1d33488 | 2016-10-21 16:24:07 -0700 | [diff] [blame] | 212 | .acpi_fill_ssdt_generator = &i2c_generic_fill_ssdt_generator, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 213 | #endif |
| 214 | }; |
| 215 | |
| 216 | static void i2c_generic_enable(struct device *dev) |
| 217 | { |
| 218 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 219 | |
| 220 | /* Check if device is present by reading GPIO */ |
| 221 | if (config->device_present_gpio) { |
| 222 | int present = gpio_get(config->device_present_gpio); |
| 223 | present ^= config->device_present_gpio_invert; |
| 224 | |
| 225 | printk(BIOS_INFO, "%s is %spresent\n", |
| 226 | dev->chip_ops->name, present ? "" : "not "); |
| 227 | |
| 228 | if (!present) { |
| 229 | dev->enabled = 0; |
| 230 | return; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | dev->ops = &i2c_generic_ops; |
| 235 | } |
| 236 | |
| 237 | struct chip_operations drivers_i2c_generic_ops = { |
| 238 | CHIP_NAME("I2C Device") |
| 239 | .enable_dev = &i2c_generic_enable |
| 240 | }; |