From patchwork Thu Mar 19 09:01:51 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Sperl X-Patchwork-Id: 6047791 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DBDB7BF90F for ; Thu, 19 Mar 2015 09:02:37 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 08D712050E for ; Thu, 19 Mar 2015 09:02:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id EC38520519 for ; Thu, 19 Mar 2015 09:02:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751928AbbCSJCf (ORCPT ); Thu, 19 Mar 2015 05:02:35 -0400 Received: from 212-186-180-163.dynamic.surfer.at ([212.186.180.163]:56367 "EHLO cgate.sperl.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751824AbbCSJCe (ORCPT ); Thu, 19 Mar 2015 05:02:34 -0400 Received: from raspb.intern.sperl.org (account martin@sperl.org [10.10.10.32] verified) by sperl.org (CommuniGate Pro SMTP 6.0.10) with ESMTPSA id 6281515; Thu, 19 Mar 2015 08:58:59 +0000 From: kernel@martin.sperl.org To: broonie@kernel.org, warren@wwwdotorg.org, lee@kernel.org, linux-spi@vger.kernel.org, linux-rpi-kernel@lists.infradead.org Cc: Martin Sperl Subject: [PATCH V2] SPI: BCM2835: allow arbitrary GPIO to act as SPI-chip_select Date: Thu, 19 Mar 2015 09:01:51 +0000 Message-Id: <1426755714-28130-2-git-send-email-kernel@martin.sperl.org> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1426755714-28130-1-git-send-email-kernel@martin.sperl.org> References: <1426755714-28130-1-git-send-email-kernel@martin.sperl.org> Sender: linux-spi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-spi@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Martin Sperl use cs-gpios for the spi bus master and standard gpio operation instead of relying on the HW with just 2 chip_selects. Signed-off-by: Martin Sperl Acked-by: Stephen Warren --- drivers/spi/spi-bcm2835.c | 65 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 58 insertions(+), 7 deletions(-) -- 1.7.10.4 -- To unsubscribe from this list: send the line "unsubscribe linux-spi" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/spi/spi-bcm2835.c b/drivers/spi/spi-bcm2835.c index e1dea40..5fbad64 100644 --- a/drivers/spi/spi-bcm2835.c +++ b/drivers/spi/spi-bcm2835.c @@ -30,6 +30,7 @@ #include #include #include +#include #include /* SPI register offsets */ @@ -116,6 +117,16 @@ static inline void bcm2835_wr_fifo(struct bcm2835_spi *bs, int len) } } +/* ideally spi_set_cs would be exported by spi-core */ +static inline void bcm2835_set_cs(struct spi_device *spi, bool enable) +{ + if (spi->mode & SPI_CS_HIGH) + enable = !enable; + + if (gpio_is_valid(spi->cs_gpio)) + gpio_set_value(spi->cs_gpio, !enable); +} + static irqreturn_t bcm2835_spi_interrupt(int irq, void *dev_id) { struct spi_master *master = dev_id; @@ -207,12 +218,24 @@ static int bcm2835_spi_start_transfer( cs |= BCM2835_SPI_CS_CPHA; if (!(spi->mode & SPI_NO_CS)) { - if (spi->mode & SPI_CS_HIGH) { - cs |= BCM2835_SPI_CS_CSPOL; - cs |= BCM2835_SPI_CS_CSPOL0 << spi->chip_select; - } + if (gpio_is_valid(spi->cs_gpio)) { + bcm2835_set_cs(spi, 1); + } else { + /* Legacy mode using HW chip selects + * note that there is a bug in this when there are + * multiple devices on the bus with at least one + * having SPI_CS_HIGH set - the other CS_CSPOLX get + * reset to 0 when any other device starts a transfer + * this is due to a shared register for the polarity + */ + if (spi->mode & SPI_CS_HIGH) { + cs |= BCM2835_SPI_CS_CSPOL; + cs |= BCM2835_SPI_CS_CSPOL0 + << spi->chip_select; + } - cs |= spi->chip_select; + cs |= spi->chip_select; + } } reinit_completion(&bs->done); @@ -248,9 +271,12 @@ static int bcm2835_spi_finish_transfer( if (tfr->delay_usecs) udelay(tfr->delay_usecs); - if (cs_change) + if (cs_change) { + /* reset CS */ + bcm2835_set_cs(spi, 0); /* Clear TA flag */ bcm2835_wr(bs, BCM2835_SPI_CS, cs & ~BCM2835_SPI_CS_TA); + } return 0; } @@ -290,15 +316,39 @@ static int bcm2835_spi_transfer_one( } out: - /* Clear FIFOs, and disable the HW block */ + /* Clear FIFOs, reset CS and disable the HW block */ bcm2835_wr(bs, BCM2835_SPI_CS, BCM2835_SPI_CS_CLEAR_RX | BCM2835_SPI_CS_CLEAR_TX); + bcm2835_set_cs(spi, 0); mesg->status = err; spi_finalize_current_message(master); return 0; } +static int bcm2835_spi_setup(struct spi_device *spi) +{ + /* do not care when in SPI_NO_CS mode */ + if (spi->mode & SPI_NO_CS) + return 0; + + /* setting up CS right from the start */ + if (gpio_is_valid(spi->cs_gpio)) { + bcm2835_set_cs(spi, 0); + return 0; + } + + /* Legacy mode using HW chipselects */ + if (spi->chip_select > 2) { + dev_err(&spi->dev, + "setup: only three native chip-selects are supported\n" + ); + return -EINVAL; + } + + return 0; +} + static int bcm2835_spi_probe(struct platform_device *pdev) { struct spi_master *master; @@ -318,6 +368,7 @@ static int bcm2835_spi_probe(struct platform_device *pdev) master->bits_per_word_mask = SPI_BPW_MASK(8); master->num_chipselect = 3; master->transfer_one_message = bcm2835_spi_transfer_one; + master->setup = bcm2835_spi_setup; master->dev.of_node = pdev->dev.of_node; bs = spi_master_get_devdata(master);