vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / w1 / masters / w1-gpio.c
blob55e11bf8ebaf37732c6566aec0ee1e335e9cfdb9
1 /*
2 * w1-gpio - GPIO w1 bus master driver
4 * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
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 */
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>
22 #include <linux/w1.h>
24 static u8 w1_gpio_set_pullup(void *data, int delay)
26 struct w1_gpio_platform_data *pdata = data;
28 if (delay) {
29 pdata->pullup_duration = delay;
30 } else {
31 if (pdata->pullup_duration) {
33 * This will OVERRIDE open drain emulation and force-pull
34 * the line high for some time.
36 gpiod_set_raw_value(pdata->gpiod, 1);
37 msleep(pdata->pullup_duration);
39 * This will simply set the line as input since we are doing
40 * open drain emulation in the GPIO library.
42 gpiod_set_value(pdata->gpiod, 1);
44 pdata->pullup_duration = 0;
47 return 0;
50 static void w1_gpio_write_bit(void *data, u8 bit)
52 struct w1_gpio_platform_data *pdata = data;
54 gpiod_set_value(pdata->gpiod, bit);
57 static u8 w1_gpio_read_bit(void *data)
59 struct w1_gpio_platform_data *pdata = data;
61 return gpiod_get_value(pdata->gpiod) ? 1 : 0;
64 #if defined(CONFIG_OF)
65 static const struct of_device_id w1_gpio_dt_ids[] = {
66 { .compatible = "w1-gpio" },
69 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
70 #endif
72 static int w1_gpio_probe(struct platform_device *pdev)
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;
82 if (of_have_populated_dt()) {
83 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
84 if (!pdata)
85 return -ENOMEM;
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.
93 if (of_get_property(np, "linux,open-drain", NULL))
94 gflags = GPIOD_OUT_LOW;
96 pdev->dev.platform_data = pdata;
98 pdata = dev_get_platdata(dev);
100 if (!pdata) {
101 dev_err(dev, "No configuration data\n");
102 return -ENXIO;
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;
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);
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);
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;
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.
137 if (gflags == GPIOD_OUT_LOW_OPEN_DRAIN)
138 master->set_pullup = w1_gpio_set_pullup;
140 err = w1_add_master_device(master);
141 if (err) {
142 dev_err(dev, "w1_add_master device failed\n");
143 return err;
146 if (pdata->enable_external_pullup)
147 pdata->enable_external_pullup(1);
149 if (pdata->pullup_gpiod)
150 gpiod_set_value(pdata->pullup_gpiod, 1);
152 platform_set_drvdata(pdev, master);
154 return 0;
157 static int w1_gpio_remove(struct platform_device *pdev)
159 struct w1_bus_master *master = platform_get_drvdata(pdev);
160 struct w1_gpio_platform_data *pdata = dev_get_platdata(&pdev->dev);
162 if (pdata->enable_external_pullup)
163 pdata->enable_external_pullup(0);
165 if (pdata->pullup_gpiod)
166 gpiod_set_value(pdata->pullup_gpiod, 0);
168 w1_remove_master_device(master);
170 return 0;
173 static int __maybe_unused w1_gpio_suspend(struct device *dev)
175 struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
177 if (pdata->enable_external_pullup)
178 pdata->enable_external_pullup(0);
180 return 0;
183 static int __maybe_unused w1_gpio_resume(struct device *dev)
185 struct w1_gpio_platform_data *pdata = dev_get_platdata(dev);
187 if (pdata->enable_external_pullup)
188 pdata->enable_external_pullup(1);
190 return 0;
193 static SIMPLE_DEV_PM_OPS(w1_gpio_pm_ops, w1_gpio_suspend, w1_gpio_resume);
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),
201 .probe = w1_gpio_probe,
202 .remove = w1_gpio_remove,
205 module_platform_driver(w1_gpio_driver);
207 MODULE_DESCRIPTION("GPIO w1 bus master driver");
208 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
209 MODULE_LICENSE("GPL");