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