1 /*
2  * Copyright (c) 2015 Google, Inc
3  * Written by Simon Glass <sjg@chromium.org>
4  *
5  * SPDX-License-Identifier:	GPL-2.0+
6  */
7 
8 #include <common.h>
9 #include <dm.h>
10 #include <errno.h>
11 #include <i2c.h>
12 #include <dm/lists.h>
13 #include <dm/root.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 /**
18  * struct i2c_mux: Information the uclass stores about an I2C mux
19  *
20  * @selected:	Currently selected mux, or -1 for none
21  * @i2c_bus: I2C bus to use for communcation
22  */
23 struct i2c_mux {
24 	int selected;
25 	struct udevice *i2c_bus;
26 };
27 
28 /**
29  * struct i2c_mux_bus: Information about each bus the mux controls
30  *
31  * @channel: Channel number used to select this bus
32  */
33 struct i2c_mux_bus {
34 	uint channel;
35 };
36 
37 /* Find out the mux channel number */
38 static int i2c_mux_child_post_bind(struct udevice *dev)
39 {
40 	struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
41 	int channel;
42 
43 	channel = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
44 	if (channel < 0)
45 		return -EINVAL;
46 	plat->channel = channel;
47 
48 	return 0;
49 }
50 
51 /* Find the I2C buses selected by this mux */
52 static int i2c_mux_post_bind(struct udevice *mux)
53 {
54 	const void *blob = gd->fdt_blob;
55 	int ret;
56 	int offset;
57 
58 	debug("%s: %s\n", __func__, mux->name);
59 	/*
60 	 * There is no compatible string in the sub-nodes, so we must manually
61 	 * bind these
62 	 */
63 	for (offset = fdt_first_subnode(blob, dev_of_offset(mux));
64 	     offset > 0;
65 	     offset = fdt_next_subnode(blob, offset)) {
66 		struct udevice *dev;
67 		const char *name;
68 
69 		name = fdt_get_name(blob, offset, NULL);
70 		ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv", name,
71 						 offset, &dev);
72 		debug("   - bind ret=%d, %s\n", ret, dev ? dev->name : NULL);
73 		if (ret)
74 			return ret;
75 	}
76 
77 	return 0;
78 }
79 
80 /* Set up the mux ready for use */
81 static int i2c_mux_post_probe(struct udevice *mux)
82 {
83 	struct i2c_mux *priv = dev_get_uclass_priv(mux);
84 	int ret;
85 
86 	debug("%s: %s\n", __func__, mux->name);
87 	priv->selected = -1;
88 
89 	/* if parent is of i2c uclass already, we'll take that, otherwise
90 	 * look if we find an i2c-parent phandle
91 	 */
92 	if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
93 		priv->i2c_bus = dev_get_parent(mux);
94 		debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
95 		      priv->i2c_bus->name);
96 		return 0;
97 	}
98 
99 	ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
100 					   &priv->i2c_bus);
101 	if (ret)
102 		return ret;
103 	debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
104 
105 	return 0;
106 }
107 
108 int i2c_mux_select(struct udevice *dev)
109 {
110 	struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
111 	struct udevice *mux = dev->parent;
112 	struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
113 
114 	if (!ops->select)
115 		return -ENOSYS;
116 
117 	return ops->select(mux, dev, plat->channel);
118 }
119 
120 int i2c_mux_deselect(struct udevice *dev)
121 {
122 	struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
123 	struct udevice *mux = dev->parent;
124 	struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
125 
126 	if (!ops->deselect)
127 		return -ENOSYS;
128 
129 	return ops->deselect(mux, dev, plat->channel);
130 }
131 
132 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
133 {
134 	struct udevice *mux = dev->parent;
135 	struct i2c_mux *priv = dev_get_uclass_priv(mux);
136 	int ret, ret2;
137 
138 	ret = i2c_mux_select(dev);
139 	if (ret)
140 		return ret;
141 	ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
142 	ret2 = i2c_mux_deselect(dev);
143 
144 	return ret ? ret : ret2;
145 }
146 
147 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
148 			     uint chip_flags)
149 {
150 	struct udevice *mux = dev->parent;
151 	struct i2c_mux *priv = dev_get_uclass_priv(mux);
152 	struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
153 	int ret, ret2;
154 
155 	debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
156 	if (!ops->probe_chip)
157 		return -ENOSYS;
158 	ret = i2c_mux_select(dev);
159 	if (ret)
160 		return ret;
161 	ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
162 	ret2 = i2c_mux_deselect(dev);
163 
164 	return ret ? ret : ret2;
165 }
166 
167 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
168 			    int nmsgs)
169 {
170 	struct udevice *mux = dev->parent;
171 	struct i2c_mux *priv = dev_get_uclass_priv(mux);
172 	struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
173 	int ret, ret2;
174 
175 	debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
176 	if (!ops->xfer)
177 		return -ENOSYS;
178 	ret = i2c_mux_select(dev);
179 	if (ret)
180 		return ret;
181 	ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
182 	ret2 = i2c_mux_deselect(dev);
183 
184 	return ret ? ret : ret2;
185 }
186 
187 static const struct dm_i2c_ops i2c_mux_bus_ops = {
188 	.xfer		= i2c_mux_bus_xfer,
189 	.probe_chip	= i2c_mux_bus_probe,
190 	.set_bus_speed	= i2c_mux_bus_set_bus_speed,
191 };
192 
193 U_BOOT_DRIVER(i2c_mux_bus) = {
194 	.name		= "i2c_mux_bus_drv",
195 	.id		= UCLASS_I2C,
196 	.ops	= &i2c_mux_bus_ops,
197 };
198 
199 UCLASS_DRIVER(i2c_mux) = {
200 	.id		= UCLASS_I2C_MUX,
201 	.name		= "i2c_mux",
202 	.post_bind	= i2c_mux_post_bind,
203 	.post_probe	= i2c_mux_post_probe,
204 	.per_device_auto_alloc_size = sizeof(struct i2c_mux),
205 	.per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
206 	.child_post_bind = i2c_mux_child_post_bind,
207 };
208