0001
0002
0003
0004
0005 #include <linux/err.h>
0006 #include <linux/kernel.h>
0007 #include <linux/module.h>
0008 #include <linux/mux/consumer.h>
0009 #include <linux/slab.h>
0010 #include <linux/spi/spi.h>
0011
0012 #define SPI_MUX_NO_CS ((unsigned int)-1)
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038 struct spi_mux_priv {
0039 struct spi_device *spi;
0040 unsigned int current_cs;
0041
0042 void (*child_msg_complete)(void *context);
0043 void *child_msg_context;
0044 struct spi_device *child_msg_dev;
0045 struct mux_control *mux;
0046 };
0047
0048
0049 static int spi_mux_select(struct spi_device *spi)
0050 {
0051 struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller);
0052 int ret;
0053
0054 ret = mux_control_select(priv->mux, spi->chip_select);
0055 if (ret)
0056 return ret;
0057
0058 if (priv->current_cs == spi->chip_select)
0059 return 0;
0060
0061 dev_dbg(&priv->spi->dev, "setting up the mux for cs %d\n",
0062 spi->chip_select);
0063
0064
0065 priv->spi->max_speed_hz = spi->max_speed_hz;
0066 priv->spi->mode = spi->mode;
0067 priv->spi->bits_per_word = spi->bits_per_word;
0068
0069 priv->current_cs = spi->chip_select;
0070
0071 return 0;
0072 }
0073
0074 static int spi_mux_setup(struct spi_device *spi)
0075 {
0076 struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller);
0077
0078
0079
0080
0081
0082
0083 return spi_setup(priv->spi);
0084 }
0085
0086 static void spi_mux_complete_cb(void *context)
0087 {
0088 struct spi_mux_priv *priv = (struct spi_mux_priv *)context;
0089 struct spi_controller *ctlr = spi_get_drvdata(priv->spi);
0090 struct spi_message *m = ctlr->cur_msg;
0091
0092 m->complete = priv->child_msg_complete;
0093 m->context = priv->child_msg_context;
0094 m->spi = priv->child_msg_dev;
0095 spi_finalize_current_message(ctlr);
0096 mux_control_deselect(priv->mux);
0097 }
0098
0099 static int spi_mux_transfer_one_message(struct spi_controller *ctlr,
0100 struct spi_message *m)
0101 {
0102 struct spi_mux_priv *priv = spi_controller_get_devdata(ctlr);
0103 struct spi_device *spi = m->spi;
0104 int ret;
0105
0106 ret = spi_mux_select(spi);
0107 if (ret)
0108 return ret;
0109
0110
0111
0112
0113
0114 priv->child_msg_complete = m->complete;
0115 priv->child_msg_context = m->context;
0116 priv->child_msg_dev = m->spi;
0117
0118 m->complete = spi_mux_complete_cb;
0119 m->context = priv;
0120 m->spi = priv->spi;
0121
0122
0123 return spi_async(priv->spi, m);
0124 }
0125
0126 static int spi_mux_probe(struct spi_device *spi)
0127 {
0128 struct spi_controller *ctlr;
0129 struct spi_mux_priv *priv;
0130 int ret;
0131
0132 ctlr = spi_alloc_master(&spi->dev, sizeof(*priv));
0133 if (!ctlr)
0134 return -ENOMEM;
0135
0136 spi_set_drvdata(spi, ctlr);
0137 priv = spi_controller_get_devdata(ctlr);
0138 priv->spi = spi;
0139
0140
0141
0142
0143
0144 lockdep_set_subclass(&ctlr->io_mutex, 1);
0145 lockdep_set_subclass(&ctlr->add_lock, 1);
0146
0147 priv->mux = devm_mux_control_get(&spi->dev, NULL);
0148 if (IS_ERR(priv->mux)) {
0149 ret = dev_err_probe(&spi->dev, PTR_ERR(priv->mux),
0150 "failed to get control-mux\n");
0151 goto err_put_ctlr;
0152 }
0153
0154 priv->current_cs = SPI_MUX_NO_CS;
0155
0156
0157 ctlr->mode_bits = spi->controller->mode_bits;
0158 ctlr->flags = spi->controller->flags;
0159 ctlr->transfer_one_message = spi_mux_transfer_one_message;
0160 ctlr->setup = spi_mux_setup;
0161 ctlr->num_chipselect = mux_control_states(priv->mux);
0162 ctlr->bus_num = -1;
0163 ctlr->dev.of_node = spi->dev.of_node;
0164 ctlr->must_async = true;
0165
0166 ret = devm_spi_register_controller(&spi->dev, ctlr);
0167 if (ret)
0168 goto err_put_ctlr;
0169
0170 return 0;
0171
0172 err_put_ctlr:
0173 spi_controller_put(ctlr);
0174
0175 return ret;
0176 }
0177
0178 static const struct spi_device_id spi_mux_id[] = {
0179 { "spi-mux" },
0180 { }
0181 };
0182 MODULE_DEVICE_TABLE(spi, spi_mux_id);
0183
0184 static const struct of_device_id spi_mux_of_match[] = {
0185 { .compatible = "spi-mux" },
0186 { }
0187 };
0188 MODULE_DEVICE_TABLE(of, spi_mux_of_match);
0189
0190 static struct spi_driver spi_mux_driver = {
0191 .probe = spi_mux_probe,
0192 .driver = {
0193 .name = "spi-mux",
0194 .of_match_table = spi_mux_of_match,
0195 },
0196 .id_table = spi_mux_id,
0197 };
0198
0199 module_spi_driver(spi_mux_driver);
0200
0201 MODULE_DESCRIPTION("SPI multiplexer");
0202 MODULE_LICENSE("GPL");