i2c_enum.h revision 1.2 1 /* $NetBSD: i2c_enum.h,v 1.2 2025/09/21 17:54:16 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2021, 2025 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef _DEV_I2C_I2C_ENUM_H_
33 #define _DEV_I2C_I2C_ENUM_H_
34
35 #include <dev/i2c/i2cvar.h>
36
37 /*
38 * Helpers for enumerating known i2c devices, that can be used from
39 * the i2c-enumerate-devices device call in cases where platform
40 * device tree information is unavailable or incomplete.
41 */
42 struct i2c_deventry {
43 const char *name;
44 const char *compat;
45 i2c_addr_t addr;
46 union {
47 const void *data;
48 uintptr_t value;
49 };
50 };
51
52 #define I2C_DEVENTRY_EOL { 0 }
53
54 static inline bool __unused
55 i2c_enumerate_device(device_t dev, struct i2c_enumerate_devices_args *args,
56 const char *name, const char *clist, size_t clist_size, i2c_addr_t addr,
57 devhandle_t child_devhandle)
58 {
59 args->ia->ia_addr = addr;
60 args->ia->ia_name = name;
61 args->ia->ia_clist = clist;
62 if (clist == NULL) {
63 clist_size = 0;
64 } else if (clist_size == 0) {
65 clist_size = strlen(clist) + 1;
66 }
67 args->ia->ia_clist_size = clist_size;
68 args->ia->ia_devhandle = child_devhandle;
69
70 return args->callback(dev, args);
71 }
72
73 static inline int __unused
74 i2c_enumerate_deventries(device_t dev, devhandle_t call_handle,
75 struct i2c_enumerate_devices_args *args,
76 const struct i2c_deventry *entry,
77 devhandle_t (*get_devhandle)(device_t, devhandle_t,
78 const struct i2c_deventry *))
79 {
80 devhandle_t child_devhandle;
81 bool cbrv;
82
83 for (; entry->name != NULL; entry++) {
84 if (get_devhandle != NULL) {
85 child_devhandle =
86 (*get_devhandle)(dev, call_handle, entry);
87 } else {
88 child_devhandle = devhandle_invalid();
89 }
90
91 cbrv = i2c_enumerate_device(dev, args, entry->name,
92 entry->compat, 0, entry->addr,
93 child_devhandle);
94
95 if (!cbrv) {
96 break;
97 }
98 }
99
100 return 0;
101 }
102
103 #endif /* _DEV_I2C_I2C_ENUM_H_ */
104