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> |
Vivien Didelot | e2aacd9 | 2015-10-20 10:08:59 -0400 | [diff] [blame] | 28 | #include <linux/platform_data/mdio-gpio.h> |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 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; |
Guenter Roeck | 7e5fbd1 | 2017-01-11 12:59:50 -0800 | [diff] [blame] | 35 | struct gpio_desc *mdc, *mdio, *mdo; |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 36 | }; |
| 37 | |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 38 | static void *mdio_gpio_of_get_data(struct device *dev) |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 39 | { |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 40 | struct mdio_gpio_platform_data *pdata; |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 41 | |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 42 | pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL); |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 43 | if (!pdata) |
| 44 | return NULL; |
| 45 | |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 46 | pdata->mdc = devm_gpiod_get_index(dev, NULL, 0, GPIOD_OUT_LOW); |
| 47 | if (IS_ERR(pdata->mdc)) |
| 48 | return ERR_CAST(pdata->mdc); |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 49 | |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 50 | pdata->mdio = devm_gpiod_get_index(dev, NULL, 1, GPIOD_IN); |
| 51 | if (IS_ERR(pdata->mdio)) |
| 52 | return ERR_CAST(pdata->mdio); |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 53 | |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 54 | pdata->mdo = devm_gpiod_get_index_optional(dev, NULL, 2, GPIOD_OUT_LOW); |
| 55 | if (IS_ERR(pdata->mdo)) |
| 56 | return ERR_CAST(pdata->mdo); |
Guenter Roeck | f1d54c4 | 2014-04-15 19:16:42 -0700 | [diff] [blame] | 57 | |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 58 | return pdata; |
| 59 | } |
| 60 | |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 61 | static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir) |
| 62 | { |
| 63 | struct mdio_gpio_info *bitbang = |
| 64 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 65 | |
Guenter Roeck | f1d54c4 | 2014-04-15 19:16:42 -0700 | [diff] [blame] | 66 | if (bitbang->mdo) { |
| 67 | /* Separate output pin. Always set its value to high |
| 68 | * when changing direction. If direction is input, |
| 69 | * assume the pin serves as pull-up. If direction is |
| 70 | * output, the default value is high. |
| 71 | */ |
Guenter Roeck | 52aab18 | 2017-01-11 12:59:51 -0800 | [diff] [blame] | 72 | gpiod_set_value(bitbang->mdo, 1); |
Guenter Roeck | f1d54c4 | 2014-04-15 19:16:42 -0700 | [diff] [blame] | 73 | return; |
| 74 | } |
| 75 | |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 76 | if (dir) |
Guenter Roeck | 52aab18 | 2017-01-11 12:59:51 -0800 | [diff] [blame] | 77 | gpiod_direction_output(bitbang->mdio, 1); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 78 | else |
Guenter Roeck | 7e5fbd1 | 2017-01-11 12:59:50 -0800 | [diff] [blame] | 79 | gpiod_direction_input(bitbang->mdio); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 80 | } |
| 81 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 82 | static int mdio_get(struct mdiobb_ctrl *ctrl) |
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 | |
Guenter Roeck | 52aab18 | 2017-01-11 12:59:51 -0800 | [diff] [blame] | 87 | return gpiod_get_value(bitbang->mdio); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 88 | } |
| 89 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 90 | static void mdio_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 | |
Guenter Roeck | f1d54c4 | 2014-04-15 19:16:42 -0700 | [diff] [blame] | 95 | if (bitbang->mdo) |
Guenter Roeck | 52aab18 | 2017-01-11 12:59:51 -0800 | [diff] [blame] | 96 | gpiod_set_value(bitbang->mdo, what); |
Guenter Roeck | f1d54c4 | 2014-04-15 19:16:42 -0700 | [diff] [blame] | 97 | else |
Guenter Roeck | 52aab18 | 2017-01-11 12:59:51 -0800 | [diff] [blame] | 98 | gpiod_set_value(bitbang->mdio, what); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 101 | static void mdc_set(struct mdiobb_ctrl *ctrl, int what) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 102 | { |
| 103 | struct mdio_gpio_info *bitbang = |
| 104 | container_of(ctrl, struct mdio_gpio_info, ctrl); |
| 105 | |
Guenter Roeck | 52aab18 | 2017-01-11 12:59:51 -0800 | [diff] [blame] | 106 | gpiod_set_value(bitbang->mdc, what); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Bhumika Goyal | 41a130f | 2017-08-22 13:43:29 +0530 | [diff] [blame] | 109 | static const struct mdiobb_ops mdio_gpio_ops = { |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 110 | .owner = THIS_MODULE, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 111 | .set_mdc = mdc_set, |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 112 | .set_mdio_dir = mdio_dir, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 113 | .set_mdio_data = mdio_set, |
| 114 | .get_mdio_data = mdio_get, |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 115 | }; |
| 116 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 117 | static struct mii_bus *mdio_gpio_bus_init(struct device *dev, |
Greg Kroah-Hartman | 1dd06ae | 2012-12-06 14:30:56 +0000 | [diff] [blame] | 118 | struct mdio_gpio_platform_data *pdata, |
| 119 | int bus_id) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 120 | { |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 121 | struct mii_bus *new_bus; |
| 122 | struct mdio_gpio_info *bitbang; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 123 | |
Guenter Roeck | 78cdb07 | 2014-04-15 19:16:40 -0700 | [diff] [blame] | 124 | bitbang = devm_kzalloc(dev, sizeof(*bitbang), GFP_KERNEL); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 125 | if (!bitbang) |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 126 | return NULL; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 127 | |
| 128 | bitbang->ctrl.ops = &mdio_gpio_ops; |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 129 | bitbang->mdc = pdata->mdc; |
| 130 | bitbang->mdio = pdata->mdio; |
| 131 | bitbang->mdo = pdata->mdo; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 132 | |
| 133 | new_bus = alloc_mdio_bitbang(&bitbang->ctrl); |
| 134 | if (!new_bus) |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 135 | return NULL; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 136 | |
Andrew Lunn | 712e5a5c | 2018-04-19 01:02:49 +0200 | [diff] [blame] | 137 | new_bus->name = "GPIO Bitbanged MDIO"; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 138 | new_bus->parent = dev; |
| 139 | |
Bert Vermeulen | 7c0c826 | 2015-05-08 16:18:49 +0200 | [diff] [blame] | 140 | if (bus_id != -1) |
| 141 | snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id); |
| 142 | else |
| 143 | strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 144 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 145 | dev_set_drvdata(dev, new_bus); |
| 146 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 147 | return new_bus; |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Stephen Rothwell | f99b4a0 | 2009-11-16 22:47:33 +0000 | [diff] [blame] | 150 | static void mdio_gpio_bus_deinit(struct device *dev) |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 151 | { |
| 152 | struct mii_bus *bus = dev_get_drvdata(dev); |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 153 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 154 | free_mdio_bitbang(bus); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 157 | static void mdio_gpio_bus_destroy(struct device *dev) |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 158 | { |
| 159 | struct mii_bus *bus = dev_get_drvdata(dev); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 160 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 161 | mdiobus_unregister(bus); |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 162 | mdio_gpio_bus_deinit(dev); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 163 | } |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 164 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 165 | static int mdio_gpio_probe(struct platform_device *pdev) |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 166 | { |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 167 | struct mdio_gpio_platform_data *pdata; |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 168 | struct mii_bus *new_bus; |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 169 | int ret, bus_id; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 170 | |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 171 | if (pdev->dev.of_node) { |
Andrew Lunn | c82fc48 | 2018-04-19 01:02:55 +0200 | [diff] [blame^] | 172 | pdata = mdio_gpio_of_get_data(&pdev->dev); |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 173 | bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio"); |
Johan Hovold | 7f52da5 | 2014-05-08 10:09:21 +0200 | [diff] [blame] | 174 | if (bus_id < 0) { |
| 175 | dev_warn(&pdev->dev, "failed to get alias id\n"); |
| 176 | bus_id = 0; |
| 177 | } |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 178 | } else { |
Jingoo Han | 9bc7b1c | 2013-08-30 14:08:55 +0900 | [diff] [blame] | 179 | pdata = dev_get_platdata(&pdev->dev); |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 180 | bus_id = pdev->id; |
| 181 | } |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 182 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 183 | if (!pdata) |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 184 | return -ENODEV; |
| 185 | |
Srinivas Kandagatla | 3272dd9 | 2012-11-16 00:33:59 +0000 | [diff] [blame] | 186 | new_bus = mdio_gpio_bus_init(&pdev->dev, pdata, bus_id); |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 187 | if (!new_bus) |
| 188 | return -ENODEV; |
| 189 | |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 190 | if (pdev->dev.of_node) |
| 191 | ret = of_mdiobus_register(new_bus, pdev->dev.of_node); |
| 192 | else |
| 193 | ret = mdiobus_register(new_bus); |
| 194 | |
Mark Ware | dacac4d | 2009-07-23 10:56:48 -0700 | [diff] [blame] | 195 | if (ret) |
| 196 | mdio_gpio_bus_deinit(&pdev->dev); |
| 197 | |
| 198 | return ret; |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 201 | static int mdio_gpio_remove(struct platform_device *pdev) |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 202 | { |
| 203 | mdio_gpio_bus_destroy(&pdev->dev); |
| 204 | |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 205 | return 0; |
| 206 | } |
| 207 | |
Fabian Frederick | d8a7dad | 2015-03-17 19:40:23 +0100 | [diff] [blame] | 208 | static const struct of_device_id mdio_gpio_of_match[] = { |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 209 | { .compatible = "virtual,mdio-gpio", }, |
| 210 | { /* sentinel */ } |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 211 | }; |
Luis de Bethencourt | 1ccb141 | 2015-09-18 18:16:29 +0200 | [diff] [blame] | 212 | MODULE_DEVICE_TABLE(of, mdio_gpio_of_match); |
Laurent Pinchart | a5edecc | 2008-05-26 11:53:21 +0200 | [diff] [blame] | 213 | |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 214 | static struct platform_driver mdio_gpio_driver = { |
| 215 | .probe = mdio_gpio_probe, |
Bill Pemberton | 633d159 | 2012-12-03 09:24:14 -0500 | [diff] [blame] | 216 | .remove = mdio_gpio_remove, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 217 | .driver = { |
| 218 | .name = "mdio-gpio", |
Srinivas Kandagatla | e92bdf4b | 2012-08-24 01:59:17 +0000 | [diff] [blame] | 219 | .of_match_table = mdio_gpio_of_match, |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 220 | }, |
| 221 | }; |
| 222 | |
Sachin Kamat | f8e5fc8 | 2013-03-20 01:41:31 +0000 | [diff] [blame] | 223 | module_platform_driver(mdio_gpio_driver); |
Paulius Zaleckas | f004f3e | 2008-11-14 00:24:34 +0000 | [diff] [blame] | 224 | |
| 225 | MODULE_ALIAS("platform:mdio-gpio"); |
| 226 | MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas"); |
| 227 | MODULE_LICENSE("GPL"); |
| 228 | MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO"); |