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