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> | |
28 | #include <linux/mdio-gpio.h> | |
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 | */ | |
83 | gpio_set_value(bitbang->mdo, 1 ^ bitbang->mdo_active_low); | |
84 | return; | |
85 | } | |
86 | ||
a5edeccb | 87 | if (dir) |
1d251481 GR |
88 | gpio_direction_output(bitbang->mdio, |
89 | 1 ^ bitbang->mdio_active_low); | |
a5edeccb LP |
90 | else |
91 | gpio_direction_input(bitbang->mdio); | |
92 | } | |
93 | ||
f004f3ea | 94 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
a5edeccb LP |
95 | { |
96 | struct mdio_gpio_info *bitbang = | |
97 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
98 | ||
1d251481 | 99 | return gpio_get_value(bitbang->mdio) ^ bitbang->mdio_active_low; |
a5edeccb LP |
100 | } |
101 | ||
f004f3ea | 102 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) |
a5edeccb LP |
103 | { |
104 | struct mdio_gpio_info *bitbang = | |
105 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
106 | ||
f1d54c47 GR |
107 | if (bitbang->mdo) |
108 | gpio_set_value(bitbang->mdo, what ^ bitbang->mdo_active_low); | |
109 | else | |
110 | gpio_set_value(bitbang->mdio, what ^ bitbang->mdio_active_low); | |
a5edeccb LP |
111 | } |
112 | ||
f004f3ea | 113 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
a5edeccb LP |
114 | { |
115 | struct mdio_gpio_info *bitbang = | |
116 | container_of(ctrl, struct mdio_gpio_info, ctrl); | |
117 | ||
1d251481 | 118 | gpio_set_value(bitbang->mdc, what ^ bitbang->mdc_active_low); |
a5edeccb LP |
119 | } |
120 | ||
121 | static struct mdiobb_ops mdio_gpio_ops = { | |
122 | .owner = THIS_MODULE, | |
f004f3ea | 123 | .set_mdc = mdc_set, |
a5edeccb | 124 | .set_mdio_dir = mdio_dir, |
f004f3ea PZ |
125 | .set_mdio_data = mdio_set, |
126 | .get_mdio_data = mdio_get, | |
a5edeccb LP |
127 | }; |
128 | ||
633d1594 | 129 | static struct mii_bus *mdio_gpio_bus_init(struct device *dev, |
1dd06ae8 GKH |
130 | struct mdio_gpio_platform_data *pdata, |
131 | int bus_id) | |
a5edeccb | 132 | { |
a5edeccb LP |
133 | struct mii_bus *new_bus; |
134 | struct mdio_gpio_info *bitbang; | |
a5edeccb LP |
135 | int i; |
136 | ||
78cdb079 | 137 | bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL); |
a5edeccb LP |
138 | if (!bitbang) |
139 | goto out; | |
140 | ||
141 | bitbang->ctrl.ops = &mdio_gpio_ops; | |
64882709 | 142 | bitbang->ctrl.reset = pdata->reset; |
f004f3ea | 143 | bitbang->mdc = pdata->mdc; |
1d251481 | 144 | bitbang->mdc_active_low = pdata->mdc_active_low; |
f004f3ea | 145 | bitbang->mdio = pdata->mdio; |
1d251481 | 146 | bitbang->mdio_active_low = pdata->mdio_active_low; |
f1d54c47 GR |
147 | bitbang->mdo = pdata->mdo; |
148 | bitbang->mdo_active_low = pdata->mdo_active_low; | |
a5edeccb LP |
149 | |
150 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); | |
151 | if (!new_bus) | |
78cdb079 | 152 | goto out; |
a5edeccb | 153 | |
f004f3ea | 154 | new_bus->name = "GPIO Bitbanged MDIO", |
a5edeccb | 155 | |
f004f3ea PZ |
156 | new_bus->phy_mask = pdata->phy_mask; |
157 | new_bus->irq = pdata->irqs; | |
158 | new_bus->parent = dev; | |
159 | ||
160 | if (new_bus->phy_mask == ~0) | |
a5edeccb LP |
161 | goto out_free_bus; |
162 | ||
163 | for (i = 0; i < PHY_MAX_ADDR; i++) | |
f004f3ea PZ |
164 | if (!new_bus->irq[i]) |
165 | new_bus->irq[i] = PHY_POLL; | |
a5edeccb | 166 | |
a77e929a | 167 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); |
f004f3ea | 168 | |
78cdb079 | 169 | if (devm_gpio_request(dev, bitbang->mdc, "mdc")) |
f004f3ea | 170 | goto out_free_bus; |
a5edeccb | 171 | |
78cdb079 GR |
172 | if (devm_gpio_request(dev, bitbang->mdio, "mdio")) |
173 | goto out_free_bus; | |
f004f3ea | 174 | |
f1d54c47 GR |
175 | if (bitbang->mdo) { |
176 | if (devm_gpio_request(dev, bitbang->mdo, "mdo")) | |
177 | goto out_free_bus; | |
178 | gpio_direction_output(bitbang->mdo, 1); | |
179 | gpio_direction_input(bitbang->mdio); | |
180 | } | |
181 | ||
664f93b4 PZ |
182 | gpio_direction_output(bitbang->mdc, 0); |
183 | ||
f004f3ea | 184 | dev_set_drvdata(dev, new_bus); |
a5edeccb | 185 | |
dacac4da | 186 | return new_bus; |
a5edeccb | 187 | |
a5edeccb | 188 | out_free_bus: |
a5edeccb LP |
189 | free_mdio_bitbang(new_bus); |
190 | out: | |
dacac4da | 191 | return NULL; |
a5edeccb LP |
192 | } |
193 | ||
f99b4a02 | 194 | static void mdio_gpio_bus_deinit(struct device *dev) |
a5edeccb | 195 | { |
f004f3ea | 196 | struct mii_bus *bus = dev_get_drvdata(dev); |
a5edeccb | 197 | |
dacac4da | 198 | free_mdio_bitbang(bus); |
f004f3ea PZ |
199 | } |
200 | ||
633d1594 | 201 | static void mdio_gpio_bus_destroy(struct device *dev) |
dacac4da MW |
202 | { |
203 | struct mii_bus *bus = dev_get_drvdata(dev); | |
204 | ||
205 | mdiobus_unregister(bus); | |
206 | mdio_gpio_bus_deinit(dev); | |
207 | } | |
208 | ||
633d1594 | 209 | static int mdio_gpio_probe(struct platform_device *pdev) |
f004f3ea | 210 | { |
e92bdf4b | 211 | struct mdio_gpio_platform_data *pdata; |
dacac4da | 212 | struct mii_bus *new_bus; |
3272dd9b | 213 | int ret, bus_id; |
f004f3ea | 214 | |
3272dd9b | 215 | if (pdev->dev.of_node) { |
e92bdf4b | 216 | pdata = mdio_gpio_of_get_data(pdev); |
3272dd9b SK |
217 | bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio"); |
218 | } else { | |
9bc7b1cd | 219 | pdata = dev_get_platdata(&pdev->dev); |
3272dd9b SK |
220 | bus_id = pdev->id; |
221 | } | |
e92bdf4b | 222 | |
f004f3ea PZ |
223 | if (!pdata) |
224 | return -ENODEV; | |
225 | ||
3272dd9b | 226 | new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id); |
dacac4da MW |
227 | if (!new_bus) |
228 | return -ENODEV; | |
229 | ||
e92bdf4b SK |
230 | if (pdev->dev.of_node) |
231 | ret = of_mdiobus_register(new_bus, pdev->dev.of_node); | |
232 | else | |
233 | ret = mdiobus_register(new_bus); | |
234 | ||
dacac4da MW |
235 | if (ret) |
236 | mdio_gpio_bus_deinit(&pdev->dev); | |
237 | ||
238 | return ret; | |
f004f3ea PZ |
239 | } |
240 | ||
633d1594 | 241 | static int mdio_gpio_remove(struct platform_device *pdev) |
f004f3ea PZ |
242 | { |
243 | mdio_gpio_bus_destroy(&pdev->dev); | |
244 | ||
245 | return 0; | |
246 | } | |
247 | ||
e92bdf4b SK |
248 | static struct of_device_id mdio_gpio_of_match[] = { |
249 | { .compatible = "virtual,mdio-gpio", }, | |
250 | { /* sentinel */ } | |
a5edeccb LP |
251 | }; |
252 | ||
f004f3ea PZ |
253 | static struct platform_driver mdio_gpio_driver = { |
254 | .probe = mdio_gpio_probe, | |
633d1594 | 255 | .remove = mdio_gpio_remove, |
f004f3ea PZ |
256 | .driver = { |
257 | .name = "mdio-gpio", | |
258 | .owner = THIS_MODULE, | |
e92bdf4b | 259 | .of_match_table = mdio_gpio_of_match, |
f004f3ea PZ |
260 | }, |
261 | }; | |
262 | ||
f8e5fc8c | 263 | module_platform_driver(mdio_gpio_driver); |
a5edeccb | 264 | |
f004f3ea PZ |
265 | MODULE_ALIAS("platform:mdio-gpio"); |
266 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); | |
267 | MODULE_LICENSE("GPL"); | |
268 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO"); |