xref: /openbmc/linux/drivers/w1/masters/w1-gpio.c (revision 05bcf503)
1 /*
2  * w1-gpio - GPIO w1 bus master driver
3  *
4  * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
5  *
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.
9  */
10 
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>
16 #include <linux/gpio.h>
17 #include <linux/of_platform.h>
18 #include <linux/of_gpio.h>
19 
20 #include "../w1.h"
21 #include "../w1_int.h"
22 
23 static void w1_gpio_write_bit_dir(void *data, u8 bit)
24 {
25 	struct w1_gpio_platform_data *pdata = data;
26 
27 	if (bit)
28 		gpio_direction_input(pdata->pin);
29 	else
30 		gpio_direction_output(pdata->pin, 0);
31 }
32 
33 static void w1_gpio_write_bit_val(void *data, u8 bit)
34 {
35 	struct w1_gpio_platform_data *pdata = data;
36 
37 	gpio_set_value(pdata->pin, bit);
38 }
39 
40 static u8 w1_gpio_read_bit(void *data)
41 {
42 	struct w1_gpio_platform_data *pdata = data;
43 
44 	return gpio_get_value(pdata->pin) ? 1 : 0;
45 }
46 
47 #ifdef CONFIG_OF
48 static struct of_device_id w1_gpio_dt_ids[] = {
49 	{ .compatible = "w1-gpio" },
50 	{}
51 };
52 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
53 
54 static int w1_gpio_probe_dt(struct platform_device *pdev)
55 {
56 	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
57 	struct device_node *np = pdev->dev.of_node;
58 	const struct of_device_id *of_id =
59 			of_match_device(w1_gpio_dt_ids, &pdev->dev);
60 
61 	if (!of_id)
62 		return 0;
63 
64 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
65 	if (!pdata)
66 		return -ENOMEM;
67 
68 	if (of_get_property(np, "linux,open-drain", NULL))
69 		pdata->is_open_drain = 1;
70 
71 	pdata->pin = of_get_gpio(np, 0);
72 	pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
73 	pdev->dev.platform_data = pdata;
74 
75 	return 0;
76 }
77 #else
78 static int w1_gpio_probe_dt(struct platform_device *pdev)
79 {
80 	return 0;
81 }
82 #endif
83 
84 static int __init w1_gpio_probe(struct platform_device *pdev)
85 {
86 	struct w1_bus_master *master;
87 	struct w1_gpio_platform_data *pdata;
88 	int err;
89 
90 	err = w1_gpio_probe_dt(pdev);
91 	if (err < 0)
92 		return err;
93 
94 	pdata = pdev->dev.platform_data;
95 
96 	if (!pdata)
97 		return -ENXIO;
98 
99 	master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
100 	if (!master)
101 		return -ENOMEM;
102 
103 	err = gpio_request(pdata->pin, "w1");
104 	if (err)
105 		goto free_master;
106 
107 	if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
108 		err = gpio_request_one(pdata->ext_pullup_enable_pin,
109 				       GPIOF_INIT_LOW, "w1 pullup");
110 		if (err < 0)
111 			goto free_gpio;
112 	}
113 
114 	master->data = pdata;
115 	master->read_bit = w1_gpio_read_bit;
116 
117 	if (pdata->is_open_drain) {
118 		gpio_direction_output(pdata->pin, 1);
119 		master->write_bit = w1_gpio_write_bit_val;
120 	} else {
121 		gpio_direction_input(pdata->pin);
122 		master->write_bit = w1_gpio_write_bit_dir;
123 	}
124 
125 	err = w1_add_master_device(master);
126 	if (err)
127 		goto free_gpio_ext_pu;
128 
129 	if (pdata->enable_external_pullup)
130 		pdata->enable_external_pullup(1);
131 
132 	if (gpio_is_valid(pdata->ext_pullup_enable_pin))
133 		gpio_set_value(pdata->ext_pullup_enable_pin, 1);
134 
135 	platform_set_drvdata(pdev, master);
136 
137 	return 0;
138 
139  free_gpio_ext_pu:
140 	if (gpio_is_valid(pdata->ext_pullup_enable_pin))
141 		gpio_free(pdata->ext_pullup_enable_pin);
142  free_gpio:
143 	gpio_free(pdata->pin);
144  free_master:
145 	kfree(master);
146 
147 	return err;
148 }
149 
150 static int __exit w1_gpio_remove(struct platform_device *pdev)
151 {
152 	struct w1_bus_master *master = platform_get_drvdata(pdev);
153 	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
154 
155 	if (pdata->enable_external_pullup)
156 		pdata->enable_external_pullup(0);
157 
158 	if (gpio_is_valid(pdata->ext_pullup_enable_pin))
159 		gpio_set_value(pdata->ext_pullup_enable_pin, 0);
160 
161 	w1_remove_master_device(master);
162 	gpio_free(pdata->pin);
163 	kfree(master);
164 
165 	return 0;
166 }
167 
168 #ifdef CONFIG_PM
169 
170 static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
171 {
172 	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
173 
174 	if (pdata->enable_external_pullup)
175 		pdata->enable_external_pullup(0);
176 
177 	return 0;
178 }
179 
180 static int w1_gpio_resume(struct platform_device *pdev)
181 {
182 	struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
183 
184 	if (pdata->enable_external_pullup)
185 		pdata->enable_external_pullup(1);
186 
187 	return 0;
188 }
189 
190 #else
191 #define w1_gpio_suspend	NULL
192 #define w1_gpio_resume	NULL
193 #endif
194 
195 static struct platform_driver w1_gpio_driver = {
196 	.driver = {
197 		.name	= "w1-gpio",
198 		.owner	= THIS_MODULE,
199 		.of_match_table = of_match_ptr(w1_gpio_dt_ids),
200 	},
201 	.remove	= __exit_p(w1_gpio_remove),
202 	.suspend = w1_gpio_suspend,
203 	.resume = w1_gpio_resume,
204 };
205 
206 static int __init w1_gpio_init(void)
207 {
208 	return platform_driver_probe(&w1_gpio_driver, w1_gpio_probe);
209 }
210 
211 static void __exit w1_gpio_exit(void)
212 {
213 	platform_driver_unregister(&w1_gpio_driver);
214 }
215 
216 module_init(w1_gpio_init);
217 module_exit(w1_gpio_exit);
218 
219 MODULE_DESCRIPTION("GPIO w1 bus master driver");
220 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
221 MODULE_LICENSE("GPL");
222