^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * GPIO based MDIO bitbang driver.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Supports OpenFirmware.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (c) 2008 CSE Semaphore Belgium.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * by Laurent Pinchart <laurentp@cse-semaphore.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 2008, Paulius Zaleckas <paulius.zaleckas@teltonika.lt>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Based on earlier work by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * Copyright (c) 2003 Intracom S.A.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * by Pantelis Antoniou <panto@intracom.gr>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * 2005 (c) MontaVista Software, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Vitaly Bordug <vbordug@ru.mvista.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/platform_data/mdio-gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/mdio-bitbang.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/mdio-gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/of_mdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct mdio_gpio_info {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct mdiobb_ctrl ctrl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) struct gpio_desc *mdc, *mdio, *mdo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static int mdio_gpio_get_data(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct mdio_gpio_info *bitbang)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) bitbang->mdc = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (IS_ERR(bitbang->mdc))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return PTR_ERR(bitbang->mdc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) bitbang->mdio = devm_gpiod_get_index(dev, NULL, MDIO_GPIO_MDIO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) if (IS_ERR(bitbang->mdio))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return PTR_ERR(bitbang->mdio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) bitbang->mdo = devm_gpiod_get_index_optional(dev, NULL, MDIO_GPIO_MDO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) GPIOD_OUT_LOW);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return PTR_ERR_OR_ZERO(bitbang->mdo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static void mdio_dir(struct mdiobb_ctrl *ctrl, int dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct mdio_gpio_info *bitbang =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) container_of(ctrl, struct mdio_gpio_info, ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) if (bitbang->mdo) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) /* Separate output pin. Always set its value to high
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * when changing direction. If direction is input,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * assume the pin serves as pull-up. If direction is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * output, the default value is high.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) gpiod_set_value_cansleep(bitbang->mdo, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) gpiod_direction_output(bitbang->mdio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) gpiod_direction_input(bitbang->mdio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) static int mdio_get(struct mdiobb_ctrl *ctrl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) struct mdio_gpio_info *bitbang =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) container_of(ctrl, struct mdio_gpio_info, ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) return gpiod_get_value_cansleep(bitbang->mdio);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) static void mdio_set(struct mdiobb_ctrl *ctrl, int what)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) struct mdio_gpio_info *bitbang =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) container_of(ctrl, struct mdio_gpio_info, ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) if (bitbang->mdo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) gpiod_set_value_cansleep(bitbang->mdo, what);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) gpiod_set_value_cansleep(bitbang->mdio, what);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static void mdc_set(struct mdiobb_ctrl *ctrl, int what)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct mdio_gpio_info *bitbang =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) container_of(ctrl, struct mdio_gpio_info, ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) gpiod_set_value_cansleep(bitbang->mdc, what);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static const struct mdiobb_ops mdio_gpio_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) .owner = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) .set_mdc = mdc_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .set_mdio_dir = mdio_dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .set_mdio_data = mdio_set,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .get_mdio_data = mdio_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static struct mii_bus *mdio_gpio_bus_init(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct mdio_gpio_info *bitbang,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) int bus_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct mdio_gpio_platform_data *pdata = dev_get_platdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct mii_bus *new_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) bitbang->ctrl.ops = &mdio_gpio_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) new_bus = alloc_mdio_bitbang(&bitbang->ctrl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (!new_bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) new_bus->name = "GPIO Bitbanged MDIO";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) new_bus->parent = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (bus_id != -1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) snprintf(new_bus->id, MII_BUS_ID_SIZE, "gpio-%x", bus_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) strncpy(new_bus->id, "gpio", MII_BUS_ID_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) if (pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) new_bus->phy_mask = pdata->phy_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) new_bus->phy_ignore_ta_mask = pdata->phy_ignore_ta_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) dev_set_drvdata(dev, new_bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) return new_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static void mdio_gpio_bus_deinit(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct mii_bus *bus = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) free_mdio_bitbang(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) static void mdio_gpio_bus_destroy(struct device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) struct mii_bus *bus = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) mdiobus_unregister(bus);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) mdio_gpio_bus_deinit(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) static int mdio_gpio_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) struct mdio_gpio_info *bitbang;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct mii_bus *new_bus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) int ret, bus_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) bitbang = devm_kzalloc(&pdev->dev, sizeof(*bitbang), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (!bitbang)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) ret = mdio_gpio_get_data(&pdev->dev, bitbang);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (pdev->dev.of_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) bus_id = of_alias_get_id(pdev->dev.of_node, "mdio-gpio");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (bus_id < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) dev_warn(&pdev->dev, "failed to get alias id\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) bus_id = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) bus_id = pdev->id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) new_bus = mdio_gpio_bus_init(&pdev->dev, bitbang, bus_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) if (!new_bus)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) ret = of_mdiobus_register(new_bus, pdev->dev.of_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) mdio_gpio_bus_deinit(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) static int mdio_gpio_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) mdio_gpio_bus_destroy(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static const struct of_device_id mdio_gpio_of_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) { .compatible = "virtual,mdio-gpio", },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) { /* sentinel */ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) MODULE_DEVICE_TABLE(of, mdio_gpio_of_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) static struct platform_driver mdio_gpio_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) .probe = mdio_gpio_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) .remove = mdio_gpio_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) .name = "mdio-gpio",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) .of_match_table = mdio_gpio_of_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) module_platform_driver(mdio_gpio_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) MODULE_ALIAS("platform:mdio-gpio");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) MODULE_AUTHOR("Laurent Pinchart, Paulius Zaleckas");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) MODULE_DESCRIPTION("Generic driver for MDIO bus emulation using GPIO");