acpi_i2c.c revision 1.12 1 /* $NetBSD: acpi_i2c.c,v 1.12 2022/07/23 03:08:17 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.12 2022/07/23 03:08:17 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 char *clist;
79 size_t clist_size;
80
81 memset(&i2cc, 0, sizeof(i2cc));
82 rv = AcpiWalkResources(ad->ad_handle, "_CRS",
83 acpi_i2c_resource_parse_callback, &i2cc);
84 if (ACPI_FAILURE(rv)) {
85 aprint_error("ACPI: unable to get resources "
86 "for %s: %s\n", ad->ad_name,
87 AcpiFormatException(rv));
88 return;
89 }
90 if (i2cc.i2c_addr == 0)
91 return;
92 dev = prop_dictionary_create();
93 if (dev == NULL) {
94 aprint_error("ignoring device %s (no memory)\n",
95 ad->ad_name);
96 return;
97 }
98 clist = acpi_pack_compat_list(ad, &clist_size);
99 if (clist == NULL) {
100 prop_object_release(dev);
101 aprint_error("ignoring device %s (no _HID or _CID)\n",
102 ad->ad_name);
103 return;
104 }
105 prop_dictionary_set_string(dev, "name", ad->ad_name);
106 prop_dictionary_set_uint32(dev, "addr", i2cc.i2c_addr);
107 prop_dictionary_set_uint64(dev, "cookie", (uintptr_t)ad->ad_handle);
108 prop_dictionary_set_uint32(dev, "cookietype", I2C_COOKIE_ACPI);
109 prop_dictionary_set_data(dev, "compatible", clist, clist_size);
110 kmem_free(clist, clist_size);
111
112 prop_array_add(array, dev);
113 prop_object_release(dev);
114 }
115
116 prop_array_t
117 acpi_enter_i2c_devs(device_t dev, struct acpi_devnode *devnode)
118 {
119 struct acpi_devnode *ad;
120 prop_array_t array = prop_array_create();
121
122 if (array == NULL)
123 return NULL;
124
125 SIMPLEQ_FOREACH(ad, &devnode->ad_child_head, ad_child_list) {
126 if (ad->ad_devinfo->Type != ACPI_TYPE_DEVICE)
127 continue;
128 if (!acpi_device_present(ad->ad_handle))
129 continue;
130 acpi_enter_i2c_device(ad, array);
131 }
132
133 if (dev != NULL) {
134 acpi_claim_childdevs(dev, devnode);
135 }
136
137 return array;
138 }
139