smuiic.c revision 1.12 1 /* $NetBSD: smuiic.c,v 1.12 2025/09/15 13:23:01 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2013 Phileas Fogg
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 NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/device.h>
33 #include <sys/proc.h>
34 #include <sys/mutex.h>
35 #include <sys/sysctl.h>
36
37 #include <machine/autoconf.h>
38
39 #include <dev/ofw/openfirm.h>
40 #include <dev/i2c/i2cvar.h>
41
42 #include <macppc/dev/smuvar.h>
43 #include <macppc/dev/smuiicvar.h>
44
45 struct smuiic_softc {
46 device_t sc_dev;
47 int sc_node;
48 struct i2c_controller *sc_i2c;
49 };
50
51 static int smuiic_match(device_t, struct cfdata *, void *);
52 static void smuiic_attach(device_t, device_t, void *);
53
54 CFATTACH_DECL_NEW(smuiic, sizeof(struct smuiic_softc),
55 smuiic_match, smuiic_attach, NULL, NULL);
56
57 static int
58 smuiic_match(device_t parent, struct cfdata *cf, void *aux)
59 {
60 struct smu_iicbus_confargs *ca = aux;
61
62 if (strcmp(ca->ca_name, "i2c-bus") == 0)
63 return 5;
64 if (strcmp(ca->ca_name, "i2c") == 0)
65 return 5;
66
67 return 0;
68 }
69
70 static void
71 smuiic_attach(device_t parent, device_t self, void *aux)
72 {
73 struct smu_iicbus_confargs *ca = aux;
74 struct smuiic_softc *sc = device_private(self);
75 prop_dictionary_t dict = device_properties(self);
76 int devs, devc;
77 uint32_t addr;
78 char compat[256];
79 prop_array_t cfg;
80 prop_dictionary_t dev;
81 prop_data_t data;
82 char name[32], descr[32], num[8];
83
84 sc->sc_dev = self;
85 sc->sc_node = ca->ca_node;
86 sc->sc_i2c = ca->ca_tag;
87 printf("\n");
88
89 cfg = prop_array_create();
90 prop_dictionary_set(dict, "i2c-child-devices", cfg);
91 prop_object_release(cfg);
92
93 /* look for i2c devices */
94 devs = OF_child(sc->sc_node);
95 while (devs != 0) {
96 if (OF_getprop(devs, "name", name, 256) <= 0)
97 goto skip;
98 if (OF_getprop(devs, "compatible",
99 compat, 256) <= 0)
100 goto skip;
101 if (OF_getprop(devs, "reg", &addr, 4) <= 0)
102 goto skip;
103 addr = (addr & 0xff) >> 1;
104 dev = prop_dictionary_create();
105 prop_dictionary_set_string(dev, "name", name);
106 data = prop_data_create_copy(compat, strlen(compat)+1);
107 prop_dictionary_set(dev, "compatible", data);
108 prop_object_release(data);
109 prop_dictionary_set_uint32(dev, "addr", addr);
110 prop_dictionary_set_uint64(dev, "cookie", devs);
111 devc = OF_child(devs);
112 while (devc != 0) {
113 int reg;
114 if (OF_getprop(devc, "reg", ®, 4) < 4) goto nope;
115 if (OF_getprop(devc, "location", descr, 32) <= 0)
116 goto nope;
117 printf("found '%s' at %02x\n", descr, reg);
118 snprintf(num, 7, "s%02x", reg);
119 prop_dictionary_set_string(dev, num, descr);
120 nope:
121 devc = OF_peer(devc);
122 }
123 prop_array_add(cfg, dev);
124 prop_object_release(dev);
125 skip:
126 devs = OF_peer(devs);
127 }
128
129 iicbus_attach(sc->sc_dev, sc->sc_i2c);
130 }
131