From 8ceb69e8548f27305e37866ec3b81a79e1402b1a Mon Sep 17 00:00:00 2001
From: Phil Elwell <phil@raspberrypi.org>
Date: Wed, 4 Feb 2015 12:59:36 +0000
Subject: [PATCH 103/114] w1-gpio: Sort out the pullup/parasitic power tangle

---
 arch/arm/boot/dts/w1-gpio-overlay.dts        |  4 +++-
 arch/arm/boot/dts/w1-gpio-pullup-overlay.dts |  6 +++--
 drivers/w1/masters/w1-gpio.c                 | 36 ++++++++++++++++++----------
 include/linux/w1-gpio.h                      |  1 +
 4 files changed, 32 insertions(+), 15 deletions(-)

--- a/arch/arm/boot/dts/w1-gpio-overlay.dts
+++ b/arch/arm/boot/dts/w1-gpio-overlay.dts
@@ -1,4 +1,4 @@
-// Definitions for lirc-rpi module
+// Definitions for w1-gpio module (without external pullup)
 /dts-v1/;
 /plugin/;
 
@@ -14,6 +14,7 @@
 				pinctrl-names = "default";
 				pinctrl-0 = <&w1_pins>;
 				gpios = <&gpio 4 0>;
+				rpi,parasitic-power = <0>;
 				status = "okay";
 			};
 		};
@@ -33,5 +34,6 @@
 	__overrides__ {
 		gpiopin =       <&w1>,"gpios:4",
 				<&w1_pins>,"brcm,pins:0";
+		pullup =        <&w1>,"rpi,parasitic-power:0";
 	};
 };
--- a/arch/arm/boot/dts/w1-gpio-pullup-overlay.dts
+++ b/arch/arm/boot/dts/w1-gpio-pullup-overlay.dts
@@ -1,4 +1,4 @@
-// Definitions for lirc-rpi module
+// Definitions for w1-gpio module (with external pullup)
 /dts-v1/;
 /plugin/;
 
@@ -14,6 +14,7 @@
 				pinctrl-names = "default";
 				pinctrl-0 = <&w1_pins>;
 				gpios = <&gpio 4 0>, <&gpio 5 1>;
+				rpi,parasitic-power = <0>;
 				status = "okay";
 			};
 		};
@@ -33,7 +34,8 @@
 	__overrides__ {
 		gpiopin =       <&w1>,"gpios:4",
 				<&w1_pins>,"brcm,pins:0";
-		pullup =        <&w1>,"gpios:16",
+		extpullup =     <&w1>,"gpios:16",
 				<&w1_pins>,"brcm,pins:4";
+		pullup =        <&w1>,"rpi,parasitic-power:0";
 	};
 };
--- a/drivers/w1/masters/w1-gpio.c
+++ b/drivers/w1/masters/w1-gpio.c
@@ -23,10 +23,14 @@
 #include "../w1.h"
 #include "../w1_int.h"
 
-static int w1_gpio_pullup = -1;
-static int w1_gpio_pullup_orig = -1;
+static int w1_gpio_pullup = 0;
+static int w1_gpio_pullup_orig = 0;
 module_param_named(pullup, w1_gpio_pullup, int, 0);
-MODULE_PARM_DESC(pullup, "GPIO pin pullup number");
+MODULE_PARM_DESC(pullup, "Enable parasitic power (power on data) mode");
+static int w1_gpio_pullup_pin = -1;
+static int w1_gpio_pullup_pin_orig = -1;
+module_param_named(extpullup, w1_gpio_pullup_pin, int, 0);
+MODULE_PARM_DESC(extpullup, "GPIO external pullup pin number");
 static int w1_gpio_pin = -1;
 static int w1_gpio_pin_orig = -1;
 module_param_named(gpiopin, w1_gpio_pin, int, 0);
@@ -99,6 +103,7 @@ static int w1_gpio_probe_dt(struct platf
 	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
 	struct device_node *np = pdev->dev.of_node;
 	int gpio;
+	u32 value;
 
 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
 	if (!pdata)
@@ -107,6 +112,9 @@ static int w1_gpio_probe_dt(struct platf
 	if (of_get_property(np, "linux,open-drain", NULL))
 		pdata->is_open_drain = 1;
 
+	if (of_property_read_u32(np, "rpi,parasitic-power", &value) == 0)
+	    pdata->parasitic_power = (value != 0);
+
 	gpio = of_get_gpio(np, 0);
 	if (gpio < 0) {
 		if (gpio != -EPROBE_DEFER)
@@ -122,7 +130,7 @@ static int w1_gpio_probe_dt(struct platf
 	if (gpio == -EPROBE_DEFER)
 		return gpio;
 	/* ignore other errors as the pullup gpio is optional */
-	pdata->ext_pullup_enable_pin = gpio;
+	pdata->ext_pullup_enable_pin = (gpio >= 0) ? gpio : -1;
 
 	pdev->dev.platform_data = pdata;
 
@@ -158,17 +166,20 @@ static int w1_gpio_probe(struct platform
 	}
 
 	w1_gpio_pin_orig = pdata->pin;
-	w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
+	w1_gpio_pullup_pin_orig = pdata->ext_pullup_enable_pin;
+	w1_gpio_pullup_orig = pdata->parasitic_power;
 
 	if(gpio_is_valid(w1_gpio_pin)) {
 		pdata->pin = w1_gpio_pin;
 		pdata->ext_pullup_enable_pin = -1;
+		pdata->parasitic_power = -1;
 	}
-	if(gpio_is_valid(w1_gpio_pullup)) {
-		pdata->ext_pullup_enable_pin = w1_gpio_pullup;
+	pdata->parasitic_power |= w1_gpio_pullup;
+	if(gpio_is_valid(w1_gpio_pullup_pin)) {
+		pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin;
 	}
 
-	dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
+	dev_info(&pdev->dev, "gpio pin %d, external pullup pin %d, parasitic power %d\n", pdata->pin, pdata->ext_pullup_enable_pin, pdata->parasitic_power);
 
 	err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
 	if (err) {
@@ -199,10 +210,10 @@ static int w1_gpio_probe(struct platform
 		master->set_pullup = w1_gpio_set_pullup;
 	}
 
-	if (gpio_is_valid(w1_gpio_pullup)) {
+	if (pdata->parasitic_power) {
 		if (pdata->is_open_drain)
-			printk(KERN_ERR "w1-gpio 'pullup' option "
-			       "doesn't work with open drain GPIO\n");
+			printk(KERN_ERR "w1-gpio 'pullup'(parasitic power) "
+			       "option doesn't work with open drain GPIO\n");
 		else
 			master->bitbang_pullup = w1_gpio_bitbang_pullup;
 	}
@@ -238,7 +249,8 @@ static int w1_gpio_remove(struct platfor
 	w1_remove_master_device(master);
 
 	pdata->pin = w1_gpio_pin_orig;
-	pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
+	pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin_orig;
+	pdata->parasitic_power = w1_gpio_pullup_orig;
 
 	return 0;
 }
--- a/include/linux/w1-gpio.h
+++ b/include/linux/w1-gpio.h
@@ -18,6 +18,7 @@
 struct w1_gpio_platform_data {
 	unsigned int pin;
 	unsigned int is_open_drain:1;
+	unsigned int parasitic_power:1;
 	void (*enable_external_pullup)(int enable);
 	unsigned int ext_pullup_enable_pin;
 	unsigned int pullup_duration;
