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_dtr_supports_op(struct spi_mem *mem,
164 const struct spi_mem_op *op)
166 if (op->cmd.nbytes != 2)
169 return spi_mem_check_buswidth(mem, op);
171 EXPORT_SYMBOL_GPL(spi_mem_dtr_supports_op);
173 bool spi_mem_default_supports_op(struct spi_mem *mem,
174 const struct spi_mem_op *op)
176 if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr)
179 if (op->cmd.nbytes != 1)
182 return spi_mem_check_buswidth(mem, op);
184 EXPORT_SYMBOL_GPL(spi_mem_default_supports_op);
186 static bool spi_mem_buswidth_is_valid(u8 buswidth)
188 if (hweight8(buswidth) > 1 || buswidth > SPI_MEM_MAX_BUSWIDTH)
194 static int spi_mem_check_op(const struct spi_mem_op *op)
196 if (!op->cmd.buswidth || !op->cmd.nbytes)
199 if ((op->addr.nbytes && !op->addr.buswidth) ||
200 (op->dummy.nbytes && !op->dummy.buswidth) ||
201 (op->data.nbytes && !op->data.buswidth))
204 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) ||
205 !spi_mem_buswidth_is_valid(op->addr.buswidth) ||
206 !spi_mem_buswidth_is_valid(op->dummy.buswidth) ||
207 !spi_mem_buswidth_is_valid(op->data.buswidth))
213 static bool spi_mem_internal_supports_op(struct spi_mem *mem,
214 const struct spi_mem_op *op)
216 struct spi_controller *ctlr = mem->spi->controller;
218 if (ctlr->mem_ops && ctlr->mem_ops->supports_op)
219 return ctlr->mem_ops->supports_op(mem, op);
221 return spi_mem_default_supports_op(mem, op);
225 * spi_mem_supports_op() - Check if a memory device and the controller it is
226 * connected to support a specific memory operation
227 * @mem: the SPI memory
228 * @op: the memory operation to check
230 * Some controllers are only supporting Single or Dual IOs, others might only
231 * support specific opcodes, or it can even be that the controller and device
232 * both support Quad IOs but the hardware prevents you from using it because
233 * only 2 IO lines are connected.
235 * This function checks whether a specific operation is supported.
237 * Return: true if @op is supported, false otherwise.
239 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op)
241 if (spi_mem_check_op(op))
244 return spi_mem_internal_supports_op(mem, op);
246 EXPORT_SYMBOL_GPL(spi_mem_supports_op);
248 static int spi_mem_access_start(struct spi_mem *mem)
250 struct spi_controller *ctlr = mem->spi->controller;
253 * Flush the message queue before executing our SPI memory
254 * operation to prevent preemption of regular SPI transfers.
256 spi_flush_queue(ctlr);
258 if (ctlr->auto_runtime_pm) {
261 ret = pm_runtime_get_sync(ctlr->dev.parent);
263 pm_runtime_put_noidle(ctlr->dev.parent);
264 dev_err(&ctlr->dev, "Failed to power device: %d\n",
270 mutex_lock(&ctlr->bus_lock_mutex);
271 mutex_lock(&ctlr->io_mutex);
276 static void spi_mem_access_end(struct spi_mem *mem)
278 struct spi_controller *ctlr = mem->spi->controller;
280 mutex_unlock(&ctlr->io_mutex);
281 mutex_unlock(&ctlr->bus_lock_mutex);
283 if (ctlr->auto_runtime_pm)
284 pm_runtime_put(ctlr->dev.parent);
288 * spi_mem_exec_op() - Execute a memory operation
289 * @mem: the SPI memory
290 * @op: the memory operation to execute
292 * Executes a memory operation.
294 * This function first checks that @op is supported and then tries to execute
297 * Return: 0 in case of success, a negative error code otherwise.
299 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
301 unsigned int tmpbufsize, xferpos = 0, totalxferlen = 0;
302 struct spi_controller *ctlr = mem->spi->controller;
303 struct spi_transfer xfers[4] = { };
304 struct spi_message msg;
308 ret = spi_mem_check_op(op);
312 if (!spi_mem_internal_supports_op(mem, op))
315 if (ctlr->mem_ops && !mem->spi->cs_gpiod) {
316 ret = spi_mem_access_start(mem);
320 ret = ctlr->mem_ops->exec_op(mem, op);
322 spi_mem_access_end(mem);
325 * Some controllers only optimize specific paths (typically the
326 * read path) and expect the core to use the regular SPI
327 * interface in other cases.
329 if (!ret || ret != -ENOTSUPP)
333 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
336 * Allocate a buffer to transmit the CMD, ADDR cycles with kmalloc() so
337 * we're guaranteed that this buffer is DMA-able, as required by the
340 tmpbuf = kzalloc(tmpbufsize, GFP_KERNEL | GFP_DMA);
344 spi_message_init(&msg);
346 tmpbuf[0] = op->cmd.opcode;
347 xfers[xferpos].tx_buf = tmpbuf;
348 xfers[xferpos].len = op->cmd.nbytes;
349 xfers[xferpos].tx_nbits = op->cmd.buswidth;
350 spi_message_add_tail(&xfers[xferpos], &msg);
354 if (op->addr.nbytes) {
357 for (i = 0; i < op->addr.nbytes; i++)
358 tmpbuf[i + 1] = op->addr.val >>
359 (8 * (op->addr.nbytes - i - 1));
361 xfers[xferpos].tx_buf = tmpbuf + 1;
362 xfers[xferpos].len = op->addr.nbytes;
363 xfers[xferpos].tx_nbits = op->addr.buswidth;
364 spi_message_add_tail(&xfers[xferpos], &msg);
366 totalxferlen += op->addr.nbytes;
369 if (op->dummy.nbytes) {
370 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes);
371 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1;
372 xfers[xferpos].len = op->dummy.nbytes;
373 xfers[xferpos].tx_nbits = op->dummy.buswidth;
374 xfers[xferpos].dummy_data = 1;
375 spi_message_add_tail(&xfers[xferpos], &msg);
377 totalxferlen += op->dummy.nbytes;
380 if (op->data.nbytes) {
381 if (op->data.dir == SPI_MEM_DATA_IN) {
382 xfers[xferpos].rx_buf = op->data.buf.in;
383 xfers[xferpos].rx_nbits = op->data.buswidth;
385 xfers[xferpos].tx_buf = op->data.buf.out;
386 xfers[xferpos].tx_nbits = op->data.buswidth;
389 xfers[xferpos].len = op->data.nbytes;
390 spi_message_add_tail(&xfers[xferpos], &msg);
392 totalxferlen += op->data.nbytes;
395 ret = spi_sync(mem->spi, &msg);
402 if (msg.actual_length != totalxferlen)
407 EXPORT_SYMBOL_GPL(spi_mem_exec_op);
410 * spi_mem_get_name() - Return the SPI mem device name to be used by the
411 * upper layer if necessary
412 * @mem: the SPI memory
414 * This function allows SPI mem users to retrieve the SPI mem device name.
415 * It is useful if the upper layer needs to expose a custom name for
416 * compatibility reasons.
418 * Return: a string containing the name of the memory device to be used
419 * by the SPI mem user
421 const char *spi_mem_get_name(struct spi_mem *mem)
425 EXPORT_SYMBOL_GPL(spi_mem_get_name);
428 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
429 * match controller limitations
430 * @mem: the SPI memory
431 * @op: the operation to adjust
433 * Some controllers have FIFO limitations and must split a data transfer
434 * operation into multiple ones, others require a specific alignment for
435 * optimized accesses. This function allows SPI mem drivers to split a single
436 * operation into multiple sub-operations when required.
438 * Return: a negative error code if the controller can't properly adjust @op,
439 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
440 * can't be handled in a single step.
442 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
444 struct spi_controller *ctlr = mem->spi->controller;
447 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size)
448 return ctlr->mem_ops->adjust_op_size(mem, op);
450 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) {
451 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
453 if (len > spi_max_transfer_size(mem->spi))
456 op->data.nbytes = min3((size_t)op->data.nbytes,
457 spi_max_transfer_size(mem->spi),
458 spi_max_message_size(mem->spi) -
460 if (!op->data.nbytes)
466 EXPORT_SYMBOL_GPL(spi_mem_adjust_op_size);
468 static ssize_t spi_mem_no_dirmap_read(struct spi_mem_dirmap_desc *desc,
469 u64 offs, size_t len, void *buf)
471 struct spi_mem_op op = desc->info.op_tmpl;
474 op.addr.val = desc->info.offset + offs;
475 op.data.buf.in = buf;
476 op.data.nbytes = len;
477 ret = spi_mem_adjust_op_size(desc->mem, &op);
481 ret = spi_mem_exec_op(desc->mem, &op);
485 return op.data.nbytes;
488 static ssize_t spi_mem_no_dirmap_write(struct spi_mem_dirmap_desc *desc,
489 u64 offs, size_t len, const void *buf)
491 struct spi_mem_op op = desc->info.op_tmpl;
494 op.addr.val = desc->info.offset + offs;
495 op.data.buf.out = buf;
496 op.data.nbytes = len;
497 ret = spi_mem_adjust_op_size(desc->mem, &op);
501 ret = spi_mem_exec_op(desc->mem, &op);
505 return op.data.nbytes;
509 * spi_mem_dirmap_create() - Create a direct mapping descriptor
510 * @mem: SPI mem device this direct mapping should be created for
511 * @info: direct mapping information
513 * This function is creating a direct mapping descriptor which can then be used
514 * to access the memory using spi_mem_dirmap_read() or spi_mem_dirmap_write().
515 * If the SPI controller driver does not support direct mapping, this function
516 * falls back to an implementation using spi_mem_exec_op(), so that the caller
517 * doesn't have to bother implementing a fallback on his own.
519 * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
521 struct spi_mem_dirmap_desc *
522 spi_mem_dirmap_create(struct spi_mem *mem,
523 const struct spi_mem_dirmap_info *info)
525 struct spi_controller *ctlr = mem->spi->controller;
526 struct spi_mem_dirmap_desc *desc;
529 /* Make sure the number of address cycles is between 1 and 8 bytes. */
530 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8)
531 return ERR_PTR(-EINVAL);
533 /* data.dir should either be SPI_MEM_DATA_IN or SPI_MEM_DATA_OUT. */
534 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA)
535 return ERR_PTR(-EINVAL);
537 desc = kzalloc(sizeof(*desc), GFP_KERNEL);
539 return ERR_PTR(-ENOMEM);
543 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create)
544 ret = ctlr->mem_ops->dirmap_create(desc);
547 desc->nodirmap = true;
548 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl))
561 EXPORT_SYMBOL_GPL(spi_mem_dirmap_create);
564 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
565 * @desc: the direct mapping descriptor to destroy
567 * This function destroys a direct mapping descriptor previously created by
568 * spi_mem_dirmap_create().
570 void spi_mem_dirmap_destroy(struct spi_mem_dirmap_desc *desc)
572 struct spi_controller *ctlr = desc->mem->spi->controller;
574 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy)
575 ctlr->mem_ops->dirmap_destroy(desc);
579 EXPORT_SYMBOL_GPL(spi_mem_dirmap_destroy);
581 static void devm_spi_mem_dirmap_release(struct device *dev, void *res)
583 struct spi_mem_dirmap_desc *desc = *(struct spi_mem_dirmap_desc **)res;
585 spi_mem_dirmap_destroy(desc);
589 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
591 * @dev: device the dirmap desc will be attached to
592 * @mem: SPI mem device this direct mapping should be created for
593 * @info: direct mapping information
595 * devm_ variant of the spi_mem_dirmap_create() function. See
596 * spi_mem_dirmap_create() for more details.
598 * Return: a valid pointer in case of success, and ERR_PTR() otherwise.
600 struct spi_mem_dirmap_desc *
601 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem,
602 const struct spi_mem_dirmap_info *info)
604 struct spi_mem_dirmap_desc **ptr, *desc;
606 ptr = devres_alloc(devm_spi_mem_dirmap_release, sizeof(*ptr),
609 return ERR_PTR(-ENOMEM);
611 desc = spi_mem_dirmap_create(mem, info);
616 devres_add(dev, ptr);
621 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_create);
623 static int devm_spi_mem_dirmap_match(struct device *dev, void *res, void *data)
625 struct spi_mem_dirmap_desc **ptr = res;
627 if (WARN_ON(!ptr || !*ptr))
634 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
636 * @dev: device the dirmap desc is attached to
637 * @desc: the direct mapping descriptor to destroy
639 * devm_ variant of the spi_mem_dirmap_destroy() function. See
640 * spi_mem_dirmap_destroy() for more details.
642 void devm_spi_mem_dirmap_destroy(struct device *dev,
643 struct spi_mem_dirmap_desc *desc)
645 devres_release(dev, devm_spi_mem_dirmap_release,
646 devm_spi_mem_dirmap_match, desc);
648 EXPORT_SYMBOL_GPL(devm_spi_mem_dirmap_destroy);
651 * spi_mem_dirmap_read() - Read data through a direct mapping
652 * @desc: direct mapping descriptor
653 * @offs: offset to start reading from. Note that this is not an absolute
654 * offset, but the offset within the direct mapping which already has
656 * @len: length in bytes
657 * @buf: destination buffer. This buffer must be DMA-able
659 * This function reads data from a memory device using a direct mapping
660 * previously instantiated with spi_mem_dirmap_create().
662 * Return: the amount of data read from the memory device or a negative error
663 * code. Note that the returned size might be smaller than @len, and the caller
664 * is responsible for calling spi_mem_dirmap_read() again when that happens.
666 ssize_t spi_mem_dirmap_read(struct spi_mem_dirmap_desc *desc,
667 u64 offs, size_t len, void *buf)
669 struct spi_controller *ctlr = desc->mem->spi->controller;
672 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN)
678 if (desc->nodirmap) {
679 ret = spi_mem_no_dirmap_read(desc, offs, len, buf);
680 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) {
681 ret = spi_mem_access_start(desc->mem);
685 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf);
687 spi_mem_access_end(desc->mem);
694 EXPORT_SYMBOL_GPL(spi_mem_dirmap_read);
697 * spi_mem_dirmap_write() - Write data through a direct mapping
698 * @desc: direct mapping descriptor
699 * @offs: offset to start writing from. Note that this is not an absolute
700 * offset, but the offset within the direct mapping which already has
702 * @len: length in bytes
703 * @buf: source buffer. This buffer must be DMA-able
705 * This function writes data to a memory device using a direct mapping
706 * previously instantiated with spi_mem_dirmap_create().
708 * Return: the amount of data written to the memory device or a negative error
709 * code. Note that the returned size might be smaller than @len, and the caller
710 * is responsible for calling spi_mem_dirmap_write() again when that happens.
712 ssize_t spi_mem_dirmap_write(struct spi_mem_dirmap_desc *desc,
713 u64 offs, size_t len, const void *buf)
715 struct spi_controller *ctlr = desc->mem->spi->controller;
718 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT)
724 if (desc->nodirmap) {
725 ret = spi_mem_no_dirmap_write(desc, offs, len, buf);
726 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) {
727 ret = spi_mem_access_start(desc->mem);
731 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf);
733 spi_mem_access_end(desc->mem);
740 EXPORT_SYMBOL_GPL(spi_mem_dirmap_write);
742 static inline struct spi_mem_driver *to_spi_mem_drv(struct device_driver *drv)
744 return container_of(drv, struct spi_mem_driver, spidrv.driver);
747 static int spi_mem_read_status(struct spi_mem *mem,
748 const struct spi_mem_op *op,
751 const u8 *bytes = (u8 *)op->data.buf.in;
754 ret = spi_mem_exec_op(mem, op);
758 if (op->data.nbytes > 1)
759 *status = ((u16)bytes[0] << 8) | bytes[1];
767 * spi_mem_poll_status() - Poll memory device status
768 * @mem: SPI memory device
769 * @op: the memory operation to execute
770 * @mask: status bitmask to ckeck
771 * @match: (status & mask) expected value
772 * @initial_delay_us: delay in us before starting to poll
773 * @polling_delay_us: time to sleep between reads in us
774 * @timeout_ms: timeout in milliseconds
776 * This function polls a status register and returns when
777 * (status & mask) == match or when the timeout has expired.
779 * Return: 0 in case of success, -ETIMEDOUT in case of error,
780 * -EOPNOTSUPP if not supported.
782 int spi_mem_poll_status(struct spi_mem *mem,
783 const struct spi_mem_op *op,
785 unsigned long initial_delay_us,
786 unsigned long polling_delay_us,
789 struct spi_controller *ctlr = mem->spi->controller;
790 int ret = -EOPNOTSUPP;
794 if (op->data.nbytes < 1 || op->data.nbytes > 2 ||
795 op->data.dir != SPI_MEM_DATA_IN)
798 if (ctlr->mem_ops && ctlr->mem_ops->poll_status) {
799 ret = spi_mem_access_start(mem);
803 ret = ctlr->mem_ops->poll_status(mem, op, mask, match,
804 initial_delay_us, polling_delay_us,
807 spi_mem_access_end(mem);
810 if (ret == -EOPNOTSUPP) {
811 if (!spi_mem_supports_op(mem, op))
814 if (initial_delay_us < 10)
815 udelay(initial_delay_us);
817 usleep_range((initial_delay_us >> 2) + 1,
820 ret = read_poll_timeout(spi_mem_read_status, read_status_ret,
821 (read_status_ret || ((status) & mask) == match),
822 polling_delay_us, timeout_ms * 1000, false, mem,
825 return read_status_ret;
830 EXPORT_SYMBOL_GPL(spi_mem_poll_status);
832 static int spi_mem_probe(struct spi_device *spi)
834 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
835 struct spi_controller *ctlr = spi->controller;
838 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL);
844 if (ctlr->mem_ops && ctlr->mem_ops->get_name)
845 mem->name = ctlr->mem_ops->get_name(mem);
847 mem->name = dev_name(&spi->dev);
849 if (IS_ERR_OR_NULL(mem->name))
850 return PTR_ERR_OR_ZERO(mem->name);
852 spi_set_drvdata(spi, mem);
854 return memdrv->probe(mem);
857 static int spi_mem_remove(struct spi_device *spi)
859 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
860 struct spi_mem *mem = spi_get_drvdata(spi);
863 return memdrv->remove(mem);
868 static void spi_mem_shutdown(struct spi_device *spi)
870 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver);
871 struct spi_mem *mem = spi_get_drvdata(spi);
873 if (memdrv->shutdown)
874 memdrv->shutdown(mem);
878 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
879 * @memdrv: the SPI memory driver to register
880 * @owner: the owner of this driver
882 * Registers a SPI memory driver.
884 * Return: 0 in case of success, a negative error core otherwise.
887 int spi_mem_driver_register_with_owner(struct spi_mem_driver *memdrv,
888 struct module *owner)
890 memdrv->spidrv.probe = spi_mem_probe;
891 memdrv->spidrv.remove = spi_mem_remove;
892 memdrv->spidrv.shutdown = spi_mem_shutdown;
894 return __spi_register_driver(owner, &memdrv->spidrv);
896 EXPORT_SYMBOL_GPL(spi_mem_driver_register_with_owner);
899 * spi_mem_driver_unregister() - Unregister a SPI memory driver
900 * @memdrv: the SPI memory driver to unregister
902 * Unregisters a SPI memory driver.
904 void spi_mem_driver_unregister(struct spi_mem_driver *memdrv)
906 spi_unregister_driver(&memdrv->spidrv);
908 EXPORT_SYMBOL_GPL(spi_mem_driver_unregister);