blob: aec35bd465064365f15157654c1ea9801bb4e927 [file] [log] [blame]
Ville Syrjalaad8dc962008-02-06 01:39:01 -08001/*
2 * w1-gpio - GPIO w1 bus master driver
3 *
4 * Copyright (C) 2007 Ville Syrjala <syrjala@sci.fi>
5 *
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 */
10
11#include <linux/init.h>
12#include <linux/module.h>
13#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Ville Syrjalaad8dc962008-02-06 01:39:01 -080015#include <linux/w1-gpio.h>
Mark Browne250b342012-02-04 16:33:55 +000016#include <linux/gpio.h>
Daniel Mack5f3d1382012-07-23 16:36:35 +020017#include <linux/of_platform.h>
18#include <linux/of_gpio.h>
Pantelis Antoniou277ed0d2012-11-22 01:13:29 +040019#include <linux/pinctrl/consumer.h>
20#include <linux/err.h>
Ville Syrjalaad8dc962008-02-06 01:39:01 -080021
22#include "../w1.h"
23#include "../w1_int.h"
24
Ville Syrjalaad8dc962008-02-06 01:39:01 -080025static void w1_gpio_write_bit_dir(void *data, u8 bit)
26{
27 struct w1_gpio_platform_data *pdata = data;
28
29 if (bit)
30 gpio_direction_input(pdata->pin);
31 else
32 gpio_direction_output(pdata->pin, 0);
33}
34
35static void w1_gpio_write_bit_val(void *data, u8 bit)
36{
37 struct w1_gpio_platform_data *pdata = data;
38
39 gpio_set_value(pdata->pin, bit);
40}
41
42static u8 w1_gpio_read_bit(void *data)
43{
44 struct w1_gpio_platform_data *pdata = data;
45
Daniel Mack8d0df7a2009-03-12 14:31:25 -070046 return gpio_get_value(pdata->pin) ? 1 : 0;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080047}
48
Daniel Mack5f3d1382012-07-23 16:36:35 +020049#ifdef CONFIG_OF
50static struct of_device_id w1_gpio_dt_ids[] = {
51 { .compatible = "w1-gpio" },
52 {}
53};
54MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
55
56static int w1_gpio_probe_dt(struct platform_device *pdev)
57{
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);
62
63 if (!of_id)
64 return 0;
65
66 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
67 if (!pdata)
68 return -ENOMEM;
69
70 if (of_get_property(np, "linux,open-drain", NULL))
71 pdata->is_open_drain = 1;
72
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;
76
77 return 0;
78}
79#else
80static int w1_gpio_probe_dt(struct platform_device *pdev)
81{
82 return 0;
83}
84#endif
85
Ville Syrjalaad8dc962008-02-06 01:39:01 -080086static int __init w1_gpio_probe(struct platform_device *pdev)
87{
88 struct w1_bus_master *master;
Daniel Mack5f3d1382012-07-23 16:36:35 +020089 struct w1_gpio_platform_data *pdata;
Pantelis Antoniou277ed0d2012-11-22 01:13:29 +040090 struct pinctrl *pinctrl;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080091 int err;
92
Pantelis Antoniou277ed0d2012-11-22 01:13:29 +040093 pinctrl = devm_pinctrl_get_select_default(&pdev->dev);
94 if (IS_ERR(pinctrl))
95 dev_warn(&pdev->dev, "unable to select pin group\n");
96
Daniel Mack5f3d1382012-07-23 16:36:35 +020097 err = w1_gpio_probe_dt(pdev);
98 if (err < 0)
99 return err;
100
101 pdata = pdev->dev.platform_data;
102
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800103 if (!pdata)
104 return -ENXIO;
105
106 master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
107 if (!master)
108 return -ENOMEM;
109
110 err = gpio_request(pdata->pin, "w1");
111 if (err)
112 goto free_master;
113
Daniel Mackd2323cf2012-07-25 22:54:29 +0200114 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");
117 if (err < 0)
118 goto free_gpio;
119 }
120
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800121 master->data = pdata;
122 master->read_bit = w1_gpio_read_bit;
123
124 if (pdata->is_open_drain) {
125 gpio_direction_output(pdata->pin, 1);
126 master->write_bit = w1_gpio_write_bit_val;
127 } else {
128 gpio_direction_input(pdata->pin);
129 master->write_bit = w1_gpio_write_bit_dir;
130 }
131
132 err = w1_add_master_device(master);
133 if (err)
Daniel Mackd2323cf2012-07-25 22:54:29 +0200134 goto free_gpio_ext_pu;
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800135
Daniel Mackc8a06c12009-06-17 16:28:15 -0700136 if (pdata->enable_external_pullup)
137 pdata->enable_external_pullup(1);
138
Daniel Mackd2323cf2012-07-25 22:54:29 +0200139 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
140 gpio_set_value(pdata->ext_pullup_enable_pin, 1);
141
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800142 platform_set_drvdata(pdev, master);
143
144 return 0;
145
Daniel Mackd2323cf2012-07-25 22:54:29 +0200146 free_gpio_ext_pu:
147 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
148 gpio_free(pdata->ext_pullup_enable_pin);
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800149 free_gpio:
150 gpio_free(pdata->pin);
151 free_master:
152 kfree(master);
153
154 return err;
155}
156
157static int __exit w1_gpio_remove(struct platform_device *pdev)
158{
159 struct w1_bus_master *master = platform_get_drvdata(pdev);
160 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
161
Daniel Mackc8a06c12009-06-17 16:28:15 -0700162 if (pdata->enable_external_pullup)
163 pdata->enable_external_pullup(0);
164
Daniel Mackd2323cf2012-07-25 22:54:29 +0200165 if (gpio_is_valid(pdata->ext_pullup_enable_pin))
166 gpio_set_value(pdata->ext_pullup_enable_pin, 0);
167
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800168 w1_remove_master_device(master);
169 gpio_free(pdata->pin);
170 kfree(master);
171
172 return 0;
173}
174
Daniel Mackc8a06c12009-06-17 16:28:15 -0700175#ifdef CONFIG_PM
176
177static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
178{
179 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
180
181 if (pdata->enable_external_pullup)
182 pdata->enable_external_pullup(0);
183
184 return 0;
185}
186
187static int w1_gpio_resume(struct platform_device *pdev)
188{
189 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
190
191 if (pdata->enable_external_pullup)
192 pdata->enable_external_pullup(1);
193
194 return 0;
195}
196
197#else
198#define w1_gpio_suspend NULL
199#define w1_gpio_resume NULL
200#endif
201
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800202static struct platform_driver w1_gpio_driver = {
203 .driver = {
204 .name = "w1-gpio",
205 .owner = THIS_MODULE,
Daniel Mack5f3d1382012-07-23 16:36:35 +0200206 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800207 },
208 .remove = __exit_p(w1_gpio_remove),
Daniel Mackc8a06c12009-06-17 16:28:15 -0700209 .suspend = w1_gpio_suspend,
210 .resume = w1_gpio_resume,
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800211};
212
213static int __init w1_gpio_init(void)
214{
215 return platform_driver_probe(&w1_gpio_driver, w1_gpio_probe);
216}
217
218static void __exit w1_gpio_exit(void)
219{
220 platform_driver_unregister(&w1_gpio_driver);
221}
222
223module_init(w1_gpio_init);
224module_exit(w1_gpio_exit);
225
226MODULE_DESCRIPTION("GPIO w1 bus master driver");
227MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
228MODULE_LICENSE("GPL");