i2c-mux-uclass.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <i2c.h>
  11. #include <dm/lists.h>
  12. #include <dm/root.h>
  13. DECLARE_GLOBAL_DATA_PTR;
  14. /**
  15. * struct i2c_mux: Information the uclass stores about an I2C mux
  16. *
  17. * @selected: Currently selected mux, or -1 for none
  18. * @i2c_bus: I2C bus to use for communcation
  19. */
  20. struct i2c_mux {
  21. int selected;
  22. struct udevice *i2c_bus;
  23. };
  24. /**
  25. * struct i2c_mux_bus: Information about each bus the mux controls
  26. *
  27. * @channel: Channel number used to select this bus
  28. */
  29. struct i2c_mux_bus {
  30. uint channel;
  31. };
  32. /* Find out the mux channel number */
  33. static int i2c_mux_child_post_bind(struct udevice *dev)
  34. {
  35. struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
  36. int channel;
  37. channel = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "reg", -1);
  38. if (channel < 0)
  39. return -EINVAL;
  40. plat->channel = channel;
  41. return 0;
  42. }
  43. /* Find the I2C buses selected by this mux */
  44. static int i2c_mux_post_bind(struct udevice *mux)
  45. {
  46. const void *blob = gd->fdt_blob;
  47. int ret;
  48. int offset;
  49. debug("%s: %s\n", __func__, mux->name);
  50. /*
  51. * There is no compatible string in the sub-nodes, so we must manually
  52. * bind these
  53. */
  54. for (offset = fdt_first_subnode(blob, mux->of_offset);
  55. offset > 0;
  56. offset = fdt_next_subnode(blob, offset)) {
  57. struct udevice *dev;
  58. const char *name;
  59. name = fdt_get_name(blob, offset, NULL);
  60. ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv", name,
  61. offset, &dev);
  62. debug(" - bind ret=%d, %s\n", ret, dev ? dev->name : NULL);
  63. if (ret)
  64. return ret;
  65. }
  66. return 0;
  67. }
  68. /* Set up the mux ready for use */
  69. static int i2c_mux_post_probe(struct udevice *mux)
  70. {
  71. struct i2c_mux *priv = dev_get_uclass_priv(mux);
  72. int ret;
  73. debug("%s: %s\n", __func__, mux->name);
  74. priv->selected = -1;
  75. ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
  76. &priv->i2c_bus);
  77. if (ret)
  78. return ret;
  79. debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
  80. return 0;
  81. }
  82. int i2c_mux_select(struct udevice *dev)
  83. {
  84. struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
  85. struct udevice *mux = dev->parent;
  86. struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
  87. if (!ops->select)
  88. return -ENOSYS;
  89. return ops->select(mux, dev, plat->channel);
  90. }
  91. int i2c_mux_deselect(struct udevice *dev)
  92. {
  93. struct i2c_mux_bus *plat = dev_get_parent_platdata(dev);
  94. struct udevice *mux = dev->parent;
  95. struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
  96. if (!ops->deselect)
  97. return -ENOSYS;
  98. return ops->deselect(mux, dev, plat->channel);
  99. }
  100. static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
  101. {
  102. struct udevice *mux = dev->parent;
  103. struct i2c_mux *priv = dev_get_uclass_priv(mux);
  104. int ret, ret2;
  105. ret = i2c_mux_select(dev);
  106. if (ret)
  107. return ret;
  108. ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
  109. ret2 = i2c_mux_deselect(dev);
  110. return ret ? ret : ret2;
  111. }
  112. static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
  113. uint chip_flags)
  114. {
  115. struct udevice *mux = dev->parent;
  116. struct i2c_mux *priv = dev_get_uclass_priv(mux);
  117. struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
  118. int ret, ret2;
  119. debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
  120. if (!ops->probe_chip)
  121. return -ENOSYS;
  122. ret = i2c_mux_select(dev);
  123. if (ret)
  124. return ret;
  125. ret = ops->probe_chip(priv->i2c_bus, chip_addr, chip_flags);
  126. ret2 = i2c_mux_deselect(dev);
  127. return ret ? ret : ret2;
  128. }
  129. static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
  130. int nmsgs)
  131. {
  132. struct udevice *mux = dev->parent;
  133. struct i2c_mux *priv = dev_get_uclass_priv(mux);
  134. struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
  135. int ret, ret2;
  136. debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
  137. if (!ops->xfer)
  138. return -ENOSYS;
  139. ret = i2c_mux_select(dev);
  140. if (ret)
  141. return ret;
  142. ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
  143. ret2 = i2c_mux_deselect(dev);
  144. return ret ? ret : ret2;
  145. }
  146. static const struct dm_i2c_ops i2c_mux_bus_ops = {
  147. .xfer = i2c_mux_bus_xfer,
  148. .probe_chip = i2c_mux_bus_probe,
  149. .set_bus_speed = i2c_mux_bus_set_bus_speed,
  150. };
  151. U_BOOT_DRIVER(i2c_mux_bus) = {
  152. .name = "i2c_mux_bus_drv",
  153. .id = UCLASS_I2C,
  154. .ops = &i2c_mux_bus_ops,
  155. };
  156. UCLASS_DRIVER(i2c_mux) = {
  157. .id = UCLASS_I2C_MUX,
  158. .name = "i2c_mux",
  159. .post_bind = i2c_mux_post_bind,
  160. .post_probe = i2c_mux_post_probe,
  161. .per_device_auto_alloc_size = sizeof(struct i2c_mux),
  162. .per_child_platdata_auto_alloc_size = sizeof(struct i2c_mux_bus),
  163. .child_post_bind = i2c_mux_child_post_bind,
  164. };