brcm2708: add linux 4.1 support
[openwrt.git] / target / linux / brcm2708 / patches-4.1 / 0031-enabling-the-realtime-clock-1-wire-chip-DS1307-and-1.patch
1 From 9c50f6318fdf116e5abaeb05bffa0b65e48ba259 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 031/121] enabling the realtime clock 1-wire chip DS1307 and
5  1-wire on 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  arch/arm/mach-bcm2708/bcm2708.c | 29 +++++++++++++++++
25  arch/arm/mach-bcm2709/bcm2709.c | 29 +++++++++++++++++
26  drivers/w1/masters/w1-gpio.c    | 69 +++++++++++++++++++++++++++++++++++++----
27  drivers/w1/w1.h                 |  6 ++++
28  drivers/w1/w1_int.c             | 14 +++++++++
29  drivers/w1/w1_io.c              | 18 +++++++++--
30  include/linux/w1-gpio.h         |  1 +
31  7 files changed, 157 insertions(+), 9 deletions(-)
32
33 --- a/arch/arm/mach-bcm2708/bcm2708.c
34 +++ b/arch/arm/mach-bcm2708/bcm2708.c
35 @@ -36,6 +36,7 @@
36  #include <linux/of_platform.h>
37  #include <linux/spi/spi.h>
38  #include <linux/gpio/machine.h>
39 +#include <linux/w1-gpio.h>
40  
41  #include <linux/version.h>
42  #include <linux/clkdev.h>
43 @@ -77,12 +78,19 @@
44   */
45  #define DMA_MASK_BITS_COMMON 32
46  
47 +// use GPIO 4 for the one-wire GPIO pin, if enabled
48 +#define W1_GPIO 4
49 +// ensure one-wire GPIO pullup is disabled by default
50 +#define W1_PULLUP -1
51 +
52  /* command line parameters */
53  static unsigned boardrev, serial;
54  static unsigned uart_clock = UART0_CLOCK;
55  static unsigned disk_led_gpio = 16;
56  static unsigned disk_led_active_low = 1;
57  static unsigned reboot_part = 0;
58 +static unsigned w1_gpio_pin = W1_GPIO;
59 +static unsigned w1_gpio_pullup = W1_PULLUP;
60  static bool vc_i2c_override = false;
61  
62  static unsigned use_dt = 0;
63 @@ -303,6 +311,20 @@ static struct platform_device bcm2708_dm
64         .num_resources = ARRAY_SIZE(bcm2708_dmaengine_resources),
65  };
66  
67 +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
68 +static struct w1_gpio_platform_data w1_gpio_pdata = {
69 +       .pin = W1_GPIO,
70 +        .ext_pullup_enable_pin = W1_PULLUP,
71 +       .is_open_drain = 0,
72 +};
73 +
74 +static struct platform_device w1_device = {
75 +       .name = "w1-gpio",
76 +       .id = -1,
77 +       .dev.platform_data = &w1_gpio_pdata,
78 +};
79 +#endif
80 +
81  static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
82  
83  static struct platform_device bcm2708_fb_device = {
84 @@ -729,6 +751,11 @@ void __init bcm2708_init(void)
85  #ifdef CONFIG_BCM2708_GPIO
86         bcm_register_device_dt(&bcm2708_gpio_device);
87  #endif
88 +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
89 +       w1_gpio_pdata.pin = w1_gpio_pin;
90 +       w1_gpio_pdata.ext_pullup_enable_pin = w1_gpio_pullup;
91 +       bcm_register_device_dt(&w1_device);
92 +#endif
93         bcm_register_device_dt(&bcm2708_fb_device);
94         bcm_register_device_dt(&bcm2708_usb_device);
95  
96 @@ -942,5 +969,7 @@ module_param(uart_clock, uint, 0644);
97  module_param(disk_led_gpio, uint, 0644);
98  module_param(disk_led_active_low, uint, 0644);
99  module_param(reboot_part, uint, 0644);
100 +module_param(w1_gpio_pin, uint, 0644);
101 +module_param(w1_gpio_pullup, uint, 0644);
102  module_param(vc_i2c_override, bool, 0644);
103  MODULE_PARM_DESC(vc_i2c_override, "Allow the use of VC's I2C peripheral.");
104 --- a/arch/arm/mach-bcm2709/bcm2709.c
105 +++ b/arch/arm/mach-bcm2709/bcm2709.c
106 @@ -36,6 +36,7 @@
107  #include <linux/of_platform.h>
108  #include <linux/spi/spi.h>
109  #include <linux/gpio/machine.h>
110 +#include <linux/w1-gpio.h>
111  
112  #include <linux/version.h>
113  #include <linux/clkdev.h>
114 @@ -79,12 +80,19 @@
115   */
116  #define DMA_MASK_BITS_COMMON 32
117  
118 +// use GPIO 4 for the one-wire GPIO pin, if enabled
119 +#define W1_GPIO 4
120 +// ensure one-wire GPIO pullup is disabled by default
121 +#define W1_PULLUP -1
122 +
123  /* command line parameters */
124  static unsigned boardrev, serial;
125  static unsigned uart_clock = UART0_CLOCK;
126  static unsigned disk_led_gpio = 16;
127  static unsigned disk_led_active_low = 1;
128  static unsigned reboot_part = 0;
129 +static unsigned w1_gpio_pin = W1_GPIO;
130 +static unsigned w1_gpio_pullup = W1_PULLUP;
131  static bool vc_i2c_override = false;
132  
133  static unsigned use_dt = 0;
134 @@ -313,6 +321,20 @@ static struct platform_device bcm2708_dm
135         .num_resources = ARRAY_SIZE(bcm2708_dmaengine_resources),
136  };
137  
138 +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
139 +static struct w1_gpio_platform_data w1_gpio_pdata = {
140 +       .pin = W1_GPIO,
141 +        .ext_pullup_enable_pin = W1_PULLUP,
142 +       .is_open_drain = 0,
143 +};
144 +
145 +static struct platform_device w1_device = {
146 +       .name = "w1-gpio",
147 +       .id = -1,
148 +       .dev.platform_data = &w1_gpio_pdata,
149 +};
150 +#endif
151 +
152  static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
153  
154  static struct platform_device bcm2708_fb_device = {
155 @@ -749,6 +771,11 @@ void __init bcm2709_init(void)
156  #ifdef CONFIG_BCM2708_GPIO
157         bcm_register_device_dt(&bcm2708_gpio_device);
158  #endif
159 +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
160 +       w1_gpio_pdata.pin = w1_gpio_pin;
161 +       w1_gpio_pdata.ext_pullup_enable_pin = w1_gpio_pullup;
162 +       bcm_register_device_dt(&w1_device);
163 +#endif
164         bcm_register_device_dt(&bcm2708_fb_device);
165         bcm_register_device_dt(&bcm2708_usb_device);
166  
167 @@ -1110,5 +1137,7 @@ module_param(uart_clock, uint, 0644);
168  module_param(disk_led_gpio, uint, 0644);
169  module_param(disk_led_active_low, uint, 0644);
170  module_param(reboot_part, uint, 0644);
171 +module_param(w1_gpio_pin, uint, 0644);
172 +module_param(w1_gpio_pullup, uint, 0644);
173  module_param(vc_i2c_override, bool, 0644);
174  MODULE_PARM_DESC(vc_i2c_override, "Allow the use of VC's I2C peripheral.");
175 --- a/drivers/w1/masters/w1-gpio.c
176 +++ b/drivers/w1/masters/w1-gpio.c
177 @@ -23,6 +23,19 @@
178  #include "../w1.h"
179  #include "../w1_int.h"
180  
181 +static int w1_gpio_pullup = 0;
182 +static int w1_gpio_pullup_orig = 0;
183 +module_param_named(pullup, w1_gpio_pullup, int, 0);
184 +MODULE_PARM_DESC(pullup, "Enable parasitic power (power on data) mode");
185 +static int w1_gpio_pullup_pin = -1;
186 +static int w1_gpio_pullup_pin_orig = -1;
187 +module_param_named(extpullup, w1_gpio_pullup_pin, int, 0);
188 +MODULE_PARM_DESC(extpullup, "GPIO external pullup pin number");
189 +static int w1_gpio_pin = -1;
190 +static int w1_gpio_pin_orig = -1;
191 +module_param_named(gpiopin, w1_gpio_pin, int, 0);
192 +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
193 +
194  static u8 w1_gpio_set_pullup(void *data, int delay)
195  {
196         struct w1_gpio_platform_data *pdata = data;
197 @@ -67,6 +80,16 @@ static u8 w1_gpio_read_bit(void *data)
198         return gpio_get_value(pdata->pin) ? 1 : 0;
199  }
200  
201 +static void w1_gpio_bitbang_pullup(void *data, u8 on)
202 +{
203 +       struct w1_gpio_platform_data *pdata = data;
204 +
205 +       if (on)
206 +               gpio_direction_output(pdata->pin, 1);
207 +       else
208 +               gpio_direction_input(pdata->pin);
209 +}
210 +
211  #if defined(CONFIG_OF)
212  static const struct of_device_id w1_gpio_dt_ids[] = {
213         { .compatible = "w1-gpio" },
214 @@ -80,6 +103,7 @@ static int w1_gpio_probe_dt(struct platf
215         struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
216         struct device_node *np = pdev->dev.of_node;
217         int gpio;
218 +       u32 value;
219  
220         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
221         if (!pdata)
222 @@ -88,6 +112,9 @@ static int w1_gpio_probe_dt(struct platf
223         if (of_get_property(np, "linux,open-drain", NULL))
224                 pdata->is_open_drain = 1;
225  
226 +       if (of_property_read_u32(np, "rpi,parasitic-power", &value) == 0)
227 +           pdata->parasitic_power = (value != 0);
228 +
229         gpio = of_get_gpio(np, 0);
230         if (gpio < 0) {
231                 if (gpio != -EPROBE_DEFER)
232 @@ -103,7 +130,7 @@ static int w1_gpio_probe_dt(struct platf
233         if (gpio == -EPROBE_DEFER)
234                 return gpio;
235         /* ignore other errors as the pullup gpio is optional */
236 -       pdata->ext_pullup_enable_pin = gpio;
237 +       pdata->ext_pullup_enable_pin = (gpio >= 0) ? gpio : -1;
238  
239         pdev->dev.platform_data = pdata;
240  
241 @@ -113,13 +140,15 @@ static int w1_gpio_probe_dt(struct platf
242  static int w1_gpio_probe(struct platform_device *pdev)
243  {
244         struct w1_bus_master *master;
245 -       struct w1_gpio_platform_data *pdata;
246 +       struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
247         int err;
248  
249 -       if (of_have_populated_dt()) {
250 -               err = w1_gpio_probe_dt(pdev);
251 -               if (err < 0)
252 -                       return err;
253 +       if(pdata == NULL) {
254 +               if (of_have_populated_dt()) {
255 +                       err = w1_gpio_probe_dt(pdev);
256 +                       if (err < 0)
257 +                               return err;
258 +               }
259         }
260  
261         pdata = dev_get_platdata(&pdev->dev);
262 @@ -136,6 +165,22 @@ static int w1_gpio_probe(struct platform
263                 return -ENOMEM;
264         }
265  
266 +       w1_gpio_pin_orig = pdata->pin;
267 +       w1_gpio_pullup_pin_orig = pdata->ext_pullup_enable_pin;
268 +       w1_gpio_pullup_orig = pdata->parasitic_power;
269 +
270 +       if(gpio_is_valid(w1_gpio_pin)) {
271 +               pdata->pin = w1_gpio_pin;
272 +               pdata->ext_pullup_enable_pin = -1;
273 +               pdata->parasitic_power = -1;
274 +       }
275 +       pdata->parasitic_power |= w1_gpio_pullup;
276 +       if(gpio_is_valid(w1_gpio_pullup_pin)) {
277 +               pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin;
278 +       }
279 +
280 +       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);
281 +
282         err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
283         if (err) {
284                 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
285 @@ -165,6 +210,14 @@ static int w1_gpio_probe(struct platform
286                 master->set_pullup = w1_gpio_set_pullup;
287         }
288  
289 +       if (pdata->parasitic_power) {
290 +               if (pdata->is_open_drain)
291 +                       printk(KERN_ERR "w1-gpio 'pullup'(parasitic power) "
292 +                              "option doesn't work with open drain GPIO\n");
293 +               else
294 +                       master->bitbang_pullup = w1_gpio_bitbang_pullup;
295 +       }
296 +
297         err = w1_add_master_device(master);
298         if (err) {
299                 dev_err(&pdev->dev, "w1_add_master device failed\n");
300 @@ -195,6 +248,10 @@ static int w1_gpio_remove(struct platfor
301  
302         w1_remove_master_device(master);
303  
304 +       pdata->pin = w1_gpio_pin_orig;
305 +       pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin_orig;
306 +       pdata->parasitic_power = w1_gpio_pullup_orig;
307 +
308         return 0;
309  }
310  
311 --- a/drivers/w1/w1.h
312 +++ b/drivers/w1/w1.h
313 @@ -171,6 +171,12 @@ struct w1_bus_master
314  
315         u8              (*set_pullup)(void *, int);
316  
317 +       /**
318 +        * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
319 +        * @return -1=Error, 0=completed
320 +        */
321 +       void (*bitbang_pullup) (void *, u8);
322 +
323         void            (*search)(void *, struct w1_master *,
324                 u8, w1_slave_found_callback);
325  };
326 --- a/drivers/w1/w1_int.c
327 +++ b/drivers/w1/w1_int.c
328 @@ -123,6 +123,20 @@ int w1_add_master_device(struct w1_bus_m
329                 return(-EINVAL);
330         }
331  
332 +       /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
333 +        * and takes care of timing itself */
334 +       if (!master->write_byte && !master->touch_bit && master->set_pullup) {
335 +               printk(KERN_ERR "w1_add_master_device: set_pullup requires "
336 +                       "write_byte or touch_bit, disabling\n");
337 +               master->set_pullup = NULL;
338 +       }
339 +
340 +       if (master->set_pullup && master->bitbang_pullup) {
341 +               printk(KERN_ERR "w1_add_master_device: set_pullup should not "
342 +                      "be set when bitbang_pullup is used, disabling\n");
343 +               master->set_pullup = NULL;
344 +       }
345 +
346         /* Lock until the device is added (or not) to w1_masters. */
347         mutex_lock(&w1_mlock);
348         /* Search for the first available id (starting at 1). */
349 --- a/drivers/w1/w1_io.c
350 +++ b/drivers/w1/w1_io.c
351 @@ -134,10 +134,22 @@ static void w1_pre_write(struct w1_maste
352  static void w1_post_write(struct w1_master *dev)
353  {
354         if (dev->pullup_duration) {
355 -               if (dev->enable_pullup && dev->bus_master->set_pullup)
356 -                       dev->bus_master->set_pullup(dev->bus_master->data, 0);
357 -               else
358 +               if (dev->enable_pullup) {
359 +                       if (dev->bus_master->set_pullup) {
360 +                               dev->bus_master->set_pullup(dev->
361 +                                                           bus_master->data,
362 +                                                           0);
363 +                       } else if (dev->bus_master->bitbang_pullup) {
364 +                               dev->bus_master->
365 +                                   bitbang_pullup(dev->bus_master->data, 1);
366                         msleep(dev->pullup_duration);
367 +                               dev->bus_master->
368 +                                   bitbang_pullup(dev->bus_master->data, 0);
369 +                       }
370 +               } else {
371 +                       msleep(dev->pullup_duration);
372 +               }
373 +
374                 dev->pullup_duration = 0;
375         }
376  }
377 --- a/include/linux/w1-gpio.h
378 +++ b/include/linux/w1-gpio.h
379 @@ -18,6 +18,7 @@
380  struct w1_gpio_platform_data {
381         unsigned int pin;
382         unsigned int is_open_drain:1;
383 +       unsigned int parasitic_power:1;
384         void (*enable_external_pullup)(int enable);
385         unsigned int ext_pullup_enable_pin;
386         unsigned int pullup_duration;