spi-mux.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // SPDX-License-Identifier: GPL-2.0
  2. //
  3. // General Purpose SPI multiplexer
  4. #include <linux/err.h>
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/mux/consumer.h>
  8. #include <linux/slab.h>
  9. #include <linux/spi/spi.h>
  10. #define SPI_MUX_NO_CS ((unsigned int)-1)
  11. /**
  12. * DOC: Driver description
  13. *
  14. * This driver supports a MUX on an SPI bus. This can be useful when you need
  15. * more chip selects than the hardware peripherals support, or than are
  16. * available in a particular board setup.
  17. *
  18. * The driver will create an additional SPI controller. Devices added under the
  19. * mux will be handled as 'chip selects' on this controller.
  20. */
  21. /**
  22. * struct spi_mux_priv - the basic spi_mux structure
  23. * @spi: pointer to the device struct attached to the parent
  24. * spi controller
  25. * @current_cs: The current chip select set in the mux
  26. * @child_msg_complete: The mux replaces the complete callback in the child's
  27. * message to its own callback; this field is used by the
  28. * driver to store the child's callback during a transfer
  29. * @child_msg_context: Used to store the child's context to the callback
  30. * @child_msg_dev: Used to store the spi_device pointer to the child
  31. * @mux: mux_control structure used to provide chip selects for
  32. * downstream spi devices
  33. */
  34. struct spi_mux_priv {
  35. struct spi_device *spi;
  36. unsigned int current_cs;
  37. void (*child_msg_complete)(void *context);
  38. void *child_msg_context;
  39. struct spi_device *child_msg_dev;
  40. struct mux_control *mux;
  41. };
  42. /* should not get called when the parent controller is doing a transfer */
  43. static int spi_mux_select(struct spi_device *spi)
  44. {
  45. struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller);
  46. int ret;
  47. ret = mux_control_select(priv->mux, spi->chip_select);
  48. if (ret)
  49. return ret;
  50. if (priv->current_cs == spi->chip_select)
  51. return 0;
  52. dev_dbg(&priv->spi->dev, "setting up the mux for cs %d\n",
  53. spi->chip_select);
  54. /* copy the child device's settings except for the cs */
  55. priv->spi->max_speed_hz = spi->max_speed_hz;
  56. priv->spi->mode = spi->mode;
  57. priv->spi->bits_per_word = spi->bits_per_word;
  58. priv->current_cs = spi->chip_select;
  59. return 0;
  60. }
  61. static int spi_mux_setup(struct spi_device *spi)
  62. {
  63. struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller);
  64. /*
  65. * can be called multiple times, won't do a valid setup now but we will
  66. * change the settings when we do a transfer (necessary because we
  67. * can't predict from which device it will be anyway)
  68. */
  69. return spi_setup(priv->spi);
  70. }
  71. static void spi_mux_complete_cb(void *context)
  72. {
  73. struct spi_mux_priv *priv = (struct spi_mux_priv *)context;
  74. struct spi_controller *ctlr = spi_get_drvdata(priv->spi);
  75. struct spi_message *m = ctlr->cur_msg;
  76. m->complete = priv->child_msg_complete;
  77. m->context = priv->child_msg_context;
  78. m->spi = priv->child_msg_dev;
  79. spi_finalize_current_message(ctlr);
  80. mux_control_deselect(priv->mux);
  81. }
  82. static int spi_mux_transfer_one_message(struct spi_controller *ctlr,
  83. struct spi_message *m)
  84. {
  85. struct spi_mux_priv *priv = spi_controller_get_devdata(ctlr);
  86. struct spi_device *spi = m->spi;
  87. int ret;
  88. ret = spi_mux_select(spi);
  89. if (ret)
  90. return ret;
  91. /*
  92. * Replace the complete callback, context and spi_device with our own
  93. * pointers. Save originals
  94. */
  95. priv->child_msg_complete = m->complete;
  96. priv->child_msg_context = m->context;
  97. priv->child_msg_dev = m->spi;
  98. m->complete = spi_mux_complete_cb;
  99. m->context = priv;
  100. m->spi = priv->spi;
  101. /* do the transfer */
  102. return spi_async(priv->spi, m);
  103. }
  104. static int spi_mux_probe(struct spi_device *spi)
  105. {
  106. struct spi_controller *ctlr;
  107. struct spi_mux_priv *priv;
  108. int ret;
  109. ctlr = spi_alloc_master(&spi->dev, sizeof(*priv));
  110. if (!ctlr)
  111. return -ENOMEM;
  112. spi_set_drvdata(spi, ctlr);
  113. priv = spi_controller_get_devdata(ctlr);
  114. priv->spi = spi;
  115. /*
  116. * Increase lockdep class as these lock are taken while the parent bus
  117. * already holds their instance's lock.
  118. */
  119. lockdep_set_subclass(&ctlr->io_mutex, 1);
  120. lockdep_set_subclass(&ctlr->add_lock, 1);
  121. priv->mux = devm_mux_control_get(&spi->dev, NULL);
  122. if (IS_ERR(priv->mux)) {
  123. ret = dev_err_probe(&spi->dev, PTR_ERR(priv->mux),
  124. "failed to get control-mux\n");
  125. goto err_put_ctlr;
  126. }
  127. priv->current_cs = SPI_MUX_NO_CS;
  128. /* supported modes are the same as our parent's */
  129. ctlr->mode_bits = spi->controller->mode_bits;
  130. ctlr->flags = spi->controller->flags;
  131. ctlr->transfer_one_message = spi_mux_transfer_one_message;
  132. ctlr->setup = spi_mux_setup;
  133. ctlr->num_chipselect = mux_control_states(priv->mux);
  134. ctlr->bus_num = -1;
  135. ctlr->dev.of_node = spi->dev.of_node;
  136. ctlr->must_async = true;
  137. ret = devm_spi_register_controller(&spi->dev, ctlr);
  138. if (ret)
  139. goto err_put_ctlr;
  140. return 0;
  141. err_put_ctlr:
  142. spi_controller_put(ctlr);
  143. return ret;
  144. }
  145. static const struct spi_device_id spi_mux_id[] = {
  146. { "spi-mux" },
  147. { }
  148. };
  149. MODULE_DEVICE_TABLE(spi, spi_mux_id);
  150. static const struct of_device_id spi_mux_of_match[] = {
  151. { .compatible = "spi-mux" },
  152. { }
  153. };
  154. MODULE_DEVICE_TABLE(of, spi_mux_of_match);
  155. static struct spi_driver spi_mux_driver = {
  156. .probe = spi_mux_probe,
  157. .driver = {
  158. .name = "spi-mux",
  159. .of_match_table = spi_mux_of_match,
  160. },
  161. .id_table = spi_mux_id,
  162. };
  163. module_spi_driver(spi_mux_driver);
  164. MODULE_DESCRIPTION("SPI multiplexer");
  165. MODULE_LICENSE("GPL");