Home | History | Annotate | Line # | Download | only in fdt
fdt_i2c.c revision 1.9
      1 /* $NetBSD: fdt_i2c.c,v 1.9 2020/12/23 16:02:11 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2015 Jared D. McNeill <jmcneill (at) invisible.ca>
      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 AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #include <sys/cdefs.h>
     30 __KERNEL_RCSID(0, "$NetBSD: fdt_i2c.c,v 1.9 2020/12/23 16:02:11 thorpej Exp $");
     31 
     32 #include <sys/param.h>
     33 #include <sys/bus.h>
     34 #include <sys/kmem.h>
     35 #include <sys/queue.h>
     36 
     37 #include <libfdt.h>
     38 #include <dev/fdt/fdtvar.h>
     39 
     40 struct fdtbus_i2c_controller {
     41 	i2c_tag_t i2c_tag;
     42 	int i2c_phandle;
     43 
     44 	LIST_ENTRY(fdtbus_i2c_controller) i2c_next;
     45 };
     46 
     47 static LIST_HEAD(, fdtbus_i2c_controller) fdtbus_i2c_controllers =
     48     LIST_HEAD_INITIALIZER(fdtbus_i2c_controllers);
     49 
     50 int
     51 fdtbus_register_i2c_controller(i2c_tag_t tag, int phandle)
     52 {
     53 	struct fdtbus_i2c_controller *i2c;
     54 
     55 	i2c = kmem_alloc(sizeof(*i2c), KM_SLEEP);
     56 	i2c->i2c_tag = tag;
     57 	i2c->i2c_phandle = phandle;
     58 
     59 	LIST_INSERT_HEAD(&fdtbus_i2c_controllers, i2c, i2c_next);
     60 
     61 	return 0;
     62 }
     63 
     64 static struct fdtbus_i2c_controller *
     65 fdtbus_get_i2c_controller(int phandle)
     66 {
     67 	struct fdtbus_i2c_controller *i2c;
     68 
     69 	LIST_FOREACH(i2c, &fdtbus_i2c_controllers, i2c_next) {
     70 		if (i2c->i2c_phandle == phandle)
     71 			return i2c;
     72 	}
     73 
     74 	return NULL;
     75 }
     76 
     77 i2c_tag_t
     78 fdtbus_get_i2c_tag(int phandle)
     79 {
     80 	struct fdtbus_i2c_controller *i2c;
     81 
     82 	i2c = fdtbus_get_i2c_controller(phandle);
     83 	if (i2c == NULL)
     84 		return NULL;
     85 
     86 	return i2c->i2c_tag;
     87 }
     88 
     89 i2c_tag_t
     90 fdtbus_i2c_acquire(int phandle, const char *prop)
     91 {
     92 	int i2c_phandle;
     93 
     94 	i2c_phandle = fdtbus_get_phandle(phandle, prop);
     95 	if (i2c_phandle == -1)
     96 		return NULL;
     97 
     98 	return fdtbus_get_i2c_tag(i2c_phandle);
     99 }
    100 
    101 device_t
    102 fdtbus_attach_i2cbus(device_t dev, int phandle, i2c_tag_t tag, cfprint_t print)
    103 {
    104 	struct i2cbus_attach_args iba;
    105 	prop_dictionary_t devs, props;
    106 	device_t ret;
    107 	u_int address_cells;
    108 
    109 	devs = prop_dictionary_create();
    110 	if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
    111 		address_cells = 1;
    112 
    113 	of_enter_i2c_devs(devs, phandle, address_cells * 4, 0);
    114 
    115 	memset(&iba, 0, sizeof(iba));
    116 	iba.iba_tag = tag;
    117 	iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices");
    118 	if (iba.iba_child_devices)
    119 		prop_object_retain(iba.iba_child_devices);
    120 	prop_object_release(devs);
    121 
    122 	props = device_properties(dev);
    123 	prop_dictionary_set_bool(props, "i2c-no-indirect-config", true);
    124 
    125 	ret = config_found_ia(dev, "i2cbus", &iba, print);
    126 	if (iba.iba_child_devices)
    127 		prop_object_release(iba.iba_child_devices);
    128 
    129 	return ret;
    130 }
    131