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