]>
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> | |
a5edeccb | 25 | #include <linux/interrupt.h> |
f004f3ea PZ |
26 | #include <linux/platform_device.h> |
27 | #include <linux/gpio.h> | |
e2aacd96 | 28 | #include <linux/platform_data/mdio-gpio.h> |
f004f3ea | 29 | |
a5edeccb | 30 | #include <linux/of_gpio.h> |
dacac4da | 31 | #include <linux/of_mdio.h> |
a5edeccb LP |
32 | |
33 | struct mdio_gpio_info { | |
34 | struct mdiobb_ctrl ctrl; | |
f1d54c47 GR |
35 | int mdc, mdio, mdo; |
36 | int mdc_active_low, mdio_active_low, mdo_active_low; | |
a5edeccb LP |
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; | |
1d251481 | 43 | enum of_gpio_flags flags; |
e92bdf4b SK |
44 | int ret; |
45 | ||
46 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); | |
47 | if (!pdata) | |
48 | return NULL; | |
49 | ||
1d251481 | 50 | ret = of_get_gpio_flags(np, 0, &flags); |
e92bdf4b SK |
51 | if (ret < 0) |
52 | return NULL; | |
53 | ||
54 | pdata->mdc = ret; | |
1d251481 | 55 | pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW; |
e92bdf4b | 56 | |
1d251481 | 57 | ret = of_get_gpio_flags(np, 1, &flags); |
e92bdf4b SK |
58 | if (ret < 0) |
59 | return NULL; | |
60 | pdata->mdio = ret; | |
1d251481 | 61 | pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW; |
e92bdf4b | 62 | |
f1d54c47 GR |
63 | ret = of_get_gpio_flags(np, 2, &flags); |
64 | if (ret > 0) { | |
65 | pdata->mdo = ret; | |
66 | pdata->mdo_active_low = flags & OF_GPIO_ACTIVE_LOW; | |
67 | } | |
68 | ||
e92bdf4b SK |
69 | return pdata; |
70 | } | |
71 | ||
a5edeccb LP |
72 | static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) |
73 | { | |
74 | struct mdio_gpio_info *bitbang = | |
75 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
76 | ||
f1d54c47 GR |
77 | if (bitbang->mdo) { |
78 | /* Separate output pin. Always set its value to high | |
79 | * when changing direction. If direction is input, | |
80 | * assume the pin serves as pull-up. If direction is | |
81 | * output, the default value is high. | |
82 | */ | |
2d6c9091 VD |
83 | gpio_set_value_cansleep(bitbang->mdo, |
84 | 1 ^ bitbang->mdo_active_low); | |
f1d54c47 GR |
85 | return; |
86 | } | |
87 | ||
a5edeccb | 88 | if (dir) |
1d251481 GR |
89 | gpio_direction_output(bitbang->mdio, |
90 | 1 ^ bitbang->mdio_active_low); | |
a5edeccb LP |
91 | else |
92 | gpio_direction_input(bitbang->mdio); | |
93 | } | |
94 | ||
f004f3ea | 95 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
a5edeccb LP |
96 | { |
97 | struct mdio_gpio_info *bitbang = | |
98 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
99 | ||
2d6c9091 VD |
100 | return gpio_get_value_cansleep(bitbang->mdio) ^ |
101 | bitbang->mdio_active_low; | |
a5edeccb LP |
102 | } |
103 | ||
f004f3ea | 104 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) |
a5edeccb LP |
105 | { |
106 | struct mdio_gpio_info *bitbang = | |
107 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
108 | ||
f1d54c47 | 109 | if (bitbang->mdo) |
2d6c9091 VD |
110 | gpio_set_value_cansleep(bitbang->mdo, |
111 | what ^ bitbang->mdo_active_low); | |
f1d54c47 | 112 | else |
2d6c9091 VD |
113 | gpio_set_value_cansleep(bitbang->mdio, |
114 | what ^ bitbang->mdio_active_low); | |
a5edeccb LP |
115 | } |
116 | ||
f004f3ea | 117 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
a5edeccb LP |
118 | { |
119 | struct mdio_gpio_info *bitbang = | |
120 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
121 | ||
2d6c9091 | 122 | gpio_set_value_cansleep(bitbang->mdc, what ^ bitbang->mdc_active_low); |
a5edeccb LP |
123 | } |
124 | ||
125 | static struct mdiobb_ops mdio_gpio_ops = { | |
126 | .owner = THIS_MODULE, | |
f004f3ea | 127 | .set_mdc = mdc_set, |
a5edeccb | 128 | .set_mdio_dir = mdio_dir, |
f004f3ea PZ |
129 | .set_mdio_data = mdio_set, |
130 | .get_mdio_data = mdio_get, | |
a5edeccb LP |
131 | }; |
132 | ||
633d1594 | 133 | static struct mii_bus *mdio_gpio_bus_init(struct device *dev, |
1dd06ae8 GKH |
134 | struct mdio_gpio_platform_data *pdata, |
135 | int bus_id) | |
a5edeccb | 136 | { |
a5edeccb LP |
137 | struct mii_bus *new_bus; |
138 | struct mdio_gpio_info *bitbang; | |
a5edeccb LP |
139 | int i; |
140 | ||
78cdb079 | 141 | bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL); |
a5edeccb LP |
142 | if (!bitbang) |
143 | goto out; | |
144 | ||
145 | bitbang->ctrl.ops = &mdio_gpio_ops; | |
64882709 | 146 | bitbang->ctrl.reset = pdata->reset; |
f004f3ea | 147 | bitbang->mdc = pdata->mdc; |
1d251481 | 148 | bitbang->mdc_active_low = pdata->mdc_active_low; |
f004f3ea | 149 | bitbang->mdio = pdata->mdio; |
1d251481 | 150 | bitbang->mdio_active_low = pdata->mdio_active_low; |
f1d54c47 GR |
151 | bitbang->mdo = pdata->mdo; |
152 | bitbang->mdo_active_low = pdata->mdo_active_low; | |
a5edeccb LP |
153 | |
154 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); | |
155 | if (!new_bus) | |
78cdb079 | 156 | goto out; |
a5edeccb | 157 | |
f004f3ea | 158 | new_bus->name = "GPIO Bitbanged MDIO", |
a5edeccb | 159 | |
f004f3ea | 160 | new_bus->phy_mask = pdata->phy_mask; |
ef7f3a5c | 161 | new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask; |
f004f3ea PZ |
162 | new_bus->irq = pdata->irqs; |
163 | new_bus->parent = dev; | |
164 | ||
165 | if (new_bus->phy_mask == ~0) | |
a5edeccb LP |
166 | goto out_free_bus; |
167 | ||
168 | for (i = 0; i < PHY_MAX_ADDR; i++) | |
f004f3ea PZ |
169 | if (!new_bus->irq[i]) |
170 | new_bus->irq[i] = PHY_POLL; | |
a5edeccb | 171 | |
7c0c8268 BV |
172 | if (bus_id != -1) |
173 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); | |
174 | else | |
175 | strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE); | |
f004f3ea | 176 | |
78cdb079 | 177 | if (devm_gpio_request(dev, bitbang->mdc, "mdc")) |
f004f3ea | 178 | goto out_free_bus; |
a5edeccb | 179 | |
78cdb079 GR |
180 | if (devm_gpio_request(dev, bitbang->mdio, "mdio")) |
181 | goto out_free_bus; | |
f004f3ea | 182 | |
f1d54c47 GR |
183 | if (bitbang->mdo) { |
184 | if (devm_gpio_request(dev, bitbang->mdo, "mdo")) | |
185 | goto out_free_bus; | |
186 | gpio_direction_output(bitbang->mdo, 1); | |
187 | gpio_direction_input(bitbang->mdio); | |
188 | } | |
189 | ||
664f93b4 PZ |
190 | gpio_direction_output(bitbang->mdc, 0); |
191 | ||
f004f3ea | 192 | dev_set_drvdata(dev, new_bus); |
a5edeccb | 193 | |
dacac4da | 194 | return new_bus; |
a5edeccb | 195 | |
a5edeccb | 196 | out_free_bus: |
a5edeccb LP |
197 | free_mdio_bitbang(new_bus); |
198 | out: | |
dacac4da | 199 | return NULL; |
a5edeccb LP |
200 | } |
201 | ||
f99b4a02 | 202 | static void mdio_gpio_bus_deinit(struct device *dev) |
a5edeccb | 203 | { |
f004f3ea | 204 | struct mii_bus *bus = dev_get_drvdata(dev); |
a5edeccb | 205 | |
dacac4da | 206 | free_mdio_bitbang(bus); |
f004f3ea PZ |
207 | } |
208 | ||
633d1594 | 209 | static void mdio_gpio_bus_destroy(struct device *dev) |
dacac4da MW |
210 | { |
211 | struct mii_bus *bus = dev_get_drvdata(dev); | |
212 | ||
213 | mdiobus_unregister(bus); | |
214 | mdio_gpio_bus_deinit(dev); | |
215 | } | |
216 | ||
633d1594 | 217 | static int mdio_gpio_probe(struct platform_device *pdev) |
f004f3ea | 218 | { |
e92bdf4b | 219 | struct mdio_gpio_platform_data *pdata; |
dacac4da | 220 | struct mii_bus *new_bus; |
3272dd9b | 221 | int ret, bus_id; |
f004f3ea | 222 | |
3272dd9b | 223 | if (pdev->dev.of_node) { |
e92bdf4b | 224 | pdata = mdio_gpio_of_get_data(pdev); |
3272dd9b | 225 | bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio"); |
7f52da56 JH |
226 | if (bus_id < 0) { |
227 | dev_warn(&pdev->dev, "failed to get alias id\n"); | |
228 | bus_id = 0; | |
229 | } | |
3272dd9b | 230 | } else { |
9bc7b1cd | 231 | pdata = dev_get_platdata(&pdev->dev); |
3272dd9b SK |
232 | bus_id = pdev->id; |
233 | } | |
e92bdf4b | 234 | |
f004f3ea PZ |
235 | if (!pdata) |
236 | return -ENODEV; | |
237 | ||
3272dd9b | 238 | new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id); |
dacac4da MW |
239 | if (!new_bus) |
240 | return -ENODEV; | |
241 | ||
e92bdf4b SK |
242 | if (pdev->dev.of_node) |
243 | ret = of_mdiobus_register(new_bus, pdev->dev.of_node); | |
244 | else | |
245 | ret = mdiobus_register(new_bus); | |
246 | ||
dacac4da MW |
247 | if (ret) |
248 | mdio_gpio_bus_deinit(&pdev->dev); | |
249 | ||
250 | return ret; | |
f004f3ea PZ |
251 | } |
252 | ||
633d1594 | 253 | static int mdio_gpio_remove(struct platform_device *pdev) |
f004f3ea PZ |
254 | { |
255 | mdio_gpio_bus_destroy(&pdev->dev); | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
d8a7dadb | 260 | static const struct of_device_id mdio_gpio_of_match[] = { |
e92bdf4b SK |
261 | { .compatible = "virtual,mdio-gpio", }, |
262 | { /* sentinel */ } | |
a5edeccb | 263 | }; |
1ccb141e | 264 | MODULE_DEVICE_TABLE(of, mdio_gpio_of_match); |
a5edeccb | 265 | |
f004f3ea PZ |
266 | static struct platform_driver mdio_gpio_driver = { |
267 | .probe = mdio_gpio_probe, | |
633d1594 | 268 | .remove = mdio_gpio_remove, |
f004f3ea PZ |
269 | .driver = { |
270 | .name = "mdio-gpio", | |
e92bdf4b | 271 | .of_match_table = mdio_gpio_of_match, |
f004f3ea PZ |
272 | }, |
273 | }; | |
274 | ||
f8e5fc8c | 275 | module_platform_driver(mdio_gpio_driver); |
a5edeccb | 276 | |
f004f3ea PZ |
277 | MODULE_ALIAS("platform:mdio-gpio"); |
278 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); | |
279 | MODULE_LICENSE("GPL"); | |
280 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO"); |