mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
To: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
Cc: barebox@lists.infradead.org
Subject: [PATCH 04/13] spi: Add SPI GPIO bitbang driver
Date: Fri, 10 Apr 2015 03:03:41 +0200	[thread overview]
Message-ID: <1428627830-17281-5-git-send-email-sebastian.hesselbarth@gmail.com> (raw)
In-Reply-To: <1428627830-17281-1-git-send-email-sebastian.hesselbarth@gmail.com>

This adds a driver for SPI master by GPIO pins.

Signed-off-by: Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
---
Cc: barebox@lists.infradead.org
---
 drivers/spi/Kconfig    |   4 +
 drivers/spi/Makefile   |   1 +
 drivers/spi/gpio_spi.c | 238 +++++++++++++++++++++++++++++++++++++++++++++++++
 include/spi/spi_gpio.h |  36 ++++++++
 4 files changed, 279 insertions(+)
 create mode 100644 drivers/spi/gpio_spi.c
 create mode 100644 include/spi/spi_gpio.h

diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index e4fa6a2d94bb..738b88ea5ed7 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -18,6 +18,10 @@ config DRIVER_SPI_ATMEL
 	bool "Atmel (AT91) SPI Master driver"
 	depends on ARCH_AT91
 
+config DRIVER_SPI_GPIO
+	bool "GPIO SPI Master driver"
+	depends on GPIOLIB
+
 config DRIVER_SPI_IMX
 	bool "i.MX SPI Master driver"
 	depends on ARCH_IMX
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 7469479c31fc..74819665856e 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -1,5 +1,6 @@
 obj-$(CONFIG_SPI) += spi.o
 obj-$(CONFIG_DRIVER_SPI_ATH79) += ath79_spi.o
+obj-$(CONFIG_DRIVER_SPI_GPIO) += gpio_spi.o
 obj-$(CONFIG_DRIVER_SPI_IMX) += imx_spi.o
 obj-$(CONFIG_DRIVER_SPI_MVEBU) += mvebu_spi.o
 obj-$(CONFIG_DRIVER_SPI_MXS) += mxs_spi.o
