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