From patchwork Sat Jul 21 14:30:51 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lorenzo Bianconi X-Patchwork-Id: 10538895 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E962E602D7 for ; Sat, 21 Jul 2018 14:31:11 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 896E128B79 for ; Sat, 21 Jul 2018 14:31:11 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 7D63428B86; Sat, 21 Jul 2018 14:31:11 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 163F828B79 for ; Sat, 21 Jul 2018 14:31:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727787AbeGUPYH (ORCPT ); Sat, 21 Jul 2018 11:24:07 -0400 Received: from mail-wr1-f68.google.com ([209.85.221.68]:35560 "EHLO mail-wr1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727766AbeGUPYG (ORCPT ); Sat, 21 Jul 2018 11:24:06 -0400 Received: by mail-wr1-f68.google.com with SMTP id a3-v6so13773596wrt.2 for ; Sat, 21 Jul 2018 07:31:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=M7ZTtsv0EEV9o4/ospJ6gR8Wcg2dOI6pROUph5lO5b0=; b=aMCimI+T4GVccAFwXmc3LWxezM0P5HjcJ6s6MbrK7IR6HALAUg33fvpfHAjPvZef9/ F8MaCZGynjkCh33Ar6cureNNQbnf2c6yCymKkUQuU7z0HU1oHsCnRk8xFjwxF8ZlzaQJ MFlEbxpuO6kSRXwz4pfs0Vb8rohnrg4rxG7hXFMU63Ti2x8LA6H3cvJOZW/TujsIXUsU Mgl+uwGIFN8H43C5VBCndHSd9736XRW2fRrKB53oL87ancGtZFiyXOxy4WNcMu5AGMDh ULchPDh/uzMx48sMW3eOOzfTPxcUfntlh4yTC8ZOsjwaSik/YQE7Ln0EqWV0/2OeadY1 AjgQ== X-Gm-Message-State: AOUpUlGIubV5wWGW5XqbiOFnROLuBdmyRTEPCLRJNbru6qp1em37Er9O zi8aEHZEMCEvDgN+KIAKGc7/tg== X-Google-Smtp-Source: AAOMgpeLca6JwKRrF67JhgfHP7Zu8Lk0GtwjaXOkjxZwAdBNtz5FLZrppjmU5BzmtLIokNLT3CF6ZQ== X-Received: by 2002:adf:ff11:: with SMTP id k17-v6mr3961116wrr.255.1532183469198; Sat, 21 Jul 2018 07:31:09 -0700 (PDT) Received: from localhost.localdomain ([151.66.54.250]) by smtp.gmail.com with ESMTPSA id l16-v6sm3969097wme.36.2018.07.21.07.31.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 21 Jul 2018 07:31:08 -0700 (PDT) From: Lorenzo Bianconi To: broonie@kernel.org Cc: linux-spi@vger.kernel.org, linus.walleij@linaro.org Subject: [RFC 2/2] spi: spi-gpio: add SPI_3WIRE support Date: Sat, 21 Jul 2018 16:30:51 +0200 Message-Id: <20180721143051.30228-3-lorenzo.bianconi@redhat.com> X-Mailer: git-send-email 2.18.0 Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Add SPI_3WIRE support to spi-gpio controller introducing set_line_direction function pointer in spi_bitbang data structure. Spi-gpio controller in 3wire mode has been tested using hts221 temp/rh iio sensor Signed-off-by: Lorenzo Bianconi --- drivers/spi/spi-bitbang.c | 14 ++++++++++++++ drivers/spi/spi-gpio.c | 17 ++++++++++++++++- include/linux/spi/spi_bitbang.h | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/drivers/spi/spi-bitbang.c b/drivers/spi/spi-bitbang.c index 76f1b534bdd7..8c98a2acc59a 100644 --- a/drivers/spi/spi-bitbang.c +++ b/drivers/spi/spi-bitbang.c @@ -244,6 +244,20 @@ static int spi_bitbang_bufs(struct spi_device *spi, struct spi_transfer *t) struct spi_bitbang_cs *cs = spi->controller_state; unsigned nsecs = cs->nsecs; + if (spi->mode & SPI_3WIRE) { + struct spi_bitbang *bitbang; + unsigned flags; + int err; + + bitbang = spi_master_get_devdata(spi->master); + err = bitbang->set_line_direction(spi, !!(t->tx_buf)); + if (err < 0) + return err; + + flags = t->tx_buf ? SPI_MASTER_NO_RX : SPI_MASTER_NO_TX; + return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, flags); + } + return cs->txrx_bufs(spi, cs->txrx_word, nsecs, t, 0); } diff --git a/drivers/spi/spi-gpio.c b/drivers/spi/spi-gpio.c index be68298cbd9c..0626e6e3ea0c 100644 --- a/drivers/spi/spi-gpio.c +++ b/drivers/spi/spi-gpio.c @@ -121,7 +121,10 @@ static inline int getmiso(const struct spi_device *spi) { struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); - return !!gpiod_get_value_cansleep(spi_gpio->miso); + if (spi->mode & SPI_3WIRE) + return !!gpiod_get_value_cansleep(spi_gpio->mosi); + else + return !!gpiod_get_value_cansleep(spi_gpio->miso); } /* @@ -250,6 +253,16 @@ static int spi_gpio_setup(struct spi_device *spi) return status; } +static int spi_gpio_set_direction(struct spi_device *spi, bool output) +{ + struct spi_gpio *spi_gpio = spi_to_spi_gpio(spi); + + if (output) + return gpiod_direction_output(spi_gpio->mosi, 1); + else + return gpiod_direction_input(spi_gpio->mosi); +} + static void spi_gpio_cleanup(struct spi_device *spi) { spi_bitbang_cleanup(spi); @@ -395,6 +408,7 @@ static int spi_gpio_probe(struct platform_device *pdev) return status; master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 32); + master->mode_bits = SPI_3WIRE | SPI_CPHA | SPI_CPOL; master->flags = master_flags; master->bus_num = pdev->id; /* The master needs to think there is a chipselect even if not connected */ @@ -407,6 +421,7 @@ static int spi_gpio_probe(struct platform_device *pdev) spi_gpio->bitbang.master = master; spi_gpio->bitbang.chipselect = spi_gpio_chipselect; + spi_gpio->bitbang.set_line_direction = spi_gpio_set_direction; if ((master_flags & (SPI_MASTER_NO_TX | SPI_MASTER_NO_RX)) == 0) { spi_gpio->bitbang.txrx_word[SPI_MODE_0] = spi_gpio_txrx_word_mode0; diff --git a/include/linux/spi/spi_bitbang.h b/include/linux/spi/spi_bitbang.h index 5847f00ef9cf..b7e021b274dc 100644 --- a/include/linux/spi/spi_bitbang.h +++ b/include/linux/spi/spi_bitbang.h @@ -31,6 +31,7 @@ struct spi_bitbang { u32 (*txrx_word[4])(struct spi_device *spi, unsigned nsecs, u32 word, u8 bits, unsigned flags); + int (*set_line_direction)(struct spi_device *spi, bool output); }; /* you can call these default bitbang->master methods from your custom