diff --git a/drivers/spi/gpio_spi.c b/drivers/spi/gpio_spi.c
new file mode 100644
index 000000000000..fd9aa3e2303c
--- /dev/null
+++ b/drivers/spi/gpio_spi.c
@@ -0,0 +1,238 @@
+/*
+ * SPI master driver using generic bitbanged GPIO
+ *
+ * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
+ *
+ * Based on Linux driver
+ *   Copyright (C) 2006,2008 David Brownell
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include <common.h>
+#include <driver.h>
+#include <errno.h>
+#include <gpio.h>
+#include <init.h>
+#include <io.h>
+#include <malloc.h>
+#include <of_gpio.h>
+#include <spi/spi.h>
+#include <spi/spi_gpio.h>
+
+struct gpio_spi {
+	struct spi_master master;
+	struct gpio_spi_pdata *data;
+};
+
+#define priv_from_spi_device(s)	container_of(s->master, struct gpio_spi, master)
+
+static inline void setsck(const struct spi_device *spi, int is_on)
+{
+	struct gpio_spi *priv = priv_from_spi_device(spi);
+	gpio_set_value(priv->data->sck, is_on);
+}
+
+static inline void setmosi(const struct spi_device *spi, int is_on)
+{
+	struct gpio_spi *priv = priv_from_spi_device(spi);
+	if (!gpio_is_valid(priv->data->mosi))
+		return;
+	gpio_set_value(priv->data->mosi, is_on);
+}
+
+static inline int getmiso(const struct spi_device *spi)
+{
+	struct gpio_spi *priv = priv_from_spi_device(spi);
+	if (!gpio_is_valid(priv->data->miso))
+		return 1;
+	return !!gpio_get_value(priv->data->miso);
+}
+
+#define spidelay(nsecs) do { } while (0)
+
+#include "spi-bitbang-txrx.h"
+
+static int gpio_spi_set_cs(struct spi_device *spi, bool en)
+{
+	struct gpio_spi *priv = priv_from_spi_device(spi);
+
+	if (!gpio_is_valid(priv->data->cs[spi->chip_select]))
+		return 0;
+
+	gpio_set_value(priv->data->cs[spi->chip_select],
+		       (spi->mode & SPI_CS_HIGH) ? en : !en);
+
+	return 0;
+}
+
+static inline u32 gpio_spi_txrx_word(struct spi_device *spi, unsigned nsecs,
+				     u32 word, u8 bits)
+{
+	int cpol = !!(spi->mode & SPI_CPOL);
+	if (spi->mode & SPI_CPHA)
+		return bitbang_txrx_be_cpha1(spi, nsecs, cpol, word, bits);
+	else
+		return bitbang_txrx_be_cpha0(spi, nsecs, cpol, word, bits);
+}
+
+static int gpio_spi_transfer_one(struct spi_device *spi, struct spi_transfer *t)
+{
+	bool read = (t->rx_buf) ? true : false;
+	u32 word = 0;
+	int n;
+
+	for (n = 0; n < t->len; n++) {
+		if (!read)
+			word = ((const u8 *)t->tx_buf)[n];
+		word = gpio_spi_txrx_word(spi, 0, word, 8);
+		if (read)
+			((u8 *)t->rx_buf)[n] = word & 0xff;
+	}
+
+	return 0;
+}
+
+static int gpio_spi_transfer(struct spi_device *spi, struct spi_message *msg)
+{
+	struct spi_transfer *t;
+	int ret;
+
+	ret = gpio_spi_set_cs(spi, true);
+	if (ret)
+		return ret;
+
+	list_for_each_entry(t, &msg->transfers, transfer_list) {
+		ret = gpio_spi_transfer_one(spi, t);
+		if (ret)
+			return ret;
+		msg->actual_length += t->len;
+	}
+
+	ret = gpio_spi_set_cs(spi, false);
+	if (ret)
+		return ret;
+
+	return 0;
+}
+
+static int gpio_spi_setup(struct spi_device *spi)
+{
+	if (spi->bits_per_word != 8) {
+		dev_err(spi->master->dev, "master does not support %d bits per word\n",
+			spi->bits_per_word);
+		return -EINVAL;
+	}
+
+	return 0;
+}
+
+static int gpio_spi_of_probe(struct device_d *dev)
+{
+	struct device_node *np = dev->device_node;
+	struct gpio_spi_pdata *pdata;
+	int n, sck;
+
+	if (!IS_ENABLED(CONFIG_OFDEVICE) || dev->platform_data)
+		return 0;
+
+	sck = of_get_named_gpio(np, "gpio-sck", 0);
+	if (!gpio_is_valid(sck)) {
+		dev_err(dev, "missing mandatory SCK gpio\n");
+		return sck;
+	}
+
+	pdata = xzalloc(sizeof(*pdata));
+	pdata->sck = sck;
+	pdata->num_cs = MAX_CHIPSELECT;
+
+	pdata->miso = of_get_named_gpio(np, "gpio-miso", 0);
+	if (!gpio_is_valid(pdata->miso))
+		pdata->miso = SPI_GPIO_NO_MISO;
+
+	pdata->mosi = of_get_named_gpio(np, "gpio-mosi", 0);
+	if (!gpio_is_valid(pdata->mosi))
+		pdata->mosi = SPI_GPIO_NO_MOSI;
+
+	for (n = 0; n < MAX_CHIPSELECT; n++) {
+		pdata->cs[n] = of_get_named_gpio(np, "cs-gpios", n);
+		if (!gpio_is_valid(pdata->cs[n]))
+		    pdata->cs[n] = SPI_GPIO_NO_CS;
+	}
+
+	dev->platform_data = pdata;
+
+	return 0;
+}
+
+static int gpio_spi_probe(struct device_d *dev)
+{
+	struct gpio_spi *priv;
+	struct gpio_spi_pdata *pdata;
+	struct spi_master *master;
+	int n, ret;
+
+	ret = gpio_spi_of_probe(dev);
+	if (ret)
+		return ret;
+	pdata = dev->platform_data;
+
+	ret = gpio_request_one(pdata->sck, GPIOF_DIR_OUT, "spi-sck");
+	if (ret)
+		return ret;
+
+	if (pdata->miso != SPI_GPIO_NO_MISO) {
+		ret = gpio_request_one(pdata->miso, GPIOF_DIR_IN, "spi-miso");
+		if (ret)
+			return ret;
+	}
+
+	if (pdata->mosi != SPI_GPIO_NO_MOSI) {
+		ret = gpio_request_one(pdata->mosi, GPIOF_DIR_OUT, "spi-mosi");
+		if (ret)
+			return ret;
+	}
+
+	for (n = 0; n < pdata->num_cs; n++) {
+		char *cs_name;
+
+		if (!gpio_is_valid(pdata->cs[n]))
+			continue;
+
+		cs_name = asprintf("spi-cs%d", n);
+		ret = gpio_request_one(pdata->cs[n], GPIOF_DIR_OUT, cs_name);
+		if (ret)
+			return ret;
+	}
+
+	priv = xzalloc(sizeof(*priv));
+	priv->data = pdata;
+	master = &priv->master;
+	master->dev = dev;
+	master->bus_num = dev->id;
+	master->setup = gpio_spi_setup;
+	master->transfer = gpio_spi_transfer;
+	master->num_chipselect = priv->data->num_cs;
+
+	return spi_register_master(&priv->master);
+}
+
+static struct of_device_id __maybe_unused gpio_spi_dt_ids[] = {
+	{ .compatible = "spi-gpio", },
+	{ }
+};
+
+static struct driver_d gpio_spi_driver = {
+	.name = "gpio-spi",
+	.probe = gpio_spi_probe,
+	.of_compatible = DRV_OF_COMPAT(gpio_spi_dt_ids),
+};
+device_platform_driver(gpio_spi_driver);
diff --git a/include/spi/spi_gpio.h b/include/spi/spi_gpio.h
new file mode 100644
index 000000000000..841c934f6ead
--- /dev/null
+++ b/include/spi/spi_gpio.h
@@ -0,0 +1,36 @@
+/*
+ * SPI master driver using generic bitbanged GPIO
+ *
+ * Sebastian Hesselbarth <sebastian.hesselbarth@gmail.com>
+ *
+ * Based on Linux driver
+ *   Copyright (C) 2006,2008 David Brownell
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#ifndef __SPI_GPIO_H
+#define __SPI_GPIO_H
+
+#define MAX_CHIPSELECT		4
+#define SPI_GPIO_NO_CS		(-1)
+#define SPI_GPIO_NO_MISO	(-1)
+#define SPI_GPIO_NO_MOSI	(-1)
+
+struct gpio_spi_pdata {
+	int sck;
+	int mosi;
+	int miso;
+	int cs[MAX_CHIPSELECT];
+	int num_cs;
+};
+
+#endif
-- 
2.1.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2015-04-10  1:04 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-10  1:03 [PATCH 00/13] Add support for Lenovo ix4-300d NAS Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 01/13] net: phy: Support Marvell 88E1318S PHY Sebastian Hesselbarth
2015-05-02 16:53   ` Ezequiel Garcia
2015-05-02 20:28     ` Ezequiel Garcia
2015-05-03  9:50       ` Sebastian Hesselbarth
2015-05-03 22:47         ` Sebastian Hesselbarth
2015-05-05 12:06           ` Ezequiel Garcia
2015-04-10  1:03 ` [PATCH 02/13] gpio: Add driver for 74x164 compatible shift-registers Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 03/13] spi: ath79: move spidelay from spi-bitbang-txrx Sebastian Hesselbarth
2015-04-10  1:03 ` Sebastian Hesselbarth [this message]
2015-04-10  1:03 ` [PATCH 05/13] bus: mvebu-mbus: Remove coherency attribute Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 06/13] bus: mvebu-mbus: Drop device reference Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 07/13] bus: mvebu-mbus: Convert mbus platform driver to direct driver Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 08/13] ARM: mvebu: Move PCIe register defines to socid.h Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 09/13] ARM: mvebu: armada-xp: Fixup broken MV78230-A0 SoC ID Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 10/13] ARM: mvebu: armada-xp: Limit PUP access to Armada XP Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 11/13] ARM: mvebu: armada-xp: Use MBUS_ERR_PROP_EN define Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 12/13] ARM: mvebu: armada-xp: Sort boards and images alphabetically Sebastian Hesselbarth
2015-04-10  1:03 ` [PATCH 13/13] ARM: mvebu: armada-xp: Add Lenovo Iomega ix4-300d Sebastian Hesselbarth
2015-04-14  8:43 ` [PATCH 00/13] Add support for Lenovo ix4-300d NAS Sascha Hauer
2015-04-14  9:42   ` Sebastian Hesselbarth

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=1428627830-17281-5-git-send-email-sebastian.hesselbarth@gmail.com \
    --to=sebastian.hesselbarth@gmail.com \
    --cc=barebox@lists.infradead.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