Home | History | Annotate | Line # | Download | only in acpi
acpi_i2c.c revision 1.13
      1 /* $NetBSD: acpi_i2c.c,v 1.13 2024/12/08 20:44:40 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.13 2024/12/08 20:44:40 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 <dev/i2c/i2cvar.h>
     41 
     42 #include <sys/kmem.h>
     43 
     44 #define _COMPONENT	ACPI_BUS_COMPONENT
     45 ACPI_MODULE_NAME	("acpi_i2c")
     46 
     47 static const struct device_compatible_entry hid_compat_data[] = {
     48 	{ .compat = "PNP0C50" },
     49 	DEVICE_COMPAT_EOL
     50 };
     51 
     52 struct acpi_i2c_context {
     53 	uint16_t i2c_addr;
     54 	struct acpi_devnode *res_src;
     55 };
     56 
     57 static struct acpi_devnode *
     58 acpi_i2c_resource_find_source(ACPI_RESOURCE_SOURCE *rs)
     59 {
     60 	ACPI_STATUS rv;
     61 	ACPI_HANDLE hdl;
     62 	struct acpi_devnode *ad;
     63 
     64 	if (rs->StringPtr == NULL) {
     65 		return NULL;
     66 	}
     67 
     68 	rv = AcpiGetHandle(NULL, rs->StringPtr, &hdl);
     69 	if (ACPI_FAILURE(rv)) {
     70 		printf("%s: couldn't lookup '%s': %s\n", __func__,
     71 		    rs->StringPtr, AcpiFormatException(rv));
     72 		return NULL;
     73 	}
     74 
     75 	SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
     76 		if (ad->ad_handle == hdl) {
     77 			return ad;
     78 		}
     79 	}
     80 
     81 	printf("%s: no acpi devnode matching resource source '%s'\n",
     82 	    __func__, rs->StringPtr);
     83 	return NULL;
     84 }
     85 
     86 static ACPI_STATUS
     87 acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context)
     88 {
     89 	struct acpi_i2c_context *i2cc = context;
     90 
     91 	switch (res->Type) {
     92 	case ACPI_RESOURCE_TYPE_END_TAG:
     93 		break;
     94 	case ACPI_RESOURCE_TYPE_SERIAL_BUS:
     95 		switch (res->Data.I2cSerialBus.Type) {
     96 		case ACPI_RESOURCE_SERIAL_TYPE_I2C:
     97 			i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
     98 			i2cc->res_src = acpi_i2c_resource_find_source(
     99 			    &res->Data.I2cSerialBus.ResourceSource);
    100 			break;
    101 		}
    102 		break;
    103 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
    104 		break;
    105 	default:
    106 		printf("resource type 0x%x ignored\n", res->Type);
    107 	}
    108 	return_ACPI_STATUS(AE_OK);
    109 }
    110 
    111 static void
    112 acpi_enter_i2c_device(struct acpi_devnode *ad, prop_array_t array)
    113 {
    114 	prop_dictionary_t dev;
    115 	struct acpi_i2c_context i2cc;
    116 	ACPI_STATUS rv;
    117 	char *clist;
    118 	size_t clist_size;
    119 
    120 	memset(&i2cc, 0, sizeof(i2cc));
    121 	rv = AcpiWalkResources(ad->ad_handle, "_CRS",
    122 	     acpi_i2c_resource_parse_callback, &i2cc);
    123 	if (ACPI_FAILURE(rv)) {
    124 		aprint_error("ACPI: unable to get resources "
    125 		   "for %s: %s\n", ad->ad_name,
    126 		   AcpiFormatException(rv));
    127 		return;
    128 	}
    129 	if (i2cc.i2c_addr == 0)
    130 		return;
    131 	dev = prop_dictionary_create();
    132 	if (dev == NULL) {
    133 		aprint_error("ignoring device %s (no memory)\n",
    134 		    ad->ad_name);
    135 		return;
    136 	}
    137 	clist = acpi_pack_compat_list(ad, &clist_size);
    138 	if (clist == NULL) {
    139 		prop_object_release(dev);
    140 		aprint_error("ignoring device %s (no _HID or _CID)\n",
    141 		    ad->ad_name);
    142 		return;
    143 	}
    144 	prop_dictionary_set_string(dev, "name", ad->ad_name);
    145 	prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
    146 	prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
    147 	prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_ACPI);
    148 	prop_dictionary_set_data(dev, "compatible", clist, clist_size);
    149 	kmem_free(clist, clist_size);
    150 
    151 	prop_array_add(array, dev);
    152 	prop_object_release(dev);
    153 }
    154 
    155 static void
    156 acpi_enter_i2chid_devs(device_t dev, struct acpi_devnode *devnode,
    157     prop_array_t array)
    158 {
    159 	struct acpi_devnode *ad;
    160 
    161 	KASSERT(dev != NULL);
    162 
    163 	SIMPLEQ_FOREACH(ad, &acpi_softc->sc_head, ad_list) {
    164 		struct acpi_attach_args aa = {
    165 			.aa_node = ad
    166 		};
    167 		struct acpi_i2c_context i2cc;
    168 		ACPI_STATUS rv;
    169 
    170 		if (!acpi_device_present(ad->ad_handle))
    171 			continue;
    172 		if (ad->ad_device != NULL)
    173 			continue;
    174 		if (acpi_compatible_match(&aa, hid_compat_data) == 0)
    175 			continue;
    176 
    177 		memset(&i2cc, 0, sizeof(i2cc));
    178 		rv = AcpiWalkResources(ad->ad_handle, "_CRS",
    179 		    acpi_i2c_resource_parse_callback, &i2cc);
    180 		if (ACPI_SUCCESS(rv) &&
    181 		    i2cc.i2c_addr != 0 &&
    182 		    i2cc.res_src == devnode) {
    183 			aprint_debug_dev(dev, "claiming %s\n", ad->ad_name);
    184 			acpi_enter_i2c_device(ad, array);
    185 		}
    186 	}
    187 }
    188 
    189 prop_array_t
    190 acpi_enter_i2c_devs(device_t dev, struct acpi_devnode *devnode)
    191 {
    192 	struct acpi_devnode *ad;
    193 	prop_array_t array = prop_array_create();
    194 
    195 	if (array == NULL)
    196 		return NULL;
    197 
    198 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    199 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    200 			continue;
    201 		if (!acpi_device_present(ad->ad_handle))
    202 			continue;
    203 		acpi_enter_i2c_device(ad, array);
    204 	}
    205 
    206 	if (dev != NULL) {
    207 		acpi_claim_childdevs(dev, devnode);
    208 		acpi_enter_i2chid_devs(dev, devnode, array);
    209 	}
    210 
    211 	return array;
    212 }
    213