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" }; |
| 32 | |
| 33 | if (!config->reset_gpio && !config->enable_gpio) |
| 34 | return; |
| 35 | |
| 36 | /* PowerResource (PRIC, 0, 0) */ |
| 37 | acpigen_write_power_res("PRIC", 0, 0, power_res_dev_states, |
| 38 | ARRAY_SIZE(power_res_dev_states)); |
| 39 | |
| 40 | /* Method (_STA, 0, NotSerialized) { Return (0x1) } */ |
| 41 | acpigen_write_STA(0x1); |
| 42 | |
| 43 | /* Method (_ON, 0, Serialized) */ |
| 44 | acpigen_write_method_serialized("_ON", 0); |
| 45 | if (config->reset_gpio) |
| 46 | acpigen_soc_set_tx_gpio(config->reset_gpio); |
| 47 | if (config->enable_gpio) { |
| 48 | acpigen_soc_set_tx_gpio(config->enable_gpio); |
| 49 | acpigen_write_sleep(config->enable_delay_ms); |
| 50 | } |
| 51 | if (config->reset_gpio) { |
| 52 | acpigen_soc_clear_tx_gpio(config->reset_gpio); |
| 53 | acpigen_write_sleep(config->reset_delay_ms); |
| 54 | } |
| 55 | acpigen_pop_len(); /* _ON method */ |
| 56 | |
| 57 | /* Method (_OFF, 0, Serialized) */ |
| 58 | acpigen_write_method_serialized("_OFF", 0); |
| 59 | if (config->reset_gpio) |
| 60 | acpigen_soc_set_tx_gpio(config->reset_gpio); |
| 61 | if (config->enable_gpio) |
| 62 | acpigen_soc_clear_tx_gpio(config->enable_gpio); |
| 63 | acpigen_pop_len(); /* _OFF method */ |
| 64 | |
| 65 | acpigen_pop_len(); /* PowerResource PRIC */ |
| 66 | } |
| 67 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 68 | static void i2c_generic_fill_ssdt(struct device *dev) |
| 69 | { |
| 70 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 71 | const char *scope = acpi_device_scope(dev); |
| 72 | struct acpi_i2c i2c = { |
| 73 | .address = dev->path.i2c.device, |
| 74 | .mode_10bit = dev->path.i2c.mode_10bit, |
Duncan Laurie | 14b2a4d | 2016-06-08 13:49:40 -0700 | [diff] [blame] | 75 | .speed = config->speed ? : I2C_SPEED_FAST, |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 76 | .resource = scope, |
| 77 | }; |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 78 | struct acpi_dp *dsd = NULL; |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 79 | |
| 80 | if (!dev->enabled || !scope) |
| 81 | return; |
| 82 | |
| 83 | if (!config->hid) { |
| 84 | printk(BIOS_ERR, "%s: ERROR: HID required\n", dev_path(dev)); |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | /* Device */ |
| 89 | acpigen_write_scope(scope); |
| 90 | acpigen_write_device(acpi_device_name(dev)); |
| 91 | acpigen_write_name_string("_HID", config->hid); |
| 92 | acpigen_write_name_integer("_UID", config->uid); |
| 93 | acpigen_write_name_string("_DDN", config->desc); |
| 94 | acpigen_write_STA(ACPI_STATUS_DEVICE_ALL_ON); |
| 95 | |
| 96 | /* Resources */ |
| 97 | acpigen_write_name("_CRS"); |
| 98 | acpigen_write_resourcetemplate_header(); |
| 99 | acpi_device_write_i2c(&i2c); |
| 100 | acpi_device_write_interrupt(&config->irq); |
| 101 | acpigen_write_resourcetemplate_footer(); |
| 102 | |
| 103 | /* Wake capabilities */ |
| 104 | if (config->wake) { |
| 105 | acpigen_write_name_integer("_S0W", 4); |
| 106 | acpigen_write_PRW(config->wake, 3); |
| 107 | } |
| 108 | |
Duncan Laurie | fbf2c79b | 2016-09-26 10:31:22 -0700 | [diff] [blame] | 109 | if (config->probed) { |
| 110 | dsd = acpi_dp_new_table("_DSD"); |
| 111 | acpi_dp_add_integer(dsd, "linux,probed", 1); |
| 112 | acpi_dp_write(dsd); |
| 113 | } |
| 114 | |
Furquan Shaikh | 626ad20 | 2016-10-20 16:01:04 -0700 | [diff] [blame^] | 115 | /* Power Resource */ |
| 116 | i2c_generic_add_power_res(config); |
| 117 | |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 118 | acpigen_pop_len(); /* Device */ |
| 119 | acpigen_pop_len(); /* Scope */ |
| 120 | |
| 121 | printk(BIOS_INFO, "%s: %s at %s\n", acpi_device_path(dev), |
| 122 | config->desc ? : dev->chip_ops->name, dev_path(dev)); |
| 123 | } |
| 124 | |
| 125 | /* Use name specified in config or build one from I2C address */ |
| 126 | static const char *i2c_generic_acpi_name(struct device *dev) |
| 127 | { |
| 128 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 129 | static char name[5]; |
| 130 | |
| 131 | if (config->name) |
Furquan Shaikh | 3f4e627 | 2016-10-18 15:11:32 -0700 | [diff] [blame] | 132 | return config->name; |
Duncan Laurie | 21a097a | 2016-05-11 09:50:59 -0700 | [diff] [blame] | 133 | |
| 134 | snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device); |
| 135 | name[4] = '\0'; |
| 136 | return name; |
| 137 | } |
| 138 | #endif |
| 139 | |
| 140 | static struct device_operations i2c_generic_ops = { |
| 141 | .read_resources = DEVICE_NOOP, |
| 142 | .set_resources = DEVICE_NOOP, |
| 143 | .enable_resources = DEVICE_NOOP, |
| 144 | #if IS_ENABLED(CONFIG_HAVE_ACPI_TABLES) |
| 145 | .acpi_name = &i2c_generic_acpi_name, |
| 146 | .acpi_fill_ssdt_generator = &i2c_generic_fill_ssdt, |
| 147 | #endif |
| 148 | }; |
| 149 | |
| 150 | static void i2c_generic_enable(struct device *dev) |
| 151 | { |
| 152 | struct drivers_i2c_generic_config *config = dev->chip_info; |
| 153 | |
| 154 | /* Check if device is present by reading GPIO */ |
| 155 | if (config->device_present_gpio) { |
| 156 | int present = gpio_get(config->device_present_gpio); |
| 157 | present ^= config->device_present_gpio_invert; |
| 158 | |
| 159 | printk(BIOS_INFO, "%s is %spresent\n", |
| 160 | dev->chip_ops->name, present ? "" : "not "); |
| 161 | |
| 162 | if (!present) { |
| 163 | dev->enabled = 0; |
| 164 | return; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | dev->ops = &i2c_generic_ops; |
| 169 | } |
| 170 | |
| 171 | struct chip_operations drivers_i2c_generic_ops = { |
| 172 | CHIP_NAME("I2C Device") |
| 173 | .enable_dev = &i2c_generic_enable |
| 174 | }; |