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