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>
14 #include "internals.h"
16 #define SPI_MEM_MAX_BUSWIDTH 8
19 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
21 * @ctlr: the SPI controller requesting this dma_map()
22 * @op: the memory operation containing the buffer to map
23 * @sgt: a pointer to a non-initialized sg_table that will be filled by this
26 * Some controllers might want to do DMA on the data buffer embedded in @op.
27 * This helper prepares everything for you and provides a ready-to-use
28 * sg_table. This function is not intended to be called from spi drivers.
29 * Only SPI controller drivers should use it.
30 * Note that the caller must ensure the memory region pointed by
31 * op->data.buf.{in,out} is DMA-able before calling this function.
33 * Return: 0 in case of success, a negative error code otherwise.
35 int spi_controller_dma_map_mem_op_data(struct spi_controller *ctlr,
36 const struct spi_mem_op *op,
39 struct device *dmadev;
44 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
45 dmadev = ctlr->dma_tx->device->dev;
46 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
47 dmadev = ctlr->dma_rx->device->dev;
49 dmadev = ctlr->dev.parent;
54 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes,
55 op->data.dir == SPI_MEM_DATA_IN ?
56 DMA_FROM_DEVICE : DMA_TO_DEVICE);
58 EXPORT_SYMBOL_GPL(spi_controller_dma_map_mem_op_data);
61 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
63 * @ctlr: the SPI controller requesting this dma_unmap()
64 * @op: the memory operation containing the buffer to unmap
65 * @sgt: a pointer to an sg_table previously initialized by
66 * spi_controller_dma_map_mem_op_data()
68 * Some controllers might want to do DMA on the data buffer embedded in @op.
69 * This helper prepares things so that the CPU can access the
70 * op->data.buf.{in,out} buffer again.
72 * This function is not intended to be called from SPI drivers. Only SPI
73 * controller drivers should use it.
75 * This function should be called after the DMA operation has finished and is
76 * only valid if the previous spi_controller_dma_map_mem_op_data() call
79 * Return: 0 in case of success, a negative error code otherwise.
81 void spi_controller_dma_unmap_mem_op_data(struct spi_controller *ctlr,
82 const struct spi_mem_op *op,
85 struct device *dmadev;
90 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx)
91 dmadev = ctlr->dma_tx->device->dev;
92 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx)
93 dmadev = ctlr->dma_rx->device->dev;
95 dmadev = ctlr->dev.parent;
97 spi_unmap_buf(ctlr, dmadev, sgt,
98 op->data.dir == SPI_MEM_DATA_IN ?
99 DMA_FROM_DEVICE : DMA_TO_DEVICE);
101 EXPORT_SYMBOL_GPL(spi_controller_dma_unmap_mem_op_data);
103 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx)
105 u32 mode = mem->spi->mode;
113 (mode & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL))) ||
115 (mode & (SPI_RX_DUAL | SPI_RX_QUAD | SPI_RX_OCTAL))))
121 if ((tx && (mode & (SPI_TX_QUAD | SPI_TX_OCTAL))) ||
122 (!tx && (mode & (SPI_RX_QUAD | SPI_RX_OCTAL))))
128 if ((tx && (mode & SPI_TX_OCTAL)) ||
129 (!tx && (mode & SPI_RX_OCTAL)))
141 static bool spi_mem_check_buswidth(struct spi_mem *mem,
142 const struct spi_mem_op *op)
144 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true))
147 if (op->addr.nbytes &&
148 spi_check_buswidth_req(mem, op->addr.buswidth, true))
151 if (op->dummy.nbytes &&
152 spi_check_buswidth_req(mem, op->dummy.buswidth, true))
155 if (op->data.dir != SPI_MEM_NO_DATA &&
156 spi_check_buswidth_req(mem, op->data.buswidth,
157 op->data.dir == SPI_MEM_DATA_OUT))
163 bool spi_mem_default_supports_op(struct spi_mem *mem,
164 const struct spi_mem_op *op)
166 struct spi_controller *ctlr = mem->spi->controller;
168 op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr;
171 if (!spi_mem_controller_is_capable(ctlr, dtr))
174 if (op->cmd.nbytes != 2)
177 if (op->cmd.nbytes != 1)
182 if (!spi_mem_controller_is_capable(ctlr, ecc))
186 return spi_mem_check_buswidth(mem, op);
188 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
190 static bool spi_mem_buswidth_is_valid(u8 buswidth)
192 if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH)
198 static int spi_mem_check_op(const struct spi_mem_op *op)
200 if (!op->cmd.buswidth || !op->cmd.nbytes)
203 if ((op->addr.nbytes && !op->addr.buswidth) ||
204 (op->dummy.nbytes && !op->dummy.buswidth) ||
205 (op->data.nbytes && !op->data.buswidth))
208 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
209 !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
210 !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
211 !spi_mem_buswidth_is_valid(op->data.buswidth))
217 static bool spi_mem_internal_supports_op(struct spi_mem *mem,
218 const struct spi_mem_op *op)
220 struct spi_controller *ctlr = mem->spi->controller;
222 if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
223 return ctlr->mem_ops->supports_op(mem, op);
225 return spi_mem_default_supports_op(mem, op);
229 * spi_mem_supports_op() - Check if a memory device and the controller it is
230 * connected to support a specific memory operation
231 * @mem: the SPI memory
232 * @op: the memory operation to check
234 * Some controllers are only supporting Single or Dual IOs, others might only
235 * support specific opcodes, or it can even be that the controller and device
236 * both support Quad IOs but the hardware prevents you from using it because
237 * only 2 IO lines are connected.
239 * This function checks whether a specific operation is supported.
241 * Return: true if @op is supported, false otherwise.
243 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
245 if (spi_mem_check_op(op))
248 return spi_mem_internal_supports_op(mem, op);
250 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
252 static int spi_mem_access_start(struct spi_mem *mem)
254 struct spi_controller *ctlr = mem->spi->controller;
257 * Flush the message queue before executing our SPI memory
258 * operation to prevent preemption of regular SPI transfers.
260 spi_flush_queue(ctlr);
262 if (ctlr->auto_runtime_pm) {
265 ret = pm_runtime_get_sync(ctlr->dev.parent);
267 pm_runtime_put_noidle(ctlr->dev.parent);
268 dev_err(&ctlr->dev, "Failed to power device: %d\n",
274 mutex_lock(&ctlr->bus_lock_mutex);
275 mutex_lock(&ctlr->io_mutex);
280 static void spi_mem_access_end(struct spi_mem *mem)
282 struct spi_controller *ctlr = mem->spi->controller;
284 mutex_unlock(&ctlr->io_mutex);
285 mutex_unlock(&ctlr->bus_lock_mutex);
287 if (ctlr->auto_runtime_pm)
288 pm_runtime_put(ctlr->dev.parent);
292 * spi_mem_exec_op() - Execute a memory operation
293 * @mem: the SPI memory
294 * @op: the memory operation to execute
296 * Executes a memory operation.
298 * This function first checks that @op is supported and then tries to execute
301 * Return: 0 in case of success, a negative error code otherwise.
303 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
305 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
306 struct spi_controller *ctlr = mem->spi->controller;
307 struct spi_transfer xfers[4] = { };
308 struct spi_message msg;
312 ret = spi_mem_check_op(op);
316 if (!spi_mem_internal_supports_op(mem, op))
319 if (ctlr->mem_ops && !mem->spi->cs_gpiod) {
320 ret = spi_mem_access_start(mem);
324 ret = ctlr->mem_ops->exec_op(mem, op);
326 spi_mem_access_end(mem);
329 * Some controllers only optimize specific paths (typically the
330 * read path) and expect the core to use the regular SPI
331 * interface in other cases.
333 if (!ret || ret != -ENOTSUPP)
337 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
340 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
341 * we're guaranteed that this buffer is DMA-able, as required by the
344 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
348 spi_message_init(&msg);
350 tmpbuf[0] = op->cmd.opcode;
351 xfers[xferpos].tx_buf = tmpbuf;
352 xfers[xferpos].len = op->cmd.nbytes;
353 xfers[xferpos].tx_nbits = op->cmd.buswidth;
354 spi_message_add_tail(&xfers[xferpos], &msg);
358 if (op->addr.nbytes) {
361 for (i = 0; i < op->addr.nbytes; i++)
362 tmpbuf[i + 1] = op->addr.val >>
363 (8 * (op->addr.nbytes - i - 1));
365 xfers[xferpos].tx_buf = tmpbuf + 1;
366 xfers[xferpos].len = op->addr.nbytes;
367 xfers[xferpos].tx_nbits = op->addr.buswidth;
368 spi_message_add_tail(&xfers[xferpos], &msg);
370 totalxferlen += op->addr.nbytes;
373 if (op->dummy.nbytes) {
374 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
375 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
376 xfers[xferpos].len = op->dummy.nbytes;
377 xfers[xferpos].tx_nbits = op->dummy.buswidth;
378 xfers[xferpos].dummy_data = 1;
379 spi_message_add_tail(&xfers[xferpos], &msg);
381 totalxferlen += op->dummy.nbytes;
384 if (op->data.nbytes) {
385 if (op->data.dir == SPI_MEM_DATA_IN) {
386 xfers[xferpos].rx_buf = op->data.buf.in;
387 xfers[xferpos].rx_nbits = op->data.buswidth;
389 xfers[xferpos].tx_buf = op->data.buf.out;
390 xfers[xferpos].tx_nbits = op->data.buswidth;
393 xfers[xferpos].len = op->data.nbytes;
394 spi_message_add_tail(&xfers[xferpos], &msg);
396 totalxferlen += op->data.nbytes;
399 ret = spi_sync(mem->spi, &msg);
406 if (msg.actual_length != totalxferlen)
411 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
414 * spi_mem_get_name() - Return the SPI mem device name to be used by the
415 * upper layer if necessary
416 * @mem: the SPI memory
418 * This function allows SPI mem users to retrieve the SPI mem device name.
419 * It is useful if the upper layer needs to expose a custom name for
420 * compatibility reasons.
422 * Return: a string containing the name of the memory device to be used
423 * by the SPI mem user
425 const char *spi_mem_get_name(struct spi_mem *mem)
429 EXPORT_SYMBOL_GPL(spi_mem_get_name);
432 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
433 * match controller limitations
434 * @mem: the SPI memory
435 * @op: the operation to adjust
437 * Some controllers have FIFO limitations and must split a data transfer
438 * operation into multiple ones, others require a specific alignment for
439 * optimized accesses. This function allows SPI mem drivers to split a single
440 * operation into multiple sub-operations when required.
442 * Return: a negative error code if the controller can't properly adjust @op,
443 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
444 * can't be handled in a single step.
446 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
448 struct spi_controller *ctlr = mem->spi->controller;
451 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
452 return ctlr->mem_ops->adjust_op_size(mem, op);
454 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
455 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
457 if (len > spi_max_transfer_size(mem->spi))
460 op->data.nbytes = min3((size_t)op->data.nbytes,
461 spi_max_transfer_size(mem->spi),
462 spi_max_message_size(mem->spi) -
464 if (!op->data.nbytes)
470 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
472 static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc,
473 u64 offs, size_t len, void *buf)
475 struct spi_mem_op op = desc->info.op_tmpl;
478 op.addr.val = desc->info.offset + offs;
479 op.data.buf.in = buf;
480 op.data.nbytes = len;
481 ret = spi_mem_adjust_op_size(desc->mem, &op);
485 ret = spi_mem_exec_op(desc->mem, &op);
489 return op.data.nbytes;
492 static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc,
493 u64 offs, size_t len, const void *buf)
495 struct spi_mem_op op = desc->info.op_tmpl;
498 op.addr.val = desc->info.offset + offs;
499 op.data.buf.out = buf;
500 op.data.nbytes = len;
501 ret = spi_mem_adjust_op_size(desc->mem, &op);
505 ret = spi_mem_exec_op(desc->mem, &op);
509 return op.data.nbytes;
513 * spi_mem_dirmap_create() - Create a direct mapping descriptor
514 * @mem: SPI mem device this direct mapping should be created for
515 * @info: direct mapping information
517 * This function is creating a direct mapping descriptor which can then be used
518 * to access the memory using spi_mem_dirmap_read() or spi_mem_dirmap_write().
519 * If the SPI controller driver does not support direct mapping, this function
520 * falls back to an implementation using spi_mem_exec_op(), so that the caller
521 * doesn't have to bother implementing a fallback on his own.
523 * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
525 struct spi_mem_dirmap_desc *
526 spi_mem_dirmap_create(struct spi_mem *mem,
527 const struct spi_mem_dirmap_info *info)
529 struct spi_controller *ctlr = mem->spi->controller;
530 struct spi_mem_dirmap_desc *desc;
533 /* Make sure the number of address cycles is between 1 and 8 bytes. */
534 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8)
535 return ERR_PTR(-EINVAL);
537 /* data.dir should either be SPI_MEM_DATA_IN or SPI_MEM_DATA_OUT. */
538 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA)
539 return ERR_PTR(-EINVAL);
541 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
543 return ERR_PTR(-ENOMEM);
547 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create)
548 ret = ctlr->mem_ops->dirmap_create(desc);
551 desc->nodirmap = true;
552 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl))
565 EXPORT_SYMBOL_GPL(spi_mem_dirmap_create);
568 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
569 * @desc: the direct mapping descriptor to destroy
571 * This function destroys a direct mapping descriptor previously created by
572 * spi_mem_dirmap_create().
574 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc)
576 struct spi_controller *ctlr = desc->mem->spi->controller;
578 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy)
579 ctlr->mem_ops->dirmap_destroy(desc);
583 EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy);
585 static void devm_spi_mem_dirmap_release(struct device *dev, void *res)
587 struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res;
589 spi_mem_dirmap_destroy(desc);
593 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
595 * @dev: device the dirmap desc will be attached to
596 * @mem: SPI mem device this direct mapping should be created for
597 * @info: direct mapping information
599 * devm_ variant of the spi_mem_dirmap_create() function. See
600 * spi_mem_dirmap_create() for more details.
602 * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
604 struct spi_mem_dirmap_desc *
605 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
606 const struct spi_mem_dirmap_info *info)
608 struct spi_mem_dirmap_desc **ptr, *desc;
610 ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr),
613 return ERR_PTR(-ENOMEM);
615 desc = spi_mem_dirmap_create(mem, info);
620 devres_add(dev, ptr);
625 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create);
627 static int devm_spi_mem_dirmap_match(struct device *dev, void *res, void *data)
629 struct spi_mem_dirmap_desc **ptr = res;
631 if (WARN_ON(!ptr || !*ptr))
638 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
640 * @dev: device the dirmap desc is attached to
641 * @desc: the direct mapping descriptor to destroy
643 * devm_ variant of the spi_mem_dirmap_destroy() function. See
644 * spi_mem_dirmap_destroy() for more details.
646 void devm_spi_mem_dirmap_destroy(struct device *dev,
647 struct spi_mem_dirmap_desc *desc)
649 devres_release(dev, devm_spi_mem_dirmap_release,
650 devm_spi_mem_dirmap_match, desc);
652 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy);
655 * spi_mem_dirmap_read() - Read data through a direct mapping
656 * @desc: direct mapping descriptor
657 * @offs: offset to start reading from. Note that this is not an absolute
658 * offset, but the offset within the direct mapping which already has
660 * @len: length in bytes
661 * @buf: destination buffer. This buffer must be DMA-able
663 * This function reads data from a memory device using a direct mapping
664 * previously instantiated with spi_mem_dirmap_create().
666 * Return: the amount of data read from the memory device or a negative error
667 * code. Note that the returned size might be smaller than @len, and the caller
668 * is responsible for calling spi_mem_dirmap_read() again when that happens.
670 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
671 u64 offs, size_t len, void *buf)
673 struct spi_controller *ctlr = desc->mem->spi->controller;
676 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN)
682 if (desc->nodirmap) {
683 ret = spi_mem_no_dirmap_read(desc, offs, len, buf);
684 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) {
685 ret = spi_mem_access_start(desc->mem);
689 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf);
691 spi_mem_access_end(desc->mem);
698 EXPORT_SYMBOL_GPL(spi_mem_dirmap_read);
701 * spi_mem_dirmap_write() - Write data through a direct mapping
702 * @desc: direct mapping descriptor
703 * @offs: offset to start writing from. Note that this is not an absolute
704 * offset, but the offset within the direct mapping which already has
706 * @len: length in bytes
707 * @buf: source buffer. This buffer must be DMA-able
709 * This function writes data to a memory device using a direct mapping
710 * previously instantiated with spi_mem_dirmap_create().
712 * Return: the amount of data written to the memory device or a negative error
713 * code. Note that the returned size might be smaller than @len, and the caller
714 * is responsible for calling spi_mem_dirmap_write() again when that happens.
716 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
717 u64 offs, size_t len, const void *buf)
719 struct spi_controller *ctlr = desc->mem->spi->controller;
722 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT)
728 if (desc->nodirmap) {
729 ret = spi_mem_no_dirmap_write(desc, offs, len, buf);
730 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) {
731 ret = spi_mem_access_start(desc->mem);
735 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf);
737 spi_mem_access_end(desc->mem);
744 EXPORT_SYMBOL_GPL(spi_mem_dirmap_write);
746 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
748 return container_of(drv, struct spi_mem_driver, spidrv.driver);
751 static int spi_mem_read_status(struct spi_mem *mem,
752 const struct spi_mem_op *op,
755 const u8 *bytes = (u8 *)op->data.buf.in;
758 ret = spi_mem_exec_op(mem, op);
762 if (op->data.nbytes > 1)
763 *status = ((u16)bytes[0] << 8) | bytes[1];
771 * spi_mem_poll_status() - Poll memory device status
772 * @mem: SPI memory device
773 * @op: the memory operation to execute
774 * @mask: status bitmask to ckeck
775 * @match: (status & mask) expected value
776 * @initial_delay_us: delay in us before starting to poll
777 * @polling_delay_us: time to sleep between reads in us
778 * @timeout_ms: timeout in milliseconds
780 * This function polls a status register and returns when
781 * (status & mask) == match or when the timeout has expired.
783 * Return: 0 in case of success, -ETIMEDOUT in case of error,
784 * -EOPNOTSUPP if not supported.
786 int spi_mem_poll_status(struct spi_mem *mem,
787 const struct spi_mem_op *op,
789 unsigned long initial_delay_us,
790 unsigned long polling_delay_us,
793 struct spi_controller *ctlr = mem->spi->controller;
794 int ret = -EOPNOTSUPP;
798 if (op->data.nbytes < 1 || op->data.nbytes > 2 ||
799 op->data.dir != SPI_MEM_DATA_IN)
802 if (ctlr->mem_ops && ctlr->mem_ops->poll_status) {
803 ret = spi_mem_access_start(mem);
807 ret = ctlr->mem_ops->poll_status(mem, op, mask, match,
808 initial_delay_us, polling_delay_us,
811 spi_mem_access_end(mem);
814 if (ret == -EOPNOTSUPP) {
815 if (!spi_mem_supports_op(mem, op))
818 if (initial_delay_us < 10)
819 udelay(initial_delay_us);
821 usleep_range((initial_delay_us >> 2) + 1,
824 ret = read_poll_timeout(spi_mem_read_status, read_status_ret,
825 (read_status_ret || ((status) & mask) == match),
826 polling_delay_us, timeout_ms * 1000, false, mem,
829 return read_status_ret;
834 EXPORT_SYMBOL_GPL(spi_mem_poll_status);
836 static int spi_mem_probe(struct spi_device *spi)
838 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
839 struct spi_controller *ctlr = spi->controller;
842 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
848 if (ctlr->mem_ops && ctlr->mem_ops->get_name)
849 mem->name = ctlr->mem_ops->get_name(mem);
851 mem->name = dev_name(&spi->dev);
853 if (IS_ERR_OR_NULL(mem->name))
854 return PTR_ERR_OR_ZERO(mem->name);
856 spi_set_drvdata(spi, mem);
858 return memdrv->probe(mem);
861 static void spi_mem_remove(struct spi_device *spi)
863 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
864 struct spi_mem *mem = spi_get_drvdata(spi);
870 static void spi_mem_shutdown(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);
875 if (memdrv->shutdown)
876 memdrv->shutdown(mem);
880 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
881 * @memdrv: the SPI memory driver to register
882 * @owner: the owner of this driver
884 * Registers a SPI memory driver.
886 * Return: 0 in case of success, a negative error core otherwise.
889 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
890 struct module *owner)
892 memdrv->spidrv.probe = spi_mem_probe;
893 memdrv->spidrv.remove = spi_mem_remove;
894 memdrv->spidrv.shutdown = spi_mem_shutdown;
896 return __spi_register_driver(owner, &memdrv->spidrv);
898 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
901 * spi_mem_driver_unregister() - Unregister a SPI memory driver
902 * @memdrv: the SPI memory driver to unregister
904 * Unregisters a SPI memory driver.
906 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
908 spi_unregister_driver(&memdrv->spidrv);
910 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);