Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

at v4.11-rc5 280 lines 6.6 kB view raw
1/* 2 * GPIO based MDIO bitbang driver. 3 * Supports OpenFirmware. 4 * 5 * Copyright (c) 2008 CSE Semaphore Belgium. 6 * by Laurent Pinchart <laurentp@cse-semaphore.com> 7 * 8 * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt> 9 * 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/interrupt.h> 26#include <linux/platform_device.h> 27#include <linux/gpio.h> 28#include <linux/platform_data/mdio-gpio.h> 29 30#include <linux/of_gpio.h> 31#include <linux/of_mdio.h> 32 33struct mdio_gpio_info { 34 struct mdiobb_ctrl ctrl; 35 struct gpio_desc *mdc, *mdio, *mdo; 36}; 37 38static 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 enum of_gpio_flags flags; 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_flags(np, 0, &flags); 50 if (ret < 0) 51 return NULL; 52 53 pdata->mdc = ret; 54 pdata->mdc_active_low = flags & OF_GPIO_ACTIVE_LOW; 55 56 ret = of_get_gpio_flags(np, 1, &flags); 57 if (ret < 0) 58 return NULL; 59 pdata->mdio = ret; 60 pdata->mdio_active_low = flags & OF_GPIO_ACTIVE_LOW; 61 62 ret = of_get_gpio_flags(np, 2, &flags); 63 if (ret > 0) { 64 pdata->mdo = ret; 65 pdata->mdo_active_low = flags & OF_GPIO_ACTIVE_LOW; 66 } 67 68 return pdata; 69} 70 71static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) 72{ 73 struct mdio_gpio_info *bitbang = 74 container_of(ctrl, struct mdio_gpio_info, ctrl); 75 76 if (bitbang->mdo) { 77 /* Separate output pin. Always set its value to high 78 * when changing direction. If direction is input, 79 * assume the pin serves as pull-up. If direction is 80 * output, the default value is high. 81 */ 82 gpiod_set_value(bitbang->mdo, 1); 83 return; 84 } 85 86 if (dir) 87 gpiod_direction_output(bitbang->mdio, 1); 88 else 89 gpiod_direction_input(bitbang->mdio); 90} 91 92static int mdio_get(struct mdiobb_ctrl *ctrl) 93{ 94 struct mdio_gpio_info *bitbang = 95 container_of(ctrl, struct mdio_gpio_info, ctrl); 96 97 return gpiod_get_value(bitbang->mdio); 98} 99 100static void mdio_set(struct mdiobb_ctrl *ctrl, int what) 101{ 102 struct mdio_gpio_info *bitbang = 103 container_of(ctrl, struct mdio_gpio_info, ctrl); 104 105 if (bitbang->mdo) 106 gpiod_set_value(bitbang->mdo, what); 107 else 108 gpiod_set_value(bitbang->mdio, what); 109} 110 111static void mdc_set(struct mdiobb_ctrl *ctrl, int what) 112{ 113 struct mdio_gpio_info *bitbang = 114 container_of(ctrl, struct mdio_gpio_info, ctrl); 115 116 gpiod_set_value(bitbang->mdc, what); 117} 118 119static struct mdiobb_ops mdio_gpio_ops = { 120 .owner = THIS_MODULE, 121 .set_mdc = mdc_set, 122 .set_mdio_dir = mdio_dir, 123 .set_mdio_data = mdio_set, 124 .get_mdio_data = mdio_get, 125}; 126 127static struct mii_bus *mdio_gpio_bus_init(struct device *dev, 128 struct mdio_gpio_platform_data *pdata, 129 int bus_id) 130{ 131 struct mii_bus *new_bus; 132 struct mdio_gpio_info *bitbang; 133 int i; 134 int mdc, mdio, mdo; 135 unsigned long mdc_flags = GPIOF_OUT_INIT_LOW; 136 unsigned long mdio_flags = GPIOF_DIR_IN; 137 unsigned long mdo_flags = GPIOF_OUT_INIT_HIGH; 138 139 bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL); 140 if (!bitbang) 141 goto out; 142 143 bitbang->ctrl.ops = &mdio_gpio_ops; 144 bitbang->ctrl.reset = pdata->reset; 145 mdc = pdata->mdc; 146 bitbang->mdc = gpio_to_desc(mdc); 147 if (pdata->mdc_active_low) 148 mdc_flags = GPIOF_OUT_INIT_HIGH | GPIOF_ACTIVE_LOW; 149 mdio = pdata->mdio; 150 bitbang->mdio = gpio_to_desc(mdio); 151 if (pdata->mdio_active_low) 152 mdio_flags |= GPIOF_ACTIVE_LOW; 153 mdo = pdata->mdo; 154 if (mdo) { 155 bitbang->mdo = gpio_to_desc(mdo); 156 if (pdata->mdo_active_low) 157 mdo_flags = GPIOF_OUT_INIT_LOW | GPIOF_ACTIVE_LOW; 158 } 159 160 new_bus = alloc_mdio_bitbang(&bitbang->ctrl); 161 if (!new_bus) 162 goto out; 163 164 new_bus->name = "GPIO Bitbanged MDIO", 165 166 new_bus->phy_mask = pdata->phy_mask; 167 new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask; 168 memcpy(new_bus->irq, pdata->irqs, sizeof(new_bus->irq)); 169 new_bus->parent = dev; 170 171 if (new_bus->phy_mask == ~0) 172 goto out_free_bus; 173 174 for (i = 0; i < PHY_MAX_ADDR; i++) 175 if (!new_bus->irq[i]) 176 new_bus->irq[i] = PHY_POLL; 177 178 if (bus_id != -1) 179 snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); 180 else 181 strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE); 182 183 if (devm_gpio_request_one(dev, mdc, mdc_flags, "mdc")) 184 goto out_free_bus; 185 186 if (devm_gpio_request_one(dev, mdio, mdio_flags, "mdio")) 187 goto out_free_bus; 188 189 if (mdo && devm_gpio_request_one(dev, mdo, mdo_flags, "mdo")) 190 goto out_free_bus; 191 192 dev_set_drvdata(dev, new_bus); 193 194 return new_bus; 195 196out_free_bus: 197 free_mdio_bitbang(new_bus); 198out: 199 return NULL; 200} 201 202static void mdio_gpio_bus_deinit(struct device *dev) 203{ 204 struct mii_bus *bus = dev_get_drvdata(dev); 205 206 free_mdio_bitbang(bus); 207} 208 209static void mdio_gpio_bus_destroy(struct device *dev) 210{ 211 struct mii_bus *bus = dev_get_drvdata(dev); 212 213 mdiobus_unregister(bus); 214 mdio_gpio_bus_deinit(dev); 215} 216 217static int mdio_gpio_probe(struct platform_device *pdev) 218{ 219 struct mdio_gpio_platform_data *pdata; 220 struct mii_bus *new_bus; 221 int ret, bus_id; 222 223 if (pdev->dev.of_node) { 224 pdata = mdio_gpio_of_get_data(pdev); 225 bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio"); 226 if (bus_id < 0) { 227 dev_warn(&pdev->dev, "failed to get alias id\n"); 228 bus_id = 0; 229 } 230 } else { 231 pdata = dev_get_platdata(&pdev->dev); 232 bus_id = pdev->id; 233 } 234 235 if (!pdata) 236 return -ENODEV; 237 238 new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id); 239 if (!new_bus) 240 return -ENODEV; 241 242 if (pdev->dev.of_node) 243 ret = of_mdiobus_register(new_bus, pdev->dev.of_node); 244 else 245 ret = mdiobus_register(new_bus); 246 247 if (ret) 248 mdio_gpio_bus_deinit(&pdev->dev); 249 250 return ret; 251} 252 253static int mdio_gpio_remove(struct platform_device *pdev) 254{ 255 mdio_gpio_bus_destroy(&pdev->dev); 256 257 return 0; 258} 259 260static const struct of_device_id mdio_gpio_of_match[] = { 261 { .compatible = "virtual,mdio-gpio", }, 262 { /* sentinel */ } 263}; 264MODULE_DEVICE_TABLE(of, mdio_gpio_of_match); 265 266static struct platform_driver mdio_gpio_driver = { 267 .probe = mdio_gpio_probe, 268 .remove = mdio_gpio_remove, 269 .driver = { 270 .name = "mdio-gpio", 271 .of_match_table = mdio_gpio_of_match, 272 }, 273}; 274 275module_platform_driver(mdio_gpio_driver); 276 277MODULE_ALIAS("platform:mdio-gpio"); 278MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); 279MODULE_LICENSE("GPL"); 280MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");