1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2018 Exceet Electronics GmbH
4 * Copyright (C) 2018 Bootlin
6 * Author: Boris Brezillon <boris.brezillon@bootlin.com>
8 #include <linux/dmaengine.h>
9 #include <linux/iopoll.h>
10 #include <linux/pm_runtime.h>
11 #include <linux/spi/spi.h>
12 #include <linux/spi/spi-mem.h>
13 #include <linux/sched/task_stack.h>
15 #include "internals.h"
17 #define SPI_MEM_MAX_BUSWIDTH 8
20 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
22 * @ctlr: the SPI controller requesting this dma_map()
23 * @op: the memory operation containing the buffer to map
24 * @sgt: a pointer to a non-initialized sg_table that will be filled by this
27 * Some controllers might want to do DMA on the data buffer embedded in @op.
28 * This helper prepares everything for you and provides a ready-to-use
29 * sg_table. This function is not intended to be called from spi drivers.
30 * Only SPI controller drivers should use it.
31 * Note that the caller must ensure the memory region pointed by
32 * op->data.buf.{in,out} is DMA-able before calling this function.
34 * Return: 0 in case of success, a negative error code otherwise.
36 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
37 const struct spi_mem_op *op,
40 struct device *dmadev;
45 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
46 dmadev = ctlr->dma_tx->device->dev;
47 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
48 dmadev = ctlr->dma_rx->device->dev;
50 dmadev = ctlr->dev.parent;
55 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
56 op->data.dir == SPI_MEM_DATA_IN ?
57 DMA_FROM_DEVICE : DMA_TO_DEVICE);
59 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
62 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
64 * @ctlr: the SPI controller requesting this dma_unmap()
65 * @op: the memory operation containing the buffer to unmap
66 * @sgt: a pointer to an sg_table previously initialized by
67 * spi_controller_dma_map_mem_op_data()
69 * Some controllers might want to do DMA on the data buffer embedded in @op.
70 * This helper prepares things so that the CPU can access the
71 * op->data.buf.{in,out} buffer again.
73 * This function is not intended to be called from SPI drivers. Only SPI
74 * controller drivers should use it.
76 * This function should be called after the DMA operation has finished and is
77 * only valid if the previous spi_controller_dma_map_mem_op_data() call
80 * Return: 0 in case of success, a negative error code otherwise.
82 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
83 const struct spi_mem_op *op,
86 struct device *dmadev;
91 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
92 dmadev = ctlr->dma_tx->device->dev;
93 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
94 dmadev = ctlr->dma_rx->device->dev;
96 dmadev = ctlr->dev.parent;
98 spi_unmap_buf(ctlr, dmadev, sgt,
99 op->data.dir == SPI_MEM_DATA_IN ?
100 DMA_FROM_DEVICE : DMA_TO_DEVICE);
102 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
104 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx)
106 u32 mode = mem->spi->mode;
114 (mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL))) ||
116 (mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))))
122 if ((tx && (mode & (SPI_TX_QUAD | SPI_TX_OCTAL))) ||
123 (!tx && (mode & (SPI_RX_QUAD | SPI_RX_OCTAL))))
129 if ((tx && (mode & SPI_TX_OCTAL)) ||
130 (!tx && (mode & SPI_RX_OCTAL)))
142 static bool spi_mem_check_buswidth(struct spi_mem *mem,
143 const struct spi_mem_op *op)
145 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
148 if (op->addr.nbytes &&
149 spi_check_buswidth_req(mem, op->addr.buswidth, true))
152 if (op->dummy.nbytes &&
153 spi_check_buswidth_req(mem, op->dummy.buswidth, true))
156 if (op->data.dir != SPI_MEM_NO_DATA &&
157 spi_check_buswidth_req(mem, op->data.buswidth,
158 op->data.dir == SPI_MEM_DATA_OUT))
164 bool spi_mem_default_supports_op(struct spi_mem *mem,
165 const struct spi_mem_op *op)
167 struct spi_controller *ctlr = mem->spi->controller;
169 op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr;
172 if (!spi_mem_controller_is_capable(ctlr, dtr))
175 if (op->cmd.nbytes != 2)
178 if (op->cmd.nbytes != 1)
183 if (!spi_mem_controller_is_capable(ctlr, ecc))
187 return spi_mem_check_buswidth(mem, op);
189 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
191 static bool spi_mem_buswidth_is_valid(u8 buswidth)
193 if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH)
199 static int spi_mem_check_op(const struct spi_mem_op *op)
201 if (!op->cmd.buswidth || !op->cmd.nbytes)
204 if ((op->addr.nbytes && !op->addr.buswidth) ||
205 (op->dummy.nbytes && !op->dummy.buswidth) ||
206 (op->data.nbytes && !op->data.buswidth))
209 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
210 !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
211 !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
212 !spi_mem_buswidth_is_valid(op->data.buswidth))
215 /* Buffers must be DMA-able. */
216 if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_IN &&
217 object_is_on_stack(op->data.buf.in)))
220 if (WARN_ON_ONCE(op->data.dir == SPI_MEM_DATA_OUT &&
221 object_is_on_stack(op->data.buf.out)))
227 static bool spi_mem_internal_supports_op(struct spi_mem *mem,
228 const struct spi_mem_op *op)
230 struct spi_controller *ctlr = mem->spi->controller;
232 if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
233 return ctlr->mem_ops->supports_op(mem, op);
235 return spi_mem_default_supports_op(mem, op);
239 * spi_mem_supports_op() - Check if a memory device and the controller it is
240 * connected to support a specific memory operation
241 * @mem: the SPI memory
242 * @op: the memory operation to check
244 * Some controllers are only supporting Single or Dual IOs, others might only
245 * support specific opcodes, or it can even be that the controller and device
246 * both support Quad IOs but the hardware prevents you from using it because
247 * only 2 IO lines are connected.
249 * This function checks whether a specific operation is supported.
251 * Return: true if @op is supported, false otherwise.
253 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
255 if (spi_mem_check_op(op))
258 return spi_mem_internal_supports_op(mem, op);
260 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
262 static int spi_mem_access_start(struct spi_mem *mem)
264 struct spi_controller *ctlr = mem->spi->controller;
267 * Flush the message queue before executing our SPI memory
268 * operation to prevent preemption of regular SPI transfers.
270 spi_flush_queue(ctlr);
272 if (ctlr->auto_runtime_pm) {
275 ret = pm_runtime_resume_and_get(ctlr->dev.parent);
277 dev_err(&ctlr->dev, "Failed to power device: %d\n",
283 mutex_lock(&ctlr->bus_lock_mutex);
284 mutex_lock(&ctlr->io_mutex);
289 static void spi_mem_access_end(struct spi_mem *mem)
291 struct spi_controller *ctlr = mem->spi->controller;
293 mutex_unlock(&ctlr->io_mutex);
294 mutex_unlock(&ctlr->bus_lock_mutex);
296 if (ctlr->auto_runtime_pm)
297 pm_runtime_put(ctlr->dev.parent);
301 * spi_mem_exec_op() - Execute a memory operation
302 * @mem: the SPI memory
303 * @op: the memory operation to execute
305 * Executes a memory operation.
307 * This function first checks that @op is supported and then tries to execute
310 * Return: 0 in case of success, a negative error code otherwise.
312 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
314 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
315 struct spi_controller *ctlr = mem->spi->controller;
316 struct spi_transfer xfers[4] = { };
317 struct spi_message msg;
321 ret = spi_mem_check_op(op);
325 if (!spi_mem_internal_supports_op(mem, op))
328 if (ctlr->mem_ops && !mem->spi->cs_gpiod) {
329 ret = spi_mem_access_start(mem);
333 ret = ctlr->mem_ops->exec_op(mem, op);
335 spi_mem_access_end(mem);
338 * Some controllers only optimize specific paths (typically the
339 * read path) and expect the core to use the regular SPI
340 * interface in other cases.
342 if (!ret || ret != -ENOTSUPP)
346 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
349 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
350 * we're guaranteed that this buffer is DMA-able, as required by the
353 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
357 spi_message_init(&msg);
359 tmpbuf[0] = op->cmd.opcode;
360 xfers[xferpos].tx_buf = tmpbuf;
361 xfers[xferpos].len = op->cmd.nbytes;
362 xfers[xferpos].tx_nbits = op->cmd.buswidth;
363 spi_message_add_tail(&xfers[xferpos], &msg);
367 if (op->addr.nbytes) {
370 for (i = 0; i < op->addr.nbytes; i++)
371 tmpbuf[i + 1] = op->addr.val >>
372 (8 * (op->addr.nbytes - i - 1));
374 xfers[xferpos].tx_buf = tmpbuf + 1;
375 xfers[xferpos].len = op->addr.nbytes;
376 xfers[xferpos].tx_nbits = op->addr.buswidth;
377 spi_message_add_tail(&xfers[xferpos], &msg);
379 totalxferlen += op->addr.nbytes;
382 if (op->dummy.nbytes) {
383 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
384 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
385 xfers[xferpos].len = op->dummy.nbytes;
386 xfers[xferpos].tx_nbits = op->dummy.buswidth;
387 xfers[xferpos].dummy_data = 1;
388 spi_message_add_tail(&xfers[xferpos], &msg);
390 totalxferlen += op->dummy.nbytes;
393 if (op->data.nbytes) {
394 if (op->data.dir == SPI_MEM_DATA_IN) {
395 xfers[xferpos].rx_buf = op->data.buf.in;
396 xfers[xferpos].rx_nbits = op->data.buswidth;
398 xfers[xferpos].tx_buf = op->data.buf.out;
399 xfers[xferpos].tx_nbits = op->data.buswidth;
402 xfers[xferpos].len = op->data.nbytes;
403 spi_message_add_tail(&xfers[xferpos], &msg);
405 totalxferlen += op->data.nbytes;
408 ret = spi_sync(mem->spi, &msg);
415 if (msg.actual_length != totalxferlen)
420 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
423 * spi_mem_get_name() - Return the SPI mem device name to be used by the
424 * upper layer if necessary
425 * @mem: the SPI memory
427 * This function allows SPI mem users to retrieve the SPI mem device name.
428 * It is useful if the upper layer needs to expose a custom name for
429 * compatibility reasons.
431 * Return: a string containing the name of the memory device to be used
432 * by the SPI mem user
434 const char *spi_mem_get_name(struct spi_mem *mem)
438 EXPORT_SYMBOL_GPL(spi_mem_get_name);
441 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
442 * match controller limitations
443 * @mem: the SPI memory
444 * @op: the operation to adjust
446 * Some controllers have FIFO limitations and must split a data transfer
447 * operation into multiple ones, others require a specific alignment for
448 * optimized accesses. This function allows SPI mem drivers to split a single
449 * operation into multiple sub-operations when required.
451 * Return: a negative error code if the controller can't properly adjust @op,
452 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
453 * can't be handled in a single step.
455 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
457 struct spi_controller *ctlr = mem->spi->controller;
460 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
461 return ctlr->mem_ops->adjust_op_size(mem, op);
463 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
464 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
466 if (len > spi_max_transfer_size(mem->spi))
469 op->data.nbytes = min3((size_t)op->data.nbytes,
470 spi_max_transfer_size(mem->spi),
471 spi_max_message_size(mem->spi) -
473 if (!op->data.nbytes)
479 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
481 static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc,
482 u64 offs, size_t len, void *buf)
484 struct spi_mem_op op = desc->info.op_tmpl;
487 op.addr.val = desc->info.offset + offs;
488 op.data.buf.in = buf;
489 op.data.nbytes = len;
490 ret = spi_mem_adjust_op_size(desc->mem, &op);
494 ret = spi_mem_exec_op(desc->mem, &op);
498 return op.data.nbytes;
501 static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc,
502 u64 offs, size_t len, const void *buf)
504 struct spi_mem_op op = desc->info.op_tmpl;
507 op.addr.val = desc->info.offset + offs;
508 op.data.buf.out = buf;
509 op.data.nbytes = len;
510 ret = spi_mem_adjust_op_size(desc->mem, &op);
514 ret = spi_mem_exec_op(desc->mem, &op);
518 return op.data.nbytes;
522 * spi_mem_dirmap_create() - Create a direct mapping descriptor
523 * @mem: SPI mem device this direct mapping should be created for
524 * @info: direct mapping information
526 * This function is creating a direct mapping descriptor which can then be used
527 * to access the memory using spi_mem_dirmap_read() or spi_mem_dirmap_write().
528 * If the SPI controller driver does not support direct mapping, this function
529 * falls back to an implementation using spi_mem_exec_op(), so that the caller
530 * doesn't have to bother implementing a fallback on his own.
532 * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
534 struct spi_mem_dirmap_desc *
535 spi_mem_dirmap_create(struct spi_mem *mem,
536 const struct spi_mem_dirmap_info *info)
538 struct spi_controller *ctlr = mem->spi->controller;
539 struct spi_mem_dirmap_desc *desc;
542 /* Make sure the number of address cycles is between 1 and 8 bytes. */
543 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8)
544 return ERR_PTR(-EINVAL);
546 /* data.dir should either be SPI_MEM_DATA_IN or SPI_MEM_DATA_OUT. */
547 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA)
548 return ERR_PTR(-EINVAL);
550 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
552 return ERR_PTR(-ENOMEM);
556 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create)
557 ret = ctlr->mem_ops->dirmap_create(desc);
560 desc->nodirmap = true;
561 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl))
574 EXPORT_SYMBOL_GPL(spi_mem_dirmap_create);
577 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
578 * @desc: the direct mapping descriptor to destroy
580 * This function destroys a direct mapping descriptor previously created by
581 * spi_mem_dirmap_create().
583 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc)
585 struct spi_controller *ctlr = desc->mem->spi->controller;
587 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy)
588 ctlr->mem_ops->dirmap_destroy(desc);
592 EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy);
594 static void devm_spi_mem_dirmap_release(struct device *dev, void *res)
596 struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res;
598 spi_mem_dirmap_destroy(desc);
602 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
604 * @dev: device the dirmap desc will be attached to
605 * @mem: SPI mem device this direct mapping should be created for
606 * @info: direct mapping information
608 * devm_ variant of the spi_mem_dirmap_create() function. See
609 * spi_mem_dirmap_create() for more details.
611 * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
613 struct spi_mem_dirmap_desc *
614 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
615 const struct spi_mem_dirmap_info *info)
617 struct spi_mem_dirmap_desc **ptr, *desc;
619 ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr),
622 return ERR_PTR(-ENOMEM);
624 desc = spi_mem_dirmap_create(mem, info);
629 devres_add(dev, ptr);
634 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create);
636 static int devm_spi_mem_dirmap_match(struct device *dev, void *res, void *data)
638 struct spi_mem_dirmap_desc **ptr = res;
640 if (WARN_ON(!ptr || !*ptr))
647 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
649 * @dev: device the dirmap desc is attached to
650 * @desc: the direct mapping descriptor to destroy
652 * devm_ variant of the spi_mem_dirmap_destroy() function. See
653 * spi_mem_dirmap_destroy() for more details.
655 void devm_spi_mem_dirmap_destroy(struct device *dev,
656 struct spi_mem_dirmap_desc *desc)
658 devres_release(dev, devm_spi_mem_dirmap_release,
659 devm_spi_mem_dirmap_match, desc);
661 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy);
664 * spi_mem_dirmap_read() - Read data through a direct mapping
665 * @desc: direct mapping descriptor
666 * @offs: offset to start reading from. Note that this is not an absolute
667 * offset, but the offset within the direct mapping which already has
669 * @len: length in bytes
670 * @buf: destination buffer. This buffer must be DMA-able
672 * This function reads data from a memory device using a direct mapping
673 * previously instantiated with spi_mem_dirmap_create().
675 * Return: the amount of data read from the memory device or a negative error
676 * code. Note that the returned size might be smaller than @len, and the caller
677 * is responsible for calling spi_mem_dirmap_read() again when that happens.
679 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
680 u64 offs, size_t len, void *buf)
682 struct spi_controller *ctlr = desc->mem->spi->controller;
685 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN)
691 if (desc->nodirmap) {
692 ret = spi_mem_no_dirmap_read(desc, offs, len, buf);
693 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) {
694 ret = spi_mem_access_start(desc->mem);
698 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf);
700 spi_mem_access_end(desc->mem);
707 EXPORT_SYMBOL_GPL(spi_mem_dirmap_read);
710 * spi_mem_dirmap_write() - Write data through a direct mapping
711 * @desc: direct mapping descriptor
712 * @offs: offset to start writing from. Note that this is not an absolute
713 * offset, but the offset within the direct mapping which already has
715 * @len: length in bytes
716 * @buf: source buffer. This buffer must be DMA-able
718 * This function writes data to a memory device using a direct mapping
719 * previously instantiated with spi_mem_dirmap_create().
721 * Return: the amount of data written to the memory device or a negative error
722 * code. Note that the returned size might be smaller than @len, and the caller
723 * is responsible for calling spi_mem_dirmap_write() again when that happens.
725 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
726 u64 offs, size_t len, const void *buf)
728 struct spi_controller *ctlr = desc->mem->spi->controller;
731 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT)
737 if (desc->nodirmap) {
738 ret = spi_mem_no_dirmap_write(desc, offs, len, buf);
739 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) {
740 ret = spi_mem_access_start(desc->mem);
744 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf);
746 spi_mem_access_end(desc->mem);
753 EXPORT_SYMBOL_GPL(spi_mem_dirmap_write);
755 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
757 return container_of(drv, struct spi_mem_driver, spidrv.driver);
760 static int spi_mem_read_status(struct spi_mem *mem,
761 const struct spi_mem_op *op,
764 const u8 *bytes = (u8 *)op->data.buf.in;
767 ret = spi_mem_exec_op(mem, op);
771 if (op->data.nbytes > 1)
772 *status = ((u16)bytes[0] << 8) | bytes[1];
780 * spi_mem_poll_status() - Poll memory device status
781 * @mem: SPI memory device
782 * @op: the memory operation to execute
783 * @mask: status bitmask to ckeck
784 * @match: (status & mask) expected value
785 * @initial_delay_us: delay in us before starting to poll
786 * @polling_delay_us: time to sleep between reads in us
787 * @timeout_ms: timeout in milliseconds
789 * This function polls a status register and returns when
790 * (status & mask) == match or when the timeout has expired.
792 * Return: 0 in case of success, -ETIMEDOUT in case of error,
793 * -EOPNOTSUPP if not supported.
795 int spi_mem_poll_status(struct spi_mem *mem,
796 const struct spi_mem_op *op,
798 unsigned long initial_delay_us,
799 unsigned long polling_delay_us,
802 struct spi_controller *ctlr = mem->spi->controller;
803 int ret = -EOPNOTSUPP;
807 if (op->data.nbytes < 1 || op->data.nbytes > 2 ||
808 op->data.dir != SPI_MEM_DATA_IN)
811 if (ctlr->mem_ops && ctlr->mem_ops->poll_status && !mem->spi->cs_gpiod) {
812 ret = spi_mem_access_start(mem);
816 ret = ctlr->mem_ops->poll_status(mem, op, mask, match,
817 initial_delay_us, polling_delay_us,
820 spi_mem_access_end(mem);
823 if (ret == -EOPNOTSUPP) {
824 if (!spi_mem_supports_op(mem, op))
827 if (initial_delay_us < 10)
828 udelay(initial_delay_us);
830 usleep_range((initial_delay_us >> 2) + 1,
833 ret = read_poll_timeout(spi_mem_read_status, read_status_ret,
834 (read_status_ret || ((status) & mask) == match),
835 polling_delay_us, timeout_ms * 1000, false, mem,
838 return read_status_ret;
843 EXPORT_SYMBOL_GPL(spi_mem_poll_status);
845 static int spi_mem_probe(struct spi_device *spi)
847 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
848 struct spi_controller *ctlr = spi->controller;
851 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
857 if (ctlr->mem_ops && ctlr->mem_ops->get_name)
858 mem->name = ctlr->mem_ops->get_name(mem);
860 mem->name = dev_name(&spi->dev);
862 if (IS_ERR_OR_NULL(mem->name))
863 return PTR_ERR_OR_ZERO(mem->name);
865 spi_set_drvdata(spi, mem);
867 return memdrv->probe(mem);
870 static void spi_mem_remove(struct spi_device *spi)
872 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
873 struct spi_mem *mem = spi_get_drvdata(spi);
879 static void spi_mem_shutdown(struct spi_device *spi)
881 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
882 struct spi_mem *mem = spi_get_drvdata(spi);
884 if (memdrv->shutdown)
885 memdrv->shutdown(mem);
889 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
890 * @memdrv: the SPI memory driver to register
891 * @owner: the owner of this driver
893 * Registers a SPI memory driver.
895 * Return: 0 in case of success, a negative error core otherwise.
898 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
899 struct module *owner)
901 memdrv->spidrv.probe = spi_mem_probe;
902 memdrv->spidrv.remove = spi_mem_remove;
903 memdrv->spidrv.shutdown = spi_mem_shutdown;
905 return __spi_register_driver(owner, &memdrv->spidrv);
907 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
910 * spi_mem_driver_unregister() - Unregister a SPI memory driver
911 * @memdrv: the SPI memory driver to unregister
913 * Unregisters a SPI memory driver.
915 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
917 spi_unregister_driver(&memdrv->spidrv);
919 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);