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