fdt_i2c.c revision 1.13 1 /* $NetBSD: fdt_i2c.c,v 1.13 2025/09/16 11:37:17 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: fdt_i2c.c,v 1.13 2025/09/16 11:37:17 thorpej Exp $");
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/kmem.h>
35 #include <sys/queue.h>
36
37 #include <libfdt.h>
38 #include <dev/fdt/fdtvar.h>
39
40 struct fdtbus_i2c_controller {
41 i2c_tag_t i2c_tag;
42 int i2c_phandle;
43
44 LIST_ENTRY(fdtbus_i2c_controller) i2c_next;
45 };
46
47 static LIST_HEAD(, fdtbus_i2c_controller) fdtbus_i2c_controllers =
48 LIST_HEAD_INITIALIZER(fdtbus_i2c_controllers);
49
50 int
51 fdtbus_register_i2c_controller(i2c_tag_t tag, int phandle)
52 {
53 struct fdtbus_i2c_controller *i2c;
54
55 i2c = kmem_alloc(sizeof(*i2c), KM_SLEEP);
56 i2c->i2c_tag = tag;
57 i2c->i2c_phandle = phandle;
58
59 LIST_INSERT_HEAD(&fdtbus_i2c_controllers, i2c, i2c_next);
60
61 return 0;
62 }
63
64 static struct fdtbus_i2c_controller *
65 fdtbus_get_i2c_controller(int phandle)
66 {
67 struct fdtbus_i2c_controller *i2c;
68
69 LIST_FOREACH(i2c, &fdtbus_i2c_controllers, i2c_next) {
70 if (i2c->i2c_phandle == phandle)
71 return i2c;
72 }
73
74 return NULL;
75 }
76
77 i2c_tag_t
78 fdtbus_i2c_get_tag(int phandle)
79 {
80 struct fdtbus_i2c_controller *i2c;
81
82 i2c = fdtbus_get_i2c_controller(phandle);
83 if (i2c == NULL)
84 return NULL;
85
86 return i2c->i2c_tag;
87 }
88
89 i2c_tag_t
90 fdtbus_i2c_acquire(int phandle, const char *prop)
91 {
92 int i2c_phandle;
93
94 i2c_phandle = fdtbus_get_phandle(phandle, prop);
95 if (i2c_phandle == -1)
96 return NULL;
97
98 return fdtbus_i2c_get_tag(i2c_phandle);
99 }
100
101 device_t
102 fdtbus_attach_i2cbus(device_t dev, int phandle __unused, i2c_tag_t tag,
103 cfprint_t print __unused)
104 {
105 return iicbus_attach(dev, tag);
106 }
107
108 prop_array_t
109 fdtbus_copy_i2c_devs(device_t dev)
110 {
111 int phandle = devhandle_to_of(device_handle(dev));
112 u_int address_cells;
113
114 if (of_getprop_uint32(phandle, "#address-cells", &address_cells)) {
115 address_cells = 1;
116 }
117
118 return of_copy_i2c_devs(phandle, address_cells * 4, 0);
119 }
120