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)
7 1-wire: Add support for configuring pin for w1-gpio kernel module
8 See: https://github.com/raspberrypi/linux/pull/457
10 Add bitbanging pullups, use them for w1-gpio
12 Allows parasite power to work, uses module option pullup=1
14 bcm2708: Ensure 1-wire pullup is disabled by default, and expose as module parameter
16 Signed-off-by: Alex J Lennon <ajlennon@dynamicdevices.co.uk>
18 w1-gpio: Add gpiopin module parameter and correctly free up gpio pull-up pin, if set
20 Signed-off-by: Alex J Lennon <ajlennon@dynamicdevices.co.uk>
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(-)
29 --- a/arch/arm/mach-bcm2708/bcm2708.c
30 +++ b/arch/arm/mach-bcm2708/bcm2708.c
33 #include <linux/module.h>
34 #include <linux/spi/spi.h>
35 +#include <linux/w1-gpio.h>
37 #include <linux/version.h>
38 #include <linux/clkdev.h>
41 #define DMA_MASK_BITS_COMMON 32
43 +// use GPIO 4 for the one-wire GPIO pin, if enabled
45 +// ensure one-wire GPIO pullup is disabled by default
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;
57 static void __init bcm2708_init_led(void);
59 @@ -258,6 +266,20 @@ static struct platform_device bcm2708_dm
60 .num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
63 +#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
64 +static struct w1_gpio_platform_data w1_gpio_pdata = {
66 + .ext_pullup_enable_pin = W1_PULLUP,
70 +static struct platform_device w1_device = {
73 + .dev.platform_data = &w1_gpio_pdata,
77 static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
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);
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);
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
102 #include "../w1_int.h"
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");
113 static u8 w1_gpio_set_pullup(void *data, int delay)
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;
120 +static void w1_gpio_bitbang_pullup(void *data, u8 on)
122 + struct w1_gpio_platform_data *pdata = data;
125 + gpio_direction_output(pdata->pin, 1);
127 + gpio_direction_input(pdata->pin);
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)
136 struct w1_bus_master *master;
137 - struct w1_gpio_platform_data *pdata;
138 + struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
141 - if (of_have_populated_dt()) {
142 - err = w1_gpio_probe_dt(pdev);
144 - dev_err(&pdev->dev, "Failed to parse DT\n");
146 + if(pdata == NULL) {
147 + if (of_have_populated_dt()) {
148 + err = w1_gpio_probe_dt(pdev);
150 + dev_err(&pdev->dev, "Failed to parse DT\n");
156 @@ -127,6 +148,19 @@ static int w1_gpio_probe(struct platform
160 + w1_gpio_pin_orig = pdata->pin;
161 + w1_gpio_pullup_orig = pdata->ext_pullup_enable_pin;
163 + if(gpio_is_valid(w1_gpio_pin)) {
164 + pdata->pin = w1_gpio_pin;
165 + pdata->ext_pullup_enable_pin = -1;
167 + if(gpio_is_valid(w1_gpio_pullup)) {
168 + pdata->ext_pullup_enable_pin = w1_gpio_pullup;
171 + dev_info(&pdev->dev, "gpio pin %d, gpio pullup pin %d\n", pdata->pin, pdata->ext_pullup_enable_pin);
173 err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
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;
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");
185 + master->bitbang_pullup = w1_gpio_bitbang_pullup;
188 err = w1_add_master_device(master);
190 dev_err(&pdev->dev, "w1_add_master device failed\n");
191 @@ -186,6 +228,9 @@ static int w1_gpio_remove(struct platfor
193 w1_remove_master_device(master);
195 + pdata->pin = w1_gpio_pin_orig;
196 + pdata->ext_pullup_enable_pin = w1_gpio_pullup_orig;
201 --- a/drivers/w1/w1.h
202 +++ b/drivers/w1/w1.h
203 @@ -148,6 +148,12 @@ struct w1_bus_master
205 u8 (*set_pullup)(void *, int);
208 + * Turns the pullup on/off in bitbanging mode, takes an on/off argument.
209 + * @return -1=Error, 0=completed
211 + void (*bitbang_pullup) (void *, u8);
213 /** Really nice hardware can handles the different types of ROM search
214 * w1_master* is passed to the slave found callback.
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
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;
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;
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)
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);
248 + if (dev->enable_pullup) {
249 + if (dev->bus_master->set_pullup) {
250 + dev->bus_master->set_pullup(dev->
253 + } else if (dev->bus_master->bitbang_pullup) {
255 + bitbang_pullup(dev->bus_master->data, 1);
256 + msleep(dev->pullup_duration);
258 + bitbang_pullup(dev->bus_master->data, 0);
261 msleep(dev->pullup_duration);
264 dev->pullup_duration = 0;