i2c-mux-uclass.c 5.3 KB

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