blob: 187e8a7c91730afe56ab52ca04ee39dc1b0b0002 [file] [log] [blame]
Simon Glass2a80c402015-08-03 08:19:21 -06001/*
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
15DECLARE_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 */
23struct 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 */
33struct i2c_mux_bus {
34 uint channel;
35};
36
37/* Find out the mux channel number */
38static 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
Simon Glassdd79d6e2017-01-17 16:52:55 -070043 channel = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "reg", -1);
Simon Glass2a80c402015-08-03 08:19:21 -060044 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 */
52static int i2c_mux_post_bind(struct udevice *mux)
53{
Simon Glasse6dd8da2017-05-18 20:09:07 -060054 ofnode node;
Simon Glass2a80c402015-08-03 08:19:21 -060055 int ret;
Simon Glass2a80c402015-08-03 08:19:21 -060056
57 debug("%s: %s\n", __func__, mux->name);
58 /*
59 * There is no compatible string in the sub-nodes, so we must manually
60 * bind these
61 */
Simon Glasse6dd8da2017-05-18 20:09:07 -060062 dev_for_each_subnode(node, mux) {
Simon Glass2a80c402015-08-03 08:19:21 -060063 struct udevice *dev;
64 const char *name;
65
Simon Glasse6dd8da2017-05-18 20:09:07 -060066 name = ofnode_get_name(node);
Simon Glass2a80c402015-08-03 08:19:21 -060067 ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv", name,
Simon Glasse6dd8da2017-05-18 20:09:07 -060068 node, &dev);
Simon Glass2a80c402015-08-03 08:19:21 -060069 debug(" - bind ret=%d, %s\n", ret, dev ? dev->name : NULL);
70 if (ret)
71 return ret;
72 }
73
74 return 0;
75}
76
77/* Set up the mux ready for use */
78static int i2c_mux_post_probe(struct udevice *mux)
79{
80 struct i2c_mux *priv = dev_get_uclass_priv(mux);
81 int ret;
82
83 debug("%s: %s\n", __func__, mux->name);
84 priv->selected = -1;
85
Moritz Fischerc5d3a5e2017-01-16 09:46:06 -080086 /* if parent is of i2c uclass already, we'll take that, otherwise
87 * look if we find an i2c-parent phandle
88 */
89 if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
90 priv->i2c_bus = dev_get_parent(mux);
91 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
92 priv->i2c_bus->name);
93 return 0;
94 }
95
Simon Glass2a80c402015-08-03 08:19:21 -060096 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
97 &priv->i2c_bus);
98 if (ret)
99 return ret;
100 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
101
102 return 0;
103}
104
105int i2c_mux_select(struct udevice *dev)
106{
107 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
108 struct udevice *mux = dev->parent;
109 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
110
111 if (!ops->select)
112 return -ENOSYS;
113
114 return ops->select(mux, dev, plat->channel);
115}
116
117int i2c_mux_deselect(struct udevice *dev)
118{
119 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
120 struct udevice *mux = dev->parent;
121 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
122
123 if (!ops->deselect)
124 return -ENOSYS;
125
126 return ops->deselect(mux, dev, plat->channel);
127}
128
129static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
130{
131 struct udevice *mux = dev->parent;
132 struct i2c_mux *priv = dev_get_uclass_priv(mux);
133 int ret, ret2;
134
135 ret = i2c_mux_select(dev);
136 if (ret)
137 return ret;
138 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
139 ret2 = i2c_mux_deselect(dev);
140
141 return ret ? ret : ret2;
142}
143
144static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
145 uint chip_flags)
146{
147 struct udevice *mux = dev->parent;
148 struct i2c_mux *priv = dev_get_uclass_priv(mux);
149 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
150 int ret, ret2;
151
152 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
153 if (!ops->probe_chip)
154 return -ENOSYS;
155 ret = i2c_mux_select(dev);
156 if (ret)
157 return ret;
158 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
159 ret2 = i2c_mux_deselect(dev);
160
161 return ret ? ret : ret2;
162}
163
164static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
165 int nmsgs)
166{
167 struct udevice *mux = dev->parent;
168 struct i2c_mux *priv = dev_get_uclass_priv(mux);
169 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
170 int ret, ret2;
171
172 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
173 if (!ops->xfer)
174 return -ENOSYS;
175 ret = i2c_mux_select(dev);
176 if (ret)
177 return ret;
178 ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
179 ret2 = i2c_mux_deselect(dev);
180
181 return ret ? ret : ret2;
182}
183
184static const struct dm_i2c_ops i2c_mux_bus_ops = {
185 .xfer = i2c_mux_bus_xfer,
186 .probe_chip = i2c_mux_bus_probe,
187 .set_bus_speed = i2c_mux_bus_set_bus_speed,
188};
189
190U_BOOT_DRIVER(i2c_mux_bus) = {
191 .name = "i2c_mux_bus_drv",
192 .id = UCLASS_I2C,
Simon Glass2a80c402015-08-03 08:19:21 -0600193 .ops = &i2c_mux_bus_ops,
194};
195
196UCLASS_DRIVER(i2c_mux) = {
197 .id = UCLASS_I2C_MUX,
198 .name = "i2c_mux",
199 .post_bind = i2c_mux_post_bind,
200 .post_probe = i2c_mux_post_probe,
201 .per_device_auto_alloc_size = sizeof(struct i2c_mux),
202 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
203 .child_post_bind = i2c_mux_child_post_bind,
204};