GNU Linux-libre 4.19.264-gnu1
[releases.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/consumer.h>
17 #include <linux/of_platform.h>
18 #include <linux/err.h>
19 #include <linux/of.h>
20 #include <linux/delay.h>
21
22 #include <linux/w1.h>
23
24 static u8 w1_gpio_set_pullup(void *data, int delay)
25 {
26         struct w1_gpio_platform_data *pdata = data;
27
28         if (delay) {
29                 pdata->pullup_duration = delay;
30         } else {
31                 if (pdata->pullup_duration) {
32                         /*
33                          * This will OVERRIDE open drain emulation and force-pull
34                          * the line high for some time.
35                          */
36                         gpiod_set_raw_value(pdata->gpiod, 1);
37                         msleep(pdata->pullup_duration);
38                         /*
39                          * This will simply set the line as input since we are doing
40                          * open drain emulation in the GPIO library.
41                          */
42                         gpiod_set_value(pdata->gpiod, 1);
43                 }
44                 pdata->pullup_duration = 0;
45         }
46
47         return 0;
48 }
49
50 static void w1_gpio_write_bit(void *data, u8 bit)
51 {
52         struct w1_gpio_platform_data *pdata = data;
53
54         gpiod_set_value(pdata->gpiod, bit);
55 }
56
57 static u8 w1_gpio_read_bit(void *data)
58 {
59         struct w1_gpio_platform_data *pdata = data;
60
61         return gpiod_get_value(pdata->gpiod) ? 1 : 0;
62 }
63
64 #if defined(CONFIG_OF)
65 static const struct of_device_id w1_gpio_dt_ids[] = {
66         { .compatible = "w1-gpio" },
67         {}
68 };
69 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
70 #endif
71
72 static int w1_gpio_probe(struct platform_device *pdev)
73 {
74         struct w1_bus_master *master;
75         struct w1_gpio_platform_data *pdata;
76         struct device *dev = &pdev->dev;
77         struct device_node *np = dev->of_node;
78         /* Enforce open drain mode by default */
79         enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
80         int err;
81
82         if (of_have_populated_dt()) {
83                 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
84                 if (!pdata)
85                         return -ENOMEM;
86
87                 /*
88                  * This parameter means that something else than the gpiolib has
89                  * already set the line into open drain mode, so we should just
90                  * driver it high/low like we are in full control of the line and
91                  * open drain will happen transparently.
92                  */
93                 if (of_get_property(np, "linux,open-drain", NULL))
94                         gflags = GPIOD_OUT_LOW;
95
96                 pdev->dev.platform_data = pdata;
97         }
98         pdata = dev_get_platdata(dev);
99
100         if (!pdata) {
101                 dev_err(dev, "No configuration data\n");
102                 return -ENXIO;
103         }
104
105         master = devm_kzalloc(dev, sizeof(struct w1_bus_master),
106                         GFP_KERNEL);
107         if (!master) {
108                 dev_err(dev, "Out of memory\n");
109                 return -ENOMEM;
110         }
111
112         pdata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
113         if (IS_ERR(pdata->gpiod)) {
114                 dev_err(dev, "gpio_request (pin) failed\n");
115                 return PTR_ERR(pdata->gpiod);
116         }
117
118         pdata->pullup_gpiod =
119                 devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
120         if (IS_ERR(pdata->pullup_gpiod)) {
121                 dev_err(dev, "gpio_request_one "
122                         "(ext_pullup_enable_pin) failed\n");
123                 return PTR_ERR(pdata->pullup_gpiod);
124         }
125
126         master->data = pdata;
127         master->read_bit = w1_gpio_read_bit;
128         gpiod_direction_output(pdata->gpiod, 1);
129         master->write_bit = w1_gpio_write_bit;
130
131         /*
132          * If we are using open drain emulation from the GPIO library,
133          * we need to use this pullup function that hammers the line
134          * high using a raw accessor to provide pull-up for the w1
135          * line.
136          */
137         if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
138                 master->set_pullup = w1_gpio_set_pullup;
139
140         err = w1_add_master_device(master);
141         if (err) {
142                 dev_err(dev, "w1_add_master device failed\n");
143                 return err;
144         }
145
146         if (pdata->enable_external_pullup)
147                 pdata->enable_external_pullup(1);
148
149         if (pdata->pullup_gpiod)
150                 gpiod_set_value(pdata->pullup_gpiod, 1);
151
152         platform_set_drvdata(pdev, master);
153
154         return 0;
155 }
156
157 static int 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 = dev_get_platdata(&pdev->dev);
161
162         if (pdata->enable_external_pullup)
163                 pdata->enable_external_pullup(0);
164
165         if (pdata->pullup_gpiod)
166                 gpiod_set_value(pdata->pullup_gpiod, 0);
167
168         w1_remove_master_device(master);
169
170         return 0;
171 }
172
173 static int __maybe_unused w1_gpio_suspend(struct device *dev)
174 {
175         struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
176
177         if (pdata->enable_external_pullup)
178                 pdata->enable_external_pullup(0);
179
180         return 0;
181 }
182
183 static int __maybe_unused w1_gpio_resume(struct device *dev)
184 {
185         struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
186
187         if (pdata->enable_external_pullup)
188                 pdata->enable_external_pullup(1);
189
190         return 0;
191 }
192
193 static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
194
195 static struct platform_driver w1_gpio_driver = {
196         .driver = {
197                 .name   = "w1-gpio",
198                 .pm     = &w1_gpio_pm_ops,
199                 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
200         },
201         .probe = w1_gpio_probe,
202         .remove = w1_gpio_remove,
203 };
204
205 module_platform_driver(w1_gpio_driver);
206
207 MODULE_DESCRIPTION("GPIO w1 bus master driver");
208 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
209 MODULE_LICENSE("GPL");