brcm2708: refresh patches
[15.05/openwrt.git] / target / linux / brcm2708 / patches-3.18 / 0021-enabling-the-realtime-clock-1-wire-chip-DS1307-and-1.patch
1 From bd7d1508a83c544f2d52f668ebabe55c2ea207c5 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 021/114] 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  arch/arm/mach-bcm2708/bcm2708.c | 29 ++++++++++++++++++++++
23  drivers/w1/masters/w1-gpio.c    | 55 +++++++++++++++++++++++++++++++++++++----
24  drivers/w1/w1.h                 |  6 +++++
25  drivers/w1/w1_int.c             | 14 +++++++++++
26  drivers/w1/w1_io.c              | 18 +++++++++++---
27  5 files changed, 114 insertions(+), 8 deletions(-)
28
29 --- a/arch/arm/mach-bcm2708/bcm2708.c
30 +++ b/arch/arm/mach-bcm2708/bcm2708.c
31 @@ -32,6 +32,7 @@
32  #include <linux/io.h>
33  #include <linux/module.h>
34  #include <linux/spi/spi.h>
35 +#include <linux/w1-gpio.h>
36  
37  #include <linux/version.h>
38  #include <linux/clkdev.h>
39 @@ -76,12 +77,19 @@
40   */
41  #define DMA_MASK_BITS_COMMON 32
42  
43 +// use GPIO 4 for the one-wire GPIO pin, if enabled
44 +#define W1_GPIO 4
45 +// ensure one-wire GPIO pullup is disabled by default
46 +#define W1_PULLUP -1
47 +
48  /* command line parameters */
49  static unsigned boardrev, serial;
50  static unsigned uart_clock;
51  static unsigned disk_led_gpio = 16;
52  static unsigned disk_led_active_low = 1;
53  static unsigned reboot_part = 0;
54 +static unsigned w1_gpio_pin = W1_GPIO;
55 +static unsigned w1_gpio_pullup = W1_PULLUP;
56  
57  static void __init bcm2708_init_led(void);
58  
59 @@ -258,6 +266,20 @@ static struct platform_device bcm2708_dm
60         .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
61  };
62  
63 +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
64 +static struct w1_gpio_platform_data w1_gpio_pdata = {
65 +       .pin = W1_GPIO,
66 +        .ext_pullup_enable_pin = W1_PULLUP,
67 +       .is_open_drain = 0,
68 +};
69 +
70 +static struct platform_device w1_device = {
71 +       .name = "w1-gpio",
72 +       .id = -1,
73 +       .dev.platform_data = &w1_gpio_pdata,
74 +};
75 +#endif
76 +
77  static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
78  
79  static struct platform_device bcm2708_fb_device = {
80 @@ -652,6 +674,11 @@ void __init bcm2708_init(void)
81  #ifdef CONFIG_BCM2708_GPIO
82         bcm_register_device(&bcm2708_gpio_device);
83  #endif
84 +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
85 +       w1_gpio_pdata.pin = w1_gpio_pin;
86 +       w1_gpio_pdata.ext_pullup_enable_pin = w1_gpio_pullup;
87 +       platform_device_register(&w1_device);
88 +#endif
89         bcm_register_device(&bcm2708_systemtimer_device);
90         bcm_register_device(&bcm2708_fb_device);
91         bcm_register_device(&bcm2708_usb_device);
92 @@ -853,3 +880,5 @@ module_param(uart_clock, uint, 0644);
93  module_param(disk_led_gpio, uint, 0644);
94  module_param(disk_led_active_low, uint, 0644);
95  module_param(reboot_part, uint, 0644);
96 +module_param(w1_gpio_pin, uint, 0644);
97 +module_param(w1_gpio_pullup, uint, 0644);
98 --- a/drivers/w1/masters/w1-gpio.c
99 +++ b/drivers/w1/masters/w1-gpio.c
100 @@ -23,6 +23,15 @@
101  #include "../w1.h"
102  #include "../w1_int.h"
103  
104 +static int w1_gpio_pullup = -1;
105 +static int w1_gpio_pullup_orig = -1;
106 +module_param_named(pullup, w1_gpio_pullup, int, 0);
107 +MODULE_PARM_DESC(pullup, "GPIO pin pullup number");
108 +static int w1_gpio_pin = -1;
109 +static int w1_gpio_pin_orig = -1;
110 +module_param_named(gpiopin, w1_gpio_pin, int, 0);
111 +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
112 +
113  static u8 w1_gpio_set_pullup(void *data, int delay)
114  {
115         struct w1_gpio_platform_data *pdata = data;
116 @@ -67,6 +76,16 @@ static u8 w1_gpio_read_bit(void *data)
117         return gpio_get_value(pdata->pin) ? 1 : 0;
118  }
119  
120 +static void w1_gpio_bitbang_pullup(void *data, u8 on)
121 +{
122 +       struct w1_gpio_platform_data *pdata = data;
123 +
124 +       if (on)
125 +               gpio_direction_output(pdata->pin, 1);
126 +       else
127 +               gpio_direction_input(pdata->pin);
128 +}
129 +
130  #if defined(CONFIG_OF)
131  static struct of_device_id w1_gpio_dt_ids[] = {
132         { .compatible = "w1-gpio" },
133 @@ -113,13 +132,15 @@ static int w1_gpio_probe_dt(struct platf
134  static int w1_gpio_probe(struct platform_device *pdev)
135  {
136         struct w1_bus_master *master;
137 -       struct w1_gpio_platform_data *pdata;
138 +       struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
139         int err;
140  
141 -       if (of_have_populated_dt()) {
142 -               err = w1_gpio_probe_dt(pdev);
143 -               if (err < 0)
144 -                       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 +               }
151         }
152  
153         pdata = dev_get_platdata(&pdev->dev);
154 @@ -136,6 +157,19 @@ static int w1_gpio_probe(struct platform
155                 return -ENOMEM;
156         }
157  
158 +       w1_gpio_pin_orig = pdata->pin;
159 +       w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
160 +
161 +       if(gpio_is_valid(w1_gpio_pin)) {
162 +               pdata->pin = w1_gpio_pin;
163 +               pdata->ext_pullup_enable_pin = -1;
164 +       }
165 +       if(gpio_is_valid(w1_gpio_pullup)) {
166 +               pdata->ext_pullup_enable_pin = w1_gpio_pullup;
167 +       }
168 +
169 +       dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
170 +
171         err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
172         if (err) {
173                 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
174 @@ -165,6 +199,14 @@ static int w1_gpio_probe(struct platform
175                 master->set_pullup = w1_gpio_set_pullup;
176         }
177  
178 +       if (gpio_is_valid(w1_gpio_pullup)) {
179 +               if (pdata->is_open_drain)
180 +                       printk(KERN_ERR "w1-gpio 'pullup' option "
181 +                              "doesn't work with open drain GPIO\n");
182 +               else
183 +                       master->bitbang_pullup = w1_gpio_bitbang_pullup;
184 +       }
185 +
186         err = w1_add_master_device(master);
187         if (err) {
188                 dev_err(&pdev->dev, "w1_add_master device failed\n");
189 @@ -195,6 +237,9 @@ static int w1_gpio_remove(struct platfor
190  
191         w1_remove_master_device(master);
192  
193 +       pdata->pin = w1_gpio_pin_orig;
194 +       pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
195 +
196         return 0;
197  }
198  
199 --- a/drivers/w1/w1.h
200 +++ b/drivers/w1/w1.h
201 @@ -171,6 +171,12 @@ struct w1_bus_master
202  
203         u8              (*set_pullup)(void *, int);
204  
205 +       /**
206 +        * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
207 +        * @return -1=Error, 0=completed
208 +        */
209 +       void (*bitbang_pullup) (void *, u8);
210 +
211         void            (*search)(void *, struct w1_master *,
212                 u8, w1_slave_found_callback);
213  };
214 --- a/drivers/w1/w1_int.c
215 +++ b/drivers/w1/w1_int.c
216 @@ -123,6 +123,20 @@ int w1_add_master_device(struct w1_bus_m
217                 return(-EINVAL);
218         }
219  
220 +       /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
221 +        * and takes care of timing itself */
222 +       if (!master->write_byte && !master->touch_bit && master->set_pullup) {
223 +               printk(KERN_ERR "w1_add_master_device: set_pullup requires "
224 +                       "write_byte or touch_bit, disabling\n");
225 +               master->set_pullup = NULL;
226 +       }
227 +
228 +       if (master->set_pullup && master->bitbang_pullup) {
229 +               printk(KERN_ERR "w1_add_master_device: set_pullup should not "
230 +                      "be set when bitbang_pullup is used, disabling\n");
231 +               master->set_pullup = NULL;
232 +       }
233 +
234         /* Lock until the device is added (or not) to w1_masters. */
235         mutex_lock(&w1_mlock);
236         /* Search for the first available id (starting at 1). */
237 --- a/drivers/w1/w1_io.c
238 +++ b/drivers/w1/w1_io.c
239 @@ -134,10 +134,22 @@ static void w1_pre_write(struct w1_maste
240  static void w1_post_write(struct w1_master *dev)
241  {
242         if (dev->pullup_duration) {
243 -               if (dev->enable_pullup && dev->bus_master->set_pullup)
244 -                       dev->bus_master->set_pullup(dev->bus_master->data, 0);
245 -               else
246 +               if (dev->enable_pullup) {
247 +                       if (dev->bus_master->set_pullup) {
248 +                               dev->bus_master->set_pullup(dev->
249 +                                                           bus_master->data,
250 +                                                           0);
251 +                       } else if (dev->bus_master->bitbang_pullup) {
252 +                               dev->bus_master->
253 +                                   bitbang_pullup(dev->bus_master->data, 1);
254                         msleep(dev->pullup_duration);
255 +                               dev->bus_master->
256 +                                   bitbang_pullup(dev->bus_master->data, 0);
257 +                       }
258 +               } else {
259 +                       msleep(dev->pullup_duration);
260 +               }
261 +
262                 dev->pullup_duration = 0;
263         }
264  }