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