]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/greybus/spi.c
greybus: spi: use the bundle struct device instead of the connector
[karo-tx-linux.git] / drivers / staging / greybus / spi.c
1 /*
2  * SPI bridge driver for the Greybus "generic" SPI module.
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
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>
15
16 #include "greybus.h"
17
18 struct gb_spi {
19         struct gb_connection    *connection;
20
21         /* Modes supported by spi controller */
22         u16                     mode;
23         /* constraints of the spi controller */
24         u16                     flags;
25
26         /*
27          * copied from kernel:
28          *
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.
34          */
35         u32                     bits_per_word_mask;
36
37         /*
38          * chipselects will be integral to many controllers; some others might
39          * use board-specific GPIOs.
40          */
41         u16                     num_chipselect;
42 };
43
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)
48 {
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;
55         void *tx_data;
56
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);
62                         return NULL;
63                 }
64
65                 if (xfer->tx_buf)
66                         tx_size += xfer->len;
67                 if (xfer->rx_buf)
68                         rx_size += xfer->len;
69
70                 *total_len += xfer->len;
71                 count++;
72         }
73
74         /* Too many transfers ? */
75         if (count > (u32)U16_MAX) {
76                 dev_err(&connection->bundle->dev,
77                         "transfer count (%u) too big\n", count);
78                 return NULL;
79         }
80
81         /*
82          * In addition to space for all message descriptors we need
83          * to have enough to hold all tx data.
84          */
85         request_size = sizeof(*request);
86         request_size += count * sizeof(*gb_xfer);
87         request_size += tx_size;
88
89         /* Response consists only of incoming data */
90         operation = gb_operation_create(connection, GB_SPI_TYPE_TRANSFER,
91                                         request_size, rx_size, GFP_KERNEL);
92         if (!operation)
93                 return NULL;
94
95         request = operation->request->payload;
96         request->count = cpu_to_le16(count);
97         request->mode = dev->mode;
98         request->chip_select = dev->chip_select;
99
100         gb_xfer = &request->transfers[0];
101         tx_data = gb_xfer + count;      /* place tx data after last gb_xfer */
102
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;
110                 gb_xfer++;
111
112                 /* Copy tx data */
113                 if (xfer->tx_buf) {
114                         memcpy(tx_data, xfer->tx_buf, xfer->len);
115                         tx_data += xfer->len;
116                 }
117         }
118
119         return operation;
120 }
121
122 static void gb_spi_decode_response(struct spi_message *msg,
123                                    struct gb_spi_transfer_response *response)
124 {
125         struct spi_transfer *xfer;
126         void *rx_data = response->data;
127
128         list_for_each_entry(xfer, &msg->transfers, transfer_list) {
129                 /* Copy rx data */
130                 if (xfer->rx_buf) {
131                         memcpy(xfer->rx_buf, rx_data, xfer->len);
132                         rx_data += xfer->len;
133                 }
134         }
135 }
136
137 static int gb_spi_transfer_one_message(struct spi_master *master,
138                                        struct spi_message *msg)
139 {
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;
144         u32 len = 0;
145         int ret;
146
147         operation = gb_spi_operation_create(connection, msg, &len);
148         if (!operation)
149                 return -ENOMEM;
150
151         ret = gb_operation_request_send_sync(operation);
152         if (!ret) {
153                 response = operation->response->payload;
154                 if (response)
155                         gb_spi_decode_response(msg, response);
156         } else {
157                 pr_err("transfer operation failed (%d)\n", ret);
158         }
159
160         gb_operation_put(operation);
161
162         msg->actual_length = len;
163         msg->status = 0;
164         spi_finalize_current_message(master);
165
166         return ret;
167 }
168
169 static int gb_spi_setup(struct spi_device *spi)
170 {
171         /* Nothing to do for now */
172         return 0;
173 }
174
175 static void gb_spi_cleanup(struct spi_device *spi)
176 {
177         /* Nothing to do for now */
178 }
179
180
181 /* Routines to get controller infomation */
182
183 /*
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.
186  */
187 #define gb_spi_mode_map(mode) mode
188 #define gb_spi_flags_map(flags) flags
189
190 static int gb_spi_mode_operation(struct gb_spi *spi)
191 {
192         struct gb_spi_mode_response response;
193         u16 mode;
194         int ret;
195
196         ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_MODE,
197                                 NULL, 0, &response, sizeof(response));
198         if (ret)
199                 return ret;
200
201         mode = le16_to_cpu(response.mode);
202         spi->mode = gb_spi_mode_map(mode);
203
204         return 0;
205 }
206
207 static int gb_spi_flags_operation(struct gb_spi *spi)
208 {
209         struct gb_spi_flags_response response;
210         u16 flags;
211         int ret;
212
213         ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_FLAGS,
214                                 NULL, 0, &response, sizeof(response));
215         if (ret)
216                 return ret;
217
218         flags = le16_to_cpu(response.flags);
219         spi->flags = gb_spi_flags_map(flags);
220
221         return 0;
222 }
223
224 static int gb_spi_bpw_operation(struct gb_spi *spi)
225 {
226         struct gb_spi_bpw_response response;
227         int ret;
228
229         ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_BITS_PER_WORD_MASK,
230                                 NULL, 0, &response, sizeof(response));
231         if (ret)
232                 return ret;
233
234         spi->bits_per_word_mask = le32_to_cpu(response.bits_per_word_mask);
235
236         return 0;
237 }
238
239 static int gb_spi_chipselect_operation(struct gb_spi *spi)
240 {
241         struct gb_spi_chipselect_response response;
242         int ret;
243
244         ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_NUM_CHIPSELECT,
245                                 NULL, 0, &response, sizeof(response));
246         if (ret)
247                 return ret;
248
249         spi->num_chipselect = le16_to_cpu(response.num_chipselect);
250
251         return 0;
252 }
253
254 /*
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
257  * mode bits & flags.
258  */
259 static int gb_spi_init(struct gb_spi *spi)
260 {
261         int ret;
262
263         /* mode never changes, just get it once */
264         ret = gb_spi_mode_operation(spi);
265         if (ret)
266                 return ret;
267
268         /* flags never changes, just get it once */
269         ret = gb_spi_flags_operation(spi);
270         if (ret)
271                 return ret;
272
273         /* total number of chipselects never changes, just get it once */
274         ret = gb_spi_chipselect_operation(spi);
275         if (ret)
276                 return ret;
277
278         /* bits-per-word-mask never changes, just get it once */
279         return gb_spi_bpw_operation(spi);
280 }
281
282 static int gb_spi_connection_init(struct gb_connection *connection)
283 {
284         struct gb_spi *spi;
285         struct spi_master *master;
286         int ret;
287
288         /* Allocate master with space for data */
289         master = spi_alloc_master(&connection->bundle->dev, sizeof(*spi));
290         if (!master) {
291                 dev_err(&connection->bundle->dev, "cannot alloc SPI master\n");
292                 return -ENOMEM;
293         }
294
295         spi = spi_master_get_devdata(master);
296         spi->connection = connection;
297         connection->private = master;
298
299         ret = gb_spi_init(spi);
300         if (ret)
301                 goto out_err;
302
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;
308
309         /* Attach methods */
310         master->cleanup = gb_spi_cleanup;
311         master->setup = gb_spi_setup;
312         master->transfer_one_message = gb_spi_transfer_one_message;
313
314         ret = spi_register_master(master);
315         if (!ret)
316                 return 0;
317
318 out_err:
319         spi_master_put(master);
320
321         return ret;
322 }
323
324 static void gb_spi_connection_exit(struct gb_connection *connection)
325 {
326         struct spi_master *master = connection->private;
327
328         spi_unregister_master(master);
329 }
330
331 static struct gb_protocol spi_protocol = {
332         .name                   = "spi",
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,
339 };
340
341 gb_builtin_protocol_driver(spi_protocol);