Skip to content

Commit

Permalink
w1-gpio: Sort out the pullup/parasitic power tangle
Browse files Browse the repository at this point in the history
  • Loading branch information
Phil Elwell committed Feb 4, 2015
1 parent c288c0e commit a827a56
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 15 deletions.
4 changes: 3 additions & 1 deletion arch/arm/boot/dts/w1-gpio-overlay.dts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Definitions for lirc-rpi module
// Definitions for w1-gpio module (without external pullup)
/dts-v1/;
/plugin/;

Expand All @@ -14,6 +14,7 @@
pinctrl-names = "default";
pinctrl-0 = <&w1_pins>;
gpios = <&gpio 4 0>;
rpi,parasitic-power = <0>;
status = "okay";
};
};
Expand All @@ -33,5 +34,6 @@
__overrides__ {
gpiopin = <&w1>,"gpios:4",
<&w1_pins>,"brcm,pins:0";
pullup = <&w1>,"rpi,parasitic-power:0";
};
};
6 changes: 4 additions & 2 deletions arch/arm/boot/dts/w1-gpio-pullup-overlay.dts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Definitions for lirc-rpi module
// Definitions for w1-gpio module (with external pullup)
/dts-v1/;
/plugin/;

Expand All @@ -14,6 +14,7 @@
pinctrl-names = "default";
pinctrl-0 = <&w1_pins>;
gpios = <&gpio 4 0>, <&gpio 5 1>;
rpi,parasitic-power = <0>;
status = "okay";
};
};
Expand All @@ -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";
};
};
36 changes: 24 additions & 12 deletions drivers/w1/masters/w1-gpio.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -99,6 +103,7 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
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)
Expand All @@ -107,6 +112,9 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
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)
Expand All @@ -122,7 +130,7 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
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;

Expand Down Expand Up @@ -158,17 +166,20 @@ static int w1_gpio_probe(struct platform_device *pdev)
}

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) {
Expand Down Expand Up @@ -199,10 +210,10 @@ static int w1_gpio_probe(struct platform_device *pdev)
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;
}
Expand Down Expand Up @@ -238,7 +249,8 @@ static int w1_gpio_remove(struct platform_device *pdev)
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;
}
Expand Down
1 change: 1 addition & 0 deletions include/linux/w1-gpio.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit a827a56

Please sign in to comment.