xref: /openbmc/linux/drivers/w1/masters/w1-gpio.c (revision 6da07bdd)
1d2912cb1SThomas 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 
w1_gpio_set_pullup(void * data,int delay)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 
w1_gpio_write_bit(void * data,u8 bit)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 
w1_gpio_read_bit(void * data)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 
w1_gpio_probe(struct platform_device * pdev)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 		 */
90*6da07bddSRob Herring 		if (of_property_present(np, "linux,open-drain"))
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 		return -ENOMEM;
106ad8dc96eSVille Syrjala 
1078a1861d9SPantelis Antoniou 	pdata->gpiod = devm_gpiod_get_index(dev, NULL, 0, gflags);
108ad8dc96eSVille Syrjala 	if (IS_ERR(pdata->gpiod)) {
109e0fc62a6SLinus Walleij 		dev_err(dev, "gpio_request (pin) failed\n");
110e0fc62a6SLinus Walleij 		return PTR_ERR(pdata->gpiod);
111e0fc62a6SLinus Walleij 	}
112e0fc62a6SLinus Walleij 
1138a1861d9SPantelis Antoniou 	pdata->pullup_gpiod =
114ad8dc96eSVille Syrjala 		devm_gpiod_get_index_optional(dev, NULL, 1, GPIOD_OUT_LOW);
115e0fc62a6SLinus Walleij 	if (IS_ERR(pdata->pullup_gpiod)) {
116e0fc62a6SLinus Walleij 		dev_err(dev, "gpio_request_one "
117e0fc62a6SLinus Walleij 			"(ext_pullup_enable_pin) failed\n");
118e0fc62a6SLinus Walleij 		return PTR_ERR(pdata->pullup_gpiod);
1198a1861d9SPantelis Antoniou 	}
120e0fc62a6SLinus Walleij 
1218a1861d9SPantelis Antoniou 	master->data = pdata;
122d2323cf7SDaniel Mack 	master->read_bit = w1_gpio_read_bit;
123ad8dc96eSVille Syrjala 	gpiod_direction_output(pdata->gpiod, 1);
124ad8dc96eSVille Syrjala 	master->write_bit = w1_gpio_write_bit;
125e0fc62a6SLinus Walleij 
126e0fc62a6SLinus Walleij 	/*
127ad8dc96eSVille Syrjala 	 * If we are using open drain emulation from the GPIO library,
128e0fc62a6SLinus Walleij 	 * we need to use this pullup function that hammers the line
129e0fc62a6SLinus Walleij 	 * high using a raw accessor to provide pull-up for the w1
130e0fc62a6SLinus Walleij 	 * line.
131e0fc62a6SLinus Walleij 	 */
132e0fc62a6SLinus Walleij 	if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
133e0fc62a6SLinus Walleij 		master->set_pullup = w1_gpio_set_pullup;
134e0fc62a6SLinus Walleij 
1353089a4c8SEvgeny Boger 	err = w1_add_master_device(master);
136ad8dc96eSVille Syrjala 	if (err) {
137ad8dc96eSVille Syrjala 		dev_err(dev, "w1_add_master device failed\n");
1388a1861d9SPantelis Antoniou 		return err;
139e0fc62a6SLinus Walleij 	}
140d27f25c9SMarkus Pargmann 
1418a1861d9SPantelis Antoniou 	if (pdata->enable_external_pullup)
142ad8dc96eSVille Syrjala 		pdata->enable_external_pullup(1);
143c8a06c1eSDaniel Mack 
144c8a06c1eSDaniel Mack 	if (pdata->pullup_gpiod)
145c8a06c1eSDaniel Mack 		gpiod_set_value(pdata->pullup_gpiod, 1);
146e0fc62a6SLinus Walleij 
147e0fc62a6SLinus Walleij 	platform_set_drvdata(pdev, master);
148d2323cf7SDaniel Mack 
149ad8dc96eSVille Syrjala 	return 0;
150ad8dc96eSVille Syrjala }
151ad8dc96eSVille Syrjala 
w1_gpio_remove(struct platform_device * pdev)152ad8dc96eSVille Syrjala static int w1_gpio_remove(struct platform_device *pdev)
153ad8dc96eSVille Syrjala {
15401230551SJohan Hovold 	struct w1_bus_master *master = platform_get_drvdata(pdev);
155ad8dc96eSVille Syrjala 	struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
156ad8dc96eSVille Syrjala 
157c853b167SJingoo Han 	if (pdata->enable_external_pullup)
158ad8dc96eSVille Syrjala 		pdata->enable_external_pullup(0);
159c8a06c1eSDaniel Mack 
160c8a06c1eSDaniel Mack 	if (pdata->pullup_gpiod)
161c8a06c1eSDaniel Mack 		gpiod_set_value(pdata->pullup_gpiod, 0);
162e0fc62a6SLinus Walleij 
163e0fc62a6SLinus Walleij 	w1_remove_master_device(master);
164d2323cf7SDaniel Mack 
165ad8dc96eSVille Syrjala 	return 0;
166ad8dc96eSVille Syrjala }
167ad8dc96eSVille Syrjala 
w1_gpio_suspend(struct device * dev)168ad8dc96eSVille Syrjala static int __maybe_unused w1_gpio_suspend(struct device *dev)
169ad8dc96eSVille Syrjala {
17036fccce0SDmitry Torokhov 	struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
171c8a06c1eSDaniel Mack 
17236fccce0SDmitry Torokhov 	if (pdata->enable_external_pullup)
173c8a06c1eSDaniel Mack 		pdata->enable_external_pullup(0);
174c8a06c1eSDaniel Mack 
175c8a06c1eSDaniel Mack 	return 0;
176c8a06c1eSDaniel Mack }
177c8a06c1eSDaniel Mack 
w1_gpio_resume(struct device * dev)178c8a06c1eSDaniel Mack static int __maybe_unused w1_gpio_resume(struct device *dev)
179c8a06c1eSDaniel Mack {
18036fccce0SDmitry Torokhov 	struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
181c8a06c1eSDaniel Mack 
18236fccce0SDmitry Torokhov 	if (pdata->enable_external_pullup)
183c8a06c1eSDaniel Mack 		pdata->enable_external_pullup(1);
184c8a06c1eSDaniel Mack 
185c8a06c1eSDaniel Mack 	return 0;
186c8a06c1eSDaniel Mack }
187c8a06c1eSDaniel Mack 
188c8a06c1eSDaniel Mack static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
189c8a06c1eSDaniel Mack 
19036fccce0SDmitry Torokhov static struct platform_driver w1_gpio_driver = {
191c8a06c1eSDaniel Mack 	.driver = {
192ad8dc96eSVille Syrjala 		.name	= "w1-gpio",
193ad8dc96eSVille Syrjala 		.pm	= &w1_gpio_pm_ops,
194ad8dc96eSVille Syrjala 		.of_match_table = of_match_ptr(w1_gpio_dt_ids),
19536fccce0SDmitry Torokhov 	},
1965f3d1382SDaniel Mack 	.probe = w1_gpio_probe,
197ad8dc96eSVille Syrjala 	.remove = w1_gpio_remove,
1988a1861d9SPantelis Antoniou };
19901230551SJohan Hovold 
200ad8dc96eSVille Syrjala module_platform_driver(w1_gpio_driver);
201ad8dc96eSVille Syrjala 
2028a1861d9SPantelis Antoniou MODULE_DESCRIPTION("GPIO w1 bus master driver");
203ad8dc96eSVille Syrjala MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
204ad8dc96eSVille Syrjala MODULE_LICENSE("GPL");
205ad8dc96eSVille Syrjala