Home | History | Annotate | Line # | Download | only in acpi
acpi_i2c.c revision 1.4.2.1
      1  1.4.2.1    martin /* $NetBSD: acpi_i2c.c,v 1.4.2.1 2020/04/13 08:04:18 martin Exp $ */
      2      1.1    bouyer 
      3      1.1    bouyer /*-
      4      1.1    bouyer  * Copyright (c) 2017 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.4.2.1    martin __KERNEL_RCSID(0, "$NetBSD: acpi_i2c.c,v 1.4.2.1 2020/04/13 08:04:18 martin 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.1    bouyer #include <dev/acpi/acpi_i2c.h>
     38      1.1    bouyer 
     39      1.2    bouyer #define _COMPONENT	ACPI_BUS_COMPONENT
     40      1.2    bouyer ACPI_MODULE_NAME	("acpi_i2c")
     41      1.2    bouyer 
     42      1.1    bouyer static void
     43      1.1    bouyer acpi_enter_i2c_hid(struct acpi_devnode *devnode, prop_dictionary_t dev)
     44      1.1    bouyer {
     45      1.1    bouyer 	ACPI_OBJECT_LIST arg;
     46      1.1    bouyer 	ACPI_OBJECT obj[4];
     47      1.1    bouyer 	ACPI_OBJECT *osc;
     48      1.1    bouyer 	ACPI_BUFFER buf;
     49      1.1    bouyer 	ACPI_STATUS rv;
     50      1.1    bouyer 	/* 3cdff6f7-4267-4555-ad05-b30a3d8938de */
     51      1.1    bouyer 	static uint8_t i2c_hid_guid[] = {
     52      1.1    bouyer 		0xF7, 0xF6, 0xDF, 0x3C, 0x67, 0x42, 0x55, 0x45,
     53      1.1    bouyer 		0xAD, 0x05, 0xB3, 0x0A, 0x3D, 0x89, 0x38, 0xDE,
     54      1.1    bouyer 	};
     55      1.1    bouyer 
     56      1.1    bouyer 	arg.Count = 4;
     57      1.1    bouyer 	arg.Pointer = obj;
     58      1.1    bouyer 
     59      1.1    bouyer 	obj[0].Type = ACPI_TYPE_BUFFER;
     60      1.1    bouyer 	obj[0].Buffer.Length = sizeof(i2c_hid_guid);
     61      1.1    bouyer 	obj[0].Buffer.Pointer = i2c_hid_guid;
     62      1.1    bouyer 
     63      1.1    bouyer 	/* rev */
     64      1.1    bouyer 	obj[1].Type = ACPI_TYPE_INTEGER;
     65      1.1    bouyer 	obj[1].Integer.Value = 1;
     66      1.1    bouyer 
     67      1.1    bouyer 	/* func */
     68      1.1    bouyer 	obj[2].Type = ACPI_TYPE_INTEGER;
     69      1.1    bouyer 	obj[2].Integer.Value = 1;
     70      1.1    bouyer 
     71  1.4.2.1    martin 	obj[3].Type = ACPI_TYPE_PACKAGE;
     72      1.1    bouyer 	obj[3].Buffer.Length = 0;
     73      1.1    bouyer 
     74      1.1    bouyer 	buf.Pointer = NULL;
     75      1.1    bouyer 	buf.Length = ACPI_ALLOCATE_LOCAL_BUFFER;
     76      1.1    bouyer 
     77      1.1    bouyer 	rv = AcpiEvaluateObject(devnode->ad_handle, "_DSM", &arg, &buf);
     78      1.1    bouyer 
     79      1.1    bouyer 	if (ACPI_FAILURE(rv)) {
     80      1.1    bouyer 		aprint_error("failed to evaluate _DSM for %s: %s\n",
     81      1.1    bouyer 		    devnode->ad_name, AcpiFormatException(rv));
     82      1.1    bouyer 		return;
     83      1.1    bouyer 	}
     84      1.1    bouyer 
     85      1.1    bouyer 	osc = buf.Pointer;
     86      1.1    bouyer 	if (osc->Type != ACPI_TYPE_INTEGER) {
     87      1.1    bouyer 		aprint_error("bad _DSM return type %d for %s\n",
     88      1.1    bouyer 		    osc->Type, devnode->ad_name);
     89      1.1    bouyer 		return;
     90      1.1    bouyer 	}
     91      1.1    bouyer 	prop_dictionary_set_uint32(dev, "hid-descr-addr", osc->Integer.Value);
     92      1.1    bouyer }
     93      1.1    bouyer 
     94      1.1    bouyer struct acpi_i2c_id {
     95      1.1    bouyer 	const char *id;
     96      1.1    bouyer 	const char *compat;
     97      1.1    bouyer 	const int compatlen;
     98      1.1    bouyer 	void (*parse)(struct acpi_devnode *, prop_dictionary_t);
     99      1.1    bouyer };
    100      1.1    bouyer 
    101      1.1    bouyer static const struct acpi_i2c_id acpi_i2c_ids[] = {
    102      1.1    bouyer 	{
    103      1.1    bouyer 		.id = "PNP0C50",
    104      1.1    bouyer 		.compat = "hid-over-i2c",
    105      1.1    bouyer 		.compatlen = 13,
    106      1.1    bouyer 		.parse = acpi_enter_i2c_hid
    107      1.1    bouyer 	},
    108      1.1    bouyer 	{
    109      1.1    bouyer 		.id = "ACPI0C50",
    110      1.1    bouyer 		.compat = "hid-over-i2c",
    111      1.1    bouyer 		.compatlen = 13,
    112      1.1    bouyer 		.parse = acpi_enter_i2c_hid
    113      1.1    bouyer 	},
    114      1.1    bouyer 	{
    115      1.1    bouyer 		.id = NULL,
    116      1.1    bouyer 		.compat = NULL,
    117      1.1    bouyer 		.compatlen = 0,
    118      1.1    bouyer 		.parse = NULL
    119      1.1    bouyer 	}
    120      1.1    bouyer };
    121      1.1    bouyer 
    122      1.1    bouyer static const struct acpi_i2c_id *
    123      1.1    bouyer acpi_i2c_search(const char *name)
    124      1.1    bouyer {
    125      1.1    bouyer 	int i;
    126      1.1    bouyer 	for (i = 0; acpi_i2c_ids[i].id != NULL; i++) {
    127      1.1    bouyer 		if (strcmp(name, acpi_i2c_ids[i].id) == 0)
    128      1.1    bouyer 			return &acpi_i2c_ids[i];
    129      1.1    bouyer 	}
    130      1.1    bouyer 	return NULL;
    131      1.1    bouyer }
    132      1.1    bouyer 
    133      1.1    bouyer struct acpi_i2c_context {
    134      1.1    bouyer 	uint16_t i2c_addr;
    135      1.1    bouyer };
    136      1.1    bouyer 
    137      1.1    bouyer static ACPI_STATUS
    138      1.1    bouyer acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context)
    139      1.1    bouyer {
    140      1.1    bouyer 	struct acpi_i2c_context *i2cc = context;
    141      1.1    bouyer 
    142      1.1    bouyer 	switch (res->Type) {
    143      1.1    bouyer 	case ACPI_RESOURCE_TYPE_END_TAG:
    144      1.1    bouyer 		break;
    145      1.1    bouyer 	case ACPI_RESOURCE_TYPE_SERIAL_BUS:
    146      1.1    bouyer 		switch (res->Data.I2cSerialBus.Type) {
    147      1.1    bouyer 		case ACPI_RESOURCE_SERIAL_TYPE_I2C:
    148      1.1    bouyer 			i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
    149      1.1    bouyer 			break;
    150      1.1    bouyer 		}
    151      1.1    bouyer 		break;
    152      1.1    bouyer 	case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
    153      1.1    bouyer 		break;
    154      1.1    bouyer 	default:
    155      1.1    bouyer 		printf("ressource type 0x%x ignored\n", res->Type);
    156      1.1    bouyer 	}
    157      1.1    bouyer 	return_ACPI_STATUS(AE_OK);
    158      1.1    bouyer }
    159      1.1    bouyer 
    160      1.1    bouyer static void
    161      1.1    bouyer acpi_enter_i2c_device(struct acpi_devnode *ad, prop_array_t array)
    162      1.1    bouyer {
    163      1.1    bouyer 	prop_dictionary_t dev;
    164      1.1    bouyer 	struct acpi_i2c_context i2cc;
    165      1.1    bouyer 	ACPI_STATUS rv;
    166      1.1    bouyer 	int cidi;
    167      1.1    bouyer 	ACPI_PNP_DEVICE_ID_LIST *idlist;
    168      1.1    bouyer 	const char *name;
    169      1.1    bouyer 	static const struct acpi_i2c_id *i2c_id;
    170      1.1    bouyer 
    171      1.1    bouyer 	memset(&i2cc, 0, sizeof(i2cc));
    172      1.1    bouyer 	rv = AcpiWalkResources(ad->ad_handle, "_CRS",
    173      1.1    bouyer 	     acpi_i2c_resource_parse_callback, &i2cc);
    174      1.1    bouyer 	if (ACPI_FAILURE(rv)) {
    175      1.1    bouyer 		aprint_error("ACPI: unable to get resources "
    176      1.1    bouyer 		   "for %s: %s\n", ad->ad_name,
    177      1.1    bouyer 		   AcpiFormatException(rv));
    178      1.1    bouyer 		return;
    179      1.1    bouyer 	}
    180      1.1    bouyer 	if (i2cc.i2c_addr == 0)
    181      1.1    bouyer 		return;
    182      1.1    bouyer 	dev = prop_dictionary_create();
    183      1.1    bouyer 	if (dev == NULL) {
    184      1.1    bouyer 		aprint_error("ignoring device %s (no memory)\n",
    185      1.1    bouyer 		    ad->ad_name);
    186      1.1    bouyer 		return;
    187      1.1    bouyer 	}
    188      1.1    bouyer 	if ((ad->ad_devinfo->Valid &  ACPI_VALID_HID) == 0)
    189      1.1    bouyer 		name = ad->ad_name;
    190      1.1    bouyer 	else
    191      1.1    bouyer 		name = ad->ad_devinfo->HardwareId.String;
    192      1.1    bouyer 	prop_dictionary_set_cstring(dev, "name", name);
    193      1.1    bouyer 	prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
    194      1.1    bouyer 	prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
    195      1.1    bouyer 	/* first search by name, then by CID */
    196      1.1    bouyer 	i2c_id = acpi_i2c_search(name);
    197      1.1    bouyer 	idlist = &ad->ad_devinfo->CompatibleIdList;
    198      1.1    bouyer 	for (cidi = 0;
    199      1.1    bouyer 	    cidi < idlist->Count && i2c_id == NULL;
    200      1.1    bouyer 	    cidi++) {
    201      1.1    bouyer 		i2c_id = acpi_i2c_search(idlist->Ids[cidi].String);
    202      1.1    bouyer 	}
    203      1.1    bouyer 	if (i2c_id != NULL) {
    204      1.1    bouyer 		if (i2c_id->compat != NULL) {
    205      1.1    bouyer 			prop_data_t data;
    206      1.1    bouyer 			data = prop_data_create_data(i2c_id->compat,
    207      1.1    bouyer 			    i2c_id->compatlen);
    208      1.1    bouyer 			prop_dictionary_set(dev, "compatible", data);
    209      1.1    bouyer 			prop_object_release(data);
    210      1.1    bouyer 		}
    211      1.1    bouyer 		if (i2c_id->parse != NULL)
    212      1.1    bouyer 			i2c_id->parse(ad, dev);
    213      1.1    bouyer 	}
    214      1.1    bouyer 	prop_array_add(array, dev);
    215      1.1    bouyer 	prop_object_release(dev);
    216      1.1    bouyer }
    217      1.1    bouyer 
    218      1.1    bouyer 
    219      1.1    bouyer prop_array_t
    220      1.1    bouyer acpi_enter_i2c_devs(struct acpi_devnode *devnode)
    221      1.1    bouyer {
    222      1.1    bouyer 	struct acpi_devnode *ad;
    223      1.1    bouyer 	prop_array_t array = prop_array_create();
    224      1.1    bouyer 
    225      1.1    bouyer 	if (array == NULL)
    226      1.1    bouyer 		return NULL;
    227      1.1    bouyer 
    228      1.1    bouyer 	SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
    229      1.1    bouyer 		if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
    230      1.1    bouyer 			continue;
    231      1.4  christos 		if (!acpi_device_present(ad->ad_handle))
    232      1.4  christos 			continue;
    233      1.1    bouyer 		acpi_enter_i2c_device(ad, array);
    234      1.1    bouyer 	}
    235      1.1    bouyer 	return array;
    236      1.1    bouyer }
    237