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