kernel: refresh patches
[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 --- 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 @@ -680,6 +702,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 @@ -883,3 +910,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 @@ -102,14 +121,16 @@ 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 - dev_err(&pdev->dev, "Failed to parse DT\n");
145 - return err;
146 + if(pdata == NULL) {
147 + if (of_have_populated_dt()) {
148 + err = w1_gpio_probe_dt(pdev);
149 + if (err < 0) {
150 + dev_err(&pdev->dev, "Failed to parse DT\n");
151 + return err;
152 + }
153 }
154 }
155
156 @@ -127,6 +148,19 @@ static int w1_gpio_probe(struct platform
157 return -ENOMEM;
158 }
159
160 + w1_gpio_pin_orig = pdata->pin;
161 + w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
162 +
163 + if(gpio_is_valid(w1_gpio_pin)) {
164 + pdata->pin = w1_gpio_pin;
165 + pdata->ext_pullup_enable_pin = -1;
166 + }
167 + if(gpio_is_valid(w1_gpio_pullup)) {
168 + pdata->ext_pullup_enable_pin = w1_gpio_pullup;
169 + }
170 +
171 + dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
172 +
173 err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
174 if (err) {
175 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
176 @@ -156,6 +190,14 @@ static int w1_gpio_probe(struct platform
177 master->set_pullup = w1_gpio_set_pullup;
178 }
179
180 + if (gpio_is_valid(w1_gpio_pullup)) {
181 + if (pdata->is_open_drain)
182 + printk(KERN_ERR "w1-gpio 'pullup' option "
183 + "doesn't work with open drain GPIO\n");
184 + else
185 + master->bitbang_pullup = w1_gpio_bitbang_pullup;
186 + }
187 +
188 err = w1_add_master_device(master);
189 if (err) {
190 dev_err(&pdev->dev, "w1_add_master device failed\n");
191 @@ -186,6 +228,9 @@ static int w1_gpio_remove(struct platfor
192
193 w1_remove_master_device(master);
194
195 + pdata->pin = w1_gpio_pin_orig;
196 + pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
197 +
198 return 0;
199 }
200
201 --- a/drivers/w1/w1.h
202 +++ b/drivers/w1/w1.h
203 @@ -148,6 +148,12 @@ struct w1_bus_master
204 */
205 u8 (*set_pullup)(void *, int);
206
207 + /**
208 + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
209 + * @return -1=Error, 0=completed
210 + */
211 + void (*bitbang_pullup) (void *, u8);
212 +
213 /** Really nice hardware can handles the different types of ROM search
214 * w1_master* is passed to the slave found callback.
215 */
216 --- a/drivers/w1/w1_int.c
217 +++ b/drivers/w1/w1_int.c
218 @@ -118,6 +118,20 @@ int w1_add_master_device(struct w1_bus_m
219 return(-EINVAL);
220 }
221
222 + /* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
223 + * and takes care of timing itself */
224 + if (!master->write_byte && !master->touch_bit && master->set_pullup) {
225 + printk(KERN_ERR "w1_add_master_device: set_pullup requires "
226 + "write_byte or touch_bit, disabling\n");
227 + master->set_pullup = NULL;
228 + }
229 +
230 + if (master->set_pullup && master->bitbang_pullup) {
231 + printk(KERN_ERR "w1_add_master_device: set_pullup should not "
232 + "be set when bitbang_pullup is used, disabling\n");
233 + master->set_pullup = NULL;
234 + }
235 +
236 /* Lock until the device is added (or not) to w1_masters. */
237 mutex_lock(&w1_mlock);
238 /* Search for the first available id (starting at 1). */
239 --- a/drivers/w1/w1_io.c
240 +++ b/drivers/w1/w1_io.c
241 @@ -127,10 +127,22 @@ static void w1_pre_write(struct w1_maste
242 static void w1_post_write(struct w1_master *dev)
243 {
244 if (dev->pullup_duration) {
245 - if (dev->enable_pullup && dev->bus_master->set_pullup)
246 - dev->bus_master->set_pullup(dev->bus_master->data, 0);
247 - else
248 + if (dev->enable_pullup) {
249 + if (dev->bus_master->set_pullup) {
250 + dev->bus_master->set_pullup(dev->
251 + bus_master->data,
252 + 0);
253 + } else if (dev->bus_master->bitbang_pullup) {
254 + dev->bus_master->
255 + bitbang_pullup(dev->bus_master->data, 1);
256 + msleep(dev->pullup_duration);
257 + dev->bus_master->
258 + bitbang_pullup(dev->bus_master->data, 0);
259 + }
260 + } else {
261 msleep(dev->pullup_duration);
262 + }
263 +
264 dev->pullup_duration = 0;
265 }
266 }