Skip to content

Commit 964040a

Browse files
committed
enabling the realtime clock 1-wire chip DS1307 and 1-wire on GPIO4 (as a module)
1-wire: Add support for configuring pin for w1-gpio kernel module See: #457 Add bitbanging pullups, use them for w1-gpio Allows parasite power to work, uses module option pullup=1
1 parent 30e564e commit 964040a

File tree

5 files changed

+78
-3
lines changed

5 files changed

+78
-3
lines changed

arch/arm/mach-bcm2708/bcm2708.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/io.h>
3333
#include <linux/module.h>
3434
#include <linux/spi/spi.h>
35+
#include <linux/w1-gpio.h>
3536

3637
#include <linux/version.h>
3738
#include <linux/clkdev.h>
@@ -76,12 +77,16 @@
7677
*/
7778
#define DMA_MASK_BITS_COMMON 32
7879

80+
// use GPIO 4 for the one-wire GPIO pin, if enabled
81+
#define W1_GPIO 4
82+
7983
/* command line parameters */
8084
static unsigned boardrev, serial;
8185
static unsigned uart_clock;
8286
static unsigned disk_led_gpio = 16;
8387
static unsigned disk_led_active_low = 1;
8488
static unsigned reboot_part = 0;
89+
static unsigned w1_gpio_pin = W1_GPIO;
8590

8691
static void __init bcm2708_init_led(void);
8792

@@ -258,6 +263,19 @@ static struct platform_device bcm2708_dmaman_device = {
258263
.num_resources = ARRAY_SIZE(bcm2708_dmaman_resources),
259264
};
260265

266+
#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
267+
static struct w1_gpio_platform_data w1_gpio_pdata = {
268+
.pin = W1_GPIO,
269+
.is_open_drain = 0,
270+
};
271+
272+
static struct platform_device w1_device = {
273+
.name = "w1-gpio",
274+
.id = -1,
275+
.dev.platform_data = &w1_gpio_pdata,
276+
};
277+
#endif
278+
261279
static u64 fb_dmamask = DMA_BIT_MASK(DMA_MASK_BITS_COMMON);
262280

263281
static struct platform_device bcm2708_fb_device = {
@@ -679,6 +697,10 @@ void __init bcm2708_init(void)
679697
bcm_register_device(&bcm2708_vcio_device);
680698
#ifdef CONFIG_BCM2708_GPIO
681699
bcm_register_device(&bcm2708_gpio_device);
700+
#endif
701+
#if defined(CONFIG_W1_MASTER_GPIO) || defined(CONFIG_W1_MASTER_GPIO_MODULE)
702+
w1_gpio_pdata.pin = w1_gpio_pin;
703+
platform_device_register(&w1_device);
682704
#endif
683705
bcm_register_device(&bcm2708_systemtimer_device);
684706
bcm_register_device(&bcm2708_fb_device);
@@ -880,3 +902,4 @@ module_param(uart_clock, uint, 0644);
880902
module_param(disk_led_gpio, uint, 0644);
881903
module_param(disk_led_active_low, uint, 0644);
882904
module_param(reboot_part, uint, 0644);
905+
module_param(w1_gpio_pin, uint, 0644);

drivers/w1/masters/w1-gpio.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include "../w1.h"
2323
#include "../w1_int.h"
2424

25+
static int w1_gpio_pullup = 0;
26+
module_param_named(pullup, w1_gpio_pullup, int, 0);
27+
2528
static void w1_gpio_write_bit_dir(void *data, u8 bit)
2629
{
2730
struct w1_gpio_platform_data *pdata = data;
@@ -46,6 +49,16 @@ static u8 w1_gpio_read_bit(void *data)
4649
return gpio_get_value(pdata->pin) ? 1 : 0;
4750
}
4851

52+
static void w1_gpio_bitbang_pullup(void *data, u8 on)
53+
{
54+
struct w1_gpio_platform_data *pdata = data;
55+
56+
if (on)
57+
gpio_direction_output(pdata->pin, 1);
58+
else
59+
gpio_direction_input(pdata->pin);
60+
}
61+
4962
#if defined(CONFIG_OF)
5063
static struct of_device_id w1_gpio_dt_ids[] = {
5164
{ .compatible = "w1-gpio" },
@@ -127,6 +140,13 @@ static int w1_gpio_probe(struct platform_device *pdev)
127140
master->write_bit = w1_gpio_write_bit_dir;
128141
}
129142

143+
if (w1_gpio_pullup)
144+
if (pdata->is_open_drain)
145+
printk(KERN_ERR "w1-gpio 'pullup' option "
146+
"doesn't work with open drain GPIO\n");
147+
else
148+
master->bitbang_pullup = w1_gpio_bitbang_pullup;
149+
130150
err = w1_add_master_device(master);
131151
if (err) {
132152
dev_err(&pdev->dev, "w1_add_master device failed\n");

drivers/w1/w1.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ struct w1_bus_master
148148
*/
149149
u8 (*set_pullup)(void *, int);
150150

151+
/**
152+
* Turns the pullup on/off in bitbanging mode, takes an on/off argument.
153+
* @return -1=Error, 0=completed
154+
*/
155+
void (*bitbang_pullup) (void *, u8);
156+
151157
/** Really nice hardware can handles the different types of ROM search
152158
* w1_master* is passed to the slave found callback.
153159
*/

drivers/w1/w1_int.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ int w1_add_master_device(struct w1_bus_master *master)
130130
master->set_pullup = NULL;
131131
}
132132

133+
/* bitbanging hardware uses bitbang_pullup, other hardware uses set_pullup
134+
* and takes care of timing itself */
135+
if (!master->write_byte && !master->touch_bit && master->set_pullup) {
136+
printk(KERN_ERR "w1_add_master_device: set_pullup requires "
137+
"write_byte or touch_bit, disabling\n");
138+
master->set_pullup = NULL;
139+
}
140+
141+
if (master->set_pullup && master->bitbang_pullup) {
142+
printk(KERN_ERR "w1_add_master_device: set_pullup should not "
143+
"be set when bitbang_pullup is used, disabling\n");
144+
master->set_pullup = NULL;
145+
}
146+
133147
/* Lock until the device is added (or not) to w1_masters. */
134148
mutex_lock(&w1_mlock);
135149
/* Search for the first available id (starting at 1). */

drivers/w1/w1_io.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,22 @@ static void w1_pre_write(struct w1_master *dev)
127127
static void w1_post_write(struct w1_master *dev)
128128
{
129129
if (dev->pullup_duration) {
130-
if (dev->enable_pullup && dev->bus_master->set_pullup)
131-
dev->bus_master->set_pullup(dev->bus_master->data, 0);
132-
else
130+
if (dev->enable_pullup) {
131+
if (dev->bus_master->set_pullup) {
132+
dev->bus_master->set_pullup(dev->
133+
bus_master->data,
134+
0);
135+
} else if (dev->bus_master->bitbang_pullup) {
136+
dev->bus_master->
137+
bitbang_pullup(dev->bus_master->data, 1);
138+
msleep(dev->pullup_duration);
139+
dev->bus_master->
140+
bitbang_pullup(dev->bus_master->data, 0);
141+
}
142+
} else {
133143
msleep(dev->pullup_duration);
144+
}
145+
134146
dev->pullup_duration = 0;
135147
}
136148
}

0 commit comments

Comments
 (0)