Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp306193imm; Fri, 21 Sep 2018 00:06:40 -0700 (PDT) X-Google-Smtp-Source: ANB0VdYf+ChB1QHEEp6OSAu07Xae4oA17b8GpCS4VGFgc/wJ9oz7S/3+kg+QuYsC0ts4TyPkJu9f X-Received: by 2002:a17:902:d201:: with SMTP id t1-v6mr7172660ply.63.1537513600685; Fri, 21 Sep 2018 00:06:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537513600; cv=none; d=google.com; s=arc-20160816; b=Ey6xtSz5m/WzsosCSLfyqarDuD3xJKrB7Iz3fHTsve6an++1evg5MPZddrbcp4Qv03 lGU6Ny3PeuoRiyQNvTPmYunvwmz1gYM/7nlol2IujdR+pjbEmDLYQZp9pvQFUIl9QGER Vy1NkbGZcaGkVpjrSizUW5LovH+HmFIoYlp9liDeCbBkAsAPJ+B4CQ14DtsMT8HLN/2Q l+BOZBwrqS2tH9TN8dq1wq9L6r8FJYGs7tV7YWMecCWrzY5AEE/4Nl7MliHRGa7s2FxQ HMFR39Jhjb2u29jIUHmJm+gqCqrtk1ByAdeFyplvoVGFj7gFwpthoayoDX/tW1kfIpVi MwFA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:date:subject:cc:to:from; bh=hVdBR66bcKoV5bxS7juXPmOVjcL4f4qj8kc4JtNkAU4=; b=EiCtRrFxzWzCWfj0CVQzYrmfovIvvC43mCXb61K3kNtivmUQ6bVV9/WnCBSKgQl290 0ORMIlnPqheG9YU3IZ4GFdWecgx5VSeFYv8CkhL1VgtDPo4Z579K+mEmTPiJ94mTwxhY ZqteQUDm3Ot//4fBz2u5rMnJHHpVWVipSiL10yMvFJvbZOTTqgEEiVKUlYzOVK7YjYeY 026m47Zoye8KXpKYJz4WzywmfF4t9r4q1N2I0wUvQb5qYo+ygaBJ1Q0Jl1d+1iNKwsq1 5tEPChAkowaA3kOjVSL/WNK6Be91TbOEzuSY0luA98K8Vq/MNR2G3oPCLWu2NWaKxVhm TfNg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p17-v6si4636033pgj.416.2018.09.21.00.06.24; Fri, 21 Sep 2018 00:06:40 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=nxp.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2389354AbeIUMxq (ORCPT + 99 others); Fri, 21 Sep 2018 08:53:46 -0400 Received: from inva021.nxp.com ([92.121.34.21]:33100 "EHLO inva021.nxp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725887AbeIUMxq (ORCPT ); Fri, 21 Sep 2018 08:53:46 -0400 Received: from inva021.nxp.com (localhost [127.0.0.1]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 196CB200247; Fri, 21 Sep 2018 09:06:13 +0200 (CEST) Received: from invc005.ap-rdc01.nxp.com (invc005.ap-rdc01.nxp.com [165.114.16.14]) by inva021.eu-rdc02.nxp.com (Postfix) with ESMTP id 0368B200017; Fri, 21 Sep 2018 09:06:10 +0200 (CEST) Received: from mega.ap.freescale.net (mega.ap.freescale.net [10.192.208.232]) by invc005.ap-rdc01.nxp.com (Postfix) with ESMTP id C1847402DD; Fri, 21 Sep 2018 15:06:05 +0800 (SGT) From: Chuanhua Han To: broonie@kernel.org Cc: linux-spi@vger.kernel.org, linux-kernel@vger.kernel.org, eha@deif.com, boris.brezillon@bootlin.com, Chuanhua Han Subject: [PATCH 1/2] spi: spi-mem: Add the spi_set_xfer_bpw function Date: Fri, 21 Sep 2018 15:06:26 +0800 Message-Id: <20180921070628.35153-1-chuanhua.han@nxp.com> X-Mailer: git-send-email 2.17.1 X-Virus-Scanned: ClamAV using ClamSMTP Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Before we add this spi_transfer to the spi_message chain table, we need bits_per_word_mask based on spi_control to set the bits_per_word of this spi_transfer. Signed-off-by: Chuanhua Han --- drivers/spi/spi-mem.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/drivers/spi/spi-mem.c b/drivers/spi/spi-mem.c index eb72dba71d83..717e711c0952 100644 --- a/drivers/spi/spi-mem.c +++ b/drivers/spi/spi-mem.c @@ -175,6 +175,41 @@ bool spi_mem_supports_op(struct spi_mem *mem, const struct spi_mem_op *op) } EXPORT_SYMBOL_GPL(spi_mem_supports_op); +/** + * spi_set_xfer_bpw() - Set the bits_per_word for each transfer based on + * the bits_per_word_mask of the spi controller + * @ctrl: the spi controller + * @xfer: the spi transfer + * + * This function sets the bits_per_word for each transfer based on the spi + * controller's bits_per_word_mask to improve the efficiency of spi transport. + * + * Return: 0 in case of success, a negative error code otherwise. + */ +int spi_set_xfer_bpw(struct spi_controller *ctlr, struct spi_transfer *xfer) +{ + if (!ctlr || !xfer) { + dev_err(&ctlr->dev, + "Fail to set bits_per_word for spi transfer\n"); + return -EINVAL; + } + + if (ctlr->bits_per_word_mask) { + if (!(xfer->len % 4)) { + if (ctlr->bits_per_word_mask & SPI_BPW_MASK(32)) + xfer->bits_per_word = 32; + } else if (!(xfer->len % 2)) { + if (ctlr->bits_per_word_mask & SPI_BPW_MASK(16)) + xfer->bits_per_word = 16; + } else { + xfer->bits_per_word = 8; + } + } + + return 0; +} +EXPORT_SYMBOL_GPL(spi_set_xfer_bpw); + /** * spi_mem_exec_op() - Execute a memory operation * @mem: the SPI memory @@ -252,6 +287,7 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) xfers[xferpos].tx_buf = tmpbuf; xfers[xferpos].len = sizeof(op->cmd.opcode); xfers[xferpos].tx_nbits = op->cmd.buswidth; + spi_set_xfer_bpw(ctlr, &xfers[xferpos]); spi_message_add_tail(&xfers[xferpos], &msg); xferpos++; totalxferlen++; @@ -266,6 +302,7 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) xfers[xferpos].tx_buf = tmpbuf + 1; xfers[xferpos].len = op->addr.nbytes; xfers[xferpos].tx_nbits = op->addr.buswidth; + spi_set_xfer_bpw(ctlr, &xfers[xferpos]); spi_message_add_tail(&xfers[xferpos], &msg); xferpos++; totalxferlen += op->addr.nbytes; @@ -276,6 +313,7 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) xfers[xferpos].tx_buf = tmpbuf + op->addr.nbytes + 1; xfers[xferpos].len = op->dummy.nbytes; xfers[xferpos].tx_nbits = op->dummy.buswidth; + spi_set_xfer_bpw(ctlr, &xfers[xferpos]); spi_message_add_tail(&xfers[xferpos], &msg); xferpos++; totalxferlen += op->dummy.nbytes; @@ -291,6 +329,7 @@ int spi_mem_exec_op(struct spi_mem *mem, const struct spi_mem_op *op) } xfers[xferpos].len = op->data.nbytes; + spi_set_xfer_bpw(ctlr, &xfers[xferpos]); spi_message_add_tail(&xfers[xferpos], &msg); xferpos++; totalxferlen += op->data.nbytes; -- 2.17.1