Home | History | Annotate | Line # | Download | only in dev
smuiic.c revision 1.2
      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 static int smuiic_print(void *, const char *);
     53 
     54 CFATTACH_DECL_NEW(smuiic, sizeof(struct smuiic_softc),
     55     smuiic_match, smuiic_attach, NULL, NULL);
     56 
     57 static int
     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 
     65 	return 0;
     66 }
     67 
     68 static void
     69 smuiic_attach(device_t parent, device_t self, void *aux)
     70 {
     71 	struct smu_iicbus_confargs *ca = aux;
     72 	struct smuiic_softc *sc = device_private(self);
     73 	struct i2cbus_attach_args iba;
     74 	struct smuiic_confargs sca;
     75 	int node, reg;
     76 	char name[32];
     77 
     78 	sc->sc_dev = self;
     79 	sc->sc_node = ca->ca_node;
     80 	sc->sc_i2c = ca->ca_tag;
     81 	printf("\n");
     82 
     83 	iba.iba_tag = sc->sc_i2c;
     84 
     85 	config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print);
     86 
     87 	for (node = OF_child(sc->sc_node); node != 0; node = OF_peer(node)) {
     88 		memset(name, 0, sizeof(name));
     89 		OF_getprop(node, "name", name, sizeof(name));
     90 
     91 		if (OF_getprop(node, "reg", &reg, sizeof(reg)) <= 0)
     92 			continue;
     93 
     94 		sca.ca_name = name;
     95 		sca.ca_node = node;
     96 		sca.ca_addr = reg & 0xfe;
     97 		sca.ca_tag = sc->sc_i2c;
     98 		config_found_ia(sc->sc_dev, "smuiic", &sca, smuiic_print);
     99 	}
    100 }
    101 
    102 static int
    103 smuiic_print(void *aux, const char *smuiic)
    104 {
    105 	struct smuiic_confargs *ca = aux;
    106 
    107 	if (smuiic) {
    108 		aprint_normal("%s at %s", ca->ca_name, smuiic);
    109 		aprint_normal(" address 0x%x", ca->ca_addr);
    110 	}
    111 
    112 	return UNCONF;
    113 }
    114