1 /* $NetBSD: fdt_i2c.c,v 1.8 2020/06/12 03:32:30 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2015 Jared D. McNeill <jmcneill@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.8 2020/06/12 03:32:30 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 device_t i2c_dev; 42 int i2c_phandle; 43 const struct fdtbus_i2c_controller_func *i2c_funcs; 44 45 LIST_ENTRY(fdtbus_i2c_controller) i2c_next; 46 }; 47 48 static LIST_HEAD(, fdtbus_i2c_controller) fdtbus_i2c_controllers = 49 LIST_HEAD_INITIALIZER(fdtbus_i2c_controllers); 50 51 int 52 fdtbus_register_i2c_controller(device_t dev, int phandle, 53 const struct fdtbus_i2c_controller_func *funcs) 54 { 55 struct fdtbus_i2c_controller *i2c; 56 57 i2c = kmem_alloc(sizeof(*i2c), KM_SLEEP); 58 i2c->i2c_dev = dev; 59 i2c->i2c_phandle = phandle; 60 i2c->i2c_funcs = funcs; 61 62 LIST_INSERT_HEAD(&fdtbus_i2c_controllers, i2c, i2c_next); 63 64 return 0; 65 } 66 67 static struct fdtbus_i2c_controller * 68 fdtbus_get_i2c_controller(int phandle) 69 { 70 struct fdtbus_i2c_controller *i2c; 71 72 LIST_FOREACH(i2c, &fdtbus_i2c_controllers, i2c_next) { 73 if (i2c->i2c_phandle == phandle) 74 return i2c; 75 } 76 77 return NULL; 78 } 79 80 i2c_tag_t 81 fdtbus_get_i2c_tag(int phandle) 82 { 83 struct fdtbus_i2c_controller *i2c; 84 85 i2c = fdtbus_get_i2c_controller(phandle); 86 if (i2c == NULL) 87 return NULL; 88 89 return i2c->i2c_funcs->get_tag(i2c->i2c_dev); 90 } 91 92 i2c_tag_t 93 fdtbus_i2c_acquire(int phandle, const char *prop) 94 { 95 int i2c_phandle; 96 97 i2c_phandle = fdtbus_get_phandle(phandle, prop); 98 if (i2c_phandle == -1) 99 return NULL; 100 101 return fdtbus_get_i2c_tag(i2c_phandle); 102 } 103 104 device_t 105 fdtbus_attach_i2cbus(device_t dev, int phandle, i2c_tag_t tag, cfprint_t print) 106 { 107 struct i2cbus_attach_args iba; 108 prop_dictionary_t devs, props; 109 device_t ret; 110 u_int address_cells; 111 112 devs = prop_dictionary_create(); 113 if (of_getprop_uint32(phandle, "#address-cells", &address_cells)) 114 address_cells = 1; 115 116 of_enter_i2c_devs(devs, phandle, address_cells * 4, 0); 117 118 memset(&iba, 0, sizeof(iba)); 119 iba.iba_tag = tag; 120 iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices"); 121 if (iba.iba_child_devices) 122 prop_object_retain(iba.iba_child_devices); 123 prop_object_release(devs); 124 125 props = device_properties(dev); 126 prop_dictionary_set_bool(props, "i2c-no-indirect-config", true); 127 128 ret = config_found_ia(dev, "i2cbus", &iba, print); 129 if (iba.iba_child_devices) 130 prop_object_release(iba.iba_child_devices); 131 132 return ret; 133 } 134