linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org
To: broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	warren-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org,
	lee-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org,
	linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
	linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org
Cc: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
Subject: [PATCH V2] SPI: BCM2835: allow arbitrary GPIO to act as SPI-chip_select
Date: Thu, 19 Mar 2015 09:01:51 +0000	[thread overview]
Message-ID: <1426755714-28130-2-git-send-email-kernel@martin.sperl.org> (raw)
In-Reply-To: <1426755714-28130-1-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>

From: Martin Sperl <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>

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 <kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
---
 drivers/spi/spi-bcm2835.c |   65 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 7 deletions(-)

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 <linux/of.h>
 #include <linux/of_irq.h>
 #include <linux/of_device.h>
+#include <linux/of_gpio.h>
 #include <linux/spi/spi.h>

 /* 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);
--
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-spi" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

  parent reply	other threads:[~2015-03-19  9:01 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-03-19  9:01 [PATCH] SPI: BCM2835: fix all checkpath --strict messages kernel-TqfNSX0MhmxHKSADF0wUEw
     [not found] ` <1426755714-28130-1-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-19  9:01   ` kernel-TqfNSX0MhmxHKSADF0wUEw [this message]
     [not found]     ` <1426755714-28130-2-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-20  5:05       ` [PATCH V2] SPI: BCM2835: allow arbitrary GPIO to act as SPI-chip_select Stephen Warren
     [not found]         ` <550BAA89.5090707-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
2015-03-20  9:52           ` Mark Brown
     [not found]             ` <20150320095247.GE2869-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-03-25  7:10               ` [PATCH V2 - RESEND] " Martin Sperl
     [not found]                 ` <1756D77D-173D-4D6D-B629-59CC8A49714F-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-25 15:16                   ` Mark Brown
     [not found]                     ` <20150325151611.GA3572-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-03-25 16:13                       ` Martin Sperl
     [not found]                         ` <681599F9-2D88-49FF-BD16-4F388D0B2874-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-25 16:54                           ` Mark Brown
     [not found]                             ` <20150325165441.GI3572-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-03-25 17:59                               ` Martin Sperl
     [not found]                                 ` <767DFCCF-C8A4-44B3-9E85-96011B0FF0FA-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-25 18:50                                   ` Mark Brown
     [not found]                                     ` <20150325185007.GL3572-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-03-25 19:43                                       ` Martin Sperl
     [not found]                                         ` <561486F6-3580-4884-8A6A-8477D8C3BDC9-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-25 21:02                                           ` Mark Brown
2015-03-26 10:08                                           ` [PATCH] SPI: bcm2835: move to the transfer_one driver model Martin Sperl
     [not found]                                             ` <30057D30-6A2D-4517-B374-76FF2448E455-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-26 17:35                                               ` Mark Brown
     [not found]                                                 ` <20150326173511.GW3572-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-03-26 17:47                                                   ` Stephen Warren
2015-03-26 19:15                                                   ` Martin Sperl
     [not found]                                                     ` <C176096E-3731-4F47-9DCF-AE83B143EB9D-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-27  1:32                                                       ` Mark Brown
2015-03-28  4:09                                               ` Stephen Warren
     [not found]                                                 ` <5516296A.2030505-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
2015-03-28 18:11                                                   ` Martin Sperl
     [not found]                                                     ` <AB966507-3A94-4421-8153-AB4090E309AF-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-29  3:24                                                       ` Stephen Warren
     [not found]                                                         ` <55177076.9000208-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
2015-03-29 10:19                                                           ` Martin Sperl
     [not found]                                                             ` <74807CAA-A444-413B-9C9B-C3B06D5C18E6-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-29 21:44                                                               ` Martin Sperl
2015-03-31 14:49                                                   ` Martin Sperl
     [not found]                                                     ` <E13569A5-33E5-48ED-B25F-EC7374E144C9-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-04-01 15:20                                                       ` Stephen Warren
     [not found]                                                         ` <551C0CA2.40406-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
2015-04-03 10:17                                                           ` Martin Sperl
     [not found]                                                             ` <A1AEABE2-F0D2-481B-BD83-E1AD7861E960-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-04-06 17:26                                                               ` Mark Brown
2015-03-19  9:01   ` [PATCH] SPI: BCM2835: clock divider can be a multiple of 2 kernel-TqfNSX0MhmxHKSADF0wUEw
     [not found]     ` <1426755714-28130-3-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-20  5:06       ` Stephen Warren
     [not found]         ` <550BAAD8.20206-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
2015-03-20  7:04           ` Martin Sperl
     [not found]             ` <6EBE48E2-80E7-454C-A84C-231D45DC64E0-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-20 14:25               ` Noralf Trønnes
2015-03-23 18:53       ` Mark Brown
2015-03-19  9:01   ` [PATCH] SPI: BCM2835: enable support of 3-wire mode kernel-TqfNSX0MhmxHKSADF0wUEw
     [not found]     ` <1426755714-28130-4-git-send-email-kernel-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-20  5:12       ` Stephen Warren
     [not found]         ` <550BAC49.3000105-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
2015-03-20  6:17           ` Martin Sperl
2015-03-23 18:57       ` Mark Brown
2015-03-20  4:58   ` [PATCH] SPI: BCM2835: fix all checkpath --strict messages Stephen Warren
     [not found]     ` <550BA8FE.7030001-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org>
2015-03-20  6:26       ` Martin Sperl
     [not found]         ` <7727F65E-D8BC-4299-A359-56FA449C3379-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-20 13:40           ` Mark Brown
     [not found]             ` <20150320134053.GN2869-GFdadSzt00ze9xe1eoZjHA@public.gmane.org>
2015-03-20 14:26               ` [PATCH V2] " Martin Sperl
     [not found]                 ` <78137BE7-37A4-4144-BBCC-CBEDA70DD31A-TqfNSX0MhmxHKSADF0wUEw@public.gmane.org>
2015-03-20 18:05                   ` Mark Brown
2015-03-20 13:36   ` [PATCH] " Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1426755714-28130-2-git-send-email-kernel@martin.sperl.org \
    --to=kernel-tqfnsx0mhmxhksadf0wuew@public.gmane.org \
    --cc=broonie-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=lee-DgEjT+Ai2ygdnm+yROfE0A@public.gmane.org \
    --cc=linux-rpi-kernel-IAPFreCvJWM7uuMidbF8XUB+6BGkLq7r@public.gmane.org \
    --cc=linux-spi-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
    --cc=warren-3lzwWm7+Weoh9ZMKESR00Q@public.gmane.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).