xref: /netbsd-src/sys/arch/macppc/dev/smuiic.c (revision e5fbc36ada28f9b9a5836ecffaf4a06aa1ebb687)
1 /*	$NetBSD: smuiic.c,v 1.11 2023/12/20 15:29:04 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
smuiic_match(device_t parent,struct cfdata * cf,void * aux)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
smuiic_attach(device_t parent,device_t self,void * aux)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 	struct i2cbus_attach_args iba;
76 	prop_dictionary_t dict = device_properties(self);
77 	int devs, devc;
78 	uint32_t addr;
79 	char compat[256];
80 	prop_array_t cfg;
81 	prop_dictionary_t dev;
82 	prop_data_t data;
83 	char name[32], descr[32], num[8];
84 
85 	sc->sc_dev = self;
86 	sc->sc_node = ca->ca_node;
87 	sc->sc_i2c = ca->ca_tag;
88 	printf("\n");
89 
90 	cfg = prop_array_create();
91 	prop_dictionary_set(dict, "i2c-child-devices", cfg);
92 	prop_object_release(cfg);
93 
94 	/* look for i2c devices */
95 	devs = OF_child(sc->sc_node);
96 	while (devs != 0) {
97 		if (OF_getprop(devs, "name", name, 256) <= 0)
98 			goto skip;
99 		if (OF_getprop(devs, "compatible",
100 		    compat, 256) <= 0)
101 			goto skip;
102 		if (OF_getprop(devs, "reg", &addr, 4) <= 0)
103 			goto skip;
104 		addr = (addr & 0xff) >> 1;
105 		dev = prop_dictionary_create();
106 		prop_dictionary_set_string(dev, "name", name);
107 		data = prop_data_create_copy(compat, strlen(compat)+1);
108 		prop_dictionary_set(dev, "compatible", data);
109 		prop_object_release(data);
110 		prop_dictionary_set_uint32(dev, "addr", addr);
111 		prop_dictionary_set_uint64(dev, "cookie", devs);
112 		devc = OF_child(devs);
113 		while (devc != 0) {
114 			int reg;
115 			if (OF_getprop(devc, "reg", &reg, 4) < 4) goto nope;
116 			if (OF_getprop(devc, "location", descr, 32) <= 0)
117 				goto nope;
118 			printf("found '%s' at %02x\n", descr, reg);
119 			snprintf(num, 7, "s%02x", reg);
120 			prop_dictionary_set_string(dev, num, descr);
121 		nope:
122 			devc = OF_peer(devc);
123 		}
124 		prop_array_add(cfg, dev);
125 		prop_object_release(dev);
126 	skip:
127 		devs = OF_peer(devs);
128 	}
129 
130 	memset(&iba, 0, sizeof(iba));
131 	iba.iba_tag = sc->sc_i2c;
132 
133 	config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
134 }
135