xref: /openbmc/linux/drivers/w1/masters/w1-gpio.c (revision d2912cb15bdda8ba4a5dd73396ad62641af2f520)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ad8dc96eSVille Syrjala /*
3ad8dc96eSVille Syrjala  * w1-gpio - GPIO w1 bus master driver
4ad8dc96eSVille Syrjala  *
5ad8dc96eSVille Syrjala  * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
6ad8dc96eSVille Syrjala  */
7ad8dc96eSVille Syrjala 
8ad8dc96eSVille Syrjala #include <linux/init.h>
9ad8dc96eSVille Syrjala #include <linux/module.h>
10ad8dc96eSVille Syrjala #include <linux/platform_device.h>
115a0e3ad6STejun Heo #include <linux/slab.h>
12ad8dc96eSVille Syrjala #include <linux/w1-gpio.h>
13e0fc62a6SLinus Walleij #include <linux/gpio/consumer.h>
145f3d1382SDaniel Mack #include <linux/of_platform.h>
15277ed0d5SPantelis Antoniou #include <linux/err.h>
168a1861d9SPantelis Antoniou #include <linux/of.h>
173089a4c8SEvgeny Boger #include <linux/delay.h>
18ad8dc96eSVille Syrjala 
19de0d6dbdSAndrew F. Davis #include <linux/w1.h>
20ad8dc96eSVille Syrjala 
213089a4c8SEvgeny Boger static u8 w1_gpio_set_pullup(void *data, int delay)
223089a4c8SEvgeny Boger {
233089a4c8SEvgeny Boger 	struct w1_gpio_platform_data *pdata = data;
243089a4c8SEvgeny Boger 
253089a4c8SEvgeny Boger 	if (delay) {
263089a4c8SEvgeny Boger 		pdata->pullup_duration = delay;
273089a4c8SEvgeny Boger 	} else {
283089a4c8SEvgeny Boger 		if (pdata->pullup_duration) {
29e0fc62a6SLinus Walleij 			/*
30e0fc62a6SLinus Walleij 			 * This will OVERRIDE open drain emulation and force-pull
31e0fc62a6SLinus Walleij 			 * the line high for some time.
32e0fc62a6SLinus Walleij 			 */
33e0fc62a6SLinus Walleij 			gpiod_set_raw_value(pdata->gpiod, 1);
343089a4c8SEvgeny Boger 			msleep(pdata->pullup_duration);
35e0fc62a6SLinus Walleij 			/*
36e0fc62a6SLinus Walleij 			 * This will simply set the line as input since we are doing
37e0fc62a6SLinus Walleij 			 * open drain emulation in the GPIO library.
38e0fc62a6SLinus Walleij 			 */
39e0fc62a6SLinus Walleij 			gpiod_set_value(pdata->gpiod, 1);
403089a4c8SEvgeny Boger 		}
413089a4c8SEvgeny Boger 		pdata->pullup_duration = 0;
423089a4c8SEvgeny Boger 	}
433089a4c8SEvgeny Boger 
443089a4c8SEvgeny Boger 	return 0;
453089a4c8SEvgeny Boger }
463089a4c8SEvgeny Boger 
47e0fc62a6SLinus Walleij static void w1_gpio_write_bit(void *data, u8 bit)
48ad8dc96eSVille Syrjala {
49ad8dc96eSVille Syrjala 	struct w1_gpio_platform_data *pdata = data;
50ad8dc96eSVille Syrjala 
51e0fc62a6SLinus Walleij 	gpiod_set_value(pdata->gpiod, bit);
52ad8dc96eSVille Syrjala }
53ad8dc96eSVille Syrjala 
54ad8dc96eSVille Syrjala static u8 w1_gpio_read_bit(void *data)
55ad8dc96eSVille Syrjala {
56ad8dc96eSVille Syrjala 	struct w1_gpio_platform_data *pdata = data;
57ad8dc96eSVille Syrjala 
58e0fc62a6SLinus Walleij 	return gpiod_get_value(pdata->gpiod) ? 1 : 0;
59ad8dc96eSVille Syrjala }
60ad8dc96eSVille Syrjala 
6134ccd873SJohan Hovold #if defined(CONFIG_OF)
620a56c0e1SFabian Frederick static const struct of_device_id w1_gpio_dt_ids[] = {
635f3d1382SDaniel Mack 	{ .compatible = "w1-gpio" },
645f3d1382SDaniel Mack 	{}
655f3d1382SDaniel Mack };
665f3d1382SDaniel Mack MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
6734ccd873SJohan Hovold #endif
685f3d1382SDaniel Mack 
6906a8f1feSHauke Mehrtens static int w1_gpio_probe(struct platform_device *pdev)
70ad8dc96eSVille Syrjala {
71ad8dc96eSVille Syrjala 	struct w1_bus_master *master;
725f3d1382SDaniel Mack 	struct w1_gpio_platform_data *pdata;
73e0fc62a6SLinus Walleij 	struct device *dev = &pdev->dev;
74e0fc62a6SLinus Walleij 	struct device_node *np = dev->of_node;
75e0fc62a6SLinus Walleij 	/* Enforce open drain mode by default */
76e0fc62a6SLinus Walleij 	enum gpiod_flags gflags = GPIOD_OUT_LOW_OPEN_DRAIN;
77ad8dc96eSVille Syrjala 	int err;
78ad8dc96eSVille Syrjala 
798a1861d9SPantelis Antoniou 	if (of_have_populated_dt()) {
80e0fc62a6SLinus Walleij 		pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
81e0fc62a6SLinus Walleij 		if (!pdata)
82e0fc62a6SLinus Walleij 			return -ENOMEM;
835f3d1382SDaniel Mack 
84e0fc62a6SLinus Walleij 		/*
85e0fc62a6SLinus Walleij 		 * This parameter means that something else than the gpiolib has
86e0fc62a6SLinus Walleij 		 * already set the line into open drain mode, so we should just
87e0fc62a6SLinus Walleij 		 * driver it high/low like we are in full control of the line and
88e0fc62a6SLinus Walleij 		 * open drain will happen transparently.
89e0fc62a6SLinus Walleij 		 */
90e0fc62a6SLinus Walleij 		if (of_get_property(np, "linux,open-drain", NULL))
91e0fc62a6SLinus Walleij 			gflags = GPIOD_OUT_LOW;
92e0fc62a6SLinus Walleij 
93e0fc62a6SLinus Walleij 		pdev->dev.platform_data = pdata;
94e0fc62a6SLinus Walleij 	}
95e0fc62a6SLinus Walleij 	pdata = dev_get_platdata(dev);
965f3d1382SDaniel Mack 
978a1861d9SPantelis Antoniou 	if (!pdata) {
98e0fc62a6SLinus Walleij 		dev_err(dev, "No configuration data\n");
99ad8dc96eSVille Syrjala 		return -ENXIO;
1008a1861d9SPantelis Antoniou 	}
101ad8dc96eSVille Syrjala 
102e0fc62a6SLinus Walleij 	master = devm_kzalloc(dev, sizeof(struct w1_bus_master),
103d27f25c9SMarkus Pargmann 			GFP_KERNEL);
1048a1861d9SPantelis Antoniou 	if (!master) {
105e0fc62a6SLinus Walleij 		dev_err(dev, "Out of memory\n");
106ad8dc96eSVille Syrjala 		return -ENOMEM;
1078a1861d9SPantelis Antoniou 	}
108ad8dc96eSVille Syrjala 
109e0fc62a6SLinus Walleij 	pdata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
110e0fc62a6SLinus Walleij 	if (IS_ERR(pdata->gpiod)) {
111e0fc62a6SLinus Walleij 		dev_err(dev, "gpio_request (pin) failed\n");
112e0fc62a6SLinus Walleij 		return PTR_ERR(pdata->gpiod);
1138a1861d9SPantelis Antoniou 	}
114ad8dc96eSVille Syrjala 
115e0fc62a6SLinus Walleij 	pdata->pullup_gpiod =
116e0fc62a6SLinus Walleij 		devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
117e0fc62a6SLinus Walleij 	if (IS_ERR(pdata->pullup_gpiod)) {
118e0fc62a6SLinus Walleij 		dev_err(dev, "gpio_request_one "
1198a1861d9SPantelis Antoniou 			"(ext_pullup_enable_pin) failed\n");
120e0fc62a6SLinus Walleij 		return PTR_ERR(pdata->pullup_gpiod);
1218a1861d9SPantelis Antoniou 	}
122d2323cf7SDaniel Mack 
123ad8dc96eSVille Syrjala 	master->data = pdata;
124ad8dc96eSVille Syrjala 	master->read_bit = w1_gpio_read_bit;
125e0fc62a6SLinus Walleij 	gpiod_direction_output(pdata->gpiod, 1);
126e0fc62a6SLinus Walleij 	master->write_bit = w1_gpio_write_bit;
127ad8dc96eSVille Syrjala 
128e0fc62a6SLinus Walleij 	/*
129e0fc62a6SLinus Walleij 	 * If we are using open drain emulation from the GPIO library,
130e0fc62a6SLinus Walleij 	 * we need to use this pullup function that hammers the line
131e0fc62a6SLinus Walleij 	 * high using a raw accessor to provide pull-up for the w1
132e0fc62a6SLinus Walleij 	 * line.
133e0fc62a6SLinus Walleij 	 */
134e0fc62a6SLinus Walleij 	if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
1353089a4c8SEvgeny Boger 		master->set_pullup = w1_gpio_set_pullup;
136ad8dc96eSVille Syrjala 
137ad8dc96eSVille Syrjala 	err = w1_add_master_device(master);
1388a1861d9SPantelis Antoniou 	if (err) {
139e0fc62a6SLinus Walleij 		dev_err(dev, "w1_add_master device failed\n");
140d27f25c9SMarkus Pargmann 		return err;
1418a1861d9SPantelis Antoniou 	}
142ad8dc96eSVille Syrjala 
143c8a06c1eSDaniel Mack 	if (pdata->enable_external_pullup)
144c8a06c1eSDaniel Mack 		pdata->enable_external_pullup(1);
145c8a06c1eSDaniel Mack 
146e0fc62a6SLinus Walleij 	if (pdata->pullup_gpiod)
147e0fc62a6SLinus Walleij 		gpiod_set_value(pdata->pullup_gpiod, 1);
148d2323cf7SDaniel Mack 
149ad8dc96eSVille Syrjala 	platform_set_drvdata(pdev, master);
150ad8dc96eSVille Syrjala 
151ad8dc96eSVille Syrjala 	return 0;
152ad8dc96eSVille Syrjala }
153ad8dc96eSVille Syrjala 
15401230551SJohan Hovold static int w1_gpio_remove(struct platform_device *pdev)
155ad8dc96eSVille Syrjala {
156ad8dc96eSVille Syrjala 	struct w1_bus_master *master = platform_get_drvdata(pdev);
157c853b167SJingoo Han 	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
158ad8dc96eSVille Syrjala 
159c8a06c1eSDaniel Mack 	if (pdata->enable_external_pullup)
160c8a06c1eSDaniel Mack 		pdata->enable_external_pullup(0);
161c8a06c1eSDaniel Mack 
162e0fc62a6SLinus Walleij 	if (pdata->pullup_gpiod)
163e0fc62a6SLinus Walleij 		gpiod_set_value(pdata->pullup_gpiod, 0);
164d2323cf7SDaniel Mack 
165ad8dc96eSVille Syrjala 	w1_remove_master_device(master);
166ad8dc96eSVille Syrjala 
167ad8dc96eSVille Syrjala 	return 0;
168ad8dc96eSVille Syrjala }
169ad8dc96eSVille Syrjala 
17036fccce0SDmitry Torokhov static int __maybe_unused w1_gpio_suspend(struct device *dev)
171c8a06c1eSDaniel Mack {
17236fccce0SDmitry Torokhov 	struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
173c8a06c1eSDaniel Mack 
174c8a06c1eSDaniel Mack 	if (pdata->enable_external_pullup)
175c8a06c1eSDaniel Mack 		pdata->enable_external_pullup(0);
176c8a06c1eSDaniel Mack 
177c8a06c1eSDaniel Mack 	return 0;
178c8a06c1eSDaniel Mack }
179c8a06c1eSDaniel Mack 
18036fccce0SDmitry Torokhov static int __maybe_unused w1_gpio_resume(struct device *dev)
181c8a06c1eSDaniel Mack {
18236fccce0SDmitry Torokhov 	struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
183c8a06c1eSDaniel Mack 
184c8a06c1eSDaniel Mack 	if (pdata->enable_external_pullup)
185c8a06c1eSDaniel Mack 		pdata->enable_external_pullup(1);
186c8a06c1eSDaniel Mack 
187c8a06c1eSDaniel Mack 	return 0;
188c8a06c1eSDaniel Mack }
189c8a06c1eSDaniel Mack 
19036fccce0SDmitry Torokhov static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
191c8a06c1eSDaniel Mack 
192ad8dc96eSVille Syrjala static struct platform_driver w1_gpio_driver = {
193ad8dc96eSVille Syrjala 	.driver = {
194ad8dc96eSVille Syrjala 		.name	= "w1-gpio",
19536fccce0SDmitry Torokhov 		.pm	= &w1_gpio_pm_ops,
1965f3d1382SDaniel Mack 		.of_match_table = of_match_ptr(w1_gpio_dt_ids),
197ad8dc96eSVille Syrjala 	},
1988a1861d9SPantelis Antoniou 	.probe = w1_gpio_probe,
19901230551SJohan Hovold 	.remove = w1_gpio_remove,
200ad8dc96eSVille Syrjala };
201ad8dc96eSVille Syrjala 
2028a1861d9SPantelis Antoniou module_platform_driver(w1_gpio_driver);
203ad8dc96eSVille Syrjala 
204ad8dc96eSVille Syrjala MODULE_DESCRIPTION("GPIO w1 bus master driver");
205ad8dc96eSVille Syrjala MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
206ad8dc96eSVille Syrjala MODULE_LICENSE("GPL");
207