treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / staging / greybus / spilib.c
blobfc27c52de74a2db2867657cdf37ace42f4b2dfbe
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Greybus SPI library
5 * Copyright 2014-2016 Google Inc.
6 * Copyright 2014-2016 Linaro Ltd.
7 */
9 #include <linux/bitops.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/greybus.h>
14 #include <linux/spi/spi.h>
16 #include "spilib.h"
18 struct gb_spilib {
19 struct gb_connection *connection;
20 struct device *parent;
21 struct spi_transfer *first_xfer;
22 struct spi_transfer *last_xfer;
23 struct spilib_ops *ops;
24 u32 rx_xfer_offset;
25 u32 tx_xfer_offset;
26 u32 last_xfer_size;
27 unsigned int op_timeout;
28 u16 mode;
29 u16 flags;
30 u32 bits_per_word_mask;
31 u8 num_chipselect;
32 u32 min_speed_hz;
33 u32 max_speed_hz;
36 #define GB_SPI_STATE_MSG_DONE ((void *)0)
37 #define GB_SPI_STATE_MSG_IDLE ((void *)1)
38 #define GB_SPI_STATE_MSG_RUNNING ((void *)2)
39 #define GB_SPI_STATE_OP_READY ((void *)3)
40 #define GB_SPI_STATE_OP_DONE ((void *)4)
41 #define GB_SPI_STATE_MSG_ERROR ((void *)-1)
43 #define XFER_TIMEOUT_TOLERANCE 200
45 static struct spi_master *get_master_from_spi(struct gb_spilib *spi)
47 return gb_connection_get_data(spi->connection);
50 static int tx_header_fit_operation(u32 tx_size, u32 count, size_t data_max)
52 size_t headers_size;
54 data_max -= sizeof(struct gb_spi_transfer_request);
55 headers_size = (count + 1) * sizeof(struct gb_spi_transfer);
57 return tx_size + headers_size > data_max ? 0 : 1;
60 static size_t calc_rx_xfer_size(u32 rx_size, u32 *tx_xfer_size, u32 len,
61 size_t data_max)
63 size_t rx_xfer_size;
65 data_max -= sizeof(struct gb_spi_transfer_response);
67 if (rx_size + len > data_max)
68 rx_xfer_size = data_max - rx_size;
69 else
70 rx_xfer_size = len;
72 /* if this is a write_read, for symmetry read the same as write */
73 if (*tx_xfer_size && rx_xfer_size > *tx_xfer_size)
74 rx_xfer_size = *tx_xfer_size;
75 if (*tx_xfer_size && rx_xfer_size < *tx_xfer_size)
76 *tx_xfer_size = rx_xfer_size;
78 return rx_xfer_size;
81 static size_t calc_tx_xfer_size(u32 tx_size, u32 count, size_t len,
82 size_t data_max)
84 size_t headers_size;
86 data_max -= sizeof(struct gb_spi_transfer_request);
87 headers_size = (count + 1) * sizeof(struct gb_spi_transfer);
89 if (tx_size + headers_size + len > data_max)
90 return data_max - (tx_size + sizeof(struct gb_spi_transfer));
92 return len;
95 static void clean_xfer_state(struct gb_spilib *spi)
97 spi->first_xfer = NULL;
98 spi->last_xfer = NULL;
99 spi->rx_xfer_offset = 0;
100 spi->tx_xfer_offset = 0;
101 spi->last_xfer_size = 0;
102 spi->op_timeout = 0;
105 static bool is_last_xfer_done(struct gb_spilib *spi)
107 struct spi_transfer *last_xfer = spi->last_xfer;
109 if ((spi->tx_xfer_offset + spi->last_xfer_size == last_xfer->len) ||
110 (spi->rx_xfer_offset + spi->last_xfer_size == last_xfer->len))
111 return true;
113 return false;
116 static int setup_next_xfer(struct gb_spilib *spi, struct spi_message *msg)
118 struct spi_transfer *last_xfer = spi->last_xfer;
120 if (msg->state != GB_SPI_STATE_OP_DONE)
121 return 0;
124 * if we transferred all content of the last transfer, reset values and
125 * check if this was the last transfer in the message
127 if (is_last_xfer_done(spi)) {
128 spi->tx_xfer_offset = 0;
129 spi->rx_xfer_offset = 0;
130 spi->op_timeout = 0;
131 if (last_xfer == list_last_entry(&msg->transfers,
132 struct spi_transfer,
133 transfer_list))
134 msg->state = GB_SPI_STATE_MSG_DONE;
135 else
136 spi->first_xfer = list_next_entry(last_xfer,
137 transfer_list);
138 return 0;
141 spi->first_xfer = last_xfer;
142 if (last_xfer->tx_buf)
143 spi->tx_xfer_offset += spi->last_xfer_size;
145 if (last_xfer->rx_buf)
146 spi->rx_xfer_offset += spi->last_xfer_size;
148 return 0;
151 static struct spi_transfer *get_next_xfer(struct spi_transfer *xfer,
152 struct spi_message *msg)
154 if (xfer == list_last_entry(&msg->transfers, struct spi_transfer,
155 transfer_list))
156 return NULL;
158 return list_next_entry(xfer, transfer_list);
161 /* Routines to transfer data */
162 static struct gb_operation *gb_spi_operation_create(struct gb_spilib *spi,
163 struct gb_connection *connection, struct spi_message *msg)
165 struct gb_spi_transfer_request *request;
166 struct spi_device *dev = msg->spi;
167 struct spi_transfer *xfer;
168 struct gb_spi_transfer *gb_xfer;
169 struct gb_operation *operation;
170 u32 tx_size = 0, rx_size = 0, count = 0, xfer_len = 0, request_size;
171 u32 tx_xfer_size = 0, rx_xfer_size = 0, len;
172 u32 total_len = 0;
173 unsigned int xfer_timeout;
174 size_t data_max;
175 void *tx_data;
177 data_max = gb_operation_get_payload_size_max(connection);
178 xfer = spi->first_xfer;
180 /* Find number of transfers queued and tx/rx length in the message */
182 while (msg->state != GB_SPI_STATE_OP_READY) {
183 msg->state = GB_SPI_STATE_MSG_RUNNING;
184 spi->last_xfer = xfer;
186 if (!xfer->tx_buf && !xfer->rx_buf) {
187 dev_err(spi->parent,
188 "bufferless transfer, length %u\n", xfer->len);
189 msg->state = GB_SPI_STATE_MSG_ERROR;
190 return NULL;
193 tx_xfer_size = 0;
194 rx_xfer_size = 0;
196 if (xfer->tx_buf) {
197 len = xfer->len - spi->tx_xfer_offset;
198 if (!tx_header_fit_operation(tx_size, count, data_max))
199 break;
200 tx_xfer_size = calc_tx_xfer_size(tx_size, count,
201 len, data_max);
202 spi->last_xfer_size = tx_xfer_size;
205 if (xfer->rx_buf) {
206 len = xfer->len - spi->rx_xfer_offset;
207 rx_xfer_size = calc_rx_xfer_size(rx_size, &tx_xfer_size,
208 len, data_max);
209 spi->last_xfer_size = rx_xfer_size;
212 tx_size += tx_xfer_size;
213 rx_size += rx_xfer_size;
215 total_len += spi->last_xfer_size;
216 count++;
218 xfer = get_next_xfer(xfer, msg);
219 if (!xfer || total_len >= data_max)
220 msg->state = GB_SPI_STATE_OP_READY;
224 * In addition to space for all message descriptors we need
225 * to have enough to hold all tx data.
227 request_size = sizeof(*request);
228 request_size += count * sizeof(*gb_xfer);
229 request_size += tx_size;
231 /* Response consists only of incoming data */
232 operation = gb_operation_create(connection, GB_SPI_TYPE_TRANSFER,
233 request_size, rx_size, GFP_KERNEL);
234 if (!operation)
235 return NULL;
237 request = operation->request->payload;
238 request->count = cpu_to_le16(count);
239 request->mode = dev->mode;
240 request->chip_select = dev->chip_select;
242 gb_xfer = &request->transfers[0];
243 tx_data = gb_xfer + count; /* place tx data after last gb_xfer */
245 /* Fill in the transfers array */
246 xfer = spi->first_xfer;
247 while (msg->state != GB_SPI_STATE_OP_DONE) {
248 if (xfer == spi->last_xfer)
249 xfer_len = spi->last_xfer_size;
250 else
251 xfer_len = xfer->len;
253 /* make sure we do not timeout in a slow transfer */
254 xfer_timeout = xfer_len * 8 * MSEC_PER_SEC / xfer->speed_hz;
255 xfer_timeout += GB_OPERATION_TIMEOUT_DEFAULT;
257 if (xfer_timeout > spi->op_timeout)
258 spi->op_timeout = xfer_timeout;
260 gb_xfer->speed_hz = cpu_to_le32(xfer->speed_hz);
261 gb_xfer->len = cpu_to_le32(xfer_len);
262 gb_xfer->delay_usecs = cpu_to_le16(xfer->delay_usecs);
263 gb_xfer->cs_change = xfer->cs_change;
264 gb_xfer->bits_per_word = xfer->bits_per_word;
266 /* Copy tx data */
267 if (xfer->tx_buf) {
268 gb_xfer->xfer_flags |= GB_SPI_XFER_WRITE;
269 memcpy(tx_data, xfer->tx_buf + spi->tx_xfer_offset,
270 xfer_len);
271 tx_data += xfer_len;
274 if (xfer->rx_buf)
275 gb_xfer->xfer_flags |= GB_SPI_XFER_READ;
277 if (xfer == spi->last_xfer) {
278 if (!is_last_xfer_done(spi))
279 gb_xfer->xfer_flags |= GB_SPI_XFER_INPROGRESS;
280 msg->state = GB_SPI_STATE_OP_DONE;
281 continue;
284 gb_xfer++;
285 xfer = get_next_xfer(xfer, msg);
288 msg->actual_length += total_len;
290 return operation;
293 static void gb_spi_decode_response(struct gb_spilib *spi,
294 struct spi_message *msg,
295 struct gb_spi_transfer_response *response)
297 struct spi_transfer *xfer = spi->first_xfer;
298 void *rx_data = response->data;
299 u32 xfer_len;
301 while (xfer) {
302 /* Copy rx data */
303 if (xfer->rx_buf) {
304 if (xfer == spi->first_xfer)
305 xfer_len = xfer->len - spi->rx_xfer_offset;
306 else if (xfer == spi->last_xfer)
307 xfer_len = spi->last_xfer_size;
308 else
309 xfer_len = xfer->len;
311 memcpy(xfer->rx_buf + spi->rx_xfer_offset, rx_data,
312 xfer_len);
313 rx_data += xfer_len;
316 if (xfer == spi->last_xfer)
317 break;
319 xfer = list_next_entry(xfer, transfer_list);
323 static int gb_spi_transfer_one_message(struct spi_master *master,
324 struct spi_message *msg)
326 struct gb_spilib *spi = spi_master_get_devdata(master);
327 struct gb_connection *connection = spi->connection;
328 struct gb_spi_transfer_response *response;
329 struct gb_operation *operation;
330 int ret = 0;
332 spi->first_xfer = list_first_entry_or_null(&msg->transfers,
333 struct spi_transfer,
334 transfer_list);
335 if (!spi->first_xfer) {
336 ret = -ENOMEM;
337 goto out;
340 msg->state = GB_SPI_STATE_MSG_IDLE;
342 while (msg->state != GB_SPI_STATE_MSG_DONE &&
343 msg->state != GB_SPI_STATE_MSG_ERROR) {
344 operation = gb_spi_operation_create(spi, connection, msg);
345 if (!operation) {
346 msg->state = GB_SPI_STATE_MSG_ERROR;
347 ret = -EINVAL;
348 continue;
351 ret = gb_operation_request_send_sync_timeout(operation,
352 spi->op_timeout);
353 if (!ret) {
354 response = operation->response->payload;
355 if (response)
356 gb_spi_decode_response(spi, msg, response);
357 } else {
358 dev_err(spi->parent,
359 "transfer operation failed: %d\n", ret);
360 msg->state = GB_SPI_STATE_MSG_ERROR;
363 gb_operation_put(operation);
364 setup_next_xfer(spi, msg);
367 out:
368 msg->status = ret;
369 clean_xfer_state(spi);
370 spi_finalize_current_message(master);
372 return ret;
375 static int gb_spi_prepare_transfer_hardware(struct spi_master *master)
377 struct gb_spilib *spi = spi_master_get_devdata(master);
379 return spi->ops->prepare_transfer_hardware(spi->parent);
382 static int gb_spi_unprepare_transfer_hardware(struct spi_master *master)
384 struct gb_spilib *spi = spi_master_get_devdata(master);
386 spi->ops->unprepare_transfer_hardware(spi->parent);
388 return 0;
391 static int gb_spi_setup(struct spi_device *spi)
393 /* Nothing to do for now */
394 return 0;
397 static void gb_spi_cleanup(struct spi_device *spi)
399 /* Nothing to do for now */
402 /* Routines to get controller information */
405 * Map Greybus spi mode bits/flags/bpw into Linux ones.
406 * All bits are same for now and so these macro's return same values.
408 #define gb_spi_mode_map(mode) mode
409 #define gb_spi_flags_map(flags) flags
411 static int gb_spi_get_master_config(struct gb_spilib *spi)
413 struct gb_spi_master_config_response response;
414 u16 mode, flags;
415 int ret;
417 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_MASTER_CONFIG,
418 NULL, 0, &response, sizeof(response));
419 if (ret < 0)
420 return ret;
422 mode = le16_to_cpu(response.mode);
423 spi->mode = gb_spi_mode_map(mode);
425 flags = le16_to_cpu(response.flags);
426 spi->flags = gb_spi_flags_map(flags);
428 spi->bits_per_word_mask = le32_to_cpu(response.bits_per_word_mask);
429 spi->num_chipselect = response.num_chipselect;
431 spi->min_speed_hz = le32_to_cpu(response.min_speed_hz);
432 spi->max_speed_hz = le32_to_cpu(response.max_speed_hz);
434 return 0;
437 static int gb_spi_setup_device(struct gb_spilib *spi, u8 cs)
439 struct spi_master *master = get_master_from_spi(spi);
440 struct gb_spi_device_config_request request;
441 struct gb_spi_device_config_response response;
442 struct spi_board_info spi_board = { {0} };
443 struct spi_device *spidev;
444 int ret;
445 u8 dev_type;
447 request.chip_select = cs;
449 ret = gb_operation_sync(spi->connection, GB_SPI_TYPE_DEVICE_CONFIG,
450 &request, sizeof(request),
451 &response, sizeof(response));
452 if (ret < 0)
453 return ret;
455 dev_type = response.device_type;
457 if (dev_type == GB_SPI_SPI_DEV)
458 strlcpy(spi_board.modalias, "spidev",
459 sizeof(spi_board.modalias));
460 else if (dev_type == GB_SPI_SPI_NOR)
461 strlcpy(spi_board.modalias, "spi-nor",
462 sizeof(spi_board.modalias));
463 else if (dev_type == GB_SPI_SPI_MODALIAS)
464 memcpy(spi_board.modalias, response.name,
465 sizeof(spi_board.modalias));
466 else
467 return -EINVAL;
469 spi_board.mode = le16_to_cpu(response.mode);
470 spi_board.bus_num = master->bus_num;
471 spi_board.chip_select = cs;
472 spi_board.max_speed_hz = le32_to_cpu(response.max_speed_hz);
474 spidev = spi_new_device(master, &spi_board);
475 if (!spidev)
476 return -EINVAL;
478 return 0;
481 int gb_spilib_master_init(struct gb_connection *connection, struct device *dev,
482 struct spilib_ops *ops)
484 struct gb_spilib *spi;
485 struct spi_master *master;
486 int ret;
487 u8 i;
489 /* Allocate master with space for data */
490 master = spi_alloc_master(dev, sizeof(*spi));
491 if (!master) {
492 dev_err(dev, "cannot alloc SPI master\n");
493 return -ENOMEM;
496 spi = spi_master_get_devdata(master);
497 spi->connection = connection;
498 gb_connection_set_data(connection, master);
499 spi->parent = dev;
500 spi->ops = ops;
502 /* get master configuration */
503 ret = gb_spi_get_master_config(spi);
504 if (ret)
505 goto exit_spi_put;
507 master->bus_num = -1; /* Allow spi-core to allocate it dynamically */
508 master->num_chipselect = spi->num_chipselect;
509 master->mode_bits = spi->mode;
510 master->flags = spi->flags;
511 master->bits_per_word_mask = spi->bits_per_word_mask;
513 /* Attach methods */
514 master->cleanup = gb_spi_cleanup;
515 master->setup = gb_spi_setup;
516 master->transfer_one_message = gb_spi_transfer_one_message;
518 if (ops && ops->prepare_transfer_hardware) {
519 master->prepare_transfer_hardware =
520 gb_spi_prepare_transfer_hardware;
523 if (ops && ops->unprepare_transfer_hardware) {
524 master->unprepare_transfer_hardware =
525 gb_spi_unprepare_transfer_hardware;
528 master->auto_runtime_pm = true;
530 ret = spi_register_master(master);
531 if (ret < 0)
532 goto exit_spi_put;
534 /* now, fetch the devices configuration */
535 for (i = 0; i < spi->num_chipselect; i++) {
536 ret = gb_spi_setup_device(spi, i);
537 if (ret < 0) {
538 dev_err(dev, "failed to allocate spi device %d: %d\n",
539 i, ret);
540 goto exit_spi_unregister;
544 return 0;
546 exit_spi_put:
547 spi_master_put(master);
549 return ret;
551 exit_spi_unregister:
552 spi_unregister_master(master);
554 return ret;
556 EXPORT_SYMBOL_GPL(gb_spilib_master_init);
558 void gb_spilib_master_exit(struct gb_connection *connection)
560 struct spi_master *master = gb_connection_get_data(connection);
562 spi_unregister_master(master);
564 EXPORT_SYMBOL_GPL(gb_spilib_master_exit);
566 MODULE_LICENSE("GPL v2");