Skip to content

Commit 947c872

Browse files
committed
enabling the realtime clock 1-wire chip DS1307 and 1-wire on GPIO4 (as a module)
1-wire: Add support for configuring pin for w1-gpio kernel module See: #457 Add bitbanging pullups, use them for w1-gpio Allows parasite power to work, uses module option pullup=1 bcm2708: Ensure 1-wire pullup is disabled by default, and expose as module parameter Signed-off-by: Alex J Lennon <[email protected]> w1-gpio: Add gpiopin module parameter and correctly free up gpio pull-up pin, if set Signed-off-by: Alex J Lennon <[email protected]> w1-gpio: Sort out the pullup/parasitic power tangle
1 parent 06199f7 commit 947c872

File tree

5 files changed

+99
-9
lines changed

5 files changed

+99
-9
lines changed

drivers/w1/masters/w1-gpio.c

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@
2222

2323
#include <linux/w1.h>
2424

25+
static int w1_gpio_pullup = 0;
26+
static int w1_gpio_pullup_orig = 0;
27+
module_param_named(pullup, w1_gpio_pullup, int, 0);
28+
MODULE_PARM_DESC(pullup, "Enable parasitic power (power on data) mode");
29+
static int w1_gpio_pullup_pin = -1;
30+
static int w1_gpio_pullup_pin_orig = -1;
31+
module_param_named(extpullup, w1_gpio_pullup_pin, int, 0);
32+
MODULE_PARM_DESC(extpullup, "GPIO external pullup pin number");
33+
static int w1_gpio_pin = -1;
34+
static int w1_gpio_pin_orig = -1;
35+
module_param_named(gpiopin, w1_gpio_pin, int, 0);
36+
MODULE_PARM_DESC(gpiopin, "GPIO pin number");
37+
2538
static u8 w1_gpio_set_pullup(void *data, int delay)
2639
{
2740
struct w1_gpio_platform_data *pdata = data;
@@ -66,6 +79,16 @@ static u8 w1_gpio_read_bit(void *data)
6679
return gpio_get_value(pdata->pin) ? 1 : 0;
6780
}
6881

