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