Commit | Line | Data |
---|---|---|
a5edeccb | 1 | /* |
f004f3ea PZ |
2 | * GPIO based MDIO bitbang driver. |
3 | * Supports OpenFirmware. | |
a5edeccb LP |
4 | * |
5 | * Copyright (c) 2008 CSE Semaphore Belgium. | |
6 | * by Laurent Pinchart <laurentp@cse-semaphore.com> | |
7 | * | |
f004f3ea PZ |
8 | * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt> |
9 | * | |
a5edeccb LP |
10 | * Based on earlier work by |
11 | * | |
12 | * Copyright (c) 2003 Intracom S.A. | |
13 | * by Pantelis Antoniou <panto@intracom.gr> | |
14 | * | |
15 | * 2005 (c) MontaVista Software, Inc. | |
16 | * Vitaly Bordug <vbordug@ru.mvista.com> | |
17 | * | |
18 | * This file is licensed under the terms of the GNU General Public License | |
19 | * version 2. This program is licensed "as is" without any warranty of any | |
20 | * kind, whether express or implied. | |
21 | */ | |
22 | ||
23 | #include <linux/module.h> | |
24 | #include <linux/slab.h> | |
25 | #include <linux/init.h> | |
26 | #include <linux/interrupt.h> | |
f004f3ea PZ |
27 | #include <linux/platform_device.h> |
28 | #include <linux/gpio.h> | |
29 | #include <linux/mdio-gpio.h> | |
30 | ||
31 | #ifdef CONFIG_OF_GPIO | |
a5edeccb | 32 | #include <linux/of_gpio.h> |
dacac4da | 33 | #include <linux/of_mdio.h> |
a5edeccb | 34 | #include <linux/of_platform.h> |
f004f3ea | 35 | #endif |
a5edeccb LP |
36 | |
37 | struct mdio_gpio_info { | |
38 | struct mdiobb_ctrl ctrl; | |
39 | int mdc, mdio; | |
40 | }; | |
41 | ||
42 | static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) | |
43 | { | |
44 | struct mdio_gpio_info *bitbang = | |
45 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
46 | ||
47 | if (dir) | |
48 | gpio_direction_output(bitbang->mdio, 1); | |
49 | else | |
50 | gpio_direction_input(bitbang->mdio); | |
51 | } | |
52 | ||
f004f3ea | 53 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
a5edeccb LP |
54 | { |
55 | struct mdio_gpio_info *bitbang = | |
56 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
57 | ||
58 | return gpio_get_value(bitbang->mdio); | |
59 | } | |
60 | ||
f004f3ea | 61 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) |
a5edeccb LP |
62 | { |
63 | struct mdio_gpio_info *bitbang = | |
64 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
65 | ||
66 | gpio_set_value(bitbang->mdio, what); | |
67 | } | |
68 | ||
f004f3ea | 69 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
a5edeccb LP |
70 | { |
71 | struct mdio_gpio_info *bitbang = | |
72 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
73 | ||
74 | gpio_set_value(bitbang->mdc, what); | |
75 | } | |
76 | ||
77 | static struct mdiobb_ops mdio_gpio_ops = { | |
78 | .owner = THIS_MODULE, | |
f004f3ea | 79 | .set_mdc = mdc_set, |
a5edeccb | 80 | .set_mdio_dir = mdio_dir, |
f004f3ea PZ |
81 | .set_mdio_data = mdio_set, |
82 | .get_mdio_data = mdio_get, | |
a5edeccb LP |
83 | }; |
84 | ||
dacac4da | 85 | static struct mii_bus * __devinit mdio_gpio_bus_init(struct device *dev, |
f004f3ea PZ |
86 | struct mdio_gpio_platform_data *pdata, |
87 | int bus_id) | |
a5edeccb | 88 | { |
a5edeccb LP |
89 | struct mii_bus *new_bus; |
90 | struct mdio_gpio_info *bitbang; | |
a5edeccb LP |
91 | int i; |
92 | ||
f004f3ea | 93 | bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL); |
a5edeccb LP |
94 | if (!bitbang) |
95 | goto out; | |
96 | ||
97 | bitbang->ctrl.ops = &mdio_gpio_ops; | |
64882709 | 98 | bitbang->ctrl.reset = pdata->reset; |
f004f3ea PZ |
99 | bitbang->mdc = pdata->mdc; |
100 | bitbang->mdio = pdata->mdio; | |
a5edeccb LP |
101 | |
102 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); | |
103 | if (!new_bus) | |
298cf9be | 104 | goto out_free_bitbang; |
a5edeccb | 105 | |
f004f3ea | 106 | new_bus->name = "GPIO Bitbanged MDIO", |
a5edeccb | 107 | |
f004f3ea PZ |
108 | new_bus->phy_mask = pdata->phy_mask; |
109 | new_bus->irq = pdata->irqs; | |
110 | new_bus->parent = dev; | |
111 | ||
112 | if (new_bus->phy_mask == ~0) | |
a5edeccb LP |
113 | goto out_free_bus; |
114 | ||
115 | for (i = 0; i < PHY_MAX_ADDR; i++) | |
f004f3ea PZ |
116 | if (!new_bus->irq[i]) |
117 | new_bus->irq[i] = PHY_POLL; | |
a5edeccb | 118 | |
a77e929a | 119 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); |
f004f3ea PZ |
120 | |
121 | if (gpio_request(bitbang->mdc, "mdc")) | |
122 | goto out_free_bus; | |
a5edeccb | 123 | |
f004f3ea PZ |
124 | if (gpio_request(bitbang->mdio, "mdio")) |
125 | goto out_free_mdc; | |
126 | ||
664f93b4 PZ |
127 | gpio_direction_output(bitbang->mdc, 0); |
128 | ||
f004f3ea | 129 | dev_set_drvdata(dev, new_bus); |
a5edeccb | 130 | |
dacac4da | 131 | return new_bus; |
a5edeccb | 132 | |
f004f3ea PZ |
133 | out_free_mdc: |
134 | gpio_free(bitbang->mdc); | |
a5edeccb | 135 | out_free_bus: |
a5edeccb | 136 | free_mdio_bitbang(new_bus); |
298cf9be LB |
137 | out_free_bitbang: |
138 | kfree(bitbang); | |
a5edeccb | 139 | out: |
dacac4da | 140 | return NULL; |
a5edeccb LP |
141 | } |
142 | ||
f99b4a02 | 143 | static void mdio_gpio_bus_deinit(struct device *dev) |
a5edeccb | 144 | { |
f004f3ea | 145 | struct mii_bus *bus = dev_get_drvdata(dev); |
a5edeccb LP |
146 | struct mdio_gpio_info *bitbang = bus->priv; |
147 | ||
f004f3ea | 148 | dev_set_drvdata(dev, NULL); |
f004f3ea | 149 | gpio_free(bitbang->mdio); |
dacac4da MW |
150 | gpio_free(bitbang->mdc); |
151 | free_mdio_bitbang(bus); | |
a5edeccb | 152 | kfree(bitbang); |
f004f3ea PZ |
153 | } |
154 | ||
dacac4da MW |
155 | static void __devexit mdio_gpio_bus_destroy(struct device *dev) |
156 | { | |
157 | struct mii_bus *bus = dev_get_drvdata(dev); | |
158 | ||
159 | mdiobus_unregister(bus); | |
160 | mdio_gpio_bus_deinit(dev); | |
161 | } | |
162 | ||
f004f3ea PZ |
163 | static int __devinit mdio_gpio_probe(struct platform_device *pdev) |
164 | { | |
165 | struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data; | |
dacac4da MW |
166 | struct mii_bus *new_bus; |
167 | int ret; | |
f004f3ea PZ |
168 | |
169 | if (!pdata) | |
170 | return -ENODEV; | |
171 | ||
dacac4da MW |
172 | new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id); |
173 | if (!new_bus) | |
174 | return -ENODEV; | |
175 | ||
176 | ret = mdiobus_register(new_bus); | |
177 | if (ret) | |
178 | mdio_gpio_bus_deinit(&pdev->dev); | |
179 | ||
180 | return ret; | |
f004f3ea PZ |
181 | } |
182 | ||
183 | static int __devexit mdio_gpio_remove(struct platform_device *pdev) | |
184 | { | |
185 | mdio_gpio_bus_destroy(&pdev->dev); | |
186 | ||
187 | return 0; | |
188 | } | |
189 | ||
190 | #ifdef CONFIG_OF_GPIO | |
f004f3ea | 191 | |
74888760 | 192 | static int __devinit mdio_ofgpio_probe(struct platform_device *ofdev) |
f004f3ea | 193 | { |
f004f3ea | 194 | struct mdio_gpio_platform_data *pdata; |
dacac4da | 195 | struct mii_bus *new_bus; |
57a57499 | 196 | int ret; |
f004f3ea PZ |
197 | |
198 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); | |
199 | if (!pdata) | |
200 | return -ENOMEM; | |
201 | ||
61c7a080 | 202 | ret = of_get_gpio(ofdev->dev.of_node, 0); |
57a57499 | 203 | if (ret < 0) |
f004f3ea | 204 | goto out_free; |
57a57499 RK |
205 | pdata->mdc = ret; |
206 | ||
61c7a080 | 207 | ret = of_get_gpio(ofdev->dev.of_node, 1); |
57a57499 | 208 | if (ret < 0) |
dacac4da | 209 | goto out_free; |
57a57499 | 210 | pdata->mdio = ret; |
f004f3ea | 211 | |
dacac4da MW |
212 | new_bus = mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc); |
213 | if (!new_bus) | |
a4b11649 | 214 | goto out_free; |
f004f3ea | 215 | |
61c7a080 | 216 | ret = of_mdiobus_register(new_bus, ofdev->dev.of_node); |
dacac4da MW |
217 | if (ret) |
218 | mdio_gpio_bus_deinit(&ofdev->dev); | |
219 | ||
220 | return ret; | |
f004f3ea PZ |
221 | |
222 | out_free: | |
223 | kfree(pdata); | |
224 | return -ENODEV; | |
225 | } | |
226 | ||
2dc11581 | 227 | static int __devexit mdio_ofgpio_remove(struct platform_device *ofdev) |
f004f3ea PZ |
228 | { |
229 | mdio_gpio_bus_destroy(&ofdev->dev); | |
230 | kfree(ofdev->dev.platform_data); | |
a5edeccb LP |
231 | |
232 | return 0; | |
233 | } | |
234 | ||
235 | static struct of_device_id mdio_ofgpio_match[] = { | |
236 | { | |
237 | .compatible = "virtual,mdio-gpio", | |
238 | }, | |
239 | {}, | |
240 | }; | |
e72701ac | 241 | MODULE_DEVICE_TABLE(of, mdio_ofgpio_match); |
a5edeccb | 242 | |
74888760 | 243 | static struct platform_driver mdio_ofgpio_driver = { |
4018294b | 244 | .driver = { |
f42af6c4 | 245 | .name = "mdio-ofgpio", |
4018294b GL |
246 | .owner = THIS_MODULE, |
247 | .of_match_table = mdio_ofgpio_match, | |
248 | }, | |
a5edeccb | 249 | .probe = mdio_ofgpio_probe, |
f004f3ea | 250 | .remove = __devexit_p(mdio_ofgpio_remove), |
a5edeccb LP |
251 | }; |
252 | ||
f004f3ea | 253 | static inline int __init mdio_ofgpio_init(void) |
a5edeccb | 254 | { |
74888760 | 255 | return platform_driver_register(&mdio_ofgpio_driver); |
a5edeccb LP |
256 | } |
257 | ||
b4e51854 | 258 | static inline void mdio_ofgpio_exit(void) |
a5edeccb | 259 | { |
74888760 | 260 | platform_driver_unregister(&mdio_ofgpio_driver); |
a5edeccb | 261 | } |
f004f3ea PZ |
262 | #else |
263 | static inline int __init mdio_ofgpio_init(void) { return 0; } | |
b4e51854 | 264 | static inline void mdio_ofgpio_exit(void) { } |
f004f3ea PZ |
265 | #endif /* CONFIG_OF_GPIO */ |
266 | ||
267 | static struct platform_driver mdio_gpio_driver = { | |
268 | .probe = mdio_gpio_probe, | |
269 | .remove = __devexit_p(mdio_gpio_remove), | |
270 | .driver = { | |
271 | .name = "mdio-gpio", | |
272 | .owner = THIS_MODULE, | |
273 | }, | |
274 | }; | |
275 | ||
276 | static int __init mdio_gpio_init(void) | |
277 | { | |
278 | int ret; | |
279 | ||
280 | ret = mdio_ofgpio_init(); | |
281 | if (ret) | |
282 | return ret; | |
283 | ||
284 | ret = platform_driver_register(&mdio_gpio_driver); | |
285 | if (ret) | |
286 | mdio_ofgpio_exit(); | |
287 | ||
288 | return ret; | |
289 | } | |
290 | module_init(mdio_gpio_init); | |
291 | ||
292 | static void __exit mdio_gpio_exit(void) | |
293 | { | |
294 | platform_driver_unregister(&mdio_gpio_driver); | |
295 | mdio_ofgpio_exit(); | |
296 | } | |
297 | module_exit(mdio_gpio_exit); | |
a5edeccb | 298 | |
f004f3ea PZ |
299 | MODULE_ALIAS("platform:mdio-gpio"); |
300 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); | |
301 | MODULE_LICENSE("GPL"); | |
302 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO"); |