Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame^] | 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/mdio-bitbang.h> |
| 28 | #include <linux/mdio-gpio.h> |
| 29 | #include <linux/gpio/consumer.h> |
| 30 | #include <linux/of_mdio.h> |
| 31 | |
| 32 | struct mdio_gpio_info { |
| 33 | struct mdiobb_ctrl ctrl; |
| 34 | struct gpio_desc *mdc, *mdio, *mdo; |
| 35 | }; |
| 36 | |
| 37 | static int mdio_gpio_get_data(struct device *dev, |
| 38 | struct mdio_gpio_info *bitbang) |
| 39 | { |
| 40 | bitbang->mdc = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDC, |
| 41 | GPIOD_OUT_LOW); |
| 42 | if (IS_ERR(bitbang->mdc)) |
| 43 | return PTR_ERR(bitbang->mdc); |
| 44 | |
| 45 | bitbang->mdio = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDIO, |
| 46 | GPIOD_IN); |
| 47 | if (IS_ERR(bitbang->mdio)) |
| 48 | return PTR_ERR(bitbang->mdio); |
| 49 | |
| 50 | bitbang->mdo = devm_gpiod_get_index_optional(dev, NULL, MDIO_GPIO_MDO, |
| 51 | GPIOD_OUT_LOW); |
| 52 | return PTR_ERR_OR_ZERO(bitbang->mdo); |
| 53 | } |
| 54 | |
| 55 | static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) |
| 56 | { |
| 57 | struct mdio_gpio_info *bitbang = |
| 58 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 59 | |
| 60 | if (bitbang->mdo) { |
| 61 | /* Separate output pin. Always set its value to high |
| 62 | * when changing direction. If direction is input, |
| 63 | * assume the pin serves as pull-up. If direction is |
| 64 | * output, the default value is high. |
| 65 | */ |
| 66 | gpiod_set_value_cansleep(bitbang->mdo, 1); |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (dir) |
| 71 | gpiod_direction_output(bitbang->mdio, 1); |
| 72 | else |
| 73 | gpiod_direction_input(bitbang->mdio); |
| 74 | } |
| 75 | |
| 76 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
| 77 | { |
| 78 | struct mdio_gpio_info *bitbang = |
| 79 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 80 | |
| 81 | return gpiod_get_value_cansleep(bitbang->mdio); |
| 82 | } |
| 83 | |
| 84 | static void mdio_set(struct mdiobb_ctrl *ctrl, int what) |
| 85 | { |
| 86 | struct mdio_gpio_info *bitbang = |
| 87 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 88 | |
| 89 | if (bitbang->mdo) |
| 90 | gpiod_set_value_cansleep(bitbang->mdo, what); |
| 91 | else |
| 92 | gpiod_set_value_cansleep(bitbang->mdio, what); |
| 93 | } |
| 94 | |
| 95 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
| 96 | { |
| 97 | struct mdio_gpio_info *bitbang = |
| 98 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 99 | |
| 100 | gpiod_set_value_cansleep(bitbang->mdc, what); |
| 101 | } |
| 102 | |
| 103 | static const struct mdiobb_ops mdio_gpio_ops = { |
| 104 | .owner = THIS_MODULE, |
| 105 | .set_mdc = mdc_set, |
| 106 | .set_mdio_dir = mdio_dir, |
| 107 | .set_mdio_data = mdio_set, |
| 108 | .get_mdio_data = mdio_get, |
| 109 | }; |
| 110 | |
| 111 | static struct mii_bus *mdio_gpio_bus_init(struct device *dev, |
| 112 | struct mdio_gpio_info *bitbang, |
| 113 | int bus_id) |
| 114 | { |
| 115 | struct mii_bus *new_bus; |
| 116 | |
| 117 | bitbang->ctrl.ops = &mdio_gpio_ops; |
| 118 | |
| 119 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); |
| 120 | if (!new_bus) |
| 121 | return NULL; |
| 122 | |
| 123 | new_bus->name = "GPIO Bitbanged MDIO"; |
| 124 | new_bus->parent = dev; |
| 125 | |
| 126 | if (bus_id != -1) |
| 127 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); |
| 128 | else |
| 129 | strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE); |
| 130 | |
| 131 | dev_set_drvdata(dev, new_bus); |
| 132 | |
| 133 | return new_bus; |
| 134 | } |
| 135 | |
| 136 | static void mdio_gpio_bus_deinit(struct device *dev) |
| 137 | { |
| 138 | struct mii_bus *bus = dev_get_drvdata(dev); |
| 139 | |
| 140 | free_mdio_bitbang(bus); |
| 141 | } |
| 142 | |
| 143 | static void mdio_gpio_bus_destroy(struct device *dev) |
| 144 | { |
| 145 | struct mii_bus *bus = dev_get_drvdata(dev); |
| 146 | |
| 147 | mdiobus_unregister(bus); |
| 148 | mdio_gpio_bus_deinit(dev); |
| 149 | } |
| 150 | |
| 151 | static int mdio_gpio_probe(struct platform_device *pdev) |
| 152 | { |
| 153 | struct mdio_gpio_info *bitbang; |
| 154 | struct mii_bus *new_bus; |
| 155 | int ret, bus_id; |
| 156 | |
| 157 | bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL); |
| 158 | if (!bitbang) |
| 159 | return -ENOMEM; |
| 160 | |
| 161 | ret = mdio_gpio_get_data(&pdev->dev, bitbang); |
| 162 | if (ret) |
| 163 | return ret; |
| 164 | |
| 165 | if (pdev->dev.of_node) { |
| 166 | bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio"); |
| 167 | if (bus_id < 0) { |
| 168 | dev_warn(&pdev->dev, "failed to get alias id\n"); |
| 169 | bus_id = 0; |
| 170 | } |
| 171 | } else { |
| 172 | bus_id = pdev->id; |
| 173 | } |
| 174 | |
| 175 | new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, bus_id); |
| 176 | if (!new_bus) |
| 177 | return -ENODEV; |
| 178 | |
| 179 | ret = of_mdiobus_register(new_bus, pdev->dev.of_node); |
| 180 | if (ret) |
| 181 | mdio_gpio_bus_deinit(&pdev->dev); |
| 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | static int mdio_gpio_remove(struct platform_device *pdev) |
| 187 | { |
| 188 | mdio_gpio_bus_destroy(&pdev->dev); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | static const struct of_device_id mdio_gpio_of_match[] = { |
| 194 | { .compatible = "virtual,mdio-gpio", }, |
| 195 | { /* sentinel */ } |
| 196 | }; |
| 197 | MODULE_DEVICE_TABLE(of, mdio_gpio_of_match); |
| 198 | |
| 199 | static struct platform_driver mdio_gpio_driver = { |
| 200 | .probe = mdio_gpio_probe, |
| 201 | .remove = mdio_gpio_remove, |
| 202 | .driver = { |
| 203 | .name = "mdio-gpio", |
| 204 | .of_match_table = mdio_gpio_of_match, |
| 205 | }, |
| 206 | }; |
| 207 | |
| 208 | module_platform_driver(mdio_gpio_driver); |
| 209 | |
| 210 | MODULE_ALIAS("platform:mdio-gpio"); |
| 211 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); |
| 212 | MODULE_LICENSE("GPL"); |
| 213 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO"); |