1
2
3
4
5#include <linux/err.h>
6#include <linux/kernel.h>
7#include <linux/module.h>
8#include <linux/mux/consumer.h>
9#include <linux/slab.h>
10#include <linux/spi/spi.h>
11
12#define SPI_MUX_NO_CS ((unsigned int)-1)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38struct spi_mux_priv {
39 struct spi_device *spi;
40 unsigned int current_cs;
41
42 void (*child_msg_complete)(void *context);
43 void *child_msg_context;
44 struct spi_device *child_msg_dev;
45 struct mux_control *mux;
46};
47
48
49static int spi_mux_select(struct spi_device *spi)
50{
51 struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller);
52 int ret;
53
54 ret = mux_control_select(priv->mux, spi->chip_select);
55 if (ret)
56 return ret;
57
58 if (priv->current_cs == spi->chip_select)
59 return 0;
60
61 dev_dbg(&priv->spi->dev, "setting up the mux for cs %d\n",
62 spi->chip_select);
63
64
65 priv->spi->max_speed_hz = spi->max_speed_hz;
66 priv->spi->mode = spi->mode;
67 priv->spi->bits_per_word = spi->bits_per_word;
68
69 priv->current_cs = spi->chip_select;
70
71 return 0;
72}
73
74static int spi_mux_setup(struct spi_device *spi)
75{
76 struct spi_mux_priv *priv = spi_controller_get_devdata(spi->controller);
77
78
79
80
81
82
83 return spi_setup(priv->spi);
84}
85
86static void spi_mux_complete_cb(void *context)
87{
88 struct spi_mux_priv *priv = (struct spi_mux_priv *)context;
89 struct spi_controller *ctlr = spi_get_drvdata(priv->spi);
90 struct spi_message *m = ctlr->cur_msg;
91
92 m->complete = priv->child_msg_complete;
93 m->context = priv->child_msg_context;
94 m->spi = priv->child_msg_dev;
95 spi_finalize_current_message(ctlr);
96 mux_control_deselect(priv->mux);
97}
98
99static int spi_mux_transfer_one_message(struct spi_controller *ctlr,
100 struct spi_message *m)
101{
102 struct spi_mux_priv *priv = spi_controller_get_devdata(ctlr);
103 struct spi_device *spi = m->spi;
104 int ret;
105
106 ret = spi_mux_select(spi);
107 if (ret)
108 return ret;
109
110
111
112
113
114 priv->child_msg_complete = m->complete;
115 priv->child_msg_context = m->context;
116 priv->child_msg_dev = m->spi;
117
118 m->complete = spi_mux_complete_cb;
119 m->context = priv;
120 m->spi = priv->spi;
121
122
123 return spi_async(priv->spi, m);
124}
125
126static int spi_mux_probe(struct spi_device *spi)
127{
128 struct spi_controller *ctlr;
129 struct spi_mux_priv *priv;
130 int ret;
131
132 ctlr = spi_alloc_master(&spi->dev, sizeof(*priv));
133 if (!ctlr)
134 return -ENOMEM;
135
136 spi_set_drvdata(spi, ctlr);
137 priv = spi_controller_get_devdata(ctlr);
138 priv->spi = spi;
139
140
141
142
143
144 lockdep_set_subclass(&ctlr->io_mutex, 1);
145 lockdep_set_subclass(&ctlr->add_lock, 1);
146
147 priv->mux = devm_mux_control_get(&spi->dev, NULL);
148 if (IS_ERR(priv->mux)) {
149 ret = dev_err_probe(&spi->dev, PTR_ERR(priv->mux),
150 "failed to get control-mux\n");
151 goto err_put_ctlr;
152 }
153
154 priv->current_cs = SPI_MUX_NO_CS;
155
156
157 ctlr->mode_bits = spi->controller->mode_bits;
158 ctlr->flags = spi->controller->flags;
159 ctlr->transfer_one_message = spi_mux_transfer_one_message;
160 ctlr->setup = spi_mux_setup;
161 ctlr->num_chipselect = mux_control_states(priv->mux);
162 ctlr->bus_num = -1;
163 ctlr->dev.of_node = spi->dev.of_node;
164
165 ret = devm_spi_register_controller(&spi->dev, ctlr);
166 if (ret)
167 goto err_put_ctlr;
168
169 return 0;
170
171err_put_ctlr:
172 spi_controller_put(ctlr);
173
174 return ret;
175}
176
177static const struct spi_device_id spi_mux_id[] = {
178 { "spi-mux" },
179 { }
180};
181MODULE_DEVICE_TABLE(spi, spi_mux_id);
182
183static const struct of_device_id spi_mux_of_match[] = {
184 { .compatible = "spi-mux" },
185 { }
186};
187MODULE_DEVICE_TABLE(of, spi_mux_of_match);
188
189static struct spi_driver spi_mux_driver = {
190 .probe = spi_mux_probe,
191 .driver = {
192 .name = "spi-mux",
193 .of_match_table = spi_mux_of_match,
194 },
195 .id_table = spi_mux_id,
196};
197
198module_spi_driver(spi_mux_driver);
199
200MODULE_DESCRIPTION("SPI multiplexer");
201MODULE_LICENSE("GPL");
202