1 // SPDX-License-Identifier: GPL-2.0-only
3 * w1-gpio - GPIO w1 bus master driver
5 * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/err.h>
11 #include <linux/gpio/consumer.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/property.h>
16 #include <linux/types.h>
20 struct w1_gpio_ddata
{
21 struct gpio_desc
*gpiod
;
22 struct gpio_desc
*pullup_gpiod
;
23 unsigned int pullup_duration
;
26 static u8
w1_gpio_set_pullup(void *data
, int delay
)
28 struct w1_gpio_ddata
*ddata
= data
;
31 ddata
->pullup_duration
= delay
;
33 if (ddata
->pullup_duration
) {
35 * This will OVERRIDE open drain emulation and force-pull
36 * the line high for some time.
38 gpiod_set_raw_value(ddata
->gpiod
, 1);
39 msleep(ddata
->pullup_duration
);
41 * This will simply set the line as input since we are doing
42 * open drain emulation in the GPIO library.
44 gpiod_set_value(ddata
->gpiod
, 1);
46 ddata
->pullup_duration
= 0;
52 static void w1_gpio_write_bit(void *data
, u8 bit
)
54 struct w1_gpio_ddata
*ddata
= data
;
56 gpiod_set_value(ddata
->gpiod
, bit
);
59 static u8
w1_gpio_read_bit(void *data
)
61 struct w1_gpio_ddata
*ddata
= data
;
63 return gpiod_get_value(ddata
->gpiod
) ? 1 : 0;
66 static int w1_gpio_probe(struct platform_device
*pdev
)
68 struct w1_bus_master
*master
;
69 struct w1_gpio_ddata
*ddata
;
70 struct device
*dev
= &pdev
->dev
;
71 /* Enforce open drain mode by default */
72 enum gpiod_flags gflags
= GPIOD_OUT_LOW_OPEN_DRAIN
;
75 ddata
= devm_kzalloc(&pdev
->dev
, sizeof(*ddata
), GFP_KERNEL
);
80 * This parameter means that something else than the gpiolib has
81 * already set the line into open drain mode, so we should just
82 * driver it high/low like we are in full control of the line and
83 * open drain will happen transparently.
85 if (device_property_present(dev
, "linux,open-drain"))
86 gflags
= GPIOD_OUT_LOW
;
88 master
= devm_kzalloc(dev
, sizeof(*master
), GFP_KERNEL
);
92 ddata
->gpiod
= devm_gpiod_get_index(dev
, NULL
, 0, gflags
);
93 if (IS_ERR(ddata
->gpiod
))
94 return dev_err_probe(dev
, PTR_ERR(ddata
->gpiod
), "gpio_request (pin) failed\n");
97 devm_gpiod_get_index_optional(dev
, NULL
, 1, GPIOD_OUT_LOW
);
98 if (IS_ERR(ddata
->pullup_gpiod
))
99 return dev_err_probe(dev
, PTR_ERR(ddata
->pullup_gpiod
),
100 "gpio_request (ext_pullup_enable_pin) failed\n");
102 master
->data
= ddata
;
103 master
->read_bit
= w1_gpio_read_bit
;
104 gpiod_direction_output(ddata
->gpiod
, 1);
105 master
->write_bit
= w1_gpio_write_bit
;
108 * If we are using open drain emulation from the GPIO library,
109 * we need to use this pullup function that hammers the line
110 * high using a raw accessor to provide pull-up for the w1
113 if (gflags
== GPIOD_OUT_LOW_OPEN_DRAIN
)
114 master
->set_pullup
= w1_gpio_set_pullup
;
116 err
= w1_add_master_device(master
);
118 return dev_err_probe(dev
, err
, "w1_add_master device failed\n");
120 gpiod_set_value(ddata
->pullup_gpiod
, 1);
122 platform_set_drvdata(pdev
, master
);
127 static void w1_gpio_remove(struct platform_device
*pdev
)
129 struct w1_bus_master
*master
= platform_get_drvdata(pdev
);
130 struct w1_gpio_ddata
*ddata
= master
->data
;
132 gpiod_set_value(ddata
->pullup_gpiod
, 0);
134 w1_remove_master_device(master
);
137 static const struct of_device_id w1_gpio_dt_ids
[] = {
138 { .compatible
= "w1-gpio" },
141 MODULE_DEVICE_TABLE(of
, w1_gpio_dt_ids
);
143 static struct platform_driver w1_gpio_driver
= {
146 .of_match_table
= w1_gpio_dt_ids
,
148 .probe
= w1_gpio_probe
,
149 .remove_new
= w1_gpio_remove
,
152 module_platform_driver(w1_gpio_driver
);
154 MODULE_DESCRIPTION("GPIO w1 bus master driver");
155 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
156 MODULE_LICENSE("GPL");