Home | History | Annotate | Line # | Download | only in ofw
ofw_i2c_subr.c revision 1.1.6.1
      1  1.1.6.1  thorpej /*	$NetBSD: ofw_i2c_subr.c,v 1.1.6.1 2021/04/25 22:16:05 thorpej Exp $	*/
      2  1.1.6.1  thorpej 
      3  1.1.6.1  thorpej /*
      4  1.1.6.1  thorpej  * Copyright (c) 2021 The NetBSD Foundation, Inc.
      5  1.1.6.1  thorpej  * All rights reserved.
      6  1.1.6.1  thorpej  *
      7  1.1.6.1  thorpej  * Redistribution and use in source and binary forms, with or without
      8  1.1.6.1  thorpej  * modification, are permitted provided that the following conditions
      9  1.1.6.1  thorpej  * are met:
     10  1.1.6.1  thorpej  * 1. Redistributions of source code must retain the above copyright
     11  1.1.6.1  thorpej  *    notice, this list of conditions and the following disclaimer.
     12  1.1.6.1  thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1.6.1  thorpej  *    notice, this list of conditions and the following disclaimer in the
     14  1.1.6.1  thorpej  *    documentation and/or other materials provided with the distribution.
     15  1.1.6.1  thorpej  *
     16  1.1.6.1  thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     17  1.1.6.1  thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1.6.1  thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1.6.1  thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     20  1.1.6.1  thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     21  1.1.6.1  thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     22  1.1.6.1  thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     23  1.1.6.1  thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     24  1.1.6.1  thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     25  1.1.6.1  thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     26  1.1.6.1  thorpej  * POSSIBILITY OF SUCH DAMAGE.
     27  1.1.6.1  thorpej  */
     28      1.1  thorpej 
     29      1.1  thorpej /*
     30      1.1  thorpej  * Copyright 1998
     31      1.1  thorpej  * Digital Equipment Corporation. All rights reserved.
     32      1.1  thorpej  *
     33      1.1  thorpej  * This software is furnished under license and may be used and
     34      1.1  thorpej  * copied only in accordance with the following terms and conditions.
     35      1.1  thorpej  * Subject to these conditions, you may download, copy, install,
     36      1.1  thorpej  * use, modify and distribute this software in source and/or binary
     37      1.1  thorpej  * form. No title or ownership is transferred hereby.
     38      1.1  thorpej  *
     39      1.1  thorpej  * 1) Any source code used, modified or distributed must reproduce
     40      1.1  thorpej  *    and retain this copyright notice and list of conditions as
     41      1.1  thorpej  *    they appear in the source file.
     42      1.1  thorpej  *
     43      1.1  thorpej  * 2) No right is granted to use any trade name, trademark, or logo of
     44      1.1  thorpej  *    Digital Equipment Corporation. Neither the "Digital Equipment
     45      1.1  thorpej  *    Corporation" name nor any trademark or logo of Digital Equipment
     46      1.1  thorpej  *    Corporation may be used to endorse or promote products derived
     47      1.1  thorpej  *    from this software without the prior written permission of
     48      1.1  thorpej  *    Digital Equipment Corporation.
     49      1.1  thorpej  *
     50      1.1  thorpej  * 3) This software is provided "AS-IS" and any express or implied
     51      1.1  thorpej  *    warranties, including but not limited to, any implied warranties
     52      1.1  thorpej  *    of merchantability, fitness for a particular purpose, or
     53      1.1  thorpej  *    non-infringement are disclaimed. In no event shall DIGITAL be
     54      1.1  thorpej  *    liable for any damages whatsoever, and in particular, DIGITAL
     55      1.1  thorpej  *    shall not be liable for special, indirect, consequential, or
     56      1.1  thorpej  *    incidental damages or damages for lost profits, loss of
     57      1.1  thorpej  *    revenue or loss of use, whether such damages arise in contract,
     58      1.1  thorpej  *    negligence, tort, under statute, in equity, at law or otherwise,
     59      1.1  thorpej  *    even if advised of the possibility of such damage.
     60      1.1  thorpej  */
     61      1.1  thorpej 
     62      1.1  thorpej #include <sys/cdefs.h>
     63  1.1.6.1  thorpej __KERNEL_RCSID(0, "$NetBSD: ofw_i2c_subr.c,v 1.1.6.1 2021/04/25 22:16:05 thorpej Exp $");
     64      1.1  thorpej 
     65      1.1  thorpej #include <sys/param.h>
     66      1.1  thorpej #include <sys/device.h>
     67      1.1  thorpej #include <sys/kmem.h>
     68      1.1  thorpej #include <sys/systm.h>
     69      1.1  thorpej #include <dev/ofw/openfirm.h>
     70      1.1  thorpej #include <dev/i2c/i2cvar.h>
     71      1.1  thorpej 
     72      1.1  thorpej /*
     73  1.1.6.1  thorpej  * Standard routine for fetching an i2c device address, according
     74  1.1.6.1  thorpej  * to the standard OpenFirmware / Device Tree bindings.
     75      1.1  thorpej  */
     76  1.1.6.1  thorpej static bool
     77  1.1.6.1  thorpej of_i2c_get_address(int node, uint32_t *addrp)
     78  1.1.6.1  thorpej {
     79  1.1.6.1  thorpej 	uint32_t reg;
     80  1.1.6.1  thorpej 
     81  1.1.6.1  thorpej 	if (of_getprop_uint32(node, "reg", &reg) == -1) {
     82  1.1.6.1  thorpej 		return false;
     83  1.1.6.1  thorpej 	}
     84  1.1.6.1  thorpej 
     85  1.1.6.1  thorpej 	*addrp = reg;
     86  1.1.6.1  thorpej 	return true;
     87  1.1.6.1  thorpej }
     88  1.1.6.1  thorpej 
     89  1.1.6.1  thorpej static int
     90  1.1.6.1  thorpej of_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
     91      1.1  thorpej {
     92  1.1.6.1  thorpej 	return of_i2c_enumerate_devices_ext(dev, call_handle, v,
     93  1.1.6.1  thorpej 	    of_i2c_get_address);
     94  1.1.6.1  thorpej }
     95  1.1.6.1  thorpej OF_DEVICE_CALL_REGISTER("i2c-enumerate-devices", of_i2c_enumerate_devices);
     96      1.1  thorpej 
     97  1.1.6.1  thorpej int
     98  1.1.6.1  thorpej of_i2c_enumerate_devices_ext(device_t dev, devhandle_t call_handle, void *v,
     99  1.1.6.1  thorpej     bool (*get_address)(int, uint32_t *))
    100  1.1.6.1  thorpej {
    101  1.1.6.1  thorpej 	struct i2c_enumerate_devices_args *args = v;
    102  1.1.6.1  thorpej 	int i2c_node, node;
    103  1.1.6.1  thorpej 	char name[32], compat_buf[32];
    104  1.1.6.1  thorpej 	prop_dictionary_t props;
    105  1.1.6.1  thorpej 	uint32_t addr;
    106  1.1.6.1  thorpej 	char *clist;
    107  1.1.6.1  thorpej 	int clist_size;
    108  1.1.6.1  thorpej 	bool cbrv;
    109  1.1.6.1  thorpej 
    110  1.1.6.1  thorpej 	i2c_node = devhandle_to_of(call_handle);
    111  1.1.6.1  thorpej 
    112  1.1.6.1  thorpej 	for (node = OF_child(i2c_node); node != 0; node = OF_peer(node)) {
    113  1.1.6.1  thorpej 		if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
    114      1.1  thorpej 			continue;
    115  1.1.6.1  thorpej 		}
    116  1.1.6.1  thorpej 		if (!get_address(node, &addr)) {
    117      1.1  thorpej 			continue;
    118      1.1  thorpej 		}
    119      1.1  thorpej 
    120  1.1.6.1  thorpej 		clist_size = OF_getproplen(node, "compatible");
    121  1.1.6.1  thorpej 		clist = kmem_tmpbuf_alloc(clist_size,
    122  1.1.6.1  thorpej 		    compat_buf, sizeof(compat_buf), KM_SLEEP);
    123  1.1.6.1  thorpej 		if (OF_getprop(node, "compatible", clist, clist_size) <
    124  1.1.6.1  thorpej 		    clist_size) {
    125  1.1.6.1  thorpej 			kmem_tmpbuf_free(clist, clist_size, compat_buf);
    126  1.1.6.1  thorpej 			continue;
    127  1.1.6.1  thorpej 		}
    128  1.1.6.1  thorpej 		props = prop_dictionary_create();
    129      1.1  thorpej 
    130  1.1.6.1  thorpej 		args->ia->ia_addr = (i2c_addr_t)addr;
    131  1.1.6.1  thorpej 		args->ia->ia_name = name;
    132  1.1.6.1  thorpej 		args->ia->ia_clist = clist;
    133  1.1.6.1  thorpej 		args->ia->ia_clist_size = clist_size;
    134  1.1.6.1  thorpej 		args->ia->ia_prop = props;
    135  1.1.6.1  thorpej 		args->ia->ia_devhandle = devhandle_from_of(node);
    136  1.1.6.1  thorpej 		args->ia->ia_cookie = node;			/* XXX */
    137  1.1.6.1  thorpej 		args->ia->ia_cookietype = I2C_COOKIE_OF;	/* XXX */
    138  1.1.6.1  thorpej 
    139  1.1.6.1  thorpej 		cbrv = args->callback(dev, args);
    140  1.1.6.1  thorpej 
    141  1.1.6.1  thorpej 		prop_object_release(props);
    142  1.1.6.1  thorpej 		kmem_tmpbuf_free(clist, clist_size, compat_buf);
    143      1.1  thorpej 
    144  1.1.6.1  thorpej 		if (!cbrv) {
    145  1.1.6.1  thorpej 			break;
    146  1.1.6.1  thorpej 		}
    147      1.1  thorpej 	}
    148  1.1.6.1  thorpej 
    149  1.1.6.1  thorpej 	return 0;
    150      1.1  thorpej }
    151