From: Andrey Smirnov <andrew.smirnov@gmail.com>
To: barebox@lists.infradead.org
Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: [PATCH v2 4/6] net: phy: Port GPIO controlled MDIO multiplexer driver
Date: Mon, 4 Dec 2017 07:27:16 -0800 [thread overview]
Message-ID: <20171204152718.10674-5-andrew.smirnov@gmail.com> (raw)
In-Reply-To: <20171204152718.10674-1-andrew.smirnov@gmail.com>
Port Linux version driver to Barebox, to support such device found in
various revisions of ZII's VF610 development board.
Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com>
---
drivers/net/phy/Kconfig | 10 +++
drivers/net/phy/Makefile | 1 +
drivers/net/phy/mdio-mux-gpio.c | 147 ++++++++++++++++++++++++++++++++++++++++
3 files changed, 158 insertions(+)
create mode 100644 drivers/net/phy/mdio-mux-gpio.c
diff --git a/drivers/net/phy/Kconfig b/drivers/net/phy/Kconfig
index 08ffd2d45..cda752b65 100644
--- a/drivers/net/phy/Kconfig
+++ b/drivers/net/phy/Kconfig
@@ -74,6 +74,16 @@ config MDIO_BUS_MUX
to a parent bus. Switching between child busses is done by
device specific drivers.
+config MDIO_BUS_MUX_GPIO
+ bool "GPIO controlled MDIO bus multiplexers"
+ depends on OF_GPIO
+ select MDIO_BUS_MUX
+ help
+ This module provides a driver for MDIO bus multiplexers that
+ are controlled via GPIO lines. The multiplexer connects one of
+ several child MDIO busses to a parent bus. Child bus
+ selection is under the control of GPIO lines.
+
endif
endmenu
diff --git a/drivers/net/phy/Makefile b/drivers/net/phy/Makefile
index 89b4b80be..30b20f8ee 100644
--- a/drivers/net/phy/Makefile
+++ b/drivers/net/phy/Makefile
@@ -11,4 +11,5 @@ obj-$(CONFIG_MDIO_MVEBU) += mdio-mvebu.o
obj-$(CONFIG_MDIO_BITBANG) += mdio-bitbang.o
obj-$(CONFIG_MDIO_GPIO) += mdio-gpio.o
obj-$(CONFIG_MDIO_BUS_MUX) += mdio-mux.o
+obj-$(CONFIG_MDIO_BUS_MUX_GPIO) += mdio-mux-gpio.o
diff --git a/drivers/net/phy/mdio-mux-gpio.c b/drivers/net/phy/mdio-mux-gpio.c
new file mode 100644
index 000000000..221353cbc
--- /dev/null
+++ b/drivers/net/phy/mdio-mux-gpio.c
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2017 Zodiac Inflight Innovation
+ * Author: Andrey Smirnov <andrew.smirnov@gmail.com>
+ *
+ * Based on analogous code from Linux kernel
+ *
+ * Copyright (C) 2011, 2012 Cavium, Inc.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ */
+#include <common.h>
+#include <driver.h>
+#include <init.h>
+#include <linux/mdio-mux.h>
+#include <gpio.h>
+#include <of_gpio.h>
+
+
+struct mdio_mux_gpio_state {
+ struct gpio *gpios;
+ int gpios_num;
+};
+
+static void mdio_mux_gpio_set_active(struct mdio_mux_gpio_state *s,
+ unsigned int mask, int value)
+{
+ while (mask) {
+ const int n = __ffs(mask);
+ mask >>= n + 1;
+
+ if (WARN_ON(n >= s->gpios_num))
+ break;
+
+ gpio_set_active(s->gpios[n].gpio, value);
+ }
+}
+
+static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,
+ void *data)
+{
+ if (current_child < 0)
+ current_child = 0;
+
+ if (current_child != desired_child) {
+ struct mdio_mux_gpio_state *s = data;
+ /*
+ * GPIOs that are set in both current and desired
+ * states can be left untouched. So we calculate them
+ * and clear corresponding bits in both states.
+ */
+ const int unchanged = current_child & desired_child;
+
+ current_child &= ~unchanged;
+ desired_child &= ~unchanged;
+
+ /*
+ * First step: disable all of the currently selected
+ * bits that are no loger needed
+ */
+ mdio_mux_gpio_set_active(s, current_child, 0);
+ /*
+ * Second step: enable all of the GPIOs that are
+ * desired to be selected
+ */
+ mdio_mux_gpio_set_active(s, desired_child, 1);
+ }
+
+ return 0;
+}
+
+static int mdio_mux_gpio_probe(struct device_d *dev)
+{
+ struct mdio_mux_gpio_state *s;
+ int i, r;
+
+ s = xzalloc(sizeof(*s));
+
+ s->gpios_num = of_gpio_count(dev->device_node);
+ if (s->gpios_num <= 0) {
+ dev_err(dev, "No GPIOs specified\n");
+ r = -EINVAL;
+ goto free_mem;
+ }
+
+ s->gpios = xzalloc(s->gpios_num * sizeof(s->gpios[0]));
+
+ for (i = 0; i < s->gpios_num; i++) {
+ enum of_gpio_flags flags;
+
+ r = of_get_gpio_flags(dev->device_node, i, &flags);
+ if (!gpio_is_valid(r)) {
+ r = (r < 0) ? r : -EINVAL;
+ goto free_mem;
+ }
+
+ s->gpios[i].gpio = r;
+ s->gpios[i].label = dev_name(dev);
+ s->gpios[i].flags = GPIOF_OUT_INIT_INACTIVE;
+
+ if (flags & OF_GPIO_ACTIVE_LOW)
+ s->gpios[i].flags |= GPIOF_ACTIVE_LOW;
+ }
+
+ r = gpio_request_array(s->gpios, s->gpios_num);
+ if (r < 0)
+ goto free_gpios;
+
+
+ r = mdio_mux_init(dev, dev->device_node,
+ mdio_mux_gpio_switch_fn, s, NULL);
+ if (r < 0)
+ goto free_gpios;
+
+ return 0;
+
+free_gpios:
+ gpio_free_array(s->gpios, s->gpios_num);
+free_mem:
+ free(s->gpios);
+ free(s);
+ return r;
+}
+
+static const struct of_device_id mdio_mux_gpio_match[] = {
+ {
+ .compatible = "mdio-mux-gpio",
+ },
+ {},
+};
+
+static struct driver_d mdio_mux_gpio_driver = {
+ .name = "mdio-mux-gpio",
+ .probe = mdio_mux_gpio_probe,
+ .of_compatible = mdio_mux_gpio_match,
+};
+device_platform_driver(mdio_mux_gpio_driver);
--
2.14.3
_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox
next prev parent reply other threads:[~2017-12-04 15:28 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-12-04 15:27 [PATCH v2 0/6] MDIO bus multiplexer support Andrey Smirnov
2017-12-04 15:27 ` [PATCH v2 1/6] ARM: vybrid: zii: Make use of DT code from Linux kernel Andrey Smirnov
2017-12-04 15:27 ` [PATCH v2 2/6] mdio_bus: Port of_mdio_find_bus() " Andrey Smirnov
2017-12-04 15:27 ` [PATCH v2 3/6] net: phy: Port MDIO bus miltiplexer framework " Andrey Smirnov
2017-12-04 15:27 ` Andrey Smirnov [this message]
2017-12-04 15:27 ` [PATCH v2 5/6] net: mdio-mux: Avoid probing multiplexed busses Andrey Smirnov
2017-12-04 15:27 ` [PATCH v2 6/6] zii_vf610_dev_defconfig: Select MDIO bus GPIO multiplexer Andrey Smirnov
2017-12-06 14:49 ` [PATCH v2 0/6] MDIO bus multiplexer support Sascha Hauer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20171204152718.10674-5-andrew.smirnov@gmail.com \
--to=andrew.smirnov@gmail.com \
--cc=barebox@lists.infradead.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox