From de5bf02cb1f61de0c65a539cd0083ac8ab07ec50 Mon Sep 17 00:00:00 2001
From: Fabio Estevam <fabio.estevam@freescale.com>
Date: Thu, 15 Nov 2012 11:23:23 +0000
Subject: [PATCH] spi: mxc_spi: Fix handling of chip select

In decode_cs() function the polarity of the chip select must be taken into
account.

Also, for the case of low active chip select, the CS was activated too early.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>
---
 drivers/spi/mxc_spi.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/mxc_spi.c b/drivers/spi/mxc_spi.c
index 13bebe8..b6bad98 100644
--- a/drivers/spi/mxc_spi.c
+++ b/drivers/spi/mxc_spi.c
@@ -387,7 +387,7 @@ static int decode_cs(struct mxc_spi_slave *mxcs, unsigned int cs)
 	if (cs > 3) {
 		mxcs->gpio = cs >> 8;
 		cs &= 3;
-		ret = gpio_direction_output(mxcs->gpio, 0);
+		ret = gpio_direction_output(mxcs->gpio, !(mxcs->ss_pol));
 		if (ret) {
 			printf("mxc_spi: cannot setup gpio %d\n", mxcs->gpio);
 			return -EINVAL;
@@ -414,6 +414,8 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
 		return NULL;
 	}
 
+	mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
+
 	ret = decode_cs(mxcs, cs);
 	if (ret < 0) {
 		free(mxcs);
@@ -425,7 +427,6 @@ struct spi_slave *spi_setup_slave(unsigned int bus, unsigned int cs,
 	mxcs->slave.bus = bus;
 	mxcs->slave.cs = cs;
 	mxcs->base = spi_bases[bus];
-	mxcs->ss_pol = (mode & SPI_CS_HIGH) ? 1 : 0;
 
 	ret = spi_cfg_mxc(mxcs, cs, max_hz, mode);
 	if (ret) {