408ac045f7d99f51322ef0698ef9fafa75f52530
[oweals/openwrt.git] /
1 From 5edc95eee5ef69d7bfe03eede0e711e8f196dedc Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Wed, 8 May 2013 11:46:50 +0100
4 Subject: [PATCH] enabling the realtime clock 1-wire chip DS1307 and 1-wire on
5  GPIO4 (as a module)
6
7 1-wire: Add support for configuring pin for w1-gpio kernel module
8 See: https://github.com/raspberrypi/linux/pull/457
9
10 Add bitbanging pullups, use them for w1-gpio
11
12 Allows parasite power to work, uses module option pullup=1
13
14 bcm2708: Ensure 1-wire pullup is disabled by default, and expose as module parameter
15
16 Signed-off-by: Alex J Lennon <ajlennon@dynamicdevices.co.uk>
17
18 w1-gpio: Add gpiopin module parameter and correctly free up gpio pull-up pin, if set
19
20 Signed-off-by: Alex J Lennon <ajlennon@dynamicdevices.co.uk>
21
22 w1-gpio: Sort out the pullup/parasitic power tangle
23 ---
24  drivers/w1/masters/w1-gpio.c | 69 ++++++++++++++++++++++++++++++++++++++++----
25  drivers/w1/w1.h              |  6 ++++
26  drivers/w1/w1_int.c          | 14 +++++++++
27  drivers/w1/w1_io.c           | 18 ++++++++++--
28  include/linux/w1-gpio.h      |  1 +
29  5 files changed, 99 insertions(+), 9 deletions(-)
30
31 --- a/drivers/w1/masters/w1-gpio.c
32 +++ b/drivers/w1/masters/w1-gpio.c
33 @@ -23,6 +23,19 @@
34  #include "../w1.h"
35  #include "../w1_int.h"
36  
37 +static int w1_gpio_pullup = 0;
38 +static int w1_gpio_pullup_orig = 0;
39 +module_param_named(pullup, w1_gpio_pullup, int, 0);
40 +MODULE_PARM_DESC(pullup, "Enable parasitic power (power on data) mode");
41 +static int w1_gpio_pullup_pin = -1;
42 +static int w1_gpio_pullup_pin_orig = -1;
43 +module_param_named(extpullup, w1_gpio_pullup_pin, int, 0);
44 +MODULE_PARM_DESC(extpullup, "GPIO external pullup pin number");
45 +static int w1_gpio_pin = -1;
46 +static int w1_gpio_pin_orig = -1;
47 +module_param_named(gpiopin, w1_gpio_pin, int, 0);
48 +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
49 +
50  static u8 w1_gpio_set_pullup(void *data, int delay)
51  {
52         struct w1_gpio_platform_data *pdata = data;
53 @@ -67,6 +80,16 @@ static u8 w1_gpio_read_bit(void *data)
54         return gpio_get_value(pdata->pin) ? 1 : 0;
55  }
56  
57 +static void w1_gpio_bitbang_pullup(void *data, u8 on)
58 +{
59 +       struct w1_gpio_platform_data *pdata = data;
60 +
61 +       if (on)
62 +               gpio_direction_output(pdata->pin, 1);
63 +       else
64 +               gpio_direction_input(pdata->pin);
65 +}
66 +
67  #if defined(CONFIG_OF)
68  static const struct of_device_id w1_gpio_dt_ids[] = {
69         { .compatible = "w1-gpio" },
70 @@ -80,6 +103,7 @@ static int w1_gpio_probe_dt(struct platf
71         struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
72         struct device_node *np = pdev->dev.of_node;
73         int gpio;
74 +       u32 value;
75  
76         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
77         if (!pdata)
78 @@ -88,6 +112,9 @@ static int w1_gpio_probe_dt(struct platf
79         if (of_get_property(np, "linux,open-drain", NULL))
80                 pdata->is_open_drain = 1;
81  
82 +       if (of_property_read_u32(np, "rpi,parasitic-power", &value) == 0)
83 +           pdata->parasitic_power = (value != 0);
84 +
85         gpio = of_get_gpio(np, 0);
86         if (gpio < 0) {
87                 if (gpio != -EPROBE_DEFER)
88 @@ -103,7 +130,7 @@ static int w1_gpio_probe_dt(struct platf
89         if (gpio == -EPROBE_DEFER)
90                 return gpio;
91         /* ignore other errors as the pullup gpio is optional */
92 -       pdata->ext_pullup_enable_pin = gpio;
93 +       pdata->ext_pullup_enable_pin = (gpio >= 0) ? gpio : -1;
94  
95         pdev->dev.platform_data = pdata;
96  
97 @@ -113,13 +140,15 @@ static int w1_gpio_probe_dt(struct platf
98  static int w1_gpio_probe(struct platform_device *pdev)
99  {
100         struct w1_bus_master *master;
101 -       struct w1_gpio_platform_data *pdata;
102 +       struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
103         int err;
104  
105 -       if (of_have_populated_dt()) {
106 -               err = w1_gpio_probe_dt(pdev);
107 -               if (err < 0)
108 -                       return err;
109 +       if(pdata == NULL) {
110 +               if (of_have_populated_dt()) {
111 +                       err = w1_gpio_probe_dt(pdev);
112 +                       if (err < 0)
113 +                               return err;
114 +               }
115         }
116  
117         pdata = dev_get_platdata(&pdev->dev);
118 @@ -136,6 +165,22 @@ static int w1_gpio_probe(struct platform
119                 return -ENOMEM;
120         }
121  
122 +       w1_gpio_pin_orig = pdata->pin;
123 +       w1_gpio_pullup_pin_orig = pdata->ext_pullup_enable_pin;
124 +       w1_gpio_pullup_orig = pdata->parasitic_power;
125 +
126 +       if(gpio_is_valid(w1_gpio_pin)) {
127 +               pdata->pin = w1_gpio_pin;
128 +               pdata->ext_pullup_enable_pin = -1;
129 +               pdata->parasitic_power = -1;
130 +       }
131 +       pdata->parasitic_power |= w1_gpio_pullup;
132 +       if(gpio_is_valid(w1_gpio_pullup_pin)) {
133 +               pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin;
134 +       }
135 +
136 +       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);
137 +
138         err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
139         if (err) {
140                 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
141 @@ -165,6 +210,14 @@ static int w1_gpio_probe(struct platform
142                 master->set_pullup = w1_gpio_set_pullup;
143         }
144  
145 +       if (pdata->parasitic_power) {
146 +               if (pdata->is_open_drain)
147 +                       printk(KERN_ERR "w1-gpio 'pullup'(parasitic power) "
148 +                              "option doesn't work with open drain GPIO\n");
149 +               else
150 +                       master->bitbang_pullup = w1_gpio_bitbang_pullup;
151 +       }
152 +
153         err = w1_add_master_device(master);
154         if (err) {
155                 dev_err(&pdev->dev, "w1_add_master device failed\n");
156 @@ -195,6 +248,10 @@ static int w1_gpio_remove(struct platfor
157  
158         w1_remove_master_device(master);
159  
160 +       pdata->pin = w1_gpio_pin_orig;
161 +       pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin_orig;
162 +       pdata->parasitic_power = w1_gpio_pullup_orig;
163 +
164         return 0;
165  }
166  
167 --- a/drivers/w1/w1.h
168 +++ b/drivers/w1/w1.h
169 @@ -173,6 +173,12 @@ struct w1_bus_master
170  
171         u8              (*set_pullup)(void *, int);
172  
173 +       /**
174 +        * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
175 +        * @return -1=Error, 0=completed
176 +        */
177 +       void (*bitbang_pullup) (void *, u8);
178 +
179         void            (*search)(void *, struct w1_master *,
180                 u8, w1_slave_found_callback);
181  };
182 --- a/drivers/w1/w1_int.c
183 +++ b/drivers/w1/w1_int.c
184 @@ -122,6 +122,20 @@ int w1_add_master_device(struct w1_bus_m
185                 return(-EINVAL);
186         }
187  
188 +       /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
189 +        * and takes care of timing itself */
190 +       if (!master->write_byte && !master->touch_bit && master->set_pullup) {
191 +               printk(KERN_ERR "w1_add_master_device: set_pullup requires "
192 +                       "write_byte or touch_bit, disabling\n");
193 +               master->set_pullup = NULL;
194 +       }
195 +
196 +       if (master->set_pullup && master->bitbang_pullup) {
197 +               printk(KERN_ERR "w1_add_master_device: set_pullup should not "
198 +                      "be set when bitbang_pullup is used, disabling\n");
199 +               master->set_pullup = NULL;
200 +       }
201 +
202         /* Lock until the device is added (or not) to w1_masters. */
203         mutex_lock(&w1_mlock);
204         /* Search for the first available id (starting at 1). */
205 --- a/drivers/w1/w1_io.c
206 +++ b/drivers/w1/w1_io.c
207 @@ -134,10 +134,22 @@ static void w1_pre_write(struct w1_maste
208  static void w1_post_write(struct w1_master *dev)
209  {
210         if (dev->pullup_duration) {
211 -               if (dev->enable_pullup && dev->bus_master->set_pullup)
212 -                       dev->bus_master->set_pullup(dev->bus_master->data, 0);
213 -               else
214 +               if (dev->enable_pullup) {
215 +                       if (dev->bus_master->set_pullup) {
216 +                               dev->bus_master->set_pullup(dev->
217 +                                                           bus_master->data,
218 +                                                           0);
219 +                       } else if (dev->bus_master->bitbang_pullup) {
220 +                               dev->bus_master->
221 +                                   bitbang_pullup(dev->bus_master->data, 1);
222                         msleep(dev->pullup_duration);
223 +                               dev->bus_master->
224 +                                   bitbang_pullup(dev->bus_master->data, 0);
225 +                       }
226 +               } else {
227 +                       msleep(dev->pullup_duration);
228 +               }
229 +
230                 dev->pullup_duration = 0;
231         }
232  }
233 --- a/include/linux/w1-gpio.h
234 +++ b/include/linux/w1-gpio.h
235 @@ -18,6 +18,7 @@
236  struct w1_gpio_platform_data {
237         unsigned int pin;
238         unsigned int is_open_drain:1;
239 +       unsigned int parasitic_power:1;
240         void (*enable_external_pullup)(int enable);
241         unsigned int ext_pullup_enable_pin;
242         unsigned int pullup_duration;