5a324dde61505b7e4b1acd6a79e5a8e025006f71
[librecmc/librecmc.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 Index: linux-4.9.111/drivers/w1/masters/w1-gpio.c
32 ===================================================================
33 --- linux-4.9.111.orig/drivers/w1/masters/w1-gpio.c
34 +++ linux-4.9.111/drivers/w1/masters/w1-gpio.c
35 @@ -23,6 +23,19 @@
36  #include "../w1.h"
37  #include "../w1_int.h"
38  
39 +static int w1_gpio_pullup = 0;
40 +static int w1_gpio_pullup_orig = 0;
41 +module_param_named(pullup, w1_gpio_pullup, int, 0);
42 +MODULE_PARM_DESC(pullup, "Enable parasitic power (power on data) mode");
43 +static int w1_gpio_pullup_pin = -1;
44 +static int w1_gpio_pullup_pin_orig = -1;
45 +module_param_named(extpullup, w1_gpio_pullup_pin, int, 0);
46 +MODULE_PARM_DESC(extpullup, "GPIO external pullup pin number");
47 +static int w1_gpio_pin = -1;
48 +static int w1_gpio_pin_orig = -1;
49 +module_param_named(gpiopin, w1_gpio_pin, int, 0);
50 +MODULE_PARM_DESC(gpiopin, "GPIO pin number");
51 +
52  static u8 w1_gpio_set_pullup(void *data, int delay)
53  {
54         struct w1_gpio_platform_data *pdata = data;
55 @@ -67,6 +80,16 @@ static u8 w1_gpio_read_bit(void *data)
56         return gpio_get_value(pdata->pin) ? 1 : 0;
57  }
58  
59 +static void w1_gpio_bitbang_pullup(void *data, u8 on)
60 +{
61 +       struct w1_gpio_platform_data *pdata = data;
62 +
63 +       if (on)
64 +               gpio_direction_output(pdata->pin, 1);
65 +       else
66 +               gpio_direction_input(pdata->pin);
67 +}
68 +
69  #if defined(CONFIG_OF)
70  static const struct of_device_id w1_gpio_dt_ids[] = {
71         { .compatible = "w1-gpio" },
72 @@ -80,6 +103,7 @@ static int w1_gpio_probe_dt(struct platf
73         struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
74         struct device_node *np = pdev->dev.of_node;
75         int gpio;
76 +       u32 value;
77  
78         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
79         if (!pdata)
80 @@ -88,6 +112,9 @@ static int w1_gpio_probe_dt(struct platf
81         if (of_get_property(np, "linux,open-drain", NULL))
82                 pdata->is_open_drain = 1;
83  
84 +       if (of_property_read_u32(np, "rpi,parasitic-power", &value) == 0)
85 +           pdata->parasitic_power = (value != 0);
86 +
87         gpio = of_get_gpio(np, 0);
88         if (gpio < 0) {
89                 if (gpio != -EPROBE_DEFER)
90 @@ -103,7 +130,7 @@ static int w1_gpio_probe_dt(struct platf
91         if (gpio == -EPROBE_DEFER)
92                 return gpio;
93         /* ignore other errors as the pullup gpio is optional */
94 -       pdata->ext_pullup_enable_pin = gpio;
95 +       pdata->ext_pullup_enable_pin = (gpio >= 0) ? gpio : -1;
96  
97         pdev->dev.platform_data = pdata;
98  
99 @@ -135,6 +162,22 @@ static int w1_gpio_probe(struct platform
100                 return -ENOMEM;
101         }
102  
103 +       w1_gpio_pin_orig = pdata->pin;
104 +       w1_gpio_pullup_pin_orig = pdata->ext_pullup_enable_pin;
105 +       w1_gpio_pullup_orig = pdata->parasitic_power;
106 +
107 +       if(gpio_is_valid(w1_gpio_pin)) {
108 +               pdata->pin = w1_gpio_pin;
109 +               pdata->ext_pullup_enable_pin = -1;
110 +               pdata->parasitic_power = -1;
111 +       }
112 +       pdata->parasitic_power |= w1_gpio_pullup;
113 +       if(gpio_is_valid(w1_gpio_pullup_pin)) {
114 +               pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin;
115 +       }
116 +
117 +       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);
118 +
119         err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
120         if (err) {
121                 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
122 @@ -164,6 +207,14 @@ static int w1_gpio_probe(struct platform
123                 master->set_pullup = w1_gpio_set_pullup;
124         }
125  
126 +       if (pdata->parasitic_power) {
127 +               if (pdata->is_open_drain)
128 +                       printk(KERN_ERR "w1-gpio 'pullup'(parasitic power) "
129 +                              "option doesn't work with open drain GPIO\n");
130 +               else
131 +                       master->bitbang_pullup = w1_gpio_bitbang_pullup;
132 +       }
133 +
134         err = w1_add_master_device(master);
135         if (err) {
136                 dev_err(&pdev->dev, "w1_add_master device failed\n");
137 @@ -194,6 +245,10 @@ static int w1_gpio_remove(struct platfor
138  
139         w1_remove_master_device(master);
140  
141 +       pdata->pin = w1_gpio_pin_orig;
142 +       pdata->ext_pullup_enable_pin = w1_gpio_pullup_pin_orig;
143 +       pdata->parasitic_power = w1_gpio_pullup_orig;
144 +
145         return 0;
146  }
147  
148 Index: linux-4.9.111/drivers/w1/w1.h
149 ===================================================================
150 --- linux-4.9.111.orig/drivers/w1/w1.h
151 +++ linux-4.9.111/drivers/w1/w1.h
152 @@ -173,6 +173,12 @@ struct w1_bus_master
153  
154         u8              (*set_pullup)(void *, int);
155  
156 +       /**
157 +        * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
158 +        * @return -1=Error, 0=completed
159 +        */
160 +       void (*bitbang_pullup) (void *, u8);
161 +
162         void            (*search)(void *, struct w1_master *,
163                 u8, w1_slave_found_callback);
164  };
165 Index: linux-4.9.111/drivers/w1/w1_int.c
166 ===================================================================
167 --- linux-4.9.111.orig/drivers/w1/w1_int.c
168 +++ linux-4.9.111/drivers/w1/w1_int.c
169 @@ -122,6 +122,20 @@ int w1_add_master_device(struct w1_bus_m
170                 return(-EINVAL);
171         }
172  
173 +       /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
174 +        * and takes care of timing itself */
175 +       if (!master->write_byte && !master->touch_bit && master->set_pullup) {
176 +               printk(KERN_ERR "w1_add_master_device: set_pullup requires "
177 +                       "write_byte or touch_bit, disabling\n");
178 +               master->set_pullup = NULL;
179 +       }
180 +
181 +       if (master->set_pullup && master->bitbang_pullup) {
182 +               printk(KERN_ERR "w1_add_master_device: set_pullup should not "
183 +                      "be set when bitbang_pullup is used, disabling\n");
184 +               master->set_pullup = NULL;
185 +       }
186 +
187         /* Lock until the device is added (or not) to w1_masters. */
188         mutex_lock(&w1_mlock);
189         /* Search for the first available id (starting at 1). */
190 Index: linux-4.9.111/drivers/w1/w1_io.c
191 ===================================================================
192 --- linux-4.9.111.orig/drivers/w1/w1_io.c
193 +++ linux-4.9.111/drivers/w1/w1_io.c
194 @@ -134,10 +134,22 @@ static void w1_pre_write(struct w1_maste
195  static void w1_post_write(struct w1_master *dev)
196  {
197         if (dev->pullup_duration) {
198 -               if (dev->enable_pullup && dev->bus_master->set_pullup)
199 -                       dev->bus_master->set_pullup(dev->bus_master->data, 0);
200 -               else
201 +               if (dev->enable_pullup) {
202 +                       if (dev->bus_master->set_pullup) {
203 +                               dev->bus_master->set_pullup(dev->
204 +                                                           bus_master->data,
205 +                                                           0);
206 +                       } else if (dev->bus_master->bitbang_pullup) {
207 +                               dev->bus_master->
208 +                                   bitbang_pullup(dev->bus_master->data, 1);
209                         msleep(dev->pullup_duration);
210 +                               dev->bus_master->
211 +                                   bitbang_pullup(dev->bus_master->data, 0);
212 +                       }
213 +               } else {
214 +                       msleep(dev->pullup_duration);
215 +               }
216 +
217                 dev->pullup_duration = 0;
218         }
219  }
220 Index: linux-4.9.111/include/linux/w1-gpio.h
221 ===================================================================
222 --- linux-4.9.111.orig/include/linux/w1-gpio.h
223 +++ linux-4.9.111/include/linux/w1-gpio.h
224 @@ -18,6 +18,7 @@
225  struct w1_gpio_platform_data {
226         unsigned int pin;
227         unsigned int is_open_drain:1;
228 +       unsigned int parasitic_power:1;
229         void (*enable_external_pullup)(int enable);
230         unsigned int ext_pullup_enable_pin;
231         unsigned int pullup_duration;