blob: afff9780654a0bdc12d0d01ab8b731cac16fe7db [file] [log] [blame]
Angel Pons8a3453f2020-04-02 23:48:19 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Duncan Laurie21a097a2016-05-11 09:50:59 -07002
Furquan Shaikh76cedd22020-05-02 10:24:23 -07003#include <acpi/acpi_device.h>
4#include <acpi/acpigen.h>
Duncan Laurie21a097a2016-05-11 09:50:59 -07005#include <console/console.h>
Matt DeVillierc6361422022-03-28 22:35:42 -05006#include <device/i2c_bus.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +02007#include <device/i2c_simple.h>
Duncan Laurie21a097a2016-05-11 09:50:59 -07008#include <device/device.h>
Duncan Laurie21a097a2016-05-11 09:50:59 -07009#include <gpio.h>
Duncan Laurie21a097a2016-05-11 09:50:59 -070010#include <string.h>
11#include "chip.h"
12
Julius Wernercd49cce2019-03-05 16:53:33 -080013#if CONFIG(HAVE_ACPI_TABLES)
Furquan Shaikh626ad202016-10-20 16:01:04 -070014
Furquan Shaikh98915bb2016-12-12 09:23:01 -080015static bool i2c_generic_add_gpios_to_crs(struct drivers_i2c_generic_config *cfg)
16{
Furquan Shaikh71d830f2017-01-25 17:53:01 -080017 /*
18 * Return false if:
Matt DeVillier4902e9b2023-01-11 17:41:37 -060019 * 1. GPIOs are exported via a power resource, or
Furquan Shaikh71d830f2017-01-25 17:53:01 -080020 * 2. Both reset and enable GPIOs are not provided.
21 */
Matt DeVillier4902e9b2023-01-11 17:41:37 -060022 if (cfg->has_power_resource ||
Furquan Shaikh71d830f2017-01-25 17:53:01 -080023 ((cfg->reset_gpio.pin_count == 0) &&
24 (cfg->enable_gpio.pin_count == 0)))
25 return false;
Furquan Shaikh98915bb2016-12-12 09:23:01 -080026
Furquan Shaikh71d830f2017-01-25 17:53:01 -080027 return true;
Furquan Shaikh98915bb2016-12-12 09:23:01 -080028}
29
30static int i2c_generic_write_gpio(struct acpi_gpio *gpio, int *curr_index)
31{
32 int ret = -1;
33
34 if (gpio->pin_count == 0)
35 return ret;
36
37 acpi_device_write_gpio(gpio);
38 ret = *curr_index;
39 (*curr_index)++;
40
41 return ret;
42}
43
Furquan Shaikh7536a392020-04-24 21:59:21 -070044void i2c_generic_fill_ssdt(const struct device *dev,
Furquan Shaikh8220c4b2020-04-24 21:44:27 -070045 void (*callback)(const struct device *dev),
Furquan Shaikhfdc1b2e2016-12-13 21:50:32 -080046 struct drivers_i2c_generic_config *config)
Duncan Laurie21a097a2016-05-11 09:50:59 -070047{
Duncan Laurie21a097a2016-05-11 09:50:59 -070048 const char *scope = acpi_device_scope(dev);
49 struct acpi_i2c i2c = {
50 .address = dev->path.i2c.device,
51 .mode_10bit = dev->path.i2c.mode_10bit,
Duncan Laurie14b2a4d2016-06-08 13:49:40 -070052 .speed = config->speed ? : I2C_SPEED_FAST,
Duncan Laurie21a097a2016-05-11 09:50:59 -070053 .resource = scope,
54 };
Duncan Lauriefbf2c79b2016-09-26 10:31:22 -070055 struct acpi_dp *dsd = NULL;
Furquan Shaikh98915bb2016-12-12 09:23:01 -080056 int curr_index = 0;
Duncan Laurieb94e9352017-03-15 11:25:31 -070057 int reset_gpio_index = -1, enable_gpio_index = -1, irq_gpio_index = -1;
Furquan Shaikh98915bb2016-12-12 09:23:01 -080058 const char *path = acpi_device_path(dev);
Duncan Laurie21a097a2016-05-11 09:50:59 -070059
Karthikeyan Ramasubramaniand1c0f952020-11-02 16:26:52 -070060 if (!scope)
Duncan Laurie21a097a2016-05-11 09:50:59 -070061 return;
62
63 if (!config->hid) {
Matt DeVillier2494e932022-10-31 14:59:31 -050064 printk(BIOS_ERR, "%s: HID required but not set\n", dev_path(dev));
Duncan Laurie21a097a2016-05-11 09:50:59 -070065 return;
66 }
67
Matt DeVillierc6361422022-03-28 22:35:42 -050068 if (config->detect) {
69 struct device *const busdev = i2c_busdev(dev);
70 if (!i2c_dev_detect(busdev, dev->path.i2c.device)) {
71 printk(BIOS_SPEW, "%s: %s at %s -- NOT FOUND, skipping\n",
72 path,
73 config->desc ? : dev->chip_ops->name,
74 dev_path(dev));
75 return;
76 }
77 }
78
Duncan Laurie21a097a2016-05-11 09:50:59 -070079 /* Device */
80 acpigen_write_scope(scope);
81 acpigen_write_device(acpi_device_name(dev));
82 acpigen_write_name_string("_HID", config->hid);
Furquan Shaikh1d334882016-10-21 16:24:07 -070083 if (config->cid)
84 acpigen_write_name_string("_CID", config->cid);
Matt DeVillier2ca55f22023-01-18 18:14:09 -060085 if (config->sub)
86 acpigen_write_name_string("_SUB", config->sub);
Duncan Laurie21a097a2016-05-11 09:50:59 -070087 acpigen_write_name_integer("_UID", config->uid);
Matt DeVillier86dce8f2020-11-19 18:29:44 -060088 if (config->desc)
89 acpigen_write_name_string("_DDN", config->desc);
Hung-Te Linb4be50c2018-09-10 10:55:49 +080090 acpigen_write_STA(acpi_device_status(dev));
Duncan Laurie21a097a2016-05-11 09:50:59 -070091
92 /* Resources */
93 acpigen_write_name("_CRS");
94 acpigen_write_resourcetemplate_header();
95 acpi_device_write_i2c(&i2c);
Duncan Laurieb94e9352017-03-15 11:25:31 -070096
97 /* Use either Interrupt() or GpioInt() */
98 if (config->irq_gpio.pin_count)
99 irq_gpio_index = i2c_generic_write_gpio(&config->irq_gpio,
100 &curr_index);
101 else
102 acpi_device_write_interrupt(&config->irq);
103
Furquan Shaikh98915bb2016-12-12 09:23:01 -0800104 if (i2c_generic_add_gpios_to_crs(config) == true) {
105 reset_gpio_index = i2c_generic_write_gpio(&config->reset_gpio,
106 &curr_index);
107 enable_gpio_index = i2c_generic_write_gpio(&config->enable_gpio,
108 &curr_index);
109 }
Duncan Laurie21a097a2016-05-11 09:50:59 -0700110 acpigen_write_resourcetemplate_footer();
111
112 /* Wake capabilities */
113 if (config->wake) {
Tony Huang1282b002021-05-03 09:35:56 +0800114 acpigen_write_name_integer("_S0W", ACPI_DEVICE_SLEEP_D3_HOT);
Duncan Laurie21a097a2016-05-11 09:50:59 -0700115 acpigen_write_PRW(config->wake, 3);
116 }
117
Furquan Shaikh98915bb2016-12-12 09:23:01 -0800118 /* DSD */
Furquan Shaikh8ce36ac2019-08-09 07:59:06 -0700119 if (config->probed || config->property_count || config->compat_string ||
Duncan Laurie1533a3c2017-08-29 08:28:58 -0700120 (reset_gpio_index != -1) ||
Duncan Laurieb94e9352017-03-15 11:25:31 -0700121 (enable_gpio_index != -1) || (irq_gpio_index != -1)) {
Duncan Lauriefbf2c79b2016-09-26 10:31:22 -0700122 dsd = acpi_dp_new_table("_DSD");
Furquan Shaikh8ce36ac2019-08-09 07:59:06 -0700123 if (config->compat_string)
124 acpi_dp_add_string(dsd, "compatible",
125 config->compat_string);
Furquan Shaikh98915bb2016-12-12 09:23:01 -0800126 if (config->probed)
127 acpi_dp_add_integer(dsd, "linux,probed", 1);
Duncan Laurieb94e9352017-03-15 11:25:31 -0700128 if (irq_gpio_index != -1)
129 acpi_dp_add_gpio(dsd, "irq-gpios", path,
130 irq_gpio_index, 0,
Furquan Shaikhd2d5e442020-07-01 01:37:13 -0700131 config->irq_gpio.active_low);
Furquan Shaikh98915bb2016-12-12 09:23:01 -0800132 if (reset_gpio_index != -1)
133 acpi_dp_add_gpio(dsd, "reset-gpios", path,
134 reset_gpio_index, 0,
Furquan Shaikhd2d5e442020-07-01 01:37:13 -0700135 config->reset_gpio.active_low);
Furquan Shaikh98915bb2016-12-12 09:23:01 -0800136 if (enable_gpio_index != -1)
137 acpi_dp_add_gpio(dsd, "enable-gpios", path,
138 enable_gpio_index, 0,
Furquan Shaikhd2d5e442020-07-01 01:37:13 -0700139 config->enable_gpio.active_low);
Duncan Laurie1533a3c2017-08-29 08:28:58 -0700140 /* Add generic property list */
141 acpi_dp_add_property_list(dsd, config->property_list,
142 config->property_count);
Duncan Lauriefbf2c79b2016-09-26 10:31:22 -0700143 acpi_dp_write(dsd);
144 }
145
Furquan Shaikh626ad202016-10-20 16:01:04 -0700146 /* Power Resource */
Shelley Chena0603392018-04-26 13:52:30 -0700147 if (config->has_power_resource) {
148 const struct acpi_power_res_params power_res_params = {
149 &config->reset_gpio,
150 config->reset_delay_ms,
151 config->reset_off_delay_ms,
152 &config->enable_gpio,
153 config->enable_delay_ms,
154 config->enable_off_delay_ms,
155 &config->stop_gpio,
156 config->stop_delay_ms,
157 config->stop_off_delay_ms
158 };
159 acpi_device_add_power_res(&power_res_params);
160 }
Furquan Shaikh626ad202016-10-20 16:01:04 -0700161
Sean Rhodesbba6a212024-01-23 15:10:59 +0000162 /* Rotation Matrix */
163 if (config->has_rotation_matrix) {
164 acpigen_write_method("ROTM", 0);
165 acpigen_write_package(3);
166
167 for (int i = 0; i < 3; i++) {
168 char matrix_row[12];
169 snprintf(matrix_row, sizeof(matrix_row), "%d %d %d",
170 config->rotation_matrix[i * 3 + 0],
171 config->rotation_matrix[i * 3 + 1],
172 config->rotation_matrix[i * 3 + 2]);
173
174 acpigen_write_string(matrix_row);
175 }
176
177 acpigen_pop_len();
178 acpigen_pop_len();
179 }
180
Furquan Shaikh1d334882016-10-21 16:24:07 -0700181 /* Callback if any. */
182 if (callback)
183 callback(dev);
184
Duncan Laurie21a097a2016-05-11 09:50:59 -0700185 acpigen_pop_len(); /* Device */
186 acpigen_pop_len(); /* Scope */
187
Furquan Shaikh98915bb2016-12-12 09:23:01 -0800188 printk(BIOS_INFO, "%s: %s at %s\n", path,
Duncan Laurie21a097a2016-05-11 09:50:59 -0700189 config->desc ? : dev->chip_ops->name, dev_path(dev));
190}
191
Furquan Shaikh7536a392020-04-24 21:59:21 -0700192static void i2c_generic_fill_ssdt_generator(const struct device *dev)
Furquan Shaikh1d334882016-10-21 16:24:07 -0700193{
Furquan Shaikhfdc1b2e2016-12-13 21:50:32 -0800194 i2c_generic_fill_ssdt(dev, NULL, dev->chip_info);
Furquan Shaikh1d334882016-10-21 16:24:07 -0700195}
196
Duncan Laurie21a097a2016-05-11 09:50:59 -0700197/* Use name specified in config or build one from I2C address */
Aaron Durbinaa090cb2017-09-13 16:01:52 -0600198static const char *i2c_generic_acpi_name(const struct device *dev)
Duncan Laurie21a097a2016-05-11 09:50:59 -0700199{
200 struct drivers_i2c_generic_config *config = dev->chip_info;
201 static char name[5];
202
203 if (config->name)
Furquan Shaikh3f4e6272016-10-18 15:11:32 -0700204 return config->name;
Duncan Laurie21a097a2016-05-11 09:50:59 -0700205
206 snprintf(name, sizeof(name), "D%03.3X", dev->path.i2c.device);
207 name[4] = '\0';
208 return name;
209}
210#endif
211
212static struct device_operations i2c_generic_ops = {
Nico Huber2f8ba692020-04-05 14:05:24 +0200213 .read_resources = noop_read_resources,
214 .set_resources = noop_set_resources,
Julius Wernercd49cce2019-03-05 16:53:33 -0800215#if CONFIG(HAVE_ACPI_TABLES)
Nico Huber68680dd2020-03-31 17:34:52 +0200216 .acpi_name = i2c_generic_acpi_name,
217 .acpi_fill_ssdt = i2c_generic_fill_ssdt_generator,
Duncan Laurie21a097a2016-05-11 09:50:59 -0700218#endif
219};
220
221static void i2c_generic_enable(struct device *dev)
222{
223 struct drivers_i2c_generic_config *config = dev->chip_info;
224
Furquan Shaikh24681f12018-06-10 13:33:44 -0700225 if (!config)
226 return;
227
Duncan Laurie21a097a2016-05-11 09:50:59 -0700228 /* Check if device is present by reading GPIO */
229 if (config->device_present_gpio) {
230 int present = gpio_get(config->device_present_gpio);
231 present ^= config->device_present_gpio_invert;
232
233 printk(BIOS_INFO, "%s is %spresent\n",
234 dev->chip_ops->name, present ? "" : "not ");
235
236 if (!present) {
237 dev->enabled = 0;
238 return;
239 }
240 }
241
242 dev->ops = &i2c_generic_ops;
Naresh G Solanki69e9e712018-06-04 17:45:19 +0530243
244 /* Name the device as per description provided in devicetree */
Furquan Shaikh24681f12018-06-10 13:33:44 -0700245 if (config->desc)
Naresh G Solanki69e9e712018-06-04 17:45:19 +0530246 dev->name = config->desc;
Duncan Laurie21a097a2016-05-11 09:50:59 -0700247}
248
249struct chip_operations drivers_i2c_generic_ops = {
Nicholas Sudsgaardbfb11be2024-01-30 09:53:46 +0900250 .name = "I2C Device",
Elyes HAOUAS2aa3b162018-11-27 17:02:10 +0100251 .enable_dev = i2c_generic_enable
Duncan Laurie21a097a2016-05-11 09:50:59 -0700252};