From patchwork Wed Mar 12 15:20:35 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Grzeschik X-Patchwork-Id: 3818521 Return-Path: X-Original-To: patchwork-linux-spi@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 004C49F1CD for ; Wed, 12 Mar 2014 15:21:10 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 86D87201F0 for ; Wed, 12 Mar 2014 15:21:08 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 65D10202F0 for ; Wed, 12 Mar 2014 15:21:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755034AbaCLPVA (ORCPT ); Wed, 12 Mar 2014 11:21:00 -0400 Received: from metis.ext.pengutronix.de ([92.198.50.35]:60181 "EHLO metis.ext.pengutronix.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755032AbaCLPU6 (ORCPT ); Wed, 12 Mar 2014 11:20:58 -0400 Received: from dude.hi.pengutronix.de ([2001:6f8:1178:2:a236:9fff:fe00:814]) by metis.ext.pengutronix.de with esmtp (Exim 4.72) (envelope-from ) id 1WNkxb-0003kj-R4; Wed, 12 Mar 2014 16:20:47 +0100 Received: from mgr by dude.hi.pengutronix.de with local (Exim 4.82) (envelope-from ) id 1WNkxa-0003Va-Pw; Wed, 12 Mar 2014 16:20:46 +0100 From: Michael Grzeschik To: linux-spi@vger.kernel.org Cc: broonie@kernel.org, linux-arm-kernel@vger.kernel.org, kernel@pengutronix.de Subject: [PATCH 2/3] spi: bitbang: add lsb first support Date: Wed, 12 Mar 2014 16:20:35 +0100 Message-Id: <1394637636-29042-3-git-send-email-m.grzeschik@pengutronix.de> X-Mailer: git-send-email 1.9.0 In-Reply-To: <1394637636-29042-1-git-send-email-m.grzeschik@pengutronix.de> References: <1394637636-29042-1-git-send-email-m.grzeschik@pengutronix.de> X-SA-Exim-Connect-IP: 2001:6f8:1178:2:a236:9fff:fe00:814 X-SA-Exim-Mail-From: mgr@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-spi@vger.kernel.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 The bitbang spi driver currently only supports the MSB mode. This patch adds the possibility to clock the data in LSB mode. Signed-off-by: Michael Grzeschik --- drivers/spi/spi-bitbang-txrx.h | 98 +++++++++++++++++++++++++++++------------- drivers/spi/spi-bitbang.c | 3 +- 2 files changed, 71 insertions(+), 30 deletions(-) diff --git a/drivers/spi/spi-bitbang-txrx.h b/drivers/spi/spi-bitbang-txrx.h index b6e348d..7f9c020 100644 --- a/drivers/spi/spi-bitbang-txrx.h +++ b/drivers/spi/spi-bitbang-txrx.h @@ -49,22 +49,42 @@ bitbang_txrx_be_cpha0(struct spi_device *spi, { /* if (cpol == 0) this is SPI_MODE_0; else this is SPI_MODE_2 */ - /* clock starts at inactive polarity */ - for (word <<= (32 - bits); likely(bits); bits--) { - - /* setup MSB (to slave) on trailing edge */ - if ((flags & SPI_MASTER_NO_TX) == 0) - setmosi(spi, word & (1 << 31)); - spidelay(nsecs); /* T(setup) */ - - setsck(spi, !cpol); - spidelay(nsecs); - - /* sample MSB (from slave) on leading edge */ - if ((flags & SPI_MASTER_NO_RX) == 0) - word |= getmiso(spi); - setsck(spi, cpol); - word <<= 1; + if (spi->mode & SPI_LSB_FIRST) { + /* clock starts at inactive polarity */ + for (; likely(bits); bits--) { + + /* setup MSB (to slave) on trailing edge */ + if ((flags & SPI_MASTER_NO_TX) == 0) + setmosi(spi, word & 1); + spidelay(nsecs); /* T(setup) */ + + setsck(spi, !cpol); + spidelay(nsecs); + + /* sample LSB (from slave) on leading edge */ + if ((flags & SPI_MASTER_NO_RX) == 0) + word |= getmiso(spi); + setsck(spi, cpol); + word >>= 1; + } + } else { + /* clock starts at inactive polarity */ + for (word <<= (32 - bits); likely(bits); bits--) { + + /* setup MSB (to slave) on trailing edge */ + if ((flags & SPI_MASTER_NO_TX) == 0) + setmosi(spi, word & (1 << 31)); + spidelay(nsecs); /* T(setup) */ + + setsck(spi, !cpol); + spidelay(nsecs); + + /* sample MSB (from slave) on leading edge */ + if ((flags & SPI_MASTER_NO_RX) == 0) + word |= getmiso(spi); + setsck(spi, cpol); + word <<= 1; + } } return word; } @@ -76,22 +96,42 @@ bitbang_txrx_be_cpha1(struct spi_device *spi, { /* if (cpol == 0) this is SPI_MODE_1; else this is SPI_MODE_3 */ - /* clock starts at inactive polarity */ - for (word <<= (32 - bits); likely(bits); bits--) { + if (spi->mode & SPI_LSB_FIRST) { + /* clock starts at inactive polarity */ + for (; likely(bits); bits--) { + + /* setup MSB (to slave) on leading edge */ + setsck(spi, !cpol); + if ((flags & SPI_MASTER_NO_TX) == 0) + setmosi(spi, word & 1); + spidelay(nsecs); /* T(setup) */ + + setsck(spi, cpol); + spidelay(nsecs); + + /* sample MSB (from slave) on trailing edge */ + if ((flags & SPI_MASTER_NO_RX) == 0) + word |= getmiso(spi); + word >>= 1; + } + } else { + /* clock starts at inactive polarity */ + for (word <<= (32 - bits); likely(bits); bits--) { - /* setup MSB (to slave) on leading edge */ - setsck(spi, !cpol); - if ((flags & SPI_MASTER_NO_TX) == 0) - setmosi(spi, word & (1 << 31)); - spidelay(nsecs); /* T(setup) */ + /* setup MSB (to slave) on leading edge */ + setsck(spi, !cpol); + if ((flags & SPI_MASTER_NO_TX) == 0) + setmosi(spi, word & (1 << 31)); + spidelay(nsecs); /* T(setup) */ - setsck(spi, cpol); - spidelay(nsecs); + setsck(spi, cpol); + spidelay(nsecs); - /* sample MSB (from slave) on trailing edge */ - if ((flags & SPI_MASTER_NO_RX) == 0) - word |= getmiso(spi); - word <<= 1; + /* sample MSB (from slave) on trailing edge */ + if ((flags & SPI_MASTER_NO_RX) == 0) + word |= getmiso(spi); + word <<= 1; + } } return word; } diff --git a/drivers/spi/spi-bitbang.c b/drivers/spi/spi-bitbang.c index bd222f6..3624f96 100644 --- a/drivers/spi/spi-bitbang.c +++ b/drivers/spi/spi-bitbang.c @@ -432,7 +432,8 @@ int spi_bitbang_start(struct spi_bitbang *bitbang) spin_lock_init(&bitbang->lock); if (!master->mode_bits) - master->mode_bits = SPI_CPOL | SPI_CPHA | bitbang->flags; + master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LSB_FIRST + | bitbang->flags; if (master->transfer || master->transfer_one_message) return -EINVAL;