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