1 /* $NetBSD: smuiic.c,v 1.5 2020/07/02 12:47:19 macallan 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/malloc.h> 33 #include <sys/device.h> 34 #include <sys/proc.h> 35 #include <sys/mutex.h> 36 #include <sys/sysctl.h> 37 38 #include <machine/autoconf.h> 39 40 #include <dev/ofw/openfirm.h> 41 #include <dev/i2c/i2cvar.h> 42 43 #include <macppc/dev/smuvar.h> 44 #include <macppc/dev/smuiicvar.h> 45 46 struct smuiic_softc { 47 device_t sc_dev; 48 int sc_node; 49 struct i2c_controller *sc_i2c; 50 }; 51 52 static int smuiic_match(device_t, struct cfdata *, void *); 53 static void smuiic_attach(device_t, device_t, void *); 54 55 CFATTACH_DECL_NEW(smuiic, sizeof(struct smuiic_softc), 56 smuiic_match, smuiic_attach, NULL, NULL); 57 58 static int 59 smuiic_match(device_t parent, struct cfdata *cf, void *aux) 60 { 61 struct smu_iicbus_confargs *ca = aux; 62 63 if (strcmp(ca->ca_name, "i2c-bus") == 0) 64 return 5; 65 66 return 0; 67 } 68 69 static void 70 smuiic_attach(device_t parent, device_t self, void *aux) 71 { 72 struct smu_iicbus_confargs *ca = aux; 73 struct smuiic_softc *sc = device_private(self); 74 struct i2cbus_attach_args iba; 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 if (OF_getprop(devs, "name", name, 256) <= 0) 97 goto skip; 98 if (OF_getprop(devs, "compatible", 99 compat, 256) <= 0) 100 goto skip; 101 if (OF_getprop(devs, "reg", &addr, 4) <= 0) 102 goto skip; 103 addr = (addr & 0xff) >> 1; 104 dev = prop_dictionary_create(); 105 prop_dictionary_set_string(dev, "name", name); 106 data = prop_data_create_copy(compat, strlen(compat)+1); 107 prop_dictionary_set(dev, "compatible", data); 108 prop_object_release(data); 109 prop_dictionary_set_uint32(dev, "addr", addr); 110 prop_dictionary_set_uint64(dev, "cookie", devs); 111 prop_array_add(cfg, dev); 112 prop_object_release(dev); 113 skip: 114 devs = OF_peer(devs); 115 } 116 117 memset(&iba, 0, sizeof(iba)); 118 iba.iba_tag = sc->sc_i2c; 119 120 config_found_ia(sc->sc_dev, "i2cbus", &iba, iicbus_print); 121 } 122