| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 1 | /* | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 2 | * GPIO based MDIO bitbang driver. | 
|  | 3 | * Supports OpenFirmware. | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 4 | * | 
|  | 5 | * Copyright (c) 2008 CSE Semaphore Belgium. | 
|  | 6 | *  by Laurent Pinchart <laurentp@cse-semaphore.com> | 
|  | 7 | * | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 8 | * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt> | 
|  | 9 | * | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 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> | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 27 | #include <linux/platform_device.h> | 
|  | 28 | #include <linux/gpio.h> | 
|  | 29 | #include <linux/mdio-gpio.h> | 
|  | 30 |  | 
|  | 31 | #ifdef CONFIG_OF_GPIO | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 32 | #include <linux/of_gpio.h> | 
|  | 33 | #include <linux/of_platform.h> | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 34 | #endif | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 35 |  | 
|  | 36 | struct mdio_gpio_info { | 
|  | 37 | struct mdiobb_ctrl ctrl; | 
|  | 38 | int mdc, mdio; | 
|  | 39 | }; | 
|  | 40 |  | 
|  | 41 | static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) | 
|  | 42 | { | 
|  | 43 | struct mdio_gpio_info *bitbang = | 
|  | 44 | container_of(ctrl, struct mdio_gpio_info, ctrl); | 
|  | 45 |  | 
|  | 46 | if (dir) | 
|  | 47 | gpio_direction_output(bitbang->mdio, 1); | 
|  | 48 | else | 
|  | 49 | gpio_direction_input(bitbang->mdio); | 
|  | 50 | } | 
|  | 51 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 52 | static int mdio_get(struct mdiobb_ctrl *ctrl) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 53 | { | 
|  | 54 | struct mdio_gpio_info *bitbang = | 
|  | 55 | container_of(ctrl, struct mdio_gpio_info, ctrl); | 
|  | 56 |  | 
|  | 57 | return gpio_get_value(bitbang->mdio); | 
|  | 58 | } | 
|  | 59 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 60 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 61 | { | 
|  | 62 | struct mdio_gpio_info *bitbang = | 
|  | 63 | container_of(ctrl, struct mdio_gpio_info, ctrl); | 
|  | 64 |  | 
|  | 65 | gpio_set_value(bitbang->mdio, what); | 
|  | 66 | } | 
|  | 67 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 68 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 69 | { | 
|  | 70 | struct mdio_gpio_info *bitbang = | 
|  | 71 | container_of(ctrl, struct mdio_gpio_info, ctrl); | 
|  | 72 |  | 
|  | 73 | gpio_set_value(bitbang->mdc, what); | 
|  | 74 | } | 
|  | 75 |  | 
|  | 76 | static struct mdiobb_ops mdio_gpio_ops = { | 
|  | 77 | .owner = THIS_MODULE, | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 78 | .set_mdc = mdc_set, | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 79 | .set_mdio_dir = mdio_dir, | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 80 | .set_mdio_data = mdio_set, | 
|  | 81 | .get_mdio_data = mdio_get, | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 82 | }; | 
|  | 83 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 84 | static int __devinit mdio_gpio_bus_init(struct device *dev, | 
|  | 85 | struct mdio_gpio_platform_data *pdata, | 
|  | 86 | int bus_id) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 87 | { | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 88 | struct mii_bus *new_bus; | 
|  | 89 | struct mdio_gpio_info *bitbang; | 
|  | 90 | int ret = -ENOMEM; | 
|  | 91 | int i; | 
|  | 92 |  | 
|  | 93 | bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL); | 
|  | 94 | if (!bitbang) | 
|  | 95 | goto out; | 
|  | 96 |  | 
|  | 97 | bitbang->ctrl.ops = &mdio_gpio_ops; | 
|  | 98 | bitbang->mdc = pdata->mdc; | 
|  | 99 | bitbang->mdio = pdata->mdio; | 
|  | 100 |  | 
|  | 101 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); | 
|  | 102 | if (!new_bus) | 
|  | 103 | goto out_free_bitbang; | 
|  | 104 |  | 
|  | 105 | new_bus->name = "GPIO Bitbanged MDIO", | 
|  | 106 |  | 
|  | 107 | ret = -ENODEV; | 
|  | 108 |  | 
|  | 109 | new_bus->phy_mask = pdata->phy_mask; | 
|  | 110 | new_bus->irq = pdata->irqs; | 
|  | 111 | new_bus->parent = dev; | 
|  | 112 |  | 
|  | 113 | if (new_bus->phy_mask == ~0) | 
|  | 114 | goto out_free_bus; | 
|  | 115 |  | 
|  | 116 | for (i = 0; i < PHY_MAX_ADDR; i++) | 
|  | 117 | if (!new_bus->irq[i]) | 
|  | 118 | new_bus->irq[i] = PHY_POLL; | 
|  | 119 |  | 
|  | 120 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id); | 
|  | 121 |  | 
|  | 122 | if (gpio_request(bitbang->mdc, "mdc")) | 
|  | 123 | goto out_free_bus; | 
|  | 124 |  | 
|  | 125 | if (gpio_request(bitbang->mdio, "mdio")) | 
|  | 126 | goto out_free_mdc; | 
|  | 127 |  | 
|  | 128 | dev_set_drvdata(dev, new_bus); | 
|  | 129 |  | 
|  | 130 | ret = mdiobus_register(new_bus); | 
|  | 131 | if (ret) | 
|  | 132 | goto out_free_all; | 
|  | 133 |  | 
|  | 134 | return 0; | 
|  | 135 |  | 
|  | 136 | out_free_all: | 
|  | 137 | dev_set_drvdata(dev, NULL); | 
|  | 138 | gpio_free(bitbang->mdio); | 
|  | 139 | out_free_mdc: | 
|  | 140 | gpio_free(bitbang->mdc); | 
|  | 141 | out_free_bus: | 
|  | 142 | free_mdio_bitbang(new_bus); | 
|  | 143 | out_free_bitbang: | 
|  | 144 | kfree(bitbang); | 
|  | 145 | out: | 
|  | 146 | return ret; | 
|  | 147 | } | 
|  | 148 |  | 
|  | 149 | static void __devexit mdio_gpio_bus_destroy(struct device *dev) | 
|  | 150 | { | 
|  | 151 | struct mii_bus *bus = dev_get_drvdata(dev); | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 152 | struct mdio_gpio_info *bitbang = bus->priv; | 
|  | 153 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 154 | mdiobus_unregister(bus); | 
|  | 155 | free_mdio_bitbang(bus); | 
|  | 156 | dev_set_drvdata(dev, NULL); | 
|  | 157 | gpio_free(bitbang->mdc); | 
|  | 158 | gpio_free(bitbang->mdio); | 
|  | 159 | kfree(bitbang); | 
|  | 160 | } | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 161 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 162 | static int __devinit mdio_gpio_probe(struct platform_device *pdev) | 
|  | 163 | { | 
|  | 164 | struct mdio_gpio_platform_data *pdata = pdev->dev.platform_data; | 
|  | 165 |  | 
|  | 166 | if (!pdata) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 167 | return -ENODEV; | 
|  | 168 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 169 | return mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id); | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | static int __devexit mdio_gpio_remove(struct platform_device *pdev) | 
|  | 173 | { | 
|  | 174 | mdio_gpio_bus_destroy(&pdev->dev); | 
|  | 175 |  | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 176 | return 0; | 
|  | 177 | } | 
|  | 178 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 179 | #ifdef CONFIG_OF_GPIO | 
|  | 180 | static void __devinit add_phy(struct mdio_gpio_platform_data *pdata, | 
|  | 181 | struct device_node *np) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 182 | { | 
|  | 183 | const u32 *data; | 
|  | 184 | int len, id, irq; | 
|  | 185 |  | 
|  | 186 | data = of_get_property(np, "reg", &len); | 
|  | 187 | if (!data || len != 4) | 
|  | 188 | return; | 
|  | 189 |  | 
|  | 190 | id = *data; | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 191 | pdata->phy_mask &= ~(1 << id); | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 192 |  | 
|  | 193 | irq = of_irq_to_resource(np, 0, NULL); | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 194 | if (irq) | 
|  | 195 | pdata->irqs[id] = irq; | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 196 | } | 
|  | 197 |  | 
|  | 198 | static int __devinit mdio_ofgpio_probe(struct of_device *ofdev, | 
|  | 199 | const struct of_device_id *match) | 
|  | 200 | { | 
|  | 201 | struct device_node *np = NULL; | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 202 | struct mdio_gpio_platform_data *pdata; | 
| Roel Kluin | 57a5749 | 2009-01-19 17:14:21 -0800 | [diff] [blame] | 203 | int ret; | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 204 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 205 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); | 
|  | 206 | if (!pdata) | 
|  | 207 | return -ENOMEM; | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 208 |  | 
| Roel Kluin | 57a5749 | 2009-01-19 17:14:21 -0800 | [diff] [blame] | 209 | ret = of_get_gpio(ofdev->node, 0); | 
|  | 210 | if (ret < 0) | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 211 | goto out_free; | 
| Roel Kluin | 57a5749 | 2009-01-19 17:14:21 -0800 | [diff] [blame] | 212 | pdata->mdc = ret; | 
|  | 213 |  | 
|  | 214 | ret = of_get_gpio(ofdev->node, 1); | 
|  | 215 | if (ret < 0) | 
|  | 216 | goto out_free; | 
|  | 217 | pdata->mdio = ret; | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 218 |  | 
|  | 219 | while ((np = of_get_next_child(ofdev->node, np))) | 
|  | 220 | if (!strcmp(np->type, "ethernet-phy")) | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 221 | add_phy(pdata, np); | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 222 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 223 | return mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc); | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 224 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 225 | out_free: | 
|  | 226 | kfree(pdata); | 
|  | 227 | return -ENODEV; | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 228 | } | 
|  | 229 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 230 | static int __devexit mdio_ofgpio_remove(struct of_device *ofdev) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 231 | { | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 232 | mdio_gpio_bus_destroy(&ofdev->dev); | 
|  | 233 | kfree(ofdev->dev.platform_data); | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 234 |  | 
|  | 235 | return 0; | 
|  | 236 | } | 
|  | 237 |  | 
|  | 238 | static struct of_device_id mdio_ofgpio_match[] = { | 
|  | 239 | { | 
|  | 240 | .compatible = "virtual,mdio-gpio", | 
|  | 241 | }, | 
|  | 242 | {}, | 
|  | 243 | }; | 
|  | 244 |  | 
|  | 245 | static struct of_platform_driver mdio_ofgpio_driver = { | 
|  | 246 | .name = "mdio-gpio", | 
|  | 247 | .match_table = mdio_ofgpio_match, | 
|  | 248 | .probe = mdio_ofgpio_probe, | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 249 | .remove = __devexit_p(mdio_ofgpio_remove), | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 250 | }; | 
|  | 251 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 252 | static inline int __init mdio_ofgpio_init(void) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 253 | { | 
|  | 254 | return of_register_platform_driver(&mdio_ofgpio_driver); | 
|  | 255 | } | 
|  | 256 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 257 | static inline void __exit mdio_ofgpio_exit(void) | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 258 | { | 
|  | 259 | of_unregister_platform_driver(&mdio_ofgpio_driver); | 
|  | 260 | } | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 261 | #else | 
|  | 262 | static inline int __init mdio_ofgpio_init(void) { return 0; } | 
|  | 263 | static inline void __exit mdio_ofgpio_exit(void) { } | 
|  | 264 | #endif /* CONFIG_OF_GPIO */ | 
| Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 265 |  | 
| Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 266 | static struct platform_driver mdio_gpio_driver = { | 
|  | 267 | .probe = mdio_gpio_probe, | 
|  | 268 | .remove = __devexit_p(mdio_gpio_remove), | 
|  | 269 | .driver		= { | 
|  | 270 | .name	= "mdio-gpio", | 
|  | 271 | .owner	= THIS_MODULE, | 
|  | 272 | }, | 
|  | 273 | }; | 
|  | 274 |  | 
|  | 275 | static int __init mdio_gpio_init(void) | 
|  | 276 | { | 
|  | 277 | int ret; | 
|  | 278 |  | 
|  | 279 | ret = mdio_ofgpio_init(); | 
|  | 280 | if (ret) | 
|  | 281 | return ret; | 
|  | 282 |  | 
|  | 283 | ret = platform_driver_register(&mdio_gpio_driver); | 
|  | 284 | if (ret) | 
|  | 285 | mdio_ofgpio_exit(); | 
|  | 286 |  | 
|  | 287 | return ret; | 
|  | 288 | } | 
|  | 289 | module_init(mdio_gpio_init); | 
|  | 290 |  | 
|  | 291 | static void __exit mdio_gpio_exit(void) | 
|  | 292 | { | 
|  | 293 | platform_driver_unregister(&mdio_gpio_driver); | 
|  | 294 | mdio_ofgpio_exit(); | 
|  | 295 | } | 
|  | 296 | module_exit(mdio_gpio_exit); | 
|  | 297 |  | 
|  | 298 | MODULE_ALIAS("platform:mdio-gpio"); | 
|  | 299 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); | 
|  | 300 | MODULE_LICENSE("GPL"); | 
|  | 301 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO"); |