82+
static void w1_gpio_bitbang_pullup(void *data, u8 on)
83+
{
84+
struct w1_gpio_platform_data *pdata = data;
85+
86+
if (on)
87+
gpio_direction_output(pdata->pin, 1);
88+
else
89+
gpio_direction_input(pdata->pin);
90+
}
91+
6992
#if defined(CONFIG_OF)
7093
static const struct of_device_id w1_gpio_dt_ids[] = {
7194
{ .compatible = "w1-gpio" },
@@ -79,6 +102,7 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
79102
struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
80103
struct device_node *np = pdev->dev.of_node;
81104
int gpio;
105+
u32 value;
82106

83107
pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
84108
if (!pdata)
@@ -87,6 +111,9 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
87111
if (of_get_property(np, "linux,open-drain", NULL))
88112
pdata->is_open_drain = 1;
89113

114+
if (of_property_read_u32(np, "rpi,parasitic-power", &value) == 0)
115+
pdata->parasitic_power = (value != 0);
116+
90117
gpio = of_get_gpio(np, 0);
91118
if (gpio < 0) {
92119
if (gpio != -EPROBE_DEFER)
@@ -102,7 +129,7 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
102129
if (gpio == -EPROBE_DEFER)
103130
return gpio;
104131
/* ignore other errors as the pullup gpio is optional */
105-
pdata->ext_pullup_enable_pin = gpio;
132+
pdata->ext_pullup_enable_pin = (gpio >= 0) ? gpio : -1;
106133

107134
pdev->dev.platform_data = pdata;
108135

@@ -112,13 +139,15 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
112139
static int w1_gpio_probe(struct platform_device *pdev)
113140
{
114141
struct w1_bus_master *master;
115-
struct w1_gpio_platform_data *pdata;
142+
struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
116143
int err;
117144

118-
if (of_have_populated_dt()) {
119-
err = w1_gpio_probe_dt(pdev);
120-
if (err < 0)
121-
return err;
145+
if(pdata == NULL) {
146+
if (of_have_populated_dt()) {
147+
err = w1_gpio_probe_dt(pdev);
148+
if (err < 0)
149+
return err;
150+
}
122151
}
123152

124153
pdata = dev_get_platdata(&pdev->dev);
@@ -135,6 +164,22 @@ static int w1_gpio_probe(struct platform_device *pdev)
135164
return -ENOMEM;
136165
}
137166

167+
w1_gpio_pin_orig = pdata->pin;
168+
w1_gpio_pullup_pin_orig = pdata->ext_pullup_enable_pin;
169+
w1_gpio_pullup_orig = pdata->parasitic_power;
170+
171+
if(gpio_is_valid(w1_gpio_pin)) {
172+
pdata->pin = w1_gpio_pin;
173+
pdata->ext_pullup_enable_pin = -1;
174+
pdata->parasitic_power = -1;
175+
}
176+
pdata->parasitic_power |= w1_gpio_pullup;
177+
if(gpio_is_valid(w1_gpio_pullup_pin)) {
178+
pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin;
179+
}
180+
181+
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);
182+
138183
err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
139184
if (err) {
140185
dev_err(&pdev->dev, "gpio_request (pin) failed\n");
@@ -164,6 +209,14 @@ static int w1_gpio_probe(struct platform_device *pdev)
164209
master->set_pullup = w1_gpio_set_pullup;
165210
}
166211

212+
if (pdata->parasitic_power) {
213+
if (pdata->is_open_drain)
214+
printk(KERN_ERR "w1-gpio 'pullup'(parasitic power) "
215+
"option doesn't work with open drain GPIO\n");
216+
else
217+
master->bitbang_pullup = w1_gpio_bitbang_pullup;
218+
}
219+
167220
err = w1_add_master_device(master);
168221
if (err) {
169222
dev_err(&pdev->dev, "w1_add_master device failed\n");
@@ -194,6 +247,10 @@ static int w1_gpio_remove(struct platform_device *pdev)
194247

195248
w1_remove_master_device(master);
196249

250+
pdata->pin = w1_gpio_pin_orig;
251+
pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin_orig;
252+
pdata->parasitic_power = w1_gpio_pullup_orig;
253+
197254
return 0;
198255
}
199256

drivers/w1/w1_int.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,20 @@ int w1_add_master_device(struct w1_bus_master *master)
114114
return(-EINVAL);
115115
}
116116

117+
/* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
118+
* and takes care of timing itself */
119+
if (!master->write_byte && !master->touch_bit && master->set_pullup) {
120+
printk(KERN_ERR "w1_add_master_device: set_pullup requires "
121+
"write_byte or touch_bit, disabling\n");
122+
master->set_pullup = NULL;
123+
}
124+
125+
if (master->set_pullup && master->bitbang_pullup) {
126+
printk(KERN_ERR "w1_add_master_device: set_pullup should not "
127+
"be set when bitbang_pullup is used, disabling\n");
128+
master->set_pullup = NULL;
129+
}
130+
117131
/* Lock until the device is added (or not) to w1_masters. */
118132
mutex_lock(&w1_mlock);
119133
/* Search for the first available id (starting at 1). */

drivers/w1/w1_io.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,22 @@ static void w1_pre_write(struct w1_master *dev)
127127
static void w1_post_write(struct w1_master *dev)
128128
{
129129
if (dev->pullup_duration) {
130-
if (dev->enable_pullup && dev->bus_master->set_pullup)
131-
dev->bus_master->set_pullup(dev->bus_master->data, 0);
132-
else
130+
if (dev->enable_pullup) {
131+
if (dev->bus_master->set_pullup) {
132+
dev->bus_master->set_pullup(dev->
133+
bus_master->data,
134+
0);
135+
} else if (dev->bus_master->bitbang_pullup) {
136+
dev->bus_master->
137+
bitbang_pullup(dev->bus_master->data, 1);
138+
msleep(dev->pullup_duration);
139+
dev->bus_master->
140+
bitbang_pullup(dev->bus_master->data, 0);
141+
}
142+
} else {
133143
msleep(dev->pullup_duration);
144+
}
145+
134146
dev->pullup_duration = 0;
135147
}
136148
}

include/linux/w1-gpio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
struct w1_gpio_platform_data {
1919
unsigned int pin;
2020
unsigned int is_open_drain:1;
21+
unsigned int parasitic_power:1;
2122
void (*enable_external_pullup)(int enable);
2223
unsigned int ext_pullup_enable_pin;
2324
unsigned int pullup_duration;

include/linux/w1.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ struct w1_bus_master {
157157

158158
u8 (*set_pullup)(void *, int);
159159

160+
/**
161+
* Turns the pullup on/off in bitbanging mode, takes an on/off argument.
162+
* @return -1=Error, 0=completed
163+
*/
164+
void (*bitbang_pullup) (void *, u8);
165+
160166
void (*search)(void *, struct w1_master *,
161167
u8, w1_slave_found_callback);
162168
};

0 commit comments

Comments
 (0)