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/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;
49 #if defined(CONFIG_OF)
50 static struct of_device_id w1_gpio_dt_ids[] = {
51 { .compatible = "w1-gpio" },
54 MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
57 static int w1_gpio_probe_dt(struct platform_device *pdev)
59 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
60 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 gpio = of_get_gpio(np, 0);
75 pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
76 pdev->dev.platform_data = pdata;
81 static int w1_gpio_probe(struct platform_device *pdev)
83 struct w1_bus_master *master;
84 struct w1_gpio_platform_data *pdata;
87 if (of_have_populated_dt()) {
88 err = w1_gpio_probe_dt(pdev);
90 dev_err(&pdev->dev, "Failed to parse DT\n");
95 pdata = pdev->dev.platform_data;
98 dev_err(&pdev->dev, "No configuration data\n");
102 master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
104 dev_err(&pdev->dev, "Out of memory\n");
108 err = gpio_request(pdata->pin, "w1");
110 dev_err(&pdev->dev, "gpio_request (pin) failed\n");
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");
118 dev_err(&pdev->dev, "gpio_request_one "
119 "(ext_pullup_enable_pin) failed\n");
124 master->data = pdata;
125 master->read_bit = w1_gpio_read_bit;
127 if (pdata->is_open_drain) {
128 gpio_direction_output(pdata->pin, 1);
129 master->write_bit = w1_gpio_write_bit_val;
131 gpio_direction_input(pdata->pin);
132 master->write_bit = w1_gpio_write_bit_dir;
135 err = w1_add_master_device(master);
137 dev_err(&pdev->dev, "w1_add_master device failed\n");
138 goto free_gpio_ext_pu;
141 if (pdata->enable_external_pullup)
142 pdata->enable_external_pullup(1);
144 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
145 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
147 platform_set_drvdata(pdev, master);
152 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
153 gpio_free(pdata->ext_pullup_enable_pin);
155 gpio_free(pdata->pin);
162 static int w1_gpio_remove(struct platform_device *pdev)
164 struct w1_bus_master *master = platform_get_drvdata(pdev);
165 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
167 if (pdata->enable_external_pullup)
168 pdata->enable_external_pullup(0);
170 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
171 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
173 w1_remove_master_device(master);
174 gpio_free(pdata->pin);
182 static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
184 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
186 if (pdata->enable_external_pullup)
187 pdata->enable_external_pullup(0);
192 static int w1_gpio_resume(struct platform_device *pdev)
194 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
196 if (pdata->enable_external_pullup)
197 pdata->enable_external_pullup(1);
203 #define w1_gpio_suspend NULL
204 #define w1_gpio_resume NULL
207 static struct platform_driver w1_gpio_driver = {
210 .owner = THIS_MODULE,
211 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
213 .probe = w1_gpio_probe,
214 .remove = w1_gpio_remove,
215 .suspend = w1_gpio_suspend,
216 .resume = w1_gpio_resume,
219 module_platform_driver(w1_gpio_driver);
221 MODULE_DESCRIPTION("GPIO w1 bus master driver");
222 MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
223 MODULE_LICENSE("GPL");