2 * w1-gpio - GPIO w1 bus master driver
4 * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/w1-gpio.h>
18 #include "../w1_int.h"
22 static void w1_gpio_write_bit_dir(void *data
, u8 bit
)
24 struct w1_gpio_platform_data
*pdata
= data
;
27 gpio_direction_input(pdata
->pin
);
29 gpio_direction_output(pdata
->pin
, 0);
32 static void w1_gpio_write_bit_val(void *data
, u8 bit
)
34 struct w1_gpio_platform_data
*pdata
= data
;
36 gpio_set_value(pdata
->pin
, bit
);
39 static u8
w1_gpio_read_bit(void *data
)
41 struct w1_gpio_platform_data
*pdata
= data
;
43 return gpio_get_value(pdata
->pin
) ? 1 : 0;
46 static int __init
w1_gpio_probe(struct platform_device
*pdev
)
48 struct w1_bus_master
*master
;
49 struct w1_gpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
55 master
= kzalloc(sizeof(struct w1_bus_master
), GFP_KERNEL
);
59 err
= gpio_request(pdata
->pin
, "w1");
64 master
->read_bit
= w1_gpio_read_bit
;
66 if (pdata
->is_open_drain
) {
67 gpio_direction_output(pdata
->pin
, 1);
68 master
->write_bit
= w1_gpio_write_bit_val
;
70 gpio_direction_input(pdata
->pin
);
71 master
->write_bit
= w1_gpio_write_bit_dir
;
74 err
= w1_add_master_device(master
);
78 if (pdata
->enable_external_pullup
)
79 pdata
->enable_external_pullup(1);
81 platform_set_drvdata(pdev
, master
);
86 gpio_free(pdata
->pin
);
93 static int __exit
w1_gpio_remove(struct platform_device
*pdev
)
95 struct w1_bus_master
*master
= platform_get_drvdata(pdev
);
96 struct w1_gpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
98 if (pdata
->enable_external_pullup
)
99 pdata
->enable_external_pullup(0);
101 w1_remove_master_device(master
);
102 gpio_free(pdata
->pin
);
110 static int w1_gpio_suspend(struct platform_device
*pdev
, pm_message_t state
)
112 struct w1_gpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
114 if (pdata
->enable_external_pullup
)
115 pdata
->enable_external_pullup(0);
120 static int w1_gpio_resume(struct platform_device
*pdev
)
122 struct w1_gpio_platform_data
*pdata
= pdev
->dev
.platform_data
;
124 if (pdata
->enable_external_pullup
)
125 pdata
->enable_external_pullup(1);
131 #define w1_gpio_suspend NULL
132 #define w1_gpio_resume NULL
135 static struct platform_driver w1_gpio_driver
= {
138 .owner
= THIS_MODULE
,
140 .remove
= __exit_p(w1_gpio_remove
),
141 .suspend
= w1_gpio_suspend
,
142 .resume
= w1_gpio_resume
,
145 static int __init
w1_gpio_init(void)
147 return platform_driver_probe(&w1_gpio_driver
, w1_gpio_probe
);
150 static void __exit
w1_gpio_exit(void)
152 platform_driver_unregister(&w1_gpio_driver
);
155 module_init(w1_gpio_init
);
156 module_exit(w1_gpio_exit
);
158 MODULE_DESCRIPTION("GPIO w1 bus master driver");
159 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
160 MODULE_LICENSE("GPL");