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.
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.h>
17 #include <linux/of_platform.h>
18 #include <linux/of_gpio.h>
19 #include <linux/pinctrl/consumer.h>
20 #include <linux/err.h>
23 #include "../w1_int.h"
25 static void w1_gpio_write_bit_dir(void *data, u8 bit)
27 struct w1_gpio_platform_data *pdata = data;
30 gpio_direction_input(pdata->pin);
32 gpio_direction_output(pdata->pin, 0);
35 static void w1_gpio_write_bit_val(void *data, u8 bit)
37 struct w1_gpio_platform_data *pdata = data;
39 gpio_set_value(pdata->pin, bit);
42 static u8 w1_gpio_read_bit(void *data)
44 struct w1_gpio_platform_data *pdata = data;
46 return gpio_get_value(pdata->pin) ? 1 : 0;
50 static struct of_device_id w1_gpio_dt_ids[] = {
51 { .compatible = "w1-gpio" },
54 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
56 static int w1_gpio_probe_dt(struct platform_device *pdev)
58 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
59 struct device_node *np = pdev->dev.of_node;
60 const struct of_device_id *of_id =
61 of_match_device(w1_gpio_dt_ids, &pdev->dev);
66 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
70 if (of_get_property(np, "linux,open-drain", NULL))
71 pdata->is_open_drain = 1;
73 pdata->pin = of_get_gpio(np, 0);
74 pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
75 pdev->dev.platform_data = pdata;
80 static int w1_gpio_probe_dt(struct platform_device *pdev)
86 static int __init w1_gpio_probe(struct platform_device *pdev)
88 struct w1_bus_master *master;
89 struct w1_gpio_platform_data *pdata;
90 struct pinctrl *pinctrl;
93 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
95 dev_warn(&pdev->dev, "unable to select pin group\n");
97 err = w1_gpio_probe_dt(pdev);
101 pdata = pdev->dev.platform_data;
106 master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
110 err = gpio_request(pdata->pin, "w1");
114 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
115 err = gpio_request_one(pdata->ext_pullup_enable_pin,
116 GPIOF_INIT_LOW, "w1 pullup");
121 master->data = pdata;
122 master->read_bit = w1_gpio_read_bit;
124 if (pdata->is_open_drain) {
125 gpio_direction_output(pdata->pin, 1);
126 master->write_bit = w1_gpio_write_bit_val;
128 gpio_direction_input(pdata->pin);
129 master->write_bit = w1_gpio_write_bit_dir;
132 err = w1_add_master_device(master);
134 goto free_gpio_ext_pu;
136 if (pdata->enable_external_pullup)
137 pdata->enable_external_pullup(1);
139 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
140 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
142 platform_set_drvdata(pdev, master);
147 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
148 gpio_free(pdata->ext_pullup_enable_pin);
150 gpio_free(pdata->pin);
157 static int __exit w1_gpio_remove(struct platform_device *pdev)
159 struct w1_bus_master *master = platform_get_drvdata(pdev);
160 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
162 if (pdata->enable_external_pullup)
163 pdata->enable_external_pullup(0);
165 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
166 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
168 w1_remove_master_device(master);
169 gpio_free(pdata->pin);
177 static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
179 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
181 if (pdata->enable_external_pullup)
182 pdata->enable_external_pullup(0);
187 static int w1_gpio_resume(struct platform_device *pdev)
189 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
191 if (pdata->enable_external_pullup)
192 pdata->enable_external_pullup(1);
198 #define w1_gpio_suspend NULL
199 #define w1_gpio_resume NULL
202 static struct platform_driver w1_gpio_driver = {
205 .owner = THIS_MODULE,
206 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
208 .remove = __exit_p(w1_gpio_remove),
209 .suspend = w1_gpio_suspend,
210 .resume = w1_gpio_resume,
213 static int __init w1_gpio_init(void)
215 return platform_driver_probe(&w1_gpio_driver, w1_gpio_probe);
218 static void __exit w1_gpio_exit(void)
220 platform_driver_unregister(&w1_gpio_driver);
223 module_init(w1_gpio_init);
224 module_exit(w1_gpio_exit);
226 MODULE_DESCRIPTION("GPIO w1 bus master driver");
227 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
228 MODULE_LICENSE("GPL");