smuiic.c revision 1.6 1 /* $NetBSD: smuiic.c,v 1.6 2021/02/25 20:51:10 macallan 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/malloc.h>
33 #include <sys/device.h>
34 #include <sys/proc.h>
35 #include <sys/mutex.h>
36 #include <sys/sysctl.h>
37
38 #include <machine/autoconf.h>
39
40 #include <dev/ofw/openfirm.h>
41 #include <dev/i2c/i2cvar.h>
42
43 #include <macppc/dev/smuvar.h>
44 #include <macppc/dev/smuiicvar.h>
45
46 struct smuiic_softc {
47 device_t sc_dev;
48 int sc_node;
49 struct i2c_controller *sc_i2c;
50 };
51
52 static int smuiic_match(device_t, struct cfdata *, void *);
53 static void smuiic_attach(device_t, device_t, void *);
54
55 CFATTACH_DECL_NEW(smuiic, sizeof(struct smuiic_softc),
56 smuiic_match, smuiic_attach, NULL, NULL);
57
58 static int
59 smuiic_match(device_t parent, struct cfdata *cf, void *aux)
60 {
61 struct smu_iicbus_confargs *ca = aux;
62
63 if (strcmp(ca->ca_name, "i2c-bus") == 0)
64 return 5;
65 if (strcmp(ca->ca_name, "i2c") == 0)
66 return 5;
67
68 return 0;
69 }
70
71 static void
72 smuiic_attach(device_t parent, device_t self, void *aux)
73 {
74 struct smu_iicbus_confargs *ca = aux;
75 struct smuiic_softc *sc = device_private(self);
76 struct i2cbus_attach_args iba;
77 prop_dictionary_t dict = device_properties(self);
78 int devs, devc;
79 uint32_t addr;
80 char compat[256];
81 prop_array_t cfg;
82 prop_dictionary_t dev;
83 prop_data_t data;
84 char name[32], descr[32], num[8];
85
86 sc->sc_dev = self;
87 sc->sc_node = ca->ca_node;
88 sc->sc_i2c = ca->ca_tag;
89 printf("\n");
90
91 cfg = prop_array_create();
92 prop_dictionary_set(dict, "i2c-child-devices", cfg);
93 prop_object_release(cfg);
94
95 /* look for i2c devices */
96 devs = OF_child(sc->sc_node);
97 while (devs != 0) {
98 if (OF_getprop(devs, "name", name, 256) <= 0)
99 goto skip;
100 if (OF_getprop(devs, "compatible",
101 compat, 256) <= 0)
102 goto skip;
103 if (OF_getprop(devs, "reg", &addr, 4) <= 0)
104 goto skip;
105 addr = (addr & 0xff) >> 1;
106 dev = prop_dictionary_create();
107 prop_dictionary_set_string(dev, "name", name);
108 data = prop_data_create_copy(compat, strlen(compat)+1);
109 prop_dictionary_set(dev, "compatible", data);
110 prop_object_release(data);
111 prop_dictionary_set_uint32(dev, "addr", addr);
112 prop_dictionary_set_uint64(dev, "cookie", devs);
113 devc = OF_child(devs);
114 while (devc != 0) {
115 int reg;
116 if (OF_getprop(devc, "reg", ®, 4) < 4) goto nope;
117 if (OF_getprop(devc, "location", descr, 32) <= 0)
118 goto nope;
119 printf("found '%s' at %02x\n", descr, reg);
120 snprintf(num, 7, "s%02x", reg);
121 prop_dictionary_set_string(dev, num, descr);
122 nope:
123 devc = OF_peer(devc);
124 }
125 prop_array_add(cfg, dev);
126 prop_object_release(dev);
127 skip:
128 devs = OF_peer(devs);
129 }
130
131 memset(&iba, 0, sizeof(iba));
132 iba.iba_tag = sc->sc_i2c;
133
134 config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
135 }
136