Home | History | Annotate | Line # | Download | only in ofw
ofw_i2c_subr.c revision 1.1.6.1
      1 /*	$NetBSD: ofw_i2c_subr.c,v 1.1.6.1 2021/04/25 22:16:05 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      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 /*
     30  * Copyright 1998
     31  * Digital Equipment Corporation. All rights reserved.
     32  *
     33  * This software is furnished under license and may be used and
     34  * copied only in accordance with the following terms and conditions.
     35  * Subject to these conditions, you may download, copy, install,
     36  * use, modify and distribute this software in source and/or binary
     37  * form. No title or ownership is transferred hereby.
     38  *
     39  * 1) Any source code used, modified or distributed must reproduce
     40  *    and retain this copyright notice and list of conditions as
     41  *    they appear in the source file.
     42  *
     43  * 2) No right is granted to use any trade name, trademark, or logo of
     44  *    Digital Equipment Corporation. Neither the "Digital Equipment
     45  *    Corporation" name nor any trademark or logo of Digital Equipment
     46  *    Corporation may be used to endorse or promote products derived
     47  *    from this software without the prior written permission of
     48  *    Digital Equipment Corporation.
     49  *
     50  * 3) This software is provided "AS-IS" and any express or implied
     51  *    warranties, including but not limited to, any implied warranties
     52  *    of merchantability, fitness for a particular purpose, or
     53  *    non-infringement are disclaimed. In no event shall DIGITAL be
     54  *    liable for any damages whatsoever, and in particular, DIGITAL
     55  *    shall not be liable for special, indirect, consequential, or
     56  *    incidental damages or damages for lost profits, loss of
     57  *    revenue or loss of use, whether such damages arise in contract,
     58  *    negligence, tort, under statute, in equity, at law or otherwise,
     59  *    even if advised of the possibility of such damage.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: ofw_i2c_subr.c,v 1.1.6.1 2021/04/25 22:16:05 thorpej Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/device.h>
     67 #include <sys/kmem.h>
     68 #include <sys/systm.h>
     69 #include <dev/ofw/openfirm.h>
     70 #include <dev/i2c/i2cvar.h>
     71 
     72 /*
     73  * Standard routine for fetching an i2c device address, according
     74  * to the standard OpenFirmware / Device Tree bindings.
     75  */
     76 static bool
     77 of_i2c_get_address(int node, uint32_t *addrp)
     78 {
     79 	uint32_t reg;
     80 
     81 	if (of_getprop_uint32(node, "reg", &reg) == -1) {
     82 		return false;
     83 	}
     84 
     85 	*addrp = reg;
     86 	return true;
     87 }
     88 
     89 static int
     90 of_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
     91 {
     92 	return of_i2c_enumerate_devices_ext(dev, call_handle, v,
     93 	    of_i2c_get_address);
     94 }
     95 OF_DEVICE_CALL_REGISTER("i2c-enumerate-devices", of_i2c_enumerate_devices);
     96 
     97 int
     98 of_i2c_enumerate_devices_ext(device_t dev, devhandle_t call_handle, void *v,
     99     bool (*get_address)(int, uint32_t *))
    100 {
    101 	struct i2c_enumerate_devices_args *args = v;
    102 	int i2c_node, node;
    103 	char name[32], compat_buf[32];
    104 	prop_dictionary_t props;
    105 	uint32_t addr;
    106 	char *clist;
    107 	int clist_size;
    108 	bool cbrv;
    109 
    110 	i2c_node = devhandle_to_of(call_handle);
    111 
    112 	for (node = OF_child(i2c_node); node != 0; node = OF_peer(node)) {
    113 		if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
    114 			continue;
    115 		}
    116 		if (!get_address(node, &addr)) {
    117 			continue;
    118 		}
    119 
    120 		clist_size = OF_getproplen(node, "compatible");
    121 		clist = kmem_tmpbuf_alloc(clist_size,
    122 		    compat_buf, sizeof(compat_buf), KM_SLEEP);
    123 		if (OF_getprop(node, "compatible", clist, clist_size) <
    124 		    clist_size) {
    125 			kmem_tmpbuf_free(clist, clist_size, compat_buf);
    126 			continue;
    127 		}
    128 		props = prop_dictionary_create();
    129 
    130 		args->ia->ia_addr = (i2c_addr_t)addr;
    131 		args->ia->ia_name = name;
    132 		args->ia->ia_clist = clist;
    133 		args->ia->ia_clist_size = clist_size;
    134 		args->ia->ia_prop = props;
    135 		args->ia->ia_devhandle = devhandle_from_of(node);
    136 		args->ia->ia_cookie = node;			/* XXX */
    137 		args->ia->ia_cookietype = I2C_COOKIE_OF;	/* XXX */
    138 
    139 		cbrv = args->callback(dev, args);
    140 
    141 		prop_object_release(props);
    142 		kmem_tmpbuf_free(clist, clist_size, compat_buf);
    143 
    144 		if (!cbrv) {
    145 			break;
    146 		}
    147 	}
    148 
    149 	return 0;
    150 }
    151