smuiic.c revision 1.5 1 1.5 macallan /* $NetBSD: smuiic.c,v 1.5 2020/07/02 12:47:19 macallan Exp $ */
2 1.5 macallan
3 1.1 macallan /*-
4 1.1 macallan * Copyright (c) 2013 Phileas Fogg
5 1.1 macallan * All rights reserved.
6 1.1 macallan *
7 1.1 macallan * Redistribution and use in source and binary forms, with or without
8 1.1 macallan * modification, are permitted provided that the following conditions
9 1.1 macallan * are met:
10 1.1 macallan * 1. Redistributions of source code must retain the above copyright
11 1.1 macallan * notice, this list of conditions and the following disclaimer.
12 1.1 macallan * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 macallan * notice, this list of conditions and the following disclaimer in the
14 1.1 macallan * documentation and/or other materials provided with the distribution.
15 1.1 macallan *
16 1.1 macallan * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.1 macallan * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.1 macallan * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.1 macallan * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.1 macallan * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.1 macallan * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.1 macallan * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.1 macallan * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.1 macallan * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.1 macallan * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.1 macallan * POSSIBILITY OF SUCH DAMAGE.
27 1.1 macallan */
28 1.1 macallan
29 1.1 macallan #include <sys/param.h>
30 1.1 macallan #include <sys/systm.h>
31 1.1 macallan #include <sys/kernel.h>
32 1.1 macallan #include <sys/malloc.h>
33 1.1 macallan #include <sys/device.h>
34 1.1 macallan #include <sys/proc.h>
35 1.1 macallan #include <sys/mutex.h>
36 1.1 macallan #include <sys/sysctl.h>
37 1.1 macallan
38 1.1 macallan #include <machine/autoconf.h>
39 1.1 macallan
40 1.1 macallan #include <dev/ofw/openfirm.h>
41 1.1 macallan #include <dev/i2c/i2cvar.h>
42 1.1 macallan
43 1.1 macallan #include <macppc/dev/smuvar.h>
44 1.1 macallan #include <macppc/dev/smuiicvar.h>
45 1.1 macallan
46 1.1 macallan struct smuiic_softc {
47 1.1 macallan device_t sc_dev;
48 1.1 macallan int sc_node;
49 1.1 macallan struct i2c_controller *sc_i2c;
50 1.1 macallan };
51 1.1 macallan
52 1.1 macallan static int smuiic_match(device_t, struct cfdata *, void *);
53 1.1 macallan static void smuiic_attach(device_t, device_t, void *);
54 1.1 macallan
55 1.1 macallan CFATTACH_DECL_NEW(smuiic, sizeof(struct smuiic_softc),
56 1.1 macallan smuiic_match, smuiic_attach, NULL, NULL);
57 1.1 macallan
58 1.1 macallan static int
59 1.1 macallan smuiic_match(device_t parent, struct cfdata *cf, void *aux)
60 1.1 macallan {
61 1.1 macallan struct smu_iicbus_confargs *ca = aux;
62 1.1 macallan
63 1.1 macallan if (strcmp(ca->ca_name, "i2c-bus") == 0)
64 1.1 macallan return 5;
65 1.1 macallan
66 1.1 macallan return 0;
67 1.1 macallan }
68 1.1 macallan
69 1.1 macallan static void
70 1.1 macallan smuiic_attach(device_t parent, device_t self, void *aux)
71 1.1 macallan {
72 1.1 macallan struct smu_iicbus_confargs *ca = aux;
73 1.1 macallan struct smuiic_softc *sc = device_private(self);
74 1.1 macallan struct i2cbus_attach_args iba;
75 1.3 macallan prop_dictionary_t dict = device_properties(self);
76 1.3 macallan int devs;
77 1.3 macallan uint32_t addr;
78 1.3 macallan char compat[256];
79 1.3 macallan prop_array_t cfg;
80 1.3 macallan prop_dictionary_t dev;
81 1.3 macallan prop_data_t data;
82 1.1 macallan char name[32];
83 1.1 macallan
84 1.1 macallan sc->sc_dev = self;
85 1.1 macallan sc->sc_node = ca->ca_node;
86 1.1 macallan sc->sc_i2c = ca->ca_tag;
87 1.2 macallan printf("\n");
88 1.1 macallan
89 1.3 macallan cfg = prop_array_create();
90 1.3 macallan prop_dictionary_set(dict, "i2c-child-devices", cfg);
91 1.3 macallan prop_object_release(cfg);
92 1.3 macallan
93 1.3 macallan /* look for i2c devices */
94 1.3 macallan devs = OF_child(sc->sc_node);
95 1.3 macallan while (devs != 0) {
96 1.3 macallan if (OF_getprop(devs, "name", name, 256) <= 0)
97 1.3 macallan goto skip;
98 1.3 macallan if (OF_getprop(devs, "compatible",
99 1.3 macallan compat, 256) <= 0)
100 1.3 macallan goto skip;
101 1.3 macallan if (OF_getprop(devs, "reg", &addr, 4) <= 0)
102 1.3 macallan goto skip;
103 1.3 macallan addr = (addr & 0xff) >> 1;
104 1.3 macallan dev = prop_dictionary_create();
105 1.4 macallan prop_dictionary_set_string(dev, "name", name);
106 1.4 macallan data = prop_data_create_copy(compat, strlen(compat)+1);
107 1.3 macallan prop_dictionary_set(dev, "compatible", data);
108 1.3 macallan prop_object_release(data);
109 1.3 macallan prop_dictionary_set_uint32(dev, "addr", addr);
110 1.3 macallan prop_dictionary_set_uint64(dev, "cookie", devs);
111 1.3 macallan prop_array_add(cfg, dev);
112 1.3 macallan prop_object_release(dev);
113 1.3 macallan skip:
114 1.3 macallan devs = OF_peer(devs);
115 1.3 macallan }
116 1.3 macallan
117 1.3 macallan memset(&iba, 0, sizeof(iba));
118 1.1 macallan iba.iba_tag = sc->sc_i2c;
119 1.2 macallan
120 1.1 macallan config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
121 1.1 macallan }
122