i2c_mux_pca954x.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * I2C multiplexer for PCA954x series of I2C multiplexer/switch chips.
  3. *
  4. * Copyright 2021 Google LLC
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * for more details.
  15. */
  16. #include "qemu/osdep.h"
  17. #include "qapi/error.h"
  18. #include "hw/i2c/i2c.h"
  19. #include "hw/i2c/i2c_mux_pca954x.h"
  20. #include "hw/i2c/smbus_slave.h"
  21. #include "hw/qdev-core.h"
  22. #include "hw/sysbus.h"
  23. #include "qemu/log.h"
  24. #include "qemu/module.h"
  25. #include "qemu/queue.h"
  26. #include "qom/object.h"
  27. #include "trace.h"
  28. #define PCA9548_CHANNEL_COUNT 8
  29. #define PCA9546_CHANNEL_COUNT 4
  30. /*
  31. * struct Pca954xState - The pca954x state object.
  32. * @control: The value written to the mux control.
  33. * @channel: The set of i2c channel buses that act as channels which own the
  34. * i2c children.
  35. */
  36. typedef struct Pca954xState {
  37. SMBusDevice parent;
  38. uint8_t control;
  39. bool enabled[PCA9548_CHANNEL_COUNT];
  40. I2CBus *bus[PCA9548_CHANNEL_COUNT];
  41. } Pca954xState;
  42. /*
  43. * struct Pca954xClass - The pca954x class object.
  44. * @nchans: The number of i2c channels this device has.
  45. */
  46. typedef struct Pca954xClass {
  47. SMBusDeviceClass parent;
  48. uint8_t nchans;
  49. } Pca954xClass;
  50. #define TYPE_PCA954X "pca954x"
  51. OBJECT_DECLARE_TYPE(Pca954xState, Pca954xClass, PCA954X)
  52. /*
  53. * For each channel, if it's enabled, recursively call match on those children.
  54. */
  55. static bool pca954x_match(I2CSlave *candidate, uint8_t address,
  56. bool broadcast,
  57. I2CNodeList *current_devs)
  58. {
  59. Pca954xState *mux = PCA954X(candidate);
  60. Pca954xClass *mc = PCA954X_GET_CLASS(mux);
  61. int i;
  62. /* They are talking to the mux itself (or all devices enabled). */
  63. if ((candidate->address == address) || broadcast) {
  64. I2CNode *node = g_new(struct I2CNode, 1);
  65. node->elt = candidate;
  66. QLIST_INSERT_HEAD(current_devs, node, next);
  67. if (!broadcast) {
  68. return true;
  69. }
  70. }
  71. for (i = 0; i < mc->nchans; i++) {
  72. if (!mux->enabled[i]) {
  73. continue;
  74. }
  75. if (i2c_scan_bus(mux->bus[i], address, broadcast,
  76. current_devs)) {
  77. if (!broadcast) {
  78. return true;
  79. }
  80. }
  81. }
  82. /* If we arrived here we didn't find a match, return broadcast. */
  83. return broadcast;
  84. }
  85. static void pca954x_enable_channel(Pca954xState *s, uint8_t enable_mask)
  86. {
  87. Pca954xClass *mc = PCA954X_GET_CLASS(s);
  88. int i;
  89. /*
  90. * For each channel, check if their bit is set in enable_mask and if yes,
  91. * enable it, otherwise disable, hide it.
  92. */
  93. for (i = 0; i < mc->nchans; i++) {
  94. if (enable_mask & (1 << i)) {
  95. s->enabled[i] = true;
  96. } else {
  97. s->enabled[i] = false;
  98. }
  99. }
  100. }
  101. static void pca954x_write(Pca954xState *s, uint8_t data)
  102. {
  103. s->control = data;
  104. pca954x_enable_channel(s, data);
  105. trace_pca954x_write_bytes(data);
  106. }
  107. static int pca954x_write_data(SMBusDevice *d, uint8_t *buf, uint8_t len)
  108. {
  109. Pca954xState *s = PCA954X(d);
  110. if (len == 0) {
  111. qemu_log_mask(LOG_GUEST_ERROR, "%s: writing empty data\n", __func__);
  112. return -1;
  113. }
  114. /*
  115. * len should be 1, because they write one byte to enable/disable channels.
  116. */
  117. if (len > 1) {
  118. qemu_log_mask(LOG_GUEST_ERROR,
  119. "%s: extra data after channel selection mask\n",
  120. __func__);
  121. return -1;
  122. }
  123. pca954x_write(s, buf[0]);
  124. return 0;
  125. }
  126. static uint8_t pca954x_read_byte(SMBusDevice *d)
  127. {
  128. Pca954xState *s = PCA954X(d);
  129. uint8_t data = s->control;
  130. trace_pca954x_read_data(data);
  131. return data;
  132. }
  133. static void pca954x_enter_reset(Object *obj, ResetType type)
  134. {
  135. Pca954xState *s = PCA954X(obj);
  136. /* Reset will disable all channels. */
  137. pca954x_write(s, 0);
  138. }
  139. I2CBus *pca954x_i2c_get_bus(I2CSlave *mux, uint8_t channel)
  140. {
  141. Pca954xClass *pc = PCA954X_GET_CLASS(mux);
  142. Pca954xState *pca954x = PCA954X(mux);
  143. g_assert(channel < pc->nchans);
  144. return pca954x->bus[channel];
  145. }
  146. static void pca9546_class_init(ObjectClass *klass, void *data)
  147. {
  148. Pca954xClass *s = PCA954X_CLASS(klass);
  149. s->nchans = PCA9546_CHANNEL_COUNT;
  150. }
  151. static void pca9548_class_init(ObjectClass *klass, void *data)
  152. {
  153. Pca954xClass *s = PCA954X_CLASS(klass);
  154. s->nchans = PCA9548_CHANNEL_COUNT;
  155. }
  156. static void pca954x_init(Object *obj)
  157. {
  158. Pca954xState *s = PCA954X(obj);
  159. Pca954xClass *c = PCA954X_GET_CLASS(obj);
  160. int i;
  161. /* SMBus modules. Cannot fail. */
  162. for (i = 0; i < c->nchans; i++) {
  163. g_autofree gchar *bus_name = g_strdup_printf("i2c.%d", i);
  164. /* start all channels as disabled. */
  165. s->enabled[i] = false;
  166. s->bus[i] = i2c_init_bus(DEVICE(s), bus_name);
  167. }
  168. }
  169. static void pca954x_class_init(ObjectClass *klass, void *data)
  170. {
  171. I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
  172. ResettableClass *rc = RESETTABLE_CLASS(klass);
  173. DeviceClass *dc = DEVICE_CLASS(klass);
  174. SMBusDeviceClass *k = SMBUS_DEVICE_CLASS(klass);
  175. sc->match_and_add = pca954x_match;
  176. rc->phases.enter = pca954x_enter_reset;
  177. dc->desc = "Pca954x i2c-mux";
  178. k->write_data = pca954x_write_data;
  179. k->receive_byte = pca954x_read_byte;
  180. }
  181. static const TypeInfo pca954x_info[] = {
  182. {
  183. .name = TYPE_PCA954X,
  184. .parent = TYPE_SMBUS_DEVICE,
  185. .instance_size = sizeof(Pca954xState),
  186. .instance_init = pca954x_init,
  187. .class_size = sizeof(Pca954xClass),
  188. .class_init = pca954x_class_init,
  189. .abstract = true,
  190. },
  191. {
  192. .name = TYPE_PCA9546,
  193. .parent = TYPE_PCA954X,
  194. .class_init = pca9546_class_init,
  195. },
  196. {
  197. .name = TYPE_PCA9548,
  198. .parent = TYPE_PCA954X,
  199. .class_init = pca9548_class_init,
  200. },
  201. };
  202. DEFINE_TYPES(pca954x_info)