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