blob: 1a87a585e74df9abac74d5a60c8715917b179ff9 [file] [log] [blame]
David Daney416912a2012-05-02 15:16:39 +00001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
6 * Copyright (C) 2011, 2012 Cavium, Inc.
7 */
8
9#include <linux/platform_device.h>
10#include <linux/device.h>
11#include <linux/of_mdio.h>
12#include <linux/module.h>
David Daney416912a2012-05-02 15:16:39 +000013#include <linux/phy.h>
14#include <linux/mdio-mux.h>
15#include <linux/of_gpio.h>
16
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010017#define DRV_VERSION "1.1"
David Daney416912a2012-05-02 15:16:39 +000018#define DRV_DESCRIPTION "GPIO controlled MDIO bus multiplexer driver"
19
20#define MDIO_MUX_GPIO_MAX_BITS 8
21
22struct mdio_mux_gpio_state {
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010023 struct gpio_desc *gpio[MDIO_MUX_GPIO_MAX_BITS];
David Daney416912a2012-05-02 15:16:39 +000024 unsigned int num_gpios;
25 void *mux_handle;
26};
27
28static int mdio_mux_gpio_switch_fn(int current_child, int desired_child,
29 void *data)
30{
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010031 int values[MDIO_MUX_GPIO_MAX_BITS];
David Daney416912a2012-05-02 15:16:39 +000032 unsigned int n;
33 struct mdio_mux_gpio_state *s = data;
34
35 if (current_child == desired_child)
36 return 0;
37
David Daney416912a2012-05-02 15:16:39 +000038 for (n = 0; n < s->num_gpios; n++) {
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010039 values[n] = (desired_child >> n) & 1;
David Daney416912a2012-05-02 15:16:39 +000040 }
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010041 gpiod_set_array_cansleep(s->num_gpios, s->gpio, values);
David Daney416912a2012-05-02 15:16:39 +000042
43 return 0;
44}
45
Bill Pemberton633d1592012-12-03 09:24:14 -050046static int mdio_mux_gpio_probe(struct platform_device *pdev)
David Daney416912a2012-05-02 15:16:39 +000047{
David Daney416912a2012-05-02 15:16:39 +000048 struct mdio_mux_gpio_state *s;
Grant Likelye80beb22013-02-12 17:48:37 +000049 int num_gpios;
David Daney416912a2012-05-02 15:16:39 +000050 unsigned int n;
51 int r;
52
53 if (!pdev->dev.of_node)
54 return -ENODEV;
55
56 num_gpios = of_gpio_count(pdev->dev.of_node);
Grant Likelye80beb22013-02-12 17:48:37 +000057 if (num_gpios <= 0 || num_gpios > MDIO_MUX_GPIO_MAX_BITS)
David Daney416912a2012-05-02 15:16:39 +000058 return -ENODEV;
59
60 s = devm_kzalloc(&pdev->dev, sizeof(*s), GFP_KERNEL);
61 if (!s)
62 return -ENOMEM;
63
64 s->num_gpios = num_gpios;
65
66 for (n = 0; n < num_gpios; ) {
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010067 struct gpio_desc *gpio = gpiod_get_index(&pdev->dev, NULL, n,
68 GPIOD_OUT_LOW);
69 if (IS_ERR(gpio)) {
70 r = PTR_ERR(gpio);
David Daney416912a2012-05-02 15:16:39 +000071 goto err;
72 }
73 s->gpio[n] = gpio;
David Daney416912a2012-05-02 15:16:39 +000074 n++;
David Daney416912a2012-05-02 15:16:39 +000075 }
76
77 r = mdio_mux_init(&pdev->dev,
78 mdio_mux_gpio_switch_fn, &s->mux_handle, s);
79
80 if (r == 0) {
81 pdev->dev.platform_data = s;
82 return 0;
83 }
84err:
85 while (n) {
86 n--;
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010087 gpiod_put(s->gpio[n]);
David Daney416912a2012-05-02 15:16:39 +000088 }
David Daney416912a2012-05-02 15:16:39 +000089 return r;
90}
91
Bill Pemberton633d1592012-12-03 09:24:14 -050092static int mdio_mux_gpio_remove(struct platform_device *pdev)
David Daney416912a2012-05-02 15:16:39 +000093{
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010094 unsigned int n;
Jingoo Han1b0e53a2013-08-30 14:09:26 +090095 struct mdio_mux_gpio_state *s = dev_get_platdata(&pdev->dev);
David Daney416912a2012-05-02 15:16:39 +000096 mdio_mux_uninit(s->mux_handle);
Rojhalat Ibrahim441e0022014-11-25 10:31:18 +010097 for (n = 0; n < s->num_gpios; n++)
98 gpiod_put(s->gpio[n]);
David Daney416912a2012-05-02 15:16:39 +000099 return 0;
100}
101
Fabian Frederickd8a7dad2015-03-17 19:40:23 +0100102static const struct of_device_id mdio_mux_gpio_match[] = {
David Daney416912a2012-05-02 15:16:39 +0000103 {
104 .compatible = "mdio-mux-gpio",
105 },
106 {
107 /* Legacy compatible property. */
108 .compatible = "cavium,mdio-mux-sn74cbtlv3253",
109 },
110 {},
111};
112MODULE_DEVICE_TABLE(of, mdio_mux_gpio_match);
113
114static struct platform_driver mdio_mux_gpio_driver = {
115 .driver = {
116 .name = "mdio-mux-gpio",
David Daney416912a2012-05-02 15:16:39 +0000117 .of_match_table = mdio_mux_gpio_match,
118 },
119 .probe = mdio_mux_gpio_probe,
Bill Pemberton633d1592012-12-03 09:24:14 -0500120 .remove = mdio_mux_gpio_remove,
David Daney416912a2012-05-02 15:16:39 +0000121};
122
123module_platform_driver(mdio_mux_gpio_driver);
124
125MODULE_DESCRIPTION(DRV_DESCRIPTION);
126MODULE_VERSION(DRV_VERSION);
127MODULE_AUTHOR("David Daney");
128MODULE_LICENSE("GPL");