xref: /openbmc/linux/drivers/w1/masters/w1-gpio.c (revision ad8dc96e)
1ad8dc96eSVille Syrjala /*
2ad8dc96eSVille Syrjala  * w1-gpio - GPIO w1 bus master driver
3ad8dc96eSVille Syrjala  *
4ad8dc96eSVille Syrjala  * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
5ad8dc96eSVille Syrjala  *
6ad8dc96eSVille Syrjala  * This program is free software; you can redistribute it and/or modify
7ad8dc96eSVille Syrjala  * it under the terms of the GNU General Public License version 2
8ad8dc96eSVille Syrjala  * as published by the Free Software Foundation.
9ad8dc96eSVille Syrjala  */
10ad8dc96eSVille Syrjala 
11ad8dc96eSVille Syrjala #include <linux/init.h>
12ad8dc96eSVille Syrjala #include <linux/module.h>
13ad8dc96eSVille Syrjala #include <linux/platform_device.h>
14ad8dc96eSVille Syrjala #include <linux/w1-gpio.h>
15ad8dc96eSVille Syrjala 
16ad8dc96eSVille Syrjala #include "../w1.h"
17ad8dc96eSVille Syrjala #include "../w1_int.h"
18ad8dc96eSVille Syrjala 
19ad8dc96eSVille Syrjala #include <asm/gpio.h>
20ad8dc96eSVille Syrjala 
21ad8dc96eSVille Syrjala static void w1_gpio_write_bit_dir(void *data, u8 bit)
22ad8dc96eSVille Syrjala {
23ad8dc96eSVille Syrjala 	struct w1_gpio_platform_data *pdata = data;
24ad8dc96eSVille Syrjala 
25ad8dc96eSVille Syrjala 	if (bit)
26ad8dc96eSVille Syrjala 		gpio_direction_input(pdata->pin);
27ad8dc96eSVille Syrjala 	else
28ad8dc96eSVille Syrjala 		gpio_direction_output(pdata->pin, 0);
29ad8dc96eSVille Syrjala }
30ad8dc96eSVille Syrjala 
31ad8dc96eSVille Syrjala static void w1_gpio_write_bit_val(void *data, u8 bit)
32ad8dc96eSVille Syrjala {
33ad8dc96eSVille Syrjala 	struct w1_gpio_platform_data *pdata = data;
34ad8dc96eSVille Syrjala 
35ad8dc96eSVille Syrjala 	gpio_set_value(pdata->pin, bit);
36ad8dc96eSVille Syrjala }
37ad8dc96eSVille Syrjala 
38ad8dc96eSVille Syrjala static u8 w1_gpio_read_bit(void *data)
39ad8dc96eSVille Syrjala {
40ad8dc96eSVille Syrjala 	struct w1_gpio_platform_data *pdata = data;
41ad8dc96eSVille Syrjala 
42ad8dc96eSVille Syrjala 	return gpio_get_value(pdata->pin);
43ad8dc96eSVille Syrjala }
44ad8dc96eSVille Syrjala 
45ad8dc96eSVille Syrjala static int __init w1_gpio_probe(struct platform_device *pdev)
46ad8dc96eSVille Syrjala {
47ad8dc96eSVille Syrjala 	struct w1_bus_master *master;
48ad8dc96eSVille Syrjala 	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
49ad8dc96eSVille Syrjala 	int err;
50ad8dc96eSVille Syrjala 
51ad8dc96eSVille Syrjala 	if (!pdata)
52ad8dc96eSVille Syrjala 		return -ENXIO;
53ad8dc96eSVille Syrjala 
54ad8dc96eSVille Syrjala 	master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
55ad8dc96eSVille Syrjala 	if (!master)
56ad8dc96eSVille Syrjala 		return -ENOMEM;
57ad8dc96eSVille Syrjala 
58ad8dc96eSVille Syrjala 	err = gpio_request(pdata->pin, "w1");
59ad8dc96eSVille Syrjala 	if (err)
60ad8dc96eSVille Syrjala 		goto free_master;
61ad8dc96eSVille Syrjala 
62ad8dc96eSVille Syrjala 	master->data = pdata;
63ad8dc96eSVille Syrjala 	master->read_bit = w1_gpio_read_bit;
64ad8dc96eSVille Syrjala 
65ad8dc96eSVille Syrjala 	if (pdata->is_open_drain) {
66ad8dc96eSVille Syrjala 		gpio_direction_output(pdata->pin, 1);
67ad8dc96eSVille Syrjala 		master->write_bit = w1_gpio_write_bit_val;
68ad8dc96eSVille Syrjala 	} else {
69ad8dc96eSVille Syrjala 		gpio_direction_input(pdata->pin);
70ad8dc96eSVille Syrjala 		master->write_bit = w1_gpio_write_bit_dir;
71ad8dc96eSVille Syrjala 	}
72ad8dc96eSVille Syrjala 
73ad8dc96eSVille Syrjala 	err = w1_add_master_device(master);
74ad8dc96eSVille Syrjala 	if (err)
75ad8dc96eSVille Syrjala 		goto free_gpio;
76ad8dc96eSVille Syrjala 
77ad8dc96eSVille Syrjala 	platform_set_drvdata(pdev, master);
78ad8dc96eSVille Syrjala 
79ad8dc96eSVille Syrjala 	return 0;
80ad8dc96eSVille Syrjala 
81ad8dc96eSVille Syrjala  free_gpio:
82ad8dc96eSVille Syrjala 	gpio_free(pdata->pin);
83ad8dc96eSVille Syrjala  free_master:
84ad8dc96eSVille Syrjala 	kfree(master);
85ad8dc96eSVille Syrjala 
86ad8dc96eSVille Syrjala 	return err;
87ad8dc96eSVille Syrjala }
88ad8dc96eSVille Syrjala 
89ad8dc96eSVille Syrjala static int __exit w1_gpio_remove(struct platform_device *pdev)
90ad8dc96eSVille Syrjala {
91ad8dc96eSVille Syrjala 	struct w1_bus_master *master = platform_get_drvdata(pdev);
92ad8dc96eSVille Syrjala 	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
93ad8dc96eSVille Syrjala 
94ad8dc96eSVille Syrjala 	w1_remove_master_device(master);
95ad8dc96eSVille Syrjala 	gpio_free(pdata->pin);
96ad8dc96eSVille Syrjala 	kfree(master);
97ad8dc96eSVille Syrjala 
98ad8dc96eSVille Syrjala 	return 0;
99ad8dc96eSVille Syrjala }
100ad8dc96eSVille Syrjala 
101ad8dc96eSVille Syrjala static struct platform_driver w1_gpio_driver = {
102ad8dc96eSVille Syrjala 	.driver = {
103ad8dc96eSVille Syrjala 		.name	= "w1-gpio",
104ad8dc96eSVille Syrjala 		.owner	= THIS_MODULE,
105ad8dc96eSVille Syrjala 	},
106ad8dc96eSVille Syrjala 	.remove	= __exit_p(w1_gpio_remove),
107ad8dc96eSVille Syrjala };
108ad8dc96eSVille Syrjala 
109ad8dc96eSVille Syrjala static int __init w1_gpio_init(void)
110ad8dc96eSVille Syrjala {
111ad8dc96eSVille Syrjala 	return platform_driver_probe(&w1_gpio_driver, w1_gpio_probe);
112ad8dc96eSVille Syrjala }
113ad8dc96eSVille Syrjala 
114ad8dc96eSVille Syrjala static void __exit w1_gpio_exit(void)
115ad8dc96eSVille Syrjala {
116ad8dc96eSVille Syrjala 	platform_driver_unregister(&w1_gpio_driver);
117ad8dc96eSVille Syrjala }
118ad8dc96eSVille Syrjala 
119ad8dc96eSVille Syrjala module_init(w1_gpio_init);
120ad8dc96eSVille Syrjala module_exit(w1_gpio_exit);
121ad8dc96eSVille Syrjala 
122ad8dc96eSVille Syrjala MODULE_DESCRIPTION("GPIO w1 bus master driver");
123ad8dc96eSVille Syrjala MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
124ad8dc96eSVille Syrjala MODULE_LICENSE("GPL");
125