1 /* $NetBSD: fdt_i2c.c,v 1.15 2025/09/16 11:55:17 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.15 2025/09/16 11:55:17 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 void 51 fdtbus_register_i2c_controller(device_t dev, i2c_tag_t tag) 52 { 53 int phandle = devhandle_to_of(device_handle(dev)); 54 struct fdtbus_i2c_controller *i2c; 55 56 i2c = kmem_alloc(sizeof(*i2c), KM_SLEEP); 57 i2c->i2c_tag = tag; 58 i2c->i2c_phandle = phandle; 59 60 LIST_INSERT_HEAD(&fdtbus_i2c_controllers, i2c, i2c_next); 61 } 62 63 static struct fdtbus_i2c_controller * 64 fdtbus_get_i2c_controller(int phandle) 65 { 66 struct fdtbus_i2c_controller *i2c; 67 68 LIST_FOREACH(i2c, &fdtbus_i2c_controllers, i2c_next) { 69 if (i2c->i2c_phandle == phandle) 70 return i2c; 71 } 72 73 return NULL; 74 } 75 76 i2c_tag_t 77 fdtbus_i2c_get_tag(int phandle) 78 { 79 struct fdtbus_i2c_controller *i2c; 80 81 i2c = fdtbus_get_i2c_controller(phandle); 82 if (i2c == NULL) 83 return NULL; 84 85 return i2c->i2c_tag; 86 } 87 88 i2c_tag_t 89 fdtbus_i2c_acquire(int phandle, const char *prop) 90 { 91 int i2c_phandle; 92 93 i2c_phandle = fdtbus_get_phandle(phandle, prop); 94 if (i2c_phandle == -1) 95 return NULL; 96 97 return fdtbus_i2c_get_tag(i2c_phandle); 98 } 99 100 prop_array_t 101 fdtbus_copy_i2c_devs(device_t dev) 102 { 103 int phandle = devhandle_to_of(device_handle(dev)); 104 u_int address_cells; 105 106 if (of_getprop_uint32(phandle, "#address-cells", &address_cells)) { 107 address_cells = 1; 108 } 109 110 return of_copy_i2c_devs(phandle, address_cells * 4, 0); 111 } 112