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, "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, mux->of_offset); 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 ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent", 90 &priv->i2c_bus); 91 if (ret) 92 return ret; 93 debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name); 94 95 return 0; 96 } 97 98 int i2c_mux_select(struct udevice *dev) 99 { 100 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev); 101 struct udevice *mux = dev->parent; 102 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux); 103 104 if (!ops->select) 105 return -ENOSYS; 106 107 return ops->select(mux, dev, plat->channel); 108 } 109 110 int i2c_mux_deselect(struct udevice *dev) 111 { 112 struct i2c_mux_bus *plat = dev_get_parent_platdata(dev); 113 struct udevice *mux = dev->parent; 114 struct i2c_mux_ops *ops = i2c_mux_get_ops(mux); 115 116 if (!ops->deselect) 117 return -ENOSYS; 118 119 return ops->deselect(mux, dev, plat->channel); 120 } 121 122 static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed) 123 { 124 struct udevice *mux = dev->parent; 125 struct i2c_mux *priv = dev_get_uclass_priv(mux); 126 int ret, ret2; 127 128 ret = i2c_mux_select(dev); 129 if (ret) 130 return ret; 131 ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed); 132 ret2 = i2c_mux_deselect(dev); 133 134 return ret ? ret : ret2; 135 } 136 137 static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr, 138 uint chip_flags) 139 { 140 struct udevice *mux = dev->parent; 141 struct i2c_mux *priv = dev_get_uclass_priv(mux); 142 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus); 143 int ret, ret2; 144 145 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name); 146 if (!ops->probe_chip) 147 return -ENOSYS; 148 ret = i2c_mux_select(dev); 149 if (ret) 150 return ret; 151 ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags); 152 ret2 = i2c_mux_deselect(dev); 153 154 return ret ? ret : ret2; 155 } 156 157 static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg, 158 int nmsgs) 159 { 160 struct udevice *mux = dev->parent; 161 struct i2c_mux *priv = dev_get_uclass_priv(mux); 162 struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus); 163 int ret, ret2; 164 165 debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name); 166 if (!ops->xfer) 167 return -ENOSYS; 168 ret = i2c_mux_select(dev); 169 if (ret) 170 return ret; 171 ret = ops->xfer(priv->i2c_bus, msg, nmsgs); 172 ret2 = i2c_mux_deselect(dev); 173 174 return ret ? ret : ret2; 175 } 176 177 static const struct dm_i2c_ops i2c_mux_bus_ops = { 178 .xfer = i2c_mux_bus_xfer, 179 .probe_chip = i2c_mux_bus_probe, 180 .set_bus_speed = i2c_mux_bus_set_bus_speed, 181 }; 182 183 U_BOOT_DRIVER(i2c_mux_bus) = { 184 .name = "i2c_mux_bus_drv", 185 .id = UCLASS_I2C, 186 .ops = &i2c_mux_bus_ops, 187 }; 188 189 UCLASS_DRIVER(i2c_mux) = { 190 .id = UCLASS_I2C_MUX, 191 .name = "i2c_mux", 192 .post_bind = i2c_mux_post_bind, 193 .post_probe = i2c_mux_post_probe, 194 .per_device_auto_alloc_size = sizeof(struct i2c_mux), 195 .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus), 196 .child_post_bind = i2c_mux_child_post_bind, 197 }; 198