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> |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 33 | #include <linux/of_mdio.h> |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 34 | #include <linux/of_platform.h> |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 35 | #endif |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 36 | |
| 37 | struct mdio_gpio_info { |
| 38 | struct mdiobb_ctrl ctrl; |
| 39 | int mdc, mdio; |
| 40 | }; |
| 41 | |
| 42 | static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) |
| 43 | { |
| 44 | struct mdio_gpio_info *bitbang = |
| 45 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 46 | |
| 47 | if (dir) |
| 48 | gpio_direction_output(bitbang->mdio, 1); |
| 49 | else |
| 50 | gpio_direction_input(bitbang->mdio); |
| 51 | } |
| 52 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 53 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 54 | { |
| 55 | struct mdio_gpio_info *bitbang = |
| 56 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 57 | |
| 58 | return gpio_get_value(bitbang->mdio); |
| 59 | } |
| 60 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 61 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 62 | { |
| 63 | struct mdio_gpio_info *bitbang = |
| 64 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 65 | |
| 66 | gpio_set_value(bitbang->mdio, what); |
| 67 | } |
| 68 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 69 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 70 | { |
| 71 | struct mdio_gpio_info *bitbang = |
| 72 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 73 | |
| 74 | gpio_set_value(bitbang->mdc, what); |
| 75 | } |
| 76 | |
| 77 | static struct mdiobb_ops mdio_gpio_ops = { |
| 78 | .owner = THIS_MODULE, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 79 | .set_mdc = mdc_set, |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 80 | .set_mdio_dir = mdio_dir, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 81 | .set_mdio_data = mdio_set, |
| 82 | .get_mdio_data = mdio_get, |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 83 | }; |
| 84 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 85 | static struct mii_bus * __devinit mdio_gpio_bus_init(struct device *dev, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 86 | struct mdio_gpio_platform_data *pdata, |
| 87 | int bus_id) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 88 | { |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 89 | struct mii_bus *new_bus; |
| 90 | struct mdio_gpio_info *bitbang; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 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 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 107 | new_bus->phy_mask = pdata->phy_mask; |
| 108 | new_bus->irq = pdata->irqs; |
| 109 | new_bus->parent = dev; |
| 110 | |
| 111 | if (new_bus->phy_mask == ~0) |
| 112 | goto out_free_bus; |
| 113 | |
| 114 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 115 | if (!new_bus->irq[i]) |
| 116 | new_bus->irq[i] = PHY_POLL; |
| 117 | |
| 118 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "%x", bus_id); |
| 119 | |
| 120 | if (gpio_request(bitbang->mdc, "mdc")) |
| 121 | goto out_free_bus; |
| 122 | |
| 123 | if (gpio_request(bitbang->mdio, "mdio")) |
| 124 | goto out_free_mdc; |
| 125 | |
Paulius Zaleckas | 664f93b | 2009-02-08 23:46:01 +0000 | [diff] [blame] | 126 | gpio_direction_output(bitbang->mdc, 0); |
| 127 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 128 | dev_set_drvdata(dev, new_bus); |
| 129 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 130 | return new_bus; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 131 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 132 | out_free_mdc: |
| 133 | gpio_free(bitbang->mdc); |
| 134 | out_free_bus: |
| 135 | free_mdio_bitbang(new_bus); |
| 136 | out_free_bitbang: |
| 137 | kfree(bitbang); |
| 138 | out: |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 139 | return NULL; |
| 140 | } |
| 141 | |
Stephen Rothwell | f99b4a0 | 2009-11-16 22:47:33 +0000 | [diff] [blame] | 142 | static void mdio_gpio_bus_deinit(struct device *dev) |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 143 | { |
| 144 | struct mii_bus *bus = dev_get_drvdata(dev); |
| 145 | struct mdio_gpio_info *bitbang = bus->priv; |
| 146 | |
| 147 | dev_set_drvdata(dev, NULL); |
| 148 | gpio_free(bitbang->mdio); |
| 149 | gpio_free(bitbang->mdc); |
| 150 | free_mdio_bitbang(bus); |
| 151 | kfree(bitbang); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | static void __devexit mdio_gpio_bus_destroy(struct device *dev) |
| 155 | { |
| 156 | struct mii_bus *bus = dev_get_drvdata(dev); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 157 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 158 | mdiobus_unregister(bus); |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 159 | mdio_gpio_bus_deinit(dev); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 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; |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 165 | struct mii_bus *new_bus; |
| 166 | int ret; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 167 | |
| 168 | if (!pdata) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 169 | return -ENODEV; |
| 170 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 171 | new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, pdev->id); |
| 172 | if (!new_bus) |
| 173 | return -ENODEV; |
| 174 | |
| 175 | ret = mdiobus_register(new_bus); |
| 176 | if (ret) |
| 177 | mdio_gpio_bus_deinit(&pdev->dev); |
| 178 | |
| 179 | return ret; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static int __devexit mdio_gpio_remove(struct platform_device *pdev) |
| 183 | { |
| 184 | mdio_gpio_bus_destroy(&pdev->dev); |
| 185 | |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 186 | return 0; |
| 187 | } |
| 188 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 189 | #ifdef CONFIG_OF_GPIO |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 190 | |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 191 | static int __devinit mdio_ofgpio_probe(struct platform_device *ofdev) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 192 | { |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 193 | struct mdio_gpio_platform_data *pdata; |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 194 | struct mii_bus *new_bus; |
Roel Kluin | 57a5749 | 2009-01-19 17:14:21 -0800 | [diff] [blame] | 195 | int ret; |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 196 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 197 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 198 | if (!pdata) |
| 199 | return -ENOMEM; |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 200 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 201 | ret = of_get_gpio(ofdev->dev.of_node, 0); |
Roel Kluin | 57a5749 | 2009-01-19 17:14:21 -0800 | [diff] [blame] | 202 | if (ret < 0) |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 203 | goto out_free; |
Roel Kluin | 57a5749 | 2009-01-19 17:14:21 -0800 | [diff] [blame] | 204 | pdata->mdc = ret; |
| 205 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 206 | ret = of_get_gpio(ofdev->dev.of_node, 1); |
Roel Kluin | 57a5749 | 2009-01-19 17:14:21 -0800 | [diff] [blame] | 207 | if (ret < 0) |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 208 | goto out_free; |
Roel Kluin | 57a5749 | 2009-01-19 17:14:21 -0800 | [diff] [blame] | 209 | pdata->mdio = ret; |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 210 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 211 | new_bus = mdio_gpio_bus_init(&ofdev->dev, pdata, pdata->mdc); |
| 212 | if (!new_bus) |
Julia Lawall | a4b1164 | 2009-09-11 06:22:09 +0000 | [diff] [blame] | 213 | goto out_free; |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 214 | |
Grant Likely | 61c7a08 | 2010-04-13 16:12:29 -0700 | [diff] [blame] | 215 | ret = of_mdiobus_register(new_bus, ofdev->dev.of_node); |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 216 | if (ret) |
| 217 | mdio_gpio_bus_deinit(&ofdev->dev); |
| 218 | |
| 219 | return ret; |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 220 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 221 | out_free: |
| 222 | kfree(pdata); |
| 223 | return -ENODEV; |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 224 | } |
| 225 | |
Grant Likely | 2dc1158 | 2010-08-06 09:25:50 -0600 | [diff] [blame] | 226 | static int __devexit mdio_ofgpio_remove(struct platform_device *ofdev) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 227 | { |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 228 | mdio_gpio_bus_destroy(&ofdev->dev); |
| 229 | kfree(ofdev->dev.platform_data); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 230 | |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | static struct of_device_id mdio_ofgpio_match[] = { |
| 235 | { |
| 236 | .compatible = "virtual,mdio-gpio", |
| 237 | }, |
| 238 | {}, |
| 239 | }; |
Anton Vorontsov | e72701a | 2009-10-14 14:54:52 -0700 | [diff] [blame] | 240 | MODULE_DEVICE_TABLE(of, mdio_ofgpio_match); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 241 | |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 242 | static struct platform_driver mdio_ofgpio_driver = { |
Grant Likely | 4018294 | 2010-04-13 16:13:02 -0700 | [diff] [blame] | 243 | .driver = { |
| 244 | .name = "mdio-gpio", |
| 245 | .owner = THIS_MODULE, |
| 246 | .of_match_table = mdio_ofgpio_match, |
| 247 | }, |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 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 | { |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 254 | return platform_driver_register(&mdio_ofgpio_driver); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 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 | { |
Grant Likely | 7488876 | 2011-02-22 21:05:51 -0700 | [diff] [blame] | 259 | platform_driver_unregister(&mdio_ofgpio_driver); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 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"); |