1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <acpi/acpigen.h>
4 #include <device/device.h>
5 #include <device/pci.h>
6 #include <device/pci_ids.h>
7
8 #include "chip.h"
9
10 #define ACPI_DSM_PRIVACY_SCREEN_UUID "C7033113-8720-4CEB-9090-9D52B3E52D73"
11
12 #define ACPI_METHOD_EPS_PRESENT "EPSP"
13 #define ACPI_METHOD_EPS_STATE "EPSS"
14 #define ACPI_METHOD_EPS_ENABLE "EPSE"
15 #define ACPI_METHOD_EPS_DISABLE "EPSD"
16
privacy_screen_detect_cb(void * arg)17 static void privacy_screen_detect_cb(void *arg)
18 {
19 struct drivers_gfx_generic_privacy_screen_config *config = arg;
20
21 acpigen_write_store();
22 acpigen_emit_namestring(config->detect_function);
23 acpigen_emit_byte(LOCAL2_OP);
24 acpigen_write_if_lequal_op_int(LOCAL2_OP, 1);
25 acpigen_write_return_singleton_buffer(0xF);
26 acpigen_pop_len();
27 }
privacy_screen_get_status_cb(void * arg)28 static void privacy_screen_get_status_cb(void *arg)
29 {
30 struct drivers_gfx_generic_privacy_screen_config *config = arg;
31
32 acpigen_emit_byte(RETURN_OP);
33 acpigen_emit_namestring(config->status_function);
34 }
privacy_screen_enable_cb(void * arg)35 static void privacy_screen_enable_cb(void *arg)
36 {
37 struct drivers_gfx_generic_privacy_screen_config *config = arg;
38
39 acpigen_emit_namestring(config->enable_function);
40 }
privacy_screen_disable_cb(void * arg)41 static void privacy_screen_disable_cb(void *arg)
42 {
43 struct drivers_gfx_generic_privacy_screen_config *config = arg;
44
45 acpigen_emit_namestring(config->disable_function);
46 }
47
48 static void (*privacy_screen_callbacks[])(void *) = {
49 privacy_screen_detect_cb,
50 privacy_screen_get_status_cb,
51 privacy_screen_enable_cb,
52 privacy_screen_disable_cb,
53 };
54
privacy_gpio_acpigen(struct acpi_gpio * gpio)55 static void privacy_gpio_acpigen(struct acpi_gpio *gpio)
56 {
57 /* EPS Present */
58 acpigen_write_method(ACPI_METHOD_EPS_PRESENT, 0);
59 acpigen_write_return_byte(1);
60 acpigen_pop_len();
61
62 /* EPS State */
63 acpigen_write_method(ACPI_METHOD_EPS_STATE, 0);
64 acpigen_get_rx_gpio(gpio);
65 acpigen_emit_byte(RETURN_OP);
66 acpigen_emit_byte(LOCAL0_OP);
67 acpigen_pop_len();
68
69 /* EPS Enable */
70 acpigen_write_method(ACPI_METHOD_EPS_ENABLE, 0);
71 acpigen_enable_tx_gpio(gpio);
72 acpigen_pop_len();
73
74 /* EPS Disable */
75 acpigen_write_method(ACPI_METHOD_EPS_DISABLE, 0);
76 acpigen_disable_tx_gpio(gpio);
77 acpigen_pop_len();
78 }
79
gfx_fill_privacy_screen_dsm(struct drivers_gfx_generic_privacy_screen_config * privacy)80 static void gfx_fill_privacy_screen_dsm(
81 struct drivers_gfx_generic_privacy_screen_config *privacy)
82 {
83 if (!privacy->enabled)
84 return;
85
86 /* Populate ACPI methods, if EPS controlled via gpio */
87 if (privacy->gpio.pin_count == 1) {
88 privacy_gpio_acpigen(&privacy->gpio);
89 privacy->detect_function = ACPI_METHOD_EPS_PRESENT;
90 privacy->status_function = ACPI_METHOD_EPS_STATE;
91 privacy->enable_function = ACPI_METHOD_EPS_ENABLE;
92 privacy->disable_function = ACPI_METHOD_EPS_DISABLE;
93 }
94
95 acpigen_write_dsm(ACPI_DSM_PRIVACY_SCREEN_UUID,
96 privacy_screen_callbacks,
97 ARRAY_SIZE(privacy_screen_callbacks),
98 privacy);
99 }
100
gfx_fill_ssdt_generator(const struct device * dev)101 static void gfx_fill_ssdt_generator(const struct device *dev)
102 {
103 size_t i;
104 struct drivers_gfx_generic_config *config = dev->chip_info;
105
106 const char *scope = acpi_device_scope(dev);
107
108 if (!scope)
109 return;
110
111 acpigen_write_scope(scope);
112
113 /* Method (_DOD, 0) */
114 acpigen_write_method("_DOD", 0);
115 acpigen_emit_byte(RETURN_OP);
116 acpigen_write_package(config->device_count);
117 for (i = 0; i < config->device_count; i++) {
118 /* Generate the Device ID if addr = 0 and type != 0 */
119 if (!config->device[i].addr && config->device[i].type)
120 /* Though not strictly necessary, set the display index and
121 port attachment to the device index, to ensure uniqueness */
122 config->device[i].addr = (config->device[i].type << 8) | (i << 4) | (i);
123 acpigen_write_dword(DOD_DID_STD | DOD_FW_DETECT | config->device[i].addr);
124 }
125 acpigen_pop_len(); /* End Package. */
126 acpigen_pop_len(); /* End Method. */
127
128 for (i = 0; i < config->device_count; i++) {
129 acpigen_write_device(config->device[i].name);
130 if (config->device[i].hid)
131 acpigen_write_name_string("_HID", config->device[i].hid);
132 else
133 acpigen_write_name_integer("_ADR", config->device[i].addr);
134
135 acpigen_write_name_integer("_STA", 0xF);
136 gfx_fill_privacy_screen_dsm(&config->device[i].privacy);
137
138 if (config->device[i].use_pld)
139 acpigen_write_pld(&config->device[i].pld);
140
141 /* Generate ACPI brightness controls for LCD on Intel iGPU */
142 if (CONFIG(INTEL_GMA_ACPI) && strcmp(config->device[i].name, "LCD0") == 0) {
143 /*
144 Method (_BCL, 0, NotSerialized)
145 {
146 Return (^^XBCL())
147 }
148 */
149 acpigen_write_method("_BCL", 0);
150 acpigen_emit_byte(RETURN_OP);
151 acpigen_emit_namestring("^^XBCL");
152 acpigen_pop_len();
153
154 /*
155 Method (_BCM, 1, NotSerialized)
156 {
157 ^^XBCM(Arg0)
158 }
159 */
160 acpigen_write_method("_BCM", 1);
161 acpigen_emit_namestring("^^XBCM");
162 acpigen_emit_byte(ARG0_OP);
163 acpigen_pop_len();
164
165 /*
166 Method (_BQC, 0, NotSerialized)
167 {
168 Return (^^XBQC())
169 }
170 */
171 acpigen_write_method("_BQC", 0);
172 acpigen_emit_byte(RETURN_OP);
173 acpigen_emit_namestring("^^XBQC");
174 acpigen_pop_len();
175 }
176
177 acpigen_pop_len(); /* Device */
178 }
179 acpigen_pop_len(); /* Scope */
180 }
181
gfx_acpi_name(const struct device * dev)182 static const char *gfx_acpi_name(const struct device *dev)
183 {
184 struct drivers_gfx_generic_config *config = dev->chip_info;
185
186 return config->name ? : "GFX0";
187 }
188
189 static struct device_operations gfx_ops = {
190 .acpi_name = gfx_acpi_name,
191 .acpi_fill_ssdt = gfx_fill_ssdt_generator,
192 };
193
gfx_enable(struct device * dev)194 static void gfx_enable(struct device *dev)
195 {
196 struct drivers_gfx_generic_config *config = dev->chip_info;
197
198 if (!config || !dev->enabled)
199 return;
200
201 dev->ops = &gfx_ops;
202 }
203
204 struct chip_operations drivers_gfx_generic_ops = {
205 .name = "Generic Graphics Device",
206 .enable_dev = gfx_enable
207 };
208