Lines Matching +full:dma +full:- +full:mem
1 // SPDX-License-Identifier: GPL-2.0+
11 #include <linux/spi/spi-mem.h>
18 * spi_controller_dma_map_mem_op_data() - DMA-map the buffer attached to a
22 * @sgt: a pointer to a non-initialized sg_table that will be filled by this
25 * Some controllers might want to do DMA on the data buffer embedded in @op.
26 * This helper prepares everything for you and provides a ready-to-use
30 * op->data.buf.{in,out} is DMA-able before calling this function.
40 if (!op->data.nbytes) in spi_controller_dma_map_mem_op_data()
41 return -EINVAL; in spi_controller_dma_map_mem_op_data()
43 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx) in spi_controller_dma_map_mem_op_data()
44 dmadev = ctlr->dma_tx->device->dev; in spi_controller_dma_map_mem_op_data()
45 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx) in spi_controller_dma_map_mem_op_data()
46 dmadev = ctlr->dma_rx->device->dev; in spi_controller_dma_map_mem_op_data()
48 dmadev = ctlr->dev.parent; in spi_controller_dma_map_mem_op_data()
51 return -EINVAL; in spi_controller_dma_map_mem_op_data()
53 return spi_map_buf(ctlr, dmadev, sgt, op->data.buf.in, op->data.nbytes, in spi_controller_dma_map_mem_op_data()
54 op->data.dir == SPI_MEM_DATA_IN ? in spi_controller_dma_map_mem_op_data()
60 * spi_controller_dma_unmap_mem_op_data() - DMA-unmap the buffer attached to a
67 * Some controllers might want to do DMA on the data buffer embedded in @op.
69 * op->data.buf.{in,out} buffer again.
74 * This function should be called after the DMA operation has finished and is
86 if (!op->data.nbytes) in spi_controller_dma_unmap_mem_op_data()
89 if (op->data.dir == SPI_MEM_DATA_OUT && ctlr->dma_tx) in spi_controller_dma_unmap_mem_op_data()
90 dmadev = ctlr->dma_tx->device->dev; in spi_controller_dma_unmap_mem_op_data()
91 else if (op->data.dir == SPI_MEM_DATA_IN && ctlr->dma_rx) in spi_controller_dma_unmap_mem_op_data()
92 dmadev = ctlr->dma_rx->device->dev; in spi_controller_dma_unmap_mem_op_data()
94 dmadev = ctlr->dev.parent; in spi_controller_dma_unmap_mem_op_data()
97 op->data.dir == SPI_MEM_DATA_IN ? in spi_controller_dma_unmap_mem_op_data()
102 static int spi_check_buswidth_req(struct spi_mem *mem, u8 buswidth, bool tx) in spi_check_buswidth_req() argument
104 u32 mode = mem->spi->mode; in spi_check_buswidth_req()
137 return -ENOTSUPP; in spi_check_buswidth_req()
140 bool spi_mem_default_supports_op(struct spi_mem *mem, in spi_mem_default_supports_op() argument
143 if (spi_check_buswidth_req(mem, op->cmd.buswidth, true)) in spi_mem_default_supports_op()
146 if (op->addr.nbytes && in spi_mem_default_supports_op()
147 spi_check_buswidth_req(mem, op->addr.buswidth, true)) in spi_mem_default_supports_op()
150 if (op->dummy.nbytes && in spi_mem_default_supports_op()
151 spi_check_buswidth_req(mem, op->dummy.buswidth, true)) in spi_mem_default_supports_op()
154 if (op->data.dir != SPI_MEM_NO_DATA && in spi_mem_default_supports_op()
155 spi_check_buswidth_req(mem, op->data.buswidth, in spi_mem_default_supports_op()
156 op->data.dir == SPI_MEM_DATA_OUT)) in spi_mem_default_supports_op()
159 if (op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr) in spi_mem_default_supports_op()
162 if (op->cmd.nbytes != 1) in spi_mem_default_supports_op()
179 if (!op->cmd.buswidth || !op->cmd.nbytes) in spi_mem_check_op()
180 return -EINVAL; in spi_mem_check_op()
182 if ((op->addr.nbytes && !op->addr.buswidth) || in spi_mem_check_op()
183 (op->dummy.nbytes && !op->dummy.buswidth) || in spi_mem_check_op()
184 (op->data.nbytes && !op->data.buswidth)) in spi_mem_check_op()
185 return -EINVAL; in spi_mem_check_op()
187 if (!spi_mem_buswidth_is_valid(op->cmd.buswidth) || in spi_mem_check_op()
188 !spi_mem_buswidth_is_valid(op->addr.buswidth) || in spi_mem_check_op()
189 !spi_mem_buswidth_is_valid(op->dummy.buswidth) || in spi_mem_check_op()
190 !spi_mem_buswidth_is_valid(op->data.buswidth)) in spi_mem_check_op()
191 return -EINVAL; in spi_mem_check_op()
196 static bool spi_mem_internal_supports_op(struct spi_mem *mem, in spi_mem_internal_supports_op() argument
199 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_internal_supports_op()
201 if (ctlr->mem_ops && ctlr->mem_ops->supports_op) in spi_mem_internal_supports_op()
202 return ctlr->mem_ops->supports_op(mem, op); in spi_mem_internal_supports_op()
204 return spi_mem_default_supports_op(mem, op); in spi_mem_internal_supports_op()
208 * spi_mem_supports_op() - Check if a memory device and the controller it is
210 * @mem: the SPI memory
222 bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) in spi_mem_supports_op() argument
227 return spi_mem_internal_supports_op(mem, op); in spi_mem_supports_op()
231 static int spi_mem_access_start(struct spi_mem *mem) in spi_mem_access_start() argument
233 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_access_start()
241 if (ctlr->auto_runtime_pm) { in spi_mem_access_start()
244 ret = pm_runtime_get_sync(ctlr->dev.parent); in spi_mem_access_start()
246 dev_err(&ctlr->dev, "Failed to power device: %d\n", in spi_mem_access_start()
252 mutex_lock(&ctlr->bus_lock_mutex); in spi_mem_access_start()
253 mutex_lock(&ctlr->io_mutex); in spi_mem_access_start()
258 static void spi_mem_access_end(struct spi_mem *mem) in spi_mem_access_end() argument
260 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_access_end()
262 mutex_unlock(&ctlr->io_mutex); in spi_mem_access_end()
263 mutex_unlock(&ctlr->bus_lock_mutex); in spi_mem_access_end()
265 if (ctlr->auto_runtime_pm) in spi_mem_access_end()
266 pm_runtime_put(ctlr->dev.parent); in spi_mem_access_end()
270 * spi_mem_exec_op() - Execute a memory operation
271 * @mem: the SPI memory
281 int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) in spi_mem_exec_op() argument
284 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_exec_op()
294 if (!spi_mem_internal_supports_op(mem, op)) in spi_mem_exec_op()
295 return -ENOTSUPP; in spi_mem_exec_op()
297 if (ctlr->mem_ops && !mem->spi->cs_gpiod) { in spi_mem_exec_op()
298 ret = spi_mem_access_start(mem); in spi_mem_exec_op()
302 ret = ctlr->mem_ops->exec_op(mem, op); in spi_mem_exec_op()
304 spi_mem_access_end(mem); in spi_mem_exec_op()
311 if (!ret || ret != -ENOTSUPP) in spi_mem_exec_op()
315 tmpbufsize = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; in spi_mem_exec_op()
319 * we're guaranteed that this buffer is DMA-able, as required by the in spi_mem_exec_op()
324 return -ENOMEM; in spi_mem_exec_op()
328 tmpbuf[0] = op->cmd.opcode; in spi_mem_exec_op()
330 xfers[xferpos].len = op->cmd.nbytes; in spi_mem_exec_op()
331 xfers[xferpos].tx_nbits = op->cmd.buswidth; in spi_mem_exec_op()
336 if (op->addr.nbytes) { in spi_mem_exec_op()
339 for (i = 0; i < op->addr.nbytes; i++) in spi_mem_exec_op()
340 tmpbuf[i + 1] = op->addr.val >> in spi_mem_exec_op()
341 (8 * (op->addr.nbytes - i - 1)); in spi_mem_exec_op()
344 xfers[xferpos].len = op->addr.nbytes; in spi_mem_exec_op()
345 xfers[xferpos].tx_nbits = op->addr.buswidth; in spi_mem_exec_op()
348 totalxferlen += op->addr.nbytes; in spi_mem_exec_op()
351 if (op->dummy.nbytes) { in spi_mem_exec_op()
352 memset(tmpbuf + op->addr.nbytes + 1, 0xff, op->dummy.nbytes); in spi_mem_exec_op()
353 xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1; in spi_mem_exec_op()
354 xfers[xferpos].len = op->dummy.nbytes; in spi_mem_exec_op()
355 xfers[xferpos].tx_nbits = op->dummy.buswidth; in spi_mem_exec_op()
358 totalxferlen += op->dummy.nbytes; in spi_mem_exec_op()
361 if (op->data.nbytes) { in spi_mem_exec_op()
362 if (op->data.dir == SPI_MEM_DATA_IN) { in spi_mem_exec_op()
363 xfers[xferpos].rx_buf = op->data.buf.in; in spi_mem_exec_op()
364 xfers[xferpos].rx_nbits = op->data.buswidth; in spi_mem_exec_op()
366 xfers[xferpos].tx_buf = op->data.buf.out; in spi_mem_exec_op()
367 xfers[xferpos].tx_nbits = op->data.buswidth; in spi_mem_exec_op()
370 xfers[xferpos].len = op->data.nbytes; in spi_mem_exec_op()
373 totalxferlen += op->data.nbytes; in spi_mem_exec_op()
376 ret = spi_sync(mem->spi, &msg); in spi_mem_exec_op()
384 return -EIO; in spi_mem_exec_op()
391 * spi_mem_get_name() - Return the SPI mem device name to be used by the
393 * @mem: the SPI memory
395 * This function allows SPI mem users to retrieve the SPI mem device name.
400 * by the SPI mem user
402 const char *spi_mem_get_name(struct spi_mem *mem) in spi_mem_get_name() argument
404 return mem->name; in spi_mem_get_name()
409 * spi_mem_adjust_op_size() - Adjust the data size of a SPI mem operation to
411 * @mem: the SPI memory
416 * optimized accesses. This function allows SPI mem drivers to split a single
417 * operation into multiple sub-operations when required.
420 * 0 otherwise. Note that @op->data.nbytes will be updated if @op
423 int spi_mem_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op) in spi_mem_adjust_op_size() argument
425 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_adjust_op_size()
428 if (ctlr->mem_ops && ctlr->mem_ops->adjust_op_size) in spi_mem_adjust_op_size()
429 return ctlr->mem_ops->adjust_op_size(mem, op); in spi_mem_adjust_op_size()
431 if (!ctlr->mem_ops || !ctlr->mem_ops->exec_op) { in spi_mem_adjust_op_size()
432 len = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes; in spi_mem_adjust_op_size()
434 if (len > spi_max_transfer_size(mem->spi)) in spi_mem_adjust_op_size()
435 return -EINVAL; in spi_mem_adjust_op_size()
437 op->data.nbytes = min3((size_t)op->data.nbytes, in spi_mem_adjust_op_size()
438 spi_max_transfer_size(mem->spi), in spi_mem_adjust_op_size()
439 spi_max_message_size(mem->spi) - in spi_mem_adjust_op_size()
441 if (!op->data.nbytes) in spi_mem_adjust_op_size()
442 return -EINVAL; in spi_mem_adjust_op_size()
452 struct spi_mem_op op = desc->info.op_tmpl; in spi_mem_no_dirmap_read()
455 op.addr.val = desc->info.offset + offs; in spi_mem_no_dirmap_read()
458 ret = spi_mem_adjust_op_size(desc->mem, &op); in spi_mem_no_dirmap_read()
462 ret = spi_mem_exec_op(desc->mem, &op); in spi_mem_no_dirmap_read()
472 struct spi_mem_op op = desc->info.op_tmpl; in spi_mem_no_dirmap_write()
475 op.addr.val = desc->info.offset + offs; in spi_mem_no_dirmap_write()
478 ret = spi_mem_adjust_op_size(desc->mem, &op); in spi_mem_no_dirmap_write()
482 ret = spi_mem_exec_op(desc->mem, &op); in spi_mem_no_dirmap_write()
490 * spi_mem_dirmap_create() - Create a direct mapping descriptor
491 * @mem: SPI mem device this direct mapping should be created for
503 spi_mem_dirmap_create(struct spi_mem *mem, in spi_mem_dirmap_create() argument
506 struct spi_controller *ctlr = mem->spi->controller; in spi_mem_dirmap_create()
508 int ret = -ENOTSUPP; in spi_mem_dirmap_create()
511 if (!info->op_tmpl.addr.nbytes || info->op_tmpl.addr.nbytes > 8) in spi_mem_dirmap_create()
512 return ERR_PTR(-EINVAL); in spi_mem_dirmap_create()
515 if (info->op_tmpl.data.dir == SPI_MEM_NO_DATA) in spi_mem_dirmap_create()
516 return ERR_PTR(-EINVAL); in spi_mem_dirmap_create()
520 return ERR_PTR(-ENOMEM); in spi_mem_dirmap_create()
522 desc->mem = mem; in spi_mem_dirmap_create()
523 desc->info = *info; in spi_mem_dirmap_create()
524 if (ctlr->mem_ops && ctlr->mem_ops->dirmap_create) in spi_mem_dirmap_create()
525 ret = ctlr->mem_ops->dirmap_create(desc); in spi_mem_dirmap_create()
528 desc->nodirmap = true; in spi_mem_dirmap_create()
529 if (!spi_mem_supports_op(desc->mem, &desc->info.op_tmpl)) in spi_mem_dirmap_create()
530 ret = -ENOTSUPP; in spi_mem_dirmap_create()
545 * spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor
553 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_destroy()
555 if (!desc->nodirmap && ctlr->mem_ops && ctlr->mem_ops->dirmap_destroy) in spi_mem_dirmap_destroy()
556 ctlr->mem_ops->dirmap_destroy(desc); in spi_mem_dirmap_destroy()
570 * devm_spi_mem_dirmap_create() - Create a direct mapping descriptor and attach
573 * @mem: SPI mem device this direct mapping should be created for
582 devm_spi_mem_dirmap_create(struct device *dev, struct spi_mem *mem, in devm_spi_mem_dirmap_create() argument
590 return ERR_PTR(-ENOMEM); in devm_spi_mem_dirmap_create()
592 desc = spi_mem_dirmap_create(mem, info); in devm_spi_mem_dirmap_create()
615 * devm_spi_mem_dirmap_destroy() - Destroy a direct mapping descriptor attached
632 * spi_mem_dirmap_read() - Read data through a direct mapping
638 * @buf: destination buffer. This buffer must be DMA-able
650 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_read()
653 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_IN) in spi_mem_dirmap_read()
654 return -EINVAL; in spi_mem_dirmap_read()
659 if (desc->nodirmap) { in spi_mem_dirmap_read()
661 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_read) { in spi_mem_dirmap_read()
662 ret = spi_mem_access_start(desc->mem); in spi_mem_dirmap_read()
666 ret = ctlr->mem_ops->dirmap_read(desc, offs, len, buf); in spi_mem_dirmap_read()
668 spi_mem_access_end(desc->mem); in spi_mem_dirmap_read()
670 ret = -ENOTSUPP; in spi_mem_dirmap_read()
678 * spi_mem_dirmap_write() - Write data through a direct mapping
684 * @buf: source buffer. This buffer must be DMA-able
696 struct spi_controller *ctlr = desc->mem->spi->controller; in spi_mem_dirmap_write()
699 if (desc->info.op_tmpl.data.dir != SPI_MEM_DATA_OUT) in spi_mem_dirmap_write()
700 return -EINVAL; in spi_mem_dirmap_write()
705 if (desc->nodirmap) { in spi_mem_dirmap_write()
707 } else if (ctlr->mem_ops && ctlr->mem_ops->dirmap_write) { in spi_mem_dirmap_write()
708 ret = spi_mem_access_start(desc->mem); in spi_mem_dirmap_write()
712 ret = ctlr->mem_ops->dirmap_write(desc, offs, len, buf); in spi_mem_dirmap_write()
714 spi_mem_access_end(desc->mem); in spi_mem_dirmap_write()
716 ret = -ENOTSUPP; in spi_mem_dirmap_write()
730 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_probe()
731 struct spi_controller *ctlr = spi->controller; in spi_mem_probe()
732 struct spi_mem *mem; in spi_mem_probe() local
734 mem = devm_kzalloc(&spi->dev, sizeof(*mem), GFP_KERNEL); in spi_mem_probe()
735 if (!mem) in spi_mem_probe()
736 return -ENOMEM; in spi_mem_probe()
738 mem->spi = spi; in spi_mem_probe()
740 if (ctlr->mem_ops && ctlr->mem_ops->get_name) in spi_mem_probe()
741 mem->name = ctlr->mem_ops->get_name(mem); in spi_mem_probe()
743 mem->name = dev_name(&spi->dev); in spi_mem_probe()
745 if (IS_ERR_OR_NULL(mem->name)) in spi_mem_probe()
746 return PTR_ERR(mem->name); in spi_mem_probe()
748 spi_set_drvdata(spi, mem); in spi_mem_probe()
750 return memdrv->probe(mem); in spi_mem_probe()
755 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_remove()
756 struct spi_mem *mem = spi_get_drvdata(spi); in spi_mem_remove() local
758 if (memdrv->remove) in spi_mem_remove()
759 return memdrv->remove(mem); in spi_mem_remove()
766 struct spi_mem_driver *memdrv = to_spi_mem_drv(spi->dev.driver); in spi_mem_shutdown()
767 struct spi_mem *mem = spi_get_drvdata(spi); in spi_mem_shutdown() local
769 if (memdrv->shutdown) in spi_mem_shutdown()
770 memdrv->shutdown(mem); in spi_mem_shutdown()
774 * spi_mem_driver_register_with_owner() - Register a SPI memory driver
786 memdrv->spidrv.probe = spi_mem_probe; in spi_mem_driver_register_with_owner()
787 memdrv->spidrv.remove = spi_mem_remove; in spi_mem_driver_register_with_owner()
788 memdrv->spidrv.shutdown = spi_mem_shutdown; in spi_mem_driver_register_with_owner()
790 return __spi_register_driver(owner, &memdrv->spidrv); in spi_mem_driver_register_with_owner()
795 * spi_mem_driver_unregister_with_owner() - Unregister a SPI memory driver
802 spi_unregister_driver(&memdrv->spidrv); in spi_mem_driver_unregister()