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