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 | ||
a5edeccb | 31 | #include <linux/of_gpio.h> |
dacac4da | 32 | #include <linux/of_mdio.h> |
a5edeccb LP |
33 | |
34 | struct mdio_gpio_info { | |
35 | struct mdiobb_ctrl ctrl; | |
36 | int mdc, mdio; | |
37 | }; | |
38 | ||
e92bdf4b SK |
39 | static void *mdio_gpio_of_get_data(struct platform_device *pdev) |
40 | { | |
41 | struct device_node *np = pdev->dev.of_node; | |
42 | struct mdio_gpio_platform_data *pdata; | |
43 | int ret; | |
44 | ||
45 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); | |
46 | if (!pdata) | |
47 | return NULL; | |
48 | ||
49 | ret = of_get_gpio(np, 0); | |
50 | if (ret < 0) | |
51 | return NULL; | |
52 | ||
53 | pdata->mdc = ret; | |
54 | ||
55 | ret = of_get_gpio(np, 1); | |
56 | if (ret < 0) | |
57 | return NULL; | |
58 | pdata->mdio = ret; | |
59 | ||
60 | return pdata; | |
61 | } | |
62 | ||
a5edeccb LP |
63 | static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) |
64 | { | |
65 | struct mdio_gpio_info *bitbang = | |
66 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
67 | ||
68 | if (dir) | |
69 | gpio_direction_output(bitbang->mdio, 1); | |
70 | else | |
71 | gpio_direction_input(bitbang->mdio); | |
72 | } | |
73 | ||
f004f3ea | 74 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
a5edeccb LP |
75 | { |
76 | struct mdio_gpio_info *bitbang = | |
77 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
78 | ||
79 | return gpio_get_value(bitbang->mdio); | |
80 | } | |
81 | ||
f004f3ea | 82 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) |
a5edeccb LP |
83 | { |
84 | struct mdio_gpio_info *bitbang = | |
85 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
86 | ||
87 | gpio_set_value(bitbang->mdio, what); | |
88 | } | |
89 | ||
f004f3ea | 90 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
a5edeccb LP |
91 | { |
92 | struct mdio_gpio_info *bitbang = | |
93 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
94 | ||
95 | gpio_set_value(bitbang->mdc, what); | |
96 | } | |
97 | ||
98 | static struct mdiobb_ops mdio_gpio_ops = { | |
99 | .owner = THIS_MODULE, | |
f004f3ea | 100 | .set_mdc = mdc_set, |
a5edeccb | 101 | .set_mdio_dir = mdio_dir, |
f004f3ea PZ |
102 | .set_mdio_data = mdio_set, |
103 | .get_mdio_data = mdio_get, | |
a5edeccb LP |
104 | }; |
105 | ||
633d1594 | 106 | static struct mii_bus *mdio_gpio_bus_init(struct device *dev, |
1dd06ae8 GKH |
107 | struct mdio_gpio_platform_data *pdata, |
108 | int bus_id) | |
a5edeccb | 109 | { |
a5edeccb LP |
110 | struct mii_bus *new_bus; |
111 | struct mdio_gpio_info *bitbang; | |
a5edeccb LP |
112 | int i; |
113 | ||
f004f3ea | 114 | bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL); |
a5edeccb LP |
115 | if (!bitbang) |
116 | goto out; | |
117 | ||
118 | bitbang->ctrl.ops = &mdio_gpio_ops; | |
64882709 | 119 | bitbang->ctrl.reset = pdata->reset; |
f004f3ea PZ |
120 | bitbang->mdc = pdata->mdc; |
121 | bitbang->mdio = pdata->mdio; | |
a5edeccb LP |
122 | |
123 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); | |
124 | if (!new_bus) | |
298cf9be | 125 | goto out_free_bitbang; |
a5edeccb | 126 | |
f004f3ea | 127 | new_bus->name = "GPIO Bitbanged MDIO", |
a5edeccb | 128 | |
f004f3ea PZ |
129 | new_bus->phy_mask = pdata->phy_mask; |
130 | new_bus->irq = pdata->irqs; | |
131 | new_bus->parent = dev; | |
132 | ||
133 | if (new_bus->phy_mask == ~0) | |
a5edeccb LP |
134 | goto out_free_bus; |
135 | ||
136 | for (i = 0; i < PHY_MAX_ADDR; i++) | |
f004f3ea PZ |
137 | if (!new_bus->irq[i]) |
138 | new_bus->irq[i] = PHY_POLL; | |
a5edeccb | 139 | |
a77e929a | 140 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); |
f004f3ea PZ |
141 | |
142 | if (gpio_request(bitbang->mdc, "mdc")) | |
143 | goto out_free_bus; | |
a5edeccb | 144 | |
f004f3ea PZ |
145 | if (gpio_request(bitbang->mdio, "mdio")) |
146 | goto out_free_mdc; | |
147 | ||
664f93b4 PZ |
148 | gpio_direction_output(bitbang->mdc, 0); |
149 | ||
f004f3ea | 150 | dev_set_drvdata(dev, new_bus); |
a5edeccb | 151 | |
dacac4da | 152 | return new_bus; |
a5edeccb | 153 | |
f004f3ea PZ |
154 | out_free_mdc: |
155 | gpio_free(bitbang->mdc); | |
a5edeccb | 156 | out_free_bus: |
a5edeccb | 157 | free_mdio_bitbang(new_bus); |
298cf9be LB |
158 | out_free_bitbang: |
159 | kfree(bitbang); | |
a5edeccb | 160 | out: |
dacac4da | 161 | return NULL; |
a5edeccb LP |
162 | } |
163 | ||
f99b4a02 | 164 | static void mdio_gpio_bus_deinit(struct device *dev) |
a5edeccb | 165 | { |
f004f3ea | 166 | struct mii_bus *bus = dev_get_drvdata(dev); |
a5edeccb LP |
167 | struct mdio_gpio_info *bitbang = bus->priv; |
168 | ||
f004f3ea | 169 | dev_set_drvdata(dev, NULL); |
f004f3ea | 170 | gpio_free(bitbang->mdio); |
dacac4da MW |
171 | gpio_free(bitbang->mdc); |
172 | free_mdio_bitbang(bus); | |
a5edeccb | 173 | kfree(bitbang); |
f004f3ea PZ |
174 | } |
175 | ||
633d1594 | 176 | static void mdio_gpio_bus_destroy(struct device *dev) |
dacac4da MW |
177 | { |
178 | struct mii_bus *bus = dev_get_drvdata(dev); | |
179 | ||
180 | mdiobus_unregister(bus); | |
181 | mdio_gpio_bus_deinit(dev); | |
182 | } | |
183 | ||
633d1594 | 184 | static int mdio_gpio_probe(struct platform_device *pdev) |
f004f3ea | 185 | { |
e92bdf4b | 186 | struct mdio_gpio_platform_data *pdata; |
dacac4da | 187 | struct mii_bus *new_bus; |
3272dd9b | 188 | int ret, bus_id; |
f004f3ea | 189 | |
3272dd9b | 190 | if (pdev->dev.of_node) { |
e92bdf4b | 191 | pdata = mdio_gpio_of_get_data(pdev); |
3272dd9b SK |
192 | bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio"); |
193 | } else { | |
e92bdf4b | 194 | pdata = pdev->dev.platform_data; |
3272dd9b SK |
195 | bus_id = pdev->id; |
196 | } | |
e92bdf4b | 197 | |
f004f3ea PZ |
198 | if (!pdata) |
199 | return -ENODEV; | |
200 | ||
3272dd9b | 201 | new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id); |
dacac4da MW |
202 | if (!new_bus) |
203 | return -ENODEV; | |
204 | ||
e92bdf4b SK |
205 | if (pdev->dev.of_node) |
206 | ret = of_mdiobus_register(new_bus, pdev->dev.of_node); | |
207 | else | |
208 | ret = mdiobus_register(new_bus); | |
209 | ||
dacac4da MW |
210 | if (ret) |
211 | mdio_gpio_bus_deinit(&pdev->dev); | |
212 | ||
213 | return ret; | |
f004f3ea PZ |
214 | } |
215 | ||
633d1594 | 216 | static int mdio_gpio_remove(struct platform_device *pdev) |
f004f3ea PZ |
217 | { |
218 | mdio_gpio_bus_destroy(&pdev->dev); | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
e92bdf4b SK |
223 | static struct of_device_id mdio_gpio_of_match[] = { |
224 | { .compatible = "virtual,mdio-gpio", }, | |
225 | { /* sentinel */ } | |
a5edeccb LP |
226 | }; |
227 | ||
f004f3ea PZ |
228 | static struct platform_driver mdio_gpio_driver = { |
229 | .probe = mdio_gpio_probe, | |
633d1594 | 230 | .remove = mdio_gpio_remove, |
f004f3ea PZ |
231 | .driver = { |
232 | .name = "mdio-gpio", | |
233 | .owner = THIS_MODULE, | |
e92bdf4b | 234 | .of_match_table = mdio_gpio_of_match, |
f004f3ea PZ |
235 | }, |
236 | }; | |
237 | ||
f8e5fc8c | 238 | module_platform_driver(mdio_gpio_driver); |
a5edeccb | 239 | |
f004f3ea PZ |
240 | MODULE_ALIAS("platform:mdio-gpio"); |
241 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); | |
242 | MODULE_LICENSE("GPL"); | |
243 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO"); |