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