Home | History | Annotate | Line # | Download | only in dev
smuiic.c revision 1.7.8.1
      1  1.7.8.1   thorpej /*	$NetBSD: smuiic.c,v 1.7.8.1 2021/08/04 02:39:49 thorpej 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.6  macallan 	if (strcmp(ca->ca_name, "i2c") == 0)
     66      1.6  macallan 		return 5;
     67      1.1  macallan 
     68      1.1  macallan 	return 0;
     69      1.1  macallan }
     70      1.1  macallan 
     71      1.1  macallan static void
     72      1.1  macallan smuiic_attach(device_t parent, device_t self, void *aux)
     73      1.1  macallan {
     74      1.1  macallan 	struct smu_iicbus_confargs *ca = aux;
     75      1.1  macallan 	struct smuiic_softc *sc = device_private(self);
     76      1.1  macallan 	struct i2cbus_attach_args iba;
     77      1.3  macallan 	prop_dictionary_t dict = device_properties(self);
     78      1.6  macallan 	int devs, devc;
     79      1.3  macallan 	uint32_t addr;
     80      1.3  macallan 	char compat[256];
     81      1.3  macallan 	prop_array_t cfg;
     82      1.3  macallan 	prop_dictionary_t dev;
     83      1.3  macallan 	prop_data_t data;
     84      1.6  macallan 	char name[32], descr[32], num[8];
     85      1.1  macallan 
     86      1.1  macallan 	sc->sc_dev = self;
     87      1.1  macallan 	sc->sc_node = ca->ca_node;
     88      1.1  macallan 	sc->sc_i2c = ca->ca_tag;
     89      1.2  macallan 	printf("\n");
     90      1.1  macallan 
     91      1.3  macallan 	cfg = prop_array_create();
     92      1.3  macallan 	prop_dictionary_set(dict, "i2c-child-devices", cfg);
     93      1.3  macallan 	prop_object_release(cfg);
     94      1.3  macallan 
     95      1.3  macallan 	/* look for i2c devices */
     96      1.3  macallan 	devs = OF_child(sc->sc_node);
     97      1.3  macallan 	while (devs != 0) {
     98      1.3  macallan 		if (OF_getprop(devs, "name", name, 256) <= 0)
     99      1.3  macallan 			goto skip;
    100      1.3  macallan 		if (OF_getprop(devs, "compatible",
    101      1.3  macallan 		    compat, 256) <= 0)
    102      1.3  macallan 			goto skip;
    103      1.3  macallan 		if (OF_getprop(devs, "reg", &addr, 4) <= 0)
    104      1.3  macallan 			goto skip;
    105      1.3  macallan 		addr = (addr & 0xff) >> 1;
    106      1.3  macallan 		dev = prop_dictionary_create();
    107      1.4  macallan 		prop_dictionary_set_string(dev, "name", name);
    108      1.4  macallan 		data = prop_data_create_copy(compat, strlen(compat)+1);
    109      1.3  macallan 		prop_dictionary_set(dev, "compatible", data);
    110      1.3  macallan 		prop_object_release(data);
    111      1.3  macallan 		prop_dictionary_set_uint32(dev, "addr", addr);
    112      1.3  macallan 		prop_dictionary_set_uint64(dev, "cookie", devs);
    113      1.6  macallan 		devc = OF_child(devs);
    114      1.6  macallan 		while (devc != 0) {
    115      1.6  macallan 			int reg;
    116      1.6  macallan 			if (OF_getprop(devc, "reg", &reg, 4) < 4) goto nope;
    117      1.6  macallan 			if (OF_getprop(devc, "location", descr, 32) <= 0)
    118      1.6  macallan 				goto nope;
    119      1.6  macallan 			printf("found '%s' at %02x\n", descr, reg);
    120      1.6  macallan 			snprintf(num, 7, "s%02x", reg);
    121      1.6  macallan 			prop_dictionary_set_string(dev, num, descr);
    122      1.6  macallan 		nope:
    123      1.6  macallan 			devc = OF_peer(devc);
    124      1.6  macallan 		}
    125      1.3  macallan 		prop_array_add(cfg, dev);
    126      1.3  macallan 		prop_object_release(dev);
    127      1.3  macallan 	skip:
    128      1.3  macallan 		devs = OF_peer(devs);
    129      1.3  macallan 	}
    130      1.3  macallan 
    131      1.3  macallan 	memset(&iba, 0, sizeof(iba));
    132      1.1  macallan 	iba.iba_tag = sc->sc_i2c;
    133      1.2  macallan 
    134  1.7.8.1   thorpej 	config_found(sc->sc_dev, &iba, iicbus_print, CFARGS_NONE);
    135      1.1  macallan }
    136