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 | |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 31 | #include <linux/of_gpio.h> |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 32 | #include <linux/of_mdio.h> |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 33 | |
| 34 | struct mdio_gpio_info { |
| 35 | struct mdiobb_ctrl ctrl; |
| 36 | int mdc, mdio; |
| 37 | }; |
| 38 | |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 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; |
| 43 | int ret; |
| 44 | |
| 45 | pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL); |
| 46 | if (!pdata) |
| 47 | return NULL; |
| 48 | |
| 49 | ret = of_get_gpio(np, 0); |
| 50 | if (ret < 0) |
| 51 | return NULL; |
| 52 | |
| 53 | pdata->mdc = ret; |
| 54 | |
| 55 | ret = of_get_gpio(np, 1); |
| 56 | if (ret < 0) |
| 57 | return NULL; |
| 58 | pdata->mdio = ret; |
| 59 | |
| 60 | return pdata; |
| 61 | } |
| 62 | |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 63 | static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) |
| 64 | { |
| 65 | struct mdio_gpio_info *bitbang = |
| 66 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 67 | |
| 68 | if (dir) |
| 69 | gpio_direction_output(bitbang->mdio, 1); |
| 70 | else |
| 71 | gpio_direction_input(bitbang->mdio); |
| 72 | } |
| 73 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 74 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 75 | { |
| 76 | struct mdio_gpio_info *bitbang = |
| 77 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 78 | |
| 79 | return gpio_get_value(bitbang->mdio); |
| 80 | } |
| 81 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 82 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 83 | { |
| 84 | struct mdio_gpio_info *bitbang = |
| 85 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 86 | |
| 87 | gpio_set_value(bitbang->mdio, what); |
| 88 | } |
| 89 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 90 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 91 | { |
| 92 | struct mdio_gpio_info *bitbang = |
| 93 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 94 | |
| 95 | gpio_set_value(bitbang->mdc, what); |
| 96 | } |
| 97 | |
| 98 | static struct mdiobb_ops mdio_gpio_ops = { |
| 99 | .owner = THIS_MODULE, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 100 | .set_mdc = mdc_set, |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 101 | .set_mdio_dir = mdio_dir, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 102 | .set_mdio_data = mdio_set, |
| 103 | .get_mdio_data = mdio_get, |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 104 | }; |
| 105 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 106 | static struct mii_bus *mdio_gpio_bus_init(struct device *dev, |
Greg Kroah-Hartman | 1dd06ae | 2012-12-06 14:30:56 +0000 | [diff] [blame] | 107 | struct mdio_gpio_platform_data *pdata, |
| 108 | int bus_id) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 109 | { |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 110 | struct mii_bus *new_bus; |
| 111 | struct mdio_gpio_info *bitbang; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 112 | int i; |
| 113 | |
| 114 | bitbang = kzalloc(sizeof(*bitbang), GFP_KERNEL); |
| 115 | if (!bitbang) |
| 116 | goto out; |
| 117 | |
| 118 | bitbang->ctrl.ops = &mdio_gpio_ops; |
Srinivas Kandagatla | 6488270 | 2011-11-15 11:54:15 +0000 | [diff] [blame] | 119 | bitbang->ctrl.reset = pdata->reset; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 120 | bitbang->mdc = pdata->mdc; |
| 121 | bitbang->mdio = pdata->mdio; |
| 122 | |
| 123 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); |
| 124 | if (!new_bus) |
| 125 | goto out_free_bitbang; |
| 126 | |
| 127 | new_bus->name = "GPIO Bitbanged MDIO", |
| 128 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 129 | new_bus->phy_mask = pdata->phy_mask; |
| 130 | new_bus->irq = pdata->irqs; |
| 131 | new_bus->parent = dev; |
| 132 | |
| 133 | if (new_bus->phy_mask == ~0) |
| 134 | goto out_free_bus; |
| 135 | |
| 136 | for (i = 0; i < PHY_MAX_ADDR; i++) |
| 137 | if (!new_bus->irq[i]) |
| 138 | new_bus->irq[i] = PHY_POLL; |
| 139 | |
Florian Fainelli | a77e929 | 2012-01-09 23:59:26 +0000 | [diff] [blame] | 140 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 141 | |
| 142 | if (gpio_request(bitbang->mdc, "mdc")) |
| 143 | goto out_free_bus; |
| 144 | |
| 145 | if (gpio_request(bitbang->mdio, "mdio")) |
| 146 | goto out_free_mdc; |
| 147 | |
Paulius Zaleckas | 664f93b | 2009-02-08 23:46:01 +0000 | [diff] [blame] | 148 | gpio_direction_output(bitbang->mdc, 0); |
| 149 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 150 | dev_set_drvdata(dev, new_bus); |
| 151 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 152 | return new_bus; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 153 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 154 | out_free_mdc: |
| 155 | gpio_free(bitbang->mdc); |
| 156 | out_free_bus: |
| 157 | free_mdio_bitbang(new_bus); |
| 158 | out_free_bitbang: |
| 159 | kfree(bitbang); |
| 160 | out: |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
| 163 | |
Stephen Rothwell | f99b4a0 | 2009-11-16 22:47:33 +0000 | [diff] [blame] | 164 | static void mdio_gpio_bus_deinit(struct device *dev) |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 165 | { |
| 166 | struct mii_bus *bus = dev_get_drvdata(dev); |
| 167 | struct mdio_gpio_info *bitbang = bus->priv; |
| 168 | |
| 169 | dev_set_drvdata(dev, NULL); |
| 170 | gpio_free(bitbang->mdio); |
| 171 | gpio_free(bitbang->mdc); |
| 172 | free_mdio_bitbang(bus); |
| 173 | kfree(bitbang); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 176 | static void mdio_gpio_bus_destroy(struct device *dev) |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 177 | { |
| 178 | struct mii_bus *bus = dev_get_drvdata(dev); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 179 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 180 | mdiobus_unregister(bus); |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 181 | mdio_gpio_bus_deinit(dev); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 182 | } |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 183 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 184 | static int mdio_gpio_probe(struct platform_device *pdev) |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 185 | { |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 186 | struct mdio_gpio_platform_data *pdata; |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 187 | struct mii_bus *new_bus; |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 188 | int ret, bus_id; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 189 | |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 190 | if (pdev->dev.of_node) { |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 191 | pdata = mdio_gpio_of_get_data(pdev); |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 192 | bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio"); |
| 193 | } else { |
Jingoo Han | 9bc7b1c | 2013-08-30 14:08:55 +0900 | [diff] [blame] | 194 | pdata = dev_get_platdata(&pdev->dev); |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 195 | bus_id = pdev->id; |
| 196 | } |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 197 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 198 | if (!pdata) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 199 | return -ENODEV; |
| 200 | |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 201 | new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id); |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 202 | if (!new_bus) |
| 203 | return -ENODEV; |
| 204 | |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 205 | if (pdev->dev.of_node) |
| 206 | ret = of_mdiobus_register(new_bus, pdev->dev.of_node); |
| 207 | else |
| 208 | ret = mdiobus_register(new_bus); |
| 209 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 210 | if (ret) |
| 211 | mdio_gpio_bus_deinit(&pdev->dev); |
| 212 | |
| 213 | return ret; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 216 | static int mdio_gpio_remove(struct platform_device *pdev) |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 217 | { |
| 218 | mdio_gpio_bus_destroy(&pdev->dev); |
| 219 | |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 220 | return 0; |
| 221 | } |
| 222 | |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 223 | static struct of_device_id mdio_gpio_of_match[] = { |
| 224 | { .compatible = "virtual,mdio-gpio", }, |
| 225 | { /* sentinel */ } |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 226 | }; |
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 | static struct platform_driver mdio_gpio_driver = { |
| 229 | .probe = mdio_gpio_probe, |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 230 | .remove = mdio_gpio_remove, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 231 | .driver = { |
| 232 | .name = "mdio-gpio", |
| 233 | .owner = THIS_MODULE, |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 234 | .of_match_table = mdio_gpio_of_match, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 235 | }, |
| 236 | }; |
| 237 | |
Sachin Kamat | f8e5fc8 | 2013-03-20 01:41:31 +0000 | [diff] [blame] | 238 | module_platform_driver(mdio_gpio_driver); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 239 | |
| 240 | MODULE_ALIAS("platform:mdio-gpio"); |
| 241 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); |
| 242 | MODULE_LICENSE("GPL"); |
| 243 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO"); |