aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLinus Walleij <linus.walleij@linaro.org>2017-09-26 14:27:09 -0400
committerLinus Walleij <linus.walleij@linaro.org>2017-12-08 09:32:53 -0500
commite0fc62a6552f3d9c21e73cc65844f9aad1892cf7 (patch)
tree2310cbf810a4576bf8e00881d051fe9ab02a733c
parent110e2a38c8daabb6f33e164dab6083afeeb2dd5e (diff)
w1: w1-gpio: Convert to use GPIO descriptors
The w1 master driver includes a complete open drain emulation reimplementation among other things. This converts the driver and all board files using it to use GPIO descriptors associated with the device to look up the GPIO wire, as well ass the optional pull-up GPIO line. When probed from the device tree, the driver will just pick descriptors and use them right off. For the two board files in the kernel, we add descriptor lookups so we do not need to keep any old platform data handling around for the GPIO lines. As the platform data is also a state container for this driver, we augment it to contain the GPIO descriptors. w1_gpio_write_bit_dir() and w1_gpio_write_bit_val() are gone since this pair was a reimplementation of open drain emulation which is now handled by gpiolib. The special "linux,open-drain" flag is a bit of mishap here: it has the same semantic as the same flags in I2C: it means that something in the platform is setting up the line as open drain behind our back. We handle this the same way as in I2C. To drive the pull-up, we need to bypass open drain emulation in gpiolib for the line, and this is done by driving it high using gpiod_set_raw_value() which has been augmented to have the semantic of overriding the open drain emulation. We also augment the documentation to reflect the way to pass GPIO descriptors from the machine. Acked-by: Evgeniy Polyakov <zbr@ioremap.net> Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
-rw-r--r--Documentation/w1/masters/w1-gpio17
-rw-r--r--arch/arm/mach-ixp4xx/vulcan-setup.c13
-rw-r--r--arch/arm/mach-pxa/raumfeld.c16
-rw-r--r--drivers/w1/masters/w1-gpio.c149
-rw-r--r--include/linux/w1-gpio.h9
5 files changed, 101 insertions, 103 deletions
diff --git a/Documentation/w1/masters/w1-gpio b/Documentation/w1/masters/w1-gpio
index af5d3b4aa851..623961d9e83f 100644
--- a/Documentation/w1/masters/w1-gpio
+++ b/Documentation/w1/masters/w1-gpio
@@ -8,17 +8,27 @@ Description
8----------- 8-----------
9 9
10GPIO 1-wire bus master driver. The driver uses the GPIO API to control the 10GPIO 1-wire bus master driver. The driver uses the GPIO API to control the
11wire and the GPIO pin can be specified using platform data. 11wire and the GPIO pin can be specified using GPIO machine descriptor tables.
12It is also possible to define the master using device tree, see
13Documentation/devicetree/bindings/w1/w1-gpio.txt
12 14
13 15
14Example (mach-at91) 16Example (mach-at91)
15------------------- 17-------------------
16 18
19#include <linux/gpio/machine.h>
17#include <linux/w1-gpio.h> 20#include <linux/w1-gpio.h>
18 21
22static struct gpiod_lookup_table foo_w1_gpiod_table = {
23 .dev_id = "w1-gpio",
24 .table = {
25 GPIO_LOOKUP_IDX("at91-gpio", AT91_PIN_PB20, NULL, 0,
26 GPIO_ACTIVE_HIGH|GPIO_OPEN_DRAIN),
27 },
28};
29
19static struct w1_gpio_platform_data foo_w1_gpio_pdata = { 30static struct w1_gpio_platform_data foo_w1_gpio_pdata = {
20 .pin = AT91_PIN_PB20, 31 .ext_pullup_enable_pin = -EINVAL,
21 .is_open_drain = 1,
22}; 32};
23 33
24static struct platform_device foo_w1_device = { 34static struct platform_device foo_w1_device = {
@@ -30,4 +40,5 @@ static struct platform_device foo_w1_device = {
30... 40...
31 at91_set_GPIO_periph(foo_w1_gpio_pdata.pin, 1); 41 at91_set_GPIO_periph(foo_w1_gpio_pdata.pin, 1);
32 at91_set_multi_drive(foo_w1_gpio_pdata.pin, 1); 42 at91_set_multi_drive(foo_w1_gpio_pdata.pin, 1);
43 gpiod_add_lookup_table(&foo_w1_gpiod_table);
33 platform_device_register(&foo_w1_device); 44 platform_device_register(&foo_w1_device);
diff --git a/arch/arm/mach-ixp4xx/vulcan-setup.c b/arch/arm/mach-ixp4xx/vulcan-setup.c
index 731fb2019ecb..2c03d2f6b647 100644
--- a/arch/arm/mach-ixp4xx/vulcan-setup.c
+++ b/arch/arm/mach-ixp4xx/vulcan-setup.c
@@ -16,6 +16,7 @@
16#include <linux/serial_8250.h> 16#include <linux/serial_8250.h>
17#include <linux/io.h> 17#include <linux/io.h>
18#include <linux/w1-gpio.h> 18#include <linux/w1-gpio.h>
19#include <linux/gpio/machine.h>
19#include <linux/mtd/plat-ram.h> 20#include <linux/mtd/plat-ram.h>
20#include <asm/mach-types.h> 21#include <asm/mach-types.h>
21#include <asm/mach/arch.h> 22#include <asm/mach/arch.h>
@@ -162,9 +163,16 @@ static struct platform_device vulcan_max6369 = {
162 .num_resources = 1, 163 .num_resources = 1,
163}; 164};
164 165
166static struct gpiod_lookup_table vulcan_w1_gpiod_table = {
167 .dev_id = "w1-gpio",
168 .table = {
169 GPIO_LOOKUP_IDX("IXP4XX_GPIO_CHIP", 14, NULL, 0,
170 GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),
171 },
172};
173
165static struct w1_gpio_platform_data vulcan_w1_gpio_pdata = { 174static struct w1_gpio_platform_data vulcan_w1_gpio_pdata = {
166 .pin = 14, 175 /* Intentionally left blank */
167 .ext_pullup_enable_pin = -EINVAL,
168}; 176};
169 177
170static struct platform_device vulcan_w1_gpio = { 178static struct platform_device vulcan_w1_gpio = {
@@ -233,6 +241,7 @@ static void __init vulcan_init(void)
233 IXP4XX_EXP_BUS_WR_EN | 241 IXP4XX_EXP_BUS_WR_EN |
234 IXP4XX_EXP_BUS_BYTE_EN; 242 IXP4XX_EXP_BUS_BYTE_EN;
235 243
244 gpiod_add_lookup_table(&vulcan_w1_gpiod_table);
236 platform_add_devices(vulcan_devices, ARRAY_SIZE(vulcan_devices)); 245 platform_add_devices(vulcan_devices, ARRAY_SIZE(vulcan_devices));
237} 246}
238 247
diff --git a/arch/arm/mach-pxa/raumfeld.c b/arch/arm/mach-pxa/raumfeld.c
index 9d662fed03ec..feddca7f3540 100644
--- a/arch/arm/mach-pxa/raumfeld.c
+++ b/arch/arm/mach-pxa/raumfeld.c
@@ -506,11 +506,16 @@ static void w1_enable_external_pullup(int enable)
506 msleep(100); 506 msleep(100);
507} 507}
508 508
509static struct gpiod_lookup_table raumfeld_w1_gpiod_table = {
510 .dev_id = "w1-gpio",
511 .table = {
512 GPIO_LOOKUP_IDX("gpio-pxa", GPIO_ONE_WIRE, NULL, 0,
513 GPIO_ACTIVE_HIGH | GPIO_OPEN_DRAIN),
514 },
515};
516
509static struct w1_gpio_platform_data w1_gpio_platform_data = { 517static struct w1_gpio_platform_data w1_gpio_platform_data = {
510 .pin = GPIO_ONE_WIRE, 518 .enable_external_pullup = w1_enable_external_pullup,
511 .is_open_drain = 0,
512 .enable_external_pullup = w1_enable_external_pullup,
513 .ext_pullup_enable_pin = -EINVAL,
514}; 519};
515 520
516static struct platform_device raumfeld_w1_gpio_device = { 521static struct platform_device raumfeld_w1_gpio_device = {
@@ -523,13 +528,14 @@ static struct platform_device raumfeld_w1_gpio_device = {
523static void __init raumfeld_w1_init(void) 528static void __init raumfeld_w1_init(void)
524{ 529{
525 int ret = gpio_request(GPIO_W1_PULLUP_ENABLE, 530 int ret = gpio_request(GPIO_W1_PULLUP_ENABLE,
526 "W1 external pullup enable"); 531 "W1 external pullup enable");
527 532
528 if (ret < 0) 533 if (ret < 0)
529 pr_warn("Unable to request GPIO_W1_PULLUP_ENABLE\n"); 534 pr_warn("Unable to request GPIO_W1_PULLUP_ENABLE\n");
530 else 535 else
531 gpio_direction_output(GPIO_W1_PULLUP_ENABLE, 0); 536 gpio_direction_output(GPIO_W1_PULLUP_ENABLE, 0);
532 537
538 gpiod_add_lookup_table(&raumfeld_w1_gpiod_table);
533 platform_device_register(&raumfeld_w1_gpio_device); 539 platform_device_register(&raumfeld_w1_gpio_device);
534} 540}
535 541
diff --git a/drivers/w1/masters/w1-gpio.c b/drivers/w1/masters/w1-gpio.c
index a90728ceec5a..55e11bf8ebaf 100644
--- a/drivers/w1/masters/w1-gpio.c
+++ b/drivers/w1/masters/w1-gpio.c
@@ -13,9 +13,8 @@
13#include <linux/platform_device.h> 13#include <linux/platform_device.h>
14#include <linux/slab.h> 14#include <linux/slab.h>
15#include <linux/w1-gpio.h> 15#include <linux/w1-gpio.h>
16#include <linux/gpio.h> 16#include <linux/gpio/consumer.h>
17#include <linux/of_platform.h> 17#include <linux/of_platform.h>
18#include <linux/of_gpio.h>
19#include <linux/err.h> 18#include <linux/err.h>
20#include <linux/of.h> 19#include <linux/of.h>
21#include <linux/delay.h> 20#include <linux/delay.h>
@@ -30,11 +29,17 @@ static u8 w1_gpio_set_pullup(void *data, int delay)
30 pdata->pullup_duration = delay; 29 pdata->pullup_duration = delay;
31 } else { 30 } else {
32 if (pdata->pullup_duration) { 31 if (pdata->pullup_duration) {
33 gpio_direction_output(pdata->pin, 1); 32 /*
34 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);
35 msleep(pdata->pullup_duration); 37 msleep(pdata->pullup_duration);
36 38 /*
37 gpio_direction_input(pdata->pin); 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);
38 } 43 }
39 pdata->pullup_duration = 0; 44 pdata->pullup_duration = 0;
40 } 45 }
@@ -42,28 +47,18 @@ static u8 w1_gpio_set_pullup(void *data, int delay)
42 return 0; 47 return 0;
43} 48}
44 49
45static void w1_gpio_write_bit_dir(void *data, u8 bit) 50static void w1_gpio_write_bit(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{ 51{
57 struct w1_gpio_platform_data *pdata = data; 52 struct w1_gpio_platform_data *pdata = data;
58 53
59 gpio_set_value(pdata->pin, bit); 54 gpiod_set_value(pdata->gpiod, bit);
60} 55}
61 56
62static u8 w1_gpio_read_bit(void *data) 57static u8 w1_gpio_read_bit(void *data)
63{ 58{
64 struct w1_gpio_platform_data *pdata = data; 59 struct w1_gpio_platform_data *pdata = data;
65 60
66 return gpio_get_value(pdata->pin) ? 1 : 0; 61 return gpiod_get_value(pdata->gpiod) ? 1 : 0;
67} 62}
68 63
69#if defined(CONFIG_OF) 64#if defined(CONFIG_OF)
@@ -74,107 +69,85 @@ static const struct of_device_id w1_gpio_dt_ids[] = {
74MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids); 69MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
75#endif 70#endif
76 71
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) 72static int w1_gpio_probe(struct platform_device *pdev)
113{ 73{
114 struct w1_bus_master *master; 74 struct w1_bus_master *master;
115 struct w1_gpio_platform_data *pdata; 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;
116 int err; 80 int err;
117 81
118 if (of_have_populated_dt()) { 82 if (of_have_populated_dt()) {
119 err = w1_gpio_probe_dt(pdev); 83 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
120 if (err < 0) 84 if (!pdata)
121 return err; 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;
122 } 97 }
123 98 pdata = dev_get_platdata(dev);
124 pdata = dev_get_platdata(&pdev->dev);
125 99
126 if (!pdata) { 100 if (!pdata) {
127 dev_err(&pdev->dev, "No configuration data\n"); 101 dev_err(dev, "No configuration data\n");
128 return -ENXIO; 102 return -ENXIO;
129 } 103 }
130 104
131 master = devm_kzalloc(&pdev->dev, sizeof(struct w1_bus_master), 105 master = devm_kzalloc(dev, sizeof(struct w1_bus_master),
132 GFP_KERNEL); 106 GFP_KERNEL);
133 if (!master) { 107 if (!master) {
134 dev_err(&pdev->dev, "Out of memory\n"); 108 dev_err(dev, "Out of memory\n");
135 return -ENOMEM; 109 return -ENOMEM;
136 } 110 }
137 111
138 err = devm_gpio_request(&pdev->dev, pdata->pin, "w1"); 112 pdata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
139 if (err) { 113 if (IS_ERR(pdata->gpiod)) {
140 dev_err(&pdev->dev, "gpio_request (pin) failed\n"); 114 dev_err(dev, "gpio_request (pin) failed\n");
141 return err; 115 return PTR_ERR(pdata->gpiod);
142 } 116 }
143 117
144 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) { 118 pdata->pullup_gpiod =
145 err = devm_gpio_request_one(&pdev->dev, 119 devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
146 pdata->ext_pullup_enable_pin, GPIOF_INIT_LOW, 120 if (IS_ERR(pdata->pullup_gpiod)) {
147 "w1 pullup"); 121 dev_err(dev, "gpio_request_one "
148 if (err < 0) { 122 "(ext_pullup_enable_pin) failed\n");
149 dev_err(&pdev->dev, "gpio_request_one " 123 return PTR_ERR(pdata->pullup_gpiod);
150 "(ext_pullup_enable_pin) failed\n");
151 return err;
152 }
153 } 124 }
154 125
155 master->data = pdata; 126 master->data = pdata;
156 master->read_bit = w1_gpio_read_bit; 127 master->read_bit = w1_gpio_read_bit;
157 128 gpiod_direction_output(pdata->gpiod, 1);
158 if (pdata->is_open_drain) { 129 master->write_bit = w1_gpio_write_bit;
159 gpio_direction_output(pdata->pin, 1); 130
160 master->write_bit = w1_gpio_write_bit_val; 131 /*
161 } else { 132 * If we are using open drain emulation from the GPIO library,
162 gpio_direction_input(pdata->pin); 133 * we need to use this pullup function that hammers the line
163 master->write_bit = w1_gpio_write_bit_dir; 134 * high using a raw accessor to provide pull-up for the w1
135 * line.
136 */
137 if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
164 master->set_pullup = w1_gpio_set_pullup; 138 master->set_pullup = w1_gpio_set_pullup;
165 }
166 139
167 err = w1_add_master_device(master); 140 err = w1_add_master_device(master);
168 if (err) { 141 if (err) {
169 dev_err(&pdev->dev, "w1_add_master device failed\n"); 142 dev_err(dev, "w1_add_master device failed\n");
170 return err; 143 return err;
171 } 144 }
172 145
173 if (pdata->enable_external_pullup) 146 if (pdata->enable_external_pullup)
174 pdata->enable_external_pullup(1); 147 pdata->enable_external_pullup(1);
175 148
176 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) 149 if (pdata->pullup_gpiod)
177 gpio_set_value(pdata->ext_pullup_enable_pin, 1); 150 gpiod_set_value(pdata->pullup_gpiod, 1);
178 151
179 platform_set_drvdata(pdev, master); 152 platform_set_drvdata(pdev, master);
180 153
@@ -189,8 +162,8 @@ static int w1_gpio_remove(struct platform_device *pdev)
189 if (pdata->enable_external_pullup) 162 if (pdata->enable_external_pullup)
190 pdata->enable_external_pullup(0); 163 pdata->enable_external_pullup(0);
191 164
192 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) 165 if (pdata->pullup_gpiod)
193 gpio_set_value(pdata->ext_pullup_enable_pin, 0); 166 gpiod_set_value(pdata->pullup_gpiod, 0);
194 167
195 w1_remove_master_device(master); 168 w1_remove_master_device(master);
196 169
diff --git a/include/linux/w1-gpio.h b/include/linux/w1-gpio.h
index d58594a32324..78901ecd2f95 100644
--- a/include/linux/w1-gpio.h
+++ b/include/linux/w1-gpio.h
@@ -10,16 +10,15 @@
10#ifndef _LINUX_W1_GPIO_H 10#ifndef _LINUX_W1_GPIO_H
11#define _LINUX_W1_GPIO_H 11#define _LINUX_W1_GPIO_H
12 12
13struct gpio_desc;
14
13/** 15/**
14 * struct w1_gpio_platform_data - Platform-dependent data for w1-gpio 16 * struct w1_gpio_platform_data - Platform-dependent data for w1-gpio
15 * @pin: GPIO pin to use
16 * @is_open_drain: GPIO pin is configured as open drain
17 */ 17 */
18struct w1_gpio_platform_data { 18struct w1_gpio_platform_data {
19 unsigned int pin; 19 struct gpio_desc *gpiod;
20 unsigned int is_open_drain:1; 20 struct gpio_desc *pullup_gpiod;
21 void (*enable_external_pullup)(int enable); 21 void (*enable_external_pullup)(int enable);
22 unsigned int ext_pullup_enable_pin;
23 unsigned int pullup_duration; 22 unsigned int pullup_duration;
24}; 23};
25 24