Home | History | Annotate | Line # | Download | only in acpi
acpi_i2c.c revision 1.11.14.1
      1  1.11.14.1   thorpej /* $NetBSD: acpi_i2c.c,v 1.11.14.1 2021/08/09 00:30:09 thorpej 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.11.14.1   thorpej  * by Manuel Bouyer and Jason R. Thorpe.
      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.11.14.1   thorpej __KERNEL_RCSID(0, "$NetBSD: acpi_i2c.c,v 1.11.14.1 2021/08/09 00:30:09 thorpej Exp $");
     34        1.1    bouyer 
     35        1.1    bouyer #include <dev/acpi/acpireg.h>
     36        1.1    bouyer #include <dev/acpi/acpivar.h>
     37        1.9  jmcneill #include <dev/i2c/i2cvar.h>
     38        1.1    bouyer 
     39       1.11   thorpej #include <sys/kmem.h>
     40       1.11   thorpej 
     41        1.2    bouyer #define _COMPONENT	ACPI_BUS_COMPONENT
     42        1.2    bouyer ACPI_MODULE_NAME	("acpi_i2c")
     43        1.2    bouyer 
     44        1.1    bouyer struct acpi_i2c_context {
     45        1.1    bouyer 	uint16_t i2c_addr;
     46        1.1    bouyer };
     47        1.1    bouyer 
     48        1.1    bouyer static ACPI_STATUS
     49        1.1    bouyer acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context)
     50        1.1    bouyer {
     51        1.1    bouyer 	struct acpi_i2c_context *i2cc = context;
     52        1.1    bouyer 
     53        1.1    bouyer 	switch (res->Type) {
     54        1.1    bouyer 	case ACPI_RESOURCE_TYPE_END_TAG:
     55        1.1    bouyer 		break;
     56        1.1    bouyer 	case ACPI_RESOURCE_TYPE_SERIAL_BUS:
     57        1.1    bouyer 		switch (res->Data.I2cSerialBus.Type) {
     58        1.1    bouyer 		case ACPI_RESOURCE_SERIAL_TYPE_I2C:
     59        1.1    bouyer 			i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
     60        1.1    bouyer 			break;
     61        1.1    bouyer 		}
     62        1.1    bouyer 		break;
     63        1.1    bouyer 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
     64        1.1    bouyer 		break;
     65        1.1    bouyer 	default:
     66        1.8   msaitoh 		printf("resource type 0x%x ignored\n", res->Type);
     67        1.1    bouyer 	}
     68        1.1    bouyer 	return_ACPI_STATUS(AE_OK);
     69        1.1    bouyer }
     70        1.1    bouyer 
     71  1.11.14.1   thorpej static bool
     72  1.11.14.1   thorpej acpi_i2c_enumerate_device(device_t dev, struct acpi_devnode *ad,
     73  1.11.14.1   thorpej     struct i2c_enumerate_devices_args * const args)
     74        1.1    bouyer {
     75       1.11   thorpej 	char *clist;
     76       1.11   thorpej 	size_t clist_size;
     77  1.11.14.1   thorpej 	prop_dictionary_t props;
     78  1.11.14.1   thorpej 	struct acpi_i2c_context i2cc;
     79  1.11.14.1   thorpej 	bool cbrv;
     80  1.11.14.1   thorpej 	ACPI_STATUS rv;
     81        1.1    bouyer 
     82        1.1    bouyer 	memset(&i2cc, 0, sizeof(i2cc));
     83        1.1    bouyer 	rv = AcpiWalkResources(ad->ad_handle, "_CRS",
     84        1.1    bouyer 	     acpi_i2c_resource_parse_callback, &i2cc);
     85        1.1    bouyer 	if (ACPI_FAILURE(rv)) {
     86        1.1    bouyer 		aprint_error("ACPI: unable to get resources "
     87        1.1    bouyer 		   "for %s: %s\n", ad->ad_name,
     88        1.1    bouyer 		   AcpiFormatException(rv));
     89  1.11.14.1   thorpej 		return true;	/* keep enumerating */
     90        1.1    bouyer 	}
     91        1.1    bouyer 	if (i2cc.i2c_addr == 0)
     92  1.11.14.1   thorpej 		return true;	/* keep enumerating */
     93  1.11.14.1   thorpej 
     94       1.11   thorpej 	clist = acpi_pack_compat_list(ad->ad_devinfo, &clist_size);
     95       1.11   thorpej 	if (clist == NULL) {
     96  1.11.14.1   thorpej 		aprint_error("ACPI: ignoring device %s (no _HID or _CID)\n",
     97       1.11   thorpej 		    ad->ad_name);
     98  1.11.14.1   thorpej 		return true;	/* keep enumerating */
     99       1.11   thorpej 	}
    100  1.11.14.1   thorpej 	props = prop_dictionary_create();
    101  1.11.14.1   thorpej 
    102  1.11.14.1   thorpej 	args->ia->ia_addr = i2cc.i2c_addr;
    103  1.11.14.1   thorpej 	args->ia->ia_name = ad->ad_name;
    104  1.11.14.1   thorpej 	args->ia->ia_clist = clist;
    105  1.11.14.1   thorpej 	args->ia->ia_clist_size = clist_size;
    106  1.11.14.1   thorpej 	args->ia->ia_prop = props;
    107  1.11.14.1   thorpej 	args->ia->ia_devhandle = devhandle_from_acpi(ad->ad_handle);
    108  1.11.14.1   thorpej 
    109  1.11.14.1   thorpej 	cbrv = args->callback(dev, args);
    110  1.11.14.1   thorpej 
    111  1.11.14.1   thorpej 	prop_object_release(props);
    112       1.11   thorpej 	kmem_free(clist, clist_size);
    113       1.11   thorpej 
    114  1.11.14.1   thorpej 	return cbrv;	/* callback decides if we keep enumerating */
    115        1.1    bouyer }
    116        1.1    bouyer 
    117  1.11.14.1   thorpej static int
    118  1.11.14.1   thorpej acpi_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
    119        1.1    bouyer {
    120  1.11.14.1   thorpej 	struct i2c_enumerate_devices_args *args = v;
    121  1.11.14.1   thorpej 	struct acpi_devnode *ad, *devnode;
    122  1.11.14.1   thorpej 	ACPI_HANDLE *hdl = devhandle_to_acpi(call_handle);
    123  1.11.14.1   thorpej 
    124  1.11.14.1   thorpej 	devnode = acpi_match_node(hdl);
    125  1.11.14.1   thorpej 	if (devnode == NULL) {
    126  1.11.14.1   thorpej 		aprint_verbose_dev(dev, "%s: no devnode matching handle\n",
    127  1.11.14.1   thorpej 		    __func__);
    128  1.11.14.1   thorpej 		return 0;
    129  1.11.14.1   thorpej 	}
    130        1.1    bouyer 
    131        1.1    bouyer 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    132        1.1    bouyer 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    133        1.1    bouyer 			continue;
    134        1.4  christos 		if (!acpi_device_present(ad->ad_handle))
    135        1.4  christos 			continue;
    136  1.11.14.1   thorpej 		if (!acpi_i2c_enumerate_device(dev, ad, args))
    137  1.11.14.1   thorpej 			break;
    138        1.1    bouyer 	}
    139       1.10  jmcneill 
    140  1.11.14.1   thorpej 	acpi_claim_childdevs(dev, devnode);
    141       1.10  jmcneill 
    142  1.11.14.1   thorpej 	return 0;
    143        1.1    bouyer }
    144  1.11.14.1   thorpej ACPI_DEVICE_CALL_REGISTER("i2c-enumerate-devices", acpi_i2c_enumerate_devices)
    145