Lines Matching +full:- +full:spi
1 // SPDX-License-Identifier: GPL-2.0
3 // General Purpose SPI multiplexer
10 #include <linux/spi/spi.h>
12 #define SPI_MUX_NO_CS ((unsigned int)-1)
17 * This driver supports a MUX on an SPI bus. This can be useful when you need
21 * The driver will create an additional SPI controller. Devices added under the
26 * struct spi_mux_priv - the basic spi_mux structure
27 * @spi: pointer to the device struct attached to the parent
28 * spi controller
36 * downstream spi devices
39 struct spi_device *spi; member
49 static int spi_mux_select(struct spi_device *spi) in spi_mux_select() argument
51 struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller); in spi_mux_select()
54 ret = mux_control_select(priv->mux, spi_get_chipselect(spi, 0)); in spi_mux_select()
58 if (priv->current_cs == spi_get_chipselect(spi, 0)) in spi_mux_select()
61 dev_dbg(&priv->spi->dev, "setting up the mux for cs %d\n", in spi_mux_select()
62 spi_get_chipselect(spi, 0)); in spi_mux_select()
65 priv->spi->max_speed_hz = spi->max_speed_hz; in spi_mux_select()
66 priv->spi->mode = spi->mode; in spi_mux_select()
67 priv->spi->bits_per_word = spi->bits_per_word; in spi_mux_select()
69 priv->current_cs = spi_get_chipselect(spi, 0); in spi_mux_select()
71 spi_setup(priv->spi); in spi_mux_select()
76 static int spi_mux_setup(struct spi_device *spi) in spi_mux_setup() argument
78 struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller); in spi_mux_setup()
85 return spi_setup(priv->spi); in spi_mux_setup()
91 struct spi_controller *ctlr = spi_get_drvdata(priv->spi); in spi_mux_complete_cb()
92 struct spi_message *m = ctlr->cur_msg; in spi_mux_complete_cb()
94 m->complete = priv->child_msg_complete; in spi_mux_complete_cb()
95 m->context = priv->child_msg_context; in spi_mux_complete_cb()
96 m->spi = priv->child_msg_dev; in spi_mux_complete_cb()
98 mux_control_deselect(priv->mux); in spi_mux_complete_cb()
105 struct spi_device *spi = m->spi; in spi_mux_transfer_one_message() local
108 ret = spi_mux_select(spi); in spi_mux_transfer_one_message()
116 priv->child_msg_complete = m->complete; in spi_mux_transfer_one_message()
117 priv->child_msg_context = m->context; in spi_mux_transfer_one_message()
118 priv->child_msg_dev = m->spi; in spi_mux_transfer_one_message()
120 m->complete = spi_mux_complete_cb; in spi_mux_transfer_one_message()
121 m->context = priv; in spi_mux_transfer_one_message()
122 m->spi = priv->spi; in spi_mux_transfer_one_message()
125 return spi_async(priv->spi, m); in spi_mux_transfer_one_message()
128 static int spi_mux_probe(struct spi_device *spi) in spi_mux_probe() argument
134 ctlr = spi_alloc_host(&spi->dev, sizeof(*priv)); in spi_mux_probe()
136 return -ENOMEM; in spi_mux_probe()
138 spi_set_drvdata(spi, ctlr); in spi_mux_probe()
140 priv->spi = spi; in spi_mux_probe()
146 lockdep_set_subclass(&ctlr->io_mutex, 1); in spi_mux_probe()
147 lockdep_set_subclass(&ctlr->add_lock, 1); in spi_mux_probe()
149 priv->mux = devm_mux_control_get(&spi->dev, NULL); in spi_mux_probe()
150 if (IS_ERR(priv->mux)) { in spi_mux_probe()
151 ret = dev_err_probe(&spi->dev, PTR_ERR(priv->mux), in spi_mux_probe()
152 "failed to get control-mux\n"); in spi_mux_probe()
156 priv->current_cs = SPI_MUX_NO_CS; in spi_mux_probe()
159 ctlr->mode_bits = spi->controller->mode_bits; in spi_mux_probe()
160 ctlr->flags = spi->controller->flags; in spi_mux_probe()
161 ctlr->bits_per_word_mask = spi->controller->bits_per_word_mask; in spi_mux_probe()
162 ctlr->transfer_one_message = spi_mux_transfer_one_message; in spi_mux_probe()
163 ctlr->setup = spi_mux_setup; in spi_mux_probe()
164 ctlr->num_chipselect = mux_control_states(priv->mux); in spi_mux_probe()
165 ctlr->bus_num = -1; in spi_mux_probe()
166 ctlr->dev.of_node = spi->dev.of_node; in spi_mux_probe()
167 ctlr->must_async = true; in spi_mux_probe()
168 ctlr->defer_optimize_message = true; in spi_mux_probe()
170 ret = devm_spi_register_controller(&spi->dev, ctlr); in spi_mux_probe()
183 { "spi-mux" },
186 MODULE_DEVICE_TABLE(spi, spi_mux_id);
189 { .compatible = "spi-mux" },
197 .name = "spi-mux",
205 MODULE_DESCRIPTION("SPI multiplexer");