1 /* SPDX-License-Identifier: GPL-2.0-only */
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>
14 #if CONFIG(HAVE_ACPI_TABLES)
16 static bool i2c_generic_add_gpios_to_crs(struct drivers_i2c_generic_config
*cfg
)
20 * 1. GPIOs are exported via a power resource, or
21 * 2. Both reset and enable GPIOs are not provided.
23 if (cfg
->has_power_resource
||
24 ((cfg
->reset_gpio
.pin_count
== 0) &&
25 (cfg
->enable_gpio
.pin_count
== 0)))
31 void i2c_generic_fill_ssdt(const struct device
*dev
,
32 void (*callback
)(const struct device
*dev
),
33 struct drivers_i2c_generic_config
*config
)
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
,
42 struct acpi_dp
*dsd
= NULL
;
44 int reset_gpio_index
= -1, enable_gpio_index
= -1, irq_gpio_index
= -1;
45 const char *path
= acpi_device_path(dev
);
51 printk(BIOS_ERR
, "%s: HID required but not set\n", dev_path(dev
));
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",
60 config
->desc
? : dev
->chip_ops
->name
,
67 acpigen_write_scope(scope
);
68 acpigen_write_device(acpi_device_name(dev
));
69 acpigen_write_name_string("_HID", config
->hid
);
71 acpigen_write_name_string("_CID", config
->cid
);
73 acpigen_write_name_string("_SUB", config
->sub
);
74 acpigen_write_name_integer("_UID", config
->uid
);
76 acpigen_write_name_string("_DDN", config
->desc
);
77 acpigen_write_STA(acpi_device_status(dev
));
80 acpigen_write_name("_CRS");
81 acpigen_write_resourcetemplate_header();
82 acpi_device_write_i2c(&i2c
);
84 /* Use either Interrupt() or GpioInt() */
85 if (config
->irq_gpio
.pin_count
)
86 irq_gpio_index
= acpi_device_write_dsd_gpio(&config
->irq_gpio
,
89 acpi_device_write_interrupt(&config
->irq
);
91 if (i2c_generic_add_gpios_to_crs(config
) == true) {
92 reset_gpio_index
= acpi_device_write_dsd_gpio(&config
->reset_gpio
,
94 enable_gpio_index
= acpi_device_write_dsd_gpio(&config
->enable_gpio
,
97 acpigen_write_resourcetemplate_footer();
99 /* Wake capabilities */
101 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT
);
102 acpigen_write_PRW(config
->wake
, 3);
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
);
114 acpi_dp_add_integer(dsd
, "linux,probed", 1);
115 if (irq_gpio_index
!= -1)
116 acpi_dp_add_gpio(dsd
, "irq-gpios", path
,
118 config
->irq_gpio
.active_low
);
119 if (reset_gpio_index
!= -1)
120 acpi_dp_add_gpio(dsd
, "reset-gpios", path
,
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 /* Add generic property list */
128 acpi_dp_add_property_list(dsd
, config
->property_list
,
129 config
->property_count
);
134 if (config
->has_power_resource
) {
135 const struct acpi_power_res_params power_res_params
= {
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
,
143 config
->stop_delay_ms
,
144 config
->stop_off_delay_ms
146 acpi_device_add_power_res(&power_res_params
);
149 /* Rotation Matrix */
150 if (config
->has_rotation_matrix
) {
151 acpigen_write_method("ROTM", 0);
152 acpigen_write_name("RBUF");
153 acpigen_write_package(3);
155 for (int i
= 0; i
< 3; i
++) {
157 snprintf(matrix_row
, sizeof(matrix_row
), "%d %d %d",
158 config
->rotation_matrix
[i
* 3 + 0],
159 config
->rotation_matrix
[i
* 3 + 1],
160 config
->rotation_matrix
[i
* 3 + 2]);
162 acpigen_write_string(matrix_row
);
165 acpigen_write_return_namestr("RBUF");
170 /* Chip Direct Mapping */
171 if (config
->cdm_index
!= CDM_NOT_PRESENT
) {
172 acpigen_write_method("_CDM", 1);
173 acpigen_write_return_integer(0xabcd00 | config
->cdm_index
);
177 /* Callback if any. */
181 acpigen_pop_len(); /* Device */
182 acpigen_pop_len(); /* Scope */
184 printk(BIOS_INFO
, "%s: %s at %s\n", path
,
185 config
->desc
? : dev
->chip_ops
->name
, dev_path(dev
));
188 static void i2c_generic_fill_ssdt_generator(const struct device
*dev
)
190 i2c_generic_fill_ssdt(dev
, NULL
, dev
->chip_info
);
193 /* Use name specified in config or build one from I2C address */
194 static const char *i2c_generic_acpi_name(const struct device
*dev
)
196 struct drivers_i2c_generic_config
*config
= dev
->chip_info
;
202 snprintf(name
, sizeof(name
), "D%03.3X", dev
->path
.i2c
.device
);
208 static struct device_operations i2c_generic_ops
= {
209 .read_resources
= noop_read_resources
,
210 .set_resources
= noop_set_resources
,
211 #if CONFIG(HAVE_ACPI_TABLES)
212 .acpi_name
= i2c_generic_acpi_name
,
213 .acpi_fill_ssdt
= i2c_generic_fill_ssdt_generator
,
217 static void i2c_generic_enable(struct device
*dev
)
219 struct drivers_i2c_generic_config
*config
= dev
->chip_info
;
224 /* Check if device is present by reading GPIO */
225 if (config
->device_present_gpio
) {
226 int present
= gpio_get(config
->device_present_gpio
);
227 present
^= config
->device_present_gpio_invert
;
229 printk(BIOS_INFO
, "%s is %spresent\n",
230 dev
->chip_ops
->name
, present
? "" : "not ");
238 dev
->ops
= &i2c_generic_ops
;
240 /* Name the device as per description provided in devicetree */
242 dev
->name
= config
->desc
;
245 struct chip_operations drivers_i2c_generic_ops
= {
246 .name
= "I2C Device",
247 .enable_dev
= i2c_generic_enable