acpi_i2c.c revision 1.16 1 /* $NetBSD: acpi_i2c.c,v 1.16 2024/12/13 12:25:39 jmcneill 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.16 2024/12/13 12:25:39 jmcneill 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 <dev/i2c/i2cvar.h>
44
45 #include <sys/kmem.h>
46
47 #define _COMPONENT ACPI_BUS_COMPONENT
48 ACPI_MODULE_NAME ("acpi_i2c")
49
50 struct acpi_i2c_address_space_context {
51 ACPI_CONNECTION_INFO conn_info; /* must be first */
52 i2c_tag_t tag;
53 };
54
55 static const struct device_compatible_entry hid_compat_data[] = {
56 { .compat = "PNP0C50" },
57 DEVICE_COMPAT_EOL
58 };
59
60 #if NIIC > 0
61 struct acpi_i2c_context {
62 uint16_t i2c_addr;
63 struct acpi_devnode *res_src;
64 };
65 #endif
66
67 static struct acpi_devnode *
68 acpi_i2c_resource_find_source(ACPI_RESOURCE_SOURCE *rs)
69 {
70 ACPI_STATUS rv;
71 ACPI_HANDLE hdl;
72 struct acpi_devnode *ad;
73
74 if (rs->StringPtr == NULL) {
75 return NULL;
76 }
77
78 rv = AcpiGetHandle(NULL, rs->StringPtr, &hdl);
79 if (ACPI_FAILURE(rv)) {
80 printf("%s: couldn't lookup '%s': %s\n", __func__,
81 rs->StringPtr, AcpiFormatException(rv));
82 return NULL;
83 }
84
85 SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
86 if (ad->ad_handle == hdl) {
87 return ad;
88 }
89 }
90
91 printf("%s: no acpi devnode matching resource source '%s'\n",
92 __func__, rs->StringPtr);
93 return NULL;
94 }
95
96 static ACPI_STATUS
97 acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context)
98 {
99 struct acpi_i2c_context *i2cc = context;
100
101 switch (res->Type) {
102 case ACPI_RESOURCE_TYPE_END_TAG:
103 break;
104 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
105 switch (res->Data.I2cSerialBus.Type) {
106 case ACPI_RESOURCE_SERIAL_TYPE_I2C:
107 i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
108 i2cc->res_src = acpi_i2c_resource_find_source(
109 &res->Data.I2cSerialBus.ResourceSource);
110 break;
111 }
112 break;
113 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
114 break;
115 default:
116 break;
117 }
118 return_ACPI_STATUS(AE_OK);
119 }
120
121 static void
122 acpi_enter_i2c_device(struct acpi_devnode *ad, prop_array_t array)
123 {
124 prop_dictionary_t dev;
125 struct acpi_i2c_context i2cc;
126 ACPI_STATUS rv;
127 char *clist;
128 size_t clist_size;
129
130 memset(&i2cc, 0, sizeof(i2cc));
131 rv = AcpiWalkResources(ad->ad_handle, "_CRS",
132 acpi_i2c_resource_parse_callback, &i2cc);
133 if (ACPI_FAILURE(rv)) {
134 return;
135 }
136 if (i2cc.i2c_addr == 0)
137 return;
138 dev = prop_dictionary_create();
139 if (dev == NULL) {
140 aprint_error("ignoring device %s (no memory)\n",
141 ad->ad_name);
142 return;
143 }
144 clist = acpi_pack_compat_list(ad, &clist_size);
145 if (clist == NULL) {
146 prop_object_release(dev);
147 aprint_error("ignoring device %s (no _HID or _CID)\n",
148 ad->ad_name);
149 return;
150 }
151 prop_dictionary_set_string(dev, "name", ad->ad_name);
152 prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
153 prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
154 prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_ACPI);
155 prop_dictionary_set_data(dev, "compatible", clist, clist_size);
156 kmem_free(clist, clist_size);
157
158 prop_array_add(array, dev);
159 prop_object_release(dev);
160 }
161
162 static void
163 acpi_enter_i2chid_devs(device_t dev, struct acpi_devnode *devnode,
164 prop_array_t array)
165 {
166 struct acpi_devnode *ad;
167
168 KASSERT(dev != NULL);
169
170 SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
171 struct acpi_attach_args aa = {
172 .aa_node = ad
173 };
174 struct acpi_i2c_context i2cc;
175 ACPI_STATUS rv;
176
177 if (!acpi_device_present(ad->ad_handle))
178 continue;
179 if (ad->ad_device != NULL)
180 continue;
181 if (acpi_compatible_match(&aa, hid_compat_data) == 0)
182 continue;
183
184 memset(&i2cc, 0, sizeof(i2cc));
185 rv = AcpiWalkResources(ad->ad_handle, "_CRS",
186 acpi_i2c_resource_parse_callback, &i2cc);
187 if (ACPI_SUCCESS(rv) &&
188 i2cc.i2c_addr != 0 &&
189 i2cc.res_src == devnode) {
190 aprint_debug_dev(dev, "claiming %s\n", ad->ad_name);
191 acpi_enter_i2c_device(ad, array);
192 }
193 }
194 }
195
196 prop_array_t
197 acpi_enter_i2c_devs(device_t dev, struct acpi_devnode *devnode)
198 {
199 struct acpi_devnode *ad;
200 prop_array_t array = prop_array_create();
201
202 if (array == NULL)
203 return NULL;
204
205 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
206 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
207 continue;
208 if (!acpi_device_present(ad->ad_handle))
209 continue;
210 acpi_enter_i2c_device(ad, array);
211 }
212
213 if (dev != NULL) {
214 acpi_claim_childdevs(dev, devnode);
215 acpi_enter_i2chid_devs(dev, devnode, array);
216 }
217
218 return array;
219 }
220
221 #if NIIC > 0
222 static ACPI_STATUS
223 acpi_i2c_gsb_init(ACPI_HANDLE region_hdl, UINT32 function,
224 void *handler_ctx, void **region_ctx)
225 {
226 if (function == ACPI_REGION_DEACTIVATE) {
227 *region_ctx = NULL;
228 } else {
229 *region_ctx = region_hdl;
230 }
231 return AE_OK;
232 }
233
234 static ACPI_STATUS
235 acpi_i2c_gsb_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
236 UINT32 bit_width, UINT64 *value, void *handler_ctx,
237 void *region_ctx)
238 {
239 ACPI_OPERAND_OBJECT *region_obj = region_ctx;
240 struct acpi_i2c_address_space_context *context = handler_ctx;
241 UINT8 *buffer = ACPI_CAST_PTR(uint8_t, value);
242 UINT8 *data_buffer;
243 UINT8 data_length;
244 ACPI_PHYSICAL_ADDRESS base_address;
245 ACPI_RESOURCE *res;
246 UINT32 length;
247 UINT8 space_id;
248 ACPI_STATUS rv;
249 ACPI_CONNECTION_INFO *conn_info= &context->conn_info;
250 i2c_tag_t tag = context->tag;
251 i2c_op_t op;
252 union {
253 uint8_t cmd8;
254 uint16_t cmd16;
255 } cmd;
256 size_t cmdlen;
257
258 if (region_obj->Region.Type != ACPI_TYPE_REGION) {
259 return AE_OK;
260 }
261
262 base_address = region_obj->Region.Address;
263 space_id = region_obj->Region.SpaceId;
264
265 KASSERT(space_id == ACPI_ADR_SPACE_GSBUS);
266
267 rv = AcpiExGetProtocolBufferLength(function >> 16, &length);
268 if (ACPI_FAILURE(rv)) {
269 return rv;
270 }
271
272 rv = AcpiBufferToResource(conn_info->Connection, conn_info->Length,
273 &res);
274 if (ACPI_FAILURE(rv)) {
275 return rv;
276 }
277 if (res->Type != ACPI_RESOURCE_TYPE_SERIAL_BUS ||
278 res->Data.CommonSerialBus.Type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
279 return AE_TYPE;
280 }
281
282 data_buffer = &buffer[2];
283 data_length = (UINT8)length;
284
285 if ((function & ACPI_IO_MASK) != 0) {
286 op = I2C_OP_WRITE_WITH_STOP;
287 } else {
288 op = I2C_OP_READ_WITH_STOP;
289 }
290
291 #ifdef ACPI_I2C_DEBUG
292 printf("%s %s: %s Attr %X Addr %.4X BaseAddr %.4X Length %.2X BitWidth %X BufLen %X",
293 __func__, AcpiUtGetRegionName (space_id),
294 (function & ACPI_IO_MASK) ? "Write" : "Read ",
295 (UINT32) (function >> 16),
296 (UINT32) address, (UINT32) base_address,
297 length, bit_width, buffer[1]);
298 printf(" [AccessLength %.2X Connection %p]\n",
299 conn_info->AccessLength, conn_info->Connection);
300 #endif
301
302 if (bit_width == 8) {
303 cmd.cmd8 = (uint8_t)(base_address + address);
304 cmdlen = 1;
305 } else if (bit_width == 16) {
306 cmd.cmd16 = (uint16_t)(base_address + address);
307 cmdlen = 2;
308 } else {
309 cmdlen = 0;
310 }
311 if (cmdlen == 0) {
312 buffer[0] = EINVAL;
313 } else {
314 const int flags = I2C_F_POLL;
315 iic_acquire_bus(tag, flags);
316 buffer[0] = iic_exec(tag, op, res->Data.I2cSerialBus.SlaveAddress,
317 &cmd, cmdlen, data_buffer, data_length, flags);
318 iic_release_bus(tag, flags);
319 if (buffer[0] == 0) {
320 buffer[1] = data_length;
321 }
322 #ifdef ACPI_I2C_DEBUG
323 printf("iic_exec returned %d\n", buffer[0]);
324 #endif
325 }
326
327 ACPI_FREE(res);
328
329 return AE_OK;
330 }
331 #endif
332
333 ACPI_STATUS
334 acpi_i2c_register(struct acpi_devnode *devnode, device_t dev, i2c_tag_t tag)
335 {
336 #if NIIC > 0
337 struct acpi_i2c_address_space_context *context;
338 ACPI_STATUS rv;
339
340 context = kmem_zalloc(sizeof(*context), KM_SLEEP);
341 context->tag = tag;
342
343 rv = AcpiInstallAddressSpaceHandler(devnode->ad_handle,
344 ACPI_ADR_SPACE_GSBUS, acpi_i2c_gsb_handler, acpi_i2c_gsb_init,
345 context);
346 if (ACPI_FAILURE(rv)) {
347 aprint_error_dev(dev,
348 "couldn't install address space handler: %s",
349 AcpiFormatException(rv));
350 }
351
352 return rv;
353 #else
354 return AE_NOT_CONFIGURED;
355 #endif
356 }
357