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>
24 #include "../w1_int.h"
26 static void w1_gpio_write_bit_dir(void *data, u8 bit)
28 struct w1_gpio_platform_data *pdata = data;
31 gpio_direction_input(pdata->pin);
33 gpio_direction_output(pdata->pin, 0);
36 static void w1_gpio_write_bit_val(void *data, u8 bit)
38 struct w1_gpio_platform_data *pdata = data;
40 gpio_set_value(pdata->pin, bit);
43 static u8 w1_gpio_read_bit(void *data)
45 struct w1_gpio_platform_data *pdata = data;
47 return gpio_get_value(pdata->pin) ? 1 : 0;
50 #if defined(CONFIG_OF)
51 static struct of_device_id w1_gpio_dt_ids[] = {
52 { .compatible = "w1-gpio" },
55 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
58 static int w1_gpio_probe_dt(struct platform_device *pdev)
60 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
61 struct device_node *np = pdev->dev.of_node;
63 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
67 if (of_get_property(np, "linux,open-drain", NULL))
68 pdata->is_open_drain = 1;
70 pdata->pin = of_get_gpio(np, 0);
71 pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
72 pdev->dev.platform_data = pdata;
77 static int w1_gpio_probe(struct platform_device *pdev)
79 struct w1_bus_master *master;
80 struct w1_gpio_platform_data *pdata;
81 struct pinctrl *pinctrl;
84 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
86 dev_warn(&pdev->dev, "unable to select pin group\n");
88 if (of_have_populated_dt()) {
89 err = w1_gpio_probe_dt(pdev);
91 dev_err(&pdev->dev, "Failed to parse DT\n");
96 pdata = pdev->dev.platform_data;
99 dev_err(&pdev->dev, "No configuration data\n");
103 master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
105 dev_err(&pdev->dev, "Out of memory\n");
109 err = gpio_request(pdata->pin, "w1");
111 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
115 if (gpio_is_valid(pdata->ext_pullup_enable_pin)) {
116 err = gpio_request_one(pdata->ext_pullup_enable_pin,
117 GPIOF_INIT_LOW, "w1 pullup");
119 dev_err(&pdev->dev, "gpio_request_one "
120 "(ext_pullup_enable_pin) failed\n");
125 master->data = pdata;
126 master->read_bit = w1_gpio_read_bit;
128 if (pdata->is_open_drain) {
129 gpio_direction_output(pdata->pin, 1);
130 master->write_bit = w1_gpio_write_bit_val;
132 gpio_direction_input(pdata->pin);
133 master->write_bit = w1_gpio_write_bit_dir;
136 err = w1_add_master_device(master);
138 dev_err(&pdev->dev, "w1_add_master device failed\n");
139 goto free_gpio_ext_pu;
142 if (pdata->enable_external_pullup)
143 pdata->enable_external_pullup(1);
145 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
146 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
148 platform_set_drvdata(pdev, master);
153 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
154 gpio_free(pdata->ext_pullup_enable_pin);
156 gpio_free(pdata->pin);
163 static int w1_gpio_remove(struct platform_device *pdev)
165 struct w1_bus_master *master = platform_get_drvdata(pdev);
166 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
168 if (pdata->enable_external_pullup)
169 pdata->enable_external_pullup(0);
171 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
172 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
174 w1_remove_master_device(master);
175 gpio_free(pdata->pin);
183 static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
185 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
187 if (pdata->enable_external_pullup)
188 pdata->enable_external_pullup(0);
193 static int w1_gpio_resume(struct platform_device *pdev)
195 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
197 if (pdata->enable_external_pullup)
198 pdata->enable_external_pullup(1);
204 #define w1_gpio_suspend NULL
205 #define w1_gpio_resume NULL
208 static struct platform_driver w1_gpio_driver = {
211 .owner = THIS_MODULE,
212 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
214 .probe = w1_gpio_probe,
215 .remove = w1_gpio_remove,
216 .suspend = w1_gpio_suspend,
217 .resume = w1_gpio_resume,
220 module_platform_driver(w1_gpio_driver);
222 MODULE_DESCRIPTION("GPIO w1 bus master driver");
223 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
224 MODULE_LICENSE("GPL");