2 * SPI bridge driver for the Greybus "generic" SPI module.
4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/bitops.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
19 struct gb_connection *connection;
21 /* Modes supported by spi controller */
23 /* constraints of the spi controller */
29 * A mask indicating which values of bits_per_word are supported by the
30 * controller. Bit n indicates that a bits_per_word n+1 is suported. If
31 * set, the SPI core will reject any transfer with an unsupported
32 * bits_per_word. If not set, this value is simply ignored, and it's up
33 * to the individual driver to perform any validation.
35 u32 bits_per_word_mask;
38 * chipselects will be integral to many controllers; some others might
39 * use board-specific GPIOs.
44 /* Routines to transfer data */
45 static struct gb_operation *
46 gb_spi_operation_create(struct gb_connection *connection,
47 struct spi_message *msg, u32 *total_len)
49 struct gb_spi_transfer_request *request;
50 struct spi_device *dev = msg->spi;
51 struct spi_transfer *xfer;
52 struct gb_spi_transfer *gb_xfer;
53 struct gb_operation *operation;
54 u32 tx_size = 0, rx_size = 0, count = 0, request_size;
57 /* Find number of transfers queued and tx/rx length in the message */
58 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
59 if (!xfer->tx_buf && !xfer->rx_buf) {
60 dev_err(&connection->bundle->dev,
61 "bufferless transfer, length %u\n", xfer->len);
70 *total_len += xfer->len;
74 /* Too many transfers ? */
75 if (count > (u32)U16_MAX) {
76 dev_err(&connection->bundle->dev,
77 "transfer count (%u) too big\n", count);
82 * In addition to space for all message descriptors we need
83 * to have enough to hold all tx data.
85 request_size = sizeof(*request);
86 request_size += count * sizeof(*gb_xfer);
87 request_size += tx_size;
89 /* Response consists only of incoming data */
90 operation = gb_operation_create(connection, GB_SPI_TYPE_TRANSFER,
91 request_size, rx_size, GFP_KERNEL);
95 request = operation->request->payload;
96 request->count = cpu_to_le16(count);
97 request->mode = dev->mode;
98 request->chip_select = dev->chip_select;
100 gb_xfer = &request->transfers[0];
101 tx_data = gb_xfer + count; /* place tx data after last gb_xfer */
103 /* Fill in the transfers array */
104 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
105 gb_xfer->speed_hz = cpu_to_le32(xfer->speed_hz);
106 gb_xfer->len = cpu_to_le32(xfer->len);
107 gb_xfer->delay_usecs = cpu_to_le16(xfer->delay_usecs);
108 gb_xfer->cs_change = xfer->cs_change;
109 gb_xfer->bits_per_word = xfer->bits_per_word;
114 memcpy(tx_data, xfer->tx_buf, xfer->len);
115 tx_data += xfer->len;
122 static void gb_spi_decode_response(struct spi_message *msg,
123 struct gb_spi_transfer_response *response)
125 struct spi_transfer *xfer;
126 void *rx_data = response->data;
128 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
131 memcpy(xfer->rx_buf, rx_data, xfer->len);
132 rx_data += xfer->len;
137 static int gb_spi_transfer_one_message(struct spi_master *master,
138 struct spi_message *msg)
140 struct gb_spi *spi = spi_master_get_devdata(master);
141 struct gb_connection *connection = spi->connection;
142 struct gb_spi_transfer_response *response;
143 struct gb_operation *operation;
147 operation = gb_spi_operation_create(connection, msg, &len);
151 ret = gb_operation_request_send_sync(operation);
153 response = operation->response->payload;
155 gb_spi_decode_response(msg, response);
157 pr_err("transfer operation failed (%d)\n", ret);
160 gb_operation_put(operation);
162 msg->actual_length = len;
164 spi_finalize_current_message(master);
169 static int gb_spi_setup(struct spi_device *spi)
171 /* Nothing to do for now */
175 static void gb_spi_cleanup(struct spi_device *spi)
177 /* Nothing to do for now */
181 /* Routines to get controller infomation */
184 * Map Greybus spi mode bits/flags/bpw into Linux ones.
185 * All bits are same for now and so these macro's return same values.
187 #define gb_spi_mode_map(mode) mode
188 #define gb_spi_flags_map(flags) flags
190 static int gb_spi_mode_operation(struct gb_spi *spi)
192 struct gb_spi_mode_response response;
196 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_MODE,
197 NULL, 0, &response, sizeof(response));
201 mode = le16_to_cpu(response.mode);
202 spi->mode = gb_spi_mode_map(mode);
207 static int gb_spi_flags_operation(struct gb_spi *spi)
209 struct gb_spi_flags_response response;
213 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_FLAGS,
214 NULL, 0, &response, sizeof(response));
218 flags = le16_to_cpu(response.flags);
219 spi->flags = gb_spi_flags_map(flags);
224 static int gb_spi_bpw_operation(struct gb_spi *spi)
226 struct gb_spi_bpw_response response;
229 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_BITS_PER_WORD_MASK,
230 NULL, 0, &response, sizeof(response));
234 spi->bits_per_word_mask = le32_to_cpu(response.bits_per_word_mask);
239 static int gb_spi_chipselect_operation(struct gb_spi *spi)
241 struct gb_spi_chipselect_response response;
244 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_NUM_CHIPSELECT,
245 NULL, 0, &response, sizeof(response));
249 spi->num_chipselect = le16_to_cpu(response.num_chipselect);
255 * Initialize the spi device. This includes verifying we can support it (based
256 * on the protocol version it advertises). If that's OK, we get and cached its
259 static int gb_spi_init(struct gb_spi *spi)
263 /* mode never changes, just get it once */
264 ret = gb_spi_mode_operation(spi);
268 /* flags never changes, just get it once */
269 ret = gb_spi_flags_operation(spi);
273 /* total number of chipselects never changes, just get it once */
274 ret = gb_spi_chipselect_operation(spi);
278 /* bits-per-word-mask never changes, just get it once */
279 return gb_spi_bpw_operation(spi);
282 static int gb_spi_connection_init(struct gb_connection *connection)
285 struct spi_master *master;
288 /* Allocate master with space for data */
289 master = spi_alloc_master(&connection->bundle->dev, sizeof(*spi));
291 dev_err(&connection->bundle->dev, "cannot alloc SPI master\n");
295 spi = spi_master_get_devdata(master);
296 spi->connection = connection;
297 connection->private = master;
299 ret = gb_spi_init(spi);
303 master->bus_num = -1; /* Allow spi-core to allocate it dynamically */
304 master->num_chipselect = spi->num_chipselect;
305 master->mode_bits = spi->mode;
306 master->flags = spi->flags;
307 master->bits_per_word_mask = spi->bits_per_word_mask;
310 master->cleanup = gb_spi_cleanup;
311 master->setup = gb_spi_setup;
312 master->transfer_one_message = gb_spi_transfer_one_message;
314 ret = spi_register_master(master);
319 spi_master_put(master);
324 static void gb_spi_connection_exit(struct gb_connection *connection)
326 struct spi_master *master = connection->private;
328 spi_unregister_master(master);
331 static struct gb_protocol spi_protocol = {
333 .id = GREYBUS_PROTOCOL_SPI,
334 .major = GB_SPI_VERSION_MAJOR,
335 .minor = GB_SPI_VERSION_MINOR,
336 .connection_init = gb_spi_connection_init,
337 .connection_exit = gb_spi_connection_exit,
338 .request_recv = NULL,
341 gb_builtin_protocol_driver(spi_protocol);