22
22
23
23
#include <linux/w1.h>
24
24
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
+
25
38
static u8 w1_gpio_set_pullup (void * data , int delay )
26
39
{
27
40
struct w1_gpio_platform_data * pdata = data ;
@@ -66,6 +79,16 @@ static u8 w1_gpio_read_bit(void *data)
66
79
return gpio_get_value (pdata -> pin ) ? 1 : 0 ;
67
80
}
68
81
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
+
69
92
#if defined(CONFIG_OF )
70
93
static const struct of_device_id w1_gpio_dt_ids [] = {
71
94
{ .compatible = "w1-gpio" },
@@ -79,6 +102,7 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
79
102
struct w1_gpio_platform_data * pdata = dev_get_platdata (& pdev -> dev );
80
103
struct device_node * np = pdev -> dev .of_node ;
81
104
int gpio ;
105
+ u32 value ;
82
106
83
107
pdata = devm_kzalloc (& pdev -> dev , sizeof (* pdata ), GFP_KERNEL );
84
108
if (!pdata )
@@ -87,6 +111,9 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
87
111
if (of_get_property (np , "linux,open-drain" , NULL ))
88
112
pdata -> is_open_drain = 1 ;
89
113
114
+ if (of_property_read_u32 (np , "rpi,parasitic-power" , & value ) == 0 )
115
+ pdata -> parasitic_power = (value != 0 );
116
+
90
117
gpio = of_get_gpio (np , 0 );
91
118
if (gpio < 0 ) {
92
119
if (gpio != - EPROBE_DEFER )
@@ -102,7 +129,7 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
102
129
if (gpio == - EPROBE_DEFER )
103
130
return gpio ;
104
131
/* 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 ;
106
133
107
134
pdev -> dev .platform_data = pdata ;
108
135
@@ -112,13 +139,15 @@ static int w1_gpio_probe_dt(struct platform_device *pdev)
112
139
static int w1_gpio_probe (struct platform_device * pdev )
113
140
{
114
141
struct w1_bus_master * master ;
115
- struct w1_gpio_platform_data * pdata ;
142
+ struct w1_gpio_platform_data * pdata = pdev -> dev . platform_data ;
116
143
int err ;
117
144
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
+ }
122
151
}
123
152
124
153
pdata = dev_get_platdata (& pdev -> dev );
@@ -135,6 +164,22 @@ static int w1_gpio_probe(struct platform_device *pdev)
135
164
return - ENOMEM ;
136
165
}
137
166
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
+
138
183
err = devm_gpio_request (& pdev -> dev , pdata -> pin , "w1" );
139
184
if (err ) {
140
185
dev_err (& pdev -> dev , "gpio_request (pin) failed\n" );
@@ -164,6 +209,14 @@ static int w1_gpio_probe(struct platform_device *pdev)
164
209
master -> set_pullup = w1_gpio_set_pullup ;
165
210
}
166
211
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
+
167
220
err = w1_add_master_device (master );
168
221
if (err ) {
169
222
dev_err (& pdev -> dev , "w1_add_master device failed\n" );
@@ -194,6 +247,10 @@ static int w1_gpio_remove(struct platform_device *pdev)
194
247
195
248
w1_remove_master_device (master );
196
249
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
+
197
254
return 0 ;
198
255
}
199
256
0 commit comments