acpi_i2c.c revision 1.8.2.1 1 /* $NetBSD: acpi_i2c.c,v 1.8.2.1 2021/04/03 22:28:43 thorpej 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.8.2.1 2021/04/03 22:28:43 thorpej Exp $");
34
35 #include <dev/acpi/acpireg.h>
36 #include <dev/acpi/acpivar.h>
37 #include <dev/acpi/acpi_i2c.h>
38 #include <dev/i2c/i2cvar.h>
39
40 #include <sys/kmem.h>
41
42 #define _COMPONENT ACPI_BUS_COMPONENT
43 ACPI_MODULE_NAME ("acpi_i2c")
44
45 struct acpi_i2c_context {
46 uint16_t i2c_addr;
47 };
48
49 static ACPI_STATUS
50 acpi_i2c_resource_parse_callback(ACPI_RESOURCE *res, void *context)
51 {
52 struct acpi_i2c_context *i2cc = context;
53
54 switch (res->Type) {
55 case ACPI_RESOURCE_TYPE_END_TAG:
56 break;
57 case ACPI_RESOURCE_TYPE_SERIAL_BUS:
58 switch (res->Data.I2cSerialBus.Type) {
59 case ACPI_RESOURCE_SERIAL_TYPE_I2C:
60 i2cc->i2c_addr = res->Data.I2cSerialBus.SlaveAddress;
61 break;
62 }
63 break;
64 case ACPI_RESOURCE_TYPE_EXTENDED_IRQ:
65 break;
66 default:
67 printf("resource type 0x%x ignored\n", res->Type);
68 }
69 return_ACPI_STATUS(AE_OK);
70 }
71
72 static void
73 acpi_enter_i2c_device(struct acpi_devnode *ad, prop_array_t array)
74 {
75 prop_dictionary_t dev;
76 struct acpi_i2c_context i2cc;
77 ACPI_STATUS rv;
78 const char *name;
79 char *clist;
80 size_t clist_size;
81
82 memset(&i2cc, 0, sizeof(i2cc));
83 rv = AcpiWalkResources(ad->ad_handle, "_CRS",
84 acpi_i2c_resource_parse_callback, &i2cc);
85 if (ACPI_FAILURE(rv)) {
86 aprint_error("ACPI: unable to get resources "
87 "for %s: %s\n", ad->ad_name,
88 AcpiFormatException(rv));
89 return;
90 }
91 if (i2cc.i2c_addr == 0)
92 return;
93 dev = prop_dictionary_create();
94 if (dev == NULL) {
95 aprint_error("ignoring device %s (no memory)\n",
96 ad->ad_name);
97 return;
98 }
99 clist = acpi_pack_compat_list(ad->ad_devinfo, &clist_size);
100 if (clist == NULL) {
101 prop_object_release(dev);
102 aprint_error("ignoring device %s (no _HID or _CID)\n",
103 ad->ad_name);
104 return;
105 }
106 if ((ad->ad_devinfo->Valid & ACPI_VALID_HID) == 0)
107 name = ad->ad_name;
108 else
109 name = ad->ad_devinfo->HardwareId.String;
110 prop_dictionary_set_string(dev, "name", name);
111 prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
112 prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
113 prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_ACPI);
114 prop_dictionary_set_data(dev, "compatible", clist, clist_size);
115 kmem_free(clist, clist_size);
116
117 prop_array_add(array, dev);
118 prop_object_release(dev);
119 }
120
121 prop_array_t
122 acpi_enter_i2c_devs(device_t dev, struct acpi_devnode *devnode)
123 {
124 struct acpi_devnode *ad;
125 prop_array_t array = prop_array_create();
126
127 if (array == NULL)
128 return NULL;
129
130 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
131 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
132 continue;
133 if (!acpi_device_present(ad->ad_handle))
134 continue;
135 acpi_enter_i2c_device(ad, array);
136 }
137
138 if (dev != NULL) {
139 acpi_claim_childdevs(dev, devnode);
140 }
141
142 return array;
143 }
144