Home | History | Annotate | Line # | Download | only in acpi
acpi_i2c.c revision 1.14
      1 /* $NetBSD: acpi_i2c.c,v 1.14 2024/12/09 22:12:54 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.14 2024/12/09 22:12:54 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 		printf("resource type 0x%x ignored\n", res->Type);
    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 		aprint_error("ACPI: unable to get resources "
    131 		   "for %s: %s\n", ad->ad_name,
    132 		   AcpiFormatException(rv));
    133 		return;
    134 	}
    135 	if (i2cc.i2c_addr == 0)
    136 		return;
    137 	dev = prop_dictionary_create();
    138 	if (dev == NULL) {
    139 		aprint_error("ignoring device %s (no memory)\n",
    140 		    ad->ad_name);
    141 		return;
    142 	}
    143 	clist = acpi_pack_compat_list(ad, &clist_size);
    144 	if (clist == NULL) {
    145 		prop_object_release(dev);
    146 		aprint_error("ignoring device %s (no _HID or _CID)\n",
    147 		    ad->ad_name);
    148 		return;
    149 	}
    150 	prop_dictionary_set_string(dev, "name", ad->ad_name);
    151 	prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
    152 	prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
    153 	prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_ACPI);
    154 	prop_dictionary_set_data(dev, "compatible", clist, clist_size);
    155 	kmem_free(clist, clist_size);
    156 
    157 	prop_array_add(array, dev);
    158 	prop_object_release(dev);
    159 }
    160 
    161 static void
    162 acpi_enter_i2chid_devs(device_t dev, struct acpi_devnode *devnode,
    163     prop_array_t array)
    164 {
    165 	struct acpi_devnode *ad;
    166 
    167 	KASSERT(dev != NULL);
    168 
    169 	SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
    170 		struct acpi_attach_args aa = {
    171 			.aa_node = ad
    172 		};
    173 		struct acpi_i2c_context i2cc;
    174 		ACPI_STATUS rv;
    175 
    176 		if (!acpi_device_present(ad->ad_handle))
    177 			continue;
    178 		if (ad->ad_device != NULL)
    179 			continue;
    180 		if (acpi_compatible_match(&aa, hid_compat_data) == 0)
    181 			continue;
    182 
    183 		memset(&i2cc, 0, sizeof(i2cc));
    184 		rv = AcpiWalkResources(ad->ad_handle, "_CRS",
    185 		    acpi_i2c_resource_parse_callback, &i2cc);
    186 		if (ACPI_SUCCESS(rv) &&
    187 		    i2cc.i2c_addr != 0 &&
    188 		    i2cc.res_src == devnode) {
    189 			aprint_debug_dev(dev, "claiming %s\n", ad->ad_name);
    190 			acpi_enter_i2c_device(ad, array);
    191 		}
    192 	}
    193 }
    194 
    195 prop_array_t
    196 acpi_enter_i2c_devs(device_t dev, struct acpi_devnode *devnode)
    197 {
    198 	struct acpi_devnode *ad;
    199 	prop_array_t array = prop_array_create();
    200 
    201 	if (array == NULL)
    202 		return NULL;
    203 
    204 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    205 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    206 			continue;
    207 		if (!acpi_device_present(ad->ad_handle))
    208 			continue;
    209 		acpi_enter_i2c_device(ad, array);
    210 	}
    211 
    212 	if (dev != NULL) {
    213 		acpi_claim_childdevs(dev, devnode);
    214 		acpi_enter_i2chid_devs(dev, devnode, array);
    215 	}
    216 
    217 	return array;
    218 }
    219 
    220 static ACPI_STATUS
    221 acpi_i2c_gsb_init(ACPI_HANDLE region_hdl, UINT32 function,
    222     void *handler_ctx, void **region_ctx)
    223 {
    224 	if (function == ACPI_REGION_DEACTIVATE) {
    225 		*region_ctx = NULL;
    226 	} else {
    227 		*region_ctx = region_hdl;
    228 	}
    229 	return AE_OK;
    230 }
    231 
    232 static ACPI_STATUS
    233 acpi_i2c_gsb_handler(UINT32 function, ACPI_PHYSICAL_ADDRESS address,
    234     UINT32 bit_width, UINT64 *value, void *handler_ctx,
    235     void *region_ctx)
    236 {
    237 	ACPI_OPERAND_OBJECT *region_obj = region_ctx;
    238 	struct acpi_i2c_address_space_context *context = handler_ctx;
    239 	UINT8 *buffer = ACPI_CAST_PTR(uint8_t, value);
    240 	UINT8 *data_buffer;
    241 	UINT8 data_length;
    242 	ACPI_PHYSICAL_ADDRESS base_address;
    243 	ACPI_RESOURCE *res;
    244 	UINT32 length;
    245 	UINT8 space_id;
    246 	ACPI_STATUS rv;
    247 	ACPI_CONNECTION_INFO *conn_info= &context->conn_info;
    248 	i2c_tag_t tag = context->tag;
    249 	i2c_op_t op;
    250 	union {
    251 		uint8_t cmd8;
    252 		uint16_t cmd16;
    253 	} cmd;
    254 	size_t cmdlen;
    255 
    256 	if (region_obj->Region.Type != ACPI_TYPE_REGION) {
    257 		return AE_OK;
    258 	}
    259 
    260 	base_address = region_obj->Region.Address;
    261 	space_id = region_obj->Region.SpaceId;
    262 
    263 	KASSERT(space_id == ACPI_ADR_SPACE_GSBUS);
    264 
    265 	rv = AcpiExGetProtocolBufferLength(function >> 16, &length);
    266 	if (ACPI_FAILURE(rv)) {
    267 		return rv;
    268 	}
    269 
    270 	rv = AcpiBufferToResource(conn_info->Connection, conn_info->Length,
    271 	    &res);
    272 	if (ACPI_FAILURE(rv)) {
    273 		return rv;
    274 	}
    275 	if (res->Type != ACPI_RESOURCE_TYPE_SERIAL_BUS ||
    276 	    res->Data.CommonSerialBus.Type != ACPI_RESOURCE_SERIAL_TYPE_I2C) {
    277 		return AE_TYPE;
    278 	}
    279 
    280 	data_buffer = &buffer[2];
    281 	data_length = (UINT8)length;
    282 
    283 	if ((function & ACPI_IO_MASK) != 0) {
    284 		op = I2C_OP_WRITE_WITH_STOP;
    285 	} else {
    286 		op = I2C_OP_READ_WITH_STOP;
    287 	}
    288 
    289 #ifdef ACPI_I2C_DEBUG
    290 	printf("%s %s: %s Attr %X Addr %.4X BaseAddr %.4X Length %.2X BitWidth %X BufLen %X",
    291 	       __func__, AcpiUtGetRegionName (space_id),
    292 	       (function & ACPI_IO_MASK) ? "Write" : "Read ",
    293 	       (UINT32) (function >> 16),
    294 	       (UINT32) address, (UINT32) base_address,
    295 	       length, bit_width, buffer[1]);
    296 	printf(" [AccessLength %.2X Connection %p]\n",
    297 	       conn_info->AccessLength, conn_info->Connection);
    298 #endif
    299 
    300 	if (bit_width == 8) {
    301 		cmd.cmd8 = (uint8_t)(base_address + address);
    302 		cmdlen = 1;
    303 	} else if (bit_width == 16) {
    304 		cmd.cmd16 = (uint16_t)(base_address + address);
    305 		cmdlen = 2;
    306 	} else {
    307 		cmdlen = 0;
    308 	}
    309 	if (cmdlen == 0) {
    310 		buffer[0] = EINVAL;
    311 	} else {
    312 		const int flags = I2C_F_POLL;
    313 		iic_acquire_bus(tag, flags);
    314 		buffer[0] = iic_exec(tag, op, res->Data.I2cSerialBus.SlaveAddress,
    315 				     &cmd, cmdlen, data_buffer, data_length, flags);
    316 		iic_release_bus(tag, flags);
    317 		if (buffer[0] == 0) {
    318 			buffer[1] = data_length;
    319 		}
    320 #ifdef ACPI_I2C_DEBUG
    321 		printf("iic_exec returned %d\n", buffer[0]);
    322 #endif
    323 	}
    324 
    325 	ACPI_FREE(res);
    326 
    327 	return AE_OK;
    328 }
    329 
    330 ACPI_STATUS
    331 acpi_i2c_register(struct acpi_devnode *devnode, device_t dev, i2c_tag_t tag)
    332 {
    333 	struct acpi_i2c_address_space_context *context;
    334 	ACPI_STATUS rv;
    335 
    336 	context = kmem_zalloc(sizeof(*context), KM_SLEEP);
    337 	context->tag = tag;
    338 
    339 	rv = AcpiInstallAddressSpaceHandler(devnode->ad_handle,
    340 	    ACPI_ADR_SPACE_GSBUS, acpi_i2c_gsb_handler, acpi_i2c_gsb_init,
    341 	    context);
    342 	if (ACPI_FAILURE(rv)) {
    343 		aprint_error_dev(dev,
    344 		    "couldn't install address space handler: %s",
    345 		    AcpiFormatException(rv));
    346 	}
    347 
    348 	return rv;
    349 }
    350