acpi_i2c.c revision 1.20 1 /* $NetBSD: acpi_i2c.c,v 1.20 2025/09/15 15:18:42 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2017, 2021 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Manuel Bouyer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "iic.h"
33
34 #include <sys/cdefs.h>
35 __KERNEL_RCSID(0, "$NetBSD: acpi_i2c.c,v 1.20 2025/09/15 15:18:42 thorpej Exp $");
36
37 #include <sys/device.h>
38
39 #include <dev/acpi/acpireg.h>
40 #include <dev/acpi/acpivar.h>
41 #include <dev/acpi/acpi_i2c.h>
42 #include <external/bsd/acpica/dist/include/acinterp.h>
43 #include <external/bsd/acpica/dist/include/amlcode.h>
44 #include <dev/i2c/i2cvar.h>
45
46 #include <sys/kmem.h>
47
48 #define _COMPONENT ACPI_BUS_COMPONENT
49 ACPI_MODULE_NAME ("acpi_i2c")
50
51 struct acpi_i2c_address_space_context {
52 ACPI_CONNECTION_INFO conn_info; /* must be first */
53 i2c_tag_t tag;
54 };
55
56 static const struct device_compatible_entry hid_compat_data[] = {
57 { .compat = "PNP0C50" },
58 DEVICE_COMPAT_EOL
59 };
60
61 #if NIIC > 0
62 struct acpi_i2c_context {
63 uint16_t i2c_addr;
64 struct acpi_devnode *res_src;
65 };
66 #endif
67
68 static struct acpi_devnode *
69 acpi_i2c_resource_find_source(ACPI_RESOURCE_SOURCE *rs)
70 {
71 ACPI_STATUS rv;
72 ACPI_HANDLE hdl;
73 struct acpi_devnode *ad;
74
75 if (rs->StringPtr == NULL) {
76 return NULL;
77 }
78
79 rv = AcpiGetHandle(NULL, rs->StringPtr, &hdl);
80 if (ACPI_FAILURE(rv)) {
81 printf("%s: couldn't lookup '%s': %s\n", __func__,
82 rs->StringPtr, AcpiFormatException(rv));
83 return NULL;
84 }
85
86 SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
87 if (ad->ad_handle == hdl) {
88 return ad;
89 }
90 }
91
92 printf("%s: no acpi devnode matching resource source '%s'\n",
93 __func__, rs->StringPtr);
94 return NULL;
95 }
96
97 static ACPI_STATUS
98 acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context)
99 {
100 struct acpi_i2c_context *i2cc = context;
101
102 switch (res->Type) {
103 case ACPI_RESOURCE_TYPE_END_TAG:
104 break;
105 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
106 switch (res->Data.I2cSerialBus.Type) {
107 case ACPI_RESOURCE_SERIAL_TYPE_I2C:
108 i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
109 i2cc->res_src = acpi_i2c_resource_find_source(
110 &res->Data.I2cSerialBus.ResourceSource);
111 break;
112 }
113 break;
114 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
115 break;
116 default:
117 break;
118 }
119 return_ACPI_STATUS(AE_OK);
120 }
121
122 static void
123 acpi_enter_i2c_device(struct acpi_devnode *ad, prop_array_t array)
124 {
125 prop_dictionary_t dev;
126 struct acpi_i2c_context i2cc;
127 ACPI_STATUS rv;
128 char *clist;
129 size_t clist_size;
130
131 memset(&i2cc, 0, sizeof(i2cc));
132 rv = AcpiWalkResources(ad->ad_handle, "_CRS",
133 acpi_i2c_resource_parse_callback, &i2cc);
134 if (ACPI_FAILURE(rv)) {
135 return;
136 }
137 if (i2cc.i2c_addr == 0)
138 return;
139 dev = prop_dictionary_create();
140 if (dev == NULL) {
141 aprint_error("ignoring device %s (no memory)\n",
142 ad->ad_name);
143 return;
144 }
145 clist = acpi_pack_compat_list(ad, &clist_size);
146 if (clist == NULL) {
147 prop_object_release(dev);
148 aprint_error("ignoring device %s (no _HID or _CID)\n",
149 ad->ad_name);
150 return;
151 }
152 prop_dictionary_set_string(dev, "name", ad->ad_name);
153 prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
154 prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
155 prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_ACPI);
156 prop_dictionary_set_data(dev, "compatible", clist, clist_size);
157 kmem_free(clist, clist_size);
158
159 prop_array_add(array, dev);
160 prop_object_release(dev);
161 }
162
163 static void
164 acpi_enter_i2chid_devs(device_t dev, struct acpi_devnode *devnode,
165 prop_array_t array)
166 {
167 struct acpi_devnode *ad;
168
169 KASSERT(dev != NULL);
170
171 SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
172 struct acpi_attach_args aa = {
173 .aa_node = ad
174 };
175 struct acpi_i2c_context i2cc;
176 ACPI_STATUS rv;
177
178 if (!acpi_device_present(ad->ad_handle))
179 continue;
180 if (ad->ad_device != NULL)
181 continue;
182 if (acpi_compatible_match(&aa, hid_compat_data) == 0)
183 continue;
184
185 memset(&i2cc, 0, sizeof(i2cc));
186 rv = AcpiWalkResources(ad->ad_handle, "_CRS",
187 acpi_i2c_resource_parse_callback, &i2cc);
188 if (ACPI_SUCCESS(rv) &&
189 i2cc.i2c_addr != 0 &&
190 i2cc.res_src == devnode) {
191 aprint_debug_dev(dev, "claiming %s\n", ad->ad_name);
192 ad->ad_device = dev;
193 acpi_claim_childdevs(dev, ad, NULL);
194 acpi_enter_i2c_device(ad, array);
195 }
196 }
197 }
198
199 prop_array_t
200 acpi_copy_i2c_devs(device_t dev)
201 {
202 struct acpi_devnode *ad;
203 ACPI_HANDLE *hdl = devhandle_to_acpi(device_handle(dev));
204 struct acpi_devnode *devnode = acpi_match_node(hdl);
205
206 if (devnode == NULL) {
207 aprint_error_dev(dev, "%s: no devnode matching handle\n",
208 __func__);
209 return NULL;
210 }
211
212 prop_array_t array = prop_array_create();
213 if (array == NULL)
214 return NULL;
215
216 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
217 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
218 continue;
219 if (!acpi_device_present(ad->ad_handle))
220 continue;
221 acpi_enter_i2c_device(ad, array);
222 }
223
224 if (dev != NULL) {
225 acpi_claim_childdevs(dev, devnode, "_CRS");
226 acpi_claim_childdevs(dev, devnode, "_ADR");
227 acpi_enter_i2chid_devs(dev, devnode, array);
228 }
229
230 return array;
231 }
232
233 #if NIIC > 0
234 static ACPI_STATUS
235 acpi_i2c_gsb_init(ACPI_HANDLE region_hdl, UINT32 function,
236 void *handler_ctx, void **region_ctx)
237 {
238 if (function == ACPI_REGION_DEACTIVATE) {
239 *region_ctx = NULL;
240 } else {
241 *region_ctx = region_hdl;
242 }
243 return AE_OK;
244 }
245
246 static ACPI_STATUS
247 acpi_i2c_gsb_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
248 UINT32 bit_width, UINT64 *value, void *handler_ctx,
249 void *region_ctx)
250 {
251 ACPI_OPERAND_OBJECT *region_obj = region_ctx;
252 struct acpi_i2c_address_space_context *context = handler_ctx;
253 UINT8 *buf = ACPI_CAST_PTR(uint8_t, value);
254 ACPI_PHYSICAL_ADDRESS base_address;
255 ACPI_RESOURCE *res;
256 ACPI_STATUS rv;
257 ACPI_CONNECTION_INFO *conn_info = &context->conn_info;
258 i2c_tag_t tag = context->tag;
259 i2c_addr_t i2c_addr;
260 i2c_op_t op;
261 union {
262 uint8_t cmd8;
263 uint16_t cmd16;
264 uint32_t cmd32;
265 } cmd;
266 size_t buflen;
267 size_t cmdlen;
268 bool do_xfer = true;
269
270 if (region_obj->Region.Type != ACPI_TYPE_REGION) {
271 return AE_OK;
272 }
273
274 base_address = region_obj->Region.Address;
275 KASSERT(region_obj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS);
276
277 rv = AcpiBufferToResource(conn_info->Connection, conn_info->Length,
278 &res);
279 if (ACPI_FAILURE(rv)) {
280 return rv;
281 }
282 if (res->Type != ACPI_RESOURCE_TYPE_SERIAL_BUS ||
283 res->Data.CommonSerialBus.Type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
284 return AE_TYPE;
285 }
286
287 i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
288 if ((function & ACPI_IO_MASK) != 0) {
289 op = I2C_OP_WRITE_WITH_STOP;
290 } else {
291 op = I2C_OP_READ_WITH_STOP;
292 }
293
294 #ifdef ACPI_I2C_DEBUG
295 UINT32 length;
296 rv = AcpiExGetProtocolBufferLength(function >> 16, &length);
297 if (ACPI_FAILURE(rv)) {
298 printf("%s AcpiExGetProtocolBufferLength failed: %s\n",
299 __func__, AcpiFormatException(rv));
300 length = UINT32_MAX;
301 }
302 printf("%s %s: %s Attr %X Addr %.4X BaseAddr %.4X Length %.2X BitWidth %X BufLen %X",
303 __func__, AcpiUtGetRegionName(region_obj->Region.SpaceId),
304 (function & ACPI_IO_MASK) ? "Write" : "Read ",
305 (UINT32) (function >> 16),
306 (UINT32) address, (UINT32) base_address,
307 length, bit_width, buf[1]);
308 printf(" [AccessLength %.2X Connection %p]\n",
309 conn_info->AccessLength, conn_info->Connection);
310 #endif
311
312 switch ((UINT32)(function >> 16)) {
313 case AML_FIELD_ATTRIB_QUICK:
314 cmdlen = 0;
315 buflen = 0;
316 break;
317 case AML_FIELD_ATTRIB_SEND_RECEIVE:
318 cmdlen = 0;
319 buflen = 1;
320 break;
321 case AML_FIELD_ATTRIB_BYTE:
322 cmdlen = bit_width / NBBY;
323 buflen = 1;
324 break;
325 case AML_FIELD_ATTRIB_WORD:
326 cmdlen = bit_width / NBBY;
327 buflen = 2;
328 break;
329 case AML_FIELD_ATTRIB_BYTES:
330 cmdlen = bit_width / NBBY;
331 buflen = buf[1];
332 break;
333 case AML_FIELD_ATTRIB_BLOCK:
334 cmdlen = bit_width / NBBY;
335 buflen = buf[1];
336 op |= I2C_OPMASK_BLKMODE;
337 break;
338 case AML_FIELD_ATTRIB_RAW_BYTES:
339 case AML_FIELD_ATTRIB_RAW_PROCESS_BYTES:
340 case AML_FIELD_ATTRIB_PROCESS_CALL:
341 default:
342 cmdlen = 0;
343 do_xfer = false;
344 #ifdef ACPI_I2C_DEBUG
345 printf("field attrib 0x%x not supported\n",
346 (UINT32)(function >> 16));
347 #endif
348 break;
349 }
350
351 switch (cmdlen) {
352 case 0:
353 case 1:
354 cmd.cmd8 = (uint8_t)(base_address + address);
355 break;
356 case 2:
357 cmd.cmd16 = (uint16_t)(base_address + address);
358 break;
359 case 4:
360 cmd.cmd32 = (uint32_t)(base_address + address);
361 break;
362 default:
363 do_xfer = false;
364 #ifdef ACPI_I2C_DEBUG
365 printf("cmdlen %zu not supported\n", cmdlen);
366 #endif
367 break;
368 }
369
370 if (!do_xfer) {
371 buf[0] = EINVAL;
372 } else {
373 const int flags = I2C_F_POLL;
374 iic_acquire_bus(tag, flags);
375 buf[0] = iic_exec(tag, op, i2c_addr,
376 &cmd, cmdlen, &buf[2], buflen, flags);
377 iic_release_bus(tag, flags);
378 if (buf[0] == 0) {
379 buf[1] = buflen;
380 }
381 #ifdef ACPI_I2C_DEBUG
382 printf("%s iic_exec op %u addr 0x%x len %zu/%zu returned %d\n",
383 __func__, op, res->Data.I2cSerialBus.SlaveAddress, cmdlen,
384 buflen, buf[0]);
385 #endif
386 }
387
388 ACPI_FREE(res);
389
390 return AE_OK;
391 }
392 #endif
393
394 ACPI_STATUS
395 acpi_i2c_register(struct acpi_devnode *devnode, device_t dev, i2c_tag_t tag)
396 {
397 #if NIIC > 0
398 struct acpi_i2c_address_space_context *context;
399 ACPI_STATUS rv;
400
401 context = kmem_zalloc(sizeof(*context), KM_SLEEP);
402 context->tag = tag;
403
404 rv = AcpiInstallAddressSpaceHandler(devnode->ad_handle,
405 ACPI_ADR_SPACE_GSBUS, acpi_i2c_gsb_handler, acpi_i2c_gsb_init,
406 context);
407 if (ACPI_FAILURE(rv)) {
408 aprint_error_dev(dev,
409 "couldn't install address space handler: %s",
410 AcpiFormatException(rv));
411 }
412
413 return rv;
414 #else
415 return AE_NOT_CONFIGURED;
416 #endif
417 }
418