Home | History | Annotate | Line # | Download | only in dev
smuiic.c revision 1.13
      1  1.13   thorpej /*	$NetBSD: smuiic.c,v 1.13 2025/09/18 02:51:03 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/device.h>
     33   1.1  macallan #include <sys/proc.h>
     34   1.1  macallan #include <sys/mutex.h>
     35   1.1  macallan #include <sys/sysctl.h>
     36   1.1  macallan 
     37   1.1  macallan #include <machine/autoconf.h>
     38   1.1  macallan 
     39   1.1  macallan #include <dev/ofw/openfirm.h>
     40   1.1  macallan #include <dev/i2c/i2cvar.h>
     41   1.1  macallan 
     42   1.1  macallan #include <macppc/dev/smuvar.h>
     43   1.1  macallan #include <macppc/dev/smuiicvar.h>
     44   1.1  macallan 
     45   1.1  macallan struct smuiic_softc {
     46   1.1  macallan 	device_t sc_dev;
     47   1.1  macallan 	int sc_node;
     48   1.1  macallan 	struct i2c_controller *sc_i2c;
     49   1.1  macallan };
     50   1.1  macallan 
     51   1.1  macallan static int smuiic_match(device_t, struct cfdata *, void *);
     52   1.1  macallan static void smuiic_attach(device_t, device_t, void *);
     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.6  macallan 	if (strcmp(ca->ca_name, "i2c") == 0)
     65   1.6  macallan 		return 5;
     66   1.1  macallan 
     67   1.1  macallan 	return 0;
     68   1.1  macallan }
     69   1.1  macallan 
     70   1.1  macallan static void
     71   1.1  macallan smuiic_attach(device_t parent, device_t self, void *aux)
     72   1.1  macallan {
     73   1.1  macallan 	struct smu_iicbus_confargs *ca = aux;
     74   1.1  macallan 	struct smuiic_softc *sc = device_private(self);
     75   1.3  macallan 	prop_dictionary_t dict = device_properties(self);
     76   1.6  macallan 	int devs, devc;
     77   1.3  macallan 	uint32_t addr;
     78   1.3  macallan 	char compat[256];
     79   1.3  macallan 	prop_array_t cfg;
     80   1.3  macallan 	prop_dictionary_t dev;
     81   1.3  macallan 	prop_data_t data;
     82   1.6  macallan 	char name[32], descr[32], num[8];
     83   1.1  macallan 
     84   1.1  macallan 	sc->sc_dev = self;
     85   1.1  macallan 	sc->sc_node = ca->ca_node;
     86   1.1  macallan 	sc->sc_i2c = ca->ca_tag;
     87   1.2  macallan 	printf("\n");
     88   1.1  macallan 
     89   1.3  macallan 	cfg = prop_array_create();
     90   1.3  macallan 	prop_dictionary_set(dict, "i2c-child-devices", cfg);
     91   1.3  macallan 	prop_object_release(cfg);
     92   1.3  macallan 
     93   1.3  macallan 	/* look for i2c devices */
     94   1.3  macallan 	devs = OF_child(sc->sc_node);
     95   1.3  macallan 	while (devs != 0) {
     96  1.13   thorpej 		devhandle_t child_devhandle;
     97  1.13   thorpej 
     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.13   thorpej 		child_devhandle = devhandle_from_of(devhandle_invalid(), devs);
    113  1.13   thorpej 		prop_dictionary_set_data(dev, "devhandle", &child_devhandle,
    114  1.13   thorpej 		    sizeof(child_devhandle));
    115  1.13   thorpej 
    116   1.6  macallan 		devc = OF_child(devs);
    117   1.6  macallan 		while (devc != 0) {
    118   1.6  macallan 			int reg;
    119   1.6  macallan 			if (OF_getprop(devc, "reg", &reg, 4) < 4) goto nope;
    120   1.6  macallan 			if (OF_getprop(devc, "location", descr, 32) <= 0)
    121   1.6  macallan 				goto nope;
    122   1.6  macallan 			printf("found '%s' at %02x\n", descr, reg);
    123   1.6  macallan 			snprintf(num, 7, "s%02x", reg);
    124   1.6  macallan 			prop_dictionary_set_string(dev, num, descr);
    125   1.6  macallan 		nope:
    126   1.6  macallan 			devc = OF_peer(devc);
    127   1.6  macallan 		}
    128   1.3  macallan 		prop_array_add(cfg, dev);
    129   1.3  macallan 		prop_object_release(dev);
    130   1.3  macallan 	skip:
    131   1.3  macallan 		devs = OF_peer(devs);
    132   1.3  macallan 	}
    133   1.3  macallan 
    134  1.12   thorpej 	iicbus_attach(sc->sc_dev, sc->sc_i2c);
    135   1.1  macallan }
    136