Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp41846imm; Wed, 12 Sep 2018 17:40:41 -0700 (PDT) X-Google-Smtp-Source: ANB0VdZFot6ctYLRYYs9jGBmd2UE0kZdDJVdsy69vuFndsJcHa8yp0fmlQzXv6VB1QdMYB31DO2X X-Received: by 2002:a63:731b:: with SMTP id o27-v6mr4582137pgc.216.1536799241892; Wed, 12 Sep 2018 17:40:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1536799241; cv=none; d=google.com; s=arc-20160816; b=djykClTwKg8RM8+tzZ/x9NhueIxWGURwed0oywk1nTIwJ1zWbKw/bc3qpCpkM54EmS dbaKn9jkOuVhQilUk1dlugHareA+L6mMLLF52hRfHln1XZ3BxwXpVDN5EZSRK+YZpaIB 1R6CsW+TynZUQL3V1HEZysZsfyxanXla/qNfpUZWcALMVda0DJ2RDnebUKMGvVW88wIX oh08PTrw/iVtfJWcSw9TPKzRklX5QUISQyM3+feQpe9mxluGYOmPApiNfldbU+sEYrF+ HFiePQYhMSI5LQdelbAiVWRE/655Oj2MbBOlse7v31XGJAlTxI/6ysFKRgWgCTQieGlF /kUQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=HGhbgAqHWtAOSQXGQmSRWuXfZcabuZIwSzqWuXebhNI=; b=O+1Mq9kbWNGhywYbO/s8PzM6wRtZmWmaVtuw9hbthVl+0k3eiA2gMIjaxpRS8xCgYk CgEsydT7mFFsApcpImQ1eAYC0GbIOgFbzVe+S3RsG5XjoLnf9VYlQPYgWFh1R3Lc/Nqt +cofU8Df8z8tBigbXTHqjucb00Tf0UYjbGH3ntWiYLM39v0zAzAdexwWiaTPqChWgMk+ xjBIybr/uG/J919CSIKoUwpxPwSFHMJCv9a7123iFVf8wX3OdIaIzMJyg55PvlCYUlXM TJV0T2O1BjQhDLMVpyoCtPxzVgF8V4jixsQyDRSU6Xj08I+x70YgIR0dy/9VX0VgrpuD yJOw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@lechnology.com header.s=default header.b=B06DEJ85; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b190-v6si2465248pfg.94.2018.09.12.17.40.27; Wed, 12 Sep 2018 17:40:41 -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; dkim=fail header.i=@lechnology.com header.s=default header.b=B06DEJ85; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727657AbeIMFrC (ORCPT + 99 others); Thu, 13 Sep 2018 01:47:02 -0400 Received: from vern.gendns.com ([206.190.152.46]:46153 "EHLO vern.gendns.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727152AbeIMFrB (ORCPT ); Thu, 13 Sep 2018 01:47:01 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lechnology.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject :Cc:To:From:Sender:Reply-To:MIME-Version:Content-Type: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=HGhbgAqHWtAOSQXGQmSRWuXfZcabuZIwSzqWuXebhNI=; b=B06DEJ85p9daS09m9fm+Qbs9X dKX+/7Zfp/2WYRPensoZ34z7P35ZkMWqXBeq9YwusUBQaOeIxkU+HpjI44OwGVKNWE6WbWCSX4Htq r2rBg5jnpH516vYSthwvJYJFTYhygRB0KL2gYoCKwcDcd6t65AwLdIPgNhy1G+aOC27bE5PDz1sAa w7F0PVrKGMvN9CxINSAnkWbu3+wg2JTdDqs2k509/4axjWCzOYvg0L36M6zao2qdBoVA0TaZbVZPx nJMBvOakUu+GZsKHmFfpXVOezSIXTZK3TDYRH2VeLlQQwqrGYtFYhhHwG2B5ka/NFjdFhvUerHmcG 189ahfNTA==; Received: from 108-198-5-147.lightspeed.okcbok.sbcglobal.net ([108.198.5.147]:33924 helo=freyr.lechnology.com) by vern.gendns.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES128-GCM-SHA256:128) (Exim 4.91) (envelope-from ) id 1g0Ffq-00AGoe-JV; Wed, 12 Sep 2018 20:39:58 -0400 From: David Lechner To: linux-spi@vger.kernel.org, linux-iio@vger.kernel.org Cc: David Lechner , Jonathan Cameron , Hartmut Knaack , Lars-Peter Clausen , Peter Meerwald-Stadler , Mark Brown , linux-kernel@vger.kernel.org Subject: [PATCH v2 2/4] spi: add software implementation for SPI_CS_WORD Date: Wed, 12 Sep 2018 19:39:18 -0500 Message-Id: <20180913003920.30600-3-david@lechnology.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20180913003920.30600-1-david@lechnology.com> References: <20180913003920.30600-1-david@lechnology.com> X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - vern.gendns.com X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - lechnology.com X-Get-Message-Sender-Via: vern.gendns.com: authenticated_id: davidmain+lechnology.com/only user confirmed/virtual account not confirmed X-Authenticated-Sender: vern.gendns.com: davidmain@lechnology.com X-Source: X-Source-Args: X-Source-Dir: Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This adds a default software implementation for the SPI_CS_WORD flag for controllers that don't have such a feature. The SPI_CS_WORD flag indicates that the CS line should be toggled between each word sent, not just between each transfer. The implementation works by using existing functions to split transfers into one-word-sized transfers and sets the cs_change bit for each of the new transfers. Signed-off-by: David Lechner --- drivers/spi/spi.c | 31 +++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 9da0bc5a036c..84518ed58872 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c @@ -2783,8 +2783,10 @@ int spi_setup(struct spi_device *spi) return -EINVAL; /* help drivers fail *cleanly* when they need options * that aren't supported with their current controller + * SPI_CS_WORD has a fallback software implementation, + * so it is ignored here. */ - bad_bits = spi->mode & ~spi->controller->mode_bits; + bad_bits = spi->mode & ~(spi->controller->mode_bits | SPI_CS_WORD); ugly_bits = bad_bits & (SPI_TX_DUAL | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD); if (ugly_bits) { @@ -2838,6 +2840,33 @@ static int __spi_validate(struct spi_device *spi, struct spi_message *message) if (list_empty(&message->transfers)) return -EINVAL; + /* If an SPI controller does not support toggling the CS line on each + * transfer (indicated by the SPI_CS_WORD flag), we can emulate it by + * splitting transfers into one-word transfers and ensuring that + * cs_change is set for each transfer. + */ + if ((spi->mode & SPI_CS_WORD) && !(ctlr->mode_bits & SPI_CS_WORD)) { + size_t maxsize; + int ret; + + maxsize = (spi->bits_per_word + 7) / 8; + + /* spi_split_transfers_maxsize() requires message->spi */ + message->spi = spi; + + ret = spi_split_transfers_maxsize(ctlr, message, maxsize, + GFP_KERNEL); + if (ret) + return ret; + + list_for_each_entry(xfer, &message->transfers, transfer_list) { + /* don't change cs_change on the last entry in the list */ + if (list_is_last(&xfer->transfer_list, &message->transfers)) + break; + xfer->cs_change = 1; + } + } + /* Half-duplex links include original MicroWire, and ones with * only one data pin like SPI_3WIRE (switches direction) or where * either MOSI or MISO is missing. They can also be caused by -- 2.17.1