From: Eugen Hristev <eugen.hristev@microchip.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH v4 02/34] w1: Add 1-Wire gpio driver
Date: Tue, 18 Sep 2018 10:35:25 +0300 [thread overview]
Message-ID: <1537256157-18001-3-git-send-email-eugen.hristev@microchip.com> (raw)
In-Reply-To: <1537256157-18001-1-git-send-email-eugen.hristev@microchip.com>
From: Maxime Ripard <maxime.ripard@free-electrons.com>
Add a bus driver for bitbanging a 1-Wire bus over a GPIO.
Signed-off-by: Maxime Ripard <maxime.ripard@bootlin.com>
[eugen.hristev at microchip.com: fixed some issues]
Signed-off-by: Eugen Hristev <eugen.hristev@microchip.com>
---
drivers/w1/Kconfig | 7 ++
drivers/w1/Makefile | 2 +
drivers/w1/w1-gpio.c | 176 +++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 185 insertions(+)
create mode 100644 drivers/w1/w1-gpio.c
diff --git a/drivers/w1/Kconfig b/drivers/w1/Kconfig
index 64b27c6..d6e0457 100644
--- a/drivers/w1/Kconfig
+++ b/drivers/w1/Kconfig
@@ -13,6 +13,13 @@ config W1
if W1
+config W1_GPIO
+ bool "Enable 1-wire GPIO bitbanging"
+ default no
+ depends on DM_GPIO
+ help
+ Emulate a 1-wire bus using a GPIO.
+
endif
endmenu
diff --git a/drivers/w1/Makefile b/drivers/w1/Makefile
index f81693b..7fd8697 100644
--- a/drivers/w1/Makefile
+++ b/drivers/w1/Makefile
@@ -1 +1,3 @@
obj-$(CONFIG_W1) += w1-uclass.o
+
+obj-$(CONFIG_W1_GPIO) += w1-gpio.o
diff --git a/drivers/w1/w1-gpio.c b/drivers/w1/w1-gpio.c
new file mode 100644
index 0000000..5e5d6b3
--- /dev/null
+++ b/drivers/w1/w1-gpio.c
@@ -0,0 +1,176 @@
+/* SPDX-License-Identifier: GPL-2.0+
+ *
+ * Copyright (c) 2015 Free Electrons
+ * Copyright (c) 2015 NextThing Co
+ *
+ * Maxime Ripard <maxime.ripard@free-electrons.com>
+ *
+ */
+
+#include <common.h>
+#include <dm.h>
+#include <w1.h>
+
+#include <asm/gpio.h>
+
+#define W1_TIMING_A 6
+#define W1_TIMING_B 64
+#define W1_TIMING_C 60
+#define W1_TIMING_D 10
+#define W1_TIMING_E 9
+#define W1_TIMING_F 55
+#define W1_TIMING_G 0
+#define W1_TIMING_H 480
+#define W1_TIMING_I 70
+#define W1_TIMING_J 410
+
+struct w1_gpio_pdata {
+ struct gpio_desc gpio;
+ u64 search_id;
+};
+
+static bool w1_gpio_read_bit(struct udevice *dev)
+{
+ struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
+ int val;
+
+ dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
+ udelay(W1_TIMING_A);
+
+ dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
+ udelay(W1_TIMING_E);
+
+ val = dm_gpio_get_value(&pdata->gpio);
+ if (val < 0)
+ debug("error in retrieving GPIO value");
+ udelay(W1_TIMING_F);
+
+ return val;
+}
+
+static u8 w1_gpio_read_byte(struct udevice *dev)
+{
+ int i;
+ u8 ret = 0;
+
+ for (i = 0; i < 8; ++i)
+ ret |= (w1_gpio_read_bit(dev) ? 1 : 0) << i;
+
+ return ret;
+}
+
+static void w1_gpio_write_bit(struct udevice *dev, bool bit)
+{
+ struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
+
+ dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT);
+
+ bit ? udelay(W1_TIMING_A) : udelay(W1_TIMING_C);
+
+ dm_gpio_set_value(&pdata->gpio, 1);
+
+ bit ? udelay(W1_TIMING_B) : udelay(W1_TIMING_D);
+}
+
+static void w1_gpio_write_byte(struct udevice *dev, u8 byte)
+{
+ int i;
+
+ for (i = 0; i < 8; ++i)
+ w1_gpio_write_bit(dev, (byte >> i) & 0x1);
+}
+
+static bool w1_gpio_reset(struct udevice *dev)
+{
+ struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
+ int val;
+
+ /* initiate the reset pulse. first we must pull the bus to low */
+ dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_OUT | GPIOD_IS_OUT_ACTIVE);
+ udelay(W1_TIMING_G);
+
+ dm_gpio_set_value(&pdata->gpio, 0);
+ /* wait for the specified time with the bus kept low */
+ udelay(W1_TIMING_H);
+
+ /* now we must read the presence pulse */
+ dm_gpio_set_dir_flags(&pdata->gpio, GPIOD_IS_IN);
+ udelay(W1_TIMING_I);
+
+ val = dm_gpio_get_value(&pdata->gpio);
+ if (val < 0)
+ debug("error in retrieving GPIO value");
+
+ /* if nobody pulled the bus down , it means nobody is on the bus */
+ if (val != 0)
+ return 1;
+ /* we have the bus pulled down, let's wait for the specified presence time */
+ udelay(W1_TIMING_J);
+
+ /* read again, the other end should leave the bus free */
+ val = dm_gpio_get_value(&pdata->gpio);
+ if (val < 0)
+ debug("error in retrieving GPIO value");
+
+ /* bus is not going up again, so we have an error */
+ if (val != 1)
+ return 1;
+
+ /* all good, presence detected */
+ return 0;
+}
+
+static u8 w1_gpio_triplet(struct udevice *dev, bool bdir)
+{
+ u8 id_bit = w1_gpio_read_bit(dev);
+ u8 comp_bit = w1_gpio_read_bit(dev);
+ u8 retval;
+
+ if (id_bit && comp_bit)
+ return 0x03; /* error */
+
+ if (!id_bit && !comp_bit) {
+ /* Both bits are valid, take the direction given */
+ retval = bdir ? 0x04 : 0;
+ } else {
+ /* Only one bit is valid, take that direction */
+ bdir = id_bit;
+ retval = id_bit ? 0x05 : 0x02;
+ }
+
+ w1_gpio_write_bit(dev, bdir);
+ return retval;
+}
+
+static const struct w1_ops w1_gpio_ops = {
+ .read_byte = w1_gpio_read_byte,
+ .reset = w1_gpio_reset,
+ .triplet = w1_gpio_triplet,
+ .write_byte = w1_gpio_write_byte,
+};
+
+static int w1_gpio_ofdata_to_platdata(struct udevice *dev)
+{
+ struct w1_gpio_pdata *pdata = dev_get_platdata(dev);
+ int ret;
+
+ ret = gpio_request_by_name(dev, "gpios", 0, &pdata->gpio, 0);
+ if (ret < 0)
+ printf("Error claiming GPIO %d\n", ret);
+
+ return ret;
+};
+
+static const struct udevice_id w1_gpio_id[] = {
+ { "w1-gpio", 0 },
+ { },
+};
+
+U_BOOT_DRIVER(w1_gpio_drv) = {
+ .id = UCLASS_W1,
+ .name = "w1_gpio_drv",
+ .of_match = w1_gpio_id,
+ .ofdata_to_platdata = w1_gpio_ofdata_to_platdata,
+ .ops = &w1_gpio_ops,
+ .platdata_auto_alloc_size = sizeof(struct w1_gpio_pdata),
+};
--
2.7.4
next prev parent reply other threads:[~2018-09-18 7:35 UTC|newest]
Thread overview: 69+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-09-18 7:35 [U-Boot] [PATCH v4 00/34] Add support for 1wire protocol and 1wire eeproms Eugen Hristev
2018-09-18 7:35 ` [U-Boot] [PATCH v4 01/34] w1: Add 1-Wire uclass Eugen Hristev
2018-09-29 15:44 ` [U-Boot] [U-Boot,v4,01/34] " Tom Rini
2018-09-18 7:35 ` Eugen Hristev [this message]
2018-09-29 15:44 ` [U-Boot] [U-Boot,v4,02/34] w1: Add 1-Wire gpio driver Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 03/34] dt-bindings: W1: w1-gpio: added bindings for w1-gpio Eugen Hristev
2018-09-29 15:44 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 04/34] W1-EEPROM: Add an W1-EEPROM uclass for 1 wire EEPROMs Eugen Hristev
2018-09-29 15:44 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 05/34] w1: identify devices with w1-eeprom uclass Eugen Hristev
2018-09-29 15:44 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 06/34] W1-EEPROM: add support for Maxim DS24 eeprom families Eugen Hristev
2018-09-29 15:44 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 07/34] dt-bindings: w1-eeprom: ds24xxx: create bindings Eugen Hristev
2018-09-29 15:44 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 08/34] W1-EEPROM: add sandbox driver Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot,v4,08/34] " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 09/34] dt-bindings: w1-eeprom: eep_sandbox: create bindings Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 10/34] w1: add command for onewire protocol Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 11/34] pinctrl: sandbox: add gpio onewire w1 group Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 12/34] sandbox: DTS: w1: add node for one wire interface on GPIO Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 13/34] configs: sandbox: add onewire w1 and sandbox eeprom Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 14/34] configs: sama5d2_xplained: add onewire and eeprom drivers Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 15/34] configs: sama5d3_xplained: " Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 16/34] configs: sama5d27_som1_ek: " Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 17/34] configs: sama5d2_ptc_ek: " Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 18/34] configs: sama5d4_xplained: " Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 19/34] board: atmel: add support for pda detection Eugen Hristev
2018-09-29 15:45 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 20/34] board: sama5d2_xplained: add pda detect call at init time Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 21/34] board: sama5d3_xplained: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 22/34] board: sama5d27_som1_ek: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 23/34] board: sama5d2_ptc_ek: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 24/34] board: sama5d4_xplained: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 25/34] configs: sama5d2_xplained: add fdt overlay support Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 26/34] configs: sama5d3_xplained: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 27/34] configs: sama5d2_ptc_ek: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 28/34] configs: sama5d27_som1_ek: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 29/34] configs: sama5d4_xplained: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 30/34] ARM: dts: at91: sama5d2_xplained: add onewire connector for LCD eeprom Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 31/34] ARM: dts: at91: sama5d3_xplained: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 32/34] ARM: dts: at91: sama5d27_som1_ek: " Eugen Hristev
2018-09-29 15:46 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 33/34] ARM: dts: at91: sama5d2_ptc: " Eugen Hristev
2018-09-29 15:47 ` [U-Boot] [U-Boot, v4, " Tom Rini
2018-09-18 7:35 ` [U-Boot] [PATCH v4 34/34] ARM: dts: at91: sama5d4_xplained: " Eugen Hristev
2018-09-29 15:47 ` [U-Boot] [U-Boot, v4, " Tom Rini
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=1537256157-18001-3-git-send-email-eugen.hristev@microchip.com \
--to=eugen.hristev@microchip.com \
--cc=u-boot@lists.denx.de \
/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