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