b38b5d175ebbb220b38942322db2c692241449c6
[openwrt/openwrt.git] / target / linux / brcm2708 / patches-3.14 / 0019-enabling-the-realtime-clock-1-wire-chip-DS1307-and-1.patch
1 From 370acd9248703fb8531cd87982fb02dcce32a2b4 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 19/54] 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 | 57 ++++++++++++++++++++++++++++++++++++-----
24 drivers/w1/w1.h | 6 +++++
25 drivers/w1/w1_int.c | 14 ++++++++++
26 drivers/w1/w1_io.c | 18 ++++++++++---
27 5 files changed, 115 insertions(+), 9 deletions(-)
28
29 diff --git a/arch/arm/mach-bcm2708/bcm2708.c b/arch/arm/mach-bcm2708/bcm2708.c
30 index e892006..221d145 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 @@ -680,6 +702,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 @@ -883,3 +910,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 9709b8b..b10f9c9 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 @@ -102,14 +121,16 @@ 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 - dev_err(&pdev->dev, "Failed to parse DT\n");
149 - return err;
150 + if(pdata == NULL) {
151 + if (of_have_populated_dt()) {
152 + err = w1_gpio_probe_dt(pdev);
153 + if (err < 0) {
154 + dev_err(&pdev->dev, "Failed to parse DT\n");
155 + return err;
156 + }
157 }
158 }
159
160 @@ -127,6 +148,19 @@ static int w1_gpio_probe(struct platform_device *pdev)
161 return -ENOMEM;
162 }
163
164 + w1_gpio_pin_orig = pdata->pin;
165 + w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
166 +
167 + if(gpio_is_valid(w1_gpio_pin)) {
168 + pdata->pin = w1_gpio_pin;
169 + pdata->ext_pullup_enable_pin = -1;
170 + }
171 + if(gpio_is_valid(w1_gpio_pullup)) {
172 + pdata->ext_pullup_enable_pin = w1_gpio_pullup;
173 + }
174 +
175 + dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
176 +
177 err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
178 if (err) {
179 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
180 @@ -156,6 +190,14 @@ static int w1_gpio_probe(struct platform_device *pdev)
181 master->set_pullup = w1_gpio_set_pullup;
182 }
183
184 + if (gpio_is_valid(w1_gpio_pullup)) {
185 + if (pdata->is_open_drain)
186 + printk(KERN_ERR "w1-gpio 'pullup' option "
187 + "doesn't work with open drain GPIO\n");
188 + else
189 + master->bitbang_pullup = w1_gpio_bitbang_pullup;
190 + }
191 +
192 err = w1_add_master_device(master);
193 if (err) {
194 dev_err(&pdev->dev, "w1_add_master device failed\n");
195 @@ -186,6 +228,9 @@ static int w1_gpio_remove(struct platform_device *pdev)
196
197 w1_remove_master_device(master);
198
199 + pdata->pin = w1_gpio_pin_orig;
200 + pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
201 +
202 return 0;
203 }
204
205 diff --git a/drivers/w1/w1.h b/drivers/w1/w1.h
206 index ca8081a..3392959 100644
207 --- a/drivers/w1/w1.h
208 +++ b/drivers/w1/w1.h
209 @@ -148,6 +148,12 @@ struct w1_bus_master
210 */
211 u8 (*set_pullup)(void *, int);
212
213 + /**
214 + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
215 + * @return -1=Error, 0=completed
216 + */
217 + void (*bitbang_pullup) (void *, u8);
218 +
219 /** Really nice hardware can handles the different types of ROM search
220 * w1_master* is passed to the slave found callback.
221 */
222 diff --git a/drivers/w1/w1_int.c b/drivers/w1/w1_int.c
223 index 590bd8a..a4d69b6 100644
224 --- a/drivers/w1/w1_int.c
225 +++ b/drivers/w1/w1_int.c
226 @@ -118,6 +118,20 @@ int w1_add_master_device(struct w1_bus_master *master)
227 return(-EINVAL);
228 }
229
230 + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
231 + * and takes care of timing itself */
232 + if (!master->write_byte && !master->touch_bit && master->set_pullup) {
233 + printk(KERN_ERR "w1_add_master_device: set_pullup requires "
234 + "write_byte or touch_bit, disabling\n");
235 + master->set_pullup = NULL;
236 + }
237 +
238 + if (master->set_pullup && master->bitbang_pullup) {
239 + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
240 + "be set when bitbang_pullup is used, disabling\n");
241 + master->set_pullup = NULL;
242 + }
243 +
244 /* Lock until the device is added (or not) to w1_masters. */
245 mutex_lock(&w1_mlock);
246 /* Search for the first available id (starting at 1). */
247 diff --git a/drivers/w1/w1_io.c b/drivers/w1/w1_io.c
248 index e10acc2..667fdd5 100644
249 --- a/drivers/w1/w1_io.c
250 +++ b/drivers/w1/w1_io.c
251 @@ -127,10 +127,22 @@ static void w1_pre_write(struct w1_master *dev)
252 static void w1_post_write(struct w1_master *dev)
253 {
254 if (dev->pullup_duration) {
255 - if (dev->enable_pullup && dev->bus_master->set_pullup)
256 - dev->bus_master->set_pullup(dev->bus_master->data, 0);
257 - else
258 + if (dev->enable_pullup) {
259 + if (dev->bus_master->set_pullup) {
260 + dev->bus_master->set_pullup(dev->
261 + bus_master->data,
262 + 0);
263 + } else if (dev->bus_master->bitbang_pullup) {
264 + dev->bus_master->
265 + bitbang_pullup(dev->bus_master->data, 1);
266 + msleep(dev->pullup_duration);
267 + dev->bus_master->
268 + bitbang_pullup(dev->bus_master->data, 0);
269 + }
270 + } else {
271 msleep(dev->pullup_duration);
272 + }
273 +
274 dev->pullup_duration = 0;
275 }
276 }
277 --
278 1.9.1
279