blob: f01c336233da0558a4e5078506429e54d83eb54b [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>
Ville Syrjalaad8dc962008-02-06 01:39:01 -080019
20#include "../w1.h"
21#include "../w1_int.h"
22
Ville Syrjalaad8dc962008-02-06 01:39:01 -080023static void w1_gpio_write_bit_dir(void *data, u8 bit)
24{
25 struct w1_gpio_platform_data *pdata = data;
26
27 if (bit)
28 gpio_direction_input(pdata->pin);
29 else
30 gpio_direction_output(pdata->pin, 0);
31}
32
33static void w1_gpio_write_bit_val(void *data, u8 bit)
34{
35 struct w1_gpio_platform_data *pdata = data;
36
37 gpio_set_value(pdata->pin, bit);
38}
39
40static u8 w1_gpio_read_bit(void *data)
41{
42 struct w1_gpio_platform_data *pdata = data;
43
Daniel Mack8d0df7a2009-03-12 14:31:25 -070044 return gpio_get_value(pdata->pin) ? 1 : 0;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080045}
46
Daniel Mack5f3d1382012-07-23 16:36:35 +020047#ifdef CONFIG_OF
48static struct of_device_id w1_gpio_dt_ids[] = {
49 { .compatible = "w1-gpio" },
50 {}
51};
52MODULE_DEVICE_TABLE(of, w1_gpio_dt_ids);
53
54static int w1_gpio_probe_dt(struct platform_device *pdev)
55{
56 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
57 struct device_node *np = pdev->dev.of_node;
58 const struct of_device_id *of_id =
59 of_match_device(w1_gpio_dt_ids, &pdev->dev);
60
61 if (!of_id)
62 return 0;
63
64 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
65 if (!pdata)
66 return -ENOMEM;
67
68 if (of_get_property(np, "linux,open-drain", NULL))
69 pdata->is_open_drain = 1;
70
71 pdata->pin = of_get_gpio(np, 0);
72 pdata->ext_pullup_enable_pin = of_get_gpio(np, 1);
73 pdev->dev.platform_data = pdata;
74
75 return 0;
76}
77#else
78static int w1_gpio_probe_dt(struct platform_device *pdev)
79{
80 return 0;
81}
82#endif
83
Ville Syrjalaad8dc962008-02-06 01:39:01 -080084static int __init w1_gpio_probe(struct platform_device *pdev)
85{
86 struct w1_bus_master *master;
Daniel Mack5f3d1382012-07-23 16:36:35 +020087 struct w1_gpio_platform_data *pdata;
Ville Syrjalaad8dc962008-02-06 01:39:01 -080088 int err;
89
Daniel Mack5f3d1382012-07-23 16:36:35 +020090 err = w1_gpio_probe_dt(pdev);
91 if (err < 0)
92 return err;
93
94 pdata = pdev->dev.platform_data;
95
Ville Syrjalaad8dc962008-02-06 01:39:01 -080096 if (!pdata)
97 return -ENXIO;
98
99 master = kzalloc(sizeof(struct w1_bus_master), GFP_KERNEL);
100 if (!master)
101 return -ENOMEM;
102
103 err = gpio_request(pdata->pin, "w1");
104 if (err)
105 goto free_master;
106
107 master->data = pdata;
108 master->read_bit = w1_gpio_read_bit;
109
110 if (pdata->is_open_drain) {
111 gpio_direction_output(pdata->pin, 1);
112 master->write_bit = w1_gpio_write_bit_val;
113 } else {
114 gpio_direction_input(pdata->pin);
115 master->write_bit = w1_gpio_write_bit_dir;
116 }
117
118 err = w1_add_master_device(master);
119 if (err)
120 goto free_gpio;
121
Daniel Mackc8a06c12009-06-17 16:28:15 -0700122 if (pdata->enable_external_pullup)
123 pdata->enable_external_pullup(1);
124
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800125 platform_set_drvdata(pdev, master);
126
127 return 0;
128
129 free_gpio:
130 gpio_free(pdata->pin);
131 free_master:
132 kfree(master);
133
134 return err;
135}
136
137static int __exit w1_gpio_remove(struct platform_device *pdev)
138{
139 struct w1_bus_master *master = platform_get_drvdata(pdev);
140 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
141
Daniel Mackc8a06c12009-06-17 16:28:15 -0700142 if (pdata->enable_external_pullup)
143 pdata->enable_external_pullup(0);
144
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800145 w1_remove_master_device(master);
146 gpio_free(pdata->pin);
147 kfree(master);
148
149 return 0;
150}
151
Daniel Mackc8a06c12009-06-17 16:28:15 -0700152#ifdef CONFIG_PM
153
154static int w1_gpio_suspend(struct platform_device *pdev, pm_message_t state)
155{
156 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
157
158 if (pdata->enable_external_pullup)
159 pdata->enable_external_pullup(0);
160
161 return 0;
162}
163
164static int w1_gpio_resume(struct platform_device *pdev)
165{
166 struct w1_gpio_platform_data *pdata = pdev->dev.platform_data;
167
168 if (pdata->enable_external_pullup)
169 pdata->enable_external_pullup(1);
170
171 return 0;
172}
173
174#else
175#define w1_gpio_suspend NULL
176#define w1_gpio_resume NULL
177#endif
178
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800179static struct platform_driver w1_gpio_driver = {
180 .driver = {
181 .name = "w1-gpio",
182 .owner = THIS_MODULE,
Daniel Mack5f3d1382012-07-23 16:36:35 +0200183 .of_match_table = of_match_ptr(w1_gpio_dt_ids),
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800184 },
185 .remove = __exit_p(w1_gpio_remove),
Daniel Mackc8a06c12009-06-17 16:28:15 -0700186 .suspend = w1_gpio_suspend,
187 .resume = w1_gpio_resume,
Ville Syrjalaad8dc962008-02-06 01:39:01 -0800188};
189
190static int __init w1_gpio_init(void)
191{
192 return platform_driver_probe(&w1_gpio_driver, w1_gpio_probe);
193}
194
195static void __exit w1_gpio_exit(void)
196{
197 platform_driver_unregister(&w1_gpio_driver);
198}
199
200module_init(w1_gpio_init);
201module_exit(w1_gpio_exit);
202
203MODULE_DESCRIPTION("GPIO w1 bus master driver");
204MODULE_AUTHOR("Ville Syrjala <syrjala@sci.fi>");
205MODULE_LICENSE("GPL");