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