i2c-mux-uclass.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2015 Google, Inc
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #define LOG_CATEGORY UCLASS_I2C_MUX
  7. #include <common.h>
  8. #include <dm.h>
  9. #include <errno.h>
  10. #include <i2c.h>
  11. #include <log.h>
  12. #include <malloc.h>
  13. #include <dm/lists.h>
  14. #include <dm/root.h>
  15. /**
  16. * struct i2c_mux: Information the uclass stores about an I2C mux
  17. *
  18. * @selected: Currently selected mux, or -1 for none
  19. * @i2c_bus: I2C bus to use for communcation
  20. */
  21. struct i2c_mux {
  22. int selected;
  23. struct udevice *i2c_bus;
  24. };
  25. /**
  26. * struct i2c_mux_bus: Information about each bus the mux controls
  27. *
  28. * @channel: Channel number used to select this bus
  29. */
  30. struct i2c_mux_bus {
  31. uint channel;
  32. };
  33. /* Find out the mux channel number */
  34. static int i2c_mux_child_post_bind(struct udevice *dev)
  35. {
  36. struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
  37. int channel;
  38. channel = dev_read_u32_default(dev, "reg", -1);
  39. if (channel < 0)
  40. return -EINVAL;
  41. plat->channel = channel;
  42. return 0;
  43. }
  44. /* Find the I2C buses selected by this mux */
  45. static int i2c_mux_post_bind(struct udevice *mux)
  46. {
  47. ofnode node;
  48. int ret;
  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. dev_for_each_subnode(node, mux) {
  55. struct udevice *dev;
  56. const char *name;
  57. const char *arrow = "->";
  58. char *full_name;
  59. int parent_name_len, arrow_len, mux_name_len, name_len;
  60. name = ofnode_get_name(node);
  61. /* Calculate lenghts of strings */
  62. parent_name_len = strlen(mux->parent->name);
  63. arrow_len = strlen(arrow);
  64. mux_name_len = strlen(mux->name);
  65. name_len = strlen(name);
  66. full_name = calloc(1, parent_name_len + arrow_len +
  67. mux_name_len + arrow_len + name_len + 1);
  68. if (!full_name)
  69. return -ENOMEM;
  70. /* Compose bus name */
  71. strcat(full_name, mux->parent->name);
  72. strcat(full_name, arrow);
  73. strcat(full_name, mux->name);
  74. strcat(full_name, arrow);
  75. strcat(full_name, name);
  76. ret = device_bind_driver_to_node(mux, "i2c_mux_bus_drv",
  77. full_name, node, &dev);
  78. debug(" - bind ret=%d, %s, seq %d\n", ret,
  79. dev ? dev->name : NULL, dev_seq(dev));
  80. if (ret)
  81. return ret;
  82. }
  83. return 0;
  84. }
  85. /* Set up the mux ready for use */
  86. static int i2c_mux_post_probe(struct udevice *mux)
  87. {
  88. struct i2c_mux *priv = dev_get_uclass_priv(mux);
  89. int ret;
  90. debug("%s: %s\n", __func__, mux->name);
  91. priv->selected = -1;
  92. /* if parent is of i2c uclass already, we'll take that, otherwise
  93. * look if we find an i2c-parent phandle
  94. */
  95. if (UCLASS_I2C == device_get_uclass_id(mux->parent)) {
  96. priv->i2c_bus = dev_get_parent(mux);
  97. debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus,
  98. priv->i2c_bus->name);
  99. return 0;
  100. }
  101. ret = uclass_get_device_by_phandle(UCLASS_I2C, mux, "i2c-parent",
  102. &priv->i2c_bus);
  103. if (ret)
  104. return ret;
  105. debug("%s: bus=%p/%s\n", __func__, priv->i2c_bus, priv->i2c_bus->name);
  106. return 0;
  107. }
  108. int i2c_mux_select(struct udevice *dev)
  109. {
  110. struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
  111. struct udevice *mux = dev->parent;
  112. struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
  113. if (!ops->select)
  114. return -ENOSYS;
  115. return ops->select(mux, dev, plat->channel);
  116. }
  117. int i2c_mux_deselect(struct udevice *dev)
  118. {
  119. struct i2c_mux_bus *plat = dev_get_parent_plat(dev);
  120. struct udevice *mux = dev->parent;
  121. struct i2c_mux_ops *ops = i2c_mux_get_ops(mux);
  122. if (!ops->deselect)
  123. return -ENOSYS;
  124. return ops->deselect(mux, dev, plat->channel);
  125. }
  126. static int i2c_mux_bus_set_bus_speed(struct udevice *dev, unsigned int speed)
  127. {
  128. struct udevice *mux = dev->parent;
  129. struct i2c_mux *priv = dev_get_uclass_priv(mux);
  130. int ret, ret2;
  131. ret = i2c_mux_select(dev);
  132. if (ret)
  133. return ret;
  134. ret = dm_i2c_set_bus_speed(priv->i2c_bus, speed);
  135. ret2 = i2c_mux_deselect(dev);
  136. return ret ? ret : ret2;
  137. }
  138. static int i2c_mux_bus_probe(struct udevice *dev, uint chip_addr,
  139. uint chip_flags)
  140. {
  141. struct udevice *mux = dev->parent;
  142. struct i2c_mux *priv = dev_get_uclass_priv(mux);
  143. struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
  144. int ret, ret2;
  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. return ret ? ret : ret2;
  154. }
  155. static int i2c_mux_bus_xfer(struct udevice *dev, struct i2c_msg *msg,
  156. int nmsgs)
  157. {
  158. struct udevice *mux = dev->parent;
  159. struct i2c_mux *priv = dev_get_uclass_priv(mux);
  160. struct dm_i2c_ops *ops = i2c_get_ops(priv->i2c_bus);
  161. int ret, ret2;
  162. debug("%s: %s, bus %s\n", __func__, dev->name, priv->i2c_bus->name);
  163. if (!ops->xfer)
  164. return -ENOSYS;
  165. ret = i2c_mux_select(dev);
  166. if (ret)
  167. return ret;
  168. ret = ops->xfer(priv->i2c_bus, msg, nmsgs);
  169. ret2 = i2c_mux_deselect(dev);
  170. return ret ? ret : ret2;
  171. }
  172. static const struct dm_i2c_ops i2c_mux_bus_ops = {
  173. .xfer = i2c_mux_bus_xfer,
  174. .probe_chip = i2c_mux_bus_probe,
  175. .set_bus_speed = i2c_mux_bus_set_bus_speed,
  176. };
  177. U_BOOT_DRIVER(i2c_mux_bus) = {
  178. .name = "i2c_mux_bus_drv",
  179. .id = UCLASS_I2C,
  180. .ops = &i2c_mux_bus_ops,
  181. };
  182. UCLASS_DRIVER(i2c_mux) = {
  183. .id = UCLASS_I2C_MUX,
  184. .name = "i2c_mux",
  185. .post_bind = i2c_mux_post_bind,
  186. .post_probe = i2c_mux_post_probe,
  187. .per_device_auto = sizeof(struct i2c_mux),
  188. .per_child_plat_auto = sizeof(struct i2c_mux_bus),
  189. .child_post_bind = i2c_mux_child_post_bind,
  190. };