fdt_i2c.c revision 1.5 1 /* $NetBSD: fdt_i2c.c,v 1.5 2018/09/26 20:03:36 jakllsch 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.5 2018/09/26 20:03:36 jakllsch 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 device_t
93 fdtbus_attach_i2cbus(device_t dev, int phandle, i2c_tag_t tag, cfprint_t print)
94 {
95 struct i2cbus_attach_args iba;
96 prop_dictionary_t devs, props;
97 u_int address_cells;
98
99 devs = prop_dictionary_create();
100 if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
101 address_cells = 1;
102
103 of_enter_i2c_devs(devs, phandle, address_cells * 4, 0);
104
105 memset(&iba, 0, sizeof(iba));
106 iba.iba_tag = tag;
107 iba.iba_child_devices = prop_dictionary_get(devs, "i2c-child-devices");
108 if (iba.iba_child_devices)
109 prop_object_retain(iba.iba_child_devices);
110 prop_object_release(devs);
111
112 props = device_properties(dev);
113 prop_dictionary_set_bool(props, "i2c-indirect-config", false);
114
115 return config_found_ia(dev, "i2cbus", &iba, print);
116 }
117