blob: a90728ceec5ad8968489021f9da44e161f399998 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
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#include <linux/err.h>
20#include <linux/of.h>
21#include <linux/delay.h>
22
23#include <linux/w1.h>
24
25static u8 w1_gpio_set_pullup(void *data, int delay)
26{
27 struct w1_gpio_platform_data *pdata = data;
28
29 if (delay) {
30 pdata->pullup_duration = delay;
31 } else {
32 if (pdata->pullup_duration) {
33 gpio_direction_output(pdata->pin, 1);
34
35 msleep(pdata->pullup_duration);
36
37 gpio_direction_input(pdata->pin);
38 }
39 pdata->pullup_duration = 0;
40 }
41
42 return 0;
43}
44
45static void w1_gpio_write_bit_dir(void *data, u8 bit)
46{
47 struct w1_gpio_platform_data *pdata = data;
48
49 if (bit)
50 gpio_direction_input(pdata->pin);
51 else
52 gpio_direction_output(pdata->pin, 0);
53}
54
55static void w1_gpio_write_bit_val(void *data, u8 bit)
56{
57 struct w1_gpio_platform_data *pdata = data;
58
59 gpio_set_value(pdata->pin, bit);
60}
61
62static u8 w1_gpio_read_bit(void *data)
63{
64 struct w1_gpio_platform_data *pdata = data;
65
66 return gpio_get_value(pdata->pin) ? 1 : 0;
67}
68
69#if defined(CONFIG_OF)
70static const struct of_device_id w1_gpio_dt_ids[] = {
71 { .compatible = "w1-gpio" },
72 {}
73};
74MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
75#endif
76
77static int w1_gpio_probe_dt(struct platform_device *pdev)
78{
79 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
80 struct device_node *np = pdev->dev.of_node;
81 int gpio;
82
83 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
84 if (!pdata)
85 return -ENOMEM;
86
87 if (of_get_property(np, "linux,open-drain", NULL))
88 pdata->is_open_drain = 1;
89
90 gpio = of_get_gpio(np, 0);
91 if (gpio < 0) {
92 if (gpio != -EPROBE_DEFER)
93 dev_err(&pdev->dev,
94 "Failed to parse gpio property for data pin (%d)\n",
95 gpio);
96
97 return gpio;
98 }
99 pdata->pin = gpio;
100
101 gpio = of_get_gpio(np, 1);
102 if (gpio == -EPROBE_DEFER)
103 return gpio;
104 /* ignore other errors as the pullup gpio is optional */
105 pdata->ext_pullup_enable_pin = gpio;
106
107 pdev->dev.platform_data = pdata;
108
109 return 0;
110}
111
112static int w1_gpio_probe(struct platform_device *pdev)
113{
114 struct w1_bus_master *master;
115 struct w1_gpio_platform_data *pdata;
116 int err;
117
118 if (of_have_populated_dt()) {
119 err = w1_gpio_probe_dt(pdev);
120 if (err < 0)
121 return err;
122 }
123
124 pdata = dev_get_platdata(&pdev->dev);
125
126 if (!pdata) {
127 dev_err(&pdev->dev, "No configuration data\n");
128 return -ENXIO;
129 }
130
131 master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master),
132 GFP_KERNEL);
133 if (!master) {
134 dev_err(&pdev->dev, "Out of memory\n");
135 return -ENOMEM;
136 }
137
138 err = devm_gpio_request(&pdev->dev, pdata->pin, "w1");
139 if (err) {
140 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
141 return err;
142 }
143
144 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
145 err = devm_gpio_request_one(&pdev->dev,
146 pdata->ext_pullup_enable_pin, GPIOF_INIT_LOW,
147 "w1 pullup");
148 if (err < 0) {
149 dev_err(&pdev->dev, "gpio_request_one "
150 "(ext_pullup_enable_pin) failed\n");
151 return err;
152 }
153 }
154
155 master->data = pdata;
156 master->read_bit = w1_gpio_read_bit;
157
158 if (pdata->is_open_drain) {
159 gpio_direction_output(pdata->pin, 1);
160 master->write_bit = w1_gpio_write_bit_val;
161 } else {
162 gpio_direction_input(pdata->pin);
163 master->write_bit = w1_gpio_write_bit_dir;
164 master->set_pullup = w1_gpio_set_pullup;
165 }
166
167 err = w1_add_master_device(master);
168 if (err) {
169 dev_err(&pdev->dev, "w1_add_master device failed\n");
170 return err;
171 }
172
173 if (pdata->enable_external_pullup)
174 pdata->enable_external_pullup(1);
175
176 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
177 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
178
179 platform_set_drvdata(pdev, master);
180
181 return 0;
182}
183
184static int w1_gpio_remove(struct platform_device *pdev)
185{
186 struct w1_bus_master *master = platform_get_drvdata(pdev);
187 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
188
189 if (pdata->enable_external_pullup)
190 pdata->enable_external_pullup(0);
191
192 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
193 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
194
195 w1_remove_master_device(master);
196
197 return 0;
198}
199
200static int __maybe_unused w1_gpio_suspend(struct device *dev)
201{
202 struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
203
204 if (pdata->enable_external_pullup)
205 pdata->enable_external_pullup(0);
206
207 return 0;
208}
209
210static int __maybe_unused w1_gpio_resume(struct device *dev)
211{
212 struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
213
214 if (pdata->enable_external_pullup)
215 pdata->enable_external_pullup(1);
216
217 return 0;
218}
219
220static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
221
222static struct platform_driver w1_gpio_driver = {
223 .driver = {
224 .name = "w1-gpio",
225 .pm = &w1_gpio_pm_ops,
226 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
227 },
228 .probe = w1_gpio_probe,
229 .remove = w1_gpio_remove,
230};
231
232module_platform_driver(w1_gpio_driver);
233
234MODULE_DESCRIPTION("GPIO w1 bus master driver");
235MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
236MODULE_LICENSE("GPL");